qse/ase/awk/func.c

504 lines
10 KiB
C
Raw Normal View History

2006-06-16 14:31:42 +00:00
/*
2006-08-13 16:05:04 +00:00
* $Id: func.c,v 1.18 2006-08-13 16:04:32 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-30 04:25:53 +00:00
#include <xp/bas/assert.h>
2006-06-21 11:45:26 +00:00
#include <xp/bas/str.h>
2006-06-16 14:31:42 +00:00
#endif
2006-07-17 04:17:40 +00:00
#ifdef _WIN32
#include <tchar.h>
#include <math.h>
#else
#include <stdlib.h>
2006-08-13 16:05:04 +00:00
#include <math.h>
2006-07-17 04:17:40 +00:00
#endif
2006-08-13 16:05:04 +00:00
static int __bfn_close (xp_awk_t* awk, void* run);
static int __bfn_index (xp_awk_t* awk, void* run);
static int __bfn_length (xp_awk_t* awk, void* run);
static int __bfn_substr (xp_awk_t* awk, void* run);
static int __bfn_split (xp_awk_t* awk, void* run);
static int __bfn_system (xp_awk_t* awk, void* run);
static int __bfn_sin (xp_awk_t* awk, 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-07-13 15:43:39 +00:00
static xp_awk_bfn_t __sys_bfn[] =
2006-06-16 14:31:42 +00:00
{
2006-07-17 04:17:40 +00:00
/* ensure that this matches XP_AWK_BNF_XXX in func.h */
2006-08-13 16:05:04 +00:00
{ XP_T("close"), 5, XP_AWK_EXTIO, 1, 1, XP_NULL, __bfn_close },
2006-07-17 04:17:40 +00:00
2006-08-13 16:05:04 +00:00
{ XP_T("index"), 5, 0, 2, 2, XP_NULL, __bfn_index },
{ XP_T("length"), 6, 0, 1, 1, XP_NULL, __bfn_length },
{ XP_T("substr"), 6, 0, 2, 3, XP_NULL, __bfn_substr },
{ XP_T("split"), 5, 0, 2, 3, XP_NULL, __bfn_split },
2006-07-17 04:17:40 +00:00
2006-08-13 16:05:04 +00:00
{ XP_T("system"), 6, 0, 1, 1, XP_NULL, __bfn_system },
{ XP_T("sin"), 3, 0, 1, 1, XP_NULL, __bfn_sin },
2006-07-17 04:17:40 +00:00
2006-08-13 16:05:04 +00:00
{ XP_NULL, 0, 0, 0, 0, XP_NULL, 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 (
2006-08-13 16:05:04 +00:00
xp_awk_t* awk, const xp_char_t* name, int when_valid,
xp_size_t min_args, xp_size_t max_args, const xp_char_t* arg_spec,
int (*handler)(xp_awk_t*,void*))
2006-06-16 14:31:42 +00:00
{
2006-07-14 04:19:22 +00:00
xp_awk_bfn_t* p;
/* TODO: complete this function??? */
p = (xp_awk_bfn_t*) xp_malloc (xp_sizeof(xp_awk_bfn_t));
if (p == XP_NULL) return XP_NULL;
2006-07-14 05:21:30 +00:00
/* NOTE: make sure that name is a constant string */
p->name = name;
2006-07-14 04:19:22 +00:00
p->valid = when_valid;
p->min_args = min_args;
p->max_args = max_args;
2006-08-13 16:05:04 +00:00
p->arg_spec = arg_spec;
2006-07-14 04:19:22 +00:00
p->handler = handler;
p->next = awk->bfn.user;
awk->bfn.user = p;
return p;
}
int xp_awk_delbfn (xp_awk_t* awk, const xp_char_t* name)
{
xp_awk_bfn_t* p, * pp = XP_NULL;
for (p = awk->bfn.user; p != XP_NULL; p++)
{
if (xp_strcmp(p->name, name) == 0)
{
if (pp == XP_NULL)
awk->bfn.user = p->next;
else pp->next = p->next;
xp_free (p);
return 0;
}
pp = p;
}
return -1;
}
void xp_awk_clrbfn (xp_awk_t* awk)
{
xp_awk_bfn_t* p, * np;
p = awk->bfn.user;
while (p != XP_NULL)
{
np = p;
xp_free (p);
p = np;
}
awk->bfn.user = XP_NULL;
2006-06-21 15:37:51 +00:00
}
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-07-13 15:43:39 +00:00
for (p = __sys_bfn; p->name != XP_NULL; p++)
{
if (p->valid != 0 &&
2006-08-04 17:36:40 +00:00
(awk->option & p->valid) == 0) continue;
2006-07-13 15:43:39 +00:00
if (xp_strcmp (p->name, name) == 0) return p;
}
/* TODO: */
for (p = awk->bfn.user; p != XP_NULL; p = p->next)
2006-06-16 14:31:42 +00:00
{
2006-06-21 15:37:51 +00:00
if (p->valid != 0 &&
2006-08-04 17:36:40 +00:00
(awk->option & p->valid) == 0) continue;
2006-06-21 15:37:51 +00:00
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
2006-08-13 16:05:04 +00:00
static int __bfn_close (xp_awk_t* awk, void* run)
2006-06-20 15:27:50 +00:00
{
2006-06-21 11:45:26 +00:00
xp_size_t nargs;
xp_str_t buf;
2006-08-02 03:22:51 +00:00
xp_awk_val_t* v, * a0;
2006-06-21 11:45:26 +00:00
int errnum, n;
2006-08-02 03:22:51 +00:00
xp_char_t* name;
xp_size_t len;
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
2006-08-02 03:22:51 +00:00
a0 = xp_awk_getarg(run, 0);
xp_assert (a0 != XP_NULL);
if (a0->type == XP_AWK_VAL_STR)
2006-06-21 11:45:26 +00:00
{
2006-08-02 03:22:51 +00:00
name = ((xp_awk_val_str_t*)a0)->buf;
len = ((xp_awk_val_str_t*)a0)->len;
2006-06-21 11:45:26 +00:00
}
2006-08-02 03:22:51 +00:00
else
2006-06-21 11:45:26 +00:00
{
2006-08-02 03:22:51 +00:00
if (xp_str_open (&buf, 256) == XP_NULL)
{
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
2006-08-03 05:05:48 +00:00
if (xp_awk_valtostr (
a0, &errnum, xp_true, &buf, XP_NULL) == XP_NULL)
2006-08-02 03:22:51 +00:00
{
xp_str_close (&buf);
xp_awk_seterrnum (run, errnum);
return -1;
}
name = XP_STR_BUF(&buf);
len = XP_STR_LEN(&buf);
2006-06-21 11:45:26 +00:00
}
2006-08-02 11:26:11 +00:00
if (len == 0)
{
/* getline or print doesn't allow an emptry for the
* input or output file name. so close should not allow
* it either.
* another reason for this is if close is called explicitly
* with an empty string, it may close the console that uses
* an empty string for its identification because closeextio
* closes any extios that match the name given unlike
* closeextio_read or closeextio_write. */
if (a0->type != XP_AWK_VAL_STR) xp_str_close (&buf);
n = -1;
/* TODO: need to set ERRNO??? */
goto skip_close;
}
2006-08-02 03:22:51 +00:00
while (len > 0)
{
if (name[--len] == XP_T('\0'))
{
/* the name contains a null string.
* make close return -1 */
if (a0->type != XP_AWK_VAL_STR) xp_str_close (&buf);
n = -1;
/* TODO: need to set ERRNO??? */
goto skip_close;
}
}
n = xp_awk_closeextio (run, name, &errnum);
2006-06-21 11:45:26 +00:00
if (n == -1 && errnum != XP_AWK_ENOERR)
{
2006-08-02 03:22:51 +00:00
if (a0->type != XP_AWK_VAL_STR) xp_str_close (&buf);
2006-06-21 11:45:26 +00:00
xp_awk_seterrnum (run, errnum);
return -1;
}
2006-08-02 03:22:51 +00:00
if (a0->type != XP_AWK_VAL_STR) xp_str_close (&buf);
2006-06-21 11:45:26 +00:00
2006-08-02 03:22:51 +00:00
skip_close:
2006-06-21 11:45:26 +00:00
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;
}
2006-07-17 04:17:40 +00:00
2006-08-13 16:05:04 +00:00
static int __bfn_index (xp_awk_t* awk, void* run)
{
xp_size_t nargs;
xp_awk_val_t* a0, * a1;
xp_char_t* str0, * str1, * ptr;
xp_size_t len0, len1;
xp_long_t idx;
int errnum;
nargs = xp_awk_getnargs (run);
xp_assert (nargs == 2);
a0 = xp_awk_getarg (run, 0);
a1 = xp_awk_getarg (run, 1);
if (a0->type == XP_AWK_VAL_STR)
{
str0 = ((xp_awk_val_str_t*)a0)->buf;
len0 = ((xp_awk_val_str_t*)a0)->len;
}
else
{
str0 = xp_awk_valtostr (a0, &errnum, xp_true, XP_NULL, &len0);
if (str0 == XP_NULL)
{
xp_awk_seterrnum (run, errnum);
return -1;
}
}
if (a1->type == XP_AWK_VAL_STR)
{
str1 = ((xp_awk_val_str_t*)a1)->buf;
len1 = ((xp_awk_val_str_t*)a1)->len;
}
else
{
str1 = xp_awk_valtostr (a1, &errnum, xp_true, XP_NULL, &len1);
if (str1 == XP_NULL)
{
xp_awk_seterrnum (run, errnum);
if (a0->type != XP_AWK_VAL_STR) xp_free (str0);
return -1;
}
}
ptr = xp_strxnstr (str0, len0, str1, len1);
idx = (ptr == XP_NULL)? -1: (xp_long_t)(ptr - str0);
if (xp_awk_getopt(awk) & XP_AWK_STRINDEXONE) idx = idx + 1;
if (a0->type != XP_AWK_VAL_STR) xp_free (str0);
if (a1->type != XP_AWK_VAL_STR) xp_free (str1);
a0 = xp_awk_makeintval (run, idx);
if (a0 == XP_NULL)
{
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
xp_awk_setretval (run, a0);
return 0;
}
static int __bfn_length (xp_awk_t* awk, void* run)
{
xp_size_t nargs;
xp_awk_val_t* v;
xp_char_t* str;
xp_size_t len;
int errnum;
nargs = xp_awk_getnargs (run);
xp_assert (nargs == 1);
v = xp_awk_getarg (run, 0);
if (v->type == XP_AWK_VAL_STR)
{
len = ((xp_awk_val_str_t*)v)->len;
}
else
{
str = xp_awk_valtostr (v, &errnum, xp_true, XP_NULL, &len);
if (str == XP_NULL)
{
xp_awk_seterrnum (run, errnum);
return -1;
}
xp_free (str);
}
v = xp_awk_makeintval (run, len);
if (v == XP_NULL)
{
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
xp_awk_setretval (run, v);
return 0;
}
static int __bfn_substr (xp_awk_t* awk, void* run)
{
xp_size_t nargs;
xp_awk_val_t* a0, * a1, * a2;
xp_char_t* str;
xp_size_t len;
xp_long_t lindex, lcount;
xp_real_t rindex, rcount;
int errnum, n;
nargs = xp_awk_getnargs (run);
xp_assert (nargs >= 2 && nargs <= 3);
a0 = xp_awk_getarg (run, 0);
a1 = xp_awk_getarg (run, 1);
a2 = (nargs >= 3)? xp_awk_getarg (run, 2): XP_NULL;
if (a0->type == XP_AWK_VAL_STR)
{
str = ((xp_awk_val_str_t*)a0)->buf;
len = ((xp_awk_val_str_t*)a0)->len;
}
else
{
str = xp_awk_valtostr (a0, &errnum, xp_true, XP_NULL, &len);
if (str == XP_NULL)
{
xp_awk_seterrnum (run, errnum);
return -1;
}
}
n = xp_awk_valtonum (a1, &lindex, &rindex);
if (n == -1)
{
if (a0->type != XP_AWK_VAL_STR) xp_free (str);
xp_awk_seterrnum (run, XP_AWK_EVALTYPE);
return -1;
}
if (n == 1) lindex = (xp_long_t)rindex;
if (a2 == XP_NULL) lcount = (xp_long_t)len;
else
{
n = xp_awk_valtonum (a2, &lcount, &rcount);
if (n == -1)
{
if (a0->type != XP_AWK_VAL_STR) xp_free (str);
xp_awk_seterrnum (run, XP_AWK_EVALTYPE);
return -1;
}
if (n == 1) lcount = (xp_long_t)rcount;
}
if (xp_awk_getopt(awk) & XP_AWK_STRINDEXONE) lindex = lindex - 1;
if (lindex >= len) lindex = len;
else if (lindex < 0) lindex = 0;
if (lcount < 0) lcount = 0;
else if (lcount > len - lindex) lcount = len - lindex;
a0 = xp_awk_makestrval (&str[lindex], (xp_size_t)lcount);
if (a0 == XP_NULL)
{
if (a0->type != XP_AWK_VAL_STR) xp_free (str);
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
if (a0->type != XP_AWK_VAL_STR) xp_free (str);
xp_awk_setretval (run, a0);
return 0;
}
static int __bfn_split (xp_awk_t* awk, void* run)
{
xp_size_t nargs;
xp_awk_val_t* a0, * a1, * a2;
nargs = xp_awk_getnargs (run);
xp_assert (nargs >= 2 && nargs <= 3);
a0 = xp_awk_getarg (run, 0);
a1 = xp_awk_getarg (run, 1);
a2 = (nargs >= 3)? xp_awk_getarg (run, 2): XP_NULL;
/* TODO: what should i do when it is nil??? */
if (a1->type != XP_AWK_VAL_MAP)
{
}
/* TODO: */
xp_awk_setretval (run, a0);
return 0;
}
static int __bfn_system (xp_awk_t* awk, void* run)
2006-07-17 04:17:40 +00:00
{
xp_size_t nargs;
xp_char_t* cmd;
xp_awk_val_t* v;
int errnum, n;
nargs = xp_awk_getnargs (run);
xp_assert (nargs == 1);
cmd = xp_awk_valtostr (
2006-08-03 05:05:48 +00:00
xp_awk_getarg(run, 0), &errnum, xp_true, XP_NULL, XP_NULL);
2006-07-17 04:17:40 +00:00
if (cmd == XP_NULL)
{
2006-08-13 16:05:04 +00:00
xp_awk_seterrnum (run, errnum);
2006-07-17 04:17:40 +00:00
return -1;
}
#ifdef _WIN32
n = _tsystem (cmd);
#else
#error NOT SUPPORTED ...
#endif
xp_free (cmd);
v = xp_awk_makeintval (run, n);
if (v == XP_NULL)
{
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
xp_awk_setretval (run, v);
return 0;
}
/* math functions */
2006-08-13 16:05:04 +00:00
static int __bfn_sin (xp_awk_t* awk, void* run)
2006-07-17 04:17:40 +00:00
{
xp_size_t nargs;
xp_awk_val_t* v;
int n;
xp_long_t lv;
xp_real_t rv;
nargs = xp_awk_getnargs (run);
xp_assert (nargs == 1);
n = xp_awk_valtonum (
xp_awk_getarg(run, 0), &lv, &rv);
if (n == -1)
{
/* wrong value */
xp_awk_seterrnum (run, XP_AWK_EVALTYPE);
return -1;
}
if (n == 0) rv = (xp_real_t)lv;
2006-07-17 14:27:09 +00:00
#if (XP_SIZEOF_LONG_DOUBLE != 0)
v = xp_awk_makerealval (run, (xp_real_t)sinl(rv));
#elif (XP_SIZEOF_DOUBLE != 0)
2006-07-17 06:21:39 +00:00
v = xp_awk_makerealval (run, (xp_real_t)sin(rv));
2006-07-17 14:27:09 +00:00
#else
#error unsupported floating-point data type
#endif
2006-07-17 04:17:40 +00:00
if (v == XP_NULL)
{
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
return -1;
}
xp_awk_setretval (run, v);
return 0;
}