-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathechokeys.c
45 lines (42 loc) · 1.11 KB
/
echokeys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <stdio.h>
struct termios saved_tattr;
void
restore_tattr (void)
{
if (isatty (STDIN_FILENO))
tcsetattr (STDIN_FILENO, TCSANOW, &saved_tattr);
}
int
main(int argc, char *argv[])
{
unsigned char c;
if (isatty (STDIN_FILENO)) {
tcgetattr (STDIN_FILENO, &saved_tattr);
struct termios tattr = saved_tattr;
tattr.c_iflag &=
~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
tattr.c_oflag &= ~OPOST;
tattr.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
tattr.c_cflag &= ~(CSIZE|PARENB);
tattr.c_cflag |= CS8;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
atexit (restore_tattr);
}
setbuf(stdout, 0);
while (*++argv)
fputs(*argv, stdout);
while ((c = getchar()) != 4) {
switch (c) {
case 0x00 ... 0x1f: putchar('^'); putchar(c + 0x40); break;
case '^': case '\\': putchar('\\'); putchar(c); break;
case 0x7f: putchar('^'); putchar('?'); break;
case 0x80 ... 0xFF: printf("\\x%2X", c); break;
default: putchar(c);
}
}
putchar ('\n');
return 0;
}