]> thyrsus.com Git - showkey.git/commitdiff
Changed to termios.
authorEric S. Raymond <esr@thyrsus.com>
Tue, 27 Aug 2002 03:13:21 +0000 (03:13 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Tue, 27 Aug 2002 03:13:21 +0000 (03:13 +0000)
showkey.c

index cbe627ff0a57a69ab3a701d1e8cd942695022139..3aa937a6d4264de68c565bae4b7feb6c6f930985 100644 (file)
--- a/showkey.c
+++ b/showkey.c
@@ -27,9 +27,10 @@ THE SOFTWARE.<P>
  * by Eric S. Raymond <esr@snark.thyrsus.com>, 1 Nov 88
  * - fix for little-endian machines (version 1.1), 21 Oct 1996.
  * - cleanup and modern packaging (version 1.2), 1 Aug 2002.
+ * - changed to use termios (version 1.3), 26 Aug 2002.
  */
 #include <stdio.h>
-#include <termio.h>
+#include <termios.h>
 #include <signal.h>
 
 #define META   0x80
@@ -46,50 +47,37 @@ static char *lowchars[] =
 
 static int signalled;
 
-static void catcher(sig)
-int sig;
+static void catcher(const int sig)
 {
     signalled = sig;
 }
 
 main()
 {
-    struct termio      cooked, raw;
+    struct termios     cooked, raw;
     unsigned char      c;
     unsigned int       i, timeouts;
 
     for (i = SIGHUP; i <= SIGPOLL; i++)
        (void) signal(c, catcher);
 
-    (void) ioctl(0, TCGETA, &cooked);
-
-    (void) memcpy(&raw, &cooked, sizeof(struct termio));
+    // Get the state of the tty 
+    (void) tcgetattr(0, &cooked);
+    // Make a copy we can mess with
+    (void) memcpy(&raw, &cooked, sizeof(struct termios));
+    // Turn off echoing, linebuffering, and special-character processing,
+    // but not the SIGINT or SIGQUIT keys.
     raw.c_lflag &=~ (ICANON | ECHO);
-    raw.c_cc[VEOL] = 1;
-    raw.c_cc[VEOF] = 2;
-    (void) ioctl(0, TCSETA, &raw);
+    // Ship the raw control blts
+    (void) tcsetattr(0, TCSANOW, &raw);
 
-    (void) printf("Type any key to see the sequence it sends, DEL to quit.\n");
+    (void) printf("Type any key to see the sequence it sends, ^C to quit.\n");
     signalled = 0;
-    timeouts = 1;
     while (!signalled)
     {
        int             meta;
 
-       if (read(0, &c, 1) != 0)
-           timeouts = 0;
-       else
-       /*
-        * This is supposed to cause the program to emit a newline .2 sec
-        * after reading a key sequence, but it doesn't work, I don't know
-        * why.
-        */
-       {
-           if (timeouts++ == 0)
-               (void) putchar('\n');
-           continue;
-       }
-
+       read(0, &c, 1);
        if (meta = (c & META))
            c &=~ META;
 
@@ -105,7 +93,8 @@ main()
     }
 
     (void) printf("\nBye...\n");
-    (void) ioctl(0, TCSETA, &cooked);
+    // Restore the cooked state
+    (void) tcsetattr(0, TCSANOW, &cooked);
 }
 
 /* showkey.c ends here */