extended hcl_attacio() to access a new input stream handler under the name of scanner.

renamed hcl_ioinarg_t to hcl_iosrarg_t
took back hcl_ioinarg_t for the new input stream handling
This commit is contained in:
hyung-hwan 2023-05-19 12:55:08 +09:00
parent 2bd78d2bfc
commit b340f09ecc
6 changed files with 247 additions and 150 deletions

View File

@ -527,7 +527,7 @@ static int feed_loop (hcl_t* hcl, xtn_t* xtn, int cflags, int verbose)
hcl_ooch_t* ptr; hcl_ooch_t* ptr;
hcl_oow_t xlen; hcl_oow_t xlen;
ptr = hcl_readbaseinraw(hcl, &xlen); ptr = hcl_readbasesrraw(hcl, &xlen);
if (HCL_UNLIKELY(!ptr)) goto oops; if (HCL_UNLIKELY(!ptr)) goto oops;
if (xlen <= 0) break; if (xlen <= 0) break;
if (hcl_feed(hcl, ptr, xlen) <= -1) goto feed_error; if (hcl_feed(hcl, ptr, xlen) <= -1) goto feed_error;

View File

@ -646,10 +646,10 @@ struct hcl_compiler_t
hcl_ioimpl_t reader; hcl_ioimpl_t reader;
/* static input data buffer */ /* static input data buffer */
hcl_ioinarg_t inarg; hcl_iosrarg_t srarg;
/* pointer to the current input data. initially, it points to &inarg */ /* pointer to the current input data. initially, it points to &inarg */
hcl_ioinarg_t* curinp; hcl_iosrarg_t* curinp;
/* information about the last meaningful character read. /* information about the last meaningful character read.
* this is a copy of curinp->lxc if no ungetting is performed. * this is a copy of curinp->lxc if no ungetting is performed.
@ -664,7 +664,7 @@ struct hcl_compiler_t
/* the last token read */ /* the last token read */
hcl_iotok_t tok; hcl_iotok_t tok;
hcl_iolink_t* io_names; hcl_iolink_t* sr_names;
hcl_synerr_t synerr; hcl_synerr_t synerr;

View File

@ -316,7 +316,7 @@ static const hcl_bch_t* get_base_name (const hcl_bch_t* path)
} }
static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg) static HCL_INLINE int open_input (hcl_t* hcl, hcl_iosrarg_t* arg)
{ {
worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl); worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl);
bb_t* bb = HCL_NULL; bb_t* bb = HCL_NULL;
@ -412,7 +412,7 @@ oops:
return -1; return -1;
} }
static HCL_INLINE int close_input (hcl_t* hcl, hcl_ioinarg_t* arg) static HCL_INLINE int close_input (hcl_t* hcl, hcl_iosrarg_t* arg)
{ {
worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl); worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl);
bb_t* bb; bb_t* bb;
@ -427,7 +427,7 @@ static HCL_INLINE int close_input (hcl_t* hcl, hcl_ioinarg_t* arg)
return 0; return 0;
} }
static HCL_INLINE int read_input (hcl_t* hcl, hcl_ioinarg_t* arg) static HCL_INLINE int read_input (hcl_t* hcl, hcl_iosrarg_t* arg)
{ {
worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl); worker_hcl_xtn_t* xtn = (worker_hcl_xtn_t*)hcl_getxtn(hcl);
bb_t* bb; bb_t* bb;
@ -545,13 +545,13 @@ static int read_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
switch (cmd) switch (cmd)
{ {
case HCL_IO_OPEN: case HCL_IO_OPEN:
return open_input(hcl, (hcl_ioinarg_t*)arg); return open_input(hcl, (hcl_iosrarg_t*)arg);
case HCL_IO_CLOSE: case HCL_IO_CLOSE:
return close_input(hcl, (hcl_ioinarg_t*)arg); return close_input(hcl, (hcl_iosrarg_t*)arg);
case HCL_IO_READ: case HCL_IO_READ:
return read_input(hcl, (hcl_ioinarg_t*)arg); return read_input(hcl, (hcl_iosrarg_t*)arg);
default: default:
hcl_seterrnum (hcl, HCL_EINTERN); hcl_seterrnum (hcl, HCL_EINTERN);
@ -700,7 +700,7 @@ 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_ignite(proto->hcl, worker->server->cfg.actor_heap_size) <= -1) goto oops;
if (hcl_addbuiltinprims(proto->hcl) <= -1) goto oops; if (hcl_addbuiltinprims(proto->hcl) <= -1) goto oops;
if (hcl_attachio(proto->hcl, read_handler, print_handler) <= -1) goto oops; if (hcl_attachio(proto->hcl, read_handler, HCL_NULL, print_handler) <= -1) goto oops;
return proto; return proto;
oops: oops:
@ -943,7 +943,7 @@ static HCL_INLINE int read_char (hcl_server_proto_t* proto)
return 0; return 0;
} }
proto->lxc = hcl_readbaseinchar(proto->hcl); proto->lxc = hcl_readbasesrchar(proto->hcl);
if (!proto->lxc) return -1; if (!proto->lxc) return -1;
return 0; return 0;
} }
@ -1406,7 +1406,7 @@ int hcl_server_proto_handle_request (hcl_server_proto_t* proto)
if (proto->req.state == HCL_SERVER_PROTO_REQ_IN_TOP_LEVEL) if (proto->req.state == HCL_SERVER_PROTO_REQ_IN_TOP_LEVEL)
{ {
hcl_setbaseinloc (proto->hcl, 1, 1); hcl_setbasesrloc (proto->hcl, 1, 1);
hcl_reset(proto->hcl); hcl_reset(proto->hcl);
} }

