2006-03-31 16:35:37 +00:00
|
|
|
/*
|
2007-03-06 14:51:51 +00:00
|
|
|
* $Id: awk.c,v 1.185 2007-03-06 14:51:04 bacon Exp $
|
2006-03-31 16:35:37 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
#include <ase/awk/awk.h>
|
2007-02-11 14:08:08 +00:00
|
|
|
#include <ase/awk/val.h>
|
|
|
|
#include <ase/awk/map.h>
|
2007-02-20 14:15:18 +00:00
|
|
|
|
|
|
|
#include <ase/utl/ctype.h>
|
|
|
|
#include <ase/utl/stdio.h>
|
|
|
|
#include <ase/utl/main.h>
|
|
|
|
|
2006-04-10 09:26:17 +00:00
|
|
|
#include <string.h>
|
2006-08-04 16:31:22 +00:00
|
|
|
#include <signal.h>
|
2006-10-12 04:17:58 +00:00
|
|
|
#include <stdarg.h>
|
2006-10-31 14:32:50 +00:00
|
|
|
#include <math.h>
|
2006-12-24 15:14:09 +00:00
|
|
|
#include <stdlib.h>
|
2006-06-30 06:09:38 +00:00
|
|
|
|
2006-11-15 05:49:22 +00:00
|
|
|
#if defined(_WIN32)
|
2006-10-27 09:19:21 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#pragma warning (disable: 4996)
|
2006-11-28 15:09:53 +00:00
|
|
|
#pragma warning (disable: 4296)
|
2007-03-01 14:40:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux) && defined(_DEBUG)
|
|
|
|
#include <mcheck.h>
|
2006-04-16 04:31:38 +00:00
|
|
|
#endif
|
2006-04-10 09:22:05 +00:00
|
|
|
|
2006-09-01 04:03:28 +00:00
|
|
|
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
|
2006-05-12 09:39:20 +00:00
|
|
|
#define _CRTDBG_MAP_ALLOC
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
struct awk_src_io
|
2006-06-29 14:38:01 +00:00
|
|
|
{
|
2006-10-27 09:19:21 +00:00
|
|
|
const ase_char_t* input_file;
|
2006-06-29 14:38:01 +00:00
|
|
|
FILE* input_handle;
|
|
|
|
};
|
|
|
|
|
2007-03-01 14:40:13 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
struct mmgr_data_t
|
|
|
|
{
|
|
|
|
HANDLE heap;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-02-21 03:49:01 +00:00
|
|
|
#if defined(vms) || defined(__vms)
|
|
|
|
/* it seems that the main function should be placed in the main object file
|
|
|
|
* in OpenVMS. otherwise, the first function in the main object file seems
|
2007-02-21 03:49:49 +00:00
|
|
|
* to become the main function resulting in program start-up failure. */
|
2007-02-21 03:49:01 +00:00
|
|
|
#include <ase/utl/main.c>
|
|
|
|
#endif
|
|
|
|
|
2007-03-06 14:51:51 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void ase_assert_abort (void)
|
|
|
|
{
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ase_assert_printf (const ase_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
#ifdef _WIN32
|
|
|
|
int n;
|
|
|
|
ase_char_t buf[1024];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
#if defined(_WIN32)
|
|
|
|
n = _vsntprintf (buf, ASE_COUNTOF(buf), fmt, ap);
|
|
|
|
if (n < 0) buf[ASE_COUNTOF(buf)-1] = ASE_T('\0');
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER<1400)
|
|
|
|
MessageBox (NULL, buf,
|
|
|
|
ASE_T("Assertion Failure"), MB_OK|MB_ICONERROR);
|
|
|
|
#else
|
|
|
|
MessageBox (NULL, buf,
|
|
|
|
ASE_T("\uB2DD\uAE30\uB9AC \uC870\uB610"), MB_OK|MB_ICONERROR);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
ase_vprintf (fmt, ap);
|
|
|
|
#endif
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-01 14:40:13 +00:00
|
|
|
static void dprintf (const ase_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
ase_vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2007-03-06 14:51:51 +00:00
|
|
|
static void custom_awk_dprintf (void* custom, const ase_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
ase_vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-01 14:40:13 +00:00
|
|
|
/* custom memory management function */
|
|
|
|
static void* custom_awk_malloc (void* custom, ase_size_t n)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return HeapAlloc (((struct mmgr_data_t*)custom)->heap, 0, n);
|
|
|
|
#else
|
|
|
|
return malloc (n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* custom_awk_realloc (void* custom, void* ptr, ase_size_t n)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* HeapReAlloc behaves differently from realloc */
|
|
|
|
if (ptr == NULL)
|
|
|
|
return HeapAlloc (((struct mmgr_data_t*)custom)->heap, 0, n);
|
|
|
|
else
|
|
|
|
return HeapReAlloc (((struct mmgr_data_t*)custom)->heap, 0, ptr, n);
|
|
|
|
#else
|
|
|
|
return realloc (ptr, n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void custom_awk_free (void* custom, void* ptr)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
HeapFree (((struct mmgr_data_t*)custom)->heap, 0, ptr);
|
|
|
|
#else
|
|
|
|
free (ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* custom character class functions */
|
2007-02-24 14:32:44 +00:00
|
|
|
static ase_bool_t custom_awk_isupper (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isupper (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_islower (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_islower (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isalpha (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isalpha (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isdigit (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isdigit (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isxdigit (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isxdigit (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isalnum (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isalnum (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isspace (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isspace (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isprint (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isprint (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_isgraph (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_isgraph (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_iscntrl (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_iscntrl (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_bool_t custom_awk_ispunct (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_ispunct (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_cint_t custom_awk_toupper (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_toupper (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_cint_t custom_awk_tolower (void* custom, ase_cint_t c)
|
|
|
|
{
|
|
|
|
return ase_tolower (c);
|
|
|
|
}
|
|
|
|
|
2007-03-01 14:40:13 +00:00
|
|
|
|
|
|
|
/* custom miscellaneous functions */
|
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
static ase_real_t custom_awk_pow (void* custom, ase_real_t x, ase_real_t y)
|
2006-06-30 04:18:47 +00:00
|
|
|
{
|
2006-12-17 13:12:08 +00:00
|
|
|
return pow (x, y);
|
|
|
|
}
|
2006-06-30 04:18:47 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
|
|
|
|
static int custom_awk_sprintf (
|
|
|
|
void* custom, ase_char_t* buf, ase_size_t size,
|
|
|
|
const ase_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
n = ase_vsprintf (buf, size, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return n;
|
2006-06-30 04:18:47 +00:00
|
|
|
}
|
|
|
|
|
2006-10-28 05:24:08 +00:00
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
static ase_ssize_t awk_srcio_in (
|
2006-10-27 09:19:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-01-18 16:12:59 +00:00
|
|
|
{
|
2007-02-20 12:44:28 +00:00
|
|
|
struct awk_src_io* src_io = (struct awk_src_io*)arg;
|
2007-02-07 05:43:37 +00:00
|
|
|
ase_cint_t c;
|
2006-01-18 16:12:59 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (cmd == ASE_AWK_IO_OPEN)
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
if (src_io->input_file == ASE_NULL) return 0;
|
2007-02-20 14:09:44 +00:00
|
|
|
src_io->input_handle = ase_fopen (src_io->input_file, ASE_T("r"));
|
2006-08-22 15:11:13 +00:00
|
|
|
if (src_io->input_handle == NULL) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_CLOSE)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
if (src_io->input_file == ASE_NULL) return 0;
|
2006-08-22 15:11:13 +00:00
|
|
|
fclose ((FILE*)src_io->input_handle);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_READ)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
|
|
|
if (size <= 0) return -1;
|
2007-02-21 04:18:09 +00:00
|
|
|
c = ase_fgetc ((FILE*)src_io->input_handle);
|
2006-10-24 04:48:52 +00:00
|
|
|
if (c == ASE_CHAR_EOF) return 0;
|
2007-02-07 05:43:37 +00:00
|
|
|
*data = (ase_char_t)c;
|
2006-08-22 15:11:13 +00:00
|
|
|
return 1;
|
2006-08-06 15:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
static ase_ssize_t awk_srcio_out (
|
2006-10-27 09:19:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-08-06 15:03:42 +00:00
|
|
|
{
|
2007-02-20 12:44:28 +00:00
|
|
|
/*struct awk_src_io* src_io = (struct awk_src_io*)arg;*/
|
2006-08-06 15:03:42 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (cmd == ASE_AWK_IO_OPEN) return 1;
|
2006-12-24 16:07:13 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_CLOSE)
|
|
|
|
{
|
|
|
|
fflush (stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_WRITE)
|
2006-08-06 15:03:42 +00:00
|
|
|
{
|
2007-02-20 12:44:28 +00:00
|
|
|
int n = ase_fprintf (stdout, ASE_T("%.*s"), size, data);
|
|
|
|
if (n < 0) return -1;
|
|
|
|
|
2006-08-22 15:11:13 +00:00
|
|
|
return size;
|
2006-04-22 13:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
static ase_ssize_t awk_extio_pipe (
|
2006-10-27 09:19:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
|
2006-06-19 04:38:51 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_OPEN:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
|
|
|
FILE* handle;
|
2006-10-27 09:19:21 +00:00
|
|
|
const ase_char_t* mode;
|
2006-06-22 04:25:44 +00:00
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ)
|
2006-10-24 04:48:52 +00:00
|
|
|
mode = ASE_T("r");
|
2006-11-21 15:06:51 +00:00
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_PIPE_WRITE)
|
2006-10-24 04:48:52 +00:00
|
|
|
mode = ASE_T("w");
|
2006-06-22 04:25:44 +00:00
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
2006-12-24 15:14:09 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("opening %s of type %d (pipe)\n"), epa->name, epa->type);
|
2007-02-20 14:09:44 +00:00
|
|
|
handle = ase_popen (epa->name, mode);
|
2006-06-19 04:38:51 +00:00
|
|
|
if (handle == NULL) return -1;
|
|
|
|
epa->handle = (void*)handle;
|
2006-07-28 10:34:22 +00:00
|
|
|
return 1;
|
2006-06-19 04:38:51 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_CLOSE:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("closing %s of type (pipe) %d\n"), epa->name, epa->type);
|
2006-06-19 04:38:51 +00:00
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
epa->handle = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_READ:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2007-02-21 04:18:09 +00:00
|
|
|
if (ase_fgets (data, size, (FILE*)epa->handle) == ASE_NULL)
|
2007-02-10 13:52:41 +00:00
|
|
|
{
|
|
|
|
if (ferror((FILE*)epa->handle)) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-02-23 08:17:51 +00:00
|
|
|
return ase_strlen(data);
|
2006-06-19 04:38:51 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_WRITE:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2007-01-29 02:56:15 +00:00
|
|
|
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
|
|
|
|
/* fwprintf seems to return an error with the file
|
|
|
|
* pointer opened by popen, as of this writing.
|
|
|
|
* anyway, hopefully the following replacement
|
|
|
|
* will work all the way. */
|
|
|
|
int n = fprintf (
|
|
|
|
(FILE*)epa->handle, "%.*ls", size, data);
|
|
|
|
#else
|
2007-01-29 02:47:20 +00:00
|
|
|
int n = ase_fprintf (
|
|
|
|
(FILE*)epa->handle, ASE_T("%.*s"), size, data);
|
2007-01-29 02:56:15 +00:00
|
|
|
#endif
|
2006-12-24 16:07:13 +00:00
|
|
|
if (n < 0) return -1;
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
return size;
|
2006-06-19 04:38:51 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_FLUSH:
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
2006-11-21 15:06:51 +00:00
|
|
|
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ) return -1;
|
2006-08-23 15:42:16 +00:00
|
|
|
else return 0;
|
2006-08-22 15:11:13 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_NEXT:
|
2006-06-19 15:43:27 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2006-06-22 04:25:44 +00:00
|
|
|
|
|
|
|
return -1;
|
2006-06-19 04:38:51 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
static ase_ssize_t awk_extio_file (
|
2006-10-27 09:19:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
|
2006-06-19 09:10:57 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_OPEN:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
|
|
|
FILE* handle;
|
2006-10-27 09:19:21 +00:00
|
|
|
const ase_char_t* mode;
|
2006-06-22 04:25:44 +00:00
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
if (epa->mode == ASE_AWK_EXTIO_FILE_READ)
|
2006-10-24 04:48:52 +00:00
|
|
|
mode = ASE_T("r");
|
2006-11-21 15:06:51 +00:00
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_FILE_WRITE)
|
2006-10-24 04:48:52 +00:00
|
|
|
mode = ASE_T("w");
|
2006-11-21 15:06:51 +00:00
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_FILE_APPEND)
|
2006-10-24 04:48:52 +00:00
|
|
|
mode = ASE_T("a");
|
2006-06-22 04:25:44 +00:00
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("opening %s of type %d (file)\n"), epa->name, epa->type);
|
2007-02-20 14:09:44 +00:00
|
|
|
handle = ase_fopen (epa->name, mode);
|
2006-06-19 09:10:57 +00:00
|
|
|
if (handle == NULL) return -1;
|
2006-10-16 14:39:21 +00:00
|
|
|
|
2006-06-19 09:10:57 +00:00
|
|
|
epa->handle = (void*)handle;
|
2006-07-28 10:34:22 +00:00
|
|
|
return 1;
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_CLOSE:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("closing %s of type %d (file)\n"), epa->name, epa->type);
|
2006-06-19 09:10:57 +00:00
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
epa->handle = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_READ:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2007-02-21 04:18:09 +00:00
|
|
|
if (ase_fgets (data, size, (FILE*)epa->handle) == ASE_NULL)
|
2007-02-10 13:52:41 +00:00
|
|
|
{
|
|
|
|
if (ferror((FILE*)epa->handle)) return -1;
|
2006-06-19 09:10:57 +00:00
|
|
|
return 0;
|
2007-02-10 13:52:41 +00:00
|
|
|
}
|
2007-02-23 08:17:51 +00:00
|
|
|
return ase_strlen(data);
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_WRITE:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2007-01-29 02:47:20 +00:00
|
|
|
int n = ase_fprintf (
|
|
|
|
(FILE*)epa->handle, ASE_T("%.*s"), size, data);
|
2006-12-24 16:07:13 +00:00
|
|
|
if (n < 0) return -1;
|
|
|
|
|
2006-08-22 15:11:13 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_FLUSH:
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
|
|
|
if (fflush ((FILE*)epa->handle) == EOF) return -1;
|
|
|
|
return 0;
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
case ASE_AWK_IO_NEXT:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-19 15:43:27 +00:00
|
|
|
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
return -1;
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
static int open_extio_console (ase_awk_extio_t* epa);
|
|
|
|
static int close_extio_console (ase_awk_extio_t* epa);
|
|
|
|
static int next_extio_console (ase_awk_extio_t* epa);
|
2006-07-28 10:34:22 +00:00
|
|
|
|
2006-10-27 09:19:21 +00:00
|
|
|
static ase_size_t infile_no = 0;
|
2007-02-20 12:44:28 +00:00
|
|
|
static const ase_char_t* infiles[1000] =
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
ASE_T(""),
|
|
|
|
ASE_NULL
|
2006-07-28 10:34:22 +00:00
|
|
|
};
|
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
static ase_ssize_t awk_extio_console (
|
2006-10-27 09:19:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (cmd == ASE_AWK_IO_OPEN)
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
return open_extio_console (epa);
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_CLOSE)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
return close_extio_console (epa);
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_READ)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2007-02-21 04:18:09 +00:00
|
|
|
while (ase_fgets (data, size, (FILE*)epa->handle) == ASE_NULL)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2007-02-10 13:52:41 +00:00
|
|
|
if (ferror((FILE*)epa->handle)) return -1;
|
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
/* it has reached the end of the current file.
|
|
|
|
* open the next file if available */
|
2006-10-24 04:48:52 +00:00
|
|
|
if (infiles[infile_no] == ASE_NULL)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
|
|
|
/* no more input console */
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
/* is this correct??? */
|
|
|
|
/*
|
2006-10-24 04:48:52 +00:00
|
|
|
if (epa->handle != ASE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
2007-01-26 16:08:55 +00:00
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
epa->handle = ASE_NULL;
|
2006-07-28 10:34:22 +00:00
|
|
|
*/
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (infiles[infile_no][0] == ASE_T('\0'))
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
if (epa->handle != ASE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
2007-01-26 16:08:55 +00:00
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
}
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle = stdin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-20 14:09:44 +00:00
|
|
|
FILE* fp = ase_fopen (infiles[infile_no], ASE_T("r"));
|
2006-10-24 04:48:52 +00:00
|
|
|
if (fp == ASE_NULL)
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("failed to open the next console of type %x - fopen failure\n"), epa->type);
|
2006-07-27 16:50:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (epa->handle != ASE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
2007-01-26 16:08:55 +00:00
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
}
|
2006-07-28 10:34:22 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("open the next console [%s]\n"), infiles[infile_no]);
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle = fp;
|
2006-07-27 16:50:29 +00:00
|
|
|
}
|
2006-06-29 14:38:01 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
infile_no++;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 08:17:51 +00:00
|
|
|
return ase_strlen(data);
|
2006-07-28 10:34:22 +00:00
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_WRITE)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2007-02-20 12:44:28 +00:00
|
|
|
int n = ase_fprintf (
|
|
|
|
(FILE*)epa->handle, ASE_T("%.*s"), size, data);
|
|
|
|
if (n < 0) return -1;
|
2006-11-02 11:36:41 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
return size;
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_FLUSH)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
|
|
|
if (fflush ((FILE*)epa->handle) == EOF) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
else if (cmd == ASE_AWK_IO_NEXT)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
|
|
|
return next_extio_console (epa);
|
|
|
|
}
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
static int open_extio_console (ase_awk_extio_t* epa)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
|
|
|
/* TODO: OpenConsole in GUI APPLICATION */
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("opening console[%s] of type %x\n"), epa->name, epa->type);
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-11-21 15:06:51 +00:00
|
|
|
if (epa->mode == ASE_AWK_EXTIO_CONSOLE_READ)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
if (infiles[infile_no] == ASE_NULL)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
/* no more input file */
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("console - no more file\n"));;
|
2006-07-28 10:34:22 +00:00
|
|
|
return 0;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (infiles[infile_no][0] == ASE_T('\0'))
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T(" console(r) - <standard input>\n"));
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle = stdin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* a temporary variable fp is used here not to change
|
|
|
|
* any fields of epa when the open operation fails */
|
2007-02-20 14:09:44 +00:00
|
|
|
FILE* fp = ase_fopen (infiles[infile_no], ASE_T("r"));
|
2006-10-24 04:48:52 +00:00
|
|
|
if (fp == ASE_NULL)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("cannot open console of type %x - fopen failure\n"), epa->type);
|
2006-07-28 10:34:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T(" console(r) - %s\n"), infiles[infile_no]);
|
2006-11-24 13:25:12 +00:00
|
|
|
if (ase_awk_setfilename (
|
2006-10-16 14:39:21 +00:00
|
|
|
epa->run, infiles[infile_no],
|
2007-02-23 08:17:51 +00:00
|
|
|
ase_strlen(infiles[infile_no])) == -1)
|
2006-10-16 14:39:21 +00:00
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle = fp;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
infile_no++;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-11-21 15:06:51 +00:00
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_CONSOLE_WRITE)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T(" console(w) - <standard output>\n"));
|
2006-10-16 14:39:21 +00:00
|
|
|
/* TODO: does output console has a name??? */
|
2006-10-24 04:48:52 +00:00
|
|
|
/*ase_awk_setconsolename (ASE_T(""));*/
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle = stdout;
|
|
|
|
return 1;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-19 09:10:57 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
static int close_extio_console (ase_awk_extio_t* epa)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("closing console of type %x\n"), epa->type);
|
2006-07-28 10:34:22 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (epa->handle != ASE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
2007-01-26 16:08:55 +00:00
|
|
|
fclose ((FILE*)epa->handle);
|
2006-07-28 10:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: CloseConsole in GUI APPLICATION */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
static int next_extio_console (ase_awk_extio_t* epa)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
|
|
|
int n;
|
2007-01-26 16:08:55 +00:00
|
|
|
FILE* fp = (FILE*)epa->handle;
|
2006-11-17 06:51:28 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("switching console[%s] of type %x\n"), epa->name, epa->type);
|
2006-07-28 10:34:22 +00:00
|
|
|
|
|
|
|
n = open_extio_console(epa);
|
|
|
|
if (n == -1) return -1;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* if there is no more file, keep the previous handle */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (fp != ASE_NULL && fp != stdin &&
|
2006-07-28 10:34:22 +00:00
|
|
|
fp != stdout && fp != stderr) fclose (fp);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-08-04 16:31:22 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_t* app_awk = NULL;
|
2006-12-17 12:50:59 +00:00
|
|
|
ase_awk_run_t* app_run = NULL;
|
2006-08-04 16:31:22 +00:00
|
|
|
|
2006-08-28 14:30:08 +00:00
|
|
|
#ifdef _WIN32
|
2007-02-11 14:08:08 +00:00
|
|
|
static BOOL WINAPI stop_run (DWORD ctrl_type)
|
2006-08-28 14:30:08 +00:00
|
|
|
{
|
|
|
|
if (ctrl_type == CTRL_C_EVENT ||
|
|
|
|
ctrl_type == CTRL_CLOSE_EVENT)
|
|
|
|
{
|
2007-03-02 14:42:04 +00:00
|
|
|
ase_awk_stop (app_run);
|
2006-08-28 14:30:08 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
2007-02-11 14:08:08 +00:00
|
|
|
static void stop_run (int sig)
|
2006-08-04 16:31:22 +00:00
|
|
|
{
|
|
|
|
signal (SIGINT, SIG_IGN);
|
2007-03-02 11:14:35 +00:00
|
|
|
ase_awk_stop (app_run);
|
2007-02-11 14:08:08 +00:00
|
|
|
signal (SIGINT, stop_run);
|
2006-08-04 16:31:22 +00:00
|
|
|
}
|
2006-08-28 14:30:08 +00:00
|
|
|
#endif
|
2006-08-04 16:31:22 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
static void on_run_start (ase_awk_run_t* run, void* custom)
|
2006-08-04 16:31:22 +00:00
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
app_run = run;
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("[AWK ABOUT TO START]\n"));
|
2007-02-11 14:08:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-01 14:40:13 +00:00
|
|
|
static int print_awk_value (ase_awk_pair_t* pair, void* arg)
|
2007-02-11 14:08:08 +00:00
|
|
|
{
|
|
|
|
ase_awk_run_t* run = (ase_awk_run_t*)arg;
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("%s = "), (const ase_char_t*)pair->key);
|
2007-02-11 14:08:08 +00:00
|
|
|
ase_awk_dprintval (run, (ase_awk_val_t*)pair->val);
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("\n"));
|
2007-02-11 14:08:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-10-28 05:24:08 +00:00
|
|
|
|
2007-03-02 14:42:04 +00:00
|
|
|
static void on_run_statement (
|
|
|
|
ase_awk_run_t* run, ase_size_t line, void* custom)
|
|
|
|
{
|
|
|
|
dprintf (L"running %d\n", (int)line);
|
|
|
|
}
|
|
|
|
|
2007-02-11 14:08:08 +00:00
|
|
|
static void on_run_return (
|
2007-02-24 14:32:44 +00:00
|
|
|
ase_awk_run_t* run, ase_awk_val_t* ret, void* custom)
|
2007-02-11 14:08:08 +00:00
|
|
|
{
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("[RETURN] - "));
|
2007-02-11 14:08:08 +00:00
|
|
|
ase_awk_dprintval (run, ret);
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("\n"));
|
2007-02-11 14:08:08 +00:00
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("[NAMED VARIABLES]\n"));
|
2007-03-01 14:40:13 +00:00
|
|
|
ase_awk_map_walk (ase_awk_getrunnamedvarmap(run), print_awk_value, run);
|
2007-02-24 14:32:44 +00:00
|
|
|
dprintf (ASE_T("[END NAMED VARIABLES]\n"));
|
2006-08-04 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
2007-02-11 14:08:08 +00:00
|
|
|
static void on_run_end (ase_awk_run_t* run, int errnum, void* custom_data)
|
2006-08-04 16:31:22 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
if (errnum != ASE_AWK_ENOERR)
|
2006-08-10 16:06:52 +00:00
|
|
|
{
|
2007-03-04 06:40:27 +00:00
|
|
|
dprintf (ASE_T("[AWK ENDED WITH AN ERROR]\n"));
|
|
|
|
ase_printf (ASE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
|
2006-12-17 14:56:07 +00:00
|
|
|
errnum,
|
|
|
|
(unsigned int)ase_awk_getrunerrlin(run),
|
|
|
|
ase_awk_getrunerrmsg(run));
|
2006-08-10 16:06:52 +00:00
|
|
|
}
|
2007-02-24 14:32:44 +00:00
|
|
|
else dprintf (ASE_T("[AWK ENDED SUCCESSFULLY]\n"));
|
2006-08-10 16:06:52 +00:00
|
|
|
|
2006-08-04 16:31:22 +00:00
|
|
|
app_run = NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-03 10:52:36 +00:00
|
|
|
static void print_usage (const ase_char_t* argv0)
|
|
|
|
{
|
2007-02-20 14:09:44 +00:00
|
|
|
ase_printf (ASE_T("Usage: %s [-m] [-d] [-a argument]* -f source-file [data-file]*\n"), argv0);
|
2007-02-03 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2007-02-07 05:43:37 +00:00
|
|
|
static int awk_main (int argc, ase_char_t* argv[])
|
2005-11-14 15:23:54 +00:00
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_t* awk;
|
|
|
|
ase_awk_srcios_t srcios;
|
|
|
|
ase_awk_runcbs_t runcbs;
|
|
|
|
ase_awk_runios_t runios;
|
2007-02-01 08:38:24 +00:00
|
|
|
ase_awk_prmfns_t prmfns;
|
2007-02-20 12:44:28 +00:00
|
|
|
struct awk_src_io src_io = { NULL, NULL };
|
2006-12-15 14:58:37 +00:00
|
|
|
int opt, i, file_count = 0, errnum;
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
2007-03-01 14:40:13 +00:00
|
|
|
struct mmgr_data_t mmgr_data;
|
2006-08-31 04:21:04 +00:00
|
|
|
#endif
|
2006-11-19 15:24:21 +00:00
|
|
|
const ase_char_t* mfn = ASE_NULL;
|
2007-02-03 10:52:36 +00:00
|
|
|
int mode = 0;
|
|
|
|
int runarg_count = 0;
|
|
|
|
ase_awk_runarg_t runarg[128];
|
|
|
|
int deparse = 0;
|
2005-11-14 15:23:54 +00:00
|
|
|
|
2007-02-18 11:21:34 +00:00
|
|
|
opt = ASE_AWK_IMPLICIT |
|
2006-11-27 04:33:22 +00:00
|
|
|
ASE_AWK_EXPLICIT |
|
2007-01-23 14:23:18 +00:00
|
|
|
ASE_AWK_UNIQUEFN |
|
2006-12-04 06:04:07 +00:00
|
|
|
ASE_AWK_IDIV |
|
2006-11-27 04:33:22 +00:00
|
|
|
ASE_AWK_SHADING |
|
|
|
|
ASE_AWK_SHIFT |
|
|
|
|
ASE_AWK_EXTIO |
|
2006-12-26 10:25:19 +00:00
|
|
|
ASE_AWK_BLOCKLESS |
|
2007-01-25 14:14:56 +00:00
|
|
|
ASE_AWK_STRBASEONE |
|
2006-11-27 04:33:22 +00:00
|
|
|
ASE_AWK_STRIPSPACES |
|
|
|
|
ASE_AWK_NEXTOFILE;
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-08-26 16:30:53 +00:00
|
|
|
if (argc <= 1)
|
|
|
|
{
|
2007-02-03 10:52:36 +00:00
|
|
|
print_usage (argv[0]);
|
2006-08-26 16:30:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-25 03:31:09 +00:00
|
|
|
for (i = 1; i < argc; i++)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2007-02-03 10:52:36 +00:00
|
|
|
if (mode == 0)
|
2006-06-29 14:38:01 +00:00
|
|
|
{
|
2007-02-23 08:17:51 +00:00
|
|
|
if (ase_strcmp(argv[i], ASE_T("-m")) == 0)
|
2007-02-03 10:52:36 +00:00
|
|
|
{
|
|
|
|
mfn = ASE_T("main");
|
|
|
|
}
|
2007-02-23 08:17:51 +00:00
|
|
|
else if (ase_strcmp(argv[i], ASE_T("-d")) == 0)
|
2007-02-03 10:52:36 +00:00
|
|
|
{
|
|
|
|
deparse = 1;
|
|
|
|
}
|
2007-02-23 08:17:51 +00:00
|
|
|
else if (ase_strcmp(argv[i], ASE_T("-f")) == 0)
|
2007-02-03 10:52:36 +00:00
|
|
|
{
|
|
|
|
/* specify source file */
|
|
|
|
mode = 1;
|
|
|
|
}
|
2007-02-23 08:17:51 +00:00
|
|
|
else if (ase_strcmp(argv[i], ASE_T("-a")) == 0)
|
2007-02-03 10:52:36 +00:00
|
|
|
{
|
|
|
|
/* specify arguments */
|
|
|
|
mode = 2;
|
|
|
|
}
|
|
|
|
else if (argv[i][0] == ASE_T('-'))
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (file_count < ASE_COUNTOF(infiles)-1)
|
|
|
|
{
|
|
|
|
infiles[file_count] = argv[i];
|
|
|
|
file_count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-29 14:38:01 +00:00
|
|
|
}
|
2007-02-03 10:52:36 +00:00
|
|
|
else if (mode == 1) /* source mode */
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2007-02-03 10:52:36 +00:00
|
|
|
if (argv[i][0] == ASE_T('-'))
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src_io.input_file != NULL)
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-25 03:31:09 +00:00
|
|
|
src_io.input_file = argv[i];
|
2007-02-03 10:52:36 +00:00
|
|
|
mode = 0;
|
2006-04-10 09:22:05 +00:00
|
|
|
}
|
2007-02-03 10:52:36 +00:00
|
|
|
else if (mode == 2) /* argument mode */
|
2006-06-29 14:38:01 +00:00
|
|
|
{
|
2007-02-03 10:52:36 +00:00
|
|
|
if (argv[i][0] == ASE_T('-'))
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runarg_count >= ASE_COUNTOF(runarg)-1)
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
runarg[runarg_count].ptr = argv[i];
|
2007-02-23 08:17:51 +00:00
|
|
|
runarg[runarg_count].len = ase_strlen(argv[i]);
|
2007-02-03 10:52:36 +00:00
|
|
|
runarg_count++;
|
|
|
|
mode = 0;
|
2006-06-29 14:38:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-03 10:52:36 +00:00
|
|
|
infiles[file_count] = ASE_NULL;
|
|
|
|
runarg[runarg_count].ptr = NULL;
|
|
|
|
runarg[runarg_count].len = 0;
|
|
|
|
|
|
|
|
if (mode != 0 || src_io.input_file == NULL)
|
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-01 08:38:24 +00:00
|
|
|
memset (&prmfns, 0, ASE_SIZEOF(prmfns));
|
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
prmfns.mmgr.malloc = custom_awk_malloc;
|
|
|
|
prmfns.mmgr.realloc = custom_awk_realloc;
|
|
|
|
prmfns.mmgr.free = custom_awk_free;
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
2007-03-01 14:40:13 +00:00
|
|
|
mmgr_data.heap = HeapCreate (0, 1000000, 1000000);
|
|
|
|
if (mmgr_data.heap == NULL)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
2007-02-20 14:09:44 +00:00
|
|
|
ase_printf (ASE_T("Error: cannot create an awk heap\n"));
|
2006-08-31 04:21:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-03-01 14:40:13 +00:00
|
|
|
prmfns.mmgr.custom_data = &mmgr_data;
|
2007-02-23 08:28:39 +00:00
|
|
|
#else
|
|
|
|
prmfns.mmgr.custom_data = NULL;
|
2006-08-31 04:21:04 +00:00
|
|
|
#endif
|
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
prmfns.ccls.is_upper = custom_awk_isupper;
|
|
|
|
prmfns.ccls.is_lower = custom_awk_islower;
|
|
|
|
prmfns.ccls.is_alpha = custom_awk_isalpha;
|
|
|
|
prmfns.ccls.is_digit = custom_awk_isdigit;
|
|
|
|
prmfns.ccls.is_xdigit = custom_awk_isxdigit;
|
|
|
|
prmfns.ccls.is_alnum = custom_awk_isalnum;
|
|
|
|
prmfns.ccls.is_space = custom_awk_isspace;
|
|
|
|
prmfns.ccls.is_print = custom_awk_isprint;
|
|
|
|
prmfns.ccls.is_graph = custom_awk_isgraph;
|
|
|
|
prmfns.ccls.is_cntrl = custom_awk_iscntrl;
|
|
|
|
prmfns.ccls.is_punct = custom_awk_ispunct;
|
|
|
|
prmfns.ccls.to_upper = custom_awk_toupper;
|
|
|
|
prmfns.ccls.to_lower = custom_awk_tolower;
|
2007-02-23 08:28:39 +00:00
|
|
|
prmfns.ccls.custom_data = NULL;
|
|
|
|
|
2007-02-24 14:32:44 +00:00
|
|
|
prmfns.misc.pow = custom_awk_pow;
|
|
|
|
prmfns.misc.sprintf = custom_awk_sprintf;
|
|
|
|
prmfns.misc.dprintf = custom_awk_dprintf;
|
2007-02-23 08:28:39 +00:00
|
|
|
prmfns.misc.custom_data = NULL;
|
|
|
|
|
2007-02-01 08:38:24 +00:00
|
|
|
if ((awk = ase_awk_open(&prmfns, ASE_NULL, &errnum)) == ASE_NULL)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2007-03-01 14:40:13 +00:00
|
|
|
HeapDestroy (mmgr_data.heap);
|
2006-08-31 04:21:04 +00:00
|
|
|
#endif
|
2007-02-20 14:09:44 +00:00
|
|
|
ase_printf (
|
2006-12-24 17:21:24 +00:00
|
|
|
ASE_T("ERROR: cannot open awk [%d] %s\n"),
|
2007-03-04 15:04:41 +00:00
|
|
|
errnum, ase_awk_geterrstr(ASE_NULL, errnum));
|
2006-08-31 04:21:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-11 14:08:08 +00:00
|
|
|
app_awk = awk;
|
|
|
|
|
2007-01-23 14:23:18 +00:00
|
|
|
ase_awk_setoption (awk, opt);
|
2006-08-04 17:02:02 +00:00
|
|
|
|
2007-03-04 15:12:13 +00:00
|
|
|
ase_awk_seterrstr (awk, ASE_AWK_EGBLRED, ASE_T("\uC804\uC5ED\uBCC0\uC218 \'%.*s\'\uAC00 \uC7AC\uC815\uC758 \uB418\uC5C8\uC2B5\uB2C8\uB2E4"));
|
|
|
|
ase_awk_seterrstr (awk, ASE_AWK_EAFNRED, ASE_T("\uD568\uC218 \'%.*s\'\uAC00 \uC7AC\uC815\uC758 \uB418\uC5C8\uC2B5\uB2C8\uB2E4"));
|
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
srcios.in = awk_srcio_in;
|
|
|
|
srcios.out = deparse? awk_srcio_out: NULL;
|
2006-08-06 08:16:03 +00:00
|
|
|
srcios.custom_data = &src_io;
|
2006-08-04 17:54:52 +00:00
|
|
|
|
2007-01-23 14:23:18 +00:00
|
|
|
ase_awk_setmaxdepth (
|
|
|
|
awk, ASE_AWK_DEPTH_BLOCK_PARSE | ASE_AWK_DEPTH_EXPR_PARSE, 20);
|
|
|
|
ase_awk_setmaxdepth (
|
|
|
|
awk, ASE_AWK_DEPTH_BLOCK_RUN | ASE_AWK_DEPTH_EXPR_RUN, 50);
|
2006-11-26 14:41:23 +00:00
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
if (ase_awk_parse (awk, &srcios) == -1)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2007-02-20 14:09:44 +00:00
|
|
|
ase_printf (
|
2007-03-04 06:40:27 +00:00
|
|
|
ASE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
|
|
|
|
ase_awk_geterrnum(awk),
|
2006-12-19 14:26:27 +00:00
|
|
|
(unsigned int)ase_awk_geterrlin(awk),
|
2007-03-04 06:40:27 +00:00
|
|
|
ase_awk_geterrmsg(awk));
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_close (awk);
|
2005-11-14 15:23:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-28 14:30:08 +00:00
|
|
|
#ifdef _WIN32
|
2007-02-11 14:08:08 +00:00
|
|
|
SetConsoleCtrlHandler (stop_run, TRUE);
|
2006-08-28 14:30:08 +00:00
|
|
|
#else
|
2007-02-11 14:08:08 +00:00
|
|
|
signal (SIGINT, stop_run);
|
2006-08-28 14:30:08 +00:00
|
|
|
#endif
|
2006-08-04 16:31:22 +00:00
|
|
|
|
2007-02-20 12:44:28 +00:00
|
|
|
runios.pipe = awk_extio_pipe;
|
|
|
|
runios.file = awk_extio_file;
|
|
|
|
runios.console = awk_extio_console;
|
2006-08-04 17:36:40 +00:00
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
runcbs.on_start = on_run_start;
|
2007-03-02 14:42:04 +00:00
|
|
|
runcbs.on_statement = on_run_statement;
|
2007-02-11 14:08:08 +00:00
|
|
|
runcbs.on_return = on_run_return;
|
2006-12-16 14:48:31 +00:00
|
|
|
runcbs.on_end = on_run_end;
|
2006-10-24 04:48:52 +00:00
|
|
|
runcbs.custom_data = ASE_NULL;
|
|
|
|
|
2006-11-28 04:30:57 +00:00
|
|
|
if (ase_awk_run (awk, mfn, &runios, &runcbs, runarg, ASE_NULL) == -1)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2007-02-20 14:09:44 +00:00
|
|
|
ase_printf (
|
2007-03-04 15:04:41 +00:00
|
|
|
ASE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
|
|
|
|
ase_awk_geterrnum(awk),
|
|
|
|
(unsigned int)ase_awk_geterrlin(awk),
|
|
|
|
ase_awk_geterrmsg(awk));
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_close (awk);
|
2006-04-06 16:25:37 +00:00
|
|
|
return -1;
|
2006-03-05 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_close (awk);
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
2007-03-01 14:40:13 +00:00
|
|
|
HeapDestroy (mmgr_data.heap);
|
2006-08-31 04:21:04 +00:00
|
|
|
#endif
|
2006-05-12 09:39:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-30 17:50:38 +00:00
|
|
|
|
2007-01-26 15:50:48 +00:00
|
|
|
int ase_main (int argc, ase_char_t* argv[])
|
2006-05-12 09:39:20 +00:00
|
|
|
{
|
|
|
|
int n;
|
2007-03-01 14:40:13 +00:00
|
|
|
|
2006-05-12 11:10:26 +00:00
|
|
|
#if defined(__linux) && defined(_DEBUG)
|
2006-05-12 09:39:20 +00:00
|
|
|
mtrace ();
|
|
|
|
#endif
|
2007-03-01 14:40:13 +00:00
|
|
|
/*
|
|
|
|
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
|
2006-05-12 10:56:18 +00:00
|
|
|
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF);
|
2007-03-01 14:40:13 +00:00
|
|
|
#endif
|
|
|
|
*/
|
2006-05-12 09:39:20 +00:00
|
|
|
|
2007-02-07 05:43:37 +00:00
|
|
|
n = awk_main (argc, argv);
|
2006-05-12 09:39:20 +00:00
|
|
|
|
2006-05-12 11:10:26 +00:00
|
|
|
#if defined(__linux) && defined(_DEBUG)
|
2006-01-30 17:50:38 +00:00
|
|
|
muntrace ();
|
|
|
|
#endif
|
2006-10-27 10:28:53 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
|
|
|
#if defined(_MSC_VER)
|
2006-05-12 09:39:20 +00:00
|
|
|
_CrtDumpMemoryLeaks ();
|
2006-10-27 10:28:53 +00:00
|
|
|
#endif
|
|
|
|
_tprintf (_T("Press ENTER to quit\n"));
|
2006-05-13 15:51:43 +00:00
|
|
|
getchar ();
|
2006-05-12 09:39:20 +00:00
|
|
|
#endif
|
2006-05-13 15:51:43 +00:00
|
|
|
|
2006-05-12 09:39:20 +00:00
|
|
|
return n;
|
2005-11-14 15:23:54 +00:00
|
|
|
}
|