tty_43.c revision 1.1 1 /* $NetBSD: tty_43.c,v 1.1 1996/02/02 18:47:57 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tty_compat.c 8.1 (Berkeley) 6/10/93
36 */
37
38 /*
39 * mapping routines for old line discipline (yuck)
40 */
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ioctl.h>
44 #include <sys/proc.h>
45 #include <sys/tty.h>
46 #include <sys/termios.h>
47 #include <sys/file.h>
48 #include <sys/conf.h>
49 #include <sys/kernel.h>
50 #include <sys/syslog.h>
51
52 int ttydebug = 0;
53
54 static struct speedtab compatspeeds[] = {
55 #define MAX_SPEED 17
56 { 115200, 17 },
57 { 57600, 16 },
58 { 38400, 15 },
59 { 19200, 14 },
60 { 9600, 13 },
61 { 4800, 12 },
62 { 2400, 11 },
63 { 1800, 10 },
64 { 1200, 9 },
65 { 600, 8 },
66 { 300, 7 },
67 { 200, 6 },
68 { 150, 5 },
69 { 134, 4 },
70 { 110, 3 },
71 { 75, 2 },
72 { 50, 1 },
73 { 0, 0 },
74 { -1, -1 },
75 };
76 static int compatspcodes[] = {
77 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
78 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200
79 };
80
81 /* Macros to clear/set/test flags. */
82 #define SET(t, f) (t) |= (f)
83 #define CLR(t, f) (t) &= ~(f)
84 #define ISSET(t, f) ((t) & (f))
85
86 /*ARGSUSED*/
87 ttcompat(tp, com, data, flag, p)
88 register struct tty *tp;
89 u_long com;
90 caddr_t data;
91 int flag;
92 struct proc *p;
93 {
94
95 switch (com) {
96 case TIOCGETP: {
97 register struct sgttyb *sg = (struct sgttyb *)data;
98 register u_char *cc = tp->t_cc;
99 register int speed;
100
101 speed = ttspeedtab(tp->t_ospeed, compatspeeds);
102 sg->sg_ospeed = (speed == -1) ? MAX_SPEED : speed;
103 if (tp->t_ispeed == 0)
104 sg->sg_ispeed = sg->sg_ospeed;
105 else {
106 speed = ttspeedtab(tp->t_ispeed, compatspeeds);
107 sg->sg_ispeed = (speed == -1) ? MAX_SPEED : speed;
108 }
109 sg->sg_erase = cc[VERASE];
110 sg->sg_kill = cc[VKILL];
111 sg->sg_flags = ttcompatgetflags(tp);
112 break;
113 }
114
115 case TIOCSETP:
116 case TIOCSETN: {
117 register struct sgttyb *sg = (struct sgttyb *)data;
118 struct termios term;
119 int speed;
120
121 term = tp->t_termios;
122 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0)
123 term.c_ispeed = speed;
124 else
125 term.c_ispeed = compatspcodes[speed];
126 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0)
127 term.c_ospeed = speed;
128 else
129 term.c_ospeed = compatspcodes[speed];
130 term.c_cc[VERASE] = sg->sg_erase;
131 term.c_cc[VKILL] = sg->sg_kill;
132 tp->t_flags = (ttcompatgetflags(tp)&0xffff0000) | (sg->sg_flags&0xffff);
133 ttcompatsetflags(tp, &term);
134 return (ttioctl(tp, com == TIOCSETP ? TIOCSETAF : TIOCSETA,
135 (caddr_t)&term, flag, p));
136 }
137
138 case TIOCGETC: {
139 struct tchars *tc = (struct tchars *)data;
140 register u_char *cc = tp->t_cc;
141
142 tc->t_intrc = cc[VINTR];
143 tc->t_quitc = cc[VQUIT];
144 tc->t_startc = cc[VSTART];
145 tc->t_stopc = cc[VSTOP];
146 tc->t_eofc = cc[VEOF];
147 tc->t_brkc = cc[VEOL];
148 break;
149 }
150 case TIOCSETC: {
151 struct tchars *tc = (struct tchars *)data;
152 register u_char *cc = tp->t_cc;
153
154 cc[VINTR] = tc->t_intrc;
155 cc[VQUIT] = tc->t_quitc;
156 cc[VSTART] = tc->t_startc;
157 cc[VSTOP] = tc->t_stopc;
158 cc[VEOF] = tc->t_eofc;
159 cc[VEOL] = tc->t_brkc;
160 if (tc->t_brkc == -1)
161 cc[VEOL2] = _POSIX_VDISABLE;
162 break;
163 }
164 case TIOCSLTC: {
165 struct ltchars *ltc = (struct ltchars *)data;
166 register u_char *cc = tp->t_cc;
167
168 cc[VSUSP] = ltc->t_suspc;
169 cc[VDSUSP] = ltc->t_dsuspc;
170 cc[VREPRINT] = ltc->t_rprntc;
171 cc[VDISCARD] = ltc->t_flushc;
172 cc[VWERASE] = ltc->t_werasc;
173 cc[VLNEXT] = ltc->t_lnextc;
174 break;
175 }
176 case TIOCGLTC: {
177 struct ltchars *ltc = (struct ltchars *)data;
178 register u_char *cc = tp->t_cc;
179
180 ltc->t_suspc = cc[VSUSP];
181 ltc->t_dsuspc = cc[VDSUSP];
182 ltc->t_rprntc = cc[VREPRINT];
183 ltc->t_flushc = cc[VDISCARD];
184 ltc->t_werasc = cc[VWERASE];
185 ltc->t_lnextc = cc[VLNEXT];
186 break;
187 }
188 case TIOCLBIS:
189 case TIOCLBIC:
190 case TIOCLSET: {
191 struct termios term;
192 int flags;
193
194 term = tp->t_termios;
195 flags = ttcompatgetflags(tp);
196 switch (com) {
197 case TIOCLSET:
198 tp->t_flags = (flags&0xffff) | (*(int *)data<<16);
199 break;
200 case TIOCLBIS:
201 tp->t_flags = flags | (*(int *)data<<16);
202 break;
203 case TIOCLBIC:
204 tp->t_flags = flags & ~(*(int *)data<<16);
205 break;
206 }
207 ttcompatsetlflags(tp, &term);
208 return (ttioctl(tp, TIOCSETA, (caddr_t)&term, flag, p));
209 }
210 case TIOCLGET:
211 *(int *)data = ttcompatgetflags(tp)>>16;
212 if (ttydebug)
213 printf("CLGET: returning %x\n", *(int *)data);
214 break;
215
216 case OTIOCGETD:
217 *(int *)data = tp->t_line ? tp->t_line : 2;
218 break;
219
220 case OTIOCSETD: {
221 int ldisczero = 0;
222
223 return (ttioctl(tp, TIOCSETD,
224 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag,
225 p));
226 }
227
228 case OTIOCCONS:
229 *(int *)data = 1;
230 return (ttioctl(tp, TIOCCONS, data, flag, p));
231
232 case TIOCHPCL:
233 SET(tp->t_cflag, HUPCL);
234 break;
235
236 case TIOCGSID:
237 if (tp->t_session == NULL)
238 return ENOTTY;
239
240 if (tp->t_session->s_leader == NULL)
241 return ENOTTY;
242
243 *(int *) data = tp->t_session->s_leader->p_pid;
244 break;
245
246 default:
247 return (-1);
248 }
249 return (0);
250 }
251
252 int
253 ttcompatgetflags(tp)
254 register struct tty *tp;
255 {
256 register tcflag_t iflag = tp->t_iflag;
257 register tcflag_t lflag = tp->t_lflag;
258 register tcflag_t oflag = tp->t_oflag;
259 register tcflag_t cflag = tp->t_cflag;
260 register int flags = 0;
261
262 if (ISSET(iflag, IXOFF))
263 SET(flags, TANDEM);
264 if (ISSET(iflag, ICRNL) || ISSET(oflag, ONLCR))
265 SET(flags, CRMOD);
266 if (ISSET(cflag, PARENB)) {
267 if (ISSET(iflag, INPCK)) {
268 if (ISSET(cflag, PARODD))
269 SET(flags, ODDP);
270 else
271 SET(flags, EVENP);
272 } else
273 SET(flags, ANYP);
274 }
275
276 if (!ISSET(lflag, ICANON)) {
277 /* fudge */
278 if (ISSET(iflag, IXON) || ISSET(lflag, ISIG|IEXTEN) ||
279 ISSET(cflag, PARENB))
280 SET(flags, CBREAK);
281 else
282 SET(flags, RAW);
283 }
284
285 if (ISSET(flags, RAW))
286 SET(flags, ISSET(tp->t_flags, LITOUT|PASS8));
287 else if (ISSET(cflag, CSIZE) == CS8) {
288 if (!ISSET(oflag, OPOST))
289 SET(flags, LITOUT);
290 if (!ISSET(iflag, ISTRIP))
291 SET(flags, PASS8);
292 }
293
294 if (ISSET(cflag, MDMBUF))
295 SET(flags, MDMBUF);
296 if (!ISSET(cflag, HUPCL))
297 SET(flags, NOHANG);
298 if (ISSET(oflag, OXTABS))
299 SET(flags, XTABS);
300 if (ISSET(lflag, ECHOE))
301 SET(flags, CRTERA|CRTBS);
302 if (ISSET(lflag, ECHOKE))
303 SET(flags, CRTKIL|CRTBS);
304 if (ISSET(lflag, ECHOPRT))
305 SET(flags, PRTERA);
306 if (ISSET(lflag, ECHOCTL))
307 SET(flags, CTLECH);
308 if (!ISSET(iflag, IXANY))
309 SET(flags, DECCTQ);
310 SET(flags, ISSET(lflag, ECHO|TOSTOP|FLUSHO|PENDIN|NOFLSH));
311 if (ttydebug)
312 printf("getflags: %x\n", flags);
313 return (flags);
314 }
315
316 ttcompatsetflags(tp, t)
317 register struct tty *tp;
318 register struct termios *t;
319 {
320 register int flags = tp->t_flags;
321 register tcflag_t iflag = t->c_iflag;
322 register tcflag_t oflag = t->c_oflag;
323 register tcflag_t lflag = t->c_lflag;
324 register tcflag_t cflag = t->c_cflag;
325
326 if (ISSET(flags, TANDEM))
327 SET(iflag, IXOFF);
328 else
329 CLR(iflag, IXOFF);
330 if (ISSET(flags, ECHO))
331 SET(lflag, ECHO);
332 else
333 CLR(lflag, ECHO);
334 if (ISSET(flags, CRMOD)) {
335 SET(iflag, ICRNL);
336 SET(oflag, ONLCR);
337 } else {
338 CLR(iflag, ICRNL);
339 CLR(oflag, ONLCR);
340 }
341 if (ISSET(flags, XTABS))
342 SET(oflag, OXTABS);
343 else
344 CLR(oflag, OXTABS);
345
346
347 if (ISSET(flags, RAW)) {
348 iflag &= IXOFF;
349 CLR(lflag, ISIG|ICANON|IEXTEN);
350 CLR(cflag, PARENB);
351 } else {
352 SET(iflag, BRKINT|IXON|IMAXBEL);
353 SET(lflag, ISIG|IEXTEN);
354 if (ISSET(flags, CBREAK))
355 CLR(lflag, ICANON);
356 else
357 SET(lflag, ICANON);
358 switch (ISSET(flags, ANYP)) {
359 case 0:
360 CLR(cflag, PARENB);
361 break;
362 case ANYP:
363 SET(cflag, PARENB);
364 CLR(iflag, INPCK);
365 break;
366 case EVENP:
367 SET(cflag, PARENB);
368 SET(iflag, INPCK);
369 CLR(cflag, PARODD);
370 break;
371 case ODDP:
372 SET(cflag, PARENB);
373 SET(iflag, INPCK);
374 SET(cflag, PARODD);
375 break;
376 }
377 }
378
379 if (ISSET(flags, RAW|LITOUT|PASS8)) {
380 CLR(cflag, CSIZE);
381 SET(cflag, CS8);
382 if (!ISSET(flags, RAW|PASS8))
383 SET(iflag, ISTRIP);
384 else
385 CLR(iflag, ISTRIP);
386 if (!ISSET(flags, RAW|LITOUT))
387 SET(oflag, OPOST);
388 else
389 CLR(oflag, OPOST);
390 } else {
391 CLR(cflag, CSIZE);
392 SET(cflag, CS7);
393 SET(iflag, ISTRIP);
394 SET(oflag, OPOST);
395 }
396
397 t->c_iflag = iflag;
398 t->c_oflag = oflag;
399 t->c_lflag = lflag;
400 t->c_cflag = cflag;
401 }
402
403 ttcompatsetlflags(tp, t)
404 register struct tty *tp;
405 register struct termios *t;
406 {
407 register int flags = tp->t_flags;
408 register tcflag_t iflag = t->c_iflag;
409 register tcflag_t oflag = t->c_oflag;
410 register tcflag_t lflag = t->c_lflag;
411 register tcflag_t cflag = t->c_cflag;
412
413 /* Nothing we can do with CRTBS. */
414 if (ISSET(flags, PRTERA))
415 SET(lflag, ECHOPRT);
416 else
417 CLR(lflag, ECHOPRT);
418 if (ISSET(flags, CRTERA))
419 SET(lflag, ECHOE);
420 else
421 CLR(lflag, ECHOE);
422 /* Nothing we can do with TILDE. */
423 if (ISSET(flags, MDMBUF))
424 SET(cflag, MDMBUF);
425 else
426 CLR(cflag, MDMBUF);
427 if (ISSET(flags, NOHANG))
428 CLR(cflag, HUPCL);
429 else
430 SET(cflag, HUPCL);
431 if (ISSET(flags, CRTKIL))
432 SET(lflag, ECHOKE);
433 else
434 CLR(lflag, ECHOKE);
435 if (ISSET(flags, CTLECH))
436 SET(lflag, ECHOCTL);
437 else
438 CLR(lflag, ECHOCTL);
439 if (!ISSET(flags, DECCTQ))
440 SET(iflag, IXANY);
441 else
442 CLR(iflag, IXANY);
443 CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
444 SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
445
446 if (ISSET(flags, RAW|LITOUT|PASS8)) {
447 CLR(cflag, CSIZE);
448 SET(cflag, CS8);
449 if (!ISSET(flags, RAW|PASS8))
450 SET(iflag, ISTRIP);
451 else
452 CLR(iflag, ISTRIP);
453 if (!ISSET(flags, RAW|LITOUT))
454 SET(oflag, OPOST);
455 else
456 CLR(oflag, OPOST);
457 } else {
458 CLR(cflag, CSIZE);
459 SET(cflag, CS7);
460 SET(iflag, ISTRIP);
461 SET(oflag, OPOST);
462 }
463
464 t->c_iflag = iflag;
465 t->c_oflag = oflag;
466 t->c_lflag = lflag;
467 t->c_cflag = cflag;
468 }
469