From 30531c291b25243e05a9734033a6a023c18f13ac Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 23 Jun 2025 14:00:21 -0300 Subject: Refactoring in the use of 'readline' by 'lua.c' More common code for 'readline' loaded statically or dynamically (or not loaded). --- lua.c | 71 ++++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) (limited to 'lua.c') diff --git a/lua.c b/lua.c index b611cbca..dfb98e38 100644 --- a/lua.c +++ b/lua.c @@ -432,32 +432,30 @@ static int handle_luainit (lua_State *L) { /* -** lua_readline defines how to show a prompt and then read a line from -** the standard input. -** lua_saveline defines how to "save" a read line in a "history". -** lua_freeline defines how to free a line read by lua_readline. +** * lua_initreadline initializes the readline system. +** * lua_readline defines how to show a prompt and then read a line from +** the standard input. +** * lua_saveline defines how to "save" a read line in a "history". +** * lua_freeline defines how to free a line read by lua_readline. +** +** If lua_readline is defined, all of them should be defined. */ -#if defined(LUA_USE_READLINE) - -#include -#include -#define lua_initreadline(L) ((void)L, rl_readline_name="lua") -#define lua_readline(b,p) ((void)b, readline(p)) -#define lua_saveline(line) add_history(line) -#define lua_freeline(b) free(b) +#if !defined(lua_readline) /* { */ -#endif +/* Code to use the readline library, either statically or dynamically linked */ +/* pointer to 'readline' function (if any) */ +typedef char *(*l_readlineT) (const char *prompt); +static l_readlineT l_readline = NULL; -#if !defined(lua_readline) /* { */ +/* pointer to 'add_history' function (if any) */ +typedef void (*l_addhistT) (const char *string); +static l_addhistT l_addhist = NULL; -/* pointer to dynamically loaded 'readline' function (if any) */ -typedef char *(*l_readline_t) (const char *prompt); -static l_readline_t l_readline = NULL; static char *lua_readline (char *buff, const char *prompt) { - if (l_readline != NULL) /* is there a dynamic 'readline'? */ + if (l_readline != NULL) /* is there a 'readline'? */ return (*l_readline)(prompt); /* use it */ else { /* emulate 'readline' over 'buff' */ fputs(prompt, stdout); @@ -467,33 +465,38 @@ static char *lua_readline (char *buff, const char *prompt) { } -/* pointer to dynamically loaded 'add_history' function (if any) */ -typedef void (*l_addhist_t) (const char *string); -static l_addhist_t l_addhist = NULL; - static void lua_saveline (const char *line) { - if (l_addhist != NULL) /* is there a dynamic 'add_history'? */ + if (l_addhist != NULL) /* is there an 'add_history'? */ (*l_addhist)(line); /* use it */ /* else nothing to be done */ } static void lua_freeline (char *line) { - if (l_readline != NULL) /* is there a dynamic 'readline'? */ + if (l_readline != NULL) /* is there a 'readline'? */ free(line); /* free line created by it */ /* else 'lua_readline' used an automatic buffer; nothing to free */ } -#if !defined(LUA_USE_DLOPEN) || !defined(LUA_READLINELIB) +#if defined(LUA_USE_READLINE) /* { */ -#define lua_initreadline(L) ((void)L) +/* assume Lua will be linked with '-lreadline' */ +#include +#include + +static void lua_initreadline(lua_State *L) { + UNUSED(L); + rl_readline_name = "lua"; + l_readline = readline; + l_addhist = add_history; +} -#else /* { */ +#elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */ +/* try to load 'readline' dynamically */ #include - static void lua_initreadline (lua_State *L) { void *lib = dlopen(LUA_READLINELIB, RTLD_NOW | RTLD_LOCAL); if (lib == NULL) @@ -502,14 +505,16 @@ static void lua_initreadline (lua_State *L) { const char **name = cast(const char**, dlsym(lib, "rl_readline_name")); if (name != NULL) *name = "Lua"; - l_readline = cast(l_readline_t, cast_func(dlsym(lib, "readline"))); - if (l_readline == NULL) - lua_warning(L, "unable to load 'readline'", 0); - else - l_addhist = cast(l_addhist_t, cast_func(dlsym(lib, "add_history"))); + l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); + l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); } } +#else /* }{ */ + +/* no readline; leave function pointers as NULL */ +#define lua_initreadline(L) cast(void, L) + #endif /* } */ #endif /* } */ -- cgit v1.2.3-55-g6feb