diff --git a/Makefile b/Makefile index 7bbbf99e..030a248e 100644 --- a/Makefile +++ b/Makefile @@ -76,7 +76,7 @@ test-r7rs: bin/picrin t/r7rs-tests.scm test-contribs: bin/picrin $(CONTRIB_TESTS) test-nostdlib: - $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_FLOAT=0'-nostdlib -fPIC -shared -std=c89 -ansi -pedantic -Wall -Wextra -o lib/libbenz.so $(BENZ_SRCS) + $(CC) -I extlib/benz/include -D'PIC_ENABLE_LIBC=0' -D'PIC_ENABLE_FLOAT=0' -D'PIC_ENABLE_STDIO=0' -nostdlib -fPIC -shared -std=c89 -ansi -pedantic -Wall -Wextra -o lib/libbenz.so $(BENZ_SRCS) etc/libc_polyfill.c -fno-stack-protector rm -f lib/libbenz.so install: all diff --git a/etc/libc_polyfill.c b/etc/libc_polyfill.c new file mode 100644 index 00000000..af7c1864 --- /dev/null +++ b/etc/libc_polyfill.c @@ -0,0 +1,20 @@ +void abort() +{ + while (1); +} + +typedef char jmp_buf[1]; + +int setjmp(jmp_buf buf) +{ + (void)buf; + return 0; +} + +void longjmp(jmp_buf buf, int r) +{ + (void)buf; + (void)r; + while (1); +} + diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h index 24f82691..ff7268c3 100644 --- a/extlib/benz/include/picrin/compat.h +++ b/extlib/benz/include/picrin/compat.h @@ -209,6 +209,10 @@ strcpy(char *dst, const char *src) # include #endif +#if PIC_ENABLE_STDIO +# include +#endif + #if defined(__cplusplus) } #endif diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index b30bc398..30c89e2d 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -17,6 +17,9 @@ /** no dependency on libc */ /* #define PIC_ENABLE_LIBC 1 */ +/** use stdio or not */ +/* #define PIC_ENABLE_STDIO 1 */ + /** custom setjmp/longjmp */ /* #define PIC_JMPBUF jmp_buf */ /* #define PIC_SETJMP(pic, buf) setjmp(buf) */ @@ -93,6 +96,10 @@ # error cannot disable float support when nan boxing is on #endif +#ifndef PIC_ENABLE_STDIO +# define PIC_ENABLE_STDIO 1 +#endif + #ifndef PIC_JMPBUF # include # define PIC_JMPBUF jmp_buf diff --git a/extlib/benz/port.c b/extlib/benz/port.c index 518b95b2..5e75a464 100644 --- a/extlib/benz/port.c +++ b/extlib/benz/port.c @@ -4,8 +4,6 @@ #include "picrin.h" -#include - pic_value pic_eof_object() { @@ -28,6 +26,8 @@ pic_assert_port(pic_state *pic) /* current-(input|output|error)-port */ +#if PIC_ENABLE_STDIO + static int file_read(pic_state PIC_UNUSED(*pic), void *cookie, char *ptr, int size) { FILE *file = cookie; @@ -118,6 +118,32 @@ pic_open_file(pic_state *pic, const char *name, int flags) { return port; } +#else + +/* null file */ + +static int +null_read(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), char PIC_UNUSED(*ptr), int PIC_UNUSED(size)) { + return 0; +} + +static int +null_write(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), const char PIC_UNUSED(*ptr), int size) { + return size; +} + +static long +null_seek(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie), long PIC_UNUSED(pos), int PIC_UNUSED(whence)) { + return 0; +} + +static int +null_close(pic_state PIC_UNUSED(*pic), void PIC_UNUSED(*cookie)) { + return 0; +} + +#endif + static void pic_define_standard_port(pic_state *pic, const char *name, xFILE *file, int dir) { @@ -854,7 +880,11 @@ pic_port_flush(pic_state *pic) void pic_init_port(pic_state *pic) { -#define FILE_VTABLE { 0, file_read, file_write, file_seek, file_close } +#if PIC_ENABLE_STDIO +# define FILE_VTABLE { 0, file_read, file_write, file_seek, file_close } +#else +# define FILE_VTABLE { 0, null_read, null_write, null_seek, null_close } +#endif static const xFILE skel[3] = { { { 0 }, 0, NULL, NULL, FILE_VTABLE, X_READ }, @@ -866,9 +896,11 @@ pic_init_port(pic_state *pic) pic->files[1] = skel[1]; pic->files[2] = skel[2]; +#if PIC_ENABLE_STDIO pic->files[0].vtable.cookie = stdin; pic->files[1].vtable.cookie = stdout; pic->files[2].vtable.cookie = stderr; +#endif pic_define_standard_port(pic, "current-input-port", xstdin, PIC_PORT_IN); pic_define_standard_port(pic, "current-output-port", xstdout, PIC_PORT_OUT);