2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2012-08-16 03:47:55 +00:00
|
|
|
* $Id$
|
2009-02-17 02:11:31 +00:00
|
|
|
*
|
2014-11-19 14:42:24 +00:00
|
|
|
Copyright (c) 2006-2014 Chung, Hyung-Hwan. All rights reserved.
|
2009-02-17 02:11:31 +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.
|
2009-02-17 02:11:31 +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.
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-22 03:18:42 +00:00
|
|
|
#include <qse/awk/stdawk.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/main.h>
|
2012-01-03 14:41:15 +00:00
|
|
|
#include <qse/cmn/mbwc.h>
|
2010-07-25 06:43:26 +00:00
|
|
|
#include <qse/cmn/xma.h>
|
2013-10-20 14:14:10 +00:00
|
|
|
#include <qse/cmn/fmt.h>
|
2016-04-29 03:55:42 +00:00
|
|
|
|
|
|
|
#include <qse/si/glob.h>
|
2016-04-28 15:29:28 +00:00
|
|
|
#include <qse/si/sio.h>
|
|
|
|
#include <qse/si/intr.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-03 14:41:15 +00:00
|
|
|
#include <locale.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
|
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)
|
2012-12-13 13:07:16 +00:00
|
|
|
# include <winsock2.h>
|
2009-01-19 08:32:51 +00:00
|
|
|
# include <windows.h>
|
2011-04-18 09:28:22 +00:00
|
|
|
#elif defined(__OS2__)
|
|
|
|
# define INCL_ERRORS
|
|
|
|
# include <os2.h>
|
2011-05-10 10:11:13 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
# include <dos.h>
|
2014-10-20 04:58:15 +00:00
|
|
|
# include <tcp.h> /* watt-32 */
|
2008-10-13 09:08:26 +00:00
|
|
|
#else
|
2009-01-19 08:32:51 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
# include <errno.h>
|
2018-06-26 08:30:02 +00:00
|
|
|
# if defined(QSE_ENABLE_LIBLTDL)
|
|
|
|
# include <ltdl.h>
|
|
|
|
# define USE_LTDL
|
|
|
|
# elif defined(HAVE_DLFCN_H)
|
|
|
|
# include <dlfcn.h>
|
|
|
|
# define USE_DLFCN
|
|
|
|
# else
|
|
|
|
# error UNSUPPORTED DYNAMIC LINKER
|
|
|
|
# endif
|
|
|
|
|
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;
|
|
|
|
|
2012-09-04 10:15:49 +00:00
|
|
|
typedef struct arg_t arg_t;
|
|
|
|
typedef struct xarg_t xarg_t;
|
|
|
|
|
|
|
|
struct xarg_t
|
|
|
|
{
|
|
|
|
qse_mmgr_t* mmgr;
|
|
|
|
qse_char_t** ptr;
|
|
|
|
qse_size_t size;
|
|
|
|
qse_size_t capa;
|
|
|
|
};
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
struct arg_t
|
2009-01-22 04:37:55 +00:00
|
|
|
{
|
2012-11-25 16:16:44 +00:00
|
|
|
int incl_conv;
|
|
|
|
qse_awk_parsestd_t* psin; /* input source streams */
|
2009-01-22 04:37:55 +00:00
|
|
|
qse_char_t* osf; /* output source file */
|
2012-11-25 16:16:44 +00:00
|
|
|
|
2012-09-04 10:15:49 +00:00
|
|
|
xarg_t icf; /* input console files */
|
2009-02-22 08:16:35 +00:00
|
|
|
|
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 */
|
2012-01-25 15:39:02 +00:00
|
|
|
qse_cmgr_t* script_cmgr;
|
|
|
|
qse_cmgr_t* console_cmgr;
|
2009-06-22 07:33:05 +00:00
|
|
|
|
2012-10-31 09:43:56 +00:00
|
|
|
unsigned int modern: 1;
|
|
|
|
unsigned int classic: 1;
|
2009-06-22 07:33:05 +00:00
|
|
|
int opton;
|
|
|
|
int optoff;
|
2012-11-25 16:16:44 +00:00
|
|
|
|
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;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t str;
|
2009-06-21 06:47:34 +00:00
|
|
|
};
|
|
|
|
|
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);
|
2013-10-20 15:22:44 +00:00
|
|
|
qse_sio_putstrvf (QSE_STDERR, fmt, ap);
|
2008-12-17 03:42:48 +00:00
|
|
|
va_end (ap);
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 05:27:25 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
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)
|
|
|
|
{
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(SA_RESTART)
|
2009-01-27 09:26:15 +00:00
|
|
|
sa_int.sa_flags |= SA_RESTART;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-14 02:45:45 +00:00
|
|
|
#if defined(SA_INTERRUPT)
|
2009-01-27 09:26:15 +00:00
|
|
|
sa_int.sa_flags |= SA_INTERRUPT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return sigaction (sig, &sa_int, NULL);
|
|
|
|
}
|
2015-10-02 05:27:25 +00:00
|
|
|
#endif
|
2009-01-27 09:26:15 +00:00
|
|
|
|
2015-10-02 05:27:25 +00:00
|
|
|
static void stop_run (void* arg)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2015-10-02 05:27:25 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__)
|
2009-01-19 08:32:51 +00:00
|
|
|
int e = errno;
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-02 05:27:25 +00:00
|
|
|
qse_awk_rtx_stop (app_rtx);
|
|
|
|
|
|
|
|
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__)
|
|
|
|
errno = e;
|
2011-04-21 08:30:47 +00:00
|
|
|
#endif
|
2015-10-02 05:27:25 +00:00
|
|
|
}
|
2011-04-21 08:30:47 +00:00
|
|
|
|
2009-01-19 08:32:51 +00:00
|
|
|
static void set_intr_run (void)
|
|
|
|
{
|
2018-01-29 15:38:32 +00:00
|
|
|
qse_set_intr_handler (stop_run, QSE_NULL);
|
2015-10-02 05:27:25 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
2012-08-10 16:05:55 +00:00
|
|
|
setsignal (SIGPIPE, SIG_IGN, 0);
|
2009-01-19 08:32:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unset_intr_run (void)
|
|
|
|
{
|
2018-01-29 15:38:32 +00:00
|
|
|
qse_clear_intr_handler ();
|
2015-10-02 05:27:25 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__OS2__) && !defined(__DOS__) && defined(SIGPIPE)
|
2012-08-10 16:05:55 +00:00
|
|
|
setsignal (SIGPIPE, SIG_DFL, 0);
|
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);
|
|
|
|
|
2019-05-24 08:58:52 +00:00
|
|
|
str = qse_awk_rtx_valtostrdup(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
|
|
|
{
|
2013-04-16 10:09:24 +00:00
|
|
|
if (qse_awk_rtx_geterrnum(rtx) == QSE_AWK_EVALTOSTR)
|
2009-06-15 02:40:52 +00:00
|
|
|
{
|
|
|
|
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"));
|
2019-05-08 07:15:17 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-04-30 07:23:34 +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
|
|
|
|
2019-04-30 07:23:34 +00:00
|
|
|
v = qse_awk_rtx_makenstrvalwithxstr(rtx, &gvmv->str);
|
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
|
|
|
{
|
2013-05-29 08:46:23 +00:00
|
|
|
if (arg->fs)
|
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 */
|
2012-10-29 14:41:39 +00:00
|
|
|
fs = qse_awk_rtx_makestrvalwithstr (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
|
|
|
}
|
|
|
|
|
2013-05-29 08:46:23 +00:00
|
|
|
if (arg->gvm)
|
2009-06-21 06:47:34 +00:00
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
2012-11-29 14:03:59 +00:00
|
|
|
if (qse_awk_rtx_isnilval (rtx, ret))
|
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
|
|
|
|
{
|
2012-10-31 08:31:58 +00:00
|
|
|
str = qse_awk_rtx_valtostrdup (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
|
|
|
}
|
|
|
|
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(ENABLE_CALLBACK)
|
2012-09-07 15:13:55 +00:00
|
|
|
static void on_statement (qse_awk_rtx_t* rtx, qse_awk_nde_t* nde)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
dprint (QSE_T("running %d at line %zu\n"), (int)nde->type, (qse_size_t)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
|
|
|
|
2012-02-24 09:09:45 +00:00
|
|
|
static void print_version (void)
|
|
|
|
{
|
2014-11-14 02:31:47 +00:00
|
|
|
qse_sio_putstrf (QSE_STDOUT, QSE_T("QSEAWK %hs\n"), QSE_PACKAGE_VERSION);
|
|
|
|
qse_sio_putstrf (QSE_STDOUT, QSE_T("Copyright 2006-2014 Chung, Hyung-Hwan\n"));
|
2012-02-24 09:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_error (const qse_char_t* fmt, ...)
|
2008-08-08 05:02:08 +00:00
|
|
|
{
|
2009-06-22 07:33:05 +00:00
|
|
|
va_list va;
|
|
|
|
|
2014-11-14 02:31:47 +00:00
|
|
|
qse_sio_putstr (QSE_STDERR, QSE_T("ERROR: "));
|
2009-06-22 07:33:05 +00:00
|
|
|
va_start (va, fmt);
|
2013-10-20 15:22:44 +00:00
|
|
|
qse_sio_putstrvf (QSE_STDERR, fmt, va);
|
2009-06-22 07:33:05 +00:00
|
|
|
va_end (va);
|
|
|
|
}
|
|
|
|
|
2014-11-14 02:31:47 +00:00
|
|
|
static void print_warning (const qse_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
qse_sio_putstr (QSE_STDERR, QSE_T("WARNING: "));
|
|
|
|
va_start (va, fmt);
|
|
|
|
qse_sio_putstrvf (QSE_STDERR, fmt, va);
|
|
|
|
va_end (va);
|
|
|
|
}
|
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
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") },
|
2013-01-29 03:43:32 +00:00
|
|
|
{ QSE_T("nextofile"), QSE_AWK_NEXTOFILE, QSE_T("enable nextofile & OFILENAME") },
|
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") },
|
2012-10-18 14:11:59 +00:00
|
|
|
{ QSE_T("stripstrspc"), QSE_AWK_STRIPSTRSPC, QSE_T("strip spaces in string-to-number conversion") },
|
2012-10-27 16:28:12 +00:00
|
|
|
{ QSE_T("blankconcat"), QSE_AWK_BLANKCONCAT, QSE_T("enable concatenation by blanks") },
|
2011-05-02 07:28:51 +00:00
|
|
|
{ QSE_T("crlf"), QSE_AWK_CRLF, QSE_T("use CRLF for a newline") },
|
2012-11-28 05:34:19 +00:00
|
|
|
{ QSE_T("flexmap"), QSE_AWK_FLEXMAP, QSE_T("allow a map to be assigned or returned") },
|
2011-05-02 07:28:51 +00:00
|
|
|
{ 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") },
|
2012-08-20 06:00:22 +00:00
|
|
|
{ QSE_T("tolerant"), QSE_AWK_TOLERANT, QSE_T("make more fault-tolerant") },
|
2011-05-19 08:36:40 +00:00
|
|
|
{ QSE_NULL, 0, QSE_NULL }
|
2009-06-22 07:33:05 +00:00
|
|
|
};
|
|
|
|
|
2013-10-20 15:22:44 +00:00
|
|
|
static void print_usage (qse_sio_t* out, const qse_char_t* argv0)
|
2009-06-22 07:33:05 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-11-03 16:48:20 +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);
|
|
|
|
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(" --version print version\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -D show extra information\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -c/--call name call a function instead of entering\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" the pattern-action loop. [datafile]* is\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" passed to the function as parameters\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -f/--file sourcefile set the source script file\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -d/--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"));
|
|
|
|
qse_fprintf (out, QSE_T(" -m/--memory-limit number limit the memory usage (bytes)\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" -w expand datafile wildcards\n"));
|
2012-09-04 10:15:49 +00:00
|
|
|
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" -X number fail the number'th memory allocation\n"));
|
2011-08-14 10:04:14 +00:00
|
|
|
#endif
|
2012-02-24 09:09:45 +00:00
|
|
|
#if defined(QSE_CHAR_IS_WCHAR)
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" --script-encoding string specify script file encoding name\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" --console-encoding string specify console encoding name\n"));
|
2012-02-24 09:09:45 +00:00
|
|
|
#endif
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out, QSE_T(" --modern run in the modern mode(default)\n"));
|
|
|
|
qse_fprintf (out, QSE_T(" --classic run in the classic mode\n"));
|
2009-06-22 07:33:05 +00:00
|
|
|
|
2012-01-25 15:39:02 +00:00
|
|
|
for (j = 0; opttab[j].name; j++)
|
2009-06-22 07:33:05 +00:00
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (out,
|
2012-01-25 15:39:02 +00:00
|
|
|
QSE_T(" --%-18s on/off %s\n"),
|
|
|
|
opttab[j].name, opttab[j].desc);
|
2009-06-22 07:33:05 +00:00
|
|
|
}
|
2008-08-08 05:02:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 10:15:49 +00:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
2014-07-08 14:30:42 +00:00
|
|
|
static int collect_into_xarg (const qse_cstr_t* path, void* ctx)
|
2012-09-04 10:15:49 +00:00
|
|
|
{
|
|
|
|
xarg_t* xarg = (xarg_t*)ctx;
|
|
|
|
|
|
|
|
if (xarg->size <= xarg->capa)
|
|
|
|
{
|
|
|
|
qse_char_t** tmp;
|
|
|
|
|
|
|
|
tmp = QSE_MMGR_REALLOC (
|
|
|
|
xarg->mmgr, xarg->ptr,
|
|
|
|
QSE_SIZEOF(*tmp) * (xarg->capa + 128 + 1));
|
|
|
|
if (tmp == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
xarg->ptr = tmp;
|
|
|
|
xarg->capa += 128;
|
|
|
|
}
|
|
|
|
|
|
|
|
xarg->ptr[xarg->size] = qse_strdup (path->ptr, xarg->mmgr);
|
|
|
|
if (xarg->ptr[xarg->size] == QSE_NULL) return -1;
|
|
|
|
xarg->size++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void purge_xarg (xarg_t* xarg)
|
|
|
|
{
|
|
|
|
if (xarg->ptr)
|
|
|
|
{
|
|
|
|
qse_size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < xarg->size; i++)
|
|
|
|
QSE_MMGR_FREE (xarg->mmgr, xarg->ptr[i]);
|
|
|
|
QSE_MMGR_FREE (xarg->mmgr, xarg->ptr);
|
|
|
|
|
|
|
|
xarg->size = 0;
|
|
|
|
xarg->capa = 0;
|
|
|
|
xarg->ptr = QSE_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int expand_wildcard (int argc, qse_char_t* argv[], int glob, xarg_t* xarg)
|
|
|
|
{
|
|
|
|
int i;
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t tmp;
|
2012-09-04 10:15:49 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (glob)
|
|
|
|
{
|
2015-02-03 02:26:32 +00:00
|
|
|
#if defined(_WIN32) || defined(__OS2__) || defined(__DOS__)
|
|
|
|
int glob_flags = QSE_GLOB_TOLERANT | QSE_GLOB_PERIOD | QSE_GLOB_NOESCAPE | QSE_GLOB_IGNORECASE;
|
|
|
|
#else
|
|
|
|
int glob_flags = QSE_GLOB_TOLERANT | QSE_GLOB_PERIOD;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
x = qse_glob (
|
|
|
|
argv[i], collect_into_xarg, xarg,
|
|
|
|
glob_flags, xarg->mmgr, qse_getdflcmgr()
|
2012-09-04 10:15:49 +00:00
|
|
|
);
|
|
|
|
if (x <= -1) return -1;
|
|
|
|
}
|
|
|
|
else x = 0;
|
|
|
|
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
/* not expanded. just use it as is */
|
|
|
|
tmp.ptr = argv[i];
|
|
|
|
tmp.len = qse_strlen(argv[i]);
|
|
|
|
if (collect_into_xarg (&tmp, xarg) <= -1) return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xarg->ptr[xarg->size] = QSE_NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
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
|
|
|
{
|
2012-10-31 09:43:56 +00:00
|
|
|
|
2009-06-22 07:33:05 +00:00
|
|
|
{ QSE_T(":implicit"), QSE_T('\0') },
|
2013-01-31 10:07:20 +00:00
|
|
|
{ QSE_T(":nextofile"), 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') },
|
2012-10-27 16:28:12 +00:00
|
|
|
{ QSE_T(":blankconcat"), QSE_T('\0') },
|
2009-06-23 07:01:28 +00:00
|
|
|
{ QSE_T(":crlf"), QSE_T('\0') },
|
2012-11-28 05:34:19 +00:00
|
|
|
{ QSE_T(":flexmap"), QSE_T('\0') },
|
2009-06-22 07:33:05 +00:00
|
|
|
{ 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') },
|
2012-08-17 06:53:17 +00:00
|
|
|
{ QSE_T(":tolerant"), 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') },
|
2011-12-21 02:02:05 +00:00
|
|
|
{ QSE_T(":deparsed-file"), QSE_T('d') },
|
2008-12-21 21:35:07 +00:00
|
|
|
{ QSE_T(":field-separator"), QSE_T('F') },
|
|
|
|
{ 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
|
|
|
|
2012-01-25 15:39:02 +00:00
|
|
|
{ QSE_T(":script-encoding"), QSE_T('\0') },
|
|
|
|
{ QSE_T(":console-encoding"), QSE_T('\0') },
|
|
|
|
|
2012-10-31 09:43:56 +00:00
|
|
|
{ QSE_T("modern"), QSE_T('\0') },
|
|
|
|
{ QSE_T("classic"), QSE_T('\0') },
|
|
|
|
|
2012-02-24 09:09:45 +00:00
|
|
|
{ QSE_T("version"), QSE_T('\0') },
|
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)
|
2012-09-04 10:15:49 +00:00
|
|
|
QSE_T("hDc:f:d:F:v:m:wX:"),
|
2011-08-14 10:04:14 +00:00
|
|
|
#else
|
2012-09-04 10:15:49 +00:00
|
|
|
QSE_T("hDc:f:d:F:v:m:w"),
|
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
|
|
|
|
2012-11-25 16:16:44 +00:00
|
|
|
qse_size_t i;
|
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 */
|
2012-11-25 16:16:44 +00:00
|
|
|
qse_awk_parsestd_t* isf = QSE_NULL; /* input source files */
|
|
|
|
|
|
|
|
qse_char_t* osf = QSE_NULL; /* output source file */
|
|
|
|
|
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
|
|
|
|
2012-02-24 09:09:45 +00:00
|
|
|
int oops_ret = -1;
|
2012-09-04 10:15:49 +00:00
|
|
|
int do_glob = 0;
|
2008-12-10 02:50:16 +00:00
|
|
|
|
2012-11-28 14:12:26 +00:00
|
|
|
isf = QSE_MMGR_ALLOC (arg->icf.mmgr, QSE_SIZEOF(*isf) * isfc);
|
2008-12-21 21:35:07 +00:00
|
|
|
if (isf == QSE_NULL)
|
2008-09-30 05:07:47 +00:00
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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 (
|
2011-12-28 14:26:02 +00:00
|
|
|
QSE_MMGR_GETDFL(), 0, 30, 70,
|
2010-10-28 06:54:37 +00:00
|
|
|
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
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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
|
|
|
|
2013-03-11 16:34:41 +00:00
|
|
|
qse_htb_setstyle (gvm,
|
|
|
|
qse_gethtbstyle(QSE_HTB_STYLE_INLINE_VALUE_COPIER)
|
2010-10-30 07:54:36 +00:00
|
|
|
);
|
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'):
|
2012-02-24 09:09:45 +00:00
|
|
|
oops_ret = 0;
|
|
|
|
goto oops;
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2011-12-21 02:02:05 +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
|
|
|
{
|
2012-11-25 16:16:44 +00:00
|
|
|
if (isfl >= isfc - 1) /* -1 for last QSE_NULL */
|
2008-08-11 02:27:21 +00:00
|
|
|
{
|
2012-12-12 15:21:37 +00:00
|
|
|
qse_awk_parsestd_t* tmp;
|
2012-11-28 14:12:26 +00:00
|
|
|
tmp = QSE_MMGR_REALLOC (arg->icf.mmgr, isf, QSE_SIZEOF(*isf)*(isfc+16));
|
2008-12-21 21:35:07 +00:00
|
|
|
if (tmp == QSE_NULL)
|
2008-10-13 09:08:26 +00:00
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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
|
|
|
|
2012-11-25 16:16:44 +00:00
|
|
|
isf[isfl].type = QSE_AWK_PARSESTD_FILE;
|
|
|
|
isf[isfl].u.file.path = opt.arg;
|
2013-01-12 16:46:12 +00:00
|
|
|
isf[isfl].u.file.cmgr = QSE_NULL;
|
2012-11-25 16:16:44 +00:00
|
|
|
isfl++;
|
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
|
|
|
|
2011-12-21 02:02:05 +00:00
|
|
|
case QSE_T('d'):
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
2011-12-21 02:02:05 +00:00
|
|
|
osf = 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
|
|
|
|
2011-12-21 02:02:05 +00:00
|
|
|
case QSE_T('F'):
|
2008-10-14 05:32:58 +00:00
|
|
|
{
|
2011-12-21 02:02:05 +00:00
|
|
|
fs = opt.arg;
|
2008-10-14 05:32:58 +00:00
|
|
|
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)
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (QSE_T("no value for '%s' in '%s'\n"), opt.arg, opt.lngopt);
|
2009-06-22 07:33:05 +00:00
|
|
|
else
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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;
|
2012-10-29 14:41:39 +00:00
|
|
|
gvmv.str.ptr = ++eq;
|
|
|
|
gvmv.str.len = qse_strlen(eq);
|
2009-06-21 06:47:34 +00:00
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
/* +1 for null-termination of the key in the table */
|
|
|
|
if (qse_htb_upsert (gvm, opt.arg, qse_strlen(opt.arg) + 1, &gvmv, 1) == QSE_NULL)
|
2008-09-30 05:07:47 +00:00
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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'):
|
|
|
|
{
|
2017-12-18 07:20:02 +00:00
|
|
|
arg->memlimit = qse_strtoulong (opt.arg, 10, QSE_NULL);
|
2010-07-29 07:27:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-04 10:15:49 +00:00
|
|
|
case QSE_T('w'):
|
|
|
|
{
|
|
|
|
do_glob = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
case QSE_T('X'):
|
|
|
|
{
|
2017-12-18 07:20:02 +00:00
|
|
|
arg->failmalloc = qse_strtoulong (opt.arg, 10, QSE_NULL);
|
2011-08-14 10:04:14 +00:00
|
|
|
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;
|
2012-02-24 09:09:45 +00:00
|
|
|
|
|
|
|
if (qse_strcmp(opt.lngopt, QSE_T("version")) == 0)
|
2008-07-27 09:37:38 +00:00
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_version ();
|
|
|
|
oops_ret = 2;
|
|
|
|
goto oops;
|
2008-07-27 09:37:38 +00:00
|
|
|
}
|
2012-02-24 09:09:45 +00:00
|
|
|
else if (qse_strcmp(opt.lngopt, QSE_T("script-encoding")) == 0)
|
2012-01-25 15:39:02 +00:00
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
arg->script_cmgr = qse_findcmgr (opt.arg);
|
2012-01-25 15:39:02 +00:00
|
|
|
if (arg->script_cmgr == QSE_NULL)
|
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (QSE_T("unknown script encoding - %s\n"), opt.arg);
|
2012-09-05 22:37:55 +00:00
|
|
|
oops_ret = 3;
|
2012-01-25 15:39:02 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (qse_strcmp(opt.lngopt, QSE_T("console-encoding")) == 0)
|
|
|
|
{
|
2012-02-13 14:43:50 +00:00
|
|
|
arg->console_cmgr = qse_findcmgr (opt.arg);
|
2012-01-25 15:39:02 +00:00
|
|
|
if (arg->console_cmgr == QSE_NULL)
|
|
|
|
{
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (QSE_T("unknown console encoding - %s\n"), opt.arg);
|
2012-09-05 22:37:55 +00:00
|
|
|
oops_ret = 3;
|
2012-01-25 15:39:02 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
}
|
2012-10-31 09:43:56 +00:00
|
|
|
else if (qse_strcmp(opt.lngopt, QSE_T("modern")) == 0)
|
|
|
|
{
|
|
|
|
arg->modern = 1;
|
|
|
|
}
|
|
|
|
else if (qse_strcmp(opt.lngopt, QSE_T("classic")) == 0)
|
2012-02-24 09:09:45 +00:00
|
|
|
{
|
2012-10-31 09:43:56 +00:00
|
|
|
arg->classic = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; opttab[i].name; i++)
|
2012-02-24 09:09:45 +00:00
|
|
|
{
|
2012-10-31 09:43:56 +00:00
|
|
|
if (qse_strcmp (opt.lngopt, opttab[i].name) == 0)
|
2012-02-24 09:09:45 +00:00
|
|
|
{
|
2012-10-31 09:43:56 +00:00
|
|
|
if (qse_strcmp (opt.arg, QSE_T("off")) == 0)
|
|
|
|
arg->optoff |= opttab[i].opt;
|
|
|
|
else if (qse_strcmp (opt.arg, QSE_T("on")) == 0)
|
|
|
|
arg->opton |= opttab[i].opt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_error (QSE_T("invalid value '%s' for '%s' - use 'on' or 'off'\n"), opt.arg, opt.lngopt);
|
|
|
|
oops_ret = 3;
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-24 09:09:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:43:56 +00:00
|
|
|
}
|
2009-06-22 07:33:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QSE_T('?'):
|
|
|
|
{
|
|
|
|
if (opt.lngopt)
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (QSE_T("illegal option - '%s'\n"), opt.lngopt);
|
2008-07-27 09:37:38 +00:00
|
|
|
else
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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)
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (QSE_T("bad argument for '%s'\n"), opt.lngopt);
|
2008-07-27 09:37:38 +00:00
|
|
|
else
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (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-10-13 09:08:26 +00:00
|
|
|
if (isfl <= 0)
|
2008-07-26 09:01:27 +00:00
|
|
|
{
|
2012-11-25 16:16:44 +00:00
|
|
|
/* no -f specified */
|
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 */
|
2012-11-25 16:16:44 +00:00
|
|
|
isf[isfl].type = QSE_AWK_PARSESTD_STR;
|
|
|
|
isf[isfl].u.str.ptr = argv[opt.ind++];
|
|
|
|
isf[isfl].u.str.len = qse_strlen(isf[isfl].u.str.ptr);
|
2008-12-31 00:08:03 +00:00
|
|
|
|
2012-11-25 16:16:44 +00:00
|
|
|
isfl++;
|
2008-07-26 09:01:27 +00:00
|
|
|
}
|
2008-07-27 09:37:38 +00:00
|
|
|
|
2013-01-12 16:46:12 +00:00
|
|
|
for (i = 0; i < isfl ; i++)
|
|
|
|
{
|
|
|
|
if (isf[i].type == QSE_AWK_PARSESTD_FILE)
|
2013-11-04 17:37:08 +00:00
|
|
|
isf[i].u.file.cmgr = arg->script_cmgr;
|
2013-01-12 16:46:12 +00:00
|
|
|
}
|
2012-11-25 16:16:44 +00:00
|
|
|
|
|
|
|
isf[isfl].type = QSE_AWK_PARSESTD_NULL;
|
|
|
|
arg->psin = isf;
|
|
|
|
|
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 */
|
2012-09-04 10:15:49 +00:00
|
|
|
if (expand_wildcard (argc - opt.ind, &argv[opt.ind], do_glob, &arg->icf) <= -1)
|
2009-06-11 07:18:25 +00:00
|
|
|
{
|
2012-11-16 16:46:49 +00:00
|
|
|
print_error (QSE_T("failed to expand wildcard\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2009-06-11 07:18:25 +00:00
|
|
|
}
|
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->osf = osf;
|
|
|
|
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:
|
2012-12-27 14:40:58 +00:00
|
|
|
if (gvm) qse_htb_close (gvm);
|
2012-09-04 10:15:49 +00:00
|
|
|
purge_xarg (&arg->icf);
|
2012-11-25 16:16:44 +00:00
|
|
|
if (isf)
|
|
|
|
{
|
|
|
|
if (arg->incl_conv) QSE_MMGR_FREE (arg->icf.mmgr, isf[0].u.str.ptr);
|
|
|
|
QSE_MMGR_FREE (arg->icf.mmgr, isf);
|
|
|
|
}
|
2012-02-24 09:09:45 +00:00
|
|
|
return oops_ret;
|
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
|
|
|
{
|
2012-11-25 16:16:44 +00:00
|
|
|
if (arg->psin)
|
|
|
|
{
|
|
|
|
if (arg->incl_conv) QSE_MMGR_FREE (arg->icf.mmgr, arg->psin[0].u.str.ptr);
|
|
|
|
QSE_MMGR_FREE (arg->icf.mmgr, arg->psin);
|
|
|
|
}
|
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
/*if (arg->osf != QSE_NULL) free (arg->osf);*/
|
2012-09-04 10:15:49 +00:00
|
|
|
purge_xarg (&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);
|
|
|
|
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (
|
2013-11-05 14:16:45 +00:00
|
|
|
QSE_T("CODE %d LINE %zu COLUMN %zu %s%s%s- %s\n"),
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_geterrnum(awk),
|
2013-11-05 14:16:45 +00:00
|
|
|
(qse_size_t)loc->line,
|
|
|
|
(qse_size_t)loc->colm,
|
2010-08-18 07:15:14 +00:00
|
|
|
((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);
|
|
|
|
|
2012-02-24 09:09:45 +00:00
|
|
|
print_error (
|
2013-11-05 14:16:45 +00:00
|
|
|
QSE_T("CODE %d LINE %zu COLUMN %zu %s%s%s- %s\n"),
|
2009-06-21 06:47:34 +00:00
|
|
|
qse_awk_rtx_geterrnum(rtx),
|
2013-11-05 14:16:45 +00:00
|
|
|
(qse_size_t)loc->line,
|
|
|
|
(qse_size_t)loc->colm,
|
2010-08-18 07:15:14 +00:00
|
|
|
((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
|
|
|
|
2012-11-06 04:30:35 +00:00
|
|
|
/* the key was inserted to the table with a null at the end
|
|
|
|
* and the key length was even incremetned for that.
|
|
|
|
* so i can pass the pointer without other adjustments. */
|
|
|
|
gvmv->idx = qse_awk_addgbl (awk, QSE_HTB_KPTR(pair));
|
|
|
|
if (gvmv->idx <= -1) return QSE_HTB_WALK_STOP;
|
2010-07-09 00:58:44 +00:00
|
|
|
return QSE_HTB_WALK_FORWARD;
|
2008-08-04 08:06:43 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
|
|
|
|
static void* xma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
|
|
|
|
{
|
|
|
|
return qse_xma_alloc (mmgr->ctx, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* xma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
|
|
|
|
{
|
|
|
|
return qse_xma_realloc (mmgr->ctx, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xma_free (qse_mmgr_t* mmgr, void* ptr)
|
|
|
|
{
|
|
|
|
qse_xma_free (mmgr->ctx, ptr);
|
|
|
|
}
|
|
|
|
|
2010-07-29 07:27:03 +00:00
|
|
|
static qse_mmgr_t xma_mmgr =
|
|
|
|
{
|
2014-07-09 15:01:16 +00:00
|
|
|
xma_alloc,
|
|
|
|
xma_realloc,
|
|
|
|
xma_free,
|
2010-07-29 07:27:03 +00:00
|
|
|
QSE_NULL
|
|
|
|
};
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
|
2011-08-14 10:04:14 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
static qse_ulong_t debug_mmgr_count = 0;
|
2011-12-21 06:40:27 +00:00
|
|
|
static qse_ulong_t debug_mmgr_alloc_count = 0;
|
|
|
|
static qse_ulong_t debug_mmgr_realloc_count = 0;
|
|
|
|
static qse_ulong_t debug_mmgr_free_count = 0;
|
2011-08-14 10:04:14 +00:00
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void* debug_mmgr_alloc (qse_mmgr_t* mmgr, qse_size_t size)
|
2011-08-14 10:04:14 +00:00
|
|
|
{
|
2011-12-21 06:40:27 +00:00
|
|
|
void* ptr;
|
2014-07-09 15:01:16 +00:00
|
|
|
struct arg_t* arg = (struct arg_t*)mmgr->ctx;
|
2011-08-14 10:04:14 +00:00
|
|
|
debug_mmgr_count++;
|
|
|
|
if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL;
|
2011-12-21 06:40:27 +00:00
|
|
|
ptr = malloc (size);
|
|
|
|
if (ptr) debug_mmgr_alloc_count++;
|
|
|
|
return ptr;
|
2011-08-14 10:04:14 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void* debug_mmgr_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
|
2011-08-14 10:04:14 +00:00
|
|
|
{
|
2011-12-21 06:40:27 +00:00
|
|
|
void* rptr;
|
2014-07-09 15:01:16 +00:00
|
|
|
struct arg_t* arg = (struct arg_t*)mmgr->ctx;
|
2011-08-14 10:04:14 +00:00
|
|
|
debug_mmgr_count++;
|
|
|
|
if (debug_mmgr_count % arg->failmalloc == 0) return QSE_NULL;
|
2011-12-21 06:40:27 +00:00
|
|
|
rptr = realloc (ptr, size);
|
|
|
|
if (rptr)
|
|
|
|
{
|
|
|
|
if (ptr) debug_mmgr_realloc_count++;
|
|
|
|
else debug_mmgr_alloc_count++;
|
|
|
|
}
|
|
|
|
return rptr;
|
2011-08-14 10:04:14 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:01:16 +00:00
|
|
|
static void debug_mmgr_free (qse_mmgr_t* mmgr, void* ptr)
|
2011-08-14 10:04:14 +00:00
|
|
|
{
|
2011-12-21 06:40:27 +00:00
|
|
|
debug_mmgr_free_count++;
|
2011-08-14 10:04:14 +00:00
|
|
|
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;
|
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
|
|
|
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(ENABLE_CALLBACK)
|
2012-09-07 15:13:55 +00:00
|
|
|
static qse_awk_rtx_ecb_t rtx_ecb =
|
|
|
|
{
|
2013-04-15 13:18:03 +00:00
|
|
|
QSE_FV(.close, QSE_NULL),
|
|
|
|
QSE_FV(.stmt, on_statement),
|
|
|
|
QSE_FV(.gblset, QSE_NULL)
|
2012-09-07 15:13:55 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2009-06-19 06:08:06 +00:00
|
|
|
/* TODO: change it to support multiple source files */
|
2012-01-06 14:38:11 +00:00
|
|
|
qse_awk_parsestd_t psout;
|
2011-12-30 14:44:21 +00:00
|
|
|
qse_mmgr_t* mmgr = QSE_MMGR_GETDFL();
|
2009-06-19 06:08:06 +00:00
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
qse_memset (&arg, 0, QSE_SIZEOF(arg));
|
2012-09-04 10:15:49 +00:00
|
|
|
arg.icf.mmgr = mmgr;
|
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
|
|
|
}
|
2012-02-24 09:09:45 +00:00
|
|
|
if (i == 2) return 0;
|
2012-09-05 22:37:55 +00:00
|
|
|
if (i == 3) return -1;
|
2008-07-21 06:42:39 +00:00
|
|
|
|
2012-11-25 16:16:44 +00:00
|
|
|
if (arg.osf)
|
2009-06-21 06:47:34 +00:00
|
|
|
{
|
2012-01-06 14:38:11 +00:00
|
|
|
psout.type = QSE_AWK_PARSESTD_FILE;
|
|
|
|
psout.u.file.path = arg.osf;
|
2013-01-12 16:46:12 +00:00
|
|
|
psout.u.file.cmgr = arg.script_cmgr;
|
2009-06-21 06:47:34 +00:00
|
|
|
}
|
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;
|
2014-07-09 15:01:16 +00:00
|
|
|
mmgr = &debug_mmgr;
|
2011-08-14 10:04:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2010-07-29 07:27:03 +00:00
|
|
|
if (arg.memlimit > 0)
|
|
|
|
{
|
2011-12-28 14:26:02 +00:00
|
|
|
xma_mmgr.ctx = qse_xma_open (QSE_MMGR_GETDFL(), 0, arg.memlimit);
|
2011-08-15 03:07:31 +00:00
|
|
|
if (xma_mmgr.ctx == QSE_NULL)
|
2010-07-29 07:27:03 +00:00
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_printf (QSE_T("ERROR: cannot open memory heap\n"));
|
2010-07-29 07:27:03 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
mmgr = &xma_mmgr;
|
|
|
|
}
|
|
|
|
|
2014-07-11 14:17:00 +00:00
|
|
|
awk = qse_awk_openstdwithmmgr (mmgr, 0, QSE_NULL);
|
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
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_printf (QSE_T("ERROR: cannot open awk\n"));
|
2009-06-21 06:47:34 +00:00
|
|
|
goto oops;
|
2009-02-22 08:16:35 +00:00
|
|
|
}
|
|
|
|
|
2012-10-31 09:43:56 +00:00
|
|
|
if (arg.modern) i = QSE_AWK_MODERN;
|
|
|
|
else if (arg.classic) i = QSE_AWK_CLASSIC;
|
|
|
|
else qse_awk_getopt (awk, QSE_AWK_TRAIT, &i);
|
2009-06-22 07:33:05 +00:00
|
|
|
if (arg.opton) i |= arg.opton;
|
|
|
|
if (arg.optoff) i &= ~arg.optoff;
|
2012-10-21 16:19:03 +00:00
|
|
|
qse_awk_setopt (awk, QSE_AWK_TRAIT, &i);
|
2009-06-22 07:33:05 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
/* TODO: get depth from command line */
|
|
|
|
{
|
2012-10-21 16:19:03 +00:00
|
|
|
qse_size_t tmp;
|
|
|
|
tmp = 50;
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_DEPTH_BLOCK_PARSE, &tmp);
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_DEPTH_EXPR_PARSE, &tmp);
|
|
|
|
tmp = 500;
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_DEPTH_BLOCK_RUN, &tmp);
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_DEPTH_EXPR_RUN, &tmp);
|
|
|
|
tmp = 64;
|
|
|
|
qse_awk_setopt (awk, QSE_AWK_DEPTH_INCLUDE, &tmp);
|
2009-06-21 06:47:34 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-25 16:16:44 +00:00
|
|
|
if (qse_awk_parsestd (awk, arg.psin,
|
2012-10-18 14:11:59 +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
|
|
|
|
2009-08-29 05:58:05 +00:00
|
|
|
rtx = qse_awk_rtx_openstd (
|
2012-04-30 09:46:58 +00:00
|
|
|
awk, 0, QSE_T("qseawk"),
|
2014-11-12 05:59:47 +00:00
|
|
|
(arg.call? QSE_NULL: (const qse_char_t**)arg.icf.ptr), /* console input */
|
2012-10-18 14:11:59 +00:00
|
|
|
QSE_NULL, /* console output */
|
|
|
|
arg.console_cmgr
|
|
|
|
);
|
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;
|
|
|
|
}
|
2012-04-27 14:33:14 +00:00
|
|
|
|
2009-06-21 06:47:34 +00:00
|
|
|
app_rtx = rtx;
|
2014-11-14 02:44:20 +00:00
|
|
|
#if defined(ENABLE_CALLBACK)
|
2012-09-07 15:13:55 +00:00
|
|
|
qse_awk_rtx_pushecb (rtx, &rtx_ecb);
|
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
|
|
|
|
2012-10-18 14:11:59 +00:00
|
|
|
retv = arg.call?
|
2014-11-12 05:59:47 +00:00
|
|
|
qse_awk_rtx_callwithstrs (rtx, arg.call, (const qse_char_t**)arg.icf.ptr, arg.icf.size):
|
2012-10-18 14:11:59 +00:00
|
|
|
qse_awk_rtx_loop (rtx);
|
2012-10-31 08:31:58 +00:00
|
|
|
|
|
|
|
unset_intr_run ();
|
|
|
|
|
2012-08-19 14:20:19 +00:00
|
|
|
if (retv)
|
2009-06-22 07:33:05 +00:00
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t tmp;
|
2012-08-19 14:20:19 +00:00
|
|
|
|
2009-07-17 02:27:53 +00:00
|
|
|
qse_awk_rtx_refdownval (rtx, retv);
|
2012-08-19 14:20:19 +00:00
|
|
|
if (app_debug) dprint_return (rtx, retv);
|
2009-07-17 02:27:53 +00:00
|
|
|
|
2012-08-19 14:20:19 +00:00
|
|
|
ret = 0;
|
2013-11-05 14:16:45 +00:00
|
|
|
if (qse_awk_rtx_valtoint (rtx, retv, &tmp) >= 0) ret = tmp;
|
2009-06-22 07:33:05 +00:00
|
|
|
}
|
2012-10-31 08:31:58 +00:00
|
|
|
else
|
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
|
|
|
|
2011-12-21 06:40:27 +00:00
|
|
|
#if defined(QSE_BUILD_DEBUG)
|
|
|
|
if (arg.failmalloc > 0)
|
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("-[MALLOC COUNTS]---------------------------------------\n"));
|
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
|
2011-12-21 06:40:27 +00:00
|
|
|
(unsigned long)debug_mmgr_alloc_count,
|
|
|
|
(unsigned long)debug_mmgr_free_count,
|
|
|
|
(unsigned long)debug_mmgr_realloc_count);
|
2013-11-03 16:48:20 +00:00
|
|
|
qse_fprintf (QSE_STDERR, QSE_T("-------------------------------------------------------\n"));
|
2011-12-21 06:40:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-17 04:20:22 +00:00
|
|
|
return ret;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
struct mpi_t
|
|
|
|
{
|
|
|
|
void* h;
|
|
|
|
int (*i) (int argc, qse_achar_t* argv[]);
|
|
|
|
void (*f) (void);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct mpi_t mpi_t;
|
|
|
|
|
|
|
|
static void open_mpi (mpi_t* mpi, int argc, qse_achar_t* argv[])
|
|
|
|
{
|
2012-11-01 15:03:02 +00:00
|
|
|
#if defined(USE_LTDL)
|
2012-11-01 06:42:38 +00:00
|
|
|
lt_dladvise adv;
|
2012-11-01 15:03:02 +00:00
|
|
|
#endif
|
2012-11-01 06:42:38 +00:00
|
|
|
|
|
|
|
qse_memset (mpi, 0, QSE_SIZEOF(*mpi));
|
|
|
|
|
|
|
|
#if defined(USE_LTDL)
|
|
|
|
|
|
|
|
#if defined(QSE_ACHAR_IS_MCHAR)
|
|
|
|
if (qse_mbscmp (qse_mbsbasename(argv[0]), QSE_MT("qseawkmp")) != 0 &&
|
|
|
|
qse_mbscmp (qse_mbsbasename(argv[0]), QSE_MT("qseawkmpi")) != 0) return;
|
|
|
|
#else
|
|
|
|
if (qse_wcscmp (qse_wcsbasename(argv[0]), QSE_WT("qseawkmp")) != 0 &&
|
|
|
|
qse_wcscmp (qse_wcsbasename(argv[0]), QSE_WT("qseawkmpi")) != 0) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (lt_dlinit () != 0) return;
|
|
|
|
|
2019-05-13 16:35:33 +00:00
|
|
|
if (lt_dladvise_init(&adv) != 0) goto oops;
|
2012-11-01 06:42:38 +00:00
|
|
|
|
|
|
|
/* If i don't set the global option, loading may end up with an error
|
|
|
|
* like this depending on your MPI library.
|
|
|
|
*
|
|
|
|
* symbol lookup error: /usr/lib/openmpi/lib/openmpi/mca_paffinity_linux.so: undefined symbol: mca_base_param_reg_int
|
|
|
|
*/
|
|
|
|
|
2019-05-13 16:35:33 +00:00
|
|
|
if (lt_dladvise_global(&adv) != 0 || lt_dladvise_ext(&adv) != 0)
|
2012-11-01 06:42:38 +00:00
|
|
|
{
|
|
|
|
lt_dladvise_destroy (&adv);
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2019-05-13 16:35:33 +00:00
|
|
|
mpi->h = lt_dlopenadvise(DEFAULT_MODPREFIX "mpi" DEFAULT_MODPOSTFIX, adv);
|
2012-11-01 06:42:38 +00:00
|
|
|
lt_dladvise_destroy (&adv);
|
|
|
|
|
|
|
|
if (mpi->h)
|
|
|
|
{
|
2019-05-13 16:35:33 +00:00
|
|
|
mpi->i = lt_dlsym(mpi->h, "qse_awk_mod_mpi_init");
|
|
|
|
mpi->f = lt_dlsym(mpi->h, "qse_awk_mod_mpi_fini");
|
2012-11-01 06:42:38 +00:00
|
|
|
|
|
|
|
if (mpi->i == QSE_NULL ||
|
|
|
|
mpi->f == QSE_NULL ||
|
2019-05-13 16:35:33 +00:00
|
|
|
mpi->i(argc, argv) <= -1)
|
2012-11-01 06:42:38 +00:00
|
|
|
{
|
|
|
|
lt_dlclose (mpi->h);
|
|
|
|
mpi->h = QSE_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
lt_dlexit ();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_mpi (mpi_t* mpi)
|
|
|
|
{
|
|
|
|
if (mpi->h)
|
|
|
|
{
|
|
|
|
#if defined(USE_LTDL)
|
|
|
|
mpi->f ();
|
|
|
|
lt_dlclose (mpi->h);
|
|
|
|
mpi->h = QSE_NULL;
|
|
|
|
lt_dlexit ();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-04-30 09:46:58 +00:00
|
|
|
int ret;
|
2012-11-01 06:42:38 +00:00
|
|
|
mpi_t mpi;
|
2012-04-30 09:46:58 +00:00
|
|
|
|
2012-01-03 14:41:15 +00:00
|
|
|
#if defined(_WIN32)
|
2012-01-10 15:05:40 +00:00
|
|
|
char locale[100];
|
2012-04-30 09:46:58 +00:00
|
|
|
UINT codepage;
|
|
|
|
WSADATA wsadata;
|
2014-10-20 04:58:15 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
extern BOOL _watt_do_exit;
|
|
|
|
int sock_inited = 0;
|
2013-10-20 15:22:44 +00:00
|
|
|
#else
|
|
|
|
/* nothing special */
|
|
|
|
#endif
|
2012-04-30 09:46:58 +00:00
|
|
|
|
2013-10-20 15:22:44 +00:00
|
|
|
#if defined(_WIN32)
|
2015-04-27 08:37:57 +00:00
|
|
|
codepage = GetConsoleOutputCP();
|
2012-01-03 14:41:15 +00:00
|
|
|
if (codepage == CP_UTF8)
|
|
|
|
{
|
|
|
|
/*SetConsoleOUtputCP (CP_UTF8);*/
|
2012-10-18 06:52:03 +00:00
|
|
|
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
|
2012-01-03 14:41:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-20 14:14:10 +00:00
|
|
|
/* .codepage */
|
|
|
|
qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale),
|
|
|
|
codepage, 10, -1, QSE_MT('\0'), QSE_MT("."));
|
2012-01-10 15:05:40 +00:00
|
|
|
setlocale (LC_ALL, locale);
|
2015-04-27 08:37:57 +00:00
|
|
|
/* qse_setdflcmgrbyid (QSE_CMGR_SLMB); */
|
2012-01-03 14:41:15 +00:00
|
|
|
}
|
2012-04-30 09:46:58 +00:00
|
|
|
|
2014-10-20 04:58:15 +00:00
|
|
|
#else
|
|
|
|
setlocale (LC_ALL, "");
|
2015-04-27 08:37:57 +00:00
|
|
|
/* qse_setdflcmgrbyid (QSE_CMGR_SLMB); */
|
2014-10-20 04:58:15 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_open_stdsios ();
|
2014-10-20 04:58:15 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2012-04-30 09:46:58 +00:00
|
|
|
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
|
|
|
|
{
|
|
|
|
print_error (QSE_T("Failed to start up winsock\n"));
|
2014-10-20 04:58:15 +00:00
|
|
|
ret = -1;
|
|
|
|
goto oops;
|
2012-04-30 09:46:58 +00:00
|
|
|
}
|
2014-10-20 04:58:15 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TODO: add an option to skip watt-32 */
|
|
|
|
_watt_do_exit = 0; /* prevent sock_init from exiting upon failure */
|
|
|
|
if (sock_init() != 0)
|
2014-11-14 02:31:47 +00:00
|
|
|
print_warning (QSE_T("Failed to initialize watt-32\n"));
|
2014-10-20 04:58:15 +00:00
|
|
|
else sock_inited = 1;
|
2012-01-03 14:41:15 +00:00
|
|
|
#endif
|
2012-04-30 09:46:58 +00:00
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
open_mpi (&mpi, argc, argv);
|
2014-10-20 04:58:15 +00:00
|
|
|
|
2018-09-13 03:16:23 +00:00
|
|
|
ret = qse_run_main(argc, argv, awk_main);
|
2012-04-30 09:46:58 +00:00
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
close_mpi (&mpi);
|
2012-10-21 16:19:03 +00:00
|
|
|
|
2012-04-30 09:46:58 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
WSACleanup ();
|
2014-10-20 04:58:15 +00:00
|
|
|
#elif defined(__DOS__)
|
|
|
|
if (sock_inited) sock_exit ();
|
2012-04-30 09:46:58 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-20 04:58:15 +00:00
|
|
|
oops:
|
2017-09-16 08:54:25 +00:00
|
|
|
qse_close_stdsios ();
|
2012-04-30 09:46:58 +00:00
|
|
|
return ret;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
2011-04-21 08:30:47 +00:00
|
|
|
|
2014-11-18 16:10:12 +00:00
|
|
|
|
|
|
|
#if defined(FAKE_SOCKET)
|
|
|
|
socket () {}
|
|
|
|
listen () {}
|
|
|
|
accept () {}
|
|
|
|
recvfrom () {}
|
|
|
|
connect () {}
|
|
|
|
getsockopt () {}
|
|
|
|
recv () {}
|
|
|
|
setsockopt () {}
|
|
|
|
send () {}
|
|
|
|
bind () {}
|
|
|
|
shutdown () {}
|
|
|
|
|
|
|
|
void* memmove (void* x, void* y, size_t z) {}
|
|
|
|
#endif
|