diff --git a/extlib/benz/codegen.c b/extlib/benz/codegen.c index 68149a05..062fc20c 100644 --- a/extlib/benz/codegen.c +++ b/extlib/benz/codegen.c @@ -555,7 +555,6 @@ analyze_quote(analyze_state *state, pic_value obj) return pic_list2(pic, pic_obj_value(pic->sQUOTE), pic_list_ref(pic, obj, 1)); } - #define ARGC_ASSERT_GE(n, name) do { \ if (pic_length(pic, obj) < (n) + 1) { \ pic_errorf(pic, \ @@ -573,7 +572,6 @@ analyze_quote(analyze_state *state, pic_value obj) } \ } while (0) - static pic_value analyze_add(analyze_state *state, pic_value obj, bool tailpos) { @@ -730,7 +728,6 @@ analyze_call_with_values(analyze_state *state, pic_value obj, bool tailpos) analyze(state, pic_list_ref(pic, obj, 1), false), \ analyze(state, pic_list_ref(pic, obj, 2), false)) - static pic_value analyze_node(analyze_state *state, pic_value obj, bool tailpos) { diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index eb969c1c..65b8e548 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -36,34 +36,27 @@ pic_get_backtrace(pic_state *pic) } void -pic_print_backtrace(pic_state *pic) +pic_print_backtrace(pic_state *pic, xFILE *file) { - size_t ai = pic_gc_arena_preserve(pic); - pic_str *trace; - assert(! pic_undef_p(pic->err)); if (! pic_error_p(pic->err)) { - trace = pic_format(pic, "raised: ~s", pic->err); + xfprintf(file, "raise: "); + pic_fwrite(pic, pic->err, file); } else { struct pic_error *e; e = pic_error_ptr(pic->err); if (e->type != pic_intern_cstr(pic, "")) { - trace = pic_format(pic, "~s ", pic_obj_value(e->type)); - } else { - trace = pic_make_str(pic, NULL, 0); + pic_fwrite(pic, pic_obj_value(e->type), file); + xfprintf(file, " "); } - trace = pic_str_cat(pic, trace, pic_format(pic, "error: ~s", pic_obj_value(e->msg))); + xfprintf(file, "error: "); + pic_fwrite(pic, pic_obj_value(e->msg), file); + xfprintf(file, "\n"); /* TODO: print error irritants */ - trace = pic_str_cat(pic, trace, pic_make_str(pic, "\n", 1)); - trace = pic_str_cat(pic, trace, e->stack); + xfputs(pic_str_cstr(pic, e->stack), file); } - - /* print! */ - xfprintf(xstderr, "%s", pic_str_cstr(pic, trace)); - - pic_gc_arena_restore(pic, ai); } diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 3fd6035b..705f0055 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -9,6 +9,7 @@ #include "picrin/data.h" #include "picrin/string.h" #include "picrin/error.h" +#include "picrin/port.h" void pic_panic(pic_state *pic, const char *msg) @@ -29,7 +30,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...) err_line = pic_xvformat(pic, fmt, ap); va_end(ap); - fprintf(stderr, "warn: %s\n", pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line)))); + xfprintf(pic_stderr(pic)->file, "warn: %s\n", pic_str_cstr(pic, pic_str_ptr(pic_car(pic, err_line)))); } void diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 6ae0107e..8965af8d 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -224,7 +224,7 @@ PIC_NORETURN void pic_errorf(pic_state *, const char *, ...); void pic_warnf(pic_state *, const char *, ...); const char *pic_errmsg(pic_state *); pic_str *pic_get_backtrace(pic_state *); -void pic_print_backtrace(pic_state *); +void pic_print_backtrace(pic_state *, xFILE *); /* obsoleted */ PIC_INLINE void pic_warn(pic_state *pic, const char *msg) diff --git a/extlib/benz/port.c b/extlib/benz/port.c index be574938..9f725f7f 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -38,6 +38,16 @@ pic_stdout(pic_state *pic) 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); +} + struct pic_port * pic_make_standard_port(pic_state *pic, xFILE *file, short dir) { diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 29b4792f..f89e0703 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -406,7 +406,7 @@ display(pic_state *pic, pic_value obj, xFILE *file) pic_value pic_write(pic_state *pic, pic_value obj) { - return pic_fwrite(pic, obj, xstdout); + return pic_fwrite(pic, obj, pic_stdout(pic)->file); } pic_value @@ -420,7 +420,7 @@ pic_fwrite(pic_state *pic, pic_value obj, xFILE *file) pic_value pic_display(pic_state *pic, pic_value obj) { - return pic_fdisplay(pic, obj, xstdout); + return pic_fdisplay(pic, obj, pic_stdout(pic)->file); } pic_value @@ -434,6 +434,7 @@ pic_fdisplay(pic_state *pic, pic_value obj, xFILE *file) void pic_printf(pic_state *pic, const char *fmt, ...) { + xFILE *file = pic_stdout(pic)->file; va_list ap; pic_str *str; @@ -443,8 +444,8 @@ pic_printf(pic_state *pic, const char *fmt, ...) va_end(ap); - xprintf("%s", pic_str_cstr(pic, str)); - xfflush(xstdout); + xfprintf(file, "%s", pic_str_cstr(pic, str)); + xfflush(file); } static pic_value diff --git a/src/main.c b/src/main.c index f43419f1..72bbaebb 100644 --- a/src/main.c +++ b/src/main.c @@ -65,7 +65,7 @@ main(int argc, char *argv[], char **envp) pic_funcall(pic, PICRIN_MAIN, "main", pic_nil_value()); } pic_catch { - pic_print_backtrace(pic); + pic_print_backtrace(pic, xstderr); status = 1; }