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