From 2bd78d2bfc1b7eb6cde45781ecda3c0d290e7d55 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 19 May 2023 00:16:51 +0900 Subject: [PATCH] added hcl_attachiostdwithucstr(). updated hcl_readbaseinraw() --- bin/main.c | 14 +++++++------- lib/hcl.h | 23 +++++++++++++---------- lib/read.c | 17 ++++++++++------- lib/std.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 78 insertions(+), 27 deletions(-) diff --git a/bin/main.c b/bin/main.c index 373107f..5a1910b 100644 --- a/bin/main.c +++ b/bin/main.c @@ -513,8 +513,6 @@ static int on_fed_cnode_in_interactive_mode (hcl_t* hcl, hcl_cnode_t* obj) static int feed_loop (hcl_t* hcl, xtn_t* xtn, int cflags, int verbose) { - hcl_ioinarg_t* inarg; - /* override the default cnode handler. the default one simply * compiles the expression node without execution */ if (hcl_beginfeed (hcl, hcl_isstdreadertty(hcl)? on_fed_cnode_in_interactive_mode: HCL_NULL) <= -1) @@ -524,13 +522,15 @@ static int feed_loop (hcl_t* hcl, xtn_t* xtn, int cflags, int verbose) } /* [NOTE] it isn't a very nice idea to get this internal data and use it with read_input() */ - inarg = hcl_getbaseinarg(hcl); while (1) { - //if (read_input(hcl, inarg) <= -1) goto oops; - if (hcl_readbaseinraw(hcl) <= -1) goto oops; - if (inarg->xlen <= 0) break; - if (hcl_feed(hcl, inarg->buf, inarg->xlen) <= -1) goto feed_error; + hcl_ooch_t* ptr; + hcl_oow_t xlen; + + ptr = hcl_readbaseinraw(hcl, &xlen); + if (HCL_UNLIKELY(!ptr)) goto oops; + if (xlen <= 0) break; + if (hcl_feed(hcl, ptr, xlen) <= -1) goto feed_error; } if (hcl_endfeed(hcl) <= -1) { diff --git a/lib/hcl.h b/lib/hcl.h index 8658687..b205c5c 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -2165,16 +2165,6 @@ HCL_EXPORT void hcl_abort ( # define hcl_switchprocess(hcl) ((hcl)->switch_proc = 1) #endif -/* TODO: don't expose hcl_getbaseinarg(), and hcl_readbaseinraw() - * find a better way not to use them */ -HCL_EXPORT hcl_ioinarg_t* hcl_getbaseinarg ( - hcl_t* hcl -); - -HCL_EXPORT int hcl_readbaseinraw ( - hcl_t* hcl -); - HCL_EXPORT void hcl_setbaseinloc ( hcl_t* hcl, hcl_oow_t line, @@ -2187,6 +2177,13 @@ HCL_EXPORT hcl_iolxc_t* hcl_readbaseinchar ( hcl_t* hcl ); +/* TODO: don't expose hcl_readbaseinraw() + * find a better way not to use them */ +HCL_EXPORT hcl_ooch_t* hcl_readbaseinraw ( + hcl_t* hcl, + hcl_oow_t* xlen +); + HCL_EXPORT int hcl_attachio ( hcl_t* hcl, hcl_ioimpl_t reader, @@ -2199,6 +2196,12 @@ HCL_EXPORT int hcl_attachiostdwithbcstr ( const hcl_bch_t* print_file ); +HCL_EXPORT int hcl_attachiostdwithucstr ( + hcl_t* hcl, + const hcl_uch_t* read_file, + const hcl_uch_t* print_file +); + HCL_EXPORT int hcl_isstdreadertty ( hcl_t* hcl ); diff --git a/lib/read.c b/lib/read.c index 38bc4ea..4f3363a 100644 --- a/lib/read.c +++ b/lib/read.c @@ -4101,11 +4101,6 @@ void hcl_detachio (hcl_t* hcl) } } -hcl_ioinarg_t* hcl_getbaseinarg (hcl_t* hcl) -{ - return &hcl->c->inarg; -} - void hcl_setbaseinloc (hcl_t* hcl, hcl_oow_t line, hcl_oow_t colm) { hcl->c->inarg.line = line; @@ -4123,8 +4118,16 @@ hcl_iolxc_t* hcl_readbaseinchar (hcl_t* hcl) } -int hcl_readbaseinraw (hcl_t* hcl) +hcl_ooch_t* hcl_readbaseinraw (hcl_t* hcl, hcl_oow_t* xlen) { - return hcl->c->reader(hcl, HCL_IO_READ, &hcl->c->inarg); + /* this function provides the raw input interface to the attached source + * input handler. it doesn't increment line/column number, nor does it + * care about ungot characters. it must be used with extra care */ + + HCL_ASSERT (hcl, hcl->c != HCL_NULL); // call hio_attachio() or hio_attachiostd() first + + if (hcl->c->reader(hcl, HCL_IO_READ, &hcl->c->inarg) <= -1) return HCL_NULL; + *xlen = hcl->c->inarg.xlen; + return hcl->c->inarg.buf; } diff --git a/lib/std.c b/lib/std.c index 651c315..22c7442 100644 --- a/lib/std.c +++ b/lib/std.c @@ -280,8 +280,13 @@ struct xtn_t hcl_t* next; hcl_t* prev; + /* hcl_attachiostdwithbcstr() and hcl_attachiostdwithucstr() + * set these two field and reset them at the end. + * since hcl_attachio() callls the open handler, these fields + * are valid only inside the open handelr */ const char* read_path; /* main source file */ - const char* print_path; + const char* print_path; /* runtime output file */ + int reader_istty; int vm_running; @@ -3494,9 +3499,50 @@ static int print_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg) int hcl_attachiostdwithbcstr (hcl_t* hcl, const hcl_bch_t* read_file, const hcl_bch_t* print_file) { xtn_t* xtn = GET_XTN(hcl); + int n; + + HCL_ASSERT (hcl, xtn->read_path == HCL_NULL); + HCL_ASSERT (hcl, xtn->print_path == HCL_NULL); + xtn->read_path = read_file; xtn->print_path = print_file; - return hcl_attachio(hcl, read_handler, print_handler); + + n = hcl_attachio(hcl, read_handler, print_handler); + + xtn->read_path = HCL_NULL; + xtn->print_path = HCL_NULL; + + return n; +} + +int hcl_attachiostdwithucstr (hcl_t* hcl, const hcl_uch_t* read_file, const hcl_uch_t* print_file) +{ + xtn_t* xtn = GET_XTN(hcl); + int n; + + HCL_ASSERT (hcl, xtn->read_path == HCL_NULL); + HCL_ASSERT (hcl, xtn->print_path == HCL_NULL); + + xtn->read_path = hcl_duputobcstr(hcl, read_file, HCL_NULL); + if (HCL_UNLIKELY(!xtn->read_path)) return -1; + + xtn->print_path = hcl_duputobcstr(hcl, print_file, HCL_NULL); + if (HCL_UNLIKELY(!xtn->print_path)) + { + hcl_freemem (hcl, xtn->read_path); + xtn->read_path = HCL_NULL; + return -1; + } + + n = hcl_attachio(hcl, read_handler, print_handler); + + hcl_freemem (hcl, xtn->read_path); + hcl_freemem (hcl, xtn->print_path); + + xtn->read_path = HCL_NULL; + xtn->print_path = HCL_NULL; + + return n; } int hcl_isstdreadertty (hcl_t* hcl) @@ -3505,4 +3551,3 @@ int hcl_isstdreadertty (hcl_t* hcl) return xtn->reader_istty; } -/* TODO: hio_attachiostdwithucstr() */