mirror of
https://github.com/lua/lua
synced 2025-01-07 18:12:00 +03:00
Handling of LUA_PATH/LUA_CPATH moved back to 'package' library
to avoid incompatibilites with previous releases
This commit is contained in:
parent
08199ade4a
commit
94c1b3a8ee
97
loadlib.c
97
loadlib.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: loadlib.c,v 1.128 2016/07/18 17:55:59 roberto Exp roberto $
|
||||
** $Id: loadlib.c,v 1.129 2016/12/04 20:17:24 roberto Exp roberto $
|
||||
** Dynamic library loader for Lua
|
||||
** See Copyright Notice in lua.h
|
||||
**
|
||||
@ -64,6 +64,9 @@ static const int CLIBS = 0;
|
||||
#define LIB_FAIL "open"
|
||||
|
||||
|
||||
#define setprogdir(L) ((void)0)
|
||||
|
||||
|
||||
/*
|
||||
** system-dependent functions
|
||||
*/
|
||||
@ -155,6 +158,30 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||
#endif
|
||||
|
||||
|
||||
#undef setprogdir
|
||||
|
||||
|
||||
/*
|
||||
** 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 */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void pusherror (lua_State *L) {
|
||||
int error = GetLastError();
|
||||
char buffer[128];
|
||||
@ -223,6 +250,67 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
|
||||
#endif /* } */
|
||||
|
||||
|
||||
/*
|
||||
** {==================================================================
|
||||
** 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 AUXMARK "\1" /* auxiliary mark */
|
||||
|
||||
|
||||
/*
|
||||
** return registry.LUA_NOENV as a boolean
|
||||
*/
|
||||
static int noenv (lua_State *L) {
|
||||
int b;
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
||||
b = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1); /* remove value */
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Set a path
|
||||
*/
|
||||
static void setpath (lua_State *L, const char *fieldname,
|
||||
const char *envname,
|
||||
const char *dft) {
|
||||
const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX);
|
||||
const char *path = getenv(nver); /* use versioned name */
|
||||
if (path == NULL) /* no environment variable? */
|
||||
path = getenv(envname); /* try unversioned name */
|
||||
if (path == NULL || noenv(L)) /* no environment variable? */
|
||||
lua_pushstring(L, dft); /* use default */
|
||||
else {
|
||||
/* 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, dft);
|
||||
lua_remove(L, -2); /* remove result from 1st 'gsub' */
|
||||
}
|
||||
setprogdir(L);
|
||||
lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
|
||||
lua_pop(L, 1); /* pop versioned variable name */
|
||||
}
|
||||
|
||||
/* }================================================================== */
|
||||
|
||||
|
||||
/*
|
||||
** return registry.CLIBS[path]
|
||||
*/
|
||||
@ -680,10 +768,9 @@ LUAMOD_API int luaopen_package (lua_State *L) {
|
||||
createclibstable(L);
|
||||
luaL_newlib(L, pk_funcs); /* create 'package' table */
|
||||
createsearcherstable(L);
|
||||
lua_pushstring(L, LUA_PATH_DEFAULT);
|
||||
lua_setfield(L, -2, "path"); /* package.path = default path */
|
||||
lua_pushstring(L, LUA_CPATH_DEFAULT);
|
||||
lua_setfield(L, -2, "cpath"); /* package.cpath = default cpath */
|
||||
/* set paths */
|
||||
setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT);
|
||||
setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT);
|
||||
/* store config information */
|
||||
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
|
||||
LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
|
||||
|
93
lua.c
93
lua.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lua.c,v 1.228 2016/12/13 15:50:58 roberto Exp roberto $
|
||||
** $Id: lua.c,v 1.229 2016/12/22 13:08:50 roberto Exp roberto $
|
||||
** Lua stand-alone interpreter
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -20,8 +20,6 @@
|
||||
#include "lualib.h"
|
||||
|
||||
|
||||
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
|
||||
|
||||
|
||||
#if !defined(LUA_PROMPT)
|
||||
#define LUA_PROMPT "> "
|
||||
@ -534,88 +532,6 @@ 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_PATHVARVERSION LUA_PATH_VAR LUA_VERSUFFIX
|
||||
#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_VERSUFFIX
|
||||
|
||||
|
||||
#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);
|
||||
@ -648,10 +564,11 @@ 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 */
|
||||
|
5
lualib.h
5
lualib.h
@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp roberto $
|
||||
** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp roberto $
|
||||
** Lua standard libraries
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -11,6 +11,9 @@
|
||||
#include "lua.h"
|
||||
|
||||
|
||||
/* version suffix for environment variable names */
|
||||
#define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
|
||||
|
||||
|
||||
LUAMOD_API int (luaopen_base) (lua_State *L);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user