Skip to content

Instantly share code, notes, and snippets.

@RhodiumToad
Created July 19, 2018 19:54
Show Gist options
  • Save RhodiumToad/9307ff042b62e0342110cd32dba4c832 to your computer and use it in GitHub Desktop.
Save RhodiumToad/9307ff042b62e0342110cd32dba4c832 to your computer and use it in GitHub Desktop.
--- src/lua.c.orig 2018-07-19 19:12:54 UTC
+++ src/lua.c
@@ -73,6 +73,9 @@ static void print_usage (const char *bad
" -l name require library 'name' into global 'name'\n"
" -v show version information\n"
" -E ignore environment variables\n"
+#if defined(LUA_USE_READLINE_DL)
+ " -N disable command-line editing\n"
+#endif
" -- stop handling options\n"
" - stop handling options and execute stdin\n"
,
@@ -235,6 +238,7 @@ static int handle_script (lua_State *L,
#define has_v 4 /* -v */
#define has_e 8 /* -e */
#define has_E 16 /* -E */
+#define has_N 32 /* -N */
/*
@@ -263,6 +267,13 @@ static int collectargs (char **argv, int
return has_error; /* invalid option */
args |= has_E;
break;
+#if defined(LUA_USE_READLINE_DL)
+ case 'N':
+ if (argv[i][2] != '\0') /* extra characters after 1st? */
+ return has_error; /* invalid option */
+ args |= has_N;
+ break;
+#endif
case 'i':
args |= has_i; /* (-i implies -v) *//* FALLTHROUGH */
case 'v':
@@ -379,7 +390,58 @@ static int handle_luainit (lua_State *L)
*/
#if !defined(lua_readline) /* { */
-#if defined(LUA_USE_READLINE) /* { */
+#if defined(LUA_USE_READLINE_DL)/* { */
+
+#include <editline/readline.h>
+#include <dlfcn.h>
+
+#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 int disable_readline = 0;
+
+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 (disable_readline)
+ return;
+ 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;
+ u.ptr = dlsym(editlib, "rl_readline_name");
+ if (u.ptr)
+ *u.rlnamevar = "lua";
+ u.ptr = dlsym(editlib, "rl_inhibit_completion");
+ if (u.ptr)
+ *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 <readline/readline.h>
#include <readline/history.h>
@@ -590,6 +652,10 @@ static int pmain (lua_State *L) {
if (script < argc && /* execute main script (if there is one) */
handle_script(L, argv + script) != LUA_OK)
return 0;
+#if defined(LUA_USE_READLINE_DL)
+ if (args & has_N)
+ disable_readline = 1;
+#endif
if (args & has_i) /* -i option? */
doREPL(L); /* do read-eval-print loop */
else if (script == argc && !(args & (has_e | has_v))) { /* no arguments? */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment