2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2011-09-01 09:43:46 +00:00
|
|
|
* $Id: fnc.c 556 2011-08-31 15:43:46Z hyunghwan.chung $
|
2007-05-02 01:07:00 +00:00
|
|
|
*
|
2011-04-23 08:28:43 +00:00
|
|
|
Copyright 2006-2011 Chung, Hyung-Hwan.
|
2009-09-16 04:01:02 +00:00
|
|
|
This file is part of QSE.
|
2008-12-27 04:35:14 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
QSE is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of
|
|
|
|
the License, or (at your option) any later version.
|
2008-12-27 04:35:14 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
QSE is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
2008-12-27 04:35:14 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2008-08-21 03:17:25 +00:00
|
|
|
#include "awk.h"
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-18 08:37:51 +00:00
|
|
|
static int fnc_close (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_fflush (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_index (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_length (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_substr (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_split (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_tolower (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_toupper (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_gsub (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_sub (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_match (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_sprintf (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
|
|
|
|
static int fnc_sin (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_cos (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_tan (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_atan (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_atan2 (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_log (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_exp (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
|
|
|
static int fnc_sqrt (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
2011-05-18 20:32:39 +00:00
|
|
|
static int fnc_int (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#undef MAX
|
2008-12-21 21:35:07 +00:00
|
|
|
#define MAX QSE_TYPE_UNSIGNED_MAX(qse_size_t)
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-10-20 07:33:40 +00:00
|
|
|
/* Argument Specifier
|
|
|
|
*
|
|
|
|
* Each character in the specifier indicates how a parameter
|
|
|
|
* of the corresponding postion should be passed to a function.
|
|
|
|
*
|
|
|
|
* - v: value. pass it after normal evaluation.
|
|
|
|
* - r: pass a variable by reference
|
|
|
|
* - x: regular expression as it it. not evaluated as /rex/ ~ $0.
|
|
|
|
*
|
|
|
|
* If the first character of the specifer is 'R', all
|
|
|
|
* parameters are passed by reference regarless of the remaining
|
|
|
|
* chracters.
|
|
|
|
*/
|
2009-02-01 03:59:46 +00:00
|
|
|
static qse_awk_fnc_t sys_fnc[] =
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* io functions */
|
2009-08-27 06:59:08 +00:00
|
|
|
{ {QSE_T("close"), 5}, 0, QSE_AWK_RIO, {1, 2, QSE_NULL}, fnc_close},
|
2009-06-15 02:40:52 +00:00
|
|
|
{ {QSE_T("fflush"), 6}, 0, QSE_AWK_RIO, {0, 1, QSE_NULL}, fnc_fflush},
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
/* string functions */
|
2009-09-16 08:03:15 +00:00
|
|
|
{ {QSE_T("index"), 5}, 0, 0, {2, 3, QSE_NULL}, fnc_index},
|
2009-06-15 02:40:52 +00:00
|
|
|
{ {QSE_T("substr"), 6}, 0, 0, {2, 3, QSE_NULL}, fnc_substr},
|
|
|
|
{ {QSE_T("length"), 6}, 1, 0, {0, 1, QSE_NULL}, fnc_length},
|
2009-10-20 07:33:40 +00:00
|
|
|
{ {QSE_T("split"), 5}, 0, 0, {2, 3, QSE_T("vrx")}, fnc_split},
|
2009-06-15 02:40:52 +00:00
|
|
|
{ {QSE_T("tolower"), 7}, 0, 0, {1, 1, QSE_NULL}, fnc_tolower},
|
|
|
|
{ {QSE_T("toupper"), 7}, 0, 0, {1, 1, QSE_NULL}, fnc_toupper},
|
|
|
|
{ {QSE_T("gsub"), 4}, 0, 0, {2, 3, QSE_T("xvr")}, fnc_gsub},
|
|
|
|
{ {QSE_T("sub"), 3}, 0, 0, {2, 3, QSE_T("xvr")}, fnc_sub},
|
2009-09-16 08:03:15 +00:00
|
|
|
{ {QSE_T("match"), 5}, 0, 0, {2, 3, QSE_T("vxv")}, fnc_match},
|
2009-06-15 02:40:52 +00:00
|
|
|
{ {QSE_T("sprintf"), 7}, 0, 0, {1, MAX, QSE_NULL}, fnc_sprintf},
|
|
|
|
|
2011-05-18 08:37:51 +00:00
|
|
|
/* math functions */
|
|
|
|
{ {QSE_T("sin"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_sin},
|
|
|
|
{ {QSE_T("cos"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_cos},
|
|
|
|
{ {QSE_T("tan"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_tan},
|
|
|
|
{ {QSE_T("atan"), 4}, 0, 0, {1, 1, QSE_NULL}, fnc_atan},
|
|
|
|
{ {QSE_T("atan2"), 5}, 0, 0, {2, 2, QSE_NULL}, fnc_atan2},
|
|
|
|
{ {QSE_T("log"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_log},
|
|
|
|
{ {QSE_T("exp"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_exp},
|
|
|
|
{ {QSE_T("sqrt"), 4}, 0, 0, {1, 1, QSE_NULL}, fnc_sqrt},
|
2011-05-18 20:32:39 +00:00
|
|
|
{ {QSE_T("int"), 3}, 0, 0, {1, 1, QSE_NULL}, fnc_int},
|
2011-05-18 08:37:51 +00:00
|
|
|
|
2009-06-15 02:40:52 +00:00
|
|
|
{ {QSE_NULL, 0}, 0, 0, {0, 0, QSE_NULL}, QSE_NULL}
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
void* qse_awk_addfnc (
|
2009-06-25 03:50:10 +00:00
|
|
|
qse_awk_t* awk,
|
|
|
|
const qse_char_t* name, qse_size_t name_len,
|
|
|
|
int when_valid,
|
|
|
|
qse_size_t min_args, qse_size_t max_args,
|
2008-12-21 21:35:07 +00:00
|
|
|
const qse_char_t* arg_spec,
|
2009-06-25 03:50:10 +00:00
|
|
|
qse_awk_fnc_fun_t handler)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_fnc_t* fnc;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t spec_len;
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2007-09-27 20:33:00 +00:00
|
|
|
if (name_len <= 0)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_EINVAL, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-09-27 20:33:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
if (qse_awk_getfnc (awk, name, name_len) != QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_cstr_t errarg;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
errarg.ptr = name;
|
|
|
|
errarg.len = name_len;
|
|
|
|
|
2009-08-17 07:44:20 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_EEXIST, &errarg);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
spec_len = (arg_spec == QSE_NULL)? 0: qse_strlen(arg_spec);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
fnc = (qse_awk_fnc_t*) QSE_AWK_ALLOC (awk,
|
|
|
|
QSE_SIZEOF(qse_awk_fnc_t) +
|
2008-12-21 21:35:07 +00:00
|
|
|
(name_len+1) * QSE_SIZEOF(qse_char_t) +
|
|
|
|
(spec_len+1) * QSE_SIZEOF(qse_char_t));
|
2009-02-01 03:59:46 +00:00
|
|
|
if (fnc == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
fnc->name.ptr = (qse_char_t*)(fnc + 1);
|
|
|
|
fnc->name.len = name_len;
|
|
|
|
qse_strxncpy (fnc->name.ptr, name_len+1, name, name_len);
|
2007-10-25 23:43:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
fnc->valid = when_valid;
|
|
|
|
fnc->arg.min = min_args;
|
|
|
|
fnc->arg.max = max_args;
|
2007-10-25 23:43:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
if (arg_spec == QSE_NULL) fnc->arg.spec = QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
else
|
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
fnc->arg.spec = fnc->name.ptr + fnc->name.len + 1;
|
|
|
|
qse_strxcpy (fnc->arg.spec, spec_len+1, arg_spec);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
fnc->handler = handler;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
if (qse_htb_insert (awk->fnc.user,
|
2009-02-01 03:59:46 +00:00
|
|
|
(qse_char_t*)name, name_len, fnc, 0) == QSE_NULL)
|
2007-10-25 23:43:00 +00:00
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
QSE_AWK_FREE (awk, fnc);
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-10-25 23:43:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
return fnc;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
int qse_awk_delfnc (
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_t* awk, const qse_char_t* name, qse_size_t name_len)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
if (qse_htb_delete (awk->fnc.user, name, name_len) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_cstr_t errarg;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2007-10-25 23:43:00 +00:00
|
|
|
errarg.ptr = name;
|
|
|
|
errarg.len = name_len;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-17 07:44:20 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_ENOENT, &errarg);
|
2007-10-25 23:43:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2007-10-25 23:43:00 +00:00
|
|
|
return 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
void qse_awk_clrfnc (qse_awk_t* awk)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_clear (awk->fnc.user);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_fnc_t* qse_awk_getfnc (
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_t* awk, const qse_char_t* name, qse_size_t len)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_fnc_t* fnc;
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_pair_t* pair;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2007-10-25 23:43:00 +00:00
|
|
|
/* search the system function table */
|
2011-05-18 08:37:51 +00:00
|
|
|
/* TODO: some speed up? binary search by ordering the table? */
|
2009-02-01 03:59:46 +00:00
|
|
|
for (fnc = sys_fnc; fnc->name.ptr != QSE_NULL; fnc++)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
if (fnc->valid != 0 &&
|
|
|
|
(awk->option & fnc->valid) != fnc->valid) continue;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-24 10:52:37 +00:00
|
|
|
if (qse_strxncmp (
|
|
|
|
fnc->name.ptr, fnc->name.len,
|
|
|
|
name, len) == 0) return fnc;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-24 10:52:37 +00:00
|
|
|
pair = qse_htb_search (awk->fnc.user, name, len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (pair == QSE_NULL) return QSE_NULL;
|
2007-06-19 13:14:00 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
fnc = (qse_awk_fnc_t*)QSE_HTB_VPTR(pair);
|
2009-02-01 03:59:46 +00:00
|
|
|
if (fnc->valid != 0 && (awk->option & fnc->valid) == 0) return QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
return fnc;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
static int fnc_close (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
2009-08-27 06:59:08 +00:00
|
|
|
qse_awk_val_t* v, * a0, * a1 = QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
int n;
|
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
qse_char_t* name, * opt = QSE_NULL;
|
|
|
|
qse_size_t len, optlen = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
QSE_ASSERT (nargs == 1 || nargs == 2);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
2009-08-29 06:47:09 +00:00
|
|
|
if (nargs >= 2) a1 = qse_awk_rtx_getarg (rtx, 1);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (a0 != QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
name = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-27 06:59:08 +00:00
|
|
|
name = qse_awk_rtx_valtocpldup (rtx, a0, &len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (name == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
if (a1 != QSE_NULL)
|
|
|
|
{
|
|
|
|
if (a1->type == QSE_AWK_VAL_STR)
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
opt = ((qse_awk_val_str_t*)a1)->val.ptr;
|
|
|
|
optlen = ((qse_awk_val_str_t*)a1)->val.len;
|
2009-08-27 06:59:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
opt = qse_awk_rtx_valtocpldup (rtx, a1, &optlen);
|
|
|
|
if (opt == QSE_NULL)
|
|
|
|
{
|
|
|
|
if (a1->type != QSE_AWK_VAL_STR)
|
|
|
|
QSE_AWK_FREE (rtx->awk, name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +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
|
2009-02-16 08:31:34 +00:00
|
|
|
* an empty string for its identification because closeio
|
|
|
|
* closes any ios that match the name given unlike
|
|
|
|
* closeio_read or closeio_write. */
|
2007-05-02 01:07:00 +00:00
|
|
|
n = -1;
|
|
|
|
goto skip_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (name[--len] == QSE_T('\0'))
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-27 06:59:08 +00:00
|
|
|
/* the name contains a null charater.
|
2007-05-02 01:07:00 +00:00
|
|
|
* make close return -1 */
|
|
|
|
n = -1;
|
|
|
|
goto skip_close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
if (opt != QSE_NULL)
|
|
|
|
{
|
|
|
|
if (optlen != 1 ||
|
|
|
|
(opt[0] != QSE_T('r') && opt[0] != QSE_T('w')))
|
|
|
|
{
|
|
|
|
n = -1;
|
|
|
|
goto skip_close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = qse_awk_rtx_closeio (rtx, name, opt);
|
|
|
|
/* failure to close is not a critical error. instead, that is
|
2009-08-28 06:52:20 +00:00
|
|
|
* flagged by the return value of close().
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n <= -1 && rtx->errinf.num != QSE_AWK_EIONMNF)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR)
|
2009-08-27 06:59:08 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, name);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2008-12-30 04:49:25 +00:00
|
|
|
skip_close:
|
2009-08-27 06:59:08 +00:00
|
|
|
if (a1 != QSE_NULL && a1->type != QSE_AWK_VAL_STR)
|
|
|
|
QSE_AWK_FREE (rtx->awk, opt);
|
|
|
|
|
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, name);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
v = qse_awk_rtx_makeintval (rtx, (qse_long_t)n);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (v == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-08-27 06:59:08 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, v);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
static int flush_io (
|
|
|
|
qse_awk_rtx_t* run, int rio, const qse_char_t* name, int n)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
int n2;
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
if (run->rio.handler[rio] != QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-16 08:31:34 +00:00
|
|
|
n2 = qse_awk_rtx_flushio (run, rio, name);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n2 <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/*
|
2009-06-15 02:40:52 +00:00
|
|
|
if (run->errinf.num == QSE_AWK_EIOIMPL) n = -1;
|
2009-06-27 07:05:19 +00:00
|
|
|
else if (run->errinf.num == QSE_AWK_EIONMNF)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
if (n != 0) n = -2;
|
|
|
|
}
|
|
|
|
else n = -99;
|
|
|
|
*/
|
2009-06-27 07:05:19 +00:00
|
|
|
if (run->errinf.num == QSE_AWK_EIONMNF)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
if (n != 0) n = -2;
|
|
|
|
}
|
|
|
|
else n = -1;
|
|
|
|
}
|
|
|
|
else if (n != -1) n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_fflush (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0;
|
|
|
|
qse_char_t* str0;
|
|
|
|
qse_size_t len0;
|
2007-05-02 01:07:00 +00:00
|
|
|
int n;
|
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs == 0 || nargs == 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (nargs == 0)
|
|
|
|
{
|
|
|
|
/* flush the console output.
|
|
|
|
* fflush() should return -1 on errors */
|
2009-02-16 08:31:34 +00:00
|
|
|
n = qse_awk_rtx_flushio (run, QSE_AWK_OUT_CONSOLE, QSE_T(""));
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t* ptr, * end;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str0 = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len0 = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-02 03:58:19 +00:00
|
|
|
str0 = qse_awk_rtx_valtocpldup (run, a0, &len0);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str0 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the target name contains a null character.
|
|
|
|
* make fflush return -1 */
|
|
|
|
ptr = str0; end = str0 + len0;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (*ptr == QSE_T('\0'))
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
n = -1;
|
|
|
|
goto skip_flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* flush the given rio */
|
|
|
|
n = flush_io (
|
|
|
|
run, QSE_AWK_RIO_FILE,
|
2008-12-21 21:35:07 +00:00
|
|
|
((len0 == 0)? QSE_NULL: str0), 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
/*if (n == -99) return -1;*/
|
2009-02-16 08:31:34 +00:00
|
|
|
n = flush_io (
|
|
|
|
run, QSE_AWK_RIO_PIPE,
|
2008-12-21 21:35:07 +00:00
|
|
|
((len0 == 0)? QSE_NULL: str0), n);
|
2007-05-02 01:07:00 +00:00
|
|
|
/*if (n == -99) return -1;*/
|
|
|
|
|
|
|
|
/* if n remains 1, no ip handlers have been defined for
|
2009-01-17 04:20:22 +00:00
|
|
|
* file, pipe, and rwpipe. so make fflush return -1.
|
2007-05-02 01:07:00 +00:00
|
|
|
* 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;
|
|
|
|
|
2008-12-30 04:49:25 +00:00
|
|
|
skip_flush:
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str0);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
a0 = qse_awk_rtx_makeintval (run, (qse_long_t)n);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (a0 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
static int fnc_index (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1;
|
|
|
|
qse_char_t* str0, * str1, * ptr;
|
|
|
|
qse_size_t len0, len1;
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_long_t idx, start = 1;
|
2008-12-21 21:35:07 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
QSE_ASSERT (nargs >= 2 && nargs <= 3);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (rtx, 1);
|
|
|
|
|
|
|
|
if (nargs >= 3)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* a2;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
a2 = qse_awk_rtx_getarg (rtx, 2);
|
2011-07-25 08:24:13 +00:00
|
|
|
n = qse_awk_rtx_valtolong (rtx, a2, &start);
|
2009-09-16 08:03:15 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str0 = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len0 = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
str0 = qse_awk_rtx_valtocpldup (rtx, a0, &len0);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str0 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a1->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str1 = ((qse_awk_val_str_t*)a1)->val.ptr;
|
|
|
|
len1 = ((qse_awk_val_str_t*)a1)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
str1 = qse_awk_rtx_valtocpldup (rtx, a1, &len1);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str1 == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR)
|
2009-09-16 08:03:15 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, str0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-17 00:35:29 +00:00
|
|
|
if (start == 0) start = 1;
|
|
|
|
else if (start < 0) start = len0 + start + 1;
|
|
|
|
|
2009-09-19 22:28:49 +00:00
|
|
|
ptr = (start > len0 || start <= 0)? QSE_NULL:
|
|
|
|
(rtx->gbl.ignorecase)?
|
|
|
|
qse_strxncasestr (&str0[start-1], len0-start+1, str1, len1):
|
|
|
|
qse_strxnstr (&str0[start-1], len0-start+1, str1, len1);
|
2009-09-17 00:35:29 +00:00
|
|
|
|
2009-02-18 07:55:48 +00:00
|
|
|
idx = (ptr == QSE_NULL)? 0: ((qse_long_t)(ptr-str0) + 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str0);
|
|
|
|
if (a1->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
a0 = qse_awk_rtx_makeintval (rtx, idx);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (a0 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_length (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* v;
|
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-06-15 02:40:52 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
QSE_ASSERT (nargs >= 0 && nargs <= 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-06-15 02:40:52 +00:00
|
|
|
if (nargs == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-06-15 02:40:52 +00:00
|
|
|
/* get the length of $0 */
|
|
|
|
len = QSE_STR_LEN(&rtx->inrec.line);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-15 02:40:52 +00:00
|
|
|
v = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
if (v->type == QSE_AWK_VAL_STR)
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
len = ((qse_awk_val_str_t*)v)->val.len;
|
2009-06-15 02:40:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = qse_awk_rtx_valtocpldup (rtx, v, &len);
|
|
|
|
if (str == QSE_NULL) return -1;
|
|
|
|
QSE_AWK_FREE (rtx->awk, str);
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 02:40:52 +00:00
|
|
|
v = qse_awk_rtx_makeintval (rtx, len);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (v == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-06-15 02:40:52 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, v);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
static int fnc_substr (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1, * a2, * r;
|
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len;
|
|
|
|
qse_long_t lindex, lcount;
|
2007-05-02 01:07:00 +00:00
|
|
|
int n;
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs >= 2 && nargs <= 3);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (rtx, 1);
|
|
|
|
a2 = (nargs >= 3)? qse_awk_rtx_getarg (rtx, 2): QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
str = qse_awk_rtx_valtocpldup (rtx, a0, &len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 08:24:13 +00:00
|
|
|
n = qse_awk_rtx_valtolong (rtx, a1, &lindex);
|
2009-09-16 08:03:15 +00:00
|
|
|
if (n <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a2 == QSE_NULL) lcount = (qse_long_t)len;
|
2007-05-02 01:07:00 +00:00
|
|
|
else
|
|
|
|
{
|
2011-07-25 08:24:13 +00:00
|
|
|
n = qse_awk_rtx_valtolong (rtx, a2, &lcount);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR)
|
2009-09-16 08:03:15 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 07:55:48 +00:00
|
|
|
lindex = lindex - 1;
|
2008-12-21 21:35:07 +00:00
|
|
|
if (lindex >= (qse_long_t)len) lindex = (qse_long_t)len;
|
2007-05-02 01:07:00 +00:00
|
|
|
else if (lindex < 0) lindex = 0;
|
|
|
|
|
|
|
|
if (lcount < 0) lcount = 0;
|
2008-12-21 21:35:07 +00:00
|
|
|
else if (lcount > (qse_long_t)len - lindex)
|
2007-11-11 00:30:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
lcount = (qse_long_t)len - lindex;
|
2007-11-11 00:30:00 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
r = qse_awk_rtx_makestrval (rtx, &str[lindex], (qse_size_t)lcount);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (r == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str);
|
|
|
|
qse_awk_rtx_setretval (rtx, r);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_split (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1, * a2, * t1, * t2, ** a1_ref;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
qse_cstr_t str, fs;
|
|
|
|
qse_char_t* str_free = QSE_NULL, * fs_free = QSE_NULL;
|
|
|
|
const qse_char_t* p;
|
|
|
|
qse_size_t str_left, org_len;
|
2008-12-21 21:35:07 +00:00
|
|
|
void* fs_rex = QSE_NULL;
|
|
|
|
void* fs_rex_free = QSE_NULL;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
qse_cstr_t tok;
|
|
|
|
qse_long_t nflds;
|
|
|
|
|
2009-06-02 03:34:34 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs >= 2 && nargs <= 3);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (run, 1);
|
|
|
|
a2 = (nargs >= 3)? qse_awk_rtx_getarg (run, 2): QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (a1->type == QSE_AWK_VAL_REF);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (((qse_awk_val_ref_t*)a1)->id >= QSE_AWK_VAL_REF_NAMEDIDX &&
|
|
|
|
((qse_awk_val_ref_t*)a1)->id <= QSE_AWK_VAL_REF_ARGIDX)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* an indexed value should not be assigned another map */
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_EIDXVALASSMAP, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (((qse_awk_val_ref_t*)a1)->id == QSE_AWK_VAL_REF_POS)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* a positional should not be assigned a map */
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_EPOSVALASSMAP, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
a1_ref = (qse_awk_val_t**)((qse_awk_val_ref_t*)a1)->adr;
|
|
|
|
if ((*a1_ref)->type != QSE_AWK_VAL_NIL &&
|
|
|
|
(*a1_ref)->type != QSE_AWK_VAL_MAP)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* cannot change a scalar value to a map */
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ESCALARTOMAP, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
str.ptr = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
str.len = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
str.ptr = qse_awk_rtx_valtocpldup (run, a0, &str.len);
|
|
|
|
if (str.ptr == QSE_NULL) return -1;
|
|
|
|
str_free = (qse_char_t*)str.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a2 == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* get the value from FS */
|
2009-02-02 08:28:04 +00:00
|
|
|
t1 = qse_awk_rtx_getgbl (run, QSE_AWK_GBL_FS);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (t1->type == QSE_AWK_VAL_NIL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
fs.ptr = QSE_T(" ");
|
|
|
|
fs.len = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2008-12-21 21:35:07 +00:00
|
|
|
else if (t1->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
fs.ptr = ((qse_awk_val_str_t*)t1)->val.ptr;
|
|
|
|
fs.len = ((qse_awk_val_str_t*)t1)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
fs.ptr = qse_awk_rtx_valtocpldup (run, t1, &fs.len);
|
|
|
|
if (fs.ptr == QSE_NULL) goto oops;
|
|
|
|
fs_free = (qse_char_t*)fs.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (fs.len > 1) fs_rex = run->gbl.fs;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2009-10-20 07:33:40 +00:00
|
|
|
else if (a2->type == QSE_AWK_VAL_REX)
|
|
|
|
{
|
|
|
|
/* the third parameter is a regular expression */
|
|
|
|
fs_rex = ((qse_awk_val_rex_t*)a2)->code;
|
|
|
|
|
|
|
|
/* make the loop below to take fs_rex by
|
|
|
|
* setting fs_len greater than 1*/
|
2011-05-19 20:50:51 +00:00
|
|
|
fs.ptr = QSE_NULL;
|
|
|
|
fs.len = 2;
|
2009-10-20 07:33:40 +00:00
|
|
|
}
|
|
|
|
else
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a2->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
fs.ptr = ((qse_awk_val_str_t*)a2)->val.ptr;
|
|
|
|
fs.len = ((qse_awk_val_str_t*)a2)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
fs.ptr = qse_awk_rtx_valtocpldup (run, a2, &fs.len);
|
|
|
|
if (fs.ptr == QSE_NULL) goto oops;
|
|
|
|
fs_free = (qse_char_t*)fs.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (fs.len > 1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
fs_rex = QSE_AWK_BUILDREX (
|
2011-05-19 20:50:51 +00:00
|
|
|
run->awk, fs.ptr, fs.len, &errnum);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (fs_rex == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
fs_rex_free = fs_rex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
t1 = qse_awk_rtx_makemapval (run);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (t1 == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_rtx_refdownval (run, *a1_ref);
|
2007-05-02 01:07:00 +00:00
|
|
|
*a1_ref = t1;
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_rtx_refupval (run, *a1_ref);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
p = str.ptr; str_left = str.len; org_len = str.len;
|
2009-06-10 07:07:42 +00:00
|
|
|
nflds = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
while (p != QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
qse_char_t key_buf[QSE_SIZEOF(qse_long_t)*8+2];
|
|
|
|
qse_size_t key_len;
|
|
|
|
|
|
|
|
if (fs.len <= 1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-15 00:21:19 +00:00
|
|
|
p = qse_awk_rtx_strxntok (run,
|
2011-05-19 20:50:51 +00:00
|
|
|
p, str.len, fs.ptr, fs.len, &tok);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-16 07:31:43 +00:00
|
|
|
p = qse_awk_rtx_strxntokbyrex (
|
2011-05-19 20:50:51 +00:00
|
|
|
run, str.ptr, org_len, p, str.len,
|
2011-05-19 08:36:40 +00:00
|
|
|
fs_rex, &tok, &errnum
|
2009-05-16 07:31:43 +00:00
|
|
|
);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (p == QSE_NULL && errnum != QSE_AWK_ENOERR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
if (nflds == 0 && p == QSE_NULL && tok.len == 0)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* no field at all*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
QSE_ASSERT ((tok.ptr != QSE_NULL && tok.len > 0) || tok.len == 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
/* create the field string */
|
2011-05-19 08:36:40 +00:00
|
|
|
t2 = qse_awk_rtx_makestrval (run, tok.ptr, tok.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (t2 == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
/* put it into the map */
|
2008-12-21 21:35:07 +00:00
|
|
|
key_len = qse_awk_longtostr (
|
2011-05-19 20:50:51 +00:00
|
|
|
run->awk, ++nflds, 10, QSE_NULL, key_buf, QSE_COUNTOF(key_buf));
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (key_len != (qse_size_t)-1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (qse_awk_rtx_setmapvalfld (
|
|
|
|
run, t1, key_buf, key_len, t2) == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
qse_awk_rtx_refupval (run, t2);
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_rtx_refdownval (run, t2);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
str.len = str_left - (p - str.ptr);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (str_free) QSE_AWK_FREE (run->awk, str_free);
|
|
|
|
if (fs_free) QSE_AWK_FREE (run->awk, fs_free);
|
|
|
|
if (fs_rex_free) QSE_AWK_FREEREX (run->awk, fs_rex_free);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-06-10 07:07:42 +00:00
|
|
|
/*nflds--;*/
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-06-10 07:07:42 +00:00
|
|
|
t1 = qse_awk_rtx_makeintval (run, nflds);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (t1 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, t1);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
oops:
|
|
|
|
if (str_free) QSE_AWK_FREE (run->awk, str_free);
|
|
|
|
if (fs_free) QSE_AWK_FREE (run->awk, fs_free);
|
|
|
|
if (fs_rex_free) QSE_AWK_FREEREX (run->awk, fs_rex_free);
|
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_tolower (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len, i;
|
|
|
|
qse_awk_val_t* a0, * r;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs == 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-02 03:58:19 +00:00
|
|
|
str = qse_awk_rtx_valtocpldup (run, a0, &len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
for (i = 0; i < len; i++) str[i] = QSE_AWK_TOLOWER (run->awk, str[i]);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
r = qse_awk_rtx_makestrval (run, str, len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (r == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str);
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, r);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_toupper (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len, i;
|
|
|
|
qse_awk_val_t* a0, * r;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs == 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-02 03:58:19 +00:00
|
|
|
str = qse_awk_rtx_valtocpldup (run, a0, &len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
for (i = 0; i < len; i++) str[i] = QSE_AWK_TOUPPER (run->awk, str[i]);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
r = qse_awk_rtx_makestrval (run, str, len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (r == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str);
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, r);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-31 04:31:40 +00:00
|
|
|
static int __substitute (qse_awk_rtx_t* run, qse_long_t max_count)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1, * a2, ** a2_ref, * v;
|
2011-05-19 08:36:40 +00:00
|
|
|
|
|
|
|
qse_cstr_t s0, s1, s2;
|
|
|
|
const qse_char_t* s2_end;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
qse_char_t* s0_free = QSE_NULL;
|
|
|
|
qse_char_t* s1_free = QSE_NULL;
|
|
|
|
qse_char_t* s2_free = QSE_NULL;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
void* rex = QSE_NULL;
|
2011-05-19 20:50:51 +00:00
|
|
|
void* rex_free = QSE_NULL;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_str_t new;
|
2011-05-19 20:50:51 +00:00
|
|
|
int new_inited = 0, opt;
|
|
|
|
|
|
|
|
qse_cstr_t mat, pmat, cur;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_long_t sub_count;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs >= 2 && nargs <= 3);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (run, 1);
|
|
|
|
a2 = (nargs >= 3)? qse_awk_rtx_getarg (run, 2): QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (a2 == QSE_NULL || a2->type == QSE_AWK_VAL_REF);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_REX)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
rex = ((qse_awk_val_rex_t*)a0)->code;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2008-12-21 21:35:07 +00:00
|
|
|
else if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s0.ptr = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
s0.len = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s0.ptr = qse_awk_rtx_valtocpldup (run, a0, &s0.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (s0.ptr == QSE_NULL) goto oops;
|
2011-05-19 08:36:40 +00:00
|
|
|
s0_free = (qse_char_t*)s0.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a1->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s1.ptr = ((qse_awk_val_str_t*)a1)->val.ptr;
|
|
|
|
s1.len = ((qse_awk_val_str_t*)a1)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s1.ptr = qse_awk_rtx_valtocpldup (run, a1, &s1.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (s1.ptr == QSE_NULL) goto oops;
|
2011-05-19 08:36:40 +00:00
|
|
|
s1_free = (qse_char_t*)s1.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a2 == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* is this correct? any needs to use inrec.d0? */
|
2011-05-19 08:36:40 +00:00
|
|
|
s2.ptr = QSE_STR_PTR(&run->inrec.line);
|
|
|
|
s2.len = QSE_STR_LEN(&run->inrec.line);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2008-12-21 21:35:07 +00:00
|
|
|
else if (((qse_awk_val_ref_t*)a2)->id == QSE_AWK_VAL_REF_POS)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t idx;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
idx = (qse_size_t)((qse_awk_val_ref_t*)a2)->adr;
|
2007-05-02 01:07:00 +00:00
|
|
|
if (idx == 0)
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s2.ptr = QSE_STR_PTR(&run->inrec.line);
|
|
|
|
s2.len = QSE_STR_LEN(&run->inrec.line);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else if (idx <= run->inrec.nflds)
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s2.ptr = run->inrec.flds[idx-1].ptr;
|
|
|
|
s2.len = run->inrec.flds[idx-1].len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s2.ptr = QSE_T("");
|
|
|
|
s2.len = 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
a2_ref = (qse_awk_val_t**)((qse_awk_val_ref_t*)a2)->adr;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if ((*a2_ref)->type == QSE_AWK_VAL_MAP)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* a map is not allowed as the third parameter */
|
2009-08-21 05:28:03 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_EMAPNA, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if ((*a2_ref)->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s2.ptr = ((qse_awk_val_str_t*)(*a2_ref))->val.ptr;
|
|
|
|
s2.len = ((qse_awk_val_str_t*)(*a2_ref))->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
s2.ptr = qse_awk_rtx_valtocpldup (run, *a2_ref, &s2.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (s2.ptr == QSE_NULL) goto oops;
|
2011-05-19 08:36:40 +00:00
|
|
|
s2_free = (qse_char_t*)s2.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (qse_str_init (&new, run->awk->mmgr, s2.len) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-19 20:50:51 +00:00
|
|
|
new_inited = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_REX)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
|
|
|
|
2009-06-18 06:43:50 +00:00
|
|
|
rex = QSE_AWK_BUILDREX (
|
2011-05-19 08:36:40 +00:00
|
|
|
run->awk, s0.ptr, s0.len, &errnum);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (rex == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
rex_free = rex;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-12-11 07:03:54 +00:00
|
|
|
opt = (run->gbl.ignorecase)? QSE_REX_IGNORECASE: 0;
|
2009-06-18 06:43:50 +00:00
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
s2_end = s2.ptr + s2.len;
|
|
|
|
cur.ptr = s2.ptr;
|
|
|
|
cur.len = s2.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
sub_count = 0;
|
|
|
|
|
2009-06-18 06:43:50 +00:00
|
|
|
pmat.ptr = QSE_NULL;
|
|
|
|
pmat.len = 0;
|
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
/* perform test when cur_ptr == s2_end also because
|
2009-06-18 06:43:50 +00:00
|
|
|
* end of string($) needs to be tested */
|
2011-05-19 08:36:40 +00:00
|
|
|
while (cur.ptr <= s2_end)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-03 06:55:55 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
2011-05-19 20:50:51 +00:00
|
|
|
int n;
|
|
|
|
qse_size_t m, i;
|
2009-09-03 06:55:55 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
if (max_count == 0 || sub_count < max_count)
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
n = QSE_AWK_MATCHREX (
|
2011-05-19 08:36:40 +00:00
|
|
|
run->awk, rex, opt, &s2, &cur, &mat, &errnum
|
2009-09-03 06:55:55 +00:00
|
|
|
);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else n = 0;
|
|
|
|
|
2009-09-03 06:55:55 +00:00
|
|
|
if (n <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-03 06:55:55 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* no more match found */
|
2008-12-21 21:35:07 +00:00
|
|
|
if (qse_str_ncat (
|
2011-05-19 08:36:40 +00:00
|
|
|
&new, cur.ptr, cur.len) == (qse_size_t)-1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-18 06:43:50 +00:00
|
|
|
if (mat.len == 0 &&
|
|
|
|
pmat.ptr != QSE_NULL &&
|
|
|
|
mat.ptr == pmat.ptr + pmat.len)
|
|
|
|
{
|
|
|
|
/* match length is 0 and the match is still at the
|
|
|
|
* end of the previous match */
|
|
|
|
goto skip_one_char;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (qse_str_ncat (
|
2011-05-19 08:36:40 +00:00
|
|
|
&new, cur.ptr, mat.ptr - cur.ptr) == (qse_size_t)-1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
for (i = 0; i < s1.len; i++)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
if ((i+1) < s1.len &&
|
|
|
|
s1.ptr[i] == QSE_T('\\') &&
|
|
|
|
s1.ptr[i+1] == QSE_T('&'))
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
m = qse_str_ccat (&new, QSE_T('&'));
|
2007-05-02 01:07:00 +00:00
|
|
|
i++;
|
|
|
|
}
|
2011-05-19 08:36:40 +00:00
|
|
|
else if (s1.ptr[i] == QSE_T('&'))
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-05-16 07:31:43 +00:00
|
|
|
m = qse_str_ncat (&new, mat.ptr, mat.len);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
m = qse_str_ccat (&new, s1.ptr[i]);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (m == (qse_size_t)-1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_count++;
|
2011-05-19 08:36:40 +00:00
|
|
|
cur.len = cur.len - ((mat.ptr - cur.ptr) + mat.len);
|
|
|
|
cur.ptr = mat.ptr + mat.len;
|
2009-06-18 06:43:50 +00:00
|
|
|
|
|
|
|
pmat = mat;
|
|
|
|
|
|
|
|
if (mat.len == 0)
|
|
|
|
{
|
|
|
|
skip_one_char:
|
|
|
|
/* special treatment is needed if match length is 0 */
|
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
m = qse_str_ncat (&new, cur.ptr, 1);
|
2009-06-18 06:43:50 +00:00
|
|
|
if (m == (qse_size_t)-1)
|
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2009-06-18 06:43:50 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
cur.ptr++; cur.len--;
|
2009-06-18 06:43:50 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (rex_free)
|
|
|
|
{
|
|
|
|
QSE_AWK_FREEREX (run->awk, rex_free);
|
|
|
|
rex_free = QSE_NULL;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (sub_count > 0)
|
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a2 == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 20:50:51 +00:00
|
|
|
int n;
|
|
|
|
n = qse_awk_rtx_setrec (run, 0,
|
|
|
|
QSE_STR_PTR(&new), QSE_STR_LEN(&new));
|
|
|
|
if (n <= -1) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2008-12-21 21:35:07 +00:00
|
|
|
else if (((qse_awk_val_ref_t*)a2)->id == QSE_AWK_VAL_REF_POS)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
n = qse_awk_rtx_setrec (
|
2008-12-21 21:35:07 +00:00
|
|
|
run, (qse_size_t)((qse_awk_val_ref_t*)a2)->adr,
|
|
|
|
QSE_STR_PTR(&new), QSE_STR_LEN(&new));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n <= -1) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
v = qse_awk_rtx_makestrval (run,
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_STR_PTR(&new), QSE_STR_LEN(&new));
|
2011-05-19 20:50:51 +00:00
|
|
|
if (v == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_rtx_refdownval (run, *a2_ref);
|
2007-05-02 01:07:00 +00:00
|
|
|
*a2_ref = v;
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_rtx_refupval (run, *a2_ref);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_str_fini (&new);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (s2_free) QSE_AWK_FREE (run->awk, s2_free);
|
|
|
|
if (s1_free) QSE_AWK_FREE (run->awk, s1_free);
|
|
|
|
if (s0_free) QSE_AWK_FREE (run->awk, s0_free);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
v = qse_awk_rtx_makeintval (run, sub_count);
|
2009-06-18 06:43:50 +00:00
|
|
|
if (v == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, v);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
oops:
|
|
|
|
if (rex_free) QSE_AWK_FREEREX (run->awk, rex_free);
|
|
|
|
if (new_inited) qse_str_fini (&new);
|
|
|
|
if (s2_free) QSE_AWK_FREE (run->awk, s2_free);
|
|
|
|
if (s1_free) QSE_AWK_FREE (run->awk, s1_free);
|
|
|
|
if (s0_free) QSE_AWK_FREE (run->awk, s0_free);
|
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_gsub (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
return __substitute (run, 0);
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_sub (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
return __substitute (run, 1);
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
static int fnc_match (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1;
|
|
|
|
qse_char_t* str0, * str1;
|
|
|
|
qse_size_t len0, len1;
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_long_t idx, start = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
void* rex;
|
2009-09-03 06:55:55 +00:00
|
|
|
int n;
|
2009-05-16 07:31:43 +00:00
|
|
|
qse_cstr_t mat;
|
2009-09-03 06:55:55 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
QSE_ASSERT (nargs >= 2 && nargs <= 3);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (rtx, 1);
|
|
|
|
|
|
|
|
if (nargs >= 3)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* a2;
|
|
|
|
|
|
|
|
a2 = qse_awk_rtx_getarg (rtx, 2);
|
2009-12-11 07:03:54 +00:00
|
|
|
#if 0
|
|
|
|
if (a2->type == QSE_AWK_VAL_MAP)
|
|
|
|
{
|
|
|
|
/* if the 3rd paramater is an array,
|
|
|
|
* it is a placeholder to store parenthesized
|
|
|
|
* subexpressions */
|
|
|
|
|
|
|
|
/* TODO: please implement this... */
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* if the 3rd parameter is not an array,
|
|
|
|
* it is treated as a match start index */
|
2011-07-25 08:24:13 +00:00
|
|
|
n = qse_awk_rtx_valtolong (rtx, a2, &start);
|
2009-12-11 07:03:54 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
}
|
2009-09-16 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str0 = ((qse_awk_val_str_t*)a0)->val.ptr;
|
|
|
|
len0 = ((qse_awk_val_str_t*)a0)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
str0 = qse_awk_rtx_valtocpldup (rtx, a0, &len0);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str0 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a1->type == QSE_AWK_VAL_REX)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
rex = ((qse_awk_val_rex_t*)a1)->code;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a1->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
str1 = ((qse_awk_val_str_t*)a1)->val.ptr;
|
|
|
|
len1 = ((qse_awk_val_str_t*)a1)->val.len;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
str1 = qse_awk_rtx_valtocpldup (rtx, a1, &len1);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str1 == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR)
|
2009-09-16 08:03:15 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, str0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
rex = QSE_AWK_BUILDREX (rtx->awk, str1, len1, &errnum);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (rex == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR)
|
2009-09-16 08:03:15 +00:00
|
|
|
QSE_AWK_FREE (rtx->awk, str0);
|
|
|
|
qse_awk_rtx_seterrnum (rtx, errnum, QSE_NULL);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
if (a1->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str1);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 00:35:29 +00:00
|
|
|
if (start == 0) start = 1;
|
|
|
|
else if (start < 0) start = len0 + start + 1;
|
|
|
|
|
|
|
|
if (start > len0 || start <= 0) n = 0;
|
2009-09-16 08:03:15 +00:00
|
|
|
else
|
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
qse_cstr_t tmp;
|
|
|
|
|
|
|
|
/*TODO: must use str0,len0?*/
|
|
|
|
tmp.ptr = str0 + start - 1;
|
|
|
|
tmp.len = len0 - start + 1;
|
2009-09-16 08:03:15 +00:00
|
|
|
n = QSE_AWK_MATCHREX (
|
|
|
|
rtx->awk, rex,
|
2009-12-11 07:03:54 +00:00
|
|
|
(rtx->gbl.ignorecase? QSE_REX_IGNORECASE: 0),
|
2011-05-19 08:36:40 +00:00
|
|
|
&tmp, &tmp, &mat, &errnum
|
2009-09-16 08:03:15 +00:00
|
|
|
);
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str0);
|
|
|
|
if (a1->type != QSE_AWK_VAL_REX) QSE_AWK_FREEREX (rtx->awk, rex);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-03 06:55:55 +00:00
|
|
|
if (n <= -1)
|
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, errnum, QSE_NULL);
|
2009-09-03 06:55:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-05-16 07:31:43 +00:00
|
|
|
idx = (n == 0)? 0: ((qse_long_t)(mat.ptr-str0) + 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
a0 = qse_awk_rtx_makeintval (rtx, idx);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (a0 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_refupval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
a1 = qse_awk_rtx_makeintval (rtx,
|
2009-05-16 07:31:43 +00:00
|
|
|
((n == 0)? (qse_long_t)-1: (qse_long_t)mat.len));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a1 == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_refupval (rtx, a1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_RSTART, a0) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, a1);
|
|
|
|
qse_awk_rtx_refdownval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-19 20:50:51 +00:00
|
|
|
if (qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_RLENGTH, a1) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, a1);
|
|
|
|
qse_awk_rtx_refdownval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, a1);
|
|
|
|
qse_awk_rtx_refdownval (rtx, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-16 04:43:31 +00:00
|
|
|
static int fnc_sprintf (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0;
|
|
|
|
qse_str_t out, fbu;
|
2011-05-19 20:50:51 +00:00
|
|
|
int out_inited = 0, fbu_inited = 0;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_xstr_t cs0;
|
|
|
|
qse_xstr_t x;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs > 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (qse_str_init (&out, run->awk->mmgr, 256) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-19 20:50:51 +00:00
|
|
|
out_inited = 1;
|
|
|
|
|
2011-09-01 09:43:46 +00:00
|
|
|
if (qse_str_init (&fbu, run->awk->mmgr, 256) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-08-17 02:08:58 +00:00
|
|
|
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-19 20:50:51 +00:00
|
|
|
fbu_inited = 1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (a0->type == QSE_AWK_VAL_STR)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-19 08:36:40 +00:00
|
|
|
cs0 = ((qse_awk_val_str_t*)a0)->val;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-02 03:58:19 +00:00
|
|
|
cs0.ptr = qse_awk_rtx_valtocpldup (run, a0, &cs0.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (cs0.ptr == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-02-15 03:14:49 +00:00
|
|
|
x.ptr = qse_awk_rtx_format (run,
|
2008-12-21 21:35:07 +00:00
|
|
|
&out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len);
|
|
|
|
if (a0->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, cs0.ptr);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (x.ptr == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
/*a0 = qse_awk_rtx_makestrval_nodup (run, x.ptr, x.len);*/
|
|
|
|
a0 = qse_awk_rtx_makestrval (run, x.ptr, x.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (a0 == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_str_fini (&fbu);
|
|
|
|
/*qse_str_yield (&out, QSE_NULL, 0);*/
|
|
|
|
qse_str_fini (&out);
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, a0);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
oops:
|
|
|
|
if (fbu_inited) qse_str_fini (&fbu);
|
|
|
|
if (out_inited) qse_str_fini (&out);
|
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-05-18 08:37:51 +00:00
|
|
|
|
|
|
|
static int fnc_math_1 (
|
|
|
|
qse_awk_rtx_t* rtx, const qse_cstr_t* fnm, qse_awk_math1_t f)
|
|
|
|
{
|
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0;
|
2011-11-22 05:03:31 +00:00
|
|
|
qse_flt_t rv;
|
2011-05-18 08:37:51 +00:00
|
|
|
qse_awk_val_t* r;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
QSE_ASSERT (nargs == 1);
|
|
|
|
|
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
n = qse_awk_rtx_valtoflt (rtx, a0, &rv);
|
2011-05-18 08:37:51 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
r = qse_awk_rtx_makefltval (rtx, f (rtx->awk, rv));
|
2011-05-18 08:37:51 +00:00
|
|
|
if (r == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_math_2 (
|
|
|
|
qse_awk_rtx_t* rtx, const qse_cstr_t* fnm, qse_awk_math2_t f)
|
|
|
|
{
|
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1;
|
2011-11-22 05:03:31 +00:00
|
|
|
qse_flt_t rv0, rv1;
|
2011-05-18 08:37:51 +00:00
|
|
|
qse_awk_val_t* r;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
QSE_ASSERT (nargs == 2);
|
|
|
|
|
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (rtx, 1);
|
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
n = qse_awk_rtx_valtoflt (rtx, a0, &rv0);
|
2011-05-18 08:37:51 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
n = qse_awk_rtx_valtoflt (rtx, a1, &rv1);
|
2011-05-18 08:37:51 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
2011-11-22 05:03:31 +00:00
|
|
|
r = qse_awk_rtx_makefltval (rtx, f (rtx->awk, rv0, rv1));
|
2011-05-18 08:37:51 +00:00
|
|
|
if (r == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_sin (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.sin);
|
|
|
|
}
|
|
|
|
static int fnc_cos (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.cos);
|
|
|
|
}
|
|
|
|
static int fnc_tan (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.tan);
|
|
|
|
}
|
|
|
|
static int fnc_atan (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.atan);
|
|
|
|
}
|
|
|
|
static int fnc_atan2 (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_2 (rtx, fnm, rtx->awk->prm.math.atan2);
|
|
|
|
}
|
|
|
|
static int fnc_log (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.log);
|
|
|
|
}
|
|
|
|
static int fnc_exp (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.exp);
|
|
|
|
}
|
|
|
|
static int fnc_sqrt (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
return fnc_math_1 (rtx, fnm, rtx->awk->prm.math.sqrt);
|
|
|
|
}
|
2011-05-18 20:32:39 +00:00
|
|
|
|
|
|
|
static int fnc_int (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
|
|
|
{
|
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0;
|
|
|
|
qse_long_t lv;
|
|
|
|
qse_awk_val_t* r;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
|
|
|
QSE_ASSERT (nargs == 1);
|
|
|
|
|
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
|
|
|
|
2011-07-25 08:24:13 +00:00
|
|
|
n = qse_awk_rtx_valtolong (run, a0, &lv);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n <= -1) return -1;
|
2011-05-18 20:32:39 +00:00
|
|
|
|
|
|
|
r = qse_awk_rtx_makeintval (run, lv);
|
|
|
|
if (r == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (run, r);
|
|
|
|
return 0;
|
|
|
|
}
|