2012-06-05 12:56:41 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2012-06-05 12:56:41 +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-06-05 12:56:41 +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-06-05 12:56:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/awk/StdAwk.hpp>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2012-06-05 12:56:41 +00:00
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void print_error (
|
2013-01-10 14:17:53 +00:00
|
|
|
const QSE::StdAwk::loc_t& loc, const QSE::StdAwk::char_t* msg)
|
2012-06-05 12:56:41 +00:00
|
|
|
{
|
|
|
|
if (loc.line > 0 || loc.colm > 0)
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s at LINE %lu COLUMN %lu\n"), msg, loc.line, loc.colm);
|
|
|
|
else
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: %s\n"), msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-10 14:17:53 +00:00
|
|
|
static int run_awk (QSE::StdAwk& awk)
|
2012-06-05 12:56:41 +00:00
|
|
|
{
|
|
|
|
// ARGV[0]
|
2013-01-10 14:17:53 +00:00
|
|
|
if (awk.addArgument (QSE_T("awk05")) <= -1) return -1;
|
2012-06-05 12:56:41 +00:00
|
|
|
|
2013-01-10 14:17:53 +00:00
|
|
|
// ARGV[1] and/or the first console input file
|
|
|
|
if (awk.addArgument (QSE_T("Makefile")) <= -1) return -1;
|
2012-06-05 12:56:41 +00:00
|
|
|
|
2013-01-10 14:17:53 +00:00
|
|
|
const qse_char_t* script = QSE_T(
|
|
|
|
"BEGIN { print \">> PRINT ALL LINES WHOSE LENGTH IS GREATER THAN 0\"; }\n"
|
|
|
|
"length($0) > 0 { print $0; count++; }\n"
|
|
|
|
"END { print \">> TOTAL\", count, \"LINES\"; }\n"
|
|
|
|
);
|
2012-06-05 12:56:41 +00:00
|
|
|
|
2013-01-10 14:17:53 +00:00
|
|
|
QSE::StdAwk::SourceString in (script);
|
|
|
|
QSE::StdAwk::SourceFile out (QSE_T("awk05.out"));
|
2012-06-05 12:56:41 +00:00
|
|
|
|
2013-01-10 14:17:53 +00:00
|
|
|
// parse the script string and deparse it to awk05.out.
|
|
|
|
if (awk.parse (in, out) == QSE_NULL) return -1;
|
2012-06-05 12:56:41 +00:00
|
|
|
|
2013-01-10 14:17:53 +00:00
|
|
|
QSE::StdAwk::Value r;
|
|
|
|
// execute the BEGIN, pattern-action, END blocks.
|
|
|
|
return awk.loop (&r);
|
2012-06-05 12:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int awk_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
2013-01-10 14:17:53 +00:00
|
|
|
QSE::StdAwk awk;
|
2012-06-05 12:56:41 +00:00
|
|
|
|
|
|
|
int ret = awk.open ();
|
|
|
|
if (ret >= 0) ret = run_awk (awk);
|
|
|
|
|
|
|
|
if (ret <= -1)
|
|
|
|
{
|
2013-01-10 14:17:53 +00:00
|
|
|
QSE::StdAwk::loc_t loc = awk.getErrorLocation();
|
2012-06-05 12:56:41 +00:00
|
|
|
print_error (loc, awk.getErrorMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
awk.close ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
2014-06-08 14:11:12 +00:00
|
|
|
int x;
|
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2012-06-05 12:56:41 +00:00
|
|
|
{
|
2014-06-08 14:11:12 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
char locale[100];
|
|
|
|
UINT codepage = GetConsoleOutputCP();
|
|
|
|
if (codepage == CP_UTF8)
|
|
|
|
{
|
|
|
|
/*SetConsoleOUtputCP (CP_UTF8);*/
|
|
|
|
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf (locale, ".%u", (unsigned int)codepage);
|
|
|
|
setlocale (LC_ALL, locale);
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2014-06-08 14:11:12 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
setlocale (LC_ALL, "");
|
2015-04-27 08:37:57 +00:00
|
|
|
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
|
2014-06-08 14:11:12 +00:00
|
|
|
#endif
|
2012-06-05 12:56:41 +00:00
|
|
|
}
|
|
|
|
|
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-06-08 14:11:12 +00:00
|
|
|
return x;
|
2012-06-05 12:56:41 +00:00
|
|
|
}
|