qse/qse/test/awk/awk03.c

110 lines
2.6 KiB
C
Raw Normal View History

2009-02-17 23:19:13 +00:00
/*
* $Id$
*
Copyright 2006-2009 Chung, Hyung-Hwan.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/****S* AWK/Calling Functions
2009-02-17 19:41:53 +00:00
* DESCRIPTION
* This program demonstrates how to use qse_awk_rtx_call().
2009-02-17 23:19:13 +00:00
* It parses the program stored in the string src and calls the functions
* stated in the array fnc. If no errors occur, it should print 24.
2009-02-17 19:41:53 +00:00
* SOURCE
*/
2009-02-17 08:22:42 +00:00
#include <qse/awk/awk.h>
#include <qse/utl/stdio.h>
static const qse_char_t* src = QSE_T(
2009-02-17 08:22:42 +00:00
"function init() { a = 20; }"
"function main() { a++; }"
2009-02-17 19:41:53 +00:00
"function fini() { print 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_parsesimple_in_t psin;
2009-02-17 19:41:53 +00:00
int ret, i;
2009-02-17 08:22:42 +00:00
/* create a main processor */
2009-02-17 08:22:42 +00:00
awk = qse_awk_opensimple ();
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
}
2009-02-17 19:41:53 +00:00
/* don't allow BEGIN, END, pattern-action blocks */
qse_awk_setoption (awk, qse_awk_getoption(awk) & ~QSE_AWK_PABLOCK);
2009-02-22 08:16:35 +00:00
psin.type = QSE_AWK_PARSESIMPLE_STR;
psin.u.str = src;
ret = qse_awk_parsesimple (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 */
2009-02-17 08:22:42 +00:00
rtx = qse_awk_rtx_opensimple (
awk,
2009-02-22 06:28:02 +00:00
QSE_NULL, /* no console input */
QSE_AWK_RTX_OPENSIMPLE_STDIO /* stdout for console output */
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-17 23:19:13 +00:00
ret = qse_awk_rtx_call (rtx, fnc[i], QSE_NULL, 0);
2009-02-17 19:41:53 +00:00
if (ret == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
2009-02-17 19:41:53 +00:00
goto oops;
}
}
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
}
2009-02-17 19:41:53 +00:00
/******/