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