Browse Source

var-arguments to the script come from 'arg' table (not from original

'argv' array)
pull/9/head
Roberto Ierusalimschy 11 years ago
parent
commit
10200585a7
  1. 26
      lua.c

26
lua.c

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.210 2014/02/26 15:27:56 roberto Exp roberto $ ** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -417,25 +417,31 @@ static void doREPL (lua_State *L) {
/* /*
** Push on the stack 'n' strings from 'argv' ** Push on the stack the contents of table 'arg' from 1 to #arg
*/ */
static void pushargs (lua_State *L, char **argv, int n) { static int pushargs (lua_State *L) {
int i; int i, n;
lua_getglobal(L, "arg");
if (!lua_istable(L, -1))
luaL_error(L, "'arg' is not a table");
n = (int)luaL_len(L, -1);
luaL_checkstack(L, n + 3, "too many arguments to script"); luaL_checkstack(L, n + 3, "too many arguments to script");
for (i = 1; i < n; i++) /* skip 0 (the script name) */ for (i = 1; i <= n; i++)
lua_pushstring(L, argv[i]); lua_rawgeti(L, -i, i);
lua_remove(L, -i); /* remove table from the stack */
return n;
} }
static int handle_script (lua_State *L, char **argv, int n) { static int handle_script (lua_State *L, char **argv) {
int status; int status;
const char *fname = argv[0]; const char *fname = argv[0];
if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
fname = NULL; /* stdin */ fname = NULL; /* stdin */
status = luaL_loadfile(L, fname); status = luaL_loadfile(L, fname);
if (status == LUA_OK) { if (status == LUA_OK) {
pushargs(L, argv, n); /* push arguments to script */ int n = pushargs(L); /* push arguments to script */
status = docall(L, n - 1, LUA_MULTRET); status = docall(L, n, LUA_MULTRET);
} }
return report(L, status); return report(L, status);
} }
@ -570,7 +576,7 @@ static int pmain (lua_State *L) {
if (!runargs(L, argv, script)) /* execute arguments -e and -l */ if (!runargs(L, argv, script)) /* execute arguments -e and -l */
return 0; /* something failed */ return 0; /* something failed */
if (script < argc && /* execute main script (if there is one) */ if (script < argc && /* execute main script (if there is one) */
handle_script(L, argv + script, argc - script) != LUA_OK) handle_script(L, argv + script) != LUA_OK)
return 0; return 0;
if (args & has_i) /* -i option? */ if (args & has_i) /* -i option? */
doREPL(L); /* do read-eval-print loop */ doREPL(L); /* do read-eval-print loop */

Loading…
Cancel
Save