-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctrl_q.c
60 lines (45 loc) · 1.19 KB
/
ctrl_q.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <ctype.h>
#include <errno.h>
#include "errorHandler.h"
#define ctrl_key(k) ((k) & 0x1f)
struct termios orig;
void throwErrorMsg(char *strstr) {
perror(strstr);
exit(1);
}
void quitRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig) == -1) throwErrorMsg("tcsetattr");
}
void enterRawMode() {
if (tcgetattr(STDIN_FILENO, &orig) == -1) throwErrorMsg("tcgetattr");
atexit(quitRawMode);
struct termios orig_copy = orig;
// Turn off:
orig_copy.c_lflag &= ~(ECHO);
orig_copy.c_lflag &= ~(ICANON); // canonical mode
orig_copy.c_lflag &= ~(ISIG); // ctrl-c, ctrl-z
orig_copy.c_lflag &= ~(ICRNL); // carriage return -> report
orig_copy.c_oflag &= ~(OPOST); // output processing -> report
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_copy) == -1)
throwErrorMsg("tcsetattr");
}
int main() {
enterRawMode();
while (1) {
char c = '\0';
if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN)
throwErrorMsg("read");
if (iscntrl(c)) {
printf("%d\r\n", c);
} else {
printf("%d ('%c')\r\n", c, c);
}
if (c == ctrl_key('q'))
break;
}
return 0;
}