qse/samples/awk/awk11.c

139 lines
3.7 KiB
C
Raw Normal View History

2012-04-30 09:46:58 +00:00
/*
2014-11-19 14:42:24 +00:00
* $Id$
2012-04-30 09:46:58 +00:00
*
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
2012-04-30 09:46:58 +00:00
2014-11-19 14:42:24 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
2012-04-30 09:46:58 +00:00
2014-11-19 14:42:24 +00:00
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2012-04-30 09:46:58 +00:00
*/
#include <qse/awk/stdawk.h>
2016-04-29 03:55:42 +00:00
#include <qse/si/sio.h>
2012-04-30 09:46:58 +00:00
const qse_char_t* src = QSE_T("BEGIN { print \"hello, world\" | \"dir\"; }");
struct rtx_xtn_t
{
qse_awk_rio_impl_t old_pipe_handler;
2012-04-30 09:46:58 +00:00
};
static qse_ssize_t new_pipe_handler (
qse_awk_rtx_t* rtx, qse_awk_rio_cmd_t cmd, qse_awk_rio_arg_t* riod,
qse_char_t* data, qse_size_t size)
{
struct rtx_xtn_t* xtn;
xtn = qse_awk_rtx_getxtn(rtx);
2012-04-30 09:46:58 +00:00
if (cmd == QSE_AWK_RIO_OPEN)
qse_fprintf (QSE_STDERR, QSE_T("LOG: Executing [%s] for piping\n"), riod->name);
return xtn->old_pipe_handler (rtx, cmd, riod, data, size);
}
static void extend_pipe_handler (qse_awk_rtx_t* rtx)
{
/* this function simply demonstrates how to extend
* runtime I/O handlers provided by qse_awk_rtx_openstd() */
struct rtx_xtn_t* xtn;
qse_awk_rio_t rio;
xtn = qse_awk_rtx_getxtn(rtx);
2012-04-30 09:46:58 +00:00
/* get the previous handler functions */
qse_awk_rtx_getrio (rtx, &rio);
/* remember the old pipe handler function */
xtn->old_pipe_handler = rio.pipe;
/* change the pipe handler to a new one */
rio.pipe = new_pipe_handler;
/* changes the handlers with a new set */
qse_awk_rtx_setrio (rtx, &rio);
}
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];
2012-04-30 09:46:58 +00:00
int ret = -1;
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
2014-07-11 14:17:00 +00:00
awk = qse_awk_openstd (0, QSE_NULL);
2012-04-30 09:46:58 +00:00
if (awk == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
goto oops;
}
psin[0].type = QSE_AWK_PARSESTD_STR;
psin[0].u.str.ptr = src;
psin[0].u.str.len = qse_strlen(src);
psin[1].type = QSE_AWK_PARSESTD_NULL;
2012-04-30 09:46:58 +00:00
if (qse_awk_parsestd (awk, psin, QSE_NULL) <= -1)
2012-04-30 09:46:58 +00:00
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
rtx = qse_awk_rtx_openstd (
awk,
QSE_SIZEOF(struct rtx_xtn_t),
QSE_T("awk11"),
QSE_NULL, /* stdin */
QSE_NULL, /* stdout */
QSE_NULL /* default cmgr */
);
if (rtx == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
qse_awk_geterrmsg(awk));
goto oops;
}
extend_pipe_handler (rtx);
retv = qse_awk_rtx_loop (rtx);
if (retv == QSE_NULL)
{
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"),
qse_awk_rtx_geterrmsg(rtx));
goto oops;
}
qse_awk_rtx_refdownval (rtx, retv);
ret = 0;
oops:
if (rtx != QSE_NULL) qse_awk_rtx_close (rtx);
if (awk != QSE_NULL) qse_awk_close (awk);
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
2012-04-30 09:46:58 +00:00
return ret;
}