From 95b904c8e9544c80942b200998fb5a3a984c30d9 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 29 Sep 2006 11:18:13 +0000 Subject: [PATCH] *** empty log message *** --- ase/awk/awk_i.h | 12 ++- ase/awk/extio.c | 90 ++++++--------------- ase/awk/extio.h | 8 +- ase/awk/run.c | 182 ++++++++++++++++++++++++++++-------------- ase/awk/val.c | 6 +- ase/test/awk/comp.awk | 4 +- 6 files changed, 168 insertions(+), 134 deletions(-) diff --git a/ase/awk/awk_i.h b/ase/awk/awk_i.h index d908cf37..c00c547c 100644 --- a/ase/awk/awk_i.h +++ b/ase/awk/awk_i.h @@ -1,5 +1,5 @@ /* - * $Id: awk_i.h,v 1.60 2006-09-27 14:11:31 bacon Exp $ + * $Id: awk_i.h,v 1.61 2006-09-29 11:18:13 bacon Exp $ */ #ifndef _XP_AWK_AWKI_H_ @@ -248,12 +248,22 @@ struct xp_awk_run_t void* fs; int ignorecase; + struct + { + xp_char_t* ptr; + xp_size_t len; + } ofmt; struct { xp_char_t* ptr; xp_size_t len; } ofs; struct + { + xp_char_t* ptr; + xp_size_t len; + } ors; + struct { xp_char_t* ptr; xp_size_t len; diff --git a/ase/awk/extio.c b/ase/awk/extio.c index f1cf8503..2b3d80d8 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.48 2006-09-22 14:04:25 bacon Exp $ + * $Id: extio.c,v 1.49 2006-09-29 11:18:13 bacon Exp $ */ #include @@ -72,10 +72,6 @@ static int __out_mask_map[] = __MASK_WRITE }; -static int __writeextio ( - xp_awk_run_t* run, int out_type, - const xp_char_t* name, xp_awk_val_t* v, xp_bool_t nl); - int xp_awk_readextio ( xp_awk_run_t* run, int in_type, const xp_char_t* name, xp_awk_str_t* buf) @@ -360,28 +356,37 @@ int xp_awk_readextio ( return ret; } -int xp_awk_writeextio ( +int xp_awk_writeextio_val ( xp_awk_run_t* run, int out_type, const xp_char_t* name, xp_awk_val_t* v) { - return __writeextio (run, out_type, name, v, xp_false); + xp_char_t* str; + xp_size_t len; + int n; + + if (v->type == XP_AWK_VAL_STR) + { + str = ((xp_awk_val_str_t*)v)->buf; + len = ((xp_awk_val_str_t*)v)->len; + } + else + { + str = xp_awk_valtostr (run, v, xp_true, XP_NULL, &len); + if (str == XP_NULL) return -1; + } + + n = xp_awk_writeextio_str (run, out_type, name, str, len); + + if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); + return n; } -int xp_awk_writeextio_nl ( +int xp_awk_writeextio_str ( xp_awk_run_t* run, int out_type, - const xp_char_t* name, xp_awk_val_t* v) -{ - return __writeextio (run, out_type, name, v, xp_true); -} - -static int __writeextio ( - xp_awk_run_t* run, int out_type, - const xp_char_t* name, xp_awk_val_t* v, xp_bool_t nl) + const xp_char_t* name, xp_char_t* str, xp_size_t len) { xp_awk_extio_t* p = run->extio.chain; xp_awk_io_t handler; - xp_char_t* str; - xp_size_t len; int extio_type, extio_mode, extio_mask, n; xp_assert (out_type >= 0 && out_type <= xp_countof(__out_type_map)); @@ -401,21 +406,6 @@ static int __writeextio ( return -1; } - if (v->type == XP_AWK_VAL_STR) - { - str = ((xp_awk_val_str_t*)v)->buf; - len = ((xp_awk_val_str_t*)v)->len; - } - else - { - /* convert the value to string representation first */ - - /* TOOD: consider using a shared buffer when calling - * xp_awk_valtostr. maybe run->shared_buf.extio */ - str = xp_awk_valtostr (run, v, xp_true, XP_NULL, &len); - if (str == XP_NULL) return -1; - } - /* look for the corresponding extio for name */ while (p != XP_NULL) { @@ -441,7 +431,6 @@ static int __writeextio ( run->awk, xp_sizeof(xp_awk_extio_t)); if (p == XP_NULL) { - if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); run->errnum = XP_AWK_ENOMEM; return -1; } @@ -450,7 +439,6 @@ static int __writeextio ( if (p->name == XP_NULL) { XP_AWK_FREE (run->awk, p); - if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); run->errnum = XP_AWK_ENOMEM; return -1; } @@ -465,7 +453,6 @@ static int __writeextio ( { XP_AWK_FREE (run->awk, p->name); XP_AWK_FREE (run->awk, p); - if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); /* TODO: use meaningful error code */ if (xp_awk_setglobal ( @@ -484,41 +471,11 @@ static int __writeextio ( if (n == 0) return 0; } -/* TODO: */ -/* TODO: */ /* TODO: if write handler returns less than the request, loop */ -/* TODO: */ -/* TODO: */ if (len > 0) { n = handler (XP_AWK_IO_WRITE, p, str, len); - if (n == -1) - { - if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); - - /* TODO: use meaningful error code */ - if (xp_awk_setglobal ( - run, XP_AWK_GLOBAL_ERRNO, - xp_awk_val_one) == -1) return -1; - - run->errnum = XP_AWK_EIOHANDLER; - return -1; - } - - if (n == 0) - { - if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); - return 0; - } - } - - if (v->type != XP_AWK_VAL_STR) XP_AWK_FREE (run->awk, str); - - if (nl) - { - /* TODO: use proper NEWLINE separator */ - n = handler (XP_AWK_IO_WRITE, p, XP_T("\n"), 1); if (n == -1) { /* TODO: use meaningful error code */ @@ -536,6 +493,7 @@ static int __writeextio ( return 1; } + int xp_awk_flushextio ( xp_awk_run_t* run, int out_type, const xp_char_t* name) { diff --git a/ase/awk/extio.h b/ase/awk/extio.h index b0af3075..2d9b3ebc 100644 --- a/ase/awk/extio.h +++ b/ase/awk/extio.h @@ -1,5 +1,5 @@ /* - * $Id: extio.h,v 1.12 2006-08-31 16:00:18 bacon Exp $ + * $Id: extio.h,v 1.13 2006-09-29 11:18:13 bacon Exp $ */ #ifndef _XP_AWK_EXTIO_H_ @@ -17,13 +17,13 @@ int xp_awk_readextio ( xp_awk_run_t* run, int in_type, const xp_char_t* name, xp_awk_str_t* buf); -int xp_awk_writeextio ( +int xp_awk_writeextio_val ( xp_awk_run_t* run, int out_type, const xp_char_t* name, xp_awk_val_t* v); -int xp_awk_writeextio_nl ( +int xp_awk_writeextio_str ( xp_awk_run_t* run, int out_type, - const xp_char_t* name, xp_awk_val_t* v); + const xp_char_t* name, xp_char_t* str, xp_size_t len); int xp_awk_flushextio ( xp_awk_run_t* run, int out_type, const xp_char_t* name); diff --git a/ase/awk/run.c b/ase/awk/run.c index d0dd133b..02b3f1eb 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.211 2006-09-28 13:47:58 bacon Exp $ + * $Id: run.c,v 1.212 2006-09-29 11:18:13 bacon Exp $ */ #include @@ -36,7 +36,16 @@ enum #define PANIC_I(run,code) \ do { (run)->errnum = (code); return -1; } while (0) +#if (XP_SIZEOF_LONG_DOUBLE != 0) + #define DEFAULT_OFMT XP_T("%.6Lg") +#elif (XP_SIZEOF_DOUBLE != 0) + #define DEFAULT_OFMT XP_T("%.6g") +#else + #error Unsupported floating-point type size +#endif + #define DEFAULT_OFS XP_T(" ") +#define DEFAULT_ORS XP_T("\n") #define DEFAULT_SUBSEP XP_T("\034") static void __add_run (xp_awk_t* awk, xp_awk_run_t* run); @@ -233,56 +242,7 @@ int xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val) PANIC_I (r, XP_AWK_ESCALARTOMAP); } - if (idx == XP_AWK_GLOBAL_RS) - { - xp_char_t* rs_ptr; - xp_size_t rs_len; - - if (val->type == XP_AWK_VAL_STR) - { - rs_ptr = ((xp_awk_val_str_t*)val)->buf; - rs_len = ((xp_awk_val_str_t*)val)->len; - } - else - { - /* due to the expression evaluation rule, the - * regular expression can not be an assigned value */ - xp_assert (val->type != XP_AWK_VAL_REX); - - rs_ptr = xp_awk_valtostr ( - run, val, xp_true, XP_NULL, &rs_len); - if (rs_ptr == XP_NULL) return -1; - } - - if (rs_len > 1) - { - void* rex; - - /* compile the regular expression */ - /* TODO: use safebuild */ - rex = xp_awk_buildrex ( - ((xp_awk_run_t*)run)->awk, - rs_ptr, rs_len, &r->errnum); - if (rex == XP_NULL) - { - if (val->type != XP_AWK_VAL_STR) - XP_AWK_FREE (((xp_awk_run_t*)run)->awk, rs_ptr); - return -1; - } - - if (r->global.rs != XP_NULL) - { - xp_awk_freerex ( - ((xp_awk_run_t*)run)->awk, - r->global.rs); - } - r->global.rs = rex; - } - - if (val->type != XP_AWK_VAL_STR) - XP_AWK_FREE (((xp_awk_run_t*)run)->awk, rs_ptr); - } - else if (idx == XP_AWK_GLOBAL_FS) + if (idx == XP_AWK_GLOBAL_FS) { xp_char_t* fs_ptr; xp_size_t fs_len; @@ -362,6 +322,18 @@ int xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val) if (__shorten_record (r, (xp_size_t)lv) == -1) return -1; } } + else if (idx == XP_AWK_GLOBAL_OFMT) + { + xp_char_t* ofmt_ptr; + xp_size_t ofmt_len; + + ofmt_ptr = xp_awk_valtostr ( + run, val, xp_true, XP_NULL, &ofmt_len); + if (ofmt_ptr == XP_NULL) return -1; + + r->global.ofmt.ptr = ofmt_ptr; + r->global.ofmt.len = ofmt_len; + } else if (idx == XP_AWK_GLOBAL_OFS) { xp_char_t* ofs_ptr; @@ -374,6 +346,67 @@ int xp_awk_setglobal (void* run, xp_size_t idx, xp_awk_val_t* val) r->global.ofs.ptr = ofs_ptr; r->global.ofs.len = ofs_len; } + else if (idx == XP_AWK_GLOBAL_ORS) + { + xp_char_t* ors_ptr; + xp_size_t ors_len; + + ors_ptr = xp_awk_valtostr ( + run, val, xp_true, XP_NULL, &ors_len); + if (ors_ptr == XP_NULL) return -1; + + r->global.ors.ptr = ors_ptr; + r->global.ors.len = ors_len; + } + else if (idx == XP_AWK_GLOBAL_RS) + { + xp_char_t* rs_ptr; + xp_size_t rs_len; + + if (val->type == XP_AWK_VAL_STR) + { + rs_ptr = ((xp_awk_val_str_t*)val)->buf; + rs_len = ((xp_awk_val_str_t*)val)->len; + } + else + { + /* due to the expression evaluation rule, the + * regular expression can not be an assigned value */ + xp_assert (val->type != XP_AWK_VAL_REX); + + rs_ptr = xp_awk_valtostr ( + run, val, xp_true, XP_NULL, &rs_len); + if (rs_ptr == XP_NULL) return -1; + } + + if (rs_len > 1) + { + void* rex; + + /* compile the regular expression */ + /* TODO: use safebuild */ + rex = xp_awk_buildrex ( + ((xp_awk_run_t*)run)->awk, + rs_ptr, rs_len, &r->errnum); + if (rex == XP_NULL) + { + if (val->type != XP_AWK_VAL_STR) + XP_AWK_FREE (((xp_awk_run_t*)run)->awk, rs_ptr); + return -1; + } + + if (r->global.rs != XP_NULL) + { + xp_awk_freerex ( + ((xp_awk_run_t*)run)->awk, + r->global.rs); + } + r->global.rs = rex; + } + + if (val->type != XP_AWK_VAL_STR) + XP_AWK_FREE (((xp_awk_run_t*)run)->awk, rs_ptr); + } else if (idx == XP_AWK_GLOBAL_SUBSEP) { xp_char_t* subsep_ptr; @@ -637,8 +670,12 @@ static int __init_run ( run->global.rs = XP_NULL; run->global.fs = XP_NULL; run->global.ignorecase = 0; + run->global.ofmt.ptr = DEFAULT_OFMT; + run->global.ofmt.len = xp_awk_strlen(DEFAULT_OFMT); run->global.ofs.ptr = DEFAULT_OFS; run->global.ofs.len = xp_awk_strlen(DEFAULT_OFS); + run->global.ors.ptr = DEFAULT_ORS; + run->global.ors.len = xp_awk_strlen(DEFAULT_ORS); run->global.subsep.ptr = DEFAULT_SUBSEP; run->global.subsep.len = xp_awk_strlen(DEFAULT_SUBSEP); @@ -665,6 +702,14 @@ static void __deinit_run (xp_awk_run_t* run) run->global.fs = XP_NULL; } + if (run->global.ofmt.ptr != XP_NULL && + run->global.ofmt.ptr != DEFAULT_OFMT) + { + XP_AWK_FREE (run->awk, run->global.ofmt.ptr); + run->global.ofmt.ptr = XP_NULL; + run->global.ofmt.len = 0; + } + if (run->global.ofs.ptr != XP_NULL && run->global.ofs.ptr != DEFAULT_OFS) { @@ -673,6 +718,14 @@ static void __deinit_run (xp_awk_run_t* run) run->global.ofs.len = 0; } + if (run->global.ors.ptr != XP_NULL && + run->global.ors.ptr != DEFAULT_ORS) + { + XP_AWK_FREE (run->awk, run->global.ors.ptr); + run->global.ors.ptr = XP_NULL; + run->global.ors.len = 0; + } + if (run->global.subsep.ptr != XP_NULL && run->global.subsep.ptr != DEFAULT_SUBSEP) { @@ -1108,8 +1161,12 @@ static int __run_block (xp_awk_run_t* run, xp_awk_nde_blk_t* nde) /* blockless pattern - execute print $0*/ xp_awk_refupval (run->inrec.d0); - n = xp_awk_writeextio_nl (run, - XP_AWK_OUT_CONSOLE, XP_T(""), run->inrec.d0); + /*n = xp_awk_writeextio_val (run, + XP_AWK_OUT_CONSOLE, XP_T(""), run->inrec.d0);*/ + n = xp_awk_writeextio_str (run, + XP_AWK_OUT_CONSOLE, XP_T(""), + XP_AWK_STR_BUF(&run->inrec.line), + XP_AWK_STR_LEN(&run->inrec.line)); if (n == -1) { xp_awk_refdownval (run, run->inrec.d0); @@ -1902,7 +1959,7 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) v = run->inrec.d0; xp_awk_refupval (v); - n = xp_awk_writeextio (run, p->out_type, dst, v); + n = xp_awk_writeextio_val (run, p->out_type, dst, v); if (n < 0 && run->errnum != XP_AWK_EIOHANDLER) { if (out != XP_NULL) XP_AWK_FREE (run->awk, out); @@ -1925,7 +1982,7 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) } xp_awk_refupval (v); - n = xp_awk_writeextio (run, p->out_type, dst, v); + n = xp_awk_writeextio_val (run, p->out_type, dst, v); if (n < 0 && run->errnum != XP_AWK_EIOHANDLER) { if (out != XP_NULL) XP_AWK_FREE (run->awk, out); @@ -1934,6 +1991,15 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) } xp_awk_refdownval (run, v); + n = xp_awk_writeextio_str ( + run, p->out_type, dst, + run->global.ofs.ptr, run->global.ofs.len); + if (n < 0 && run->errnum != XP_AWK_EIOHANDLER) + { + if (out != XP_NULL) XP_AWK_FREE (run->awk, out); + return -1; + } + /* TODO: how to handle n == -1 && run->errnum == XP_AWK_EIOHANDLER. * that is the user handler returned an error... */ @@ -1941,9 +2007,9 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) } } - /* TODO: change xp_awk_val_nil to - * xp_awk_val_empty_string or something */ - n = xp_awk_writeextio_nl (run, p->out_type, dst, xp_awk_val_nil); + n = xp_awk_writeextio_str ( + run, p->out_type, dst, + run->global.ors.ptr, run->global.ors.len); if (n < 0 && run->errnum != XP_AWK_EIOHANDLER) { if (out != XP_NULL) XP_AWK_FREE (run->awk, out); diff --git a/ase/awk/val.c b/ase/awk/val.c index 5e505d1d..f5eb14f5 100644 --- a/ase/awk/val.c +++ b/ase/awk/val.c @@ -1,5 +1,5 @@ /* - * $Id: val.c,v 1.64 2006-09-28 14:21:23 bacon Exp $ + * $Id: val.c,v 1.65 2006-09-29 11:18:13 bacon Exp $ */ #include @@ -564,10 +564,10 @@ static xp_char_t* __val_real_to_str ( #if (XP_SIZEOF_LONG_DOUBLE != 0) run->awk->syscas->sprintf ( - tbuf, xp_countof(tbuf), XP_T("%.30Lg"), (long double)v->val); + tbuf, xp_countof(tbuf), XP_T("%.6Lg"), (long double)v->val); #elif (XP_SIZEOF_DOUBLE != 0) run->awk->syscas->sprintf ( - tbuf, xp_countof(tbuf), XP_T("%.30g"), (double)v->val); + tbuf, xp_countof(tbuf), XP_T("%.6g"), (double)v->val); #else #error unsupported floating-point data type #endif diff --git a/ase/test/awk/comp.awk b/ase/test/awk/comp.awk index 9f82c47a..132c175f 100644 --- a/ase/test/awk/comp.awk +++ b/ase/test/awk/comp.awk @@ -1,6 +1,6 @@ BEGIN { - print "1==1 : " (1 == 1); - print "1==0 : " (1 == 0); + print "1==1 : ", (1 == 1); + print "1==0 : ", (1 == 0); print "1.0==1 : " (1.0 == 1); print "1.1==1 : " (1.1 == 1);