diff --git a/ase/cmd/tgp/tgp.c b/ase/cmd/tgp/tgp.c index bf3a7037..2e504fb3 100644 --- a/ase/cmd/tgp/tgp.c +++ b/ase/cmd/tgp/tgp.c @@ -29,74 +29,10 @@ #include #endif -#if 0 -static ase_ssize_t get_input ( - int cmd, void* arg, ase_char_t* data, ase_size_t size) -{ - switch (cmd) - { - case ASE_TGP_IO_OPEN: - case ASE_TGP_IO_CLOSE: - return 0; - - case ASE_TGP_IO_READ: - { - /* - if (ase_fgets (data, size, stdin) == ASE_NULL) - { - if (ferror(stdin)) return -1; - return 0; - } - return ase_tgp_strlen(data); - */ - - ase_cint_t c; - - if (size <= 0) return -1; - c = ase_fgetc (stdin); - - if (c == ASE_CHAR_EOF) - { - if (ferror(stdin)) return -1; - return 0; - } - - data[0] = c; - return 1; - } - } - - return -1; -} - -static ase_ssize_t put_output ( - int cmd, void* arg, ase_char_t* data, ase_size_t size) -{ - switch (cmd) - { - case ASE_TGP_IO_OPEN: - case ASE_TGP_IO_CLOSE: - return 0; - - case ASE_TGP_IO_WRITE: - { - int n = ase_fprintf ( - stdout, ASE_T("%.*s"), size, data); - if (n < 0) return -1; - - return size; - } - } - - return -1; -} - -#endif - static void print_usage (const ase_char_t* argv0) { ase_fprintf (ASE_STDERR, - ASE_T("Usage: %s [options]\n"), argv0); + ASE_T("Usage: %s [options] [file]\n"), argv0); ase_fprintf (ASE_STDERR, ASE_T(" -h print this message\n")); @@ -161,9 +97,49 @@ static int handle_args (int argc, ase_char_t* argv[]) return 0; } +struct xin_t +{ + const ase_char_t* name; + ASE_FILE* fp; +}; + +struct xout_t +{ + const ase_char_t* name; + ASE_FILE* fp; +}; + + +static int io_1 (ase_tgp_t* tgp, int cmd, ase_char_t* buf, int len) +{ + xin_t* xin = (xin_t*)arg; + + switch (cmd) + { + case ASE_IO_OPEN: + xin->fp = ase_fopen (ASE_T("abc.tgp"), ASE_T("r")); + return (xin->fp == NULL) -1: 0; + + case ASE_IO_CLOSE + ase_fclose (xin->fp); + return 0; + + case ASE_IO_READ: + ase_fgets (xin->fp); + return 0; + } + + return -1; +} + +static int io_2 (ase_tgp_t* tgp, int cmd, ase_char_t* buf, int len) +{ +} + int tgp_main (int argc, ase_char_t* argv[]) { ase_tgp_t* tgp; + int ret = 0; if (handle_args (argc, argv) == -1) return -1; @@ -175,11 +151,22 @@ int tgp_main (int argc, ase_char_t* argv[]) return -1; } - //ase_tgp_attinput (tgp, get_input, ASE_NULL); - //ase_tgp_attoutput (tgp, put_output, ASE_NULL); + ase_tgp_setstdin (tgp, io, xin); + ase_tgp_setstdout (tgp, io, ASE_NULL); + /* + ase_tgp_setexecin (tgp, io, ); + ase_tgp_setexecout (tgp, io, ); + */ + + if (ase_tgp_run (tgp) == -1) + { + ase_fprintf (ASE_STDERR, + ASE_T("Error: cannot run a tgp instance\n")); + ret = -1; + } ase_tgp_close (tgp); - return 0; + return ret; } int ase_main (int argc, ase_achar_t* argv[]) diff --git a/ase/include/ase/awk/awk.h b/ase/include/ase/awk/awk.h index 5aeae8ef..237c5616 100644 --- a/ase/include/ase/awk/awk.h +++ b/ase/include/ase/awk/awk.h @@ -1,5 +1,5 @@ /* - * $Id: awk.h 232 2008-06-28 09:38:00Z baconevi $ + * $Id: awk.h 233 2008-06-29 13:41:11Z baconevi $ * * {License} */ @@ -464,7 +464,7 @@ int ase_awk_clear (ase_awk_t* awk); /** * @function ase_awk_setassocdata - * @brief ssociats the user-specified data with an interpreter + * @brief associats the user-specified data with an interpreter */ void ase_awk_setassocdata (ase_awk_t* awk, void* data); /** diff --git a/ase/include/ase/tgp/tgp.h b/ase/include/ase/tgp/tgp.h index 654a6722..a92c249b 100644 --- a/ase/include/ase/tgp/tgp.h +++ b/ase/include/ase/tgp/tgp.h @@ -1,5 +1,5 @@ /* - * $Id: tgp.h 229 2008-06-26 10:46:39Z baconevi $ + * $Id: tgp.h 233 2008-06-29 13:41:11Z baconevi $ * * {License} */ @@ -30,6 +30,12 @@ extern "C" { ase_tgp_t* ase_tgp_open (ase_mmgr_t* mmgr); void ase_tgp_close (ase_tgp_t* tgp); +void ase_tgp_setassocdata (ase_tgp_t* tgp, void* data); +void* ase_tgp_getassocdata (ase_tgp_t* tgp); + +void ase_tgp_setio (ase_tgp_t* tgp, ase_tgp_io_t* io); +void ase_tgp_getio (ase_tgp_t* tgp, ase_tgp_io_t* io); + #ifdef __cplusplus } #endif diff --git a/ase/lib/tgp/tgp.c b/ase/lib/tgp/tgp.c index da6a78b0..df694215 100644 --- a/ase/lib/tgp/tgp.c +++ b/ase/lib/tgp/tgp.c @@ -8,8 +8,17 @@ struct ase_tgp_t { ase_mmgr_t mmgr; + void* assoc_data; int errnum; + ase_tgp_io_t* ih; + ase_tgp_io_t* oh; + ase_tgp_io_t* rh; + + void* ih_arg; + void* oh_arg; + void* rh_arg; + struct { ase_size_t pos; @@ -62,6 +71,16 @@ void ase_tgp_close (ase_tgp_t* tgp) ASE_FREE (&tgp->mmgr, tgp); } +void ase_tgp_setassocdata (ase_tgp_t* tgp, void* data) +{ + tgp->assoc_data = data; +} + +void* ase_tgp_getassocdata (ase_tgp_t* tgp) +{ + return tgp->assoc_data; +} + int ase_tgp_geterrnum (ase_tgp_t* tgp) { return tgp->errnum; @@ -73,7 +92,7 @@ static int getc (ase_tgp_t* tgp, ase_char_t* c) { ase_ssize_t n; - n = tgp->read (tgp, tgp->ib.ptr, ASE_COUNTOF(tgp->ib.ptr)); + n = tgp->ih (tgp->ih.arg, ASE_TGP_IO_READ, tgp->ib.ptr, ASE_COUNTOF(tgp->ib.ptr)); if (n < 0) return -1; else if (n == 0) { @@ -97,7 +116,8 @@ static int putc (ase_tgp_t* tgp, ase_char_t c) { ase_ssize_t n; - n = tgp->write (tgp, tgp->ob.ptr, tgp->ob.len); + /* TODO: submit on a newline as well */ + n = tgp->oh (tgp->oh.arg, ASE_TGP_IO_WRITE, tgp->ob.ptr, ASE_COUNTOF(tgp->ob.ptr)); if (n < 0) return -1; else if (n == 0) return 0; } @@ -112,9 +132,11 @@ static int runc (ase_tgp_t* tgp, ase_char_t c) { ase_ssize_t n; - n = tgp->run (tgp, tgp->rb.ptr, tgp->rb.len); + n = tgp->rh (tgp->rh.arg, ASE_TGP_IO_PUT, tgp->rb.ptr, tgp->rb.len); if (n < 0) return -1; else if (n == 0) return 0; + + tgp->rh (tgp->rh.arg, ASE_TGP_IO_GET, tgp->rb.ptr, tgp->rb.len); } tgp->rb.ptr[tgp->rb.len++] = c; @@ -132,6 +154,33 @@ int ase_tgp_run (ase_tgp_t* tgp) tgp->ob.len = 0; tgp->rb.len = 0; + n = tgp->ih.func (ASE_TGP_IO_OPEN, tgp->ih.arg, ASE_NULL, 0); + if (n == -1) + { + /* error */ + return -1; + } + if (n == 0) + { + /* reached end of input upon opening the file... */ + tgp->ih.func (ASE_TGP_IO_CLOSE, tgp->ih.arg, ASE_NULL, 0); + return 0; + } + + n = tgp->oh.func (ASE_TGP_IO_OPEN, tgp->oh.arg, ASE_NULL, 0); + if (n == -1) + { + tgp->ih.func (ASE_TGP_IO_CLOSE, tgp->ih.arg, ASE_NULL, 0); + return -1; + } + if (n == 0) + { + /* reached end of input upon opening the file... */ + tgp->oh.func (ASE_TGP_IO_CLOSE, tgp->oh.arg, ASE_NULL, 0); + tgp->ih.func (ASE_TGP_IO_CLOSE, tgp->ih.arg, ASE_NULL, 0); + return 0; + } + while (1) { n = getc (tgp, &c); @@ -198,5 +247,43 @@ int ase_tgp_run (ase_tgp_t* tgp) } } + tgp->oh.func (ASE_TGP_IO_CLOSE, tgp->oh.arg, ASE_NULL, 0); + tgp->ih.func (ASE_TGP_IO_CLOSE, tgp->ih.arg, ASE_NULL, 0); return 0; } + +void ase_tgp_attachin (ase_tgp_t* tgp, ase_tgp_io_t* io, void* arg) +{ + tgp->ih = io; + tgp->ih_arg = arg; +} + +void ase_tgp_detachin (ase_tgp_t* tgp) +{ + tgp->ih = ASE_NULL; + tgp->ih_arg = ASE_NULL; +} + +void ase_tgp_attachout (ase_tgp_t* tgp, ase_tgp_io_t* io) +{ + tgp->oh = io; + tgp->oh_arg = arg; +} + +void ase_tgp_detachout (ase_tgp_t* tgp) +{ + tgp->oh = ASE_NULL; + tgp->oh_arg = ASE_NULL; +} + +void ase_tgp_attachin (ase_tgp_t* tgp, ase_tgp_io_t* io) +{ + tgp->rh = io; + tgp->rh_arg = arg; +} + +void ase_tgp_detachin (ase_tgp_t* tgp) +{ + tgp->rh = ASE_NULL; + tgp->rh_arg = ASE_NULL; +}