diff --git a/ase/awk/awk.h b/ase/awk/awk.h index 49f1332f..e7d992fe 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.67 2006-06-20 15:27:50 bacon Exp $ + * $Id: awk.h,v 1.68 2006-06-21 11:44:55 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -158,6 +158,7 @@ int xp_awk_run (xp_awk_t* awk, xp_awk_io_t txtio, void* txtio_arg); xp_size_t xp_awk_getnargs (void* run); xp_awk_val_t* xp_awk_getarg (void* run, xp_size_t idx); void xp_awk_setretval (void* run, xp_awk_val_t* val); +void xp_awk_seterrnum (void* run, int errnum); /* utility functions exported by awk.h */ xp_long_t xp_awk_strtolong ( diff --git a/ase/awk/awk_i.h b/ase/awk/awk_i.h index 093cd6ec..97c45c2e 100644 --- a/ase/awk/awk_i.h +++ b/ase/awk/awk_i.h @@ -1,5 +1,5 @@ /* - * $Id: awk_i.h,v 1.19 2006-06-20 15:27:50 bacon Exp $ + * $Id: awk_i.h,v 1.20 2006-06-21 11:44:55 bacon Exp $ */ #ifndef _XP_AWK_AWKI_H_ @@ -10,12 +10,6 @@ typedef struct xp_awk_run_t xp_awk_run_t; typedef struct xp_awk_tree_t xp_awk_tree_t; #include -#include -#include -#include -#include -#include -#include #ifdef XP_AWK_STAND_ALONE #include @@ -23,6 +17,14 @@ typedef struct xp_awk_tree_t xp_awk_tree_t; #include #endif +#include +#include +#include +#include +#include +#include +#include + #if defined(_WIN32) && defined(XP_AWK_STAND_ALONE) && defined(_DEBUG) #define _CRTDBG_MAP_ALLOC #include diff --git a/ase/awk/extio.h b/ase/awk/extio.h new file mode 100644 index 00000000..01fdb3ac --- /dev/null +++ b/ase/awk/extio.h @@ -0,0 +1,25 @@ +#ifndef _XP_AWK_EXTIO_H_ +#define _XP_AWK_EXTIO_H_ + +#ifndef _XP_AWK_AWK_H_ +#error Never include this file directly. Include instead +#endif + + +#ifdef __cplusplus +extern "C" +#endif + +int xp_awk_readextio (xp_awk_run_t* run, + xp_awk_extio_t** extio, xp_awk_io_t handler, + const xp_char_t* name, int* errnum); + +int xp_awk_closeextio (xp_awk_run_t* run, + xp_awk_extio_t** extio, xp_awk_io_t handler, + const xp_char_t* name, int* errnum); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ase/awk/func.c b/ase/awk/func.c index cae53c1d..ac56eaa6 100644 --- a/ase/awk/func.c +++ b/ase/awk/func.c @@ -1,5 +1,5 @@ /* - * $Id: func.c,v 1.2 2006-06-20 15:27:50 bacon Exp $ + * $Id: func.c,v 1.3 2006-06-21 11:44:55 bacon Exp $ */ #include @@ -7,6 +7,7 @@ #ifndef XP_AWK_STAND_ALONE #include #include +#include #endif static int __bfn_close (void* run); @@ -33,16 +34,48 @@ xp_awk_bfn_t* xp_awk_getbfn (const xp_char_t* name) static int __bfn_close (void* run) { - xp_size_t nargs, i; + xp_size_t nargs; + xp_str_t buf; + xp_awk_val_t* v; + int errnum, n; nargs = xp_awk_getnargs (run); - for (i = 0; i < nargs; i++) + xp_assert (nargs == 1); + + if (xp_str_open (&buf, 256) == XP_NULL) { - xp_printf (XP_T("arg %d => "), (int)i); - xp_awk_printval (xp_awk_getarg (run, i)); - xp_printf (XP_T("\n")); + xp_awk_seterrnum (run, XP_AWK_ENOMEM); + return -1; } - xp_awk_setretval (run, xp_awk_makeintval(run,10)); + if (xp_awk_valtostr (xp_awk_getarg(run, 0), &errnum, &buf) == XP_NULL) + { + xp_str_close (&buf); + xp_awk_seterrnum (run, errnum); + return -1; + } + + /* + n = xp_awk_closeextio (run, XP_STR_BUF(&buf), &errnum); + if (n == -1 && errnum != XP_AWK_ENOERR) + { + xp_str_close (&buf); + xp_awk_seterrnum (run, errnum); + return -1; + } + */ +n = -1; +xp_printf (XP_T("closing %s\n"), XP_STR_BUF(&buf)); + + xp_str_close (&buf); + + v = xp_awk_makeintval (run, n); + if (v == XP_NULL) + { + xp_awk_seterrnum (run, XP_AWK_ENOMEM); + return -1; + } + + xp_awk_setretval (run, v); return 0; } diff --git a/ase/awk/run.c b/ase/awk/run.c index d00ed454..76b996ea 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.101 2006-06-20 15:27:50 bacon Exp $ + * $Id: run.c,v 1.102 2006-06-21 11:44:55 bacon Exp $ */ #include @@ -147,7 +147,6 @@ static void __raw_pop_times (xp_awk_run_t* run, xp_size_t times); static int __read_text_input (xp_awk_run_t* run); static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r); -static xp_char_t* __val_to_str (xp_awk_val_t* v, int* errnum, xp_str_t* buf); static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde); typedef xp_awk_val_t* (*binop_func_t) ( @@ -182,6 +181,12 @@ void xp_awk_setretval (void* run, xp_awk_val_t* val) xp_awk_refupval (val); } +void xp_awk_seterrnum (void* run, int errnum) +{ + xp_awk_run_t* r = (xp_awk_run_t*)run; + r->errnum = errnum; +} + int xp_awk_run (xp_awk_t* awk, xp_awk_io_t txtio, void* txtio_arg) { xp_awk_run_t* run; @@ -545,7 +550,7 @@ static int __run_pattern_block_chain (xp_awk_run_t* run, xp_awk_chain_t* chain) if (ptn->next == XP_NULL) { - if (xp_awk_boolval(v1)) + if (xp_awk_valtobool(v1)) { if (__run_block (run, (xp_awk_nde_blk_t*)chain->action) == -1) { @@ -749,7 +754,7 @@ static int __run_if_statement (xp_awk_run_t* run, xp_awk_nde_if_t* nde) if (test == XP_NULL) return -1; xp_awk_refupval (test); - if (xp_awk_boolval(test)) + if (xp_awk_valtobool(test)) { n = __run_statement (run, nde->then_part); } @@ -780,7 +785,7 @@ static int __run_while_statement (xp_awk_run_t* run, xp_awk_nde_while_t* nde) xp_awk_refupval (test); - if (xp_awk_boolval(test)) + if (xp_awk_valtobool(test)) { if (__run_statement(run,nde->body) == -1) { @@ -835,7 +840,7 @@ static int __run_while_statement (xp_awk_run_t* run, xp_awk_nde_while_t* nde) xp_awk_refupval (test); - if (!xp_awk_boolval(test)) + if (!xp_awk_valtobool(test)) { xp_awk_refdownval (run, test); break; @@ -877,7 +882,7 @@ static int __run_for_statement (xp_awk_run_t* run, xp_awk_nde_for_t* nde) if (test == XP_NULL) return -1; xp_awk_refupval (test); - if (xp_awk_boolval(test)) + if (xp_awk_valtobool(test)) { if (__run_statement(run,nde->body) == -1) { @@ -1448,7 +1453,7 @@ static xp_awk_val_t* __eval_binop_lor ( xp_awk_val_t* res = XP_NULL; res = xp_awk_makeintval (run, - xp_awk_boolval(left) || xp_awk_boolval(right)); + xp_awk_valtobool(left) || xp_awk_valtobool(right)); if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM); return res; @@ -1462,7 +1467,7 @@ static xp_awk_val_t* __eval_binop_lor ( if (lv == XP_NULL) return XP_NULL; xp_awk_refupval (lv); - if (xp_awk_boolval(lv)) + if (xp_awk_valtobool(lv)) { res = xp_awk_makeintval (run, 1); } @@ -1477,7 +1482,7 @@ static xp_awk_val_t* __eval_binop_lor ( } xp_awk_refupval (rv); - res = xp_awk_makeintval (run, (xp_awk_boolval(rv)? 1: 0)); + res = xp_awk_makeintval (run, (xp_awk_valtobool(rv)? 1: 0)); xp_awk_refdownval (run, rv); } @@ -1492,7 +1497,7 @@ static xp_awk_val_t* __eval_binop_land ( xp_awk_val_t* res = XP_NULL; res = xp_awk_makeintval (run, - xp_awk_boolval(left) && xp_awk_boolval(right)); + xp_awk_valtobool(left) && xp_awk_valtobool(right)); if (res == XP_NULL) PANIC (run, XP_AWK_ENOMEM); return res; @@ -1506,7 +1511,7 @@ static xp_awk_val_t* __eval_binop_land ( if (lv == XP_NULL) return XP_NULL; xp_awk_refupval (lv); - if (!xp_awk_boolval(lv)) + if (!xp_awk_valtobool(lv)) { res = xp_awk_makeintval (run, 0); } @@ -1521,7 +1526,7 @@ static xp_awk_val_t* __eval_binop_land ( } xp_awk_refupval (rv); - res = xp_awk_makeintval (run, (xp_awk_boolval(rv)? 1: 0)); + res = xp_awk_makeintval (run, (xp_awk_valtobool(rv)? 1: 0)); xp_awk_refdownval (run, rv); } @@ -2544,7 +2549,7 @@ static xp_awk_val_t* __eval_cnd (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_assert (cnd->left->next == XP_NULL && cnd->right->next == XP_NULL); - v = (xp_awk_boolval(tv))? + v = (xp_awk_valtobool(tv))? __eval_expression (run, cnd->left): __eval_expression (run, cnd->right); @@ -2930,7 +2935,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde) if (in == XP_NULL) return XP_NULL; xp_awk_refupval (in); - str = __val_to_str (in, &errnum, XP_NULL); + str = xp_awk_valtostr (in, &errnum, XP_NULL); if (str == XP_NULL) { xp_awk_refdownval (run, in); @@ -2961,7 +2966,7 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde) if (in == XP_NULL) return XP_NULL; xp_awk_refupval (in); - str = __val_to_str (in, &errnum, XP_NULL); + str = xp_awk_valtostr (in, &errnum, XP_NULL); if (str == XP_NULL) { xp_awk_refdownval (run, in); @@ -3101,121 +3106,6 @@ static int __val_to_num (xp_awk_val_t* v, xp_long_t* l, xp_real_t* r) return -1; /* error */ } -static xp_char_t* __val_to_str (xp_awk_val_t* v, int* errnum, xp_str_t* buf) -{ - if (v->type == XP_AWK_VAL_INT) - { - xp_char_t* tmp; - xp_long_t t; - xp_size_t len = 0; - - t = ((xp_awk_val_int_t*)v)->val; - if (t == 0) - { - /* handle zero */ - if (buf == XP_NULL) - { - tmp = xp_malloc (2 * xp_sizeof(xp_char_t)); - if (tmp == XP_NULL) - { - *errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - tmp[0] = XP_T('0'); - tmp[1] = XP_T('\0'); - return tmp; - } - else - { - if (xp_str_cat (buf, XP_T("0")) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - return XP_STR_BUF(buf); - } - } - - /* non-zero values */ - if (t < 0) { t = -t; len++; } - while (t > 0) { len++; t /= 10; } - - if (buf == XP_NULL) - { - tmp = xp_malloc (len + 1 * xp_sizeof(xp_char_t)); - if (tmp == XP_NULL) - { - *errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - - tmp[len] = XP_T('\0'); - } - else - { - /* get the current end of the buffer */ - tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); - - /* extend the buffer */ - if (xp_str_nccat ( - buf, XP_T(' '), len) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - return XP_NULL; - } - } - - t = ((xp_awk_val_int_t*)v)->val; - if (t < 0) t = -t; - - while (t > 0) - { - tmp[--len] = (xp_char_t)(t % 10) + XP_T('0'); - t /= 10; - } - - if (((xp_awk_val_int_t*)v)->val < 0) tmp[--len] = XP_T('-'); - - /*return (buf == XP_NULL) tmp: XP_STR_BUF(buf);*/ - return tmp; - } - - if (v->type == XP_AWK_VAL_STR) - { - xp_char_t* tmp; - - if (buf == XP_NULL) - { - tmp = xp_strxdup ( - ((xp_awk_val_str_t*)v)->buf, - ((xp_awk_val_str_t*)v)->len); - - if (tmp == XP_NULL) *errnum = XP_AWK_ENOMEM; - } - else - { - tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); - - if (xp_str_ncat (buf, - ((xp_awk_val_str_t*)v)->buf, - ((xp_awk_val_str_t*)v)->len) == (xp_size_t)-1) - { - *errnum = XP_AWK_ENOMEM; - tmp = XP_NULL; - } - } - - return tmp; - } - -/* TODO: process more value types */ - - *errnum = XP_AWK_EWRONGINDEX; - return XP_NULL; -} - static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_char_t* str; @@ -3232,7 +3122,7 @@ static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) xp_awk_refupval (idx); - str = __val_to_str (idx, &errnum, XP_NULL); + str = xp_awk_valtostr (idx, &errnum, XP_NULL); if (str == XP_NULL) { xp_awk_refdownval (run, idx); @@ -3271,7 +3161,7 @@ static xp_char_t* __idxnde_to_str (xp_awk_run_t* run, xp_awk_nde_t* nde) PANIC (run, XP_AWK_ENOMEM); } - if (__val_to_str (idx, &errnum, &idxstr) == XP_NULL) + if (xp_awk_valtostr (idx, &errnum, &idxstr) == XP_NULL) { xp_awk_refdownval (run, idx); xp_str_close (&idxstr); diff --git a/ase/awk/val.c b/ase/awk/val.c index ea8c2d06..e760d63a 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.30 2006-05-13 16:33:07 bacon Exp $ + * $Id: val.c,v 1.31 2006-06-21 11:44:55 bacon Exp $ */ #include @@ -299,7 +299,7 @@ xp_awk_val_t* xp_awk_cloneval (xp_awk_run_t* run, xp_awk_val_t* val) return XP_NULL; } -xp_bool_t xp_awk_boolval (xp_awk_val_t* val) +xp_bool_t xp_awk_valtobool (xp_awk_val_t* val) { if (val == XP_NULL) return xp_false; @@ -321,6 +321,121 @@ xp_bool_t xp_awk_boolval (xp_awk_val_t* val) return xp_false; } +xp_char_t* xp_awk_valtostr (xp_awk_val_t* v, int* errnum, xp_str_t* buf) +{ + if (v->type == XP_AWK_VAL_INT) + { + xp_char_t* tmp; + xp_long_t t; + xp_size_t len = 0; + + t = ((xp_awk_val_int_t*)v)->val; + if (t == 0) + { + /* handle zero */ + if (buf == XP_NULL) + { + tmp = xp_malloc (2 * xp_sizeof(xp_char_t)); + if (tmp == XP_NULL) + { + *errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + tmp[0] = XP_T('0'); + tmp[1] = XP_T('\0'); + return tmp; + } + else + { + if (xp_str_cat (buf, XP_T("0")) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + return XP_STR_BUF(buf); + } + } + + /* non-zero values */ + if (t < 0) { t = -t; len++; } + while (t > 0) { len++; t /= 10; } + + if (buf == XP_NULL) + { + tmp = xp_malloc (len + 1 * xp_sizeof(xp_char_t)); + if (tmp == XP_NULL) + { + *errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + + tmp[len] = XP_T('\0'); + } + else + { + /* get the current end of the buffer */ + tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); + + /* extend the buffer */ + if (xp_str_nccat ( + buf, XP_T(' '), len) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + return XP_NULL; + } + } + + t = ((xp_awk_val_int_t*)v)->val; + if (t < 0) t = -t; + + while (t > 0) + { + tmp[--len] = (xp_char_t)(t % 10) + XP_T('0'); + t /= 10; + } + + if (((xp_awk_val_int_t*)v)->val < 0) tmp[--len] = XP_T('-'); + + /*return (buf == XP_NULL) tmp: XP_STR_BUF(buf);*/ + return tmp; + } + + if (v->type == XP_AWK_VAL_STR) + { + xp_char_t* tmp; + + if (buf == XP_NULL) + { + tmp = xp_strxdup ( + ((xp_awk_val_str_t*)v)->buf, + ((xp_awk_val_str_t*)v)->len); + + if (tmp == XP_NULL) *errnum = XP_AWK_ENOMEM; + } + else + { + tmp = XP_STR_BUF(buf) + XP_STR_LEN(buf); + + if (xp_str_ncat (buf, + ((xp_awk_val_str_t*)v)->buf, + ((xp_awk_val_str_t*)v)->len) == (xp_size_t)-1) + { + *errnum = XP_AWK_ENOMEM; + tmp = XP_NULL; + } + } + + return tmp; + } + +/* TODO: process more value types */ + + *errnum = XP_AWK_EWRONGINDEX; + return XP_NULL; +} + static int __print_pair (xp_awk_pair_t* pair, void* arg) { xp_printf (XP_T(" %s=>"), pair->key); diff --git a/ase/awk/val.h b/ase/awk/val.h index 17b2eeb1..f50aae22 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.23 2006-06-20 15:27:50 bacon Exp $ + * $Id: val.h,v 1.24 2006-06-21 11:44:55 bacon Exp $ */ #ifndef _XP_AWK_VAL_H_ @@ -110,7 +110,10 @@ void xp_awk_refdownval (xp_awk_run_t* run, xp_awk_val_t* val); void xp_awk_refdownval_nofree (xp_awk_run_t* run, xp_awk_val_t* val); xp_awk_val_t* xp_awk_cloneval (xp_awk_run_t* run, xp_awk_val_t* val); -xp_bool_t xp_awk_boolval (xp_awk_val_t* val); + +xp_bool_t xp_awk_valtobool (xp_awk_val_t* val); +xp_char_t* xp_awk_valtostr (xp_awk_val_t* val, int* errnum, xp_str_t* buf); + void xp_awk_printval (xp_awk_val_t* val); #ifdef __cplusplus diff --git a/ase/test/awk/t10.awk b/ase/test/awk/t10.awk new file mode 100644 index 00000000..28d7729a --- /dev/null +++ b/ase/test/awk/t10.awk @@ -0,0 +1,10 @@ +BEGIN +{ + getline x < "abc"; /* open("abc", O_RDONLY|O_LARGEFILE) = 3 */ + print 10 >> "abc"; /* open("abc", O_WRONLY|O_APPEND|O_CREAT|O_LARGEFILE, 0666) = 4 */ + getline x < "abc"; + print x; + close ("abc"); /* close(4) */ + print "hey" + close ("abc"); /* close(3) */ +} diff --git a/ase/test/awk/t9.awk b/ase/test/awk/t9.awk new file mode 100644 index 00000000..9f5d9f9a --- /dev/null +++ b/ase/test/awk/t9.awk @@ -0,0 +1,26 @@ +BEGIN +{ + while (("xxx /p" | getline var) > 0) ; + while (("dir /w" | getline var) > 0) ; + while ((getline var < "t9.awk") > 0) ; + + zzz = close ("xxx /p"); + /* + while ("ls -l" | getline var) + { + "ls -l" | getline x; + print var; print x; + } + + while (getline < "/etc/passwd") + { + print $0; + } + + while (getline x < "/etc/shadow") + { + print x; + } + */ +} + diff --git a/ase/test/awk/x.awk b/ase/test/awk/x.awk new file mode 100644 index 00000000..4fe9e866 --- /dev/null +++ b/ase/test/awk/x.awk @@ -0,0 +1,6 @@ +BEGIN +{ + print 1 + | + "test"; +}