xref: /freebsd/lib/libc/gen/termios.c (revision c96ae1968a6ab7056427a739bce81bf07447c2d4)
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(fd, t)
49 	int fd;
50 	struct termios *t;
51 {
52 
53 	return (_ioctl(fd, TIOCGETA, t));
54 }
55 
56 int
57 tcsetattr(fd, opt, t)
58 	int fd, opt;
59 	const struct termios *t;
60 {
61 	struct termios localterm;
62 
63 	if (opt & TCSASOFT) {
64 		localterm = *t;
65 		localterm.c_cflag |= CIGNORE;
66 		t = &localterm;
67 	}
68 	switch (opt & ~TCSASOFT) {
69 	case TCSANOW:
70 		return (_ioctl(fd, TIOCSETA, t));
71 	case TCSADRAIN:
72 		return (_ioctl(fd, TIOCSETAW, t));
73 	case TCSAFLUSH:
74 		return (_ioctl(fd, TIOCSETAF, t));
75 	default:
76 		errno = EINVAL;
77 		return (-1);
78 	}
79 }
80 
81 int
82 tcsetpgrp(int fd, pid_t pgrp)
83 {
84 	int s;
85 
86 	s = pgrp;
87 	return (_ioctl(fd, TIOCSPGRP, &s));
88 }
89 
90 pid_t
91 tcgetpgrp(fd)
92 	int fd;
93 {
94 	int s;
95 
96 	if (_ioctl(fd, TIOCGPGRP, &s) < 0)
97 		return ((pid_t)-1);
98 
99 	return ((pid_t)s);
100 }
101 
102 speed_t
103 cfgetospeed(t)
104 	const struct termios *t;
105 {
106 
107 	return (t->c_ospeed);
108 }
109 
110 speed_t
111 cfgetispeed(t)
112 	const struct termios *t;
113 {
114 
115 	return (t->c_ispeed);
116 }
117 
118 int
119 cfsetospeed(t, speed)
120 	struct termios *t;
121 	speed_t speed;
122 {
123 
124 	t->c_ospeed = speed;
125 	return (0);
126 }
127 
128 int
129 cfsetispeed(t, speed)
130 	struct termios *t;
131 	speed_t speed;
132 {
133 
134 	t->c_ispeed = speed;
135 	return (0);
136 }
137 
138 int
139 cfsetspeed(t, speed)
140 	struct termios *t;
141 	speed_t speed;
142 {
143 
144 	t->c_ispeed = t->c_ospeed = speed;
145 	return (0);
146 }
147 
148 /*
149  * Make a pre-existing termios structure into "raw" mode: character-at-a-time
150  * mode with no characters interpreted, 8-bit data path.
151  */
152 void
153 cfmakeraw(t)
154 	struct termios *t;
155 {
156 
157 	t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
158 	t->c_iflag |= IGNBRK;
159 	t->c_oflag &= ~OPOST;
160 	t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
161 	t->c_cflag &= ~(CSIZE|PARENB);
162 	t->c_cflag |= CS8|CREAD;
163 	t->c_cc[VMIN] = 1;
164 	t->c_cc[VTIME] = 0;
165 }
166 
167 int
168 tcsendbreak(fd, len)
169 	int fd, len;
170 {
171 	struct timeval sleepytime;
172 
173 	sleepytime.tv_sec = 0;
174 	sleepytime.tv_usec = 400000;
175 	if (_ioctl(fd, TIOCSBRK, 0) == -1)
176 		return (-1);
177 	(void)_select(0, 0, 0, 0, &sleepytime);
178 	if (_ioctl(fd, TIOCCBRK, 0) == -1)
179 		return (-1);
180 	return (0);
181 }
182 
183 int
184 __tcdrain(fd)
185 	int fd;
186 {
187 	return (_ioctl(fd, TIOCDRAIN, 0));
188 }
189 
190 __weak_reference(__tcdrain, tcdrain);
191 __weak_reference(__tcdrain, _tcdrain);
192 
193 int
194 tcflush(fd, which)
195 	int fd, which;
196 {
197 	int com;
198 
199 	switch (which) {
200 	case TCIFLUSH:
201 		com = FREAD;
202 		break;
203 	case TCOFLUSH:
204 		com = FWRITE;
205 		break;
206 	case TCIOFLUSH:
207 		com = FREAD | FWRITE;
208 		break;
209 	default:
210 		errno = EINVAL;
211 		return (-1);
212 	}
213 	return (_ioctl(fd, TIOCFLUSH, &com));
214 }
215 
216 int
217 tcflow(fd, action)
218 	int fd, action;
219 {
220 	struct termios term;
221 	u_char c;
222 
223 	switch (action) {
224 	case TCOOFF:
225 		return (_ioctl(fd, TIOCSTOP, 0));
226 	case TCOON:
227 		return (_ioctl(fd, TIOCSTART, 0));
228 	case TCION:
229 	case TCIOFF:
230 		if (tcgetattr(fd, &term) == -1)
231 			return (-1);
232 		c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
233 		if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1)
234 			return (-1);
235 		return (0);
236 	default:
237 		errno = EINVAL;
238 		return (-1);
239 	}
240 	/* NOTREACHED */
241 }
242