]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_compat.c
xnu-7195.101.1.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 90static void ttcompatsetlflags(struct tty *tp, struct termios *t);
f427ee49 91static unsigned int ttcompatspeedtab(speed_t 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[] = {
f427ee49 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 *
f427ee49
A
130 * Description: Given a baud rate value as a speed_t, and a speed table,
131 * convert the baud rate to a speed code integer, according to the
91447636
A
132 * contents of the table. This effectively changes termios.h
133 * baud rate values into ttydev.h baud rate codes.
134 *
f427ee49
A
135 * Parameters: speed_t speed Baud rate
136 * struct speedtab *table Baud rate table to speed code table
91447636 137 *
f427ee49
A
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 * [2, MAX_SPEED] A speed code matching the requested
144 * baud rate (potentially rounded down,
145 * if there is no exact match).
91447636
A
146 *
147 * Notes: This function is used for TIOCGETP, TIOCSETP, and TIOCSETN.
148 */
f427ee49
A
149static unsigned int
150ttcompatspeedtab(speed_t 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 */
f427ee49 157 return (unsigned int)table->sp_code;
0a7de745
A
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 *
f427ee49 208 * WARNING: This compatibility code is not 64/32 clean; it will only
b0d623f7
A
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 {
f427ee49
A
230 __IGNORE_WCASTALIGN(struct sgttyb *sg = (struct sgttyb *)data);
231 if (sg->sg_ispeed < 0) {
232 return EINVAL;
233 }
234 unsigned int ispeed = (unsigned int)sg->sg_ispeed;
235 if (ispeed > MAX_SPEED) {
0a7de745 236 return EINVAL;
f427ee49
A
237 }
238 if (ispeed != ttcompatspeedtab(tp->t_ispeed, compatspeeds)) {
239 term->c_ispeed = compatspcodes[ispeed];
0a7de745 240 } else {
1c79356b 241 term->c_ispeed = tp->t_ispeed;
0a7de745 242 }
f427ee49
A
243
244 /*
245 * Can't error out at the beginning due to potential for
246 * backwards-incompatibility. For instance:
247 *
248 * struct sgttyb sg; // uninitialized
249 * sg.sg_ispeed = SOME_VALID_VALUE;
250 *
251 * Should still set the input speed.
252 */
253 if (sg->sg_ospeed < 0) {
254 return EINVAL;
255 }
256 unsigned int ospeed = (unsigned int)sg->sg_ospeed;
257 if (ospeed > MAX_SPEED) {
0a7de745 258 return EINVAL;
f427ee49
A
259 }
260 if (ospeed != ttcompatspeedtab(tp->t_ospeed, compatspeeds)) {
261 term->c_ospeed = compatspcodes[ospeed];
0a7de745 262 } else {
1c79356b 263 term->c_ospeed = tp->t_ospeed;
0a7de745 264 }
f427ee49 265
1c79356b
A
266 term->c_cc[VERASE] = sg->sg_erase;
267 term->c_cc[VKILL] = sg->sg_kill;
0a7de745 268 tp->t_flags = (tp->t_flags & 0xffff0000) | (sg->sg_flags & 0xffff);
1c79356b
A
269 ttcompatsetflags(tp, term);
270 *com = (*com == TIOCSETP) ? TIOCSETAF : TIOCSETA;
271 break;
272 }
91447636
A
273 case TIOCSETC:
274 /*
275 * Set the terminal control characters per the contents of
276 * the struct tchars that 'data' points to.
277 */
0a7de745 278 {
f427ee49 279 __IGNORE_WCASTALIGN(struct tchars *tc = (struct tchars *)data);
39037602 280 cc_t *cc;
1c79356b
A
281
282 cc = term->c_cc;
283 cc[VINTR] = tc->t_intrc;
284 cc[VQUIT] = tc->t_quitc;
285 cc[VSTART] = tc->t_startc;
286 cc[VSTOP] = tc->t_stopc;
287 cc[VEOF] = tc->t_eofc;
288 cc[VEOL] = tc->t_brkc;
0a7de745 289 if (tc->t_brkc == -1) {
1c79356b 290 cc[VEOL2] = _POSIX_VDISABLE;
0a7de745 291 }
1c79356b
A
292 *com = TIOCSETA;
293 break;
294 }
91447636
A
295 case TIOCSLTC:
296 /*
297 * Set the terminal control characters per the contents of
298 * the struct ltchars that 'data' points to.
299 */
300 {
f427ee49 301 __IGNORE_WCASTALIGN(struct ltchars *ltc = (struct ltchars *)data);
39037602 302 cc_t *cc;
1c79356b
A
303
304 cc = term->c_cc;
305 cc[VSUSP] = ltc->t_suspc;
306 cc[VDSUSP] = ltc->t_dsuspc;
307 cc[VREPRINT] = ltc->t_rprntc;
308 cc[VDISCARD] = ltc->t_flushc;
309 cc[VWERASE] = ltc->t_werasc;
310 cc[VLNEXT] = ltc->t_lnextc;
311 *com = TIOCSETA;
312 break;
313 }
314 case TIOCLBIS:
0a7de745
A
315 /*
316 * Set the bits in the terminal state local flags word
317 * (16 bits) for the terminal to the current bits OR
318 * those in the 16 bit value pointed to by 'data'.
319 */
1c79356b 320 case TIOCLBIC:
0a7de745
A
321 /*
322 * Clear the bits in the terminal state local flags word
323 * for the terminal to the current bits AND those bits NOT
324 * in the 16 bit value pointed to by 'data'.
325 */
1c79356b 326 case TIOCLSET:
91447636
A
327 /*
328 * Set the terminal state local flags word to exactly those
329 * bits that correspond to the 16 bit value pointed to by
330 * 'data'.
331 */
f427ee49
A
332 {
333 __IGNORE_WCASTALIGN(int set = *(int *)data);
0a7de745 334 if (*com == TIOCLSET) {
f427ee49 335 tp->t_flags = (tp->t_flags & 0xffff) | set << 16;
0a7de745 336 } else {
1c79356b 337 tp->t_flags =
0a7de745
A
338 (ttcompatgetflags(tp) & 0xffff0000) | (tp->t_flags & 0xffff);
339 if (*com == TIOCLBIS) {
f427ee49 340 tp->t_flags |= set << 16;
0a7de745 341 } else {
f427ee49 342 tp->t_flags &= ~(set << 16);
0a7de745 343 }
1c79356b
A
344 }
345 ttcompatsetlflags(tp, term);
346 *com = TIOCSETA;
347 break;
348 }
f427ee49 349 }
1c79356b
A
350 return 0;
351}
352
91447636
A
353/*
354 * ttcompat
355 *
356 * Description: For 'set' commands, convert the command and arguments as
b0d623f7
A
357 * necessary, and call ttioctl_locked(), returning the result
358 * as our result; for 'get' commands, obtain the requested data
91447636
A
359 * from the appropriate source, and return it in the expected
360 * format. If the command is not recognized, return EINVAL.
361 *
362 * Parameters struct tty *tp The tty on which the operation is
363 * being performed.
364 * u_long com The terminal input/output command
365 * being requested.
366 * caddr_t data The pointer to the user data argument
367 * provided with the command.
368 * int flag The file open flags (e.g. FREAD).
369 * struct proc *p The current process pointer for the
370 * operation.
371 *
372 * Returns: 0 Most 'get' operations can't fail, and
373 * therefore return this.
374 * ENOTTY TIOCGSID may return this when you
375 * attempt to get the session ID for a
376 * terminal with no associated session,
377 * or for which there is a session, but
378 * no session leader.
b0d623f7 379 * ENOTTY If the command cannot be handled at
91447636 380 * this layer, this will be returned.
b0d623f7
A
381 * * Any value returned by ttioctl_locked(),
382 * if a set command is requested.
91447636 383 *
b0d623f7 384 * Notes: The process pointer may be a proxy on whose behalf we are
91447636
A
385 * operating, so it is not safe to simply use current_process()
386 * instead.
387 */
1c79356b 388/*ARGSUSED*/
1c79356b 389__private_extern__ int
91447636 390ttcompat(struct tty *tp, u_long com, caddr_t data, int flag, struct proc *p)
1c79356b
A
391{
392 switch (com) {
393 case TIOCSETP:
394 case TIOCSETN:
395 case TIOCSETC:
396 case TIOCSLTC:
397 case TIOCLBIS:
398 case TIOCLBIC:
91447636
A
399 case TIOCLSET:
400 /*
401 * See ttsetcompat() for a full description of these command
402 * values and their meanings.
403 */
404 {
1c79356b
A
405 struct termios term;
406 int error;
407
408 term = tp->t_termios;
0a7de745 409 if ((error = ttsetcompat(tp, &com, data, &term)) != 0) {
1c79356b 410 return error;
0a7de745 411 }
b0d623f7 412 return ttioctl_locked(tp, com, (caddr_t) &term, flag, p);
1c79356b 413 }
91447636
A
414 case TIOCGETP:
415 /*
416 * Get the current input and output speeds, and device
417 * flags, into the structure pointed to by 'data'.
418 */
0a7de745 419 {
f427ee49 420 __IGNORE_WCASTALIGN(struct sgttyb *sg = (struct sgttyb *)data);
39037602 421 cc_t *cc = tp->t_cc;
1c79356b 422
f427ee49
A
423 static_assert(MAX_SPEED <= CHAR_MAX, "maximum speed fits in a char");
424 sg->sg_ospeed = (char)ttcompatspeedtab(tp->t_ospeed, compatspeeds);
0a7de745 425 if (tp->t_ispeed == 0) {
1c79356b 426 sg->sg_ispeed = sg->sg_ospeed;
0a7de745 427 } else {
f427ee49 428 sg->sg_ispeed = (char)ttcompatspeedtab(tp->t_ispeed, compatspeeds);
0a7de745 429 }
1c79356b
A
430 sg->sg_erase = cc[VERASE];
431 sg->sg_kill = cc[VKILL];
f427ee49
A
432 tp->t_flags = ttcompatgetflags(tp);
433 sg->sg_flags = (short)tp->t_flags;
1c79356b
A
434 break;
435 }
91447636
A
436 case TIOCGETC:
437 /*
438 * Get the terminal control characters into the struct
439 * tchars that 'data' points to.
440 */
0a7de745 441 {
1c79356b 442 struct tchars *tc = (struct tchars *)data;
39037602 443 cc_t *cc = tp->t_cc;
1c79356b
A
444
445 tc->t_intrc = cc[VINTR];
446 tc->t_quitc = cc[VQUIT];
447 tc->t_startc = cc[VSTART];
448 tc->t_stopc = cc[VSTOP];
449 tc->t_eofc = cc[VEOF];
450 tc->t_brkc = cc[VEOL];
451 break;
452 }
91447636
A
453 case TIOCGLTC:
454 /*
455 * Get the terminal control characters into the struct
456 * ltchars that 'data' points to.
457 */
458 {
1c79356b 459 struct ltchars *ltc = (struct ltchars *)data;
39037602 460 cc_t *cc = tp->t_cc;
1c79356b
A
461
462 ltc->t_suspc = cc[VSUSP];
463 ltc->t_dsuspc = cc[VDSUSP];
464 ltc->t_rprntc = cc[VREPRINT];
465 ltc->t_flushc = cc[VDISCARD];
466 ltc->t_werasc = cc[VWERASE];
467 ltc->t_lnextc = cc[VLNEXT];
468 break;
469 }
470 case TIOCLGET:
91447636
A
471 /*
472 * Get the terminal state local flags word into the 16 bit
473 * value pointed to by 'data'.
474 */
1c79356b 475 tp->t_flags =
0a7de745
A
476 (ttcompatgetflags(tp) & 0xffff0000UL)
477 | (tp->t_flags & 0xffff);
478 *(int *)data = tp->t_flags >> 16;
1c79356b
A
479 break;
480
481 case OTIOCGETD:
91447636
A
482 /*
483 * Get the current line discipline into the int pointed to
484 * by 'data'.
485 */
1c79356b
A
486 *(int *)data = tp->t_line ? tp->t_line : 2;
487 break;
488
91447636
A
489 case OTIOCSETD:
490 /*
491 * Set the current line discipline based on the value of the
492 * int pointed to by 'data'.
493 */
0a7de745 494 {
1c79356b
A
495 int ldisczero = 0;
496
0a7de745
A
497 return ttioctl_locked(tp, TIOCSETD,
498 *(int *)data == 2 ? (caddr_t)&ldisczero : data, flag, p);
499 }
1c79356b
A
500
501 case OTIOCCONS:
91447636
A
502 /*
503 * Become the console device.
504 */
1c79356b 505 *(int *)data = 1;
0a7de745 506 return ttioctl_locked(tp, TIOCCONS, data, flag, p);
1c79356b
A
507
508 case TIOCGSID:
91447636
A
509 /*
510 * Get the current session ID (controlling process' PID).
511 */
0a7de745 512 if (tp->t_session == NULL) {
1c79356b 513 return ENOTTY;
0a7de745 514 }
1c79356b 515
0a7de745 516 if (tp->t_session->s_leader == NULL) {
1c79356b 517 return ENOTTY;
0a7de745 518 }
1c79356b
A
519
520 *(int *) data = tp->t_session->s_leader->p_pid;
521 break;
1c79356b
A
522
523 default:
91447636
A
524 /*
525 * This ioctl is not handled at this layer.
526 */
0a7de745 527 return ENOTTY;
1c79356b 528 }
91447636
A
529
530 /*
531 * Successful 'get' operation.
532 */
0a7de745 533 return 0;
1c79356b
A
534}
535
91447636
A
536/*
537 * ttcompatgetflags
538 *
539 * Description: Get the terminal state local flags, device flags, and current
540 * speed code for the device (all 32 bits are returned).
541 *
542 * Parameters struct tty *tp The tty on which the operation is
543 * being performed.
544 *
545 * Returns: * Integer value corresponding to the
546 * current terminal state local flags
547 * word.
548 *
549 * Notes: Caller is responsible for breaking these bits back out into
550 * separate 16 bit filelds, if that's what was actually desired.
551 */
1c79356b 552static int
91447636 553ttcompatgetflags(struct tty *tp)
1c79356b 554{
0a7de745
A
555 tcflag_t iflag = tp->t_iflag;
556 tcflag_t lflag = tp->t_lflag;
557 tcflag_t oflag = tp->t_oflag;
558 tcflag_t cflag = tp->t_cflag;
39037602 559 int flags = 0;
1c79356b 560
0a7de745 561 if (iflag & IXOFF) {
1c79356b 562 flags |= TANDEM;
0a7de745
A
563 }
564 if (iflag & ICRNL || oflag & ONLCR) {
1c79356b 565 flags |= CRMOD;
0a7de745
A
566 }
567 if ((cflag & CSIZE) == CS8) {
1c79356b 568 flags |= PASS8;
0a7de745 569 if (iflag & ISTRIP) {
1c79356b 570 flags |= ANYP;
0a7de745
A
571 }
572 } else if (cflag & PARENB) {
573 if (iflag & INPCK) {
574 if (cflag & PARODD) {
1c79356b 575 flags |= ODDP;
0a7de745 576 } else {
1c79356b 577 flags |= EVENP;
0a7de745
A
578 }
579 } else {
1c79356b 580 flags |= EVENP | ODDP;
0a7de745 581 }
1c79356b
A
582 }
583
0a7de745 584 if ((lflag & ICANON) == 0) {
1c79356b 585 /* fudge */
0a7de745
A
586 if (iflag & (INPCK | ISTRIP | IXON) || lflag & (IEXTEN | ISIG)
587 || (cflag & (CSIZE | PARENB)) != CS8) {
1c79356b 588 flags |= CBREAK;
0a7de745 589 } else {
1c79356b 590 flags |= RAW;
0a7de745 591 }
1c79356b 592 }
0a7de745 593 if (!(flags & RAW) && !(oflag & OPOST) && (cflag & (CSIZE | PARENB)) == CS8) {
1c79356b 594 flags |= LITOUT;
0a7de745
A
595 }
596 if (cflag & MDMBUF) {
1c79356b 597 flags |= MDMBUF;
0a7de745
A
598 }
599 if ((cflag & HUPCL) == 0) {
1c79356b 600 flags |= NOHANG;
0a7de745
A
601 }
602 if (oflag & OXTABS) {
1c79356b 603 flags |= XTABS;
0a7de745
A
604 }
605 if (lflag & ECHOE) {
606 flags |= CRTERA | CRTBS;
607 }
608 if (lflag & ECHOKE) {
609 flags |= CRTKIL | CRTBS;
610 }
611 if (lflag & ECHOPRT) {
1c79356b 612 flags |= PRTERA;
0a7de745
A
613 }
614 if (lflag & ECHOCTL) {
1c79356b 615 flags |= CTLECH;
0a7de745
A
616 }
617 if ((iflag & IXANY) == 0) {
1c79356b 618 flags |= DECCTQ;
0a7de745
A
619 }
620 flags |= lflag & (ECHO | TOSTOP | FLUSHO | PENDIN | NOFLSH);
621 return flags;
1c79356b
A
622}
623
91447636
A
624/*
625 * ttcompatsetflags
626 *
627 * Description: Given a set of compatability flags, convert the compatability
628 * flags in the terminal flags fields into canonical flags in the
629 * provided termios struct.
630 *
631 * Parameters: struct tty *tp The tty on which the operation is
632 * being performed.
633 * struct termios *t The termios structure into which to
634 * return the converted flags.
635 *
636 * Returns: void (implicit: *t, modified)
637 */
1c79356b 638static void
91447636 639ttcompatsetflags(struct tty *tp, struct termios *t)
1c79356b 640{
39037602 641 int flags = tp->t_flags;
0a7de745
A
642 tcflag_t iflag = t->c_iflag;
643 tcflag_t oflag = t->c_oflag;
644 tcflag_t lflag = t->c_lflag;
645 tcflag_t cflag = t->c_cflag;
1c79356b
A
646
647 if (flags & RAW) {
648 iflag = IGNBRK;
0a7de745 649 lflag &= ~(ECHOCTL | ISIG | ICANON | IEXTEN);
1c79356b 650 } else {
0a7de745
A
651 iflag &= ~(PARMRK | IGNPAR | IGNCR | INLCR);
652 iflag |= BRKINT | IXON | IMAXBEL;
653 lflag |= ISIG | IEXTEN | ECHOCTL; /* XXX was echoctl on ? */
654 if (flags & XTABS) {
1c79356b 655 oflag |= OXTABS;
0a7de745 656 } else {
1c79356b 657 oflag &= ~OXTABS;
0a7de745
A
658 }
659 if (flags & CBREAK) {
1c79356b 660 lflag &= ~ICANON;
0a7de745 661 } else {
1c79356b 662 lflag |= ICANON;
0a7de745
A
663 }
664 if (flags & CRMOD) {
1c79356b
A
665 iflag |= ICRNL;
666 oflag |= ONLCR;
667 } else {
668 iflag &= ~ICRNL;
669 oflag &= ~ONLCR;
670 }
671 }
0a7de745 672 if (flags & ECHO) {
1c79356b 673 lflag |= ECHO;
0a7de745 674 } else {
1c79356b 675 lflag &= ~ECHO;
0a7de745 676 }
1c79356b 677
0a7de745
A
678 cflag &= ~(CSIZE | PARENB);
679 if (flags & (RAW | LITOUT | PASS8)) {
1c79356b 680 cflag |= CS8;
0a7de745
A
681 if (!(flags & (RAW | PASS8))
682 || (flags & (RAW | PASS8 | ANYP)) == (PASS8 | ANYP)) {
1c79356b 683 iflag |= ISTRIP;
0a7de745 684 } else {
1c79356b 685 iflag &= ~ISTRIP;
0a7de745
A
686 }
687 if (flags & (RAW | LITOUT)) {
1c79356b 688 oflag &= ~OPOST;
0a7de745 689 } else {
1c79356b 690 oflag |= OPOST;
0a7de745 691 }
1c79356b 692 } else {
0a7de745 693 cflag |= CS7 | PARENB;
1c79356b
A
694 iflag |= ISTRIP;
695 oflag |= OPOST;
696 }
697 /* XXX don't set INPCK if RAW or PASS8? */
0a7de745 698 if ((flags & (EVENP | ODDP)) == EVENP) {
1c79356b
A
699 iflag |= INPCK;
700 cflag &= ~PARODD;
0a7de745 701 } else if ((flags & (EVENP | ODDP)) == ODDP) {
1c79356b
A
702 iflag |= INPCK;
703 cflag |= PARODD;
0a7de745 704 } else {
1c79356b 705 iflag &= ~INPCK;
0a7de745
A
706 }
707 if (flags & TANDEM) {
1c79356b 708 iflag |= IXOFF;
0a7de745 709 } else {
1c79356b 710 iflag &= ~IXOFF;
0a7de745
A
711 }
712 if ((flags & DECCTQ) == 0) {
1c79356b 713 iflag |= IXANY;
0a7de745 714 } else {
1c79356b 715 iflag &= ~IXANY;
0a7de745 716 }
1c79356b
A
717 t->c_iflag = iflag;
718 t->c_oflag = oflag;
719 t->c_lflag = lflag;
720 t->c_cflag = cflag;
721}
722
91447636
A
723/*
724 * ttcompatsetlflags
725 *
726 * Description: Given a set of compatability terminal state local flags,
727 * convert the compatability flags in the terminal flags
728 * fields into canonical flags in the provided termios struct.
729 *
730 * Parameters: struct tty *tp The tty on which the operation is
731 * being performed.
732 * struct termios *t The termios structure into which to
733 * return the converted local flags.
734 *
735 * Returns: void (implicit: *t, modified)
736 */
1c79356b 737static void
91447636 738ttcompatsetlflags(struct tty *tp, struct termios *t)
1c79356b 739{
39037602 740 int flags = tp->t_flags;
0a7de745
A
741 tcflag_t iflag = t->c_iflag;
742 tcflag_t oflag = t->c_oflag;
743 tcflag_t lflag = t->c_lflag;
744 tcflag_t cflag = t->c_cflag;
1c79356b 745
0a7de745
A
746 iflag &= ~(PARMRK | IGNPAR | IGNCR | INLCR);
747 if (flags & CRTERA) {
1c79356b 748 lflag |= ECHOE;
0a7de745 749 } else {
1c79356b 750 lflag &= ~ECHOE;
0a7de745
A
751 }
752 if (flags & CRTKIL) {
1c79356b 753 lflag |= ECHOKE;
0a7de745 754 } else {
1c79356b 755 lflag &= ~ECHOKE;
0a7de745
A
756 }
757 if (flags & PRTERA) {
1c79356b 758 lflag |= ECHOPRT;
0a7de745 759 } else {
1c79356b 760 lflag &= ~ECHOPRT;
0a7de745
A
761 }
762 if (flags & CTLECH) {
1c79356b 763 lflag |= ECHOCTL;
0a7de745 764 } else {
1c79356b 765 lflag &= ~ECHOCTL;
0a7de745
A
766 }
767 if (flags & TANDEM) {
1c79356b 768 iflag |= IXOFF;
0a7de745 769 } else {
1c79356b 770 iflag &= ~IXOFF;
0a7de745
A
771 }
772 if ((flags & DECCTQ) == 0) {
1c79356b 773 iflag |= IXANY;
0a7de745 774 } else {
1c79356b 775 iflag &= ~IXANY;
0a7de745
A
776 }
777 if (flags & MDMBUF) {
1c79356b 778 cflag |= MDMBUF;
0a7de745 779 } else {
1c79356b 780 cflag &= ~MDMBUF;
0a7de745
A
781 }
782 if (flags & NOHANG) {
1c79356b 783 cflag &= ~HUPCL;
0a7de745 784 } else {
1c79356b 785 cflag |= HUPCL;
0a7de745
A
786 }
787 lflag &= ~(TOSTOP | FLUSHO | PENDIN | NOFLSH);
788 lflag |= flags & (TOSTOP | FLUSHO | PENDIN | NOFLSH);
1c79356b
A
789
790 /*
791 * The next if-else statement is copied from above so don't bother
792 * checking it separately. We could avoid fiddlling with the
793 * character size if the mode is already RAW or if neither the
794 * LITOUT bit or the PASS8 bit is being changed, but the delta of
795 * the change is not available here and skipping the RAW case would
796 * make the code different from above.
797 */
0a7de745
A
798 cflag &= ~(CSIZE | PARENB);
799 if (flags & (RAW | LITOUT | PASS8)) {
1c79356b 800 cflag |= CS8;
0a7de745
A
801 if (!(flags & (RAW | PASS8))
802 || (flags & (RAW | PASS8 | ANYP)) == (PASS8 | ANYP)) {
1c79356b 803 iflag |= ISTRIP;
0a7de745 804 } else {
1c79356b 805 iflag &= ~ISTRIP;
0a7de745
A
806 }
807 if (flags & (RAW | LITOUT)) {
1c79356b 808 oflag &= ~OPOST;
0a7de745 809 } else {
1c79356b 810 oflag |= OPOST;
0a7de745 811 }
1c79356b 812 } else {
0a7de745 813 cflag |= CS7 | PARENB;
1c79356b
A
814 iflag |= ISTRIP;
815 oflag |= OPOST;
816 }
817 t->c_iflag = iflag;
818 t->c_oflag = oflag;
819 t->c_lflag = lflag;
820 t->c_cflag = cflag;
821}