2006-03-02 15:36:30 +00:00
|
|
|
/*
|
2006-09-22 14:05:30 +00:00
|
|
|
* $Id: awk.c,v 1.77 2006-09-22 14:04:25 bacon Exp $
|
2006-03-02 15:36:30 +00:00
|
|
|
*/
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
#include <xp/awk/awk_i.h>
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-06-20 15:27:50 +00:00
|
|
|
static void __free_afn (void* awk, void* afn);
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-08-31 04:21:04 +00:00
|
|
|
xp_awk_t* xp_awk_open (xp_awk_syscas_t* syscas)
|
2006-02-23 14:42:33 +00:00
|
|
|
{
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_t* awk;
|
|
|
|
|
2006-08-31 04:21:04 +00:00
|
|
|
if (syscas == XP_NULL ||
|
|
|
|
syscas->malloc == XP_NULL ||
|
|
|
|
syscas->free == XP_NULL) return XP_NULL;
|
|
|
|
|
2006-09-01 04:03:28 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
|
|
|
awk = (xp_awk_t*) malloc (xp_sizeof(xp_awk_t));
|
|
|
|
#else
|
2006-08-31 04:21:04 +00:00
|
|
|
awk = (xp_awk_t*) syscas->malloc (
|
|
|
|
xp_sizeof(xp_awk_t), syscas->custom_data);
|
2006-09-01 04:03:28 +00:00
|
|
|
#endif
|
2006-03-31 16:35:37 +00:00
|
|
|
if (awk == XP_NULL) return XP_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
awk->syscas = syscas;
|
|
|
|
|
2006-08-31 16:00:20 +00:00
|
|
|
if (xp_awk_str_open (&awk->token.name, 128, awk) == XP_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-04-22 13:54:53 +00:00
|
|
|
/* TODO: initial map size?? */
|
2006-05-13 16:33:07 +00:00
|
|
|
if (xp_awk_map_open (
|
2006-09-01 03:44:51 +00:00
|
|
|
&awk->tree.afns, awk, 256, __free_afn, awk) == XP_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-08-31 16:00:20 +00:00
|
|
|
xp_awk_str_close (&awk->token.name);
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
if (xp_awk_tab_open (&awk->parse.globals, awk) == XP_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-08-31 16:00:20 +00:00
|
|
|
xp_awk_str_close (&awk->token.name);
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_map_close (&awk->tree.afns);
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
if (xp_awk_tab_open (&awk->parse.locals, awk) == XP_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-08-31 16:00:20 +00:00
|
|
|
xp_awk_str_close (&awk->token.name);
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_map_close (&awk->tree.afns);
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_awk_tab_close (&awk->parse.globals);
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-31 15:09:24 +00:00
|
|
|
if (xp_awk_tab_open (&awk->parse.params, awk) == XP_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-08-31 16:00:20 +00:00
|
|
|
xp_awk_str_close (&awk->token.name);
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_map_close (&awk->tree.afns);
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_awk_tab_close (&awk->parse.globals);
|
|
|
|
xp_awk_tab_close (&awk->parse.locals);
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-04 17:36:40 +00:00
|
|
|
awk->option = 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->errnum = XP_AWK_ENOERR;
|
2006-03-07 15:55:14 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->parse.nlocals_max = 0;
|
2006-03-07 15:55:14 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.nglobals = 0;
|
2006-08-04 16:31:22 +00:00
|
|
|
awk->tree.nbglobals = 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.begin = XP_NULL;
|
|
|
|
awk->tree.end = XP_NULL;
|
|
|
|
awk->tree.chain = XP_NULL;
|
|
|
|
awk->tree.chain_tail = XP_NULL;
|
2006-08-13 05:55:02 +00:00
|
|
|
awk->tree.chain_size = 0;
|
2006-03-07 15:55:14 +00:00
|
|
|
|
2006-06-27 10:53:04 +00:00
|
|
|
awk->token.prev = 0;
|
|
|
|
awk->token.type = 0;
|
2006-08-03 09:54:16 +00:00
|
|
|
awk->token.line = 0;
|
|
|
|
awk->token.column = 0;
|
2006-05-13 16:33:07 +00:00
|
|
|
|
2006-08-06 08:16:03 +00:00
|
|
|
awk->src.ios = XP_NULL;
|
|
|
|
awk->src.lex.curc = XP_CHAR_EOF;
|
|
|
|
awk->src.lex.ungotc_count = 0;
|
|
|
|
awk->src.lex.line = 1;
|
|
|
|
awk->src.lex.column = 1;
|
|
|
|
awk->src.shared.buf_pos = 0;
|
|
|
|
awk->src.shared.buf_len = 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-07-13 15:43:39 +00:00
|
|
|
awk->bfn.sys = XP_NULL;
|
|
|
|
awk->bfn.user = XP_NULL;
|
|
|
|
|
2006-08-10 16:02:15 +00:00
|
|
|
awk->run.count = 0;
|
|
|
|
awk->run.ptr = XP_NULL;
|
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
return awk;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xp_awk_close (xp_awk_t* awk)
|
|
|
|
{
|
2006-08-10 16:02:15 +00:00
|
|
|
if (xp_awk_clear (awk) == -1) return -1;
|
|
|
|
|
|
|
|
xp_assert (awk->run.count == 0 && awk->run.ptr == XP_NULL);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_map_close (&awk->tree.afns);
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_awk_tab_close (&awk->parse.globals);
|
|
|
|
xp_awk_tab_close (&awk->parse.locals);
|
|
|
|
xp_awk_tab_close (&awk->parse.params);
|
2006-08-31 16:00:20 +00:00
|
|
|
xp_awk_str_close (&awk->token.name);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-09-01 04:03:28 +00:00
|
|
|
/* XP_AWK_ALLOC, XP_AWK_FREE, etc can not be used
|
2006-08-31 15:09:24 +00:00
|
|
|
* from the next line onwards */
|
|
|
|
XP_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-10 16:02:15 +00:00
|
|
|
int xp_awk_clear (xp_awk_t* awk)
|
2006-02-23 14:42:33 +00:00
|
|
|
{
|
2006-08-10 16:02:15 +00:00
|
|
|
/* you should stop all running instances beforehand */
|
|
|
|
/* TODO: can i stop all instances??? */
|
|
|
|
if (awk->run.ptr != XP_NULL)
|
|
|
|
{
|
|
|
|
awk->errnum = XP_AWK_ERUNNING;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TOOD: clear bfns when they can be added dynamically
|
|
|
|
awk->bfn.sys
|
|
|
|
awk->bfn.user
|
|
|
|
*/
|
|
|
|
|
2006-08-06 08:16:03 +00:00
|
|
|
awk->src.ios = XP_NULL;
|
|
|
|
awk->src.lex.curc = XP_CHAR_EOF;
|
|
|
|
awk->src.lex.ungotc_count = 0;
|
|
|
|
awk->src.lex.line = 1;
|
|
|
|
awk->src.lex.column = 1;
|
|
|
|
awk->src.shared.buf_pos = 0;
|
|
|
|
awk->src.shared.buf_len = 0;
|
2006-04-20 16:17:01 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_awk_tab_clear (&awk->parse.globals);
|
|
|
|
xp_awk_tab_clear (&awk->parse.locals);
|
|
|
|
xp_awk_tab_clear (&awk->parse.params);
|
2006-04-22 13:54:53 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->parse.nlocals_max = 0;
|
2006-08-01 15:57:43 +00:00
|
|
|
awk->parse.depth.loop = 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
|
|
|
/* clear parse trees */
|
2006-08-06 08:16:03 +00:00
|
|
|
awk->tree.nbglobals = 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.nglobals = 0;
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_map_clear (&awk->tree.afns);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-04-24 14:38:46 +00:00
|
|
|
if (awk->tree.begin != XP_NULL)
|
|
|
|
{
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_assert (awk->tree.begin->next == XP_NULL);
|
2006-09-01 03:44:51 +00:00
|
|
|
xp_awk_clrpt (awk, awk->tree.begin);
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.begin = XP_NULL;
|
|
|
|
}
|
2006-04-24 14:38:46 +00:00
|
|
|
|
|
|
|
if (awk->tree.end != XP_NULL)
|
|
|
|
{
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_assert (awk->tree.end->next == XP_NULL);
|
2006-09-01 03:44:51 +00:00
|
|
|
xp_awk_clrpt (awk, awk->tree.end);
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.end = XP_NULL;
|
|
|
|
}
|
2006-04-24 14:38:46 +00:00
|
|
|
|
|
|
|
while (awk->tree.chain != XP_NULL)
|
|
|
|
{
|
2006-02-23 14:42:33 +00:00
|
|
|
xp_awk_chain_t* next = awk->tree.chain->next;
|
|
|
|
if (awk->tree.chain->pattern != XP_NULL)
|
2006-09-01 03:44:51 +00:00
|
|
|
xp_awk_clrpt (awk, awk->tree.chain->pattern);
|
2006-02-23 14:42:33 +00:00
|
|
|
if (awk->tree.chain->action != XP_NULL)
|
2006-09-01 03:44:51 +00:00
|
|
|
xp_awk_clrpt (awk, awk->tree.chain->action);
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE (awk, awk->tree.chain);
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.chain = next;
|
|
|
|
}
|
2006-08-03 09:54:16 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->tree.chain_tail = XP_NULL;
|
2006-08-13 05:55:02 +00:00
|
|
|
awk->tree.chain_size = 0;
|
2006-08-10 16:02:15 +00:00
|
|
|
return 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-08-13 16:05:04 +00:00
|
|
|
int xp_awk_getopt (xp_awk_t* awk)
|
|
|
|
{
|
|
|
|
return awk->option;
|
|
|
|
}
|
|
|
|
|
2006-08-04 17:36:40 +00:00
|
|
|
void xp_awk_setopt (xp_awk_t* awk, int opt)
|
2006-03-31 16:35:37 +00:00
|
|
|
{
|
2006-08-04 17:36:40 +00:00
|
|
|
awk->option = opt;
|
2006-04-09 15:31:13 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 15:27:50 +00:00
|
|
|
static void __free_afn (void* owner, void* afn)
|
2006-02-23 14:42:33 +00:00
|
|
|
{
|
2006-06-20 15:27:50 +00:00
|
|
|
xp_awk_afn_t* f = (xp_awk_afn_t*)afn;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
|
|
|
/* f->name doesn't have to be freed */
|
2006-08-31 15:09:24 +00:00
|
|
|
/*XP_AWK_FREE ((xp_awk_t*)owner, f->name);*/
|
2006-04-24 14:38:46 +00:00
|
|
|
|
2006-09-01 03:44:51 +00:00
|
|
|
xp_awk_clrpt ((xp_awk_t*)owner, f->body);
|
2006-08-31 15:09:24 +00:00
|
|
|
XP_AWK_FREE ((xp_awk_t*)owner, f);
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
2006-05-13 16:33:07 +00:00
|
|
|
|
|
|
|
xp_size_t xp_awk_getsrcline (xp_awk_t* awk)
|
|
|
|
{
|
|
|
|
return awk->token.line;
|
|
|
|
}
|
2006-06-19 04:38:51 +00:00
|
|
|
|