"Fossies" - the Fresh Open Source Software Archive 
Member "showkey-1.8/showkey.c" (27 Jan 2017, 2785 Bytes) of package /linux/misc/showkey-1.8.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "showkey.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
1.7_vs_1.8.
1 /*
2 * showkey.c -- display cooked key sequences
3 *
4 * Invoke this (no arguments needed) to see keycap-to-keystrokes mappings.
5 *
6 * by Eric S. Raymond <esr@snark.thyrsus.com>.
7 */
8 #include <stdio.h>
9 #include <termios.h>
10 #include <signal.h>
11 #include <string.h>
12 #include <stdbool.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #define ALT 0x80
17 #define ESC 0x1b
18
19 static char *lowchars[] =
20 {
21 "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
22 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
23 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
24 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
25 "SP",
26 };
27
28 static int signalled;
29
30 static void catcher(const int sig)
31 {
32 signalled = sig;
33 }
34
35 #define STARTCOOKIE '<'
36 #define ENDCOOKIE '>'
37
38 static void visualize(int c, /*@out@*/char *buf)
39 {
40 char *end;
41 bool unprintable;
42
43 buf[0] = '\0';
44 if ((c & ALT) != 0) {
45 unprintable = true;
46 c &=~ ALT;
47 buf[0] = STARTCOOKIE;
48 buf[1] = '\0';
49 (void) strcat(buf, "ALT-");
50 }
51
52 if (c <= ' ') {
53 unprintable = true;
54 end = buf + strlen(buf);
55 if (buf[0] != STARTCOOKIE) {
56 *end++ = STARTCOOKIE;
57 *end++ = '\0';
58 } if (c > 0 && c < 27) {
59 (void) strcat(buf, "CTL-");
60 end = buf + strlen(buf);
61 *end++ = (c + 0x40);
62 *end++ = '=';
63 *end = 0;
64 }
65 (void) strcat(buf, lowchars[c]);
66 }
67 else if (c == 0x3F) {
68 unprintable = true;
69 (void) strcat(buf, "DEL");
70 } else {
71 unprintable = false;
72 buf[0] = c;
73 buf[1] = '\0';
74 }
75
76 if (unprintable) {
77 end = buf + strlen(buf);
78 *end++ = ENDCOOKIE;
79 *end++ = '\0';
80 }
81 }
82
83
84 int main()
85 {
86 struct termios cooked, raw;
87 unsigned char c;
88 int i;
89 char intrchar[32], quitchar[32];
90
91 /*@-unrecog@*/
92 for (i = SIGHUP; i <= SIGIO; i++)
93 (void) signal(i, catcher);
94 /*@=unrecog@*/
95
96 // Get the state of the tty
97 (void) tcgetattr(0, &cooked);
98 // Make a copy we can mess with
99 (void) memcpy(&raw, &cooked, sizeof(struct termios));
100 // Turn off echoing, linebuffering, and special-character processing,
101 // but not the SIGINT or SIGQUIT keys.
102 raw.c_lflag &=~ (ICANON | ECHO);
103 // Ship the raw control blts
104 (void) tcsetattr(0, TCSANOW, &raw);
105
106 (void) printf("Type any key to see the sequence it sends.\n");
107 visualize(raw.c_cc[VINTR], intrchar);
108 visualize(raw.c_cc[VQUIT], quitchar);
109 (void) printf("Terminate with your shell interrupt %s or quit %s character.\n",
110 intrchar, quitchar);
111 signalled = 0;
112 while (signalled == 0)
113 {
114 char cbuf[32];
115
116 (void)read(0, &c, 1);
117 visualize(c, cbuf);
118 (void)fputs(cbuf, stdout);
119 (void) fflush(stdout);
120 }
121
122 (void) printf("\nBye...\n");
123 // Restore the cooked state
124 (void) tcsetattr(0, TCSANOW, &cooked);
125 exit(0);
126 }
127
128 /* showkey.c ends here */