aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto I <roberto@inf.puc-rio.br>2026-01-22 13:47:10 -0300
committerRoberto I <roberto@inf.puc-rio.br>2026-01-22 13:47:10 -0300
commit3360710bd3ea8da06fa5062f9d10c2719083097c (patch)
tree6bba1b0da30881feb318dd68abd044b7438677af /lua.c
parente992c6a95939c8e1fe357bfce481e0d0c762c3c6 (diff)
downloadlua-3360710bd3ea8da06fa5062f9d10c2719083097c.tar.gz
lua-3360710bd3ea8da06fa5062f9d10c2719083097c.tar.bz2
lua-3360710bd3ea8da06fa5062f9d10c2719083097c.zip
Another way to handle option -E
A pointer to function 'l_getenv' can point to the regular 'getenv' or to a dumb function (that always returns NULL) to ignore environment variables.
Diffstat (limited to '')
-rw-r--r--lua.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/lua.c b/lua.c
index 5054583d..37fd1cb8 100644
--- a/lua.c
+++ b/lua.c
@@ -374,12 +374,21 @@ static int runargs (lua_State *L, char **argv, int n) {
374} 374}
375 375
376 376
377static char *(*l_getenv)(const char *name);
378
379/* Function to ignore environment variables, used by option -E */
380static char *no_getenv (const char *name) {
381 UNUSED(name);
382 return NULL;
383}
384
385
377static int handle_luainit (lua_State *L) { 386static int handle_luainit (lua_State *L) {
378 const char *name = "=" LUA_INITVARVERSION; 387 const char *name = "=" LUA_INITVARVERSION;
379 const char *init = getenv(name + 1); 388 const char *init = l_getenv(name + 1);
380 if (init == NULL) { 389 if (init == NULL) {
381 name = "=" LUA_INIT_VAR; 390 name = "=" LUA_INIT_VAR;
382 init = getenv(name + 1); /* try alternative name */ 391 init = l_getenv(name + 1); /* try alternative name */
383 } 392 }
384 if (init == NULL) return LUA_OK; 393 if (init == NULL) return LUA_OK;
385 else if (init[0] == '@') 394 else if (init[0] == '@')
@@ -715,17 +724,18 @@ static int pmain (lua_State *L) {
715 if (args & has_v) /* option '-v'? */ 724 if (args & has_v) /* option '-v'? */
716 print_version(); 725 print_version();
717 if (args & has_E) { /* option '-E'? */ 726 if (args & has_E) { /* option '-E'? */
727 l_getenv = &no_getenv; /* program will ignore environment variables */
718 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */ 728 lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
719 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); 729 lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
720 } 730 }
731 else
732 l_getenv = &getenv;
721 luai_openlibs(L); /* open standard libraries */ 733 luai_openlibs(L); /* open standard libraries */
722 createargtable(L, argv, argc, script); /* create table 'arg' */ 734 createargtable(L, argv, argc, script); /* create table 'arg' */
723 lua_gc(L, LUA_GCRESTART); /* start GC... */ 735 lua_gc(L, LUA_GCRESTART); /* start GC... */
724 lua_gc(L, LUA_GCGEN); /* ...in generational mode */ 736 lua_gc(L, LUA_GCGEN); /* ...in generational mode */
725 if (!(args & has_E)) { /* no option '-E'? */ 737 if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */
726 if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ 738 return 0; /* error running LUA_INIT */
727 return 0; /* error running LUA_INIT */
728 }
729 if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */ 739 if (!runargs(L, argv, optlim)) /* execute arguments -e, -l, and -W */
730 return 0; /* something failed */ 740 return 0; /* something failed */
731 if (script > 0) { /* execute main script (if there is one) */ 741 if (script > 0) { /* execute main script (if there is one) */