From a834381afe97ed1562bb32325edaff85b7b22fbe Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 27 Aug 2002 03:13:21 +0000 Subject: [PATCH] Changed to termios. --- showkey.c | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/showkey.c b/showkey.c index cbe627f..3aa937a 100644 --- a/showkey.c +++ b/showkey.c @@ -27,9 +27,10 @@ THE SOFTWARE.

* by Eric S. Raymond , 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 -#include +#include #include #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 */ -- 2.43.0