From 61ff69b9688d059318a3086780faef4924732fbc Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Jun 2015 22:59:22 +0900 Subject: [PATCH 1/4] remove pic->xSTDXX --- extlib/benz/gc.c | 11 ----------- extlib/benz/include/picrin.h | 5 +---- extlib/benz/port.c | 10 +++++++--- extlib/benz/state.c | 10 ---------- 4 files changed, 8 insertions(+), 28 deletions(-) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 5ed749b7..59ea6850 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -617,17 +617,6 @@ gc_mark_phase(pic_state *pic) /* library table */ gc_mark(pic, pic->libs); - /* standard I/O ports */ - if (pic->xSTDIN) { - gc_mark_object(pic, (struct pic_object *)pic->xSTDIN); - } - if (pic->xSTDOUT) { - gc_mark_object(pic, (struct pic_object *)pic->xSTDOUT); - } - if (pic->xSTDERR) { - gc_mark_object(pic, (struct pic_object *)pic->xSTDERR); - } - /* parameter table */ gc_mark(pic, pic->ptable); diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index a063fa68..73504f87 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -143,12 +143,9 @@ typedef struct { size_t arena_size, arena_idx; struct pic_reg *regs; - struct pic_port *xSTDIN, *xSTDOUT, *xSTDERR; - pic_value err; pic_code *iseq; /* for pic_apply_trampoline */ - char *native_stack_start; } pic_state; @@ -257,7 +254,7 @@ pic_value pic_display(pic_state *, pic_value); pic_value pic_fdisplay(pic_state *, pic_value, xFILE *); #if DEBUG -# define pic_debug(pic,obj) pic_fwrite(pic,obj,pic->xSTDERR->file) +# define pic_debug(pic,obj) pic_fwrite(pic,obj,xstderr) # define pic_fdebug(pic,obj,file) pic_fwrite(pic,obj,file) #endif diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 5b04f89b..9100be4a 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -773,9 +773,13 @@ pic_port_flush(pic_state *pic) void pic_init_port(pic_state *pic) { - pic_defvar(pic, "current-input-port", pic_obj_value(pic->xSTDIN), NULL); - pic_defvar(pic, "current-output-port", pic_obj_value(pic->xSTDOUT), NULL); - pic_defvar(pic, "current-error-port", pic_obj_value(pic->xSTDERR), NULL); + struct pic_port *xSTDIN = pic_make_standard_port(pic, xstdin, PIC_PORT_IN); + struct pic_port *xSTDOUT = pic_make_standard_port(pic, xstdout, PIC_PORT_OUT); + struct pic_port *xSTDERR = pic_make_standard_port(pic, xstderr, PIC_PORT_OUT); + + pic_defvar(pic, "current-input-port", pic_obj_value(xSTDIN), NULL); + pic_defvar(pic, "current-output-port", pic_obj_value(xSTDOUT), NULL); + pic_defvar(pic, "current-error-port", pic_obj_value(xSTDERR), NULL); pic_defun(pic, "call-with-port", pic_port_call_with_port); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 6e11f812..64b58b25 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -254,11 +254,6 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) /* raised error object */ pic->err = pic_invalid_value(); - /* standard ports */ - pic->xSTDIN = NULL; - pic->xSTDOUT = NULL; - pic->xSTDERR = NULL; - /* parameter table */ pic->ptable = pic_nil_value(); @@ -372,11 +367,6 @@ pic_open(int argc, char *argv[], char **envp, pic_allocf allocf) /* reader */ pic->reader = pic_reader_open(pic); - /* standard I/O */ - pic->xSTDIN = pic_make_standard_port(pic, xstdin, PIC_PORT_IN); - pic->xSTDOUT = pic_make_standard_port(pic, xstdout, PIC_PORT_OUT); - pic->xSTDERR = pic_make_standard_port(pic, xstderr, PIC_PORT_OUT); - /* parameter table */ pic->ptable = pic_cons(pic, pic_obj_value(pic_make_dict(pic)), pic->ptable); From b4c3e2cc4f652715528be2cde1ad96646c753ac5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Jun 2015 23:15:09 +0900 Subject: [PATCH 2/4] small refactoring. use DEFINE_STANDARD_PORT_ACCESSOR macro to define pic_stdxx --- extlib/benz/include/picrin.h | 4 ++++ extlib/benz/port.c | 41 ++++++++++++------------------------ extlib/benz/vm.c | 6 ++++++ 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 73504f87..d0dedfa5 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -201,6 +201,10 @@ void pic_load_port(pic_state *, struct pic_port *); void pic_load_cstr(pic_state *, const char *); pic_value pic_funcall(pic_state *pic, struct pic_lib *, const char *, pic_list); +pic_value pic_funcall0(pic_state *pic, struct pic_lib *, const char *); +pic_value pic_funcall1(pic_state *pic, struct pic_lib *, const char *, pic_value); +pic_value pic_funcall2(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value); +pic_value pic_funcall3(pic_state *pic, struct pic_lib *, const char *, pic_value, pic_value, pic_value); pic_value pic_ref(pic_state *, struct pic_lib *, const char *); void pic_set(pic_state *, struct pic_lib *, const char *, pic_value); diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 9100be4a..51fc5439 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -14,35 +14,20 @@ pic_eof_object() return v; } -struct pic_port * -pic_stdin(pic_state *pic) -{ - pic_value obj; +#define DEFINE_STANDARD_PORT_ACCESSOR(name, var) \ + struct pic_port * \ + name(pic_state *pic) \ + { \ + pic_value obj; \ + \ + obj = pic_funcall0(pic, pic->PICRIN_BASE, var); \ + \ + return pic_port_ptr(obj); \ + } - obj = pic_funcall(pic, pic->PICRIN_BASE, "current-input-port", pic_nil_value()); - - return pic_port_ptr(obj); -} - -struct pic_port * -pic_stdout(pic_state *pic) -{ - pic_value obj; - - obj = pic_funcall(pic, pic->PICRIN_BASE, "current-output-port", pic_nil_value()); - - return pic_port_ptr(obj); -} - -struct pic_port * -pic_stderr(pic_state *pic) -{ - pic_value obj; - - obj = pic_funcall(pic, pic->PICRIN_BASE, "current-error-port", pic_nil_value()); - - return pic_port_ptr(obj); -} +DEFINE_STANDARD_PORT_ACCESSOR(pic_stdin, "current-input-port") +DEFINE_STANDARD_PORT_ACCESSOR(pic_stdout, "current-output-port") +DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port") struct pic_port * pic_make_standard_port(pic_state *pic, xFILE *file, short dir) diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index 791529d0..2f56467f 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -467,6 +467,12 @@ pic_funcall(pic_state *pic, struct pic_lib *lib, const char *name, pic_list args return pic_apply(pic, pic_proc_ptr(proc), args); } +pic_value +pic_funcall0(pic_state *pic, struct pic_lib *lib, const char *name) +{ + return pic_funcall(pic, lib, name, pic_nil_value()); +} + void pic_defun(pic_state *pic, const char *name, pic_func_t cfunc) { From 0b66447e795c645df8a3386e5f595a91180a0d7f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 18 Jun 2015 23:26:31 +0900 Subject: [PATCH 3/4] remove port->status property --- contrib/05.r7rs/src/file.c | 3 +- contrib/05.r7rs/src/load.c | 3 +- extlib/benz/include/picrin/port.h | 9 +---- extlib/benz/port.c | 66 +++++++++++++------------------ 4 files changed, 32 insertions(+), 49 deletions(-) diff --git a/contrib/05.r7rs/src/file.c b/contrib/05.r7rs/src/file.c index ce9cb1b2..36e50e86 100644 --- a/contrib/05.r7rs/src/file.c +++ b/contrib/05.r7rs/src/file.c @@ -27,8 +27,7 @@ generic_open_file(pic_state *pic, const char *fname, char *mode, short flags) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); port->file = file; - port->flags = flags; - port->status = PIC_PORT_OPEN; + port->flags = flags | PIC_PORT_OPEN; return pic_obj_value(port); } diff --git a/contrib/05.r7rs/src/load.c b/contrib/05.r7rs/src/load.c index c887a1b2..385767d8 100644 --- a/contrib/05.r7rs/src/load.c +++ b/contrib/05.r7rs/src/load.c @@ -17,8 +17,7 @@ pic_load(pic_state *pic, const char *filename) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); port->file = file; - port->flags = PIC_PORT_IN | PIC_PORT_TEXT; - port->status = PIC_PORT_OPEN; + port->flags = PIC_PORT_IN | PIC_PORT_TEXT | PIC_PORT_OPEN; pic_load_port(pic, port); diff --git a/extlib/benz/include/picrin/port.h b/extlib/benz/include/picrin/port.h index 98dcff83..dfb664e6 100644 --- a/extlib/benz/include/picrin/port.h +++ b/extlib/benz/include/picrin/port.h @@ -13,19 +13,14 @@ enum pic_port_flag { PIC_PORT_IN = 1, PIC_PORT_OUT = 2, PIC_PORT_TEXT = 4, - PIC_PORT_BINARY = 8 -}; - -enum pic_port_status { - PIC_PORT_OPEN, - PIC_PORT_CLOSE + PIC_PORT_BINARY = 8, + PIC_PORT_OPEN = 16 }; struct pic_port { PIC_OBJECT_HEADER xFILE *file; int flags; - int status; }; #define pic_port_p(v) (pic_type(v) == PIC_TT_PORT) diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 51fc5439..a562b9f6 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -36,8 +36,7 @@ pic_make_standard_port(pic_state *pic, xFILE *file, short dir) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); port->file = file; - port->flags = dir | PIC_PORT_TEXT; - port->status = PIC_PORT_OPEN; + port->flags = dir | PIC_PORT_TEXT | PIC_PORT_OPEN; return port; } @@ -140,8 +139,7 @@ pic_open_input_string(pic_state *pic, const char *str) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); port->file = string_open(pic, str, strlen(str)); - port->flags = PIC_PORT_IN | PIC_PORT_TEXT; - port->status = PIC_PORT_OPEN; + port->flags = PIC_PORT_IN | PIC_PORT_TEXT | PIC_PORT_OPEN; return port; } @@ -153,8 +151,7 @@ pic_open_output_string(pic_state *pic) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); port->file = string_open(pic, NULL, 0); - port->flags = PIC_PORT_OUT | PIC_PORT_TEXT; - port->status = PIC_PORT_OPEN; + port->flags = PIC_PORT_OUT | PIC_PORT_TEXT | PIC_PORT_OPEN; return port; } @@ -181,7 +178,7 @@ pic_close_port(pic_state *pic, struct pic_port *port) if (xfclose(port->file) == EOF) { pic_errorf(pic, "close-port: failure"); } - port->status = PIC_PORT_CLOSE; + port->flags &= ~PIC_PORT_OPEN; } static pic_value @@ -300,7 +297,7 @@ pic_port_port_open_p(pic_state *pic) pic_get_args(pic, "p", &port); - return pic_bool_value(port->status == PIC_PORT_OPEN); + return pic_bool_value(port->flags & PIC_PORT_OPEN); } static pic_value @@ -315,7 +312,7 @@ pic_port_close_port(pic_state *pic) return pic_undef_value(); } -#define assert_port_profile(port, flgs, stat, caller) do { \ +#define assert_port_profile(port, flgs, caller) do { \ if ((port->flags & (flgs)) != (flgs)) { \ switch (flgs) { \ case PIC_PORT_IN: \ @@ -332,13 +329,8 @@ pic_port_close_port(pic_state *pic) pic_errorf(pic, caller ": expected output/binary port"); \ } \ } \ - if (port->status != stat) { \ - switch (stat) { \ - case PIC_PORT_OPEN: \ - pic_errorf(pic, caller ": expected open port"); \ - case PIC_PORT_CLOSE: \ - pic_errorf(pic, caller ": expected close port"); \ - } \ + if ((port->flags & PIC_PORT_OPEN) == 0) { \ + pic_errorf(pic, caller ": expected open port"); \ } \ } while (0) @@ -374,7 +366,7 @@ pic_port_get_output_string(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "get-output-string"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "get-output-string"); return pic_obj_value(pic_get_output_string(pic, port)); } @@ -389,8 +381,7 @@ pic_port_open_input_blob(pic_state *pic) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); port->file = string_open(pic, (const char *)blob->data, blob->len); - port->flags = PIC_PORT_IN | PIC_PORT_BINARY; - port->status = PIC_PORT_OPEN; + port->flags = PIC_PORT_IN | PIC_PORT_BINARY | PIC_PORT_OPEN; return pic_obj_value(port); } @@ -404,8 +395,7 @@ pic_port_open_output_bytevector(pic_state *pic) port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); port->file = string_open(pic, NULL, 0); - port->flags = PIC_PORT_OUT | PIC_PORT_BINARY; - port->status = PIC_PORT_OPEN; + port->flags = PIC_PORT_OUT | PIC_PORT_BINARY | PIC_PORT_OPEN; return pic_obj_value(port); } @@ -419,7 +409,7 @@ pic_port_get_output_bytevector(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "get-output-bytevector"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "get-output-bytevector"); if (port->file->vtable.write != string_write) { pic_errorf(pic, "get-output-bytevector: port is not made by open-output-bytevector"); @@ -443,7 +433,7 @@ pic_port_read_char(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-char"); if ((c = xfgetc(port->file)) == EOF) { return pic_eof_object(); @@ -461,7 +451,7 @@ pic_port_peek_char(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "peek-char"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "peek-char"); if ((c = xfgetc(port->file)) == EOF) { return pic_eof_object(); @@ -481,7 +471,7 @@ pic_port_read_line(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-line"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-line"); buf = pic_open_output_string(pic); while ((c = xfgetc(port->file)) != EOF && c != '\n') { @@ -502,7 +492,7 @@ pic_port_char_ready_p(pic_state *pic) { struct pic_port *port = pic_stdin(pic); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "char-ready?"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "char-ready?"); pic_get_args(pic, "|p", &port); @@ -518,7 +508,7 @@ pic_port_read_string(pic_state *pic){ pic_get_args(pic, "i|p", &k, &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-stritg"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_TEXT, "read-stritg"); c = EOF; buf = pic_open_output_string(pic); @@ -545,7 +535,7 @@ pic_port_read_byte(pic_state *pic){ int c; pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-u8"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-u8"); if ((c = xfgetc(port->file)) == EOF) { return pic_eof_object(); } @@ -561,7 +551,7 @@ pic_port_peek_byte(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "peek-u8"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "peek-u8"); c = xfgetc(port->file); if (c == EOF) { @@ -580,7 +570,7 @@ pic_port_byte_ready_p(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "u8-ready?"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "u8-ready?"); return pic_true_value(); /* FIXME: always returns #t */ } @@ -595,7 +585,7 @@ pic_port_read_blob(pic_state *pic) pic_get_args(pic, "k|p", &k, &port); - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-bytevector"); blob = pic_make_blob(pic, k); @@ -629,7 +619,7 @@ pic_port_read_blob_ip(pic_state *pic) end = bv->len; } - assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector!"); + assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, "read-bytevector!"); if (end < start) { pic_errorf(pic, "read-bytevector!: end index must be greater than or equal to start index"); @@ -657,7 +647,7 @@ pic_port_newline(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "newline"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "newline"); xfputs("\n", port->file); return pic_undef_value(); @@ -671,7 +661,7 @@ pic_port_write_char(pic_state *pic) pic_get_args(pic, "c|p", &c, &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "write-char"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-char"); xfputc(c, port->file); return pic_undef_value(); @@ -694,7 +684,7 @@ pic_port_write_string(pic_state *pic) end = INT_MAX; } - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "write-string"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, "write-string"); for (i = start; i < end && str[i] != '\0'; ++i) { xfputc(str[i], port->file); @@ -710,7 +700,7 @@ pic_port_write_byte(pic_state *pic) pic_get_args(pic, "i|p", &i, &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "write-u8"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-u8"); xfputc(i, port->file); return pic_undef_value(); @@ -734,7 +724,7 @@ pic_port_write_blob(pic_state *pic) end = blob->len; } - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "write-bytevector"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, "write-bytevector"); for (i = start; i < end; ++i) { xfputc(blob->data[i], port->file); @@ -749,7 +739,7 @@ pic_port_flush(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT, PIC_PORT_OPEN, "flush-output-port"); + assert_port_profile(port, PIC_PORT_OUT, "flush-output-port"); xfflush(port->file); return pic_undef_value(); From f2e6feea7f60515fe6a25fb9a7e014043feb0e5b Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 19 Jun 2015 00:02:24 +0900 Subject: [PATCH 4/4] assert value bound to current-(input|output|error)-port is port --- extlib/benz/port.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/extlib/benz/port.c b/extlib/benz/port.c index a562b9f6..c73cabd5 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -14,6 +14,30 @@ pic_eof_object() return v; } +static pic_value +pic_assert_port(pic_state *pic) +{ + struct pic_port *port; + + pic_get_args(pic, "p", &port); + + return pic_obj_value(port); +} + +/* current-(input|output|error)-port */ + +static void +pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) +{ + struct pic_port *port; + + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); + port->file = file; + port->flags = dir | PIC_PORT_TEXT | PIC_PORT_OPEN; + + pic_defvar(pic, name, pic_obj_value(port), pic_make_proc(pic, pic_assert_port, "pic_assert_port")); +} + #define DEFINE_STANDARD_PORT_ACCESSOR(name, var) \ struct pic_port * \ name(pic_state *pic) \ @@ -29,17 +53,6 @@ DEFINE_STANDARD_PORT_ACCESSOR(pic_stdin, "current-input-port") DEFINE_STANDARD_PORT_ACCESSOR(pic_stdout, "current-output-port") DEFINE_STANDARD_PORT_ACCESSOR(pic_stderr, "current-error-port") -struct pic_port * -pic_make_standard_port(pic_state *pic, xFILE *file, short dir) -{ - struct pic_port *port; - - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port), PIC_TT_PORT); - port->file = file; - port->flags = dir | PIC_PORT_TEXT | PIC_PORT_OPEN; - return port; -} - struct strfile { pic_state *pic; char *buf; @@ -748,13 +761,9 @@ pic_port_flush(pic_state *pic) void pic_init_port(pic_state *pic) { - struct pic_port *xSTDIN = pic_make_standard_port(pic, xstdin, PIC_PORT_IN); - struct pic_port *xSTDOUT = pic_make_standard_port(pic, xstdout, PIC_PORT_OUT); - struct pic_port *xSTDERR = pic_make_standard_port(pic, xstderr, PIC_PORT_OUT); - - pic_defvar(pic, "current-input-port", pic_obj_value(xSTDIN), NULL); - pic_defvar(pic, "current-output-port", pic_obj_value(xSTDOUT), NULL); - pic_defvar(pic, "current-error-port", pic_obj_value(xSTDERR), NULL); + pic_define_standard_port(pic, "current-input-port", xstdin, PIC_PORT_IN); + pic_define_standard_port(pic, "current-output-port", xstdout, PIC_PORT_OUT); + pic_define_standard_port(pic, "current-error-port", xstderr, PIC_PORT_OUT); pic_defun(pic, "call-with-port", pic_port_call_with_port);