qse/ase/awk/func.c

1331 lines
29 KiB
C
Raw Normal View History

2006-06-16 14:31:42 +00:00
/*
2006-10-22 11:34:53 +00:00
* $Id: func.c,v 1.66 2006-10-22 11:34:53 bacon Exp $
2006-06-16 14:31:42 +00:00
*/
2006-10-22 11:34:53 +00:00
#include <sse/awk/awk_i.h>
2006-06-16 14:31:42 +00:00
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-10-22 11:34:53 +00:00
static int __bfn_close (sse_awk_run_t* run);
static int __bfn_fflush (sse_awk_run_t* run);
static int __bfn_index (sse_awk_run_t* run);
static int __bfn_length (sse_awk_run_t* run);
static int __bfn_substr (sse_awk_run_t* run);
static int __bfn_split (sse_awk_run_t* run);
static int __bfn_tolower (sse_awk_run_t* run);
static int __bfn_toupper (sse_awk_run_t* run);
static int __bfn_gsub (sse_awk_run_t* run);
static int __bfn_sub (sse_awk_run_t* run);
static int __bfn_match (sse_awk_run_t* run);
static int __bfn_system (sse_awk_run_t* run);
/*static int __bfn_sin (sse_awk_run_t* run);*/
2006-09-14 06:40:06 +00:00
2006-06-21 15:37:51 +00:00
/* TODO: move it under the awk structure... */
2006-10-22 11:34:53 +00:00
static sse_awk_bfn_t __sys_bfn[] =
2006-06-16 14:31:42 +00:00
{
2006-08-21 14:51:32 +00:00
/* io functions */
2006-10-22 11:34:53 +00:00
{SSE_T("close"), 5, SSE_AWK_EXTIO, 1, 1, SSE_NULL, __bfn_close},
{SSE_T("fflush"), 6, SSE_AWK_EXTIO, 0, 1, SSE_NULL, __bfn_fflush},
2006-07-17 04:17:40 +00:00
2006-08-17 03:49:30 +00:00
/* string functions */
2006-10-22 11:34:53 +00:00
{SSE_T("index"), 5, 0, 2, 2, SSE_NULL, __bfn_index},
{SSE_T("substr"), 6, 0, 2, 3, SSE_NULL, __bfn_substr},
{SSE_T("length"), 6, 0, 1, 1, SSE_NULL, __bfn_length},
{SSE_T("split"), 5, 0, 2, 3, SSE_T("vrv"), __bfn_split},
{SSE_T("tolower"), 7, 0, 1, 1, SSE_NULL, __bfn_tolower},
{SSE_T("toupper"), 7, 0, 1, 1, SSE_NULL, __bfn_toupper},
{SSE_T("gsub"), 4, 0, 2, 3, SSE_T("xvr"), __bfn_gsub},
{SSE_T("sub"), 3, 0, 2, 3, SSE_T("xvr"), __bfn_sub},
{SSE_T("match"), 5, 0, 2, 2, SSE_T("vx"), __bfn_match},
2006-07-17 04:17:40 +00:00
2006-08-21 14:51:32 +00:00
/* TODO: remove these two functions */
2006-10-22 11:34:53 +00:00
{SSE_T("system"), 6, 0, 1, 1, SSE_NULL, __bfn_system},
/*{ SSE_T("sin"), 3, 0, 1, 1, SSE_NULL, __bfn_sin},*/
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
{SSE_NULL, 0, 0, 0, 0, SSE_NULL, SSE_NULL}
2006-06-16 14:31:42 +00:00
};
2006-10-22 11:34:53 +00:00
sse_awk_bfn_t* sse_awk_addbfn (
sse_awk_t* awk, const sse_char_t* name, sse_size_t name_len,
int when_valid, sse_size_t min_args, sse_size_t max_args,
const sse_char_t* arg_spec, int (*handler)(sse_awk_run_t*))
2006-06-16 14:31:42 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_bfn_t* p;
2006-07-14 04:19:22 +00:00
/* TODO: complete this function??? */
2006-10-22 11:34:53 +00:00
p = (sse_awk_bfn_t*) SSE_AWK_MALLOC (awk, sse_sizeof(sse_awk_bfn_t));
if (p == SSE_NULL) return SSE_NULL;
2006-07-14 04:19:22 +00:00
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-10-22 11:34:53 +00:00
int sse_awk_delbfn (sse_awk_t* awk, const sse_char_t* name, sse_size_t name_len)
2006-07-14 04:19:22 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_bfn_t* p, * pp = SSE_NULL;
2006-07-14 04:19:22 +00:00
2006-10-22 11:34:53 +00:00
for (p = awk->bfn.user; p != SSE_NULL; p++)
2006-07-14 04:19:22 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_strxncmp(p->name, p->name_len, name, name_len) == 0)
2006-07-14 04:19:22 +00:00
{
2006-10-22 11:34:53 +00:00
if (pp == SSE_NULL)
2006-07-14 04:19:22 +00:00
awk->bfn.user = p->next;
else pp->next = p->next;
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (awk, p);
2006-07-14 04:19:22 +00:00
return 0;
}
pp = p;
}
return -1;
}
2006-10-22 11:34:53 +00:00
void sse_awk_clrbfn (sse_awk_t* awk)
2006-07-14 04:19:22 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_bfn_t* p, * np;
2006-07-14 04:19:22 +00:00
p = awk->bfn.user;
2006-10-22 11:34:53 +00:00
while (p != SSE_NULL)
2006-07-14 04:19:22 +00:00
{
np = p;
2006-10-22 11:34:53 +00:00
SSE_AWK_FREE (awk, p);
2006-07-14 04:19:22 +00:00
p = np;
}
2006-10-22 11:34:53 +00:00
awk->bfn.user = SSE_NULL;
2006-06-21 15:37:51 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_bfn_t* sse_awk_getbfn (
sse_awk_t* awk, const sse_char_t* name, sse_size_t name_len)
2006-06-21 15:37:51 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_bfn_t* p;
2006-06-16 14:31:42 +00:00
2006-10-22 11:34:53 +00:00
for (p = __sys_bfn; p->name != SSE_NULL; p++)
2006-07-13 15:43:39 +00:00
{
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-10-22 11:34:53 +00:00
if (sse_awk_strxncmp (
2006-09-01 06:23:58 +00:00
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-10-22 11:34:53 +00:00
for (p = awk->bfn.user; p != SSE_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-10-22 11:34:53 +00:00
if (sse_awk_strxncmp (
2006-09-01 06:23:58 +00:00
p->name, p->name_len, name, name_len) == 0) return p;
2006-06-16 14:31:42 +00:00
}
2006-10-22 11:34:53 +00:00
return SSE_NULL;
2006-06-16 14:31:42 +00:00
}
2006-06-20 15:27:50 +00:00
2006-10-22 11:34:53 +00:00
static int __bfn_close (sse_awk_run_t* run)
2006-06-20 15:27:50 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* v, * a0;
2006-08-31 15:39:14 +00:00
int n;
2006-08-02 03:22:51 +00:00
2006-10-22 11:34:53 +00:00
sse_char_t* name;
sse_size_t len;
2006-06-20 15:27:50 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, 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-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
sse_awk_assert (run->awk, a0 != SSE_NULL);
2006-08-02 03:22:51 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
name = ((sse_awk_val_str_t*)a0)->buf;
len = ((sse_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-10-22 11:34:53 +00:00
name = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len);
if (name == SSE_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.
2006-10-22 11:34:53 +00:00
* another reason for this is if close is called esselicitly
2006-08-02 11:26:11 +00:00
* 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-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->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)
{
2006-10-22 11:34:53 +00:00
if (name[--len] == SSE_T('\0'))
2006-08-02 03:22:51 +00:00
{
/* the name contains a null string.
* make close return -1 */
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->awk, name);
2006-08-02 03:22:51 +00:00
n = -1;
/* TODO: need to set ERRNO??? */
goto skip_close;
}
}
2006-10-22 11:34:53 +00:00
n = sse_awk_closeextio (run, name);
if (n == -1 && run->errnum != SSE_AWK_EIOHANDLER)
2006-06-21 11:45:26 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->awk, name);
2006-06-21 11:45:26 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, name);
2006-06-21 11:45:26 +00:00
2006-08-02 03:22:51 +00:00
skip_close:
2006-10-22 11:34:53 +00:00
v = sse_awk_makeintval (run, n);
if (v == SSE_NULL)
2006-06-20 15:27:50 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-06-21 11:45:26 +00:00
return -1;
2006-06-20 15:27:50 +00:00
}
2006-10-22 11:34:53 +00:00
sse_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 (
2006-10-22 11:34:53 +00:00
sse_awk_run_t* run, int extio, const sse_char_t* name, int n)
2006-08-27 15:29:21 +00:00
{
int n2;
2006-10-22 11:34:53 +00:00
if (run->extio.handler[extio] != SSE_NULL)
2006-08-27 15:29:21 +00:00
{
2006-10-22 11:34:53 +00:00
n2 = sse_awk_flushextio (run, extio, name);
2006-08-27 15:29:21 +00:00
if (n2 == -1)
{
2006-10-22 11:34:53 +00:00
if (run->errnum == SSE_AWK_EIOHANDLER) n = -1;
else if (run->errnum == SSE_AWK_ENOSUCHIO)
2006-08-27 15:29:21 +00:00
{
if (n != 0) n = -2;
}
else n = -99;
}
else if (n != -1) n = 0;
}
return n;
}
2006-10-22 11:34:53 +00:00
static int __bfn_fflush (sse_awk_run_t* run)
2006-08-21 14:51:32 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* a0;
sse_char_t* str0;
sse_size_t len0;
2006-08-31 15:39:14 +00:00
int n;
2006-08-21 14:51:32 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs >= 0 && nargs <= 1);
2006-08-21 14:51:32 +00:00
if (nargs == 0)
{
/* flush the console output */
2006-10-22 11:34:53 +00:00
n = sse_awk_flushextio (run, SSE_AWK_OUT_CONSOLE, SSE_T(""));
2006-08-23 15:42:16 +00:00
if (n == -1 &&
2006-10-22 11:34:53 +00:00
run->errnum != SSE_AWK_EIOHANDLER &&
run->errnum != SSE_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-10-22 11:34:53 +00:00
sse_char_t* ptr, * end;
2006-08-22 15:11:13 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
if (a0->type == SSE_AWK_VAL_STR)
2006-08-21 14:51:32 +00:00
{
2006-10-22 11:34:53 +00:00
str0 = ((sse_awk_val_str_t*)a0)->buf;
len0 = ((sse_awk_val_str_t*)a0)->len;
2006-08-21 14:51:32 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str0 = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len0);
if (str0 == SSE_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)
{
2006-10-22 11:34:53 +00:00
if (*ptr == SSE_T('\0'))
2006-08-22 15:11:13 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->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 (
2006-10-22 11:34:53 +00:00
run, SSE_AWK_EXTIO_FILE,
((len0 == 0)? SSE_NULL: str0), 1);
2006-08-27 15:29:21 +00:00
if (n == -99) return -1;
n = __flush_extio (
2006-10-22 11:34:53 +00:00
run, SSE_AWK_EXTIO_PIPE,
((len0 == 0)? SSE_NULL: str0), n);
2006-08-27 15:29:21 +00:00
if (n == -99) return -1;
n = __flush_extio (
2006-10-22 11:34:53 +00:00
run, SSE_AWK_EXTIO_COPROC,
((len0 == 0)? SSE_NULL: str0), n);
2006-08-27 15:29:21 +00:00
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-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str0);
2006-08-21 14:51:32 +00:00
}
2006-08-22 15:11:13 +00:00
skip_flush:
2006-10-22 11:34:53 +00:00
a0 = sse_awk_makeintval (run, (sse_long_t)n);
if (a0 == SSE_NULL)
2006-08-21 14:51:32 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-21 14:51:32 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, a0);
2006-08-21 14:51:32 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_index (sse_awk_run_t* run)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* a0, * a1;
sse_char_t* str0, * str1, * ptr;
sse_size_t len0, len1;
sse_long_t idx;
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 2);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
a1 = sse_awk_getarg (run, 1);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
str0 = ((sse_awk_val_str_t*)a0)->buf;
len0 = ((sse_awk_val_str_t*)a0)->len;
2006-08-13 16:05:04 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str0 = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len0);
if (str0 == SSE_NULL) return -1;
2006-08-13 16:05:04 +00:00
}
2006-10-22 11:34:53 +00:00
if (a1->type == SSE_AWK_VAL_STR)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
str1 = ((sse_awk_val_str_t*)a1)->buf;
len1 = ((sse_awk_val_str_t*)a1)->len;
2006-08-13 16:05:04 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str1 = sse_awk_valtostr (
run, a1, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len1);
if (str1 == SSE_NULL)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->awk, str0);
2006-08-13 16:05:04 +00:00
return -1;
}
}
2006-10-22 11:34:53 +00:00
ptr = sse_awk_strxnstr (str0, len0, str1, len1);
idx = (ptr == SSE_NULL)? -1: (sse_long_t)(ptr - str0);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
if (sse_awk_getopt(run->awk) & SSE_AWK_STRINDEXONE) idx = idx + 1;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str0);
if (a1->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str1);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_makeintval (run, idx);
if (a0 == SSE_NULL)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-13 16:05:04 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, a0);
2006-08-13 16:05:04 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_length (sse_awk_run_t* run)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* v;
sse_char_t* str;
sse_size_t len;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 1);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
v = sse_awk_getarg (run, 0);
if (v->type == SSE_AWK_VAL_STR)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
len = ((sse_awk_val_str_t*)v)->len;
2006-08-13 16:05:04 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str = sse_awk_valtostr (
run, v, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len);
if (str == SSE_NULL) return -1;
SSE_AWK_FREE (run->awk, str);
2006-08-13 16:05:04 +00:00
}
2006-10-22 11:34:53 +00:00
v = sse_awk_makeintval (run, len);
if (v == SSE_NULL)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-13 16:05:04 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, v);
2006-08-13 16:05:04 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_substr (sse_awk_run_t* run)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* a0, * a1, * a2, * r;
sse_char_t* str;
sse_size_t len;
sse_long_t lindex, lcount;
sse_real_t rindex, rcount;
2006-08-31 15:39:14 +00:00
int n;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs >= 2 && nargs <= 3);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
a1 = sse_awk_getarg (run, 1);
a2 = (nargs >= 3)? sse_awk_getarg (run, 2): SSE_NULL;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
str = ((sse_awk_val_str_t*)a0)->buf;
len = ((sse_awk_val_str_t*)a0)->len;
2006-08-13 16:05:04 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len);
if (str == SSE_NULL) return -1;
2006-08-13 16:05:04 +00:00
}
2006-10-22 11:34:53 +00:00
n = sse_awk_valtonum (run, a1, &lindex, &rindex);
2006-08-13 16:05:04 +00:00
if (n == -1)
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
2006-08-13 16:05:04 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (n == 1) lindex = (sse_long_t)rindex;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
if (a2 == SSE_NULL) lcount = (sse_long_t)len;
2006-08-13 16:05:04 +00:00
else
{
2006-10-22 11:34:53 +00:00
n = sse_awk_valtonum (run, a2, &lcount, &rcount);
2006-08-13 16:05:04 +00:00
if (n == -1)
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->awk, str);
2006-08-13 16:05:04 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (n == 1) lcount = (sse_long_t)rcount;
2006-08-13 16:05:04 +00:00
}
2006-10-22 11:34:53 +00:00
if (sse_awk_getopt(run->awk) & SSE_AWK_STRINDEXONE) lindex = lindex - 1;
2006-08-13 16:05:04 +00:00
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-10-22 11:34:53 +00:00
r = sse_awk_makestrval (run, &str[lindex], (sse_size_t)lcount);
if (r == SSE_NULL)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-13 16:05:04 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
sse_awk_setretval (run, r);
2006-08-13 16:05:04 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_split (sse_awk_run_t* run)
2006-08-13 16:05:04 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref;
sse_char_t* str, * str_free, * p, * tok;
sse_size_t str_len, str_left, tok_len;
sse_long_t sta, num;
sse_char_t key[sse_sizeof(sse_long_t)*8+2];
sse_size_t key_len;
sse_char_t* fs_ptr, * fs_free;
sse_size_t fs_len;
void* fs_rex = SSE_NULL;
void* fs_rex_free = SSE_NULL;
2006-09-05 04:11:11 +00:00
int errnum;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs >= 2 && nargs <= 3);
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
a1 = sse_awk_getarg (run, 1);
a2 = (nargs >= 3)? sse_awk_getarg (run, 2): SSE_NULL;
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_assert (run->awk, a1->type == SSE_AWK_VAL_REF);
2006-08-18 07:52:47 +00:00
2006-10-22 11:34:53 +00:00
if (((sse_awk_val_ref_t*)a1)->id >= SSE_AWK_VAL_REF_NAMEDIDX &&
((sse_awk_val_ref_t*)a1)->id <= SSE_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 */
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_EIDXVALASSMAP);
2006-10-10 07:02:38 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (((sse_awk_val_ref_t*)a1)->id == SSE_AWK_VAL_REF_POS)
2006-10-10 07:02:38 +00:00
{
/* a positional should not be assigned a map */
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_EPOSVALASSMAP);
2006-08-20 15:49:48 +00:00
return -1;
2006-08-13 16:05:04 +00:00
}
2006-10-22 11:34:53 +00:00
a1_ref = (sse_awk_val_t**)((sse_awk_val_ref_t*)a1)->adr;
if ((*a1_ref)->type != SSE_AWK_VAL_NIL &&
(*a1_ref)->type != SSE_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 */
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ESCALARTOMAP);
2006-08-17 14:10:21 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-08-18 17:46:07 +00:00
{
2006-10-22 11:34:53 +00:00
str = ((sse_awk_val_str_t*)a0)->buf;
str_len = ((sse_awk_val_str_t*)a0)->len;
str_free = SSE_NULL;
2006-08-18 17:46:07 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &str_len);
if (str == SSE_NULL) return -1;
2006-09-03 15:47:11 +00:00
str_free = str;
}
2006-10-22 11:34:53 +00:00
if (a2 == SSE_NULL)
2006-09-03 15:47:11 +00:00
{
/* get the value from FS */
2006-10-22 11:34:53 +00:00
t1 = sse_awk_getglobal (run, SSE_AWK_GLOBAL_FS);
if (t1->type == SSE_AWK_VAL_NIL)
2006-09-03 15:47:11 +00:00
{
2006-10-22 11:34:53 +00:00
fs_ptr = SSE_T(" ");
2006-09-03 15:47:11 +00:00
fs_len = 1;
2006-10-22 11:34:53 +00:00
fs_free = SSE_NULL;
2006-09-03 15:47:11 +00:00
}
2006-10-22 11:34:53 +00:00
else if (t1->type == SSE_AWK_VAL_STR)
2006-09-03 15:47:11 +00:00
{
2006-10-22 11:34:53 +00:00
fs_ptr = ((sse_awk_val_str_t*)t1)->buf;
fs_len = ((sse_awk_val_str_t*)t1)->len;
fs_free = SSE_NULL;
2006-09-03 15:47:11 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
fs_ptr = sse_awk_valtostr (
run, t1, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &fs_len);
if (fs_ptr == SSE_NULL)
2006-09-03 15:47:11 +00:00
{
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
2006-09-03 15:47:11 +00:00
return -1;
}
fs_free = fs_ptr;
}
2006-09-05 04:11:11 +00:00
if (fs_len > 1)
{
2006-10-10 07:02:38 +00:00
fs_rex = run->global.fs;
2006-10-22 11:34:53 +00:00
fs_rex_free = SSE_NULL;
2006-09-05 04:11:11 +00:00
}
2006-09-03 15:47:11 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
if (a2->type == SSE_AWK_VAL_STR)
2006-09-03 15:47:11 +00:00
{
2006-10-22 11:34:53 +00:00
fs_ptr = ((sse_awk_val_str_t*)a2)->buf;
fs_len = ((sse_awk_val_str_t*)a2)->len;
fs_free = SSE_NULL;
2006-09-03 15:47:11 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
fs_ptr = sse_awk_valtostr (
run, a2, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &fs_len);
if (fs_ptr == SSE_NULL)
2006-09-03 15:47:11 +00:00
{
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
2006-09-03 15:47:11 +00:00
return -1;
}
fs_free = fs_ptr;
}
2006-09-05 04:11:11 +00:00
if (fs_len > 1)
{
2006-10-22 11:34:53 +00:00
fs_rex = sse_awk_buildrex (
2006-10-10 07:02:38 +00:00
run->awk, fs_ptr, fs_len, &errnum);
2006-10-22 11:34:53 +00:00
if (fs_rex == SSE_NULL)
2006-09-05 04:11:11 +00:00
{
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
if (fs_free != SSE_NULL)
SSE_AWK_FREE (run->awk, fs_free);
sse_awk_setrunerrnum (run, errnum);
2006-09-05 04:11:11 +00:00
return -1;
}
fs_rex_free = fs_rex;
}
2006-08-18 17:46:07 +00:00
}
2006-10-22 11:34:53 +00:00
t1 = sse_awk_makemapval (run);
if (t1 == SSE_NULL)
2006-08-18 17:46:07 +00:00
{
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
if (fs_free != SSE_NULL)
SSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != SSE_NULL)
sse_awk_freerex (run->awk, fs_rex_free);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-18 17:46:07 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, *a1_ref);
2006-09-13 14:16:35 +00:00
*a1_ref = t1;
2006-10-22 11:34:53 +00:00
sse_awk_refupval (*a1_ref);
2006-08-19 16:34:24 +00:00
2006-10-16 08:48:19 +00:00
p = str; str_left = str_len;
2006-10-22 11:34:53 +00:00
sta = (sse_awk_getopt(run->awk) & SSE_AWK_STRINDEXONE)? 1: 0;
2006-10-16 08:48:19 +00:00
num = sta;
2006-10-22 11:34:53 +00:00
while (p != SSE_NULL)
2006-08-18 17:46:07 +00:00
{
2006-09-03 15:47:11 +00:00
if (fs_len <= 1)
{
2006-10-22 11:34:53 +00:00
p = sse_awk_strxntok (run,
2006-09-03 15:47:11 +00:00
p, str_len, fs_ptr, fs_len, &tok, &tok_len);
}
else
{
2006-10-22 11:34:53 +00:00
p = sse_awk_strxntokbyrex (run, p, str_len,
2006-09-05 04:11:11 +00:00
fs_rex, &tok, &tok_len, &errnum);
2006-10-22 11:34:53 +00:00
if (p == SSE_NULL && errnum != SSE_AWK_ENOERR)
2006-09-05 04:11:11 +00:00
{
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
if (fs_free != SSE_NULL)
SSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != SSE_NULL)
sse_awk_freerex (run->awk, fs_rex_free);
sse_awk_setrunerrnum (run, errnum);
2006-09-05 04:11:11 +00:00
return -1;
}
2006-09-03 15:47:11 +00:00
}
2006-08-18 17:46:07 +00:00
2006-10-22 11:34:53 +00:00
if (num == 0 && p == SSE_NULL && tok_len == 0)
2006-08-18 17:46:07 +00:00
{
/* no field at all*/
break;
}
2006-10-22 11:34:53 +00:00
sse_awk_assert (run->awk,
(tok != SSE_NULL && tok_len > 0) || tok_len == 0);
2006-08-18 17:46:07 +00:00
/* create the field string */
2006-10-22 11:34:53 +00:00
t2 = sse_awk_makestrval (run, tok, tok_len);
if (t2 == SSE_NULL)
2006-08-18 17:46:07 +00:00
{
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
if (fs_free != SSE_NULL)
SSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != SSE_NULL)
sse_awk_freerex (run->awk, fs_rex_free);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-18 17:46:07 +00:00
return -1;
}
/* put it into the map */
2006-10-22 11:34:53 +00:00
key_len = sse_awk_longtostr (
num, 10, SSE_NULL, key, sse_countof(key));
sse_awk_assert (run->awk, key_len != (sse_size_t)-1);
2006-08-18 17:46:07 +00:00
2006-10-11 03:19:08 +00:00
/* don't forget to update the reference count when you
* handle the assignment-like situation. anyway, it is
* incremented in advance as if the assignment was successful.
* it is decremented if the assignement fails. */
2006-10-22 11:34:53 +00:00
sse_awk_refupval (t2);
2006-10-11 03:19:08 +00:00
2006-10-22 11:34:53 +00:00
if (sse_awk_map_putx (
((sse_awk_val_map_t*)t1)->map,
key, key_len, t2, SSE_NULL) == -1)
2006-08-18 17:46:07 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, t2);
if (str_free != SSE_NULL)
SSE_AWK_FREE (run->awk, str_free);
if (fs_free != SSE_NULL)
SSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != SSE_NULL)
sse_awk_freerex (run->awk, fs_rex_free);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-18 17:46:07 +00:00
return -1;
}
num++;
2006-09-05 04:11:11 +00:00
str_len = str_left - (p - str);
2006-08-18 17:46:07 +00:00
}
2006-10-22 11:34:53 +00:00
if (str_free != SSE_NULL) SSE_AWK_FREE (run->awk, str_free);
if (fs_free != SSE_NULL) SSE_AWK_FREE (run->awk, fs_free);
if (fs_rex_free != SSE_NULL) sse_awk_freerex (run->awk, fs_rex_free);
2006-08-18 17:46:07 +00:00
2006-10-16 08:48:19 +00:00
if (sta == 1) num--;
2006-10-22 11:34:53 +00:00
t1 = sse_awk_makeintval (run, num);
if (t1 == SSE_NULL)
2006-08-18 17:46:07 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-18 17:46:07 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, t1);
2006-08-18 17:46:07 +00:00
return 0;
}
2006-08-13 16:05:04 +00:00
2006-10-22 11:34:53 +00:00
static int __bfn_tolower (sse_awk_run_t* run)
2006-08-17 03:49:30 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_char_t* str;
sse_size_t len, i;
sse_awk_val_t* a0, * r;
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 1);
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-08-17 03:49:30 +00:00
{
2006-10-22 11:34:53 +00:00
str = ((sse_awk_val_str_t*)a0)->buf;
len = ((sse_awk_val_str_t*)a0)->len;
2006-08-17 03:49:30 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len);
if (str == SSE_NULL) return -1;
2006-08-17 03:49:30 +00:00
}
2006-10-22 11:34:53 +00:00
for (i = 0; i < len; i++) str[i] = SSE_AWK_TOLOWER (run->awk, str[i]);
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
r = sse_awk_makestrval (run, str, len);
if (r == SSE_NULL)
2006-08-17 03:49:30 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-17 03:49:30 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
sse_awk_setretval (run, r);
2006-08-17 03:49:30 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_toupper (sse_awk_run_t* run)
2006-08-17 03:49:30 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_char_t* str;
sse_size_t len, i;
sse_awk_val_t* a0, * r;
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 1);
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-08-17 03:49:30 +00:00
{
2006-10-22 11:34:53 +00:00
str = ((sse_awk_val_str_t*)a0)->buf;
len = ((sse_awk_val_str_t*)a0)->len;
2006-08-17 03:49:30 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len);
if (str == SSE_NULL) return -1;
2006-08-17 03:49:30 +00:00
}
2006-10-22 11:34:53 +00:00
for (i = 0; i < len; i++) str[i] = SSE_AWK_TOUPPER (run->awk, str[i]);
2006-08-17 03:49:30 +00:00
2006-10-22 11:34:53 +00:00
r = sse_awk_makestrval (run, str, len);
if (r == SSE_NULL)
2006-08-17 03:49:30 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-08-17 03:49:30 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str);
sse_awk_setretval (run, r);
2006-08-17 03:49:30 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __substitute (sse_awk_run_t* run, sse_long_t max_count)
2006-09-14 06:40:06 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* a0, * a1, * a2, ** a2_ref, * v;
sse_char_t* a0_ptr, * a1_ptr, * a2_ptr;
sse_size_t a0_len, a1_len, a2_len;
sse_char_t* a0_ptr_free = SSE_NULL;
sse_char_t* a1_ptr_free = SSE_NULL;
sse_char_t* a2_ptr_free = SSE_NULL;
2006-09-12 15:20:18 +00:00
void* rex;
int opt, n;
2006-10-22 11:34:53 +00:00
const sse_char_t* cur_ptr, * mat_ptr;
sse_size_t cur_len, mat_len, i, m;
sse_awk_str_t new;
sse_long_t sub_count;
2006-09-11 14:29:23 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs >= 2 && nargs <= 3);
2006-09-11 14:29:23 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
a1 = sse_awk_getarg (run, 1);
a2 = (nargs >= 3)? sse_awk_getarg (run, 2): SSE_NULL;
2006-09-11 14:29:23 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_assert (run->awk, a2 == SSE_NULL || a2->type == SSE_AWK_VAL_REF);
2006-09-11 14:29:23 +00:00
2006-09-14 06:40:06 +00:00
#define FREE_A_PTRS(awk) \
do { \
2006-10-22 11:34:53 +00:00
if (a2_ptr_free != SSE_NULL) SSE_AWK_FREE (awk, a2_ptr_free); \
if (a1_ptr_free != SSE_NULL) SSE_AWK_FREE (awk, a1_ptr_free); \
if (a0_ptr_free != SSE_NULL) SSE_AWK_FREE (awk, a0_ptr_free); \
2006-09-14 06:40:06 +00:00
} while (0)
#define FREE_A0_REX(awk,rex) \
do { \
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_REX) sse_awk_freerex (awk, rex); \
2006-09-14 06:40:06 +00:00
} while (0)
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_REX)
2006-09-13 14:16:35 +00:00
{
2006-10-22 11:34:53 +00:00
rex = ((sse_awk_val_rex_t*)a0)->code;
2006-09-13 14:16:35 +00:00
}
2006-10-22 11:34:53 +00:00
else if (a0->type == SSE_AWK_VAL_STR)
2006-09-11 14:29:23 +00:00
{
2006-10-22 11:34:53 +00:00
a0_ptr = ((sse_awk_val_str_t*)a0)->buf;
a0_len = ((sse_awk_val_str_t*)a0)->len;
2006-09-11 14:29:23 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
a0_ptr = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &a0_len);
if (a0_ptr == SSE_NULL)
2006-09-14 06:40:06 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-14 06:40:06 +00:00
return -1;
}
2006-09-11 14:29:23 +00:00
a0_ptr_free = a0_ptr;
}
2006-10-22 11:34:53 +00:00
if (a1->type == SSE_AWK_VAL_STR)
2006-09-11 14:29:23 +00:00
{
2006-10-22 11:34:53 +00:00
a1_ptr = ((sse_awk_val_str_t*)a1)->buf;
a1_len = ((sse_awk_val_str_t*)a1)->len;
2006-09-11 14:29:23 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
a1_ptr = sse_awk_valtostr (
run, a1, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &a1_len);
if (a1_ptr == SSE_NULL)
2006-09-11 14:29:23 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-11 14:29:23 +00:00
return -1;
}
a1_ptr_free = a1_ptr;
}
2006-10-22 11:34:53 +00:00
if (a2 == SSE_NULL)
2006-09-11 14:29:23 +00:00
{
2006-09-14 06:40:06 +00:00
/* is this correct? any needs to use inrec.d0? */
2006-10-22 11:34:53 +00:00
a2_ptr = SSE_AWK_STR_BUF(&run->inrec.line);
a2_len = SSE_AWK_STR_LEN(&run->inrec.line);
2006-10-02 14:54:32 +00:00
}
2006-10-22 11:34:53 +00:00
else if (((sse_awk_val_ref_t*)a2)->id == SSE_AWK_VAL_REF_POS)
2006-10-02 14:54:32 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t idx;
2006-10-02 14:54:32 +00:00
2006-10-22 11:34:53 +00:00
idx = (sse_size_t)((sse_awk_val_ref_t*)a2)->adr;
2006-10-02 14:54:32 +00:00
if (idx == 0)
{
2006-10-22 11:34:53 +00:00
a2_ptr = SSE_AWK_STR_BUF(&run->inrec.line);
a2_len = SSE_AWK_STR_LEN(&run->inrec.line);
2006-10-02 14:54:32 +00:00
}
2006-10-10 07:02:38 +00:00
else if (idx <= run->inrec.nflds)
2006-10-02 14:54:32 +00:00
{
2006-10-10 07:02:38 +00:00
a2_ptr = run->inrec.flds[idx-1].ptr;
a2_len = run->inrec.flds[idx-1].len;
2006-10-02 14:54:32 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
a2_ptr = SSE_T("");
2006-10-02 14:54:32 +00:00
a2_len = 0;
}
2006-09-11 14:29:23 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
a2_ref = (sse_awk_val_t**)((sse_awk_val_ref_t*)a2)->adr;
2006-10-02 14:54:32 +00:00
2006-10-22 11:34:53 +00:00
if ((*a2_ref)->type == SSE_AWK_VAL_MAP)
2006-09-12 15:20:18 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-14 06:40:06 +00:00
/* a map is not allowed as the third parameter */
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_EMAPNOTALLOWED);
2006-09-14 06:40:06 +00:00
return -1;
2006-09-12 15:20:18 +00:00
}
2006-09-14 06:40:06 +00:00
2006-10-22 11:34:53 +00:00
if ((*a2_ref)->type == SSE_AWK_VAL_STR)
2006-09-13 14:16:35 +00:00
{
2006-10-22 11:34:53 +00:00
a2_ptr = ((sse_awk_val_str_t*)(*a2_ref))->buf;
a2_len = ((sse_awk_val_str_t*)(*a2_ref))->len;
2006-09-13 14:16:35 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
a2_ptr = sse_awk_valtostr (
run, *a2_ref, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &a2_len);
if (a2_ptr == SSE_NULL)
2006-09-13 14:16:35 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-13 14:16:35 +00:00
return -1;
}
a2_ptr_free = a2_ptr;
}
2006-09-12 15:20:18 +00:00
}
2006-10-22 11:34:53 +00:00
if (sse_awk_str_open (&new, a2_len, run->awk) == SSE_NULL)
2006-09-14 06:40:06 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-09-14 06:40:06 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_REX)
2006-09-12 15:20:18 +00:00
{
2006-10-22 11:34:53 +00:00
rex = sse_awk_buildrex (run->awk, a0_ptr, a0_len, &run->errnum);
if (rex == SSE_NULL)
2006-09-13 14:16:35 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-13 14:16:35 +00:00
return -1;
}
2006-09-12 15:20:18 +00:00
}
2006-10-22 11:34:53 +00:00
opt = (run->global.ignorecase)? SSE_AWK_REX_IGNORECASE: 0;
2006-09-12 15:20:18 +00:00
cur_ptr = a2_ptr;
cur_len = a2_len;
2006-09-13 14:16:35 +00:00
sub_count = 0;
2006-09-12 15:20:18 +00:00
while (1)
{
2006-09-14 06:40:06 +00:00
if (max_count == 0 || sub_count < max_count)
{
2006-10-22 11:34:53 +00:00
n = sse_awk_matchrex (
2006-10-10 07:02:38 +00:00
run->awk, rex, opt, cur_ptr, cur_len,
&mat_ptr, &mat_len, &run->errnum);
2006-09-14 06:40:06 +00:00
}
else n = 0;
2006-09-12 15:20:18 +00:00
if (n == -1)
{
2006-10-10 07:02:38 +00:00
FREE_A0_REX (run->awk, rex);
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-12 15:20:18 +00:00
return -1;
}
if (n == 0)
{
/* no more match found */
2006-10-22 11:34:53 +00:00
if (sse_awk_str_ncat (
&new, cur_ptr, cur_len) == (sse_size_t)-1)
2006-09-12 15:20:18 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A0_REX (run->awk, rex);
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-12 15:20:18 +00:00
return -1;
}
break;
}
2006-10-22 11:34:53 +00:00
if (sse_awk_str_ncat (
&new, cur_ptr, mat_ptr - cur_ptr) == (sse_size_t)-1)
2006-09-12 15:20:18 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A0_REX (run->awk, rex);
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-12 15:20:18 +00:00
return -1;
}
2006-09-14 06:40:06 +00:00
2006-09-16 12:58:38 +00:00
for (i = 0; i < a1_len; i++)
2006-09-12 15:20:18 +00:00
{
2006-09-16 12:58:38 +00:00
if ((i+1) < a1_len &&
2006-10-22 11:34:53 +00:00
a1_ptr[i] == SSE_T('\\') &&
a1_ptr[i+1] == SSE_T('&'))
2006-09-16 12:58:38 +00:00
{
2006-10-22 11:34:53 +00:00
m = sse_awk_str_ccat (&new, SSE_T('&'));
2006-09-16 12:58:38 +00:00
i++;
}
2006-10-22 11:34:53 +00:00
else if (a1_ptr[i] == SSE_T('&'))
2006-09-16 12:58:38 +00:00
{
2006-10-22 11:34:53 +00:00
m = sse_awk_str_ncat (&new, mat_ptr, mat_len);
2006-09-16 12:58:38 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
m = sse_awk_str_ccat (&new, a1_ptr[i]);
2006-09-16 12:58:38 +00:00
}
2006-10-22 11:34:53 +00:00
if (m == (sse_size_t)-1)
2006-09-16 12:58:38 +00:00
{
2006-10-10 07:02:38 +00:00
FREE_A0_REX (run->awk, rex);
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-16 12:58:38 +00:00
return -1;
}
2006-09-12 15:20:18 +00:00
}
2006-09-13 14:16:35 +00:00
sub_count++;
2006-09-16 12:58:38 +00:00
cur_len = cur_len - ((mat_ptr - cur_ptr) + mat_len);
2006-09-30 17:03:11 +00:00
cur_ptr = mat_ptr + mat_len;
2006-09-12 15:20:18 +00:00
}
2006-10-10 07:02:38 +00:00
FREE_A0_REX (run->awk, rex);
2006-09-12 15:20:18 +00:00
2006-10-02 14:54:32 +00:00
if (sub_count > 0)
2006-09-12 15:20:18 +00:00
{
2006-10-22 11:34:53 +00:00
if (a2 == SSE_NULL)
2006-09-12 15:20:18 +00:00
{
2006-10-22 11:34:53 +00:00
if (sse_awk_setrec (run, 0,
SSE_AWK_STR_BUF(&new), SSE_AWK_STR_LEN(&new)) == -1)
2006-10-02 14:54:32 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-10-02 14:54:32 +00:00
return -1;
}
2006-09-12 15:20:18 +00:00
}
2006-10-22 11:34:53 +00:00
else if (((sse_awk_val_ref_t*)a2)->id == SSE_AWK_VAL_REF_POS)
2006-09-13 14:16:35 +00:00
{
2006-10-02 14:54:32 +00:00
int n;
2006-10-22 11:34:53 +00:00
n = sse_awk_setrec (
run, (sse_size_t)((sse_awk_val_ref_t*)a2)->adr,
SSE_AWK_STR_BUF(&new), SSE_AWK_STR_LEN(&new));
2006-10-02 14:54:32 +00:00
if (n == -1)
{
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-10-02 14:54:32 +00:00
return -1;
}
2006-09-13 14:16:35 +00:00
}
2006-10-02 14:54:32 +00:00
else
{
2006-10-22 11:34:53 +00:00
v = sse_awk_makestrval (run,
SSE_AWK_STR_BUF(&new), SSE_AWK_STR_LEN(&new));
if (v == SSE_NULL)
2006-10-02 14:54:32 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-10-02 14:54:32 +00:00
return -1;
}
2006-09-13 14:16:35 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, *a2_ref);
2006-10-02 14:54:32 +00:00
*a2_ref = v;
2006-10-22 11:34:53 +00:00
sse_awk_refupval (*a2_ref);
2006-10-02 14:54:32 +00:00
}
2006-09-11 14:29:23 +00:00
}
2006-10-22 11:34:53 +00:00
sse_awk_str_close (&new);
2006-10-10 07:02:38 +00:00
FREE_A_PTRS (run->awk);
2006-09-11 14:29:23 +00:00
2006-09-14 06:40:06 +00:00
#undef FREE_A0_REX
#undef FREE_A_PTRS
2006-09-11 14:29:23 +00:00
2006-10-22 11:34:53 +00:00
v = sse_awk_makeintval (run, sub_count);
if (v == SSE_NULL)
2006-09-13 14:16:35 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-09-13 14:16:35 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, v);
2006-09-11 14:29:23 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_gsub (sse_awk_run_t* run)
2006-10-10 07:02:38 +00:00
{
return __substitute (run, 0);
}
2006-10-22 11:34:53 +00:00
static int __bfn_sub (sse_awk_run_t* run)
2006-10-10 07:02:38 +00:00
{
return __substitute (run, 1);
}
2006-10-22 11:34:53 +00:00
static int __bfn_match (sse_awk_run_t* run)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* a0, * a1;
sse_char_t* str0, * str1;
sse_size_t len0, len1;
sse_long_t idx;
2006-10-18 14:06:47 +00:00
void* rex;
int opt, n;
2006-10-22 11:34:53 +00:00
const sse_char_t* mat_ptr;
sse_size_t mat_len;
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 2);
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_getarg (run, 0);
a1 = sse_awk_getarg (run, 1);
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
if (a0->type == SSE_AWK_VAL_STR)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
str0 = ((sse_awk_val_str_t*)a0)->buf;
len0 = ((sse_awk_val_str_t*)a0)->len;
2006-10-18 14:06:47 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str0 = sse_awk_valtostr (
run, a0, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len0);
if (str0 == SSE_NULL) return -1;
2006-10-18 14:06:47 +00:00
}
2006-10-22 11:34:53 +00:00
if (a1->type == SSE_AWK_VAL_REX)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
rex = ((sse_awk_val_rex_t*)a1)->code;
2006-10-18 14:06:47 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
if (a1->type == SSE_AWK_VAL_STR)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
str1 = ((sse_awk_val_str_t*)a1)->buf;
len1 = ((sse_awk_val_str_t*)a1)->len;
2006-10-18 14:06:47 +00:00
}
else
{
2006-10-22 11:34:53 +00:00
str1 = sse_awk_valtostr (
run, a1, SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, &len1);
if (str1 == SSE_NULL)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->awk, str0);
2006-10-18 14:06:47 +00:00
return -1;
}
}
2006-10-22 11:34:53 +00:00
rex = sse_awk_buildrex (run->awk, str1, len1, &run->errnum);
if (rex == SSE_NULL)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR)
SSE_AWK_FREE (run->awk, str0);
2006-10-18 14:06:47 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (a1->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str1);
2006-10-18 14:06:47 +00:00
}
2006-10-22 11:34:53 +00:00
opt = (run->global.ignorecase)? SSE_AWK_REX_IGNORECASE: 0;
n = sse_awk_matchrex (
2006-10-18 14:06:47 +00:00
run->awk, rex, opt, str0, len0,
&mat_ptr, &mat_len, &run->errnum);
2006-10-22 11:34:53 +00:00
if (a0->type != SSE_AWK_VAL_STR) SSE_AWK_FREE (run->awk, str0);
if (a1->type != SSE_AWK_VAL_REX) sse_awk_freerex (run->awk, rex);
2006-10-18 14:06:47 +00:00
if (n == -1) return -1;
2006-10-22 11:34:53 +00:00
idx = (n == 0)? -1: (sse_long_t)(mat_ptr - str0);
if (sse_awk_getopt(run->awk) & SSE_AWK_STRINDEXONE) idx = idx + 1;
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
a0 = sse_awk_makeintval (run, idx);
if (a0 == SSE_NULL)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-10-18 14:06:47 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_refupval (a0);
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
a1 = sse_awk_makeintval (run,
((n == 0)? (sse_long_t)-1: (sse_long_t)mat_len));
if (a1 == SSE_NULL)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, a0);
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-10-18 14:06:47 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_refupval (a1);
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
if (sse_awk_setglobal (run, SSE_AWK_GLOBAL_RSTART, a0) == -1)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, a1);
sse_awk_refdownval (run, a0);
2006-10-18 14:06:47 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
if (sse_awk_setglobal (run, SSE_AWK_GLOBAL_RLENGTH, a1) == -1)
2006-10-18 14:06:47 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, a1);
sse_awk_refdownval (run, a0);
2006-10-18 14:06:47 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, a0);
2006-10-18 14:06:47 +00:00
2006-10-22 11:34:53 +00:00
sse_awk_refdownval (run, a1);
sse_awk_refdownval (run, a0);
2006-10-18 14:06:47 +00:00
return 0;
}
2006-10-22 11:34:53 +00:00
static int __bfn_system (sse_awk_run_t* run)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_char_t* cmd;
sse_awk_val_t* v;
2006-08-31 15:39:14 +00:00
int n;
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 1);
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
cmd = sse_awk_valtostr (
run, sse_awk_getarg(run, 0),
SSE_AWK_VALTOSTR_CLEAR, SSE_NULL, SSE_NULL);
if (cmd == SSE_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-10-22 11:34:53 +00:00
SSE_AWK_FREE (run->awk, cmd);
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
v = sse_awk_makeintval (run, n);
if (v == SSE_NULL)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-07-17 04:17:40 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, v);
2006-07-17 04:17:40 +00:00
return 0;
}
/* math functions */
2006-10-04 10:11:04 +00:00
#if 0
2006-10-22 11:34:53 +00:00
static int __bfn_sin (sse_awk_run_t* run)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
sse_size_t nargs;
sse_awk_val_t* v;
2006-07-17 04:17:40 +00:00
int n;
2006-10-22 11:34:53 +00:00
sse_long_t lv;
sse_real_t rv;
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
nargs = sse_awk_getnargs (run);
sse_awk_assert (run->awk, nargs == 1);
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
n = sse_awk_valtonum (run, sse_awk_getarg(run, 0), &lv, &rv);
2006-07-17 04:17:40 +00:00
if (n == -1)
{
/* wrong value */
return -1;
}
2006-10-22 11:34:53 +00:00
if (n == 0) rv = (sse_real_t)lv;
2006-07-17 04:17:40 +00:00
2006-10-22 11:34:53 +00:00
#if (SSE_SIZEOF_REAL == SSE_SIZEOF_LONG_DOUBLE)
v = sse_awk_makerealval (run, (sse_real_t)sinl(rv));
#elif (SSE_SIZEOF_REAL == SSE_SIZEOF_DOUBLE)
v = sse_awk_makerealval (run, (sse_real_t)sin(rv));
2006-07-17 14:27:09 +00:00
#else
#error unsupported floating-point data type
#endif
2006-10-04 10:11:04 +00:00
2006-10-22 11:34:53 +00:00
if (v == SSE_NULL)
2006-07-17 04:17:40 +00:00
{
2006-10-22 11:34:53 +00:00
sse_awk_setrunerrnum (run, SSE_AWK_ENOMEM);
2006-07-17 04:17:40 +00:00
return -1;
}
2006-10-22 11:34:53 +00:00
sse_awk_setretval (run, v);
2006-07-17 04:17:40 +00:00
return 0;
}
2006-10-04 10:11:04 +00:00
#endif