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,6 +394,17 @@ 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)
@ -403,6 +418,9 @@ static int print_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* 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);
return -1; return -1;
@ -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

@ -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