2005-11-06 12:01:29 +00:00
|
|
|
/*
|
2006-01-09 16:03:56 +00:00
|
|
|
* $Id: awk.c,v 1.7 2006-01-09 16:03:55 bacon Exp $
|
2005-11-06 12:01:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/awk/awk.h>
|
|
|
|
#include <xp/bas/memory.h>
|
2005-11-07 16:02:44 +00:00
|
|
|
#include <xp/bas/assert.h>
|
2005-11-06 12:01:29 +00:00
|
|
|
|
|
|
|
xp_awk_t* xp_awk_open (xp_awk_t* awk)
|
|
|
|
{
|
|
|
|
if (awk == XP_NULL) {
|
|
|
|
awk = (xp_awk_t*) xp_malloc (xp_sizeof(awk));
|
|
|
|
if (awk == XP_NULL) return XP_NULL;
|
2005-12-05 15:11:29 +00:00
|
|
|
awk->__dynamic = xp_true;
|
2005-11-06 12:01:29 +00:00
|
|
|
}
|
2005-12-05 15:11:29 +00:00
|
|
|
else awk->__dynamic = xp_false;
|
2005-11-06 12:01:29 +00:00
|
|
|
|
2005-11-14 15:23:54 +00:00
|
|
|
if (xp_str_open(&awk->token.name, 128) == XP_NULL) {
|
2005-12-05 15:11:29 +00:00
|
|
|
if (awk->__dynamic) xp_free (awk);
|
2005-11-07 16:02:44 +00:00
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-09 12:51:47 +00:00
|
|
|
awk->tree = XP_NULL;
|
2005-11-06 12:01:29 +00:00
|
|
|
awk->errnum = XP_AWK_ENOERR;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_func = XP_NULL;
|
|
|
|
awk->inp_func = XP_NULL;
|
|
|
|
awk->outp_func = XP_NULL;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_arg = XP_NULL;
|
|
|
|
awk->inp_arg = XP_NULL;
|
|
|
|
awk->outp_arg = XP_NULL;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
|
|
|
awk->lex.curc = XP_CHAR_EOF;
|
2005-11-14 15:23:54 +00:00
|
|
|
awk->lex.ungotc_count = 0;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-11-06 12:01:29 +00:00
|
|
|
return awk;
|
|
|
|
}
|
|
|
|
|
2006-01-09 16:03:56 +00:00
|
|
|
static void __collapse_tree (xp_awk_t* awk)
|
2005-11-06 12:01:29 +00:00
|
|
|
{
|
2006-01-09 12:51:47 +00:00
|
|
|
/* TODO: collapse the tree */
|
2006-01-09 16:03:56 +00:00
|
|
|
/* TODO */
|
|
|
|
awk->tree = XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xp_awk_close (xp_awk_t* awk)
|
|
|
|
{
|
2006-01-09 12:51:47 +00:00
|
|
|
|
2006-01-09 16:03:56 +00:00
|
|
|
if (awk->tree != XP_NULL) __collapse_tree (awk);
|
2006-01-09 12:51:47 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
if (xp_awk_detsrc(awk) == -1) return -1;
|
2005-11-14 15:23:54 +00:00
|
|
|
xp_str_close (&awk->token.name);
|
2005-12-05 15:11:29 +00:00
|
|
|
if (awk->__dynamic) xp_free (awk);
|
2005-11-06 12:01:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg)
|
2005-11-07 16:02:44 +00:00
|
|
|
{
|
2005-12-29 12:04:51 +00:00
|
|
|
if (xp_awk_detsrc(awk) == -1) return -1;
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
xp_assert (awk->src_func == XP_NULL);
|
2005-11-07 16:02:44 +00:00
|
|
|
|
2006-01-09 16:03:56 +00:00
|
|
|
if (src(XP_AWK_IO_OPEN, arg, XP_NULL, 0) == -1) {
|
2005-11-07 16:02:44 +00:00
|
|
|
awk->errnum = XP_AWK_ESRCOP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_func = src;
|
|
|
|
awk->src_arg = arg;
|
2005-11-14 15:23:54 +00:00
|
|
|
awk->lex.curc = XP_CHAR_EOF;
|
|
|
|
awk->lex.ungotc_count = 0;
|
2005-11-07 16:02:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
int xp_awk_detsrc (xp_awk_t* awk)
|
2005-11-07 16:02:44 +00:00
|
|
|
{
|
2005-12-29 12:04:51 +00:00
|
|
|
if (awk->src_func != XP_NULL) {
|
|
|
|
if (awk->src_func(XP_AWK_IO_CLOSE, awk->src_arg, XP_NULL, 0) == -1) {
|
2005-11-07 16:02:44 +00:00
|
|
|
awk->errnum = XP_AWK_ESRCCL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:04:51 +00:00
|
|
|
awk->src_func = XP_NULL;
|
|
|
|
awk->src_arg = XP_NULL;
|
2005-11-14 15:23:54 +00:00
|
|
|
awk->lex.curc = XP_CHAR_EOF;
|
|
|
|
awk->lex.ungotc_count = 0;
|
2005-11-07 16:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|