qse/ase/awk/awk.c

248 lines
5.1 KiB
C
Raw Normal View History

2006-03-02 15:36:30 +00:00
/*
2006-08-04 16:31:22 +00:00
* $Id: awk.c,v 1.65 2006-08-04 16:31:21 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-04-16 04:31:38 +00:00
#ifndef XP_AWK_STAND_ALONE
2006-02-23 14:42:33 +00:00
#include <xp/bas/memory.h>
#include <xp/bas/assert.h>
#endif
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-03-31 16:35:37 +00:00
xp_awk_t* xp_awk_open (void)
2006-02-23 14:42:33 +00:00
{
2006-03-31 16:35:37 +00:00
xp_awk_t* awk;
2006-06-19 09:10:57 +00:00
xp_size_t i;
2006-03-31 16:35:37 +00:00
2006-03-31 17:16:23 +00:00
awk = (xp_awk_t*) xp_malloc (xp_sizeof(xp_awk_t));
2006-03-31 16:35:37 +00:00
if (awk == XP_NULL) return XP_NULL;
2006-02-23 14:42:33 +00:00
2006-05-13 16:33:07 +00:00
if (xp_str_open (&awk->token.name, 128) == XP_NULL)
2006-04-21 16:21:27 +00:00
{
2006-03-31 16:35:37 +00:00
xp_free (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-06-20 15:27:50 +00:00
&awk->tree.afns, awk, 256, __free_afn) == XP_NULL)
2006-04-21 16:21:27 +00:00
{
2006-02-23 14:42:33 +00:00
xp_str_close (&awk->token.name);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
2006-05-13 16:33:07 +00:00
if (xp_awk_tab_open (&awk->parse.globals) == XP_NULL)
2006-04-21 16:21:27 +00:00
{
2006-02-23 14:42:33 +00:00
xp_str_close (&awk->token.name);
2006-06-20 15:27:50 +00:00
xp_awk_map_close (&awk->tree.afns);
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
2006-05-13 16:33:07 +00:00
if (xp_awk_tab_open (&awk->parse.locals) == XP_NULL)
2006-04-21 16:21:27 +00:00
{
2006-02-23 14:42:33 +00:00
xp_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-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
2006-05-13 16:33:07 +00:00
if (xp_awk_tab_open (&awk->parse.params) == XP_NULL)
2006-04-21 16:21:27 +00:00
{
2006-02-23 14:42:33 +00:00
xp_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-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return XP_NULL;
}
awk->opt.parse = 0;
awk->opt.run = 0;
awk->errnum = XP_AWK_ENOERR;
2006-04-22 13:54:53 +00:00
awk->srcio = XP_NULL;
awk->srcio_arg = XP_NULL;
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-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-02-23 14:42:33 +00:00
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
2006-05-10 16:02:39 +00:00
awk->lex.buf_pos = 0;
awk->lex.buf_len = 0;
2006-05-13 16:33:07 +00:00
awk->lex.line = 1;
awk->lex.column = 1;
2006-02-23 14:42:33 +00:00
2006-06-19 09:10:57 +00:00
for (i = 0; i < xp_countof(awk->extio); i++) awk->extio[i] = XP_NULL;
2006-06-18 13:43:28 +00:00
2006-07-13 15:43:39 +00:00
awk->bfn.sys = XP_NULL;
awk->bfn.user = XP_NULL;
2006-02-23 14:42:33 +00:00
return awk;
}
int xp_awk_close (xp_awk_t* awk)
{
xp_awk_clear (awk);
2006-03-05 17:07:33 +00:00
2006-02-23 14:42:33 +00:00
if (xp_awk_detsrc(awk) == -1) return -1;
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);
xp_str_close (&awk->token.name);
2006-03-05 17:07:33 +00:00
2006-03-31 16:35:37 +00:00
xp_free (awk);
2006-02-23 14:42:33 +00:00
return 0;
}
2006-04-14 10:56:42 +00:00
/* TODO: write a function to clear awk->parse data structure.
this would be need either as a separate function or as a part of xp_awk_clear...
do i have to pass an option to xp_awk_clear to do this??? */
2006-02-23 14:42:33 +00:00
void xp_awk_clear (xp_awk_t* awk)
{
2006-04-21 17:24:31 +00:00
/* TODO: kill all associated run instances... */
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 */
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);
xp_awk_clrpt (awk->tree.begin);
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);
xp_awk_clrpt (awk->tree.end);
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)
xp_awk_clrpt (awk->tree.chain->pattern);
if (awk->tree.chain->action != XP_NULL)
xp_awk_clrpt (awk->tree.chain->action);
xp_free (awk->tree.chain);
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-03-31 16:35:37 +00:00
void xp_awk_setparseopt (xp_awk_t* awk, int opt)
{
awk->opt.parse = opt;
}
2006-04-09 15:31:13 +00:00
void xp_awk_setrunopt (xp_awk_t* awk, int opt)
{
awk->opt.run = opt;
}
2006-06-22 04:25:44 +00:00
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t handler, void* arg)
2006-02-23 14:42:33 +00:00
{
if (xp_awk_detsrc(awk) == -1) return -1;
2006-04-22 13:54:53 +00:00
xp_assert (awk->srcio == XP_NULL);
2006-07-28 10:34:22 +00:00
if (handler (XP_AWK_IO_OPEN, arg, XP_NULL, 0) == -1)
2006-04-24 14:38:46 +00:00
{
2006-04-22 13:54:53 +00:00
awk->errnum = XP_AWK_ESRCINOPEN;
2006-02-23 14:42:33 +00:00
return -1;
}
2006-06-22 04:25:44 +00:00
awk->srcio = handler;
2006-04-22 13:54:53 +00:00
awk->srcio_arg = arg;
2006-02-23 14:42:33 +00:00
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
2006-05-10 16:02:39 +00:00
awk->lex.buf_pos = 0;
awk->lex.buf_len = 0;
2006-05-13 16:33:07 +00:00
awk->lex.line = 1;
awk->lex.column = 1;
2006-02-23 14:42:33 +00:00
return 0;
}
int xp_awk_detsrc (xp_awk_t* awk)
{
2006-04-24 14:38:46 +00:00
if (awk->srcio != XP_NULL)
{
2006-04-22 13:54:53 +00:00
xp_ssize_t n;
2006-07-28 10:34:22 +00:00
n = awk->srcio (XP_AWK_IO_CLOSE, awk->srcio_arg, XP_NULL, 0);
2006-04-22 13:54:53 +00:00
if (n == -1)
{
awk->errnum = XP_AWK_ESRCINCLOSE;
2006-02-23 14:42:33 +00:00
return -1;
}
2006-04-22 13:54:53 +00:00
awk->srcio = XP_NULL;
awk->srcio_arg = XP_NULL;
2006-02-23 14:42:33 +00:00
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
2006-05-10 16:02:39 +00:00
awk->lex.buf_pos = 0;
awk->lex.buf_len = 0;
2006-05-13 16:33:07 +00:00
awk->lex.line = 1;
awk->lex.column = 1;
2006-02-23 14:42:33 +00:00
}
return 0;
}
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 */
/*xp_free (f->name);*/
2006-04-24 14:38:46 +00:00
2006-02-23 14:42:33 +00:00
xp_awk_clrpt (f->body);
xp_free (f);
}
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
2006-06-19 15:43:27 +00:00
/* TODO: imrove this... should it close io when it is overridden with a new handler??? */
2006-06-19 09:10:57 +00:00
int xp_awk_setextio (xp_awk_t* awk, int id, xp_awk_io_t handler, void* arg)
2006-06-19 04:38:51 +00:00
{
2006-06-19 09:10:57 +00:00
if (id < 0 || id >= xp_countof(awk->extio))
{
awk->errnum = XP_AWK_EINVAL;
return -1;
}
2006-06-19 15:43:27 +00:00
2006-06-19 09:10:57 +00:00
awk->extio[id] = handler;
2006-06-19 04:38:51 +00:00
return 0;
}