From de3fd8ab83763960433283a07d32e7a6bb986ea8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 18 Jul 2016 14:55:59 -0300 Subject: Handling of LUA_PATH/LUA_CPATH moved from package library to stand alone interpreter (so that 'lua.c' concentrates all handling of environment variables) --- lua.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 91 insertions(+), 5 deletions(-) (limited to 'lua.c') diff --git a/lua.c b/lua.c index 898c6869..8bd470ae 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.225 2015/03/30 15:42:59 roberto Exp roberto $ +** $Id: lua.c,v 1.226 2015/08/14 19:11:20 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -55,6 +55,8 @@ #elif defined(LUA_USE_WINDOWS) /* }{ */ #include +#include + #define lua_stdin_is_tty() _isatty(_fileno(stdin)) #else /* }{ */ @@ -529,6 +531,91 @@ static int runargs (lua_State *L, char **argv, int n) { } + +/* +** {================================================================== +** Set Paths +** =================================================================== +*/ + +/* +** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment +** variables that Lua check to set its paths. +*/ +#if !defined(LUA_PATH_VAR) +#define LUA_PATH_VAR "LUA_PATH" +#endif + +#if !defined(LUA_CPATH_VAR) +#define LUA_CPATH_VAR "LUA_CPATH" +#endif + +#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR + +#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_PATHSUFFIX +#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_PATHSUFFIX + + +#define AUXMARK "\1" /* auxiliary mark */ + + +#if defined(LUA_USE_WINDOWS) + + +/* +** Replace in the path (on the top of the stack) any occurrence +** of LUA_EXEC_DIR with the executable's path. +*/ +static void setprogdir (lua_State *L) { + char buff[MAX_PATH + 1]; + char *lb; + DWORD nsize = sizeof(buff)/sizeof(char); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) + luaL_error(L, "unable to get ModuleFileName"); + else { + *lb = '\0'; /* cut name on the last '\\' to get the path */ + luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); + lua_remove(L, -2); /* remove original string */ + } +} + +#else + +#define setprogdir(L) ((void)0) + +#endif + +/* +** Change a path according to corresponding environment variables +*/ +static void chgpath (lua_State *L, const char *fieldname, + const char *envname1, + const char *envname2, + int noenv) { + const char *path = getenv(envname1); + lua_getglobal(L, LUA_LOADLIBNAME); /* get 'package' table */ + lua_getfield(L, -1, fieldname); /* get original path */ + if (path == NULL) /* no environment variable? */ + path = getenv(envname2); /* try alternative name */ + if (path == NULL || noenv) /* no environment variable? */ + lua_pushvalue(L, -1); /* use original value */ + else { + const char *def = lua_tostring(L, -1); /* default path */ + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, + LUA_PATH_SEP AUXMARK LUA_PATH_SEP); + luaL_gsub(L, path, AUXMARK, def); + lua_remove(L, -2); /* remove result from 1st 'gsub' */ + } + setprogdir(L); + lua_setfield(L, -3, fieldname); /* set path value */ + lua_pop(L, 2); /* pop 'package' table and original path */ +} + +/* }================================================================== */ + + static int handle_luainit (lua_State *L) { const char *name = "=" LUA_INITVARVERSION; const char *init = getenv(name + 1); @@ -561,11 +648,10 @@ static int pmain (lua_State *L) { } if (args & has_v) /* option '-v'? */ print_version(); - if (args & has_E) { /* option '-E'? */ - lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ - lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); - } luaL_openlibs(L); /* open standard libraries */ + /* change paths according to env variables */ + chgpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, (args & has_E)); + chgpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, (args & has_E)); createargtable(L, argv, argc, script); /* create table 'arg' */ if (!(args & has_E)) { /* no option '-E'? */ if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ -- cgit v1.2.3-55-g6feb