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
|
|
|
*
|
2012-07-20 04:13:39 +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>
|
2009-02-23 08:10:34 +00:00
|
|
|
#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
|
|
|
|
2009-02-17 21:27:03 +00:00
|
|
|
static const qse_char_t* src = QSE_T(
|
2009-02-23 08:10:34 +00:00
|
|
|
"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
|
|
|
|
2012-01-06 14:38:11 +00:00
|
|
|
qse_awk_parsestd_t psin;
|
2009-02-22 08:16:35 +00:00
|
|
|
|
2012-10-21 16:19:03 +00:00
|
|
|
int ret, i, opt;
|
2009-02-17 08:22:42 +00:00
|
|
|
|
2009-02-17 21:27:03 +00:00
|
|
|
/* create a main processor */
|
2009-02-27 04:56:12 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-21 16:19:03 +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 */
|
2012-10-21 16:19:03 +00:00
|
|
|
opt &= ~QSE_AWK_PABLOCK;
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_TRAIT, &opt);
|
2009-02-17 19:41:53 +00:00
|
|
|
|
2012-01-06 14:38:11 +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
|
|
|
|
2009-02-23 08:10:34 +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;
|
|
|
|
}
|
|
|
|
|
2009-02-17 21:27:03 +00:00
|
|
|
/* create a runtime context */
|
2009-02-23 08:10:34 +00:00
|
|
|
rtx = qse_awk_rtx_openstd (
|
2009-06-11 07:18:25 +00:00
|
|
|
awk,
|
|
|
|
0,
|
|
|
|
QSE_T("awk03"),
|
|
|
|
QSE_NULL, /* stdin */
|
2012-01-15 15:25:28 +00:00
|
|
|
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
|
|
|
{
|
2009-02-23 08:10:34 +00:00
|
|
|
qse_awk_val_t* v;
|
2009-03-01 09:27:03 +00:00
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len;
|
2009-02-23 08:10:34 +00:00
|
|
|
|
|
|
|
v = qse_awk_rtx_call (rtx, fnc[i], QSE_NULL, 0);
|
|
|
|
if (v == QSE_NULL)
|
2009-02-17 19:41:53 +00:00
|
|
|
{
|
2009-02-17 21:27:03 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
|
|
|
|
qse_awk_rtx_geterrmsg(rtx));
|
2009-02-23 08:10:34 +00:00
|
|
|
ret = -1; goto oops;
|
2009-02-17 19:41:53 +00:00
|
|
|
}
|
2009-02-23 08:10:34 +00:00
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
str = qse_awk_rtx_valtostrdup (rtx, v, &len);
|
2009-03-01 09:27:03 +00:00
|
|
|
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);
|
2009-03-01 09:27:03 +00:00
|
|
|
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);
|
2009-02-23 08:10:34 +00:00
|
|
|
|
|
|
|
/* 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:
|
2009-02-17 21:27:03 +00:00
|
|
|
/* destroy a runtime context */
|
2009-02-17 08:22:42 +00:00
|
|
|
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
|
2009-02-17 21:27:03 +00:00
|
|
|
/* 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
|
|
|
}
|