ttcompat.c revision 1.6 1 /*
2 * Copyright (c) 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * ttcompat.c -- convert sgtty flags to termios
37 * originally from /sys/kern/tty_compat.c
38 */
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42
43 #include <unistd.h>
44 #include <sys/ioctl_compat.h>
45 #include <termios.h>
46 #include <syslog.h>
47 #include <fcntl.h>
48 #include <dirent.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include "extern.h"
54
55 /* Macros to clear/set/test flags. */
56 #define SET(t, f) (t) |= (f)
57 #define CLR(t, f) (t) &= ~(f)
58 #define ISSET(t, f) ((t) & (f))
59
60 static int
61 sttygetoflags(tp)
62 struct termios *tp;
63 {
64 register tcflag_t iflag = tp->t_iflag;
65 register tcflag_t lflag = tp->t_lflag;
66 register tcflag_t oflag = tp->t_oflag;
67 register tcflag_t cflag = tp->t_cflag;
68 register int flags = 0;
69
70 if (ISSET(cflag, PARENB)) {
71 if (ISSET(iflag, INPCK)) {
72 if (ISSET(cflag, PARODD))
73 SET(flags, ODDP);
74 else
75 SET(flags, EVENP);
76 } else
77 SET(flags, EVENP|ODDP);
78 }
79 if (ISSET(cflag, CSIZE) == CS8) {
80 if (!ISSET(iflag, ISTRIP))
81 SET(flags, PASS8);
82 if (!ISSET(oflag, OPOST))
83 SET(flags, LITOUT);
84 }
85
86 if (!ISSET(lflag, ICANON)) {
87 /* fudge */
88 if (ISSET(iflag, IXON) || ISSET(lflag, ISIG|IEXTEN) ||
89 ISSET(cflag, PARENB))
90 SET(flags, CBREAK);
91 else
92 SET(flags, RAW);
93 }
94
95 return (flags);
96 }
97
98 static void
99 sttysetoflags(tp, flags)
100 struct termios *tp;
101 int flags;
102 {
103 register tcflag_t iflag = tp->c_iflag;
104 register tcflag_t oflag = tp->c_oflag;
105 register tcflag_t lflag = tp->c_lflag;
106 register tcflag_t cflag = tp->c_cflag;
107
108 switch (ISSET(flags, ANYP)) {
109 case EVENP:
110 SET(iflag, INPCK);
111 CLR(cflag, PARODD);
112 break;
113 case ODDP:
114 SET(iflag, INPCK);
115 SET(cflag, PARODD);
116 break;
117 default:
118 CLR(iflag, INPCK);
119 break;
120 }
121
122 if (ISSET(flags, RAW|LITOUT|PASS8)) {
123 CLR(cflag, CSIZE|PARENB);
124 SET(cflag, CS8);
125 if (!ISSET(flags, RAW|PASS8))
126 SET(iflag, ISTRIP);
127 else
128 CLR(iflag, ISTRIP);
129 if (!ISSET(flags, RAW|LITOUT))
130 SET(oflag, OPOST);
131 else
132 CLR(oflag, OPOST);
133 } else {
134 CLR(cflag, CSIZE);
135 SET(cflag, CS7|PARENB);
136 SET(iflag, ISTRIP);
137 SET(oflag, OPOST);
138 }
139
140 tp->c_iflag = iflag;
141 tp->c_oflag = oflag;
142 tp->c_lflag = lflag;
143 tp->c_cflag = cflag;
144 }
145
146 void
147 sttyclearflags(tp, flags)
148 struct termios *tp;
149 int flags;
150 {
151 register tcflag_t iflag = tp->c_iflag;
152 register tcflag_t oflag = tp->c_oflag;
153 register tcflag_t lflag = tp->c_lflag;
154 register tcflag_t cflag = tp->c_cflag;
155 register int oflags = sttygetoflags(tp) & ~flags;
156
157 if (ISSET(flags, TANDEM))
158 CLR(iflag, IXOFF);
159 if (ISSET(flags, ECHO))
160 CLR(lflag, ECHO);
161 if (ISSET(flags, CRMOD)) {
162 CLR(iflag, ICRNL);
163 CLR(oflag, ONLCR);
164 }
165 if (ISSET(flags, XTABS))
166 CLR(oflag, OXTABS);
167
168
169 if (ISSET(flags, RAW)) {
170 SET(iflag, BRKINT|IXON|IMAXBEL);
171 SET(lflag, ISIG|IEXTEN);
172 if (ISSET(flags, CBREAK))
173 CLR(lflag, ICANON);
174 else
175 SET(lflag, ICANON);
176 }
177
178 tp->c_iflag = iflag;
179 tp->c_oflag = oflag;
180 tp->c_lflag = lflag;
181 tp->c_cflag = cflag;
182
183 sttysetoflags(tp, oflags);
184 }
185
186 void
187 sttysetflags(tp, flags)
188 struct termios *tp;
189 int flags;
190 {
191 register tcflag_t iflag = tp->c_iflag;
192 register tcflag_t oflag = tp->c_oflag;
193 register tcflag_t lflag = tp->c_lflag;
194 register tcflag_t cflag = tp->c_cflag;
195 register int oflags = sttygetoflags(tp) | flags;
196
197 if (ISSET(flags, TANDEM))
198 SET(iflag, IXOFF);
199 if (ISSET(flags, ECHO))
200 SET(lflag, ECHO);
201 if (ISSET(flags, CRMOD)) {
202 SET(iflag, ICRNL);
203 SET(oflag, ONLCR);
204 }
205 if (ISSET(flags, XTABS))
206 SET(oflag, OXTABS);
207
208 if (ISSET(flags, RAW)) {
209 iflag &= IXOFF;
210 CLR(lflag, ISIG|ICANON|IEXTEN);
211 }
212
213 tp->c_iflag = iflag;
214 tp->c_oflag = oflag;
215 tp->c_lflag = lflag;
216 tp->c_cflag = cflag;
217
218 sttysetoflags(tp, oflags);
219 }
220
221 void
222 sttyclearlflags(tp, flags)
223 struct termios *tp;
224 int flags;
225 {
226 register tcflag_t iflag = tp->c_iflag;
227 register tcflag_t oflag = tp->c_oflag;
228 register tcflag_t lflag = tp->c_lflag;
229 register tcflag_t cflag = tp->c_cflag;
230 register int oflags = sttygetoflags(tp) & ~flags;
231
232 /* Nothing we can do with CRTBS. */
233 if (ISSET(flags, PRTERA))
234 CLR(lflag, ECHOPRT);
235 if (ISSET(flags, CRTERA))
236 CLR(lflag, ECHOE);
237 /* Nothing we can do with TILDE. */
238 if (ISSET(flags, MDMBUF))
239 CLR(cflag, MDMBUF);
240 if (ISSET(flags, NOHANG))
241 SET(cflag, HUPCL);
242 if (ISSET(flags, CRTKIL))
243 CLR(lflag, ECHOKE);
244 if (ISSET(flags, CTLECH))
245 CLR(lflag, ECHOCTL);
246 if (ISSET(flags, DECCTQ))
247 SET(iflag, IXANY);
248 CLR(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
249
250 tp->c_iflag = iflag;
251 tp->c_oflag = oflag;
252 tp->c_lflag = lflag;
253 tp->c_cflag = cflag;
254
255 sttysetoflags(tp, oflags);
256 }
257
258 void
259 sttysetlflags(tp, flags)
260 struct termios *tp;
261 int flags;
262 {
263 register tcflag_t iflag = tp->c_iflag;
264 register tcflag_t oflag = tp->c_oflag;
265 register tcflag_t lflag = tp->c_lflag;
266 register tcflag_t cflag = tp->c_cflag;
267 register int oflags = sttygetoflags(tp) | flags;
268
269 /* Nothing we can do with CRTBS. */
270 if (ISSET(flags, PRTERA))
271 SET(lflag, ECHOPRT);
272 if (ISSET(flags, CRTERA))
273 SET(lflag, ECHOE);
274 /* Nothing we can do with TILDE. */
275 if (ISSET(flags, MDMBUF))
276 SET(cflag, MDMBUF);
277 if (ISSET(flags, NOHANG))
278 CLR(cflag, HUPCL);
279 if (ISSET(flags, CRTKIL))
280 SET(lflag, ECHOKE);
281 if (ISSET(flags, CTLECH))
282 SET(lflag, ECHOCTL);
283 if (ISSET(flags, DECCTQ))
284 CLR(iflag, IXANY);
285 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
286
287 tp->c_iflag = iflag;
288 tp->c_oflag = oflag;
289 tp->c_lflag = lflag;
290 tp->c_cflag = cflag;
291
292 sttysetoflags(tp, oflags);
293 }
294