write is now pluggable

This commit is contained in:
Yuichi Nishiwaki 2016-02-23 04:37:58 +09:00
parent 4e1aaf8b89
commit 2ca7e630f0
11 changed files with 42 additions and 19 deletions

View File

@ -71,7 +71,7 @@ test: test-contribs test-nostdlib test-issue
test-contribs: bin/picrin $(CONTRIB_TESTS) test-contribs: bin/picrin $(CONTRIB_TESTS)
test-nostdlib: 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 strip lib/libbenz-tiny.so
ls -lh lib/libbenz-tiny.so ls -lh lib/libbenz-tiny.so
rm -f lib/libbenz-tiny.so rm -f lib/libbenz-tiny.so

View File

@ -35,6 +35,8 @@ pic_get_backtrace(pic_state *pic)
return trace; return trace;
} }
#if PIC_USE_WRITE
void void
pic_print_error(pic_state *pic, xFILE *file) 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); xfputs(pic, pic_str(pic, pic_obj_value(e->stack)), file);
} }
} }
#endif

View File

@ -14,7 +14,7 @@ pic_panic(pic_state *pic, const char *msg)
pic->panicf(pic, msg); pic->panicf(pic, msg);
} }
#if PIC_ENABLE_STDIO #if PIC_USE_STDIO
fprintf(stderr, "picrin panic!: %s\n", msg); fprintf(stderr, "picrin panic!: %s\n", msg);
#endif #endif
@ -26,6 +26,7 @@ pic_panic(pic_state *pic, const char *msg)
void void
pic_warnf(pic_state *pic, const char *fmt, ...) pic_warnf(pic_state *pic, const char *fmt, ...)
{ {
xFILE *file = pic_fileno(pic, pic_stderr(pic));
va_list ap; va_list ap;
pic_value err; pic_value err;
@ -33,7 +34,7 @@ pic_warnf(pic_state *pic, const char *fmt, ...)
err = pic_vstrf_value(pic, fmt, ap); err = pic_vstrf_value(pic, fmt, ap);
va_end(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 void

View File

@ -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_xstdout(pic_state *pic) { return &pic->files[1]; }
xFILE *xfile_xstderr(pic_state *pic) { return &pic->files[2]; } xFILE *xfile_xstderr(pic_state *pic) { return &pic->files[2]; }
#if PIC_ENABLE_STDIO #if PIC_USE_STDIO
static int static int
file_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) { file_read(pic_state *PIC_UNUSED(pic), void *cookie, char *ptr, int size) {

View File

@ -82,7 +82,7 @@ pic_heap_close(pic_state *pic, struct heap *heap)
pic_free(pic, heap); pic_free(pic, heap);
} }
#if PIC_ENABLE_LIBC #if PIC_USE_LIBC
void * void *
pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size) pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size)
{ {

View File

@ -2,11 +2,14 @@
* See Copyright Notice in picrin.h * See Copyright Notice in picrin.h
*/ */
/** no dependency on libc */ /** no dependency on libc? */
/* #define PIC_ENABLE_LIBC 1 */ /* #define PIC_USE_LIBC 1 */
/** use stdio or not */ /** 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 */ /** essential external functions */
/* #define PIC_JMPBUF jmp_buf */ /* #define PIC_JMPBUF jmp_buf */

View File

@ -10,7 +10,7 @@ extern "C" {
#endif #endif
#if PIC_ENABLE_LIBC #if PIC_USE_LIBC
void *pic_default_allocf(void *, void *, size_t); void *pic_default_allocf(void *, void *, size_t);
#endif #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(pic_state *, pic_value port);
void pic_load_cstr(pic_state *, const char *); void pic_load_cstr(pic_state *, const char *);
#if PIC_USE_WRITE
void pic_printf(pic_state *, const char *fmt, ...); void pic_printf(pic_state *, const char *fmt, ...);
void pic_fprintf(pic_state *, pic_value port, 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); void pic_vfprintf(pic_state *, pic_value port, const char *fmt, va_list ap);
#endif
/* extra xfile methods */ /* extra xfile methods */
@ -35,7 +37,7 @@ xFILE *xfile_xstderr(pic_state *);
#define xstdin (xfile_xstdin(pic)) #define xstdin (xfile_xstdin(pic))
#define xstdout (xfile_xstdout(pic)) #define xstdout (xfile_xstdout(pic))
#define xstderr (xfile_xstderr(pic)) #define xstderr (xfile_xstderr(pic))
#if PIC_ENABLE_STDIO #if PIC_USE_STDIO
xFILE *xfopen_file(pic_state *, FILE *, const char *mode); xFILE *xfopen_file(pic_state *, FILE *, const char *mode);
#endif #endif
xFILE *xfopen_buf(pic_state *, const char *buf, int len, const char *mode); 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 *, ...); void pic_warnf(pic_state *, const char *, ...);
pic_value pic_get_backtrace(pic_state *); pic_value pic_get_backtrace(pic_state *);
#if PIC_USE_WRITE
void pic_print_error(pic_state *, xFILE *); void pic_print_error(pic_state *, xFILE *);
#endif
pic_value pic_library_environment(pic_state *, const char *); pic_value pic_library_environment(pic_state *, const char *);

View File

@ -4,12 +4,16 @@
#include "picrin/config.h" #include "picrin/config.h"
#ifndef PIC_ENABLE_LIBC #ifndef PIC_USE_LIBC
# define PIC_ENABLE_LIBC 1 # define PIC_USE_LIBC 1
#endif #endif
#ifndef PIC_ENABLE_STDIO #ifndef PIC_USE_STDIO
# define PIC_ENABLE_STDIO 1 # define PIC_USE_STDIO 1
#endif
#ifndef PIC_USE_WRITE
# define PIC_USE_WRITE 1
#endif #endif
#ifndef PIC_JMPBUF #ifndef PIC_JMPBUF
@ -156,7 +160,7 @@ typedef unsigned long uint32_t;
} while (0) } while (0)
#if PIC_ENABLE_LIBC #if PIC_USE_LIBC
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
@ -375,7 +379,7 @@ atof(const char *nptr)
#endif #endif
#if PIC_ENABLE_STDIO #if PIC_USE_STDIO
# include <stdio.h> # include <stdio.h>
PIC_INLINE void PIC_INLINE void

