2013-02-22 03:18:42 +00:00
|
|
|
#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;
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_awk_val_t* retv;
|
2013-01-08 05:51:58 +00:00
|
|
|
qse_awk_parsestd_t psin[2];
|
2009-12-15 06:50:11 +00:00
|
|
|
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)
|
|
|
|
{
|
2009-07-17 02:27:53 +00:00
|
|
|
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 */
|
2013-01-08 05:51:58 +00:00
|
|
|
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 */
|
2013-01-08 05:51:58 +00:00
|
|
|
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
|
2009-02-17 08:00:01 +00:00
|
|
|
{
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
|
2009-02-17 08:00:01 +00:00
|
|
|
qse_awk_geterrmsg(awk));
|
2009-12-15 06:50:11 +00:00
|
|
|
goto oops;
|
2009-02-17 08:00:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:08:22 +00:00
|
|
|
/* open 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("awk01"),
|
|
|
|
QSE_NULL, /* stdin */
|
2013-01-07 15:08:22 +00:00
|
|
|
QSE_NULL, /* stdout */
|
2012-01-15 15:25:28 +00:00
|
|
|
QSE_NULL /* default cmgr */
|
2009-02-17 08:00:01 +00:00
|
|
|
);
|
|
|
|
if (rtx == QSE_NULL)
|
|
|
|
{
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
|
2009-02-17 08:00:01 +00:00
|
|
|
qse_awk_geterrmsg(awk));
|
2009-12-15 06:50:11 +00:00
|
|
|
goto oops;
|
2009-02-17 08:00:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-07 15:08:22 +00:00
|
|
|
/* execute pattern-action blocks */
|
2009-07-17 02:27:53 +00:00
|
|
|
retv = qse_awk_rtx_loop (rtx);
|
|
|
|
if (retv == QSE_NULL)
|
2009-02-17 08:00:01 +00:00
|
|
|
{
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
|
2009-02-17 08:00:01 +00:00
|
|
|
qse_awk_rtx_geterrmsg(rtx));
|
2009-12-15 06:50:11 +00:00
|
|
|
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 */
|
2009-07-17 02:27:53 +00:00
|
|
|
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 ();
|
2009-07-17 02:27:53 +00:00
|
|
|
return ret;
|
2009-02-17 08:00:01 +00:00
|
|
|
}
|