added hcl_flushio() and HCL_IO_FLUSH

This commit is contained in:
hyung-hwan 2019-04-17 05:37:56 +00:00
parent 842b5491bd
commit 0cbf9e4edf
3 changed files with 43 additions and 8 deletions

View File

@ -833,7 +833,8 @@ enum hcl_iocmd_t
HCL_IO_OPEN, HCL_IO_OPEN,
HCL_IO_CLOSE, HCL_IO_CLOSE,
HCL_IO_READ, HCL_IO_READ,
HCL_IO_WRITE HCL_IO_WRITE,
HCL_IO_FLUSH
}; };
typedef enum hcl_iocmd_t hcl_iocmd_t; typedef enum hcl_iocmd_t hcl_iocmd_t;
@ -1620,6 +1621,9 @@ HCL_EXPORT void hcl_detachio (
hcl_t* hcl hcl_t* hcl
); );
HCL_EXPORT void hcl_flushio (
hcl_t* hcl
);
HCL_EXPORT hcl_oop_t hcl_read ( HCL_EXPORT hcl_oop_t hcl_read (
hcl_t* hcl hcl_t* hcl

View File

@ -310,6 +310,10 @@ static int read_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
case HCL_IO_READ: case HCL_IO_READ:
return read_input (hcl, (hcl_ioinarg_t*)arg); return read_input (hcl, (hcl_ioinarg_t*)arg);
case HCL_IO_FLUSH:
/* no effect on an input stream */
return 0;
default: default:
hcl_seterrnum (hcl, HCL_EINTERN); hcl_seterrnum (hcl, HCL_EINTERN);
return -1; return -1;
@ -390,18 +394,32 @@ static HCL_INLINE int write_output (hcl_t* hcl, hcl_iooutarg_t* arg)
return 0; 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) static int print_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
{ {
switch (cmd) switch (cmd)
{ {
case HCL_IO_OPEN: case HCL_IO_OPEN:
return open_output (hcl, (hcl_iooutarg_t*)arg); return open_output(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_CLOSE: case HCL_IO_CLOSE:
return close_output (hcl, (hcl_iooutarg_t*)arg); return close_output(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_WRITE: 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: default:
hcl_seterrnum (hcl, HCL_EINTERN); hcl_seterrnum (hcl, HCL_EINTERN);
@ -1191,6 +1209,7 @@ count++;
} }
else if (xtn->reader_istty) else if (xtn->reader_istty)
{ {
/* interactive mode */
hcl_oop_t retv; hcl_oop_t retv;
hcl_decode (hcl, code_offset, hcl_getbclen(hcl)); hcl_decode (hcl, code_offset, hcl_getbclen(hcl));
@ -1199,6 +1218,10 @@ count++;
//setup_tick (); //setup_tick ();
retv = hcl_executefromip(hcl, code_offset); 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) if (!retv)
{ {
hcl_logbfmt (hcl, HCL_LOG_STDERR, "ERROR: cannot execute - [%d] %js\n", hcl_geterrnum(hcl), hcl_geterrmsg(hcl)); hcl_logbfmt (hcl, HCL_LOG_STDERR, "ERROR: cannot execute - [%d] %js\n", hcl_geterrnum(hcl), hcl_geterrmsg(hcl));

View File

@ -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)); HCL_MEMSET (&cb, 0, HCL_SIZEOF(cb));
cb.gc = gc_compiler; cb.gc = gc_compiler;
cb.fini = fini_compiler; cb.fini = fini_compiler;
cbp = hcl_regcb (hcl, &cb); cbp = hcl_regcb(hcl, &cb);
if (!cbp) return -1; 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) if (!hcl->c)
{ {
hcl_deregcb (hcl, cbp); 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; hcl->c->inarg.colm = 1;
/* open the top-level stream */ /* 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; if (n <= -1) goto oops;
HCL_MEMSET (&hcl->c->outarg, 0, HCL_SIZEOF(hcl->c->outarg)); 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) if (n <= -1)
{ {
hcl->c->reader (hcl, HCL_IO_CLOSE, &hcl->c->inarg); hcl->c->reader (hcl, HCL_IO_CLOSE, &hcl->c->inarg);
@ -2371,6 +2371,14 @@ oops:
return -1; 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) void hcl_detachio (hcl_t* hcl)
{ {
/* an error occurred and control has reached here /* an error occurred and control has reached here