From 0cbf9e4edfe3a0b3d0f279aa6528efb1454b8419 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Wed, 17 Apr 2019 05:37:56 +0000 Subject: [PATCH] added hcl_flushio() and HCL_IO_FLUSH --- lib/hcl.h | 6 +++++- lib/main.c | 29 ++++++++++++++++++++++++++--- lib/read.c | 16 ++++++++++++---- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lib/hcl.h b/lib/hcl.h index 0c16c91..201237e 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -833,7 +833,8 @@ enum hcl_iocmd_t HCL_IO_OPEN, HCL_IO_CLOSE, HCL_IO_READ, - HCL_IO_WRITE + HCL_IO_WRITE, + HCL_IO_FLUSH }; typedef enum hcl_iocmd_t hcl_iocmd_t; @@ -1620,6 +1621,9 @@ HCL_EXPORT void hcl_detachio ( hcl_t* hcl ); +HCL_EXPORT void hcl_flushio ( + hcl_t* hcl +); HCL_EXPORT hcl_oop_t hcl_read ( hcl_t* hcl diff --git a/lib/main.c b/lib/main.c index dd59063..3b3a366 100644 --- a/lib/main.c +++ b/lib/main.c @@ -310,6 +310,10 @@ static int read_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg) case HCL_IO_READ: return read_input (hcl, (hcl_ioinarg_t*)arg); + case HCL_IO_FLUSH: + /* no effect on an input stream */ + return 0; + default: hcl_seterrnum (hcl, HCL_EINTERN); return -1; @@ -390,18 +394,32 @@ static HCL_INLINE int write_output (hcl_t* hcl, hcl_iooutarg_t* arg) return 0; } +static HCL_INLINE int flush_output (hcl_t* hcl, hcl_iooutarg_t* arg) +{ + FILE* fp; + + fp = (FILE*)arg->handle; + HCL_ASSERT (hcl, fp != HCL_NULL); + + fflush (fp); + return 0; +} + static int print_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg) { switch (cmd) { case HCL_IO_OPEN: - return open_output (hcl, (hcl_iooutarg_t*)arg); + return open_output(hcl, (hcl_iooutarg_t*)arg); case HCL_IO_CLOSE: - return close_output (hcl, (hcl_iooutarg_t*)arg); + return close_output(hcl, (hcl_iooutarg_t*)arg); case HCL_IO_WRITE: - return write_output (hcl, (hcl_iooutarg_t*)arg); + return write_output(hcl, (hcl_iooutarg_t*)arg); + + case HCL_IO_FLUSH: + return flush_output(hcl, (hcl_iooutarg_t*)arg); default: hcl_seterrnum (hcl, HCL_EINTERN); @@ -1191,6 +1209,7 @@ count++; } else if (xtn->reader_istty) { + /* interactive mode */ hcl_oop_t retv; hcl_decode (hcl, code_offset, hcl_getbclen(hcl)); @@ -1199,6 +1218,10 @@ count++; //setup_tick (); retv = hcl_executefromip(hcl, code_offset); + + /* flush pending output data in the interactive mode(e.g. printf without a newline) */ + hcl_flushio (hcl); + if (!retv) { hcl_logbfmt (hcl, HCL_LOG_STDERR, "ERROR: cannot execute - [%d] %js\n", hcl_geterrnum(hcl), hcl_geterrmsg(hcl)); diff --git a/lib/read.c b/lib/read.c index f4ba8c1..27502ae 100644 --- a/lib/read.c +++ b/lib/read.c @@ -2298,10 +2298,10 @@ int hcl_attachio (hcl_t* hcl, hcl_ioimpl_t reader, hcl_ioimpl_t printer) HCL_MEMSET (&cb, 0, HCL_SIZEOF(cb)); cb.gc = gc_compiler; cb.fini = fini_compiler; - cbp = hcl_regcb (hcl, &cb); + cbp = hcl_regcb(hcl, &cb); if (!cbp) return -1; - hcl->c = (hcl_compiler_t*)hcl_callocmem (hcl, HCL_SIZEOF(*hcl->c)); + hcl->c = (hcl_compiler_t*)hcl_callocmem(hcl, HCL_SIZEOF(*hcl->c)); if (!hcl->c) { hcl_deregcb (hcl, cbp); @@ -2341,11 +2341,11 @@ int hcl_attachio (hcl_t* hcl, hcl_ioimpl_t reader, hcl_ioimpl_t printer) hcl->c->inarg.colm = 1; /* open the top-level stream */ - n = hcl->c->reader (hcl, HCL_IO_OPEN, &hcl->c->inarg); + n = hcl->c->reader(hcl, HCL_IO_OPEN, &hcl->c->inarg); if (n <= -1) goto oops; HCL_MEMSET (&hcl->c->outarg, 0, HCL_SIZEOF(hcl->c->outarg)); - n = hcl->c->printer (hcl, HCL_IO_OPEN, &hcl->c->outarg); + n = hcl->c->printer(hcl, HCL_IO_OPEN, &hcl->c->outarg); if (n <= -1) { hcl->c->reader (hcl, HCL_IO_CLOSE, &hcl->c->inarg); @@ -2371,6 +2371,14 @@ oops: return -1; } +void hcl_flushio (hcl_t* hcl) +{ + if (hcl->c) + { + if (hcl->c->printer) hcl->c->printer (hcl, HCL_IO_FLUSH, &hcl->c->outarg); + } +} + void hcl_detachio (hcl_t* hcl) { /* an error occurred and control has reached here