View File

@ -854,7 +854,7 @@ struct hcl_class_t
{ {
HCL_OBJ_HEADER; HCL_OBJ_HEADER;
hcl_oop_t mdic; /* method dictioanry. nil or a dictionary object */ hcl_oop_t mdic; /* method dictionary. nil or a dictionary object */
hcl_oop_t superclass; hcl_oop_t superclass;
hcl_oop_t nivars; /* smooi. */ hcl_oop_t nivars; /* smooi. */
@ -1217,8 +1217,8 @@ struct hcl_iolxc_t
}; };
typedef struct hcl_iolxc_t hcl_iolxc_t; typedef struct hcl_iolxc_t hcl_iolxc_t;
typedef struct hcl_ioinarg_t hcl_ioinarg_t; typedef struct hcl_iosrarg_t hcl_iosrarg_t;
struct hcl_ioinarg_t struct hcl_iosrarg_t
{ {
/** /**
* [IN] I/O object name. * [IN] I/O object name.
@ -1249,7 +1249,7 @@ struct hcl_ioinarg_t
* [IN] points to the data of the includer. It is #HCL_NULL for the * [IN] points to the data of the includer. It is #HCL_NULL for the
* main stream. * main stream.
*/ */
hcl_ioinarg_t* includer; hcl_iosrarg_t* includer;
/*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/
/*----------- from here down, internal use only -------------------*/ /*----------- from here down, internal use only -------------------*/
@ -1267,12 +1267,34 @@ struct hcl_ioinarg_t
/*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/
}; };
typedef struct hcl_ioinarg_t hcl_ioinarg_t;
struct hcl_ioinarg_t
{
/**
* [OUT] I/O handle set by a handler.
* The stream handler can set this field when it opens a stream.
* All subsequent operations on the stream see this field as set
* during opening.
*/
void* handle;
/**
* [OUT] place data here for #HCL_IO_READ
*/
hcl_ooch_t buf[2048]; /* TODO: resize this if necessary */
/**
* [OUT] place the number of characters read here for #HCL_IO_READ
*/
hcl_oow_t xlen;
};
typedef struct hcl_iooutarg_t hcl_iooutarg_t; typedef struct hcl_iooutarg_t hcl_iooutarg_t;
struct hcl_iooutarg_t struct hcl_iooutarg_t
{ {
/** /**
* [OUT] I/O handle set by a handler. * [OUT] I/O handle set by a handler.
* The source stream handler can set this field when it opens a stream. * The stream handler can set this field when it opens a stream.
* All subsequent operations on the stream see this field as set * All subsequent operations on the stream see this field as set
* during opening. * during opening.
*/ */
@ -1302,7 +1324,7 @@ struct hcl_iooutarg_t
typedef int (*hcl_ioimpl_t) ( typedef int (*hcl_ioimpl_t) (
hcl_t* hcl, hcl_t* hcl,
hcl_iocmd_t cmd, hcl_iocmd_t cmd,
void* arg /* hcl_ioinarg_t* or hcl_iooutarg_t* */ void* arg /* hcl_iosrarg_t* or hcl_iooutarg_t* */
); );
/* ========================================================================= /* =========================================================================
@ -1723,6 +1745,10 @@ struct hcl_t
struct struct
{ {
/* input handler */
hcl_ioimpl_t scanner;
hcl_ioinarg_t inarg;
/* output handler */ /* output handler */
hcl_ioimpl_t printer; hcl_ioimpl_t printer;
hcl_iooutarg_t outarg; hcl_iooutarg_t outarg;
@ -2165,29 +2191,30 @@ HCL_EXPORT void hcl_abort (
# define hcl_switchprocess(hcl) ((hcl)->switch_proc = 1) # define hcl_switchprocess(hcl) ((hcl)->switch_proc = 1)
#endif #endif
HCL_EXPORT void hcl_setbaseinloc ( HCL_EXPORT void hcl_setbasesrloc (
hcl_t* hcl, hcl_t* hcl,
hcl_oow_t line, hcl_oow_t line,
hcl_oow_t colm hcl_oow_t colm
); );
/* if you should read charcters from the input stream before hcl_read(), /* if you should read charcters from the input stream before hcl_read(),
* you can call hcl_readbaseinchar() */ * you can call hcl_readbasesrchar() */
HCL_EXPORT hcl_iolxc_t* hcl_readbaseinchar ( HCL_EXPORT hcl_iolxc_t* hcl_readbasesrchar (
hcl_t* hcl hcl_t* hcl
); );
/* TODO: don't expose hcl_readbaseinraw() /* TODO: don't expose hcl_readbasesrraw()
* find a better way not to use them */ * find a better way not to use them */
HCL_EXPORT hcl_ooch_t* hcl_readbaseinraw ( HCL_EXPORT hcl_ooch_t* hcl_readbasesrraw (
hcl_t* hcl, hcl_t* hcl,
hcl_oow_t* xlen hcl_oow_t* xlen
); );
HCL_EXPORT int hcl_attachio ( HCL_EXPORT int hcl_attachio (
hcl_t* hcl, hcl_t* hcl,
hcl_ioimpl_t reader, hcl_ioimpl_t reader, /* source stream heandler */
hcl_ioimpl_t printer hcl_ioimpl_t scanner, /* runtime input stream handler */
hcl_ioimpl_t printer /* runtime output stream handler */
); );
HCL_EXPORT int hcl_attachiostdwithbcstr ( HCL_EXPORT int hcl_attachiostdwithbcstr (

View File

@ -452,7 +452,7 @@ static int get_directive_token_type (hcl_t* hcl, hcl_iotok_type_t* tok_type)
return -1; return -1;
} }
static int _get_char (hcl_t* hcl, hcl_ioinarg_t* inp) static int _get_char (hcl_t* hcl, hcl_iosrarg_t* inp)
{ {
hcl_ooci_t lc; hcl_ooci_t lc;
@ -1393,50 +1393,78 @@ retry:
return 0; return 0;
} }
static void clear_io_names (hcl_t* hcl) static int is_sr_name_in_use (hcl_t* hcl, const hcl_ooch_t* sr_name)
{
/* [NOTE]
* this is very error prone. if there are changes in refernece
* points of this sr_name in the source code, this function also
* must be modifed. */
hcl_iosrarg_t* cur;
if (hcl->c->synerr.loc.file == sr_name) return 1;
cur = hcl->c->curinp;
while (cur)
{
if (cur->lxc.l.file == sr_name) return 1;
cur = cur->includer;
}
return 0;
}
static void clear_sr_names (hcl_t* hcl)
{ {
hcl_iolink_t* cur; hcl_iolink_t* cur;
HCL_ASSERT (hcl, hcl->c != HCL_NULL); HCL_ASSERT (hcl, hcl->c != HCL_NULL);
while (hcl->c->io_names) while (hcl->c->sr_names)
{ {
cur = hcl->c->io_names; cur = hcl->c->sr_names;
hcl->c->io_names = cur->link; hcl->c->sr_names = cur->link;
hcl_freemem (hcl, cur); hcl_freemem (hcl, cur);
} }
} }
static const hcl_ooch_t* add_io_name (hcl_t* hcl, const hcl_oocs_t* name) static const hcl_ooch_t* add_sr_name (hcl_t* hcl, const hcl_oocs_t* name)
{ {
hcl_iolink_t* link; hcl_iolink_t* link;
hcl_ooch_t* ptr; hcl_ooch_t* nptr;
/* TODO: make search faster */
link = hcl->c->sr_names;
while (link)
{
nptr = (hcl_ooch_t*)(link + 1);
if (hcl_comp_oochars_oocstr(name->ptr, name->len, nptr) == 0) return nptr;
link = link->link;
}
link = (hcl_iolink_t*)hcl_callocmem (hcl, HCL_SIZEOF(*link) + HCL_SIZEOF(hcl_ooch_t) * (name->len + 1)); link = (hcl_iolink_t*)hcl_callocmem (hcl, HCL_SIZEOF(*link) + HCL_SIZEOF(hcl_ooch_t) * (name->len + 1));
if (HCL_UNLIKELY(!link)) return HCL_NULL; if (HCL_UNLIKELY(!link)) return HCL_NULL;
ptr = (hcl_ooch_t*)(link + 1); nptr = (hcl_ooch_t*)(link + 1);
hcl_copy_oochars (ptr, name->ptr, name->len); hcl_copy_oochars (nptr, name->ptr, name->len);
ptr[name->len] = '\0'; nptr[name->len] = '\0';
link->link = hcl->c->io_names; link->link = hcl->c->sr_names;
hcl->c->io_names = link; hcl->c->sr_names = link;
return ptr; return nptr;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static int begin_include (hcl_t* hcl) static int begin_include (hcl_t* hcl)
{ {
hcl_ioinarg_t* arg; hcl_iosrarg_t* arg;
const hcl_ooch_t* io_name; const hcl_ooch_t* io_name;
io_name = add_io_name(hcl, TOKEN_NAME(hcl)); io_name = add_sr_name(hcl, TOKEN_NAME(hcl));
if (HCL_UNLIKELY(!io_name)) return -1; if (HCL_UNLIKELY(!io_name)) return -1;
arg = (hcl_ioinarg_t*)hcl_callocmem(hcl, HCL_SIZEOF(*arg)); arg = (hcl_iosrarg_t*)hcl_callocmem(hcl, HCL_SIZEOF(*arg));
if (HCL_UNLIKELY(!arg)) goto oops; if (HCL_UNLIKELY(!arg)) goto oops;
arg->name = io_name; arg->name = io_name;
@ -1487,9 +1515,9 @@ oops:
static int end_include (hcl_t* hcl) static int end_include (hcl_t* hcl)
{ {
int x; int x;
hcl_ioinarg_t* cur; hcl_iosrarg_t* cur;
if (hcl->c->curinp == &hcl->c->inarg) return 0; /* no include */ if (hcl->c->curinp == &hcl->c->srarg) return 0; /* no include */
/* if it is an included file, close it and /* if it is an included file, close it and
* retry to read a character from an outer file */ * retry to read a character from an outer file */
@ -2180,6 +2208,7 @@ oops:
while (hcl->c->r.st) while (hcl->c->r.st)
{ {
hcl_rstl_t* rstl; hcl_rstl_t* rstl;
rstl = hcl->c->r.st; rstl = hcl->c->r.st;
hcl->c->r.st = rstl->prev; hcl->c->r.st = rstl->prev;
if (rstl->head) hcl_freecnode (hcl, rstl->head); if (rstl->head) hcl_freecnode (hcl, rstl->head);
@ -2237,13 +2266,13 @@ static void init_feed (hcl_t* hcl)
static int feed_begin_include (hcl_t* hcl) static int feed_begin_include (hcl_t* hcl)
{ {
hcl_ioinarg_t* arg; hcl_iosrarg_t* arg;
const hcl_ooch_t* io_name; const hcl_ooch_t* io_name;
io_name = add_io_name(hcl, TOKEN_NAME(hcl)); io_name = add_sr_name(hcl, TOKEN_NAME(hcl));
if (HCL_UNLIKELY(!io_name)) return -1; if (HCL_UNLIKELY(!io_name)) return -1;
arg = (hcl_ioinarg_t*)hcl_callocmem(hcl, HCL_SIZEOF(*arg)); arg = (hcl_iosrarg_t*)hcl_callocmem(hcl, HCL_SIZEOF(*arg));
if (HCL_UNLIKELY(!arg)) goto oops; if (HCL_UNLIKELY(!arg)) goto oops;
arg->name = io_name; arg->name = io_name;
@ -2259,11 +2288,11 @@ static int feed_begin_include (hcl_t* hcl)
goto oops; goto oops;
} }
if (arg->includer == &hcl->c->inarg) /* top-level include */ if (arg->includer == &hcl->c->srarg) /* top-level include */
{ {
/* TODO: remove hcl_readbaseinchar() and clean up this part. /* TODO: remove hcl_readbasesrchar() and clean up this part.
* hcl_readbaseinchar(), if called in the middle of feeds, * hcl_readbasesrchar(), if called in the middle of feeds,
* updates hcl->c->inarg's line and colm. so use a separate * updates hcl->c->srarg's line and colm. so use a separate
* field to store the current feed location for now */ * field to store the current feed location for now */
hcl->c->feed.lx._oloc = hcl->c->feed.lx.loc; hcl->c->feed.lx._oloc = hcl->c->feed.lx.loc;
} }
@ -2291,9 +2320,9 @@ oops:
static int feed_end_include (hcl_t* hcl) static int feed_end_include (hcl_t* hcl)
{ {
int x; int x;
hcl_ioinarg_t* cur; hcl_iosrarg_t* cur;
if (hcl->c->curinp == &hcl->c->inarg) return 0; /* no include */ if (hcl->c->curinp == &hcl->c->srarg) return 0; /* no include */
/* if it is an included file, close it and /* if it is an included file, close it and
* retry to read a character from an outer file */ * retry to read a character from an outer file */
@ -2307,7 +2336,7 @@ static int feed_end_include (hcl_t* hcl)
cur = hcl->c->curinp; cur = hcl->c->curinp;
hcl->c->curinp = hcl->c->curinp->includer; hcl->c->curinp = hcl->c->curinp->includer;
if (hcl->c->curinp == &hcl->c->inarg) if (hcl->c->curinp == &hcl->c->srarg)
{ {
hcl->c->feed.lx.loc = hcl->c->feed.lx._oloc; hcl->c->feed.lx.loc = hcl->c->feed.lx._oloc;
} }
@ -2332,6 +2361,19 @@ static int feed_end_include (hcl_t* hcl)
return 1; /* ended the included file successfully */ return 1; /* ended the included file successfully */
} }
static void feed_clean_up_reader_stack (hcl_t* hcl)
{
/* clean up the reader stack for a list */
while (hcl->c->r.st)
{
hcl_rstl_t* rstl;
rstl = hcl->c->r.st;
hcl->c->r.st = rstl->prev;
if (rstl->head) hcl_freecnode (hcl, rstl->head);
hcl_freemem (hcl, rstl);
}
}
static int feed_process_token (hcl_t* hcl) static int feed_process_token (hcl_t* hcl)
{ {
hcl_frd_t* frd = &hcl->c->feed.rd; hcl_frd_t* frd = &hcl->c->feed.rd;
@ -2730,19 +2772,10 @@ oops:
} }
/* clean up the reader stack for a list */ /* clean up the reader stack for a list */
while (hcl->c->r.st) feed_clean_up_reader_stack (hcl);
{
hcl_rstl_t* rstl;
rstl = hcl->c->r.st;
hcl->c->r.st = rstl->prev;
if (rstl->head) hcl_freecnode (hcl, rstl->head);
hcl_freemem (hcl, rstl);
}
return -1; return -1;
} }
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
struct delim_token_t struct delim_token_t
@ -3716,7 +3749,7 @@ static int feed_from_includee (hcl_t* hcl)
{ {
int x; int x;
HCL_ASSERT (hcl, hcl->c->curinp != HCL_NULL && hcl->c->curinp != &hcl->c->inarg); HCL_ASSERT (hcl, hcl->c->curinp != HCL_NULL && hcl->c->curinp != &hcl->c->srarg);
do do
{ {
@ -3757,7 +3790,7 @@ static int feed_from_includee (hcl_t* hcl)
if (feed_begin_include(hcl) <= -1) return -1; if (feed_begin_include(hcl) <= -1) return -1;
} }
} }
while (hcl->c->curinp != &hcl->c->inarg); while (hcl->c->curinp != &hcl->c->srarg);
return 0; return 0;
} }
@ -3792,7 +3825,7 @@ int hcl_feed (hcl_t* hcl, const hcl_ooch_t* data, hcl_oow_t len)
for (i = 0; i < len; ) for (i = 0; i < len; )
{ {
x = feed_char(hcl, data[i]); x = feed_char(hcl, data[i]);
if (x <= -1) return -1; /* TODO: return the number of processed characters via an argument? */ if (x <= -1) goto oops; /* TODO: return the number of processed characters via an argument? */
if (x > 0) if (x > 0)
{ {
@ -3803,14 +3836,14 @@ int hcl_feed (hcl_t* hcl, const hcl_ooch_t* data, hcl_oow_t len)
if (hcl->c->feed.rd.do_include_file) if (hcl->c->feed.rd.do_include_file)
{ {
if (feed_begin_include(hcl) <= -1) return -1; if (feed_begin_include(hcl) <= -1) goto oops;
hcl->c->feed.rd.do_include_file = 0; hcl->c->feed.rd.do_include_file = 0;
} }
if (hcl->c->curinp && hcl->c->curinp != &hcl->c->inarg && feed_from_includee(hcl) <= -1) if (hcl->c->curinp && hcl->c->curinp != &hcl->c->srarg && feed_from_includee(hcl) <= -1)
{ {
/* TODO: return the number of processed characters via an argument? */ /* TODO: return the number of processed characters via an argument? */
return -1; goto oops;
} }
/* feed data[i] again if not consumed */ /* feed data[i] again if not consumed */
@ -3832,7 +3865,7 @@ int hcl_feed (hcl_t* hcl, const hcl_ooch_t* data, hcl_oow_t len)
} }
else else
{ {
return -1; goto oops;
} }
} }
i += x; i += x;
@ -3840,6 +3873,10 @@ int hcl_feed (hcl_t* hcl, const hcl_ooch_t* data, hcl_oow_t len)
} }
return 0; return 0;
oops:
feed_clean_up_reader_stack (hcl);
return -1;
} }
@ -3926,7 +3963,7 @@ static void fini_compiler_cb (hcl_t* hcl)
hcl->c->fnblk.depth = -1; hcl->c->fnblk.depth = -1;
} }
clear_io_names (hcl); clear_sr_names (hcl);
if (hcl->c->tok.name.ptr) hcl_freemem (hcl, hcl->c->tok.name.ptr); if (hcl->c->tok.name.ptr) hcl_freemem (hcl, hcl->c->tok.name.ptr);
hcl_detachio (hcl); hcl_detachio (hcl);
@ -3984,75 +4021,104 @@ static int init_compiler (hcl_t* hcl)
return 0; return 0;
} }
int hcl_attachio (hcl_t* hcl, hcl_ioimpl_t reader, hcl_ioimpl_t printer) int hcl_attachio (hcl_t* hcl, hcl_ioimpl_t reader, hcl_ioimpl_t scanner, hcl_ioimpl_t printer)
{ {
int n; int n;
int inited_compiler = 0; int inited_compiler = 0;
hcl_iosrarg_t new_srarg;
hcl_ioinarg_t new_inarg; hcl_ioinarg_t new_inarg;
hcl_iooutarg_t new_outarg; hcl_iooutarg_t new_outarg;
if (!reader || !printer) if (reader)
{ {
hcl_seterrbfmt (hcl, HCL_EINVAL, "reader and/or printer not supplied"); if (!hcl->c)
return -1; {
if (init_compiler(hcl) <= -1) return -1;
inited_compiler = 1;
}
} }
if (!hcl->c) if (reader)
{ {
if (init_compiler(hcl) <= -1) return -1; /* The name field and the includer field are HCL_NULL
inited_compiler = 1; * for the main stream */
HCL_MEMSET (&new_srarg, 0, HCL_SIZEOF(new_srarg));
new_srarg.line = 1;
new_srarg.colm = 1;
/* open the top-level source input stream */
n = reader(hcl, HCL_IO_OPEN, &new_srarg);
if (n <= -1) goto oops;
} }
#if 0 if (scanner)
/* Some IO names could have been stored in earlier calls to this function.
* I clear such names before i begin this function. i don't clear them
* at the end of this function because they may be referenced as an error
* location */
clear_io_names (hcl);
#endif
/* The name field and the includer field are HCL_NULL
* for the main stream */
HCL_MEMSET (&new_inarg, 0, HCL_SIZEOF(new_inarg));
new_inarg.line = 1;
new_inarg.colm = 1;
/* open the top-level source input stream */
n = reader(hcl, HCL_IO_OPEN, &new_inarg);
if (n <= -1) goto oops;
/* open the new output stream */
HCL_MEMSET (&new_outarg, 0, HCL_SIZEOF(new_outarg));
n = printer(hcl, HCL_IO_OPEN, &new_outarg);
if (n <= -1)
{ {
reader (hcl, HCL_IO_CLOSE, &new_inarg); HCL_MEMSET (&new_inarg, 0, HCL_SIZEOF(new_inarg));
goto oops; n = scanner(hcl, HCL_IO_OPEN, &new_inarg);
if (n <= -1)
{
reader (hcl, HCL_IO_CLOSE, &new_srarg);
goto oops;
}
} }
if (hcl->c->reader) if (printer)
{ {
/* close the old source input stream */ /* open the new output stream */
hcl->c->reader (hcl, HCL_IO_CLOSE, &hcl->c->inarg); HCL_MEMSET (&new_outarg, 0, HCL_SIZEOF(new_outarg));
n = printer(hcl, HCL_IO_OPEN, &new_outarg);
if (n <= -1)
{
if (scanner) scanner (hcl, HCL_IO_CLOSE, &new_inarg);
if (reader) reader (hcl, HCL_IO_CLOSE, &new_srarg);
goto oops;
}
} }
hcl->c->reader = reader;
hcl->c->inarg = new_inarg;
if (hcl->io.printer) if (reader)
{ {
/* close the old output stream */ if (hcl->c->reader)
hcl->io.printer (hcl, HCL_IO_CLOSE, &hcl->io.outarg); {
/* close the old source input stream */
hcl->c->reader (hcl, HCL_IO_CLOSE, &hcl->c->srarg);
}
hcl->c->reader = reader;
hcl->c->srarg = new_srarg;
} }
hcl->io.printer = printer;
hcl->io.outarg = new_outarg;
/* initialize some other key fields */ if (scanner)
hcl->c->nungots = 0; {
if (hcl->io.scanner)
{
/* close the old input stream */
hcl->io.scanner (hcl, HCL_IO_CLOSE, &hcl->io.inarg);
}
hcl->io.scanner = scanner;
hcl->io.inarg = new_inarg;
}
/* the source stream is open. set it as the current input stream */ if (printer)
hcl->c->curinp = &hcl->c->inarg; {
if (hcl->io.printer)
{
/* close the old output stream */
hcl->io.printer (hcl, HCL_IO_CLOSE, &hcl->io.outarg);
}
hcl->io.printer = printer;
hcl->io.outarg = new_outarg;
}
if (reader)
{
/* clear unneeded source stream names */
/*clear_sr_names (hcl, 0); */
/* initialize some other key fields */
hcl->c->nungots = 0;
/* the source stream is open. set it as the current input stream */
hcl->c->curinp = &hcl->c->srarg;
}
clear_io_names (hcl);
return 0; return 0;
oops: oops:
@ -4075,9 +4141,9 @@ void hcl_detachio (hcl_t* hcl)
{ {
if (hcl->c->reader) if (hcl->c->reader)
{ {
while (hcl->c->curinp != &hcl->c->inarg) while (hcl->c->curinp != &hcl->c->srarg)
{ {
hcl_ioinarg_t* prev; hcl_iosrarg_t* prev;
/* nothing much to do about a close error */ /* nothing much to do about a close error */
hcl->c->reader (hcl, HCL_IO_CLOSE, hcl->c->curinp); hcl->c->reader (hcl, HCL_IO_CLOSE, hcl->c->curinp);
@ -4094,6 +4160,12 @@ void hcl_detachio (hcl_t* hcl)
} }
if (hcl->io.scanner)
{
hcl->io.scanner (hcl, HCL_IO_CLOSE, &hcl->io.inarg);
hcl->io.scanner = HCL_NULL; /* ready for another attachment */
}
if (hcl->io.printer) if (hcl->io.printer)
{ {
hcl->io.printer (hcl, HCL_IO_CLOSE, &hcl->io.outarg); hcl->io.printer (hcl, HCL_IO_CLOSE, &hcl->io.outarg);
@ -4101,33 +4173,31 @@ void hcl_detachio (hcl_t* hcl)
} }
} }
void hcl_setbaseinloc (hcl_t* hcl, hcl_oow_t line, hcl_oow_t colm) void hcl_setbasesrloc (hcl_t* hcl, hcl_oow_t line, hcl_oow_t colm)
{ {
hcl->c->inarg.line = line; hcl->c->srarg.line = line;
hcl->c->inarg.colm = colm; hcl->c->srarg.colm = colm;
} }
hcl_iolxc_t* hcl_readbaseinchar (hcl_t* hcl) hcl_iolxc_t* hcl_readbasesrchar (hcl_t* hcl)
{ {
/* read a character using the base input stream. the caller must care extra /* read a character using the base input stream. the caller must care extra
* care when using this function. this function reads the main stream regardless * care when using this function. this function reads the main stream regardless
* of the inclusion status and ignores the ungot characters. */ * of the inclusion status and ignores the ungot characters. */
int n = _get_char(hcl, &hcl->c->inarg); int n = _get_char(hcl, &hcl->c->srarg);
if (n <= -1) return HCL_NULL; if (n <= -1) return HCL_NULL;
return &hcl->c->inarg.lxc; return &hcl->c->srarg.lxc;
} }
hcl_ooch_t* hcl_readbasesrraw (hcl_t* hcl, hcl_oow_t* xlen)
hcl_ooch_t* hcl_readbaseinraw (hcl_t* hcl, hcl_oow_t* xlen)
{ {
/* this function provides the raw input interface to the attached source /* this function provides the raw input interface to the attached source
* input handler. it doesn't increment line/column number, nor does it * input handler. it doesn't increment line/column number, nor does it
* care about ungot characters. it must be used with extra care */ * care about ungot characters. it must be used with extra care */
HCL_ASSERT (hcl, hcl->c != HCL_NULL); // call hio_attachio() or hio_attachiostd() first HCL_ASSERT (hcl, hcl->c != HCL_NULL); /* call hio_attachio() or hio_attachiostd() with proper arguments first */
if (hcl->c->reader(hcl, HCL_IO_READ, &hcl->c->inarg) <= -1) return HCL_NULL; if (hcl->c->reader(hcl, HCL_IO_READ, &hcl->c->srarg) <= -1) return HCL_NULL;
*xlen = hcl->c->inarg.xlen; *xlen = hcl->c->srarg.xlen;
return hcl->c->inarg.buf; return hcl->c->srarg.buf;
} }

View File

@ -3195,7 +3195,7 @@ static const hcl_bch_t* get_base_name (const hcl_bch_t* path)
return (last == HCL_NULL)? path: (last + 1); return (last == HCL_NULL)? path: (last + 1);
} }
static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg) static HCL_INLINE int open_sr_stream (hcl_t* hcl, hcl_iosrarg_t* arg)
{ {
xtn_t* xtn = GET_XTN(hcl); xtn_t* xtn = GET_XTN(hcl);
bb_t* bb = HCL_NULL; bb_t* bb = HCL_NULL;
@ -3277,7 +3277,7 @@ static HCL_INLINE int open_input (hcl_t* hcl, hcl_ioinarg_t* arg)
arg->name = hcl_dupbtooocstr(hcl, bb->fn, HCL_NULL); arg->name = hcl_dupbtooocstr(hcl, bb->fn, HCL_NULL);
/* ignore duplication failure */ /* ignore duplication failure */
/* TODO: change the type of arg->name from const hcl_ooch_t* to hcl_ooch_t*. /* TODO: change the type of arg->name from const hcl_ooch_t* to hcl_ooch_t*.
* change its specification from [IN] only to [INOUT] in hcl_ioinarg_t. */ * change its specification from [IN] only to [INOUT] in hcl_iosrarg_t. */
/* END HACK */ /* END HACK */
} }
@ -3293,7 +3293,7 @@ oops:
return -1; return -1;
} }
static HCL_INLINE int close_input (hcl_t* hcl, hcl_ioinarg_t* arg) static HCL_INLINE int close_sr_stream (hcl_t* hcl, hcl_iosrarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(hcl);*/ /*xtn_t* xtn = GET_XTN(hcl);*/
bb_t* bb; bb_t* bb;
@ -3316,7 +3316,7 @@ static HCL_INLINE int close_input (hcl_t* hcl, hcl_ioinarg_t* arg)
return 0; return 0;
} }
static HCL_INLINE int read_input (hcl_t* hcl, hcl_ioinarg_t* arg) static HCL_INLINE int read_sr_stream (hcl_t* hcl, hcl_iosrarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(hcl);*/ /*xtn_t* xtn = GET_XTN(hcl);*/
bb_t* bb; bb_t* bb;
@ -3371,13 +3371,13 @@ static int read_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
switch (cmd) switch (cmd)
{ {
case HCL_IO_OPEN: case HCL_IO_OPEN:
return open_input(hcl, (hcl_ioinarg_t*)arg); return open_sr_stream(hcl, (hcl_iosrarg_t*)arg);
case HCL_IO_CLOSE: case HCL_IO_CLOSE:
return close_input(hcl, (hcl_ioinarg_t*)arg); return close_sr_stream(hcl, (hcl_iosrarg_t*)arg);
case HCL_IO_READ: case HCL_IO_READ:
return read_input(hcl, (hcl_ioinarg_t*)arg); return read_sr_stream(hcl, (hcl_iosrarg_t*)arg);
case HCL_IO_FLUSH: case HCL_IO_FLUSH:
/* no effect on an input stream */ /* no effect on an input stream */
@ -3389,7 +3389,7 @@ static int read_handler (hcl_t* hcl, hcl_iocmd_t cmd, void* arg)
} }
} }
static HCL_INLINE int open_output (hcl_t* hcl, hcl_iooutarg_t* arg) static HCL_INLINE int open_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{ {
xtn_t* xtn = GET_XTN(hcl); xtn_t* xtn = GET_XTN(hcl);
FILE* fp; FILE* fp;
@ -3413,7 +3413,7 @@ static HCL_INLINE int open_output (hcl_t* hcl, hcl_iooutarg_t* arg)
return 0; return 0;
} }
static HCL_INLINE int close_output (hcl_t* hcl, hcl_iooutarg_t* arg) static HCL_INLINE int close_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(hcl);*/ /*xtn_t* xtn = GET_XTN(hcl);*/
FILE* fp; FILE* fp;
@ -3425,7 +3425,7 @@ static HCL_INLINE int close_output (hcl_t* hcl, hcl_iooutarg_t* arg)
return 0; return 0;
} }
static HCL_INLINE int write_output (hcl_t* hcl, hcl_iooutarg_t* arg) static HCL_INLINE int write_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{ {
/*xtn_t* xtn = GET_XTN(hcl);*/ /*xtn_t* xtn = GET_XTN(hcl);*/
hcl_bch_t bcsbuf[1024]; hcl_bch_t bcsbuf[1024];
@ -3463,7 +3463,7 @@ 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) static HCL_INLINE int flush_out_stream (hcl_t* hcl, hcl_iooutarg_t* arg)
{ {
FILE* fp; FILE* fp;
@ -3479,16 +3479,16 @@ 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_out_stream(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_CLOSE: case HCL_IO_CLOSE:
return close_output(hcl, (hcl_iooutarg_t*)arg); return close_out_stream(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_WRITE: case HCL_IO_WRITE:
return write_output(hcl, (hcl_iooutarg_t*)arg); return write_out_stream(hcl, (hcl_iooutarg_t*)arg);
case HCL_IO_FLUSH: case HCL_IO_FLUSH:
return flush_output(hcl, (hcl_iooutarg_t*)arg); return flush_out_stream(hcl, (hcl_iooutarg_t*)arg);
default: default:
hcl_seterrnum (hcl, HCL_EINTERN); hcl_seterrnum (hcl, HCL_EINTERN);
@ -3507,7 +3507,7 @@ int hcl_attachiostdwithbcstr (hcl_t* hcl, const hcl_bch_t* read_file, const hcl_
xtn->read_path = read_file; xtn->read_path = read_file;
xtn->print_path = print_file; xtn->print_path = print_file;
n = hcl_attachio(hcl, read_handler, print_handler); n = hcl_attachio(hcl, read_handler, HCL_NULL, print_handler);
xtn->read_path = HCL_NULL; xtn->read_path = HCL_NULL;
xtn->print_path = HCL_NULL; xtn->print_path = HCL_NULL;
@ -3534,7 +3534,7 @@ int hcl_attachiostdwithucstr (hcl_t* hcl, const hcl_uch_t* read_file, const hcl_
return -1; return -1;
} }
n = hcl_attachio(hcl, read_handler, print_handler); n = hcl_attachio(hcl, read_handler, HCL_NULL, print_handler);
hcl_freemem (hcl, xtn->read_path); hcl_freemem (hcl, xtn->read_path);
hcl_freemem (hcl, xtn->print_path); hcl_freemem (hcl, xtn->print_path);