2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2007-05-02 01:07:00 +00:00
|
|
|
*
|
2013-12-31 10:24:12 +00:00
|
|
|
Copyright 2006-2014 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"
|
2014-05-27 14:13:12 +00:00
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
static int fnc_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
|
|
|
|
static int fnc_fflush (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
|
|
|
|
static int fnc_int (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
#define A_MAX QSE_TYPE_MAX(int)
|
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.
|
2014-07-01 15:27:27 +00:00
|
|
|
*
|
|
|
|
* NOTE: If min is greater than max, the specifier indicate the
|
|
|
|
* name of the module where the function is located.
|
2009-10-20 07:33:40 +00:00
|
|
|
*/
|
2012-11-06 04:30:35 +00:00
|
|
|
static qse_awk_fnc_t sysfnctab[] =
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
/* io functions */
|
2014-06-08 12:46:04 +00:00
|
|
|
{ {QSE_T("close"), 5}, 0, { {1, 2, QSE_NULL}, fnc_close, QSE_AWK_RIO }, QSE_NULL},
|
|
|
|
{ {QSE_T("fflush"), 6}, 0, { {0, 1, QSE_NULL}, fnc_fflush, QSE_AWK_RIO }, QSE_NULL},
|
|
|
|
|
|
|
|
/* integer conversion */
|
|
|
|
{ {QSE_T("int"), 3}, 0, { {1, 1, QSE_NULL}, fnc_int, 0 }, QSE_NULL},
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
/* string functions */
|
2014-05-28 08:40:51 +00:00
|
|
|
{ {QSE_T("index"), 5}, 0, { {2, 3, QSE_NULL}, qse_awk_fnc_index, 0 }, QSE_NULL},
|
2014-05-27 14:13:12 +00:00
|
|
|
{ {QSE_T("substr"), 6}, 0, { {2, 3, QSE_NULL}, qse_awk_fnc_substr, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("length"), 6}, 1, { {0, 1, QSE_NULL}, qse_awk_fnc_length, 0 }, QSE_NULL},
|
2014-05-27 14:26:04 +00:00
|
|
|
{ {QSE_T("split"), 5}, 0, { {2, 3, QSE_T("vrx")}, qse_awk_fnc_split, 0 }, QSE_NULL},
|
2014-05-27 14:13:12 +00:00
|
|
|
{ {QSE_T("tolower"), 7}, 0, { {1, 1, QSE_NULL}, qse_awk_fnc_tolower, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("toupper"), 7}, 0, { {1, 1, QSE_NULL}, qse_awk_fnc_toupper, 0 }, QSE_NULL},
|
2014-05-27 15:28:31 +00:00
|
|
|
{ {QSE_T("gsub"), 4}, 0, { {2, 3, QSE_T("xvr")}, qse_awk_fnc_gsub, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("sub"), 3}, 0, { {2, 3, QSE_T("xvr")}, qse_awk_fnc_sub, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("match"), 5}, 0, { {2, 3, QSE_T("vxv")}, qse_awk_fnc_match, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("sprintf"), 7}, 0, { {1, A_MAX, QSE_NULL}, qse_awk_fnc_sprintf, 0 }, QSE_NULL},
|
2009-06-15 02:40:52 +00:00
|
|
|
|
2011-05-18 08:37:51 +00:00
|
|
|
/* math functions */
|
2014-07-01 15:27:27 +00:00
|
|
|
{ {QSE_T("sin"), 3}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("cos"), 3}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("tan"), 3}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("atan"), 4}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("atan2"), 5}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("log"), 3}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("log10"), 5}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("exp"), 3}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL},
|
|
|
|
{ {QSE_T("sqrt"), 4}, 0, { {A_MAX, 0, QSE_T("math") }, QSE_NULL, 0 }, QSE_NULL}
|
2007-05-02 01:07:00 +00:00
|
|
|
};
|
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
qse_awk_fnc_t* qse_awk_addfnc (qse_awk_t* awk, const qse_char_t* name, const qse_awk_fnc_spec_t* spec)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-02-01 03:59:46 +00:00
|
|
|
qse_awk_fnc_t* fnc;
|
2012-01-17 16:45:01 +00:00
|
|
|
qse_size_t fnc_size;
|
2012-11-06 04:30:35 +00:00
|
|
|
qse_size_t speclen;
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t ncs;
|
2012-11-06 04:30:35 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
ncs.ptr = (qse_char_t*)name;
|
2012-11-06 04:30:35 +00:00
|
|
|
ncs.len = qse_strlen (name);
|
2007-05-06 19:44:00 +00:00
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
if (ncs.len <= 0)
|
2007-09-27 20:33:00 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2012-10-20 15:58:20 +00:00
|
|
|
/* Note it doesn't check if it conflicts with a keyword.
|
|
|
|
* such a function registered won't take effect because
|
|
|
|
* the word is treated as a keyword */
|
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
if (qse_awk_findfnc (awk, &ncs) != QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-11-06 04:30:35 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_EEXIST, &ncs);
|
2008-12-21 21:35:07 +00:00
|
|
|
return QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
speclen = spec->arg.spec? qse_strlen(spec->arg.spec): 0;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
fnc_size = QSE_SIZEOF(*fnc) + (ncs.len + 1 + speclen + 1) * QSE_SIZEOF(qse_char_t);
|
2012-11-03 17:44:23 +00:00
|
|
|
fnc = (qse_awk_fnc_t*) qse_awk_callocmem (awk, fnc_size);
|
|
|
|
if (fnc)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-11-03 17:44:23 +00:00
|
|
|
qse_char_t* tmp;
|
2007-10-25 23:43:00 +00:00
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
tmp = (qse_char_t*)(fnc + 1);
|
2012-11-06 04:30:35 +00:00
|
|
|
fnc->name.len = qse_strcpy (tmp, ncs.ptr);
|
2012-11-03 17:44:23 +00:00
|
|
|
fnc->name.ptr = tmp;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
fnc->spec = *spec;
|
|
|
|
if (spec->arg.spec)
|
|
|
|
{
|
|
|
|
tmp = fnc->name.ptr + fnc->name.len + 1;
|
2012-11-06 04:30:35 +00:00
|
|
|
qse_strcpy (tmp, spec->arg.spec);
|
2012-11-03 17:44:23 +00:00
|
|
|
fnc->spec.arg.spec = tmp;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
if (qse_htb_insert (awk->fnc.user,
|
2012-11-06 04:30:35 +00:00
|
|
|
(qse_char_t*)ncs.ptr, ncs.len, fnc, 0) == QSE_NULL)
|
2012-11-03 17:44:23 +00:00
|
|
|
{
|
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
|
|
|
|
QSE_AWK_FREE (awk, fnc); fnc = 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
|
|
|
}
|
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
int qse_awk_delfnc (qse_awk_t* awk, const qse_char_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t ncs;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
ncs.ptr = (qse_char_t*)name;
|
2012-11-06 04:30:35 +00:00
|
|
|
ncs.len = qse_strlen (name);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
if (qse_htb_delete (awk->fnc.user, ncs.ptr, ncs.len) <= -1)
|
|
|
|
{
|
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_ENOENT, &ncs);
|
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
|
|
|
}
|
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_awk_fnc_t* qse_awk_findfnc (qse_awk_t* awk, const qse_xstr_t* name)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_pair_t* pair;
|
2012-11-06 04:30:35 +00:00
|
|
|
int i;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-01-17 16:45:01 +00:00
|
|
|
/* search the system function table
|
|
|
|
* though some optimization like binary search can
|
|
|
|
* speed up the search, i don't do that since this
|
|
|
|
* function is called durting parse-time only.
|
2012-11-03 17:44:23 +00:00
|
|
|
*/
|
2012-11-06 04:30:35 +00:00
|
|
|
for (i = 0; i < QSE_COUNTOF(sysfnctab); i++)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2012-11-06 04:30:35 +00:00
|
|
|
if ((awk->opt.trait & sysfnctab[i].spec.trait) != sysfnctab[i].spec.trait) continue;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2011-05-24 10:52:37 +00:00
|
|
|
if (qse_strxncmp (
|
2012-11-06 04:30:35 +00:00
|
|
|
sysfnctab[i].name.ptr, sysfnctab[i].name.len,
|
|
|
|
name->ptr, name->len) == 0) return &sysfnctab[i];
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
pair = qse_htb_search (awk->fnc.user, name->ptr, name->len);
|
2012-11-06 04:30:35 +00:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
qse_awk_fnc_t* fnc;
|
|
|
|
fnc = (qse_awk_fnc_t*)QSE_HTB_VPTR(pair);
|
|
|
|
if ((awk->opt.trait & fnc->spec.trait) == fnc->spec.trait) return fnc;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
return QSE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
static int fnc_close (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
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
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
name = qse_awk_rtx_getvalstr (rtx, a0, &len);
|
|
|
|
if (name == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-01-16 05:04:56 +00:00
|
|
|
if (a1)
|
2009-08-27 06:59:08 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
opt = qse_awk_rtx_getvalstr (rtx, a1, &optlen);
|
|
|
|
if (opt == QSE_NULL)
|
2009-08-27 06:59:08 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, name);
|
|
|
|
return -1;
|
2009-08-27 06:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
if (len == 0)
|
|
|
|
{
|
2013-01-16 05:04:56 +00:00
|
|
|
/* getline or print doesn't allow an empty string for the
|
2007-05-02 01:07:00 +00:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
if (opt)
|
2009-08-27 06:59:08 +00:00
|
|
|
{
|
|
|
|
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:
|
2014-02-19 15:24:33 +00:00
|
|
|
if (a1) qse_awk_rtx_freevalstr (rtx, a1, opt);
|
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, name);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
v = qse_awk_rtx_makeintval (rtx, (qse_awk_int_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;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
static int fnc_fflush (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_val_t* a0, * v;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t* str0;
|
|
|
|
qse_size_t len0;
|
2007-05-02 01:07:00 +00:00
|
|
|
int n;
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
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)
|
|
|
|
{
|
2013-01-16 05:04:56 +00:00
|
|
|
/* fflush() flushes the console output.
|
|
|
|
* fflush() should return -1 on errors.
|
|
|
|
*
|
|
|
|
* if no previous console output statement is seen,
|
|
|
|
* this function won't be able to find the entry.
|
|
|
|
* so it returns -1;
|
|
|
|
*
|
|
|
|
* BEGIN { flush(); } # flush() returns -1
|
|
|
|
* BEGIN { print 1; flush(); } # flush() returns 0
|
|
|
|
*/
|
2014-02-19 15:24:33 +00:00
|
|
|
n = qse_awk_rtx_flushio (rtx, 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
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
str0 = qse_awk_rtx_getvalstr (rtx, a0, &len0);
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:04:56 +00:00
|
|
|
/* flush the given rio.
|
|
|
|
*
|
|
|
|
* flush("") flushes all output streams regardless of names.
|
|
|
|
* pass QSE_NULL for the name in that case so that the
|
|
|
|
* callee matches any streams.
|
|
|
|
*
|
|
|
|
* fflush() doesn't specify the type of output streams
|
|
|
|
* so it attemps to flush all types of output streams.
|
|
|
|
*
|
|
|
|
* though not useful, it's possible to have multiple
|
|
|
|
* streams with the same name but of different types.
|
|
|
|
*
|
|
|
|
* BEGIN {
|
|
|
|
* print 1 | "/tmp/x";
|
|
|
|
* print 1 > "/tmp/x";
|
|
|
|
* fflush("/tmp/x");
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
n = flush_io (
|
2014-02-19 15:24:33 +00:00
|
|
|
rtx, QSE_AWK_OUT_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 (
|
2014-02-19 15:24:33 +00:00
|
|
|
rtx, QSE_AWK_OUT_APFILE,
|
2013-01-16 05:04:56 +00:00
|
|
|
((len0 == 0)? QSE_NULL: str0), n);
|
|
|
|
/*if (n == -99) return -1;*/
|
|
|
|
n = flush_io (
|
2014-02-19 15:24:33 +00:00
|
|
|
rtx, QSE_AWK_OUT_PIPE,
|
2013-01-16 05:04:56 +00:00
|
|
|
((len0 == 0)? QSE_NULL: str0), n);
|
|
|
|
/*if (n == -99) return -1;*/
|
|
|
|
n = flush_io (
|
2014-02-19 15:24:33 +00:00
|
|
|
rtx, QSE_AWK_OUT_RWPIPE,
|
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;*/
|
|
|
|
|
2013-01-16 05:04:56 +00:00
|
|
|
/* if n remains 1, no io 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:
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str0);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
v = qse_awk_rtx_makeintval (rtx, (qse_awk_int_t)n);
|
|
|
|
if (v == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, v);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-28 08:40:51 +00:00
|
|
|
static int index_or_rindex (qse_awk_rtx_t* rtx, int rindex)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-05-28 08:40:51 +00:00
|
|
|
/* this is similar to the built-in index() function but doesn't
|
|
|
|
* care about IGNORECASE. */
|
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0, * a1;
|
|
|
|
qse_char_t* str0, * str1, * ptr;
|
|
|
|
qse_size_t len0, len1;
|
|
|
|
qse_awk_int_t idx, boundary = 1;
|
|
|
|
|
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
a1 = qse_awk_rtx_getarg (rtx, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
index ("abc", "d", 3);
|
|
|
|
rindex ("abcdefabcdx", "cd", 8);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nargs >= 3)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* a2;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
a2 = qse_awk_rtx_getarg (rtx, 2);
|
|
|
|
n = qse_awk_rtx_valtoint (rtx, a2, &boundary);
|
|
|
|
if (n <= -1) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
str0 = qse_awk_rtx_getvalstr (rtx, a0, &len0);
|
|
|
|
if (str0 == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
str1 = qse_awk_rtx_getvalstr (rtx, a1, &len1);
|
|
|
|
if (str1 == QSE_NULL)
|
|
|
|
{
|
|
|
|
if (a0->type != QSE_AWK_VAL_STR)
|
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nargs < 3)
|
|
|
|
{
|
|
|
|
boundary = rindex? len0: 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (boundary == 0) boundary = 1;
|
|
|
|
else if (boundary < 0) boundary = len0 + boundary + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundary > len0 || boundary <= 0)
|
|
|
|
{
|
|
|
|
ptr = QSE_NULL;
|
|
|
|
}
|
|
|
|
else if (rindex)
|
|
|
|
{
|
|
|
|
/* 'boundary' acts as an end position */
|
|
|
|
ptr = rtx->gbl.ignorecase?
|
|
|
|
qse_strxnrcasestr (&str0[0], boundary, str1, len1):
|
|
|
|
qse_strxnrstr (&str0[0], boundary, str1, len1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* 'boundary' acts as an start position */
|
|
|
|
ptr = rtx->gbl.ignorecase?
|
|
|
|
qse_strxncasestr (&str0[boundary-1], len0-boundary+1, str1, len1):
|
|
|
|
qse_strxnstr (&str0[boundary-1], len0-boundary+1, str1, len1);
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = (ptr == QSE_NULL)? 0: ((qse_awk_int_t)(ptr-str0) + 1);
|
|
|
|
|
|
|
|
qse_awk_rtx_freevalstr (rtx, a1, str1);
|
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str0);
|
|
|
|
|
|
|
|
a0 = qse_awk_rtx_makeintval (rtx, idx);
|
|
|
|
if (a0 == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, a0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_awk_fnc_index (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
return index_or_rindex (rtx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_awk_fnc_rindex (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
return index_or_rindex (rtx, 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 14:13:12 +00:00
|
|
|
int qse_awk_fnc_length (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
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);
|
2012-11-25 16:16:44 +00:00
|
|
|
if (v->type == QSE_AWK_VAL_MAP)
|
2009-06-15 02:40:52 +00:00
|
|
|
{
|
2012-11-25 16:16:44 +00:00
|
|
|
/* map size */
|
|
|
|
len = QSE_HTB_SIZE(((qse_awk_val_map_t*)v)->map);
|
|
|
|
}
|
|
|
|
else if (v->type == QSE_AWK_VAL_STR)
|
|
|
|
{
|
|
|
|
/* string length */
|
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
|
|
|
|
{
|
2012-11-25 16:16:44 +00:00
|
|
|
/* convert to string and get length */
|
2012-10-31 08:31:58 +00:00
|
|
|
str = qse_awk_rtx_valtostrdup (rtx, v, &len);
|
2009-06-15 02:40:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:13:12 +00:00
|
|
|
int qse_awk_fnc_substr (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
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;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_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
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
str = qse_awk_rtx_getvalstr (rtx, a0, &len);
|
|
|
|
if (str == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
n = qse_awk_rtx_valtoint (rtx, a1, &lindex);
|
2009-09-16 08:03:15 +00:00
|
|
|
if (n <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
if (a2 == QSE_NULL) lcount = (qse_awk_int_t)len;
|
2007-05-02 01:07:00 +00:00
|
|
|
else
|
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
n = qse_awk_rtx_valtoint (rtx, a2, &lcount);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-18 07:55:48 +00:00
|
|
|
lindex = lindex - 1;
|
2013-11-05 14:16:45 +00:00
|
|
|
if (lindex >= (qse_awk_int_t)len) lindex = (qse_awk_int_t)len;
|
2007-05-02 01:07:00 +00:00
|
|
|
else if (lindex < 0) lindex = 0;
|
|
|
|
|
|
|
|
if (lcount < 0) lcount = 0;
|
2013-11-05 14:16:45 +00:00
|
|
|
else if (lcount > (qse_awk_int_t)len - lindex)
|
2007-11-11 00:30:00 +00:00
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
lcount = (qse_awk_int_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
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str);
|
2009-09-16 08:03:15 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, r);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:26:04 +00:00
|
|
|
int qse_awk_fnc_split (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_val_t* a0, * a1, * a2, * t1, * t2;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t str, fs;
|
2011-05-19 20:50:51 +00:00
|
|
|
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
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t tok;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t nflds;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
2009-06-02 03:34:34 +00:00
|
|
|
qse_awk_errnum_t errnum;
|
2013-04-16 10:09:24 +00:00
|
|
|
int x;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-08-23 15:19:29 +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
|
|
|
|
2013-08-23 15:19:29 +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
|
|
|
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 (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
|
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
str.ptr = qse_awk_rtx_valtostrdup (rtx, a0, &str.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
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 */
|
2013-08-23 15:19:29 +00:00
|
|
|
t1 = qse_awk_rtx_getgbl (rtx, 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
|
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
fs.ptr = qse_awk_rtx_valtostrdup (rtx, t1, &fs.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (fs.ptr == QSE_NULL) goto oops;
|
|
|
|
fs_free = (qse_char_t*)fs.ptr;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
if (fs.len > 1) fs_rex = rtx->gbl.fs[rtx->gbl.ignorecase];
|
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 */
|
2013-08-23 15:19:29 +00:00
|
|
|
fs_rex = ((qse_awk_val_rex_t*)a2)->code[rtx->gbl.ignorecase];
|
2009-10-20 07:33:40 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
fs.ptr = qse_awk_rtx_valtostrdup (rtx, a2, &fs.len);
|
2011-05-19 20:50:51 +00:00
|
|
|
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
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
int x;
|
|
|
|
|
|
|
|
if (rtx->gbl.ignorecase)
|
|
|
|
x = qse_awk_buildrex (rtx->awk, fs.ptr, fs.len, &errnum, QSE_NULL, &fs_rex);
|
|
|
|
else
|
|
|
|
x = qse_awk_buildrex (rtx->awk, fs.ptr, fs.len, &errnum, &fs_rex, QSE_NULL);
|
|
|
|
|
|
|
|
if (x <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, errnum, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2013-08-23 15:19:29 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
fs_rex_free = fs_rex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
t1 = qse_awk_rtx_makemapval (rtx);
|
2011-05-19 20:50:51 +00:00
|
|
|
if (t1 == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_awk_rtx_refupval (rtx, t1);
|
|
|
|
x = qse_awk_rtx_setrefval (rtx, (qse_awk_val_ref_t*)a1, t1);
|
|
|
|
qse_awk_rtx_refdownval (rtx, t1);
|
2013-04-16 10:09:24 +00:00
|
|
|
if (x <= -1) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-04-16 10:09:24 +00:00
|
|
|
/* fill the map with actual values */
|
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
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_char_t key_buf[QSE_SIZEOF(qse_awk_int_t)*8+2];
|
2011-05-19 20:50:51 +00:00
|
|
|
qse_size_t key_len;
|
|
|
|
|
|
|
|
if (fs.len <= 1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
p = qse_awk_rtx_strxntok (rtx,
|
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 (
|
2013-08-23 15:19:29 +00:00
|
|
|
rtx, 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
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, 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
|
|
|
|
2013-05-29 08:46:23 +00:00
|
|
|
/* create the field string - however, the split function must
|
|
|
|
* create a numeric string if the string is a number */
|
2014-07-02 12:28:58 +00:00
|
|
|
/*t2 = qse_awk_rtx_makestrvalwithxstr (rtx, &tok);*/
|
|
|
|
t2 = qse_awk_rtx_makenstrvalwithxstr (rtx, &tok);
|
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 */
|
2013-11-05 14:16:45 +00:00
|
|
|
key_len = qse_awk_inttostr (
|
2013-08-23 15:19:29 +00:00
|
|
|
rtx->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 (
|
2013-08-23 15:19:29 +00:00
|
|
|
rtx, t1, key_buf, key_len, t2) == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_awk_rtx_refupval (rtx, t2);
|
|
|
|
qse_awk_rtx_refdownval (rtx, 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
|
|
|
}
|
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
if (str_free) QSE_AWK_FREE (rtx->awk, str_free);
|
|
|
|
if (fs_free) QSE_AWK_FREE (rtx->awk, fs_free);
|
|
|
|
if (fs_rex_free)
|
|
|
|
{
|
|
|
|
if (rtx->gbl.ignorecase)
|
|
|
|
qse_awk_freerex (rtx->awk, QSE_NULL, fs_rex_free);
|
|
|
|
else
|
|
|
|
qse_awk_freerex (rtx->awk, fs_rex_free, QSE_NULL);
|
|
|
|
}
|
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
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
t1 = qse_awk_rtx_makeintval (rtx, nflds);
|
2009-08-17 02:08:58 +00:00
|
|
|
if (t1 == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, t1);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
|
|
|
oops:
|
2013-08-23 15:19:29 +00:00
|
|
|
if (str_free) QSE_AWK_FREE (rtx->awk, str_free);
|
|
|
|
if (fs_free) QSE_AWK_FREE (rtx->awk, fs_free);
|
|
|
|
if (fs_rex_free)
|
|
|
|
{
|
|
|
|
if (rtx->gbl.ignorecase)
|
|
|
|
qse_awk_freerex (rtx->awk, QSE_NULL, fs_rex_free);
|
|
|
|
else
|
|
|
|
qse_awk_freerex (rtx->awk, fs_rex_free, QSE_NULL);
|
|
|
|
}
|
2011-05-19 20:50:51 +00:00
|
|
|
return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 14:13:12 +00:00
|
|
|
int qse_awk_fnc_tolower (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_size_t i;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_t* a0, * r;
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_xstr_t str;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs == 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
str.ptr = qse_awk_rtx_getvalstr (rtx, a0, &str.len);
|
|
|
|
if (str.ptr == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
for (i = 0; i < str.len; i++) str.ptr[i] = QSE_AWK_TOLOWER (rtx->awk, str.ptr[i]);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
r = qse_awk_rtx_makestrvalwithxstr (rtx, &str);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (r == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str.ptr);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str.ptr);
|
|
|
|
qse_awk_rtx_setretval (rtx, r);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-27 14:13:12 +00:00
|
|
|
int qse_awk_fnc_toupper (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_size_t i;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_t* a0, * r;
|
2012-10-29 14:41:39 +00:00
|
|
|
qse_xstr_t str;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs == 1);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
str.ptr = qse_awk_rtx_getvalstr (rtx, a0, &str.len);
|
|
|
|
if (str.ptr == QSE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
for (i = 0; i < str.len; i++) str.ptr[i] = QSE_AWK_TOUPPER (rtx->awk, str.ptr[i]);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
r = qse_awk_rtx_makestrvalwithxstr (rtx, &str);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (r == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str.ptr);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str.ptr);
|
|
|
|
qse_awk_rtx_setretval (rtx, r);
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_awk_val_t* a0, * a1, * a2, * v;
|
2011-05-19 08:36:40 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t s0, s1, s2;
|
2011-05-19 08:36:40 +00:00
|
|
|
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;
|
2013-08-23 15:19:29 +00:00
|
|
|
int new_inited = 0;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t mat, pmat, cur;
|
2011-05-19 20:50:51 +00:00
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_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
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
rex = ((qse_awk_val_rex_t*)a0)->code[run->gbl.ignorecase];
|
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
|
|
|
|
{
|
2012-10-31 08:31:58 +00:00
|
|
|
s0.ptr = qse_awk_rtx_valtostrdup (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
|
|
|
|
{
|
2012-10-31 08:31:58 +00:00
|
|
|
s1.ptr = qse_awk_rtx_valtostrdup (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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
s2.ptr = qse_awk_rtx_valtostrdup (run, a2, &s2.len);
|
|
|
|
if (s2.ptr == QSE_NULL) goto oops;
|
|
|
|
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;
|
2013-08-23 15:19:29 +00:00
|
|
|
int x;
|
|
|
|
|
|
|
|
if (run->gbl.ignorecase)
|
|
|
|
x = qse_awk_buildrex (run->awk, s0.ptr, s0.len, &errnum, QSE_NULL, &rex);
|
|
|
|
else
|
|
|
|
x = qse_awk_buildrex (run->awk, s0.ptr, s0.len, &errnum, &rex, QSE_NULL);
|
2009-09-16 08:03:15 +00:00
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
if (x <= -1)
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2013-04-06 13:39:56 +00:00
|
|
|
n = qse_awk_matchrex (
|
2013-08-23 15:19:29 +00:00
|
|
|
run->awk, rex, run->gbl.ignorecase,
|
|
|
|
&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)
|
|
|
|
{
|
2013-08-23 15:19:29 +00:00
|
|
|
if (run->gbl.ignorecase)
|
|
|
|
qse_awk_freerex (run->awk, QSE_NULL, rex_free);
|
|
|
|
else
|
|
|
|
qse_awk_freerex (run->awk, rex_free, QSE_NULL);
|
2011-05-19 20:50:51 +00:00
|
|
|
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;
|
2014-07-02 12:28:58 +00:00
|
|
|
n = qse_awk_rtx_setrec (run, 0, QSE_STR_XSTR(&new));
|
2011-05-19 20:50:51 +00:00
|
|
|
if (n <= -1) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2013-04-16 10:09:24 +00:00
|
|
|
else
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
v = qse_awk_rtx_makestrvalwithxstr (run, QSE_STR_XSTR(&new));
|
2011-05-19 20:50:51 +00:00
|
|
|
if (v == QSE_NULL) goto oops;
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_refupval (run, v);
|
2013-08-23 15:19:29 +00:00
|
|
|
n = qse_awk_rtx_setrefval (run, (qse_awk_val_ref_t*)a2, v);
|
2013-04-16 10:09:24 +00:00
|
|
|
qse_awk_rtx_refdownval (run, v);
|
|
|
|
if (n <= -1) goto oops;
|
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:
|
2013-08-23 15:19:29 +00:00
|
|
|
if (rex_free)
|
|
|
|
{
|
|
|
|
if (run->gbl.ignorecase)
|
|
|
|
qse_awk_freerex (run->awk, QSE_NULL, rex_free);
|
|
|
|
else
|
|
|
|
qse_awk_freerex (run->awk, rex_free, QSE_NULL);
|
|
|
|
}
|
2011-05-19 20:50:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-05-27 15:28:31 +00:00
|
|
|
int qse_awk_fnc_gsub (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
return __substitute (run, 0);
|
|
|
|
}
|
|
|
|
|
2014-05-27 15:28:31 +00:00
|
|
|
int qse_awk_fnc_sub (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
return __substitute (run, 1);
|
|
|
|
}
|
|
|
|
|
2014-05-27 15:28:31 +00:00
|
|
|
int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
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;
|
2013-08-23 15:19:29 +00:00
|
|
|
qse_char_t* str0;
|
|
|
|
qse_size_t len0;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t idx, start = 1;
|
2009-09-03 06:55:55 +00:00
|
|
|
int n;
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t mat;
|
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 */
|
2013-11-05 14:16:45 +00:00
|
|
|
n = qse_awk_rtx_valtoint (rtx, a2, &start);
|
2009-12-11 07:03:54 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
}
|
2009-09-16 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
str0 = qse_awk_rtx_getvalstr (rtx, a0, &len0);
|
|
|
|
if (str0 == QSE_NULL) return -1;
|
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
|
|
|
|
{
|
2014-07-02 12:28:58 +00:00
|
|
|
qse_xstr_t tmp;
|
2011-05-19 08:36:40 +00:00
|
|
|
|
2013-08-23 15:19:29 +00:00
|
|
|
/*TODO: must use str0,len0? */
|
2011-05-19 08:36:40 +00:00
|
|
|
tmp.ptr = str0 + start - 1;
|
|
|
|
tmp.len = len0 - start + 1;
|
2013-08-23 15:19:29 +00:00
|
|
|
|
|
|
|
n = qse_awk_rtx_matchrex (rtx, a1, &tmp, &tmp, &mat);
|
|
|
|
if (n <= -1) return -1;
|
2009-09-16 08:03:15 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, str0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
idx = (n == 0)? 0: ((qse_awk_int_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,
|
2013-11-05 14:16:45 +00:00
|
|
|
((n == 0)? (qse_awk_int_t)-1: (qse_awk_int_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;
|
|
|
|
}
|
|
|
|
|
2014-05-27 15:28:31 +00:00
|
|
|
int qse_awk_fnc_sprintf (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
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
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (rtx);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs > 0);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
if (qse_str_init (&out, rtx->awk->mmgr, 256) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, 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;
|
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
if (qse_str_init (&fbu, rtx->awk->mmgr, 256) <= -1)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, 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
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (rtx, 0);
|
|
|
|
cs0.ptr = qse_awk_rtx_getvalstr (rtx, a0, &cs0.len);
|
|
|
|
if (cs0.ptr == QSE_NULL) goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2014-02-19 15:24:33 +00:00
|
|
|
x.ptr = qse_awk_rtx_format (rtx,
|
2008-12-21 21:35:07 +00:00
|
|
|
&out, &fbu, cs0.ptr, cs0.len, nargs, QSE_NULL, &x.len);
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_freevalstr (rtx, a0, 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
|
|
|
|
2014-07-02 12:28:58 +00:00
|
|
|
a0 = qse_awk_rtx_makestrvalwithxstr (rtx, &x);
|
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_fini (&out);
|
2014-02-19 15:24:33 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, 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
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
static int fnc_int (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi)
|
2011-05-18 20:32:39 +00:00
|
|
|
{
|
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t lv;
|
2011-05-18 20:32:39 +00:00
|
|
|
qse_awk_val_t* r;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
|
|
|
QSE_ASSERT (nargs == 1);
|
|
|
|
|
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
n = qse_awk_rtx_valtoint (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;
|
|
|
|
}
|