diff --git a/ase/awk/extio.c b/ase/awk/extio.c index cd2ec74a..d60a8d8c 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.15 2006-06-28 14:19:01 bacon Exp $ + * $Id: extio.c,v 1.16 2006-06-29 14:38:01 bacon Exp $ */ #include @@ -10,13 +10,21 @@ #include #endif +enum +{ + __MASK_READ = 0x01FF, + __MASK_WRITE = 0x02FF, + __MASK_RDWR = 0x04FF, + __MASK_CLEAR = 0x00FF +}; + int xp_awk_readextio ( xp_awk_run_t* run, int in_type, const xp_char_t* name, xp_str_t* buf, int* errnum) { xp_awk_extio_t* p = run->extio; xp_awk_io_t handler; - int extio_type, extio_opt; + int extio_type, extio_opt, extio_mask; static int __in_type_map[] = { @@ -24,7 +32,8 @@ int xp_awk_readextio ( * XP_AWK_IN_XXX values in tree.h */ XP_AWK_EXTIO_PIPE, XP_AWK_EXTIO_COPROC, - XP_AWK_EXTIO_FILE + XP_AWK_EXTIO_FILE, + XP_AWK_EXTIO_CONSOLE }; static int __in_opt_map[] = @@ -33,15 +42,26 @@ int xp_awk_readextio ( * XP_AWK_IN_XXX values in tree.h */ XP_AWK_IO_PIPE_READ, 0, + XP_AWK_IO_FILE_READ, XP_AWK_IO_FILE_READ }; + static int __in_mask_map[] = + { + __MASK_READ, + __MASK_RDWR, + __MASK_READ, + __MASK_READ + }; + xp_assert (in_type >= 0 && in_type <= xp_countof(__in_type_map)); xp_assert (in_type >= 0 && in_type <= xp_countof(__in_opt_map)); + xp_assert (in_type >= 0 && in_type <= xp_countof(__in_mask_map)); - /* translate the out_type into the relevant extio type and option */ + /* translate the in_type into the relevant extio type and option */ extio_type = __in_type_map[in_type]; extio_opt = __in_opt_map[in_type]; + extio_mask = __in_mask_map[in_type]; handler = run->awk->extio[extio_type]; if (handler == XP_NULL) @@ -53,7 +73,7 @@ int xp_awk_readextio ( while (p != XP_NULL) { - if (p->type == in_type && + if (p->type == (extio_type | extio_mask) && xp_strcmp(p->name,name) == 0) break; p = p->next; } @@ -75,7 +95,7 @@ int xp_awk_readextio ( return -1; } - p->type = in_type; + p->type = (extio_type | extio_mask); p->handle = XP_NULL; p->in.buf[0] = XP_C('\0'); @@ -170,7 +190,7 @@ int xp_awk_writeextio ( xp_awk_extio_t* p = run->extio; xp_awk_io_t handler; xp_str_t buf; - int extio_type, extio_opt; + int extio_type, extio_opt, extio_mask; static int __out_type_map[] = { @@ -194,12 +214,23 @@ int xp_awk_writeextio ( XP_AWK_IO_CONSOLE_WRITE }; + static int __out_mask_map[] = + { + __MASK_WRITE, + __MASK_RDWR, + __MASK_WRITE, + __MASK_WRITE, + __MASK_WRITE + }; + xp_assert (out_type >= 0 && out_type <= xp_countof(__out_type_map)); xp_assert (out_type >= 0 && out_type <= xp_countof(__out_opt_map)); + xp_assert (out_type >= 0 && out_type <= xp_countof(__out_mask_map)); /* translate the out_type into the relevant extio type and option */ extio_type = __out_type_map[out_type]; extio_opt = __out_opt_map[out_type]; + extio_mask = __out_mask_map[out_type]; handler = run->awk->extio[extio_type]; if (handler == XP_NULL) @@ -237,7 +268,7 @@ int xp_awk_writeextio ( * print "1111" > "1.tmp" */ - if (p->type == extio_type && + if (p->type == (extio_type | extio_mask) && xp_strcmp(p->name,name) == 0) break; p = p->next; } @@ -262,7 +293,7 @@ int xp_awk_writeextio ( return -1; } - p->type = extio_type; + p->type = (extio_type | extio_mask); p->handle = XP_NULL; p->next = XP_NULL; @@ -316,8 +347,9 @@ int xp_awk_closeextio (xp_awk_run_t* run, const xp_char_t* name, int* errnum) * regardless of the extio type */ if (xp_strcmp(p->name,name) == 0) { - xp_awk_io_t handler = run->awk->extio[p->type]; - + xp_awk_io_t handler; + + handler = run->awk->extio[p->type & __MASK_CLEAR]; if (handler != NULL) { /* TODO: io command should not be XP_AWK_IO_CLOSE @@ -355,7 +387,7 @@ void xp_awk_clearextio (xp_awk_run_t* run) while (run->extio != XP_NULL) { - handler = run->awk->extio[run->extio->type]; + handler = run->awk->extio[run->extio->type & __MASK_CLEAR]; next = run->extio->next; if (handler != XP_NULL) diff --git a/ase/awk/parse.c b/ase/awk/parse.c index c6e3cbac..73f90915 100644 --- a/ase/awk/parse.c +++ b/ase/awk/parse.c @@ -1,5 +1,5 @@ /* - * $Id: parse.c,v 1.127 2006-06-28 10:40:24 bacon Exp $ + * $Id: parse.c,v 1.128 2006-06-29 14:38:01 bacon Exp $ */ #include @@ -1142,7 +1142,11 @@ static xp_awk_nde_t* __parse_expression (xp_awk_t* awk) if (x == XP_NULL) return XP_NULL; opcode = __assign_to_opcode (awk); - if (opcode == -1) return x; + if (opcode == -1) + { + /* no assignment operator found. */ + return x; + } xp_assert (x->next == XP_NULL); if (!__is_var(x) && x->type != XP_AWK_NDE_POS) @@ -2009,7 +2013,8 @@ static xp_awk_nde_t* __parse_primary (xp_awk_t* awk) nde->type = XP_AWK_NDE_GETLINE; nde->next = XP_NULL; nde->var = var; - nde->in_type = XP_AWK_IN_FILE; + nde->in_type = (in == XP_NULL)? + XP_AWK_IN_CONSOLE: XP_AWK_IN_FILE; nde->in = in; return (xp_awk_nde_t*)nde; diff --git a/ase/awk/run.c b/ase/awk/run.c index bd87e443..e96d36bf 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.113 2006-06-28 14:19:01 bacon Exp $ + * $Id: run.c,v 1.114 2006-06-29 14:38:01 bacon Exp $ */ #include @@ -3066,39 +3066,45 @@ static xp_awk_val_t* __eval_pos (xp_awk_run_t* run, xp_awk_nde_t* nde) static xp_awk_val_t* __eval_getline (xp_awk_run_t* run, xp_awk_nde_t* nde) { xp_awk_nde_getline_t* p; - xp_awk_val_t* in, * res; - xp_char_t* str; + xp_awk_val_t* v, * res; + xp_char_t* in = XP_NULL; + const xp_char_t* dst; xp_str_t buf; int errnum, n; p = (xp_awk_nde_getline_t*)nde; - xp_assert (p->in_type == XP_AWK_IN_PIPE || - p->in_type == XP_AWK_IN_COPROC || - p->in_type == XP_AWK_IN_FILE); - xp_assert (p->in != XP_NULL); + xp_assert ((p->in_type == XP_AWK_IN_PIPE && p->in != XP_NULL) || + (p->in_type == XP_AWK_IN_COPROC && p->in != XP_NULL) || + (p->in_type == XP_AWK_IN_FILE && p->in != XP_NULL) || + (p->in_type == XP_AWK_IN_CONSOLE && p->in == XP_NULL)); - in = __eval_expression (run, p->in); - if (in == XP_NULL) return XP_NULL; - - xp_awk_refupval (in); - str = xp_awk_valtostr (in, &errnum, XP_NULL); - if (str == XP_NULL) + if (p->in != XP_NULL) { - xp_awk_refdownval (run, in); - PANIC (run, errnum); + v = __eval_expression (run, p->in); + if (v == XP_NULL) return XP_NULL; + + xp_awk_refupval (v); + in = xp_awk_valtostr (v, &errnum, XP_NULL); + if (in == XP_NULL) + { + xp_awk_refdownval (run, v); + PANIC (run, errnum); + } + xp_awk_refdownval (run, v); } - xp_awk_refdownval (run, in); + + dst = (in == XP_NULL)? XP_T(""): in; /* TODO: optimization in line buffer management */ if (xp_str_open (&buf, 256) == XP_NULL) { - xp_free (str); + if (in != XP_NULL) xp_free (in); PANIC (run, XP_AWK_ENOMEM); } - n = xp_awk_readextio (run, p->in_type, str, &buf, &errnum); - xp_free (str); + n = xp_awk_readextio (run, p->in_type, dst, &buf, &errnum); + if (in != XP_NULL) xp_free (in); if (n < 0 && errnum != XP_AWK_ENOERR) { diff --git a/ase/awk/tree.c b/ase/awk/tree.c index 13a64cec..852c5300 100644 --- a/ase/awk/tree.c +++ b/ase/awk/tree.c @@ -1,5 +1,5 @@ /* - * $Id: tree.c,v 1.59 2006-06-28 10:40:24 bacon Exp $ + * $Id: tree.c,v 1.60 2006-06-29 14:38:01 bacon Exp $ */ #include @@ -72,7 +72,8 @@ static const xp_char_t* __getline_inop_str[] = { XP_T("|"), XP_T("|&"), - XP_T("<") + XP_T("<"), + XP_T("") }; static const xp_char_t* __print_outop_str[] = diff --git a/ase/awk/tree.h b/ase/awk/tree.h index 26aa43c0..f1eab2b3 100644 --- a/ase/awk/tree.h +++ b/ase/awk/tree.h @@ -1,5 +1,5 @@ /* - * $Id: tree.h,v 1.54 2006-06-28 14:19:01 bacon Exp $ + * $Id: tree.h,v 1.55 2006-06-29 14:38:01 bacon Exp $ */ #ifndef _XP_AWK_TREE_H_ @@ -65,7 +65,8 @@ enum XP_AWK_IN_PIPE, XP_AWK_IN_COPROC, - XP_AWK_IN_FILE + XP_AWK_IN_FILE, + XP_AWK_IN_CONSOLE }; enum diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index a28a46b5..958af074 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.45 2006-06-28 08:56:59 bacon Exp $ + * $Id: awk.c,v 1.46 2006-06-29 14:38:01 bacon Exp $ */ #include @@ -36,15 +36,41 @@ #include #endif +struct src_io +{ + const xp_char_t* input_file; + FILE* input_handle; +}; + +struct data_io +{ + const char* input_file; + FILE* input_handle; +}; + static xp_ssize_t process_source ( int cmd, int opt, void* arg, xp_char_t* data, xp_size_t size) { + struct src_io* src_io = (struct src_io*)arg; xp_char_t c; switch (cmd) { case XP_AWK_IO_OPEN: + { + if (src_io->input_file == XP_NULL) return 0; + src_io->input_handle = _tfopen (src_io->input_file, _T("r")); + if (src_io->input_handle == NULL) return -1; + return 0; + } + case XP_AWK_IO_CLOSE: + { + if (src_io->input_file == XP_NULL) return 0; + fclose ((FILE*)src_io->input_handle); + return 0; + } + case XP_AWK_IO_NEXT: { return 0; @@ -54,9 +80,9 @@ static xp_ssize_t process_source ( { if (size <= 0) return -1; #ifdef XP_CHAR_IS_MCHAR - c = fgetc (stdin); + c = fgetc ((FILE*)src_io->input_handle); #else - c = fgetwc (stdin); + c = fgetwc ((FILE*)src_io->input_handle); #endif if (c == XP_CHAR_EOF) return 0; *data = c; @@ -65,6 +91,7 @@ static xp_ssize_t process_source ( case XP_AWK_IO_WRITE: { + xp_printf (XP_T("XP_AWK_IO_WRITE CALLED FOR SOURCE\n")); return -1; } } @@ -73,11 +100,6 @@ static xp_ssize_t process_source ( } -struct data_io -{ - const char* input_file; - FILE* input_handle; -}; static xp_ssize_t process_data ( int cmd, int opt, void* arg, xp_char_t* data, xp_size_t size) @@ -260,25 +282,30 @@ static xp_ssize_t process_extio_console ( /* opt: XP_AWK_IO_CONSOLE_READ, * XP_AWK_IO_CONSOLE_WRITE */ -xp_printf (XP_TEXT("opending %s of type %d (console)\n"), epa->name, epa->type); +xp_printf (XP_TEXT("opening [%s] of type %d (console)\n"), epa->name, epa->type); if (opt == XP_AWK_IO_CONSOLE_READ) epa->handle = stdin; else if (opt == XP_AWK_IO_CONSOLE_WRITE) epa->handle = stdout; else return -1; + + return 0; } case XP_AWK_IO_CLOSE: { -xp_printf (XP_TEXT("closing %s of type %d (console)\n"), epa->name, epa->type); +xp_printf (XP_TEXT("closing [%s] of type %d (console)\n"), epa->name, epa->type); /* TODO: CloseConsole in GUI APPLICATION */ return 0; } case XP_AWK_IO_READ: { - if (_fgetts (data, size, (FILE*)epa->handle) == XP_NULL) + if (_fgetts (data, size, (FILE*)epa->handle) == XP_NULL) + //if (_fgetts (data, size, stdin) == XP_NULL) + { return 0; + } return xp_strlen(data); } @@ -286,6 +313,7 @@ xp_printf (XP_TEXT("closing %s of type %d (console)\n"), epa->name, epa->type); { /* TODO: how to return error or 0 */ _fputts (data, /*size,*/ (FILE*)epa->handle); + //_fputts (data, /*size,*/ stdout); return size; } @@ -307,6 +335,7 @@ static int __main (int argc, xp_char_t* argv[]) { xp_awk_t* awk; struct data_io data_io = { "awk.in", NULL }; + struct src_io src_io = { NULL, NULL }; if ((awk = xp_awk_open()) == XP_NULL) { @@ -314,13 +343,6 @@ static int __main (int argc, xp_char_t* argv[]) return -1; } - if (xp_awk_attsrc(awk, process_source, XP_NULL) == -1) - { - xp_awk_close (awk); - xp_printf (XP_T("Error: cannot attach source\n")); - return -1; - } - /* TODO: */ if (xp_awk_setextio (awk, XP_AWK_EXTIO_PIPE, process_extio_pipe, XP_NULL) == -1) @@ -357,10 +379,46 @@ static int __main (int argc, xp_char_t* argv[]) if (strcmp(argv[1], "-m") == 0) #else if (xp_strcmp(argv[1], XP_T("-m")) == 0) +#endif + { + xp_awk_close (awk); + xp_printf (XP_T("Usage: %s [-m] source_file\n"), argv[0]); + return -1; + } + + src_io.input_file = argv[1]; + } + else if (argc == 3) + { +#if defined(__STAND_ALONE) && !defined(_WIN32) + if (strcmp(argv[1], "-m") == 0) +#else + if (xp_strcmp(argv[1], XP_T("-m")) == 0) #endif { xp_awk_setrunopt (awk, XP_AWK_RUNMAIN); } + else + { + xp_awk_close (awk); + xp_printf (XP_T("Usage: %s [-m] source_file\n"), argv[0]); + return -1; + } + + src_io.input_file = argv[2]; + } + else + { + xp_awk_close (awk); + xp_printf (XP_T("Usage: %s [-m] source_file\n"), argv[0]); + return -1; + } + + if (xp_awk_attsrc(awk, process_source, (void*)&src_io) == -1) + { + xp_awk_close (awk); + xp_printf (XP_T("Error: cannot attach source\n")); + return -1; } if (xp_awk_parse(awk) == -1) diff --git a/ase/test/awk/t12.awk b/ase/test/awk/t12.awk index 32646da0..d9eb2e21 100644 --- a/ase/test/awk/t12.awk +++ b/ase/test/awk/t12.awk @@ -1,5 +1,6 @@ BEGIN { + /* print "line 1" >> "1"; print "line 2" > "1"; print "line 3" >> "1"; @@ -16,5 +17,16 @@ BEGIN getline x < "abc"; if (x == "a") print "xxxxxxxxxxxxxxxx"; else print x; - + + */ + + /* + print getline x; + print "[", x, "]"; + print "--------------"; + */ + + getline x < "abc"; + print x > "abc"; + }