From Lua 5.2: Add -E command line option (ignore env vars).

master
Mike Pall 2012-07-16 22:47:01 +02:00
parent bf2d4acf00
commit 63bb052bbe
3 changed files with 30 additions and 11 deletions

View File

@ -41,6 +41,9 @@ Run in interactive mode.
.B "\-v"
Show \fBLuaJIT\fR version.
.TP
.B "\-E"
Ignore environment variables.
.TP
.B "\-\-"
Stop processing options.
.TP

View File

@ -517,7 +517,7 @@ static int lj_cf_package_seeall(lua_State *L)
#define AUXMARK "\1"
static void setpath(lua_State *L, const char *fieldname, const char *envname,
const char *def)
const char *def, int noenv)
{
#if LJ_TARGET_CONSOLE
const char *path = NULL;
@ -525,7 +525,7 @@ static void setpath(lua_State *L, const char *fieldname, const char *envname,
#else
const char *path = getenv(envname);
#endif
if (path == NULL) {
if (path == NULL || noenv) {
lua_pushstring(L, def);
} else {
path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,
@ -562,6 +562,7 @@ static const lua_CFunction package_loaders[] =
LUALIB_API int luaopen_package(lua_State *L)
{
int i;
int noenv;
luaL_newmetatable(L, "_LOADLIB");
lj_lib_pushcf(L, lj_cf_package_unloadlib, 1);
lua_setfield(L, -2, "__gc");
@ -574,8 +575,11 @@ LUALIB_API int luaopen_package(lua_State *L)
lua_rawseti(L, -2, i+1);
}
lua_setfield(L, -2, "loaders");
setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT);
setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT);
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
noenv = lua_toboolean(L, -1);
lua_pop(L, 1);
setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv);
setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv);
lua_pushliteral(L, LUA_PATH_CONFIG);
lua_setfield(L, -2, "config");
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);

View File

@ -71,6 +71,7 @@ static void print_usage(void)
" -O[opt] Control LuaJIT optimizations.\n"
" -i Enter interactive mode after executing " LUA_QL("script") ".\n"
" -v Show version information.\n"
" -E Ignore environment variables.\n"
" -- Stop handling options.\n"
" - Execute stdin and stop handling options.\n"
,
@ -406,6 +407,7 @@ static int dobytecode(lua_State *L, char **argv)
#define FLAGS_VERSION 2
#define FLAGS_EXEC 4
#define FLAGS_OPTION 8
#define FLAGS_NOENV 16
static int collectargs(char **argv, int *flags)
{
@ -442,6 +444,9 @@ static int collectargs(char **argv, int *flags)
if (*flags) return -1;
*flags |= FLAGS_EXEC;
return 0;
case 'E':
*flags |= FLAGS_NOENV;
break;
default: return -1; /* invalid option */
}
}
@ -521,23 +526,30 @@ static int pmain(lua_State *L)
globalL = L;
if (argv[0] && argv[0][0]) progname = argv[0];
LUAJIT_VERSION_SYM(); /* linker-enforced version check */
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
luaL_openlibs(L); /* open libraries */
lua_gc(L, LUA_GCRESTART, -1);
s->status = handle_luainit(L);
if (s->status != 0) return 0;
script = collectargs(argv, &flags);
if (script < 0) { /* invalid args? */
print_usage();
s->status = 1;
return 0;
}
if ((flags & FLAGS_NOENV)) {
lua_pushboolean(L, 1);
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
}
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
luaL_openlibs(L); /* open libraries */
lua_gc(L, LUA_GCRESTART, -1);
if (!(flags & FLAGS_NOENV)) {
s->status = handle_luainit(L);
if (s->status != 0) return 0;
}
if ((flags & FLAGS_VERSION)) print_version();
s->status = runargs(L, argv, (script > 0) ? script : s->argc);
if (s->status != 0) return 0;
if (script)
if (script) {
s->status = handle_script(L, argv, script);
if (s->status != 0) return 0;
if (s->status != 0) return 0;
}
if ((flags & FLAGS_INTERACTIVE)) {
print_jit_status(L);
dotty(L);