1 /*
2 * TtySet ()
3 *
4 * - set terminal CBREAK mode.
5 *
6 * TtyReset ()
7 *
8 * - restore terminal mode.
9 *
10 * TtyFlushInput ()
11 * - flush input queue.
12 */
13
14 #if HAVE_TERMIOS_H
15 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
16 # include <sys/termios.h>
17 #else
18 # include <termios.h>
19 #endif
20 # if HAVE_SYS_IOCTL_H
21 # include <sys/ioctl.h>
22 # endif
23 # define HAVE_TERMIO_H 1
24 # define termio termios
25 # ifdef sun
26 # undef TCGETA
27 # undef TCSETA
28 # undef TCSETAW
29 # undef TIOCSLTC
30 # define TCGETA TCGETS
31 # define TCSETA TCSETS
32 # define TCSETAW TCSETSW
33 # endif
34 #else
35 #if HAVE_TERMIO_H
36 # include <termio.h>
37 #else
38 # include <sgtty.h>
39 #endif
40 #endif
41
42 #if HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #include "scr.h"
46
47 #ifndef TCGETA
48 # define TCGETA TIOCGETA
49 #endif
50 #ifndef TCSETA
51 # define TCSETA TIOCSETA
52 #endif
53 #ifndef TCSETAW
54 # define TCSETAW TIOCSETAW
55 #endif
56 #ifndef OLCUC
57 # define OLCUC 0
58 #endif
59 #ifndef IUCLC
60 # define IUCLC 0
61 #endif
62 #ifndef OCRNL
63 # define OCRNL 0
64 #endif
65 #ifndef XCASE
66 # define XCASE 0
67 #endif
68
69 #define CHANNEL 2 /* output file descriptor */
70
71 #if HAVE_TERMIO_H
72 static struct termio oldtio, newtio;
73 #else
74 static struct sgttyb tty;
75 static ttyflags;
76 #ifdef TIOCSETC
77 static struct tchars oldtchars, newtchars;
78 #endif
79 #endif
80
81 #ifdef TIOCSLTC
82 static struct ltchars oldchars, newchars;
83 #endif
84
85 #ifdef TAB3
86 # define OXTABS TAB3
87 #endif
88
89 int TtyUpperCase = 0;
90
91 #define NOCHAR 0
92
93 #if HAVE_TCGETATTR
94 # define GET(addr) tcgetattr (CHANNEL, addr)
95 # define SET(addr) tcsetattr (CHANNEL, TCSADRAIN, addr)
96 #else
97 # define GET(addr) ioctl (CHANNEL, TCGETA, addr)
98 # define SET(addr) ioctl (CHANNEL, TCSETAW, addr)
99 #endif
100
TtySet()101 void TtySet ()
102 {
103 #if HAVE_TERMIO_H
104 if (GET (&oldtio) < 0)
105 return;
106 if (oldtio.c_oflag & OLCUC)
107 TtyUpperCase = 1; /* uppercase on output */
108 newtio = oldtio;
109 newtio.c_iflag &= ~(INLCR | ICRNL | IGNCR | ISTRIP | IUCLC);
110 newtio.c_oflag &= ~(OLCUC | OCRNL | OXTABS);
111 newtio.c_lflag &= ~(ECHO | ICANON | XCASE);
112 newtio.c_cc [VMIN] = 1; /* break input after each character */
113 newtio.c_cc [VTIME] = 1; /* timeout is 100 msecs */
114 newtio.c_cc [VINTR] = NOCHAR;
115 newtio.c_cc [VQUIT] = NOCHAR;
116 #ifdef VSWTCH
117 newtio.c_cc [VSWTCH] = NOCHAR;
118 #endif
119 #ifdef VDSUSP
120 newtio.c_cc [VDSUSP] = NOCHAR;
121 #endif
122 #ifdef VLNEXT
123 newtio.c_cc [VLNEXT] = NOCHAR;
124 #endif
125 #ifdef VDISCARD
126 newtio.c_cc [VDISCARD] = NOCHAR;
127 #endif
128 SET (&newtio);
129 #else
130 if (gtty (CHANNEL, &tty) < 0)
131 return;
132 if (tty.sg_flags & LCASE)
133 TtyUpperCase = 1; /* uppercase on output */
134 ttyflags = tty.sg_flags;
135 tty.sg_flags &= ~(XTABS | ECHO | CRMOD | LCASE);
136 # ifdef CBREAK
137 tty.sg_flags |= CBREAK;
138 # endif
139 stty (CHANNEL, &tty);
140 # ifdef TIOCSETC
141 ioctl (CHANNEL, TIOCGETC, (char *) &oldtchars);
142 newtchars = oldtchars;
143 newtchars.t_intrc = NOCHAR;
144 newtchars.t_quitc = NOCHAR;
145 newtchars.t_eofc = NOCHAR;
146 newtchars.t_brkc = NOCHAR;
147 ioctl (CHANNEL, TIOCSETC, (char *) &newtchars);
148 # endif
149 #endif /* HAVE_TERMIO_H */
150 #ifdef TIOCSLTC
151 ioctl (CHANNEL, TIOCGLTC, (char *) &oldchars);
152 newchars = oldchars;
153 newchars.t_lnextc = NOCHAR;
154 newchars.t_rprntc = NOCHAR;
155 newchars.t_dsuspc = NOCHAR;
156 newchars.t_flushc = NOCHAR;
157 ioctl (CHANNEL, TIOCSLTC, (char *) &newchars);
158 #endif
159 }
160
TtyReset()161 void TtyReset ()
162 {
163 #if HAVE_TERMIO_H
164 SET (&oldtio);
165 #else
166 tty.sg_flags = ttyflags;
167 stty (CHANNEL, &tty);
168 # ifdef TIOCSETC
169 ioctl (CHANNEL, TIOCSETC, (char *) &oldtchars);
170 # endif
171 #endif
172 #ifdef TIOCSLTC
173 ioctl (CHANNEL, TIOCSLTC, (char *) &oldchars);
174 #endif
175 }
176
TtyFlushInput()177 void TtyFlushInput ()
178 {
179 #ifdef TCFLSH
180 ioctl (CHANNEL, TCFLSH, 0);
181 #else
182 # ifdef TIOCFLUSH
183 int p = 1;
184
185 ioctl (CHANNEL, TIOCFLUSH, (char *) &p);
186 # endif
187 #endif
188 }
189