separted the IO handlers to the source code handler and user stream handlers
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-11-05 16:58:45 +09:00
parent bcfe69feda
commit 94a44f1fd7
9 changed files with 312 additions and 213 deletions

View File

@ -718,7 +718,8 @@ hcl_server_proto_t* hcl_server_proto_open (hcl_oow_t xtnsize, hcl_server_worker_
if (hcl_ignite(proto->hcl, worker->server->cfg.actor_heap_size) <= -1) goto oops;
if (hcl_addbuiltinprims(proto->hcl) <= -1) goto oops;
if (hcl_attachio(proto->hcl, read_handler, HCL_NULL, print_handler) <= -1) goto oops;
if (hcl_attachscio(proto->hcl, read_handler) <= -1) goto oops;
if (hcl_attachudio(proto->hcl, HCL_NULL, print_handler) <= -1) goto oops;
return proto;
oops:

View File

@ -219,6 +219,10 @@ void hcl_fini (hcl_t* hcl)
/* deregister all callbacks */
while (hcl->cblist) hcl_deregcb (hcl, hcl->cblist);
/* detach the user data io handlers just in case */
hcl_detachudio (hcl);
if (hcl->sem_list)
{
hcl_freemem (hcl, hcl->sem_list);

View File

@ -2240,36 +2240,54 @@ HCL_EXPORT hcl_ooch_t* hcl_readbasesrraw (
hcl_oow_t* xlen
);
HCL_EXPORT int hcl_attachio (
hcl_t* hcl,
hcl_io_impl_t sci_rdr, /* source code input handler */
hcl_io_impl_t udi_rdr, /* user data input handler */
hcl_io_impl_t udo_wrtr /* user data output handler */
HCL_EXPORT int hcl_attachscio (
hcl_t* hcl,
hcl_io_impl_t sci_rdr
);
HCL_EXPORT int hcl_attachiostdwithbcstr (
HCL_EXPORT void hcl_detachscio (
hcl_t* hcl
);
HCL_EXPORT int hcl_attachudio (
hcl_t* hcl,
hcl_io_impl_t udi_rdr,
hcl_io_impl_t udo_wrtr
);
HCL_EXPORT void hcl_detachudio (
hcl_t* hcl
);
HCL_EXPORT int hcl_attachsciostdwithucstr (
hcl_t* hcl,
const hcl_bch_t* read_file,
const hcl_bch_t* scan_file,
const hcl_bch_t* print_file
const hcl_uch_t* sci_file
);
HCL_EXPORT int hcl_attachiostdwithucstr (
HCL_EXPORT int hcl_attachsciostdwithbcstr (
hcl_t* hcl,
const hcl_uch_t* read_file,
const hcl_uch_t* scan_file,
const hcl_uch_t* print_file
const hcl_bch_t* sci_file
);
HCL_EXPORT int hcl_isstdreadertty (
hcl_t* hcl
HCL_EXPORT int hcl_attachudiostdwithucstr (
hcl_t* hcl,
const hcl_uch_t* udi_file,
const hcl_uch_t* udo_file
);
HCL_EXPORT int hcl_attachudiostdwithbcstr (
hcl_t* hcl,
const hcl_bch_t* udi_file,
const hcl_bch_t* udo_file
);
HCL_EXPORT void hcl_detachio (
hcl_t* hcl
);
HCL_EXPORT void hcl_flushio (
HCL_EXPORT void hcl_flushudio (
hcl_t* hcl
);

View File

@ -2413,7 +2413,7 @@ static int feed_from_includee (hcl_t* hcl)
int hcl_beginfeed (hcl_t* hcl, hcl_on_cnode_t on_cnode)
{
HCL_ASSERT (hcl, hcl->c != HCL_NULL); /* call hcl_attachio() or hcl_attachiostd() first */
HCL_ASSERT (hcl, hcl->c != HCL_NULL); /* call hcl_attachscio() or hcl_attachsciostd() first */
init_feed (hcl);
if (on_cnode) hcl->c->feed.on_cnode = on_cnode;
@ -2732,7 +2732,7 @@ static void fini_compiler_cb (hcl_t* hcl)
clear_sr_names (hcl);
if (hcl->c->tok.name.ptr) hcl_freemem (hcl, hcl->c->tok.name.ptr);
hcl_detachio (hcl);
hcl_detachscio (hcl);
hcl_freemem (hcl, hcl->c);
hcl->c = HCL_NULL;
@ -2786,13 +2786,11 @@ static int init_compiler (hcl_t* hcl)
return 0;
}
int hcl_attachio (hcl_t* hcl, hcl_io_impl_t sci_rdr, hcl_io_impl_t udi_rdr, hcl_io_impl_t udo_wrtr)
int hcl_attachscio (hcl_t* hcl, hcl_io_impl_t sci_rdr)
{
int n;
int inited_compiler = 0;
hcl_io_sciarg_t new_sciarg;
hcl_io_udiarg_t new_udiarg;
hcl_io_udoarg_t new_udoarg;
if (!hcl->c)
{
@ -2800,6 +2798,7 @@ int hcl_attachio (hcl_t* hcl, hcl_io_impl_t sci_rdr, hcl_io_impl_t udi_rdr, hcl_
inited_compiler = 1;
}
if (sci_rdr)
{
/* The name field and the includer field are HCL_NULL
@ -2811,34 +2810,7 @@ int hcl_attachio (hcl_t* hcl, hcl_io_impl_t sci_rdr, hcl_io_impl_t udi_rdr, hcl_
/* open the top-level source input stream */
n = sci_rdr(hcl, HCL_IO_OPEN, &new_sciarg);
if (n <= -1) goto oops;
}
if (udi_rdr)
{
HCL_MEMSET (&new_udiarg, 0, HCL_SIZEOF(new_udiarg));
n = udi_rdr(hcl, HCL_IO_OPEN, &new_udiarg);
if (n <= -1)
{
sci_rdr (hcl, HCL_IO_CLOSE, &new_sciarg);
goto oops;
}
}
if (udo_wrtr)
{
/* open the new output stream */
HCL_MEMSET (&new_udoarg, 0, HCL_SIZEOF(new_udoarg));
n = udo_wrtr(hcl, HCL_IO_OPEN, &new_udoarg);
if (n <= -1)
{
if (udi_rdr) udi_rdr (hcl, HCL_IO_CLOSE, &new_udiarg);
if (sci_rdr) sci_rdr (hcl, HCL_IO_CLOSE, &new_sciarg);
goto oops;
}
}
if (sci_rdr)
{
if (hcl->c->sci_rdr)
{
/* close the old source input stream */
@ -2846,32 +2818,7 @@ int hcl_attachio (hcl_t* hcl, hcl_io_impl_t sci_rdr, hcl_io_impl_t udi_rdr, hcl_
}
hcl->c->sci_rdr = sci_rdr;
hcl->c->sci_arg = new_sciarg;
}
if (udi_rdr)
{
if (hcl->io.udi_rdr)
{
/* close the old input stream */
hcl->io.udi_rdr (hcl, HCL_IO_CLOSE, &hcl->io.udi_arg);
}
hcl->io.udi_rdr = udi_rdr;
hcl->io.udi_arg = new_udiarg;
}
if (udo_wrtr)
{
if (hcl->io.udo_wrtr)
{
/* close the old output stream */
hcl->io.udo_wrtr (hcl, HCL_IO_CLOSE, &hcl->io.udo_arg);
}
hcl->io.udo_wrtr = udo_wrtr;
hcl->io.udo_arg = new_udoarg;
}
if (sci_rdr)
{
/* clear unneeded source stream names */
/*clear_sr_names (hcl); <---- TODO: tricky to clean up here */
@ -2888,12 +2835,7 @@ oops:
return -1;
}
void hcl_flushio (hcl_t* hcl)
{
if (hcl->io.udo_wrtr) hcl->io.udo_wrtr (hcl, HCL_IO_FLUSH, &hcl->io.udo_arg);
}
void hcl_detachio (hcl_t* hcl)
void hcl_detachscio (hcl_t* hcl)
{
/* an error occurred and control has reached here
* probably, some included files might not have been
@ -2919,9 +2861,68 @@ void hcl_detachio (hcl_t* hcl)
hcl->c->sci_rdr (hcl, HCL_IO_CLOSE, hcl->c->curinp);
hcl->c->sci_rdr = HCL_NULL; /* ready for another attachment */
}
}
}
int hcl_attachudio (hcl_t* hcl, hcl_io_impl_t udi_rdr, hcl_io_impl_t udo_wrtr)
{
int n;
hcl_io_udiarg_t new_udiarg;
hcl_io_udoarg_t new_udoarg;
if (udi_rdr)
{
HCL_MEMSET (&new_udiarg, 0, HCL_SIZEOF(new_udiarg));
n = udi_rdr(hcl, HCL_IO_OPEN, &new_udiarg);
if (n <= -1)
{
goto oops;
}
}
if (udo_wrtr)
{
/* open the new output stream */
HCL_MEMSET (&new_udoarg, 0, HCL_SIZEOF(new_udoarg));
n = udo_wrtr(hcl, HCL_IO_OPEN, &new_udoarg);
if (n <= -1)
{
if (udi_rdr) udi_rdr (hcl, HCL_IO_CLOSE, &new_udiarg);
goto oops;
}
}
if (udi_rdr)
{
if (hcl->io.udi_rdr)
{
/* close the old input stream */
hcl->io.udi_rdr (hcl, HCL_IO_CLOSE, &hcl->io.udi_arg);
}
hcl->io.udi_rdr = udi_rdr;
hcl->io.udi_arg = new_udiarg;
}
if (udo_wrtr)
{
if (hcl->io.udo_wrtr)
{
/* close the old output stream */
hcl->io.udo_wrtr (hcl, HCL_IO_CLOSE, &hcl->io.udo_arg);
}
hcl->io.udo_wrtr = udo_wrtr;
hcl->io.udo_arg = new_udoarg;
}
return 0;
oops:
return -1;
}
void hcl_detachudio (hcl_t* hcl)
{
if (hcl->io.udi_rdr)
{
hcl->io.udi_rdr (hcl, HCL_IO_CLOSE, &hcl->io.udi_arg);
@ -2935,6 +2936,11 @@ void hcl_detachio (hcl_t* hcl)
}
}
void hcl_flushudio (hcl_t* hcl)
{
if (hcl->io.udo_wrtr) hcl->io.udo_wrtr (hcl, HCL_IO_FLUSH, &hcl->io.udo_arg);
}
void hcl_setbasesrloc (hcl_t* hcl, hcl_oow_t line, hcl_oow_t colm)
{
hcl->c->sci_arg.line = line;

172
lib/std.c
View File

@ -288,11 +288,9 @@ struct xtn_t
* 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* scan_path; /* runtime input file */
const char* print_path; /* runtime output file */
int reader_istty;
const char* sci_path; /* main source file */
const char* udi_path; /* runtime input file */
const char* udo_path; /* runtime output file */
int vm_running;
int rcv_tick;
@ -3241,42 +3239,38 @@ static HCL_INLINE int open_sci_stream (hcl_t* hcl, hcl_io_sciarg_t* arg)
#endif
bb->fp = fopen(bb->fn, FOPEN_R_FLAGS);
if (!bb->fp)
{
hcl_seterrbfmt (hcl, HCL_EIOERR, "unable to open %hs", bb->fn);
goto oops;
}
}
else
{
/* main stream */
/* main stream */
hcl_oow_t pathlen;
pathlen = xtn->read_path? hcl_count_bcstr(xtn->read_path): 0;
pathlen = xtn->sci_path? hcl_count_bcstr(xtn->sci_path): 0;
bb = (bb_t*)hcl_callocmem(hcl, HCL_SIZEOF(*bb) + (HCL_SIZEOF(hcl_bch_t) * (pathlen + 1)));
if (!bb) goto oops;
bb->fn = (hcl_bch_t*)(bb + 1);
if (pathlen > 0 && xtn->read_path)
if (pathlen > 0 && xtn->sci_path)
{
hcl_copy_bcstr (bb->fn, pathlen + 1, xtn->read_path);
bb->fp = fopen(bb->fn, FOPEN_R_FLAGS);
hcl_copy_bcstr (bb->fn, pathlen + 1, xtn->sci_path);
/*bb->fp = fopen(bb->fn, FOPEN_R_FLAGS);*/
}
else
{
bb->fn[0] = '\0';
bb->fp = stdin;
/*bb->fp = stdin;*/
}
}
if (!bb->fp)
{
hcl_seterrbfmt (hcl, HCL_EIOERR, "unable to open %hs", bb->fn);
goto oops;
}
if (!arg->includer) /* if main stream */
{
#if defined(HAVE_ISATTY)
xtn->reader_istty = isatty(fileno(bb->fp));
#endif
/* HACK */
HCL_ASSERT (hcl, arg->name == HCL_NULL);
arg->name = hcl_dupbtooocstr(hcl, bb->fn, HCL_NULL);
@ -3304,17 +3298,18 @@ static HCL_INLINE int close_sci_stream (hcl_t* hcl, hcl_io_sciarg_t* arg)
bb_t* bb;
bb = (bb_t*)arg->handle;
HCL_ASSERT (hcl, bb != HCL_NULL && bb->fp != HCL_NULL);
HCL_ASSERT (hcl, bb != HCL_NULL /*&& bb->fp != HCL_NULL*/);
/* HACK */
if (!arg->includer && arg->name)
{
/* main stream closing */
hcl_freemem (hcl, arg->name);
arg->name = HCL_NULL;
}
/* END HACK */
if (bb->fp != stdin) fclose (bb->fp);
if (bb->fp /*&& bb->fp != stdin*/) fclose (bb->fp);
hcl_freemem (hcl, bb);
arg->handle = HCL_NULL;
@ -3403,15 +3398,15 @@ static HCL_INLINE int open_in_stream (hcl_t* hcl, hcl_io_udiarg_t* arg)
hcl_oow_t pathlen;
pathlen = xtn->read_path? hcl_count_bcstr(xtn->read_path): 0;
pathlen = xtn->sci_path? hcl_count_bcstr(xtn->sci_path): 0;
bb = (bb_t*)hcl_callocmem(hcl, HCL_SIZEOF(*bb) + (HCL_SIZEOF(hcl_bch_t) * (pathlen + 1)));
if (!bb) goto oops;
bb->fn = (hcl_bch_t*)(bb + 1);
if (pathlen > 0 && xtn->read_path)
if (pathlen > 0 && xtn->sci_path)
{
hcl_copy_bcstr (bb->fn, pathlen + 1, xtn->read_path);
hcl_copy_bcstr (bb->fn, pathlen + 1, xtn->sci_path);
bb->fp = fopen(bb->fn, FOPEN_R_FLAGS);
}
else
@ -3503,7 +3498,7 @@ static HCL_INLINE int read_in_stream (hcl_t* hcl, hcl_io_udiarg_t* arg)
return 0;
}
static int scan_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg)
static int udi_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg)
{
switch (cmd)
{
@ -3538,11 +3533,11 @@ static HCL_INLINE int open_out_stream (hcl_t* hcl, hcl_io_udoarg_t* arg)
#define FOPEN_W_FLAGS "w"
#endif
fp = (xtn->print_path && xtn->print_path[0] != '\0'? fopen(xtn->print_path, FOPEN_W_FLAGS): stdout);
fp = (xtn->udo_path && xtn->udo_path[0] != '\0'? fopen(xtn->udo_path, FOPEN_W_FLAGS): stdout);
if (!fp)
{
if (xtn->print_path)
hcl_seterrbfmt (hcl, HCL_EIOERR, "unable to open %hs", xtn->print_path);
if (xtn->udo_path)
hcl_seterrbfmt (hcl, HCL_EIOERR, "unable to open %hs", xtn->udo_path);
else
hcl_seterrnum (hcl, HCL_EIOERR);
return -1;
@ -3632,7 +3627,7 @@ static HCL_INLINE int flush_out_stream (hcl_t* hcl, hcl_io_udoarg_t* arg)
return 0;
}
static int print_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg)
static int udo_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg)
{
switch (cmd)
{
@ -3659,73 +3654,96 @@ static int print_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg)
/* --------------------------------------------------------------------- */
int hcl_attachiostdwithbcstr (hcl_t* hcl, const hcl_bch_t* read_file, const hcl_bch_t* scan_file, const hcl_bch_t* print_file)
int hcl_attachsciostdwithbcstr (hcl_t* hcl, const hcl_bch_t* sci_file)
{
xtn_t* xtn = GET_XTN(hcl);
int n;
HCL_ASSERT (hcl, xtn->read_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->scan_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->print_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->sci_path == HCL_NULL);
xtn->sci_path = sci_file;
xtn->read_path = read_file;
xtn->scan_path = scan_file;
xtn->print_path = print_file;
n = hcl_attachscio(hcl, sci_handler);
n = hcl_attachio(hcl, sci_handler, scan_handler, print_handler);
xtn->read_path = HCL_NULL;
xtn->scan_path = HCL_NULL;
xtn->print_path = HCL_NULL;
xtn->sci_path = HCL_NULL;
return n;
}
int hcl_attachiostdwithucstr (hcl_t* hcl, const hcl_uch_t* read_file, const hcl_uch_t* scan_file, const hcl_uch_t* print_file)
int hcl_attachsciostdwithucstr (hcl_t* hcl, const hcl_uch_t* sci_file)
{
xtn_t* xtn = GET_XTN(hcl);
int n;
HCL_ASSERT (hcl, xtn->read_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->scan_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->print_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->sci_path == HCL_NULL);
xtn->read_path = hcl_duputobcstr(hcl, read_file, HCL_NULL);
if (HCL_UNLIKELY(!xtn->read_path)) return -1;
xtn->sci_path = hcl_duputobcstr(hcl, sci_file, HCL_NULL);
if (HCL_UNLIKELY(!xtn->sci_path)) return -1;
n = hcl_attachscio(hcl, sci_handler);
xtn->scan_path = hcl_duputobcstr(hcl, scan_file, HCL_NULL);
if (HCL_UNLIKELY(!xtn->scan_path))
{
hcl_freemem (hcl, (void*)xtn->read_path);
xtn->read_path = HCL_NULL;
return -1;
}
xtn->print_path = hcl_duputobcstr(hcl, print_file, HCL_NULL);
if (HCL_UNLIKELY(!xtn->print_path))
{
hcl_freemem (hcl, (void*)xtn->scan_path);
hcl_freemem (hcl, (void*)xtn->read_path);
xtn->scan_path = HCL_NULL;
xtn->read_path = HCL_NULL;
return -1;
}
n = hcl_attachio(hcl, sci_handler, scan_handler, print_handler);
hcl_freemem (hcl, (void*)xtn->read_path);
hcl_freemem (hcl, (void*)xtn->scan_path);
hcl_freemem (hcl, (void*)xtn->print_path);
xtn->read_path = HCL_NULL;
xtn->scan_path = HCL_NULL;
xtn->print_path = HCL_NULL;
hcl_freemem (hcl, (void*)xtn->sci_path);
xtn->sci_path = HCL_NULL;
return n;
}
int hcl_isstdreadertty (hcl_t* hcl)
/* --------------------------------------------------------------------- */
int hcl_attachudiostdwithbcstr (hcl_t* hcl, const hcl_bch_t* udi_file, const hcl_bch_t* udo_file)
{
xtn_t* xtn = GET_XTN(hcl);
return xtn->reader_istty;
int n;
HCL_ASSERT (hcl, xtn->udi_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->udo_path == HCL_NULL);
xtn->udi_path = udi_file;
xtn->udo_path = udo_file;
n = hcl_attachudio(hcl, udi_handler, udo_handler);
xtn->udi_path = HCL_NULL;
xtn->udo_path = HCL_NULL;
return n;
}
int hcl_attachudiostdwithucstr (hcl_t* hcl, const hcl_uch_t* udi_file, const hcl_uch_t* udo_file)
{
xtn_t* xtn = GET_XTN(hcl);
int n;
HCL_ASSERT (hcl, xtn->udi_path == HCL_NULL);
HCL_ASSERT (hcl, xtn->udo_path == HCL_NULL);
xtn->udi_path = hcl_duputobcstr(hcl, udi_file, HCL_NULL);
if (HCL_UNLIKELY(!xtn->udi_path))
{
hcl_freemem (hcl, (void*)xtn->sci_path);
return -1;
}
xtn->udo_path = hcl_duputobcstr(hcl, udo_file, HCL_NULL);
if (HCL_UNLIKELY(!xtn->udo_path))
{
hcl_freemem (hcl, (void*)xtn->udi_path);
xtn->udi_path = HCL_NULL;
return -1;
}
n = hcl_attachudio(hcl, udi_handler, udo_handler);
hcl_freemem (hcl, (void*)xtn->udi_path);
hcl_freemem (hcl, (void*)xtn->udo_path);
xtn->udi_path = HCL_NULL;
xtn->udo_path = HCL_NULL;
return n;
}