diff --git a/ase/awk/awk_i.h b/ase/awk/awk_i.h index 810fc65e..03528582 100644 --- a/ase/awk/awk_i.h +++ b/ase/awk/awk_i.h @@ -1,5 +1,5 @@ /* - * $Id: awk_i.h,v 1.62 2006-10-03 14:38:26 bacon Exp $ + * $Id: awk_i.h,v 1.63 2006-10-11 15:01:54 bacon Exp $ */ #ifndef _XP_AWK_AWKI_H_ @@ -247,6 +247,11 @@ struct xp_awk_run_t void* fs; int ignorecase; + struct + { + xp_char_t* ptr; + xp_size_t len; + } convfmt; struct { xp_char_t* ptr; diff --git a/ase/awk/extio.c b/ase/awk/extio.c index d50f40d7..9ceb76e7 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.50 2006-10-09 14:37:14 bacon Exp $ + * $Id: extio.c,v 1.51 2006-10-11 15:01:55 bacon Exp $ */ #include @@ -183,7 +183,8 @@ int xp_awk_readextio ( } else { - rs_ptr = xp_awk_valtostr (run, rs, xp_true, XP_NULL, &rs_len); + rs_ptr = xp_awk_valtostr ( + run, rs, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &rs_len); if (rs_ptr == XP_NULL) { xp_awk_refdownval (run, rs); @@ -371,7 +372,8 @@ int xp_awk_writeextio_val ( } else { - str = xp_awk_valtostr (run, v, xp_true, XP_NULL, &len); + str = xp_awk_valtostr ( + run, v, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len); if (str == XP_NULL) return -1; } diff --git a/ase/awk/func.c b/ase/awk/func.c index b03831bf..d440aaaa 100644 --- a/ase/awk/func.c +++ b/ase/awk/func.c @@ -1,5 +1,5 @@ /* - * $Id: func.c,v 1.60 2006-10-11 03:18:28 bacon Exp $ + * $Id: func.c,v 1.61 2006-10-11 15:01:55 bacon Exp $ */ #include @@ -163,7 +163,8 @@ static int __bfn_close (xp_awk_run_t* run) } else { - name = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len); + name = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len); if (name == XP_NULL) return -1; } @@ -279,7 +280,7 @@ static int __bfn_fflush (xp_awk_run_t* run) else { str0 = xp_awk_valtostr ( - run, a0, xp_true, XP_NULL, &len0); + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len0); if (str0 == XP_NULL) return -1; } @@ -358,7 +359,8 @@ static int __bfn_index (xp_awk_run_t* run) } else { - str0 = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len0); + str0 = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len0); if (str0 == XP_NULL) return -1; } @@ -369,7 +371,8 @@ static int __bfn_index (xp_awk_run_t* run) } else { - str1 = xp_awk_valtostr (run, a1, xp_true, XP_NULL, &len1); + str1 = xp_awk_valtostr ( + run, a1, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len1); if (str1 == XP_NULL) { if (a0->type != XP_AWK_VAL_STR) @@ -414,7 +417,8 @@ static int __bfn_length (xp_awk_run_t* run) } else { - str = xp_awk_valtostr (run, v, xp_true, XP_NULL, &len); + str = xp_awk_valtostr ( + run, v, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len); if (str == XP_NULL) return -1; XP_AWK_FREE (run->awk, str); } @@ -454,7 +458,8 @@ static int __bfn_substr (xp_awk_run_t* run) } else { - str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len); + str = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len); if (str == XP_NULL) return -1; } @@ -555,7 +560,8 @@ static int __bfn_split (xp_awk_run_t* run) } else { - str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &str_len); + str = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &str_len); if (str == XP_NULL) return -1; str_free = str; } @@ -579,7 +585,7 @@ static int __bfn_split (xp_awk_run_t* run) else { fs_ptr = xp_awk_valtostr ( - run, t1, xp_true, XP_NULL, &fs_len); + run, t1, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &fs_len); if (fs_ptr == XP_NULL) { if (str_free != XP_NULL) @@ -606,7 +612,7 @@ static int __bfn_split (xp_awk_run_t* run) else { fs_ptr = xp_awk_valtostr ( - run, a2, xp_true, XP_NULL, &fs_len); + run, a2, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &fs_len); if (fs_ptr == XP_NULL) { if (str_free != XP_NULL) @@ -762,7 +768,8 @@ static int __bfn_tolower (xp_awk_run_t* run) } else { - str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len); + str = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len); if (str == XP_NULL) return -1; } @@ -800,7 +807,8 @@ static int __bfn_toupper (xp_awk_run_t* run) } else { - str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len); + str = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &len); if (str == XP_NULL) return -1; } @@ -866,7 +874,8 @@ static int __substitute (xp_awk_run_t* run, xp_long_t max_count) } else { - a0_ptr = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &a0_len); + a0_ptr = xp_awk_valtostr ( + run, a0, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &a0_len); if (a0_ptr == XP_NULL) { FREE_A_PTRS (run->awk); @@ -882,7 +891,8 @@ static int __substitute (xp_awk_run_t* run, xp_long_t max_count) } else { - a1_ptr = xp_awk_valtostr (run, a1, xp_true, XP_NULL, &a1_len); + a1_ptr = xp_awk_valtostr ( + run, a1, XP_AWK_VALTOSTR_CLEAR, XP_NULL, &a1_len); if (a1_ptr == XP_NULL) { FREE_A_PTRS (run->awk); @@ -937,8 +947,8 @@ static int __substitute (xp_awk_run_t* run, xp_long_t max_count) } else { - a2_ptr = xp_awk_valtostr ( - run, *a2_ref, xp_true, XP_NULL, &a2_len); + a2_ptr = xp_awk_valtostr (run, *a2_ref, + XP_AWK_VALTOSTR_CLEAR, XP_NULL, &a2_len); if (a2_ptr == XP_NULL) { FREE_A_PTRS (run->awk); @@ -1129,7 +1139,8 @@ static int __bfn_system (xp_awk_run_t* run) xp_assert (nargs == 1); cmd = xp_awk_valtostr ( - run, xp_awk_getarg(run, 0), xp_true, XP_NULL, XP_NULL); + run, xp_awk_getarg(run, 0), + XP_AWK_VALTOSTR_CLEAR, XP_NULL, XP_NULL); if (cmd == XP_NULL) return -1; #ifdef _WIN32 diff --git a/ase/awk/run.c b/ase/awk/run.c index 167261ba..5a8e65d6 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.231 2006-10-11 03:18:29 bacon Exp $ + * $Id: run.c,v 1.232 2006-10-11 15:01:55 bacon Exp $ */ #include @@ -36,6 +36,7 @@ enum #define PANIC_I(run,code) \ do { (run)->errnum = (code); return -1; } while (0) +#define DEFAULT_CONVFMT XP_T("%.6g") #define DEFAULT_OFMT XP_T("%.6g") #define DEFAULT_OFS XP_T(" ") #define DEFAULT_ORS XP_T("\n") @@ -240,7 +241,19 @@ int xp_awk_setglobal (xp_awk_run_t* run, xp_size_t idx, xp_awk_val_t* val) PANIC_I (run, XP_AWK_ESCALARTOMAP); } - if (idx == XP_AWK_GLOBAL_FS) + if (idx == XP_AWK_GLOBAL_CONVFMT) + { + xp_char_t* convfmt_ptr; + xp_size_t convfmt_len; + + convfmt_ptr = xp_awk_valtostr ( + run, val, xp_true, XP_NULL, &convfmt_len); + if (convfmt_ptr == XP_NULL) return -1; + + run->global.convfmt.ptr = convfmt_ptr; + run->global.convfmt.len = convfmt_len; + } + else if (idx == XP_AWK_GLOBAL_FS) { xp_char_t* fs_ptr; xp_size_t fs_len; @@ -637,6 +650,8 @@ static int __init_run (xp_awk_run_t* run, xp_awk_runios_t* runios, int* errnum) run->global.rs = XP_NULL; run->global.fs = XP_NULL; run->global.ignorecase = 0; + run->global.convfmt.ptr = DEFAULT_CONVFMT; + run->global.convfmt.len = xp_awk_strlen(DEFAULT_CONVFMT); run->global.ofmt.ptr = DEFAULT_OFMT; run->global.ofmt.len = xp_awk_strlen(DEFAULT_OFMT); run->global.ofs.ptr = DEFAULT_OFS; @@ -669,6 +684,14 @@ static void __deinit_run (xp_awk_run_t* run) run->global.fs = XP_NULL; } + if (run->global.convfmt.ptr != XP_NULL && + run->global.convfmt.ptr != DEFAULT_CONVFMT) + { + XP_AWK_FREE (run->awk, run->global.convfmt.ptr); + run->global.convfmt.ptr = XP_NULL; + run->global.convfmt.len = 0; + } + if (run->global.ofmt.ptr != XP_NULL && run->global.ofmt.ptr != DEFAULT_OFMT) { diff --git a/ase/awk/val.c b/ase/awk/val.c index fd089998..0b371389 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,18 +1,18 @@ /* - * $Id: val.c,v 1.70 2006-10-08 05:46:41 bacon Exp $ + * $Id: val.c,v 1.71 2006-10-11 15:01:55 bacon Exp $ */ #include static xp_char_t* __str_to_str ( xp_awk_run_t* run, const xp_char_t* str, xp_size_t str_len, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); + int opt, xp_awk_str_t* buf, xp_size_t* len); static xp_char_t* __val_int_to_str ( xp_awk_run_t* run, xp_awk_val_int_t* v, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); + int opt, xp_awk_str_t* buf, xp_size_t* len); static xp_char_t* __val_real_to_str ( xp_awk_run_t* run, xp_awk_val_real_t* v, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); + int opt, xp_awk_str_t* buf, xp_size_t* len); static xp_awk_val_nil_t __awk_nil = { XP_AWK_VAL_NIL, 0 }; xp_awk_val_t* xp_awk_val_nil = (xp_awk_val_t*)&__awk_nil; @@ -373,11 +373,11 @@ xp_bool_t xp_awk_valtobool (xp_awk_val_t* val) xp_char_t* xp_awk_valtostr ( xp_awk_run_t* run, xp_awk_val_t* v, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) + int opt, xp_awk_str_t* buf, xp_size_t* len) { if (v->type == XP_AWK_VAL_NIL) { - return __str_to_str (run, XP_T(""), 0, clear_buf, buf, len); + return __str_to_str (run, XP_T(""), 0, opt, buf, len); } if (v->type == XP_AWK_VAL_INT) @@ -389,12 +389,12 @@ xp_char_t* xp_awk_valtostr ( { return __str_to_str ( run, vi->nde->str, vi->nde->len, - clear_buf, buf, len); + opt, buf, len); } else { */ - return __val_int_to_str (run, vi, clear_buf, buf, len); + return __val_int_to_str (run, vi, opt, buf, len); /*}*/ } @@ -407,11 +407,11 @@ xp_char_t* xp_awk_valtostr ( { return __str_to_str ( run, vr->nde->str, vr->nde->len, - clear_buf, buf, len); + opt, buf, len); } else {*/ - return __val_real_to_str (run, vr, clear_buf, buf, len); + return __val_real_to_str (run, vr, opt, buf, len); /*}*/ } @@ -420,7 +420,7 @@ xp_char_t* xp_awk_valtostr ( xp_awk_val_str_t* vs = (xp_awk_val_str_t*)v; return __str_to_str ( - run, vs->buf, vs->len, clear_buf, buf, len); + run, vs->buf, vs->len, opt, buf, len); } /* TODO: process more value types */ @@ -432,7 +432,7 @@ xp_printf (XP_T("*** ERROR: WRONG VALUE TYPE [%d] in xp_awk_valtostr v=> %p***\n static xp_char_t* __str_to_str ( xp_awk_run_t* run, const xp_char_t* str, xp_size_t str_len, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) + int opt, xp_awk_str_t* buf, xp_size_t* len) { if (buf == XP_NULL) { @@ -451,7 +451,7 @@ static xp_char_t* __str_to_str ( { xp_size_t n; - if (clear_buf) xp_awk_str_clear (buf); + if (opt & XP_AWK_VALTOSTR_CLEAR) xp_awk_str_clear (buf); n = xp_awk_str_ncat (buf, str, str_len); if (n == (xp_size_t)-1) { @@ -466,7 +466,7 @@ static xp_char_t* __str_to_str ( static xp_char_t* __val_int_to_str ( xp_awk_run_t* run, xp_awk_val_int_t* v, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) + int opt, xp_awk_str_t* buf, xp_size_t* len) { xp_char_t* tmp; xp_long_t t; @@ -493,7 +493,7 @@ static xp_char_t* __val_int_to_str ( } else { - if (clear_buf) xp_awk_str_clear (buf); + if (opt & XP_AWK_VALTOSTR_CLEAR) xp_awk_str_clear (buf); if (xp_awk_str_cat (buf, XP_T("0")) == (xp_size_t)-1) { run->errnum = XP_AWK_ENOMEM; @@ -525,7 +525,7 @@ static xp_char_t* __val_int_to_str ( else { /* clear the buffer */ - if (clear_buf) xp_awk_str_clear (buf); + if (opt & XP_AWK_VALTOSTR_CLEAR) xp_awk_str_clear (buf); tmp = XP_AWK_STR_BUF(buf) + XP_AWK_STR_LEN(buf); @@ -560,29 +560,18 @@ static xp_char_t* __val_int_to_str ( static xp_char_t* __val_real_to_str ( xp_awk_run_t* run, xp_awk_val_real_t* v, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len) + int opt, xp_awk_str_t* buf, xp_size_t* len) { /* TODO: change the code */ xp_char_t tbuf[256], * tmp; + tmp = (opt & XP_AWK_VALTOSTR_PRINT)? + run->global.ofmt.ptr: run->global.convfmt.ptr; + /* TODO: need to use awk's own version of sprintf so that it would have * problems with handling long double or double... */ -/* -#if (XP_SIZEOF_LONG_DOUBLE != 0) - run->awk->syscas->sprintf ( - tbuf, xp_countof(tbuf), run->global.ofmt.ptr, (long double)v->val); -#elif (XP_SIZEOF_DOUBLE != 0) -*/ -/* TODO: does it need to check if a null character is included in ofmt??? */ - run->awk->syscas->sprintf ( - tbuf, xp_countof(tbuf), - run->global.ofmt.ptr, (double)v->val); -/* -#else - #error unsupported floating-point data type -#endif -*/ - +/* TODO: does it need to check if a null character is included in convfmt??? */ + run->awk->syscas->sprintf (tbuf, xp_countof(tbuf), tmp, (double)v->val); if (buf == XP_NULL) { tmp = xp_awk_strdup (run->awk, tbuf); @@ -596,7 +585,7 @@ static xp_char_t* __val_real_to_str ( } else { - if (clear_buf) xp_awk_str_clear (buf); + if (opt & XP_AWK_VALTOSTR_CLEAR) xp_awk_str_clear (buf); if (xp_awk_str_cat (buf, tbuf) == (xp_size_t)-1) { diff --git a/ase/awk/val.h b/ase/awk/val.h index 70f029e6..3c1e0f46 100644 --- a/ase/awk/val.h +++ b/ase/awk/val.h @@ -1,5 +1,5 @@ /* - * $Id: val.h,v 1.44 2006-10-01 14:48:48 bacon Exp $ + * $Id: val.h,v 1.45 2006-10-11 15:01:55 bacon Exp $ */ #ifndef _XP_AWK_VAL_H_ @@ -39,6 +39,12 @@ enum XP_AWK_VAL_REF_POS }; +enum +{ + XP_AWK_VALTOSTR_CLEAR = (1 << 0), + XP_AWK_VALTOSTR_PRINT = (1 << 1) +}; + typedef struct xp_awk_val_nil_t xp_awk_val_nil_t; typedef struct xp_awk_val_int_t xp_awk_val_int_t; typedef struct xp_awk_val_real_t xp_awk_val_real_t; @@ -161,7 +167,7 @@ void xp_awk_refdownval_nofree (xp_awk_run_t* run, xp_awk_val_t* val); xp_bool_t xp_awk_valtobool (xp_awk_val_t* val); xp_char_t* xp_awk_valtostr ( xp_awk_run_t* run, xp_awk_val_t* val, - xp_bool_t clear_buf, xp_awk_str_t* buf, xp_size_t* len); + int opt, xp_awk_str_t* buf, xp_size_t* len); int xp_awk_valtonum ( xp_awk_run_t* run, xp_awk_val_t* v, xp_long_t* l, xp_real_t* r); diff --git a/ase/test/awk/t50.awk b/ase/test/awk/t50.awk new file mode 100644 index 00000000..b6258c84 --- /dev/null +++ b/ase/test/awk/t50.awk @@ -0,0 +1,6 @@ +BEGIN { + two = 2; three = 3; + print (two three) + 4; +} + +