*** empty log message ***

This commit is contained in:
hyung-hwan 2005-11-14 15:23:54 +00:00
parent 700ce0450f
commit 7d1d716594
9 changed files with 307 additions and 408 deletions

View File

@ -1,4 +1,4 @@
SRCS = awk.c
SRCS = awk.c parse.c
OBJS = $(SRCS:.c=.obj)
OUT = xpawk.lib

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.c,v 1.2 2005-11-07 16:02:44 bacon Exp $
* $Id: awk.c,v 1.3 2005-11-14 15:23:53 bacon Exp $
*/
#include <xp/awk/awk.h>
@ -15,7 +15,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
}
else awk->__malloced = xp_false;
if (xp_str_open(&awk->lex.token, 128) == XP_NULL) {
if (xp_str_open(&awk->token.name, 128) == XP_NULL) {
if (awk->__malloced) xp_free (awk);
return XP_NULL;
}
@ -30,8 +30,8 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
awk->input_arg = XP_NULL;
awk->output_arg = XP_NULL;
awk->lex.ungotc_count = 0;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
return awk;
}
@ -39,6 +39,7 @@ xp_awk_t* xp_awk_open (xp_awk_t* awk)
int xp_awk_close (xp_awk_t* awk)
{
if (xp_awk_detach_source(awk) == -1) return -1;
xp_str_close (&awk->token.name);
if (awk->__malloced) xp_free (awk);
return 0;
}
@ -56,7 +57,8 @@ int xp_awk_attach_source (xp_awk_t* awk, xp_awk_io_t source, void* arg)
awk->source_func = source;
awk->source_arg = arg;
awk->curc = XP_CHAR_EOF;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
return 0;
}
@ -70,7 +72,8 @@ int xp_awk_detach_source (xp_awk_t* awk)
awk->source_func = XP_NULL;
awk->source_arg = XP_NULL;
awk->curc = XP_CHAR_EOF;
awk->lex.curc = XP_CHAR_EOF;
awk->lex.ungotc_count = 0;
}
return 0;

View File

@ -1,5 +1,5 @@
/*
* $Id: awk.h,v 1.3 2005-11-07 16:02:44 bacon Exp $
* $Id: awk.h,v 1.4 2005-11-14 15:23:53 bacon Exp $
*/
#ifndef _XP_AWK_AWK_H_
@ -7,12 +7,17 @@
#include <xp/types.h>
#include <xp/macros.h>
#include <xp/bas/string.h>
enum
{
XP_AWK_ENOERR,
XP_AWK_ENOMEM, /* out of memory */
XP_AWK_ESRCOP,
XP_AWK_ESRCCL
XP_AWK_ESRCCL,
XP_AWK_ESRCDT, /* error in reading source */
XP_AWK_ELXCHR, /* lexer came accross an wrong character */
XP_AWK_ELXUNG /* lexer failed to unget a character */
};
/*
@ -49,10 +54,14 @@ struct xp_awk_t
xp_cint_t curc;
xp_cint_t ungotc[5];
xp_size_t ungotc_count;
xp_str_t token;
int ttype;
} lex;
/* token */
struct {
int type;
xp_str_t name;
} token;
/* housekeeping */
int errnum;
xp_bool_t __malloced;

View File

