*** empty log message ***
This commit is contained in:
parent
74ca89e002
commit
b4d8b06a66
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* $Id: awk.c,v 1.141 2006-12-17 12:50:59 bacon Exp $
|
* $Id: awk.c,v 1.142 2006-12-17 13:12:08 bacon Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ase/awk/awk.h>
|
#include <ase/awk/awk.h>
|
||||||
@ -8,6 +8,7 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
@ -22,13 +23,13 @@
|
|||||||
#else
|
#else
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#include <wctype.h>
|
#include <wctype.h>
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
#include <xp/bas/stdio.h>
|
#include <xp/bas/stdio.h>
|
||||||
#include <xp/bas/stdlib.h>
|
#include <xp/bas/stdlib.h>
|
||||||
#include <xp/bas/string.h>
|
#include <xp/bas/string.h>
|
||||||
#include <xp/bas/memory.h>
|
#include <xp/bas/memory.h>
|
||||||
#include <xp/bas/sysapi.h>
|
#include <xp/bas/sysapi.h>
|
||||||
#include <xp/bas/locale.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
|
#if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
|
||||||
@ -46,31 +47,14 @@ struct src_io
|
|||||||
FILE* input_handle;
|
FILE* input_handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
static FILE* fopen_t (const ase_char_t* path, const ase_char_t* mode)
|
static ase_real_t awk_pow (ase_real_t x, ase_real_t y)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
return pow (x, y);
|
||||||
return _tfopen (path, mode);
|
}
|
||||||
#else
|
|
||||||
#ifdef ASE_CHAR_IS_MCHAR
|
|
||||||
const ase_mchar_t* path_mb;
|
|
||||||
const ase_mchar_t* mode_mb;
|
|
||||||
#else
|
|
||||||
ase_mchar_t path_mb[XP_PATH_MAX + 1];
|
|
||||||
ase_mchar_t mode_mb[32];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ASE_CHAR_IS_MCHAR
|
static void awk_abort (void* custom_data)
|
||||||
path_mb = path;
|
{
|
||||||
mode_mb = mode;
|
abort ();
|
||||||
#else
|
|
||||||
if (xp_wcstomcs_strict (
|
|
||||||
path, path_mb, ASE_COUNTOF(path_mb)) == -1) return ASE_NULL;
|
|
||||||
if (xp_wcstomcs_strict (
|
|
||||||
mode, mode_mb, ASE_COUNTOF(mode_mb)) == -1) return ASE_NULL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return fopen (path_mb, mode_mb);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int awk_sprintf (
|
static int awk_sprintf (
|
||||||
@ -158,59 +142,67 @@ static void awk_printf (const ase_char_t* fmt, ...)
|
|||||||
va_end (ap);
|
va_end (ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ase_real_t awk_pow (ase_real_t x, ase_real_t y)
|
static FILE* awk_fopen (const ase_char_t* path, const ase_char_t* mode)
|
||||||
{
|
{
|
||||||
return pow (x, y);
|
#ifdef _WIN32
|
||||||
|
return _tfopen (path, mode);
|
||||||
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
||||||
|
return fopen (path, mode);
|
||||||
|
#else
|
||||||
|
char path_mb[PATH_MAX + 1];
|
||||||
|
char mode_mb[32];
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
n = wcstombs (path, path_mb, ASE_COUNTOF(path_mb))
|
||||||
|
if (n == -1) return NULL;
|
||||||
|
if (n == ASE_COUNTOF(path_mb)) path_mb[ASE_COUNTOF(path_mb)-1] = '\0';
|
||||||
|
|
||||||
|
n = wcstombs (mode, mode_mb, ASE_COUNTOF(mode_mb))
|
||||||
|
if (n == -1) return NULL;
|
||||||
|
if (n == ASE_COUNTOF(mode_mb)) path_mb[ASE_COUNTOF(mode_mb)-1] = '\0';
|
||||||
|
|
||||||
|
return fopen (path_mb, mode_mb);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void awk_abort (void* custom_data)
|
static FILE* awk_popen (const ase_char_t* cmd, const ase_char_t* mode)
|
||||||
{
|
|
||||||
abort ();
|
|
||||||
}
|
|
||||||
|
|
||||||
static FILE* popen_t (const ase_char_t* cmd, const ase_char_t* mode)
|
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
return _tpopen (cmd, mode);
|
return _tpopen (cmd, mode);
|
||||||
#elif defined(__MSDOS__)
|
#elif defined(__MSDOS__)
|
||||||
/* TODO: support this */
|
/* TODO: support this */
|
||||||
return NULL;
|
return NULL;
|
||||||
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
||||||
|
return popen (cmd, mode);
|
||||||
#else
|
#else
|
||||||
#ifdef ASE_CHAR_IS_MCHAR
|
char cmd_mb[PATH_MAX + 1];
|
||||||
const ase_mchar_t* cmd_mb;
|
char mode_mb[32];
|
||||||
const ase_mchar_t* mode_mb;
|
size_t n;
|
||||||
#else
|
|
||||||
ase_mchar_t cmd_mb[2048];
|
|
||||||
ase_mchar_t mode_mb[32];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ASE_CHAR_IS_MCHAR
|
n = wcstombs (cmd, cmd_mb, ASE_COUNTOF(cmd_mb))
|
||||||
cmd_mb = cmd;
|
if (n == -1) return NULL;
|
||||||
mode_mb = mode;
|
if (n == ASE_COUNTOF(cmd_mb)) cmd_mb[ASE_COUNTOF(cmd_mb)-1] = '\0';
|
||||||
#else
|
|
||||||
if (xp_wcstomcs_strict (
|
n = wcstombs (mode, mode_mb, ASE_COUNTOF(mode_mb))
|
||||||
cmd, cmd_mb, ASE_COUNTOF(cmd_mb)) == -1) return ASE_NULL;
|
if (n == -1) return NULL;
|
||||||
if (xp_wcstomcs_strict (
|
if (n == ASE_COUNTOF(mode_mb)) cmd_mb[ASE_COUNTOF(mode_mb)-1] = '\0';
|
||||||
mode, mode_mb, ASE_COUNTOF(mode_mb)) == -1) return ASE_NULL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return popen (cmd_mb, mode_mb);
|
return popen (cmd_mb, mode_mb);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WIN32
|
#if defined(_WIN32)
|
||||||
#define fgets_t _fgetts
|
#define awk_fgets _fgetts
|
||||||
#define fputs_t _fputts
|
#define awk_fputs _fputts
|
||||||
#define fputc_t _fputtc
|
#define awk_fputc _fputtc
|
||||||
|
#elif defined(ASE_CHAR_IS_MCHAR)
|
||||||
|
#define awk_fgets fgets
|
||||||
|
#define awk_fputs fputs
|
||||||
|
#define awk_fputc fputc
|
||||||
#else
|
#else
|
||||||
#ifdef ASE_CHAR_IS_MCHAR
|
#define awk_fgets fgetws
|
||||||
#define fgets_t fgets
|
#define awk_fputs fputws
|
||||||
#define fputs_t fputs
|
#define awk_fputc fputwc
|
||||||
#define fputc_t fputc
|
|
||||||
#else
|
|
||||||
#define fgets_t fgetws
|
|
||||||
#define fputc_t fputwc
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static ase_ssize_t process_source (
|
static ase_ssize_t process_source (
|
||||||
@ -222,7 +214,7 @@ static ase_ssize_t process_source (
|
|||||||
if (cmd == ASE_AWK_IO_OPEN)
|
if (cmd == ASE_AWK_IO_OPEN)
|
||||||
{
|
{
|
||||||
if (src_io->input_file == ASE_NULL) return 0;
|
if (src_io->input_file == ASE_NULL) return 0;
|
||||||
src_io->input_handle = fopen_t (src_io->input_file, ASE_T("r"));
|
src_io->input_handle = awk_fopen (src_io->input_file, ASE_T("r"));
|
||||||
if (src_io->input_handle == NULL) return -1;
|
if (src_io->input_handle == NULL) return -1;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -290,7 +282,7 @@ static ase_ssize_t process_extio_pipe (
|
|||||||
mode = ASE_T("w");
|
mode = ASE_T("w");
|
||||||
else return -1; /* TODO: any way to set the error number? */
|
else return -1; /* TODO: any way to set the error number? */
|
||||||
awk_dprintf (ASE_T("opending %s of type %d (pipe)\n"), epa->name, epa->type);
|
awk_dprintf (ASE_T("opending %s of type %d (pipe)\n"), epa->name, epa->type);
|
||||||
handle = popen_t (epa->name, mode);
|
handle = awk_popen (epa->name, mode);
|
||||||
if (handle == NULL) return -1;
|
if (handle == NULL) return -1;
|
||||||
epa->handle = (void*)handle;
|
epa->handle = (void*)handle;
|
||||||
return 1;
|
return 1;
|
||||||
@ -306,7 +298,7 @@ static ase_ssize_t process_extio_pipe (
|
|||||||
|
|
||||||
case ASE_AWK_IO_READ:
|
case ASE_AWK_IO_READ:
|
||||||
{
|
{
|
||||||
if (fgets_t (data, size, (FILE*)epa->handle) == ASE_NULL)
|
if (awk_fgets (data, size, (FILE*)epa->handle) == ASE_NULL)
|
||||||
return 0;
|
return 0;
|
||||||
return ase_awk_strlen(data);
|
return ase_awk_strlen(data);
|
||||||
}
|
}
|
||||||
@ -317,7 +309,7 @@ static ase_ssize_t process_extio_pipe (
|
|||||||
/* TODO: how to return error or 0 */
|
/* TODO: how to return error or 0 */
|
||||||
for (i = 0; i < size; i++)
|
for (i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
fputc_t (data[i], (FILE*)epa->handle);
|
awk_fputc (data[i], (FILE*)epa->handle);
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@ -358,7 +350,7 @@ static ase_ssize_t process_extio_file (
|
|||||||
else return -1; /* TODO: any way to set the error number? */
|
else return -1; /* TODO: any way to set the error number? */
|
||||||
|
|
||||||
awk_dprintf (ASE_T("opending %s of type %d (file)\n"), epa->name, epa->type);
|
awk_dprintf (ASE_T("opending %s of type %d (file)\n"), epa->name, epa->type);
|
||||||
handle = fopen_t (epa->name, mode);
|
handle = awk_fopen (epa->name, mode);
|
||||||
if (handle == NULL) return -1;
|
if (handle == NULL) return -1;
|
||||||
|
|
||||||
epa->handle = (void*)handle;
|
epa->handle = (void*)handle;
|
||||||
@ -375,7 +367,7 @@ static ase_ssize_t process_extio_file (
|
|||||||
|
|
||||||
case ASE_AWK_IO_READ:
|
case ASE_AWK_IO_READ:
|
||||||
{
|
{
|
||||||
if (fgets_t (data, size, (FILE*)epa->handle) == ASE_NULL)
|
if (awk_fgets (data, size, (FILE*)epa->handle) == ASE_NULL)
|
||||||
return 0;
|
return 0;
|
||||||
return ase_awk_strlen(data);
|
return ase_awk_strlen(data);
|
||||||
}
|
}
|
||||||
@ -386,7 +378,7 @@ static ase_ssize_t process_extio_file (
|
|||||||
/* TODO: how to return error or 0 */
|
/* TODO: how to return error or 0 */
|
||||||
for (i = 0; i < size; i++)
|
for (i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
fputc_t (data[i], (FILE*)epa->handle);
|
awk_fputc (data[i], (FILE*)epa->handle);
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@ -439,7 +431,7 @@ static ase_ssize_t process_extio_console (
|
|||||||
}
|
}
|
||||||
else if (cmd == ASE_AWK_IO_READ)
|
else if (cmd == ASE_AWK_IO_READ)
|
||||||
{
|
{
|
||||||
while (fgets_t (data, size, epa->handle) == ASE_NULL)
|
while (awk_fgets (data, size, epa->handle) == ASE_NULL)
|
||||||
{
|
{
|
||||||
/* it has reached the end of the current file.
|
/* it has reached the end of the current file.
|
||||||
* open the next file if available */
|
* open the next file if available */
|
||||||
@ -469,7 +461,7 @@ static ase_ssize_t process_extio_console (
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
FILE* fp = fopen_t (infiles[infile_no], ASE_T("r"));
|
FILE* fp = awk_fopen (infiles[infile_no], ASE_T("r"));
|
||||||
if (fp == ASE_NULL)
|
if (fp == ASE_NULL)
|
||||||
{
|
{
|
||||||
awk_dprintf (ASE_T("failed to open the next console of type %x - fopen failure\n"), epa->type);
|
awk_dprintf (ASE_T("failed to open the next console of type %x - fopen failure\n"), epa->type);
|
||||||
@ -496,7 +488,7 @@ static ase_ssize_t process_extio_console (
|
|||||||
/* TODO: how to return error or 0 */
|
/* TODO: how to return error or 0 */
|
||||||
for (i = 0; i < size; i++)
|
for (i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
fputc_t (data[i], (FILE*)epa->handle);
|
awk_fputc (data[i], (FILE*)epa->handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*MessageBox (NULL, data, data, MB_OK);*/
|
/*MessageBox (NULL, data, data, MB_OK);*/
|
||||||
@ -542,7 +534,7 @@ static int open_extio_console (ase_awk_extio_t* epa)
|
|||||||
{
|
{
|
||||||
/* a temporary variable fp is used here not to change
|
/* a temporary variable fp is used here not to change
|
||||||
* any fields of epa when the open operation fails */
|
* any fields of epa when the open operation fails */
|
||||||
FILE* fp = fopen_t (infiles[infile_no], ASE_T("r"));
|
FILE* fp = awk_fopen (infiles[infile_no], ASE_T("r"));
|
||||||
if (fp == ASE_NULL)
|
if (fp == ASE_NULL)
|
||||||
{
|
{
|
||||||
awk_dprintf (ASE_T("cannot open console of type %x - fopen failure\n"), epa->type);
|
awk_dprintf (ASE_T("cannot open console of type %x - fopen failure\n"), epa->type);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user