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