summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2009-12-11 00:37:02 +0100
committerJohannes Weiner <hannes@cmpxchg.org>2009-12-11 00:37:02 +0100
commit9c7fe574daed9e752f85c26e0d1af30952fdc3b5 (patch)
treebc20acbacb06ae031989b276c03e5c61f0601152
parenteafe0ff1a108be5cc6980e6b39346d1bb9e4666e (diff)
vm: export runtime arguments as `argv'
Parse the runtime arguments and make them available as a list of strings through the builtin variable `argv'. Signed-off-by: Johannes Weiner <hannes@cmpxchg.org>
-rw-r--r--include/sheep/vm.h2
-rw-r--r--sheep/main.c18
-rw-r--r--sheep/vm.c23
3 files changed, 32 insertions, 11 deletions
diff --git a/include/sheep/vm.h b/include/sheep/vm.h
index 8abee73..0384be9 100644
--- a/include/sheep/vm.h
+++ b/include/sheep/vm.h
@@ -46,7 +46,7 @@ static inline unsigned int sheep_vm_global(struct sheep_vm *vm)
unsigned int sheep_vm_variable(struct sheep_vm *, const char *, sheep_t);
void sheep_vm_function(struct sheep_vm *, const char *, sheep_alien_t);
-void sheep_vm_init(struct sheep_vm *);
+void sheep_vm_init(struct sheep_vm *, int, char **);
void sheep_vm_exit(struct sheep_vm *);
void sheep_vm_mark(struct sheep_vm *);
diff --git a/sheep/main.c b/sheep/main.c
index dd3336a..27fe561 100644
--- a/sheep/main.c
+++ b/sheep/main.c
@@ -9,19 +9,19 @@
#include <string.h>
#include <stdio.h>
-static int do_file(const char *path)
+static int do_file(int ac, char **av)
{
struct sheep_vm vm;
int ret = 1;
FILE *in;
- in = fopen(path, "r");
+ in = fopen(av[1], "r");
if (!in) {
- perror(path);
+ perror(av[1]);
return 1;
}
- sheep_vm_init(&vm);
+ sheep_vm_init(&vm, ac, av);
while (1) {
sheep_t exp, fun, val;
@@ -44,13 +44,13 @@ out:
return ret;
}
-static int do_stdin(void)
+static int do_stdin(int ac, char **av)
{
struct timeval start, end, diff;
struct sheep_vm vm;
gettimeofday(&start, NULL);
- sheep_vm_init(&vm);
+ sheep_vm_init(&vm, ac, av);
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
@@ -86,8 +86,8 @@ static int do_stdin(void)
int main(int ac, char **av)
{
- if (ac == 2)
- return do_file(av[1]);
+ if (ac > 1)
+ return do_file(ac, av);
else
- return do_stdin();
+ return do_stdin(ac, av);
}
diff --git a/sheep/vm.c b/sheep/vm.c
index 9e53765..0ec03d7 100644
--- a/sheep/vm.c
+++ b/sheep/vm.c
@@ -52,7 +52,27 @@ void sheep_vm_function(struct sheep_vm *vm, const char *name, sheep_alien_t f)
sheep_vm_variable(vm, name, sheep_make_alien(vm, f, name));
}
-void sheep_vm_init(struct sheep_vm *vm)
+static void setup_argv(struct sheep_vm *vm, int ac, char **av)
+{
+ struct sheep_list *p;
+ sheep_t list;
+
+ list = sheep_make_list(vm, NULL, NULL);
+ sheep_protect(vm, list);
+
+ p = sheep_list(list);
+ while (ac--) {
+ p->head = sheep_make_string(vm, *av);
+ p->tail = sheep_make_list(vm, NULL, NULL);
+ p = sheep_list(p->tail);
+ av++;
+ }
+
+ sheep_vm_variable(vm, "argv", list);
+ sheep_unprotect(vm, list);
+}
+
+void sheep_vm_init(struct sheep_vm *vm, int ac, char **av)
{
memset(vm, 0, sizeof(*vm));
sheep_core_init(vm);
@@ -62,6 +82,7 @@ void sheep_vm_init(struct sheep_vm *vm)
sheep_list_builtins(vm);
sheep_sequence_builtins(vm);
sheep_function_builtins(vm);
+ setup_argv(vm, ac, av);
}
void sheep_vm_exit(struct sheep_vm *vm)