qse/qse/samples/awk/awk01.c

93 lines
2.1 KiB
C
Raw Normal View History

2009-02-17 23:19:13 +00:00
/*
2009-09-16 04:01:02 +00:00
* $Id: awk01.c 287 2009-09-15 10:01:02Z hyunghwan.chung $
2009-02-17 23:19:13 +00:00
*
2009-09-16 04:01:02 +00:00
Copyright 2006-2009 Chung, Hyung-Hwan.
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:00:01 +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:00:01 +00:00
const qse_char_t* src = QSE_T(
"BEGIN {"
" for (i=2;i<=9;i++)"
" {"
" for (j=1;j<=9;j++)"
" print i \"*\" j \"=\" i * j;"
" print \"---------------------\";"
" }"
"}"
);
int main ()
{
qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL;
qse_awk_val_t* retv;
qse_awk_parsestd_in_t psin;
2009-02-17 08:00:01 +00:00
int ret;
awk = qse_awk_openstd (0);
2009-02-17 08:00:01 +00:00
if (awk == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
2009-02-17 08:00:01 +00:00
goto oops;
}
psin.type = QSE_AWK_PARSESTD_CP;
psin.u.cp = src;
2009-02-22 08:16:35 +00:00
ret = qse_awk_parsestd (awk, &psin, QSE_NULL);
if (ret <= -1)
2009-02-17 08:00:01 +00:00
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
2009-02-17 08:00:01 +00:00
qse_awk_geterrmsg(awk));
ret = -1; goto oops;
2009-02-17 08:00:01 +00:00
}
rtx = qse_awk_rtx_openstd (
awk,
0,
QSE_T("awk01"),
QSE_NULL, /* stdin */
QSE_NULL /* stdout */
2009-02-17 08:00:01 +00:00
);
if (rtx == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
2009-02-17 08:00:01 +00:00
qse_awk_geterrmsg(awk));
ret = -1; goto oops;
2009-02-17 08:00:01 +00:00
}
retv = qse_awk_rtx_loop (rtx);
if (retv == QSE_NULL)
2009-02-17 08:00:01 +00:00
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
2009-02-17 08:00:01 +00:00
qse_awk_rtx_geterrmsg(rtx));
ret = -1; goto oops;
2009-02-17 08:00:01 +00:00
}
qse_awk_rtx_refdownval (rtx, retv);
ret = 0;
2009-02-17 08:00:01 +00:00
oops:
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
if (awk != QSE_NULL) qse_awk_close (awk);
return ret;
2009-02-17 08:00:01 +00:00
}