Environment variable for readline library name

The name of the readline library can be changed from its default value
through environment variable LUA_READLINELIB.
This commit is contained in:
Roberto I
2026-01-30 16:47:33 -03:00
parent efbc297545
commit c6b4848238
2 changed files with 40 additions and 7 deletions

24
lua.c
View File

@@ -30,6 +30,12 @@
#define LUA_INIT_VAR "LUA_INIT"
#endif
/* Name of the environment variable with the name of the readline library */
#if !defined(LUA_RLLIB_VAR)
#define LUA_RLLIB_VAR "LUA_READLINELIB"
#endif
#define LUA_INITVARVERSION LUA_INIT_VAR LUA_VERSUFFIX
@@ -507,18 +513,24 @@ static void lua_freeline (char *line) {
#include <dlfcn.h>
static void lua_initreadline (lua_State *L) {
void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL);
if (lib == NULL)
lua_warning(L, "library '" LUA_READLINELIB "' not found", 0);
else {
const char *rllib = l_getenv(LUA_RLLIB_VAR); /* name of readline library */
void *lib; /* library handle */
if (rllib == NULL) /* no environment variable? */
rllib = LUA_READLINELIB; /* use default name */
lib = dlopen(rllib, RTLD_NOW | RTLD_LOCAL);
if (lib != NULL) {
const char **name = cast(const char**, dlsym(lib, "rl_readline_name"));
if (name != NULL)
*name = "lua";
l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline")));
l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history")));
if (l_readline == NULL)
lua_warning(L, "unable to load 'readline'", 0);
if (l_readline != NULL) /* could load readline function? */
return; /* everything ok */
/* else emit a warning */
}
lua_warning(L, "unable to load readline library '", 1);
lua_warning(L, rllib, 1);
lua_warning(L, "'", 0);
}
#else /* }{ */

View File

@@ -78,6 +78,9 @@ end
RUN('lua -v')
RUN('lua -v > %s', out)
local release = string.match(getoutput(), "Lua (%d+%.%d+%.%d+)")
print(string.format("(temporary program file used in these tests: %s)", prog))
-- running stdin as a file
@@ -167,7 +170,9 @@ checkout("10\n11\n")
-- test errors in LUA_INIT
NoRun('LUA_INIT:1: msg', 'env LUA_INIT="error(\'msg\')" lua')
-- test option '-E'
print("testing option '-E'")
local defaultpath, defaultCpath
do
@@ -192,6 +197,22 @@ assert(not string.find(defaultpath, "xxx") and
string.find(defaultCpath, "lua"))
-- (LUA_READLINELIB was introduced in 5.5.1)
if release >= "5.5.1" then
print"testing readline library name"
-- should generate a warning when trying to load inexistent library "xuxu"
local env = [[LUA_READLINELIB=xuxu LUA_INIT="warn('@allow')"]]
local code = 'echo " " | env %s lua %s -W -i >%s 2>&1'
RUN(code, env, "", out) -- run code with no extra options
assert(string.find(getoutput(),
"warning: unable to load readline library 'xuxu'"))
RUN(code, env, "-E", out) -- run again with option -E
-- no warning when LUA_READLINELIB is to be ignored
assert(not string.find(getoutput(), "warning"))
end
-- test replacement of ';;' to default path
local function convert (p)
prepfile("print(package.path)")