aboutsummaryrefslogtreecommitdiff
path: root/loadlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-07-18 14:55:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-07-18 14:55:59 -0300
commitde3fd8ab83763960433283a07d32e7a6bb986ea8 (patch)
tree50b522bf8019527322aa0d400788e82d9049f470 /loadlib.c
parent788109a3de05462f19c5c05033581c1eab9e9283 (diff)
downloadlua-de3fd8ab83763960433283a07d32e7a6bb986ea8.tar.gz
lua-de3fd8ab83763960433283a07d32e7a6bb986ea8.tar.bz2
lua-de3fd8ab83763960433283a07d32e7a6bb986ea8.zip
Handling of LUA_PATH/LUA_CPATH moved from package library to stand
alone interpreter (so that 'lua.c' concentrates all handling of environment variables)
Diffstat (limited to 'loadlib.c')
-rw-r--r--loadlib.c94
1 files changed, 5 insertions, 89 deletions
diff --git a/loadlib.c b/loadlib.c
index 7a9b011f..24910351 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loadlib.c,v 1.126 2015/02/16 13:14:33 roberto Exp roberto $ 2** $Id: loadlib.c,v 1.127 2015/11/23 11:30:45 roberto Exp roberto $
3** Dynamic library loader for Lua 3** Dynamic library loader for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5** 5**
@@ -25,40 +25,9 @@
25 25
26 26
27/* 27/*
28** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment
29** variables that Lua check to set its paths.
30*/
31#if !defined(LUA_PATH_VAR)
32#define LUA_PATH_VAR "LUA_PATH"
33#endif
34
35#if !defined(LUA_CPATH_VAR)
36#define LUA_CPATH_VAR "LUA_CPATH"
37#endif
38
39#define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR
40
41#define LUA_PATHVARVERSION LUA_PATH_VAR LUA_PATHSUFFIX
42#define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_PATHSUFFIX
43
44/*
45** LUA_PATH_SEP is the character that separates templates in a path.
46** LUA_PATH_MARK is the string that marks the substitution points in a
47** template.
48** LUA_EXEC_DIR in a Windows path is replaced by the executable's
49** directory.
50** LUA_IGMARK is a mark to ignore all before it when building the 28** LUA_IGMARK is a mark to ignore all before it when building the
51** luaopen_ function name. 29** luaopen_ function name.
52*/ 30*/
53#if !defined (LUA_PATH_SEP)
54#define LUA_PATH_SEP ";"
55#endif
56#if !defined (LUA_PATH_MARK)
57#define LUA_PATH_MARK "?"
58#endif
59#if !defined (LUA_EXEC_DIR)
60#define LUA_EXEC_DIR "!"
61#endif
62#if !defined (LUA_IGMARK) 31#if !defined (LUA_IGMARK)
63#define LUA_IGMARK "-" 32#define LUA_IGMARK "-"
64#endif 33#endif
@@ -94,8 +63,6 @@ static const int CLIBS = 0;
94 63
95#define LIB_FAIL "open" 64#define LIB_FAIL "open"
96 65
97#define setprogdir(L) ((void)0)
98
99 66
100/* 67/*
101** system-dependent functions 68** system-dependent functions
@@ -179,7 +146,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
179 146
180#include <windows.h> 147#include <windows.h>
181 148
182#undef setprogdir
183 149
184/* 150/*
185** optional flags for LoadLibraryEx 151** optional flags for LoadLibraryEx
@@ -189,21 +155,6 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
189#endif 155#endif
190 156
191 157
192static void setprogdir (lua_State *L) {
193 char buff[MAX_PATH + 1];
194 char *lb;
195 DWORD nsize = sizeof(buff)/sizeof(char);
196 DWORD n = GetModuleFileNameA(NULL, buff, nsize);
197 if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
198 luaL_error(L, "unable to get ModuleFileName");
199 else {
200 *lb = '\0';
201 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
202 lua_remove(L, -2); /* remove original string */
203 }
204}
205
206
207static void pusherror (lua_State *L) { 158static void pusherror (lua_State *L) {
208 int error = GetLastError(); 159 int error = GetLastError();
209 char buffer[128]; 160 char buffer[128];
@@ -666,41 +617,6 @@ static int ll_seeall (lua_State *L) {
666 617
667 618
668 619
669/* auxiliary mark (for internal use) */
670#define AUXMARK "\1"
671
672
673/*
674** return registry.LUA_NOENV as a boolean
675*/
676static int noenv (lua_State *L) {
677 int b;
678 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
679 b = lua_toboolean(L, -1);
680 lua_pop(L, 1); /* remove value */
681 return b;
682}
683
684
685static void setpath (lua_State *L, const char *fieldname, const char *envname1,
686 const char *envname2, const char *def) {
687 const char *path = getenv(envname1);
688 if (path == NULL) /* no environment variable? */
689 path = getenv(envname2); /* try alternative name */
690 if (path == NULL || noenv(L)) /* no environment variable? */
691 lua_pushstring(L, def); /* use default */
692 else {
693 /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
694 path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP,
695 LUA_PATH_SEP AUXMARK LUA_PATH_SEP);
696 luaL_gsub(L, path, AUXMARK, def);
697 lua_remove(L, -2);
698 }
699 setprogdir(L);
700 lua_setfield(L, -2, fieldname);
701}
702
703
704static const luaL_Reg pk_funcs[] = { 620static const luaL_Reg pk_funcs[] = {
705 {"loadlib", ll_loadlib}, 621 {"loadlib", ll_loadlib},
706 {"searchpath", ll_searchpath}, 622 {"searchpath", ll_searchpath},
@@ -764,10 +680,10 @@ LUAMOD_API int luaopen_package (lua_State *L) {
764 createclibstable(L); 680 createclibstable(L);
765 luaL_newlib(L, pk_funcs); /* create 'package' table */ 681 luaL_newlib(L, pk_funcs); /* create 'package' table */
766 createsearcherstable(L); 682 createsearcherstable(L);
767 /* set field 'path' */ 683 lua_pushstring(L, LUA_PATH_DEFAULT);
768 setpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, LUA_PATH_DEFAULT); 684 lua_setfield(L, -2, "path"); /* package.path = default path */
769 /* set field 'cpath' */ 685 lua_pushstring(L, LUA_CPATH_DEFAULT);
770 setpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, LUA_CPATH_DEFAULT); 686 lua_setfield(L, -2, "cpath"); /* package.cpath = default cpath */
771 /* store config information */ 687 /* store config information */
772 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" 688 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
773 LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); 689 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");