*** empty log message ***
This commit is contained in:
parent
cdb1677f38
commit
1bd23651cf
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.c,v 1.68 2006-08-10 16:02:15 bacon Exp $
|
||||
* $Id: awk.c,v 1.69 2006-08-13 05:55:02 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -71,6 +71,7 @@ xp_awk_t* xp_awk_open (xp_awk_thrlks_t* thrlks)
|
||||
awk->tree.end = XP_NULL;
|
||||
awk->tree.chain = XP_NULL;
|
||||
awk->tree.chain_tail = XP_NULL;
|
||||
awk->tree.chain_size = 0;
|
||||
|
||||
awk->token.prev = 0;
|
||||
awk->token.type = 0;
|
||||
@ -172,6 +173,7 @@ int xp_awk_clear (xp_awk_t* awk)
|
||||
}
|
||||
|
||||
awk->tree.chain_tail = XP_NULL;
|
||||
awk->tree.chain_size = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk.h,v 1.94 2006-08-10 16:02:15 bacon Exp $
|
||||
* $Id: awk.h,v 1.95 2006-08-13 05:55:02 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWK_H_
|
||||
@ -18,7 +18,6 @@ typedef struct xp_awk_runios_t xp_awk_runios_t;
|
||||
typedef struct xp_awk_runcbs_t xp_awk_runcbs_t;
|
||||
|
||||
typedef void (*xp_awk_lk_t) (xp_awk_t* awk, void* arg);
|
||||
typedef void (*xp_awk_cb_t) (xp_awk_t* awk, void* handle, void* arg);
|
||||
typedef xp_ssize_t (*xp_awk_io_t) (
|
||||
int cmd, void* arg, xp_char_t* data, xp_size_t count);
|
||||
|
||||
@ -65,8 +64,8 @@ struct xp_awk_runios_t
|
||||
|
||||
struct xp_awk_runcbs_t
|
||||
{
|
||||
xp_awk_cb_t start;
|
||||
xp_awk_cb_t end;
|
||||
void (*on_start) (xp_awk_t* awk, void* handle, void* arg);
|
||||
void (*on_end) (xp_awk_t* awk, void* handle, int errnum, void* arg);
|
||||
void* custom_data;
|
||||
};
|
||||
|
||||
@ -258,10 +257,25 @@ void xp_awk_setopt (xp_awk_t* awk, int opt);
|
||||
|
||||
int xp_awk_parse (xp_awk_t* awk, xp_awk_srcios_t* srcios);
|
||||
|
||||
/*
|
||||
* xp_awk_run return 0 on success and -1 on failure, generally speaking.
|
||||
* A runtime context is required for it to start running the program.
|
||||
* Once the runtime context is created, the program starts to run.
|
||||
* The context creation failure is reported by the return value -1 of
|
||||
* this function. however, the runtime error after the context creation
|
||||
* is reported differently depending on the use of the callback.
|
||||
* When no callback is specified (i.e. runcbs is XP_NULL), xp_awk_run
|
||||
* returns -1 on an error and awk->errnum is set accordingly.
|
||||
* However, if a callback is specified (i.e. runcbs is not XP_NULL),
|
||||
* xp_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 denotes this error number.
|
||||
*/
|
||||
int xp_awk_run (xp_awk_t* awk,
|
||||
xp_awk_runios_t* runios, xp_awk_runcbs_t* runcbs);
|
||||
|
||||
int xp_awk_stop (xp_awk_t* awk, void* run);
|
||||
void xp_awk_stopall (xp_awk_t* awk);
|
||||
int xp_awk_getrunerrnum (xp_awk_t* awk, void* run, int* errnum);
|
||||
|
||||
/* functions to access internal stack structure */
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: awk_i.h,v 1.43 2006-08-10 16:02:15 bacon Exp $
|
||||
* $Id: awk_i.h,v 1.44 2006-08-13 05:55:02 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_AWKI_H_
|
||||
@ -49,6 +49,7 @@ struct xp_awk_tree_t
|
||||
xp_awk_nde_t* end;
|
||||
xp_awk_chain_t* chain;
|
||||
xp_awk_chain_t* chain_tail;
|
||||
xp_size_t chain_size; /* number of nodes in the chain */
|
||||
};
|
||||
|
||||
struct xp_awk_t
|
||||
@ -138,7 +139,6 @@ struct xp_awk_t
|
||||
struct xp_awk_chain_t
|
||||
{
|
||||
xp_awk_nde_t* pattern;
|
||||
int pattern_range_state; /* used when pattern is a range */
|
||||
xp_awk_nde_t* action;
|
||||
xp_awk_chain_t* next;
|
||||
};
|
||||
@ -160,6 +160,7 @@ struct xp_awk_run_t
|
||||
xp_size_t rcache_count;
|
||||
|
||||
xp_awk_nde_blk_t* active_block;
|
||||
xp_byte_t* pattern_range_state;
|
||||
|
||||
struct
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: parse.c,v 1.163 2006-08-10 16:02:15 bacon Exp $
|
||||
* $Id: parse.c,v 1.164 2006-08-13 05:55:02 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -841,7 +841,6 @@ static xp_awk_chain_t* __parse_pattern_block (
|
||||
}
|
||||
|
||||
chain->pattern = ptn;
|
||||
chain->pattern_range_state = 0;
|
||||
chain->action = nde;
|
||||
chain->next = XP_NULL;
|
||||
|
||||
@ -849,11 +848,13 @@ static xp_awk_chain_t* __parse_pattern_block (
|
||||
{
|
||||
awk->tree.chain = chain;
|
||||
awk->tree.chain_tail = chain;
|
||||
awk->tree.chain_size++;
|
||||
}
|
||||
else
|
||||
{
|
||||
awk->tree.chain_tail->next = chain;
|
||||
awk->tree.chain_tail = chain;
|
||||
awk->tree.chain_size++;
|
||||
}
|
||||
|
||||
return chain;
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: run.c,v 1.164 2006-08-10 16:02:15 bacon Exp $
|
||||
* $Id: run.c,v 1.165 2006-08-13 05:55:02 bacon Exp $
|
||||
*/
|
||||
|
||||
#include <xp/awk/awk_i.h>
|
||||
@ -52,8 +52,10 @@ static void __deinit_run (xp_awk_run_t* run);
|
||||
|
||||
static int __run_main (xp_awk_run_t* run);
|
||||
static int __run_pattern_blocks (xp_awk_run_t* run);
|
||||
static int __run_pattern_block_chain (xp_awk_run_t* run, xp_awk_chain_t* chain);
|
||||
static int __run_pattern_block (xp_awk_run_t* run, xp_awk_chain_t* chain);
|
||||
static int __run_pattern_block_chain (
|
||||
xp_awk_run_t* run, xp_awk_chain_t* chain);
|
||||
static int __run_pattern_block (
|
||||
xp_awk_run_t* run, xp_awk_chain_t* chain, xp_size_t block_no);
|
||||
static int __run_block (xp_awk_run_t* run, xp_awk_nde_blk_t* nde);
|
||||
static int __run_statement (xp_awk_run_t* run, xp_awk_nde_t* nde);
|
||||
static int __run_if (xp_awk_run_t* run, xp_awk_nde_if_t* nde);
|
||||
@ -246,14 +248,29 @@ int xp_awk_run (xp_awk_t* awk, xp_awk_runios_t* runios, xp_awk_runcbs_t* runcbs)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (runcbs->start != XP_NULL)
|
||||
runcbs->start (awk, run, runcbs->custom_data);
|
||||
if (runcbs != XP_NULL && runcbs->on_start != XP_NULL)
|
||||
{
|
||||
runcbs->on_start (awk, run, runcbs->custom_data);
|
||||
}
|
||||
|
||||
n = __run_main (run);
|
||||
if (n == -1) awk->errnum = XP_AWK_ERUNTIME;
|
||||
if (n == -1)
|
||||
{
|
||||
/* if no callback is specified, awk's error number
|
||||
* is updated with the run's error number */
|
||||
awk->errnum = (runcbs == XP_NULL)? run->errnum: XP_AWK_ERUNTIME;
|
||||
}
|
||||
|
||||
if (runcbs->end != XP_NULL)
|
||||
runcbs->end (awk, run, runcbs->custom_data);
|
||||
if (runcbs != XP_NULL && runcbs->on_end != XP_NULL)
|
||||
{
|
||||
runcbs->on_end (awk, run,
|
||||
((n == -1)? run->errnum: XP_AWK_ENOERR),
|
||||
runcbs->custom_data);
|
||||
|
||||
/* when using callbacks, the function always returns 0 after
|
||||
* the start callbacks has been triggered */
|
||||
n = 0;
|
||||
}
|
||||
|
||||
__deinit_run (run);
|
||||
|
||||
@ -296,6 +313,22 @@ int xp_awk_stop (xp_awk_t* awk, void* run)
|
||||
return n;
|
||||
}
|
||||
|
||||
void xp_awk_stopall (xp_awk_t* awk)
|
||||
{
|
||||
xp_awk_run_t* r;
|
||||
|
||||
if (awk->thr.lks != XP_NULL)
|
||||
awk->thr.lks->lock (awk, awk->thr.lks->custom_data);
|
||||
|
||||
for (r = awk->run.ptr; r != XP_NULL; r = r->next)
|
||||
{
|
||||
r->exit_level = EXIT_ABORT;
|
||||
}
|
||||
|
||||
if (awk->thr.lks != XP_NULL)
|
||||
awk->thr.lks->unlock (awk, awk->thr.lks->custom_data);
|
||||
}
|
||||
|
||||
int xp_awk_getrunerrnum (xp_awk_t* awk, void* run, int* errnum)
|
||||
{
|
||||
xp_awk_run_t* r;
|
||||
@ -407,6 +440,16 @@ static int __init_run (
|
||||
return -1;
|
||||
}
|
||||
|
||||
run->pattern_range_state = (xp_byte_t*)
|
||||
xp_calloc (run->awk->tree.chain_size, xp_sizeof(xp_byte_t));
|
||||
if (run->pattern_range_state == XP_NULL)
|
||||
{
|
||||
xp_awk_map_close (&run->named);
|
||||
xp_str_close (&run->inrec.line);
|
||||
*errnum = XP_AWK_ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
run->extio.handler[XP_AWK_EXTIO_PIPE] = runios->pipe;
|
||||
run->extio.handler[XP_AWK_EXTIO_COPROC] = runios->coproc;
|
||||
run->extio.handler[XP_AWK_EXTIO_FILE] = runios->file;
|
||||
@ -418,6 +461,8 @@ static int __init_run (
|
||||
|
||||
static void __deinit_run (xp_awk_run_t* run)
|
||||
{
|
||||
xp_free (run->pattern_range_state);
|
||||
|
||||
/* close all pending eio's */
|
||||
/* TODO: what if this operation fails? */
|
||||
xp_awk_clearextio (run);
|
||||
@ -643,20 +688,11 @@ static int __run_pattern_blocks (xp_awk_run_t* run)
|
||||
xp_ssize_t n;
|
||||
xp_bool_t need_to_close = xp_false;
|
||||
int errnum;
|
||||
xp_awk_chain_t* chain;
|
||||
|
||||
run->inrec.buf_pos = 0;
|
||||
run->inrec.buf_len = 0;
|
||||
run->inrec.eof = xp_false;
|
||||
|
||||
/* clear the pattern_range_state */
|
||||
chain = run->awk->tree.chain;
|
||||
while (chain != XP_NULL)
|
||||
{
|
||||
chain->pattern_range_state = 0;
|
||||
chain = chain->next;
|
||||
}
|
||||
|
||||
/* run each pattern block */
|
||||
while (run->exit_level != EXIT_GLOBAL &&
|
||||
run->exit_level != EXIT_ABORT)
|
||||
@ -710,6 +746,8 @@ static int __run_pattern_blocks (xp_awk_run_t* run)
|
||||
|
||||
static int __run_pattern_block_chain (xp_awk_run_t* run, xp_awk_chain_t* chain)
|
||||
{
|
||||
xp_size_t block_no = 0;
|
||||
|
||||
while (run->exit_level != EXIT_GLOBAL &&
|
||||
run->exit_level != EXIT_ABORT && chain != XP_NULL)
|
||||
{
|
||||
@ -719,14 +757,17 @@ static int __run_pattern_block_chain (xp_awk_run_t* run, xp_awk_chain_t* chain)
|
||||
break;
|
||||
}
|
||||
|
||||
if (__run_pattern_block (run, chain) == -1) return -1;
|
||||
chain = chain->next;
|
||||
if (__run_pattern_block (run, chain, block_no) == -1) return -1;
|
||||
|
||||
chain = chain->next;
|
||||
block_no++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __run_pattern_block (xp_awk_run_t* run, xp_awk_chain_t* chain)
|
||||
static int __run_pattern_block (
|
||||
xp_awk_run_t* run, xp_awk_chain_t* chain, xp_size_t block_no)
|
||||
{
|
||||
xp_awk_nde_t* ptn;
|
||||
xp_awk_nde_blk_t* blk;
|
||||
@ -769,7 +810,7 @@ static int __run_pattern_block (xp_awk_run_t* run, xp_awk_chain_t* chain)
|
||||
/* pattern, pattern { ... } */
|
||||
xp_assert (ptn->next->next == XP_NULL);
|
||||
|
||||
if (chain->pattern_range_state == 0)
|
||||
if (run->pattern_range_state[block_no] == 0)
|
||||
{
|
||||
xp_awk_val_t* v1;
|
||||
|
||||
@ -786,12 +827,12 @@ static int __run_pattern_block (xp_awk_run_t* run, xp_awk_chain_t* chain)
|
||||
return -1;
|
||||
}
|
||||
|
||||
chain->pattern_range_state = 1;
|
||||
run->pattern_range_state[block_no] = 1;
|
||||
}
|
||||
|
||||
xp_awk_refdownval (run, v1);
|
||||
}
|
||||
else if (chain->pattern_range_state == 1)
|
||||
else if (run->pattern_range_state[block_no] == 1)
|
||||
{
|
||||
xp_awk_val_t* v2;
|
||||
|
||||
@ -807,7 +848,7 @@ static int __run_pattern_block (xp_awk_run_t* run, xp_awk_chain_t* chain)
|
||||
}
|
||||
|
||||
if (xp_awk_valtobool(v2))
|
||||
chain->pattern_range_state = 0;
|
||||
run->pattern_range_state[block_no] = 0;
|
||||
|
||||
xp_awk_refdownval (run, v2);
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: sa.h,v 1.27 2006-07-22 16:40:39 bacon Exp $
|
||||
* $Id: sa.h,v 1.28 2006-08-13 05:55:02 bacon Exp $
|
||||
*/
|
||||
|
||||
#ifndef _XP_AWK_SA_H_
|
||||
@ -32,6 +32,7 @@
|
||||
#endif
|
||||
|
||||
#define xp_malloc malloc
|
||||
#define xp_calloc calloc
|
||||
#define xp_realloc realloc
|
||||
#define xp_free free
|
||||
#define xp_memset memset
|
||||
|
Loading…
Reference in New Issue
Block a user