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.9 (Berkeley) 05/20/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 int s; 62 63 s = pgrp; 64 return(ioctl(fd, TIOCSPGRP, &s)); 65 } 66 67 pid_t 68 tcgetpgrp(fd) 69 { 70 int s; 71 72 if (ioctl(fd, TIOCGPGRP, &s) < 0) 73 return((pid_t)-1); 74 75 return((pid_t)s); 76 } 77 78 speed_t 79 cfgetospeed(t) 80 const struct termios *t; 81 { 82 83 return(t->c_ospeed); 84 } 85 86 speed_t 87 cfgetispeed(t) 88 const struct termios *t; 89 { 90 91 return(t->c_ispeed); 92 } 93 94 int 95 cfsetospeed(t, speed) 96 struct termios *t; 97 speed_t speed; 98 { 99 t->c_ospeed = speed; 100 101 return (0); 102 } 103 104 int 105 cfsetispeed(t, speed) 106 struct termios *t; 107 speed_t speed; 108 { 109 t->c_ispeed = speed; 110 111 return (0); 112 } 113 114 void 115 cfsetspeed(t, speed) 116 struct termios *t; 117 speed_t speed; 118 { 119 t->c_ispeed = t->c_ospeed = speed; 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 unsigned 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