2013-01-08 05:51:58 +00:00
|
|
|
/*
|
2014-11-19 14:42:24 +00:00
|
|
|
* $Id$
|
2013-01-08 05:51:58 +00:00
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2013-01-08 05:51: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.
|
2013-01-08 05:51: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.
|
2013-01-08 05:51:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/awk/awk.h>
|
2013-02-22 03:18:42 +00:00
|
|
|
#include <qse/awk/stdawk.h>
|
2013-01-08 05:51:58 +00:00
|
|
|
#include <qse/cmn/mem.h>
|
2019-08-29 08:42:17 +00:00
|
|
|
#include <qse/cmn/path.h>
|
|
|
|
#include <qse/cmn/str.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2013-01-08 05:51:58 +00:00
|
|
|
|
|
|
|
static 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 \"---------------------\";"
|
|
|
|
" }"
|
|
|
|
"}"
|
|
|
|
);
|
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
int main (int argc, char* argv[])
|
2013-01-08 05:51:58 +00:00
|
|
|
{
|
|
|
|
qse_awk_t* awk = QSE_NULL;
|
|
|
|
qse_awk_rtx_t* rtx = QSE_NULL;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
qse_awk_parsestd_t psin[2];
|
|
|
|
qse_awk_parsestd_t psout;
|
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
int ret, omode = 0;
|
2013-01-08 05:51:58 +00:00
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2014-06-08 14:11:12 +00:00
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
if (argc >= 2)
|
|
|
|
{
|
|
|
|
if (argc == 2 && qse_mbscmp(argv[1], "-m") == 0) omode = 1;
|
|
|
|
else if (argc == 2 && qse_mbscmp(argv[1], "-w") == 0) omode = 2;
|
|
|
|
else
|
|
|
|
{
|
2019-09-10 08:53:59 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("USAGE: %hs [-m | -w]\n"), qse_mbsbasename(argv[0]));
|
2019-08-29 08:42:17 +00:00
|
|
|
ret = -1; goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
awk = qse_awk_openstd(0, QSE_NULL);
|
2013-01-08 05:51:58 +00:00
|
|
|
if (awk == QSE_NULL)
|
|
|
|
{
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: cannot open awk\n"));
|
|
|
|
ret = -1; 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;
|
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
switch (omode)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
psout.type = QSE_AWK_PARSESTD_MBS;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
psout.type = QSE_AWK_PARSESTD_WCS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
psout.type = QSE_AWK_PARSESTD_STR;
|
|
|
|
break;
|
2013-01-08 05:51:58 +00:00
|
|
|
/* ps.out.u.str.ptr and ps.out.u.str.len are set when qse_awk_parsestd()
|
|
|
|
* returns success */
|
2019-08-29 08:42:17 +00:00
|
|
|
}
|
2013-01-08 05:51:58 +00:00
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
ret = qse_awk_parsestd(awk, psin, &psout);
|
2013-01-08 05:51:58 +00:00
|
|
|
if (ret <= -1)
|
|
|
|
{
|
2019-08-29 08:42:17 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
|
2013-01-08 05:51:58 +00:00
|
|
|
ret = -1; goto oops;
|
|
|
|
}
|
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
qse_printf (QSE_T("DEPARSED SOURCE:\n"));
|
|
|
|
switch (omode)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
qse_printf (QSE_T("%hs\n"), psout.u.mbs.ptr);
|
|
|
|
qse_awk_freemem (awk, psout.u.mbs.ptr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
qse_printf (QSE_T("%ls\n"), psout.u.wcs.ptr);
|
|
|
|
qse_awk_freemem (awk, psout.u.wcs.ptr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
qse_printf (QSE_T("%js\n"), psout.u.str.ptr);
|
|
|
|
qse_awk_freemem (awk, psout.u.str.ptr);
|
|
|
|
break;
|
|
|
|
}
|
2013-01-08 05:51:58 +00:00
|
|
|
qse_printf (QSE_T("=================================\n"));
|
|
|
|
qse_fflush (QSE_STDOUT);
|
|
|
|
|
|
|
|
rtx = qse_awk_rtx_openstd (
|
|
|
|
awk,
|
|
|
|
0,
|
2019-08-29 08:42:17 +00:00
|
|
|
QSE_T("awk15"),
|
2013-01-08 05:51:58 +00:00
|
|
|
QSE_NULL, /* stdin */
|
|
|
|
QSE_NULL, /* stdout */
|
|
|
|
QSE_NULL /* default cmgr */
|
|
|
|
);
|
|
|
|
if (rtx == QSE_NULL)
|
|
|
|
{
|
2019-08-29 08:42:17 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_geterrmsg(awk));
|
2013-01-08 05:51:58 +00:00
|
|
|
ret = -1; goto oops;
|
|
|
|
}
|
|
|
|
|
2019-08-29 08:42:17 +00:00
|
|
|
retv = qse_awk_rtx_loop(rtx);
|
2013-01-08 05:51:58 +00:00
|
|
|
if (retv == QSE_NULL)
|
|
|
|
{
|
2019-08-29 08:42:17 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), qse_awk_rtx_geterrmsg(rtx));
|
2013-01-08 05:51:58 +00:00
|
|
|
ret = -1; 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);
|
2014-06-08 14:11:12 +00:00
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2013-01-08 05:51:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|