summaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-05-14 18:04:41 +0200
committerJo-Philipp Wich <jo@mein.io>2021-05-14 18:04:41 +0200
commit7b81ab2911f023e5e09a002730d5350532cee461 (patch)
treee276dfeebec6522d4db25408b669d42384e971ca /main.c
parent7283a70c8cdc3fcdeedc34e17a8025c15664464b (diff)
main: expose argv as global ARGV array to ucode scripts
This allows accessing the arguments of the invoked command line. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'main.c')
-rw-r--r--main.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/main.c b/main.c
index d291447..baea9fb 100644
--- a/main.c
+++ b/main.c
@@ -55,10 +55,11 @@ print_usage(const char *app)
}
static void
-globals_init(uc_vm *vm, uc_value_t *scope)
+globals_init(uc_vm *vm, uc_value_t *scope, int argc, char **argv)
{
uc_value_t *arr = ucv_array_new(vm);
const char *p, *last;
+ int i;
for (p = last = LIB_SEARCH_PATH;; p++) {
if (*p == ':' || *p == '\0') {
@@ -72,6 +73,13 @@ globals_init(uc_vm *vm, uc_value_t *scope)
}
ucv_object_add(scope, "REQUIRE_SEARCH_PATH", arr);
+
+ arr = ucv_array_new(vm);
+
+ for (i = 0; i < argc; i++)
+ ucv_array_push(arr, ucv_string_new(argv[i]));
+
+ ucv_object_add(scope, "ARGV", arr);
}
static void
@@ -94,7 +102,8 @@ register_variable(uc_value_t *scope, const char *key, uc_value_t *val)
static int
parse(uc_parse_config *config, uc_source *src,
- bool skip_shebang, uc_value_t *env, uc_value_t *modules)
+ bool skip_shebang, uc_value_t *env, uc_value_t *modules,
+ int argc, char **argv)
{
uc_value_t *globals = NULL;
uc_function_t *entry;
@@ -134,7 +143,7 @@ parse(uc_parse_config *config, uc_source *src,
globals = ucv_object_new(&vm);
/* load global variables */
- globals_init(&vm, globals);
+ globals_init(&vm, globals, argc, argv);
/* load env variables */
if (env) {
@@ -385,7 +394,7 @@ main(int argc, char **argv)
goto out;
}
- rv = parse(&config, source, shebang, env, modules);
+ rv = parse(&config, source, shebang, env, modules, argc, argv);
out:
ucv_put(modules);