2012-06-05 07:45:52 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2012-06-05 07:45:52 +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 07:45:52 +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 07:45:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qse/awk/StdAwk.hpp>
|
2015-03-11 13:33:23 +00:00
|
|
|
#include <qse/cmn/HeapMmgr.hpp>
|
2013-01-09 08:03:21 +00:00
|
|
|
#include <qse/cmn/opt.h>
|
2012-06-05 07:45:52 +00:00
|
|
|
#include <qse/cmn/main.h>
|
|
|
|
#include <qse/cmn/mbwc.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
#include <qse/si/sio.h>
|
2013-01-09 08:03:21 +00:00
|
|
|
#include <cstring>
|
2012-06-05 07:45:52 +00:00
|
|
|
|
|
|
|
#include <locale.h>
|
2013-01-09 08:03:21 +00:00
|
|
|
|
2012-06-05 07:45:52 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
2013-01-09 08:03:21 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
# define INCL_DOSPROCESS
|
|
|
|
# include <os2.h>
|
2012-06-05 07:45:52 +00:00
|
|
|
#else
|
2013-01-09 08:03:21 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <signal.h>
|
|
|
|
# include <errno.h>
|
2012-06-05 07:45:52 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
/* these three definitions for doxygen cross-reference */
|
2012-06-05 07:45:52 +00:00
|
|
|
typedef QSE::StdAwk StdAwk;
|
|
|
|
typedef QSE::StdAwk::Run Run;
|
|
|
|
typedef QSE::StdAwk::Value Value;
|
|
|
|
|
|
|
|
class MyAwk: public StdAwk
|
|
|
|
{
|
|
|
|
public:
|
2015-03-11 13:33:23 +00:00
|
|
|
MyAwk (QSE::Mmgr* mmgr = QSE_NULL): StdAwk (mmgr) { }
|
2012-06-05 07:45:52 +00:00
|
|
|
~MyAwk () { close (); }
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
int open ()
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
if (StdAwk::open () <= -1) return -1;
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
idLastSleep = addGlobal (QSE_T("LAST_SLEEP"));
|
|
|
|
if (idLastSleep <= -1) goto oops;
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
/* this is for demonstration only.
|
|
|
|
* you can use sys::sleep() instead */
|
|
|
|
if (addFunction (QSE_T("sleep"), 1, 1, QSE_NULL,
|
|
|
|
(FunctionHandler)&MyAwk::sleep) <= -1) goto oops;
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
if (addFunction (QSE_T("sumintarray"), 1, 1, QSE_NULL,
|
|
|
|
(FunctionHandler)&MyAwk::sumintarray) <= -1) goto oops;
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
if (addFunction (QSE_T("arrayindices"), 1, 1, QSE_NULL,
|
|
|
|
(FunctionHandler)&MyAwk::arrayindices) <= -1) goto oops;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
StdAwk::close ();
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
int sleep (
|
|
|
|
Run& run, Value& ret, Value* args, size_t nargs,
|
|
|
|
const char_t* name, size_t len)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
if (args[0].isIndexed())
|
|
|
|
{
|
|
|
|
run.setError (QSE_AWK_EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-08 14:11:12 +00:00
|
|
|
Awk::int_t x = args[0].toInt();
|
2013-01-09 08:03:21 +00:00
|
|
|
|
|
|
|
/*Value arg;
|
|
|
|
if (run.getGlobal(idLastSleep, arg) == 0)
|
|
|
|
qse_printf (QSE_T("GOOD: [%d]\n"), (int)arg.toInt());
|
|
|
|
else { qse_printf (QSE_T("BAD:\n")); }
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (run.setGlobal (idLastSleep, x) <= -1) return -1;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
::Sleep ((DWORD)(x * 1000));
|
|
|
|
return ret.setInt (0);
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
::DosSleep ((ULONG)(x * 1000));
|
|
|
|
return ret.setInt (0);
|
|
|
|
#else
|
|
|
|
return ret.setInt (::sleep (x));
|
|
|
|
#endif
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
int sumintarray (
|
|
|
|
Run& run, Value& ret, Value* args, size_t nargs,
|
|
|
|
const char_t* name, size_t len)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
// BEGIN {
|
|
|
|
// for(i=0;i<=10;i++) x[i]=i;
|
|
|
|
// print sumintarray(x);
|
|
|
|
// }
|
|
|
|
long_t x = 0;
|
|
|
|
|
|
|
|
if (args[0].isIndexed())
|
|
|
|
{
|
|
|
|
Value val(run);
|
|
|
|
Value::Index idx;
|
|
|
|
Value::IndexIterator ii;
|
|
|
|
|
|
|
|
ii = args[0].getFirstIndex (&idx);
|
|
|
|
while (ii != ii.END)
|
|
|
|
{
|
|
|
|
if (args[0].getIndexed(idx, &val) <= -1) return -1;
|
|
|
|
x += val.toInt ();
|
|
|
|
|
|
|
|
ii = args[0].getNextIndex (&idx, ii);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else x += args[0].toInt();
|
|
|
|
|
|
|
|
return ret.setInt (x);
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
int arrayindices (
|
|
|
|
Run& run,
|
|
|
|
Value& ret,
|
|
|
|
Value* args,
|
|
|
|
size_t nargs,
|
|
|
|
const char_t* name,
|
|
|
|
size_t len)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
// create another array composed of array indices
|
|
|
|
// BEGIN {
|
|
|
|
// for(i=0;i<=10;i++) x[i]=i;
|
|
|
|
// y=arrayindices(x);
|
|
|
|
// for (i in y) print y[i];
|
|
|
|
// }
|
|
|
|
if (!args[0].isIndexed()) return 0;
|
|
|
|
|
|
|
|
Value::Index idx;
|
|
|
|
Value::IndexIterator ii;
|
|
|
|
long_t i;
|
|
|
|
|
|
|
|
ii = args[0].getFirstIndex (&idx);
|
|
|
|
for (i = 0; ii != ii.END ; i++)
|
|
|
|
{
|
|
|
|
Value::IntIndex iidx (i);
|
|
|
|
if (ret.setIndexedStr (
|
|
|
|
iidx, idx.pointer(), idx.length()) <= -1) return -1;
|
|
|
|
ii = args[0].getNextIndex (&idx, ii);
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
2013-01-09 08:03:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
2013-01-09 08:03:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int idLastSleep;
|
2012-06-05 07:45:52 +00:00
|
|
|
};
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
static MyAwk* app_awk = QSE_NULL;
|
|
|
|
|
|
|
|
static void print_error (const qse_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: "));
|
|
|
|
|
|
|
|
va_start (va, fmt);
|
|
|
|
qse_vfprintf (QSE_STDERR, fmt, va);
|
|
|
|
va_end (va);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_error (MyAwk& awk)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
MyAwk::loc_t loc = awk.getErrorLocation();
|
|
|
|
|
|
|
|
if (loc.file)
|
|
|
|
{
|
|
|
|
print_error (QSE_T("line %u at %s - %s\n"), (unsigned)loc.line, loc.file, awk.getErrorMessage());
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
else
|
2013-01-09 08:03:21 +00:00
|
|
|
{
|
|
|
|
print_error (QSE_T("line %u - %s\n"), (unsigned)loc.line, awk.getErrorMessage());
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static BOOL WINAPI stop_run (DWORD ctrl_type)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
if (ctrl_type == CTRL_C_EVENT ||
|
|
|
|
ctrl_type == CTRL_CLOSE_EVENT)
|
|
|
|
{
|
|
|
|
if (app_awk) app_awk->stop ();
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
static int setsignal (int sig, void(*handler)(int), int restart)
|
|
|
|
{
|
|
|
|
struct sigaction sa_int;
|
|
|
|
|
|
|
|
sa_int.sa_handler = handler;
|
|
|
|
sigemptyset (&sa_int.sa_mask);
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
sa_int.sa_flags = 0;
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
if (restart)
|
|
|
|
{
|
|
|
|
#ifdef SA_RESTART
|
|
|
|
sa_int.sa_flags |= SA_RESTART;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef SA_INTERRUPT
|
|
|
|
sa_int.sa_flags |= SA_INTERRUPT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return sigaction (sig, &sa_int, NULL);
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
static void stop_run (int sig)
|
|
|
|
{
|
|
|
|
int e = errno;
|
2019-11-12 07:55:32 +00:00
|
|
|
if (app_awk) app_awk->halt ();
|
2013-01-09 08:03:21 +00:00
|
|
|
errno = e;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void set_signal (void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleCtrlHandler (stop_run, TRUE);
|
|
|
|
#else
|
|
|
|
/*setsignal (SIGINT, stop_run, 1); TO BE MORE COMPATIBLE WITH WIN32*/
|
|
|
|
setsignal (SIGINT, stop_run, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unset_signal (void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleCtrlHandler (stop_run, FALSE);
|
|
|
|
#else
|
|
|
|
setsignal (SIGINT, SIG_DFL, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-08 14:11:12 +00:00
|
|
|
static void print_usage (qse_sio_t* out, const qse_char_t* argv0)
|
2013-01-09 08:03:21 +00:00
|
|
|
{
|
|
|
|
qse_fprintf (out, QSE_T("USAGE: %s [options] -f sourcefile [ -- ] [datafile]*\n"), argv0);
|
|
|
|
qse_fprintf (out, QSE_T(" %s [options] [ -- ] sourcestring [datafile]*\n"), argv0);
|
|
|
|
qse_fprintf (out, QSE_T("Where options are:\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -h print this message\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -f sourcefile set the source script file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -d deparsedfile set the deparsing output file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -o outputfile set the console output file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -F string set a field separator(FS)\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cmdline_t
|
|
|
|
{
|
|
|
|
qse_char_t* ins;
|
|
|
|
qse_char_t* inf;
|
|
|
|
qse_char_t* outf;
|
|
|
|
qse_char_t* outc;
|
|
|
|
qse_char_t* fs;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int handle_cmdline (
|
|
|
|
MyAwk& awk, int argc, qse_char_t* argv[], cmdline_t* cmdline)
|
|
|
|
{
|
|
|
|
static qse_opt_t opt =
|
|
|
|
{
|
|
|
|
QSE_T("hF:f:d:o:"),
|
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
qse_cint_t c;
|
|
|
|
|
|
|
|
std::memset (cmdline, 0, QSE_SIZEOF(*cmdline));
|
|
|
|
while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case QSE_T('h'):
|
|
|
|
print_usage (QSE_STDOUT, argv[0]);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case QSE_T('F'):
|
|
|
|
cmdline->fs = opt.arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('f'):
|
|
|
|
cmdline->inf = opt.arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('d'):
|
|
|
|
cmdline->outf = opt.arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('o'):
|
|
|
|
cmdline->outc = opt.arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QSE_T('?'):
|
|
|
|
print_error (QSE_T("illegal option - '%c'\n"), opt.opt);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case QSE_T(':'):
|
|
|
|
print_error (QSE_T("bad argument for '%c'\n"), opt.opt);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
print_usage (QSE_STDERR, argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opt.ind < argc && !cmdline->inf)
|
|
|
|
cmdline->ins = argv[opt.ind++];
|
|
|
|
|
|
|
|
while (opt.ind < argc)
|
|
|
|
{
|
|
|
|
if (awk.addArgument (argv[opt.ind++]) <= -1)
|
|
|
|
{
|
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmdline->ins && !cmdline->inf)
|
|
|
|
{
|
|
|
|
print_usage (QSE_STDERR, argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int awk_main_2 (MyAwk& awk, int argc, qse_char_t* argv[])
|
|
|
|
{
|
|
|
|
MyAwk::Run* run;
|
|
|
|
cmdline_t cmdline;
|
|
|
|
int n;
|
|
|
|
|
2013-01-29 03:43:32 +00:00
|
|
|
awk.setTrait (awk.getTrait() | QSE_AWK_FLEXMAP | QSE_AWK_RWPIPE | QSE_AWK_NEXTOFILE);
|
2013-01-09 08:03:21 +00:00
|
|
|
|
|
|
|
// ARGV[0]
|
2013-01-29 03:43:32 +00:00
|
|
|
if (awk.addArgument (QSE_T("awk25")) <= -1)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
if ((n = handle_cmdline (awk, argc, argv, &cmdline)) <= 0) return n;
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
MyAwk::Source* in, * out;
|
|
|
|
MyAwk::SourceString in_str (cmdline.ins);
|
|
|
|
MyAwk::SourceFile in_file (cmdline.inf);
|
|
|
|
MyAwk::SourceFile out_file (cmdline.outf);
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
in = (cmdline.ins)? (MyAwk::Source*)&in_str: (MyAwk::Source*)&in_file;
|
|
|
|
out = (cmdline.outf)? (MyAwk::Source*)&out_file: &MyAwk::Source::NONE;
|
|
|
|
run = awk.parse (*in, *out);
|
|
|
|
if (run == QSE_NULL)
|
|
|
|
{
|
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
if (cmdline.fs)
|
|
|
|
{
|
|
|
|
MyAwk::Value fs (run);
|
|
|
|
if (fs.setStr (cmdline.fs) <= -1)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (awk.setGlobal (QSE_AWK_GBL_FS, fs) <= -1)
|
|
|
|
{
|
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmdline.outc)
|
|
|
|
{
|
|
|
|
if (awk.addConsoleOutput (cmdline.outc) <= -1)
|
|
|
|
{
|
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
MyAwk::Value ret;
|
|
|
|
if (awk.loop (&ret) <= -1)
|
|
|
|
{
|
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int awk_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
2015-03-11 13:33:23 +00:00
|
|
|
//QSE::HeapMmgr hm (1000000);
|
|
|
|
//MyAwk awk (&hm);
|
2012-06-05 07:45:52 +00:00
|
|
|
MyAwk awk;
|
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
if (awk.open() <= -1)
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
print_error (awk);
|
|
|
|
return -1;
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
2013-01-09 08:03:21 +00:00
|
|
|
app_awk = &awk;
|
|
|
|
|
|
|
|
set_signal ();
|
|
|
|
int n = awk_main_2 (awk, argc, argv);
|
|
|
|
unset_signal ();
|
2012-06-05 07:45:52 +00:00
|
|
|
|
2013-01-09 08:03:21 +00:00
|
|
|
app_awk = QSE_NULL;
|
2012-06-05 07:45:52 +00:00
|
|
|
awk.close ();
|
2013-01-09 08:03:21 +00:00
|
|
|
return n;
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
|
|
|
{
|
2013-01-09 08:03:21 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2013-01-09 08:03:21 +00:00
|
|
|
|
2012-06-05 07:45:52 +00:00
|
|
|
{
|
2014-06-08 14:11:12 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
char locale[100];
|
|
|
|
UINT codepage;
|
|
|
|
WSADATA wsadata;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
|
|
|
|
{
|
|
|
|
print_error (QSE_T("Failed to start up winsock\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 07:45:52 +00:00
|
|
|
}
|
2013-01-09 08:03:21 +00:00
|
|
|
|
2018-09-13 03:16:23 +00:00
|
|
|
ret = qse_run_main (argc, argv, awk_main);
|
2013-01-09 08:03:21 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
WSACleanup ();
|
|
|
|
#endif
|
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2013-01-09 08:03:21 +00:00
|
|
|
return ret;
|
2012-06-05 07:45:52 +00:00
|
|
|
}
|