2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2011-08-15 03:07:31 +00:00
|
|
|
* $Id: awk.c 549 2011-08-14 09:07:31Z hyunghwan.chung $
|
2009-02-17 02:11:31 +00:00
|
|
|
*
|
2011-04-23 08:28:43 +00:00
|
|
|
Copyright 2006-2011 Chung, Hyung-Hwan.
|
2009-09-16 04:01:02 +00:00
|
|
|
This file is part of QSE.
|
2009-02-17 02:11:31 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
QSE is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of
|
|
|
|
the License, or (at your option) any later version.
|
2009-02-17 02:11:31 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
QSE is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
2009-02-17 02:11:31 +00:00
|
|
|
|
2009-09-16 04:01:02 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
#include <qse/awk/awk.h>
|
2009-02-23 08:10:34 +00:00
|
|
|
#include <qse/awk/std.h>
|
2008-12-21 21:35:07 +00:00
|
|
|
#include <qse/cmn/sll.h>
|
|
|
|
#include <qse/cmn/mem.h>
|
|
|
|
#include <qse/cmn/chr.h>
|
|
|
|
#include <qse/cmn/opt.h>
|
2011-10-14 22:57:41 +00:00
|
|
|
#include <qse/cmn/path.h>
|
2009-06-04 15:50:32 +00:00
|
|
|
#include <qse/cmn/stdio.h>
|
|
|
|
#include <qse/cmn/main.h>
|
2010-07-25 06:43:26 +00:00
|
|
|
#include <qse/cmn/xma.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-08-06 01:31:17 +00:00
|
|
|
#define ENABLE_CALLBACK
|
2008-10-01 05:14:20 +00:00
|
|
|
#define ABORT(label) goto label
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
#if defined(_WIN32)
|
2009-01-19 08:32:51 +00:00
|
|
|
# include <windows.h>
|
|
|
|
# include <tchar.h>
|
|
|
|
# include <process.h>
|
2011-04-18 09:28:22 +00:00
|
|
|
#elif defined(__OS2__)
|
2011-04-18 09:38:21 +00:00
|
|
|
# define INCL_DOSPROCESS
|
2011-04-18 09:28:22 +00:00
|
|
|
# define INCL_DOSEXCEPTIONS
|
|
|
|
# define INCL_ERRORS
|
|
|
|
# include <os2.h>
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
# include <dos.h>
|
2008-10-13 09:08:26 +00:00
|
|
|
#else
|
2009-01-19 08:32:51 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <errno.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
static qse_awk_rtx_t* app_rtx = QSE_NULL;
|
2008-12-17 03:42:48 +00:00
|
|
|
static int app_debug = 0;
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
struct arg_t
|
2009-01-22 04:37:55 +00:00
|
|
|
{
|
2009-02-23 08:10:34 +00:00
|
|
|
qse_awk_parsestd_type_t ist; /* input source type */
|
2009-02-22 08:16:35 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const qse_char_t* str;
|
|
|
|
qse_char_t** files;
|
|
|
|
} isp;
|
2009-01-22 04:37:55 +00:00
|
|
|
qse_size_t isfl; /* the number of input source files */
|
2009-02-22 08:16:35 +00:00
|
|
|
|
2009-02-23 08:10:34 +00:00
|
|
|
qse_awk_parsestd_type_t ost; /* output source type */
|
2009-01-22 04:37:55 +00:00
|
|
|
qse_char_t* osf; /* output source file */
|
2009-02-22 08:16:35 +00:00
|
|
|
|
2009-01-22 04:37:55 +00:00
|
|
|
qse_char_t** icf; /* input console files */
|
|
|
|
qse_size_t icfl; /* the number of input console files */
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_t* gvm; /* global variable map */
|
2009-01-22 04:37:55 +00:00
|
|
|
qse_char_t* fs; /* field separator */
|
2009-06-22 07:33:05 +00:00
|
|
|
qse_char_t* call; /* function to call */
|
|
|
|
|
|
|
|
int opton;
|
|
|
|
int optoff;
|
2010-07-29 07:27:03 +00:00
|
|
|
qse_ulong_t memlimit;
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
qse_ulong_t failmalloc;
|
|
|
|
#endif
|
2009-01-22 04:37:55 +00:00
|
|
|
};
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
struct gvmv_t
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
qse_char_t* ptr;
|
|
|
|
qse_size_t len;
|
|
|
|
};
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
static void dprint (const qse_char_t* fmt, ...)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-12-17 03:42:48 +00:00
|
|
|
if (app_debug)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
2009-08-29 05:58:05 +00:00
|
|
|
qse_vfprintf (QSE_STDERR, fmt, ap);
|
2008-12-17 03:42:48 +00:00
|
|
|
va_end (ap);
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2011-04-18 09:28:22 +00:00
|
|
|
#if defined(_WIN32)
|
2007-05-02 01:07:00 +00:00
|
|
|
static BOOL WINAPI stop_run (DWORD ctrl_type)
|
|
|
|
{
|
|
|
|
if (ctrl_type == CTRL_C_EVENT ||
|
|
|
|
ctrl_type == CTRL_CLOSE_EVENT)
|
|
|
|
{
|
2009-02-14 04:57:09 +00:00
|
|
|
qse_awk_rtx_stop (app_rtx);
|
2007-05-02 01:07:00 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-04-18 09:28:22 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
|
|
|
|
static ULONG _System stop_run (
|
|
|
|
PEXCEPTIONREPORTRECORD p1,
|
|
|
|
PEXCEPTIONREGISTRATIONRECORD p2,
|
|
|
|
PCONTEXTRECORD p3,
|
|
|
|
PVOID pv)
|
|
|
|
{
|
|
|
|
if (p1->ExceptionNum == XCPT_SIGNAL)
|
|
|
|
{
|
|
|
|
if (p1->ExceptionInfo[0] == XCPT_SIGNAL_INTR ||
|
|
|
|
p1->ExceptionInfo[0] == XCPT_SIGNAL_KILLPROC ||
|
|
|
|
p1->ExceptionInfo[0] == XCPT_SIGNAL_BREAK)
|
|
|
|
{
|
|
|
|
APIRET rc;
|
|
|
|
|
|
|
|
qse_awk_rtx_stop (app_rtx);
|
|
|
|
rc = DosAcknowledgeSignalException (p1->ExceptionInfo[0]);
|
|
|
|
return (rc != NO_ERROR)? 1: XCPT_CONTINUE_EXECUTION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return XCPT_CONTINUE_SEARCH; /* exception not resolved */
|
|
|
|
}
|
2011-05-10 10:11:13 +00:00
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
|
|
|
|
static void setsignal (int sig, void(*handler)(int))
|
|
|
|
{
|
|
|
|
signal (sig, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_run (int sig)
|
|
|
|
{
|
|
|
|
qse_awk_rtx_stop (app_rtx);
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
#else
|
2009-01-27 09:26:15 +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);
|
|
|
|
|
|
|
|
sa_int.sa_flags = 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
static void stop_run (int sig)
|
|
|
|
{
|
2009-01-19 08:32:51 +00:00
|
|
|
int e = errno;
|
2009-02-14 04:57:09 +00:00
|
|
|
qse_awk_rtx_stop (app_rtx);
|
2009-01-19 08:32:51 +00:00
|
|
|
errno = e;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2009-01-27 09:26:15 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2011-04-21 08:30:47 +00:00
|
|
|
#if defined(__OS2__)
|
|
|
|
static EXCEPTIONREGISTRATIONRECORD os2_excrr = { 0 };
|
|
|
|
#endif
|
|
|
|
|
2009-01-19 08:32:51 +00:00
|
|
|
static void set_intr_run (void)
|
|
|
|
{
|
2011-04-18 09:28:22 +00:00
|
|
|
#if defined(_WIN32)
|
2009-01-19 08:32:51 +00:00
|
|
|
SetConsoleCtrlHandler (stop_run, TRUE);
|
2011-04-18 09:28:22 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
APIRET rc;
|
2011-04-21 08:30:47 +00:00
|
|
|
os2_excrr.ExceptionHandler = (ERR)stop_run;
|
|
|
|
rc = DosSetExceptionHandler (&os2_excrr);
|
2011-04-18 09:28:22 +00:00
|
|
|
/*if (rc != NO_ERROR)...*/
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
setsignal (SIGINT, stop_run);
|
2009-01-19 08:32:51 +00:00
|
|
|
#else
|
2009-02-15 00:21:19 +00:00
|
|
|
/*setsignal (SIGINT, stop_run, 1); TO BE MORE COMPATIBLE WITH WIN32*/
|
|
|
|
setsignal (SIGINT, stop_run, 0);
|
2009-01-19 08:32:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unset_intr_run (void)
|
|
|
|
{
|
2011-04-18 09:28:22 +00:00
|
|
|
#if defined(_WIN32)
|
2009-01-19 08:32:51 +00:00
|
|
|
SetConsoleCtrlHandler (stop_run, FALSE);
|
2011-04-18 09:28:22 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
APIRET rc;
|
2011-04-21 08:30:47 +00:00
|
|
|
rc = DosUnsetExceptionHandler (&os2_excrr);
|
2011-04-18 09:28:22 +00:00
|
|
|
/*if (rc != NO_ERROR) ...*/
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
setsignal (SIGINT, SIG_DFL);
|
2009-01-19 08:32:51 +00:00
|
|
|
#else
|
2009-01-27 09:26:15 +00:00
|
|
|
setsignal (SIGINT, SIG_DFL, 1);
|
2009-01-19 08:32:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
static qse_htb_walk_t print_awk_value (
|
|
|
|
qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-06-15 02:40:52 +00:00
|
|
|
qse_awk_rtx_t* rtx = (qse_awk_rtx_t*)arg;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t* str;
|
|
|
|
qse_size_t len;
|
2009-06-15 02:40:52 +00:00
|
|
|
qse_awk_errinf_t oerrinf;
|
2008-12-11 04:19:59 +00:00
|
|
|
|
2009-06-15 02:40:52 +00:00
|
|
|
qse_awk_rtx_geterrinf (rtx, &oerrinf);
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
str = qse_awk_rtx_valtocpldup (rtx, QSE_HTB_VPTR(pair), &len);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (str == QSE_NULL)
|
2008-12-11 04:19:59 +00:00
|
|
|
{
|
2009-06-15 02:40:52 +00:00
|
|
|
if (qse_awk_rtx_geterrnum(rtx) == QSE_AWK_EVALTYPE)
|
|
|
|
{
|
|
|
|
dprint (QSE_T("%.*s = [not printable]\n"),
|
2010-07-09 00:58:44 +00:00
|
|
|
(int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair));
|
2009-06-15 02:40:52 +00:00
|
|
|
|
|
|
|
qse_awk_rtx_seterrinf (rtx, &oerrinf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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"),
|
2010-07-09 00:58:44 +00:00
|
|
|
(int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair),
|
2008-12-11 04:19:59 +00:00
|
|
|
(int)len, str);
|
2011-07-21 10:17:16 +00:00
|
|
|
qse_awk_freemem (qse_awk_rtx_getawk(rtx), str);
|
2008-12-11 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
return QSE_HTB_WALK_FORWARD;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
static qse_htb_walk_t set_global (
|
|
|
|
qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
|
2009-06-21 06:47:34 +00:00
|
|
|
{
|
|
|
|
qse_awk_val_t* v;
|
|
|
|
qse_awk_rtx_t* rtx = (qse_awk_rtx_t*)arg;
|
2010-07-09 00:58:44 +00:00
|
|
|
struct gvmv_t* gvmv = (struct gvmv_t*)QSE_HTB_VPTR(pair);
|
2009-06-21 06:47:34 +00:00
|
|
|
|
|
|
|
v = qse_awk_rtx_makenstrval (rtx, gvmv->ptr, gvmv->len);
|
2010-07-09 00:58:44 +00:00
|
|
|
if (v == QSE_NULL) return QSE_HTB_WALK_STOP;
|
2009-06-21 06:47:34 +00:00
|
|
|
|
|
|
|
qse_awk_rtx_refupval (rtx, v);
|
|
|
|
qse_awk_rtx_setgbl (rtx, gvmv->idx, v);
|
|
|
|
qse_awk_rtx_refdownval (rtx, v);
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
return QSE_HTB_WALK_FORWARD;
|
2009-06-21 06:47:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
static int apply_fs_and_gvm (qse_awk_rtx_t* rtx, struct arg_t* arg)
|
2009-01-23 04:40:57 +00:00
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
if (arg->fs != QSE_NULL)
|
2009-01-23 04:40:57 +00:00
|
|
|
{
|
|
|
|
qse_awk_val_t* fs;
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* compose a string value to use to set FS to */
|
2009-06-21 06:47:34 +00:00
|
|
|
fs = qse_awk_rtx_makestrval0 (rtx, arg->fs);
|
2009-01-23 04:40:57 +00:00
|
|
|
if (fs == QSE_NULL) return -1;
|
|
|
|
|
2009-02-16 08:31:34 +00:00
|
|
|
/* change FS according to the command line argument */
|
|
|
|
qse_awk_rtx_refupval (rtx, fs);
|
|
|
|
qse_awk_rtx_setgbl (rtx, QSE_AWK_GBL_FS, fs);
|
|
|
|
qse_awk_rtx_refdownval (rtx, fs);
|
2009-01-23 04:40:57 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
if (arg->gvm != QSE_NULL)
|
|
|
|
{
|
|
|
|
/* set the value of user-defined global variables
|
|
|
|
* to a runtime context */
|
2009-08-17 07:44:20 +00:00
|
|
|
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOERR, QSE_NULL);
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_walk (arg->gvm, set_global, rtx);
|
2009-06-21 06:47:34 +00:00
|
|
|
if (qse_awk_rtx_geterrnum(rtx) != QSE_AWK_ENOERR) return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-23 04:40:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-17 02:27:53 +00:00
|
|
|
static void dprint_return (qse_awk_rtx_t* rtx, qse_awk_val_t* ret)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
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
|
|
|
|
{
|
2009-03-02 03:58:19 +00:00
|
|
|
str = qse_awk_rtx_valtocpldup (rtx, ret, &len);
|
2008-12-21 21:35:07 +00:00
|
|
|
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);
|
2011-07-21 10:17:16 +00:00
|
|
|
qse_awk_freemem (qse_awk_rtx_getawk(rtx), str);
|
2008-12-16 03:56:48 +00:00
|
|
|
}
|
2008-12-11 04:19:59 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
dprint (QSE_T("[NAMED VARIABLES]\n"));
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_walk (qse_awk_rtx_getnvmap(rtx), print_awk_value, rtx);
|
2008-12-21 21:35:07 +00:00
|
|
|
dprint (QSE_T("[END NAMED VARIABLES]\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2010-08-06 01:31:17 +00:00
|
|
|
#ifdef ENABLE_CALLBACK
|
2009-06-22 07:33:05 +00:00
|
|
|
static void on_statement (
|
2010-08-06 01:31:17 +00:00
|
|
|
qse_awk_rtx_t* rtx, qse_awk_nde_t* nde, void* data)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2011-05-22 10:20:01 +00:00
|
|
|
dprint (QSE_T("running %d at line %d\n"), (int)nde->type, (int)nde->loc.line);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2009-07-17 06:43:47 +00:00
|
|
|
#endif
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-07-17 02:27:53 +00:00
|
|
|
static int fnc_sleep (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
2007-05-26 01:30:00 +00:00
|
|
|
{
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t nargs;
|
|
|
|
qse_awk_val_t* a0;
|
|
|
|
qse_long_t lv;
|
2011-11-22 05:03:31 +00:00
|
|
|
qse_flt_t rv;
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_awk_val_t* r;
|
2007-05-26 01:30:00 +00:00
|
|
|
int n;
|
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
nargs = qse_awk_rtx_getnargs (run);
|
2008-12-21 21:35:07 +00:00
|
|
|
QSE_ASSERT (nargs == 1);
|
2007-05-26 01:30:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
a0 = qse_awk_rtx_getarg (run, 0);
|
2007-05-26 01:30:00 +00:00
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
n = qse_awk_rtx_valtonum (run, a0, &lv, &rv);
|
2007-05-26 01:30:00 +00:00
|
|
|
if (n == -1) return -1;
|
2008-12-21 21:35:07 +00:00
|
|
|
if (n == 1) lv = (qse_long_t)rv;
|
2007-05-26 01:30:00 +00:00
|
|
|
|
2011-04-18 09:38:21 +00:00
|
|
|
#if defined(_WIN32)
|
2007-10-02 00:22:00 +00:00
|
|
|
Sleep ((DWORD)(lv * 1000));
|
2007-05-26 01:30:00 +00:00
|
|
|
n = 0;
|
2011-04-18 09:38:21 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
DosSleep ((ULONG)(lv * 1000));
|
|
|
|
n = 0;
|
2007-05-26 01:30:00 +00:00
|
|
|
#else
|
|
|
|
n = sleep (lv);
|
|
|
|
#endif
|
|
|
|
|
2009-02-01 03:59:46 +00:00
|
|
|
r = qse_awk_rtx_makeintval (run, n);
|
2009-11-23 07:58:53 +00:00
|
|
|
if (r == QSE_NULL) return -1;
|
2007-05-26 01:30:00 +00:00
|
|
|
|
2009-01-31 22:03:05 +00:00
|
|
|
qse_awk_rtx_setretval (run, r);
|
2007-05-26 01:30:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
static void print_err (const qse_char_t* fmt, ...)
|
2008-08-08 05:02:08 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
va_list va;
|
|
|
|
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ERROR: "));
|
|
|
|
|
|
|
|
va_start (va, fmt);
|
|
|
|
qse_vfprintf (QSE_STDERR, fmt, va);
|
|
|
|
va_end (va);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct opttab_t
|
|
|
|
{
|
|
|
|
const qse_char_t* name;
|
|
|
|
int opt;
|
|
|
|
const qse_char_t* desc;
|
|
|
|
} opttab[] =
|
|
|
|
{
|
2011-05-02 07:28:51 +00:00
|
|
|
{ QSE_T("implicit"), QSE_AWK_IMPLICIT, QSE_T("allow undeclared variables") },
|
|
|
|
{ QSE_T("explicit"), QSE_AWK_EXPLICIT, QSE_T("allow declared variables(local,global)") },
|
2011-07-24 03:03:48 +00:00
|
|
|
{ QSE_T("extraops"), QSE_AWK_EXTRAOPS, QSE_T("enable extra operators(<<,>>,^^,\\)") },
|
2011-05-02 07:28:51 +00:00
|
|
|
{ QSE_T("rio"), QSE_AWK_RIO, QSE_T("enable builtin I/O including getline & print") },
|
|
|
|
{ QSE_T("rwpipe"), QSE_AWK_RWPIPE, QSE_T("allow a dual-directional pipe") },
|
|
|
|
{ QSE_T("newline"), QSE_AWK_NEWLINE, QSE_T("enable a newline to terminate a statement") },
|
|
|
|
{ QSE_T("striprecspc"), QSE_AWK_STRIPRECSPC, QSE_T("strip spaces in splitting a record") },
|
|
|
|
{ QSE_T("stripstrspc"), QSE_AWK_STRIPSTRSPC, QSE_T("strip spaces in converting a string to a number") },
|
|
|
|
{ QSE_T("nextofile"), QSE_AWK_NEXTOFILE, QSE_T("enable 'nextofile'") },
|
|
|
|
{ QSE_T("reset"), QSE_AWK_RESET, QSE_T("enable 'reset'") },
|
|
|
|
{ QSE_T("crlf"), QSE_AWK_CRLF, QSE_T("use CRLF for a newline") },
|
|
|
|
{ QSE_T("maptovar"), QSE_AWK_MAPTOVAR, QSE_T("allow a map to be assigned or returned") },
|
|
|
|
{ QSE_T("pablock"), QSE_AWK_PABLOCK, QSE_T("enable pattern-action loop") },
|
|
|
|
{ QSE_T("rexbound"), QSE_AWK_REXBOUND, QSE_T("enable {n,m} in a regular expression") },
|
|
|
|
{ QSE_T("ncmponstr"), QSE_AWK_NCMPONSTR, QSE_T("perform numeric comparsion on numeric strings") },
|
|
|
|
{ QSE_T("strictnaming"), QSE_AWK_STRICTNAMING, QSE_T("enable the strict naming rule") },
|
|
|
|
{ QSE_T("include"), QSE_AWK_INCLUDE, QSE_T("enable 'include'") },
|
2011-05-19 08:36:40 +00:00
|
|
|
{ QSE_NULL, 0, QSE_NULL }
|
2009-06-22 07:33:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void print_usage (QSE_FILE* out, const qse_char_t* argv0)
|
|
|
|
{
|
|
|
|
int j;
|
2009-09-05 07:08:19 +00:00
|
|
|
const qse_char_t* b = qse_basename (argv0);
|
2009-06-22 07:33:05 +00:00
|
|
|
|
2009-09-05 07:08:19 +00:00
|
|
|
qse_fprintf (out, QSE_T("USAGE: %s [options] -f sourcefile [ -- ] [datafile]*\n"), b);
|
|
|
|
qse_fprintf (out, QSE_T(" %s [options] [ -- ] sourcestring [datafile]*\n"), b);
|
2009-06-22 07:33:05 +00:00
|
|
|
qse_fprintf (out, QSE_T("Where options are:\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -h/--help print this message\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -d show extra information\n"));
|
2009-06-23 07:01:28 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -c/--call name call a function instead of entering\n"));
|
2009-06-22 07:33:05 +00:00
|
|
|
qse_fprintf (out, QSE_T(" the pattern-action loop\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -f/--file sourcefile set the source script file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -o/--deparsed-file deparsedfile set the deparsing output file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -F/--field-separator string set a field separator(FS)\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -v/--assign var=value add a global variable with a value\n"));
|
2010-07-29 07:27:03 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -m/--memory-limit number limit the memory usage (bytes)\n"));
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
qse_fprintf (out, QSE_T(" -X number fail the number'th memory allocation\n"));
|
|
|
|
#endif
|
2009-06-22 07:33:05 +00:00
|
|
|
|
|
|
|
for (j = 0; opttab[j].name != QSE_NULL; j++)
|
|
|
|
{
|
|
|
|
qse_fprintf (out, QSE_T(" --%-18s on/off %s\n"), opttab[j].name, opttab[j].desc);
|
|
|
|
}
|
2008-08-08 05:02:08 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
static int comparg (int argc, qse_char_t* argv[], struct arg_t* arg)
|
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
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
{ QSE_T(":implicit"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":explicit"), QSE_T('\0') },
|
2009-07-18 06:42:02 +00:00
|
|
|
{ QSE_T(":extraops"), QSE_T('\0') },
|
2009-06-22 07:33:05 +00:00
|
|
|
{ QSE_T(":rio"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":rwpipe"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":newline"), QSE_T('\0') },
|
2009-07-18 06:42:02 +00:00
|
|
|
{ QSE_T(":striprecspc"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":stripstrspc"), QSE_T('\0') },
|
2009-06-22 07:33:05 +00:00
|
|
|
{ QSE_T(":nextofile"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":reset"), QSE_T('\0') },
|
2009-06-23 07:01:28 +00:00
|
|
|
{ QSE_T(":crlf"), QSE_T('\0') },
|
2009-06-22 07:33:05 +00:00
|
|
|
{ QSE_T(":maptovar"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":pablock"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":rexbound"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":ncmponstr"), QSE_T('\0') },
|
2009-06-29 07:41:47 +00:00
|
|
|
{ QSE_T(":strictnaming"), QSE_T('\0') },
|
2009-07-19 06:02:24 +00:00
|
|
|
{ QSE_T(":include"), QSE_T('\0') },
|
2009-06-22 07:33:05 +00:00
|
|
|
|
|
|
|
{ QSE_T(":call"), QSE_T('c') },
|
2008-12-21 21:35:07 +00:00
|
|
|
{ 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') },
|
2010-07-29 07:27:03 +00:00
|
|
|
{ QSE_T(":memory-limit"), QSE_T('m') },
|
2008-12-21 21:35:07 +00:00
|
|
|
|
2011-05-19 08:36:40 +00:00
|
|
|
{ QSE_T("help"), QSE_T('h') },
|
|
|
|
{ QSE_NULL, QSE_T('\0') }
|
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
|
|
|
{
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
QSE_T("dc:f:F:o:v:m:X:h"),
|
|
|
|
#else
|
2010-07-29 07:27:03 +00:00
|
|
|
QSE_T("dc:f:F:o:v:m:h"),
|
2011-08-14 10:04:14 +00:00
|
|
|
#endif
|
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-10 02:50:16 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_size_t icfc = 0; /* the capacity of icf */
|
2009-06-10 07:07:42 +00:00
|
|
|
qse_size_t icfl = 0; /* the number of input console files */
|
2008-12-10 02:50:16 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t** isf = QSE_NULL; /* input source files */
|
2009-02-22 06:28:02 +00:00
|
|
|
qse_char_t* osf = QSE_NULL; /* output source file */
|
2008-12-21 21:35:07 +00:00
|
|
|
qse_char_t** icf = QSE_NULL; /* input console files */
|
2008-12-10 02:50:16 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_t* gvm = QSE_NULL; /* global variable map */
|
2009-01-22 04:37:55 +00:00
|
|
|
qse_char_t* fs = QSE_NULL; /* field separator */
|
2009-06-22 07:33:05 +00:00
|
|
|
qse_char_t* call = QSE_NULL; /* function to call */
|
2009-01-22 04:37:55 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
memset (arg, 0, QSE_SIZEOF(*arg));
|
2008-12-10 02:50:16 +00:00
|
|
|
|
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
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("out of memory\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
2010-10-28 06:54:37 +00:00
|
|
|
gvm = qse_htb_open (
|
|
|
|
QSE_NULL, 0, 30, 70,
|
|
|
|
QSE_SIZEOF(qse_char_t), QSE_SIZEOF(struct gvmv_t)
|
|
|
|
);
|
2009-06-21 06:47:34 +00:00
|
|
|
if (gvm == QSE_NULL)
|
2008-09-30 05:07:47 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("out of memory\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
2010-10-28 06:54:37 +00:00
|
|
|
|
2010-10-30 07:54:36 +00:00
|
|
|
qse_htb_setmancbs (gvm,
|
|
|
|
qse_htb_mancbs(QSE_HTB_MANCBS_INLINE_VALUE_COPIER)
|
|
|
|
);
|
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-12-21 21:35:07 +00:00
|
|
|
case QSE_T('h'):
|
|
|
|
if (isf != QSE_NULL) free (isf);
|
2010-07-09 00:58:44 +00:00
|
|
|
if (gvm != QSE_NULL) qse_htb_close (gvm);
|
2009-06-21 06:47:34 +00:00
|
|
|
return 0;
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_T('d'):
|
2008-12-17 03:42:48 +00:00
|
|
|
{
|
|
|
|
app_debug = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
case QSE_T('c'):
|
|
|
|
{
|
|
|
|
call = opt.arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
case QSE_T('f'):
|
2008-08-08 05:02:08 +00:00
|
|
|
{
|
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)
|
2008-10-13 09:08:26 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("out of memory\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2008-10-13 09:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isf = tmp;
|
|
|
|
isfc = isfc + 16;
|
2008-08-11 02:27:21 +00:00
|
|
|
}
|
2008-08-08 05:02:08 +00:00
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
isf[isfl++] = opt.arg;
|
2008-07-27 09:37:38 +00:00
|
|
|
break;
|
2008-08-08 05:02:08 +00:00
|
|
|
}
|
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
|
|
|
{
|
2009-01-22 04:37:55 +00:00
|
|
|
fs = 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
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
struct gvmv_t gvmv;
|
|
|
|
qse_char_t* eq;
|
|
|
|
|
|
|
|
eq = qse_strchr(opt.arg, QSE_T('='));
|
2009-06-22 07:33:05 +00:00
|
|
|
if (eq == QSE_NULL)
|
2008-09-30 05:07:47 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
if (opt.lngopt)
|
|
|
|
print_err (QSE_T("no value for '%s' in '%s'\n"), opt.arg, opt.lngopt);
|
|
|
|
else
|
|
|
|
print_err (QSE_T("no value for '%s' in '%c'\n"), opt.arg, opt.opt);
|
2009-06-21 06:47:34 +00:00
|
|
|
goto 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
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
gvmv.idx = -1;
|
|
|
|
gvmv.ptr = ++eq;
|
|
|
|
gvmv.len = qse_strlen(eq);
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
if (qse_htb_upsert (gvm, opt.arg, qse_strlen(opt.arg), &gvmv, 1) == QSE_NULL)
|
2008-09-30 05:07:47 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("out of memory\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-29 07:27:03 +00:00
|
|
|
case QSE_T('m'):
|
|
|
|
{
|
|
|
|
arg->memlimit = qse_strtoulong (opt.arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
case QSE_T('X'):
|
|
|
|
{
|
|
|
|
arg->failmalloc = qse_strtoulong (opt.arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
case QSE_T('\0'):
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
/* a long option with no corresponding short option */
|
|
|
|
qse_size_t i;
|
|
|
|
for (i = 0; opttab[i].name != QSE_NULL; i++)
|
2008-07-27 09:37:38 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
if (qse_strcmp (opt.lngopt, opttab[i].name) == 0)
|
|
|
|
{
|
|
|
|
if (qse_strcmp (opt.arg, QSE_T("off")) == 0)
|
|
|
|
arg->optoff |= opttab[i].opt;
|
2009-07-18 06:42:02 +00:00
|
|
|
else if (qse_strcmp (opt.arg, QSE_T("on")) == 0)
|
2009-06-22 07:33:05 +00:00
|
|
|
arg->opton |= opttab[i].opt;
|
2009-07-18 06:42:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
print_err (QSE_T("invalid value for '%s' - '%s'\n"), opt.lngopt, opt.arg);
|
|
|
|
goto oops;
|
|
|
|
}
|
2009-06-22 07:33:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-07-27 09:37:38 +00:00
|
|
|
}
|
2009-06-22 07:33:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_T('?'):
|
|
|
|
{
|
|
|
|
if (opt.lngopt)
|
|
|
|
print_err (QSE_T("illegal option - '%s'\n"), opt.lngopt);
|
2008-07-27 09:37:38 +00:00
|
|
|
else
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("illegal option - '%c'\n"), opt.opt);
|
2008-08-19 21:16:02 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
goto 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)
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("bad argument for '%s'\n"), opt.lngopt);
|
2008-07-27 09:37:38 +00:00
|
|
|
else
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("bad argument for '%c'\n"), opt.opt);
|
2008-08-19 21:16:02 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
goto 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:
|
2009-06-21 06:47:34 +00:00
|
|
|
goto 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
|
|
|
|
2008-10-13 09:08:26 +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 */
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2008-08-19 05:21:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-10 02:50:16 +00:00
|
|
|
/* the source code is the string, not from the file */
|
2009-06-21 06:47:34 +00:00
|
|
|
arg->ist = QSE_AWK_PARSESTD_CP;
|
|
|
|
arg->isp.str = argv[opt.ind++];
|
2008-12-31 00:08:03 +00:00
|
|
|
|
|
|
|
free (isf);
|
2008-07-26 09:01:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
arg->ist = QSE_AWK_PARSESTD_FILE;
|
|
|
|
arg->isp.files = isf;
|
2008-07-26 09:01:27 +00:00
|
|
|
}
|
2008-07-27 09:37:38 +00:00
|
|
|
|
2009-06-11 07:18:25 +00:00
|
|
|
if (opt.ind < argc)
|
2008-12-10 02:50:16 +00:00
|
|
|
{
|
2009-06-11 07:18:25 +00:00
|
|
|
/* the remaining arguments are input console file names */
|
2008-12-10 02:50:16 +00:00
|
|
|
|
2009-06-11 07:18:25 +00:00
|
|
|
icfc = argc - opt.ind + 1;
|
|
|
|
icf = (qse_char_t**) malloc (QSE_SIZEOF(qse_char_t*)*icfc);
|
|
|
|
if (icf == QSE_NULL)
|
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (QSE_T("out of memory\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2009-06-11 07:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opt.ind >= argc)
|
|
|
|
{
|
|
|
|
/* no input(console) file names are specified.
|
|
|
|
* the standard input becomes the input console */
|
|
|
|
icf[icfl++] = QSE_T("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do { icf[icfl++] = argv[opt.ind++]; } while (opt.ind < argc);
|
|
|
|
}
|
|
|
|
icf[icfl] = QSE_NULL;
|
2008-12-10 02:50:16 +00:00
|
|
|
}
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
arg->ost = QSE_AWK_PARSESTD_FILE;
|
|
|
|
arg->osf = osf;
|
2009-02-21 23:06:45 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
arg->icf = icf;
|
|
|
|
arg->icfl = icfl;
|
|
|
|
arg->gvm = gvm;
|
|
|
|
arg->fs = fs;
|
2009-06-22 07:33:05 +00:00
|
|
|
arg->call = call;
|
2008-10-01 05:14:20 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
return 1;
|
2008-10-01 05:14:20 +00:00
|
|
|
|
2008-12-16 03:56:48 +00:00
|
|
|
oops:
|
2010-07-09 00:58:44 +00:00
|
|
|
if (gvm != QSE_NULL) qse_htb_close (gvm);
|
2008-12-21 21:35:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
static void freearg (struct arg_t* arg)
|
2008-08-04 08:06:43 +00:00
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
if (arg->ist == QSE_AWK_PARSESTD_FILE &&
|
|
|
|
arg->isp.files != QSE_NULL) free (arg->isp.files);
|
|
|
|
/*if (arg->osf != QSE_NULL) free (arg->osf);*/
|
|
|
|
if (arg->icf != QSE_NULL) free (arg->icf);
|
2010-07-09 00:58:44 +00:00
|
|
|
if (arg->gvm != QSE_NULL) qse_htb_close (arg->gvm);
|
2009-06-21 06:47:34 +00:00
|
|
|
}
|
2008-08-04 08:06:43 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
static void print_awkerr (qse_awk_t* awk)
|
|
|
|
{
|
2009-08-26 03:50:07 +00:00
|
|
|
const qse_awk_loc_t* loc = qse_awk_geterrloc (awk);
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (
|
2009-08-26 03:50:07 +00:00
|
|
|
QSE_T("CODE %d LINE %u COLUMN %u %s%s%s- %s\n"),
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_geterrnum(awk),
|
2010-08-18 07:15:14 +00:00
|
|
|
(unsigned int)loc->line,
|
|
|
|
(unsigned int)loc->colm,
|
|
|
|
((loc->file == QSE_NULL)? QSE_T(""): QSE_T("FILE ")),
|
|
|
|
((loc->file == QSE_NULL)? QSE_T(""): loc->file),
|
|
|
|
((loc->file == QSE_NULL)? QSE_T(""): QSE_T(" ")),
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_geterrmsg(awk)
|
|
|
|
);
|
|
|
|
}
|
2008-08-04 08:06:43 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
static void print_rtxerr (qse_awk_rtx_t* rtx)
|
|
|
|
{
|
2009-08-26 03:50:07 +00:00
|
|
|
const qse_awk_loc_t* loc = qse_awk_rtx_geterrloc (rtx);
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
print_err (
|
2009-08-26 03:50:07 +00:00
|
|
|
QSE_T("CODE %d LINE %u COLUMN %u %s%s%s- %s\n"),
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_rtx_geterrnum(rtx),
|
2010-08-18 07:15:14 +00:00
|
|
|
(unsigned int)loc->line,
|
|
|
|
(unsigned int)loc->colm,
|
|
|
|
((loc->file == QSE_NULL)? QSE_T(""): QSE_T("FILE ")),
|
|
|
|
((loc->file == QSE_NULL)? QSE_T(""): loc->file),
|
|
|
|
((loc->file == QSE_NULL)? QSE_T(""): QSE_T(" ")),
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_rtx_geterrmsg(rtx)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_walk_t add_global (qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
|
2009-06-21 06:47:34 +00:00
|
|
|
{
|
|
|
|
qse_awk_t* awk = (qse_awk_t*)arg;
|
2010-07-09 00:58:44 +00:00
|
|
|
struct gvmv_t* gvmv = (struct gvmv_t*)QSE_HTB_VPTR(pair);
|
2009-06-21 06:47:34 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
gvmv->idx = qse_awk_addgbl (awk, QSE_HTB_KPTR(pair), QSE_HTB_KLEN(pair));
|
2009-06-21 06:47:34 +00:00
|
|
|
if (gvmv->idx <= -1)
|
2008-08-19 05:21:48 +00:00
|
|
|
{
|
2010-07-09 00:58:44 +00:00
|
|
|
return QSE_HTB_WALK_STOP;
|
2008-08-19 05:21:48 +00:00
|
|
|
}
|
2008-08-04 08:06:43 +00:00
|
|
|
|
2010-07-09 00:58:44 +00:00
|
|
|
return QSE_HTB_WALK_FORWARD;
|
2008-08-04 08:06:43 +00:00
|
|
|
}
|
|
|
|
|
2010-07-29 07:27:03 +00:00
|
|
|
static qse_mmgr_t xma_mmgr =
|
|
|
|
{
|
2011-04-21 08:30:47 +00:00
|
|
|
(qse_mmgr_alloc_t)qse_xma_alloc,
|
|
|
|
(qse_mmgr_realloc_t)qse_xma_realloc,
|
|
|
|
(qse_mmgr_free_t)qse_xma_free,
|
2010-07-29 07:27:03 +00:00
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
static qse_ulong_t debug_mmgr_count = 0;
|
|
|
|
|
|
|
|
static void* debug_mmgr_alloc (void* ctx, qse_size_t size)
|
|
|
|
{
|
|
|
|
struct arg_t* arg = (struct arg_t*)ctx;
|
|
|
|
debug_mmgr_count++;
|
|
|
|
if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL;
|
|
|
|
return malloc (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* debug_mmgr_realloc (void* ctx, void* ptr, qse_size_t size)
|
|
|
|
{
|
|
|
|
struct arg_t* arg = (struct arg_t*)ctx;
|
|
|
|
debug_mmgr_count++;
|
|
|
|
if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL;
|
|
|
|
return realloc (ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void debug_mmgr_free (void* ctx, void* ptr)
|
|
|
|
{
|
|
|
|
free (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static qse_mmgr_t debug_mmgr =
|
|
|
|
{
|
|
|
|
debug_mmgr_alloc,
|
|
|
|
debug_mmgr_realloc,
|
|
|
|
debug_mmgr_free,
|
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2010-07-29 07:27:03 +00:00
|
|
|
static int awk_main (int argc, qse_char_t* argv[])
|
2008-07-21 06:42:39 +00:00
|
|
|
{
|
2009-06-23 07:01:28 +00:00
|
|
|
qse_awk_t* awk = QSE_NULL;
|
|
|
|
qse_awk_rtx_t* rtx = QSE_NULL;
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_awk_val_t* retv;
|
2010-08-06 01:31:17 +00:00
|
|
|
#ifdef ENABLE_CALLBACK
|
2009-02-12 04:46:24 +00:00
|
|
|
qse_awk_rcb_t rcb;
|
2009-07-17 06:43:47 +00:00
|
|
|
#endif
|
2009-01-23 04:40:57 +00:00
|
|
|
int i;
|
2009-06-21 06:47:34 +00:00
|
|
|
struct arg_t arg;
|
|
|
|
int ret = -1;
|
2008-10-01 05:14:20 +00:00
|
|
|
|
2009-06-19 06:08:06 +00:00
|
|
|
/* TODO: change it to support multiple source files */
|
|
|
|
qse_awk_parsestd_in_t psin;
|
|
|
|
qse_awk_parsestd_out_t psout;
|
2010-07-29 07:27:03 +00:00
|
|
|
qse_mmgr_t* mmgr = QSE_NULL;
|
2009-06-19 06:08:06 +00:00
|
|
|
|
2009-09-22 07:28:18 +00:00
|
|
|
memset (&arg, 0, QSE_SIZEOF(arg));
|
2008-10-13 09:08:26 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
i = comparg (argc, argv, &arg);
|
2009-06-22 07:33:05 +00:00
|
|
|
if (i <= 0)
|
2008-07-21 06:42:39 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
print_usage (((i == 0)? QSE_STDOUT: QSE_STDERR), argv[0]);
|
|
|
|
return i;
|
2008-07-21 06:42:39 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
psin.type = arg.ist;
|
|
|
|
if (arg.ist == QSE_AWK_PARSESTD_CP) psin.u.cp = arg.isp.str;
|
|
|
|
else psin.u.file = arg.isp.files[0];
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
if (arg.osf != QSE_NULL)
|
|
|
|
{
|
|
|
|
psout.type = arg.ost;
|
|
|
|
psout.u.file = arg.osf;
|
|
|
|
}
|
2009-02-22 08:16:35 +00:00
|
|
|
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
if (arg.failmalloc > 0)
|
|
|
|
{
|
2011-08-15 03:07:31 +00:00
|
|
|
debug_mmgr.ctx = &arg;
|
2011-08-14 10:04:14 +00:00
|
|
|
mmgr = &debug_mmgr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2010-07-29 07:27:03 +00:00
|
|
|
if (arg.memlimit > 0)
|
|
|
|
{
|
2011-08-15 03:07:31 +00:00
|
|
|
xma_mmgr.ctx = qse_xma_open (QSE_NULL, 0, arg.memlimit);
|
|
|
|
if (xma_mmgr.ctx == QSE_NULL)
|
2010-07-29 07:27:03 +00:00
|
|
|
{
|
|
|
|
qse_printf (QSE_T("ERROR: cannot open memory heap\n"));
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
mmgr = &xma_mmgr;
|
|
|
|
}
|
|
|
|
|
2010-07-25 06:43:26 +00:00
|
|
|
awk = qse_awk_openstdwithmmgr (mmgr, 0);
|
2010-07-29 07:27:03 +00:00
|
|
|
/*awk = qse_awk_openstd (0);*/
|
2009-06-21 06:47:34 +00:00
|
|
|
if (awk == QSE_NULL)
|
2009-02-22 08:16:35 +00:00
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_printf (QSE_T("ERROR: cannot open awk\n"));
|
|
|
|
goto oops;
|
2009-02-22 08:16:35 +00:00
|
|
|
}
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
i = qse_awk_getoption (awk);
|
|
|
|
if (arg.opton) i |= arg.opton;
|
|
|
|
if (arg.optoff) i &= ~arg.optoff;
|
|
|
|
qse_awk_setoption (awk, i);
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
/* TODO: get depth from command line */
|
|
|
|
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);
|
2009-07-27 20:31:58 +00:00
|
|
|
qse_awk_setmaxdepth (awk, QSE_AWK_DEPTH_INCLUDE, 32);
|
2009-06-21 06:47:34 +00:00
|
|
|
|
|
|
|
if (qse_awk_addfnc (awk,
|
|
|
|
QSE_T("sleep"), 5, 0,
|
|
|
|
1, 1, QSE_NULL, fnc_sleep) == QSE_NULL)
|
|
|
|
{
|
|
|
|
print_awkerr (awk);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2009-08-17 07:44:20 +00:00
|
|
|
qse_awk_seterrnum (awk, QSE_AWK_ENOERR, QSE_NULL);
|
2010-07-09 00:58:44 +00:00
|
|
|
qse_htb_walk (arg.gvm, add_global, awk);
|
2009-06-21 06:47:34 +00:00
|
|
|
if (qse_awk_geterrnum(awk) != QSE_AWK_ENOERR)
|
|
|
|
{
|
|
|
|
print_awkerr (awk);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2009-02-23 08:10:34 +00:00
|
|
|
if (qse_awk_parsestd (awk, &psin,
|
2009-06-21 06:47:34 +00:00
|
|
|
((arg.osf == QSE_NULL)? QSE_NULL: &psout)) == -1)
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
print_awkerr (awk);
|
2009-01-17 04:20:22 +00:00
|
|
|
goto oops;
|
2008-10-01 05:14:20 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2010-08-06 01:31:17 +00:00
|
|
|
#ifdef ENABLE_CALLBACK
|
|
|
|
rcb.stm = on_statement;
|
2011-08-15 03:07:31 +00:00
|
|
|
rcb.ctx = &arg;
|
2009-07-17 06:43:47 +00:00
|
|
|
#endif
|
2008-12-16 03:56:48 +00:00
|
|
|
|
2009-08-29 05:58:05 +00:00
|
|
|
rtx = qse_awk_rtx_openstd (
|
|
|
|
awk, 0, QSE_T("qseawk"),
|
|
|
|
(const qse_char_t*const*)arg.icf, QSE_NULL);
|
2009-02-14 04:57:09 +00:00
|
|
|
if (rtx == QSE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-06-21 06:47:34 +00:00
|
|
|
print_awkerr (awk);
|
|
|
|
goto oops;
|
2009-02-14 04:57:09 +00:00
|
|
|
}
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
if (apply_fs_and_gvm (rtx, &arg) <= -1)
|
|
|
|
{
|
|
|
|
print_awkerr (awk);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
app_rtx = rtx;
|
2010-08-06 01:31:17 +00:00
|
|
|
#ifdef ENABLE_CALLBACK
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_rtx_setrcb (rtx, &rcb);
|
2009-07-17 06:43:47 +00:00
|
|
|
#endif
|
2009-02-14 04:57:09 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
set_intr_run ();
|
2009-07-17 02:27:53 +00:00
|
|
|
|
|
|
|
retv = (arg.call == QSE_NULL)?
|
|
|
|
qse_awk_rtx_loop (rtx):
|
|
|
|
qse_awk_rtx_call (rtx, arg.call, QSE_NULL, 0);
|
|
|
|
if (retv != QSE_NULL)
|
2009-06-22 07:33:05 +00:00
|
|
|
{
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, retv);
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
dprint_return (rtx, retv);
|
2009-06-22 07:33:05 +00:00
|
|
|
}
|
2009-07-17 02:27:53 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
unset_intr_run ();
|
2009-02-14 04:57:09 +00:00
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
if (ret <= -1)
|
2009-06-21 06:47:34 +00:00
|
|
|
{
|
|
|
|
print_rtxerr (rtx);
|
|
|
|
goto oops;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2009-01-17 04:20:22 +00:00
|
|
|
oops:
|
2010-07-29 07:27:03 +00:00
|
|
|
if (rtx) qse_awk_rtx_close (rtx);
|
|
|
|
if (awk) qse_awk_close (awk);
|
2008-09-30 05:07:47 +00:00
|
|
|
|
2011-08-15 03:07:31 +00:00
|
|
|
if (xma_mmgr.ctx) qse_xma_close (xma_mmgr.ctx);
|
2009-06-21 06:47:34 +00:00
|
|
|
freearg (&arg);
|
2011-04-21 08:30:47 +00:00
|
|
|
|
2009-01-17 04:20:22 +00:00
|
|
|
return ret;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-21 21:35:07 +00:00
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2009-06-11 07:18:25 +00:00
|
|
|
return qse_runmain (argc, argv, awk_main);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-04-21 08:30:47 +00:00
|
|
|
|