diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2026-01-22 13:47:10 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2026-01-22 13:47:10 -0300 |
| commit | 3360710bd3ea8da06fa5062f9d10c2719083097c (patch) | |
| tree | 6bba1b0da30881feb318dd68abd044b7438677af /lua.c | |
| parent | e992c6a95939c8e1fe357bfce481e0d0c762c3c6 (diff) | |
| download | lua-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.c | 22 |
1 files changed, 16 insertions, 6 deletions
| @@ -374,12 +374,21 @@ static int runargs (lua_State *L, char **argv, int n) { | |||
| 374 | } | 374 | } |
| 375 | 375 | ||
| 376 | 376 | ||
| 377 | static char *(*l_getenv)(const char *name); | ||
| 378 | |||
| 379 | /* Function to ignore environment variables, used by option -E */ | ||
| 380 | static char *no_getenv (const char *name) { | ||
| 381 | UNUSED(name); | ||
| 382 | return NULL; | ||
| 383 | } | ||
| 384 | |||
| 385 | |||
| 377 | static int handle_luainit (lua_State *L) { | 386 | static 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) */ |
