1 /*-
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)termios.c 8.2 (Berkeley) 02/21/94";
10 #endif /* LIBC_SCCS and not lint */
11
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 #include <sys/tty.h>
15 #include <sys/time.h>
16 #define KERNEL /* XXX - FREAD and FWRITE ifdef'd KERNEL*/
17 #include <sys/fcntl.h>
18 #undef KERNEL
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <termios.h>
23 #include <unistd.h>
24
25 int
tcgetattr(fd,t)26 tcgetattr(fd, t)
27 int fd;
28 struct termios *t;
29 {
30
31 return (ioctl(fd, TIOCGETA, t));
32 }
33
34 int
tcsetattr(fd,opt,t)35 tcsetattr(fd, opt, t)
36 int fd, opt;
37 const struct termios *t;
38 {
39 struct termios localterm;
40
41 if (opt & TCSASOFT) {
42 localterm = *t;
43 localterm.c_cflag |= CIGNORE;
44 t = &localterm;
45 }
46 switch (opt & ~TCSASOFT) {
47 case TCSANOW:
48 return (ioctl(fd, TIOCSETA, t));
49 case TCSADRAIN:
50 return (ioctl(fd, TIOCSETAW, t));
51 case TCSAFLUSH:
52 return (ioctl(fd, TIOCSETAF, t));
53 default:
54 errno = EINVAL;
55 return (-1);
56 }
57 }
58
59 int
60 #if __STDC__
tcsetpgrp(int fd,pid_t pgrp)61 tcsetpgrp(int fd, pid_t pgrp)
62 #else
63 tcsetpgrp(fd, pgrp)
64 int fd;
65 pid_t pgrp;
66 #endif
67 {
68 int s;
69
70 s = pgrp;
71 return (ioctl(fd, TIOCSPGRP, &s));
72 }
73
74 pid_t
tcgetpgrp(fd)75 tcgetpgrp(fd)
76 {
77 int s;
78
79 if (ioctl(fd, TIOCGPGRP, &s) < 0)
80 return ((pid_t)-1);
81
82 return ((pid_t)s);
83 }
84
85 speed_t
cfgetospeed(t)86 cfgetospeed(t)
87 const struct termios *t;
88 {
89
90 return (t->c_ospeed);
91 }
92
93 speed_t
cfgetispeed(t)94 cfgetispeed(t)
95 const struct termios *t;
96 {
97
98 return (t->c_ispeed);
99 }
100
101 int
cfsetospeed(t,speed)102 cfsetospeed(t, speed)
103 struct termios *t;
104 speed_t speed;
105 {
106
107 t->c_ospeed = speed;
108 return (0);
109 }
110
111 int
cfsetispeed(t,speed)112 cfsetispeed(t, speed)
113 struct termios *t;
114 speed_t speed;
115 {
116
117 t->c_ispeed = speed;
118 return (0);
119 }
120
121 int
cfsetspeed(t,speed)122 cfsetspeed(t, speed)
123 struct termios *t;
124 speed_t speed;
125 {
126
127 t->c_ispeed = t->c_ospeed = speed;
128 return (0);
129 }
130
131 /*
132 * Make a pre-existing termios structure into "raw" mode: character-at-a-time
133 * mode with no characters interpreted, 8-bit data path.
134 */
135 void
cfmakeraw(t)136 cfmakeraw(t)
137 struct termios *t;
138 {
139
140 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
141 t->c_oflag &= ~OPOST;
142 t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
143 t->c_cflag &= ~(CSIZE|PARENB);
144 t->c_cflag |= CS8;
145 /* XXX set MIN/TIME */
146 }
147
tcsendbreak(fd,len)148 tcsendbreak(fd, len)
149 int fd, len;
150 {
151 struct timeval sleepytime;
152
153 sleepytime.tv_sec = 0;
154 sleepytime.tv_usec = 400000;
155 if (ioctl(fd, TIOCSBRK, 0) == -1)
156 return (-1);
157 (void)select(0, 0, 0, 0, &sleepytime);
158 if (ioctl(fd, TIOCCBRK, 0) == -1)
159 return (-1);
160 return (0);
161 }
162
tcdrain(fd)163 tcdrain(fd)
164 int fd;
165 {
166
167 return (ioctl(fd, TIOCDRAIN, 0));
168 }
169
tcflush(fd,which)170 tcflush(fd, which)
171 int fd, which;
172 {
173 int com;
174
175 switch (which) {
176 case TCIFLUSH:
177 com = FREAD;
178 break;
179 case TCOFLUSH:
180 com = FWRITE;
181 break;
182 case TCIOFLUSH:
183 com = FREAD | FWRITE;
184 break;
185 default:
186 errno = EINVAL;
187 return (-1);
188 }
189 return (ioctl(fd, TIOCFLUSH, &com));
190 }
191
tcflow(fd,action)192 tcflow(fd, action)
193 int fd, action;
194 {
195 struct termios term;
196 u_char c;
197
198 switch (action) {
199 case TCOOFF:
200 return (ioctl(fd, TIOCSTOP, 0));
201 case TCOON:
202 return (ioctl(fd, TIOCSTART, 0));
203 case TCION:
204 case TCIOFF:
205 if (tcgetattr(fd, &term) == -1)
206 return (-1);
207 c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
208 if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1)
209 return (-1);
210 return (0);
211 default:
212 errno = EINVAL;
213 return (-1);
214 }
215 /* NOTREACHED */
216 }
217