From 9ebf1ae5bf236f7797c6256f3784f04f45777c85 Mon Sep 17 00:00:00 2001
From: Pixel <pixel@nobis-crew.org>
Date: Tue, 24 Nov 2009 16:06:27 -0800
Subject: Castrating a bit lua-interface, to still be able to publish 0.8
 versions out there, with an unthreadsafe Lua environment.

---
 src/lua-interface.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 71 insertions(+), 8 deletions(-)

(limited to 'src')

diff --git a/src/lua-interface.cpp b/src/lua-interface.cpp
index 2da6675..c26df03 100644
--- a/src/lua-interface.cpp
+++ b/src/lua-interface.cpp
@@ -21,7 +21,17 @@
 
 #define WIP
 
-#define VERSION "0.8"
+#define LUA_INTERFACE_VERSION_MAJOR 0
+#define LUA_INTERFACE_VERSION_MINOR 8
+
+#define SUBNUM2STR(num) #num
+#define NUM2STR(num) SUBNUM2STR(num)
+
+#define LUA_INTERFACE_VERSION NUM2STR(LUA_INTERFACE_VERSION_MAJOR) "." NUM2STR(LUA_INTERFACE_VERSION_MINOR)
+
+#if (LUA_INTERFACE_VERSION_MAJOR > 0) || (LUA_INTERFACE_VERSION_MINOR > 8)
+#define HAS_FULL_MULTITHREADING
+#endif
 
 #include <stdio.h>
 
@@ -35,15 +45,17 @@
 #include <Output.h>
 #include <Buffer.h>
 #include <BLua.h>
+#ifdef HAS_FULL_MULTITHREADING
 #include <LuaCommandLine.h>
 #include <HttpServ.h>
 #include <CopyJob.h>
 #include <MailServer.h>
 #include <Message.h>
 #include <Form.h>
-#include <BRegex.h>
 #include <LuaTask.h>
 #include <TaskMan.h>
+#endif
+#include <BRegex.h>
 #include <generic.h>
 #include <Main.h>
 #include <lua-plugin.h>
@@ -72,13 +84,15 @@ extern void luaconfigfiles_init(Lua * L) WEAK;
 extern void luaftgl_init(Lua * L) WEAK;
 extern void luagl_init(Lua * L) WEAK;
 extern void luahandle_init(Lua * L) WEAK;
+#ifdef HAS_FULL_MULTITHREADING
 extern void luahttp_init(Lua * L) WEAK;
 extern void luasmtp_init(Lua * L) WEAK;
+extern void luatask_init(Lua * L) WEAK;
+#endif
 extern void lualibs_init(Lua * L) WEAK;
 extern void luaocci_init(Lua * L) WEAK;
 extern void luaregex_init(Lua * L) WEAK;
 extern void luasql_init(Lua * L) WEAK;
-extern void luatask_init(Lua * L) WEAK;
 extern void luaxml_init(Lua * L) WEAK;
 extern void luaosmesa_init(Lua * L) WEAK;
 extern void luacd_init(Lua * L) WEAK;
