qse/samples/awk/awk06.c

144 lines
3.3 KiB
C
Raw Normal View History

#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"
2011-05-13 10:16:57 +00:00
static const qse_char_t* src = QSE_T(
2013-01-12 16:46:12 +00:00
"function pow(x,y) { return x ** y; }"
2011-05-13 10:16:57 +00:00
);
2013-01-12 16:46:12 +00:00
static int awk_main (int argc, qse_char_t* argv[])
2011-05-13 10:16:57 +00:00
{
qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL;
qse_awk_parsestd_t psin[2];
2013-01-12 16:46:12 +00:00
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
qse_awk_fun_t* fun;
2013-01-09 08:03:21 +00:00
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);
2011-05-13 10:16:57 +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;
2011-05-13 10:16:57 +00:00
}
2013-01-09 08:03:21 +00:00
/* get the awk's trait */
qse_awk_getopt (awk, QSE_AWK_TRAIT, &opt);
2013-01-09 08:03:21 +00:00
/* change the trait value to disallow BEGIN, END, pattern-action blocks */
2011-05-13 10:16:57 +00:00
opt &= ~QSE_AWK_PABLOCK;
2013-01-09 08:03:21 +00:00
/* update the trait */
qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);
2011-05-13 10:16:57 +00:00
2013-01-12 16:46:12 +00:00
/* 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;
2011-05-13 10:16:57 +00:00
2013-01-09 08:03:21 +00:00
/* parse the script */
2013-01-14 16:02:04 +00:00
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
2011-05-13 10:16:57 +00:00
{
2013-01-14 16:02:04 +00:00
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
2011-05-13 10:16:57 +00:00
goto oops;
}
2013-01-12 16:46:12 +00:00
/* create a runtime context */
2011-05-13 10:16:57 +00:00
rtx = qse_awk_rtx_openstd (
awk,
0,
2013-01-12 16:46:12 +00:00
QSE_T("awk05"),
2011-05-13 10:16:57 +00:00
QSE_NULL, /* stdin */
QSE_NULL, /* stdout */
QSE_NULL /* default cmgr */
2011-05-13 10:16:57 +00:00
);
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;
2011-05-13 10:16:57 +00:00
}
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)
2011-05-13 10:16:57 +00:00
{
2013-01-14 16:02:04 +00:00
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
goto oops;
2011-05-13 10:16:57 +00:00
}
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)
2011-05-13 10:16:57 +00:00
{
2013-01-14 16:02:04 +00:00
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
goto oops;
2011-05-13 10:16:57 +00:00
}
2013-01-12 16:46:12 +00:00
qse_awk_rtx_refupval (rtx, arg[1]);
2011-05-13 10:16:57 +00:00
2013-01-12 16:46:12 +00:00
/* find the pow function */
fun = qse_awk_rtx_findfun (rtx, QSE_T("pow"));
if (fun == QSE_NULL)
2011-05-13 10:16:57 +00:00
{
2013-01-14 16:02:04 +00:00
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
goto oops;
2011-05-13 10:16:57 +00:00
}
2013-01-12 16:46:12 +00:00
/* call the function found */
rtv = qse_awk_rtx_callfun (rtx, fun, arg, 2);
if (rtv == QSE_NULL)
2011-05-13 10:16:57 +00:00
{
2013-01-14 16:02:04 +00:00
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
goto oops;
2011-05-13 10:16:57 +00:00
}
2013-01-12 16:46:12 +00:00
/* duplicate the return value to a string */
str = qse_awk_rtx_valtostrdup (rtx, rtv, &len);
2011-05-13 10:16:57 +00:00
/* clear the return value */
2013-01-12 16:46:12 +00:00
qse_awk_rtx_refdownval (rtx, rtv);
2011-05-13 10:16:57 +00:00
2013-01-12 16:46:12 +00:00
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]);
}
2011-05-13 10:16:57 +00:00
/* destroy a runtime context */
if (rtx) qse_awk_rtx_close (rtx);
2013-01-14 16:02:04 +00:00
2011-05-13 10:16:57 +00:00
/* destroy the processor */
if (awk) qse_awk_close (awk);
2013-01-09 08:03:21 +00:00
2011-05-13 10:16:57 +00:00
return ret;
}
2013-01-12 16:46:12 +00:00
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 ();
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
}