environment variables consulted by Lua may be version-specific

This commit is contained in:
Roberto Ierusalimschy 2010-07-25 12:03:37 -03:00
parent 85c1461422
commit 73b0a3451d
2 changed files with 32 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: loadlib.c,v 1.86 2010/06/30 17:40:27 roberto Exp roberto $ ** $Id: loadlib.c,v 1.87 2010/07/02 11:38:13 roberto Exp roberto $
** Dynamic library loader for Lua ** Dynamic library loader for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
** **
@ -23,17 +23,21 @@
/* /*
** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment ** LUA_PATH and LUA_CPATH are the names of the environment
** variables that Lua check to set its paths. ** variables that Lua check to set its paths.
*/ */
#if !defined(LUA_PATH_VAR) #if !defined(LUA_PATH)
#define LUA_PATH_VAR "LUA_PATH" #define LUA_PATH "LUA_PATH"
#endif #endif
#if !defined(LUA_CPATH_VAR) #if !defined(LUA_CPATH)
#define LUA_CPATH_VAR "LUA_CPATH" #define LUA_CPATH "LUA_CPATH"
#endif #endif
#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
#define LUA_PATHVERSION LUA_PATH LUA_PATHSUFFIX
#define LUA_CPATHVERSION LUA_CPATH LUA_PATHSUFFIX
/* /*
** LUA_PATH_SEP is the character that separates templates in a path. ** LUA_PATH_SEP is the character that separates templates in a path.
@ -573,9 +577,11 @@ static int ll_seeall (lua_State *L) {
/* auxiliary mark (for internal use) */ /* auxiliary mark (for internal use) */
#define AUXMARK "\1" #define AUXMARK "\1"
static void setpath (lua_State *L, const char *fieldname, const char *envname, static void setpath (lua_State *L, const char *fieldname, const char *envname1,
const char *def) { const char *envname2, const char *def) {
const char *path = getenv(envname); const char *path = getenv(envname1);
if (path == NULL) /* no environment variable? */
path = getenv(envname2); /* try alternative name */
if (path == NULL) /* no environment variable? */ if (path == NULL) /* no environment variable? */
lua_pushstring(L, def); /* use default */ lua_pushstring(L, def); /* use default */
else { else {
@ -626,8 +632,10 @@ LUAMOD_API int luaopen_package (lua_State *L) {
lua_rawseti(L, -2, i+1); lua_rawseti(L, -2, i+1);
} }
lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); /* set field `path' */ /* set field 'path' */
setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); /* set field `cpath' */ setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);
/* set field 'cpath' */
setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);
/* store config information */ /* store config information */
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); LUA_EXEC_DIR "\n" LUA_IGMARK "\n");

18
lua.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.c,v 1.190 2010/04/14 15:14:21 roberto Exp roberto $ ** $Id: lua.c,v 1.191 2010/07/02 17:36:32 roberto Exp roberto $
** Lua stand-alone interpreter ** Lua stand-alone interpreter
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -31,10 +31,13 @@
#define LUA_MAXINPUT 512 #define LUA_MAXINPUT 512
#endif #endif
#if !defined(LUA_INIT_VAR) #if !defined(LUA_INIT)
#define LUA_INIT_VAR "LUA_INIT" #define LUA_INIT "LUA_INIT"
#endif #endif
#define LUA_INITVERSION \
LUA_INIT "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
/* /*
** lua_stdin_is_tty detects whether the standard input is a 'tty' (that ** lua_stdin_is_tty detects whether the standard input is a 'tty' (that
@ -409,12 +412,17 @@ static int runargs (lua_State *L, char **argv, int n) {
static int handle_luainit (lua_State *L) { static int handle_luainit (lua_State *L) {
const char *init = getenv(LUA_INIT_VAR); const char *name = "=" LUA_INITVERSION;
const char *init = getenv(name + 1);
if (init == NULL) {
name = "=" LUA_INIT;
init = getenv(name + 1); /* try alternative name */
}
if (init == NULL) return LUA_OK; if (init == NULL) return LUA_OK;
else if (init[0] == '@') else if (init[0] == '@')
return dofile(L, init+1); return dofile(L, init+1);
else else
return dostring(L, init, "=" LUA_INIT_VAR); return dostring(L, init, name);
} }