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