diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-06-26 14:08:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-06-26 14:08:52 -0300 |
| commit | 10200585a7d634b9d8688ecf8afab5b203619508 (patch) | |
| tree | 92e4ccd304b7ed7889fbb2b1b335a917fd77e246 | |
| parent | c94f11d783f617af701429bb9ebfc7fc96d6a2d9 (diff) | |
| download | lua-10200585a7d634b9d8688ecf8afab5b203619508.tar.gz lua-10200585a7d634b9d8688ecf8afab5b203619508.tar.bz2 lua-10200585a7d634b9d8688ecf8afab5b203619508.zip | |
var-arguments to the script come from 'arg' table (not from original
'argv' array)
| -rw-r--r-- | lua.c | 26 |
1 files changed, 16 insertions, 10 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.210 2014/02/26 15:27:56 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp roberto $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -417,25 +417,31 @@ static void doREPL (lua_State *L) { | |||
| 417 | 417 | ||
| 418 | 418 | ||
| 419 | /* | 419 | /* |
| 420 | ** Push on the stack 'n' strings from 'argv' | 420 | ** Push on the stack the contents of table 'arg' from 1 to #arg |
| 421 | */ | 421 | */ |
| 422 | static void pushargs (lua_State *L, char **argv, int n) { | 422 | static int pushargs (lua_State *L) { |
| 423 | int i; | 423 | int i, n; |
| 424 | lua_getglobal(L, "arg"); | ||
| 425 | if (!lua_istable(L, -1)) | ||
| 426 | luaL_error(L, "'arg' is not a table"); | ||
| 427 | n = (int)luaL_len(L, -1); | ||
| 424 | luaL_checkstack(L, n + 3, "too many arguments to script"); | 428 | luaL_checkstack(L, n + 3, "too many arguments to script"); |
| 425 | for (i = 1; i < n; i++) /* skip 0 (the script name) */ | 429 | for (i = 1; i <= n; i++) |
| 426 | lua_pushstring(L, argv[i]); | 430 | lua_rawgeti(L, -i, i); |
| 431 | lua_remove(L, -i); /* remove table from the stack */ | ||
| 432 | return n; | ||
| 427 | } | 433 | } |
| 428 | 434 | ||
| 429 | 435 | ||
| 430 | static int handle_script (lua_State *L, char **argv, int n) { | 436 | static int handle_script (lua_State *L, char **argv) { |
| 431 | int status; | 437 | int status; |
| 432 | const char *fname = argv[0]; | 438 | const char *fname = argv[0]; |
| 433 | if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) | 439 | if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0) |
| 434 | fname = NULL; /* stdin */ | 440 | fname = NULL; /* stdin */ |
| 435 | status = luaL_loadfile(L, fname); | 441 | status = luaL_loadfile(L, fname); |
| 436 | if (status == LUA_OK) { | 442 | if (status == LUA_OK) { |
| 437 | pushargs(L, argv, n); /* push arguments to script */ | 443 | int n = pushargs(L); /* push arguments to script */ |
| 438 | status = docall(L, n - 1, LUA_MULTRET); | 444 | status = docall(L, n, LUA_MULTRET); |
| 439 | } | 445 | } |
| 440 | return report(L, status); | 446 | return report(L, status); |
| 441 | } | 447 | } |
| @@ -570,7 +576,7 @@ static int pmain (lua_State *L) { | |||
| 570 | if (!runargs(L, argv, script)) /* execute arguments -e and -l */ | 576 | if (!runargs(L, argv, script)) /* execute arguments -e and -l */ |
| 571 | return 0; /* something failed */ | 577 | return 0; /* something failed */ |
| 572 | if (script < argc && /* execute main script (if there is one) */ | 578 | if (script < argc && /* execute main script (if there is one) */ |
| 573 | handle_script(L, argv + script, argc - script) != LUA_OK) | 579 | handle_script(L, argv + script) != LUA_OK) |
| 574 | return 0; | 580 | return 0; |
| 575 | if (args & has_i) /* -i option? */ | 581 | if (args & has_i) /* -i option? */ |
| 576 | doREPL(L); /* do read-eval-print loop */ | 582 | doREPL(L); /* do read-eval-print loop */ |
