qse/qse/samples/awk/awk03.c

130 lines
3.0 KiB
C
Raw Normal View History

2009-02-17 23:19:13 +00:00
/*
2011-07-26 09:42:35 +00:00
* $Id: awk03.c 523 2011-07-25 15:42:35Z hyunghwan.chung $
2009-02-17 23:19:13 +00:00
*
Copyright 2006-2012 Chung, Hyung-Hwan.
2009-09-16 04:01:02 +00:00
This file is part of QSE.
2009-02-17 23:19:13 +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.
2009-02-17 23:19:13 +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.
2009-02-17 23:19:13 +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/>.
2009-02-17 23:19:13 +00:00
*/
2009-02-17 08:22:42 +00:00
#include <qse/awk/awk.h>
#include <qse/awk/std.h>
2009-06-04 15:50:32 +00:00
#include <qse/cmn/stdio.h>
2009-02-17 08:22:42 +00:00
static const qse_char_t* src = QSE_T(
"function init() { a = 20; return a; }"
"function main() { return ++a; }"
2011-05-26 08:42:26 +00:00
"function fini() { print \"a in fini() =>\", ++a; return a; }"
2009-02-17 08:22:42 +00:00
);
2009-02-17 23:19:13 +00:00
static const qse_char_t* fnc[] =
2009-02-17 19:41:53 +00:00
{
QSE_T("init"),
QSE_T("main"),
QSE_T("main"),
QSE_T("main"),
QSE_T("main"),
QSE_T("fini"),
};
2009-02-17 08:22:42 +00:00
int main ()
{
qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL;
2009-02-22 08:16:35 +00:00
qse_awk_parsestd_t psin;
2009-02-22 08:16:35 +00:00
int ret, i, opt;
2009-02-17 08:22:42 +00:00
/* create a main processor */
awk = qse_awk_openstd (0);
2009-02-17 08:22:42 +00:00
if (awk == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: cannot open awk\n"));
2009-02-17 19:41:53 +00:00
ret = -1; goto oops;
2009-02-17 08:22:42 +00:00
}
qse_awk_getopt (awk, QSE_AWK_TRAIT, &opt);
2009-02-17 19:41:53 +00:00
/* don't allow BEGIN, END, pattern-action blocks */
opt &= ~QSE_AWK_PABLOCK;
qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);
2009-02-17 19:41:53 +00:00
psin.type = QSE_AWK_PARSESTD_STR;
psin.u.str.ptr = src;
psin.u.str.len = qse_strlen(src);
2009-02-22 08:16:35 +00:00
ret = qse_awk_parsestd (awk, &psin, QSE_NULL);
2009-02-17 08:22:42 +00:00
if (ret == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
/* create a runtime context */
rtx = qse_awk_rtx_openstd (
awk,
0,
QSE_T("awk03"),
QSE_NULL, /* stdin */
QSE_NULL, /* stdout */
QSE_NULL /* default cmgr */
2009-02-17 08:22:42 +00:00
);
if (rtx == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_geterrmsg(awk));
2009-02-17 19:41:53 +00:00
ret = -1; goto oops;
2009-02-17 08:22:42 +00:00
}
2009-02-17 23:19:13 +00:00
/* invoke functions as indicated in the array fnc */
for (i = 0; i < QSE_COUNTOF(fnc); i++)
2009-02-17 19:41:53 +00:00
{
qse_awk_val_t* v;
qse_char_t* str;
qse_size_t len;
v = qse_awk_rtx_call (rtx, fnc[i], QSE_NULL, 0);
if (v == QSE_NULL)
2009-02-17 19:41:53 +00:00
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
ret = -1; goto oops;
2009-02-17 19:41:53 +00:00
}
str = qse_awk_rtx_valtostrdup (rtx, v, &len);
if (str == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
2011-05-13 10:16:57 +00:00
qse_awk_rtx_refdownval (rtx, v);
ret = -1; goto oops;
}
qse_printf (QSE_T("return: [%.*s]\n"), (int)len, str);
2011-07-26 09:42:35 +00:00
qse_awk_rtx_freemem (rtx, str);
/* clear the return value */
qse_awk_rtx_refdownval (rtx, v);
2009-02-17 19:41:53 +00:00
}
2009-02-17 08:22:42 +00:00
oops:
/* destroy a runtime context */
2009-02-17 08:22:42 +00:00
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
/* destroy the processor */
2009-02-17 08:22:42 +00:00
if (awk != QSE_NULL) qse_awk_close (awk);
2009-02-17 19:41:53 +00:00
return ret;
2009-02-17 08:22:42 +00:00
}