Handle CTRL-C at least for readline.
authorJan Kratochvil <jan.kratochvil@redhat.com>
Sat, 11 Aug 2012 11:56:50 +0000 (13:56 +0200)
committerJan Kratochvil <jan.kratochvil@redhat.com>
Sat, 11 Aug 2012 11:56:50 +0000 (13:56 +0200)
main.c

diff --git a/main.c b/main.c
index f4be40c..b00bc3d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,6 +6,7 @@
 #include <readline/history.h>
 #include <assert.h>
 #include <errno.h>
+#include <setjmp.h>
 #include "ansidecl.h"
 #include "mi_gdb.h"
 
@@ -198,6 +199,43 @@ gdb_done (mi_h *h, const char *command)
   mi_free_output (res);
 }
 
+static jmp_buf gdb_readline_jmp_buf;
+
+static void
+gdb_readline_sigint_handler (int signum)
+{
+  gdb_assert (signum == SIGINT);
+  longjmp (gdb_readline_jmp_buf, 1);
+}
+
+static char *
+gdb_readline (const char *prompt)
+{
+  char *retval;
+  sighandler_t saved_handler;
+  sigset_t mask;
+  int i;
+
+  i = sigprocmask (SIG_SETMASK, NULL, &mask);
+  assert (i == 0);
+  saved_handler = signal (SIGINT, gdb_readline_sigint_handler);
+  if (setjmp (gdb_readline_jmp_buf) != 0)
+    {
+      rl_free_line_state ();
+      rl_cleanup_after_signal ();
+
+      /* GDB prints this.  */
+      puts ("Quit");
+
+      i = sigprocmask (SIG_SETMASK, &mask, NULL);
+      assert (i == 0);
+    }
+  retval = readline (prompt);
+  saved_handler = signal (SIGINT, saved_handler);
+  gdb_assert (saved_handler == gdb_readline_sigint_handler);
+  return retval;
+}
+
 static void
 commands_command_console_cb (const char *str, void *data)
 {
@@ -236,7 +274,7 @@ commands_command (mi_h *h, const char *cmd_param)
       int do_nest = 0;
 
       prompt = xstrprintf ("%*s>", (int) nesting);
-      data = readline (prompt);
+      data = gdb_readline (prompt);
       xfree (prompt);
       if (data == NULL)
        data = xstrdup ("end");
@@ -525,7 +563,7 @@ main (int argc, char **argv)
       char *prompt, *cmd;
       
       prompt = gdb_show_string (h, "prompt");
-      cmd = readline (prompt);
+      cmd = gdb_readline (prompt);
       xfree (prompt);
 
       if (cmd == NULL)