2
0
Эх сурвалжийг харах

cleaner configuration for lua.c

Roberto Ierusalimschy 20 жил өмнө
parent
commit
6d475731ca
2 өөрчлөгдсөн 61 нэмэгдсэн , 56 устгасан
  1. 30 45
      lua.c
  2. 31 11
      luaconf.h

+ 30 - 45
lua.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.c,v 1.134 2005/01/10 16:30:59 roberto Exp roberto $
+** $Id: lua.c,v 1.135 2005/01/10 17:21:10 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -30,7 +30,7 @@
 
 
 static lua_State *globalL = NULL;
 static lua_State *globalL = NULL;
 
 
-static const char *progname = PROGNAME;
+static const char *progname = LUA_PROGNAME;
 
 
 
 
 
 
@@ -150,43 +150,12 @@ static int dolibrary (lua_State *L, const char *name) {
 }
 }
 
 
 
 
-
-/*
-** this macro defines a function to show the prompt and reads the
-** next line for manual input
-*/
-#ifndef lua_readline
-#define lua_readline(L,prompt)		readline(L,prompt)
-
-/* maximum length of an input line */
-#ifndef MAXINPUT
-#define MAXINPUT	512
-#endif
-
-
-static int readline (lua_State *L, const char *prompt) {
-  static char buffer[MAXINPUT];
-  if (prompt) {
-    fputs(prompt, stdout);
-    fflush(stdout);
-  }
-  if (fgets(buffer, sizeof(buffer), stdin) == NULL)
-    return 0;  /* read fails */
-  else {
-    lua_pushstring(L, buffer);
-    return 1;
-  }
-}
-
-#endif
-
-
 static const char *get_prompt (lua_State *L, int firstline) {
 static const char *get_prompt (lua_State *L, int firstline) {
-  const char *p = NULL;
+  const char *p;
   lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
   lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2");
   lua_rawget(L, LUA_GLOBALSINDEX);
   lua_rawget(L, LUA_GLOBALSINDEX);
   p = lua_tostring(L, -1);
   p = lua_tostring(L, -1);
-  if (p == NULL) p = (firstline ? PROMPT : PROMPT2);
+  if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
   lua_pop(L, 1);  /* remove global */
   lua_pop(L, 1);  /* remove global */
   return p;
   return p;
 }
 }
@@ -203,23 +172,39 @@ static int incomplete (lua_State *L, int status) {
 }
 }
 
 
 
 
+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 */
+  if (firstline && b[0] == '=')  /* first line starts with `=' ? */
+    lua_pushfstring(L, "return %s", b+1);  /* change it to `return' */
+  else
+    lua_pushstring(L, b);
+  return 1;
+}
+
+
 static int loadline (lua_State *L) {
 static int loadline (lua_State *L) {
   int status;
   int status;
   lua_settop(L, 0);
   lua_settop(L, 0);
-  if (lua_readline(L, get_prompt(L, 1)) == 0)  /* no input? */
-    return -1;
-  if (lua_tostring(L, -1)[0] == '=') {  /* line starts with `=' ? */
-    lua_pushfstring(L, "return %s", lua_tostring(L, -1)+1);/* `=' -> `return' */
-    lua_remove(L, -2);  /* remove original line */
-  }
+  if (!pushline(L, 1))
+    return -1;  /* no input */
   for (;;) {  /* repeat until gets a complete line */
   for (;;) {  /* repeat until gets a complete line */
     status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
     status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
     if (!incomplete(L, status)) break;  /* cannot try to add lines? */
     if (!incomplete(L, status)) break;  /* cannot try to add lines? */
-    if (lua_readline(L, get_prompt(L, 0)) == 0)  /* no more input? */
+    if (!pushline(L, 0))  /* no more input? */
       return -1;
       return -1;
-    lua_concat(L, lua_gettop(L));  /* join lines */
+    lua_pushliteral(L, "\n");  /* add a new line... */
+    lua_insert(L, -2);  /* ...between the two lines */
+    lua_concat(L, 3);  /* join them */
   }
   }
-  lua_saveline(L, lua_tostring(L, 1));
+  lua_saveline(L, 1);
   lua_remove(L, 1);  /* remove line */
   lua_remove(L, 1);  /* remove line */
   return status;
   return status;
 }
 }
@@ -260,7 +245,7 @@ static int checkvar (lua_State *L) {
 static int handle_argv (lua_State *L, char *argv[], int *interactive) {
 static int handle_argv (lua_State *L, char *argv[], int *interactive) {
   if (argv[1] == NULL) {  /* no arguments? */
   if (argv[1] == NULL) {  /* no arguments? */
     *interactive = 0;
     *interactive = 0;
-    if (stdin_is_tty())
+    if (lua_stdin_is_tty())
       dotty(L);
       dotty(L);
     else
     else
       dofile(L, NULL);  /* executes stdin as a file */
       dofile(L, NULL);  /* executes stdin as a file */

+ 31 - 11
luaconf.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: luaconf.h,v 1.36 2005/03/18 18:02:04 roberto Exp roberto $
+** $Id: luaconf.h,v 1.37 2005/03/18 18:55:45 roberto Exp roberto $
 ** Configuration file for Lua
 ** Configuration file for Lua
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -133,28 +133,48 @@
 /* CONFIG: definition of isatty */
 /* CONFIG: definition of isatty */
 #ifdef _POSIX_C_SOURCE
 #ifdef _POSIX_C_SOURCE
 #include <unistd.h>
 #include <unistd.h>
-#define stdin_is_tty()		isatty(0)
+#define lua_stdin_is_tty()	isatty(0)
 #elif defined(_WIN32)
 #elif defined(_WIN32)
 #include <io.h>
 #include <io.h>
 #include <stdio.h>
 #include <stdio.h>
-#define stdin_is_tty()		_isatty(_fileno(stdin))
+#define lua_stdin_is_tty()	_isatty(_fileno(stdin))
 #else
 #else
-#define stdin_is_tty()		1  /* assume stdin is a tty */
+#define lua_stdin_is_tty()	1  /* assume stdin is a tty */
 #endif
 #endif
 
 
 
 
-#define PROMPT		"> "
-#define PROMPT2		">> "
-#define PROGNAME	"lua"
-
+#define LUA_PROMPT		"> "
+#define LUA_PROMPT2		">> "
+#define LUA_PROGNAME		"lua"
 
 
+/*
+*@ LUA_MAXINPUT is the maximum length for an input line
+** CHANGE it if you need longer lines.
+*/
+#define LUA_MAXINPUT	512
 
 
 
 
 /*
 /*
-** CONFIG: this macro can be used by some history system to save lines
-** read in manual input
+*@ 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.
+** CHANGE them if you want to improve this functionality (e.g., using GNU
+** readline and history facilities). (Lua already tries to use those
+** facilities when it detects a GNU compiler.)
 */
 */
-#define lua_saveline(L,line)	/* empty */
+#ifdef __GNUC__
+#include <readline/readline.h>
+#include <readline/history.h>
+#define lua_readline(L,b,p)	(((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 */
+#else
+#define lua_readline(L,b,p)	\
+	(fputs(p, stdout), fflush(stdout),  /* show prompt */ \
+	fgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */
+#define lua_saveline(L,idx)	((void)0)
+#endif