@ -1,184 +0,0 @@
/*
* $Id: lex.c,v 1.2 2005-11-07 16:02:44 bacon Exp $
*/
#include <xp/awk/lex.h>
#include <xp/bas/memory.h>
#include <xp/bas/ctype.h>
static int __get_char (xp_awk_t* awk);
static int __unget_char (xp_awk_t* awk, xp_cint_t c);
static int __skip_spaces (xp_awk_t* awk);
static int __skip_comment (xp_awk_t* awk);
#define GET_CHAR(awk) \
do { if (__get_char(awk) == -1) return -1; } while(0)
#define GET_CHAR_TO(awk, c) \
do { if (__get_char(awk) == -1) return -1; c = (awk)->lex.curc; } while(0)
#define SET_TOKEN_TYPE(awk,code) ((awk)->lex.type = code)
#define ADD_TOKEN_STR(awk,str) \
do { if (xp_str_cat(&(awk)->lex.token, (str)) == -1) return -1; } while (0)
int xp_awk_lex (xp_awk_t* awk)
{
xp_cint_t c;
int n;
do {
if (__skip_spaces(awk) == -1) return -1;
if ((n = __skip_comment(awk)) == -1) return -1;
} while (n == 1);
xp_str_clear (&awk->lex.token);
c = awk->lex.curc;
if (c == XP_CHAR_EOF) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_END);
}
else if (xp_isdigit(c)) {
}
else if (xp_isalpha(c) || c == XP_CHAR('_')) {
}
else if (c == XP_CHAR('\"')) {
}
else if (c == XP_CHAR('=')) {
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_EQ);
ADD_TOKEN_STR(lex, XP_TEXT("=="));
GET_CHAR_TO (lex, c);
}
else {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_ASSIGN);
ADD_TOKEN_STR(lex, XP_TEXT("="));
}
}
else if (c == XP_CHAR('!')) {
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_NE);
ADD_TOKEN_STR(lex, XP_TEXT("!="));
GET_CHAR_TO (lex, c);
}
else {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_NOT);
ADD_TOKEN_STR(lex, XP_TEXT("!"));
}
}
else if (c == XP_CHAR('+')) {
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('+')) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_INC);
ADD_TOKEN_STR(lex, XP_TEXT("++"));
GET_CHAR_TO (lex, c);
}
else if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_PLUS_ASSIGN);
ADD_TOKEN_STR(lex, XP_TEXT("+="));
GET_CHAR_TO (lex, c);
}
else if (xp_isdigit(c)) {
// read_number (XP_CHAR('+'));
}
else {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_PLUS);
ADD_TOKEN_STR(lex, XP_TEXT("+"));
}
}
else if (c == XP_CHAR('-')) {
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('-')) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_DEC);
ADD_TOKEN_STR(lex, XP_TEXT("--"));
GET_CHAR_TO (lex, c);
}
else if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_MINUS_ASSIGN);
ADD_TOKEN_STR(lex, XP_TEXT("-="));
GET_CHAR_TO (lex, c);
}
else if (xp_isdigit(c)) {
// read_number (XP_CHAR('-'));
}
else {
SET_TOKEN_TYPE (lex, XP_SCE_TOKEN_MINUS);
ADD_TOKEN_STR(lex, XP_TEXT("-"));
}
}
else {
/* set the error into awk directly though it
* might look a bit awkard */
lex->awk->error_code = XP_SCE_ERROR_WRONG_CHAR;
return -1;
}
return 0;
}
static int __get_char (xp_awk_t* awk)
{
if (awk->ungotc_count > 0) {
awk->curc = awk->ungotc[--awk->ungotc_count];
return 0;
}
if (awk->source_func(XP_AWK_IO_DATA,
awk->source_arg, &awk->curc, 1) == -1) {
awk->error_code = XP_SCE_ESRCDT;
return -1;
}
return 0;
}
static int __unget_char (xp_awk_t* awk, xp_cint_t c)
{
if (awk->ungotc_count >= xp_countof(awk->ungotc)) {
awk->error_code = XP_SCE_EUNGET;
return -1;
}
lex->ungotc[awk->ungotc_count++] = c;
return 0;
}
static int __skip_spaces (xp_awk_t* awk)
{
xp_cint_t c = awk->lex.curc;
while (xp_isspace(c)) GET_CHAR_TO (awk, c);
return 0;
}
static int __skip_comment (xp_awk_t* awk)
{
xp_cint_t c = awk->curc;
if (c != XP_CHAR('/')) return 0;
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('/')) {
do {
GET_CHAR_TO (lex, c);
} while (c != '\n' && c != XP_CHAR_EOF);
GET_CHAR (lex);
return 1;
}
else if (c == XP_CHAR('*')) {
do {
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('*')) {
GET_CHAR_TO (lex, c);
if (c == XP_CHAR('/')) {
GET_CHAR_TO (lex, c);
break;
}
}
} while (0);
return 1;
}
if (__unget_char(lex, c) == -1) return -1;
return 0;
}

