From adab797391e46cec1018e37866a6f136f10c56a3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 21 Oct 2013 11:51:02 +0900 Subject: [PATCH] add system.c --- include/picrin.h | 5 +++- src/init.c | 2 ++ src/state.c | 7 ++++- src/system.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/main.c | 4 +-- 5 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/system.c diff --git a/include/picrin.h b/include/picrin.h index b7cd063e..ec593460 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -17,6 +17,9 @@ typedef struct pic_callinfo { } pic_callinfo; typedef struct { + int argc; + char **argv, **envp; + pic_value *sp; pic_value *stbase, *stend; @@ -56,7 +59,7 @@ void pic_gc_protect(pic_state *, pic_value); int pic_gc_arena_preserve(pic_state *); void pic_gc_arena_restore(pic_state *, int); -pic_state *pic_open(); +pic_state *pic_open(int argc, char *argv[], char **envp); void pic_close(pic_state *); void pic_get_args(pic_state *, const char *, ...); diff --git a/src/init.c b/src/init.c index 2c330247..a4d0407d 100644 --- a/src/init.c +++ b/src/init.c @@ -3,6 +3,7 @@ void pic_init_port(pic_state *); void pic_init_number(pic_state *); void pic_init_time(pic_state *); +void pic_init_system(pic_state *); #define DONE pic_gc_arena_restore(pic, ai); @@ -15,4 +16,5 @@ pic_init_core(pic_state *pic) pic_init_port(pic); DONE; pic_init_number(pic); DONE; pic_init_time(pic); DONE; + pic_init_system(pic); DONE; } diff --git a/src/state.c b/src/state.c index 09bc261f..5cebdac9 100644 --- a/src/state.c +++ b/src/state.c @@ -35,13 +35,18 @@ sym_tbl_new() void pic_init_core(pic_state *); pic_state * -pic_open() +pic_open(int argc, char *argv[], char **envp) { pic_state *pic; int ai; pic = (pic_state *)malloc(sizeof(pic_state)); + /* command line */ + pic->argc = argc; + pic->argv = argv; + pic->envp = envp; + /* prepare VM stack */ pic->stbase = pic->sp = (pic_value *)malloc(sizeof(pic_value) * PIC_STACK_SIZE); pic->stend = pic->stbase + PIC_STACK_SIZE; diff --git a/src/system.c b/src/system.c new file mode 100644 index 00000000..04c38804 --- /dev/null +++ b/src/system.c @@ -0,0 +1,72 @@ +#include + +#include "picrin.h" +#include "picrin/pair.h" + +static pic_value +pic_system_cmdline(pic_state *pic) +{ + pic_value v = pic_nil_value(); + int i; + + pic_get_args(pic, ""); + + for (i = 0; i < pic->argc; ++i) { + int ai = pic_gc_arena_preserve(pic); + + v = pic_cons(pic, pic_str_new_cstr(pic, pic->argv[i]), v); + pic_gc_arena_restore(pic, ai); + } + + return v; +} + +static pic_value +pic_system_getenv(pic_state *pic) +{ + char *str, *val; + size_t len; + + pic_get_args(pic, "s", &str, &len); + + val = getenv(str); + + return pic_str_new_cstr(pic, val); +} + +static pic_value +pic_system_getenvs(pic_state *pic) +{ + char **envp; + pic_value data = pic_nil_value(); + int ai = pic_gc_arena_preserve(pic); + + pic_get_args(pic, ""); + + for (envp = pic->envp; *envp; ++envp) { + pic_value key, val; + int i; + + for (i = 0; (*envp)[i] != '='; ++i) + ; + + key = pic_str_new(pic, *envp, i); + val = pic_str_new_cstr(pic, getenv(*envp)); + + /* push */ + data = pic_acons(pic, key, val, data); + + pic_gc_arena_restore(pic, ai); + pic_gc_protect(pic, data); + } + + return data; +} + +void +pic_init_system(pic_state *pic) +{ + pic_defun(pic, "command-line", pic_system_cmdline); + pic_defun(pic, "get-environment-variable", pic_system_getenv); + pic_defun(pic, "get-environment-variables", pic_system_getenvs); +} diff --git a/tools/main.c b/tools/main.c index a8f752cc..275e01dd 100644 --- a/tools/main.c +++ b/tools/main.c @@ -13,7 +13,7 @@ #define LINE_MAX_LENGTH 256 int -main() +main(int argc, char *argv[], char **envp) { pic_state *pic; char code[CODE_MAX_LENGTH] = "", line[LINE_MAX_LENGTH]; @@ -28,7 +28,7 @@ main() int char_index; #endif - pic = pic_open(); + pic = pic_open(argc, argv, envp); ai = pic_gc_arena_preserve(pic);