renamed hawk_rtx_cleario() to hawk_rtx_clearallios().

added hawk_rtx_flushallios().
revised hawk_rtx_loop() and hawk_rtx_callfun() to call hawk_rtx_flushallios() instead of hawk_rtx_clearallios().
changed the io handler to discard unflushed data if flushing fails in std.c
This commit is contained in:
hyung-hwan 2020-04-08 10:23:31 +00:00
parent a23eeee168
commit d9ffad1227
5 changed files with 72 additions and 40 deletions

View File

@ -405,7 +405,7 @@ static int fnc_close (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
} }
} }
n = hawk_rtx_closeio (rtx, name, opt); n = hawk_rtx_closeio(rtx, name, opt);
/* failure to close is not a critical error. instead, that is /* failure to close is not a critical error. instead, that is
* flagged by the return value of close(). * flagged by the return value of close().
if (n <= -1 && rtx->errinf.num != HAWK_EIONMNF) if (n <= -1 && rtx->errinf.num != HAWK_EIONMNF)
@ -430,7 +430,7 @@ static int flush_io (hawk_rtx_t* rtx, int rio, const hawk_ooch_t* name, int n)
{ {
int n2; int n2;
if (rtx->rio.handler[rio] != HAWK_NULL) if (rtx->rio.handler[rio])
{ {
n2 = hawk_rtx_flushio(rtx, rio, name); n2 = hawk_rtx_flushio(rtx, rio, name);
if (n2 <= -1) if (n2 <= -1)
@ -484,8 +484,8 @@ static int fnc_fflush (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{ {
hawk_ooch_t* ptr, * end; hawk_ooch_t* ptr, * end;
a0 = hawk_rtx_getarg (rtx, 0); a0 = hawk_rtx_getarg(rtx, 0);
str0 = hawk_rtx_getvaloocstr (rtx, a0, &len0); str0 = hawk_rtx_getvaloocstr(rtx, a0, &len0);
if (str0 == HAWK_NULL) return -1; if (str0 == HAWK_NULL) return -1;
/* the target name contains a null character. /* the target name contains a null character.
@ -521,21 +521,13 @@ static int fnc_fflush (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
* } * }
*/ */
n = flush_io ( n = flush_io(rtx, HAWK_OUT_FILE, ((len0 == 0)? HAWK_NULL: str0), 1);
rtx, HAWK_OUT_FILE,
((len0 == 0)? HAWK_NULL: str0), 1);
/*if (n == -99) return -1;*/ /*if (n == -99) return -1;*/
n = flush_io ( n = flush_io(rtx, HAWK_OUT_APFILE, ((len0 == 0)? HAWK_NULL: str0), n);
rtx, HAWK_OUT_APFILE,
((len0 == 0)? HAWK_NULL: str0), n);
/*if (n == -99) return -1;*/ /*if (n == -99) return -1;*/
n = flush_io ( n = flush_io(rtx, HAWK_OUT_PIPE, ((len0 == 0)? HAWK_NULL: str0), n);
rtx, HAWK_OUT_PIPE,
((len0 == 0)? HAWK_NULL: str0), n);
/*if (n == -99) return -1;*/ /*if (n == -99) return -1;*/
n = flush_io ( n = flush_io(rtx, HAWK_OUT_RWPIPE, ((len0 == 0)? HAWK_NULL: str0), n);
rtx, HAWK_OUT_RWPIPE,
((len0 == 0)? HAWK_NULL: str0), n);
/*if (n == -99) return -1;*/ /*if (n == -99) return -1;*/
/* if n remains 1, no io handlers have been defined for /* if n remains 1, no io handlers have been defined for

View File

@ -32,41 +32,42 @@ extern "C" {
#endif #endif
int hawk_rtx_readio ( int hawk_rtx_readio (
hawk_rtx_t* run, int in_type, hawk_rtx_t* rtx, int in_type,
const hawk_ooch_t* name, hawk_ooecs_t* buf); const hawk_ooch_t* name, hawk_ooecs_t* buf);
int hawk_rtx_readiobytes ( int hawk_rtx_readiobytes (
hawk_rtx_t* run, int in_type, hawk_rtx_t* rtx, int in_type,
const hawk_ooch_t* name, hawk_becs_t* buf); const hawk_ooch_t* name, hawk_becs_t* buf);
int hawk_rtx_writeioval ( int hawk_rtx_writeioval (
hawk_rtx_t* run, int out_type, hawk_rtx_t* rtx, int out_type,
const hawk_ooch_t* name, hawk_val_t* v); const hawk_ooch_t* name, hawk_val_t* v);
int hawk_rtx_writeiostr ( int hawk_rtx_writeiostr (
hawk_rtx_t* run, int out_type, hawk_rtx_t* rtx, int out_type,
const hawk_ooch_t* name, hawk_ooch_t* str, hawk_oow_t len); const hawk_ooch_t* name, hawk_ooch_t* str, hawk_oow_t len);
int hawk_rtx_writeiobytes ( int hawk_rtx_writeiobytes (
hawk_rtx_t* run, int out_type, hawk_rtx_t* rtx, int out_type,
const hawk_ooch_t* name, hawk_bch_t* str, hawk_oow_t len); const hawk_ooch_t* name, hawk_bch_t* str, hawk_oow_t len);
int hawk_rtx_flushio ( int hawk_rtx_flushio (
hawk_rtx_t* run, int out_type, const hawk_ooch_t* name); hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name);
int hawk_rtx_nextio_read ( int hawk_rtx_nextio_read (
hawk_rtx_t* run, int in_type, const hawk_ooch_t* name); hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name);
int hawk_rtx_nextio_write ( int hawk_rtx_nextio_write (
hawk_rtx_t* run, int out_type, const hawk_ooch_t* name); hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name);
int hawk_rtx_closeio ( int hawk_rtx_closeio (
hawk_rtx_t* run, hawk_rtx_t* rtx,
const hawk_ooch_t* name, const hawk_ooch_t* name,
const hawk_ooch_t* opt const hawk_ooch_t* opt
); );
void hawk_rtx_cleario (hawk_rtx_t* run); void hawk_rtx_flushallios (hawk_rtx_t* rtx);
void hawk_rtx_clearallios (hawk_rtx_t* rtx);
#if defined(__cplusplus) #if defined(__cplusplus)
} }

View File

@ -1039,7 +1039,7 @@ static int prepare_for_write_io_data (hawk_rtx_t* rtx, int out_type, const hawk_
io_mask = out_mask_map[out_type]; io_mask = out_mask_map[out_type];
handler = rtx->rio.handler[io_type]; handler = rtx->rio.handler[io_type];
if (handler == HAWK_NULL) if (HAWK_UNLIKELY(!handler))
{ {
/* no I/O handler provided */ /* no I/O handler provided */
hawk_rtx_seterrnum (rtx, HAWK_NULL, HAWK_EIOUSER); hawk_rtx_seterrnum (rtx, HAWK_NULL, HAWK_EIOUSER);
@ -1067,12 +1067,12 @@ static int prepare_for_write_io_data (hawk_rtx_t* rtx, int out_type, const hawk_
if (p == HAWK_NULL) if (p == HAWK_NULL)
{ {
p = (hawk_rio_arg_t*)hawk_rtx_allocmem(rtx, HAWK_SIZEOF(hawk_rio_arg_t)); p = (hawk_rio_arg_t*)hawk_rtx_allocmem(rtx, HAWK_SIZEOF(hawk_rio_arg_t));
if (p == HAWK_NULL) return -1; if (HAWK_UNLIKELY(!p)) return -1;
HAWK_MEMSET (p, 0, HAWK_SIZEOF(*p)); HAWK_MEMSET (p, 0, HAWK_SIZEOF(*p));
p->name = hawk_rtx_dupoocstr(rtx, name, HAWK_NULL); p->name = hawk_rtx_dupoocstr(rtx, name, HAWK_NULL);
if (p->name == HAWK_NULL) if (HAWK_UNLIKELY(!p->name))
{ {
hawk_rtx_freemem (rtx, p); hawk_rtx_freemem (rtx, p);
return -1; return -1;
@ -1422,7 +1422,7 @@ int hawk_rtx_closio_write (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* nam
io_mask = out_mask_map[out_type]; io_mask = out_mask_map[out_type];
handler = rtx->rio.handler[io_type]; handler = rtx->rio.handler[io_type];
if (!handler) if (HAWK_UNLIKELY(!handler))
{ {
/* no io handler provided */ /* no io handler provided */
hawk_rtx_seterrnum (rtx, HAWK_NULL, HAWK_EIOUSER); hawk_rtx_seterrnum (rtx, HAWK_NULL, HAWK_EIOUSER);
@ -1434,7 +1434,7 @@ int hawk_rtx_closio_write (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* nam
if (p->type == (io_type | io_mask) && hawk_comp_oocstr(p->name, name, 0) == 0) if (p->type == (io_type | io_mask) && hawk_comp_oocstr(p->name, name, 0) == 0)
{ {
hawk_rio_impl_t handler; hawk_rio_impl_t handler;
handler = rtx->rio.handler[p->type & IO_MASK_CLEAR]; handler = rtx->rio.handler[p->type & IO_MASK_CLEAR];
if (handler && handler(rtx, HAWK_RIO_CMD_CLOSE, p, HAWK_NULL, 0) <= -1) return -1; if (handler && handler(rtx, HAWK_RIO_CMD_CLOSE, p, HAWK_NULL, 0) <= -1) return -1;
@ -1525,7 +1525,7 @@ int hawk_rtx_closeio (hawk_rtx_t* rtx, const hawk_ooch_t* name, const hawk_ooch_
} }
} }
if (px != HAWK_NULL) px->next = p->next; if (px) px->next = p->next;
else rtx->rio.chain = p->next; else rtx->rio.chain = p->next;
hawk_rtx_freemem (rtx, p->name); hawk_rtx_freemem (rtx, p->name);
@ -1543,7 +1543,22 @@ int hawk_rtx_closeio (hawk_rtx_t* rtx, const hawk_ooch_t* name, const hawk_ooch_
return -1; return -1;
} }
void hawk_rtx_cleario (hawk_rtx_t* rtx) void hawk_rtx_flushallios (hawk_rtx_t* rtx)
{
hawk_rio_arg_t* rio;
hawk_rio_impl_t handler;
for (rio = rtx->rio.chain; rio; rio = rio->next)
{
handler = rtx->rio.handler[rio->type & IO_MASK_CLEAR];
if (handler)
{
handler (rtx, HAWK_RIO_CMD_FLUSH, rio, HAWK_NULL, 0);
}
}
}
void hawk_rtx_clearallios (hawk_rtx_t* rtx)
{ {
hawk_rio_arg_t* next; hawk_rio_arg_t* next;
hawk_rio_impl_t handler; hawk_rio_impl_t handler;

View File

@ -1158,7 +1158,7 @@ static void fini_rtx (hawk_rtx_t* rtx, int fini_globals)
/* close all pending io's */ /* close all pending io's */
/* TODO: what if this operation fails? */ /* TODO: what if this operation fails? */
hawk_rtx_cleario (rtx); hawk_rtx_clearallios (rtx);
HAWK_ASSERT (rtx->rio.chain == HAWK_NULL); HAWK_ASSERT (rtx->rio.chain == HAWK_NULL);
if (rtx->gbl.rs[0]) if (rtx->gbl.rs[0])
@ -1672,8 +1672,8 @@ hawk_val_t* hawk_rtx_loop (hawk_rtx_t* rtx)
/* reset the exit level */ /* reset the exit level */
rtx->exit_level = EXIT_NONE; rtx->exit_level = EXIT_NONE;
/* clear any pending io's */ /* flush all buffered io data */
hawk_rtx_cleario (rtx); hawk_rtx_flushallios (rtx);
return retv; return retv;
} }
@ -1829,8 +1829,8 @@ hawk_val_t* hawk_rtx_callfun (hawk_rtx_t* rtx, hawk_fun_t* fun, hawk_val_t* args
hawk_rtx_refupval (rtx, v); hawk_rtx_refupval (rtx, v);
} }
/* clear any pending io's */ /* flush all buffered io data */
hawk_rtx_cleario (rtx); hawk_rtx_flushallios (rtx);
/* return the return value with its reference count at least 1. /* return the return value with its reference count at least 1.
* the caller of this function should count down its reference. */ * the caller of this function should count down its reference. */

View File

@ -2020,7 +2020,19 @@ static hawk_ooi_t awk_rio_file (hawk_rtx_t* rtx, hawk_rio_cmd_t cmd, hawk_rio_ar
return hawk_sio_putbchars((hawk_sio_t*)riod->handle, data, size); return hawk_sio_putbchars((hawk_sio_t*)riod->handle, data, size);
case HAWK_RIO_CMD_FLUSH: case HAWK_RIO_CMD_FLUSH:
return hawk_sio_flush((hawk_sio_t*)riod->handle); {
int n;
n = hawk_sio_flush((hawk_sio_t*)riod->handle);
if (HAWK_UNLIKELY(n <= -1))
{
/* if flushing fails, discard the buffered data
* keeping the unflushed data causes causes subsequent write or close() to
* flush again and again */
hawk_sio_drain ((hawk_sio_t*)riod->handle);
}
return n;
}
case HAWK_RIO_CMD_NEXT: case HAWK_RIO_CMD_NEXT:
return -1; return -1;
@ -2376,7 +2388,19 @@ static hawk_ooi_t awk_rio_console (hawk_rtx_t* rtx, hawk_rio_cmd_t cmd, hawk_rio
return hawk_sio_putbchars((hawk_sio_t*)riod->handle, data, size); return hawk_sio_putbchars((hawk_sio_t*)riod->handle, data, size);
case HAWK_RIO_CMD_FLUSH: case HAWK_RIO_CMD_FLUSH:
return hawk_sio_flush((hawk_sio_t*)riod->handle); {
int n;
n = hawk_sio_flush((hawk_sio_t*)riod->handle);
if (HAWK_UNLIKELY(n <= -1))
{
/* if flushing fails, discard the buffered data
* keeping the unflushed data causes causes subsequent write or close() to
* flush again and again */
hawk_sio_drain ((hawk_sio_t*)riod->handle);
}
return n;
}
case HAWK_RIO_CMD_NEXT: case HAWK_RIO_CMD_NEXT:
{ {