diff --git a/include/libmes-mini.h b/include/libmes-mini.h index 28696418..75800f1a 100644 --- a/include/libmes-mini.h +++ b/include/libmes-mini.h @@ -21,6 +21,11 @@ #ifndef __MES_LIBMES_MINI_H #define __MES_LIBMES_MINI_H +char **environ; +int g_stdin; +int g_stdout; +int g_stderr; + #ifndef _SIZE_T #define _SIZE_T #ifndef __SIZE_T diff --git a/include/stdio.h b/include/stdio.h index 36b33672..9395d39b 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -20,21 +20,7 @@ #ifndef __MES_STDIO_H #define __MES_STDIO_H 1 -char **environ; -int g_stdin; -int g_stdout; - -#ifndef STDIN -#define STDIN 0 -#endif - -#ifndef STDOUT -#define STDOUT 1 -#endif - -#ifndef STDERR -#define STDERR 2 -#endif +#include #if WITH_GLIBC #ifndef _GNU_SOURCE diff --git a/lib/mes/eputc.c b/lib/mes/eputc.c index e9f7dc05..ac4cd15e 100644 --- a/lib/mes/eputc.c +++ b/lib/mes/eputc.c @@ -23,5 +23,5 @@ int eputc (int c) { - return fdputc (c, STDERR); + return fdputc (c, g_stderr); } diff --git a/lib/mes/eputs.c b/lib/mes/eputs.c index 3b0ece0b..7b6896ef 100644 --- a/lib/mes/eputs.c +++ b/lib/mes/eputs.c @@ -24,6 +24,6 @@ int eputs (char const* s) { int i = strlen (s); - write (STDERR, s, i); + write (g_stderr, s, i); return 0; } diff --git a/lib/mes/oputc.c b/lib/mes/oputc.c index b16ed3be..3b50e27e 100644 --- a/lib/mes/oputc.c +++ b/lib/mes/oputc.c @@ -23,5 +23,5 @@ int oputc (int c) { - return fdputc (c, STDOUT); + return fdputc (c, g_stdout); } diff --git a/lib/mes/oputs.c b/lib/mes/oputs.c index 123a8117..f82e3b8d 100644 --- a/lib/mes/oputs.c +++ b/lib/mes/oputs.c @@ -24,6 +24,6 @@ int oputs (char const* s) { int i = strlen (s); - write (1, s, i); + write (g_stdout, s, i); return 0; } diff --git a/mes/module/mes/boot-0.scm.in b/mes/module/mes/boot-0.scm.in index d8b61bab..b497b0e1 100644 --- a/mes/module/mes/boot-0.scm.in +++ b/mes/module/mes/boot-0.scm.in @@ -111,8 +111,6 @@ (define (primitive-eval e) (core:eval e (current-module))) (define eval core:eval) -(define (current-output-port) 1) -(define (current-error-port) 2) (define (port-filename port) "") (define (port-line port) 0) (define (port-column port) 0) diff --git a/src/lib.c b/src/lib.c index 25bb0884..3dd6dd35 100644 --- a/src/lib.c +++ b/src/lib.c @@ -228,7 +228,7 @@ SCM display_error_ (SCM x) { g_depth = 5; - return display_helper (x, 0, "", STDERR, 0); + return display_helper (x, 0, "", g_stderr, 0); } SCM @@ -249,7 +249,7 @@ SCM write_error_ (SCM x) { g_depth = 5; - return display_helper (x, 0, "", STDERR, 1); + return display_helper (x, 0, "", g_stderr, 1); } SCM diff --git a/src/mes.c b/src/mes.c index 698ebe27..48a91e24 100644 --- a/src/mes.c +++ b/src/mes.c @@ -2365,18 +2365,18 @@ a = acons (list_to_symbol (scm_getenv_.string), cell_getenv_, a); if (g_debug > 3) { - fdputs ("functions: ", STDERR); - fdputs (itoa (g_function), STDERR); - fdputs ("\n", STDERR); + fdputs ("functions: ", g_stderr); + fdputs (itoa (g_function), g_stderr); + fdputs ("\n", g_stderr); for (int i = 0; i < g_function; i++) { - fdputs ("[", STDERR); - fdputs (itoa (i), STDERR); - fdputs ("]: ", STDERR); - fdputs (g_functions[i].name, STDERR); - fdputs ("\n", STDERR); + fdputs ("[", g_stderr); + fdputs (itoa (i), g_stderr); + fdputs ("]: ", g_stderr); + fdputs (g_functions[i].name, g_stderr); + fdputs ("\n", g_stderr); } - fdputs ("\n", STDERR); + fdputs ("\n", g_stderr); } return a; @@ -2549,6 +2549,7 @@ main (int argc, char *argv[]) STACK_SIZE = atoi (p); g_stdin = STDIN; g_stdout = STDOUT; + g_stderr = STDERR; SCM a = mes_environment (argc, argv); a = mes_builtins (a); diff --git a/src/posix.c b/src/posix.c index eba767a4..d8deb3a9 100644 --- a/src/posix.c +++ b/src/posix.c @@ -136,6 +136,8 @@ write_byte (SCM x) ///((arity . n)) int fd = g_stdout; if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER && VALUE (CAR (p)) != 1) fd = VALUE (CAR (p)); + if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER && VALUE (CAR (p)) == 2) + fd = g_stderr; char cc = VALUE (c); write (fd, (char*)&cc, 1); #if !__MESC__ @@ -230,6 +232,12 @@ current_output_port () return MAKE_NUMBER (g_stdout); } +SCM +current_error_port () +{ + return MAKE_NUMBER (g_stderr); +} + SCM open_output_file (SCM x) ///((arity . n)) { @@ -248,6 +256,13 @@ set_current_output_port (SCM port) return current_output_port (); } +SCM +set_current_error_port (SCM port) +{ + g_stderr = VALUE (port) ? VALUE (port) : STDERR; + return current_error_port (); +} + SCM force_output (SCM p) ///((arity . n)) {