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