From db07f6b70405398e653ecb9cc0cb516e65b8f541 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 18 Sep 2005 13:06:43 +0000 Subject: [PATCH] *** empty log message *** --- ase/lsp/init.c | 20 ++++++++++++------- ase/lsp/lsp.h | 19 ++++++++++++------ ase/lsp/print.c | 5 +++-- ase/lsp/read.c | 47 +++++++++++++++++++++++---------------------- ase/test/lsp/lisp.c | 40 +++++++++++++++----------------------- 5 files changed, 69 insertions(+), 62 deletions(-) diff --git a/ase/lsp/init.c b/ase/lsp/init.c index 7c9451aa..0187da51 100644 --- a/ase/lsp/init.c +++ b/ase/lsp/init.c @@ -1,5 +1,5 @@ /* - * $Id: init.c,v 1.2 2005-09-18 12:20:43 bacon Exp $ + * $Id: init.c,v 1.3 2005-09-18 13:06:43 bacon Exp $ */ #include @@ -28,6 +28,8 @@ xp_lsp_t* xp_lsp_open (xp_lsp_t* lsp, lsp->curc = XP_CHAR_EOF; lsp->input_func = XP_NULL; lsp->output_func = XP_NULL; + lsp->input_arg = XP_NULL; + lsp->output_arg = XP_NULL; lsp->mem = xp_lsp_mem_new (mem_ubound, mem_ubound_inc); if (lsp->mem == XP_NULL) { @@ -70,55 +72,59 @@ int xp_lsp_error (xp_lsp_t* lsp, xp_char_t* buf, xp_size_t size) return lsp->errnum; } -int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input) +int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input, void* arg) { if (xp_lsp_detach_input(lsp) == -1) return -1; xp_assert (lsp->input_func == XP_NULL); - if (input(lsp, XP_LSP_IO_OPEN, XP_NULL) == -1) { + if (input(XP_LSP_IO_OPEN, arg, XP_NULL, 0) == -1) { /* TODO: set error number */ return -1; } lsp->input_func = input; + lsp->input_arg = arg; return 0; } int xp_lsp_detach_input (xp_lsp_t* lsp) { if (lsp->input_func != XP_NULL) { - if (lsp->input_func(lsp, XP_LSP_IO_CLOSE, XP_NULL) == -1) { + if (lsp->input_func(XP_LSP_IO_CLOSE, lsp->input_arg, XP_NULL, 0) == -1) { /* TODO: set error number */ return -1; } lsp->input_func = XP_NULL; + lsp->input_arg = XP_NULL; } return 0; } -int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output) +int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output, void* arg) { if (xp_lsp_detach_output(lsp) == -1) return -1; xp_assert (lsp->output_func == XP_NULL); - if (output(lsp, XP_LSP_IO_OPEN, XP_NULL) == -1) { + if (output(XP_LSP_IO_OPEN, arg, XP_NULL, 0) == -1) { /* TODO: set error number */ return -1; } lsp->output_func = output; + lsp->output_arg = arg; return 0; } int xp_lsp_detach_output (xp_lsp_t* lsp) { if (lsp->output_func != XP_NULL) { - if (lsp->output_func(lsp, XP_LSP_IO_CLOSE, XP_NULL) == -1) { + if (lsp->output_func(XP_LSP_IO_CLOSE, lsp->output_arg, XP_NULL, 0) == -1) { /* TODO: set error number */ return -1; } lsp->output_func = XP_NULL; + lsp->output_arg = XP_NULL; } return 0; diff --git a/ase/lsp/lsp.h b/ase/lsp/lsp.h index cb859091..7e3e526d 100644 --- a/ase/lsp/lsp.h +++ b/ase/lsp/lsp.h @@ -1,5 +1,5 @@ /* - * $Id: lsp.h,v 1.9 2005-09-18 12:20:43 bacon Exp $ + * $Id: lsp.h,v 1.10 2005-09-18 13:06:43 bacon Exp $ */ #ifndef _XP_LSP_LSP_H_ @@ -48,13 +48,18 @@ enum */ typedef struct xp_lsp_t xp_lsp_t; -typedef int (*xp_lsp_io_t) (xp_lsp_t* lsp, int cmd, void* arg); +/* + * TYPEDEF: xp_lsp_io_t + * Defines an IO handler + */ +typedef xp_ssize_t (*xp_lsp_io_t) ( + int cmd, void* arg, xp_char_t* data, xp_size_t count); + enum { XP_LSP_IO_OPEN, XP_LSP_IO_CLOSE, - XP_LSP_IO_CHAR, - XP_LSP_IO_STR + XP_LSP_IO_DATA }; struct xp_lsp_t @@ -74,6 +79,8 @@ struct xp_lsp_t /* io functions */ xp_lsp_io_t input_func; xp_lsp_io_t output_func; + void* input_arg; + void* output_arg; /* memory manager */ xp_lsp_mem_t* mem; @@ -113,7 +120,7 @@ int xp_lsp_error (xp_lsp_t* lsp, xp_char_t* buf, xp_size_t size); /* * FUNCTION: xp_lsp_attach_input */ -int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input); +int xp_lsp_attach_input (xp_lsp_t* lsp, xp_lsp_io_t input, void* arg); /* * FUNCTION: xp_lsp_detach_input @@ -123,7 +130,7 @@ int xp_lsp_detach_input (xp_lsp_t* lsp); /* * FUNCTION: xp_lsp_attach_output */ -int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output); +int xp_lsp_attach_output (xp_lsp_t* lsp, xp_lsp_io_t output, void* arg); /* * FUNCTION: xp_lsp_detach_output diff --git a/ase/lsp/print.c b/ase/lsp/print.c index efb10be8..29fa25ae 100644 --- a/ase/lsp/print.c +++ b/ase/lsp/print.c @@ -1,9 +1,10 @@ /* - * $Id: print.c,v 1.7 2005-09-18 12:20:43 bacon Exp $ + * $Id: print.c,v 1.8 2005-09-18 13:06:43 bacon Exp $ */ #include #include +#include void xp_lsp_print_debug (xp_lsp_obj_t* obj) { @@ -60,7 +61,7 @@ void xp_lsp_print_debug (xp_lsp_obj_t* obj) #define OUTPUT_STR(lsp,str) \ do { \ - if (lsp->output_func(lsp, XP_LSP_IO_STR, (void*)str) == -1) { \ + if (lsp->output_func(XP_LSP_IO_DATA, lsp->output_arg, (void*)str, xp_strlen(str)) == -1) { \ lsp->errnum = XP_LSP_ERR_OUTPUT; \ return -1; \ } \ diff --git a/ase/lsp/read.c b/ase/lsp/read.c index d638e8dc..047ee1e9 100644 --- a/ase/lsp/read.c +++ b/ase/lsp/read.c @@ -1,5 +1,5 @@ /* - * $Id: read.c,v 1.12 2005-09-18 12:20:43 bacon Exp $ + * $Id: read.c,v 1.13 2005-09-18 13:06:43 bacon Exp $ */ #include @@ -48,26 +48,16 @@ #define TOKEN_UNTERM_STRING 51 #define NEXT_CHAR(lsp) \ - do { \ - if (lsp->input_func == XP_NULL) { \ - lsp->errnum = XP_LSP_ERR_INPUT_NOT_ATTACHED; \ - return -1; \ - } \ - else if (lsp->input_func(lsp, XP_LSP_IO_CHAR, XP_NULL) == -1) { \ - lsp->errnum = XP_LSP_ERR_INPUT; \ - return -1; \ - } \ - } while (0) + do { if (read_char(lsp) == -1) return -1;} while (0) #define NEXT_TOKEN(lsp) \ - do { \ - if (read_token(lsp) == -1) return XP_NULL; \ - } while (0) + do { if (read_token(lsp) == -1) return XP_NULL; } while (0) static xp_lsp_obj_t* read_obj (xp_lsp_t* lsp); static xp_lsp_obj_t* read_list (xp_lsp_t* lsp); static xp_lsp_obj_t* read_quote (xp_lsp_t* lsp); +static int read_char (xp_lsp_t* lsp); static int read_token (xp_lsp_t* lsp); static int read_number (xp_lsp_t* lsp, int negative); static int read_ident (xp_lsp_t* lsp); @@ -75,15 +65,7 @@ static int read_string (xp_lsp_t* lsp); xp_lsp_obj_t* xp_lsp_read (xp_lsp_t* lsp) { - /*NEXT_CHAR (lsp);*/ - if (lsp->input_func == XP_NULL) { - lsp->errnum = XP_LSP_ERR_INPUT_NOT_ATTACHED; - return XP_NULL; - } - else if (lsp->input_func(lsp, XP_LSP_IO_CHAR, XP_NULL) == -1) { - lsp->errnum = XP_LSP_ERR_INPUT; - return XP_NULL; - } + if (read_char(lsp) == -1) return XP_NULL; lsp->errnum = XP_LSP_ERR_NONE; NEXT_TOKEN (lsp); @@ -239,6 +221,25 @@ static xp_lsp_obj_t* read_quote (xp_lsp_t* lsp) return cons; } +static int read_char (xp_lsp_t* lsp) +{ + xp_ssize_t n; + + if (lsp->input_func == XP_NULL) { + lsp->errnum = XP_LSP_ERR_INPUT_NOT_ATTACHED; + return -1; + } + + n = lsp->input_func(XP_LSP_IO_DATA, lsp->input_arg, &lsp->curc, 1); + if (n == -1) { + lsp->errnum = XP_LSP_ERR_INPUT; + return -1; + } + + if (n == 0) lsp->curc = XP_CHAR_EOF; + return 0; +} + static int read_token (xp_lsp_t* lsp) { xp_assert (lsp->input_func != XP_NULL); diff --git a/ase/test/lsp/lisp.c b/ase/test/lsp/lisp.c index 25cb9c65..fea792b5 100644 --- a/ase/test/lsp/lisp.c +++ b/ase/test/lsp/lisp.c @@ -3,36 +3,33 @@ #include #include #include +#include #ifdef __linux #include #endif -static int get_char (xp_lsp_t* lsp, int cmd, void* arg) +static xp_ssize_t get_input (int cmd, void* arg, xp_char_t* data, xp_size_t size) { - xp_cint_t c; + xp_ssize_t n; switch (cmd) { case XP_LSP_IO_OPEN: case XP_LSP_IO_CLOSE: return 0; - case XP_LSP_IO_CHAR: - c = xp_fgetc (xp_stdin); - if (c == XP_CHAR_EOF) { - if (xp_ferror(xp_stdin)) return -1; - } - - break; - - case XP_LSP_IO_STR: - return -1; + case XP_LSP_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 0; + return -1; } -static int put_char (xp_lsp_t* lsp, int cmd, void* arg) +static xp_ssize_t put_output (int cmd, void* arg, xp_char_t* data, xp_size_t size) { switch (cmd) { @@ -40,16 +37,11 @@ static int put_char (xp_lsp_t* lsp, int cmd, void* arg) case XP_LSP_IO_CLOSE: return 0; - case XP_LSP_IO_CHAR: - xp_fputc (*(xp_char_t*)arg, xp_stdout); - break; - - case XP_LSP_IO_STR: - xp_fputs ((xp_char_t*)arg, xp_stdout); - break; + case XP_LSP_IO_DATA: + return xp_sio_putsx (xp_sio_out, data, size); } - return 0; + return -1; } @@ -159,8 +151,8 @@ int xp_main (int argc, xp_char_t* argv[]) xp_printf (XP_TEXT("LSP 0.0001\n")); - xp_lsp_attach_input (lsp, get_char); - xp_lsp_attach_output (lsp, put_char); + xp_lsp_attach_input (lsp, get_input, XP_NULL); + xp_lsp_attach_output (lsp, put_output, XP_NULL); for (;;) { xp_printf (XP_TEXT("%s> "), argv[0]);