xref: /original-bsd/lib/libc/gen/termios.c (revision de3f5c4e)
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.8 (Berkeley) 05/06/91";
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 was 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 		opt &= ~TCSASOFT;
44 	}
45 	if (opt == TCSANOW)
46 		return (ioctl(fd, TIOCSETA, t));
47 	else if (opt == TCSADRAIN)
48 		return (ioctl(fd, TIOCSETAW, t));
49 	return (ioctl(fd, TIOCSETAF, t));
50 }
51 
52 int
53 #if __STDC__
54 tcsetpgrp(int fd, pid_t pgrp)
55 #else
56 tcsetpgrp(fd, pgrp)
57 	int fd;
58 	pid_t pgrp;
59 #endif
60 {
61 
62 	return(ioctl(fd, TIOCSPGRP, &pgrp));
63 }
64 
65 pid_t
66 tcgetpgrp(fd)
67 {
68 	int s;
69 
70 	if (ioctl(fd, TIOCGPGRP, &s) < 0)
71 		return((pid_t)-1);
72 
73 	return((pid_t)s);
74 }
75 
76 speed_t
77 cfgetospeed(t)
78 	const struct termios *t;
79 {
80 
81 	return(t->c_ospeed);
82 }
83 
84 speed_t
85 cfgetispeed(t)
86 	const struct termios *t;
87 {
88 
89 	return(t->c_ispeed);
90 }
91 
92 int
93 cfsetospeed(t, speed)
94 	struct termios *t;
95 	speed_t speed;
96 {
97 	t->c_ospeed = speed;
98 
99 	return (0);
100 }
101 
102 int
103 cfsetispeed(t, speed)
104 	struct termios *t;
105 	speed_t speed;
106 {
107 	t->c_ispeed = speed;
108 
109 	return (0);
110 }
111 
112 void
113 cfsetspeed(t, speed)
114 	struct termios *t;
115 	speed_t speed;
116 {
117 	t->c_ispeed = t->c_ospeed = speed;
118 }
119 
120 /*
121  * Make a pre-existing termios structure into "raw" mode:
122  * character-at-a-time mode with no characters interpreted,
123  * 8-bit data path.
124  */
125 void
126 cfmakeraw(t)
127 	struct termios *t;
128 {
129 	t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
130 	t->c_oflag &= ~OPOST;
131 	t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
132 	t->c_cflag &= ~(CSIZE|PARENB);
133 	t->c_cflag |= CS8;
134 	/* set MIN/TIME */
135 }
136 
137 tcsendbreak(fd, len)
138 	int fd, len;
139 {
140 	struct timeval sleepytime;
141 
142 	sleepytime.tv_sec = 0;
143 	sleepytime.tv_usec = 400000;
144 	if (ioctl(fd, TIOCSBRK, 0) == -1)
145 		return (-1);
146 	select(0, 0, 0, 0, &sleepytime);
147 	if (ioctl(fd, TIOCCBRK, 0) == -1)
148 		return (-1);
149 
150 	return (0);
151 }
152 
153 tcdrain(fd)
154 	int fd;
155 {
156 	if (ioctl(fd, TIOCDRAIN, 0) == -1)
157 		return (-1);
158 
159 	return (0);
160 }
161 
162 tcflush(fd, which)
163 	int fd, which;
164 {
165 	int com;
166 
167 	switch (which) {
168 	case TCIFLUSH:
169 		com = FREAD;
170 		break;
171 	case TCOFLUSH:
172 		com = FWRITE;
173 		break;
174 	case TCIOFLUSH:
175 		com = FREAD | FWRITE;
176 		break;
177 	default:
178 		errno = EINVAL;
179 		return (-1);
180 	}
181 	if (ioctl(fd, TIOCFLUSH, &com) == -1)
182 		return (-1);
183 
184 	return (0);
185 }
186 
187 tcflow(fd, action)
188 	int fd, action;
189 {
190 	switch (action) {
191 	case TCOOFF:
192 		return (ioctl(fd, TIOCSTOP, 0));
193 		break;
194 	case TCOON:
195 		return (ioctl(fd, TIOCSTART, 0));
196 		break;
197 	case TCIOFF:
198 	case TCION: {		/* these posix functions are STUPID */
199 		struct termios term;
200 		unsigned char c;
201 
202 		if (tcgetattr(fd, &term) == -1)
203 			return (-1);
204 		c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
205 		if (c != _POSIX_VDISABLE && write(fd, &c, 1) == -1)
206 			return (-1);
207 		break;
208 	}
209 	default:
210 		errno = EINVAL;
211 		return (-1);
212 	}
213 
214 	return (0);
215 }
216