add system.c

This commit is contained in:
Yuichi Nishiwaki 2013-10-21 11:51:02 +09:00
parent 7dbb2c6de4
commit adab797391
5 changed files with 86 additions and 4 deletions

View File

@ -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 *, ...);

View File

@ -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;
}

View File

@ -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;

72
src/system.c Normal file
View File

@ -0,0 +1,72 @@
#include <stdlib.h>
#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);
}

View File

@ -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);