View File

@ -1,32 +0,0 @@
/*
* $Id: lex.h,v 1.2 2005-11-07 16:02:44 bacon Exp $
*/
#ifndef _XP_AWK_LEX_H_
#define _XP_AWK_LEX_H_
#include <xp/awk/awk.h>
/*
struct xp_awk_lex_t
{
xp_awk_t* awk;
xp_str_t token;
xp_cint_t curc;
xp_cint_t ungotc[5];
xp_size_t ungotc_count;
xp_bool_t __malloced;
};
*/
#ifdef __cplusplus
extern "C" {
#endif
int xp_awk_lex (xp_awk_t* awk);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,11 +1,239 @@
/*
* $Id: parse.c,v 1.2 2005-11-07 16:02:44 bacon Exp $
* $Id: parse.c,v 1.3 2005-11-14 15:23:53 bacon Exp $
*/
#include <xp/awk/awk.h>
#include <xp/bas/memory.h>
#include <xp/bas/ctype.h>
enum
{
TOKEN_EOF,
TOKEN_ASSIGN,
TOKEN_EQ,
TOKEN_NE,
TOKEN_NOT,
TOKEN_PLUS,
TOKEN_PLUS_PLUS,
TOKEN_PLUS_ASSIGN,
TOKEN_MINUS,
TOKEN_MINUS_MINUS,
TOKEN_MINUS_ASSIGN,
TOKEN_IDENT,
TOEKN_BEGIN,
TOKEN_END,
};
static int __parse (xp_awk_t* awk);
static int __get_token (xp_awk_t* awk);
static int __get_char (xp_awk_t* awk);
static int __unget_char (xp_awk_t* awk, xp_cint_t c);
static int __skip_spaces (xp_awk_t* awk);
static int __skip_comment (xp_awk_t* awk);
#define GET_CHAR(awk) \
do { if (__get_char(awk) == -1) return -1; } while(0)
#define GET_CHAR_TO(awk, c) do { \
if (__get_char(awk) == -1) return -1; \
c = (awk)->lex.curc; \
} while(0)
#define SET_TOKEN_TYPE(awk,code) ((awk)->token.type = code)
#define ADD_TOKEN_CHAR(awk,c) do { \
if (xp_str_catc(&(awk)->token.name,(c)) == -1) { \
(awk)->errnum = XP_AWK_ENOMEM; return -1; \
} \
} while (0)
#define ADD_TOKEN_STR(awk,str) do { \
if (xp_str_cat(&(awk)->token.name,(str)) == -1) { \
(awk)->errnum = XP_AWK_ENOMEM; return -1; \
} while (0)
int xp_awk_parse (xp_awk_t* awk)
{
return -1;
GET_CHAR (awk);
return __parse (awk);
}
static int __parse (xp_awk_t* awk)
{
if (awk->token.type == TOKEN_EOF) return 0;
return -1;
}
static int __get_token (xp_awk_t* awk)
{
xp_cint_t c;
int n;
do {
if (__skip_spaces(awk) == -1) return -1;
if ((n = __skip_comment(awk)) == -1) return -1;
} while (n == 1);
xp_str_clear (&awk->token.name);
c = awk->lex.curc;
if (c == XP_CHAR_EOF) {
SET_TOKEN_TYPE (awk, TOKEN_EOF);
}
else if (xp_isdigit(c)) {
}
else if (xp_isalpha(c) || c == XP_CHAR('_')) {
}
else if (c == XP_CHAR('\"')) {
}
else if (c == XP_CHAR('=')) {
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (awk, TOKEN_EQ);
ADD_TOKEN_STR (awk, XP_TEXT("=="));
GET_CHAR_TO (awk, c);
}
else {
SET_TOKEN_TYPE (awk, TOKEN_ASSIGN);
ADD_TOKEN_STR (awk, XP_TEXT("="));
}
}
else if (c == XP_CHAR('!')) {
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (awk, TOKEN_NE);
ADD_TOKEN_STR (awk, XP_TEXT("!="));
GET_CHAR_TO (awk, c);
}
else {
SET_TOKEN_TYPE (awk, TOKEN_NOT);
ADD_TOKEN_STR (awk, XP_TEXT("!"));
}
}
else if (c == XP_CHAR('+')) {
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('+')) {
SET_TOKEN_TYPE (awk, TOKEN_PLUS_PLUS);
ADD_TOKEN_STR (awk, XP_TEXT("++"));
GET_CHAR_TO (awk, c);
}
else if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (awk, TOKEN_PLUS_ASSIGN);
ADD_TOKEN_STR (awk, XP_TEXT("+="));
GET_CHAR_TO (awk, c);
}
else if (xp_isdigit(c)) {
// read_number (XP_CHAR('+'));
}
else {
SET_TOKEN_TYPE (awk, TOKEN_PLUS);
ADD_TOKEN_STR (awk, XP_TEXT("+"));
}
}
else if (c == XP_CHAR('-')) {
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('-')) {
SET_TOKEN_TYPE (awk, TOKEN_MINUS_MINUS);
ADD_TOKEN_STR (awk, XP_TEXT("--"));
GET_CHAR_TO (awk, c);
}
else if (c == XP_CHAR('=')) {
SET_TOKEN_TYPE (awk, TOKEN_MINUS_ASSIGN);
ADD_TOKEN_STR (awk, XP_TEXT("-="));
GET_CHAR_TO (awk, c);
}
else if (xp_isdigit(c)) {
// read_number (XP_CHAR('-'));
}
else {
SET_TOKEN_TYPE (awk, TOKEN_MINUS);
ADD_TOKEN_STR (awk, XP_TEXT("-"));
}
}
else if (__is_ident_char(c)) {
SET_TOKEN_TYPE (awk, TOKEN_IDENT);
/*
do {
ADD_TOKEN_CHAR (awk, c);
GET_CHAR_TO (awk, c);
} while (__is_ident_char(c));
*/
}
else {
awk->errnum = XP_AWK_ELXCHR;
return -1;
}
return 0;
}
static int __get_char (xp_awk_t* awk)
{
if (awk->lex.ungotc_count > 0) {
awk->lex.curc = awk->lex.ungotc[--awk->lex.ungotc_count];
return 0;
}
if (awk->source_func(XP_AWK_IO_DATA,
awk->source_arg, &awk->lex.curc, 1) == -1) {
awk->errnum = XP_AWK_ESRCDT;
return -1;
}
return 0;
}
static int __unget_char (xp_awk_t* awk, xp_cint_t c)
{
if (awk->lex.ungotc_count >= xp_countof(awk->lex.ungotc)) {
awk->errnum = XP_AWK_ELXUNG;
return -1;
}
awk->lex.ungotc[awk->lex.ungotc_count++] = c;
return 0;
}
static int __skip_spaces (xp_awk_t* awk)
{
xp_cint_t c = awk->lex.curc;
while (xp_isspace(c)) GET_CHAR_TO (awk, c);
return 0;
}
static int __skip_comment (xp_awk_t* awk)
{
xp_cint_t c = awk->lex.curc;
if (c != XP_CHAR('/')) return 0;
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('/')) {
do {
GET_CHAR_TO (awk, c);
} while (c != '\n' && c != XP_CHAR_EOF);
GET_CHAR (awk);
return 1;
}
else if (c == XP_CHAR('*')) {
do {
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('*')) {
GET_CHAR_TO (awk, c);
if (c == XP_CHAR('/')) {
GET_CHAR_TO (awk, c);
break;
}
}
} while (0);
return 1;
}
if (__unget_char(awk, c) == -1) return -1;
return 0;
}

