Bug 660235 — Consistent signal handlers to a terminal would be cool
authorPhilip Withnall <philip@tecnocode.co.uk>
Fri, 13 Jan 2012 19:37:29 +0000 (19:37 +0000)
committerPhilip Withnall <philip@tecnocode.co.uk>
Mon, 26 Mar 2012 12:08:46 +0000 (13:08 +0100)
Fix folks-inspect’s handling of SIGINT, SIGTERM and EOF. In the former case,
we want to clear the current input buffer and display a new prompt. In
the case of SIGTERM we want to exit cleanly, and in the case of EOF we want
to exit if the input buffer is empty.

The combination of readline, Unix signals and threads in this was unfun.

Closes: https://bugzilla.gnome.org/show_bug.cgi?id=660235

NEWS
tools/inspect/Makefile.am
tools/inspect/inspect.vala

diff --git a/NEWS b/NEWS
index c1c9858..b5a6bdd 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,7 @@ Bugs fixed:
 * Bug 671900 — Folks should not suggest linking contacts from telepathy-salut
 * Bug 670872 — Should be less sensitive to accentuated chars
 * Bug 669984 — Does not notify when contacts are added to groups
+* Bug 660235 — Consistent signal handlers to a terminal would be cool
 
 Overview of changes from libfolks 0.6.6 to libfolks 0.6.7
 =============================================================
index 3ceb370..0bf0c9b 100644 (file)
@@ -1,6 +1,7 @@
 VALAFLAGS = \
        $(AM_VALAFLAGS) \
        --vapidir=$(top_srcdir)/folks \
+       --pkg=posix \
        --pkg=readline \
        --pkg=gobject-2.0 \
        --pkg=gio-2.0 \
index f0c587f..1f34103 100644 (file)
@@ -57,9 +57,19 @@ public class Folks.Inspect.Client : Object
           return 1;
         }
 
-      /* Create the client and run the command. */
+      /* Create the client. */
       main_client = new Client ();
 
+      /* Set up signal handling. */
+      Unix.signal_add (Posix.SIGTERM, () =>
+        {
+          /* Quit the client and let that exit the process. */
+          main_client.quit ();
+
+          return false;
+        });
+
+      /* Run the command. */
       if (args.length == 1)
         {
           main_client.run_interactive ();
@@ -209,6 +219,24 @@ public class Folks.Inspect.Client : Object
           assert_not_reached ();
         });
 
+      /* Handle SIGINT. */
+      Unix.signal_add (Posix.SIGINT, () =>
+        {
+          /* Tidy up. */
+          Readline.free_line_state ();
+          Readline.cleanup_after_signal ();
+          Readline.reset_after_signal ();
+
+          /* Display a fresh prompt. */
+          stdout.printf ("^C");
+          Readline.crlf ();
+          Readline.reset_line_state ();
+          Readline.replace_line ("", 0);
+          Readline.redisplay ();
+
+          return true;
+        });
+
       /* Allow things to be set for folks-inspect in ~/.inputrc, and install our
        * own completion function. */
       Readline.readline_name = "folks-inspect";