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