diff -uNpr luajit/src/luajit.c luajit2-2.1-20170808/src/luajit.c
--- luajit2-2.1-20170808/src/luajit.c 2017-10-09 16:04:26.254012331 +0800
+++ luajit2-2.1-20170808/src/luajit.c 2017-10-09 16:03:16.812275688 +0800
@@ -9,6 +9,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <readline/readline.h>
+#include <readline/history.h>
#define luajit_c
@@ -19,6 +21,8 @@
#include "lj_arch.h"
+#define HISTSIZE 512
+
#if LJ_TARGET_POSIX
#include <unistd.h>
#define lua_stdin_is_tty() isatty(0)
@@ -182,15 +186,14 @@ static int dolibrary(lua_State *L, const
return report(L, docall(L, 1, 1));
}
-static void write_prompt(lua_State *L, int firstline)
+static const char *get_prompt(lua_State *L, int firstline)
{
const char *p;
lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
p = lua_tostring(L, -1);
if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;
- fputs(p, stdout);
- fflush(stdout);
lua_pop(L, 1); /* remove global */
+ return p;
}
static int incomplete(lua_State *L, int status)
@@ -207,21 +210,46 @@ static int incomplete(lua_State *L, int
return 0; /* else... */
}
+void readline_add_history(char *line)
+{
+ HIST_ENTRY *pre_hist;
+ pre_hist = previous_history();
+
+ if(pre_hist) {
+ if(strcmp(pre_hist->line, line)!=0)
+ add_history(line);
+ } else {
+ add_history(line);
+ }
+}
+
+
static int pushline(lua_State *L, int firstline)
{
- char buf[LUA_MAXINPUT];
- write_prompt(L, firstline);
- if (fgets(buf, LUA_MAXINPUT, stdin)) {
- size_t len = strlen(buf);
- if (len > 0 && buf[len-1] == '\n')
- buf[len-1] = '\0';
- if (firstline && buf[0] == '=')
- lua_pushfstring(L, "return %s", buf+1);
- else
- lua_pushstring(L, buf);
- return 1;
+ char *line;
+ char *buf=NULL;
+ size_t len;
+
+ const char *prompt = get_prompt(L, firstline);
+ line = readline(prompt);
+ if(*line) {
+ buf = line;
+ readline_add_history(line);
+ } else {
+ buf = "";
}
- return 0;
+
+ len = strlen(buf);
+ if (len > 0 && buf[len-1] == '\n')
+ buf[len-1] = '\0';
+ if (firstline && buf[0] == '=')
+ lua_pushfstring(L, "return %s", buf+1);
+ else
+ lua_pushstring(L, buf);
+
+ if(*line)
+ free(line);
+ return 1;
}
static int loadline(lua_State *L)
@@ -577,6 +605,7 @@ int main(int argc, char **argv)
}
smain.argc = argc;
smain.argv = argv;
+ stifle_history(HISTSIZE);
status = lua_cpcall(L, pmain, NULL);
report(L, status);
lua_close(L);
--- luajit2-2.1-20170808/src/Makefile 2017-10-09 15:41:16.361229163 +0800
+++ luajit2-2.1-20170808/src/Makefile 2017-10-09 16:08:38.227636634 +0800
@@ -223,7 +223,7 @@ TARGET_DYNXLDOPTS=
TARGET_LFSFLAGS= -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
TARGET_XCFLAGS= $(TARGET_LFSFLAGS) -U_FORTIFY_SOURCE
TARGET_XLDFLAGS=
-TARGET_XLIBS= -lm
+TARGET_XLIBS= -lm -lreadline -lncurses
TARGET_TCFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS)
TARGET_ACFLAGS= $(CCOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS)
TARGET_ASFLAGS= $(ASOPTIONS) $(TARGET_XCFLAGS) $(TARGET_FLAGS) $(TARGET_CFLAGS)
每次更新都要打补丁,希望后面发的luajit2 的官方版本可以直接 支持readline,有时候在命令行敲测试代码时,还是挺有用的。