aboutsummaryrefslogtreecommitdiffstats
path: root/src/getkey.c
blob: b17558a3840945e858d24598f5ae3d93766a867a (plain)
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

#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

/* A very simple keygrabber. */

struct termios tp;

void reset_term(int x) {
	tcsetattr(0,TCSANOW,&tp);
	exit(x);
}

int main(int argc, char **argv) {
	char foo[2];
	int tp_if,tp_of,tp_lf;
	int x;
	
	if (argc>1) {
		for (x=0;argv[1][x];x++) argv[1][x]=toupper(argv[1][x]);
	}
	foo[0]=foo[1]='\0';
	signal(SIGTERM,reset_term);
	tcgetattr(0,&tp);
	tp_if=tp.c_iflag;
	tp_of=tp.c_oflag;
	tp_lf=tp.c_lflag;
	tp.c_iflag=0;
	tp.c_oflag &= ~OPOST;
	tp.c_lflag &= ~(ISIG | ICANON);
	tcsetattr(0,TCSANOW,&tp);
	tp.c_iflag=tp_if;
	tp.c_oflag=tp_of;
	tp.c_lflag=tp_lf;
	while (1) {
		read(0,foo,1);
		foo[0]=toupper(foo[0]);
		/* Die if we get a control-c or control-d */
		if (foo[0]==3 || foo[0]==4) reset_term(1);
		if ((!argv[1]) || strstr(argv[1],foo)) {
			reset_term(0);
		}
	}
}