Showing the changes to src/lua.c (1 of 51 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.200 2011/06/16 14:30:58 roberto Exp $ | 2 | ** $Id: lua.c,v 1.202 2011/08/17 20:19:52 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 | */ |
| @@ -118,6 +118,7 @@ static void print_usage (const char *badoption) { · 112 unmodified lines | |||
| 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 | , |
| @@ -348,7 +349,16 @@ static int handle_script (lua_State *L, char **argv, int n) { · 224 unmodified lines | |||
| 348 | #define noextrachars(x) {if ((x)[2] != '\0') return -1;} | 349 | #define noextrachars(x) {if ((x)[2] != '\0') return -1;} |
| 349 | 350 | ||
| 350 | 351 | ||
| 351 | static int collectargs (char **argv, int *pi, int *pv, int *pe) { | 352 | /* indices of various argument indicators in array args */ |
| 353 | #define has_i 0 /* -i */ | ||
| 354 | #define has_v 1 /* -v */ | ||
| 355 | #define has_e 2 /* -e */ | ||
| 356 | #define has_E 3 /* -E */ | ||
| 357 | |||
| 358 | #define num_has 4 /* number of 'has_*' */ | ||
| 359 | |||
| 360 | |||
| 361 | static int collectargs (char **argv, int *args) { | ||
| 352 | int i; | 362 | int i; |
| 353 | for (i = 1; argv[i] != NULL; i++) { | 363 | for (i = 1; argv[i] != NULL; i++) { |
| 354 | if (argv[i][0] != '-') /* not an option? */ | 364 | if (argv[i][0] != '-') /* not an option? */ |
| @@ -359,15 +369,18 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { · 4 unmodified lines | |||
| 359 | return (argv[i+1] != NULL ? i+1 : 0); | 369 | return (argv[i+1] != NULL ? i+1 : 0); |
| 360 | case '\0': | 370 | case '\0': |
| 361 | return i; | 371 | return i; |
| 372 | case 'E': | ||
| 373 | args[has_E] = 1; | ||
| 374 | break; | ||
| 362 | case 'i': | 375 | case 'i': |
| 363 | noextrachars(argv[i]); | 376 | noextrachars(argv[i]); |
| 364 | *pi = 1; /* go through */ | 377 | args[has_i] = 1; /* go through */ |
| 365 | case 'v': | 378 | case 'v': |
| 366 | noextrachars(argv[i]); | 379 | noextrachars(argv[i]); |
| 367 | *pv = 1; | 380 | args[has_v] = 1; |
| 368 | break; | 381 | break; |
| 369 | case 'e': | 382 | case 'e': |
| 370 | *pe = 1; /* go through */ | 383 | args[has_e] = 1; /* go through */ |
| 371 | case 'l': /* both options need an argument */ | 384 | case 'l': /* both options need an argument */ |
| 372 | if (argv[i][2] == '\0') { /* no concatenated argument? */ | 385 | if (argv[i][2] == '\0') { /* no concatenated argument? */ |
| 373 | i++; /* try next 'argv' */ | 386 | i++; /* try next 'argv' */ |
| @@ -426,32 +439,46 @@ static int handle_luainit (lua_State *L) { · 52 unmodified lines | |||
| 426 | } | 439 | } |
| 427 | 440 | ||
| 428 | 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 | |||
| 429 | static int pmain (lua_State *L) { | 454 | static int pmain (lua_State *L) { |
| 430 | int argc = (int)lua_tointeger(L, 1); | 455 | int argc = (int)lua_tointeger(L, 1); |
| 431 | char **argv = (char **)lua_touserdata(L, 2); | 456 | char **argv = (char **)lua_touserdata(L, 2); |
| 432 | int script; | 457 | int script; |
| 433 | int has_i = 0, has_v = 0, has_e = 0; | 458 | int args[num_has]; |
| 459 | args[has_i] = args[has_v] = args[has_e] = args[has_E] = 0; | ||
| 434 | if (argv[0] && argv[0][0]) progname = argv[0]; | 460 | if (argv[0] && argv[0][0]) progname = argv[0]; |
| 435 | script = collectargs(argv, &has_i, &has_v, &has_e); | 461 | script = collectargs(argv, args); |
| 436 | if (script < 0) { /* invalid arg? */ | 462 | if (script < 0) { /* invalid arg? */ |
| 437 | print_usage(argv[-script]); | 463 | print_usage(argv[-script]); |
| 438 | return 0; | 464 | return 0; |
| 439 | } | 465 | } |
| 440 | if (has_v) print_version(); | 466 | if (args[has_v]) print_version(); |
| 441 | /* open standard libraries */ | 467 | /* open standard libraries */ |
| 442 | luaL_checkversion(L); | 468 | luaL_checkversion(L); |
| 443 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ | 469 | lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
| 444 | luaL_openlibs(L); /* open libraries */ | 470 | luaL_openlibs(L); /* open libraries */ |
| 445 | lua_gc(L, LUA_GCRESTART, 0); | 471 | lua_gc(L, LUA_GCRESTART, 0); |
| 446 | /* run LUA_INIT */ | 472 | if (args[has_E]) /* avoid LUA_INIT? */ |
| 447 | if (handle_luainit(L) != LUA_OK) return 0; | 473 | resetpaths(L); |
| 474 | else if (handle_luainit(L) != LUA_OK) return 0; | ||
| 448 | /* execute arguments -e and -l */ | 475 | /* execute arguments -e and -l */ |
| 449 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; | 476 | if (!runargs(L, argv, (script > 0) ? script : argc)) return 0; |
| 450 | /* execute main script (if there is one) */ | 477 | /* execute main script (if there is one) */ |
| 451 | if (script && handle_script(L, argv, script) != LUA_OK) return 0; | 478 | if (script && handle_script(L, argv, script) != LUA_OK) return 0; |
| 452 | if (has_i) /* -i option? */ | 479 | if (args[has_i]) /* -i option? */ |
| 453 | dotty(L); | 480 | dotty(L); |
| 454 | else if (script == 0 && !has_e && !has_v) { /* no arguments? */ | 481 | else if (script == 0 && !args[has_e] && !args[has_v]) { /* no arguments? */ |
| 455 | if (lua_stdin_is_tty()) { | 482 | if (lua_stdin_is_tty()) { |
| 456 | print_version(); | 483 | print_version(); |
| 457 | dotty(L); | 484 | dotty(L); |