aboutsummaryrefslogtreecommitdiff
path: root/lua.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-07-09 14:43:31 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-07-09 14:43:31 -0300
commitc612685d4b9ecdf0525b4d4410efa9f70d4b4518 (patch)
treeaadd4562a5d2fb58c701a350e1be87aead91e7d4 /lua.c
parent85a3c1699c9587a9e952b4ab75b1c6c310ebebea (diff)
downloadlua-c612685d4b9ecdf0525b4d4410efa9f70d4b4518.tar.gz
lua-c612685d4b9ecdf0525b4d4410efa9f70d4b4518.tar.bz2
lua-c612685d4b9ecdf0525b4d4410efa9f70d4b4518.zip
lua.c doesn't use function pointers with LUA_READLINE
Bugs in macOS prevent assigning 'add_history' to 'l_addhist' without a warning.
Diffstat (limited to 'lua.c')
-rw-r--r--lua.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/lua.c b/lua.c
index 66fb74b7..b2967a44 100644
--- a/lua.c
+++ b/lua.c
@@ -438,13 +438,24 @@ static int handle_luainit (lua_State *L) {
438** the standard input. 438** the standard input.
439** * lua_saveline defines how to "save" a read line in a "history". 439** * lua_saveline defines how to "save" a read line in a "history".
440** * lua_freeline defines how to free a line read by lua_readline. 440** * lua_freeline defines how to free a line read by lua_readline.
441**
442** If lua_readline is defined, all of them should be defined.
443*/ 441*/
444 442
445#if !defined(lua_readline) /* { */ 443#if !defined(lua_readline) /* { */
444/* Otherwise, all previously listed functions should be defined. */
446 445
447/* Code to use the readline library, either statically or dynamically linked */ 446#if defined(LUA_USE_READLINE) /* { */
447/* Lua will be linked with '-lreadline' */
448
449#include <readline/readline.h>
450#include <readline/history.h>
451
452#define lua_initreadline(L) ((void)L, rl_readline_name="lua")
453#define lua_readline(buff,prompt) ((void)buff, readline(prompt))
454#define lua_saveline(line) add_history(line)
455#define lua_freeline(line) free(line)
456
457#else /* }{ */
458/* use dynamically loaded readline (or nothing) */
448 459
449/* pointer to 'readline' function (if any) */ 460/* pointer to 'readline' function (if any) */
450typedef char *(*l_readlineT) (const char *prompt); 461typedef char *(*l_readlineT) (const char *prompt);
@@ -480,22 +491,9 @@ static void lua_freeline (char *line) {
480} 491}
481 492
482 493
483#if defined(LUA_USE_READLINE) /* { */ 494#if defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* { */
484
485/* assume Lua will be linked with '-lreadline' */
486#include <readline/readline.h>
487#include <readline/history.h>
488
489static void lua_initreadline(lua_State *L) {
490 UNUSED(L);
491 rl_readline_name = "lua";
492 l_readline = cast(l_readlineT, readline);
493 l_addhist = cast(l_addhistT, add_history);
494}
495
496#elif defined(LUA_USE_DLOPEN) && defined(LUA_READLINELIB) /* }{ */
497
498/* try to load 'readline' dynamically */ 495/* try to load 'readline' dynamically */
496
499#include <dlfcn.h> 497#include <dlfcn.h>
500 498
501static void lua_initreadline (lua_State *L) { 499static void lua_initreadline (lua_State *L) {
@@ -508,15 +506,20 @@ static void lua_initreadline (lua_State *L) {
508 *name = "lua"; 506 *name = "lua";
509 l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline"))); 507 l_readline = cast(l_readlineT, cast_func(dlsym(lib, "readline")));
510 l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history"))); 508 l_addhist = cast(l_addhistT, cast_func(dlsym(lib, "add_history")));
509 if (l_readline == NULL)
510 lua_warning(L, "unable to load 'readline'", 0);
511 } 511 }
512} 512}
513 513
514#else /* }{ */ 514#else /* }{ */
515/* no dlopen or LUA_READLINELIB undefined */
515 516
516/* no readline; leave function pointers as NULL */ 517/* Leave pointers with NULL */
517#define lua_initreadline(L) cast(void, L) 518#define lua_initreadline(L) ((void)L)
518 519
519#endif /* } */ 520#endif /* } */
521
522#endif /* } */
520 523
521#endif /* } */ 524#endif /* } */
522 525