qse/samples/awk/awk01.c

77 lines
1.6 KiB
C
Raw Normal View History

#include <qse/awk/stdawk.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
2009-02-17 08:00:01 +00:00
2013-01-07 15:08:22 +00:00
static const qse_char_t* script = QSE_T("BEGIN { print \"hello, world\"; }");
2009-02-17 08:00:01 +00:00
int main ()
{
qse_awk_t* awk = QSE_NULL;
qse_awk_rtx_t* rtx = QSE_NULL;
qse_awk_val_t* retv;
qse_awk_parsestd_t psin[2];
int ret = -1;
2009-02-17 08:00:01 +00:00
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
2014-03-03 15:36:30 +00:00
2013-01-07 15:08:22 +00:00
/* create an awk object */
2014-07-11 14:17:00 +00:00
awk = qse_awk_openstd (0, QSE_NULL);
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;
}
2013-01-08 15:56:56 +00:00
/* prepare a script to parse */
psin[0].type = QSE_AWK_PARSESTD_STR;
psin[0].u.str.ptr = script;
psin[0].u.str.len = qse_strlen(script);
psin[1].type = QSE_AWK_PARSESTD_NULL;
2009-02-22 08:16:35 +00:00
2013-01-07 15:08:22 +00:00
/* parse a script in a string */
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -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));
goto oops;
2009-02-17 08:00:01 +00:00
}
2013-01-07 15:08:22 +00:00
/* open a runtime context */
rtx = qse_awk_rtx_openstd (
awk,
0,
QSE_T("awk01"),
QSE_NULL, /* stdin */
2013-01-07 15:08:22 +00:00
QSE_NULL, /* stdout */
QSE_NULL /* default cmgr */
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));
goto oops;
2009-02-17 08:00:01 +00:00
}
2013-01-07 15:08:22 +00:00
/* execute pattern-action blocks */
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));
goto oops;
2009-02-17 08:00:01 +00:00
}
2013-01-07 15:08:22 +00:00
/* decrement the reference count of the return value */
qse_awk_rtx_refdownval (rtx, retv);
ret = 0;
2009-02-17 08:00:01 +00:00
oops:
2013-01-07 15:08:22 +00:00
/* destroy the runtime context */
if (rtx) qse_awk_rtx_close (rtx);
/* destroy the awk object */
if (awk) qse_awk_close (awk);
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
return ret;
2009-02-17 08:00:01 +00:00
}