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