View File

@ -1,30 +0,0 @@
/*
* $Id: sce.c,v 1.2 2005-10-03 04:13:12 bacon Exp $
*/
#include <xp/sce/sce.h>
#include <xp/sce/mem.h>
xp_sce_t* xp_sce_open (xp_sce_t* sce, xp_word_t capacity)
{
if (sce == XP_NULL) {
sce = (xp_sce_t*)xp_malloc (xp_sizeof(sce));
if (sce == XP_NULL) return XP_NULL;
sce->__malloced = xp_true;
}
else sce->__malloced = xp_false;
if (xp_sce_mem_open (&sce->mem, capacity) == XP_NULL) {
if (sce->__malloced) xp_free (sce);
return XP_NULL;
}
return sce;
}
void xp_sce_close (xp_sce_t* sce)
{
xp_sce_mem_close (&sce->mem);
if (sce->__malloced) xp_free (sce);
}

View File

@ -1,150 +0,0 @@
/*
* $Id: sce.h,v 1.3 2005-10-03 04:13:12 bacon Exp $
*/
#ifndef _XP_SCE_SCE_H_
#define _XP_SCE_SCE_H_
#include <xp/types.h>
#include <xp/macros.h>
typedef struct xp_sce_objhdr_t xp_sce_objhdr_t;
typedef struct xp_sce_obj_t xp_sce_obj_t;
typedef struct xp_sce_word_obj_t xp_sce_word_obj_t;
typedef struct xp_sce_byte_obj_t xp_sce_byte_obj_t;
typedef struct xp_sce_char_obj_t xp_sce_char_obj_t;
typedef struct xp_sce_int_obj_t xp_sce_int_obj_t;
typedef struct xp_sce_real_obj_t xp_sce_real_obj_t;
typedef struct xp_sce_mem_t xp_sce_mem_t;
typedef struct xp_sce_t xp_sce_t;
/* common obj structure */
struct xp_sce_objhdr_t
{
/*
* lower 4 bytes: atomic types...
* CHAR
* INT
* REAL
*
* the rest of the bytes: number of elements in the array.
* this value will be 1 for atomic types
*
* // array types
* ARRAY OF ATTOMIC TYPES
*/
xp_word_t access;
};
struct xp_sce_obj_t
{
xp_sce_objhdr_t hdr;
};
struct xp_sce_word_obj_t
{
xp_sce_objhdr_t hdr;
xp_word_t data[1];
};
struct xp_sce_byte_obj_t
{
xp_sce_objhdr_t hdr;
xp_byte_t data[1];
};
struct xp_sce_char_obj_t
{
xp_sce_objhdr_t hdr;
xp_char_t data[1];
};
struct xp_sce_int_obj_t
{
xp_sce_objhdr_t hdr;
xp_long_t data[1];
};
struct xp_sce_real_obj_t
{
xp_sce_objhdr_t hdr;
xp_real_t data[1];
};
struct xp_sce_mem_t
{
xp_word_t capacity;
xp_sce_obj_t** slots;
xp_sce_obj_t** free;
xp_bool_t __malloced;
};
struct xp_sce_t
{
xp_sce_mem_t mem;
xp_bool_t __malloced;
};
#define XP_SCE_IS_SMALLINT(x) (((x) & 0x01) == 0x01)
#define XP_SCE_TO_SMALLINT(x) (((x) << 1) | 0x01)
#define XP_SCE_FROM_SMALLINT(x) ((x) >> 1)
#define XP_SCE_IS_OINDEX(x) (((x) & 0x01) == 0x00)
#define XP_SCE_TO_OINDEX(x) (((x) << 1) | 0x00)
#define XP_SCE_FROM_OINDEX(x) ((x) >> 1)
#define XP_SCE_NIL XP_SCE_TO_OINDEX(0)
#define XP_SCE_OBJ(sce,idx) (((sce)->mem).slots[XP_SCE_FROM_OINDEX(idx)])
#define XP_SCE_ACCESS(sce,idx) (XP_SCE_OBJ(sce,(idx))->hdr.access)
#define XP_SCE_DATA(sce,idx) ((void*)(XP_SCE_OBJ(sce,idx) + 1))
#define XP_SCE_TYPE(sce,idx) (XP_SCE_ACCESS(sce,idx) & 0x0F)
#define XP_SCE_SIZE(sce,idx) (XP_SCE_ACCESS(sce,idx) >> 0x04)
#define XP_SCE_WORD_INDEXED (0x00)
#define XP_SCE_BYTE_INDEXED (0x01)
#define XP_SCE_CHAR_INDEXED (0x02)
#define XP_SCE_INT_INDEXED (0x03)
#define XP_SCE_REAL_INDEXED (0x04)
#define XP_SCE_IS_WORD_OBJ(sce,idx) \
(XP_SCE_TYPE(sce,idx) == XP_SCE_WORD_INDEXED)
#define XP_SCE_IS_BYTE_OBJ(sce,idx) \
(XP_SCE_TYPE(sce,idx) == XP_SCE_BYTE_INDEXED)
#define XP_SCE_IS_CHAR_OBJ(sce,idx) \
(XP_SCE_TYPE(sce,idx) == XP_SCE_CHAR_INDEXED)
#define XP_SCE_IS_INT_OBJ(sce,idx) \
(XP_SCE_TYPE(sce,idx) == XP_SCE_INT_INDEXED)
#define XP_SCE_IS_REAL_OBJ(sce,idx) \
(XP_SCE_TYPE(sce,idx) == XP_SCE_REAL_INDEXED)
#define XP_SCE_WORD_OBJ(sce,idx) \
((xp_sce_word_obj_t*)XP_SCE_OBJ(sce,idx))
#define XP_SCE_BYTE_OBJ(sce,idx) \
((xp_sce_byte_obj_t*)XP_SCE_OBJ(sce,idx))
#define XP_SCE_CHAR_OBJ(sce,idx) \
((xp_sce_char_obj_t*)XP_SCE_OBJ(sce,idx))
#define XP_SCE_INT_OBJ(sce,idx) \
((xp_sce_int_obj_t*)XP_SCE_OBJ(sce,idx))
#define XP_SCE_REAL_OBJ(sce,idx) \
((xp_sce_real_obj_t*)XP_SCE_OBJ(sce,idx))
#define XP_SCE_WORD_AT(sce,idx,n) (XP_SCE_WORD_OBJ(sce,idx)->data[n])
#define XP_SCE_BYTE_AT(sce,idx,n) (XP_SCE_BYTE_OBJ(sce,idx)->data[n])
#define XP_SCE_CHAR_AT(sce,idx,n) (XP_SCE_CHAR_OBJ(sce,idx)->data[n])
#define XP_SCE_INT_AT(sce,idx,n) (XP_SCE_INT_OBJ(sce,idx)->data[n])
#define XP_SCE_REAL_AT(sce,idx,n) (XP_SCE_REAL_OBJ(sce,idx)->data[n])
#ifdef __cplusplus
extern "C" {
#endif
xp_sce_t* xp_sce_open (xp_sce_t* sce, xp_word_t capacity);
void xp_sce_close (xp_sce_t* sce);
#ifdef __cplusplus
}
#endif
#endif

