2006-06-16 14:31:42 +00:00
|
|
|
/*
|
2006-09-08 15:26:49 +00:00
|
|
|
* $Id: func.c,v 1.47 2006-09-08 15:26:49 bacon Exp $
|
2006-06-16 14:31:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/awk/awk_i.h>
|
|
|
|
|
|
|
|
#ifndef XP_AWK_STAND_ALONE
|
2006-06-30 04:25:53 +00:00
|
|
|
#include <xp/bas/assert.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);
|
2006-08-21 14:51:32 +00:00
|
|
|
static int __bfn_fflush (xp_awk_t* awk, void* run);
|
2006-08-13 16:05:04 +00:00
|
|
|
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);
|
2006-08-17 03:49:30 +00:00
|
|
|
static int __bfn_tolower (xp_awk_t* awk, void* run);
|
|
|
|
static int __bfn_toupper (xp_awk_t* awk, void* run);
|
2006-08-13 16:05:04 +00:00
|
|
|
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-08-21 14:51:32 +00:00
|
|
|
/* io functions */
|
2006-08-17 03:49:30 +00:00
|
|
|
{ XP_T("close"), 5, XP_AWK_EXTIO, 1, 1, XP_NULL, __bfn_close },
|
2006-08-21 14:51:32 +00:00
|
|
|
{ XP_T("fflush"), 6, XP_AWK_EXTIO, 0, 1, XP_NULL, __bfn_fflush },
|
2006-07-17 04:17:40 +00:00
|
|
|
|
2006-08-17 03:49:30 +00:00
|
|
|
/* string functions */
|
|
|
|
{ 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 },
|
2006-08-20 15:49:48 +00:00
|
|
|
{ XP_T("split"), 5, 0, 2, 3, XP_T("vrv"), __bfn_split },
|
2006-08-17 03:49:30 +00:00
|
|
|
{ XP_T("tolower"), 7, 0, 1, 1, XP_NULL, __bfn_tolower },
|
|
|
|
{ XP_T("toupper"), 7, 0, 1, 1, XP_NULL, __bfn_toupper },
|
2006-08-24 03:30:32 +00:00
|
|
|
//{ XP_T("gsub"), 4, 0, 2, 3, XP_T("vrr"), __bfn_gsub },
|
2006-07-17 04:17:40 +00:00
|
|
|
|
2006-08-21 14:51:32 +00:00
|
|
|
/* TODO: remove these two functions */
|
2006-08-17 03:49:30 +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-17 03:49:30 +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-09-01 07:18:40 +00:00
|
|
|
xp_awk_t* awk, const xp_char_t* name, xp_size_t name_len,
|
|
|
|
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??? */
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
p = (xp_awk_bfn_t*) XP_AWK_MALLOC (awk, xp_sizeof(xp_awk_bfn_t));
|
2006-07-14 04:19:22 +00:00
|
|
|
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-09-01 06:23:58 +00:00
|
|
|
p->name_len = name_len;
|
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;
|
|
|
|
}
|
|
|
|
|
2006-09-01 07:18:40 +00:00
|
|
|
int xp_awk_delbfn (xp_awk_t* awk, const xp_char_t* name, xp_size_t name_len)
|
2006-07-14 04:19:22 +00:00
|
|
|
{
|
|
|
|
xp_awk_bfn_t* p, * pp = XP_NULL;
|
|
|
|
|
|
|
|
for (p = awk->bfn.user; p != XP_NULL; p++)
|
|
|
|
{
|
2006-09-01 06:23:58 +00:00
|
|
|
if (xp_awk_strxncmp(p->name, p->name_len, name, name_len) == 0)
|
2006-07-14 04:19:22 +00:00
|
|
|
{
|
|
|
|
if (pp == XP_NULL)
|
|
|
|
awk->bfn.user = p->next;
|
|
|
|
else pp->next = p->next;
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
XP_AWK_FREE (awk, p);
|
2006-07-14 04:19:22 +00:00
|
|
|
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;
|
2006-08-31 14:52:12 +00:00
|
|
|
XP_AWK_FREE (awk, p);
|
2006-07-14 04:19:22 +00:00
|
|
|
p = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
awk->bfn.user = XP_NULL;
|
2006-06-21 15:37:51 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
xp_awk_bfn_t* xp_awk_getbfn (
|
2006-09-01 07:18:40 +00:00
|
|
|
xp_awk_t* awk, const xp_char_t* name, xp_size_t name_len)
|
2006-06-21 15:37:51 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
if (xp_awk_strxncmp (
|
|
|
|
p->name, p->name_len, name, name_len) == 0) return p;
|
2006-07-13 15:43:39 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
/* TODO: user-added builtin functions... */
|
2006-07-13 15:43:39 +00:00
|
|
|
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-09-01 06:23:58 +00:00
|
|
|
if (xp_awk_strxncmp (
|
|
|
|
p->name, p->name_len, name, name_len) == 0) return p;
|
2006-06-16 14:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2006-08-02 03:22:51 +00:00
|
|
|
xp_awk_val_t* v, * a0;
|
2006-08-31 15:39:14 +00:00
|
|
|
int 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-31 15:39:14 +00:00
|
|
|
name = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len);
|
|
|
|
if (name == XP_NULL) return -1;
|
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. */
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, name);
|
2006-08-02 11:26:11 +00:00
|
|
|
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 */
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, name);
|
2006-08-02 03:22:51 +00:00
|
|
|
n = -1;
|
|
|
|
/* TODO: need to set ERRNO??? */
|
|
|
|
goto skip_close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-27 15:29:21 +00:00
|
|
|
n = xp_awk_closeextio (run, name);
|
|
|
|
if (n == -1 && ((xp_awk_run_t*)run)->errnum != XP_AWK_EIOHANDLER)
|
2006-06-21 11:45:26 +00:00
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, name);
|
2006-06-21 11:45:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, name);
|
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-27 15:29:21 +00:00
|
|
|
static int __flush_extio (
|
|
|
|
xp_awk_run_t* run, int extio, const xp_char_t* name, int n)
|
|
|
|
{
|
|
|
|
int n2;
|
|
|
|
|
|
|
|
if (run->extio.handler[extio] != XP_NULL)
|
|
|
|
{
|
2006-08-30 14:48:09 +00:00
|
|
|
n2 = xp_awk_flushextio (run, extio, name);
|
2006-08-27 15:29:21 +00:00
|
|
|
if (n2 == -1)
|
|
|
|
{
|
|
|
|
if (run->errnum == XP_AWK_EIOHANDLER) n = -1;
|
|
|
|
else if (run->errnum == XP_AWK_ENOSUCHIO)
|
|
|
|
{
|
|
|
|
if (n != 0) n = -2;
|
|
|
|
}
|
|
|
|
else n = -99;
|
|
|
|
}
|
|
|
|
else if (n != -1) n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-08-21 14:51:32 +00:00
|
|
|
static int __bfn_fflush (xp_awk_t* awk, void* run)
|
|
|
|
{
|
|
|
|
xp_size_t nargs;
|
|
|
|
xp_awk_val_t* a0;
|
|
|
|
xp_char_t* str0;
|
|
|
|
xp_size_t len0;
|
2006-08-31 15:39:14 +00:00
|
|
|
int n;
|
2006-08-21 14:51:32 +00:00
|
|
|
|
|
|
|
nargs = xp_awk_getnargs (run);
|
|
|
|
xp_assert (nargs >= 0 && nargs <= 1);
|
|
|
|
|
|
|
|
if (nargs == 0)
|
|
|
|
{
|
|
|
|
/* flush the console output */
|
2006-08-27 15:29:21 +00:00
|
|
|
n = xp_awk_flushextio (run, XP_AWK_OUT_CONSOLE, XP_T(""));
|
2006-08-23 15:42:16 +00:00
|
|
|
if (n == -1 &&
|
2006-08-27 15:29:21 +00:00
|
|
|
((xp_awk_run_t*)run)->errnum != XP_AWK_EIOHANDLER &&
|
|
|
|
((xp_awk_run_t*)run)->errnum != XP_AWK_ENOSUCHIO)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2006-08-27 15:29:21 +00:00
|
|
|
|
|
|
|
/* fflush() should return -1 on EIOHANDLER and ENOSUCHIO */
|
2006-08-21 14:51:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-22 15:11:13 +00:00
|
|
|
xp_char_t* ptr, * end;
|
|
|
|
|
2006-08-21 14:51:32 +00:00
|
|
|
a0 = xp_awk_getarg (run, 0);
|
|
|
|
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 (
|
2006-08-31 15:39:14 +00:00
|
|
|
run, a0, xp_true, XP_NULL, &len0);
|
|
|
|
if (str0 == XP_NULL) return -1;
|
2006-08-22 15:11:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the target name contains a null character.
|
|
|
|
* make fflush return -1 and set ERRNO accordingly */
|
|
|
|
ptr = str0; end = str0 + len0;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
if (*ptr == XP_T('\0'))
|
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str0);
|
2006-08-22 15:11:13 +00:00
|
|
|
n = -1;
|
|
|
|
goto skip_flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
2006-08-21 14:51:32 +00:00
|
|
|
}
|
|
|
|
|
2006-08-23 15:42:16 +00:00
|
|
|
/* flush the given extio */
|
|
|
|
n = 1;
|
2006-08-22 15:11:13 +00:00
|
|
|
|
2006-08-27 15:29:21 +00:00
|
|
|
n = __flush_extio (
|
|
|
|
run, XP_AWK_EXTIO_FILE,
|
|
|
|
((len0 == 0)? XP_NULL: str0), 1);
|
|
|
|
if (n == -99) return -1;
|
|
|
|
n = __flush_extio (
|
|
|
|
run, XP_AWK_EXTIO_PIPE,
|
|
|
|
((len0 == 0)? XP_NULL: str0), n);
|
|
|
|
if (n == -99) return -1;
|
|
|
|
n = __flush_extio (
|
|
|
|
run, XP_AWK_EXTIO_COPROC,
|
|
|
|
((len0 == 0)? XP_NULL: str0), n);
|
|
|
|
if (n == -99) return -1;
|
2006-08-21 14:51:32 +00:00
|
|
|
|
2006-08-23 15:42:16 +00:00
|
|
|
/* if n remains 1, no ip handlers have been defined for
|
|
|
|
* file, pipe, and coproc. so make fflush return -1.
|
|
|
|
* if n is -2, no such named io has been found at all
|
|
|
|
* if n is -1, the io handler has returned an error */
|
|
|
|
if (n != 0) n = -1;
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str0);
|
2006-08-21 14:51:32 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 15:11:13 +00:00
|
|
|
skip_flush:
|
2006-08-21 14:51:32 +00:00
|
|
|
a0 = xp_awk_makeintval (run, (xp_long_t)n);
|
|
|
|
if (a0 == XP_NULL)
|
|
|
|
{
|
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_awk_setretval (run, a0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2006-08-31 15:39:14 +00:00
|
|
|
str0 = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len0);
|
|
|
|
if (str0 == XP_NULL) return -1;
|
2006-08-13 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a1->type == XP_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
str1 = ((xp_awk_val_str_t*)a1)->buf;
|
|
|
|
len1 = ((xp_awk_val_str_t*)a1)->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-31 15:39:14 +00:00
|
|
|
str1 = xp_awk_valtostr (run, a1, xp_true, XP_NULL, &len1);
|
2006-08-13 16:05:04 +00:00
|
|
|
if (str1 == XP_NULL)
|
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str0);
|
2006-08-13 16:05:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
ptr = xp_awk_strxnstr (str0, len0, str1, len1);
|
2006-08-13 16:05:04 +00:00
|
|
|
idx = (ptr == XP_NULL)? -1: (xp_long_t)(ptr - str0);
|
|
|
|
|
|
|
|
if (xp_awk_getopt(awk) & XP_AWK_STRINDEXONE) idx = idx + 1;
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str0);
|
|
|
|
if (a1->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str1);
|
2006-08-13 16:05:04 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2006-08-31 15:39:14 +00:00
|
|
|
str = xp_awk_valtostr (run, v, xp_true, XP_NULL, &len);
|
|
|
|
if (str == XP_NULL) return -1;
|
2006-08-13 16:05:04 +00:00
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
XP_AWK_FREE (awk, str);
|
2006-08-13 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_awk_val_t* a0, * a1, * a2, * r;
|
2006-08-13 16:05:04 +00:00
|
|
|
xp_char_t* str;
|
|
|
|
xp_size_t len;
|
|
|
|
xp_long_t lindex, lcount;
|
|
|
|
xp_real_t rindex, rcount;
|
2006-08-31 15:39:14 +00:00
|
|
|
int n;
|
2006-08-13 16:05:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2006-08-31 15:39:14 +00:00
|
|
|
str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len);
|
|
|
|
if (str == XP_NULL) return -1;
|
2006-08-13 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
n = xp_awk_valtonum (run, a1, &lindex, &rindex);
|
2006-08-13 16:05:04 +00:00
|
|
|
if (n == -1)
|
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-13 16:05:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (n == 1) lindex = (xp_long_t)rindex;
|
|
|
|
|
|
|
|
if (a2 == XP_NULL) lcount = (xp_long_t)len;
|
|
|
|
else
|
|
|
|
{
|
2006-09-01 06:23:58 +00:00
|
|
|
n = xp_awk_valtonum (run, a2, &lcount, &rcount);
|
2006-08-13 16:05:04 +00:00
|
|
|
if (n == -1)
|
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-13 16:05:04 +00:00
|
|
|
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;
|
|
|
|
|
2006-08-31 15:22:40 +00:00
|
|
|
r = xp_awk_makestrval (run, &str[lindex], (xp_size_t)lcount);
|
2006-08-17 14:10:21 +00:00
|
|
|
if (r == XP_NULL)
|
2006-08-13 16:05:04 +00:00
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-13 16:05:04 +00:00
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_awk_setretval (run, r);
|
2006-08-13 16:05:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __bfn_split (xp_awk_t* awk, void* run)
|
|
|
|
{
|
|
|
|
xp_size_t nargs;
|
2006-09-03 15:47:11 +00:00
|
|
|
xp_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1ref;
|
|
|
|
xp_char_t* str, * str_free, * p, * tok;
|
|
|
|
xp_size_t str_len, str_left, tok_len;
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_long_t num;
|
2006-09-01 16:31:13 +00:00
|
|
|
xp_char_t key[xp_sizeof(xp_long_t)*8+2];
|
2006-09-02 14:59:15 +00:00
|
|
|
xp_size_t key_len;
|
2006-09-03 15:47:11 +00:00
|
|
|
xp_char_t* fs_ptr, * fs_free;
|
|
|
|
xp_size_t fs_len;
|
2006-09-05 04:11:11 +00:00
|
|
|
void* fs_rex = XP_NULL;
|
|
|
|
void* fs_rex_free = XP_NULL;
|
|
|
|
int errnum;
|
2006-08-13 16:05:04 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2006-08-20 15:49:48 +00:00
|
|
|
xp_assert (a1->type == XP_AWK_VAL_REF);
|
2006-08-18 07:52:47 +00:00
|
|
|
|
2006-08-20 15:49:48 +00:00
|
|
|
if (((xp_awk_val_ref_t*)a1)->id >= XP_AWK_VAL_REF_NAMEDIDX &&
|
|
|
|
((xp_awk_val_ref_t*)a1)->id <= XP_AWK_VAL_REF_ARGIDX)
|
2006-08-13 16:05:04 +00:00
|
|
|
{
|
2006-08-20 15:49:48 +00:00
|
|
|
/* an indexed value should not be assigned another map */
|
|
|
|
xp_awk_seterrnum (run, XP_AWK_EIDXVALASSMAP);
|
|
|
|
return -1;
|
2006-08-13 16:05:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-03 15:47:11 +00:00
|
|
|
a1ref = (xp_awk_val_t**)((xp_awk_val_ref_t*)a1)->adr;
|
|
|
|
if ((*a1ref)->type != XP_AWK_VAL_NIL &&
|
|
|
|
(*a1ref)->type != XP_AWK_VAL_MAP)
|
2006-08-17 14:10:21 +00:00
|
|
|
{
|
2006-08-20 15:49:48 +00:00
|
|
|
/* cannot change a scalar value to a map */
|
|
|
|
xp_awk_seterrnum (run, XP_AWK_ESCALARTOMAP);
|
2006-08-17 14:10:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-18 17:46:07 +00:00
|
|
|
if (a0->type == XP_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
str = ((xp_awk_val_str_t*)a0)->buf;
|
2006-09-03 15:47:11 +00:00
|
|
|
str_len = ((xp_awk_val_str_t*)a0)->len;
|
|
|
|
str_free = XP_NULL;
|
2006-08-18 17:46:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-03 15:47:11 +00:00
|
|
|
str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &str_len);
|
2006-08-31 15:39:14 +00:00
|
|
|
if (str == XP_NULL) return -1;
|
2006-09-03 15:47:11 +00:00
|
|
|
str_free = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a2 == XP_NULL)
|
|
|
|
{
|
|
|
|
/* get the value from FS */
|
|
|
|
t1 = xp_awk_getglobal (run, XP_AWK_GLOBAL_FS);
|
|
|
|
if (t1->type == XP_AWK_VAL_NIL)
|
|
|
|
{
|
|
|
|
fs_ptr = XP_T(" ");
|
|
|
|
fs_len = 1;
|
|
|
|
fs_free = XP_NULL;
|
|
|
|
}
|
|
|
|
else if (t1->type == XP_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
fs_ptr = ((xp_awk_val_str_t*)t1)->buf;
|
|
|
|
fs_len = ((xp_awk_val_str_t*)t1)->len;
|
|
|
|
fs_free = XP_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fs_ptr = xp_awk_valtostr (
|
|
|
|
run, t1, xp_true, XP_NULL, &fs_len);
|
|
|
|
if (fs_ptr == XP_NULL)
|
|
|
|
{
|
|
|
|
if (str_free != XP_NULL)
|
|
|
|
XP_AWK_FREE (awk, str_free);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fs_free = fs_ptr;
|
|
|
|
}
|
2006-09-05 04:11:11 +00:00
|
|
|
|
|
|
|
if (fs_len > 1)
|
|
|
|
{
|
|
|
|
fs_rex = ((xp_awk_run_t*)run)->rex.fs;
|
|
|
|
fs_rex_free = XP_NULL;
|
|
|
|
}
|
2006-09-03 15:47:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (a2->type == XP_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
fs_ptr = ((xp_awk_val_str_t*)a2)->buf;
|
|
|
|
fs_len = ((xp_awk_val_str_t*)a2)->len;
|
|
|
|
fs_free = XP_NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fs_ptr = xp_awk_valtostr (
|
|
|
|
run, a2, xp_true, XP_NULL, &fs_len);
|
|
|
|
if (fs_ptr == XP_NULL)
|
|
|
|
{
|
|
|
|
if (str_free != XP_NULL)
|
|
|
|
XP_AWK_FREE (awk, str_free);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fs_free = fs_ptr;
|
|
|
|
}
|
2006-09-05 04:11:11 +00:00
|
|
|
|
|
|
|
if (fs_len > 1)
|
|
|
|
{
|
|
|
|
fs_rex = xp_awk_buildrex (awk, fs_ptr, fs_len, &errnum);
|
|
|
|
if (fs_rex == XP_NULL)
|
|
|
|
{
|
|
|
|
if (str_free != XP_NULL)
|
|
|
|
XP_AWK_FREE (awk, str_free);
|
|
|
|
if (fs_free != XP_NULL)
|
|
|
|
XP_AWK_FREE (awk, fs_free);
|
|
|
|
xp_awk_seterrnum (run, errnum);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fs_rex_free = fs_rex;
|
|
|
|
}
|
2006-08-18 17:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t1 = xp_awk_makemapval (run);
|
|
|
|
if (t1 == XP_NULL)
|
|
|
|
{
|
2006-09-03 15:47:11 +00:00
|
|
|
if (str_free != XP_NULL) XP_AWK_FREE (awk, str_free);
|
|
|
|
if (fs_free != XP_NULL) XP_AWK_FREE (awk, fs_free);
|
2006-09-05 04:11:11 +00:00
|
|
|
if (fs_rex_free != XP_NULL) xp_awk_freerex (awk, fs_rex_free);
|
2006-08-18 17:46:07 +00:00
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-03 15:47:11 +00:00
|
|
|
xp_awk_refdownval (run, *a1ref);
|
|
|
|
*a1ref = t1;
|
|
|
|
xp_awk_refupval (*a1ref);
|
2006-08-19 16:34:24 +00:00
|
|
|
|
2006-09-03 15:47:11 +00:00
|
|
|
p = str; str_left = str_len; num = 0;
|
2006-08-18 17:46:07 +00:00
|
|
|
while (p != XP_NULL)
|
|
|
|
{
|
2006-09-03 15:47:11 +00:00
|
|
|
if (fs_len <= 1)
|
|
|
|
{
|
2006-09-08 15:26:49 +00:00
|
|
|
p = xp_awk_strxntok (run,
|
2006-09-03 15:47:11 +00:00
|
|
|
p, str_len, fs_ptr, fs_len, &tok, &tok_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-08 15:26:49 +00:00
|
|
|
p = xp_awk_strxntokbyrex (run, p, str_len,
|
2006-09-05 04:11:11 +00:00
|
|
|
fs_rex, &tok, &tok_len, &errnum);
|
|
|
|
if (p == XP_NULL && errnum != XP_AWK_ENOERR)
|
|
|
|
{
|
|
|
|
if (str_free != XP_NULL)
|
|
|
|
XP_AWK_FREE (awk, str_free);
|
|
|
|
if (fs_free != XP_NULL)
|
|
|
|
XP_AWK_FREE (awk, fs_free);
|
|
|
|
if (fs_rex_free != XP_NULL)
|
|
|
|
xp_awk_freerex (awk, fs_rex_free);
|
|
|
|
xp_awk_seterrnum (run, errnum);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-03 15:47:11 +00:00
|
|
|
}
|
2006-08-18 17:46:07 +00:00
|
|
|
|
|
|
|
if (num == 0 && p == XP_NULL && tok_len == 0)
|
|
|
|
{
|
|
|
|
/* no field at all*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_assert ((tok != XP_NULL && tok_len > 0) || tok_len == 0);
|
|
|
|
|
|
|
|
/* create the field string */
|
2006-08-31 15:22:40 +00:00
|
|
|
t2 = xp_awk_makestrval (run, tok, tok_len);
|
2006-08-18 17:46:07 +00:00
|
|
|
if (t2 == XP_NULL)
|
|
|
|
{
|
2006-09-03 15:47:11 +00:00
|
|
|
if (str_free != XP_NULL) XP_AWK_FREE (awk, str_free);
|
|
|
|
if (fs_free != XP_NULL) XP_AWK_FREE (awk, fs_free);
|
2006-09-05 04:11:11 +00:00
|
|
|
if (fs_rex_free != XP_NULL) xp_awk_freerex (awk, fs_rex_free);
|
2006-08-18 17:46:07 +00:00
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put it into the map */
|
2006-09-02 14:59:15 +00:00
|
|
|
key_len = xp_awk_longtostr (
|
|
|
|
num, 10, XP_NULL, key, xp_countof(key));
|
|
|
|
xp_assert (key_len != (xp_size_t)-1);
|
2006-08-18 17:46:07 +00:00
|
|
|
|
|
|
|
if (xp_awk_map_putx (
|
|
|
|
((xp_awk_val_map_t*)t1)->map,
|
2006-09-02 14:59:15 +00:00
|
|
|
key, key_len, t2, XP_NULL) == -1)
|
2006-08-18 17:46:07 +00:00
|
|
|
{
|
2006-09-03 15:47:11 +00:00
|
|
|
if (str_free != XP_NULL) XP_AWK_FREE (awk, str_free);
|
|
|
|
if (fs_free != XP_NULL) XP_AWK_FREE (awk, fs_free);
|
2006-09-05 04:11:11 +00:00
|
|
|
if (fs_rex_free != XP_NULL) xp_awk_freerex (awk, fs_rex_free);
|
2006-08-18 17:46:07 +00:00
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't forget to update the reference count
|
|
|
|
* when you handle the assignment-like situation
|
|
|
|
* with the internal data structures */
|
|
|
|
xp_awk_refupval (t2);
|
|
|
|
|
|
|
|
num++;
|
2006-09-05 04:11:11 +00:00
|
|
|
str_len = str_left - (p - str);
|
2006-08-18 17:46:07 +00:00
|
|
|
}
|
|
|
|
|
2006-09-03 15:47:11 +00:00
|
|
|
if (str_free != XP_NULL) XP_AWK_FREE (awk, str_free);
|
|
|
|
if (fs_free != XP_NULL) XP_AWK_FREE (awk, fs_free);
|
2006-09-05 04:11:11 +00:00
|
|
|
if (fs_rex_free != XP_NULL) xp_awk_freerex (awk, fs_rex_free);
|
2006-08-18 17:46:07 +00:00
|
|
|
|
|
|
|
t1 = xp_awk_makeintval (run, num);
|
|
|
|
if (t1 == XP_NULL)
|
|
|
|
{
|
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_awk_setretval (run, t1);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-13 16:05:04 +00:00
|
|
|
|
2006-08-17 03:49:30 +00:00
|
|
|
static int __bfn_tolower (xp_awk_t* awk, void* run)
|
|
|
|
{
|
|
|
|
xp_size_t nargs;
|
|
|
|
xp_char_t* str;
|
|
|
|
xp_size_t len, i;
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_awk_val_t* a0, * r;
|
2006-08-17 03:49:30 +00:00
|
|
|
|
|
|
|
nargs = xp_awk_getnargs (run);
|
|
|
|
xp_assert (nargs == 1);
|
|
|
|
|
|
|
|
a0 = xp_awk_getarg (run, 0);
|
|
|
|
|
|
|
|
if (a0->type == XP_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
str = ((xp_awk_val_str_t*)a0)->buf;
|
|
|
|
len = ((xp_awk_val_str_t*)a0)->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-31 15:39:14 +00:00
|
|
|
str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len);
|
|
|
|
if (str == XP_NULL) return -1;
|
2006-08-17 03:49:30 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
for (i = 0; i < len; i++) str[i] = XP_AWK_TOLOWER (awk, str[i]);
|
2006-08-17 03:49:30 +00:00
|
|
|
|
2006-08-31 15:22:40 +00:00
|
|
|
r = xp_awk_makestrval (run, str, len);
|
2006-08-17 14:10:21 +00:00
|
|
|
if (r == XP_NULL)
|
2006-08-17 03:49:30 +00:00
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-17 03:49:30 +00:00
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_awk_setretval (run, r);
|
2006-08-17 03:49:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __bfn_toupper (xp_awk_t* awk, void* run)
|
|
|
|
{
|
|
|
|
xp_size_t nargs;
|
|
|
|
xp_char_t* str;
|
|
|
|
xp_size_t len, i;
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_awk_val_t* a0, * r;
|
2006-08-17 03:49:30 +00:00
|
|
|
|
|
|
|
nargs = xp_awk_getnargs (run);
|
|
|
|
xp_assert (nargs == 1);
|
|
|
|
|
|
|
|
a0 = xp_awk_getarg (run, 0);
|
|
|
|
|
|
|
|
if (a0->type == XP_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
str = ((xp_awk_val_str_t*)a0)->buf;
|
|
|
|
len = ((xp_awk_val_str_t*)a0)->len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-08-31 15:39:14 +00:00
|
|
|
str = xp_awk_valtostr (run, a0, xp_true, XP_NULL, &len);
|
|
|
|
if (str == XP_NULL) return -1;
|
2006-08-17 03:49:30 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
for (i = 0; i < len; i++) str[i] = XP_AWK_TOUPPER (awk, str[i]);
|
2006-08-17 03:49:30 +00:00
|
|
|
|
2006-08-31 15:22:40 +00:00
|
|
|
r = xp_awk_makestrval (run, str, len);
|
2006-08-17 14:10:21 +00:00
|
|
|
if (r == XP_NULL)
|
2006-08-17 03:49:30 +00:00
|
|
|
{
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-17 03:49:30 +00:00
|
|
|
xp_awk_seterrnum (run, XP_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
if (a0->type != XP_AWK_VAL_STR) XP_AWK_FREE (awk, str);
|
2006-08-17 14:10:21 +00:00
|
|
|
xp_awk_setretval (run, r);
|
2006-08-17 03:49:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-13 16:05:04 +00:00
|
|
|
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;
|
2006-08-31 15:39:14 +00:00
|
|
|
int n;
|
2006-07-17 04:17:40 +00:00
|
|
|
|
|
|
|
nargs = xp_awk_getnargs (run);
|
|
|
|
xp_assert (nargs == 1);
|
|
|
|
|
|
|
|
cmd = xp_awk_valtostr (
|
2006-08-31 15:39:14 +00:00
|
|
|
run, xp_awk_getarg(run, 0), xp_true, XP_NULL, XP_NULL);
|
|
|
|
if (cmd == XP_NULL) return -1;
|
2006-07-17 04:17:40 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
n = _tsystem (cmd);
|
|
|
|
#else
|
2006-08-26 15:28:08 +00:00
|
|
|
/* TODO: support system on other platforms that win32 */
|
|
|
|
n = -1;
|
2006-07-17 04:17:40 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-31 14:52:12 +00:00
|
|
|
XP_AWK_FREE (awk, cmd);
|
2006-07-17 04:17:40 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2006-09-01 06:23:58 +00:00
|
|
|
n = xp_awk_valtonum (run, xp_awk_getarg(run, 0), &lv, &rv);
|
2006-07-17 04:17:40 +00:00
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
/* wrong value */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) rv = (xp_real_t)lv;
|
|
|
|
|
2006-08-26 16:30:53 +00:00
|
|
|
#if (XP_SIZEOF_REAL == XP_SIZEOF_LONG_DOUBLE)
|
2006-07-17 14:27:09 +00:00
|
|
|
v = xp_awk_makerealval (run, (xp_real_t)sinl(rv));
|
2006-08-26 16:30:53 +00:00
|
|
|
#elif (XP_SIZEOF_REAL == XP_SIZEOF_DOUBLE)
|
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;
|
|
|
|
}
|