2013-02-22 03:18:42 +00:00
|
|
|
#include <qse/awk/stdawk.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2013-01-12 16:46:12 +00:00
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include "awk00.h"
|
|
|
|
|
|
|
|
static const qse_char_t* src = QSE_T(
|
|
|
|
"function pow(x,y) { return x ** y; }"
|
|
|
|
);
|
|
|
|
|
|
|
|
static int awk_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
|
|
|
qse_awk_t* awk = QSE_NULL;
|
|
|
|
qse_awk_rtx_t* rtx = QSE_NULL;
|
|
|
|
qse_awk_parsestd_t psin[2];
|
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len;
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_awk_val_t* rtv;
|
2013-01-12 16:46:12 +00:00
|
|
|
qse_awk_val_t* arg[2] = { QSE_NULL, QSE_NULL };
|
2013-01-14 16:02:04 +00:00
|
|
|
int ret = -1, i, opt;
|
2013-01-12 16:46:12 +00:00
|
|
|
|
|
|
|
/* create an awk object */
|
2014-07-11 14:17:00 +00:00
|
|
|
awk = qse_awk_openstd (0, QSE_NULL);
|
2013-01-12 16:46:12 +00:00
|
|
|
if (awk == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
|
|
|
|
goto oops;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the awk's trait */
|
|
|
|
qse_awk_getopt (awk, QSE_AWK_TRAIT, &opt);
|
|
|
|
/* change the trait value to disallow BEGIN, END, pattern-action blocks */
|
|
|
|
opt &= ~QSE_AWK_PABLOCK;
|
|
|
|
/* update the trait */
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);
|
|
|
|
|
|
|
|
/* prepare an awk script to parse */
|
|
|
|
psin[0].type = QSE_AWK_PARSESTD_STR;
|
|
|
|
psin[0].u.str.ptr = src;
|
|
|
|
psin[0].u.str.len = qse_strlen(src);
|
|
|
|
psin[1].type = QSE_AWK_PARSESTD_NULL;
|
|
|
|
|
|
|
|
/* parse the script */
|
2013-01-14 16:02:04 +00:00
|
|
|
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
|
2013-01-12 16:46:12 +00:00
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
|
2013-01-12 16:46:12 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a runtime context */
|
|
|
|
rtx = qse_awk_rtx_openstd (
|
|
|
|
awk,
|
|
|
|
0,
|
|
|
|
QSE_T("awk04"),
|
|
|
|
QSE_NULL, /* stdin */
|
|
|
|
QSE_NULL, /* stdout */
|
|
|
|
QSE_NULL /* default cmgr */
|
|
|
|
);
|
|
|
|
if (rtx == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
|
|
|
|
goto oops;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create the first argument to the pow function to call */
|
|
|
|
arg[0] = qse_awk_rtx_makeintval (rtx, 50);
|
|
|
|
if (arg[0] == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
|
|
|
|
goto oops;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
qse_awk_rtx_refupval (rtx, arg[0]);
|
|
|
|
|
|
|
|
/* create the second argument to the pow function to call */
|
|
|
|
arg[1] = qse_awk_rtx_makeintval (rtx, 3);
|
|
|
|
if (arg[1] == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
|
|
|
|
goto oops;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
qse_awk_rtx_refupval (rtx, arg[1]);
|
|
|
|
|
|
|
|
/* call the pow function */
|
|
|
|
rtv = qse_awk_rtx_call (rtx, QSE_T("pow"), arg, 2);
|
|
|
|
if (rtv == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
|
|
|
|
goto oops;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* duplicate the return value to a string */
|
|
|
|
str = qse_awk_rtx_valtostrdup (rtx, rtv, &len);
|
|
|
|
|
|
|
|
/* clear the return value */
|
|
|
|
qse_awk_rtx_refdownval (rtx, rtv);
|
|
|
|
|
|
|
|
if (str == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
|
|
|
|
goto oops;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qse_printf (QSE_T("[%.*s]\n"), (int)len, str);
|
|
|
|
|
|
|
|
/* destroy the duplicated string */
|
|
|
|
qse_awk_rtx_freemem (rtx, str);
|
|
|
|
|
2013-01-14 16:02:04 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
oops:
|
|
|
|
/* dereference all arguments */
|
|
|
|
for (i = 0; i < QSE_COUNTOF(arg); i++)
|
|
|
|
{
|
|
|
|
if (arg[i]) qse_awk_rtx_refdownval (rtx, arg[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy a runtime context */
|
|
|
|
if (rtx) qse_awk_rtx_close (rtx);
|
2013-01-14 16:02:04 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* destroy the processor */
|
|
|
|
if (awk) qse_awk_close (awk);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
2014-03-03 15:36:30 +00:00
|
|
|
int x;
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2013-01-12 16:46:12 +00:00
|
|
|
init_awk_sample_locale ();
|
2018-09-13 03:16:23 +00:00
|
|
|
x = qse_run_main (argc, argv, awk_main);
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2014-03-03 15:36:30 +00:00
|
|
|
return x;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
|
|
|
|