lua-users home
lua-l archive

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


> Does anyone have a patch integrating the GNU readline library
> with the interactive lua interpreter? I tried doing one
> myself but didn't do too well ;)

Yes, I've done this. Here's the diff (-u):

--- lua/src/lua/lua.c	Fri Oct 20 17:36:32 2000
+++ ../lua-4.0/src/lua/lua.c	Tue Jan 30 11:13:00 2001
@@ -10,6 +10,9 @@
 #include <stdlib.h>
 #include <string.h>

+#include <readline/readline.h>
+#include <readline/history.h>
+
 #include "lua.h"

 #include "luadebug.h"
@@ -55,6 +58,8 @@
   lua_mathlibopen(L);
   lua_dblibopen(L);
   /* add your libraries here */
+  lua_rexlibopen(L);
+  lua_bitlibopen(L);
 }


@@ -170,40 +175,24 @@
 #endif

 static void manual_input (int version, int prompt) {
-  int cont = 1;
+  char *buffer;
   if (version) print_version();
-  while (cont) {
-    char buffer[MAXINPUT];
-    int i = 0;
+  do {
+    const char *s = NULL;
     if (prompt) {
-      const char *s;
       lua_getglobal(L, "_PROMPT");
       s = lua_tostring(L, -1);
       if (!s) s = PROMPT;
-      fputs(s, stdout);
       lua_pop(L, 1);  /* remove global */
     }
-    for(;;) {
-      int c = getchar();
-      if (c == EOF) {
-        cont = 0;
-        break;
-      }
-      else if (c == '\n') {
-        if (i>0 && buffer[i-1] == '\\')
-          buffer[i-1] = '\n';
-        else break;
-      }
-      else if (i >= MAXINPUT-1) {
-        fprintf(stderr, "lua: input line too long\n");
-        break;
-      }
-      else buffer[i++] = (char)c;
+    buffer = readline(s);
+    if (buffer) {
+      add_history(buffer);
+      ldo(lua_dostring, buffer);
+      free(buffer);
     }
-    buffer[i] = '\0';
-    ldo(lua_dostring, buffer);
     lua_settop(L, 0);  /* remove eventual results */
-  }
+  } while (buffer);
   printf("\n");
 }

@@ -319,4 +308,3 @@
     lua_close(L);
   return status;
 }
-

-- 
http://sc3d.org/rrt/ | humour, n.  unexpected recognition