55
ase/test/awk/awk.c Normal file
View File

@ -0,0 +1,55 @@
#include <xp/awk/awk.h>
#include <xp/bas/stdio.h>
#include <xp/bas/sio.h>
static xp_ssize_t process_source (int cmd, void* arg, xp_char_t* data, xp_size_t size)
{
xp_ssize_t n;
switch (cmd) {
case XP_AWK_IO_OPEN:
case XP_AWK_IO_CLOSE:
return 0;
case XP_AWK_IO_DATA:
if (size < 0) return -1;
n = xp_sio_getc (xp_sio_in, data);
if (n == 0) return 0;
if (n != 1) return -1;
return n;
}
return -1;
}
int xp_main (int argc, xp_char_t* argv[])
{
xp_awk_t awk;
#if 0
if (argc != 2) {
xp_fprintf (xp_stderr, XP_TEXT("Usage: %s file\n"), argv[0]);
return -1;
}
#endif
if (xp_awk_open(&awk) == XP_NULL) {
xp_fprintf (xp_stderr, XP_TEXT("Error: cannot open awk\n"));
return -1;
}
if (xp_awk_attach_source(&awk, process_source, XP_NULL) == -1) {
xp_awk_close (&awk);
xp_fprintf (xp_stderr, XP_TEXT("error: cannot attach source\n"));
return -1;
}
if (xp_awk_parse(&awk) == -1) {
xp_awk_close (&awk);
xp_fprintf (xp_stderr, XP_TEXT("error: cannot parse program\n"));
return -1;
}
xp_awk_close (&awk);
return 0;
}