diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-01-12 15:14:26 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-01-12 15:14:26 -0200 |
commit | 94c1b3a8eef6778cf995b906d77c0c066c6d55f3 (patch) | |
tree | 1b7cd3a0d95a655d9e5016046e56063ab548aeba | |
parent | 08199ade4ace6addd63148df4c596d6b61def8cd (diff) | |
download | lua-94c1b3a8eef6778cf995b906d77c0c066c6d55f3.tar.gz lua-94c1b3a8eef6778cf995b906d77c0c066c6d55f3.tar.bz2 lua-94c1b3a8eef6778cf995b906d77c0c066c6d55f3.zip |
Handling of LUA_PATH/LUA_CPATH moved back to 'package' library
to avoid incompatibilites with previous releases
-rw-r--r-- | loadlib.c | 97 | ||||
-rw-r--r-- | lua.c | 93 | ||||
-rw-r--r-- | lualib.h | 5 |
3 files changed, 101 insertions, 94 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.128 2016/07/18 17:55:59 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.129 2016/12/04 20:17:24 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 | ** |
@@ -64,6 +64,9 @@ static const int CLIBS = 0; | |||
64 | #define LIB_FAIL "open" | 64 | #define LIB_FAIL "open" |
65 | 65 | ||
66 | 66 | ||
67 | #define setprogdir(L) ((void)0) | ||
68 | |||
69 | |||
67 | /* | 70 | /* |
68 | ** system-dependent functions | 71 | ** system-dependent functions |
69 | */ | 72 | */ |
@@ -155,6 +158,30 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { | |||
155 | #endif | 158 | #endif |
156 | 159 | ||
157 | 160 | ||
161 | #undef setprogdir | ||
162 | |||
163 | |||
164 | /* | ||
165 | ** Replace in the path (on the top of the stack) any occurrence | ||
166 | ** of LUA_EXEC_DIR with the executable's path. | ||
167 | */ | ||
168 | static void setprogdir (lua_State *L) { | ||
169 | char buff[MAX_PATH + 1]; | ||
170 | char *lb; | ||
171 | DWORD nsize = sizeof(buff)/sizeof(char); | ||
172 | DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ | ||
173 | if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) | ||
174 | luaL_error(L, "unable to get ModuleFileName"); | ||
175 | else { | ||
176 | *lb = '\0'; /* cut name on the last '\\' to get the path */ | ||
177 | luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); | ||
178 | lua_remove(L, -2); /* remove original string */ | ||
179 | } | ||
180 | } | ||
181 | |||
182 | |||
183 | |||
184 | |||
158 | static void pusherror (lua_State *L) { | 185 | static void pusherror (lua_State *L) { |
159 | int error = GetLastError(); | 186 | int error = GetLastError(); |
160 | char buffer[128]; | 187 | char buffer[128]; |
@@ -224,6 +251,67 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { | |||
224 | 251 | ||
225 | 252 | ||
226 | /* | 253 | /* |
254 | ** {================================================================== | ||
255 | ** Set Paths | ||
256 | ** =================================================================== | ||
257 | */ | ||
258 | |||
259 | /* | ||
260 | ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment | ||
261 | ** variables that Lua check to set its paths. | ||
262 | */ | ||
263 | #if !defined(LUA_PATH_VAR) | ||
264 | #define LUA_PATH_VAR "LUA_PATH" | ||
265 | #endif | ||
266 | |||
267 | #if !defined(LUA_CPATH_VAR) | ||
268 | #define LUA_CPATH_VAR "LUA_CPATH" | ||
269 | #endif | ||
270 | |||
271 | |||
272 | #define AUXMARK "\1" /* auxiliary mark */ | ||
273 | |||
274 | |||
275 | /* | ||
276 | ** return registry.LUA_NOENV as a boolean | ||
277 | */ | ||
278 | static int noenv (lua_State *L) { | ||
279 | int b; | ||
280 | lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); | ||
281 | b = lua_toboolean(L, -1); | ||
282 | lua_pop(L, 1); /* remove value */ | ||
283 | return b; | ||
284 | } | ||
285 | |||
286 | |||
287 | /* | ||
288 | ** Set a path | ||
289 | */ | ||
290 | static void setpath (lua_State *L, const char *fieldname, | ||
291 | const char *envname, | ||
292 | const char *dft) { | ||
293 | const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); | ||
294 | const char *path = getenv(nver); /* use versioned name */ | ||
295 | if (path == NULL) /* no environment variable? */ | ||
296 | path = getenv(envname); /* try unversioned name */ | ||
297 | if (path == NULL || noenv(L)) /* no environment variable? */ | ||
298 | lua_pushstring(L, dft); /* use default */ | ||
299 | else { | ||
300 | /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ | ||
301 | path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, | ||
302 | LUA_PATH_SEP AUXMARK LUA_PATH_SEP); | ||
303 | luaL_gsub(L, path, AUXMARK, dft); | ||
304 | lua_remove(L, -2); /* remove result from 1st 'gsub' */ | ||
305 | } | ||
306 | setprogdir(L); | ||
307 | lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ | ||
308 | lua_pop(L, 1); /* pop versioned variable name */ | ||
309 | } | ||
310 | |||
311 | /* }================================================================== */ | ||
312 | |||
313 | |||
314 | /* | ||
227 | ** return registry.CLIBS[path] | 315 | ** return registry.CLIBS[path] |
228 | */ | 316 | */ |
229 | static void *checkclib (lua_State *L, const char *path) { | 317 | static void *checkclib (lua_State *L, const char *path) { |
@@ -680,10 +768,9 @@ LUAMOD_API int luaopen_package (lua_State *L) { | |||
680 | createclibstable(L); | 768 | createclibstable(L); |
681 | luaL_newlib(L, pk_funcs); /* create 'package' table */ | 769 | luaL_newlib(L, pk_funcs); /* create 'package' table */ |
682 | createsearcherstable(L); | 770 | createsearcherstable(L); |
683 | lua_pushstring(L, LUA_PATH_DEFAULT); | 771 | /* set paths */ |
684 | lua_setfield(L, -2, "path"); /* package.path = default path */ | 772 | setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); |
685 | lua_pushstring(L, LUA_CPATH_DEFAULT); | 773 | setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); |
686 | lua_setfield(L, -2, "cpath"); /* package.cpath = default cpath */ | ||
687 | /* store config information */ | 774 | /* store config information */ |
688 | lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" | 775 | lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" |
689 | LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); | 776 | LUA_EXEC_DIR "\n" LUA_IGMARK "\n"); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.228 2016/12/13 15:50:58 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.229 2016/12/22 13:08:50 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -20,8 +20,6 @@ | |||
20 | #include "lualib.h" | 20 | #include "lualib.h" |
21 | 21 | ||
22 | 22 | ||
23 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR | ||
24 | |||
25 | 23 | ||
26 | #if !defined(LUA_PROMPT) | 24 | #if !defined(LUA_PROMPT) |
27 | #define LUA_PROMPT "> " | 25 | #define LUA_PROMPT "> " |
@@ -534,88 +532,6 @@ static int runargs (lua_State *L, char **argv, int n) { | |||
534 | 532 | ||
535 | 533 | ||
536 | 534 | ||
537 | /* | ||
538 | ** {================================================================== | ||
539 | ** Set Paths | ||
540 | ** =================================================================== | ||
541 | */ | ||
542 | |||
543 | /* | ||
544 | ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment | ||
545 | ** variables that Lua check to set its paths. | ||
546 | */ | ||
547 | #if !defined(LUA_PATH_VAR) | ||
548 | #define LUA_PATH_VAR "LUA_PATH" | ||
549 | #endif | ||
550 | |||
551 | #if !defined(LUA_CPATH_VAR) | ||
552 | #define LUA_CPATH_VAR "LUA_CPATH" | ||
553 | #endif | ||
554 | |||
555 | #define LUA_PATHVARVERSION LUA_PATH_VAR LUA_VERSUFFIX | ||
556 | #define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_VERSUFFIX | ||
557 | |||
558 | |||
559 | #define AUXMARK "\1" /* auxiliary mark */ | ||
560 | |||
561 | |||
562 | #if defined(LUA_USE_WINDOWS) | ||
563 | |||
564 | |||
565 | /* | ||
566 | ** Replace in the path (on the top of the stack) any occurrence | ||
567 | ** of LUA_EXEC_DIR with the executable's path. | ||
568 | */ | ||
569 | static void setprogdir (lua_State *L) { | ||
570 | char buff[MAX_PATH + 1]; | ||
571 | char *lb; | ||
572 | DWORD nsize = sizeof(buff)/sizeof(char); | ||
573 | DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */ | ||
574 | if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) | ||
575 | luaL_error(L, "unable to get ModuleFileName"); | ||
576 | else { | ||
577 | *lb = '\0'; /* cut name on the last '\\' to get the path */ | ||
578 | luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); | ||
579 | lua_remove(L, -2); /* remove original string */ | ||
580 | } | ||
581 | } | ||
582 | |||
583 | #else | ||
584 | |||
585 | #define setprogdir(L) ((void)0) | ||
586 | |||
587 | #endif | ||
588 | |||
589 | /* | ||
590 | ** Change a path according to corresponding environment variables | ||
591 | */ | ||
592 | static void chgpath (lua_State *L, const char *fieldname, | ||
593 | const char *envname1, | ||
594 | const char *envname2, | ||
595 | int noenv) { | ||
596 | const char *path = getenv(envname1); | ||
597 | lua_getglobal(L, LUA_LOADLIBNAME); /* get 'package' table */ | ||
598 | lua_getfield(L, -1, fieldname); /* get original path */ | ||
599 | if (path == NULL) /* no environment variable? */ | ||
600 | path = getenv(envname2); /* try alternative name */ | ||
601 | if (path == NULL || noenv) /* no environment variable? */ | ||
602 | lua_pushvalue(L, -1); /* use original value */ | ||
603 | else { | ||
604 | const char *def = lua_tostring(L, -1); /* default path */ | ||
605 | /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ | ||
606 | path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, | ||
607 | LUA_PATH_SEP AUXMARK LUA_PATH_SEP); | ||
608 | luaL_gsub(L, path, AUXMARK, def); | ||
609 | lua_remove(L, -2); /* remove result from 1st 'gsub' */ | ||
610 | } | ||
611 | setprogdir(L); | ||
612 | lua_setfield(L, -3, fieldname); /* set path value */ | ||
613 | lua_pop(L, 2); /* pop 'package' table and original path */ | ||
614 | } | ||
615 | |||
616 | /* }================================================================== */ | ||
617 | |||
618 | |||
619 | static int handle_luainit (lua_State *L) { | 535 | static int handle_luainit (lua_State *L) { |
620 | const char *name = "=" LUA_INITVARVERSION; | 536 | const char *name = "=" LUA_INITVARVERSION; |
621 | const char *init = getenv(name + 1); | 537 | const char *init = getenv(name + 1); |
@@ -648,10 +564,11 @@ static int pmain (lua_State *L) { | |||
648 | } | 564 | } |
649 | if (args & has_v) /* option '-v'? */ | 565 | if (args & has_v) /* option '-v'? */ |
650 | print_version(); | 566 | print_version(); |
567 | if (args & has_E) { /* option '-E'? */ | ||
568 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ | ||
569 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); | ||
570 | } | ||
651 | luaL_openlibs(L); /* open standard libraries */ | 571 | luaL_openlibs(L); /* open standard libraries */ |
652 | /* change paths according to env variables */ | ||
653 | chgpath(L, "path", LUA_PATHVARVERSION, LUA_PATH_VAR, (args & has_E)); | ||
654 | chgpath(L, "cpath", LUA_CPATHVARVERSION, LUA_CPATH_VAR, (args & has_E)); | ||
655 | createargtable(L, argv, argc, script); /* create table 'arg' */ | 572 | createargtable(L, argv, argc, script); /* create table 'arg' */ |
656 | if (!(args & has_E)) { /* no option '-E'? */ | 573 | if (!(args & has_E)) { /* no option '-E'? */ |
657 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ | 574 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp roberto $ | 2 | ** $Id: lualib.h,v 1.44 2014/02/06 17:32:33 roberto Exp roberto $ |
3 | ** Lua standard libraries | 3 | ** Lua standard libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -11,6 +11,9 @@ | |||
11 | #include "lua.h" | 11 | #include "lua.h" |
12 | 12 | ||
13 | 13 | ||
14 | /* version suffix for environment variable names */ | ||
15 | #define LUA_VERSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR | ||
16 | |||
14 | 17 | ||
15 | LUAMOD_API int (luaopen_base) (lua_State *L); | 18 | LUAMOD_API int (luaopen_base) (lua_State *L); |
16 | 19 | ||