2007-05-02 01:07:00 +00:00
|
|
|
/*
|
2008-12-10 00:52:03 +00:00
|
|
|
* $Id: awk.c 463 2008-12-09 06:52:03Z baconevi $
|
2007-05-02 01:07:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ase/awk/awk.h>
|
2008-08-08 05:02:08 +00:00
|
|
|
#include <ase/cmn/sll.h>
|
2008-08-17 08:08:53 +00:00
|
|
|
#include <ase/cmn/mem.h>
|
2008-08-21 04:58:19 +00:00
|
|
|
#include <ase/cmn/chr.h>
|
2008-08-23 02:13:47 +00:00
|
|
|
#include <ase/cmn/opt.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#include <ase/utl/stdio.h>
|
|
|
|
#include <ase/utl/main.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2008-10-01 05:14:20 +00:00
|
|
|
#define ABORT(label) goto label
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <process.h>
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#pragma warning (disable: 4296)
|
|
|
|
|
2008-08-19 21:16:02 +00:00
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG)
|
|
|
|
#define _CRTDBG_MAP_ALLOC
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
2008-10-13 09:08:26 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
#define SRCIO_FILE 1
|
|
|
|
#define SRCIO_STR 2
|
|
|
|
|
|
|
|
typedef struct srcio_data_t
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-08-04 08:06:43 +00:00
|
|
|
int type; /* file or string */
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
ase_char_t* ptr;
|
|
|
|
ase_char_t* cur;
|
|
|
|
} str;
|
2008-08-19 05:21:48 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
ase_sll_t* sll;
|
|
|
|
ase_sll_node_t* cur;
|
2008-08-04 08:06:43 +00:00
|
|
|
FILE* handle;
|
|
|
|
} file;
|
|
|
|
} data;
|
2008-08-19 05:21:48 +00:00
|
|
|
} srcio_data_t;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
static void local_dprintf (const ase_char_t* fmt, ...)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
ase_vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* custom memory management function */
|
|
|
|
static void* custom_awk_malloc (void* custom, ase_size_t n)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2008-08-19 05:21:48 +00:00
|
|
|
return HeapAlloc ((HANDLE)custom, 0, n);
|
2007-05-02 01:07:00 +00:00
|
|
|
#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 */
|
2008-08-19 05:21:48 +00:00
|
|
|
return (ptr == NULL)?
|
|
|
|
HeapAlloc ((HANDLE)custom, 0, n):
|
|
|
|
HeapReAlloc ((HANDLE)custom, 0, ptr, n);
|
2007-05-02 01:07:00 +00:00
|
|
|
#else
|
|
|
|
return realloc (ptr, n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void custom_awk_free (void* custom, void* ptr)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2008-08-19 05:21:48 +00:00
|
|
|
HeapFree ((HANDLE)custom, 0, ptr);
|
2007-05-02 01:07:00 +00:00
|
|
|
#else
|
|
|
|
free (ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* custom miscellaneous functions */
|
|
|
|
static ase_real_t custom_awk_pow (void* custom, ase_real_t x, ase_real_t y)
|
|
|
|
{
|
|
|
|
return pow (x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_ssize_t awk_srcio_out (
|
2008-02-13 01:57:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-08-19 05:21:48 +00:00
|
|
|
/*struct srcio_data_t* srcio = (struct srcio_data_t*)arg;*/
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (cmd == ASE_AWK_IO_OPEN) return 1;
|
|
|
|
else if (cmd == ASE_AWK_IO_CLOSE)
|
|
|
|
{
|
|
|
|
fflush (stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (cmd == ASE_AWK_IO_WRITE)
|
|
|
|
{
|
2007-11-10 00:20:00 +00:00
|
|
|
ase_size_t left = size;
|
2007-11-08 00:32:00 +00:00
|
|
|
|
2007-11-10 00:20:00 +00:00
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
if (*data == ASE_T('\0'))
|
|
|
|
{
|
|
|
|
if (ase_fputc (*data, stdout) == ASE_CHAR_EOF) return -1;
|
|
|
|
left -= 1; data += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
|
|
|
int n = ase_fprintf (stdout, ASE_T("%.*s"), chunk, data);
|
|
|
|
if (n < 0) return -1;
|
|
|
|
left -= n; data += n;
|
|
|
|
}
|
2007-11-08 00:32:00 +00:00
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_ssize_t awk_extio_pipe (
|
2008-02-13 01:57:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
{
|
|
|
|
FILE* handle;
|
|
|
|
const ase_char_t* mode;
|
|
|
|
|
|
|
|
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ)
|
|
|
|
mode = ASE_T("r");
|
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_PIPE_WRITE)
|
|
|
|
mode = ASE_T("w");
|
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("opening %s of type %d (pipe)\n"), epa->name, epa->type);
|
2007-05-02 01:07:00 +00:00
|
|
|
handle = ase_popen (epa->name, mode);
|
|
|
|
if (handle == NULL) return -1;
|
|
|
|
epa->handle = (void*)handle;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("closing %s of type (pipe) %d\n"), epa->name, epa->type);
|
2007-05-02 01:07:00 +00:00
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
epa->handle = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_READ:
|
|
|
|
{
|
2008-01-16 00:36:07 +00:00
|
|
|
/*
|
2007-11-11 00:30:00 +00:00
|
|
|
int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size;
|
|
|
|
if (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
if (ferror((FILE*)epa->handle)) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ase_strlen(data);
|
2008-01-16 00:36:07 +00:00
|
|
|
*/
|
|
|
|
ase_ssize_t n = 0;
|
|
|
|
FILE* fp = (FILE*)epa->handle;
|
|
|
|
while (!ase_feof(fp) && n < size)
|
|
|
|
{
|
|
|
|
ase_cint_t c = ase_fgetc (fp);
|
|
|
|
if (c == ASE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
if (ase_ferror(fp)) n = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data[n++] = c;
|
|
|
|
}
|
|
|
|
return n;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_WRITE:
|
|
|
|
{
|
2007-05-15 01:21:00 +00:00
|
|
|
FILE* fp = (FILE*)epa->handle;
|
|
|
|
size_t left = size;
|
|
|
|
|
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
if (*data == ASE_T('\0'))
|
|
|
|
{
|
|
|
|
#if defined(ASE_CHAR_IS_WCHAR) && defined(__linux)
|
|
|
|
if (fputc ('\0', fp) == EOF)
|
|
|
|
#else
|
|
|
|
if (ase_fputc (*data, fp) == ASE_CHAR_EOF)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
left -= 1; data += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#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. */
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
2007-11-08 00:32:00 +00:00
|
|
|
int n = fprintf (fp, "%.*ls", chunk, data);
|
2007-05-15 01:21:00 +00:00
|
|
|
if (n >= 0)
|
|
|
|
{
|
|
|
|
size_t x;
|
2007-11-08 00:32:00 +00:00
|
|
|
for (x = 0; x < chunk; x++)
|
2007-05-15 01:21:00 +00:00
|
|
|
{
|
|
|
|
if (data[x] == ASE_T('\0')) break;
|
|
|
|
}
|
|
|
|
n = x;
|
|
|
|
}
|
|
|
|
#else
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
2007-11-08 00:32:00 +00:00
|
|
|
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, data);
|
2007-05-15 01:21:00 +00:00
|
|
|
#endif
|
|
|
|
|
2007-11-08 00:32:00 +00:00
|
|
|
if (n < 0 || n > chunk) return -1;
|
2007-05-15 01:21:00 +00:00
|
|
|
left -= n; data += n;
|
|
|
|
}
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_FLUSH:
|
|
|
|
{
|
|
|
|
if (epa->mode == ASE_AWK_EXTIO_PIPE_READ) return -1;
|
2007-05-15 01:21:00 +00:00
|
|
|
return fflush ((FILE*)epa->handle);
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_NEXT:
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ase_ssize_t awk_extio_file (
|
2008-02-13 01:57:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case ASE_AWK_IO_OPEN:
|
|
|
|
{
|
|
|
|
FILE* handle;
|
|
|
|
const ase_char_t* mode;
|
|
|
|
|
|
|
|
if (epa->mode == ASE_AWK_EXTIO_FILE_READ)
|
|
|
|
mode = ASE_T("r");
|
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_FILE_WRITE)
|
|
|
|
mode = ASE_T("w");
|
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_FILE_APPEND)
|
|
|
|
mode = ASE_T("a");
|
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("opening %s of type %d (file)\n"), epa->name, epa->type);
|
2007-05-02 01:07:00 +00:00
|
|
|
handle = ase_fopen (epa->name, mode);
|
|
|
|
if (handle == NULL)
|
|
|
|
{
|
|
|
|
ase_cstr_t errarg;
|
|
|
|
|
|
|
|
errarg.ptr = epa->name;
|
|
|
|
errarg.len = ase_strlen(epa->name);
|
|
|
|
|
|
|
|
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
epa->handle = (void*)handle;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_CLOSE:
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("closing %s of type %d (file)\n"), epa->name, epa->type);
|
2007-05-02 01:07:00 +00:00
|
|
|
fclose ((FILE*)epa->handle);
|
|
|
|
epa->handle = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_READ:
|
|
|
|
{
|
2008-01-16 00:36:07 +00:00
|
|
|
/*
|
2007-11-11 00:30:00 +00:00
|
|
|
int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size;
|
|
|
|
if (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
if (ferror((FILE*)epa->handle)) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ase_strlen(data);
|
2008-01-16 00:36:07 +00:00
|
|
|
*/
|
|
|
|
ase_ssize_t n = 0;
|
|
|
|
FILE* fp = (FILE*)epa->handle;
|
|
|
|
while (!ase_feof(fp) && n < size)
|
|
|
|
{
|
|
|
|
ase_cint_t c = ase_fgetc (fp);
|
|
|
|
if (c == ASE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
if (ase_ferror(fp)) n = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data[n++] = c;
|
|
|
|
}
|
|
|
|
return n;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_WRITE:
|
|
|
|
{
|
2007-05-14 00:48:00 +00:00
|
|
|
FILE* fp = (FILE*)epa->handle;
|
2007-05-15 01:21:00 +00:00
|
|
|
ase_ssize_t left = size;
|
2007-05-14 00:48:00 +00:00
|
|
|
|
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
if (*data == ASE_T('\0'))
|
|
|
|
{
|
|
|
|
if (ase_fputc (*data, fp) == ASE_CHAR_EOF) return -1;
|
|
|
|
left -= 1; data += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
2007-11-08 00:32:00 +00:00
|
|
|
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, data);
|
2007-05-14 00:48:00 +00:00
|
|
|
if (n < 0) return -1;
|
|
|
|
left -= n; data += n;
|
|
|
|
}
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_FLUSH:
|
|
|
|
{
|
|
|
|
if (fflush ((FILE*)epa->handle) == EOF) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ASE_AWK_IO_NEXT:
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
static ase_size_t infile_no = 0;
|
|
|
|
static const ase_char_t* infiles[1000] =
|
|
|
|
{
|
|
|
|
ASE_T(""),
|
|
|
|
ASE_NULL
|
|
|
|
};
|
|
|
|
|
2008-01-16 00:36:07 +00:00
|
|
|
static ase_ssize_t getdata (ase_char_t* data, ase_size_t size, FILE* fp)
|
|
|
|
{
|
|
|
|
ase_ssize_t n = 0;
|
|
|
|
while (!ase_feof(fp) && n < size)
|
|
|
|
{
|
|
|
|
ase_cint_t c = ase_fgetc (fp);
|
|
|
|
if (c == ASE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
if (ase_ferror(fp)) n = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
data[n++] = c;
|
|
|
|
if (c == ASE_T('\n')) break;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
static ase_ssize_t awk_extio_console (
|
2008-02-13 01:57:21 +00:00
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
ase_awk_extio_t* epa = (ase_awk_extio_t*)arg;
|
|
|
|
|
|
|
|
if (cmd == ASE_AWK_IO_OPEN)
|
|
|
|
{
|
|
|
|
return open_extio_console (epa);
|
|
|
|
}
|
|
|
|
else if (cmd == ASE_AWK_IO_CLOSE)
|
|
|
|
{
|
|
|
|
return close_extio_console (epa);
|
|
|
|
}
|
|
|
|
else if (cmd == ASE_AWK_IO_READ)
|
|
|
|
{
|
2008-01-16 00:36:07 +00:00
|
|
|
ase_ssize_t n;
|
|
|
|
/*int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size;
|
2007-11-11 00:30:00 +00:00
|
|
|
|
|
|
|
while (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
2008-01-16 00:36:07 +00:00
|
|
|
if (ferror((FILE*)epa->handle)) return -1;*/
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-01-16 00:36:07 +00:00
|
|
|
while ((n = getdata(data,size,(FILE*)epa->handle)) == 0)
|
|
|
|
{
|
2007-05-02 01:07:00 +00:00
|
|
|
/* it has reached the end of the current file.
|
|
|
|
* open the next file if available */
|
|
|
|
if (infiles[infile_no] == ASE_NULL)
|
|
|
|
{
|
|
|
|
/* no more input console */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (infiles[infile_no][0] == ASE_T('\0'))
|
|
|
|
{
|
|
|
|
if (epa->handle != ASE_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
/* TODO: ................................ */
|
|
|
|
if (fclose ((FILE*)epa->handle) == EOF)
|
|
|
|
{
|
|
|
|
ase_cstr_t errarg;
|
|
|
|
|
2007-11-08 00:32:00 +00:00
|
|
|
errarg.ptr = ASE_T("console");
|
2007-05-02 01:07:00 +00:00
|
|
|
errarg.len = 7;
|
|
|
|
|
|
|
|
ase_awk_setrunerror (epa->run, ASE_AWK_ECLOSE, 0, &errarg, 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
epa->handle = stdin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE* fp = ase_fopen (infiles[infile_no], ASE_T("r"));
|
|
|
|
if (fp == ASE_NULL)
|
|
|
|
{
|
|
|
|
ase_cstr_t errarg;
|
|
|
|
|
|
|
|
errarg.ptr = infiles[infile_no];
|
|
|
|
errarg.len = ase_strlen(infiles[infile_no]);
|
|
|
|
|
|
|
|
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ase_awk_setfilename (
|
|
|
|
epa->run, infiles[infile_no],
|
|
|
|
ase_strlen(infiles[infile_no])) == -1)
|
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-29 00:11:00 +00:00
|
|
|
if (ase_awk_setglobal (
|
2007-10-30 00:20:00 +00:00
|
|
|
epa->run, ASE_AWK_GLOBAL_FNR, ase_awk_val_zero) == -1)
|
2007-10-29 00:11:00 +00:00
|
|
|
{
|
2007-10-30 00:20:00 +00:00
|
|
|
/* need to reset FNR */
|
2007-10-29 00:11:00 +00:00
|
|
|
fclose (fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
if (epa->handle != ASE_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
/* TODO: ................................ */
|
|
|
|
if (fclose ((FILE*)epa->handle) == EOF)
|
|
|
|
{
|
|
|
|
ase_cstr_t errarg;
|
|
|
|
|
|
|
|
errarg.ptr = ASE_T("console");
|
|
|
|
errarg.len = 7;
|
|
|
|
|
|
|
|
ase_awk_setrunerror (epa->run, ASE_AWK_ECLOSE, 0, &errarg, 1);
|
|
|
|
|
|
|
|
fclose (fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("open the next console [%s]\n"), infiles[infile_no]);
|
2007-05-02 01:07:00 +00:00
|
|
|
epa->handle = fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
infile_no++;
|
|
|
|
}
|
|
|
|
|
2008-01-16 00:36:07 +00:00
|
|
|
/*return ase_strlen(data);*/
|
|
|
|
return n;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
else if (cmd == ASE_AWK_IO_WRITE)
|
|
|
|
{
|
2007-11-08 00:32:00 +00:00
|
|
|
FILE* fp = (FILE*)epa->handle;
|
|
|
|
ase_ssize_t left = size;
|
|
|
|
|
|
|
|
while (left > 0)
|
|
|
|
{
|
|
|
|
if (*data == ASE_T('\0'))
|
|
|
|
{
|
|
|
|
if (ase_fputc (*data, fp) == ASE_CHAR_EOF) return -1;
|
|
|
|
left -= 1; data += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-10 00:20:00 +00:00
|
|
|
int chunk = (left > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)left;
|
2007-11-08 00:32:00 +00:00
|
|
|
int n = ase_fprintf (fp, ASE_T("%.*s"), chunk, data);
|
|
|
|
if (n < 0) return -1;
|
|
|
|
left -= n; data += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else if (cmd == ASE_AWK_IO_FLUSH)
|
|
|
|
{
|
|
|
|
if (fflush ((FILE*)epa->handle) == EOF) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (cmd == ASE_AWK_IO_NEXT)
|
|
|
|
{
|
|
|
|
return next_extio_console (epa);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_extio_console (ase_awk_extio_t* epa)
|
|
|
|
{
|
|
|
|
/* TODO: OpenConsole in GUI APPLICATION */
|
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("opening console[%s] of type %x\n"), epa->name, epa->type);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (epa->mode == ASE_AWK_EXTIO_CONSOLE_READ)
|
|
|
|
{
|
|
|
|
if (infiles[infile_no] == ASE_NULL)
|
|
|
|
{
|
|
|
|
/* no more input file */
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("console - no more file\n"));;
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (infiles[infile_no][0] == ASE_T('\0'))
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T(" console(r) - <standard input>\n"));
|
2007-05-02 01:07:00 +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 */
|
|
|
|
FILE* fp = ase_fopen (infiles[infile_no], ASE_T("r"));
|
|
|
|
if (fp == ASE_NULL)
|
|
|
|
{
|
|
|
|
ase_cstr_t errarg;
|
|
|
|
|
|
|
|
errarg.ptr = infiles[infile_no];
|
|
|
|
errarg.len = ase_strlen(infiles[infile_no]);
|
|
|
|
|
|
|
|
ase_awk_setrunerror (epa->run, ASE_AWK_EOPEN, 0, &errarg, 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T(" console(r) - %s\n"), infiles[infile_no]);
|
2007-05-02 01:07:00 +00:00
|
|
|
if (ase_awk_setfilename (
|
|
|
|
epa->run, infiles[infile_no],
|
|
|
|
ase_strlen(infiles[infile_no])) == -1)
|
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
epa->handle = fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
infile_no++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (epa->mode == ASE_AWK_EXTIO_CONSOLE_WRITE)
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T(" console(w) - <standard output>\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (ase_awk_setofilename (epa->run, ASE_T(""), 0) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
epa->handle = stdout;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int close_extio_console (ase_awk_extio_t* epa)
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("closing console of type %x\n"), epa->type);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (epa->handle != ASE_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
if (fclose ((FILE*)epa->handle) == EOF)
|
|
|
|
{
|
|
|
|
ase_cstr_t errarg;
|
|
|
|
|
|
|
|
errarg.ptr = epa->name;
|
|
|
|
errarg.len = ase_strlen(epa->name);
|
|
|
|
|
|
|
|
ase_awk_setrunerror (epa->run, ASE_AWK_ECLOSE, 0, &errarg, 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int next_extio_console (ase_awk_extio_t* epa)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
FILE* fp = (FILE*)epa->handle;
|
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("switching console[%s] of type %x\n"), epa->name, epa->type);
|
2007-05-02 01:07:00 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp != ASE_NULL && fp != stdin &&
|
|
|
|
fp != stdout && fp != stderr) fclose (fp);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
ase_awk_t* app_awk = NULL;
|
|
|
|
ase_awk_run_t* app_run = NULL;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static BOOL WINAPI stop_run (DWORD ctrl_type)
|
|
|
|
{
|
|
|
|
if (ctrl_type == CTRL_C_EVENT ||
|
|
|
|
ctrl_type == CTRL_CLOSE_EVENT)
|
|
|
|
{
|
|
|
|
ase_awk_stop (app_run);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void stop_run (int sig)
|
|
|
|
{
|
|
|
|
signal (SIGINT, SIG_IGN);
|
|
|
|
ase_awk_stop (app_run);
|
|
|
|
signal (SIGINT, stop_run);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void on_run_start (ase_awk_run_t* run, void* custom)
|
|
|
|
{
|
|
|
|
app_run = run;
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("[AWK ABOUT TO START]\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-09-28 03:51:23 +00:00
|
|
|
static ase_map_walk_t print_awk_value (
|
|
|
|
ase_map_t* map, ase_map_pair_t* pair, void* arg)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
ase_awk_run_t* run = (ase_awk_run_t*)arg;
|
2008-09-28 03:51:23 +00:00
|
|
|
local_dprintf (ASE_T("%.*s = "),
|
|
|
|
(int)ASE_MAP_KLEN(pair), ASE_MAP_KPTR(pair));
|
|
|
|
ase_awk_dprintval (run, (ase_awk_val_t*)ASE_MAP_VPTR(pair));
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("\n"));
|
2008-09-28 03:51:23 +00:00
|
|
|
return ASE_MAP_WALK_FORWARD;
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void on_run_statement (
|
|
|
|
ase_awk_run_t* run, ase_size_t line, void* custom)
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
/*local_dprintf (L"running %d\n", (int)line);*/
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void on_run_return (
|
|
|
|
ase_awk_run_t* run, ase_awk_val_t* ret, void* custom)
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("[RETURN] - "));
|
2007-05-02 01:07:00 +00:00
|
|
|
ase_awk_dprintval (run, ret);
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("[NAMED VARIABLES]\n"));
|
2008-02-13 01:39:56 +00:00
|
|
|
ase_map_walk (ase_awk_getrunnamedvarmap(run), print_awk_value, run);
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("[END NAMED VARIABLES]\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
static void on_run_end (ase_awk_run_t* run, int errnum, void* data)
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
if (errnum != ASE_AWK_ENOERR)
|
|
|
|
{
|
2008-03-04 03:58:25 +00:00
|
|
|
local_dprintf (ASE_T("[AWK ENDED WITH AN ERROR]\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
ase_printf (ASE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
|
|
|
|
errnum,
|
|
|
|
(unsigned int)ase_awk_getrunerrlin(run),
|
|
|
|
ase_awk_getrunerrmsg(run));
|
|
|
|
}
|
2008-03-04 03:58:25 +00:00
|
|
|
else local_dprintf (ASE_T("[AWK ENDED SUCCESSFULLY]\n"));
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
app_run = NULL;
|
|
|
|
}
|
|
|
|
|
2008-08-19 21:16:02 +00:00
|
|
|
/* TODO: remove otab... */
|
2007-10-13 01:13:00 +00:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
const ase_char_t* name;
|
|
|
|
int opt;
|
|
|
|
} otab[] =
|
|
|
|
{
|
|
|
|
{ ASE_T("implicit"), ASE_AWK_IMPLICIT },
|
|
|
|
{ ASE_T("explicit"), ASE_AWK_EXPLICIT },
|
2008-07-23 07:12:56 +00:00
|
|
|
{ ASE_T("bxor"), ASE_AWK_BXOR },
|
2007-10-13 01:13:00 +00:00
|
|
|
{ ASE_T("shift"), ASE_AWK_SHIFT },
|
|
|
|
{ ASE_T("idiv"), ASE_AWK_IDIV },
|
|
|
|
{ ASE_T("extio"), ASE_AWK_EXTIO },
|
2008-07-15 23:56:32 +00:00
|
|
|
{ ASE_T("newline"), ASE_AWK_NEWLINE },
|
2007-10-13 01:13:00 +00:00
|
|
|
{ ASE_T("baseone"), ASE_AWK_BASEONE },
|
|
|
|
{ ASE_T("stripspaces"), ASE_AWK_STRIPSPACES },
|
|
|
|
{ ASE_T("nextofile"), ASE_AWK_NEXTOFILE },
|
|
|
|
{ ASE_T("crfl"), ASE_AWK_CRLF },
|
|
|
|
{ ASE_T("argstomain"), ASE_AWK_ARGSTOMAIN },
|
|
|
|
{ ASE_T("reset"), ASE_AWK_RESET },
|
|
|
|
{ ASE_T("maptovar"), ASE_AWK_MAPTOVAR },
|
2008-07-15 23:56:32 +00:00
|
|
|
{ ASE_T("pablock"), ASE_AWK_PABLOCK }
|
2007-10-13 01:13:00 +00:00
|
|
|
};
|
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
static void print_usage (const ase_char_t* argv0)
|
|
|
|
{
|
2007-10-13 01:13:00 +00:00
|
|
|
int j;
|
|
|
|
|
|
|
|
ase_printf (ASE_T("Usage: %s [-m] [-d] [-a argument]* -f source-file [data-file]*\n"), argv0);
|
|
|
|
|
|
|
|
ase_printf (ASE_T("\nYou may specify the following options to change the behavior of the interpreter.\n"));
|
|
|
|
for (j = 0; j < ASE_COUNTOF(otab); j++)
|
|
|
|
{
|
|
|
|
ase_printf (ASE_T(" -%-20s -no%-20s\n"), otab[j].name, otab[j].name);
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int run_awk (ase_awk_t* awk,
|
|
|
|
const ase_char_t* mfn, ase_awk_runarg_t* runarg)
|
|
|
|
{
|
|
|
|
ase_awk_runcbs_t runcbs;
|
|
|
|
ase_awk_runios_t runios;
|
|
|
|
|
|
|
|
runios.pipe = awk_extio_pipe;
|
|
|
|
runios.file = awk_extio_file;
|
|
|
|
runios.console = awk_extio_console;
|
2008-08-19 05:21:48 +00:00
|
|
|
runios.data = ASE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
runcbs.on_start = on_run_start;
|
|
|
|
runcbs.on_statement = on_run_statement;
|
|
|
|
runcbs.on_return = on_run_return;
|
|
|
|
runcbs.on_end = on_run_end;
|
2008-08-19 05:21:48 +00:00
|
|
|
runcbs.data = ASE_NULL;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
if (ase_awk_run (awk, mfn, &runios, &runcbs, runarg, ASE_NULL) == -1)
|
|
|
|
{
|
|
|
|
ase_printf (
|
|
|
|
ASE_T("RUN ERROR: CODE [%d] LINE [%u] %s\n"),
|
|
|
|
ase_awk_geterrnum(awk),
|
|
|
|
(unsigned int)ase_awk_geterrlin(awk),
|
|
|
|
ase_awk_geterrmsg(awk));
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-26 01:30:00 +00:00
|
|
|
static int bfn_sleep (
|
|
|
|
ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl)
|
|
|
|
{
|
|
|
|
ase_size_t nargs;
|
|
|
|
ase_awk_val_t* a0;
|
|
|
|
ase_long_t lv;
|
|
|
|
ase_real_t rv;
|
|
|
|
ase_awk_val_t* r;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
nargs = ase_awk_getnargs (run);
|
|
|
|
ASE_ASSERT (nargs == 1);
|
|
|
|
|
|
|
|
a0 = ase_awk_getarg (run, 0);
|
|
|
|
|
|
|
|
n = ase_awk_valtonum (run, a0, &lv, &rv);
|
|
|
|
if (n == -1) return -1;
|
|
|
|
if (n == 1) lv = (ase_long_t)rv;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2007-10-02 00:22:00 +00:00
|
|
|
Sleep ((DWORD)(lv * 1000));
|
2007-05-26 01:30:00 +00:00
|
|
|
n = 0;
|
|
|
|
#else
|
|
|
|
n = sleep (lv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
r = ase_awk_makeintval (run, n);
|
|
|
|
if (r == ASE_NULL)
|
|
|
|
{
|
|
|
|
ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ase_awk_setretval (run, r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-08 05:02:08 +00:00
|
|
|
static void out_of_memory (void)
|
|
|
|
{
|
|
|
|
ase_fprintf (ASE_STDERR, ASE_T("Error: out of memory\n"));
|
|
|
|
}
|
|
|
|
|
2008-10-01 05:14:20 +00:00
|
|
|
struct argout_t
|
|
|
|
{
|
2008-10-13 09:08:26 +00:00
|
|
|
ase_char_t* iss; /* input source string */
|
|
|
|
ase_char_t** isf; /* input source files */
|
|
|
|
ase_size_t isfl; /* the number of input source filse */
|
|
|
|
ase_char_t* osf; /* output source file */
|
|
|
|
ase_map_t* vm;
|
2008-10-01 05:14:20 +00:00
|
|
|
};
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao)
|
2008-07-23 08:22:24 +00:00
|
|
|
{
|
2008-07-25 08:08:37 +00:00
|
|
|
static ase_opt_lng_t lng[] =
|
|
|
|
{
|
2008-08-04 08:06:43 +00:00
|
|
|
{ ASE_T("implicit"), 0 },
|
|
|
|
{ ASE_T("explicit"), 0 },
|
|
|
|
{ ASE_T("bxor"), 0 },
|
|
|
|
{ ASE_T("shift"), 0 },
|
|
|
|
{ ASE_T("idiv"), 0 },
|
|
|
|
{ ASE_T("extio"), 0 },
|
|
|
|
{ ASE_T("newline"), 0 },
|
|
|
|
{ ASE_T("baseone"), 0 },
|
|
|
|
{ ASE_T("stripspaces"), 0 },
|
|
|
|
{ ASE_T("nextofile"), 0 },
|
|
|
|
{ ASE_T("crlf"), 0 },
|
|
|
|
{ ASE_T("argstomain"), 0 },
|
|
|
|
{ ASE_T("reset"), 0 },
|
|
|
|
{ ASE_T("maptovar"), 0 },
|
|
|
|
{ ASE_T("pablock"), 0 },
|
2008-07-28 00:16:54 +00:00
|
|
|
|
|
|
|
{ ASE_T(":main"), ASE_T('m') },
|
|
|
|
{ ASE_T(":file"), ASE_T('f') },
|
|
|
|
{ ASE_T(":field-separator"), ASE_T('F') },
|
2008-10-14 05:32:58 +00:00
|
|
|
{ ASE_T(":deparse-file"), ASE_T('d') },
|
2008-07-28 00:16:54 +00:00
|
|
|
{ ASE_T(":assign"), ASE_T('v') },
|
|
|
|
|
2008-08-04 08:06:43 +00:00
|
|
|
{ ASE_T("help"), ASE_T('h') }
|
2008-07-25 08:08:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static ase_opt_t opt =
|
|
|
|
{
|
2008-10-14 05:32:58 +00:00
|
|
|
ASE_T("hm:f:F:d:v:"),
|
2008-07-25 08:08:37 +00:00
|
|
|
lng
|
|
|
|
};
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2008-08-19 21:16:02 +00:00
|
|
|
ase_cint_t c;
|
2008-10-13 09:08:26 +00:00
|
|
|
ase_size_t isfc = 16;
|
|
|
|
ase_size_t isfl = 0;
|
|
|
|
ase_char_t** isf = ASE_NULL;
|
2008-10-14 05:32:58 +00:00
|
|
|
ase_char_t* osf = ASE_NULL;
|
2008-09-30 05:07:47 +00:00
|
|
|
ase_map_t* vm = ASE_NULL;
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
isf = (ase_char_t**)malloc (ASE_SIZEOF(*isf) * isfc);
|
|
|
|
if (isf == ASE_NULL)
|
2008-09-30 05:07:47 +00:00
|
|
|
{
|
|
|
|
out_of_memory ();
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vm = ase_map_open (ASE_NULL, 0, 30, 70);
|
|
|
|
if (vm == ASE_NULL)
|
|
|
|
{
|
|
|
|
out_of_memory ();
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
|
|
|
ase_map_setcopier (vm, ASE_MAP_KEY, ASE_MAP_COPIER_INLINE);
|
|
|
|
ase_map_setcopier (vm, ASE_MAP_VAL, ASE_MAP_COPIER_INLINE);
|
|
|
|
ase_map_setscale (vm, ASE_MAP_KEY, ASE_SIZEOF(ase_char_t));
|
|
|
|
ase_map_setscale (vm, ASE_MAP_VAL, ASE_SIZEOF(ase_char_t));
|
2008-07-23 08:22:24 +00:00
|
|
|
|
|
|
|
while ((c = ase_getopt (argc, argv, &opt)) != ASE_CHAR_EOF)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2008-07-26 09:01:27 +00:00
|
|
|
case 0:
|
2008-07-27 09:37:38 +00:00
|
|
|
ase_printf (ASE_T(">>> [%s] [%s]\n"), opt.lngopt, opt.arg);
|
2008-07-26 09:01:27 +00:00
|
|
|
break;
|
|
|
|
|
2008-07-23 08:22:24 +00:00
|
|
|
case ASE_T('h'):
|
|
|
|
print_usage (argv[0]);
|
2008-10-13 09:08:26 +00:00
|
|
|
if (isf != ASE_NULL) free (isf);
|
|
|
|
if (vm != ASE_NULL) ase_map_close (vm);
|
2008-07-27 09:37:38 +00:00
|
|
|
return 1;
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2008-07-25 08:08:37 +00:00
|
|
|
case ASE_T('f'):
|
2008-08-08 05:02:08 +00:00
|
|
|
{
|
2008-10-13 09:08:26 +00:00
|
|
|
if (isfl >= isfc)
|
2008-08-11 02:27:21 +00:00
|
|
|
{
|
2008-10-13 09:08:26 +00:00
|
|
|
ase_char_t** tmp;
|
|
|
|
tmp = (ase_char_t**)realloc (isf, ASE_SIZEOF(*isf)*(isfc+16));
|
|
|
|
if (tmp == ASE_NULL)
|
|
|
|
{
|
|
|
|
out_of_memory ();
|
|
|
|
ABORT (on_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
isf = tmp;
|
|
|
|
isfc = isfc + 16;
|
2008-08-11 02:27:21 +00:00
|
|
|
}
|
2008-08-08 05:02:08 +00:00
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
isf[isfl++] = opt.arg;
|
2008-07-27 09:37:38 +00:00
|
|
|
break;
|
2008-08-08 05:02:08 +00:00
|
|
|
}
|
2008-07-27 09:37:38 +00:00
|
|
|
|
|
|
|
case ASE_T('F'):
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
2008-07-27 09:37:38 +00:00
|
|
|
ase_printf (ASE_T("[field separator] = %s\n"), opt.arg);
|
2008-07-23 08:22:24 +00:00
|
|
|
break;
|
2008-10-01 05:14:20 +00:00
|
|
|
}
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2008-10-14 05:32:58 +00:00
|
|
|
case ASE_T('d'):
|
|
|
|
{
|
|
|
|
osf = opt.arg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-30 05:07:47 +00:00
|
|
|
case ASE_T('v'):
|
|
|
|
{
|
|
|
|
ase_char_t* eq = ase_strchr(opt.arg, ASE_T('='));
|
|
|
|
if (eq == ASE_NULL)
|
|
|
|
{
|
|
|
|
/* INVALID VALUE... */
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*eq = ASE_T('\0');
|
|
|
|
|
|
|
|
if (ase_map_upsert (vm, opt.arg, ase_strlen(opt.arg)+1, eq, ase_strlen(eq)+1) == ASE_NULL)
|
|
|
|
{
|
|
|
|
out_of_memory ();
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
2008-09-30 05:07:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-07-23 08:22:24 +00:00
|
|
|
case ASE_T('?'):
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
2008-07-27 09:37:38 +00:00
|
|
|
if (opt.lngopt)
|
|
|
|
{
|
|
|
|
ase_printf (ASE_T("Error: illegal option - %s\n"), opt.lngopt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ase_printf (ASE_T("Error: illegal option - %c\n"), opt.opt);
|
|
|
|
}
|
2008-08-19 21:16:02 +00:00
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
ABORT (on_error);
|
2008-10-01 05:14:20 +00:00
|
|
|
}
|
2008-07-23 08:22:24 +00:00
|
|
|
|
|
|
|
case ASE_T(':'):
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
2008-07-27 09:37:38 +00:00
|
|
|
if (opt.lngopt)
|
|
|
|
{
|
|
|
|
ase_printf (ASE_T("Error: bad argument for %s\n"), opt.lngopt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ase_printf (ASE_T("Error: bad argument for %c\n"), opt.opt);
|
|
|
|
}
|
2008-08-19 21:16:02 +00:00
|
|
|
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
|
|
|
}
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2008-07-26 09:01:27 +00:00
|
|
|
default:
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
2008-07-23 08:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 04:52:25 +00:00
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
if (isfl <= 0)
|
2008-07-26 09:01:27 +00:00
|
|
|
{
|
2008-08-19 05:21:48 +00:00
|
|
|
if (opt.ind >= argc)
|
|
|
|
{
|
|
|
|
/* no source code specified */
|
2008-10-01 05:14:20 +00:00
|
|
|
ABORT (on_error);
|
2008-08-19 05:21:48 +00:00
|
|
|
}
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
ao->iss = argv[opt.ind++];
|
|
|
|
ao->isf = ASE_NULL;
|
|
|
|
ao->isfl = 0;
|
2008-07-26 09:01:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-13 09:08:26 +00:00
|
|
|
ao->iss = ASE_NULL;
|
|
|
|
ao->isf = isf;
|
|
|
|
ao->isfl = isfl;
|
2008-07-26 09:01:27 +00:00
|
|
|
}
|
2008-07-27 09:37:38 +00:00
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
/* TODO; remaining args are input(console) file names */
|
2008-07-23 08:22:24 +00:00
|
|
|
|
2008-10-14 05:32:58 +00:00
|
|
|
ao->osf = osf;
|
2008-10-01 05:14:20 +00:00
|
|
|
ao->vm = vm;
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
return 0;
|
2008-10-01 05:14:20 +00:00
|
|
|
|
|
|
|
on_error:
|
2008-10-13 09:08:26 +00:00
|
|
|
if (isf != ASE_NULL) free (isf);
|
2008-10-01 05:14:20 +00:00
|
|
|
if (vm != ASE_NULL) ase_map_close (vm);
|
|
|
|
return -1;
|
2008-07-23 08:22:24 +00:00
|
|
|
}
|
|
|
|
|
2008-08-08 05:02:08 +00:00
|
|
|
typedef struct extension_t
|
2008-07-21 06:42:39 +00:00
|
|
|
{
|
2008-07-21 23:27:34 +00:00
|
|
|
ase_mmgr_t mmgr;
|
2008-07-21 06:42:39 +00:00
|
|
|
ase_awk_prmfns_t prmfns;
|
|
|
|
}
|
2008-08-08 05:02:08 +00:00
|
|
|
extension_t;
|
2008-07-21 06:42:39 +00:00
|
|
|
|
2008-09-28 03:51:23 +00:00
|
|
|
static void init_awk_extension (ase_awk_t* awk)
|
2008-07-21 23:27:34 +00:00
|
|
|
{
|
2008-10-13 09:08:26 +00:00
|
|
|
extension_t* ext = (extension_t*) ase_awk_getextension(awk);
|
2008-08-19 05:21:48 +00:00
|
|
|
|
|
|
|
ext->mmgr = *ase_awk_getmmgr(awk);
|
|
|
|
ase_awk_setmmgr (awk, &ext->mmgr);
|
2008-08-21 04:58:19 +00:00
|
|
|
ase_awk_setccls (awk, ASE_CCLS_GETDFL());
|
2008-08-19 05:21:48 +00:00
|
|
|
|
|
|
|
ext->prmfns.pow = custom_awk_pow;
|
|
|
|
ext->prmfns.sprintf = custom_awk_sprintf;
|
|
|
|
ext->prmfns.dprintf = custom_awk_dprintf;
|
|
|
|
ext->prmfns.data = ASE_NULL;
|
|
|
|
ase_awk_setprmfns (awk, &ext->prmfns);
|
2008-07-21 23:27:34 +00:00
|
|
|
}
|
|
|
|
|
2008-08-04 08:06:43 +00:00
|
|
|
static ase_awk_t* open_awk (void)
|
|
|
|
{
|
|
|
|
ase_awk_t* awk;
|
|
|
|
ase_mmgr_t mmgr;
|
|
|
|
|
|
|
|
memset (&mmgr, 0, ASE_SIZEOF(mmgr));
|
2008-08-21 03:17:25 +00:00
|
|
|
mmgr.alloc = custom_awk_malloc;
|
2008-08-04 08:06:43 +00:00
|
|
|
mmgr.realloc = custom_awk_realloc;
|
|
|
|
mmgr.free = custom_awk_free;
|
2008-08-19 05:21:48 +00:00
|
|
|
|
2008-08-04 08:06:43 +00:00
|
|
|
#ifdef _WIN32
|
2008-08-19 05:21:48 +00:00
|
|
|
mmgr.data = (void*)HeapCreate (0, 1000000, 1000000); /* TODO: get size from xxxx */
|
|
|
|
if (mmgr.data == NULL)
|
2008-08-04 08:06:43 +00:00
|
|
|
{
|
|
|
|
ase_printf (ASE_T("Error: cannot create an awk heap\n"));
|
2008-08-19 05:21:48 +00:00
|
|
|
return ASE_NULL;
|
2008-08-04 08:06:43 +00:00
|
|
|
}
|
|
|
|
#else
|
2008-08-19 05:21:48 +00:00
|
|
|
mmgr.data = ASE_NULL;
|
2008-08-04 08:06:43 +00:00
|
|
|
#endif
|
|
|
|
|
2008-09-28 03:51:23 +00:00
|
|
|
awk = ase_awk_open (&mmgr, ASE_SIZEOF(extension_t));
|
2008-08-04 08:06:43 +00:00
|
|
|
if (awk == ASE_NULL)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2008-08-19 05:21:48 +00:00
|
|
|
HeapDestroy ((HANDLE)mmgr.data);
|
2008-08-04 08:06:43 +00:00
|
|
|
#endif
|
|
|
|
ase_printf (ASE_T("ERROR: cannot open awk\n"));
|
2008-08-08 05:02:08 +00:00
|
|
|
return ASE_NULL;
|
2008-08-04 08:06:43 +00:00
|
|
|
}
|
2008-09-28 03:51:23 +00:00
|
|
|
|
|
|
|
init_awk_extension (awk);
|
2008-08-04 08:06:43 +00:00
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
ase_awk_setoption (awk,
|
|
|
|
ASE_AWK_IMPLICIT | ASE_AWK_EXTIO | ASE_AWK_NEWLINE |
|
|
|
|
ASE_AWK_BASEONE | ASE_AWK_PABLOCK);
|
|
|
|
|
|
|
|
/* TODO: get depth from command line */
|
|
|
|
ase_awk_setmaxdepth (
|
|
|
|
awk, ASE_AWK_DEPTH_BLOCK_PARSE | ASE_AWK_DEPTH_EXPR_PARSE, 50);
|
|
|
|
ase_awk_setmaxdepth (
|
|
|
|
awk, ASE_AWK_DEPTH_BLOCK_RUN | ASE_AWK_DEPTH_EXPR_RUN, 500);
|
2008-08-04 08:06:43 +00:00
|
|
|
|
2008-08-19 05:21:48 +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"));
|
|
|
|
*/
|
|
|
|
/*ase_awk_setkeyword (awk, ASE_T("func"), 4, ASE_T("FX"), 2);*/
|
2008-08-04 08:06:43 +00:00
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
if (ase_awk_addfunc (awk,
|
|
|
|
ASE_T("sleep"), 5, 0,
|
|
|
|
1, 1, ASE_NULL, bfn_sleep) == ASE_NULL)
|
|
|
|
{
|
|
|
|
ase_awk_close (awk);
|
|
|
|
#ifdef _WIN32
|
|
|
|
HeapDestroy ((HANDLE)mmgr.data);
|
|
|
|
#endif
|
|
|
|
ase_printf (ASE_T("ERROR: cannot add function 'sleep'\n"));
|
|
|
|
return ASE_NULL;
|
|
|
|
}
|
2008-08-04 08:06:43 +00:00
|
|
|
|
|
|
|
return awk;
|
|
|
|
}
|
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
static void close_awk (ase_awk_t* awk)
|
2008-08-12 04:52:25 +00:00
|
|
|
{
|
2008-10-13 09:08:26 +00:00
|
|
|
extension_t* ext = (extension_t*)ase_awk_getextension(awk);
|
2008-08-19 05:21:48 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-12-10 00:52:03 +00:00
|
|
|
HANDLE heap = (HANDLE)ext->mmgr.data;
|
2008-08-19 05:21:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ase_awk_close (awk);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
HeapDestroy (heap);
|
|
|
|
#endif
|
2008-08-12 04:52:25 +00:00
|
|
|
}
|
2008-08-19 05:21:48 +00:00
|
|
|
|
2008-07-21 06:42:39 +00:00
|
|
|
static int awk_main (int argc, ase_char_t* argv[])
|
|
|
|
{
|
|
|
|
ase_awk_t* awk;
|
|
|
|
|
|
|
|
ase_awk_srcios_t srcios;
|
2008-08-04 08:06:43 +00:00
|
|
|
int i, file_count = 0;
|
2008-07-21 06:42:39 +00:00
|
|
|
const ase_char_t* mfn = ASE_NULL;
|
|
|
|
int mode = 0;
|
|
|
|
int runarg_count = 0;
|
|
|
|
ase_awk_runarg_t runarg[128];
|
|
|
|
int deparse = 0;
|
2008-10-01 05:14:20 +00:00
|
|
|
struct argout_t ao;
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
ase_memset (&ao, 0, ASE_SIZEOF(ao));
|
|
|
|
|
|
|
|
i = handle_args (argc, argv, &ao);
|
2008-07-27 09:37:38 +00:00
|
|
|
if (i == -1)
|
2008-07-21 06:42:39 +00:00
|
|
|
{
|
|
|
|
print_usage (argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-07-27 09:37:38 +00:00
|
|
|
if (i == 1) return 0;
|
2008-07-21 06:42:39 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
infiles[file_count] = ASE_NULL;
|
|
|
|
runarg[runarg_count].ptr = NULL;
|
|
|
|
runarg[runarg_count].len = 0;
|
|
|
|
|
2008-08-04 08:06:43 +00:00
|
|
|
awk = open_awk ();
|
|
|
|
if (awk == ASE_NULL) return -1;
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
app_awk = awk;
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
if (ase_awk_parsefiles (awk, ao.isf, ao.isfl, ao.osf) == -1)
|
2008-10-01 05:14:20 +00:00
|
|
|
{
|
|
|
|
ase_printf (
|
|
|
|
ASE_T("PARSE ERROR: CODE [%d] LINE [%u] %s\n"),
|
|
|
|
ase_awk_geterrnum(awk),
|
|
|
|
(unsigned int)ase_awk_geterrlin(awk),
|
|
|
|
ase_awk_geterrmsg(awk));
|
|
|
|
close_awk (awk);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-02 01:07:00 +00:00
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleCtrlHandler (stop_run, TRUE);
|
|
|
|
#else
|
|
|
|
signal (SIGINT, stop_run);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (run_awk (awk, mfn, runarg) == -1)
|
|
|
|
{
|
2008-08-19 05:21:48 +00:00
|
|
|
close_awk (awk);
|
2007-05-02 01:07:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-19 05:21:48 +00:00
|
|
|
close_awk (awk);
|
|
|
|
|
2008-10-13 09:08:26 +00:00
|
|
|
if (ao.iss != ASE_NULL) free (ao.iss);
|
|
|
|
if (ao.isf != ASE_NULL) free (ao.isf);
|
|
|
|
if (ao.osf != ASE_NULL) free (ao.osf);
|
|
|
|
if (ao.vm != ASE_NULL) ase_map_close (ao.vm);
|
2008-09-30 05:07:47 +00:00
|
|
|
|
2007-05-02 01:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-18 01:21:00 +00:00
|
|
|
int ase_main (int argc, ase_achar_t* argv[])
|
2007-05-02 01:07:00 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2007-06-20 22:29:00 +00:00
|
|
|
#if defined(_WIN32) && defined(_DEBUG) && defined(_MSC_VER)
|
|
|
|
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-18 01:21:00 +00:00
|
|
|
n = ase_runmain (argc, argv, awk_main);
|
2007-05-02 01:07:00 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(_DEBUG)
|
2007-06-20 22:29:00 +00:00
|
|
|
/*#if defined(_MSC_VER)
|
2007-05-02 01:07:00 +00:00
|
|
|
_CrtDumpMemoryLeaks ();
|
2007-06-20 22:29:00 +00:00
|
|
|
#endif*/
|
2007-05-02 01:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|