deprecated qse_awk_runsimple() and created qse_awk_rtx_opensimple()

- still need to clean up qse_awk_rtx_opensimple
- deprecated on_start and on_end in qse_awk_rcb_t
- modified other parts according to the changes
This commit is contained in:
hyung-hwan 2009-02-14 04:57:09 +00:00
parent 527e1a23de
commit 172725273c
6 changed files with 140 additions and 258 deletions

View File

@ -32,7 +32,7 @@
# include <errno.h>
#endif
static qse_awk_rtx_t* app_run = NULL;
static qse_awk_rtx_t* app_rtx = NULL;
static int app_debug = 0;
struct argout_t
@ -65,7 +65,7 @@ static BOOL WINAPI stop_run (DWORD ctrl_type)
if (ctrl_type == CTRL_C_EVENT ||
ctrl_type == CTRL_CLOSE_EVENT)
{
qse_awk_rtx_stop (app_run);
qse_awk_rtx_stop (app_rtx);
return TRUE;
}
@ -100,7 +100,7 @@ static int setsignal (int sig, void(*handler)(int), int restart)
static void stop_run (int sig)
{
int e = errno;
qse_awk_rtx_stop (app_run);
qse_awk_rtx_stop (app_rtx);
errno = e;
}
@ -124,15 +124,6 @@ static void unset_intr_run (void)
#endif
}
static int on_run_start (qse_awk_rtx_t* run, void* data)
{
app_run = run;
set_intr_run ();
dprint (QSE_T("[AWK ABOUT TO START]\n"));
return 0;
}
static qse_map_walk_t print_awk_value (
qse_map_t* map, qse_map_pair_t* pair, void* arg)
{
@ -211,22 +202,6 @@ static void on_run_exit (
dprint (QSE_T("[END NAMED VARIABLES]\n"));
}
static void on_run_end (qse_awk_rtx_t* run, int errnum, void* data)
{
if (errnum != QSE_AWK_ENOERR)
{
dprint (QSE_T("[AWK ENDED WITH AN ERROR]\n"));
qse_printf (QSE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
errnum,
(unsigned int)qse_awk_rtx_geterrlin(run),
qse_awk_rtx_geterrmsg(run));
}
else dprint (QSE_T("[AWK ENDED SUCCESSFULLY]\n"));
unset_intr_run ();
app_run = NULL;
}
/* TODO: remove otab... */
static struct
{
@ -585,7 +560,7 @@ static qse_awk_t* open_awk (void)
static int awk_main (int argc, qse_char_t* argv[])
{
qse_awk_t* awk;
qse_awk_rtx_t* rtx;
qse_awk_rcb_t rcb;
int i;
@ -624,24 +599,43 @@ static int awk_main (int argc, qse_char_t* argv[])
goto oops;
}
rcb.on_start = on_run_start;
rcb.on_enter = on_run_enter;
rcb.on_statement = on_run_statement;
rcb.on_exit = on_run_exit;
rcb.on_end = on_run_end;
rcb.data = &ao;
if (qse_awk_runsimple (awk, ao.icf, &rcb) == -1)
rtx = qse_awk_rtx_opensimple (awk, ao.icf);
if (rtx == QSE_NULL)
{
qse_printf (
QSE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
QSE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
qse_awk_geterrnum(awk),
(unsigned int)qse_awk_geterrlin(awk),
qse_awk_geterrmsg(awk)
);
ret = -1;
goto oops;
}
else
{
app_rtx = rtx;
set_intr_run ();
qse_awk_rtx_setrcb (rtx, &rcb);
ret = qse_awk_rtx_loop (rtx);
unset_intr_run ();
if (ret == -1)
{
qse_printf (QSE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
(unsigned int)qse_awk_rtx_geterrnum(rtx),
(unsigned int)qse_awk_rtx_geterrlin(rtx),
qse_awk_rtx_geterrmsg(rtx)
);
}
qse_awk_rtx_close (rtx);
}
oops:

View File

@ -134,9 +134,6 @@ struct qse_awk_rio_t
struct qse_awk_rcb_t
{
int (*on_start) (
qse_awk_rtx_t* rtx, void* data);
int (*on_enter) (
qse_awk_rtx_t* rtx, void* data);
@ -146,9 +143,6 @@ struct qse_awk_rcb_t
void (*on_exit) (
qse_awk_rtx_t* rtx, qse_awk_val_t* ret, void* data);
void (*on_end) (
qse_awk_rtx_t* rtx, int errnum, void* data);
void* data;
};
@ -1548,31 +1542,17 @@ int qse_awk_parsesimple (
);
/******/
/****f* AWK/qse_awk_runsimple
/****f* AWK/qse_awk_rtx_opensimple
* NAME
* qse_awk_runsimple - run a parsed program
* DESCRIPTION
* A runtime context is required for it to start running the program.
* Once a runtime context is created, the program starts to run.
* The failure of context creation is reported by the return value of -1.
* However, the runtime error after context creation is reported differently
* depending on the callbacks specified. When no callback is specified
* (i.e. rcb is QSE_NULL), the qse_awk_run() function returns -1 on an
* error and awk->errnum is set accordingly. If a callback is specified
* (i.e. rcb is not QSE_NULL), the qse_awk_run() returns 0 on both success
* and failure. Instead, the on_end handler of the callback is triggered with
* the relevant error number. The third parameter to on_end is the error
* number.
* qse_awk_rtx_opensimple - create a runtime context
* SYNOPSIS
*/
int qse_awk_runsimple (
qse_awk_t* awk,
qse_char_t** icf /* input console files */,
qse_awk_rcb_t* cbs /* callbacks */
qse_awk_rtx_t* qse_awk_rtx_opensimple (
qse_awk_t* awk,
qse_char_t** icf
);
/******/
#ifdef __cplusplus
}
#endif

View File

@ -1348,8 +1348,9 @@ int Awk::run (const char_t** args, size_t nargs)
if (runCallback)
{
QSE_MEMSET (&rcb, 0, QSE_SIZEOF(rcb));
rcb.on_start = onRunStart;
rcb.on_end = onRunEnd;
// TODO: deprecate onRunStart and onRunEnd
//rcb.on_start = onRunStart;
//rcb.on_end = onRunEnd;
rcb.on_enter = onRunEnter;
rcb.on_statement = onRunStatement;
rcb.on_exit = onRunExit;

View File

@ -372,7 +372,7 @@ struct qse_awk_rtx_t
qse_char_t errmsg[256];
qse_awk_t* awk;
qse_awk_rcb_t* rcb;
qse_awk_rcb_t rcb;
};

View File

@ -630,7 +630,6 @@ qse_mmgr_t* qse_awk_rtx_getmmgr (qse_awk_rtx_t* rtx)
return rtx->awk->mmgr;
}
qse_map_t* qse_awk_rtx_getnvmap (qse_awk_rtx_t* rtx)
{
return rtx->named;
@ -712,12 +711,12 @@ qse_bool_t qse_awk_rtx_shouldstop (qse_awk_rtx_t* rtx)
qse_awk_rcb_t* qse_awk_rtx_getrcb (qse_awk_rtx_t* rtx)
{
return rtx->rcb;
return &rtx->rcb;
}
void qse_awk_rtx_setrcb (qse_awk_rtx_t* rtx, qse_awk_rcb_t* rcb)
{
rtx->rcb = rcb;
rtx->rcb = *rcb;
}
static void free_namedval (qse_map_t* map, void* dptr, qse_size_t dlen)
@ -738,7 +737,6 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio)
QSE_MEMSET (rtx, 0, QSE_SIZEOF(qse_awk_rtx_t));
rtx->awk = awk;
rtx->rcb = QSE_NULL;
rtx->stack = QSE_NULL;
rtx->stack_top = 0;
@ -859,124 +857,124 @@ static int init_rtx (qse_awk_rtx_t* rtx, qse_awk_t* awk, qse_awk_rio_t* rio)
return 0;
}
static void fini_rtx (qse_awk_rtx_t* run)
static void fini_rtx (qse_awk_rtx_t* rtx)
{
if (run->pattern_range_state != QSE_NULL)
QSE_AWK_FREE (run->awk, run->pattern_range_state);
if (rtx->pattern_range_state != QSE_NULL)
QSE_AWK_FREE (rtx->awk, rtx->pattern_range_state);
/* close all pending eio's */
/* TODO: what if this operation fails? */
qse_awk_cleareio (run);
QSE_ASSERT (run->eio.chain == QSE_NULL);
qse_awk_cleareio (rtx);
QSE_ASSERT (rtx->eio.chain == QSE_NULL);
if (run->gbl.rs != QSE_NULL)
if (rtx->gbl.rs != QSE_NULL)
{
QSE_AWK_FREE (run->awk, run->gbl.rs);
run->gbl.rs = QSE_NULL;
QSE_AWK_FREE (rtx->awk, rtx->gbl.rs);
rtx->gbl.rs = QSE_NULL;
}
if (run->gbl.fs != QSE_NULL)
if (rtx->gbl.fs != QSE_NULL)
{
QSE_AWK_FREE (run->awk, run->gbl.fs);
run->gbl.fs = QSE_NULL;
QSE_AWK_FREE (rtx->awk, rtx->gbl.fs);
rtx->gbl.fs = QSE_NULL;
}
if (run->gbl.convfmt.ptr != QSE_NULL &&
run->gbl.convfmt.ptr != DEFAULT_CONVFMT)
if (rtx->gbl.convfmt.ptr != QSE_NULL &&
rtx->gbl.convfmt.ptr != DEFAULT_CONVFMT)
{
QSE_AWK_FREE (run->awk, run->gbl.convfmt.ptr);
run->gbl.convfmt.ptr = QSE_NULL;
run->gbl.convfmt.len = 0;
QSE_AWK_FREE (rtx->awk, rtx->gbl.convfmt.ptr);
rtx->gbl.convfmt.ptr = QSE_NULL;
rtx->gbl.convfmt.len = 0;
}
if (run->gbl.ofmt.ptr != QSE_NULL &&
run->gbl.ofmt.ptr != DEFAULT_OFMT)
if (rtx->gbl.ofmt.ptr != QSE_NULL &&
rtx->gbl.ofmt.ptr != DEFAULT_OFMT)
{
QSE_AWK_FREE (run->awk, run->gbl.ofmt.ptr);
run->gbl.ofmt.ptr = QSE_NULL;
run->gbl.ofmt.len = 0;
QSE_AWK_FREE (rtx->awk, rtx->gbl.ofmt.ptr);
rtx->gbl.ofmt.ptr = QSE_NULL;
rtx->gbl.ofmt.len = 0;
}
if (run->gbl.ofs.ptr != QSE_NULL &&
run->gbl.ofs.ptr != DEFAULT_OFS)
if (rtx->gbl.ofs.ptr != QSE_NULL &&
rtx->gbl.ofs.ptr != DEFAULT_OFS)
{
QSE_AWK_FREE (run->awk, run->gbl.ofs.ptr);
run->gbl.ofs.ptr = QSE_NULL;
run->gbl.ofs.len = 0;
QSE_AWK_FREE (rtx->awk, rtx->gbl.ofs.ptr);
rtx->gbl.ofs.ptr = QSE_NULL;
rtx->gbl.ofs.len = 0;
}
if (run->gbl.ors.ptr != QSE_NULL &&
run->gbl.ors.ptr != DEFAULT_ORS &&
run->gbl.ors.ptr != DEFAULT_ORS_CRLF)
if (rtx->gbl.ors.ptr != QSE_NULL &&
rtx->gbl.ors.ptr != DEFAULT_ORS &&
rtx->gbl.ors.ptr != DEFAULT_ORS_CRLF)
{
QSE_AWK_FREE (run->awk, run->gbl.ors.ptr);
run->gbl.ors.ptr = QSE_NULL;
run->gbl.ors.len = 0;
QSE_AWK_FREE (rtx->awk, rtx->gbl.ors.ptr);
rtx->gbl.ors.ptr = QSE_NULL;
rtx->gbl.ors.len = 0;
}
if (run->gbl.subsep.ptr != QSE_NULL &&
run->gbl.subsep.ptr != DEFAULT_SUBSEP)
if (rtx->gbl.subsep.ptr != QSE_NULL &&
rtx->gbl.subsep.ptr != DEFAULT_SUBSEP)
{
QSE_AWK_FREE (run->awk, run->gbl.subsep.ptr);
run->gbl.subsep.ptr = QSE_NULL;
run->gbl.subsep.len = 0;
QSE_AWK_FREE (rtx->awk, rtx->gbl.subsep.ptr);
rtx->gbl.subsep.ptr = QSE_NULL;
rtx->gbl.subsep.len = 0;
}
QSE_AWK_FREE (run->awk, run->format.tmp.ptr);
run->format.tmp.ptr = QSE_NULL;
run->format.tmp.len = 0;
qse_str_fini (&run->format.fmt);
qse_str_fini (&run->format.out);
QSE_AWK_FREE (rtx->awk, rtx->format.tmp.ptr);
rtx->format.tmp.ptr = QSE_NULL;
rtx->format.tmp.len = 0;
qse_str_fini (&rtx->format.fmt);
qse_str_fini (&rtx->format.out);
/* destroy input record. qse_awk_rtx_clrrec should be called
* before the run stack has been destroyed because it may try
* before the rtx stack has been destroyed because it may try
* to change the value to QSE_AWK_GBL_NF. */
qse_awk_rtx_clrrec (run, QSE_FALSE);
if (run->inrec.flds != QSE_NULL)
qse_awk_rtx_clrrec (rtx, QSE_FALSE);
if (rtx->inrec.flds != QSE_NULL)
{
QSE_AWK_FREE (run->awk, run->inrec.flds);
run->inrec.flds = QSE_NULL;
run->inrec.maxflds = 0;
QSE_AWK_FREE (rtx->awk, rtx->inrec.flds);
rtx->inrec.flds = QSE_NULL;
rtx->inrec.maxflds = 0;
}
qse_str_fini (&run->inrec.line);
qse_str_fini (&rtx->inrec.line);
/* destroy the stack if necessary */
if (run->stack != QSE_NULL)
if (rtx->stack != QSE_NULL)
{
QSE_ASSERT (run->stack_top == 0);
QSE_ASSERT (rtx->stack_top == 0);
QSE_AWK_FREE (run->awk, run->stack);
run->stack = QSE_NULL;
run->stack_top = 0;
run->stack_base = 0;
run->stack_limit = 0;
QSE_AWK_FREE (rtx->awk, rtx->stack);
rtx->stack = QSE_NULL;
rtx->stack_top = 0;
rtx->stack_base = 0;
rtx->stack_limit = 0;
}
/* destroy named variables */
qse_map_close (run->named);
qse_map_close (rtx->named);
/* destroy values in free list */
while (run->fcache_count > 0)
while (rtx->fcache_count > 0)
{
qse_awk_val_ref_t* tmp = run->fcache[--run->fcache_count];
qse_awk_rtx_freeval (run, (qse_awk_val_t*)tmp, QSE_FALSE);
qse_awk_val_ref_t* tmp = rtx->fcache[--rtx->fcache_count];
qse_awk_rtx_freeval (rtx, (qse_awk_val_t*)tmp, QSE_FALSE);
}
/*while (run->scache32_count > 0)
/*while (rtx->scache32_count > 0)
{
qse_awk_val_str_t* tmp = run->scache32[--run->scache32_count];
qse_awk_rtx_freeval (run, (qse_awk_val_t*)tmp, QSE_FALSE);
qse_awk_val_str_t* tmp = rtx->scache32[--rtx->scache32_count];
qse_awk_rtx_freeval (rtx, (qse_awk_val_t*)tmp, QSE_FALSE);
}
while (run->scache64_count > 0)
while (rtx->scache64_count > 0)
{
qse_awk_val_str_t* tmp = run->scache64[--run->scache64_count];
qse_awk_rtx_freeval (run, (qse_awk_val_t*)tmp, QSE_FALSE);
qse_awk_val_str_t* tmp = rtx->scache64[--rtx->scache64_count];
qse_awk_rtx_freeval (rtx, (qse_awk_val_t*)tmp, QSE_FALSE);
}*/
qse_awk_rtx_freevalchunk (run, run->vmgr.ichunk);
qse_awk_rtx_freevalchunk (run, run->vmgr.rchunk);
run->vmgr.ichunk = QSE_NULL;
run->vmgr.rchunk = QSE_NULL;
qse_awk_rtx_freevalchunk (rtx, rtx->vmgr.ichunk);
qse_awk_rtx_freevalchunk (rtx, rtx->vmgr.rchunk);
rtx->vmgr.ichunk = QSE_NULL;
rtx->vmgr.rchunk = QSE_NULL;
}
static int build_runarg (
@ -1336,10 +1334,10 @@ static int run_bpae_loop (qse_awk_rtx_t* rtx)
STACK_NARGS(rtx) = (void*)nargs;
/* call the callback */
if (rtx->rcb != QSE_NULL && rtx->rcb->on_enter != QSE_NULL)
if (rtx->rcb.on_enter != QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR);
ret = rtx->rcb->on_enter (rtx, rtx->rcb->data);
ret = rtx->rcb.on_enter (rtx, rtx->rcb.data);
if (ret <= -1)
{
if (rtx->errnum == QSE_AWK_ENOMEM)
@ -1438,8 +1436,8 @@ static int run_bpae_loop (qse_awk_rtx_t* rtx)
v = STACK_RETVAL(rtx);
if (ret == 0)
{
if (rtx->rcb != QSE_NULL && rtx->rcb->on_exit != QSE_NULL)
rtx->rcb->on_exit (rtx, v, rtx->rcb->data);
if (rtx->rcb.on_exit != QSE_NULL)
rtx->rcb.on_exit (rtx, v, rtx->rcb.data);
}
/* end the life of the gbl return value */
qse_awk_rtx_refdownval (rtx, v);
@ -1546,8 +1544,8 @@ int qse_awk_rtx_call (
{
if (rtx->errnum == QSE_AWK_ENOERR)
{
if (rtx->rcb != QSE_NULL && rtx->rcb->on_exit != QSE_NULL)
rtx->rcb->on_exit (rtx, crdata.val, rtx->rcb->data);
if (rtx->rcb.on_exit != QSE_NULL)
rtx->rcb.on_exit (rtx, crdata.val, rtx->rcb.data);
}
else ret = -1;
qse_awk_rtx_refdownval(rtx, crdata.val);
@ -1557,8 +1555,8 @@ int qse_awk_rtx_call (
{
qse_awk_rtx_refupval (rtx, v);
if (rtx->rcb != QSE_NULL && rtx->rcb->on_exit != QSE_NULL)
rtx->rcb->on_exit (rtx, v, rtx->rcb->data);
if (rtx->rcb.on_exit != QSE_NULL)
rtx->rcb.on_exit (rtx, v, rtx->rcb.data);
qse_awk_rtx_refdownval (rtx, v);
}
@ -1849,11 +1847,10 @@ static int run_block0 (qse_awk_rtx_t* run, qse_awk_nde_blk_t* nde)
#define ON_STATEMENT(rtx,nde) \
if ((rtx)->awk->stopall) (rtx)->exit_level = EXIT_ABORT; \
if ((rtx)->rcb != QSE_NULL && \
(rtx)->rcb->on_statement != QSE_NULL) \
if ((rtx)->rcb.on_statement != QSE_NULL) \
{ \
(rtx)->rcb->on_statement ( \
rtx, (nde)->line, (rtx)->rcb->data); \
(rtx)->rcb.on_statement ( \
rtx, (nde)->line, (rtx)->rcb.data); \
}
static int run_statement (qse_awk_rtx_t* run, qse_awk_nde_t* nde)

View File

@ -32,9 +32,19 @@ typedef struct xtn_t
qse_awk_prm_t prm;
} xtn_t;
typedef struct rio_data_t
{
struct
{
qse_char_t** files;
qse_size_t index;
} ic; /* input console */
} rio_data_t;
typedef struct rxtn_t
{
unsigned int seed;
rio_data_t rd;
} rxtn_t;
static qse_real_t custom_awk_pow (void* custom, qse_real_t x, qse_real_t y)
@ -327,15 +337,6 @@ int qse_awk_parsesimple (
/*** RUNSIMPLE ***/
typedef struct rio_data_t
{
struct
{
qse_char_t** files;
qse_size_t index;
} ic; /* input console */
} rio_data_t;
static qse_ssize_t awk_eio_pipe (
int cmd, void* arg, qse_char_t* data, qse_size_t size)
{
@ -738,26 +739,24 @@ static qse_ssize_t awk_eio_console (
return -1;
}
#if 0
qse_awk_rtx_t* qse_awk_opensimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_rcb_t* rcb)
qse_awk_rtx_t* qse_awk_rtx_opensimple (qse_awk_t* awk, qse_char_t** icf)
{
qse_awk_rtx_t* rtx;
qse_awk_rio_t rio;
rio_data_t rd;
rxtn_t* rxtn;
qse_ntime_t now;
int n;
rd.ic.files = icf;
rd.ic.index = 0;
rio.pipe = awk_eio_pipe;
rio.file = awk_eio_file;
rio.console = awk_eio_console;
rio.data = &rd;
/* TODO: here.... */
//rio.data = &rd;
rtx = qse_awk_rtx_open (
awk,
xtn + QSE_SIZEOF(rxtn),
QSE_SIZEOF(rxtn_t),
&rio,
QSE_NULL/*runarg*/
);
@ -768,101 +767,12 @@ qse_awk_rtx_t* qse_awk_opensimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_rcb
else rxtn->seed = (unsigned int) now;
srand (rxtn->seed);
//qse_awk_rtx_setrcb (rtx, rcb);
rxtn->rd.ic.files = icf;
rxtn->rd.ic.index = 0;
rtx->eio.data = &rxtn->rd;
return rtx;
}
#endif
int qse_awk_runsimple (qse_awk_t* awk, qse_char_t** icf, qse_awk_rcb_t* rcb)
{
qse_awk_rtx_t* rtx;
qse_awk_rio_t rio;
rio_data_t rd;
rxtn_t* rxtn;
qse_ntime_t now;
int n;
rd.ic.files = icf;
rd.ic.index = 0;
rio.pipe = awk_eio_pipe;
rio.file = awk_eio_file;
rio.console = awk_eio_console;
rio.data = &rd;
rtx = qse_awk_rtx_open (
awk,
QSE_SIZEOF(rxtn),
&rio,
QSE_NULL/*runarg*/
);
if (rtx == QSE_NULL) return -1;
rxtn = (rxtn_t*) qse_awk_rtx_getxtn (rtx);
if (qse_gettime (&now) == -1) rxtn->seed = 0;
else rxtn->seed = (unsigned int) now;
srand (rxtn->seed);
qse_awk_rtx_setrcb (rtx, rcb);
/* execute the start callback if it exists */
if (rcb != QSE_NULL && rcb->on_start != QSE_NULL)
{
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR);
n = rcb->on_start (rtx, rcb->data);
if (n <= -1)
{
if (rtx->errnum == QSE_AWK_ENOERR)
qse_awk_seterrnum (awk, QSE_AWK_EUNKNOWN);
n = -1;
goto oops;
}
}
n = qse_awk_rtx_loop (rtx);
if (n == -1)
{
/* if no callback is specified, awk's error number
* is updated with the run's error number */
if (rcb == QSE_NULL)
{
awk->errnum = rtx->errnum;
awk->errlin = rtx->errlin;
qse_strxcpy (
awk->errmsg, QSE_COUNTOF(awk->errmsg),
rtx->errmsg);
}
else
{
qse_awk_seterrnum (awk, QSE_AWK_ERUNTIME);
}
}
/* the run loop ended. execute the end callback if it exists */
if (rcb != QSE_NULL && rcb->on_end != QSE_NULL)
{
if (n == 0)
{
/* clear error if run is successful just in case */
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR);
}
rcb->on_end (rtx,
((n == -1)? rtx->errnum: QSE_AWK_ENOERR),
rcb->data);
/* when using callbacks, this function always returns 0
* after the start callbacks has been triggered */
n = 0;
}
oops:
qse_awk_rtx_close (rtx);
return n;
}
/*** EXTRA BUILTIN FUNCTIONS ***/
enum