2006-03-31 16:35:37 +00:00
|
|
|
/*
|
2007-02-10 13:52:41 +00:00
|
|
|
* $Id: awk.c,v 1.164 2007-02-10 13:52:41 bacon Exp $
|
2006-03-31 16:35:37 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
#include <ase/awk/awk.h>
|
2006-03-31 18:13:31 +00:00
|
|
|
#include <stdio.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-17 13:12:08 +00:00
|
|
|
#include <limits.h>
|
2006-11-28 15:09:53 +00:00
|
|
|
#include <assert.h>
|
2006-12-24 15:14:09 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#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)
|
2006-12-17 12:50:59 +00:00
|
|
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
2006-11-15 05:49:22 +00:00
|
|
|
#include <ctype.h>
|
2006-12-17 12:50:59 +00:00
|
|
|
#include <locale.h>
|
2006-10-27 09:19:21 +00:00
|
|
|
#else
|
2006-12-17 12:50:59 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2006-12-17 13:12:08 +00:00
|
|
|
#include <locale.h>
|
2006-04-16 04:31:38 +00:00
|
|
|
#endif
|
2006-04-10 09:22:05 +00:00
|
|
|
|
2007-01-28 11:28:27 +00:00
|
|
|
#include "../../etc/printf.c"
|
|
|
|
#include "../../etc/main.c"
|
|
|
|
|
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
|
|
|
|
|
2006-05-12 11:10:26 +00:00
|
|
|
#if defined(__linux) && defined(_DEBUG)
|
2006-05-12 09:39:20 +00:00
|
|
|
#include <mcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2006-12-26 10:25:19 +00:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 2048
|
|
|
|
#endif
|
|
|
|
|
2006-06-29 14:38:01 +00:00
|
|
|
struct src_io
|
|
|
|
{
|
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;
|
|
|
|
};
|
|
|
|
|
2006-12-17 13:12:08 +00:00
|
|
|
static ase_real_t awk_pow (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
|
|
|
|
2006-12-17 13:12:08 +00:00
|
|
|
static void awk_abort (void* custom_data)
|
|
|
|
{
|
|
|
|
abort ();
|
2006-06-30 04:18:47 +00:00
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static int awk_sprintf (
|
2006-11-15 05:49:22 +00:00
|
|
|
ase_char_t* buf, ase_size_t len, const ase_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2007-01-26 15:50:48 +00:00
|
|
|
n = ase_vsprintf (buf, len, fmt, ap);
|
2006-11-15 05:49:22 +00:00
|
|
|
va_end (ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void awk_aprintf (const ase_char_t* fmt, ...)
|
2006-10-12 04:17:58 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
#ifdef _WIN32
|
2006-11-18 15:36:57 +00:00
|
|
|
int n;
|
2006-10-27 09:19:21 +00:00
|
|
|
ase_char_t buf[1024];
|
2006-10-12 04:17:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2006-11-15 05:49:22 +00:00
|
|
|
#if defined(_WIN32)
|
2006-11-29 03:55:57 +00:00
|
|
|
n = _vsntprintf (buf, ASE_COUNTOF(buf), fmt, ap);
|
|
|
|
if (n < 0) buf[ASE_COUNTOF(buf)-1] = ASE_T('\0');
|
2006-10-27 09:19:21 +00:00
|
|
|
|
2006-11-15 05:49:22 +00:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER<1400)
|
2006-11-18 15:36:57 +00:00
|
|
|
MessageBox (NULL, buf,
|
|
|
|
ASE_T("Assertion Failure"), MB_OK|MB_ICONERROR);
|
2006-11-15 05:49:22 +00:00
|
|
|
#else
|
2006-11-18 15:36:57 +00:00
|
|
|
MessageBox (NULL, buf,
|
|
|
|
ASE_T("\uB2DD\uAE30\uB9AC \uC870\uB610"), MB_OK|MB_ICONERROR);
|
2006-11-15 05:49:22 +00:00
|
|
|
#endif
|
2006-10-12 04:17:58 +00:00
|
|
|
#else
|
2007-01-26 15:50:48 +00:00
|
|
|
ase_vprintf (fmt, ap);
|
2006-10-12 04:17:58 +00:00
|
|
|
#endif
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void awk_dprintf (const ase_char_t* fmt, ...)
|
2006-10-28 05:24:08 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
2007-01-26 15:50:48 +00:00
|
|
|
ase_vfprintf (stderr, fmt, ap);
|
2006-10-28 05:24:08 +00:00
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2006-12-17 12:50:59 +00:00
|
|
|
static void awk_printf (const ase_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
2007-01-26 15:50:48 +00:00
|
|
|
ase_vprintf (fmt, ap);
|
2006-12-17 12:50:59 +00:00
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2006-12-17 13:12:08 +00:00
|
|
|
static FILE* awk_fopen (const ase_char_t* path, const ase_char_t* mode)
|
2006-10-31 14:32:50 +00:00
|
|
|
{
|
2007-02-03 10:52:36 +00:00
|
|
|
#if defined(_WIN32)
|
2006-12-17 13:12:08 +00:00
|
|
|
return _tfopen (path, mode);
|
|
|
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
|
|
|
return fopen (path, mode);
|
|
|
|
#else
|
2006-12-26 10:25:19 +00:00
|
|
|
|
2006-12-17 13:12:08 +00:00
|
|
|
char path_mb[PATH_MAX + 1];
|
|
|
|
char mode_mb[32];
|
|
|
|
size_t n;
|
2006-10-28 05:24:08 +00:00
|
|
|
|
2006-12-19 14:49:24 +00:00
|
|
|
n = wcstombs (path_mb, path, ASE_COUNTOF(path_mb));
|
2007-02-01 09:19:15 +00:00
|
|
|
if (n == (size_t)-1) return NULL;
|
2006-12-17 13:12:08 +00:00
|
|
|
if (n == ASE_COUNTOF(path_mb)) path_mb[ASE_COUNTOF(path_mb)-1] = '\0';
|
|
|
|
|
2006-12-19 14:49:24 +00:00
|
|
|
n = wcstombs (mode_mb, mode, ASE_COUNTOF(mode_mb));
|
2007-02-01 09:19:15 +00:00
|
|
|
if (n == (size_t)-1) return NULL;
|
2006-12-17 13:12:08 +00:00
|
|
|
if (n == ASE_COUNTOF(mode_mb)) path_mb[ASE_COUNTOF(mode_mb)-1] = '\0';
|
|
|
|
|
|
|
|
return fopen (path_mb, mode_mb);
|
|
|
|
#endif
|
2006-12-16 16:14:40 +00:00
|
|
|
}
|
|
|
|
|
2006-12-17 13:12:08 +00:00
|
|
|
static FILE* awk_popen (const ase_char_t* cmd, const ase_char_t* mode)
|
2006-06-30 04:25:53 +00:00
|
|
|
{
|
2006-11-15 05:49:22 +00:00
|
|
|
#if defined(_WIN32)
|
2006-06-30 04:25:53 +00:00
|
|
|
return _tpopen (cmd, mode);
|
2006-12-17 13:12:08 +00:00
|
|
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
|
|
|
return popen (cmd, mode);
|
2006-06-30 04:25:53 +00:00
|
|
|
#else
|
2006-12-17 13:12:08 +00:00
|
|
|
char cmd_mb[PATH_MAX + 1];
|
|
|
|
char mode_mb[32];
|
|
|
|
size_t n;
|
2006-06-30 04:25:53 +00:00
|
|
|
|
2006-12-19 14:49:24 +00:00
|
|
|
n = wcstombs (cmd_mb, cmd, ASE_COUNTOF(cmd_mb));
|
2007-02-01 09:19:15 +00:00
|
|
|
if (n == (size_t)-1) return NULL;
|
2006-12-17 13:12:08 +00:00
|
|
|
if (n == ASE_COUNTOF(cmd_mb)) cmd_mb[ASE_COUNTOF(cmd_mb)-1] = '\0';
|
|
|
|
|
2006-12-19 14:49:24 +00:00
|
|
|
n = wcstombs (mode_mb, mode, ASE_COUNTOF(mode_mb));
|
2007-02-01 09:19:15 +00:00
|
|
|
if (n == (size_t)-1) return NULL;
|
2006-12-17 13:12:08 +00:00
|
|
|
if (n == ASE_COUNTOF(mode_mb)) cmd_mb[ASE_COUNTOF(mode_mb)-1] = '\0';
|
2006-12-23 06:33:47 +00:00
|
|
|
|
2006-06-30 04:25:53 +00:00
|
|
|
return popen (cmd_mb, mode_mb);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-12-17 13:12:08 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#define awk_fgets _fgetts
|
2006-12-24 16:07:13 +00:00
|
|
|
#define awk_fgetc _fgettc
|
2006-12-17 13:12:08 +00:00
|
|
|
#define awk_fputs _fputts
|
|
|
|
#define awk_fputc _fputtc
|
|
|
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
|
|
|
#define awk_fgets fgets
|
2006-12-24 16:07:13 +00:00
|
|
|
#define awk_fgetc fgetc
|
2006-12-17 13:12:08 +00:00
|
|
|
#define awk_fputs fputs
|
|
|
|
#define awk_fputc fputc
|
2006-06-30 04:18:47 +00:00
|
|
|
#else
|
2006-12-17 13:12:08 +00:00
|
|
|
#define awk_fgets fgetws
|
2006-12-24 16:07:13 +00:00
|
|
|
#define awk_fgetc fgetwc
|
2006-12-17 13:12:08 +00:00
|
|
|
#define awk_fputs fputws
|
|
|
|
#define awk_fputc fputwc
|
2006-06-30 04:18:47 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-27 09:19:21 +00:00
|
|
|
static ase_ssize_t process_source (
|
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-01-18 16:12:59 +00:00
|
|
|
{
|
2006-06-29 14:38:01 +00:00
|
|
|
struct src_io* src_io = (struct 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;
|
2006-12-17 13:12:08 +00:00
|
|
|
src_io->input_handle = awk_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;
|
2006-12-24 16:07:13 +00:00
|
|
|
c = awk_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;
|
|
|
|
}
|
|
|
|
|
2006-10-27 09:19:21 +00:00
|
|
|
static ase_ssize_t dump_source (
|
|
|
|
int cmd, void* arg, ase_char_t* data, ase_size_t size)
|
2006-08-06 15:03:42 +00:00
|
|
|
{
|
2006-08-26 16:30:53 +00:00
|
|
|
/*struct src_io* src_io = (struct 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
|
|
|
{
|
2006-10-27 09:19:21 +00:00
|
|
|
ase_size_t i;
|
2006-08-22 15:11:13 +00:00
|
|
|
for (i = 0; i < size; i++)
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
2006-12-24 16:07:13 +00:00
|
|
|
if (awk_fputc (data[i], stdout) == ASE_CHAR_EOF) return -1;
|
2006-06-13 04:26:24 +00:00
|
|
|
}
|
2006-08-22 15:11:13 +00:00
|
|
|
return size;
|
2006-04-22 13:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-27 09:19:21 +00:00
|
|
|
static ase_ssize_t process_extio_pipe (
|
|
|
|
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
|
|
|
|
2006-12-17 14:56:07 +00:00
|
|
|
awk_dprintf (ASE_T("opening %s of type %d (pipe)\n"), epa->name, epa->type);
|
2006-12-17 13:12:08 +00:00
|
|
|
handle = awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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-10 13:52:41 +00:00
|
|
|
if (awk_fgets (data, size, (FILE*)epa->handle) == ASE_NULL)
|
|
|
|
{
|
|
|
|
if (ferror((FILE*)epa->handle)) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
return ase_awk_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
|
|
|
}
|
|
|
|
|
2006-10-27 09:19:21 +00:00
|
|
|
static ase_ssize_t process_extio_file (
|
|
|
|
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? */
|
|
|
|
|
2006-12-17 14:56:07 +00:00
|
|
|
awk_dprintf (ASE_T("opening %s of type %d (file)\n"), epa->name, epa->type);
|
2006-12-17 13:12:08 +00:00
|
|
|
handle = awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
{
|
2006-12-17 13:12:08 +00:00
|
|
|
if (awk_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
|
|
|
}
|
2006-10-24 04:48:52 +00:00
|
|
|
return ase_awk_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
|
|
|
{
|
2006-12-24 16:07:13 +00:00
|
|
|
/*
|
2006-11-02 11:36:41 +00:00
|
|
|
ase_size_t i;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2006-12-24 16:07:13 +00:00
|
|
|
if (awk_fputc (data[i], (FILE*)epa->handle) == ASE_CHAR_EOF) return -1;
|
2006-11-02 11:36:41 +00:00
|
|
|
}
|
2006-12-24 16:07:13 +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;
|
|
|
|
static const ase_char_t* infiles[10000] =
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-11-22 15:12:04 +00:00
|
|
|
/*
|
|
|
|
ASE_T("c1.txt"),
|
|
|
|
ASE_T("c2.txt"),
|
|
|
|
ASE_T("c3.txt"),
|
|
|
|
*/
|
2006-10-24 04:48:52 +00:00
|
|
|
ASE_T(""),
|
|
|
|
ASE_NULL
|
2006-07-28 10:34:22 +00:00
|
|
|
};
|
|
|
|
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-10-27 09:19:21 +00:00
|
|
|
static ase_ssize_t process_extio_console (
|
|
|
|
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-01-26 16:08:55 +00:00
|
|
|
while (awk_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
|
|
|
|
{
|
2006-12-17 13:12:08 +00:00
|
|
|
FILE* fp = awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
}
|
|
|
|
|
2006-10-24 04:48:52 +00:00
|
|
|
return ase_awk_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
|
|
|
{
|
2006-11-02 11:36:41 +00:00
|
|
|
ase_size_t i;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2006-12-24 16:07:13 +00:00
|
|
|
if (awk_fputc (data[i], (FILE*)epa->handle) == ASE_CHAR_EOF) 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
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
/* epa->name is always empty for console */
|
2006-11-28 15:09:53 +00:00
|
|
|
assert (epa->name[0] == ASE_T('\0'));
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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 */
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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 */
|
2006-12-17 13:12:08 +00:00
|
|
|
FILE* fp = awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_dprintf (ASE_T("cannot open console of type %x - fopen failure\n"), epa->type);
|
2006-07-28 10:34:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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],
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
{
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_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
|
|
|
|
static BOOL WINAPI __stop_run (DWORD ctrl_type)
|
|
|
|
{
|
|
|
|
if (ctrl_type == CTRL_C_EVENT ||
|
|
|
|
ctrl_type == CTRL_CLOSE_EVENT)
|
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_stop (app_awk, app_run);
|
2006-08-28 14:30:08 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#else
|
2006-08-04 16:31:22 +00:00
|
|
|
static void __stop_run (int sig)
|
|
|
|
{
|
|
|
|
signal (SIGINT, SIG_IGN);
|
2006-10-24 04:48:52 +00:00
|
|
|
ase_awk_stop (app_awk, app_run);
|
2006-11-15 05:49:22 +00:00
|
|
|
/*ase_awk_stoprun (awk, handle);*/
|
2006-10-24 04:48:52 +00:00
|
|
|
/*ase_awk_stopallruns (awk); */
|
2006-08-04 16:31:22 +00:00
|
|
|
signal (SIGINT, __stop_run);
|
|
|
|
}
|
2006-08-28 14:30:08 +00:00
|
|
|
#endif
|
2006-08-04 16:31:22 +00:00
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void on_run_start (
|
|
|
|
ase_awk_t* awk, ase_awk_run_t* run, void* custom_data)
|
2006-08-04 16:31:22 +00:00
|
|
|
{
|
|
|
|
app_awk = awk;
|
2006-12-16 14:48:31 +00:00
|
|
|
app_run = run;
|
2006-10-28 05:24:08 +00:00
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
awk_dprintf (ASE_T("AWK ABOUT TO START...\n"));
|
2006-08-04 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void on_run_end (
|
|
|
|
ase_awk_t* awk, 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
|
|
|
{
|
2006-12-17 14:56:07 +00:00
|
|
|
awk_dprintf (ASE_T("AWK ENDED WITH AN ERROR\n"));
|
|
|
|
awk_dprintf (ASE_T("CODE [%d] LINE [%u] %s\n"),
|
|
|
|
errnum,
|
|
|
|
(unsigned int)ase_awk_getrunerrlin(run),
|
|
|
|
ase_awk_getrunerrmsg(run));
|
2006-08-10 16:06:52 +00:00
|
|
|
}
|
2006-12-16 14:48:31 +00:00
|
|
|
else awk_dprintf (ASE_T("AWK ENDED SUCCESSFULLY\n"));
|
2006-08-10 16:06:52 +00:00
|
|
|
|
2006-08-04 16:31:22 +00:00
|
|
|
app_awk = NULL;
|
|
|
|
app_run = NULL;
|
|
|
|
}
|
|
|
|
|
2006-08-31 14:03:38 +00:00
|
|
|
#ifdef _WIN32
|
2007-02-01 08:38:24 +00:00
|
|
|
typedef struct prmfns_data_t prmfns_data_t;
|
|
|
|
struct prmfns_data_t
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
|
|
|
HANDLE heap;
|
|
|
|
};
|
2006-08-31 14:03:38 +00:00
|
|
|
#endif
|
2006-08-31 04:21:04 +00:00
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void* awk_malloc (ase_size_t n, void* custom_data)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2007-02-01 08:38:24 +00:00
|
|
|
return HeapAlloc (((prmfns_data_t*)custom_data)->heap, 0, n);
|
2006-08-31 04:21:04 +00:00
|
|
|
#else
|
|
|
|
return malloc (n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void* awk_realloc (void* ptr, ase_size_t n, void* custom_data)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2006-09-01 03:44:51 +00:00
|
|
|
/* HeapReAlloc behaves differently from realloc */
|
|
|
|
if (ptr == NULL)
|
2007-02-01 08:38:24 +00:00
|
|
|
return HeapAlloc (((prmfns_data_t*)custom_data)->heap, 0, n);
|
2006-09-01 03:44:51 +00:00
|
|
|
else
|
2007-02-01 08:38:24 +00:00
|
|
|
return HeapReAlloc (((prmfns_data_t*)custom_data)->heap, 0, ptr, n);
|
2006-08-31 04:21:04 +00:00
|
|
|
#else
|
|
|
|
return realloc (ptr, n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
static void awk_free (void* ptr, void* custom_data)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2007-02-01 08:38:24 +00:00
|
|
|
HeapFree (((prmfns_data_t*)custom_data)->heap, 0, ptr);
|
2006-08-31 04:21:04 +00:00
|
|
|
#else
|
|
|
|
free (ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-02-01 09:19:15 +00:00
|
|
|
static void* awk_memcpy (void* dst, const void* src, ase_size_t n)
|
|
|
|
{
|
|
|
|
return memcpy (dst, src, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* awk_memset (void* dst, int val, ase_size_t n)
|
|
|
|
{
|
|
|
|
return memset (dst, val, n);
|
|
|
|
}
|
|
|
|
|
2006-11-15 05:49:22 +00:00
|
|
|
#if defined(ASE_CHAR_IS_MCHAR)
|
|
|
|
#if (__TURBOC__<=513) /* turboc 2.01 or earlier */
|
2006-12-16 14:48:31 +00:00
|
|
|
static int awk_isupper (int c) { return isupper (c); }
|
|
|
|
static int awk_islower (int c) { return islower (c); }
|
|
|
|
static int awk_isalpha (int c) { return isalpha (c); }
|
|
|
|
static int awk_isdigit (int c) { return isdigit (c); }
|
|
|
|
static int awk_isxdigit (int c) { return isxdigit (c); }
|
|
|
|
static int awk_isalnum (int c) { return isalnum (c); }
|
|
|
|
static int awk_isspace (int c) { return isspace (c); }
|
|
|
|
static int awk_isprint (int c) { return isprint (c); }
|
|
|
|
static int awk_isgraph (int c) { return isgraph (c); }
|
|
|
|
static int awk_iscntrl (int c) { return iscntrl (c); }
|
|
|
|
static int awk_ispunct (int c) { return ispunct (c); }
|
|
|
|
static int awk_toupper (int c) { return toupper (c); }
|
|
|
|
static int awk_tolower (int c) { return tolower (c); }
|
2006-11-15 05:49:22 +00:00
|
|
|
#else
|
2006-12-16 14:48:31 +00:00
|
|
|
#define awk_isupper isupper
|
|
|
|
#define awk_islower islower
|
|
|
|
#define awk_isalpha isalpha
|
|
|
|
#define awk_isdigit isdigit
|
|
|
|
#define awk_isxdigit isxdigit
|
|
|
|
#define awk_isalnum isalnum
|
|
|
|
#define awk_isspace isspace
|
|
|
|
#define awk_isprint isprint
|
|
|
|
#define awk_isgraph isgraph
|
|
|
|
#define awk_iscntrl iscntrl
|
|
|
|
#define awk_ispunct ispunct
|
|
|
|
#define awk_toupper tolower
|
|
|
|
#define awk_tolower tolower
|
2006-11-15 05:49:22 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2006-12-16 14:48:31 +00:00
|
|
|
#define awk_isupper iswupper
|
|
|
|
#define awk_islower iswlower
|
|
|
|
#define awk_isalpha iswalpha
|
|
|
|
#define awk_isdigit iswdigit
|
|
|
|
#define awk_isxdigit iswxdigit
|
|
|
|
#define awk_isalnum iswalnum
|
|
|
|
#define awk_isspace iswspace
|
|
|
|
#define awk_isprint iswprint
|
|
|
|
#define awk_isgraph iswgraph
|
|
|
|
#define awk_iscntrl iswcntrl
|
|
|
|
#define awk_ispunct iswpunct
|
|
|
|
|
|
|
|
#define awk_toupper towlower
|
|
|
|
#define awk_tolower towlower
|
2006-11-15 05:49:22 +00:00
|
|
|
#endif
|
|
|
|
|
2007-02-03 10:52:36 +00:00
|
|
|
static void print_usage (const ase_char_t* argv0)
|
|
|
|
{
|
|
|
|
awk_printf (ASE_T("Usage: %s [-m] [-d] [-a argument]* -f source-file [data-file]*\n"), argv0);
|
|
|
|
}
|
|
|
|
|
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;
|
2006-06-29 14:38:01 +00:00
|
|
|
struct 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-02-01 08:38:24 +00:00
|
|
|
prmfns_data_t prmfns_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
|
|
|
|
2006-12-26 10:25:19 +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-15 14:58:37 +00:00
|
|
|
/*ASE_AWK_COPROC |*/
|
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-03 10:52:36 +00:00
|
|
|
if (ase_awk_strcmp(argv[i], ASE_T("-m")) == 0)
|
|
|
|
{
|
|
|
|
mfn = ASE_T("main");
|
|
|
|
}
|
|
|
|
else if (ase_awk_strcmp(argv[i], ASE_T("-d")) == 0)
|
|
|
|
{
|
|
|
|
deparse = 1;
|
|
|
|
}
|
|
|
|
else if (ase_awk_strcmp(argv[i], ASE_T("-f")) == 0)
|
|
|
|
{
|
|
|
|
/* specify source file */
|
|
|
|
mode = 1;
|
|
|
|
}
|
|
|
|
else if (ase_awk_strcmp(argv[i], ASE_T("-a")) == 0)
|
|
|
|
{
|
|
|
|
/* 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];
|
|
|
|
runarg[runarg_count].len = ase_awk_strlen(argv[i]);
|
|
|
|
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));
|
|
|
|
|
|
|
|
prmfns.malloc = awk_malloc;
|
|
|
|
prmfns.realloc = awk_realloc;
|
|
|
|
prmfns.free = awk_free;
|
2007-02-01 09:19:15 +00:00
|
|
|
prmfns.memcpy = awk_memcpy;
|
|
|
|
prmfns.memset = awk_memset;
|
2007-02-01 08:38:24 +00:00
|
|
|
|
|
|
|
prmfns.is_upper = (ase_awk_isctype_t)awk_isupper;
|
|
|
|
prmfns.is_lower = (ase_awk_isctype_t)awk_islower;
|
|
|
|
prmfns.is_alpha = (ase_awk_isctype_t)awk_isalpha;
|
|
|
|
prmfns.is_digit = (ase_awk_isctype_t)awk_isdigit;
|
|
|
|
prmfns.is_xdigit = (ase_awk_isctype_t)awk_isxdigit;
|
|
|
|
prmfns.is_alnum = (ase_awk_isctype_t)awk_isalnum;
|
|
|
|
prmfns.is_space = (ase_awk_isctype_t)awk_isspace;
|
|
|
|
prmfns.is_print = (ase_awk_isctype_t)awk_isprint;
|
|
|
|
prmfns.is_graph = (ase_awk_isctype_t)awk_isgraph;
|
|
|
|
prmfns.is_cntrl = (ase_awk_isctype_t)awk_iscntrl;
|
|
|
|
prmfns.is_punct = (ase_awk_isctype_t)awk_ispunct;
|
|
|
|
prmfns.to_upper = (ase_awk_toctype_t)awk_toupper;
|
|
|
|
prmfns.to_lower = (ase_awk_toctype_t)awk_tolower;
|
|
|
|
|
|
|
|
prmfns.pow = awk_pow;
|
|
|
|
prmfns.sprintf = awk_sprintf;
|
|
|
|
prmfns.aprintf = awk_aprintf;
|
|
|
|
prmfns.dprintf = awk_dprintf;
|
|
|
|
prmfns.abort = awk_abort;
|
|
|
|
prmfns.lock = NULL;
|
|
|
|
prmfns.unlock = NULL;
|
2006-09-01 06:23:58 +00:00
|
|
|
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
2007-02-01 08:38:24 +00:00
|
|
|
prmfns_data.heap = HeapCreate (0, 1000000, 1000000);
|
|
|
|
if (prmfns_data.heap == NULL)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
2006-12-17 12:50:59 +00:00
|
|
|
awk_printf (ASE_T("Error: cannot create an awk heap\n"));
|
2006-08-31 04:21:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-02-01 08:38:24 +00:00
|
|
|
prmfns.custom_data = &prmfns_data;
|
2006-08-31 04:21:04 +00:00
|
|
|
#endif
|
|
|
|
|
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-02-01 08:38:24 +00:00
|
|
|
HeapDestroy (prmfns_data.heap);
|
2006-08-31 04:21:04 +00:00
|
|
|
#endif
|
2006-12-17 12:50:59 +00:00
|
|
|
awk_printf (
|
2006-12-24 17:21:24 +00:00
|
|
|
ASE_T("ERROR: cannot open awk [%d] %s\n"),
|
2006-12-15 14:58:37 +00:00
|
|
|
errnum, ase_awk_geterrstr(errnum));
|
2006-08-31 04:21:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-01-23 14:23:18 +00:00
|
|
|
ase_awk_setoption (awk, opt);
|
2006-08-04 17:02:02 +00:00
|
|
|
|
2006-08-04 17:54:52 +00:00
|
|
|
srcios.in = process_source;
|
2007-02-03 10:52:36 +00:00
|
|
|
srcios.out = deparse? dump_source: 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
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
int errnum = ase_awk_geterrnum(awk);
|
2006-12-17 12:50:59 +00:00
|
|
|
awk_printf (
|
2006-10-24 04:48:52 +00:00
|
|
|
ASE_T("ERROR: cannot parse program - line %u [%d] %s\n"),
|
2006-12-19 14:26:27 +00:00
|
|
|
(unsigned int)ase_awk_geterrlin(awk),
|
2006-12-24 17:21:24 +00:00
|
|
|
errnum, 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
|
|
|
|
SetConsoleCtrlHandler (__stop_run, TRUE);
|
|
|
|
#else
|
2006-08-04 16:31:22 +00:00
|
|
|
signal (SIGINT, __stop_run);
|
2006-08-28 14:30:08 +00:00
|
|
|
#endif
|
2006-08-04 16:31:22 +00:00
|
|
|
|
2006-08-04 17:36:40 +00:00
|
|
|
runios.pipe = process_extio_pipe;
|
2006-10-24 04:48:52 +00:00
|
|
|
runios.coproc = ASE_NULL;
|
2006-08-04 17:36:40 +00:00
|
|
|
runios.file = process_extio_file;
|
|
|
|
runios.console = process_extio_console;
|
|
|
|
|
2006-12-16 14:48:31 +00:00
|
|
|
runcbs.on_start = on_run_start;
|
|
|
|
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
|
|
|
{
|
2006-10-24 04:48:52 +00:00
|
|
|
int errnum = ase_awk_geterrnum(awk);
|
2006-12-17 12:50:59 +00:00
|
|
|
awk_printf (
|
2006-10-24 04:48:52 +00:00
|
|
|
ASE_T("error: cannot run program - [%d] %s\n"),
|
|
|
|
errnum, ase_awk_geterrstr(errnum));
|
|
|
|
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-02-01 08:38:24 +00:00
|
|
|
HeapDestroy (prmfns_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;
|
2006-05-12 11:10:26 +00:00
|
|
|
#if defined(__linux) && defined(_DEBUG)
|
2006-05-12 09:39:20 +00:00
|
|
|
mtrace ();
|
|
|
|
#endif
|
2006-09-01 04:03:28 +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);
|
|
|
|
#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
|
|
|
}
|
2006-05-12 09:39:20 +00:00
|
|
|
|