summaryrefslogtreecommitdiff
path: root/src/lua-interface.cpp
diff options
context:
space:
mode:
authorpixel <pixel>2008-07-04 14:31:23 +0000
committerpixel <pixel>2008-07-04 14:31:23 +0000
commit014e46b413d1d7221db3d29d2f5971dea11e25e8 (patch)
tree807e481c7146ed9b1227bc7c832917064979745b /src/lua-interface.cpp
parent31d0dc47ccad718ae9854577a76d617805f5096c (diff)
Putting the command line interface into a thread, and enabling win32 compilation for pthreads.
Diffstat (limited to 'src/lua-interface.cpp')
-rw-r--r--src/lua-interface.cpp189
1 files changed, 108 insertions, 81 deletions
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,11 +738,20 @@ 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());
new ReloadLUA();
@@ -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