diff --git a/ase/awk/awk.c b/ase/awk/awk.c index e1ac1aed..202ef44f 100644 --- a/ase/awk/awk.c +++ b/ase/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.81 2006-10-15 15:45:41 bacon Exp $ + * $Id: awk.c,v 1.82 2006-10-16 08:47:59 bacon Exp $ */ #include @@ -41,6 +41,10 @@ xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas) #endif if (awk == XP_NULL) return XP_NULL; + /* it uses the built-in xp_awk_memset because awk is not + * fully initialized yet */ + xp_awk_memset (awk, 0, xp_sizeof(xp_awk_t)); + if (syscas->memcpy == XP_NULL) { xp_awk_memcpy (&awk->syscas, syscas, xp_sizeof(awk->syscas)); diff --git a/ase/awk/func.c b/ase/awk/func.c index b56420c8..cde95901 100644 --- a/ase/awk/func.c +++ b/ase/awk/func.c @@ -1,5 +1,5 @@ /* - * $Id: func.c,v 1.62 2006-10-12 04:17:30 bacon Exp $ + * $Id: func.c,v 1.63 2006-10-16 08:47:59 bacon Exp $ */ #include @@ -510,7 +510,7 @@ static int __bfn_split (xp_awk_run_t* run) xp_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref; xp_char_t* str, * str_free, * p, * tok; xp_size_t str_len, str_left, tok_len; - xp_long_t num; + xp_long_t sta, num; xp_char_t key[xp_sizeof(xp_long_t)*8+2]; xp_size_t key_len; xp_char_t* fs_ptr, * fs_free; @@ -656,7 +656,10 @@ static int __bfn_split (xp_awk_run_t* run) *a1_ref = t1; xp_awk_refupval (*a1_ref); - p = str; str_left = str_len; num = 0; + p = str; str_left = str_len; + sta = (xp_awk_getopt(run->awk) & XP_AWK_STRINDEXONE)? 1: 0; + num = sta; + while (p != XP_NULL) { if (fs_len <= 1) @@ -739,6 +742,8 @@ static int __bfn_split (xp_awk_run_t* run) if (fs_free != XP_NULL) XP_AWK_FREE (run->awk, fs_free); if (fs_rex_free != XP_NULL) xp_awk_freerex (run->awk, fs_rex_free); + if (sta == 1) num--; + t1 = xp_awk_makeintval (run, num); if (t1 == XP_NULL) { diff --git a/ase/awk/run.c b/ase/awk/run.c index e1d8c31a..a6d97f62 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.235 2006-10-13 10:18:10 bacon Exp $ + * $Id: run.c,v 1.236 2006-10-16 08:47:59 bacon Exp $ */ #include @@ -59,7 +59,9 @@ static void __del_run (xp_awk_t* awk, xp_awk_run_t* run); static int __init_run ( xp_awk_run_t* run, xp_awk_runios_t* runios, int* errnum); static void __deinit_run (xp_awk_run_t* run); + static int __build_runarg (xp_awk_run_t* run, xp_awk_runarg_t* runarg); +static int __set_globals_to_default (xp_awk_run_t* run); static int __run_main (xp_awk_run_t* run, xp_awk_runarg_t* runarg); static int __run_pattern_blocks (xp_awk_run_t* run); @@ -465,6 +467,8 @@ int xp_awk_run (xp_awk_t* awk, return -1; } + XP_AWK_MEMSET (awk, run, 0, xp_sizeof(xp_awk_run_t)); + __add_run (awk, run); if (__init_run (run, runios, &errnum) == -1) @@ -651,16 +655,6 @@ 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; - 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); return 0; } @@ -839,7 +833,8 @@ static int __build_runarg (xp_awk_run_t* run, xp_awk_runarg_t* runarg) xp_awk_refupval (v_argc); - xp_awk_assert (run->awk, STACK_GLOBAL(run,XP_AWK_GLOBAL_ARGC) == xp_awk_val_nil); + xp_awk_assert (run->awk, + STACK_GLOBAL(run,XP_AWK_GLOBAL_ARGC) == xp_awk_val_nil); if (xp_awk_setglobal (run, XP_AWK_GLOBAL_ARGC, v_argc) == -1) { @@ -867,12 +862,64 @@ static int __build_runarg (xp_awk_run_t* run, xp_awk_runarg_t* runarg) return 0; } +static int __set_globals_to_default (xp_awk_run_t* run) +{ + struct __gtab_t + { + int idx; + const xp_char_t* str; + }; + + static struct __gtab_t gtab[] = + { + { XP_AWK_GLOBAL_CONVFMT, DEFAULT_CONVFMT }, + { XP_AWK_GLOBAL_OFMT, DEFAULT_OFMT }, + { XP_AWK_GLOBAL_OFS, DEFAULT_OFS }, + { XP_AWK_GLOBAL_ORS, DEFAULT_ORS }, + { XP_AWK_GLOBAL_SUBSEP, DEFAULT_SUBSEP }, + }; + + xp_awk_val_t* tmp; + xp_size_t i, j; + + for (i = 0; i < xp_countof(gtab); i++) + { + tmp = xp_awk_makestrval0 (run, gtab[i].str); + if (tmp == XP_NULL) + { + run->errnum = XP_AWK_ENOMEM; + return -1; + } + + xp_awk_refupval (tmp); + + xp_awk_assert (run->awk, + STACK_GLOBAL(run,gtab[i].idx) == xp_awk_val_nil); + + if (xp_awk_setglobal (run, gtab[i].idx, tmp) == -1) + { + for (j = 0; j < i; j++) + { + xp_awk_setglobal ( + run, gtab[i].idx, xp_awk_val_nil); + } + + xp_awk_refdownval (run, tmp); + return -1; + } + + xp_awk_refdownval (run, tmp); + } + + return 0; +} + static int __run_main (xp_awk_run_t* run, xp_awk_runarg_t* runarg) { xp_size_t nglobals, nargs, i; xp_size_t saved_stack_top; xp_awk_val_t* v; - int n = 0; + int n; xp_awk_assert (run->awk, run->stack_base == 0 && run->stack_top == 0); @@ -903,6 +950,7 @@ static int __run_main (xp_awk_run_t* run, xp_awk_runarg_t* runarg) run->stack_top = saved_stack_top; return -1; } + if (xp_awk_setglobal (run, XP_AWK_GLOBAL_NF, xp_awk_val_zero) == -1) { /* it can simply restore the top of the stack this way @@ -925,7 +973,8 @@ static int __run_main (xp_awk_run_t* run, xp_awk_runarg_t* runarg) run->exit_level = EXIT_NONE; - if (run->awk->option & XP_AWK_RUNMAIN) + n = __set_globals_to_default (run); + if (n == 0 && (run->awk->option & XP_AWK_RUNMAIN)) { /* TODO: should the main function be user-specifiable? */ xp_awk_nde_call_t nde; @@ -946,7 +995,7 @@ static int __run_main (xp_awk_run_t* run, xp_awk_runarg_t* runarg) xp_awk_refdownval (run, v); } } - else + else if (n == 0) { saved_stack_top = run->stack_top; if (__raw_push(run,(void*)run->stack_base) == -1) diff --git a/ase/test/awk/arr.awk b/ase/test/awk/arr.awk new file mode 100644 index 00000000..f3b58e93 --- /dev/null +++ b/ase/test/awk/arr.awk @@ -0,0 +1,18 @@ +BEGIN { + a[1,2,3] = 20; + a[4,5,6] = 30; + + for (i in a) + { + n = split (i, k, SUBSEP); + for (j = 1; j < n; j++) + { + print k[j] + } + } + + if ((1,2,3) in a) + { + print a[1,2,3]; + } +} diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index 4efa277c..b433a1f1 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.97 2006-10-15 15:46:14 bacon Exp $ + * $Id: awk.c,v 1.98 2006-10-16 08:48:19 bacon Exp $ */ #include @@ -99,7 +99,12 @@ static int __dprintf (const xp_char_t* fmt, ...) va_start (ap, fmt); #ifdef _WIN32 n = xp_vsprintf (buf, xp_countof(buf), fmt, ap); - MessageBox (NULL, buf, XP_T("ASSERTION FAILURE"), MB_OK | MB_ICONERROR); +#if defined(_MSC_VER) && (_MSC_VER>=1400) + MessageBox (NULL, buf, XP_T("\uD655\uC778\uC2E4\uD328 Assertion Failure"), MB_OK | MB_ICONERROR); +#else + MessageBox (NULL, buf, XP_T("Assertion Failure"), MB_OK | MB_ICONERROR); +#endif + #else n = xp_vprintf (fmt, ap); #endif