qse/ase/awk/func.c

89 lines
1.7 KiB
C
Raw Normal View History

2006-06-16 14:31:42 +00:00
/*
2006-06-21 15:37:51 +00:00
* $Id: func.c,v 1.5 2006-06-21 15:37:51 bacon Exp $
2006-06-16 14:31:42 +00:00
*/
#include <xp/awk/awk_i.h>
#ifndef XP_AWK_STAND_ALONE
#include <xp/bas/memory.h>
#include <xp/bas/string.h>
2006-06-21 11:45:26 +00:00
#include <xp/bas/str.h>
2006-06-16 14:31:42 +00:00
#endif
2006-06-20 15:27:50 +00:00
static int __bfn_close (void* run);
2006-06-16 14:31:42 +00:00
2006-06-21 15:37:51 +00:00
/* TODO: move it under the awk structure... */
2006-06-16 14:31:42 +00:00
static xp_awk_bfn_t __bfn[] =
{
2006-06-21 15:37:51 +00:00
{ XP_T("system"), (1 << 5), 1, 1, XP_NULL },
{ XP_T("close"), XP_AWK_EXTIO, 1, 1, __bfn_close },
2006-06-20 15:27:50 +00:00
{ XP_NULL, 0, 0, 0, XP_NULL }
2006-06-16 14:31:42 +00:00
};
2006-06-21 15:37:51 +00:00
xp_awk_bfn_t* xp_awk_addbfn (
xp_awk_t* awk, const xp_char_t* name, int when_valid,
xp_size_t min_args, xp_size_t max_args, int (*handler)(void*))
2006-06-16 14:31:42 +00:00
{
2006-06-21 15:37:51 +00:00
/* TODO */
}
xp_awk_bfn_t* xp_awk_getbfn (xp_awk_t* awk, const xp_char_t* name)
{
xp_awk_bfn_t* p;
2006-06-16 14:31:42 +00:00
2006-06-21 15:37:51 +00:00
for (p = __bfn; p->name != XP_NULL; p++)
2006-06-16 14:31:42 +00:00
{
2006-06-21 15:37:51 +00:00
if (p->valid != 0 &&
(awk->opt.parse & p->valid) == 0) continue;
2006-06-16 14:31:42 +00:00
if (xp_strcmp (p->name, name) == 0) return p;
}
return XP_NULL;
}
2006-06-20 15:27:50 +00:00
static int __bfn_close (void* run)
{
2006-06-21 11:45:26 +00:00
xp_size_t nargs;
xp_str_t buf;
xp_awk_val_t* v;
int errnum, n;
2006-06-20 15:27:50 +00:00
nargs = xp_awk_getnargs (run);
2006-06-21 11:45:26 +00:00
xp_assert (nargs == 1);
2006-06-21 15:37:51 +00:00
/* TODO: support close (xxx, "to"/"from") like gawk */
2006-06-21 11:45:26 +00:00
if (xp_str_open (&buf, 256) == XP_NULL)
{
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
if (xp_awk_valtostr (xp_awk_getarg(run, 0), &errnum, &buf) == XP_NULL)
{
xp_str_close (&buf);
xp_awk_seterrnum (run, errnum);
return -1;
}
n = xp_awk_closeextio (run, XP_STR_BUF(&buf), &errnum);
if (n == -1 && errnum != XP_AWK_ENOERR)
{
xp_str_close (&buf);
xp_awk_seterrnum (run, errnum);
return -1;
}
xp_str_close (&buf);
v = xp_awk_makeintval (run, n);
if (v == XP_NULL)
2006-06-20 15:27:50 +00:00
{
2006-06-21 11:45:26 +00:00
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
2006-06-20 15:27:50 +00:00
}
2006-06-21 11:45:26 +00:00
xp_awk_setretval (run, v);
2006-06-20 15:27:50 +00:00
return 0;
}