diff --git a/ase/awk/awk.c b/ase/awk/awk.c index 7c7300b1..3bc53fcb 100644 --- a/ase/awk/awk.c +++ b/ase/awk/awk.c @@ -1,5 +1,5 @@ /* - * $Id: awk.c,v 1.51 2006-06-18 13:43:28 bacon Exp $ + * $Id: awk.c,v 1.52 2006-06-19 04:38:51 bacon Exp $ */ #include @@ -224,3 +224,12 @@ xp_size_t xp_awk_getsrcline (xp_awk_t* awk) { return awk->token.line; } + + +/* TODO: redo it */ +int xp_awk_setextio (xp_awk_t* awk, xp_awk_io_t io, void* arg) +{ + awk->extio.pipe = io; + return 0; +} + diff --git a/ase/awk/awk.h b/ase/awk/awk.h index 0567d074..8c776ecc 100644 --- a/ase/awk/awk.h +++ b/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h,v 1.64 2006-06-18 11:18:49 bacon Exp $ + * $Id: awk.h,v 1.65 2006-06-19 04:38:51 bacon Exp $ */ #ifndef _XP_AWK_AWK_H_ @@ -13,6 +13,15 @@ typedef struct xp_awk_t xp_awk_t; typedef xp_ssize_t (*xp_awk_io_t) ( int cmd, void* arg, xp_char_t* data, xp_size_t count); +typedef struct xp_awk_cmd_t xp_awk_cmd_t; + +struct xp_awk_cmd_t +{ + xp_char_t* name; + void* handle; + xp_awk_cmd_t* next; +}; + /* io function commands */ enum { @@ -123,6 +132,8 @@ void xp_awk_setrunopt (xp_awk_t* awk, int opt); int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg); int xp_awk_detsrc (xp_awk_t* awk); +int xp_awk_setextio (xp_awk_t* awk, xp_awk_io_t io, void* arg); + xp_size_t xp_awk_getsrcline (xp_awk_t* awk); /* TODO: xp_awk_parse (xp_awk_t* awk, xp_awk_io_t src, void* arg)??? */ int xp_awk_parse (xp_awk_t* awk); diff --git a/ase/awk/awk_i.h b/ase/awk/awk_i.h index 3ebf259f..3438d3ef 100644 --- a/ase/awk/awk_i.h +++ b/ase/awk/awk_i.h @@ -1,5 +1,5 @@ /* - * $Id: awk_i.h,v 1.15 2006-06-18 13:43:28 bacon Exp $ + * $Id: awk_i.h,v 1.16 2006-06-19 04:38:51 bacon Exp $ */ #ifndef _XP_AWK_AWKI_H_ @@ -8,7 +8,6 @@ typedef struct xp_awk_chain_t xp_awk_chain_t; typedef struct xp_awk_run_t xp_awk_run_t; typedef struct xp_awk_tree_t xp_awk_tree_t; -typedef struct xp_awk_cmd_t xp_awk_cmd_t; #include #include @@ -177,15 +176,10 @@ struct xp_awk_run_t int opt; int errnum; - xp_awk_tree_t* tree; - xp_size_t nglobals; -}; -struct xp_awk_cmd_t -{ - xp_char_t* name; - void* handle; - xp_awk_cmd_t* next; + xp_awk_tree_t* tree; + /*xp_size_t nglobals;*/ + xp_awk_t* awk; }; #endif diff --git a/ase/awk/extio.c b/ase/awk/extio.c index 2469cb14..fadcb933 100644 --- a/ase/awk/extio.c +++ b/ase/awk/extio.c @@ -1,5 +1,5 @@ /* - * $Id: extio.c,v 1.3 2006-06-18 13:43:28 bacon Exp $ + * $Id: extio.c,v 1.4 2006-06-19 04:38:51 bacon Exp $ */ #include @@ -48,11 +48,9 @@ int xp_awk_readcmd (xp_awk_run_t* run, const xp_char_t* cmd, int* errnum) if (p->handle == XP_NULL) { - xp_awk_io_t pipe_io = run->awk.extio.pipe; + xp_awk_io_t pipe_io = run->awk->extio.pipe; - //p->handle = (void*) _tpopen (p->name, XP_T("r")); - p->handle = pipe_io (XP_AWK_INPUT_OPEN, p->name, XP_NULL, 0); - if (p->handle == NULL) + if (pipe_io (XP_AWK_INPUT_OPEN, p, XP_NULL, 0) == -1) { /* this is treated as pipe open error. * the return value of getline should be -1 @@ -60,12 +58,28 @@ int xp_awk_readcmd (xp_awk_run_t* run, const xp_char_t* cmd, int* errnum) */ return -1; } + + if (p->handle == XP_NULL) + { + /* TODO: break the chain ... */ + + + /* *errnum = XP_AWK_EEXTIO; external io handler error */ + *errnum = XP_AWK_EINTERNAL; + return -1; + } } { - xp_char_t buf[1024]; - if (_fgetts (buf, xp_countof(buf), p->handle) == XP_NULL) return 0; - xp_printf(XP_TEXT("%s"), buf); +xp_char_t buf[1024]; +xp_awk_io_t pipe_io = run->awk->extio.pipe; + + if (pipe_io (XP_AWK_INPUT_DATA, p, buf, xp_countof(buf)) == 0) + { + return 0; + } + +xp_printf(XP_TEXT("%s"), buf); } return 1; @@ -79,9 +93,16 @@ int xp_awk_closecmd (xp_awk_run_t* run, const xp_char_t* cmd, int* errnum) { if (xp_strcmp(p->name,cmd) == 0) { - fclose ((FILE*)p->handle); - p->handle = XP_NULL; + xp_awk_io_t pipe_io = run->awk->extio.pipe; + if (pipe_io (XP_AWK_INPUT_CLOSE, p, XP_NULL, 0) == -1) + { + /* TODO: how to handle this... */ + p->handle = XP_NULL; + return -1; + } + + p->handle = XP_NULL; //if (opt_remove_closed_cmd) //{ if (px != XP_NULL) px->next = p->next; @@ -90,6 +111,7 @@ int xp_awk_closecmd (xp_awk_run_t* run, const xp_char_t* cmd, int* errnum) xp_free (p->name); xp_free (p); //} + return 0; } px = p; diff --git a/ase/awk/run.c b/ase/awk/run.c index 05b4f0c5..cbd912d3 100644 --- a/ase/awk/run.c +++ b/ase/awk/run.c @@ -1,5 +1,5 @@ /* - * $Id: run.c,v 1.97 2006-06-18 10:53:06 bacon Exp $ + * $Id: run.c,v 1.98 2006-06-19 04:38:51 bacon Exp $ */ #include @@ -21,7 +21,8 @@ #define STACK_LOCAL(run,n) STACK_AT(run,3+(xp_size_t)STACK_NARGS(run)+1+(n)) #define STACK_RETVAL(run) STACK_AT(run,2) #define STACK_GLOBAL(run,n) ((run)->stack[(n)]) -#define STACK_RETVAL_GLOBAL(run) ((run)->stack[(run)->nglobals+2]) +/*#define STACK_RETVAL_GLOBAL(run) ((run)->stack[(run)->nglobals+2])*/ +#define STACK_RETVAL_GLOBAL(run) ((run)->stack[(run)->tree->nglobals+2]) #define EXIT_NONE 0 #define EXIT_BREAK 1 @@ -212,7 +213,8 @@ static int __open_run ( run->opt = awk->opt.run; run->errnum = XP_AWK_ENOERR; run->tree = &awk->tree; - run->nglobals = awk->tree.nglobals; + /*run->nglobals = awk->tree.nglobals;*/ + run->awk = awk; run->input.buf_pos = 0; run->input.buf_len = 0; @@ -284,7 +286,9 @@ static int __run_main (xp_awk_run_t* run) /* secure space for global variables */ saved_stack_top = run->stack_top; - nglobals = run->nglobals; + /*nglobals = run->nglobals;*/ + nglobals = run->tree->nglobals; + while (nglobals > 0) { --nglobals; @@ -337,14 +341,16 @@ static int __run_main (xp_awk_run_t* run) /* restore the stack top in a cheesy(?) way */ run->stack_top = saved_stack_top; /* pops off global variables in a decent way */ - __raw_pop_times (run, run->nglobals); + /*__raw_pop_times (run, run->nglobals);*/ + __raw_pop_times (run, run->tree->nglobals); PANIC_I (run, XP_AWK_ENOMEM); } if (__raw_push(run,(void*)saved_stack_top) == -1) { run->stack_top = saved_stack_top; - __raw_pop_times (run, run->nglobals); + /*__raw_pop_times (run, run->nglobals);*/ + __raw_pop_times (run, run->tree->nglobals); PANIC_I (run, XP_AWK_ENOMEM); } @@ -352,7 +358,8 @@ static int __run_main (xp_awk_run_t* run) if (__raw_push(run,xp_awk_val_nil) == -1) { run->stack_top = saved_stack_top; - __raw_pop_times (run, run->nglobals); + /*__raw_pop_times (run, run->nglobals);*/ + __raw_pop_times (run, run->tree->nglobals); PANIC_I (run, XP_AWK_ENOMEM); } @@ -360,7 +367,8 @@ static int __run_main (xp_awk_run_t* run) if (__raw_push(run,xp_awk_val_nil) == -1) { run->stack_top = saved_stack_top; - __raw_pop_times (run, run->nglobals); + /*__raw_pop_times (run, run->nglobals);*/ + __raw_pop_times (run, run->tree->nglobals); PANIC_I (run, XP_AWK_ENOMEM); } @@ -420,7 +428,7 @@ xp_printf (XP_T("\n")); } /* pops off the global variables */ - nglobals = run->nglobals; + nglobals = run->tree->nglobals; /*run->nglobals */ while (nglobals > 0) { --nglobals; diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index eccc06e4..8959861c 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -1,11 +1,11 @@ /* - * $Id: awk.c,v 1.36 2006-06-18 13:43:28 bacon Exp $ + * $Id: awk.c,v 1.37 2006-06-19 04:38:51 bacon Exp $ */ #include #include #include -#ifdef XP_CHAR_IS_CHAR +#ifdef XP_CHAR_IS_WCHAR #include #endif @@ -14,11 +14,17 @@ #include #endif +#ifdef _WIN32 +#include +#endif + #ifdef __STAND_ALONE #define xp_printf xp_awk_printf extern int xp_awk_printf (const xp_char_t* fmt, ...); #define xp_strcmp xp_awk_strcmp extern int xp_awk_strcmp (const xp_char_t* s1, const xp_char_t* s2); + #define xp_strlen xp_awk_strlen + extern int xp_awk_strlen (const xp_char_t* s); #endif #if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG) @@ -128,6 +134,52 @@ static xp_ssize_t process_data ( return -1; } +static xp_ssize_t process_extio_pipe ( + int cmd, void* arg, xp_char_t* data, xp_size_t size) +{ + xp_awk_cmd_t* epa = (xp_awk_cmd_t*)arg; + + switch (cmd) + { + case XP_AWK_INPUT_OPEN: + { + FILE* handle; + handle = _tpopen (epa->name, XP_T("r")); + if (handle == NULL) return -1; + epa->handle = (void*)handle; + return 0; + } + + case XP_AWK_INPUT_CLOSE: + { + fclose ((FILE*)epa->handle); + epa->handle = NULL; + return 0; + } + + case XP_AWK_INPUT_DATA: + { + if (_fgetts (data, size, epa->handle) == XP_NULL) + return 0; + return xp_strlen(data); + } + + case XP_AWK_INPUT_NEXT: + { + return -1; + } + + case XP_AWK_OUTPUT_OPEN: + case XP_AWK_OUTPUT_CLOSE: + case XP_AWK_OUTPUT_DATA: + case XP_AWK_OUTPUT_NEXT: + { + return -1; + } + } + +} + #if defined(__STAND_ALONE) && !defined(_WIN32) static int __main (int argc, char* argv[]) #else @@ -150,6 +202,14 @@ static int __main (int argc, xp_char_t* argv[]) return -1; } +/* TODO: */ + if (xp_awk_setextio (awk, process_extio_pipe, XP_NULL) == -1) + { + xp_awk_close (awk); + xp_printf (XP_T("Error: cannot attach source\n")); + return -1; + } + xp_awk_setparseopt (awk, XP_AWK_EXPLICIT | XP_AWK_UNIQUE | XP_AWK_SHADING | XP_AWK_IMPLICIT | XP_AWK_SHIFT | XP_AWK_EXTIO);