2006-03-31 16:35:37 +00:00
|
|
|
/*
|
2006-08-02 14:36:23 +00:00
|
|
|
* $Id: awk.c,v 1.61 2006-08-02 14:36:23 bacon Exp $
|
2006-03-31 16:35:37 +00:00
|
|
|
*/
|
|
|
|
|
2005-11-14 15:23:54 +00:00
|
|
|
#include <xp/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-06-30 06:09:38 +00:00
|
|
|
|
2006-06-19 04:38:51 +00:00
|
|
|
#ifdef XP_CHAR_IS_WCHAR
|
2006-06-30 06:09:38 +00:00
|
|
|
#include <wchar.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>
|
|
|
|
#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-06-30 06:09:38 +00:00
|
|
|
#else
|
|
|
|
#include <limits.h>
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define XP_PATH_MAX 4096
|
|
|
|
#else
|
|
|
|
#define XP_PATH_MAX PATH_MAX
|
|
|
|
#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-05-06 16:05:12 +00:00
|
|
|
#define xp_printf xp_awk_printf
|
|
|
|
extern int xp_awk_printf (const xp_char_t* fmt, ...);
|
|
|
|
#define xp_strcmp xp_awk_strcmp
|
|
|
|
extern int xp_awk_strcmp (const xp_char_t* s1, const xp_char_t* s2);
|
2006-06-19 04:38:51 +00:00
|
|
|
#define xp_strlen xp_awk_strlen
|
|
|
|
extern int xp_awk_strlen (const xp_char_t* s);
|
2006-07-27 16:50:29 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#define xp_assert assert
|
2006-07-28 10:34:22 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define xp_malloc malloc
|
|
|
|
#define xp_free free
|
2006-04-16 04:31:38 +00:00
|
|
|
#endif
|
2006-04-10 09:22:05 +00:00
|
|
|
|
2006-05-12 09:39:20 +00:00
|
|
|
#if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG)
|
|
|
|
#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
|
|
|
|
#ifdef XP_CHAR_IS_MCHAR
|
|
|
|
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
|
|
|
|
|
|
|
|
#ifdef XP_CHAR_IS_MCHAR
|
|
|
|
path_mb = path;
|
|
|
|
mode_mb = mode;
|
|
|
|
#else
|
|
|
|
if (xp_wcstomcs_strict (
|
2006-06-30 04:25:53 +00:00
|
|
|
path, path_mb, xp_countof(path_mb)) == -1) return XP_NULL;
|
2006-06-30 04:18:47 +00:00
|
|
|
if (xp_wcstomcs_strict (
|
2006-06-30 04:25:53 +00:00
|
|
|
mode, mode_mb, xp_countof(mode_mb)) == -1) return XP_NULL;
|
2006-06-30 04:18:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return fopen (path_mb, mode_mb);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
#ifdef XP_CHAR_IS_MCHAR
|
|
|
|
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
|
|
|
|
|
|
|
|
#ifdef XP_CHAR_IS_MCHAR
|
|
|
|
cmd_mb = cmd;
|
|
|
|
mode_mb = mode;
|
|
|
|
#else
|
|
|
|
if (xp_wcstomcs_strict (
|
|
|
|
cmd, cmd_mb, xp_countof(cmd_mb)) == -1) return XP_NULL;
|
|
|
|
if (xp_wcstomcs_strict (
|
|
|
|
mode, mode_mb, xp_countof(mode_mb)) == -1) return XP_NULL;
|
|
|
|
#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
|
|
|
|
#ifdef XP_CHAR_IS_MCHAR
|
|
|
|
#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-06-13 04:26:24 +00:00
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_OPEN:
|
2006-06-29 14:38:01 +00:00
|
|
|
{
|
|
|
|
if (src_io->input_file == XP_NULL) return 0;
|
2006-06-30 04:25:53 +00:00
|
|
|
src_io->input_handle = fopen_t (src_io->input_file, XP_T("r"));
|
2006-06-29 14:38:01 +00:00
|
|
|
if (src_io->input_handle == NULL) return -1;
|
2006-07-28 10:34:22 +00:00
|
|
|
return 1;
|
2006-06-29 14:38:01 +00:00
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_CLOSE:
|
2006-06-29 14:38:01 +00:00
|
|
|
{
|
|
|
|
if (src_io->input_file == XP_NULL) return 0;
|
|
|
|
fclose ((FILE*)src_io->input_handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_NEXT:
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_READ:
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
|
|
|
if (size <= 0) return -1;
|
|
|
|
#ifdef XP_CHAR_IS_MCHAR
|
2006-06-29 14:38:01 +00:00
|
|
|
c = fgetc ((FILE*)src_io->input_handle);
|
2006-06-13 04:26:24 +00:00
|
|
|
#else
|
2006-06-29 14:38:01 +00:00
|
|
|
c = fgetwc ((FILE*)src_io->input_handle);
|
2006-06-13 04:26:24 +00:00
|
|
|
#endif
|
|
|
|
if (c == XP_CHAR_EOF) return 0;
|
|
|
|
*data = c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_WRITE:
|
2006-06-13 04:26:24 +00:00
|
|
|
{
|
2006-06-29 14:38:01 +00:00
|
|
|
xp_printf (XP_T("XP_AWK_IO_WRITE CALLED FOR SOURCE\n"));
|
2006-06-22 04:25:44 +00:00
|
|
|
return -1;
|
2006-06-13 04:26:24 +00:00
|
|
|
}
|
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-06-19 09:10:57 +00:00
|
|
|
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
|
2006-06-19 04:38:51 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_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-07-28 10:34:22 +00:00
|
|
|
if (epa->mode == XP_AWK_IO_PIPE_READ)
|
2006-06-22 04:25:44 +00:00
|
|
|
mode = XP_T("r");
|
2006-07-28 10:34:22 +00:00
|
|
|
else if (epa->mode == XP_AWK_IO_PIPE_WRITE)
|
2006-06-22 04:25:44 +00:00
|
|
|
mode = XP_T("w");
|
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
2006-06-28 08:56:59 +00:00
|
|
|
xp_printf (XP_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-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_CLOSE:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-06-28 08:56:59 +00:00
|
|
|
xp_printf (XP_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-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_READ:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-06-30 04:18:47 +00:00
|
|
|
if (fgets_t (data, size, (FILE*)epa->handle) == XP_NULL)
|
2006-06-19 04:38:51 +00:00
|
|
|
return 0;
|
|
|
|
return xp_strlen(data);
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_WRITE:
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
2006-06-22 04:25:44 +00:00
|
|
|
/*
|
2006-06-30 04:18:47 +00:00
|
|
|
if (fputs_t (data, size, (FILE*)epa->handle) == XP_NULL)
|
2006-06-22 04:25:44 +00:00
|
|
|
return 0;
|
|
|
|
return size;
|
|
|
|
*/
|
2006-06-19 04:38:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_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
|
|
|
{
|
|
|
|
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_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-07-28 10:34:22 +00:00
|
|
|
if (epa->mode == XP_AWK_IO_FILE_READ)
|
2006-06-22 04:25:44 +00:00
|
|
|
mode = XP_T("r");
|
2006-07-28 10:34:22 +00:00
|
|
|
else if (epa->mode == XP_AWK_IO_FILE_WRITE)
|
2006-06-22 04:25:44 +00:00
|
|
|
mode = XP_T("w");
|
2006-07-28 10:34:22 +00:00
|
|
|
else if (epa->mode == XP_AWK_IO_FILE_APPEND)
|
2006-06-22 04:25:44 +00:00
|
|
|
mode = XP_T("a");
|
|
|
|
else return -1; /* TODO: any way to set the error number? */
|
|
|
|
|
2006-06-28 08:56:59 +00:00
|
|
|
xp_printf (XP_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;
|
|
|
|
epa->handle = (void*)handle;
|
2006-07-28 10:34:22 +00:00
|
|
|
return 1;
|
2006-06-19 09:10:57 +00:00
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_CLOSE:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-06-28 08:56:59 +00:00
|
|
|
xp_printf (XP_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-06-22 04:25:44 +00:00
|
|
|
case XP_AWK_IO_READ:
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
2006-06-30 04:18:47 +00:00
|
|
|
if (fgets_t (data, size, (FILE*)epa->handle) == XP_NULL)
|
2006-06-19 09:10:57 +00:00
|
|
|
return 0;
|
|
|
|
return xp_strlen(data);
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_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-06-19 09:10:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-06-22 04:25:44 +00:00
|
|
|
case XP_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-07-28 10:34:22 +00:00
|
|
|
static int open_extio_console (xp_awk_extio_t* epa);
|
|
|
|
static int close_extio_console (xp_awk_extio_t* epa);
|
|
|
|
static int next_extio_console (xp_awk_extio_t* epa);
|
|
|
|
|
|
|
|
static const xp_char_t* infiles[] =
|
|
|
|
{
|
2006-08-02 14:36:23 +00:00
|
|
|
//XP_T(""),
|
|
|
|
XP_T("awk.in"),
|
2006-07-28 10:34:22 +00:00
|
|
|
XP_NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static xp_size_t infile_no = 0;
|
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
|
|
|
{
|
|
|
|
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
|
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
if (cmd == XP_AWK_IO_OPEN)
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
return open_extio_console (epa);
|
|
|
|
}
|
|
|
|
else if (cmd == XP_AWK_IO_CLOSE)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
return close_extio_console (epa);
|
|
|
|
}
|
|
|
|
else if (cmd == XP_AWK_IO_READ)
|
|
|
|
{
|
|
|
|
while (fgets_t (data, size, epa->handle) == XP_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 */
|
|
|
|
if (infiles[infile_no] == XP_NULL)
|
|
|
|
{
|
|
|
|
/* no more input console */
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
/* is this correct??? */
|
|
|
|
/*
|
|
|
|
if (epa->handle != XP_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr) fclose (epa->handle);
|
|
|
|
epa->handle = XP_NULL;
|
|
|
|
*/
|
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-07-28 10:34:22 +00:00
|
|
|
if (infiles[infile_no][0] == XP_T('\0'))
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
if (epa->handle != XP_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr) fclose (epa->handle);
|
|
|
|
epa->handle = stdin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE* fp = fopen_t (infiles[infile_no], XP_T("r"));
|
|
|
|
if (fp == XP_NULL)
|
2006-07-27 16:50:29 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
xp_printf (XP_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-07-28 10:34:22 +00:00
|
|
|
if (epa->handle != XP_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr) fclose (epa->handle);
|
|
|
|
|
|
|
|
xp_printf (XP_TEXT("open the next console [%s]\n"), infiles[infile_no]);
|
|
|
|
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-07-28 10:34:22 +00:00
|
|
|
return xp_strlen(data);
|
|
|
|
}
|
|
|
|
else if (cmd == XP_AWK_IO_WRITE)
|
|
|
|
{
|
|
|
|
/* TODO: how to return error or 0 */
|
|
|
|
fputs_t (data, /*size,*/ (FILE*)epa->handle);
|
|
|
|
/*MessageBox (NULL, data, data, MB_OK);*/
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else if (cmd == XP_AWK_IO_NEXT)
|
|
|
|
{
|
|
|
|
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-07-28 10:34:22 +00:00
|
|
|
static int open_extio_console (xp_awk_extio_t* epa)
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
xp_assert (epa->name[0] == XP_T('\0'));
|
2006-07-27 16:50:29 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
xp_printf (XP_TEXT("opening console[%s] of type %x\n"), epa->name, epa->type);
|
2006-06-25 15:26:57 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
if (epa->mode == XP_AWK_IO_CONSOLE_READ)
|
|
|
|
{
|
|
|
|
if (infiles[infile_no] == XP_NULL)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
/* no more input file */
|
|
|
|
xp_printf (XP_TEXT("console - no more file\n"));;
|
|
|
|
return 0;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
if (infiles[infile_no][0] == XP_T('\0'))
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
2006-07-28 10:34:22 +00:00
|
|
|
xp_printf (XP_T(" console(r) - <standard input>\n"));
|
|
|
|
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 = fopen_t (infiles[infile_no], XP_T("r"));
|
|
|
|
if (fp == XP_NULL)
|
|
|
|
{
|
|
|
|
xp_printf (XP_TEXT("failed to open console of type %x - fopen failure\n"), epa->type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xp_printf (XP_T(" console(r) - %s\n"), infiles[infile_no]);
|
|
|
|
epa->handle = fp;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
infile_no++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (epa->mode == XP_AWK_IO_CONSOLE_WRITE)
|
|
|
|
{
|
|
|
|
xp_printf (XP_T(" console(w) - <standard output>\n"));
|
|
|
|
epa->handle = stdout;
|
|
|
|
return 1;
|
2006-06-25 15:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2006-06-19 09:10:57 +00:00
|
|
|
|
2006-07-28 10:34:22 +00:00
|
|
|
static int close_extio_console (xp_awk_extio_t* epa)
|
|
|
|
{
|
|
|
|
xp_printf (XP_TEXT("closing console of type %x\n"), epa->type);
|
|
|
|
|
|
|
|
if (epa->handle != XP_NULL &&
|
|
|
|
epa->handle != stdin &&
|
|
|
|
epa->handle != stdout &&
|
|
|
|
epa->handle != stderr)
|
|
|
|
{
|
|
|
|
fclose (epa->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: CloseConsole in GUI APPLICATION */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int next_extio_console (xp_awk_extio_t* epa)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
FILE* fp = epa->handle;
|
|
|
|
xp_printf (XP_TEXT("switching console[%s] of type %x\n"), epa->name, epa->type);
|
|
|
|
|
|
|
|
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 != XP_NULL && fp != stdin &&
|
|
|
|
fp != stdout && fp != stderr) fclose (fp);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
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-03-31 16:35:37 +00:00
|
|
|
xp_awk_t* awk;
|
2006-06-29 14:38:01 +00:00
|
|
|
struct src_io src_io = { NULL, NULL };
|
2005-11-14 15:23:54 +00:00
|
|
|
|
2006-04-10 09:22:05 +00:00
|
|
|
if ((awk = xp_awk_open()) == XP_NULL)
|
|
|
|
{
|
2006-05-06 16:05:12 +00:00
|
|
|
xp_printf (XP_T("Error: cannot open awk\n"));
|
2005-11-14 15:23:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-06-19 04:38:51 +00:00
|
|
|
/* TODO: */
|
2006-06-19 09:10:57 +00:00
|
|
|
if (xp_awk_setextio (awk,
|
|
|
|
XP_AWK_EXTIO_PIPE, process_extio_pipe, XP_NULL) == -1)
|
2006-06-19 04:38:51 +00:00
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
2006-06-19 09:10:57 +00:00
|
|
|
xp_printf (XP_T("Error: cannot set extio pipe\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: */
|
|
|
|
if (xp_awk_setextio (awk,
|
|
|
|
XP_AWK_EXTIO_FILE, process_extio_file, XP_NULL) == -1)
|
2006-06-25 15:26:57 +00:00
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
|
|
|
xp_printf (XP_T("Error: cannot set extio file\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xp_awk_setextio (awk,
|
|
|
|
XP_AWK_EXTIO_CONSOLE, process_extio_console, XP_NULL) == -1)
|
2006-06-19 09:10:57 +00:00
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
|
|
|
xp_printf (XP_T("Error: cannot set extio file\n"));
|
2006-06-19 04:38:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_setparseopt (awk,
|
2006-06-21 13:52:15 +00:00
|
|
|
XP_AWK_EXPLICIT | XP_AWK_UNIQUE | XP_AWK_DBLSLASHES |
|
2006-06-18 13:43:28 +00:00
|
|
|
XP_AWK_SHADING | XP_AWK_IMPLICIT | XP_AWK_SHIFT | XP_AWK_EXTIO);
|
2006-03-05 17:07:33 +00:00
|
|
|
|
2006-04-10 09:22:05 +00:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
2006-04-18 16:27:29 +00:00
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32)
|
2006-04-14 11:13:06 +00:00
|
|
|
if (strcmp(argv[1], "-m") == 0)
|
|
|
|
#else
|
2006-05-06 16:05:12 +00:00
|
|
|
if (xp_strcmp(argv[1], XP_T("-m")) == 0)
|
2006-06-29 14:38:01 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
|
|
|
xp_printf (XP_T("Usage: %s [-m] source_file\n"), argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
src_io.input_file = argv[1];
|
|
|
|
}
|
|
|
|
else if (argc == 3)
|
|
|
|
{
|
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32)
|
|
|
|
if (strcmp(argv[1], "-m") == 0)
|
|
|
|
#else
|
|
|
|
if (xp_strcmp(argv[1], XP_T("-m")) == 0)
|
2006-04-14 11:13:06 +00:00
|
|
|
#endif
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
|
|
|
xp_awk_setrunopt (awk, XP_AWK_RUNMAIN);
|
|
|
|
}
|
2006-06-29 14:38:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
|
|
|
xp_printf (XP_T("Usage: %s [-m] source_file\n"), argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
src_io.input_file = argv[2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
|
|
|
xp_printf (XP_T("Usage: %s [-m] source_file\n"), argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xp_awk_attsrc(awk, process_source, (void*)&src_io) == -1)
|
|
|
|
{
|
|
|
|
xp_awk_close (awk);
|
|
|
|
xp_printf (XP_T("Error: cannot attach source\n"));
|
|
|
|
return -1;
|
2006-04-10 09:22:05 +00:00
|
|
|
}
|
2006-04-09 15:34:38 +00:00
|
|
|
|
2006-04-10 09:22:05 +00:00
|
|
|
if (xp_awk_parse(awk) == -1)
|
|
|
|
{
|
2006-04-18 16:27:29 +00:00
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(XP_CHAR_IS_WCHAR)
|
2006-04-14 16:26:00 +00:00
|
|
|
xp_printf (
|
2006-07-26 16:43:35 +00:00
|
|
|
XP_T("ERROR: cannot parse program - line %u [%d] %ls"),
|
2006-05-13 16:33:07 +00:00
|
|
|
(unsigned int)xp_awk_getsrcline(awk),
|
2006-04-14 16:26:00 +00:00
|
|
|
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
|
2006-07-26 16:43:35 +00:00
|
|
|
if (xp_awk_getsuberrnum(awk) != XP_AWK_ENOERR)
|
|
|
|
{
|
|
|
|
xp_printf (XP_T(" - %ls\n"), xp_awk_getsuberrstr(awk));
|
|
|
|
}
|
2006-04-14 16:26:00 +00:00
|
|
|
#else
|
2006-01-31 16:57:45 +00:00
|
|
|
xp_printf (
|
2006-07-26 16:43:35 +00:00
|
|
|
XP_T("ERROR: cannot parse program - line %u [%d] %s"),
|
2006-05-13 16:33:07 +00:00
|
|
|
(unsigned int)xp_awk_getsrcline(awk),
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
|
2006-07-26 16:43:35 +00:00
|
|
|
if (xp_awk_getsuberrnum(awk) != XP_AWK_ENOERR)
|
|
|
|
{
|
|
|
|
xp_printf (XP_T(" - %s\n"), xp_awk_getsuberrstr(awk));
|
|
|
|
}
|
2006-04-14 16:26:00 +00:00
|
|
|
#endif
|
2006-07-26 16:43:35 +00:00
|
|
|
xp_printf (XP_T("\n"));
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_close (awk);
|
2005-11-14 15:23:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-27 16:50:29 +00:00
|
|
|
if (xp_awk_run (awk) == -1)
|
2006-04-10 09:22:05 +00:00
|
|
|
{
|
2006-04-18 16:27:29 +00:00
|
|
|
#if defined(__STAND_ALONE) && !defined(_WIN32) && defined(XP_CHAR_IS_WCHAR)
|
2006-04-14 16:26:00 +00:00
|
|
|
xp_printf (
|
2006-08-01 04:36:33 +00:00
|
|
|
XP_T("error: cannot run program - [%d] %ls"),
|
2006-04-14 16:26:00 +00:00
|
|
|
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
|
|
|
|
#else
|
2006-03-05 17:07:33 +00:00
|
|
|
xp_printf (
|
2006-08-01 04:36:33 +00:00
|
|
|
XP_T("error: cannot run program - [%d] %s"),
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_geterrnum(awk), xp_awk_geterrstr(awk));
|
2006-04-14 16:26:00 +00:00
|
|
|
#endif
|
2006-08-01 04:36:33 +00:00
|
|
|
if (xp_awk_getsuberrnum(awk) != XP_AWK_ENOERR)
|
|
|
|
{
|
|
|
|
xp_printf (XP_T(" - %ls\n"), xp_awk_getsuberrstr(awk));
|
|
|
|
}
|
|
|
|
xp_printf (XP_T("\n"));
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_close (awk);
|
2006-04-06 16:25:37 +00:00
|
|
|
return -1;
|
2006-03-05 17:07:33 +00:00
|
|
|
}
|
|
|
|
|
2006-03-31 16:35:37 +00:00
|
|
|
xp_awk_close (awk);
|
2006-05-12 09:39:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-30 17:50:38 +00:00
|
|
|
|
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-05-12 10:56:18 +00:00
|
|
|
/*#if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG)
|
|
|
|
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF);
|
|
|
|
#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-05-12 09:39:20 +00:00
|
|
|
#if defined(_WIN32) && defined(__STAND_ALONE) && defined(_DEBUG)
|
|
|
|
_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
|
|
|
|