qse/qse/cmd/awk/awk.c

615 lines
13 KiB
C
Raw Normal View History

/*
* $Id: awk.c 499 2008-12-16 09:42:48Z baconevi $
*/
2008-12-21 21:35:07 +00:00
#include <qse/awk/awk.h>
#include <qse/cmn/sll.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/chr.h>
#include <qse/cmn/opt.h>
2008-12-21 21:35:07 +00:00
#include <qse/utl/stdio.h>
#include <qse/utl/main.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
2008-10-01 05:14:20 +00:00
#define ABORT(label) goto label
#if defined(_WIN32)
#include <windows.h>
#include <tchar.h>
#include <process.h>
#pragma warning (disable: 4996)
#pragma warning (disable: 4296)
2008-08-19 21:16:02 +00:00
#if defined(_MSC_VER) && defined(_DEBUG)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#endif
#else
#include <unistd.h>
#endif
2008-12-21 21:35:07 +00:00
static qse_awk_t* app_awk = NULL;
static qse_awk_run_t* app_run = NULL;
static int app_debug = 0;
2008-12-21 21:35:07 +00:00
static void dprint (const qse_char_t* fmt, ...)
{
if (app_debug)
{
va_list ap;
va_start (ap, fmt);
2008-12-21 21:35:07 +00:00
qse_vfprintf (stderr, fmt, ap);
va_end (ap);
}
}
#ifdef _WIN32
static BOOL WINAPI stop_run (DWORD ctrl_type)
{
if (ctrl_type == CTRL_C_EVENT ||
ctrl_type == CTRL_CLOSE_EVENT)
{
2008-12-21 21:35:07 +00:00
qse_awk_stop (app_run);
return TRUE;
}
return FALSE;
}
#else
static void stop_run (int sig)
{
signal (SIGINT, SIG_IGN);
2008-12-21 21:35:07 +00:00
qse_awk_stop (app_run);
signal (SIGINT, stop_run);
}
#endif
2008-12-21 21:35:07 +00:00
static void on_run_start (qse_awk_run_t* run, void* custom)
{
app_run = run;
2008-12-21 21:35:07 +00:00
dprint (QSE_T("[AWK ABOUT TO START]\n"));
}
2008-12-21 21:35:07 +00:00
static qse_map_walk_t print_awk_value (
qse_map_t* map, qse_map_pair_t* pair, void* arg)
{
2008-12-21 21:35:07 +00:00
qse_awk_run_t* run = (qse_awk_run_t*)arg;
qse_char_t* str;
qse_size_t len;
2008-12-11 04:19:59 +00:00
2008-12-21 21:35:07 +00:00
str = qse_awk_valtostr (run, QSE_MAP_VPTR(pair), 0, QSE_NULL, &len);
if (str == QSE_NULL)
2008-12-11 04:19:59 +00:00
{
2008-12-21 21:35:07 +00:00
dprint (QSE_T("***OUT OF MEMORY***\n"));
2008-12-11 04:19:59 +00:00
}
else
{
2008-12-21 21:35:07 +00:00
dprint (QSE_T("%.*s = %.*s\n"),
(int)QSE_MAP_KLEN(pair), QSE_MAP_KPTR(pair),
2008-12-11 04:19:59 +00:00
(int)len, str);
2008-12-21 21:35:07 +00:00
qse_awk_free (qse_awk_getrunawk(run), str);
2008-12-11 04:19:59 +00:00
}
2008-12-21 21:35:07 +00:00
return QSE_MAP_WALK_FORWARD;
}
static void on_run_statement (
2008-12-21 21:35:07 +00:00
qse_awk_run_t* run, qse_size_t line, void* custom)
{
2008-12-11 04:19:59 +00:00
/*dprint (L"running %d\n", (int)line);*/
}
static void on_run_return (
2008-12-21 21:35:07 +00:00
qse_awk_run_t* run, qse_awk_val_t* ret, void* custom)
{
2008-12-21 21:35:07 +00:00
qse_size_t len;
qse_char_t* str;
2008-12-11 04:19:59 +00:00
2008-12-21 21:35:07 +00:00
if (ret == qse_awk_val_nil)
2008-12-11 04:19:59 +00:00
{
2008-12-21 21:35:07 +00:00
dprint (QSE_T("[RETURN] - ***nil***\n"));
2008-12-11 04:19:59 +00:00
}
else
{
2008-12-21 21:35:07 +00:00
str = qse_awk_valtostr (run, ret, 0, QSE_NULL, &len);
if (str == QSE_NULL)
2008-12-16 03:56:48 +00:00
{
2008-12-21 21:35:07 +00:00
dprint (QSE_T("[RETURN] - ***OUT OF MEMORY***\n"));
2008-12-16 03:56:48 +00:00
}
else
{
2008-12-21 21:35:07 +00:00
dprint (QSE_T("[RETURN] - [%.*s]\n"), (int)len, str);
qse_awk_free (qse_awk_getrunawk(run), str);
2008-12-16 03:56:48 +00:00
}
2008-12-11 04:19:59 +00:00
}
2008-12-21 21:35:07 +00:00
dprint (QSE_T("[NAMED VARIABLES]\n"));
qse_map_walk (qse_awk_getrunnvmap(run), print_awk_value, run);
dprint (QSE_T("[END NAMED VARIABLES]\n"));
}
2008-12-21 21:35:07 +00:00
static void on_run_end (qse_awk_run_t* run, int errnum, void* data)
{
2008-12-21 21:35:07 +00:00
if (errnum != QSE_AWK_ENOERR)
{
2008-12-21 21:35:07 +00:00
dprint (QSE_T("[AWK ENDED WITH AN ERROR]\n"));
qse_printf (QSE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
errnum,
2008-12-21 21:35:07 +00:00
(unsigned int)qse_awk_getrunerrlin(run),
qse_awk_getrunerrmsg(run));
}
2008-12-21 21:35:07 +00:00
else dprint (QSE_T("[AWK ENDED SUCCESSFULLY]\n"));
app_run = NULL;
}
2008-08-19 21:16:02 +00:00
/* TODO: remove otab... */
static struct
{
2008-12-21 21:35:07 +00:00
const qse_char_t* name;
int opt;
} otab[] =
{
2008-12-21 21:35:07 +00:00
{ QSE_T("implicit"), QSE_AWK_IMPLICIT },
{ QSE_T("explicit"), QSE_AWK_EXPLICIT },
{ QSE_T("bxor"), QSE_AWK_BXOR },
{ QSE_T("shift"), QSE_AWK_SHIFT },
{ QSE_T("idiv"), QSE_AWK_IDIV },
{ QSE_T("extio"), QSE_AWK_EXTIO },
{ QSE_T("rwpipe"), QSE_AWK_RWPIPE },
2008-12-21 21:35:07 +00:00
{ QSE_T("newline"), QSE_AWK_NEWLINE },
{ QSE_T("baseone"), QSE_AWK_BASEONE },
{ QSE_T("stripspaces"), QSE_AWK_STRIPSPACES },
{ QSE_T("nextofile"), QSE_AWK_NEXTOFILE },
{ QSE_T("crfl"), QSE_AWK_CRLF },
{ QSE_T("argstomain"), QSE_AWK_ARGSTOMAIN },
{ QSE_T("reset"), QSE_AWK_RESET },
{ QSE_T("maptovar"), QSE_AWK_MAPTOVAR },
{ QSE_T("pablock"), QSE_AWK_PABLOCK }
};
2008-12-21 21:35:07 +00:00
static void print_usage (const qse_char_t* argv0)
{
int j;
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("Usage: %s [options] -f sourcefile [ -- ] [datafile]*\n"), argv0);
qse_printf (QSE_T(" %s [options] [ -- ] sourcestring [datafile]*\n"), argv0);
qse_printf (QSE_T("Where options are:\n"));
qse_printf (QSE_T(" -h print this message\n"));
qse_printf (QSE_T(" -d show extra information\n"));
qse_printf (QSE_T(" -f/--file sourcefile set the source script file\n"));
qse_printf (QSE_T(" -o/--deparsed-file deparsedfile set the deparsing output file\n"));
qse_printf (QSE_T(" -F/--field-separator string set a field separator(FS)\n"));
qse_printf (QSE_T("\nYou may specify the following options to change the behavior of the interpreter.\n"));
for (j = 0; j < QSE_COUNTOF(otab); j++)
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T(" -%-20s -no%-20s\n"), otab[j].name, otab[j].name);
}
}
static int bfn_sleep (
2008-12-21 21:35:07 +00:00
qse_awk_run_t* run, const qse_char_t* fnm, qse_size_t fnl)
{
2008-12-21 21:35:07 +00:00
qse_size_t nargs;
qse_awk_val_t* a0;
qse_long_t lv;
qse_real_t rv;
qse_awk_val_t* r;
int n;
2008-12-21 21:35:07 +00:00
nargs = qse_awk_getnargs (run);
QSE_ASSERT (nargs == 1);
2008-12-21 21:35:07 +00:00
a0 = qse_awk_getarg (run, 0);
2008-12-21 21:35:07 +00:00
n = qse_awk_valtonum (run, a0, &lv, &rv);
if (n == -1) return -1;
2008-12-21 21:35:07 +00:00
if (n == 1) lv = (qse_long_t)rv;
#ifdef _WIN32
Sleep ((DWORD)(lv * 1000));
n = 0;
#else
n = sleep (lv);
#endif
2008-12-21 21:35:07 +00:00
r = qse_awk_makeintval (run, n);
if (r == QSE_NULL)
{
2008-12-21 21:35:07 +00:00
qse_awk_setrunerrnum (run, QSE_AWK_ENOMEM);
return -1;
}
2008-12-21 21:35:07 +00:00
qse_awk_setretval (run, r);
return 0;
}
static void out_of_memory (void)
{
2008-12-21 21:35:07 +00:00
qse_fprintf (QSE_STDERR, QSE_T("Error: out of memory\n"));
}
2008-10-01 05:14:20 +00:00
struct argout_t
{
2008-12-16 03:56:48 +00:00
void* isp; /* input source files or string */
2008-12-13 03:42:32 +00:00
int ist; /* input source type */
2008-12-21 21:35:07 +00:00
qse_size_t isfl; /* the number of input source files */
qse_char_t* osf; /* output source file */
qse_char_t** icf; /* input console files */
qse_size_t icfl; /* the number of input console files */
qse_map_t* vm; /* global variable map */
2008-10-01 05:14:20 +00:00
};
2008-12-21 21:35:07 +00:00
static int handle_args (int argc, qse_char_t* argv[], struct argout_t* ao)
2008-07-23 08:22:24 +00:00
{
2008-12-21 21:35:07 +00:00
static qse_opt_lng_t lng[] =
2008-07-25 08:08:37 +00:00
{
2008-12-21 21:35:07 +00:00
{ QSE_T("implicit"), 0 },
{ QSE_T("explicit"), 0 },
{ QSE_T("bxor"), 0 },
{ QSE_T("shift"), 0 },
{ QSE_T("idiv"), 0 },
{ QSE_T("extio"), 0 },
{ QSE_T("newline"), 0 },
{ QSE_T("baseone"), 0 },
{ QSE_T("stripspaces"), 0 },
{ QSE_T("nextofile"), 0 },
{ QSE_T("crlf"), 0 },
{ QSE_T("argstomain"), 0 },
{ QSE_T("reset"), 0 },
{ QSE_T("maptovar"), 0 },
{ QSE_T("pablock"), 0 },
{ QSE_T(":main"), QSE_T('m') },
{ QSE_T(":file"), QSE_T('f') },
{ QSE_T(":field-separator"), QSE_T('F') },
{ QSE_T(":deparsed-file"), QSE_T('o') },
{ QSE_T(":assign"), QSE_T('v') },
{ QSE_T("help"), QSE_T('h') }
2008-07-25 08:08:37 +00:00
};
2008-12-21 21:35:07 +00:00
static qse_opt_t opt =
2008-07-25 08:08:37 +00:00
{
2008-12-21 21:35:07 +00:00
QSE_T("hdm:f:F:o:v:"),
2008-07-25 08:08:37 +00:00
lng
};
2008-07-23 08:22:24 +00:00
2008-12-21 21:35:07 +00:00
qse_cint_t c;
2008-09-30 05:07:47 +00:00
2008-12-21 21:35:07 +00:00
qse_size_t isfc = 16; /* the capacity of isf */
qse_size_t isfl = 0; /* number of input source files */
2008-12-21 21:35:07 +00:00
qse_size_t icfc = 0; /* the capacity of icf */
qse_size_t icfl = 0; /* the number of input console files */
2008-12-21 21:35:07 +00:00
qse_char_t** isf = QSE_NULL; /* input source files */
qse_char_t* osf = QSE_NULL; /* output source file */
qse_char_t** icf = QSE_NULL; /* input console files */
2008-12-21 21:35:07 +00:00
qse_map_t* vm = QSE_NULL; /* global variable map */
2008-12-21 21:35:07 +00:00
isf = (qse_char_t**) malloc (QSE_SIZEOF(*isf) * isfc);
if (isf == QSE_NULL)
2008-09-30 05:07:47 +00:00
{
out_of_memory ();
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-09-30 05:07:47 +00:00
}
2008-12-21 21:35:07 +00:00
vm = qse_map_open (QSE_NULL, 0, 30, 70);
if (vm == QSE_NULL)
2008-09-30 05:07:47 +00:00
{
out_of_memory ();
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-09-30 05:07:47 +00:00
}
2008-12-21 21:35:07 +00:00
qse_map_setcopier (vm, QSE_MAP_KEY, QSE_MAP_COPIER_INLINE);
qse_map_setcopier (vm, QSE_MAP_VAL, QSE_MAP_COPIER_INLINE);
qse_map_setscale (vm, QSE_MAP_KEY, QSE_SIZEOF(qse_char_t));
qse_map_setscale (vm, QSE_MAP_VAL, QSE_SIZEOF(qse_char_t));
2008-07-23 08:22:24 +00:00
2008-12-21 21:35:07 +00:00
while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF)
2008-07-23 08:22:24 +00:00
{
switch (c)
{
2008-07-26 09:01:27 +00:00
case 0:
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T(">>> [%s] [%s]\n"), opt.lngopt, opt.arg);
2008-07-26 09:01:27 +00:00
break;
2008-12-21 21:35:07 +00:00
case QSE_T('h'):
2008-07-23 08:22:24 +00:00
print_usage (argv[0]);
2008-12-21 21:35:07 +00:00
if (isf != QSE_NULL) free (isf);
if (vm != QSE_NULL) qse_map_close (vm);
2008-07-27 09:37:38 +00:00
return 1;
2008-07-23 08:22:24 +00:00
2008-12-21 21:35:07 +00:00
case QSE_T('d'):
{
app_debug = 1;
break;
}
2008-12-21 21:35:07 +00:00
case QSE_T('f'):
{
2008-12-21 21:35:07 +00:00
if (isfl >= isfc-1) /* -1 for last QSE_NULL */
2008-08-11 02:27:21 +00:00
{
2008-12-21 21:35:07 +00:00
qse_char_t** tmp;
tmp = (qse_char_t**) realloc (isf, QSE_SIZEOF(*isf)*(isfc+16));
if (tmp == QSE_NULL)
{
out_of_memory ();
2008-12-16 03:56:48 +00:00
ABORT (oops);
}
isf = tmp;
isfc = isfc + 16;
2008-08-11 02:27:21 +00:00
}
isf[isfl++] = opt.arg;
2008-07-27 09:37:38 +00:00
break;
}
2008-07-27 09:37:38 +00:00
2008-12-21 21:35:07 +00:00
case QSE_T('F'):
2008-10-01 05:14:20 +00:00
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("[field separator] = %s\n"), opt.arg);
2008-07-23 08:22:24 +00:00
break;
2008-10-01 05:14:20 +00:00
}
2008-07-23 08:22:24 +00:00
2008-12-21 21:35:07 +00:00
case QSE_T('o'):
2008-10-14 05:32:58 +00:00
{
osf = opt.arg;
break;
}
2008-12-21 21:35:07 +00:00
case QSE_T('v'):
2008-09-30 05:07:47 +00:00
{
2008-12-21 21:35:07 +00:00
qse_char_t* eq = qse_strchr(opt.arg, QSE_T('='));
if (eq == QSE_NULL)
2008-09-30 05:07:47 +00:00
{
/* INVALID VALUE... */
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-09-30 05:07:47 +00:00
}
2008-12-21 21:35:07 +00:00
*eq = QSE_T('\0');
2008-09-30 05:07:47 +00:00
2008-12-21 21:35:07 +00:00
if (qse_map_upsert (vm, opt.arg, qse_strlen(opt.arg)+1, eq, qse_strlen(eq)+1) == QSE_NULL)
2008-09-30 05:07:47 +00:00
{
out_of_memory ();
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-09-30 05:07:47 +00:00
}
break;
}
2008-12-21 21:35:07 +00:00
case QSE_T('?'):
2008-10-01 05:14:20 +00:00
{
2008-07-27 09:37:38 +00:00
if (opt.lngopt)
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("Error: illegal option - %s\n"), opt.lngopt);
2008-07-27 09:37:38 +00:00
}
else
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("Error: illegal option - %c\n"), opt.opt);
2008-07-27 09:37:38 +00:00
}
2008-08-19 21:16:02 +00:00
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-10-01 05:14:20 +00:00
}
2008-07-23 08:22:24 +00:00
2008-12-21 21:35:07 +00:00
case QSE_T(':'):
2008-10-01 05:14:20 +00:00
{
2008-07-27 09:37:38 +00:00
if (opt.lngopt)
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("Error: bad argument for %s\n"), opt.lngopt);
2008-07-27 09:37:38 +00:00
}
else
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("Error: bad argument for %c\n"), opt.opt);
2008-07-27 09:37:38 +00:00
}
2008-08-19 21:16:02 +00:00
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-10-01 05:14:20 +00:00
}
2008-07-23 08:22:24 +00:00
2008-07-26 09:01:27 +00:00
default:
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-07-23 08:22:24 +00:00
}
}
2008-12-21 21:35:07 +00:00
isf[isfl] = QSE_NULL;
2008-08-12 04:52:25 +00:00
if (isfl <= 0)
2008-07-26 09:01:27 +00:00
{
2008-08-19 05:21:48 +00:00
if (opt.ind >= argc)
{
/* no source code specified */
2008-12-16 03:56:48 +00:00
ABORT (oops);
2008-08-19 05:21:48 +00:00
}
/* the source code is the string, not from the file */
2008-12-21 21:35:07 +00:00
ao->ist = QSE_AWK_PARSE_STRING;
2008-12-13 03:42:32 +00:00
ao->isp = argv[opt.ind++];
2008-12-31 00:08:03 +00:00
free (isf);
2008-07-26 09:01:27 +00:00
}
else
{
2008-12-21 21:35:07 +00:00
ao->ist = QSE_AWK_PARSE_FILES;
2008-12-13 03:42:32 +00:00
ao->isp = isf;
2008-07-26 09:01:27 +00:00
}
2008-07-27 09:37:38 +00:00
/* the remaining arguments are input console file names */
icfc = (opt.ind >= argc)? 2: (argc - opt.ind + 1);
2008-12-21 21:35:07 +00:00
icf = (qse_char_t**) malloc (QSE_SIZEOF(*icf)*icfc);
if (icf == QSE_NULL)
{
out_of_memory ();
2008-12-16 03:56:48 +00:00
ABORT (oops);
}
if (opt.ind >= argc)
{
/* no input(console) file names are specified.
* the standard input becomes the input console */
2008-12-21 21:35:07 +00:00
icf[icfl++] = QSE_T("");
}
else
{
do { icf[icfl++] = argv[opt.ind++]; } while (opt.ind < argc);
}
2008-12-21 21:35:07 +00:00
icf[icfl] = QSE_NULL;
2008-07-23 08:22:24 +00:00
2008-10-14 05:32:58 +00:00
ao->osf = osf;
ao->icf = icf;
ao->icfl = icfl;
2008-10-01 05:14:20 +00:00
ao->vm = vm;
return 0;
2008-10-01 05:14:20 +00:00
2008-12-16 03:56:48 +00:00
oops:
2008-12-21 21:35:07 +00:00
if (vm != QSE_NULL) qse_map_close (vm);
if (icf != QSE_NULL) free (icf);
if (isf != QSE_NULL) free (isf);
2008-10-01 05:14:20 +00:00
return -1;
2008-07-23 08:22:24 +00:00
}
2008-12-21 21:35:07 +00:00
static qse_awk_t* open_awk (void)
2008-08-04 08:06:43 +00:00
{
2008-12-21 21:35:07 +00:00
qse_awk_t* awk;
2008-08-04 08:06:43 +00:00
2008-12-21 21:35:07 +00:00
awk = qse_awk_opensimple (0);
if (awk == QSE_NULL)
2008-08-04 08:06:43 +00:00
{
2008-12-21 21:35:07 +00:00
qse_printf (QSE_T("ERROR: cannot open awk\n"));
return QSE_NULL;
2008-08-04 08:06:43 +00:00
}
2008-08-19 05:21:48 +00:00
/* TODO: get depth from command line */
2008-12-21 21:35:07 +00:00
qse_awk_setmaxdepth (
awk, QSE_AWK_DEPTH_BLOCK_PARSE | QSE_AWK_DEPTH_EXPR_PARSE, 50);
qse_awk_setmaxdepth (
awk, QSE_AWK_DEPTH_BLOCK_RUN | QSE_AWK_DEPTH_EXPR_RUN, 500);
2008-08-04 08:06:43 +00:00
2008-08-19 05:21:48 +00:00
/*
2008-12-21 21:35:07 +00:00
qse_awk_seterrstr (awk, QSE_AWK_EGBLRED,
QSE_T("\uC804\uC5ED\uBCC0\uC218 \'%.*s\'\uAC00 \uC7AC\uC815\uC758 \uB418\uC5C8\uC2B5\uB2C8\uB2E4"));
qse_awk_seterrstr (awk, QSE_AWK_EAFNRED,
QSE_T("\uD568\uC218 \'%.*s\'\uAC00 \uC7AC\uC815\uC758 \uB418\uC5C8\uC2B5\uB2C8\uB2E4"));
2008-08-19 05:21:48 +00:00
*/
2008-12-21 21:35:07 +00:00
/*qse_awk_setkeyword (awk, QSE_T("func"), 4, QSE_T("FX"), 2);*/
2008-08-04 08:06:43 +00:00
2008-12-21 21:35:07 +00:00
if (qse_awk_addfunc (awk,
QSE_T("sleep"), 5, 0,
1, 1, QSE_NULL, bfn_sleep) == QSE_NULL)
2008-08-19 05:21:48 +00:00
{
2008-12-21 21:35:07 +00:00
qse_awk_close (awk);
qse_printf (QSE_T("ERROR: cannot add function 'sleep'\n"));
return QSE_NULL;
2008-08-19 05:21:48 +00:00
}
2008-08-04 08:06:43 +00:00
return awk;
}
2008-12-21 21:35:07 +00:00
static int awk_main (int argc, qse_char_t* argv[])
2008-07-21 06:42:39 +00:00
{
2008-12-21 21:35:07 +00:00
qse_awk_t* awk;
2008-07-21 06:42:39 +00:00
2008-12-21 21:35:07 +00:00
qse_awk_runcbs_t runcbs;
2008-12-16 03:56:48 +00:00
2008-08-04 08:06:43 +00:00
int i, file_count = 0;
2008-12-21 21:35:07 +00:00
const qse_char_t* mfn = QSE_NULL;
2008-07-21 06:42:39 +00:00
int mode = 0;
int runarg_count = 0;
2008-12-21 21:35:07 +00:00
qse_awk_runarg_t runarg[128];
2008-07-21 06:42:39 +00:00
int deparse = 0;
2008-10-01 05:14:20 +00:00
struct argout_t ao;
int ret = 0;
2008-10-01 05:14:20 +00:00
2008-12-21 21:35:07 +00:00
qse_memset (&ao, 0, QSE_SIZEOF(ao));
i = handle_args (argc, argv, &ao);
2008-07-27 09:37:38 +00:00
if (i == -1)
2008-07-21 06:42:39 +00:00
{
print_usage (argv[0]);
return -1;
}
2008-07-27 09:37:38 +00:00
if (i == 1) return 0;
2008-07-21 06:42:39 +00:00
runarg[runarg_count].ptr = NULL;
runarg[runarg_count].len = 0;
2008-08-04 08:06:43 +00:00
awk = open_awk ();
2008-12-21 21:35:07 +00:00
if (awk == QSE_NULL) return -1;
app_awk = awk;
2008-12-21 21:35:07 +00:00
if (qse_awk_parsesimple (awk, ao.isp, ao.ist, ao.osf) == -1)
2008-10-01 05:14:20 +00:00
{
2008-12-21 21:35:07 +00:00
qse_printf (
QSE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
qse_awk_geterrnum(awk),
(unsigned int)qse_awk_geterrlin(awk),
qse_awk_geterrmsg(awk)
2008-12-12 04:05:28 +00:00
);
ret = -1;
goto oops;
2008-10-01 05:14:20 +00:00
}
#ifdef _WIN32
SetConsoleCtrlHandler (stop_run, TRUE);
#else
signal (SIGINT, stop_run);
#endif
2008-12-16 03:56:48 +00:00
runcbs.on_start = on_run_start;
runcbs.on_statement = on_run_statement;
runcbs.on_return = on_run_return;
runcbs.on_end = on_run_end;
2008-12-21 21:35:07 +00:00
runcbs.data = QSE_NULL;
2008-12-16 03:56:48 +00:00
2008-12-21 21:35:07 +00:00
if (qse_awk_runsimple (awk, ao.icf, &runcbs) == -1)
{
2008-12-21 21:35:07 +00:00
qse_printf (
QSE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
qse_awk_geterrnum(awk),
(unsigned int)qse_awk_geterrlin(awk),
qse_awk_geterrmsg(awk)
2008-12-12 04:05:28 +00:00
);
ret = -1;
goto oops;
}
oops:
2008-12-21 21:35:07 +00:00
qse_awk_close (awk);
2008-08-19 05:21:48 +00:00
2008-12-21 21:35:07 +00:00
if (ao.ist == QSE_AWK_PARSE_FILES && ao.isp != QSE_NULL) free (ao.isp);
2008-12-31 00:08:03 +00:00
/*if (ao.osf != QSE_NULL) free (ao.osf);*/
2008-12-21 21:35:07 +00:00
if (ao.icf != QSE_NULL) free (ao.icf);
if (ao.vm != QSE_NULL) qse_map_close (ao.vm);
2008-09-30 05:07:47 +00:00
return ret;
}
2008-12-21 21:35:07 +00:00
int qse_main (int argc, qse_achar_t* argv[])
{
int n;
#if defined(_WIN32) && defined(_DEBUG) && defined(_MSC_VER)
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
#endif
2008-12-21 21:35:07 +00:00
n = qse_runmain (argc, argv, awk_main);
#if defined(_WIN32) && defined(_DEBUG)
/*#if defined(_MSC_VER)
_CrtDumpMemoryLeaks ();
#endif*/
#endif
return n;
}