@@ -219,12 +233,14 @@ class LuaStderrPrinter : public LuaPrinter {
 
 LuaStderrPrinter lp_stderr;
 
+#ifdef HAS_FULL_MULTITHREADING
 String server_fname = "server.lua";
 
 static int StartTaskLoop(void * foo) {
     TaskMan::MainLoop();
     return 0;
 }
+#endif
 
 class Luabaselua_interface : public LuaObject {
   public:
@@ -314,10 +330,14 @@ int sLua_baselua_interface::baselua_interface_proceed_statics(Lua * L, int n, in
 #endif
         } else if ((filename == "luahandle") && (luahandle_init != NULL)) {
             luahandle_init(L);
+#ifdef HAS_FULL_MULTITHREADING
         } else if ((filename == "luahttp") && (luahttp_init != NULL)) {
             luahttp_init(L);
         } else if ((filename == "luasmtp") && (luasmtp_init != NULL)) {
             luasmtp_init(L);
+        } else if ((filename == "luatask") && (luatask_init != NULL)) {
+            luatask_init(L);
+#endif
         } else if ((filename == "lualibs") && (lualibs_init != NULL)) {
             lualibs_init(L);
 #ifndef LUA_INTERFACE_LIGHT
@@ -332,8 +352,6 @@ int sLua_baselua_interface::baselua_interface_proceed_statics(Lua * L, int n, in
         } else if ((filename == "luasql") && (luasql_init != NULL)) {
             luasql_init(L);
 #endif
-        } else if ((filename == "luatask") && (luatask_init != NULL)) {
-            luatask_init(L);
         } else if ((filename == "luaxml") && (luaxml_init != NULL)) {
             luaxml_init(L);
 #ifndef LUA_INTERFACE_LIGHT
@@ -443,16 +461,19 @@ struct option long_options[] = {
     {"built-in",	0, NULL, 'b'},
     {"probe",		0, NULL, 'p'},
     {"log",             1, NULL, 'g'},
+#ifdef HAS_FULL_MULTITHREADING
     {"mail",            2, NULL, 'm'},
     {"server",          2, NULL, 's'},
     {"telnet",          2, NULL, 't'},
     {"server_fname",    1, NULL, 'f'},
+#endif
 #ifdef HAVE_FORK
     {"daemonize",	1, NULL, 'z'},
 #endif
     {0,			0, NULL,  0 }
 };
 
+#ifdef HAS_FULL_MULTITHREADING
 String LUACall_Names[] = {"identifier"};
 String LUACall_Defaults[] = {""};
 String LUACall_Invites[] = {"LUA function name:"};
@@ -497,6 +518,7 @@ Task * LUACall::Do(Variables * v, Variables * headers, Handle * h) {
     }
     return Message::Do(v, headers, h);
 }
+#endif
 
 bool auto_exec = true;
 
@@ -535,12 +557,15 @@ Lua * start_full_lua(void) {
 
 void showbanner() {
     printm(M_BARE,
-"Lua-Interface" LIGHT " version " VERSION " (c) 2003-2009 Nicolas \"Pixel\" Noble\n"
+"Lua-Interface" LIGHT " version " LUA_INTERFACE_VERSION " (c) 2003-2009 Nicolas \"Pixel\" Noble\n"
 LUA_RELEASE " " LUA_COPYRIGHT "\n"
 LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n"
 #ifdef WIP
 "Special version Work In Progress, compiled the " __DATE__ " at " __TIME__ "\n"
 #endif
+#ifndef HAS_FULL_MULTITHREADING
+"This version doesn't support full multithreading yet.\n"
+#endif
 "This is free software with ABSOLUTELY NO WARRANTY.\n"
 "\n");
 }
@@ -578,10 +603,12 @@ void showhelp(bool longhelp = false) {
 "  -e <cmd> to execute this single command in LUA.\n"
 "  -b       to force the use of the built-in lua-interface.lua\n"
 "  -g <log> to log into a file.\n"
+#ifdef HAS_FULL_MULTITHREADING
 "  -m[mp]   start the mail server on port mp (2500 by default).\n"
 "  -s[hp]   start the built-in http server on port hp (1500 by default).\n"
 "  -t[tp]   start the telnet server, on port tp (1550 by default).\n"
 "  -f <fil> filename of server code to use (server.lua by default).\n"
+#endif
 #ifdef HAVE_FORK
 "  -z       daemonize; incompatible with -i; use with -g.\n"
 #endif
@@ -641,6 +668,7 @@ static bool runlua(Lua * L, Buffer * command) {
     return true;
 }
 
+#ifdef HAS_FULL_MULTITHREADING
 class PrompterTask : public Task {
   public:
       PrompterTask(Lua * L) : r(Socket::CreatePair()), w(r.GetPair()), c(0), L(L) {
@@ -680,6 +708,7 @@ class PrompterTask : public Task {
     Task * c;
     Lua * L;
 };
+#endif
 
 struct interactive_prompt_data_t {
     Lua * L;
@@ -699,9 +728,11 @@ static void * interactive_prompt(void * __ipd) {
 #ifdef _WIN32
     rl_getc_function = my_getc;
 #else
+#ifdef HAS_FULL_MULTITHREADING
     sigset_t sigset;
     sigfillset(&sigset);
     pthread_sigmask(SIG_BLOCK, &sigset, NULL);
+#endif
 #endif
     rl_bind_key('\t', rl_insert);
     while (interactive) {
@@ -775,18 +806,22 @@ void autoload_exports(Lua * L) {
         luahandle_init(L);
     if (luaconfigfiles_init)
         luaconfigfiles_init(L);
+#ifdef HAS_FULL_MULTITHREADING
     if (luahttp_init)
         luahttp_init(L);
     if (luasmtp_init)
         luasmtp_init(L);
+#endif
     if (luaregex_init)
         luaregex_init(L);
 #ifndef LUA_INTERFACE_LIGHT
     if (luasql_init)
         luasql_init(L);
 #endif
+#ifdef HAS_FULL_MULTITHREADING
     if (luatask_init)
         luatask_init(L);
+#endif
     if (luaxml_init)
         luaxml_init(L);
 #ifndef LUA_INTERFACE_LIGHT
@@ -816,8 +851,10 @@ virtual int startup() throw (GeneralException) {
     bool todo = false, write = false, builtin = false, daemonize = false;
     char * file = 0, * output = 0, * exec = 0;
     Lua * L = 0;
+#ifdef HAS_FULL_MULTITHREADING
     String hport = "1500", tport = "1550", mport = "2500";
     pthread_t interactive_thread;
+#endif
     
     verbosity = M_WARNING;
 
@@ -856,25 +893,41 @@ virtual int startup() throw (GeneralException) {
             printer = new lua_interface_printer_t(new Output(optarg));
             break;
         case 's':
+#ifdef HAS_FULL_MULTITHREADING
     	    hserver = true;
     	    todo = true;
             if (optarg)
                 hport = optarg;
     	    break;
+#else
+            throw GeneralException("This version doesn't support full multithreading yet.");
+#endif    	
         case 'm':
+#ifdef HAS_FULL_MULTITHREADING
     	    mserver = true;
     	    todo = true;
             if (optarg)
                 mport = optarg;
+#else
+            throw GeneralException("This version doesn't support full multithreading yet.");
+#endif
     	    break;
     	case 't':
+#ifdef HAS_FULL_MULTITHREADING
             tserver = true;
             todo = true;
             if (optarg)
     	        tport = optarg;
     	    break;
+#else
+            throw GeneralException("This version doesn't support full multithreading yet.");
+#endif
     	case 'f':
+#ifdef HAS_FULL_MULTITHREADING
     	    server_fname = optarg;
+#else
+            throw GeneralException("This version doesn't support full multithreading yet.");
+#endif
     	    break;
 #ifdef HAVE_FORK
 	case 'z':
@@ -972,33 +1025,42 @@ virtual int startup() throw (GeneralException) {
 	L->load(&command);
     }
     
-#ifdef HAVE_FORK
     if (daemonize) {
+#ifdef HAVE_FORK
         if (fork()) {
             throw GeneralException("Going daemon...");
         }
-    }
+#else
+        throw GeneralException("Can't fork on this platform.");
 #endif
+    }
     
+#ifdef HAS_FULL_MULTITHREADING
     if (mserver || hserver || tserver) {
 	TaskMan::Init();
         if (hserver)
 	    L->load(&Input(server_fname));
     }
+#endif
 
     if (interactive) {
         interactive_prompt_data_t * ipd = (interactive_prompt_data_t *) malloc(sizeof(interactive_prompt_data_t));
+#ifdef HAS_FULL_MULTITHREADING
         if (mserver || tserver || hserver) {
             ipd->w = (new PrompterTask(L))->GetWriter();
             ipd->L = NULL;
     	    pthread_create(&interactive_thread, NULL, interactive_prompt, ipd);
         } else {
+#else
+        {
+#endif
             ipd->L = L;
             ipd->w = NULL;
             interactive_prompt(ipd);
         }
     }
 
+#ifdef HAS_FULL_MULTITHREADING
     if (hserver) {
 	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(L));
@@ -1018,6 +1080,7 @@ virtual int startup() throw (GeneralException) {
         if (interactive)
     	    pthread_join(interactive_thread, NULL);
     }
+#endif
 
     return 0;
 }
-- 
cgit v1.2.3