]> git.saurik.com Git - apple/libc.git/blob - gen/termios-fbsd.c
ab540d6798bc37264acac8e99e222d579d087ca3
[apple/libc.git] / gen / termios-fbsd.c
1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)termios.c 8.2 (Berkeley) 2/21/94";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/lib/libc/gen/termios.c,v 1.16 2009/05/07 13:49:48 ed Exp $");
35
36 #if __DARWIN_UNIX03
37 #ifdef VARIANT_CANCELABLE
38 #include <pthread.h>
39
40 extern void _pthread_testcancel(pthread_t thread, int isconforming);
41 #endif /* VARIANT_CANCELABLE */
42 #endif /* __DARWIN_UNIX03 */
43
44 #include "namespace.h"
45 #include <sys/types.h>
46 #include <sys/fcntl.h>
47 #include <sys/ioctl.h>
48 #include <sys/time.h>
49
50 #include <errno.h>
51 #include <termios.h>
52 #include <unistd.h>
53 #include "un-namespace.h"
54
55 #ifndef BUILDING_VARIANT
56 int
57 tcgetattr(fd, t)
58 int fd;
59 struct termios *t;
60 {
61
62 return (_ioctl(fd, TIOCGETA, t));
63 }
64
65 int
66 tcsetattr(fd, opt, t)
67 int fd, opt;
68 const struct termios *t;
69 {
70 struct termios localterm;
71
72 if (opt & TCSASOFT) {
73 localterm = *t;
74 localterm.c_cflag |= CIGNORE;
75 t = &localterm;
76 }
77 switch (opt & ~TCSASOFT) {
78 case TCSANOW:
79 return (_ioctl(fd, TIOCSETA, t));
80 case TCSADRAIN:
81 return (_ioctl(fd, TIOCSETAW, t));
82 case TCSAFLUSH:
83 return (_ioctl(fd, TIOCSETAF, t));
84 default:
85 errno = EINVAL;
86 return (-1);
87 }
88 }
89
90 int
91 tcsetpgrp(int fd, pid_t pgrp)
92 {
93 int s;
94
95 if (isatty(fd) == 0)
96 return (-1);
97
98 s = pgrp;
99 return (_ioctl(fd, TIOCSPGRP, &s));
100 }
101
102 pid_t
103 tcgetpgrp(fd)
104 int fd;
105 {
106 int s;
107
108 if (isatty(fd) == 0)
109 return ((pid_t)-1);
110
111 if (_ioctl(fd, TIOCGPGRP, &s) < 0)
112 return ((pid_t)-1);
113
114 return ((pid_t)s);
115 }
116
117 #if 0 // Needs API review first
118 pid_t
119 tcgetsid(int fd)
120 {
121 int s;
122
123 if (_ioctl(fd, TIOCGSID, &s) < 0)
124 return ((pid_t)-1);
125
126 return ((pid_t)s);
127 }
128
129 int
130 tcsetsid(int fd, pid_t pid)
131 {
132
133 if (pid != getsid(0)) {
134 errno = EINVAL;
135 return (-1);
136 }
137
138 return (_ioctl(fd, TIOCSCTTY, NULL));
139 }
140 #endif
141
142 speed_t
143 cfgetospeed(t)
144 const struct termios *t;
145 {
146
147 return (t->c_ospeed);
148 }
149
150 speed_t
151 cfgetispeed(t)
152 const struct termios *t;
153 {
154
155 return (t->c_ispeed);
156 }
157
158 int
159 cfsetospeed(t, speed)
160 struct termios *t;
161 speed_t speed;
162 {
163
164 t->c_ospeed = speed;
165 return (0);
166 }
167
168 int
169 cfsetispeed(t, speed)
170 struct termios *t;
171 speed_t speed;
172 {
173
174 t->c_ispeed = speed;
175 return (0);
176 }
177
178 int
179 cfsetspeed(t, speed)
180 struct termios *t;
181 speed_t speed;
182 {
183
184 t->c_ispeed = t->c_ospeed = speed;
185 return (0);
186 }
187
188 /*
189 * Make a pre-existing termios structure into "raw" mode: character-at-a-time
190 * mode with no characters interpreted, 8-bit data path.
191 */
192 void
193 cfmakeraw(t)
194 struct termios *t;
195 {
196
197 t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
198 t->c_iflag |= IGNBRK;
199 t->c_oflag &= ~OPOST;
200 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
201 t->c_cflag &= ~(CSIZE|PARENB);
202 t->c_cflag |= CS8|CREAD;
203 t->c_cc[VMIN] = 1;
204 t->c_cc[VTIME] = 0;
205 }
206
207 int
208 tcsendbreak(fd, len)
209 int fd, len;
210 {
211 struct timeval sleepytime;
212
213 sleepytime.tv_sec = 0;
214 sleepytime.tv_usec = 400000;
215 if (_ioctl(fd, TIOCSBRK, 0) == -1)
216 return (-1);
217 (void)_select(0, 0, 0, 0, &sleepytime);
218 if (_ioctl(fd, TIOCCBRK, 0) == -1)
219 return (-1);
220 return (0);
221 }
222 #endif /* BUILDING_VARIANT */
223
224 int
225 __tcdrain(fd)
226 int fd;
227 {
228 #if __DARWIN_UNIX03
229 #ifdef VARIANT_CANCELABLE
230 _pthread_testcancel(pthread_self(), 1);
231 #endif /* VARIANT_CANCELABLE */
232 #endif /* __DARWIN_UNIX03 */
233 return (_ioctl(fd, TIOCDRAIN, 0));
234 }
235
236 __weak_reference(__tcdrain, tcdrain);
237 __weak_reference(__tcdrain, _tcdrain);
238
239 #ifndef BUILDING_VARIANT
240 int
241 tcflush(fd, which)
242 int fd, which;
243 {
244 int com;
245
246 switch (which) {
247 case TCIFLUSH:
248 com = FREAD;
249 break;
250 case TCOFLUSH:
251 com = FWRITE;
252 break;
253 case TCIOFLUSH:
254 com = FREAD | FWRITE;
255 break;
256 default:
257 errno = EINVAL;
258 return (-1);
259 }
260 return (_ioctl(fd, TIOCFLUSH, &com));
261 }
262
263 int
264 tcflow(fd, action)
265 int fd, action;
266 {
267 struct termios term;
268 u_char c;
269
270 switch (action) {
271 case TCOOFF:
272 return (_ioctl(fd, TIOCSTOP, 0));
273 case TCOON:
274 return (_ioctl(fd, TIOCSTART, 0));
275 case TCION:
276 return (_ioctl(fd, TIOCIXON, 0));
277 case TCIOFF:
278 return (_ioctl(fd, TIOCIXOFF, 0));
279 default:
280 errno = EINVAL;
281 return (-1);
282 }
283 /* NOTREACHED */
284 }
285 #endif /* BUILDING_VARIANT */