--- src/lua.c.orig 2018-03-16 14:23:08 UTC +++ src/lua.c @@ -379,7 +379,54 @@ static int handle_luainit (lua_State *L) */ #if !defined(lua_readline) /* { */ -#if defined(LUA_USE_READLINE) /* { */ +#if defined(LUA_USE_READLINE_DL)/* { */ + +#include + +#ifndef LUA_READLINE_LIBPATH +#define LUA_READLINE_LIBPATH "/usr/local/lib/libedit.so" +#endif + +typedef char *readline_functype(const char *); +typedef int add_history_functype(const char *); + +static readline_functype *lua_readline_p = NULL; +static add_history_functype *lua_saveline_p = NULL; + +static void lua_initreadline(lua_State *L) +{ + void *editlib = NULL; + union dl_func_hack { + void *ptr; + readline_functype *rlfunc; + add_history_functype *ahfunc; + char **rlnamevar; + int *icompvar; + } u; + (void) L; + if ((editlib = dlopen(LUA_READLINE_LIBPATH, RTLD_LAZY | RTLD_LOCAL))) { + u.ptr = dlsym(editlib, "readline"); + lua_readline_p = u.rlfunc; + u.ptr = dlsym(editlib, "add_history"); + lua_saveline_p = u.ahfunc; + if ((u.ptr = dlsym(editlib, "rl_readline_name"))) + *u.rlnamevar = "lua"; + if ((u.ptr = dlsym(editlib, "rl_inhibit_completion"))) + *u.icompvar = 1; + } +} + +#define lua_readline(L,b,p) \ + ((void)L, \ + (lua_readline_p) \ + ? (((b)=lua_readline_p(p)) != NULL) \ + : (fputs(p, stdout), fflush(stdout), fgets(b, LUA_MAXINPUT, stdin) != NULL)) +#define lua_saveline(L,line) \ + do { (void)L; if (lua_saveline_p) lua_saveline_p(line); } while(0) +#define lua_freeline(L,b) \ + do { (void)L; if (lua_readline_p) free(b); } while(0) + +#elif defined(LUA_USE_READLINE) /* { */ #include #include