2006-03-02 15:36:30 +00:00
|
|
|
/*
|
2006-12-12 05:16:30 +00:00
|
|
|
* $Id: awk.c,v 1.97 2006-12-12 05:16:29 bacon Exp $
|
2006-03-02 15:36:30 +00:00
|
|
|
*/
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-10-22 14:00:05 +00:00
|
|
|
#if defined(__BORLANDC__)
|
|
|
|
#pragma hdrstop
|
|
|
|
#define Library
|
|
|
|
#endif
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
#include <ase/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-10-24 04:10:12 +00:00
|
|
|
ase_awk_t* ase_awk_open (const ase_awk_syscas_t* syscas)
|
2006-10-22 14:00:05 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_t* awk;
|
|
|
|
|
|
|
|
if (syscas == ASE_NULL) return ASE_NULL;
|
|
|
|
|
|
|
|
if (syscas->malloc == ASE_NULL ||
|
|
|
|
syscas->free == ASE_NULL) return ASE_NULL;
|
|
|
|
|
|
|
|
if (syscas->is_upper == ASE_NULL ||
|
|
|
|
syscas->is_lower == ASE_NULL ||
|
|
|
|
syscas->is_alpha == ASE_NULL ||
|
|
|
|
syscas->is_digit == ASE_NULL ||
|
|
|
|
syscas->is_xdigit == ASE_NULL ||
|
|
|
|
syscas->is_alnum == ASE_NULL ||
|
|
|
|
syscas->is_space == ASE_NULL ||
|
|
|
|
syscas->is_print == ASE_NULL ||
|
|
|
|
syscas->is_graph == ASE_NULL ||
|
|
|
|
syscas->is_cntrl == ASE_NULL ||
|
|
|
|
syscas->is_punct == ASE_NULL ||
|
|
|
|
syscas->to_upper == ASE_NULL ||
|
|
|
|
syscas->to_lower == ASE_NULL) return ASE_NULL;
|
|
|
|
|
|
|
|
if (syscas->sprintf == ASE_NULL ||
|
2006-10-28 05:24:08 +00:00
|
|
|
syscas->aprintf == ASE_NULL ||
|
2006-10-24 04:10:12 +00:00
|
|
|
syscas->dprintf == ASE_NULL ||
|
2006-10-28 05:24:08 +00:00
|
|
|
syscas->abort == ASE_NULL) return ASE_NULL;
|
2006-10-12 04:17:58 +00:00
|
|
|
|
2006-10-31 14:32:50 +00:00
|
|
|
if (syscas->pow == ASE_NULL) return ASE_NULL;
|
|
|
|
|
2006-11-29 03:19:49 +00:00
|
|
|
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
|
2006-11-29 02:54:17 +00:00
|
|
|
awk = (ase_awk_t*) malloc (ASE_SIZEOF(ase_awk_t));
|
2006-09-01 04:03:28 +00:00
|
|
|
#else
|
2006-10-24 04:10:12 +00:00
|
|
|
awk = (ase_awk_t*) syscas->malloc (
|
2006-11-29 02:54:17 +00:00
|
|
|
ASE_SIZEOF(ase_awk_t), syscas->custom_data);
|
2006-09-01 04:03:28 +00:00
|
|
|
#endif
|
2006-10-24 04:10:12 +00:00
|
|
|
if (awk == ASE_NULL) return ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
/* it uses the built-in ase_awk_memset because awk is not
|
2006-10-16 08:48:19 +00:00
|
|
|
* fully initialized yet */
|
2006-11-29 02:54:17 +00:00
|
|
|
ase_awk_memset (awk, 0, ASE_SIZEOF(ase_awk_t));
|
2006-10-16 08:48:19 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (syscas->memcpy == ASE_NULL)
|
2006-10-12 14:36:25 +00:00
|
|
|
{
|
2006-11-29 02:54:17 +00:00
|
|
|
ase_awk_memcpy (&awk->syscas, syscas, ASE_SIZEOF(awk->syscas));
|
2006-10-24 04:10:12 +00:00
|
|
|
awk->syscas.memcpy = ase_awk_memcpy;
|
2006-10-12 14:36:25 +00:00
|
|
|
}
|
2006-11-29 02:54:17 +00:00
|
|
|
else syscas->memcpy (&awk->syscas, syscas, ASE_SIZEOF(awk->syscas));
|
2006-10-24 04:10:12 +00:00
|
|
|
if (syscas->memset == ASE_NULL) awk->syscas.memset = ase_awk_memset;
|
2006-08-31 15:09:24 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_awk_str_open (&awk->token.name, 128, awk) == ASE_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ASE_AWK_FREE (awk, awk);
|
|
|
|
return ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-04-22 13:54:53 +00:00
|
|
|
/* TODO: initial map size?? */
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_awk_map_open (
|
|
|
|
&awk->tree.afns, awk, 256, __free_afn, awk) == ASE_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_str_close (&awk->token.name);
|
|
|
|
ASE_AWK_FREE (awk, awk);
|
|
|
|
return ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_awk_tab_open (&awk->parse.globals, awk) == ASE_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_str_close (&awk->token.name);
|
|
|
|
ase_awk_map_close (&awk->tree.afns);
|
|
|
|
ASE_AWK_FREE (awk, awk);
|
|
|
|
return ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_awk_tab_open (&awk->parse.locals, awk) == ASE_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_str_close (&awk->token.name);
|
|
|
|
ase_awk_map_close (&awk->tree.afns);
|
|
|
|
ase_awk_tab_close (&awk->parse.globals);
|
|
|
|
ASE_AWK_FREE (awk, awk);
|
|
|
|
return ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_awk_tab_open (&awk->parse.params, awk) == ASE_NULL)
|
2006-04-21 16:21:27 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_str_close (&awk->token.name);
|
|
|
|
ase_awk_map_close (&awk->tree.afns);
|
|
|
|
ase_awk_tab_close (&awk->parse.globals);
|
|
|
|
ase_awk_tab_close (&awk->parse.locals);
|
|
|
|
ASE_AWK_FREE (awk, awk);
|
|
|
|
return ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-08-04 17:36:40 +00:00
|
|
|
awk->option = 0;
|
2006-10-24 04:10:12 +00:00
|
|
|
awk->errnum = ASE_AWK_ENOERR;
|
2006-03-07 15:55:14 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
awk->parse.nlocals_max = 0;
|
2006-10-15 15:46:14 +00:00
|
|
|
awk->parse.nl_semicolon = 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-10-24 04:10:12 +00:00
|
|
|
awk->tree.begin = ASE_NULL;
|
|
|
|
awk->tree.end = ASE_NULL;
|
|
|
|
awk->tree.chain = ASE_NULL;
|
|
|
|
awk->tree.chain_tail = ASE_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-10-24 04:10:12 +00:00
|
|
|
awk->src.lex.curc = ASE_CHAR_EOF;
|
2006-08-06 08:16:03 +00:00
|
|
|
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-10-24 04:10:12 +00:00
|
|
|
awk->bfn.sys = ASE_NULL;
|
|
|
|
awk->bfn.user = ASE_NULL;
|
2006-07-13 15:43:39 +00:00
|
|
|
|
2006-11-25 15:51:57 +00:00
|
|
|
awk->parse.depth.cur.block = 0;
|
|
|
|
awk->parse.depth.cur.loop = 0;
|
|
|
|
awk->parse.depth.cur.expr = 0;
|
|
|
|
|
|
|
|
ase_awk_setmaxparsedepth (awk, ASE_AWK_DEPTH_BLOCK, 0);
|
|
|
|
ase_awk_setmaxparsedepth (awk, ASE_AWK_DEPTH_EXPR, 0);
|
2006-12-04 12:59:01 +00:00
|
|
|
ase_awk_setmaxrundepth (awk, ASE_AWK_DEPTH_BLOCK, 0);
|
|
|
|
ase_awk_setmaxrundepth (awk, ASE_AWK_DEPTH_EXPR, 0);
|
2006-10-28 12:17:57 +00:00
|
|
|
|
2006-08-10 16:02:15 +00:00
|
|
|
awk->run.count = 0;
|
2006-10-24 04:10:12 +00:00
|
|
|
awk->run.ptr = ASE_NULL;
|
2006-08-10 16:02:15 +00:00
|
|
|
|
2006-02-23 14:42:33 +00:00
|
|
|
return awk;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
int ase_awk_close (ase_awk_t* awk)
|
2006-02-23 14:42:33 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
if (ase_awk_clear (awk) == -1) return -1;
|
2006-11-28 04:30:57 +00:00
|
|
|
ase_awk_clrbfn (awk);
|
2006-08-10 16:02:15 +00:00
|
|
|
|
2006-10-26 09:31:28 +00:00
|
|
|
ASE_AWK_ASSERT (awk, awk->run.count == 0 && awk->run.ptr == ASE_NULL);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_map_close (&awk->tree.afns);
|
|
|
|
ase_awk_tab_close (&awk->parse.globals);
|
|
|
|
ase_awk_tab_close (&awk->parse.locals);
|
|
|
|
ase_awk_tab_close (&awk->parse.params);
|
|
|
|
ase_awk_str_close (&awk->token.name);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
/* ASE_AWK_ALLOC, ASE_AWK_FREE, etc can not be used
|
2006-08-31 15:09:24 +00:00
|
|
|
* from the next line onwards */
|
2006-10-24 04:10:12 +00:00
|
|
|
ASE_AWK_FREE (awk, awk);
|
2006-02-23 14:42:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
int ase_awk_clear (ase_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??? */
|
2006-10-24 04:10:12 +00:00
|
|
|
if (awk->run.ptr != ASE_NULL)
|
2006-08-10 16:02:15 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
awk->errnum = ASE_AWK_ERUNNING;
|
2006-08-10 16:02:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-29 02:54:17 +00:00
|
|
|
ASE_AWK_MEMSET (awk, &awk->src.ios, 0, ASE_SIZEOF(awk->src.ios));
|
2006-10-24 04:10:12 +00:00
|
|
|
awk->src.lex.curc = ASE_CHAR_EOF;
|
2006-08-06 08:16:03 +00:00
|
|
|
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-10-24 04:10:12 +00:00
|
|
|
ase_awk_tab_clear (&awk->parse.globals);
|
|
|
|
ase_awk_tab_clear (&awk->parse.locals);
|
|
|
|
ase_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-11-25 15:51:57 +00:00
|
|
|
awk->parse.depth.cur.block = 0;
|
|
|
|
awk->parse.depth.cur.loop = 0;
|
|
|
|
awk->parse.depth.cur.expr = 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
|
|
|
/* clear parse trees */
|
2006-12-12 05:16:30 +00:00
|
|
|
awk->tree.ok = 0;
|
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-10-24 04:10:12 +00:00
|
|
|
ase_awk_map_clear (&awk->tree.afns);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (awk->tree.begin != ASE_NULL)
|
2006-04-24 14:38:46 +00:00
|
|
|
{
|
2006-10-26 09:31:28 +00:00
|
|
|
ASE_AWK_ASSERT (awk, awk->tree.begin->next == ASE_NULL);
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_clrpt (awk, awk->tree.begin);
|
|
|
|
awk->tree.begin = ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
2006-04-24 14:38:46 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
if (awk->tree.end != ASE_NULL)
|
2006-04-24 14:38:46 +00:00
|
|
|
{
|
2006-10-26 09:31:28 +00:00
|
|
|
ASE_AWK_ASSERT (awk, awk->tree.end->next == ASE_NULL);
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_clrpt (awk, awk->tree.end);
|
|
|
|
awk->tree.end = ASE_NULL;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
2006-04-24 14:38:46 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
while (awk->tree.chain != ASE_NULL)
|
2006-04-24 14:38:46 +00:00
|
|
|
{
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_chain_t* next = awk->tree.chain->next;
|
|
|
|
if (awk->tree.chain->pattern != ASE_NULL)
|
|
|
|
ase_awk_clrpt (awk, awk->tree.chain->pattern);
|
|
|
|
if (awk->tree.chain->action != ASE_NULL)
|
|
|
|
ase_awk_clrpt (awk, awk->tree.chain->action);
|
|
|
|
ASE_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-10-24 04:10:12 +00:00
|
|
|
awk->tree.chain_tail = ASE_NULL;
|
2006-08-13 05:55:02 +00:00
|
|
|
awk->tree.chain_size = 0;
|
2006-11-27 15:11:14 +00:00
|
|
|
|
2006-08-10 16:02:15 +00:00
|
|
|
return 0;
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
int ase_awk_getopt (ase_awk_t* awk)
|
2006-08-13 16:05:04 +00:00
|
|
|
{
|
|
|
|
return awk->option;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
void ase_awk_setopt (ase_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-10-24 04:10:12 +00:00
|
|
|
ase_awk_afn_t* f = (ase_awk_afn_t*)afn;
|
2006-02-23 14:42:33 +00:00
|
|
|
|
|
|
|
/* f->name doesn't have to be freed */
|
2006-10-24 04:10:12 +00:00
|
|
|
/*ASE_AWK_FREE ((ase_awk_t*)owner, f->name);*/
|
2006-04-24 14:38:46 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_awk_clrpt ((ase_awk_t*)owner, f->body);
|
|
|
|
ASE_AWK_FREE ((ase_awk_t*)owner, f);
|
2006-02-23 14:42:33 +00:00
|
|
|
}
|
2006-05-13 16:33:07 +00:00
|
|
|
|
2006-10-24 04:10:12 +00:00
|
|
|
ase_size_t ase_awk_getsrcline (ase_awk_t* awk)
|
2006-05-13 16:33:07 +00:00
|
|
|
{
|
|
|
|
return awk->token.line;
|
|
|
|
}
|
2006-06-19 04:38:51 +00:00
|
|
|
|
2006-12-04 12:59:01 +00:00
|
|
|
void ase_awk_setmaxrundepth (ase_awk_t* awk, int types, ase_size_t depth)
|
|
|
|
{
|
|
|
|
if (types & ASE_AWK_DEPTH_BLOCK)
|
|
|
|
{
|
|
|
|
awk->run.depth.max.block = depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (types & ASE_AWK_DEPTH_EXPR)
|
|
|
|
{
|
|
|
|
awk->run.depth.max.expr = depth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|