fix console to ignore input while waiting, and rename have_input -> have_prompt lua-threads
authorTero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:23:27 +0300
branchlua-threads
changeset 206 47837a6bbbea
parent 205 c13d2fc7b480
child 207 3fa22abb5421
fix console to ignore input while waiting, and rename have_input -> have_prompt
src/console.c
src/console.h
--- a/src/console.c	Thu May 21 16:23:08 2009 +0300
+++ b/src/console.c	Thu May 21 16:23:27 2009 +0300
@@ -32,8 +32,8 @@
     /** Set as log output function? */
     bool log_output;
 
-    /** Prompt/input displayed? */
-    bool have_input;
+    /** Prompt displayed? */
+    bool have_prompt;
 
     /** Waiting for line to be processed */
     bool waiting;
@@ -52,13 +52,13 @@
     (void) fd;
     (void) what;
 
-    // update state
-    // XXX: needed?
-    console->have_input = true;
+    if (console->waiting)
+        // can't feed readline input while it's disabled
+        return log_warn("discrding input while waiting");
 
-    // tell readline to process it
-    // XXX: don't if console->waiting
-    rl_callback_read_char();
+    else
+        // tell readline to process it
+        rl_callback_read_char();
 }
 
 /**
@@ -82,7 +82,7 @@
     }
 
     // update state for console_print during processing
-    console->have_input = false;
+    console->have_prompt = false;
 
     // invoke the console callback
     if (console->callbacks && console->callbacks->on_line)
@@ -96,16 +96,20 @@
     
     switch (status) {
         case CONSOLE_CONTINUE:
-            // update state, as the prompt will be displayed again
-            console->have_input = true;
+            // the prompt will be displayed again
+            console->have_prompt = true;
 
             break;
 
         case CONSOLE_WAIT:
+            // deactivate our read event
+            if (event_del(console->ev))
+                log_warn("unable to deactivate console read event");
+
             // remove the readline stuff to stop it from displaying the prompt
             rl_callback_handler_remove();
 
-            // update state to ignore input
+            // ignore input
             console->waiting = true;
 
             break;
@@ -153,6 +157,9 @@
     sigact.sa_handler = on_sigint;
     sigaction(SIGINT, &sigact, &console->old_sigint);
 
+    // setup state for initial readline prompt
+    console->have_prompt = true;
+
     // setup readline
     rl_callback_handler_install(config->prompt, console_line);
     
@@ -182,13 +189,17 @@
 
 void console_continue (struct console *console)
 {
-    assert(console->waiting);
+    if (!console->waiting)
+        return;
 
     // re-enable input
     console->waiting = false;
 
+    if (event_add(console->ev, NULL))
+        log_fatal("unable to re-enable console read event");
+    
     // the prompt will be displayed again
-    console->have_input = true;
+    console->have_prompt = true;
 
     // re-setup readline with prompt
     rl_callback_handler_install(console->config.prompt, console_line);
@@ -196,7 +207,7 @@
 
 err_t console_print (struct console *console, const char *line)
 {
-    if (console->have_input)
+    if (console->have_prompt)
         // don't interrupt current input line
         rl_crlf();
 
@@ -204,7 +215,7 @@
     if (printf("%s\n", line) < 0)
         return _ERR_GENERAL;
     
-    if (console->have_input) {
+    if (console->have_prompt) {
         // restore input
         rl_on_new_line();
         rl_redisplay();
--- a/src/console.h	Thu May 21 16:23:08 2009 +0300
+++ b/src/console.h	Thu May 21 16:23:27 2009 +0300
@@ -84,6 +84,9 @@
 
 /**
  * Continue reading input after a CONSOLE_WAIT return code from console_callbacks::on_line.
+ *
+ * This does nothing if the console is not currently in the waiting state (i.e. last console_callbacks::on_line
+ * returned CONSOLE_WAIT).
  */
 void console_continue (struct console *console);