View File

@ -175,7 +175,7 @@ pic_make_library(pic_state *pic, const char *lib)
it = kh_put(ltable, h, pic_str(pic, name), &ret); it = kh_put(ltable, h, pic_str(pic, name), &ret);
if (ret == 0) { /* if exists */ 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); kh_val(h, it).name = pic_str_ptr(pic, name);

View File

@ -168,7 +168,6 @@ pic_init_core(pic_state *pic)
pic_init_error(pic); DONE; pic_init_error(pic); DONE;
pic_init_str(pic); DONE; pic_init_str(pic); DONE;
pic_init_var(pic); DONE; pic_init_var(pic); DONE;
pic_init_write(pic); DONE;
pic_init_read(pic); DONE; pic_init_read(pic); DONE;
pic_init_dict(pic); DONE; pic_init_dict(pic); DONE;
pic_init_record(pic); DONE; pic_init_record(pic); DONE;
@ -176,6 +175,10 @@ pic_init_core(pic_state *pic)
pic_init_lib(pic); DONE; pic_init_lib(pic); DONE;
pic_init_weak(pic); DONE; pic_init_weak(pic); DONE;
#if PIC_USE_WRITE
pic_init_write(pic); DONE;
#endif
pic_defun(pic, "features", pic_features); pic_defun(pic, "features", pic_features);
pic_load_cstr(pic, &pic_boot[0][0]); pic_load_cstr(pic, &pic_boot[0][0]);
@ -275,7 +278,7 @@ pic_open(pic_allocf allocf, void *userdata)
/* file pool */ /* file pool */
memset(pic->files, 0, sizeof pic->files); memset(pic->files, 0, sizeof pic->files);
#if PIC_ENABLE_STDIO #if PIC_USE_STDIO
xfopen_file(pic, stdin, "r"); xfopen_file(pic, stdin, "r");
xfopen_file(pic, stdout, "w"); xfopen_file(pic, stdout, "w");
xfopen_file(pic, stderr, "w"); xfopen_file(pic, stderr, "w");

View File

@ -6,6 +6,8 @@
#include "picrin/extra.h" #include "picrin/extra.h"
#include "picrin/private/object.h" #include "picrin/private/object.h"
#if PIC_USE_WRITE
struct writer_control { struct writer_control {
int mode; int mode;
int op; int op;
@ -501,3 +503,5 @@ pic_init_write(pic_state *pic)
pic_defun(pic, "write-shared", pic_write_write_shared); pic_defun(pic, "write-shared", pic_write_write_shared);
pic_defun(pic, "display", pic_write_display); pic_defun(pic, "display", pic_write_display);
} }
#endif