Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / gen / termios.c
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  * $FreeBSD: src/lib/libc/gen/termios.c,v 1.9.2.1 2000/03/18 23:13:25 jasone Exp $
34  */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)termios.c   8.2 (Berkeley) 2/21/94";
38 #endif /* LIBC_SCCS and not lint */
39
40 #include <sys/types.h>
41 #include <sys/fcntl.h>
42 #include <sys/ioctl.h>
43 #include <sys/time.h>
44
45 #include <errno.h>
46 #include <termios.h>
47 #include <unistd.h>
48
49 int
50 tcgetattr(fd, t)
51         int fd;
52         struct termios *t;
53 {
54
55         return (ioctl(fd, TIOCGETA, t));
56 }
57
58 int
59 tcsetattr(fd, opt, t)
60         int fd, opt;
61         const struct termios *t;
62 {
63         struct termios localterm;
64
65         if (opt & TCSASOFT) {
66                 localterm = *t;
67                 localterm.c_cflag |= CIGNORE;
68                 t = &localterm;
69         }
70         switch (opt & ~TCSASOFT) {
71         case TCSANOW:
72                 return (ioctl(fd, TIOCSETA, t));
73         case TCSADRAIN:
74                 return (ioctl(fd, TIOCSETAW, t));
75         case TCSAFLUSH:
76                 return (ioctl(fd, TIOCSETAF, t));
77         default:
78                 errno = EINVAL;
79                 return (-1);
80         }
81 }
82
83 int
84 #if __STDC__
85 tcsetpgrp(int fd, pid_t pgrp)
86 #else
87 tcsetpgrp(fd, pgrp)
88         int fd;
89         pid_t pgrp;
90 #endif
91 {
92         int s;
93
94         s = pgrp;
95         return (ioctl(fd, TIOCSPGRP, &s));
96 }
97
98 pid_t
99 tcgetpgrp(fd)
100         int fd;
101 {
102         int s;
103
104         if (ioctl(fd, TIOCGPGRP, &s) < 0)
105                 return ((pid_t)-1);
106
107         return ((pid_t)s);
108 }
109
110 speed_t
111 cfgetospeed(t)
112         const struct termios *t;
113 {
114
115         return (t->c_ospeed);
116 }
117
118 speed_t
119 cfgetispeed(t)
120         const struct termios *t;
121 {
122
123         return (t->c_ispeed);
124 }
125
126 int
127 cfsetospeed(t, speed)
128         struct termios *t;
129         speed_t speed;
130 {
131
132         t->c_ospeed = speed;
133         return (0);
134 }
135
136 int
137 cfsetispeed(t, speed)
138         struct termios *t;
139         speed_t speed;
140 {
141
142         t->c_ispeed = speed;
143         return (0);
144 }
145
146 int
147 cfsetspeed(t, speed)
148         struct termios *t;
149         speed_t speed;
150 {
151
152         t->c_ispeed = t->c_ospeed = speed;
153         return (0);
154 }
155
156 /*
157  * Make a pre-existing termios structure into "raw" mode: character-at-a-time
158  * mode with no characters interpreted, 8-bit data path.
159  */
160 void
161 cfmakeraw(t)
162         struct termios *t;
163 {
164
165         t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
166         t->c_iflag |= IGNBRK;
167         t->c_oflag &= ~OPOST;
168         t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP|PENDIN);
169         t->c_cflag &= ~(CSIZE|PARENB);
170         t->c_cflag |= CS8|CREAD;
171         t->c_cc[VMIN] = 1;
172         t->c_cc[VTIME] = 0;
173 }
174
175 int
176 tcsendbreak(fd, len)
177         int fd, len;
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(fd)
193         int fd;
194 {
195         return (ioctl(fd, TIOCDRAIN, 0));
196 }
197
198 #ifndef _THREAD_SAFE
199 __weak_reference(__tcdrain, tcdrain);
200 #endif
201
202 int
203 tcflush(fd, which)
204         int fd, which;
205 {
206         int com;
207
208         switch (which) {
209         case TCIFLUSH:
210                 com = FREAD;
211                 break;
212         case TCOFLUSH:
213                 com = FWRITE;
214                 break;
215         case TCIOFLUSH:
216                 com = FREAD | FWRITE;
217                 break;
218         default:
219                 errno = EINVAL;
220                 return (-1);
221         }
222         return (ioctl(fd, TIOCFLUSH, &com));
223 }
224
225 int
226 tcflow(fd, action)
227         int fd, action;
228 {
229         struct termios term;
230         u_char c;
231
232         switch (action) {
233         case TCOOFF:
234                 return (ioctl(fd, TIOCSTOP, 0));
235         case TCOON:
236                 return (ioctl(fd, TIOCSTART, 0));
237         case TCION:
238         case TCIOFF:
239                 if (tcgetattr(fd, &term) == -1)
240                         return (-1);
241                 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
242                 if (c != _POSIX_VDISABLE && _write(fd, &c, sizeof(c)) == -1)
243                         return (-1);
244                 return (0);
245         default:
246                 errno = EINVAL;
247                 return (-1);
248         }
249         /* NOTREACHED */
250 }