From 014e46b413d1d7221db3d29d2f5971dea11e25e8 Mon Sep 17 00:00:00 2001
From: pixel <pixel>
Date: Fri, 4 Jul 2008 14:31:23 +0000
Subject: Putting the command line interface into a thread, and enabling win32
 compilation for pthreads.

---
 Makefile.mingw32      |   3 +-
 src/lua-interface.cpp | 189 ++++++++++++++++++++++++++++----------------------
 2 files changed, 110 insertions(+), 82 deletions(-)

diff --git a/Makefile.mingw32 b/Makefile.mingw32
index 522947c..c8d136b 100644
--- a/Makefile.mingw32
+++ b/Makefile.mingw32
@@ -10,12 +10,13 @@ INCLUDES = \
 -I../Baltisot/lib/lua/include -I../Baltisot/lib/lua/includes \
 -I../lua-modules/src \
 -I../Baltisot/MSVC/iconv -I../Baltisot/MSVC/regex -I../Baltisot/MSVC \
+-I../pthreads-w32-2-8-0-release/ \
 
 HAVES = -DHAVE_VSSCANF -DHAVE_LIBJPEG -DHAVE_MALLOC_H -DHAVE_BYTESWAP_H
 
 CPPFLAGS += $(INCLUDES) -g -fexceptions -DLUATASK_OMIT_COMMAND -DSTDC_HEADERS -DLIBICONV_PLUG -DREADLINE_STATIC -fexceptions -DWORDS_LITTLEENDIAN -march=i686 $(HAVES) -DUSE_MPQLIB
 
-LDFLAGS += -g -fexceptions -Wl,--enable-auto-image-base -Wl,--export-dynamic --export-all-symbols -Wl,--enable-auto-import -fexceptions ../libreadline-static.a ../libz.a -lwsock32 -lws2_32
+LDFLAGS += -g -fexceptions -Wl,--enable-auto-image-base -Wl,--export-dynamic --export-all-symbols -Wl,--enable-auto-import -fexceptions ../libreadline-static.a ../libz.a -lwsock32 -lws2_32 ../pthreads-w32-2-8-0-release/libpthreadGC2.a
 
 vpath %.c ../Baltisot/lib:../Baltisot/src:../Baltisot/lib/zlib/src:../Baltisot/lib/lua/src:../Baltisot/lib/lua/src/LuaLib:src:../paperIdol/src:../mogltk/lib:../tinyxml:../lua-modules/src:../Baltisot/MSVC/iconv:../Baltisot/MSVC/regex
 vpath %.cc ../Baltisot/lib:../Baltisot/src:../Baltisot/lib/zlib/src:../Baltisot/lib/lua/src:../Baltisot/lib/lua/src/LuaLib:src:../paperIdol/src:../mogltk/lib:../tinyxml:../lua-modules/src:../Baltisot/MSVC/iconv:../Baltisot/MSVC/regex
diff --git a/src/lua-interface.cpp b/src/lua-interface.cpp
index 91af42d..d4e56c2 100644
--- a/src/lua-interface.cpp
+++ b/src/lua-interface.cpp
@@ -17,7 +17,7 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/* $Id: lua-interface.cpp,v 1.2 2008-07-04 12:59:11 pixel Exp $ */
+/* $Id: lua-interface.cpp,v 1.3 2008-07-04 14:31:23 pixel Exp $ */
 
 #define WIP
 
@@ -25,6 +25,8 @@
 
 #include <stdio.h>
 
+#include <pthread.h>
+
 #include <readline/readline.h>
 #include <readline/history.h>
 
@@ -422,6 +424,8 @@ Task * LUACall::Do(Variables * v, Variables * headers, Handle * h) {
     return Message::Do(v, headers, h);
 }
 
+bool auto_exec = true;
+
 CODE_BEGINS
 
 /* That's the basic lua starter for non interactive mode */
