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 |
if (isatty(fileno(stdin))) { |
23 |
flags |= FLAG_TTY; |
24 |
|
25 |
if (tcgetattr(fileno(stdin), &otio)) { |
26 |
error("getting term attributes; %s\n", strerror(errno)); |
27 |
return ERROR_TERMIO; |
28 |
} |
29 |
} |
30 |
return 0; |
31 |
} |
32 |
|
33 |
|
34 |
/* |
35 |
* Disable echo. |
36 |
*/ |
37 |
int |
38 |
tty_disable_echo(void) |
39 |
{ |
40 |
if (!(flags & FLAG_TTY)) |
41 |
return 0; |
42 |
|
43 |
ntio = otio; |
44 |
ntio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); |
45 |
|
46 |
flags |= FLAG_TTY_MODIFIED; |
47 |
|
48 |
if (tcsetattr(fileno(stdin), TCSAFLUSH, &ntio)) { |
49 |
error("setting term attributes; %s\n", strerror(errno)); |
50 |
return ERROR_TERMIO; |
51 |
} |
52 |
return 0; |
53 |
} |
54 |
|
55 |
|
56 |
/* |
57 |
* Restore original term attributes. |
58 |
*/ |
59 |
int |
60 |
tty_restore(void) |
61 |
{ |
62 |
if (!(flags & FLAG_TTY) || !(flags & FLAG_TTY_MODIFIED)) |
63 |
return 0; |
64 |
|
65 |
if (tcsetattr(fileno(stdin), TCSAFLUSH, &otio)) { |
66 |
error("setting term attributes; %s\n", strerror(errno)); |
67 |
return ERROR_TERMIO; |
68 |
} |
69 |
flags &= ~(FLAG_TTY_MODIFIED); |
70 |
|
71 |
return 0; |
72 |
} |