Showing the changes to src/lua.c (1 of 41 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.211 2014/06/05 20:42:06 roberto Exp $ | 2 | ** $Id: lua.c,v 1.213 2014/06/30 19:48:08 roberto Exp $ |
| 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 | */ |
| @@ -115,7 +115,7 @@ static void lstop (lua_State *L, lua_Debug *ar) { · 109 unmodified lines | |||
| 115 | 115 | ||
| 116 | /* | 116 | /* |
| 117 | ** Function to be called at a C signal. Because a C signal cannot | 117 | ** Function to be called at a C signal. Because a C signal cannot |
| 118 | ** just change a Lua state (as there is no proper syncronization), | 118 | ** just change a Lua state (as there is no proper synchronization), |
| 119 | ** this function only sets a hook that, when called, will stop the | 119 | ** this function only sets a hook that, when called, will stop the |
| 120 | ** interpreter. | 120 | ** interpreter. |
| 121 | */ | 121 | */ |
| @@ -284,7 +284,7 @@ static const char *get_prompt (lua_State *L, int firstline) { · 162 unmodified lines | |||
| 284 | /* | 284 | /* |
| 285 | ** Check whether 'status' signals a syntax error and the error | 285 | ** Check whether 'status' signals a syntax error and the error |
| 286 | ** message at the top of the stack ends with the above mark for | 286 | ** message at the top of the stack ends with the above mark for |
| 287 | ** incoplete statements. | 287 | ** incomplete statements. |
| 288 | */ | 288 | */ |
| 289 | static int incomplete (lua_State *L, int status) { | 289 | static int incomplete (lua_State *L, int status) { |
| 290 | if (status == LUA_ERRSYNTAX) { | 290 | if (status == LUA_ERRSYNTAX) { |
| @@ -417,25 +417,31 @@ static void doREPL (lua_State *L) { · 126 unmodified lines | |||
| 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) { · 128 unmodified lines | |||
| 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 */ |