xref: /csrg-svn/lib/libc/gen/termios.c (revision 52295)
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.10 (Berkeley) 02/03/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 TIOCSETAF:
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 	t->c_ospeed = speed;
105 	return (0);
106 }
107 
108 int
109 cfsetispeed(t, speed)
110 	struct termios *t;
111 	speed_t speed;
112 {
113 	t->c_ispeed = speed;
114 	return (0);
115 }
116 
117 int
118 cfsetspeed(t, speed)
119 	struct termios *t;
120 	speed_t speed;
121 {
122 	t->c_ispeed = t->c_ospeed = speed;
123 	return (0);
124 }
125 
126 /*
127  * Make a pre-existing termios structure into "raw" mode: character-at-a-time
128  * mode with no characters interpreted, 8-bit data path.
129  */
130 void
131 cfmakeraw(t)
132 	struct termios *t;
133 {
134 	t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
135 	t->c_oflag &= ~OPOST;
136 	t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
137 	t->c_cflag &= ~(CSIZE|PARENB);
138 	t->c_cflag |= CS8;
139 	/* XXX set MIN/TIME */
140 }
141 
142 tcsendbreak(fd, len)
143 	int fd, len;
144 {
145 	struct timeval sleepytime;
146 
147 	sleepytime.tv_sec = 0;
148 	sleepytime.tv_usec = 400000;
149 	if (ioctl(fd, TIOCSBRK, 0) == -1)
150 		return (-1);
151 	(void)select(0, 0, 0, 0, &sleepytime);
152 	if (ioctl(fd, TIOCCBRK, 0) == -1)
153 		return (-1);
154 	return (0);
155 }
156 
157 tcdrain(fd)
158 	int fd;
159 {
160 	return (ioctl(fd, TIOCDRAIN, 0) == -1 ? -1 : 0);
161 }
162 
163 tcflush(fd, which)
164 	int fd, which;
165 {
166 	int com;
167 
168 	switch (which) {
169 	case TCIFLUSH:
170 		com = FREAD;
171 		break;
172 	case TCOFLUSH:
173 		com = FWRITE;
174 		break;
175 	case TCIOFLUSH:
176 		com = FREAD | FWRITE;
177 		break;
178 	default:
179 		errno = EINVAL;
180 		return (-1);
181 	}
182 	return (ioctl(fd, TIOCFLUSH, &com) == -1 ? -1 : 0);
183 }
184 
185 tcflow(fd, action)
186 	int fd, action;
187 {
188 	struct termios term;
189 	u_char c;
190 
191 	switch (action) {
192 	case TCOOFF:
193 		return (ioctl(fd, TIOCSTOP, 0) == -1 ? -1 : 0);
194 	case TCOON:
195 		return (ioctl(fd, TIOCSTART, 0) == -1 ? -1 : 0);
196 	case TCION:
197 	case TCIOFF:
198 		if (tcgetattr(fd, &term) == -1)
199 			return (-1);
200 		c = term.c_cc[action == TCIOFF ? VSTOP : VSTART];
201 		if (c != _POSIX_VDISABLE && write(fd, &c, sizeof(c)) == -1)
202 			return (-1);
203 		return (0);
204 	default:
205 		errno = EINVAL;
206 		return (-1);
207 	}
208 	/* NOTREACHED */
209 }
210