qse/qse/test/awk/awk01.c

85 lines
1.9 KiB
C
Raw Normal View History

2009-02-17 23:19:13 +00:00
/*
2009-05-22 00:50:02 +00:00
* $Id: awk01.c 151 2009-05-21 06:50:02Z hyunghwan.chung $
2009-02-17 23:19:13 +00:00
*
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.
*/
2009-02-17 08:00:01 +00:00
#include <qse/awk/awk.h>
#include <qse/awk/std.h>
2009-02-17 08:00:01 +00:00
#include <qse/utl/stdio.h>
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_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"));
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);
2009-02-17 08:00:01 +00:00
if (ret == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
rtx = qse_awk_rtx_openstd (
awk, 0,
QSE_NULL, /* no console input */
QSE_AWK_RTX_OPENSTD_STDIO /* stdout for console output */
2009-02-17 08:00:01 +00:00
);
if (rtx == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
ret = qse_awk_rtx_loop (rtx);
if (ret == -1)
{
qse_fprintf (QSE_STDERR, QSE_T("error: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
goto oops;
}
oops:
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
if (awk != QSE_NULL) qse_awk_close (awk);
return -1;
}