diff --git a/qse/doc/page/awk-lang.md b/qse/doc/page/awk-lang.md index 13ac119d..de5200ca 100644 --- a/qse/doc/page/awk-lang.md +++ b/qse/doc/page/awk-lang.md @@ -1491,6 +1491,55 @@ BEGIN { } ~~~~~ +~~~~~{.awk} +BEGIN { + if (sys::pipe(p0, p1, sys::O_NONBLOCK | sys::O_CLOEXEC) <= -1) + { + print "pipe error"; + return -1; + } + a = sys::fork(); + if (a <= -1) + { + print "fork error"; + sys::close (p0); + sys::close (p1); + } + else if (a == 0) + { + ## child + sys::close (p0); + + stdout = sys::openfd(1); + sys::dup(p1, stdout); + + print B"hello world"; + print B"testing sys::dup()"; + print B"writing to standard output.."; + + sys::close (p1); + sys::close (stdout); + } + else + { + sys::close (p1); + while (1) + { + n = sys::read(p0, k, 10); + if (n <= 0) + { + if (n == sys::RC_EAGAIN) continue; ## nonblock but data not available + if (n != 0) print "ERROR: " sys::errmsg(); + break; + } + print "[" k "]"; + } + sys::close (p0); + sys::wait(a); + } +} +~~~~~ + ~~~~~{.awk} BEGIN { d = sys::opendir("/etc", sys::DIR_SORT); diff --git a/qse/lib/awk/imap-imp.h b/qse/lib/awk/imap-imp.h index 033fa6f7..be579ca9 100644 --- a/qse/lib/awk/imap-imp.h +++ b/qse/lib/awk/imap-imp.h @@ -61,16 +61,16 @@ static __IMAP_NODE_T* __MAKE_IMAP_NODE (qse_awk_rtx_t* rtx, __IMAP_LIST_T* list) node = QSE_NULL; - if (list->free) node = list->free; + if (list->free) + { + node = list->free; + list->free = node->next; + } else { node = qse_awk_rtx_callocmem(rtx, QSE_SIZEOF(*node)); if (!node) goto oops; - } - - if (node == list->free) list->free = node->next; - else - { + if (list->map.high <= list->map.capa) { qse_size_t newcapa, inc; @@ -92,11 +92,12 @@ static __IMAP_NODE_T* __MAKE_IMAP_NODE (qse_awk_rtx_t* rtx, __IMAP_LIST_T* list) } node->id = list->map.high; - QSE_ASSERT (list->map.tab[node->id] == QSE_NULL); - list->map.tab[node->id] = node; list->map.high++; } + QSE_ASSERT (list->map.tab[node->id] == QSE_NULL); + list->map.tab[node->id] = node; + /* append it to the tail */ node->next = QSE_NULL; node->prev = list->tail; diff --git a/qse/lib/awk/mod-str.c b/qse/lib/awk/mod-str.c index 068454e5..58375551 100644 --- a/qse/lib/awk/mod-str.c +++ b/qse/lib/awk/mod-str.c @@ -30,6 +30,7 @@ #include #include "../cmn/mem-prv.h" #include "fnc.h" +#include "val.h" static int fnc_normspace (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) { diff --git a/qse/lib/awk/mod-sys.c b/qse/lib/awk/mod-sys.c index a5327af2..c2cfb92e 100644 --- a/qse/lib/awk/mod-sys.c +++ b/qse/lib/awk/mod-sys.c @@ -484,7 +484,7 @@ static int fnc_openfd (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi) rx = awkerr_to_rc(qse_awk_rtx_geterrnum(rtx)); set_errmsg_on_sys_list (rtx, sys_list, QSE_NULL); } - else if (fd >= 0) + else if (fd >= 0 && fd <= QSE_TYPE_MAX(int)) { sys_node_t* sys_node;