diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-08-17 17:19:52 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-08-17 17:19:52 -0300 |
| commit | 166dd0261a0fc012808a6448a1c46030c8db4806 (patch) | |
| tree | 3efe5571a7ee26c446a3b0bc2b62317da36dcb58 | |
| parent | 51471ba74821d7caee9e4c9dd11faf70d614f533 (diff) | |
| download | lua-166dd0261a0fc012808a6448a1c46030c8db4806.tar.gz lua-166dd0261a0fc012808a6448a1c46030c8db4806.tar.bz2 lua-166dd0261a0fc012808a6448a1c46030c8db4806.zip | |
new option '-E' to avoid environment variables
| -rw-r--r-- | lua.c | 30 |
1 files changed, 25 insertions, 5 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.200 2011/06/16 14:30:58 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.201 2011/08/04 18:16:16 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 | */ |
| @@ -118,6 +118,7 @@ static void print_usage (const char *badoption) { | |||
| 118 | " -i enter interactive mode after executing " LUA_QL("script") "\n" | 118 | " -i enter interactive mode after executing " LUA_QL("script") "\n" |
| 119 | " -l name require library " LUA_QL("name") "\n" | 119 | " -l name require library " LUA_QL("name") "\n" |
| 120 | " -v show version information\n" | 120 | " -v show version information\n" |
| 121 | " -E ignore environment variables\n" | ||
| 121 | " -- stop handling options\n" | 122 | " -- stop handling options\n" |
| 122 | " - stop handling options and execute stdin\n" | 123 | " - stop handling options and execute stdin\n" |
| 123 | , | 124 | , |
| @@ -352,6 +353,9 @@ static int handle_script (lua_State *L, char **argv, int n) { | |||
| 352 | #define has_i 0 /* -i */ | 353 | #define has_i 0 /* -i */ |
| 353 | #define has_v 1 /* -v */ | 354 | #define has_v 1 /* -v */ |
| 354 | #define has_e 2 /* -e */ | 355 | #define has_e 2 /* -e */ |
| 356 | #define has_E 3 /* -E */ | ||
| 357 | |||
| 358 | #define num_has 4 /* number of 'has_*' */ | ||
| 355 | 359 | ||
| 356 | 360 | ||
| 357 | static int collectargs (char **argv, int *args) { | 361 | static int collectargs (char **argv, int *args) { |
| @@ -365,6 +369,9 @@ static int collectargs (char **argv, int *args) { | |||
| 365 | return (argv[i+1] != NULL ? i+1 : 0); | 369 | return (argv[i+1] != NULL ? i+1 : 0); |
| 366 | case '\0': | 370 | case '\0': |
| 367 | return i; | 371 | return i; |
| 372 | case 'E': | ||
| 373 | args[has_E] = 1; | ||
| 374 | break; | ||
| 368 | case 'i': | 375 | case 'i': |
| 369 | noextrachars(argv[i]); | 376 | noextrachars(argv[i]); |
| 370 | args[has_i] = 1; /* go through */ | 377 | args[has_i] = 1; /* go through */ |
| @@ -432,12 +439,24 @@ static int handle_luainit (lua_State *L) { | |||
| 432 | } | 439 | } |
| 433 | 440 | ||
| 434 | 441 | ||
| 442 | static void resetpaths (lua_State *L) { | ||
| 443 | lua_getglobal(L, "package"); | ||
| 444 | if (!lua_istable(L, -1)) /* no module 'package'? */ | ||
| 445 | return; /* nothing to be done */ | ||
| 446 | lua_pushliteral(L, LUA_PATH_DEFAULT); | ||
| 447 | lua_setfield(L, -2, "path"); /* package.path = default */ | ||
| 448 | lua_pushliteral(L, LUA_CPATH_DEFAULT); | ||
| 449 | lua_setfield(L, -2, "cpath"); /* package.cpath = default */ | ||
| 450 | lua_pop(L, 1); /* remove 'package' */ | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 435 | static int pmain (lua_State *L) { | 454 | static int pmain (lua_State *L) { |
| 436 | int argc = (int)lua_tointeger(L, 1); | 455 | int argc = (int)lua_tointeger(L, 1); |
| 437 | char **argv = (char **)lua_touserdata(L, 2); | 456 | char **argv = (char **)lua_touserdata(L, 2); |
| 438 | int script; | 457 | int script; |
| 439 | int args[3]; | 458 | int args[num_has]; |
| 440 | args[has_i] = args[has_v] = args[has_e] = 0; | 459 | args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; |
| 441 | if (argv[0] && argv[0][0]) progname = argv[0]; | 460 | if (argv[0] && argv[0][0]) progname = argv[0]; |
| 442 | script = collectargs(argv, args); | 461 | script = collectargs(argv, args); |
| 443 | if (script < 0) { /* invalid arg? */ | 462 | if (script < 0) { /* invalid arg? */ |
| @@ -450,8 +469,9 @@ static int pmain (lua_State *L) { | |||
| 450 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | 469 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
| 451 | luaL_openlibs(L); /* open libraries */ | 470 | luaL_openlibs(L); /* open libraries */ |
| 452 | lua_gc(L, LUA_GCRESTART, 0); | 471 | lua_gc(L, LUA_GCRESTART, 0); |
| 453 | /* run LUA_INIT */ | 472 | if (args[has_E]) /* avoid LUA_INIT? */ |
| 454 | if (handle_luainit(L) != LUA_OK) return 0; | 473 | resetpaths(L); |
| 474 | else if (handle_luainit(L) != LUA_OK) return 0; | ||
| 455 | /* execute arguments -e and -l */ | 475 | /* execute arguments -e and -l */ |
| 456 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; | 476 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; |
| 457 | /* execute main script (if there is one) */ | 477 | /* execute main script (if there is one) */ |
