diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2026-01-30 16:47:33 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2026-01-30 16:47:33 -0300 |
| commit | c6b484823806e08e1756b1a6066a3ace6f080fae (patch) | |
| tree | 5524308cc0db8cc84878caa8a44236bcfe9dde4a | |
| parent | efbc29754544dd820bfdc81edf17d7dcfad31d05 (diff) | |
| download | lua-c6b484823806e08e1756b1a6066a3ace6f080fae.tar.gz lua-c6b484823806e08e1756b1a6066a3ace6f080fae.tar.bz2 lua-c6b484823806e08e1756b1a6066a3ace6f080fae.zip | |
The name of the readline library can be changed from its default value
through environment variable LUA_READLINELIB.
| -rw-r--r-- | lua.c | 24 | ||||
| -rw-r--r-- | testes/main.lua | 23 |
2 files changed, 40 insertions, 7 deletions
| @@ -30,6 +30,12 @@ | |||
| 30 | #define LUA_INIT_VAR "LUA_INIT" | 30 | #define LUA_INIT_VAR "LUA_INIT" |
| 31 | #endif | 31 | #endif |
| 32 | 32 | ||
| 33 | /* Name of the environment variable with the name of the readline library */ | ||
| 34 | #if !defined(LUA_RLLIB_VAR) | ||
| 35 | #define LUA_RLLIB_VAR "LUA_READLINELIB" | ||
| 36 | #endif | ||
| 37 | |||
| 38 | |||
| 33 | #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX | 39 | #define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX |
| 34 | 40 | ||
| 35 | 41 | ||
| @@ -507,18 +513,24 @@ static void lua_freeline (char *line) { | |||
| 507 | #include <dlfcn.h> | 513 | #include <dlfcn.h> |
| 508 | 514 | ||
| 509 | static void lua_initreadline (lua_State *L) { | 515 | static void lua_initreadline (lua_State *L) { |
| 510 | void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); | 516 | const char *rllib = l_getenv(LUA_RLLIB_VAR); /* name of readline library */ |
| 511 | if (lib == NULL) | 517 | void *lib; /* library handle */ |
| 512 | lua_warning(L, "library '" LUA_READLINELIB "' not found", 0); | 518 | if (rllib == NULL) /* no environment variable? */ |
| 513 | else { | 519 | rllib = LUA_READLINELIB; /* use default name */ |
| 520 | lib = dlopen(rllib, RTLD_NOW | RTLD_LOCAL); | ||
| 521 | if (lib != NULL) { | ||
| 514 | const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); | 522 | const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); |
| 515 | if (name != NULL) | 523 | if (name != NULL) |
| 516 | *name = "lua"; | 524 | *name = "lua"; |
| 517 | l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); | 525 | l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); |
| 518 | l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); | 526 | l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); |
| 519 | if (l_readline == NULL) | 527 | if (l_readline != NULL) /* could load readline function? */ |
| 520 | lua_warning(L, "unable to load 'readline'", 0); | 528 | return; /* everything ok */ |
| 529 | /* else emit a warning */ | ||
| 521 | } | 530 | } |
| 531 | lua_warning(L, "unable to load readline library '", 1); | ||
| 532 | lua_warning(L, rllib, 1); | ||
| 533 | lua_warning(L, "'", 0); | ||
| 522 | } | 534 | } |
| 523 | 535 | ||
| 524 | #else /* }{ */ | 536 | #else /* }{ */ |
diff --git a/testes/main.lua b/testes/main.lua index dc48dc48..98d36951 100644 --- a/testes/main.lua +++ b/testes/main.lua | |||
| @@ -78,6 +78,9 @@ end | |||
| 78 | 78 | ||
| 79 | RUN('lua -v') | 79 | RUN('lua -v') |
| 80 | 80 | ||
| 81 | RUN('lua -v > %s', out) | ||
| 82 | local release = string.match(getoutput(), "Lua (%d+%.%d+%.%d+)") | ||
| 83 | |||
| 81 | print(string.format("(temporary program file used in these tests: %s)", prog)) | 84 | print(string.format("(temporary program file used in these tests: %s)", prog)) |
| 82 | 85 | ||
| 83 | -- running stdin as a file | 86 | -- running stdin as a file |
| @@ -167,7 +170,9 @@ checkout("10\n11\n") | |||
| 167 | -- test errors in LUA_INIT | 170 | -- test errors in LUA_INIT |
| 168 | NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') | 171 | NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua') |
| 169 | 172 | ||
| 170 | -- test option '-E' | 173 | |
| 174 | print("testing option '-E'") | ||
| 175 | |||
| 171 | local defaultpath, defaultCpath | 176 | local defaultpath, defaultCpath |
| 172 | 177 | ||
| 173 | do | 178 | do |
| @@ -192,6 +197,22 @@ assert(not string.find(defaultpath, "xxx") and | |||
| 192 | string.find(defaultCpath, "lua")) | 197 | string.find(defaultCpath, "lua")) |
| 193 | 198 | ||
| 194 | 199 | ||
| 200 | -- (LUA_READLINELIB was introduced in 5.5.1) | ||
| 201 | if release >= "5.5.1" then | ||
| 202 | print"testing readline library name" | ||
| 203 | -- should generate a warning when trying to load inexistent library "xuxu" | ||
| 204 | local env = [[LUA_READLINELIB=xuxu LUA_INIT="warn('@allow')"]] | ||
| 205 | local code = 'echo " " | env %s lua %s -W -i >%s 2>&1' | ||
| 206 | RUN(code, env, "", out) -- run code with no extra options | ||
| 207 | assert(string.find(getoutput(), | ||
| 208 | "warning: unable to load readline library 'xuxu'")) | ||
| 209 | |||
| 210 | RUN(code, env, "-E", out) -- run again with option -E | ||
| 211 | -- no warning when LUA_READLINELIB is to be ignored | ||
| 212 | assert(not string.find(getoutput(), "warning")) | ||
| 213 | end | ||
| 214 | |||
| 215 | |||
| 195 | -- test replacement of ';;' to default path | 216 | -- test replacement of ';;' to default path |
| 196 | local function convert (p) | 217 | local function convert (p) |
| 197 | prepfile("print(package.path)") | 218 | prepfile("print(package.path)") |
