From: Eric S. Raymond Date: Thu, 21 Oct 2010 23:18:52 +0000 (-0400) Subject: Factor out the report function. X-Git-Tag: 1.5~6 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=2144dec309d3eefca5103420cffbcf77a4ad3f6f;p=showkey.git Factor out the report function. --- diff --git a/showkey.c b/showkey.c index c25060a..d6aa4d4 100644 --- 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), + "", 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("", c); - else - (void) putchar(c); + (void)fputs(visualize(c), stdout); (void) fflush(stdout); }