Fix command-line argv handling.

This commit is contained in:
Mike Pall 2022-01-27 22:18:39 +01:00
parent c2237247cc
commit 9ebebc9b58

View File

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