@@ -492,8 +496,7 @@ void showhelp(bool longhelp = false) {
 "\n"
 "Verbose mode can be somewhat a floody thing.\n"
 "Options -i/-e and -c are mutually exclusive.\n"
-"Options -i, -s and -c are mutually exclusive.\n"
-"Options -i/-s and -e are NOT mutually exclusive. For example:\n"
+"Options -i, -s and -e are NOT mutually exclusive. For example:\n"
 "\n"
 "  $ %s -i -e \"main()\" somescript.lua\n"
 "\n"
@@ -520,16 +523,100 @@ class lua_interface_printer_t : public printer_t {
     Handle * log;
 };
 
+static void * interactive_prompt(void * foo) {
+    char prompt[10], * line_read = 0;
+    String line, endline;
+    bool runit;
+    Buffer command;
+    int pos;
+    
+    
+
+    /* Interactive mode loop */
+    strcpy(prompt, "> ");
+#ifdef _WIN32
+    rl_getc_function = my_getc;
+#else
+    sigset_t sigset;
+    sigfillset(&sigset);
+    pthread_sigmask(SIG_BLOCK, &sigset, NULL);
+#endif
+    rl_bind_key('\t', rl_insert);
+    while (interactive) {
+	/* Basic usage of readline */
+	if (line_read)
+	    free(line_read);
+	
+	line_read = readline(prompt);
+	
+	if (!line_read) {
+	    printm(M_BARE, "\n");
+	    break;
+	}
+	
+	if (*line_read)
+	    add_history(line_read);
+	
+	line = line_read;
+	line = line.trim();
+	endline = "";
+	
+	/* Splitting the line between ;; */
+	while (line.strlen()) {	
+	    runit = false;
+	
+	    if ((pos = line.strstr(";;")) >= 0) {
+		endline = line.extract(pos + 2);
+		line = line.extract(0, pos);
+		runit = true;
+	    } else {
+		endline = "";
+	    }
+	    
+	    if (line[line.strlen() - 1] == '\\') {
+		line[line.strlen() - 1] = ' ';
+	    } else if (auto_exec) {
+		runit = true;
+            }
+	    
+	    command << line;
+	
+	    if (runit) {
+		try {
+		    L->load(&command);
+	        }
+		catch (LuaException e) {
+		    /* If there was an error, ignore it, and free the stack */
+		    while(L->gettop())
+			L->pop();
+                    printm(M_ERROR, "%s\n", e.GetMsg());
+	        }
+		catch (GeneralException e) {
+		    /* A more severe exception... */
+		    while(L->gettop())
+			L->pop();
+		    printm(M_ERROR, "Aborted. LUA caused the following exception: %s\n", e.GetMsg());
+		}
+		strcpy(prompt, "> ");
+	    } else {
+		strcpy(prompt, "- ");
+                command << "\n";
+	    }
+	    line = endline.trim();
+	}
+    }
+    
+    return NULL;
+}
+
 virtual int startup() throw (GeneralException) {
     char c;
 
-    bool auto_exec = true, strip = true, todo = false, runit, write = false, builtin = false;
-    char * file = 0, * output = 0, * compile = 0, * exec = 0, * line_read = 0;
-    char prompt[10];
+    bool strip = true, todo = false, write = false, builtin = false;
+    char * file = 0, * output = 0, * compile = 0, * exec = 0;
     Lua * L = 0;
-    Buffer command;
-    String line, endline, hport = "1500", tport = "1550";
-    int pos;
+    String hport = "1500", tport = "1550";
+    pthread_t interactive_thread;
     
     verbosity = M_WARNING;
 
@@ -651,10 +738,19 @@ virtual int startup() throw (GeneralException) {
     
     /* One shot command */
     if (exec) {
+	Buffer command;
 	command << exec;
 	L->load(&command);
     }
     
+    if (server) {
+	TaskMan::Init();
+    }
+    
+    if (interactive) {
+	pthread_create(&interactive_thread, NULL, interactive_prompt, NULL);
+    }
+    
     if (server) {
 	HttpServ * httpserv = new HttpServ(new Message("Welcome", "Welcome.", "start"), hport.to_int(), "Lua Interface");
 	new Form("LUACall", "luacallform", "Enter the function name to call", LUACall_Names, LUACall_Invites, LUACall_Defaults, LUACall_Lists, LUACall_Descs, 1, new LUACall());
@@ -665,79 +761,10 @@ virtual int startup() throw (GeneralException) {
 	StartTaskLoop(0);
     }
     
-    if (!interactive)
-	Exit(0);
-    
-    /* Interactive mode loop */
-    strcpy(prompt, "> ");
-#ifdef _WIN32
-    rl_getc_function = my_getc;
-#endif
-    rl_bind_key('\t', rl_insert);
-    while (interactive) {
-	/* Basic usage of readline */
-	if (line_read)
-	    free(line_read);
-	
-	line_read = readline(prompt);
-	
-	if (!line_read) {
-	    printm(M_BARE, "\n");
-	    break;
-	}
-	
-	if (*line_read)
-	    add_history(line_read);
-	
-	line = line_read;
-	line = line.trim();
-	endline = "";
-	
-	/* Splitting the line between ;; */
-	while (line.strlen()) {	
-	    runit = false;
-	
-	    if ((pos = line.strstr(";;")) >= 0) {
-		endline = line.extract(pos + 2);
-		line = line.extract(0, pos);
-		runit = true;
-	    } else {
-		endline = "";
-	    }
-	    
-	    if (line[line.strlen() - 1] == '\\') {
-		line[line.strlen() - 1] = ' ';
-	    } else if (auto_exec) {
-		runit = true;
-            }
-	    
-	    command << line;
-	
-	    if (runit) {
-		try {
-		    L->load(&command);
-	        }
-		catch (LuaException e) {
-		    /* If there was an error, ignore it, and free the stack */
-		    while(L->gettop())
-			L->pop();
-                    printm(M_ERROR, "%s\n", e.GetMsg());
-	        }
-		catch (GeneralException e) {
-		    /* A more severe exception... */
-		    while(L->gettop())
-			L->pop();
-		    printm(M_ERROR, "Aborted. LUA caused the following exception: %s\n", e.GetMsg());
-		}
-		strcpy(prompt, "> ");
-	    } else {
-		strcpy(prompt, "- ");
-                command << "\n";
-	    }
-	    line = endline.trim();
-	}
+    if (interactive) {
+	pthread_join(interactive_thread, NULL);
     }
-    
+
     return 0;
 }
 CODE_ENDS
-- 
cgit v1.2.3