1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
lefcha |
1.10 |
#include <unistd.h> |
3 |
lefcha |
1.9 |
#include <string.h> |
4 |
lefcha |
1.1 |
#include <errno.h> |
5 |
|
|
#include <termios.h> |
6 |
|
|
|
7 |
lefcha |
1.2 |
#include "config.h" |
8 |
lefcha |
1.1 |
#include "imapfilter.h" |
9 |
|
|
|
10 |
|
|
|
11 |
lefcha |
1.4 |
extern unsigned int flags; |
12 |
|
|
|
13 |
lefcha |
1.3 |
static struct termios otio, ntio; /* Original and new tty settings. */ |
14 |
lefcha |
1.1 |
|
15 |
|
|
|
16 |
|
|
/* |
17 |
|
|
* Store original term attributes. |
18 |
|
|
*/ |
19 |
lefcha |
1.7 |
int |
20 |
|
|
tty_store(void) |
21 |
lefcha |
1.1 |
{ |
22 |
lefcha |
1.12 |
|
23 |
lefcha |
1.8 |
if (isatty(fileno(stdin))) { |
24 |
lefcha |
1.7 |
flags |= FLAG_TTY; |
25 |
lefcha |
1.6 |
|
26 |
lefcha |
1.8 |
if (tcgetattr(fileno(stdin), &otio)) { |
27 |
|
|
error("getting term attributes; %s\n", strerror(errno)); |
28 |
|
|
return ERROR_TERMIO; |
29 |
|
|
} |
30 |
lefcha |
1.7 |
} |
31 |
|
|
return 0; |
32 |
lefcha |
1.1 |
} |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
* Disable echo. |
37 |
|
|
*/ |
38 |
lefcha |
1.7 |
int |
39 |
lefcha |
1.11 |
tty_noecho(void) |
40 |
lefcha |
1.1 |
{ |
41 |
lefcha |
1.12 |
|
42 |
lefcha |
1.7 |
if (!(flags & FLAG_TTY)) |
43 |
|
|
return 0; |
44 |
lefcha |
1.6 |
|
45 |
lefcha |
1.7 |
ntio = otio; |
46 |
|
|
ntio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); |
47 |
lefcha |
1.4 |
|
48 |
lefcha |
1.11 |
flags |= FLAG_TTYMOD; |
49 |
lefcha |
1.2 |
|
50 |
lefcha |
1.7 |
if (tcsetattr(fileno(stdin), TCSAFLUSH, &ntio)) { |
51 |
|
|
error("setting term attributes; %s\n", strerror(errno)); |
52 |
|
|
return ERROR_TERMIO; |
53 |
|
|
} |
54 |
|
|
return 0; |
55 |
lefcha |
1.1 |
} |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/* |
59 |
|
|
* Restore original term attributes. |
60 |
|
|
*/ |
61 |
lefcha |
1.7 |
int |
62 |
|
|
tty_restore(void) |
63 |
lefcha |
1.1 |
{ |
64 |
lefcha |
1.12 |
|
65 |
lefcha |
1.11 |
if (!(flags & FLAG_TTY) || !(flags & FLAG_TTYMOD)) |
66 |
lefcha |
1.7 |
return 0; |
67 |
lefcha |
1.5 |
|
68 |
lefcha |
1.7 |
if (tcsetattr(fileno(stdin), TCSAFLUSH, &otio)) { |
69 |
|
|
error("setting term attributes; %s\n", strerror(errno)); |
70 |
|
|
return ERROR_TERMIO; |
71 |
|
|
} |
72 |
lefcha |
1.11 |
flags &= ~(FLAG_TTYMOD); |
73 |
lefcha |
1.4 |
|
74 |
lefcha |
1.7 |
return 0; |
75 |
lefcha |
1.1 |
} |