lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2011/8/9 Dimiter "malkia" Stanev <malkia@gmail.com>:
> Thanks Francesco!
>
> Is this one here, in the luajit branch (there also luajit+ffi, and one more)
>
> http://git.savannah.gnu.org/cgit/gsl-shell.git?h=doc-2.0&=switch

Hi Dimiter,

the currently maintained branch is luajit2. Actually I need to clean
up some old branches on github, sorry :-)

I've made a patch to add the readline support to the git HEAD of
LuaJIT. The patch is in attachment. I send it in attachment, I hope it
will be fine for the git purists :-)

Best regards,
Francesco
From 833547d5ead6fe3455ecd6399d94e2558ad73b0a Mon Sep 17 00:00:00 2001
From: Francesco Abbate <francesco.bbt@gmail.com>
Date: Tue, 9 Aug 2011 22:16:40 +0200
Subject: [PATCH] Added readline support on Linux

---
 src/Makefile  |    2 +-
 src/luaconf.h |   29 +++++++++++++++++++++++++++++
 src/luajit.c  |   38 ++++++++++++++++++++------------------
 3 files changed, 50 insertions(+), 19 deletions(-)

diff --git a/src/Makefile b/src/Makefile
index acde0db..af226af 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -299,7 +299,7 @@ else
     TARGET_XLDFLAGS+= -Wl,-E
   endif
   ifeq (Linux,$(TARGET_SYS))
-    TARGET_XLIBS+= -ldl
+    TARGET_XLIBS+= -ldl -lreadline -lhistory
   endif
   ifeq (GNU/kFreeBSD,$(TARGET_SYS))
     TARGET_XLIBS+= -ldl
diff --git a/src/luaconf.h b/src/luaconf.h
index 4a243ff..f75af35 100644
--- a/src/luaconf.h
+++ b/src/luaconf.h
@@ -9,6 +9,10 @@
 #include <limits.h>
 #include <stddef.h>
 
+#ifndef _WIN32
+#define LUA_USE_READLINE
+#endif
+
 /* Default path for loading Lua and C modules with require(). */
 #if defined(_WIN32)
 /*
@@ -83,6 +87,31 @@
 #define LUA_MAXINPUT	512	/* Max. input line length. */
 #endif
 
+/*
+  @@ 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.
+  ** CHANGE them if you want to improve this functionality (e.g., by using
+  ** GNU readline and history facilities).
+*/
+#if defined(LUA_USE_READLINE)
+#include <stdio.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+#define lua_readline(L,b,p)	((void)L, ((b)=readline(p)) != NULL)
+#define lua_saveline(L,idx) \
+	if (lua_strlen(L,idx) > 0)  /* non-empty line? */ \
+	  add_history(lua_tostring(L, idx));  /* add it to history */
+#define lua_freeline(L,b)	((void)L, free(b))
+#else
+#define lua_readline(L,b,p)	\
+	((void)L, fputs(p, stdout), fflush(stdout),  /* show prompt */ \
+	fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
+#define lua_saveline(L,idx)	{ (void)L; (void)idx; }
+#define lua_freeline(L,b)	{ (void)L; (void)b; }
+#endif
+
 /* Note: changing the following defines breaks the Lua 5.1 ABI. */
 #define LUA_INTEGER	ptrdiff_t
 #define LUA_IDSIZE	60	/* Size of lua_Debug.short_src. */
diff --git a/src/luajit.c b/src/luajit.c
index 17d3b5f..ff227fc 100644
--- a/src/luajit.c
+++ b/src/luajit.c
@@ -186,15 +186,16 @@ static int dolibrary(lua_State *L, const char *name)
   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)
@@ -211,21 +212,21 @@ static int incomplete(lua_State *L, int status)
   return 0;  /* else... */
 }
 
-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;
-  }
-  return 0;
+static int pushline (lua_State *L, int firstline) {
+  char buffer[LUA_MAXINPUT];
+  char *b = buffer;
+  size_t l;
+  const char *prmt = get_prompt(L, firstline);
+
+  if (lua_readline(L, b, prmt) == 0)
+    return 0;  /* no input */
+
+  l = strlen(b);
+  if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
+    b[l-1] = '\0';  /* remove it */
+  lua_pushstring(L, b);
+  lua_freeline(L, b);
+  return 1;
 }
 
 static int loadline(lua_State *L)
@@ -243,6 +244,7 @@ static int loadline(lua_State *L)
     lua_insert(L, -2);  /* ...between the two lines */
     lua_concat(L, 3);  /* join them */
   }
+  lua_saveline(L, 1);
   lua_remove(L, 1);  /* remove line */
   return status;
 }
-- 
1.7.1