qse/samples/awk/awk04.c

129 lines
2.8 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"
static const qse_char_t* src = QSE_T(
2013-01-12 16:46:12 +00:00
"function init() { a = 20; return a; }"
"function main() { return ++a; }"
"function fini() { print \"a in fini() =>\", ++a; return a; }"
);
2013-01-12 16:46:12 +00:00
static const qse_char_t* fnc[] =
{
QSE_T("init"),
QSE_T("main"),
QSE_T("main"),
QSE_T("main"),
QSE_T("main"),
QSE_T("fini"),
};
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];
2013-01-14 16:02:04 +00:00
int ret = -1, i, opt;
2013-01-08 15:56:56 +00:00
/* create an awk object */
2014-07-11 14:17:00 +00:00
awk = qse_awk_openstd (0, QSE_NULL);
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-08 15:56:56 +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 */
2009-08-10 21:29:59 +00:00
opt &= ~QSE_AWK_PABLOCK;
2013-01-08 15:56:56 +00:00
/* update the trait */
qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);
2013-01-12 16:46:12 +00:00
/* prepare a 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;
2013-01-12 16:46:12 +00:00
/* parse a script */
2013-01-14 16:02:04 +00:00
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
{
2013-01-14 16:02:04 +00:00
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
/* create a runtime context */
rtx = qse_awk_rtx_openstd (
2013-01-12 16:46:12 +00:00
awk,
0,
2013-01-12 16:46:12 +00:00
QSE_T("awk03"),
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
/* call init() initially, followed by 4 calls to main(),
* and a final call to fini() */
for (i = 0; i < QSE_COUNTOF(fnc); i++)
{
2013-01-12 16:46:12 +00:00
qse_awk_val_t* v;
qse_char_t* str;
qse_size_t len;
/* call the function */
v = qse_awk_rtx_call (rtx, fnc[i], QSE_NULL, 0);
if (v == 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
}
/* convert the return value to a string with duplication */
str = qse_awk_rtx_valtostrdup (rtx, v, &len);
/* clear the return value */
qse_awk_rtx_refdownval (rtx, v);
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
}
/* print the return value */
qse_printf (QSE_T("return: [%.*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;
2011-05-13 10:16:57 +00:00
oops:
/* destroy a runtime context */
2011-05-13 10:16:57 +00:00
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 awk object */
2011-05-13 10:16:57 +00:00
if (awk) qse_awk_close (awk);
2013-01-08 15:56:56 +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;
2013-01-12 16:46:12 +00:00
init_awk_sample_locale ();
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
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
}