2013-02-22 03:18:42 +00:00
|
|
|
#include <qse/awk/stdawk.h>
|
2013-01-12 16:46:12 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2013-01-12 16:46:12 +00:00
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include "awk00.h"
|
2009-02-17 08:22:42 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* i'll print records with the second field grater than 4.
|
|
|
|
* at the end, we'll print the number of records seen so far */
|
|
|
|
static const qse_char_t* script =
|
|
|
|
QSE_T("$2 > 4 { print $0; } END { print NR; }");
|
2009-02-17 08:22:42 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
struct console_t
|
2009-02-17 19:41:53 +00:00
|
|
|
{
|
2013-01-12 16:46:12 +00:00
|
|
|
/* console input */
|
|
|
|
const qse_char_t* conin;
|
|
|
|
qse_size_t coninpos;
|
|
|
|
|
|
|
|
/* console output */
|
|
|
|
qse_char_t conout[10000]; /* fixed-size console buffer for demo only */
|
|
|
|
qse_size_t conoutpos;
|
2009-02-17 19:41:53 +00:00
|
|
|
};
|
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
typedef struct console_t console_t;
|
|
|
|
|
|
|
|
/* this is the console I/O handler */
|
|
|
|
static qse_ssize_t handle_console (
|
|
|
|
qse_awk_rtx_t* rtx,
|
|
|
|
qse_awk_rio_cmd_t cmd,
|
|
|
|
qse_awk_rio_arg_t* arg,
|
|
|
|
qse_char_t* data,
|
|
|
|
qse_size_t count)
|
|
|
|
{
|
2019-06-24 08:53:49 +00:00
|
|
|
console_t* con = qse_awk_rtx_getxtn(rtx);
|
2013-01-12 16:46:12 +00:00
|
|
|
|
|
|
|
/* this function is called separately for the console input and console
|
|
|
|
* output. however, since i don't maintain underlying resources like
|
|
|
|
* file handles, i don't really check if it's input or output.
|
|
|
|
* you can check the value of #qse_awk_rio_mode_t in the arg->mode
|
|
|
|
* field if you want to tell.
|
|
|
|
*/
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case QSE_AWK_RIO_OPEN:
|
|
|
|
/* 0 for success, -1 for failure. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case QSE_AWK_RIO_CLOSE:
|
|
|
|
/* 0 for success, -1 for failure. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case QSE_AWK_RIO_READ:
|
|
|
|
{
|
|
|
|
qse_ssize_t len = 0;
|
|
|
|
|
|
|
|
while (con->conin[con->coninpos] && len < count)
|
|
|
|
data[len++] = con->conin[con->coninpos++];
|
|
|
|
|
|
|
|
/* 0 for EOF, -1 for failure.
|
|
|
|
positive numbers for the number of characters read */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_AWK_RIO_WRITE:
|
|
|
|
con->conoutpos += qse_strxncpy (
|
|
|
|
&con->conout[con->conoutpos], QSE_COUNTOF(con->conout) - con->conoutpos,
|
|
|
|
data, count);
|
|
|
|
/* 0 for EOF, -1 for failure.
|
|
|
|
positive numbers for the number of characters written */
|
|
|
|
return count;
|
|
|
|
|
|
|
|
case QSE_AWK_RIO_FLUSH:
|
|
|
|
/* 0 for success, -1 for failure. */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case QSE_AWK_RIO_NEXT:
|
|
|
|
/* 0 for success, -1 for failure. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this part will never be reached */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int awk_main (int argc, qse_char_t* argv[])
|
2009-02-17 08:22:42 +00:00
|
|
|
{
|
|
|
|
qse_awk_t* awk = QSE_NULL;
|
|
|
|
qse_awk_rtx_t* rtx = QSE_NULL;
|
2013-01-12 16:46:12 +00:00
|
|
|
qse_awk_val_t* retv;
|
2013-01-08 05:51:58 +00:00
|
|
|
qse_awk_parsestd_t psin[2];
|
2013-01-12 16:46:12 +00:00
|
|
|
qse_awk_rio_t rio;
|
|
|
|
int ret = -1;
|
|
|
|
console_t* con;
|
2009-02-17 08:22:42 +00:00
|
|
|
|
2013-01-08 15:56:56 +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:22:42 +00:00
|
|
|
if (awk == QSE_NULL)
|
|
|
|
{
|
2013-01-12 16:46:12 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
|
|
|
|
goto oops;
|
2009-02-17 08:22:42 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2013-01-12 16:46:12 +00:00
|
|
|
psin[0].u.str.ptr = script;
|
|
|
|
psin[0].u.str.len = qse_strlen(script);
|
2013-01-08 05:51:58 +00:00
|
|
|
psin[1].type = QSE_AWK_PARSESTD_NULL;
|
2009-02-22 08:16:35 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* parse a script in a string */
|
|
|
|
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
|
2009-02-17 08:22:42 +00:00
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
|
2009-02-17 08:22:42 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* open a runtime context */
|
2009-02-23 08:10:34 +00:00
|
|
|
rtx = qse_awk_rtx_openstd (
|
2013-01-12 16:46:12 +00:00
|
|
|
awk,
|
|
|
|
QSE_SIZEOF(console_t), /* the size of extenstion area */
|
|
|
|
QSE_T("awk01"),
|
2009-06-11 07:18:25 +00:00
|
|
|
QSE_NULL, /* stdin */
|
2013-01-12 16:46:12 +00:00
|
|
|
QSE_NULL, /* stdout */
|
2012-01-15 15:25:28 +00:00
|
|
|
QSE_NULL /* default cmgr */
|
2009-02-17 08:22:42 +00:00
|
|
|
);
|
|
|
|
if (rtx == QSE_NULL)
|
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
|
2013-01-12 16:46:12 +00:00
|
|
|
goto oops;
|
2009-02-17 08:22:42 +00:00
|
|
|
}
|
2013-01-12 16:46:12 +00:00
|
|
|
|
|
|
|
/* get the pointer to the extension area. */
|
2019-06-24 08:53:49 +00:00
|
|
|
con = (console_t*)qse_awk_rtx_getxtn(rtx);
|
2013-01-12 16:46:12 +00:00
|
|
|
/* initialize fields that require non-zero values.
|
|
|
|
* the entire extension area was initialized to zeros
|
|
|
|
* when it was created. */
|
|
|
|
con->conin = QSE_T("Beth 4.00 0\nDan 3.74 0\nKathy 4.00 10\nMark 5.00 20\nMary 5.50 22\nSusie 4.25 18\n");
|
2009-02-17 08:22:42 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* retrieve the I/O handlers created by qse_awk_rtx_openstd() */
|
|
|
|
qse_awk_rtx_getrio (rtx, &rio);
|
|
|
|
/* override the console handler */
|
|
|
|
rio.console = handle_console;
|
|
|
|
/* update the I/O handlers */
|
|
|
|
qse_awk_rtx_setrio (rtx, &rio);
|
|
|
|
|
|
|
|
/* execute pattern-action blocks */
|
|
|
|
retv = qse_awk_rtx_loop (rtx);
|
|
|
|
if (retv == QSE_NULL)
|
2009-02-17 19:41:53 +00:00
|
|
|
{
|
2013-01-14 16:02:04 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
|
2013-01-12 16:46:12 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2009-02-23 08:10:34 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* decrement the reference count of the return value */
|
|
|
|
qse_awk_rtx_refdownval (rtx, retv);
|
2009-02-17 08:22:42 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
/* the buffer is available during the runtime context is alive */
|
|
|
|
qse_printf (QSE_T("Console Output:\n================\n%.*s\n"), (int)con->conoutpos, con->conout);
|
|
|
|
|
2013-01-14 16:02:04 +00:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
oops:
|
2013-01-12 16:46:12 +00:00
|
|
|
/* destroy the runtime context */
|
2013-01-08 15:56:56 +00:00
|
|
|
if (rtx) qse_awk_rtx_close (rtx);
|
2013-01-12 16:46:12 +00:00
|
|
|
|
2013-01-08 15:56:56 +00:00
|
|
|
/* destroy the awk object */
|
|
|
|
if (awk) qse_awk_close (awk);
|
|
|
|
|
2009-02-17 19:41:53 +00:00
|
|
|
return ret;
|
2009-02-17 08:22:42 +00:00
|
|
|
}
|
2013-01-12 16:46:12 +00:00
|
|
|
|
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
2014-03-03 15:36:30 +00:00
|
|
|
int x;
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2013-01-12 16:46:12 +00:00
|
|
|
init_awk_sample_locale ();
|
2018-09-13 03:16:23 +00:00
|
|
|
x = qse_run_main (argc, argv, awk_main);
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2014-03-03 15:36:30 +00:00
|
|
|
return x;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|