]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
Fix command-line argv handling.
authorMike Pall <mike>
Thu, 27 Jan 2022 21:18:39 +0000 (22:18 +0100)
committerMike Pall <mike>
Thu, 27 Jan 2022 21:18:39 +0000 (22:18 +0100)
src/luajit.c

index e723e522f72047ad40339e4320ca4d4b2ba00cf5..cf4982a6b35ceee80fc6e6c40eefae27b2400f98 100644 (file)
@@ -39,6 +39,7 @@
 
 static lua_State *globalL = NULL;
 static const char *progname = LUA_PROGNAME;
+static char *empty_argv[2] = { NULL, NULL };
 
 #if !LJ_TARGET_CONSOLE
 static void lstop(lua_State *L, lua_Debug *ar)
@@ -79,9 +80,9 @@ static void print_usage(void)
   fflush(stderr);
 }
 
-static void l_message(const char *pname, const char *msg)
+static void l_message(const char *msg)
 {
-  if (pname) fprintf(stderr, "%s: ", pname);
+  if (progname) fprintf(stderr, "%s: ", progname);
   fprintf(stderr, "%s\n", msg);
   fflush(stderr);
 }
@@ -91,7 +92,7 @@ static int report(lua_State *L, int status)
   if (status && !lua_isnil(L, -1)) {
     const char *msg = lua_tostring(L, -1);
     if (msg == NULL) msg = "(error object is not a string)";
-    l_message(progname, msg);
+    l_message(msg);
     lua_pop(L, 1);
   }
   return status;
@@ -264,9 +265,8 @@ static void dotty(lua_State *L)
       lua_getglobal(L, "print");
       lua_insert(L, 1);
       if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
-       l_message(progname,
-         lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
-                             lua_tostring(L, -1)));
+       l_message(lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
+                                 lua_tostring(L, -1)));
     }
   }
   lua_settop(L, 0);  /* clear stack */
@@ -309,8 +309,7 @@ static int loadjitmodule(lua_State *L)
   lua_getfield(L, -1, "start");
   if (lua_isnil(L, -1)) {
   nomodule:
-    l_message(progname,
-             "unknown luaJIT command or jit.* modules not installed");
+    l_message("unknown luaJIT command or jit.* modules not installed");
     return 1;
   }
   lua_remove(L, -2);  /* Drop module table. */
@@ -514,7 +513,6 @@ static int pmain(lua_State *L)
   int script;
   int flags = 0;
   globalL = L;
-  if (argv[0] && argv[0][0]) progname = argv[0];
   LUAJIT_VERSION_SYM();  /* linker-enforced version check */
   script = collectargs(argv, &flags);
   if (script < 0) {  /* invalid args? */
@@ -558,9 +556,11 @@ static int pmain(lua_State *L)
 int main(int argc, char **argv)
 {
   int status;
-  lua_State *L = lua_open();  /* create state */
+  lua_State *L;
+  if (!argv[0]) argv = empty_argv; else if (argv[0][0]) progname = argv[0];
+  L = lua_open();  /* create state */
   if (L == NULL) {
-    l_message(argv[0], "cannot create state: not enough memory");
+    l_message("cannot create state: not enough memory");
     return EXIT_FAILURE;
   }
   smain.argc = argc;