]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_compat.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / kern / tty_compat.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2019 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
29/*-
30 * Copyright (c) 1982, 1986, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)tty_compat.c 8.1 (Berkeley) 6/10/93
62 */
63
64/*
b0d623f7
A
65 * Compatibility routines for BSD 4.3 tty ioctl() commands
66 *
67 * The only function externalized from this file is ttcompat() and it is
68 * externalized as private extern to prevent exporting of the symbol when
69 * KEXTs link against the kernel.
70 *
71 * Locks: All functions in this file assume that the tty_lock()
72 * is held on the tty structure before these functions are
73 * called.
1c79356b
A
74 */
75
76#include <sys/param.h>
77#include <sys/systm.h>
78#include <sys/ioctl.h>
91447636 79#include <sys/proc_internal.h>
1c79356b
A
80#include <sys/tty.h>
81#include <sys/termios.h>
91447636 82#include <sys/file_internal.h>
1c79356b
A
83#include <sys/conf.h>
84#include <sys/kernel.h>
85#include <sys/sysctl.h>
86#include <sys/syslog.h>
87
91447636 88static int ttcompatgetflags(struct tty *tp);
0a7de745 89static void ttcompatsetflags(struct tty *tp, struct termios *t);
91447636
A
90static void ttcompatsetlflags(struct tty *tp, struct termios *t);
91static int ttcompatspeedtab(int speed, struct speedtab *table);
1c79356b 92
91447636
A
93/*
94 * These two tables encode baud rate to speed code and speed code to
95 * baud rate information. They are a mapping between the <sys/termios.h>
96 * baud rate constants and the <sys/ttydev.h> baud rate constants. We
97 * cannot use those constants directly here because they occupy the same
98 * name space.
99 */
1c79356b 100static struct speedtab compatspeeds[] = {
0a7de745 101#define MAX_SPEED 17
cb323159
A
102 { .sp_speed = 115200, .sp_code = 17 },
103 { .sp_speed = 57600, .sp_code = 16 },
104 { .sp_speed = 38400, .sp_code = 15 },
105 { .sp_speed = 19200, .sp_code = 14 },
106 { .sp_speed = 9600, .sp_code = 13 },
107 { .sp_speed = 4800, .sp_code = 12 },
108 { .sp_speed = 2400, .sp_code = 11 },
109 { .sp_speed = 1800, .sp_code = 10 },
110 { .sp_speed = 1200, .sp_code = 9 },
111 { .sp_speed = 600, .sp_code = 8 },
112 { .sp_speed = 300, .sp_code = 7 },
113 { .sp_speed = 200, .sp_code = 6 },
114 { .sp_speed = 150, .sp_code = 5 },
115 { .sp_speed = 134, .sp_code = 4 },
116 { .sp_speed = 110, .sp_code = 3 },
117 { .sp_speed = 75, .sp_code = 2 },
118 { .sp_speed = 50, .sp_code = 1 },
119 { .sp_speed = 0, .sp_code = 0 },
120 { .sp_speed = -1, .sp_code = -1 },
1c79356b
A
121};
122static int compatspcodes[] = {
123 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
124 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
125};
126
91447636
A
127/*
128 * ttcompatspeedtab
129 *
130 * Description: Given a baud rate value as an integer, and a speed table,
131 * convert the baud rate to a speed code, according to the
132 * contents of the table. This effectively changes termios.h
133 * baud rate values into ttydev.h baud rate codes.
134 *
135 * Parameters: int speed Baud rate, as an integer
136 * struct speedtab *table Baud rate table to speed code table
137 *
138 * Returns: 1 B50 speed code; returned if we can
139 * not find an answer in the table.
140 * 0 If a 0 was requested in order to
141 * trigger a hangup (250ms of line
142 * silence, per Bell 103C standard).
143 * * A speed code matching the requested
144 * baud rate (potentially rounded down,
145 * if there is no exact match).
146 *
147 * Notes: This function is used for TIOCGETP, TIOCSETP, and TIOCSETN.
148 */
1c79356b 149static int
91447636 150ttcompatspeedtab(int speed, struct speedtab *table)
1c79356b 151{
0a7de745
A
152 if (speed == 0) {
153 return 0; /* hangup */
154 }
155 for (; table->sp_speed > 0; table++) {
156 if (table->sp_speed <= speed) { /* nearest one, rounded down */
157 return table->sp_code;
158 }
159 }
160 return 1; /* 50, min and not hangup */
1c79356b
A
161}
162
b0d623f7 163
91447636
A
164/*
165 * ttsetcompat
166 *
167 * Description: Convert backward compatability set command arguments as
168 * follows:
169 *
170 * TIOCSETP -> TIOSETAF
171 * TIOCSETN -> TIOCSETA
172 * TIOCSETC -> TIOCSETA
173 * TIOCSLTC -> TIOCSETA
174 * TIOCLBIS -> TIOCSETA
175 * TIOCLBIC -> TIOCSETA
176 * TIOCLSET -> TIOCSETA
177 *
178 * The converted command argument and potentially modified 'term'
b0d623f7
A
179 * argument are returned to ttcompat(), which will then call
180 * ttioctl_locked(), if this function returns successfully.
91447636
A
181 *
182 * Parameters struct tty *tp The tty on which the operation is
183 * being performed.
184 * u_long *com A pointer to the terminal input/output
185 * command being requested; its contents
186 * will be modified per the table above,
187 * on a non-error return.
188 * caddr_t data Command specific parameter data; this
189 * data is read but not modified.
190 * struct termios *term A local stack termios structure from
191 * ttcompat(), whose contents are to be
192 * modified based on *com and *data.
193 *
194 * Returns: EINVAL An input speed or output speed is
195 * outside the allowable range for a
196 * TIOCSETP or TIOCSETN command.
197 * 0 All other cases return 0.
198 *
199 * Notes: This function may modify the contents of the tp->t_flags
200 * field in a successful call to TIOCSETP, TIOCSETN, TIOCLBIS,
201 * TIOCLBIC, or TIOCLSET.
202 *
203 * All other tp fields will remain unmodifed, since the struct
b0d623f7
A
204 * termios is a local stack copy from ttcompat(), and not the
205 * real thing. A subsequent call to ttioctl_locked() in
206 * ttcompat(), however, may result in subsequent changes.
207 *
208 * WARNING: This compatibility code is not 6/432 clean; it will only
209 * work for 32 bit processes on 32 bit kernels or 64 bit
210 * processes on 64 bit kernels. We are not addressing this
211 * due to <rdar://6904053>.
91447636 212 */
b0d623f7 213static int
91447636 214ttsetcompat(struct tty *tp, u_long *com, caddr_t data, struct termios *term)
1c79356b
A
215{
216 switch (*com) {
217 case TIOCSETP:
0a7de745
A
218 /*
219 * Wait for all characters queued for output to drain, then
220 * Discard all characters queued for input, and then set
221 * the input and output speeds and device flags, per the
222 * contents of the struct sgttyb that 'data' points to.
223 */
91447636
A
224 case TIOCSETN:
225 /*
226 * Same as TIOCSETP, but the output is not drained, and any
227 * pending input is not discarded.
228 */
0a7de745 229 {
39037602 230 struct sgttyb *sg = (struct sgttyb *)data;
1c79356b
A
231 int speed;
232
0a7de745
A
233 if ((speed = sg->sg_ispeed) > MAX_SPEED || speed < 0) {
234 return EINVAL;
235 } else if (speed != ttcompatspeedtab(tp->t_ispeed, compatspeeds)) {
1c79356b 236 term->c_ispeed = compatspcodes[speed];
0a7de745 237 } else {
1c79356b 238 term->c_ispeed = tp->t_ispeed;
0a7de745
A
239 }
240 if ((speed = sg->sg_ospeed) > MAX_SPEED || speed < 0) {
241 return EINVAL;
242 } else if (speed != ttcompatspeedtab(tp->t_ospeed, compatspeeds)) {
1c79356b 243 term->c_ospeed = compatspcodes[speed];
0a7de745 244 } else {
1c79356b 245 term->c_ospeed = tp->t_ospeed;
0a7de745 246 }
1c79356b
A
247 term->c_cc[VERASE] = sg->sg_erase;
248 term->c_cc[VKILL] = sg->sg_kill;
0a7de745 249 tp->t_flags = (tp->t_flags & 0xffff0000) | (sg->sg_flags & 0xffff);
1c79356b
A
250 ttcompatsetflags(tp, term);
251 *com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
252 break;
253 }
91447636
A
254 case TIOCSETC:
255 /*
256 * Set the terminal control characters per the contents of
257 * the struct tchars that 'data' points to.
258 */
0a7de745 259 {
1c79356b 260 struct tchars *tc = (struct tchars *)data;
39037602 261 cc_t *cc;
1c79356b
A
262
263 cc = term->c_cc;
264 cc[VINTR] = tc->t_intrc;
265 cc[VQUIT] = tc->t_quitc;
266 cc[VSTART] = tc->t_startc;
267 cc[VSTOP] = tc->t_stopc;
268 cc[VEOF] = tc->t_eofc;
269 cc[VEOL] = tc->t_brkc;
0a7de745 270 if (tc->t_brkc == -1) {
1c79356b 271 cc[VEOL2] = _POSIX_VDISABLE;
0a7de745 272 }
1c79356b
A
273 *com = TIOCSETA;
274 break;
275 }
91447636
A
276 case TIOCSLTC:
277 /*
278 * Set the terminal control characters per the contents of
279 * the struct ltchars that 'data' points to.
280 */
281 {
1c79356b 282 struct ltchars *ltc = (struct ltchars *)data;
39037602 283 cc_t *cc;
1c79356b
A
284
285 cc = term->c_cc;
286 cc[VSUSP] = ltc->t_suspc;
287 cc[VDSUSP] = ltc->t_dsuspc;
288 cc[VREPRINT] = ltc->t_rprntc;
289 cc[VDISCARD] = ltc->t_flushc;
290 cc[VWERASE] = ltc->t_werasc;
291 cc[VLNEXT] = ltc->t_lnextc;
292 *com = TIOCSETA;
293 break;
294 }
295 case TIOCLBIS:
0a7de745
A
296 /*
297 * Set the bits in the terminal state local flags word
298 * (16 bits) for the terminal to the current bits OR
299 * those in the 16 bit value pointed to by 'data'.
300 */
1c79356b 301 case TIOCLBIC:
0a7de745
A
302 /*
303 * Clear the bits in the terminal state local flags word
304 * for the terminal to the current bits AND those bits NOT
305 * in the 16 bit value pointed to by 'data'.
306 */
1c79356b 307 case TIOCLSET:
91447636
A
308 /*
309 * Set the terminal state local flags word to exactly those
310 * bits that correspond to the 16 bit value pointed to by
311 * 'data'.
312 */
0a7de745
A
313 if (*com == TIOCLSET) {
314 tp->t_flags = (tp->t_flags & 0xffff) | *(int *)data << 16;
315 } else {
1c79356b 316 tp->t_flags =
0a7de745
A
317 (ttcompatgetflags(tp) & 0xffff0000) | (tp->t_flags & 0xffff);
318 if (*com == TIOCLBIS) {
319 tp->t_flags |= *(int *)data << 16;
320 } else {
321 tp->t_flags &= ~(*(int *)data << 16);
322 }
1c79356b
A
323 }
324 ttcompatsetlflags(tp, term);
325 *com = TIOCSETA;
326 break;
327 }
328 return 0;
329}
330
91447636
A
331/*
332 * ttcompat
333 *
334 * Description: For 'set' commands, convert the command and arguments as
b0d623f7
A
335 * necessary, and call ttioctl_locked(), returning the result
336 * as our result; for 'get' commands, obtain the requested data
91447636
A
337 * from the appropriate source, and return it in the expected
338 * format. If the command is not recognized, return EINVAL.
339 *
340 * Parameters struct tty *tp The tty on which the operation is
341 * being performed.
342 * u_long com The terminal input/output command
343 * being requested.
344 * caddr_t data The pointer to the user data argument
345 * provided with the command.
346 * int flag The file open flags (e.g. FREAD).
347 * struct proc *p The current process pointer for the
348 * operation.
349 *
350 * Returns: 0 Most 'get' operations can't fail, and
351 * therefore return this.
352 * ENOTTY TIOCGSID may return this when you
353 * attempt to get the session ID for a
354 * terminal with no associated session,
355 * or for which there is a session, but
356 * no session leader.
b0d623f7 357 * ENOTTY If the command cannot be handled at
91447636 358 * this layer, this will be returned.
b0d623f7
A
359 * * Any value returned by ttioctl_locked(),
360 * if a set command is requested.
91447636 361 *
b0d623f7 362 * Notes: The process pointer may be a proxy on whose behalf we are
91447636
A
363 * operating, so it is not safe to simply use current_process()
364 * instead.
365 */
1c79356b 366/*ARGSUSED*/
1c79356b 367__private_extern__ int
91447636 368ttcompat(struct tty *tp, u_long com, caddr_t data, int flag, struct proc *p)
1c79356b
A
369{
370 switch (com) {
371 case TIOCSETP:
372 case TIOCSETN:
373 case TIOCSETC:
374 case TIOCSLTC:
375 case TIOCLBIS:
376 case TIOCLBIC:
91447636
A
377 case TIOCLSET:
378 /*
379 * See ttsetcompat() for a full description of these command
380 * values and their meanings.
381 */
382 {
1c79356b
A
383 struct termios term;
384 int error;
385
386 term = tp->t_termios;
0a7de745 387 if ((error = ttsetcompat(tp, &com, data, &term)) != 0) {
1c79356b 388 return error;
0a7de745 389 }
b0d623f7 390 return ttioctl_locked(tp, com, (caddr_t) &term, flag, p);
1c79356b 391 }
91447636
A
392 case TIOCGETP:
393 /*
394 * Get the current input and output speeds, and device
395 * flags, into the structure pointed to by 'data'.
396 */
0a7de745 397 {
39037602
A
398 struct sgttyb *sg = (struct sgttyb *)data;
399 cc_t *cc = tp->t_cc;
1c79356b
A
400
401 sg->sg_ospeed = ttcompatspeedtab(tp->t_ospeed, compatspeeds);
0a7de745 402 if (tp->t_ispeed == 0) {
1c79356b 403 sg->sg_ispeed = sg->sg_ospeed;
0a7de745 404 } else {
1c79356b 405 sg->sg_ispeed = ttcompatspeedtab(tp->t_ispeed, compatspeeds);
0a7de745 406 }
1c79356b
A
407 sg->sg_erase = cc[VERASE];
408 sg->sg_kill = cc[VKILL];
409 sg->sg_flags = tp->t_flags = ttcompatgetflags(tp);
410 break;
411 }
91447636
A
412 case TIOCGETC:
413 /*
414 * Get the terminal control characters into the struct
415 * tchars that 'data' points to.
416 */
0a7de745 417 {
1c79356b 418 struct tchars *tc = (struct tchars *)data;
39037602 419 cc_t *cc = tp->t_cc;
1c79356b
A
420
421 tc->t_intrc = cc[VINTR];
422 tc->t_quitc = cc[VQUIT];
423 tc->t_startc = cc[VSTART];
424 tc->t_stopc = cc[VSTOP];
425 tc->t_eofc = cc[VEOF];
426 tc->t_brkc = cc[VEOL];
427 break;
428 }
91447636
A
429 case TIOCGLTC:
430 /*
431 * Get the terminal control characters into the struct
432 * ltchars that 'data' points to.
433 */
434 {
1c79356b 435 struct ltchars *ltc = (struct ltchars *)data;
39037602 436 cc_t *cc = tp->t_cc;
1c79356b
A
437
438 ltc->t_suspc = cc[VSUSP];
439 ltc->t_dsuspc = cc[VDSUSP];
440 ltc->t_rprntc = cc[VREPRINT];
441 ltc->t_flushc = cc[VDISCARD];
442 ltc->t_werasc = cc[VWERASE];
443 ltc->t_lnextc = cc[VLNEXT];
444 break;
445 }
446 case TIOCLGET:
91447636
A
447 /*
448 * Get the terminal state local flags word into the 16 bit
449 * value pointed to by 'data'.
450 */
1c79356b 451 tp->t_flags =
0a7de745
A
452 (ttcompatgetflags(tp) & 0xffff0000UL)
453 | (tp->t_flags & 0xffff);
454 *(int *)data = tp->t_flags >> 16;
1c79356b
A
455 break;
456
457 case OTIOCGETD:
91447636
A
458 /*
459 * Get the current line discipline into the int pointed to
460 * by 'data'.
461 */
1c79356b
A
462 *(int *)data = tp->t_line ? tp->t_line : 2;
463 break;
464
91447636
A
465 case OTIOCSETD:
466 /*
467 * Set the current line discipline based on the value of the
468 * int pointed to by 'data'.
469 */
0a7de745 470 {
1c79356b
A
471 int ldisczero = 0;
472
0a7de745
A
473 return ttioctl_locked(tp, TIOCSETD,
474 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag, p);
475 }
1c79356b
A
476
477 case OTIOCCONS:
91447636
A
478 /*
479 * Become the console device.
480 */
1c79356b 481 *(int *)data = 1;
0a7de745 482 return ttioctl_locked(tp, TIOCCONS, data, flag, p);
1c79356b
A
483
484 case TIOCGSID:
91447636
A
485 /*
486 * Get the current session ID (controlling process' PID).
487 */
0a7de745 488 if (tp->t_session == NULL) {
1c79356b 489 return ENOTTY;
0a7de745 490 }
1c79356b 491
0a7de745 492 if (tp->t_session->s_leader == NULL) {
1c79356b 493 return ENOTTY;
0a7de745 494 }
1c79356b
A
495
496 *(int *) data = tp->t_session->s_leader->p_pid;
497 break;
1c79356b
A
498
499 default:
91447636
A
500 /*
501 * This ioctl is not handled at this layer.
502 */
0a7de745 503 return ENOTTY;
1c79356b 504 }
91447636
A
505
506 /*
507 * Successful 'get' operation.
508 */
0a7de745 509 return 0;
1c79356b
A
510}
511
91447636
A
512/*
513 * ttcompatgetflags
514 *
515 * Description: Get the terminal state local flags, device flags, and current
516 * speed code for the device (all 32 bits are returned).
517 *
518 * Parameters struct tty *tp The tty on which the operation is
519 * being performed.
520 *
521 * Returns: * Integer value corresponding to the
522 * current terminal state local flags
523 * word.
524 *
525 * Notes: Caller is responsible for breaking these bits back out into
526 * separate 16 bit filelds, if that's what was actually desired.
527 */
1c79356b 528static int
91447636 529ttcompatgetflags(struct tty *tp)
1c79356b 530{
0a7de745
A
531 tcflag_t iflag = tp->t_iflag;
532 tcflag_t lflag = tp->t_lflag;
533 tcflag_t oflag = tp->t_oflag;
534 tcflag_t cflag = tp->t_cflag;
39037602 535 int flags = 0;
1c79356b 536
0a7de745 537 if (iflag & IXOFF) {
1c79356b 538 flags |= TANDEM;
0a7de745
A
539 }
540 if (iflag & ICRNL || oflag & ONLCR) {
1c79356b 541 flags |= CRMOD;
0a7de745
A
542 }
543 if ((cflag & CSIZE) == CS8) {
1c79356b 544 flags |= PASS8;
0a7de745 545 if (iflag & ISTRIP) {
1c79356b 546 flags |= ANYP;
0a7de745
A
547 }
548 } else if (cflag & PARENB) {
549 if (iflag & INPCK) {
550 if (cflag & PARODD) {
1c79356b 551 flags |= ODDP;
0a7de745 552 } else {
1c79356b 553 flags |= EVENP;
0a7de745
A
554 }
555 } else {
1c79356b 556 flags |= EVENP | ODDP;
0a7de745 557 }
1c79356b
A
558 }
559
0a7de745 560 if ((lflag & ICANON) == 0) {
1c79356b 561 /* fudge */
0a7de745
A
562 if (iflag & (INPCK | ISTRIP | IXON) || lflag & (IEXTEN | ISIG)
563 || (cflag & (CSIZE | PARENB)) != CS8) {
1c79356b 564 flags |= CBREAK;
0a7de745 565 } else {
1c79356b 566 flags |= RAW;
0a7de745 567 }
1c79356b 568 }
0a7de745 569 if (!(flags & RAW) && !(oflag & OPOST) && (cflag & (CSIZE | PARENB)) == CS8) {
1c79356b 570 flags |= LITOUT;
0a7de745
A
571 }
572 if (cflag & MDMBUF) {
1c79356b 573 flags |= MDMBUF;
0a7de745
A
574 }
575 if ((cflag & HUPCL) == 0) {
1c79356b 576 flags |= NOHANG;
0a7de745
A
577 }
578 if (oflag & OXTABS) {
1c79356b 579 flags |= XTABS;
0a7de745
A
580 }
581 if (lflag & ECHOE) {
582 flags |= CRTERA | CRTBS;
583 }
584 if (lflag & ECHOKE) {
585 flags |= CRTKIL | CRTBS;
586 }
587 if (lflag & ECHOPRT) {
1c79356b 588 flags |= PRTERA;
0a7de745
A
589 }
590 if (lflag & ECHOCTL) {
1c79356b 591 flags |= CTLECH;
0a7de745
A
592 }
593 if ((iflag & IXANY) == 0) {
1c79356b 594 flags |= DECCTQ;
0a7de745
A
595 }
596 flags |= lflag & (ECHO | TOSTOP | FLUSHO | PENDIN | NOFLSH);
597 return flags;
1c79356b
A
598}
599
91447636
A
600/*
601 * ttcompatsetflags
602 *
603 * Description: Given a set of compatability flags, convert the compatability
604 * flags in the terminal flags fields into canonical flags in the
605 * provided termios struct.
606 *
607 * Parameters: struct tty *tp The tty on which the operation is
608 * being performed.
609 * struct termios *t The termios structure into which to
610 * return the converted flags.
611 *
612 * Returns: void (implicit: *t, modified)
613 */
1c79356b 614static void
91447636 615ttcompatsetflags(struct tty *tp, struct termios *t)
1c79356b 616{
39037602 617 int flags = tp->t_flags;
0a7de745
A
618 tcflag_t iflag = t->c_iflag;
619 tcflag_t oflag = t->c_oflag;
620 tcflag_t lflag = t->c_lflag;
621 tcflag_t cflag = t->c_cflag;
1c79356b
A
622
623 if (flags & RAW) {
624 iflag = IGNBRK;
0a7de745 625 lflag &= ~(ECHOCTL | ISIG | ICANON | IEXTEN);
1c79356b 626 } else {
0a7de745
A
627 iflag &= ~(PARMRK | IGNPAR | IGNCR | INLCR);
628 iflag |= BRKINT | IXON | IMAXBEL;
629 lflag |= ISIG | IEXTEN | ECHOCTL; /* XXX was echoctl on ? */
630 if (flags & XTABS) {
1c79356b 631 oflag |= OXTABS;
0a7de745 632 } else {
1c79356b 633 oflag &= ~OXTABS;
0a7de745
A
634 }
635 if (flags & CBREAK) {
1c79356b 636 lflag &= ~ICANON;
0a7de745 637 } else {
1c79356b 638 lflag |= ICANON;
0a7de745
A
639 }
640 if (flags & CRMOD) {
1c79356b
A
641 iflag |= ICRNL;
642 oflag |= ONLCR;
643 } else {
644 iflag &= ~ICRNL;
645 oflag &= ~ONLCR;
646 }
647 }
0a7de745 648 if (flags & ECHO) {
1c79356b 649 lflag |= ECHO;
0a7de745 650 } else {
1c79356b 651 lflag &= ~ECHO;
0a7de745 652 }
1c79356b 653
0a7de745
A
654 cflag &= ~(CSIZE | PARENB);
655 if (flags & (RAW | LITOUT | PASS8)) {
1c79356b 656 cflag |= CS8;
0a7de745
A
657 if (!(flags & (RAW | PASS8))
658 || (flags & (RAW | PASS8 | ANYP)) == (PASS8 | ANYP)) {
1c79356b 659 iflag |= ISTRIP;
0a7de745 660 } else {
1c79356b 661 iflag &= ~ISTRIP;
0a7de745
A
662 }
663 if (flags & (RAW | LITOUT)) {
1c79356b 664 oflag &= ~OPOST;
0a7de745 665 } else {
1c79356b 666 oflag |= OPOST;
0a7de745 667 }
1c79356b 668 } else {
0a7de745 669 cflag |= CS7 | PARENB;
1c79356b
A
670 iflag |= ISTRIP;
671 oflag |= OPOST;
672 }
673 /* XXX don't set INPCK if RAW or PASS8? */
0a7de745 674 if ((flags & (EVENP | ODDP)) == EVENP) {
1c79356b
A
675 iflag |= INPCK;
676 cflag &= ~PARODD;
0a7de745 677 } else if ((flags & (EVENP | ODDP)) == ODDP) {
1c79356b
A
678 iflag |= INPCK;
679 cflag |= PARODD;
0a7de745 680 } else {
1c79356b 681 iflag &= ~INPCK;
0a7de745
A
682 }
683 if (flags & TANDEM) {
1c79356b 684 iflag |= IXOFF;
0a7de745 685 } else {
1c79356b 686 iflag &= ~IXOFF;
0a7de745
A
687 }
688 if ((flags & DECCTQ) == 0) {
1c79356b 689 iflag |= IXANY;
0a7de745 690 } else {
1c79356b 691 iflag &= ~IXANY;
0a7de745 692 }
1c79356b
A
693 t->c_iflag = iflag;
694 t->c_oflag = oflag;
695 t->c_lflag = lflag;
696 t->c_cflag = cflag;
697}
698
91447636
A
699/*
700 * ttcompatsetlflags
701 *
702 * Description: Given a set of compatability terminal state local flags,
703 * convert the compatability flags in the terminal flags
704 * fields into canonical flags in the provided termios struct.
705 *
706 * Parameters: struct tty *tp The tty on which the operation is
707 * being performed.
708 * struct termios *t The termios structure into which to
709 * return the converted local flags.
710 *
711 * Returns: void (implicit: *t, modified)
712 */
1c79356b 713static void
91447636 714ttcompatsetlflags(struct tty *tp, struct termios *t)
1c79356b 715{
39037602 716 int flags = tp->t_flags;
0a7de745
A
717 tcflag_t iflag = t->c_iflag;
718 tcflag_t oflag = t->c_oflag;
719 tcflag_t lflag = t->c_lflag;
720 tcflag_t cflag = t->c_cflag;
1c79356b 721
0a7de745
A
722 iflag &= ~(PARMRK | IGNPAR | IGNCR | INLCR);
723 if (flags & CRTERA) {
1c79356b 724 lflag |= ECHOE;
0a7de745 725 } else {
1c79356b 726 lflag &= ~ECHOE;
0a7de745
A
727 }
728 if (flags & CRTKIL) {
1c79356b 729 lflag |= ECHOKE;
0a7de745 730 } else {
1c79356b 731 lflag &= ~ECHOKE;
0a7de745
A
732 }
733 if (flags & PRTERA) {
1c79356b 734 lflag |= ECHOPRT;
0a7de745 735 } else {
1c79356b 736 lflag &= ~ECHOPRT;
0a7de745
A
737 }
738 if (flags & CTLECH) {
1c79356b 739 lflag |= ECHOCTL;
0a7de745 740 } else {
1c79356b 741 lflag &= ~ECHOCTL;
0a7de745
A
742 }
743 if (flags & TANDEM) {
1c79356b 744 iflag |= IXOFF;
0a7de745 745 } else {
1c79356b 746 iflag &= ~IXOFF;
0a7de745
A
747 }
748 if ((flags & DECCTQ) == 0) {
1c79356b 749 iflag |= IXANY;
0a7de745 750 } else {
1c79356b 751 iflag &= ~IXANY;
0a7de745
A
752 }
753 if (flags & MDMBUF) {
1c79356b 754 cflag |= MDMBUF;
0a7de745 755 } else {
1c79356b 756 cflag &= ~MDMBUF;
0a7de745
A
757 }
758 if (flags & NOHANG) {
1c79356b 759 cflag &= ~HUPCL;
0a7de745 760 } else {
1c79356b 761 cflag |= HUPCL;
0a7de745
A
762 }
763 lflag &= ~(TOSTOP | FLUSHO | PENDIN | NOFLSH);
764 lflag |= flags & (TOSTOP | FLUSHO | PENDIN | NOFLSH);
1c79356b
A
765
766 /*
767 * The next if-else statement is copied from above so don't bother
768 * checking it separately. We could avoid fiddlling with the
769 * character size if the mode is already RAW or if neither the
770 * LITOUT bit or the PASS8 bit is being changed, but the delta of
771 * the change is not available here and skipping the RAW case would
772 * make the code different from above.
773 */
0a7de745
A
774 cflag &= ~(CSIZE | PARENB);
775 if (flags & (RAW | LITOUT | PASS8)) {
1c79356b 776 cflag |= CS8;
0a7de745
A
777 if (!(flags & (RAW | PASS8))
778 || (flags & (RAW | PASS8 | ANYP)) == (PASS8 | ANYP)) {
1c79356b 779 iflag |= ISTRIP;
0a7de745 780 } else {
1c79356b 781 iflag &= ~ISTRIP;
0a7de745
A
782 }
783 if (flags & (RAW | LITOUT)) {
1c79356b 784 oflag &= ~OPOST;
0a7de745 785 } else {
1c79356b 786 oflag |= OPOST;
0a7de745 787 }
1c79356b 788 } else {
0a7de745 789 cflag |= CS7 | PARENB;
1c79356b
A
790 iflag |= ISTRIP;
791 oflag |= OPOST;
792 }
793 t->c_iflag = iflag;
794 t->c_oflag = oflag;
795 t->c_lflag = lflag;
796 t->c_cflag = cflag;
797}