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