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