diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-22 14:47:12 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-12-22 14:47:12 -0200 |
| commit | cc1cbd19a0730744a5db45086a0f37137e4f7bef (patch) | |
| tree | 2443c50888badbbb20782572dbdd877e4d5ecfa1 | |
| parent | cb3f95d51696005e69d24b2bbc22f2fc3116b424 (diff) | |
| download | lua-cc1cbd19a0730744a5db45086a0f37137e4f7bef.tar.gz lua-cc1cbd19a0730744a5db45086a0f37137e4f7bef.tar.bz2 lua-cc1cbd19a0730744a5db45086a0f37137e4f7bef.zip | |
'lua_cpcall' is deprecated + other small changes in 'pmain' (comments
and reordering to check arguments before running any Lua code)
| -rw-r--r-- | lua.c | 59 |
1 files changed, 28 insertions, 31 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.180 2009/12/17 16:20:01 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.181 2009/12/22 15:32:50 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 | */ |
| @@ -405,63 +405,60 @@ static int handle_luainit (lua_State *L) { | |||
| 405 | } | 405 | } |
| 406 | 406 | ||
| 407 | 407 | ||
| 408 | struct Smain { | ||
| 409 | int argc; | ||
| 410 | char **argv; | ||
| 411 | int ok; | ||
| 412 | }; | ||
| 413 | |||
| 414 | |||
| 415 | static int pmain (lua_State *L) { | 408 | static int pmain (lua_State *L) { |
| 416 | struct Smain *s = (struct Smain *)lua_touserdata(L, 1); | 409 | int argc = lua_tointeger(L, 1); |
| 417 | char **argv = s->argv; | 410 | char **argv = (char **)lua_touserdata(L, 2); |
| 418 | int script; | 411 | int script; |
| 419 | int has_i = 0, has_v = 0, has_e = 0; | 412 | int has_i = 0, has_v = 0, has_e = 0; |
| 420 | if (argv[0] && argv[0][0]) progname = argv[0]; | 413 | if (argv[0] && argv[0][0]) progname = argv[0]; |
| 421 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | ||
| 422 | luaL_openlibs(L); /* open libraries */ | ||
| 423 | lua_gc(L, LUA_GCRESTART, 0); | ||
| 424 | luaL_checkversion(L); | ||
| 425 | s->ok = (handle_luainit(L) == LUA_OK); | ||
| 426 | if (!s->ok) return 0; | ||
| 427 | script = collectargs(argv, &has_i, &has_v, &has_e); | 414 | script = collectargs(argv, &has_i, &has_v, &has_e); |
| 428 | if (script < 0) { /* invalid args? */ | 415 | if (script < 0) { /* invalid args? */ |
| 429 | print_usage(); | 416 | print_usage(); |
| 430 | s->ok = 0; | ||
| 431 | return 0; | 417 | return 0; |
| 432 | } | 418 | } |
| 433 | if (has_v) print_version(); | 419 | if (has_v) print_version(); |
| 434 | s->ok = runargs(L, argv, (script > 0) ? script : s->argc); | 420 | /* open standard libraries */ |
| 435 | if (!s->ok) return 0; | 421 | luaL_checkversion(L); |
| 436 | if (script) | 422 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
| 437 | s->ok = (handle_script(L, argv, script) == LUA_OK); | 423 | luaL_openlibs(L); /* open libraries */ |
| 438 | if (!s->ok) return 0; | 424 | lua_gc(L, LUA_GCRESTART, 0); |
| 439 | if (has_i) | 425 | /* run LUA_INIT */ |
| 426 | if (handle_luainit(L) != LUA_OK) return 0; | ||
| 427 | /* execute arguments -e and -l */ | ||
| 428 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; | ||
| 429 | /* execute main script (if there is one) */ | ||
| 430 | if (script && handle_script(L, argv, script) != LUA_OK) return 0; | ||
| 431 | if (has_i) /* -i option? */ | ||
| 440 | dotty(L); | 432 | dotty(L); |
| 441 | else if (script == 0 && !has_e && !has_v) { | 433 | else if (script == 0 && !has_e && !has_v) { /* no arguments? */ |
| 442 | if (lua_stdin_is_tty()) { | 434 | if (lua_stdin_is_tty()) { |
| 443 | print_version(); | 435 | print_version(); |
| 444 | dotty(L); | 436 | dotty(L); |
| 445 | } | 437 | } |
| 446 | else dofile(L, NULL); /* executes stdin as a file */ | 438 | else dofile(L, NULL); /* executes stdin as a file */ |
| 447 | } | 439 | } |
| 448 | return 0; | 440 | lua_pushboolean(L, 1); /* signal no errors */ |
| 441 | return 1; | ||
| 449 | } | 442 | } |
| 450 | 443 | ||
| 451 | 444 | ||
| 452 | int main (int argc, char **argv) { | 445 | int main (int argc, char **argv) { |
| 453 | int status; | 446 | static lua_CFunction ppmain = &pmain; |
| 454 | struct Smain s; | 447 | int status, result; |
| 455 | lua_State *L = luaL_newstate(); /* create state */ | 448 | lua_State *L = luaL_newstate(); /* create state */ |
| 456 | if (L == NULL) { | 449 | if (L == NULL) { |
| 457 | l_message(argv[0], "cannot create state: not enough memory"); | 450 | l_message(argv[0], "cannot create state: not enough memory"); |
| 458 | return EXIT_FAILURE; | 451 | return EXIT_FAILURE; |
| 459 | } | 452 | } |
| 460 | s.argc = argc; | 453 | /* call 'pmain' in protected mode */ |
| 461 | s.argv = argv; | 454 | lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_CPCALL); /* calling function */ |
| 462 | status = lua_cpcall(L, &pmain, &s); | 455 | lua_pushlightuserdata(L, &ppmain); |
| 456 | lua_pushinteger(L, argc); | ||
| 457 | lua_pushlightuserdata(L, argv); | ||
| 458 | status = lua_pcall(L, 3, 1, 0); | ||
| 459 | result = lua_toboolean(L, -1); /* get result */ | ||
| 463 | finalreport(L, status); | 460 | finalreport(L, status); |
| 464 | lua_close(L); | 461 | lua_close(L); |
| 465 | return (s.ok && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; | 462 | return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 466 | } | 463 | } |
| 467 | 464 | ||
