about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-12-11 10:36:35 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-12-11 10:36:35 -0800
commitd25c37f86b64309618a255c26c6e8647841f675f (patch)
treee4c0c333885ca1f4dc7b820787e24b3a2966caa9 /src
parentc0c9d3168873deb063721d3ba5db2205862d0547 (diff)
downloadteliva-d25c37f86b64309618a255c26c6e8647841f675f.tar.gz
bring back commandline args
Diffstat (limited to 'src')
-rw-r--r--src/lua.c19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/lua.c b/src/lua.c
index 62441f7..5ee8481 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -168,6 +168,23 @@ static int docall (lua_State *L, int narg, int clear) {
 }
 
 
+/* initialize global binding "args" for commandline args */
+static void set_args (lua_State *L, char **argv, int n) {
+  int narg;
+  int i;
+  int argc = 0;
+  while (argv[argc]) argc++;  /* count total number of arguments */
+  narg = argc - (n + 1);  /* number of arguments to the script */
+  luaL_checkstack(L, narg + 3, "too many arguments to script");
+  lua_newtable(L);
+  for (i=0; i < argc; i++) {
+    lua_pushstring(L, argv[i]);
+    lua_rawseti(L, -2, i - n);
+  }
+  lua_setglobal(L, "arg");
+}
+
+
 static int dofile (lua_State *L, const char *name) {
   int status = luaL_loadfile(L, name) || docall(L, 0, 1);
   return report_in_developer_mode(L, status);
@@ -310,7 +327,7 @@ char *Image_name = NULL;
 extern void load_tlv (lua_State *L, char *filename);
 static int handle_image (lua_State *L, char **argv, int n) {
   int status;
-  /* TODO: pass args in */
+  set_args(L, argv, n);
   /* parse and load file contents (teliva_program array) */
   Image_name = argv[n];
   load_tlv(L, Image_name);