aboutsummaryrefslogtreecommitdiff
path: root/lua.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 /lua.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 'lua.c')
-rw-r--r--lua.c96
1 files changed, 91 insertions, 5 deletions
diff --git a/lua.c b/lua.c
index 898c6869..8bd470ae 100644
--- a/lua.c
+++ b/lua.c
@@ -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*/
569static 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*/
592static 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
532static int handle_luainit (lua_State *L) { 619static 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 */