diff --git a/ase/awk/awk.h b/ase/awk/awk.h index eb5d6755..fea3c6f5 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.99 2006-08-22 15:10:48 bacon Exp $ + * $Id: awk.h,v 1.100 2006-08-23 15:41:45 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -224,6 +224,8 @@ enum XP_AWK_ENEXTCALL, /* next called from BEGIN or END */ XP_AWK_ENEXTFILECALL, /* nextfile called from BEGIN or END */ XP_AWK_EIOIMPL, /* wrong implementation of user io handler */ + XP_AWK_ENOSUCHIO, /* no such io name found */ + XP_AWK_EIOHANDLER, /* io handler has returned an error */ XP_AWK_EINTERNAL, /* internal error */ /* regular expression error */ diff --git a/ase/awk/err.c b/ase/awk/err.c index 641cd12d..9ad93351 100644 --- a/ase/awk/err.c +++ b/ase/awk/err.c @@ -1,5 +1,5 @@ /* - * $Id: err.c,v 1.37 2006-08-20 15:49:06 bacon Exp $ + * $Id: err.c,v 1.38 2006-08-23 15:41:46 bacon Exp $ */ #include @@ -96,6 +96,8 @@ const xp_char_t* xp_awk_geterrstr (int errnum) XP_T("next cannot be called from the BEGIN or END block"), XP_T("nextfile cannot be called from the BEGIN or END block"), XP_T("wrong implementation of user-defined io handler"), + XP_T("no such io name found"), + XP_T("io handler has returned an error"), XP_T("internal error that should never have happened"), XP_T("a right parenthesis is expected in the regular expression"), diff --git a/ase/awk/extio.c b/ase/awk/extio.c index 247d064a..8fb92006 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.28 2006-08-22 15:10:48 bacon Exp $ + * $Id: extio.c,v 1.29 2006-08-23 15:41:46 bacon Exp $ */ #include @@ -16,7 +16,7 @@ enum __MASK_WRITE = 0x0200, __MASK_RDWR = 0x0400, - __MASK_CLEAR = 0x00FF + __MASK_CLEAR = 0x00FF }; static int __in_type_map[] = @@ -152,7 +152,7 @@ int xp_awk_readextio ( xp_awk_setglobal (run, XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -194,7 +194,7 @@ int xp_awk_readextio ( xp_awk_setglobal (run, XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -333,7 +333,7 @@ static int __writeextio ( xp_awk_setglobal (run, XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -361,7 +361,7 @@ static int __writeextio ( /* TODO: use meaningful error code */ xp_awk_setglobal (run, XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -383,7 +383,7 @@ static int __writeextio ( /* TODO: use meaningful error code */ xp_awk_setglobal (run, XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -399,6 +399,7 @@ int xp_awk_flushextio ( xp_awk_extio_t* p = run->extio.chain; xp_awk_io_t handler; int extio_type, extio_mode, extio_mask, n; + xp_bool_t ok = xp_false; xp_assert (out_type >= 0 && out_type <= xp_countof(__out_type_map)); xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mode_map)); @@ -421,32 +422,33 @@ int xp_awk_flushextio ( while (p != XP_NULL) { if (p->type == (extio_type | extio_mask) && - xp_strcmp (p->name, name) == 0) break; + (name == XP_NULL || xp_strcmp (p->name, name) == 0)) + { + n = handler (XP_AWK_IO_FLUSH, p, XP_NULL, 0); + + if (n == -1) + { + /* TODO: use meaningful error code */ + xp_awk_setglobal (run, + XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); + *errnum = XP_AWK_EIOHANDLER; + return -1; + } + + ok = xp_true; + } + p = p->next; } - /* there is not corresponding extio for name */ - if (p == XP_NULL) - { - /* TODO: use meaningful error code. but is this needed? */ - xp_awk_setglobal (run, - XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; - return -1; - } + if (ok) return 0; - n = handler (XP_AWK_IO_FLUSH, p, XP_NULL, 0); - - if (n == -1) - { - /* TODO: use meaningful error code */ - xp_awk_setglobal (run, - XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); - *errnum = XP_AWK_ENOERR; - return -1; - } - - return 0; + /* there is no corresponding extio for name */ + /* TODO: use meaningful error code. but is this needed? */ + xp_awk_setglobal (run, + XP_AWK_GLOBAL_ERRNO, xp_awk_val_one); + *errnum = XP_AWK_ENOSUCHIO; + return -1; } int xp_awk_nextextio_read ( @@ -491,7 +493,7 @@ int xp_awk_nextextio_read ( if (n == -1) { /* TODO: is this errnum correct? */ - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -535,7 +537,8 @@ int xp_awk_closeextio_read ( if (handler (XP_AWK_IO_CLOSE, p, XP_NULL, 0) == -1) { /* this is not a run-time error.*/ - *errnum = XP_AWK_ENOERR; + /* TODO: set ERRNO */ + *errnum = XP_AWK_EIOHANDLER; return -1; } } @@ -553,7 +556,7 @@ int xp_awk_closeextio_read ( } /* this is not a run-time error */ - *errnum = XP_AWK_ENOERR; + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -594,7 +597,8 @@ int xp_awk_closeextio_write ( if (handler (XP_AWK_IO_CLOSE, p, XP_NULL, 0) == -1) { /* this is not a run-time error.*/ - *errnum = XP_AWK_ENOERR; + /* TODO: set ERRNO */ + *errnum = XP_AWK_EIOHANDLER; return -1; } } @@ -612,7 +616,8 @@ int xp_awk_closeextio_write ( } /* this is not a run-time error */ - *errnum = XP_AWK_ENOERR; + /* TODO: set ERRNO */ + *errnum = XP_AWK_EIOHANDLER; return -1; } @@ -635,7 +640,8 @@ int xp_awk_closeextio ( if (handler (XP_AWK_IO_CLOSE, p, XP_NULL, 0) == -1) { /* this is not a run-time error.*/ - *errnum = XP_AWK_ENOERR; + /* TODO: set ERRNO */ + *errnum = XP_AWK_EIOHANDLER; return -1; } } @@ -653,7 +659,8 @@ int xp_awk_closeextio ( } /* this is not a run-time error */ - *errnum = XP_AWK_ENOERR; + /* TODO: set ERRNO */ + *errnum = XP_AWK_EIOHANDLER; return -1; } diff --git a/ase/awk/func.c b/ase/awk/func.c index 180abda4..21235a7d 100644 --- a/ase/awk/func.c +++ b/ase/awk/func.c @@ -1,5 +1,5 @@ /* - * $Id: func.c,v 1.26 2006-08-22 15:10:48 bacon Exp $ + * $Id: func.c,v 1.27 2006-08-23 15:41:46 bacon Exp $ */ #include @@ -200,7 +200,7 @@ static int __bfn_close (xp_awk_t* awk, void* run) } n = xp_awk_closeextio (run, name, &errnum); - if (n == -1 && errnum != XP_AWK_ENOERR) + if (n == -1 && errnum != XP_AWK_EIOHANDLER) { if (a0->type != XP_AWK_VAL_STR) xp_free (name); xp_awk_seterrnum (run, errnum); @@ -237,7 +237,9 @@ static int __bfn_fflush (xp_awk_t* awk, void* run) /* flush the console output */ n = xp_awk_flushextio (run, XP_AWK_OUT_CONSOLE, XP_T(""), &errnum); - if (n == -1 && errnum != XP_AWK_ENOERR) + if (n == -1 && + errnum != XP_AWK_EIOHANDLER && + errnum != XP_AWK_ENOSUCHIO) { xp_awk_seterrnum (run, errnum); return -1; @@ -280,56 +282,78 @@ static int __bfn_fflush (xp_awk_t* awk, void* run) ptr++; } - if (len0 == 0) - { - /* flush all open files and pipes */ - /* TODO: */ - } - else - { - /* flush the given extio */ - n = 0; + /* flush the given extio */ + n = 1; - /* TODO: no file -> error, at least on file -> not an error */ - n2 = xp_awk_flushextio (run, - XP_AWK_OUT_FILE, str0, &errnum); + if (((xp_awk_run_t*)run)->extio.handler[XP_AWK_EXTIO_FILE] != XP_NULL) + { + n2 = xp_awk_flushextio ( + run, XP_AWK_OUT_FILE, + ((len0 == 0)? XP_NULL: str0), &errnum); if (n2 == -1) { - if (errnum != XP_AWK_ENOERR) + if (errnum == XP_AWK_EIOHANDLER) n = -1; + else if (errnum == XP_AWK_ENOSUCHIO) + { + if (n != 0) n = -2; + } + else { xp_awk_seterrnum (run, errnum); return -1; } - n = -1; } - - n2 = xp_awk_flushextio (run, - XP_AWK_OUT_PIPE, str0, &errnum); - if (n2 == -1) - { - if (errnum != XP_AWK_ENOERR) - { - xp_awk_seterrnum (run, errnum); - return -1; - } - n = -1; - } -/* TODO: include this */ -#if 0 - n2 = xp_awk_flushextio (run, - XP_AWK_OUT_COPROC, str0, &errnum); - if (n2 == -1) - { - if (errnum != XP_AWK_ENOERR) - { - xp_awk_seterrnum (run, errnum); - return -1; - } - n = -1; - } -#endif + else if (n != -1) n = 0; } + if (((xp_awk_run_t*)run)->extio.handler[XP_AWK_EXTIO_PIPE] != XP_NULL) + { + n2 = xp_awk_flushextio ( + run, XP_AWK_OUT_PIPE, + ((len0 == 0)? XP_NULL: str0), &errnum); + if (n2 == -1) + { + if (errnum == XP_AWK_EIOHANDLER) n = -1; + else if (errnum == XP_AWK_ENOSUCHIO) + { + if (n != 0) n = -2; + } + else + { + xp_awk_seterrnum (run, errnum); + return -1; + } + } + else if (n != -1) n = 0; + } + + if (((xp_awk_run_t*)run)->extio.handler[XP_AWK_EXTIO_COPROC] != XP_NULL) + { + n2 = xp_awk_flushextio ( + run, XP_AWK_OUT_COPROC, + ((len0 == 0)? XP_NULL: str0), &errnum); + if (n2 == -1) + { + if (errnum == XP_AWK_EIOHANDLER) n = -1; + else if (errnum == XP_AWK_ENOSUCHIO) + { + if (n != 0) n = -2; + } + else + { + xp_awk_seterrnum (run, errnum); + return -1; + } + } + else if (n != -1) n = 0; + } + + /* if n remains 1, no ip handlers have been defined for + * file, pipe, and coproc. so make fflush return -1. + * if n is -2, no such named io has been found at all + * if n is -1, the io handler has returned an error */ + if (n != 0) n = -1; + if (a0->type != XP_AWK_VAL_STR) xp_free (str0); } diff --git a/ase/awk/parse.c b/ase/awk/parse.c index 6c98da56..a4dd60c0 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.165 2006-08-13 16:04:32 bacon Exp $ + * $Id: parse.c,v 1.166 2006-08-23 15:41:46 bacon Exp $ */ #include @@ -3105,6 +3105,19 @@ static xp_awk_nde_t* __parse_print (xp_awk_t* awk) out = ep->right; out_type = XP_AWK_OUT_FILE_APPEND; + xp_free (tmp); + } + else if (ep->opcode == XP_AWK_BINOP_BOR) + { + xp_awk_nde_t* tmp = args_tail; + + if (tail_prev != XP_NULL) + tail_prev->next = ep->left; + else args = ep->left; + + out = ep->right; + out_type = XP_AWK_OUT_PIPE; + xp_free (tmp); } } @@ -3135,7 +3148,7 @@ static xp_awk_nde_t* __parse_print (xp_awk_t* awk) } } - nde = (xp_awk_nde_print_t*)xp_malloc(xp_sizeof(xp_awk_nde_print_t)); + nde = (xp_awk_nde_print_t*) xp_malloc (xp_sizeof(xp_awk_nde_print_t)); if (nde == XP_NULL) { if (args != XP_NULL) xp_awk_clrpt (args); diff --git a/ase/awk/run.c b/ase/awk/run.c index a31b810c..a1fb8be9 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.174 2006-08-22 15:10:48 bacon Exp $ + * $Id: run.c,v 1.175 2006-08-23 15:41:46 bacon Exp $ */ #include @@ -735,7 +735,7 @@ static int __run_pattern_blocks (xp_awk_run_t* run) /* In case of getline, the code would make getline return -1, * set ERRNO, make this function return 0 after having checked * if closextio has returned -1 and errnum has been set to - * XP_AWK_ENOERR. But this part of the code ends the input for + * XP_AWK_EIOHANDLER. But this part of the code ends the input for * the implicit pattern-block loop, which is totally different * from getline. so it returns -1 as long as closeextio returns * -1 regardless of the value of errnum. */ @@ -745,7 +745,7 @@ static int __run_pattern_blocks (xp_awk_run_t* run) run, XP_AWK_IN_CONSOLE, XP_T(""), &errnum); if (n == -1) { - if (errnum == XP_AWK_ENOERR) + if (errnum == XP_AWK_EIOHANDLER) PANIC_I (run, XP_AWK_ECONINCLOSE); else PANIC_I (run, errnum); @@ -889,7 +889,7 @@ static int __run_block (xp_awk_run_t* run, xp_awk_nde_blk_t* nde) { xp_awk_refdownval (run, run->inrec.d0); - if (errnum == XP_AWK_ENOERR) + if (errnum == XP_AWK_EIOHANDLER) PANIC_I (run, XP_AWK_ECONOUTDATA); else PANIC_I (run, errnum); @@ -1416,7 +1416,7 @@ static int __run_nextfile (xp_awk_run_t* run, xp_awk_nde_nextfile_t* nde) run, XP_AWK_IN_CONSOLE, XP_T(""), &errnum); if (n == -1) { - if (errnum == XP_AWK_ENOERR) + if (errnum == XP_AWK_EIOHANDLER) PANIC_I (run, XP_AWK_ECONINNEXT); else PANIC_I (run, errnum); @@ -1670,14 +1670,15 @@ 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, &errnum); - if (n < 0 && errnum != XP_AWK_ENOERR) + if (n < 0 && errnum != XP_AWK_EIOHANDLER) { if (out != XP_NULL) xp_free (out); xp_awk_refdownval (run, v); PANIC_I (run, errnum); } xp_awk_refdownval (run, v); - /* TODO: how to handle n == -1 && errnum == XP_AWK_ENOERR. that is the user handler returned an error... */ + /* TODO: how to handle n == -1 && errnum == XP_AWK_EIOHANDLER. + * that is the user handler returned an error... */ } else { @@ -1693,7 +1694,7 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) n = xp_awk_writeextio ( run, p->out_type, dst, v, &errnum); - if (n < 0 && errnum != XP_AWK_ENOERR) + if (n < 0 && errnum != XP_AWK_EIOHANDLER) { if (out != XP_NULL) xp_free (out); xp_awk_refdownval (run, v); @@ -1701,7 +1702,8 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) } xp_awk_refdownval (run, v); - /* TODO: how to handle n == -1 && errnum == XP_AWK_ENOERR. that is the user handler returned an error... */ + /* TODO: how to handle n == -1 && errnum == XP_AWK_EIOHANDLER. + * that is the user handler returned an error... */ /* TODO: print proper field separator */ } @@ -1711,13 +1713,14 @@ static int __run_print (xp_awk_run_t* run, xp_awk_nde_print_t* nde) * xp_awk_val_empty_string or something */ n = xp_awk_writeextio_nl ( run, p->out_type, dst, xp_awk_val_nil, &errnum); - if (n < 0 && errnum != XP_AWK_ENOERR) + if (n < 0 && errnum != XP_AWK_EIOHANDLER) { if (out != XP_NULL) xp_free (out); PANIC_I (run, errnum); } - /* TODO: how to handle n == -1 && errnum == XP_AWK_ENOERR. that is the user handler returned an error... */ + /* TODO: how to handle n == -1 && errnum == XP_AWK_EIOHANDLER. + * that is the user handler returned an error... */ if (out != XP_NULL) xp_free (out); @@ -4372,13 +4375,13 @@ static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde) if (n < 0) { - if (errnum != XP_AWK_ENOERR) + if (errnum != XP_AWK_EIOHANDLER) { xp_str_close (&buf); PANIC (run, errnum); } - /* if errnum == XP_AWK_ENOERR, make getline return -1 */ + /* if errnum == XP_AWK_EIOHANDLER, make getline return -1 */ n = -1; } @@ -4483,7 +4486,7 @@ static int __read_record (xp_awk_run_t* run) XP_T(""), &run->inrec.line, &errnum); if (n < 0) { - if (errnum == XP_AWK_ENOERR) + if (errnum == XP_AWK_EIOHANDLER) PANIC_I (run, XP_AWK_ECONINDATA); else PANIC_I (run, errnum); diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index ffb7c6a0..a8fa722c 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.73 2006-08-22 15:11:13 bacon Exp $ + * $Id: awk.c,v 1.74 2006-08-23 15:42:16 bacon Exp $ */ #include @@ -229,17 +229,15 @@ xp_printf (XP_TEXT("closing %s of type (pipe) %d\n"), epa->name, epa->type); case XP_AWK_IO_WRITE: { - /* - if (fputs_t (data, size, (FILE*)epa->handle) == XP_NULL) - return 0; + /* TODO: size... */ + fputs_t (data, (FILE*)epa->handle); return size; - */ - return -1; } case XP_AWK_IO_FLUSH: { - return -1; + if (epa->mode == XP_AWK_IO_PIPE_READ) return -1; + else return 0; } case XP_AWK_IO_NEXT: