diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-07-18 14:55:59 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-07-18 14:55:59 -0300 |
| commit | de3fd8ab83763960433283a07d32e7a6bb986ea8 (patch) | |
| tree | 50b522bf8019527322aa0d400788e82d9049f470 | |
| parent | 788109a3de05462f19c5c05033581c1eab9e9283 (diff) | |
| download | lua-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)
| -rw-r--r-- | loadlib.c | 94 | ||||
| -rw-r--r-- | lua.c | 96 | ||||
| -rw-r--r-- | luaconf.h | 14 |
3 files changed, 109 insertions, 95 deletions
| @@ -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 | ||
| 192 | static 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 | |||
| 207 | static void pusherror (lua_State *L) { | 158 | static 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 | */ | ||
| 676 | static 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 | |||
| 685 | static 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 | |||
| 704 | static const luaL_Reg pk_funcs[] = { | 620 | static 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"); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.225 2015/03/30 15:42:59 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.226 2015/08/14 19:11:20 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 | */ |
| @@ -55,6 +55,8 @@ | |||
| 55 | #elif defined(LUA_USE_WINDOWS) /* }{ */ | 55 | #elif defined(LUA_USE_WINDOWS) /* }{ */ |
| 56 | 56 | ||
| 57 | #include <io.h> | 57 | #include <io.h> |
| 58 | #include <windows.h> | ||
| 59 | |||
| 58 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) | 60 | #define lua_stdin_is_tty() _isatty(_fileno(stdin)) |
| 59 | 61 | ||
| 60 | #else /* }{ */ | 62 | #else /* }{ */ |
| @@ -529,6 +531,91 @@ static int runargs (lua_State *L, char **argv, int n) { | |||
| 529 | } | 531 | } |
| 530 | 532 | ||
| 531 | 533 | ||
| 534 | |||
| 535 | /* | ||
| 536 | ** {================================================================== | ||
| 537 | ** Set Paths | ||
| 538 | ** =================================================================== | ||
| 539 | */ | ||
| 540 | |||
| 541 | /* | ||
| 542 | ** LUA_PATH_VAR and LUA_CPATH_VAR are the names of the environment | ||
| 543 | ** variables that Lua check to set its paths. | ||
| 544 | */ | ||
| 545 | #if !defined(LUA_PATH_VAR) | ||
| 546 | #define LUA_PATH_VAR "LUA_PATH" | ||
| 547 | #endif | ||
| 548 | |||
| 549 | #if !defined(LUA_CPATH_VAR) | ||
| 550 | #define LUA_CPATH_VAR "LUA_CPATH" | ||
| 551 | #endif | ||
| 552 | |||
| 553 | #define LUA_PATHSUFFIX "_" LUA_VERSION_MAJOR "_" LUA_VERSION_MINOR | ||
| 554 | |||
| 555 | #define LUA_PATHVARVERSION LUA_PATH_VAR LUA_PATHSUFFIX | ||
| 556 | #define LUA_CPATHVARVERSION LUA_CPATH_VAR LUA_PATHSUFFIX | ||
| 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 | |||
| 532 | static int handle_luainit (lua_State *L) { | 619 | static int handle_luainit (lua_State *L) { |
| 533 | const char *name = "=" LUA_INITVARVERSION; | 620 | const char *name = "=" LUA_INITVARVERSION; |
| 534 | const char *init = getenv(name + 1); | 621 | const char *init = getenv(name + 1); |
| @@ -561,11 +648,10 @@ static int pmain (lua_State *L) { | |||
| 561 | } | 648 | } |
| 562 | if (args & has_v) /* option '-v'? */ | 649 | if (args & has_v) /* option '-v'? */ |
| 563 | print_version(); | 650 | print_version(); |
| 564 | if (args & has_E) { /* option '-E'? */ | ||
| 565 | lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ | ||
| 566 | lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); | ||
| 567 | } | ||
| 568 | luaL_openlibs(L); /* open standard libraries */ | 651 | 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)); | ||
| 569 | createargtable(L, argv, argc, script); /* create table 'arg' */ | 655 | createargtable(L, argv, argc, script); /* create table 'arg' */ |
| 570 | if (!(args & has_E)) { /* no option '-E'? */ | 656 | if (!(args & has_E)) { /* no option '-E'? */ |
| 571 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ | 657 | if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.254 2015/10/21 18:17:40 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.255 2016/05/01 20:06:09 roberto Exp roberto $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -159,6 +159,18 @@ | |||
| 159 | */ | 159 | */ |
| 160 | 160 | ||
| 161 | /* | 161 | /* |
| 162 | ** LUA_PATH_SEP is the character that separates templates in a path. | ||
| 163 | ** LUA_PATH_MARK is the string that marks the substitution points in a | ||
| 164 | ** template. | ||
| 165 | ** LUA_EXEC_DIR in a Windows path is replaced by the executable's | ||
| 166 | ** directory. | ||
| 167 | */ | ||
| 168 | #define LUA_PATH_SEP ";" | ||
| 169 | #define LUA_PATH_MARK "?" | ||
| 170 | #define LUA_EXEC_DIR "!" | ||
| 171 | |||
| 172 | |||
| 173 | /* | ||
| 162 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for | 174 | @@ LUA_PATH_DEFAULT is the default path that Lua uses to look for |
| 163 | ** Lua libraries. | 175 | ** Lua libraries. |
| 164 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for | 176 | @@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for |
