ref: 3c77420b78c6c828c66a5a31bf4d685e289a21e1
parent: 4d255d3f3e8bba62cdf62a4b1202d83f686b05b8
author: telephil9 <[email protected]>
date: Wed Oct 28 02:17:39 EDT 2020
Pass command line arguments through arg table Command line arguments are now available through the global arg table. As with the standard lua interpreter, positive indices are arguments, program and script names are at indices <= 0.
--- a/lua9.c
+++ b/lua9.c
@@ -25,6 +25,22 @@
{ NULL, NULL },
};
+static void
+createargtable (lua_State *L, char **argv, int argc, int script)
+{
+ int i, narg;
+
+ if (script == argc)
+ script = 0; /* no script name? */
+ narg = argc - (script + 1); /* number of positive indices */
+ lua_createtable(L, narg, script + 1);
+ for (i = 0; i < argc; i++) {
+ lua_pushstring(L, argv[i]);
+ lua_rawseti(L, -2, i - script);
+ }
+ lua_setglobal(L, "arg");
+}
+
int
main(int argc, char *argv[])
{
@@ -39,6 +55,7 @@
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1);
}
+ createargtable(L, argv, argc, 1);
r = luaL_dofile(L, argc > 1 ? argv[1] : NULL);
if(r != LUA_OK){
s = luaL_checkstring(L, lua_gettop(L));