xref: /freebsd/lib/libc/gen/termios.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
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$");
35 
36 #include "namespace.h"
37 #include <sys/types.h>
38 #include <sys/fcntl.h>
39 #include <sys/ioctl.h>
40 #include <sys/time.h>
41 
42 #include <errno.h>
43 #include <termios.h>
44 #include <unistd.h>
45 #include "un-namespace.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 int
177 tcsendbreak(int fd, int len __unused)
178 {
179 	struct timeval sleepytime;
180 
181 	sleepytime.tv_sec = 0;
182 	sleepytime.tv_usec = 400000;
183 	if (_ioctl(fd, TIOCSBRK, 0) == -1)
184 		return (-1);
185 	(void)_select(0, 0, 0, 0, &sleepytime);
186 	if (_ioctl(fd, TIOCCBRK, 0) == -1)
187 		return (-1);
188 	return (0);
189 }
190 
191 int
192 __tcdrain(int fd)
193 {
194 	return (_ioctl(fd, TIOCDRAIN, 0));
195 }
196 
197 __weak_reference(__tcdrain, tcdrain);
198 __weak_reference(__tcdrain, _tcdrain);
199 
200 int
201 tcflush(int fd, int which)
202 {
203 	int com;
204 
205 	switch (which) {
206 	case TCIFLUSH:
207 		com = FREAD;
208 		break;
209 	case TCOFLUSH:
210 		com = FWRITE;
211 		break;
212 	case TCIOFLUSH:
213 		com = FREAD | FWRITE;
214 		break;
215 	default:
216 		errno = EINVAL;
217 		return (-1);
218 	}
219 	return (_ioctl(fd, TIOCFLUSH, &com));
220 }
221 
222 int
223 tcflow(int fd, int action)
224 {
225 	struct termios term;
226 	u_char c;
227 
228 	switch (action) {
229 	case TCOOFF:
230 		return (_ioctl(fd, TIOCSTOP, 0));
231 	case TCOON:
232 		return (_ioctl(fd, TIOCSTART, 0));
233 	case TCION:
234 	case TCIOFF:
235 		if (tcgetattr(fd, &term) == -1)
236 			return (-1);
237 		c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
238 		if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1)
239 			return (-1);
240 		return (0);
241 	default:
242 		errno = EINVAL;
243 		return (-1);
244 	}
245 	/* NOTREACHED */
246 }
247