*** empty log message ***

This commit is contained in:
hyung-hwan 2006-06-22 04:25:44 +00:00
parent fbfceeda5f
commit 6a969fbdd8
6 changed files with 155 additions and 129 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c,v 1.55 2006-06-20 15:27:50 bacon Exp $
* $Id: awk.c,v 1.56 2006-06-22 04:25:44 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -160,18 +160,18 @@ void xp_awk_setrunopt (xp_awk_t* awk, int opt)
awk->opt.run = opt;
}
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t src, void* arg)
int xp_awk_attsrc (xp_awk_t* awk, xp_awk_io_t handler, void* arg)
{
if (xp_awk_detsrc(awk) == -1) return -1;
xp_assert (awk->srcio == XP_NULL);
if (src(XP_AWK_INPUT_OPEN, arg, XP_NULL, 0) == -1)
if (handler (XP_AWK_IO_OPEN, 0, arg, XP_NULL, 0) == -1)
{
awk->errnum = XP_AWK_ESRCINOPEN;
return -1;
}
awk->srcio = src;
awk->srcio = handler;
awk->srcio_arg = arg;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
@ -188,7 +188,8 @@ int xp_awk_detsrc (xp_awk_t* awk)
{
xp_ssize_t n;
n = awk->srcio (XP_AWK_INPUT_CLOSE, awk->srcio_arg, XP_NULL, 0);
n = awk->srcio (
XP_AWK_IO_CLOSE, 0, awk->srcio_arg, XP_NULL, 0);
if (n == -1)
{
awk->errnum = XP_AWK_ESRCINCLOSE;

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h,v 1.70 2006-06-21 15:37:51 bacon Exp $
* $Id: awk.h,v 1.71 2006-06-22 04:25:44 bacon Exp $
*/
#ifndef _XP_AWK_AWK_H_
@ -9,12 +9,11 @@
#include <xp/macros.h>
typedef struct xp_awk_t xp_awk_t;
typedef struct xp_awk_val_t xp_awk_val_t;
typedef struct xp_awk_extio_t xp_awk_extio_t;
typedef xp_ssize_t (*xp_awk_io_t) (
int cmd, void* arg, xp_char_t* data, xp_size_t count);
typedef struct xp_awk_extio_t xp_awk_extio_t;
typedef struct xp_awk_val_t xp_awk_val_t;
int cmd, int opt, void* arg, xp_char_t* data, xp_size_t count);
struct xp_awk_extio_t
{
@ -27,15 +26,21 @@ struct xp_awk_extio_t
/* io function commands */
enum
{
XP_AWK_INPUT_OPEN = 0,
XP_AWK_INPUT_CLOSE = 1,
XP_AWK_INPUT_NEXT = 2,
XP_AWK_INPUT_DATA = 3,
XP_AWK_IO_OPEN = 0,
XP_AWK_IO_CLOSE = 1,
XP_AWK_IO_READ = 2,
XP_AWK_IO_WRITE = 3,
XP_AWK_IO_NEXT = 4
};
XP_AWK_OUTPUT_OPEN = 4,
XP_AWK_OUTPUT_CLOSE = 5,
XP_AWK_OUTPUT_NEXT = 6,
XP_AWK_OUTPUT_DATA = 7
enum
{
XP_AWK_IO_FILE_READ = 0,
XP_AWK_IO_FILE_WRITE = 1,
XP_AWK_IO_FILE_APPEND = 2,
XP_AWK_IO_PIPE_READ = 0,
XP_AWK_IO_PIPE_WRITE = 1
};
/* parse options */

View File

@ -1,5 +1,5 @@
/*
* $Id: extio.c,v 1.7 2006-06-21 15:37:51 bacon Exp $
* $Id: extio.c,v 1.8 2006-06-22 04:25:44 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -15,6 +15,7 @@ int xp_awk_readextio (
{
xp_awk_extio_t* p = run->extio;
xp_awk_io_t handler = run->awk->extio[type];
int ioopt;
if (handler == XP_NULL)
{
@ -51,7 +52,13 @@ int xp_awk_readextio (
p->handle = XP_NULL;
p->next = XP_NULL;
if (handler (XP_AWK_INPUT_OPEN, p, XP_NULL, 0) == -1)
if (type == XP_AWK_EXTIO_PIPE)
ioopt = XP_AWK_IO_PIPE_READ;
else if (type == XP_AWK_EXTIO_FILE)
ioopt = XP_AWK_IO_FILE_READ;
else ioopt = 0; /* TODO: how to handle this??? */
if (handler (XP_AWK_IO_OPEN, ioopt, p, XP_NULL, 0) == -1)
{
xp_free (p->name);
xp_free (p);
@ -81,7 +88,7 @@ int xp_awk_readextio (
{
xp_char_t buf[1024];
if (handler (XP_AWK_INPUT_DATA, p, buf, xp_countof(buf)) == 0)
if (handler (XP_AWK_IO_READ, 0, p, buf, xp_countof(buf)) == 0)
{
/* no more data. end of data stream */
return 0;
@ -107,9 +114,9 @@ int xp_awk_closeextio (xp_awk_run_t* run, const xp_char_t* name, int* errnum)
if (handler != NULL)
{
/* TODO: io command should not be XP_AWK_INPUT_CLOSE
/* TODO: io command should not be XP_AWK_IO_CLOSE
* it should be more generic form than this... */
if (handler (XP_AWK_INPUT_CLOSE, p, XP_NULL, 0) == -1)
if (handler (XP_AWK_IO_CLOSE, 0, p, XP_NULL, 0) == -1)
{
/* this is not a run-time error.*/
*errnum = XP_AWK_ENOERR;
@ -149,7 +156,7 @@ void xp_awk_clearextio (xp_awk_run_t* run)
{
/* TODO: io command should not be XP_AWK_INPUT_CLOSE
* it should be more generic form than this... */
n = handler (XP_AWK_INPUT_CLOSE, run->extio, XP_NULL, 0);
n = handler (XP_AWK_IO_CLOSE, 0, run->extio, XP_NULL, 0);
if (n == -1)
{
/* TODO:

View File

@ -1,5 +1,5 @@
/*
* $Id: parse.c,v 1.119 2006-06-21 15:37:51 bacon Exp $
* $Id: parse.c,v 1.120 2006-06-22 04:25:44 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -3443,7 +3443,7 @@ static int __get_char (xp_awk_t* awk)
}
/*
n = awk->srcio (XP_AWK_INPUT_DATA, awk->srcio_arg, &c, 1);
n = awk->srcio (XP_AWK_IO_READ, awk->srcio_arg, &c, 1);
if (n == -1)
{
awk->errnum = XP_AWK_ESRCINDATA;
@ -3453,7 +3453,7 @@ static int __get_char (xp_awk_t* awk)
*/
if (awk->lex.buf_pos >= awk->lex.buf_len)
{
n = awk->srcio (XP_AWK_INPUT_DATA,
n = awk->srcio (XP_AWK_IO_READ, 0,
awk->srcio_arg, awk->lex.buf, xp_countof(awk->lex.buf));
if (n == -1)
{

View File

@ -1,5 +1,5 @@
/*
* $Id: run.c,v 1.104 2006-06-21 15:37:51 bacon Exp $
* $Id: run.c,v 1.105 2006-06-22 04:25:44 bacon Exp $
*/
#include <xp/awk/awk_i.h>
@ -481,7 +481,7 @@ static int __run_pattern_blocks (xp_awk_run_t* run)
xp_assert (run->txtio != XP_NULL);
n = run->txtio (XP_AWK_INPUT_OPEN, run->txtio_arg, XP_NULL, 0);
n = run->txtio (XP_AWK_IO_OPEN, 0, run->txtio_arg, XP_NULL, 0);
if (n == -1) PANIC_I (run, XP_AWK_ETXTINOPEN);
run->input.buf_pos = 0;
@ -499,7 +499,7 @@ static int __run_pattern_blocks (xp_awk_run_t* run)
if (x == -1)
{
/* don't care about the result of input close */
run->txtio (XP_AWK_INPUT_CLOSE,
run->txtio (XP_AWK_IO_CLOSE, 0,
run->txtio_arg, XP_NULL, 0);
return -1;
}
@ -515,13 +515,13 @@ xp_printf (XP_T("**** line [%s]\n"), XP_STR_BUF(&run->input.line));
if (__run_pattern_block_chain (run, run->awk->tree.chain) == -1)
{
/* don't care about the result of input close */
run->txtio (XP_AWK_INPUT_CLOSE,
run->txtio (XP_AWK_IO_CLOSE, 0,
run->txtio_arg, XP_NULL, 0);
return -1;
}
}
n = run->txtio (XP_AWK_INPUT_CLOSE, run->txtio_arg, XP_NULL, 0);
n = run->txtio (XP_AWK_IO_CLOSE, 0, run->txtio_arg, XP_NULL, 0);
if (n == -1) PANIC_I (run, XP_AWK_ETXTINCLOSE);
return 0;
@ -1070,7 +1070,9 @@ xp_printf (XP_T("**** next NOT IMPLEMENTED...\n"));
static int __run_nextfile_statement (xp_awk_run_t* run, xp_awk_nde_nextfile_t* nde)
{
xp_ssize_t n;
n = run->txtio (XP_AWK_INPUT_NEXT, run->txtio_arg, XP_NULL, 0);
/* TODO: how to pass opt properly for IO_NEXT??? -> READ? WRITE? */
n = run->txtio (XP_AWK_IO_NEXT, 0, run->txtio_arg, XP_NULL, 0);
if (n == -1) PANIC_I (run, XP_AWK_ETXTINNEXT);
return (n == -1)? -1: 0;
}
@ -3048,7 +3050,7 @@ static int __read_text_input (xp_awk_run_t* run)
{
if (run->input.buf_pos >= run->input.buf_len)
{
n = run->txtio (XP_AWK_INPUT_DATA, run->txtio_arg,
n = run->txtio (XP_AWK_IO_READ, 0, run->txtio_arg,
run->input.buf, xp_countof(run->input.buf));
if (n == -1) PANIC_I (run, XP_AWK_ETXTINDATA);
if (n == 0)

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c,v 1.40 2006-06-21 13:52:15 bacon Exp $
* $Id: awk.c,v 1.41 2006-06-22 04:25:44 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -37,20 +37,20 @@
#endif
static xp_ssize_t process_source (
int cmd, void* arg, xp_char_t* data, xp_size_t size)
int cmd, int opt, void* arg, xp_char_t* data, xp_size_t size)
{
xp_char_t c;
switch (cmd)
{
case XP_AWK_INPUT_OPEN:
case XP_AWK_INPUT_CLOSE:
case XP_AWK_INPUT_NEXT:
case XP_AWK_IO_OPEN:
case XP_AWK_IO_CLOSE:
case XP_AWK_IO_NEXT:
{
return 0;
}
case XP_AWK_INPUT_DATA:
case XP_AWK_IO_READ:
{
if (size <= 0) return -1;
#ifdef XP_CHAR_IS_MCHAR
@ -63,18 +63,16 @@ static xp_ssize_t process_source (
return 1;
}
case XP_AWK_OUTPUT_OPEN:
case XP_AWK_OUTPUT_CLOSE:
case XP_AWK_OUTPUT_NEXT:
case XP_AWK_OUTPUT_DATA:
case XP_AWK_IO_WRITE:
{
return 0;
return -1;
}
}
return -1;
}
struct data_io
{
const char* input_file;
@ -82,34 +80,28 @@ struct data_io
};
static xp_ssize_t process_data (
int cmd, void* arg, xp_char_t* data, xp_size_t size)
int cmd, int opt, void* arg, xp_char_t* data, xp_size_t size)
{
struct data_io* io = (struct data_io*)arg;
xp_char_t c;
switch (cmd)
{
case XP_AWK_INPUT_OPEN:
case XP_AWK_IO_OPEN:
{
io->input_handle = fopen (io->input_file, "r");
if (io->input_handle == NULL) return -1;
return 0;
}
case XP_AWK_INPUT_CLOSE:
case XP_AWK_IO_CLOSE:
{
fclose (io->input_handle);
io->input_handle = NULL;
return 0;
}
case XP_AWK_INPUT_NEXT:
{
/* input switching not supported for the time being... */
return -1;
}
case XP_AWK_INPUT_DATA:
case XP_AWK_IO_READ:
{
if (size <= 0) return -1;
#ifdef XP_CHAR_IS_MCHAR
@ -122,10 +114,71 @@ static xp_ssize_t process_data (
return 1;
}
case XP_AWK_OUTPUT_OPEN:
case XP_AWK_OUTPUT_CLOSE:
case XP_AWK_OUTPUT_NEXT:
case XP_AWK_OUTPUT_DATA:
case XP_AWK_IO_WRITE:
{
return -1;
}
case XP_AWK_IO_NEXT:
{
/* input switching not supported for the time being... */
return -1;
}
}
return -1;
}
static xp_ssize_t process_extio_pipe (
int cmd, int opt, void* arg, xp_char_t* data, xp_size_t size)
{
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
switch (cmd)
{
case XP_AWK_IO_OPEN:
{
FILE* handle;
const xp_char_t* mode;
if (opt == XP_AWK_IO_PIPE_READ)
mode = XP_T("r");
else if (opt == XP_AWK_IO_PIPE_WRITE)
mode = XP_T("w");
else return -1; /* TODO: any way to set the error number? */
handle = _tpopen (epa->name, mode);
if (handle == NULL) return -1;
epa->handle = (void*)handle;
return 0;
}
case XP_AWK_IO_CLOSE:
{
xp_printf (XP_TEXT("closing %s of type %d\n"), epa->name, epa->type);
fclose ((FILE*)epa->handle);
epa->handle = NULL;
return 0;
}
case XP_AWK_IO_READ:
{
if (_fgetts (data, size, (FILE*)epa->handle) == XP_NULL)
return 0;
return xp_strlen(data);
}
case XP_AWK_IO_WRITE:
{
/*
if (_fputts (data, size, (FILE*)epa->handle) == XP_NULL)
return 0;
return size;
*/
return -1;
}
case XP_AWK_IO_NEXT:
{
return -1;
}
@ -134,74 +187,33 @@ static xp_ssize_t process_data (
return -1;
}
static xp_ssize_t process_extio_pipe (
int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
switch (cmd)
{
case XP_AWK_INPUT_OPEN:
{
FILE* handle;
handle = _tpopen (epa->name, XP_T("r"));
if (handle == NULL) return -1;
epa->handle = (void*)handle;
return 0;
}
case XP_AWK_INPUT_CLOSE:
{
xp_printf (XP_TEXT("closing %s of type %d\n"), epa->name, epa->type);
fclose ((FILE*)epa->handle);
epa->handle = NULL;
return 0;
}
case XP_AWK_INPUT_DATA:
{
if (_fgetts (data, size, epa->handle) == XP_NULL)
return 0;
return xp_strlen(data);
}
case XP_AWK_INPUT_NEXT:
{
return -1;
}
case XP_AWK_OUTPUT_OPEN:
case XP_AWK_OUTPUT_CLOSE:
case XP_AWK_OUTPUT_DATA:
case XP_AWK_OUTPUT_NEXT:
{
return -1;
}
default:
{
return -1;
}
}
}
static xp_ssize_t process_extio_file (
int cmd, void* arg, xp_char_t* data, xp_size_t size)
int cmd, int opt, void* arg, xp_char_t* data, xp_size_t size)
{
xp_awk_extio_t* epa = (xp_awk_extio_t*)arg;
switch (cmd)
{
case XP_AWK_INPUT_OPEN:
case XP_AWK_IO_OPEN:
{
FILE* handle;
handle = _tfopen (epa->name, XP_T("r"));
const xp_char_t* mode;
if (opt == XP_AWK_IO_FILE_READ)
mode = XP_T("r");
else if (opt == XP_AWK_IO_FILE_WRITE)
mode = XP_T("w");
else if (opt == XP_AWK_IO_FILE_APPEND)
mode = XP_T("a");
else return -1; /* TODO: any way to set the error number? */
handle = _tfopen (epa->name, mode);
if (handle == NULL) return -1;
epa->handle = (void*)handle;
return 0;
}
case XP_AWK_INPUT_CLOSE:
case XP_AWK_IO_CLOSE:
{
xp_printf (XP_TEXT("closing %s of type %d\n"), epa->name, epa->type);
fclose ((FILE*)epa->handle);
@ -209,32 +221,31 @@ xp_printf (XP_TEXT("closing %s of type %d\n"), epa->name, epa->type);
return 0;
}
case XP_AWK_INPUT_DATA:
case XP_AWK_IO_READ:
{
if (_fgetts (data, size, epa->handle) == XP_NULL)
if (_fgetts (data, size, (FILE*)epa->handle) == XP_NULL)
return 0;
return xp_strlen(data);
}
case XP_AWK_INPUT_NEXT:
case XP_AWK_IO_WRITE:
{
/*
if (_fputts (data, size, (FILE*)epa->handle) == XP_NULL)
return 0;
return size;
*/
return -1;
}
case XP_AWK_IO_NEXT:
{
return -1;
}
case XP_AWK_OUTPUT_OPEN:
case XP_AWK_OUTPUT_CLOSE:
case XP_AWK_OUTPUT_DATA:
case XP_AWK_OUTPUT_NEXT:
{
return -1;
}
default:
{
return -1;
}
}
return -1;
}