xref: /freebsd/lib/libc/gen/termios.c (revision d82e286489da73321a47e329d98a98817b0438b6)
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 
38 #include <sys/types.h>
39 #include <sys/fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/time.h>
42 
43 #include <errno.h>
44 #include <termios.h>
45 #include <unistd.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 #if __STDC__
83 tcsetpgrp(int fd, pid_t pgrp)
84 #else
85 tcsetpgrp(fd, pgrp)
86 	int fd;
87 	pid_t pgrp;
88 #endif
89 {
90 	int s;
91 
92 	s = pgrp;
93 	return (ioctl(fd, TIOCSPGRP, &s));
94 }
95 
96 pid_t
97 tcgetpgrp(fd)
98 	int fd;
99 {
100 	int s;
101 
102 	if (ioctl(fd, TIOCGPGRP, &s) < 0)
103 		return ((pid_t)-1);
104 
105 	return ((pid_t)s);
106 }
107 
108 speed_t
109 cfgetospeed(t)
110 	const struct termios *t;
111 {
112 
113 	return (t->c_ospeed);
114 }
115 
116 speed_t
117 cfgetispeed(t)
118 	const struct termios *t;
119 {
120 
121 	return (t->c_ispeed);
122 }
123 
124 int
125 cfsetospeed(t, speed)
126 	struct termios *t;
127 	speed_t speed;
128 {
129 
130 	t->c_ospeed = speed;
131 	return (0);
132 }
133 
134 int
135 cfsetispeed(t, speed)
136 	struct termios *t;
137 	speed_t speed;
138 {
139 
140 	t->c_ispeed = speed;
141 	return (0);
142 }
143 
144 int
145 cfsetspeed(t, speed)
146 	struct termios *t;
147 	speed_t speed;
148 {
149 
150 	t->c_ispeed = t->c_ospeed = speed;
151 	return (0);
152 }
153 
154 /*
155  * Make a pre-existing termios structure into "raw" mode: character-at-a-time
156  * mode with no characters interpreted, 8-bit data path.
157  */
158 void
159 cfmakeraw(t)
160 	struct termios *t;
161 {
162 
163 	t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
164 	t->c_iflag |= IGNBRK;
165 	t->c_oflag &= ~OPOST;
166 	t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
167 	t->c_cflag &= ~(CSIZE|PARENB);
168 	t->c_cflag |= CS8;
169 	t->c_cc[VMIN] = 1;
170 	t->c_cc[VTIME] = 0;
171 }
172 
173 int
174 tcsendbreak(fd, len)
175 	int fd, len;
176 {
177 	struct timeval sleepytime;
178 
179 	sleepytime.tv_sec = 0;
180 	sleepytime.tv_usec = 400000;
181 	if (ioctl(fd, TIOCSBRK, 0) == -1)
182 		return (-1);
183 	(void)select(0, 0, 0, 0, &sleepytime);
184 	if (ioctl(fd, TIOCCBRK, 0) == -1)
185 		return (-1);
186 	return (0);
187 }
188 
189 int
190 tcdrain(fd)
191 	int fd;
192 {
193 
194 	return (ioctl(fd, TIOCDRAIN, 0));
195 }
196 
197 int
198 tcflush(fd, which)
199 	int fd, which;
200 {
201 	int com;
202 
203 	switch (which) {
204 	case TCIFLUSH:
205 		com = FREAD;
206 		break;
207 	case TCOFLUSH:
208 		com = FWRITE;
209 		break;
210 	case TCIOFLUSH:
211 		com = FREAD | FWRITE;
212 		break;
213 	default:
214 		errno = EINVAL;
215 		return (-1);
216 	}
217 	return (ioctl(fd, TIOCFLUSH, &com));
218 }
219 
220 int
221 tcflow(fd, action)
222 	int fd, action;
223 {
224 	struct termios term;
225 	u_char c;
226 
227 	switch (action) {
228 	case TCOOFF:
229 		return (ioctl(fd, TIOCSTOP, 0));
230 	case TCOON:
231 		return (ioctl(fd, TIOCSTART, 0));
232 	case TCION:
233 	case TCIOFF:
234 		if (tcgetattr(fd, &term) == -1)
235 			return (-1);
236 		c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
237 		if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1)
238 			return (-1);
239 		return (0);
240 	default:
241 		errno = EINVAL;
242 		return (-1);
243 	}
244 	/* NOTREACHED */
245 }
246