/[imapfilter]/imapfilter/tty.c
ViewVC logotype

Annotation of /imapfilter/tty.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Tue Dec 17 19:17:04 2002 UTC (21 years, 3 months ago) by lefcha
Branch: MAIN
Changes since 1.5: +11 -4 lines
File MIME type: text/plain
Corrected tty functions in case the file descriptor is not a terminal.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.6 #include <unistd.h>
3 lefcha 1.1 #include <string.h>
4     #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     int tty_store(void)
20     {
21 lefcha 1.6 if (isatty(fileno(stdin)))
22     flags |= FLAG_TTY;
23    
24     if (tcgetattr(fileno(stdin), &otio)) {
25 lefcha 1.1 error("imapfilter: getting term attributes; %s\n", strerror(errno));
26     return ERROR_TERMIO;
27     }
28     return 0;
29     }
30    
31    
32     /*
33     * Disable echo.
34     */
35     int tty_disable_echo(void)
36     {
37 lefcha 1.6 if (!(flags & FLAG_TTY))
38     return 0;
39    
40 lefcha 1.1 ntio = otio;
41     ntio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
42 lefcha 1.4
43     flags |= FLAG_TTY_MODIFIED;
44 lefcha 1.2
45 lefcha 1.6 if (tcsetattr(fileno(stdin), TCSAFLUSH, &ntio)) {
46 lefcha 1.1 error("imapfilter: setting term attributes; %s\n", strerror(errno));
47     return ERROR_TERMIO;
48     }
49     return 0;
50     }
51    
52    
53     /*
54     * Restore original term attributes.
55     */
56     int tty_restore(void)
57     {
58 lefcha 1.6 if (!(flags & FLAG_TTY) || !(flags & FLAG_TTY_MODIFIED))
59 lefcha 1.3 return 0;
60 lefcha 1.5
61 lefcha 1.6 if (tcsetattr(fileno(stdin), TCSAFLUSH, &otio)) {
62 lefcha 1.1 error("imapfilter: setting term attributes; %s\n", strerror(errno));
63     return ERROR_TERMIO;
64     }
65 lefcha 1.5 flags &= ~(FLAG_TTY_MODIFIED);
66 lefcha 1.4
67 lefcha 1.1 return 0;
68     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26