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