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