History

Lua 5.2.0-rc1

Lua Team committed 14 years ago

Commit d74d0e51 parent 8203bab51 files · +1861 −1136Browse files

Showing the changes to src/lua.c (1 of 51 files). Show all

Unified · Split

src/lua.c+39−12
@@ -1,5 +1,5 @@
11 /*
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 $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -118,6 +118,7 @@ static void print_usage (const char *badoption) { · 112 unmodified lines
118118 " -i enter interactive mode after executing " LUA_QL("script") "\n"
119119 " -l name require library " LUA_QL("name") "\n"
120120 " -v show version information\n"
121+ " -E ignore environment variables\n"
121122 " -- stop handling options\n"
122123 " - stop handling options and execute stdin\n"
123124 ,
@@ -348,7 +349,16 @@ static int handle_script (lua_State *L, char **argv, int n) { · 224 unmodified lines
348349 #define noextrachars(x) {if ((x)[2] != '\0') return -1;}
349350
350351
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) {
352362 int i;
353363 for (i = 1; argv[i] != NULL; i++) {
354364 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
359369 return (argv[i+1] != NULL ? i+1 : 0);
360370 case '\0':
361371 return i;
372+ case 'E':
373+ args[has_E] = 1;
374+ break;
362375 case 'i':
363376 noextrachars(argv[i]);
364− *pi = 1; /* go through */
377+ args[has_i] = 1; /* go through */
365378 case 'v':
366379 noextrachars(argv[i]);
367− *pv = 1;
380+ args[has_v] = 1;
368381 break;
369382 case 'e':
370− *pe = 1; /* go through */
383+ args[has_e] = 1; /* go through */
371384 case 'l': /* both options need an argument */
372385 if (argv[i][2] == '\0') { /* no concatenated argument? */
373386 i++; /* try next 'argv' */
@@ -426,32 +439,46 @@ static int handle_luainit (lua_State *L) { · 52 unmodified lines
426439 }
427440
428441
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+
429454 static int pmain (lua_State *L) {
430455 int argc = (int)lua_tointeger(L, 1);
431456 char **argv = (char **)lua_touserdata(L, 2);
432457 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;
434460 if (argv[0] && argv[0][0]) progname = argv[0];
435− script = collectargs(argv, &has_i, &has_v, &has_e);
461+ script = collectargs(argv, args);
436462 if (script < 0) { /* invalid arg? */
437463 print_usage(argv[-script]);
438464 return 0;
439465 }
440− if (has_v) print_version();
466+ if (args[has_v]) print_version();
441467 /* open standard libraries */
442468 luaL_checkversion(L);
443469 lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
444470 luaL_openlibs(L); /* open libraries */
445471 lua_gc(L, LUA_GCRESTART, 0);
446− /* run LUA_INIT */
447− if (handle_luainit(L) != LUA_OK) return 0;
472+ if (args[has_E]) /* avoid LUA_INIT? */
473+ resetpaths(L);
474+ else if (handle_luainit(L) != LUA_OK) return 0;
448475 /* execute arguments -e and -l */
449476 if (!runargs(L, argv, (script > 0) ? script : argc)) return 0;
450477 /* execute main script (if there is one) */
451478 if (script && handle_script(L, argv, script) != LUA_OK) return 0;
452− if (has_i) /* -i option? */
479+ if (args[has_i]) /* -i option? */
453480 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? */
455482 if (lua_stdin_is_tty()) {
456483 print_version();
457484 dotty(L);