2006-03-31 16:35:37 +00:00
|
|
|
/*
|
2006-10-22 12:39:30 +00:00
|
|
|
* $Id: awk.c,v 1.101 2006-10-22 12:39:30 bacon Exp $
|
2006-03-31 16:35:37 +00:00
|
|
|
*/
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
#include <sse/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-06-30 06:09:38 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_WCHAR
|
2006-06-30 06:09:38 +00:00
|
|
|
#include <wchar.h>
|
2006-09-01 07:18:40 +00:00
|
|
|
#include <wctype.h>
|
2006-04-30 18:05:07 +00:00
|
|
|
#endif
|
2006-01-18 16:12:59 +00:00
|
|
|
|
2006-05-06 16:05:12 +00:00
|
|
|
#ifndef __STAND_ALONE
|
2006-06-30 06:09:38 +00:00
|
|
|
#include <xp/bas/stdio.h>
|
2006-10-09 14:37:42 +00:00
|
|
|
#include <xp/bas/stdlib.h>
|
2006-06-30 06:09:38 +00:00
|
|
|
#include <xp/bas/string.h>
|
2006-07-28 10:34:22 +00:00
|
|
|
#include <xp/bas/memory.h>
|
2006-06-30 06:09:38 +00:00
|
|
|
#include <xp/bas/sysapi.h>
|
2006-07-27 16:50:29 +00:00
|
|
|
#include <xp/bas/assert.h>
|
2006-08-26 15:28:08 +00:00
|
|
|
#include <xp/bas/locale.h>
|
2006-06-30 06:09:38 +00:00
|
|
|
#else
|
2006-10-12 04:17:58 +00:00
|
|
|
#include <xp/bas/stdio.h>
|
2006-06-30 06:09:38 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#ifndef PATH_MAX
|
2006-10-22 12:39:30 +00:00
|
|
|
#define SSE_PATH_MAX 4096
|
2006-06-30 06:09:38 +00:00
|
|
|
#else
|
2006-10-22 12:39:30 +00:00
|
|
|
#define SSE_PATH_MAX PATH_MAX
|
2006-06-30 06:09:38 +00:00
|
|
|
#endif
|
2006-10-09 14:37:42 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define xp_sprintf _sntprintf
|
|
|
|
#endif
|
2006-04-16 04:31:38 +00:00
|
|
|
#endif
|
2006-01-18 16:12:59 +00:00
|
|
|
|
2006-06-19 04:38:51 +00:00
|
|
|
#ifdef _WIN32
|
2006-07-02 12:16:24 +00:00
|
|
|
#include <windows.h>
|
2006-06-19 04:38:51 +00:00
|
|
|
#include <tchar.h>
|
2006-06-30 04:18:47 +00:00
|
|
|
#pragma warning (disable: 4996)
|
2006-06-19 04:38:51 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-16 04:31:38 +00:00
|
|
|
#ifdef __STAND_ALONE
|
2006-07-27 16:50:29 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#define xp_assert assert
|
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
|
|
|
|
|
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-06-29 14:38:01 +00:00
|
|
|
struct src_io
|
|
|
|
{
|
|
|
|
const xp_char_t* input_file;
|
|
|
|
FILE* input_handle;
|
|
|
|
};
|
|
|
|
|
2006-06-30 04:18:47 +00:00
|
|
|
static FILE* fopen_t (const xp_char_t* path, const xp_char_t* mode)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return _tfopen (path, mode);
|
|
|
|
#else
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-06-30 04:18:47 +00:00
|
|
|
const xp_mchar_t* path_mb;
|
|
|
|
const xp_mchar_t* mode_mb;
|
|
|
|
#else
|
|
|
|
xp_mchar_t path_mb[XP_PATH_MAX + 1];
|
|
|
|
xp_mchar_t mode_mb[32];
|
|
|
|
#endif
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-06-30 04:18:47 +00:00
|
|
|
path_mb = path;
|
|
|
|
mode_mb = mode;
|
|
|
|
#else
|
|
|
|
if (xp_wcstomcs_strict (
|
2006-10-22 12:39:30 +00:00
|
|
|
path, path_mb, xp_countof(path_mb)) == -1) return SSE_NULL;
|
2006-06-30 04:18:47 +00:00
|
|
|
if (xp_wcstomcs_strict (
|
2006-10-22 12:39:30 +00:00
|
|
|
mode, mode_mb, xp_countof(mode_mb)) == -1) return SSE_NULL;
|
2006-06-30 04:18:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return fopen (path_mb, mode_mb);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-10-12 04:17:58 +00:00
|
|
|
static int __dprintf (const xp_char_t* fmt, ...)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
va_list ap;
|
|
|
|
#ifdef _WIN32
|
|
|
|
xp_char_t buf[1024];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
#ifdef _WIN32
|
|
|
|
n = xp_vsprintf (buf, xp_countof(buf), fmt, ap);
|
2006-10-16 08:48:19 +00:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER>=1400)
|
2006-10-22 12:39:30 +00:00
|
|
|
MessageBox (NULL, buf, SSE_T("\uD655\uC778\uC2E4\uD328 Assertion Failure"), MB_OK | MB_ICONERROR);
|
2006-10-16 08:48:19 +00:00
|
|
|
#else
|
2006-10-22 12:39:30 +00:00
|
|
|
MessageBox (NULL, buf, SSE_T("Assertion Failure"), MB_OK | MB_ICONERROR);
|
2006-10-16 08:48:19 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-12 04:17:58 +00:00
|
|
|
#else
|
|
|
|
n = xp_vprintf (fmt, ap);
|
|
|
|
#endif
|
|
|
|
va_end (ap);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2006-06-30 04:25:53 +00:00
|
|
|
static FILE* popen_t (const xp_char_t* cmd, const xp_char_t* mode)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return _tpopen (cmd, mode);
|
|
|
|
#else
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-06-30 04:25:53 +00:00
|
|
|
const xp_mchar_t* cmd_mb;
|
|
|
|
const xp_mchar_t* mode_mb;
|
|
|
|
#else
|
|
|
|
xp_mchar_t cmd_mb[2048];
|
|
|
|
xp_mchar_t mode_mb[32];
|
|
|
|
#endif
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-06-30 04:25:53 +00:00
|
|
|
cmd_mb = cmd;
|
|
|
|
mode_mb = mode;
|
|
|
|
#else
|
|
|
|
if (xp_wcstomcs_strict (
|
2006-10-22 12:39:30 +00:00
|
|
|
cmd, cmd_mb, xp_countof(cmd_mb)) == -1) return SSE_NULL;
|
2006-06-30 04:25:53 +00:00
|
|
|
if (xp_wcstomcs_strict (
|
2006-10-22 12:39:30 +00:00
|
|
|
mode, mode_mb, xp_countof(mode_mb)) == -1) return SSE_NULL;
|
2006-06-30 04:25:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return popen (cmd_mb, mode_mb);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-06-30 04:18:47 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define fgets_t _fgetts
|
|
|
|
#define fputs_t _fputts
|
|
|
|
#else
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-06-30 04:18:47 +00:00
|
|
|
#define fgets_t fgets
|
|
|
|
#define fputs_t fputs
|
|
|
|
#else
|
|
|
|
#define fgets_t fgetws
|
|
|
|
#define fputs_t fputws
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-04-16 04:31:38 +00:00
|
|
|
static xp_ssize_t process_source (
|
2006-07-28 10:34:22 +00:00
|
|
|
int cmd, void* arg, xp_char_t* data, xp_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;
|
2006-03-29 16:39:04 +00:00
|
|
|
xp_char_t c;
|
2006-01-18 16:12:59 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (cmd == SSE_AWK_IO_OPEN)
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (src_io->input_file == SSE_NULL) return 0;
|
|
|
|
src_io->input_handle = fopen_t (src_io->input_file, SSE_T("r"));
|
2006-08-22 15:11:13 +00:00
|
|
|
if (src_io->input_handle == NULL) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_AWK_IO_CLOSE)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (src_io->input_file == SSE_NULL) return 0;
|
2006-08-22 15:11:13 +00:00
|
|
|
fclose ((FILE*)src_io->input_handle);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_AWK_IO_READ)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
|
|
|
if (size <= 0) return -1;
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-08-22 15:11:13 +00:00
|
|
|
c = fgetc ((FILE*)src_io->input_handle);
|
|
|
|
#else
|
|
|
|
c = fgetwc ((FILE*)src_io->input_handle);
|
|
|
|
#endif
|
2006-10-22 12:39:30 +00:00
|
|
|
if (c == SSE_CHAR_EOF) return 0;
|
2006-08-22 15:11:13 +00:00
|
|
|
*data = c;
|
|
|
|
return 1;
|
2006-08-06 15:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static xp_ssize_t dump_source (
|
|
|
|
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
|
|
|
{
|
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-22 12:39:30 +00:00
|
|
|
if (cmd == SSE_AWK_IO_OPEN) return 1;
|
|
|
|
else if (cmd == SSE_AWK_IO_CLOSE) return 0;
|
|
|
|
else if (cmd == SSE_AWK_IO_WRITE)
|
2006-08-06 15:03:42 +00:00
|
|
|
{
|
2006-08-22 15:11:13 +00:00
|
|
|
xp_size_t i;
|
|
|
|
for (i = 0; i < size; i++)
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-08-22 15:11:13 +00:00
|
|
|
fputc (data[i], stdout);
|
|
|
|
#else
|
|
|
|
fputwc (data[i], stdout);
|
|
|
|
#endif
|
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-06-19 04:38:51 +00:00
|
|
|
static xp_ssize_t process_extio_pipe (
|
2006-07-28 10:34:22 +00:00
|
|
|
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_extio_t* epa = (sse_awk_extio_t*)arg;
|
2006-06-19 04:38:51 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_OPEN:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
|
|
|
FILE* handle;
|
2006-06-22 04:25:44 +00:00
|
|
|
const xp_char_t* mode;
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (epa->mode == SSE_AWK_IO_PIPE_READ)
|
|
|
|
mode = SSE_T("r");
|
|
|
|
else if (epa->mode == SSE_AWK_IO_PIPE_WRITE)
|
|
|
|
mode = SSE_T("w");
|
2006-06-22 04:25:44 +00:00
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("opending %s of type %d (pipe)\n"), epa->name, epa->type);
|
2006-06-30 04:25:53 +00:00
|
|
|
handle = popen_t (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-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_CLOSE:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("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-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_READ:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (fgets_t (data, size, (FILE*)epa->handle) == SSE_NULL)
|
2006-06-19 04:38:51 +00:00
|
|
|
return 0;
|
2006-10-22 12:39:30 +00:00
|
|
|
return sse_awk_strlen(data);
|
2006-06-19 04:38:51 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_WRITE:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-08-23 15:42:16 +00:00
|
|
|
/* TODO: size... */
|
|
|
|
fputs_t (data, (FILE*)epa->handle);
|
2006-06-22 04:25:44 +00:00
|
|
|
return size;
|
2006-06-19 04:38:51 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_FLUSH:
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (epa->mode == SSE_AWK_IO_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-22 12:39:30 +00:00
|
|
|
case SSE_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-06-19 09:10:57 +00:00
|
|
|
static xp_ssize_t process_extio_file (
|
2006-07-28 10:34:22 +00:00
|
|
|
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_extio_t* epa = (sse_awk_extio_t*)arg;
|
2006-06-19 09:10:57 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_OPEN:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
|
|
|
FILE* handle;
|
2006-06-22 04:25:44 +00:00
|
|
|
const xp_char_t* mode;
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (epa->mode == SSE_AWK_IO_FILE_READ)
|
|
|
|
mode = SSE_T("r");
|
|
|
|
else if (epa->mode == SSE_AWK_IO_FILE_WRITE)
|
|
|
|
mode = SSE_T("w");
|
|
|
|
else if (epa->mode == SSE_AWK_IO_FILE_APPEND)
|
|
|
|
mode = SSE_T("a");
|
2006-06-22 04:25:44 +00:00
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("opending %s of type %d (file)\n"), epa->name, epa->type);
|
2006-06-30 04:18:47 +00:00
|
|
|
handle = fopen_t (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-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_CLOSE:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("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-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_READ:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (fgets_t (data, size, (FILE*)epa->handle) == SSE_NULL)
|
2006-06-19 09:10:57 +00:00
|
|
|
return 0;
|
2006-10-22 12:39:30 +00:00
|
|
|
return sse_awk_strlen(data);
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
case SSE_AWK_IO_WRITE:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-06-27 10:53:04 +00:00
|
|
|
/* TODO: how to return error or 0 */
|
2006-06-30 04:18:47 +00:00
|
|
|
fputs_t (data, /*size,*/ (FILE*)epa->handle);
|
2006-08-22 15:11:13 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
case SSE_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-22 12:39:30 +00:00
|
|
|
case SSE_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-22 12:39:30 +00:00
|
|
|
static int open_extio_console (sse_awk_extio_t* epa);
|
|
|
|
static int close_extio_console (sse_awk_extio_t* epa);
|
|
|
|
static int next_extio_console (sse_awk_extio_t* epa);
|
2006-07-28 10:34:22 +00:00
|
|
|
|
2006-10-16 14:39:21 +00:00
|
|
|
static xp_size_t infile_no = 0;
|
|
|
|
static const xp_char_t* infiles[10000] =
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
SSE_T(""),
|
|
|
|
SSE_NULL
|
2006-07-28 10:34:22 +00:00
|
|
|
};
|
|
|
|
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-06-25 15:26:57 +00:00
|
|
|
static xp_ssize_t process_extio_console (
|
2006-07-28 10:34:22 +00:00
|
|
|
int cmd, void* arg, xp_char_t* data, xp_size_t size)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_extio_t* epa = (sse_awk_extio_t*)arg;
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (cmd == SSE_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-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_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-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_AWK_IO_READ)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
while (fgets_t (data, size, epa->handle) == SSE_NULL)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
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-22 12:39:30 +00:00
|
|
|
if (infiles[infile_no] == SSE_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-22 12:39:30 +00:00
|
|
|
if (epa->handle != SSE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr) fclose (epa->handle);
|
2006-10-22 12:39:30 +00:00
|
|
|
epa->handle = SSE_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-22 12:39:30 +00:00
|
|
|
if (infiles[infile_no][0] == SSE_T('\0'))
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (epa->handle != SSE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr) fclose (epa->handle);
|
|
|
|
epa->handle = stdin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
FILE* fp = fopen_t (infiles[infile_no], SSE_T("r"));
|
|
|
|
if (fp == SSE_NULL)
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("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-22 12:39:30 +00:00
|
|
|
if (epa->handle != SSE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr) fclose (epa->handle);
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("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-22 12:39:30 +00:00
|
|
|
return sse_awk_strlen(data);
|
2006-07-28 10:34:22 +00:00
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_AWK_IO_WRITE)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
|
|
|
/* TODO: how to return error or 0 */
|
|
|
|
fputs_t (data, /*size,*/ (FILE*)epa->handle);
|
|
|
|
/*MessageBox (NULL, data, data, MB_OK);*/
|
|
|
|
return size;
|
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_AWK_IO_FLUSH)
|
2006-08-22 15:11:13 +00:00
|
|
|
{
|
|
|
|
if (fflush ((FILE*)epa->handle) == EOF) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else if (cmd == SSE_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-22 12:39:30 +00:00
|
|
|
static int open_extio_console (sse_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-10-22 12:39:30 +00:00
|
|
|
xp_assert (epa->name[0] == SSE_T('\0'));
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("opening console[%s] of type %x\n"), epa->name, epa->type);
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (epa->mode == SSE_AWK_IO_CONSOLE_READ)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (infiles[infile_no] == SSE_NULL)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
/* no more input file */
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("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-22 12:39:30 +00:00
|
|
|
if (infiles[infile_no][0] == SSE_T('\0'))
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_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-10-22 12:39:30 +00:00
|
|
|
FILE* fp = fopen_t (infiles[infile_no], SSE_T("r"));
|
|
|
|
if (fp == SSE_NULL)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("failed to open console of type %x - fopen failure\n"), epa->type);
|
2006-07-28 10:34:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T(" console(r) - %s\n"), infiles[infile_no]);
|
|
|
|
if (sse_awk_setconsolename (
|
2006-10-16 14:39:21 +00:00
|
|
|
epa->run, infiles[infile_no],
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_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-10-22 12:39:30 +00:00
|
|
|
else if (epa->mode == SSE_AWK_IO_CONSOLE_WRITE)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T(" console(w) - <standard output>\n"));
|
2006-10-16 14:39:21 +00:00
|
|
|
/* TODO: does output console has a name??? */
|
2006-10-22 12:39:30 +00:00
|
|
|
/*sse_awk_setconsolename (SSE_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-22 12:39:30 +00:00
|
|
|
static int close_extio_console (sse_awk_extio_t* epa)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("closing console of type %x\n"), epa->type);
|
2006-07-28 10:34:22 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (epa->handle != SSE_NULL &&
|
2006-07-28 10:34:22 +00:00
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
fclose (epa->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: CloseConsole in GUI APPLICATION */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
static int next_extio_console (sse_awk_extio_t* epa)
|
2006-07-28 10:34:22 +00:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
FILE* fp = epa->handle;
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_TEXT("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-22 12:39:30 +00:00
|
|
|
if (fp != SSE_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-22 12:39:30 +00:00
|
|
|
sse_awk_t* app_awk = NULL;
|
2006-08-04 16:31:22 +00:00
|
|
|
void* app_run = NULL;
|
|
|
|
|
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-22 12:39:30 +00:00
|
|
|
sse_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-22 12:39:30 +00:00
|
|
|
sse_awk_stop (app_awk, app_run);
|
|
|
|
//sse_awk_stoprun (awk, handle);
|
|
|
|
/*sse_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-10-22 12:39:30 +00:00
|
|
|
static void __on_run_start (sse_awk_t* awk, void* handle, void* arg)
|
2006-08-04 16:31:22 +00:00
|
|
|
{
|
|
|
|
app_awk = awk;
|
|
|
|
app_run = handle;
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("AWK PRORAM ABOUT TO START...\n"));
|
2006-08-04 16:31:22 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
static void __on_run_end (sse_awk_t* awk, void* handle, int errnum, void* arg)
|
2006-08-04 16:31:22 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
if (errnum != SSE_AWK_ENOERR)
|
2006-08-10 16:06:52 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("AWK PRORAM ABOUT TO END WITH AN ERROR - %d - %s\n"), errnum, sse_awk_geterrstr (errnum));
|
2006-08-10 16:06:52 +00:00
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else xp_printf (SSE_T("AWK PRORAM 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
|
2006-08-31 04:21:04 +00:00
|
|
|
typedef struct syscas_data_t syscas_data_t;
|
|
|
|
struct syscas_data_t
|
|
|
|
{
|
|
|
|
HANDLE heap;
|
|
|
|
};
|
2006-08-31 14:03:38 +00:00
|
|
|
#endif
|
2006-08-31 04:21:04 +00:00
|
|
|
|
|
|
|
static void* __awk_malloc (xp_size_t n, void* custom_data)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return HeapAlloc (((syscas_data_t*)custom_data)->heap, 0, n);
|
|
|
|
#else
|
|
|
|
return malloc (n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void* __awk_realloc (void* ptr, xp_size_t n, void* custom_data)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2006-09-01 03:44:51 +00:00
|
|
|
/* HeapReAlloc behaves differently from realloc */
|
|
|
|
if (ptr == NULL)
|
|
|
|
return HeapAlloc (((syscas_data_t*)custom_data)->heap, 0, n);
|
|
|
|
else
|
|
|
|
return HeapReAlloc (((syscas_data_t*)custom_data)->heap, 0, ptr, n);
|
2006-08-31 04:21:04 +00:00
|
|
|
#else
|
|
|
|
return realloc (ptr, n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __awk_free (void* ptr, void* custom_data)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
HeapFree (((syscas_data_t*)custom_data)->heap, 0, ptr);
|
|
|
|
#else
|
|
|
|
free (ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-04-18 16:27:29 +00:00
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32)
|
2006-05-12 09:39:20 +00:00
|
|
|
static int __main (int argc, char* argv[])
|
2006-04-14 11:13:06 +00:00
|
|
|
#else
|
2006-05-12 09:39:20 +00:00
|
|
|
static int __main (int argc, xp_char_t* argv[])
|
2006-04-14 11:13:06 +00:00
|
|
|
#endif
|
2005-11-14 15:23:54 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_t* awk;
|
|
|
|
sse_awk_srcios_t srcios;
|
|
|
|
sse_awk_runcbs_t runcbs;
|
|
|
|
sse_awk_runios_t runios;
|
|
|
|
sse_awk_runarg_t runarg[10];
|
|
|
|
sse_awk_syscas_t syscas;
|
2006-06-29 14:38:01 +00:00
|
|
|
struct src_io src_io = { NULL, NULL };
|
2006-08-25 03:31:09 +00:00
|
|
|
int opt, i, file_count = 0;
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
syscas_data_t syscas_data;
|
|
|
|
#endif
|
2005-11-14 15:23:54 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
opt = SSE_AWK_EXPLICIT | SSE_AWK_UNIQUE | SSE_AWK_HASHSIGN |
|
|
|
|
/*SSE_AWK_DBLSLASHES |*/
|
|
|
|
SSE_AWK_SHADING | SSE_AWK_IMPLICIT | SSE_AWK_SHIFT |
|
|
|
|
SSE_AWK_EXTIO | SSE_AWK_BLOCKLESS | SSE_AWK_STRINDEXONE |
|
|
|
|
SSE_AWK_STRIPSPACES | SSE_AWK_NEWLINE;
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-08-26 16:30:53 +00:00
|
|
|
if (argc <= 1)
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Usage: %s [-m] source_file [data_file ...]\n"), 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
|
|
|
{
|
2006-04-18 16:27:29 +00:00
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32)
|
2006-08-25 03:31:09 +00:00
|
|
|
if (strcmp(argv[i], "-m") == 0)
|
2006-04-14 11:13:06 +00:00
|
|
|
#else
|
2006-10-22 12:39:30 +00:00
|
|
|
if (sse_awk_strcmp(argv[i], SSE_T("-m")) == 0)
|
2006-06-29 14:38:01 +00:00
|
|
|
#endif
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
opt |= SSE_AWK_RUNMAIN;
|
2006-06-29 14:38:01 +00:00
|
|
|
}
|
2006-08-25 03:31:09 +00:00
|
|
|
else if (file_count == 0)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2006-08-25 03:31:09 +00:00
|
|
|
src_io.input_file = argv[i];
|
|
|
|
file_count++;
|
|
|
|
}
|
2006-10-16 14:39:21 +00:00
|
|
|
else if (file_count >= 1 && file_count < xp_countof(infiles)-1)
|
2006-08-25 03:31:09 +00:00
|
|
|
{
|
2006-10-16 14:39:21 +00:00
|
|
|
infiles[file_count-1] = argv[i];
|
2006-10-22 12:39:30 +00:00
|
|
|
infiles[file_count] = SSE_NULL;
|
2006-08-25 03:31:09 +00:00
|
|
|
file_count++;
|
2006-04-10 09:22:05 +00:00
|
|
|
}
|
2006-06-29 14:38:01 +00:00
|
|
|
else
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Usage: %s [-m] [-f source_file] [data_file ...]\n"), argv[0]);
|
2006-06-29 14:38:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-31 04:21:04 +00:00
|
|
|
memset (&syscas, 0, sizeof(syscas));
|
|
|
|
syscas.malloc = __awk_malloc;
|
2006-09-01 03:44:51 +00:00
|
|
|
syscas.realloc = __awk_realloc;
|
2006-08-31 04:21:04 +00:00
|
|
|
syscas.free = __awk_free;
|
2006-09-01 06:23:58 +00:00
|
|
|
|
2006-08-31 04:21:04 +00:00
|
|
|
syscas.lock = NULL;
|
|
|
|
syscas.unlock = NULL;
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
#ifdef SSE_CHAR_IS_MCHAR
|
2006-09-01 06:23:58 +00:00
|
|
|
syscas.is_upper = isupper;
|
|
|
|
syscas.is_lower = islower;
|
|
|
|
syscas.is_alpha = isalpha;
|
|
|
|
syscas.is_digit = isdigit;
|
|
|
|
syscas.is_xdigit = isxdigit;
|
|
|
|
syscas.is_alnum = isalnum;
|
|
|
|
syscas.is_space = isspace;
|
|
|
|
syscas.is_print = isprint;
|
|
|
|
syscas.is_graph = isgraph;
|
|
|
|
syscas.is_cntrl = iscntrl;
|
|
|
|
syscas.is_punct = ispunct;
|
|
|
|
syscas.to_upper = toupper;
|
|
|
|
syscas.to_lower = tolower;
|
|
|
|
#else
|
|
|
|
syscas.is_upper = iswupper;
|
|
|
|
syscas.is_lower = iswlower;
|
|
|
|
syscas.is_alpha = iswalpha;
|
|
|
|
syscas.is_digit = iswdigit;
|
|
|
|
syscas.is_xdigit = iswxdigit;
|
|
|
|
syscas.is_alnum = iswalnum;
|
|
|
|
syscas.is_space = iswspace;
|
|
|
|
syscas.is_print = iswprint;
|
|
|
|
syscas.is_graph = iswgraph;
|
|
|
|
syscas.is_cntrl = iswcntrl;
|
|
|
|
syscas.is_punct = iswpunct;
|
|
|
|
syscas.to_upper = towupper;
|
|
|
|
syscas.to_lower = towlower;
|
|
|
|
#endif
|
2006-09-25 06:17:19 +00:00
|
|
|
syscas.memcpy = memcpy;
|
|
|
|
syscas.memset = memset;
|
2006-09-22 14:05:30 +00:00
|
|
|
syscas.sprintf = xp_sprintf;
|
2006-10-12 04:17:58 +00:00
|
|
|
syscas.dprintf = __dprintf;
|
|
|
|
syscas.abort = abort;
|
2006-09-01 06:23:58 +00:00
|
|
|
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
2006-09-01 03:44:51 +00:00
|
|
|
syscas_data.heap = HeapCreate (0, 1000000, 1000000);
|
2006-08-31 04:21:04 +00:00
|
|
|
if (syscas_data.heap == NULL)
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Error: cannot create an awk heap\n"));
|
2006-08-31 04:21:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
syscas.custom_data = &syscas_data;
|
|
|
|
#endif
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if ((awk = sse_awk_open(&syscas)) == SSE_NULL)
|
2006-08-31 04:21:04 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
HeapDestroy (syscas_data.heap);
|
|
|
|
#endif
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Error: cannot open awk\n"));
|
2006-08-31 04:21:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_setopt (awk, opt);
|
2006-08-04 17:02:02 +00:00
|
|
|
|
2006-08-04 17:54:52 +00:00
|
|
|
srcios.in = process_source;
|
2006-08-06 15:03:42 +00:00
|
|
|
srcios.out = dump_source;
|
2006-08-06 08:16:03 +00:00
|
|
|
srcios.custom_data = &src_io;
|
2006-08-04 17:54:52 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (sse_awk_parse (awk, &srcios) == -1)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
int errnum = sse_awk_geterrnum(awk);
|
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(SSE_CHAR_IS_WCHAR)
|
2006-04-14 16:26:00 +00:00
|
|
|
xp_printf (
|
2006-10-22 12:39:30 +00:00
|
|
|
SSE_T("ERROR: cannot parse program - line %u [%d] %ls\n"),
|
|
|
|
(unsigned int)sse_awk_getsrcline(awk),
|
|
|
|
errnum, sse_awk_geterrstr(errnum));
|
2006-04-14 16:26:00 +00:00
|
|
|
#else
|
2006-01-31 16:57:45 +00:00
|
|
|
xp_printf (
|
2006-10-22 12:39:30 +00:00
|
|
|
SSE_T("ERROR: cannot parse program - line %u [%d] %s\n"),
|
|
|
|
(unsigned int)sse_awk_getsrcline(awk),
|
|
|
|
errnum, sse_awk_geterrstr(errnum));
|
2006-04-14 16:26:00 +00:00
|
|
|
#endif
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_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-22 12:39:30 +00:00
|
|
|
runios.coproc = SSE_NULL;
|
2006-08-04 17:36:40 +00:00
|
|
|
runios.file = process_extio_file;
|
|
|
|
runios.console = process_extio_console;
|
|
|
|
|
2006-08-13 06:33:30 +00:00
|
|
|
runcbs.on_start = __on_run_start;
|
|
|
|
runcbs.on_end = __on_run_end;
|
2006-10-22 12:39:30 +00:00
|
|
|
runcbs.custom_data = SSE_NULL;
|
|
|
|
|
|
|
|
runarg[0].ptr = SSE_T("argument 0");
|
|
|
|
runarg[0].len = sse_awk_strlen(runarg[0].ptr);
|
|
|
|
runarg[1].ptr = SSE_T("argumetn 1");
|
|
|
|
runarg[1].len = sse_awk_strlen(runarg[1].ptr);
|
|
|
|
runarg[2].ptr = SSE_T("argumetn 2");
|
|
|
|
runarg[2].len = sse_awk_strlen(runarg[2].ptr);
|
|
|
|
runarg[3].ptr = SSE_NULL;
|
2006-10-16 09:11:53 +00:00
|
|
|
runarg[3].len = 0;
|
2006-10-10 14:09:23 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
if (sse_awk_run (awk, &runios, &runcbs, runarg) == -1)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
int errnum = sse_awk_geterrnum(awk);
|
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(SSE_CHAR_IS_WCHAR)
|
2006-04-14 16:26:00 +00:00
|
|
|
xp_printf (
|
2006-10-22 12:39:30 +00:00
|
|
|
SSE_T("error: cannot run program - [%d] %ls\n"),
|
|
|
|
errnum, sse_awk_geterrstr(errnum));
|
2006-04-14 16:26:00 +00:00
|
|
|
#else
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_printf (
|
2006-10-22 12:39:30 +00:00
|
|
|
SSE_T("error: cannot run program - [%d] %s\n"),
|
|
|
|
errnum, sse_awk_geterrstr(errnum));
|
2006-04-14 16:26:00 +00:00
|
|
|
#endif
|
2006-08-01 04:36:33 +00:00
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_close (awk);
|
2006-04-06 16:25:37 +00:00
|
|
|
return -1;
|
2006-03-05 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2006-10-22 12:39:30 +00:00
|
|
|
sse_awk_close (awk);
|
2006-08-31 04:21:04 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
HeapDestroy (syscas_data.heap);
|
|
|
|
#endif
|
2006-05-12 09:39:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-30 17:50:38 +00:00
|
|
|
|
2006-09-22 14:05:30 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/*
|
|
|
|
NTSYSAPI PTEB NTAPI NtCurrentTeb();
|
|
|
|
Function NtCurrentTeb returns address of TEB (Thread Environment Block) for calling thread.
|
|
|
|
NtCurrentTeb isn't typical NT CALL realised via INT 2E, becouse TEB is accessable at address fs:[0018h].
|
|
|
|
Microsoft declare NtCurrentTeb as __cdecl, but ntdll.dll export it as __stdcall (it don't have metter, becouse function don't have any parameters), so you cannot use ntdll.dll export. In this case the better way is write NtCurrentTeb manually, declaring it as __cdecl.
|
|
|
|
|
|
|
|
typedef UCHAR BOOLEAN;
|
|
|
|
|
|
|
|
typedef struct _TEB {
|
|
|
|
|
|
|
|
NT_TIB Tib;
|
|
|
|
PVOID EnvironmentPointer;
|
|
|
|
CLIENT_ID Cid;
|
|
|
|
PVOID ActiveRpcInfo;
|
|
|
|
PVOID ThreadLocalStoragePointer;
|
|
|
|
PPEB Peb;
|
|
|
|
ULONG LastErrorValue;
|
|
|
|
ULONG CountOfOwnedCriticalSections;
|
|
|
|
PVOID CsrClientThread;
|
|
|
|
PVOID Win32ThreadInfo;
|
|
|
|
ULONG Win32ClientInfo[0x1F];
|
|
|
|
PVOID WOW32Reserved;
|
|
|
|
ULONG CurrentLocale;
|
|
|
|
ULONG FpSoftwareStatusRegister;
|
|
|
|
PVOID SystemReserved1[0x36];
|
|
|
|
PVOID Spare1;
|
|
|
|
ULONG ExceptionCode;
|
|
|
|
ULONG SpareBytes1[0x28];
|
|
|
|
PVOID SystemReserved2[0xA];
|
|
|
|
ULONG GdiRgn;
|
|
|
|
ULONG GdiPen;
|
|
|
|
ULONG GdiBrush;
|
|
|
|
CLIENT_ID RealClientId;
|
|
|
|
PVOID GdiCachedProcessHandle;
|
|
|
|
ULONG GdiClientPID;
|
|
|
|
ULONG GdiClientTID;
|
|
|
|
PVOID GdiThreadLocaleInfo;
|
|
|
|
PVOID UserReserved[5];
|
|
|
|
PVOID GlDispatchTable[0x118];
|
|
|
|
ULONG GlReserved1[0x1A];
|
|
|
|
PVOID GlReserved2;
|
|
|
|
PVOID GlSectionInfo;
|
|
|
|
PVOID GlSection;
|
|
|
|
PVOID GlTable;
|
|
|
|
PVOID GlCurrentRC;
|
|
|
|
PVOID GlContext;
|
|
|
|
NTSTATUS LastStatusValue;
|
|
|
|
UNICODE_STRING StaticUnicodeString;
|
|
|
|
WCHAR StaticUnicodeBuffer[0x105];
|
|
|
|
PVOID DeallocationStack;
|
|
|
|
PVOID TlsSlots[0x40];
|
|
|
|
LIST_ENTRY TlsLinks;
|
|
|
|
PVOID Vdm;
|
|
|
|
PVOID ReservedForNtRpc;
|
|
|
|
PVOID DbgSsReserved[0x2];
|
|
|
|
ULONG HardErrorDisabled;
|
|
|
|
PVOID Instrumentation[0x10];
|
|
|
|
PVOID WinSockData;
|
|
|
|
ULONG GdiBatchCount;
|
|
|
|
ULONG Spare2;
|
|
|
|
ULONG Spare3;
|
|
|
|
ULONG Spare4;
|
|
|
|
PVOID ReservedForOle;
|
|
|
|
ULONG WaitingOnLoaderLock;
|
|
|
|
PVOID StackCommit;
|
|
|
|
PVOID StackCommitMax;
|
|
|
|
PVOID StackReserved;
|
|
|
|
|
|
|
|
} TEB, *PTEB;
|
|
|
|
|
|
|
|
typedef struct _PEB {
|
|
|
|
BOOLEAN InheritedAddressSpace;
|
|
|
|
BOOLEAN ReadImageFileExecOptions;
|
|
|
|
BOOLEAN BeingDebugged;
|
|
|
|
BOOLEAN Spare;
|
|
|
|
HANDLE Mutant;
|
|
|
|
PVOID ImageBaseAddress;
|
|
|
|
PPEB_LDR_DATA LoaderData;
|
|
|
|
PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
|
|
|
|
PVOID SubSystemData;
|
|
|
|
PVOID ProcessHeap;
|
|
|
|
PVOID FastPebLock;
|
|
|
|
PPEBLOCKROUTINE FastPebLockRoutine;
|
|
|
|
PPEBLOCKROUTINE FastPebUnlockRoutine;
|
|
|
|
ULONG EnvironmentUpdateCount;
|
|
|
|
PPVOID KernelCallbackTable;
|
|
|
|
PVOID EventLogSection;
|
|
|
|
PVOID EventLog;
|
|
|
|
PPEB_FREE_BLOCK FreeList;
|
|
|
|
ULONG TlsExpansionCounter;
|
|
|
|
PVOID TlsBitmap;
|
|
|
|
ULONG TlsBitmapBits[0x2];
|
|
|
|
PVOID ReadOnlySharedMemoryBase;
|
|
|
|
PVOID ReadOnlySharedMemoryHeap;
|
|
|
|
PPVOID ReadOnlyStaticServerData;
|
|
|
|
PVOID AnsiCodePageData;
|
|
|
|
PVOID OemCodePageData;
|
|
|
|
PVOID UnicodeCaseTableData;
|
|
|
|
ULONG NumberOfProcessors;
|
|
|
|
ULONG NtGlobalFlag;
|
|
|
|
BYTE Spare2[0x4];
|
|
|
|
LARGE_INTEGER CriticalSectionTimeout;
|
|
|
|
ULONG HeapSegmentReserve;
|
|
|
|
ULONG HeapSegmentCommit;
|
|
|
|
ULONG HeapDeCommitTotalFreeThreshold;
|
|
|
|
ULONG HeapDeCommitFreeBlockThreshold;
|
|
|
|
ULONG NumberOfHeaps;
|
|
|
|
ULONG MaximumNumberOfHeaps;
|
|
|
|
PPVOID *ProcessHeaps;
|
|
|
|
PVOID GdiSharedHandleTable;
|
|
|
|
PVOID ProcessStarterHelper;
|
|
|
|
PVOID GdiDCAttributeList;
|
|
|
|
PVOID LoaderLock;
|
|
|
|
ULONG OSMajorVersion;
|
|
|
|
ULONG OSMinorVersion;
|
|
|
|
ULONG OSBuildNumber;
|
|
|
|
ULONG OSPlatformId;
|
|
|
|
ULONG ImageSubSystem;
|
|
|
|
ULONG ImageSubSystemMajorVersion;
|
|
|
|
ULONG ImageSubSystemMinorVersion;
|
|
|
|
ULONG GdiHandleBuffer[0x22];
|
|
|
|
ULONG PostProcessInitRoutine;
|
|
|
|
ULONG TlsExpansionBitmap;
|
|
|
|
BYTE TlsExpansionBitmapBits[0x80];
|
|
|
|
ULONG SessionId;
|
|
|
|
|
|
|
|
} PEB, *PPEB;
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
void* get_current_teb ()
|
|
|
|
{
|
|
|
|
_asm
|
|
|
|
{
|
|
|
|
mov eax, fs:[0x18]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* get_current_peb ()
|
|
|
|
{
|
|
|
|
void* teb = get_current_teb ();
|
|
|
|
return *(void**)((char*)teb + 0x30);
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_debugger_present ()
|
|
|
|
{
|
|
|
|
void *peb = get_current_peb ();
|
|
|
|
return *((char*)peb+0x02);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int is_debugger_present2 ()
|
|
|
|
{
|
|
|
|
_asm
|
|
|
|
{
|
|
|
|
mov eax, fs:[0x18]
|
|
|
|
mov ebx, [eax+0x30]
|
|
|
|
xor eax, eax
|
|
|
|
mov al, byte ptr[ebx+0x02]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-12 09:39:20 +00:00
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32)
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
#else
|
|
|
|
int xp_main (int argc, xp_char_t* argv[])
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2006-09-02 15:17:44 +00:00
|
|
|
#ifdef _WIN32
|
2006-09-01 16:31:13 +00:00
|
|
|
{
|
|
|
|
xp_char_t buf[xp_sizeof(xp_long_t)*8+2+2];
|
|
|
|
xp_size_t n;
|
2006-10-22 12:39:30 +00:00
|
|
|
n = sse_awk_longtostr (-0x7FFFFFFFFFFFFFFFi64, 16, SSE_T("0x"), buf, xp_countof(buf));
|
2006-09-01 16:31:13 +00:00
|
|
|
if (n == (xp_size_t)-1)
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("cannot convert...\n"));
|
2006-09-01 16:31:13 +00:00
|
|
|
}
|
2006-10-22 12:39:30 +00:00
|
|
|
else xp_printf (SSE_T("%d, %s\n"), n, buf);
|
2006-09-01 16:31:13 +00:00
|
|
|
}
|
2006-09-22 14:05:30 +00:00
|
|
|
|
|
|
|
if (IsDebuggerPresent ())
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Running application in a debugger....\n"));
|
2006-09-22 14:05:30 +00:00
|
|
|
}
|
|
|
|
if (is_debugger_present ())
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Running application in a debugger by is_debugger_present...\n"));
|
2006-09-22 14:05:30 +00:00
|
|
|
}
|
|
|
|
if (is_debugger_present2 ())
|
|
|
|
{
|
2006-10-22 12:39:30 +00:00
|
|
|
xp_printf (SSE_T("Running application in a debugger by is_debugger_present2...\n"));
|
2006-09-22 14:05:30 +00:00
|
|
|
}
|
2006-09-02 15:17:44 +00:00
|
|
|
#endif
|
|
|
|
|
2006-05-12 09:39:20 +00:00
|
|
|
n = __main (argc, argv);
|
|
|
|
|
2006-05-12 11:10:26 +00:00
|
|
|
#if defined(__linux) && defined(_DEBUG)
|
2006-01-30 17:50:38 +00:00
|
|
|
muntrace ();
|
|
|
|
#endif
|
2006-09-01 04:03:28 +00:00
|
|
|
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
|
2006-05-12 09:39:20 +00:00
|
|
|
_CrtDumpMemoryLeaks ();
|
2006-05-13 15:51:43 +00:00
|
|
|
wprintf (L"Press ENTER to quit\n");
|
|
|
|
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
|
|
|
|