From 2ca7e630f0f4be74f0528721c099c1af00bfc758 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 23 Feb 2016 04:37:58 +0900 Subject: [PATCH] write is now pluggable --- Makefile | 2 +- extlib/benz/debug.c | 4 ++++ extlib/benz/error.c | 5 +++-- extlib/benz/file.c | 2 +- extlib/benz/gc.c | 2 +- extlib/benz/include/picrin/config.h | 9 ++++++--- extlib/benz/include/picrin/extra.h | 8 ++++++-- extlib/benz/include/picrin/setup.h | 16 ++++++++++------ extlib/benz/lib.c | 2 +- extlib/benz/state.c | 7 +++++-- extlib/benz/write.c | 4 ++++ 11 files changed, 42 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 6c80d105..e498d0fd 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ test: test-contribs test-nostdlib test-issue test-contribs: bin/picrin $(CONTRIB_TESTS) test-nostdlib: - $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_STDIO=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector + $(CC) -I extlib/benz/include -D'PIC_USE_LIBC=0' -D'PIC_USE_STDIO=0' -D'PIC_USE_WRITE=0' -ffreestanding -nostdlib -Os -fPIC -shared -std=c89 -pedantic -Wall -Wextra -Werror -o lib/libbenz-tiny.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector strip lib/libbenz-tiny.so ls -lh lib/libbenz-tiny.so rm -f lib/libbenz-tiny.so diff --git a/extlib/benz/debug.c b/extlib/benz/debug.c index 043571b9..0a0efa0a 100644 --- a/extlib/benz/debug.c +++ b/extlib/benz/debug.c @@ -35,6 +35,8 @@ pic_get_backtrace(pic_state *pic) return trace; } +#if PIC_USE_WRITE + void pic_print_error(pic_state *pic, xFILE *file) { @@ -65,3 +67,5 @@ pic_print_error(pic_state *pic, xFILE *file) xfputs(pic, pic_str(pic, pic_obj_value(e->stack)), file); } } + +#endif diff --git a/extlib/benz/error.c b/extlib/benz/error.c index 3012fc58..0105215f 100644 --- a/extlib/benz/error.c +++ b/extlib/benz/error.c @@ -14,7 +14,7 @@ pic_panic(pic_state *pic, const char *msg) pic->panicf(pic, msg); } -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO fprintf(stderr, "picrin panic!: %s\n", msg); #endif @@ -26,6 +26,7 @@ pic_panic(pic_state *pic, const char *msg) void pic_warnf(pic_state *pic, const char *fmt, ...) { + xFILE *file = pic_fileno(pic, pic_stderr(pic)); va_list ap; pic_value err; @@ -33,7 +34,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...) err = pic_vstrf_value(pic, fmt, ap); va_end(ap); - pic_fprintf(pic, pic_stderr(pic), "warn: %s\n", pic_str(pic, err)); + xfprintf(pic, file, "warn: %s\n", pic_str(pic, err)); } void diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 9a844acd..7b846b00 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -364,7 +364,7 @@ xFILE *xfile_xstdin(pic_state *pic) { return &pic->files[0]; } xFILE *xfile_xstdout(pic_state *pic) { return &pic->files[1]; } xFILE *xfile_xstderr(pic_state *pic) { return &pic->files[2]; } -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO static int file_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) { diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index f5ecd292..23ddd5e0 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -82,7 +82,7 @@ pic_heap_close(pic_state *pic, struct heap *heap) pic_free(pic, heap); } -#if PIC_ENABLE_LIBC +#if PIC_USE_LIBC void * pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size) { diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index e928f95f..eb1c3643 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -2,11 +2,14 @@ * See Copyright Notice in picrin.h */ -/** no dependency on libc */ -/* #define PIC_ENABLE_LIBC 1 */ +/** no dependency on libc? */ +/* #define PIC_USE_LIBC 1 */ /** use stdio or not */ -/* #define PIC_ENABLE_STDIO 1 */ +/* #define PIC_USE_STDIO 1 */ + +/** enable some specific features? */ +/* #define PIC_USE_WRITE 1 */ /** essential external functions */ /* #define PIC_JMPBUF jmp_buf */ diff --git a/extlib/benz/include/picrin/extra.h b/extlib/benz/include/picrin/extra.h index 955be4b1..e574e24c 100644 --- a/extlib/benz/include/picrin/extra.h +++ b/extlib/benz/include/picrin/extra.h @@ -10,7 +10,7 @@ extern "C" { #endif -#if PIC_ENABLE_LIBC +#if PIC_USE_LIBC void *pic_default_allocf(void *, void *, size_t); #endif @@ -23,9 +23,11 @@ pic_value pic_eval(pic_state *, pic_value program, const char *lib); void pic_load(pic_state *, pic_value port); void pic_load_cstr(pic_state *, const char *); +#if PIC_USE_WRITE void pic_printf(pic_state *, const char *fmt, ...); void pic_fprintf(pic_state *, pic_value port, const char *fmt, ...); void pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap); +#endif /* extra xfile methods */ @@ -35,7 +37,7 @@ xFILE *xfile_xstderr(pic_state *); #define xstdin (xfile_xstdin(pic)) #define xstdout (xfile_xstdout(pic)) #define xstderr (xfile_xstderr(pic)) -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO xFILE *xfopen_file(pic_state *, FILE *, const char *mode); #endif xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); @@ -109,7 +111,9 @@ pic_value pic_err(pic_state *); void pic_warnf(pic_state *, const char *, ...); pic_value pic_get_backtrace(pic_state *); +#if PIC_USE_WRITE void pic_print_error(pic_state *, xFILE *); +#endif pic_value pic_library_environment(pic_state *, const char *); diff --git a/extlib/benz/include/picrin/setup.h b/extlib/benz/include/picrin/setup.h index 999a6f96..4fb9e36c 100644 --- a/extlib/benz/include/picrin/setup.h +++ b/extlib/benz/include/picrin/setup.h @@ -4,12 +4,16 @@ #include "picrin/config.h" -#ifndef PIC_ENABLE_LIBC -# define PIC_ENABLE_LIBC 1 +#ifndef PIC_USE_LIBC +# define PIC_USE_LIBC 1 #endif -#ifndef PIC_ENABLE_STDIO -# define PIC_ENABLE_STDIO 1 +#ifndef PIC_USE_STDIO +# define PIC_USE_STDIO 1 +#endif + +#ifndef PIC_USE_WRITE +# define PIC_USE_WRITE 1 #endif #ifndef PIC_JMPBUF @@ -156,7 +160,7 @@ typedef unsigned long uint32_t; } while (0) -#if PIC_ENABLE_LIBC +#if PIC_USE_LIBC #include #include @@ -375,7 +379,7 @@ atof(const char *nptr) #endif -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO # include PIC_INLINE void diff --git a/extlib/benz/lib.c b/extlib/benz/lib.c index b200b4cb..b65fa3b0 100644 --- a/extlib/benz/lib.c +++ b/extlib/benz/lib.c @@ -175,7 +175,7 @@ pic_make_library(pic_state *pic, const char *lib) it = kh_put(ltable, h, pic_str(pic, name), &ret); if (ret == 0) { /* if exists */ - pic_error(pic, "library name already in use", pic_cstr_value(pic, lib)); + pic_error(pic, "library name already in use", 1, pic_cstr_value(pic, lib)); } kh_val(h, it).name = pic_str_ptr(pic, name); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 373412b8..bbbc28d7 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -168,7 +168,6 @@ pic_init_core(pic_state *pic) pic_init_error(pic); DONE; pic_init_str(pic); DONE; pic_init_var(pic); DONE; - pic_init_write(pic); DONE; pic_init_read(pic); DONE; pic_init_dict(pic); DONE; pic_init_record(pic); DONE; @@ -176,6 +175,10 @@ pic_init_core(pic_state *pic) pic_init_lib(pic); DONE; pic_init_weak(pic); DONE; +#if PIC_USE_WRITE + pic_init_write(pic); DONE; +#endif + pic_defun(pic, "features", pic_features); pic_load_cstr(pic, &pic_boot[0][0]); @@ -275,7 +278,7 @@ pic_open(pic_allocf allocf, void *userdata) /* file pool */ memset(pic->files, 0, sizeof pic->files); -#if PIC_ENABLE_STDIO +#if PIC_USE_STDIO xfopen_file(pic, stdin, "r"); xfopen_file(pic, stdout, "w"); xfopen_file(pic, stderr, "w"); diff --git a/extlib/benz/write.c b/extlib/benz/write.c index 78cccb02..b76914d0 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -6,6 +6,8 @@ #include "picrin/extra.h" #include "picrin/private/object.h" +#if PIC_USE_WRITE + struct writer_control { int mode; int op; @@ -501,3 +503,5 @@ pic_init_write(pic_state *pic) pic_defun(pic, "write-shared", pic_write_write_shared); pic_defun(pic, "display", pic_write_display); } + +#endif