1 /*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)key.c 8.3 (Berkeley) 4/2/94"; 37 #else 38 static const char rcsid[] = 39 "$FreeBSD$"; 40 #endif 41 #endif /* not lint */ 42 43 #include <sys/types.h> 44 45 #include <err.h> 46 #include <errno.h> 47 #include <stdlib.h> 48 #include <stdio.h> 49 #include <string.h> 50 51 #include "stty.h" 52 #include "extern.h" 53 54 __BEGIN_DECLS 55 static int c_key __P((const void *, const void *)); 56 void f_all __P((struct info *)); 57 void f_cbreak __P((struct info *)); 58 void f_columns __P((struct info *)); 59 void f_dec __P((struct info *)); 60 void f_ek __P((struct info *)); 61 void f_everything __P((struct info *)); 62 void f_extproc __P((struct info *)); 63 void f_ispeed __P((struct info *)); 64 void f_nl __P((struct info *)); 65 void f_ospeed __P((struct info *)); 66 void f_raw __P((struct info *)); 67 void f_rows __P((struct info *)); 68 void f_sane __P((struct info *)); 69 void f_size __P((struct info *)); 70 void f_speed __P((struct info *)); 71 void f_tty __P((struct info *)); 72 __END_DECLS 73 74 static struct key { 75 const char *name; /* name */ 76 void (*f) __P((struct info *)); /* function */ 77 #define F_NEEDARG 0x01 /* needs an argument */ 78 #define F_OFFOK 0x02 /* can turn off */ 79 int flags; 80 } keys[] = { 81 { "all", f_all, 0 }, 82 { "cbreak", f_cbreak, F_OFFOK }, 83 { "cols", f_columns, F_NEEDARG }, 84 { "columns", f_columns, F_NEEDARG }, 85 { "cooked", f_sane, 0 }, 86 { "dec", f_dec, 0 }, 87 { "ek", f_ek, 0 }, 88 { "everything", f_everything, 0 }, 89 { "extproc", f_extproc, F_OFFOK }, 90 { "ispeed", f_ispeed, F_NEEDARG }, 91 { "new", f_tty, 0 }, 92 { "nl", f_nl, F_OFFOK }, 93 { "old", f_tty, 0 }, 94 { "ospeed", f_ospeed, F_NEEDARG }, 95 { "raw", f_raw, F_OFFOK }, 96 { "rows", f_rows, F_NEEDARG }, 97 { "sane", f_sane, 0 }, 98 { "size", f_size, 0 }, 99 { "speed", f_speed, 0 }, 100 { "tty", f_tty, 0 }, 101 }; 102 103 static int 104 c_key(a, b) 105 const void *a, *b; 106 { 107 108 return (strcmp(((const struct key *)a)->name, ((const struct key *)b)->name)); 109 } 110 111 int 112 ksearch(argvp, ip) 113 char ***argvp; 114 struct info *ip; 115 { 116 char *name; 117 struct key *kp, tmp; 118 119 name = **argvp; 120 if (*name == '-') { 121 ip->off = 1; 122 ++name; 123 } else 124 ip->off = 0; 125 126 tmp.name = name; 127 if (!(kp = (struct key *)bsearch(&tmp, keys, 128 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key))) 129 return (0); 130 if (!(kp->flags & F_OFFOK) && ip->off) { 131 warnx("illegal option -- -%s", name); 132 usage(); 133 } 134 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) { 135 warnx("option requires an argument -- %s", name); 136 usage(); 137 } 138 kp->f(ip); 139 return (1); 140 } 141 142 void 143 f_all(ip) 144 struct info *ip; 145 { 146 print(&ip->t, &ip->win, ip->ldisc, BSD); 147 } 148 149 void 150 f_cbreak(ip) 151 struct info *ip; 152 { 153 154 if (ip->off) 155 f_sane(ip); 156 else { 157 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL; 158 ip->t.c_oflag |= OPOST; 159 ip->t.c_lflag |= ISIG|IEXTEN; 160 ip->t.c_lflag &= ~ICANON; 161 ip->set = 1; 162 } 163 } 164 165 void 166 f_columns(ip) 167 struct info *ip; 168 { 169 170 ip->win.ws_col = atoi(ip->arg); 171 ip->wset = 1; 172 } 173 174 void 175 f_dec(ip) 176 struct info *ip; 177 { 178 179 ip->t.c_cc[VERASE] = (u_char)0177; 180 ip->t.c_cc[VKILL] = CTRL('u'); 181 ip->t.c_cc[VINTR] = CTRL('c'); 182 ip->t.c_lflag &= ~ECHOPRT; 183 ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL; 184 ip->t.c_iflag &= ~IXANY; 185 ip->set = 1; 186 } 187 188 void 189 f_ek(ip) 190 struct info *ip; 191 { 192 193 ip->t.c_cc[VERASE] = CERASE; 194 ip->t.c_cc[VKILL] = CKILL; 195 ip->set = 1; 196 } 197 198 void 199 f_everything(ip) 200 struct info *ip; 201 { 202 203 print(&ip->t, &ip->win, ip->ldisc, BSD); 204 } 205 206 void 207 f_extproc(ip) 208 struct info *ip; 209 { 210 211 if (ip->off) { 212 int tmp = 0; 213 (void)ioctl(ip->fd, TIOCEXT, &tmp); 214 } else { 215 int tmp = 1; 216 (void)ioctl(ip->fd, TIOCEXT, &tmp); 217 } 218 } 219 220 void 221 f_ispeed(ip) 222 struct info *ip; 223 { 224 225 cfsetispeed(&ip->t, (speed_t)atoi(ip->arg)); 226 ip->set = 1; 227 } 228 229 void 230 f_nl(ip) 231 struct info *ip; 232 { 233 234 if (ip->off) { 235 ip->t.c_iflag |= ICRNL; 236 ip->t.c_oflag |= ONLCR; 237 } else { 238 ip->t.c_iflag &= ~ICRNL; 239 ip->t.c_oflag &= ~ONLCR; 240 } 241 ip->set = 1; 242 } 243 244 void 245 f_ospeed(ip) 246 struct info *ip; 247 { 248 249 cfsetospeed(&ip->t, (speed_t)atoi(ip->arg)); 250 ip->set = 1; 251 } 252 253 void 254 f_raw(ip) 255 struct info *ip; 256 { 257 258 if (ip->off) 259 f_sane(ip); 260 else { 261 cfmakeraw(&ip->t); 262 ip->t.c_cflag &= ~(CSIZE|PARENB); 263 ip->t.c_cflag |= CS8; 264 ip->set = 1; 265 } 266 } 267 268 void 269 f_rows(ip) 270 struct info *ip; 271 { 272 273 ip->win.ws_row = atoi(ip->arg); 274 ip->wset = 1; 275 } 276 277 void 278 f_sane(ip) 279 struct info *ip; 280 { 281 282 ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL); 283 ip->t.c_iflag = TTYDEF_IFLAG; 284 ip->t.c_iflag |= ICRNL; 285 /* preserve user-preference flags in lflag */ 286 #define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH) 287 ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP); 288 ip->t.c_oflag = TTYDEF_OFLAG; 289 ip->set = 1; 290 } 291 292 void 293 f_size(ip) 294 struct info *ip; 295 { 296 297 (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col); 298 } 299 300 void 301 f_speed(ip) 302 struct info *ip; 303 { 304 305 (void)printf("%lu\n", (u_long)cfgetospeed(&ip->t)); 306 } 307 308 void 309 f_tty(ip) 310 struct info *ip; 311 { 312 int tmp; 313 314 tmp = TTYDISC; 315 if (ioctl(ip->fd, TIOCSETD, &tmp) < 0) 316 err(1, "TIOCSETD"); 317 } 318