]> thyrsus.com Git - showkey.git/commitdiff
Factor out the report function.
authorEric S. Raymond <esr@thyrsus.com>
Thu, 21 Oct 2010 23:18:52 +0000 (19:18 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Thu, 21 Oct 2010 23:18:52 +0000 (19:18 -0400)
showkey.c

index c25060afe1ac6304afb4f013ca7396a65657781e..d6aa4d46fd7c755650d438b09c38e34a4016cd8b 100644 (file)
--- a/showkey.c
+++ b/showkey.c
@@ -54,6 +54,32 @@ static void catcher(const int sig)
     signalled = sig;
 }
 
+char *visualize(int c)
+{
+    static char buf[32];
+    int meta;
+
+    if (meta = (c & META))
+       c &=~ META;
+
+    if (c <= ' ')
+       (void) snprintf(buf, sizeof(buf), 
+                       "<%s%s>", meta ? "ALT-" : "", lowchars[c]);
+    else if (c == 0x3F)
+       (void) snprintf(buf, sizeof(buf),
+                       "<%sDEL>", meta ? "ALT-" : "");
+    else if (meta)
+       (void) snprintf(buf, sizeof(buf), 
+                       "<ALT-%c>", c);
+    else {
+       buf[0] = c;
+       buf[1] = '\0';
+    }
+
+    return buf;
+}
+
+
 main()
 {
     struct termios     cooked, raw;
@@ -74,24 +100,13 @@ main()
     (void) tcsetattr(0, TCSANOW, &raw);
 
     (void) printf("Type any key to see the sequence it sends.\n");
-    (void) printf("Terminate with your shell interrupt character.\n");
+    (void) printf("Terminate with your shell interrupt character %s.\n",
+                 visualize(raw.c_cc[VINTR]));
     signalled = 0;
     while (!signalled)
     {
-       int             meta;
-
        read(0, &c, 1);
-       if (meta = (c & META))
-           c &=~ META;
-
-       if (c <= ' ')
-           (void) printf("<%s%s>", meta ? "ALT-" : "", lowchars[c]);
-       else if (c == 0x3F)
-           (void) printf("<%sDEL>", meta ? "ALT-" : "");
-       else if (meta)
-           (void) printf("<ALT-%c>", c);
-       else
-           (void) putchar(c);
+       (void)fputs(visualize(c), stdout);
        (void) fflush(stdout);
     }