key.c revision 1556
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 35static char sccsid[] = "@(#)key.c 8.3 (Berkeley) 4/2/94"; 36#endif /* not lint */ 37 38#include <sys/types.h> 39 40#include <err.h> 41#include <errno.h> 42#include <stdlib.h> 43#include <stdio.h> 44#include <string.h> 45 46#include "stty.h" 47#include "extern.h" 48 49__BEGIN_DECLS 50void f_all __P((struct info *)); 51void f_cbreak __P((struct info *)); 52void f_columns __P((struct info *)); 53void f_dec __P((struct info *)); 54void f_everything __P((struct info *)); 55void f_extproc __P((struct info *)); 56void f_ispeed __P((struct info *)); 57void f_nl __P((struct info *)); 58void f_ospeed __P((struct info *)); 59void f_raw __P((struct info *)); 60void f_rows __P((struct info *)); 61void f_sane __P((struct info *)); 62void f_size __P((struct info *)); 63void f_speed __P((struct info *)); 64void f_tty __P((struct info *)); 65__END_DECLS 66 67static struct key { 68 char *name; /* name */ 69 void (*f) __P((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 { "everything", f_everything, 0 }, 81 { "extproc", f_extproc, F_OFFOK }, 82 { "ispeed", f_ispeed, F_NEEDARG }, 83 { "new", f_tty, 0 }, 84 { "nl", f_nl, F_OFFOK }, 85 { "old", f_tty, 0 }, 86 { "ospeed", f_ospeed, F_NEEDARG }, 87 { "raw", f_raw, F_OFFOK }, 88 { "rows", f_rows, F_NEEDARG }, 89 { "sane", f_sane, 0 }, 90 { "size", f_size, 0 }, 91 { "speed", f_speed, 0 }, 92 { "tty", f_tty, 0 }, 93}; 94 95static int 96c_key(a, b) 97 const void *a, *b; 98{ 99 100 return (strcmp(((struct key *)a)->name, ((struct key *)b)->name)); 101} 102 103int 104ksearch(argvp, ip) 105 char ***argvp; 106 struct info *ip; 107{ 108 char *name; 109 struct key *kp, tmp; 110 111 name = **argvp; 112 if (*name == '-') { 113 ip->off = 1; 114 ++name; 115 } else 116 ip->off = 0; 117 118 tmp.name = name; 119 if (!(kp = (struct key *)bsearch(&tmp, keys, 120 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key))) 121 return (0); 122 if (!(kp->flags & F_OFFOK) && ip->off) { 123 errx(1, "illegal option -- %s", name); 124 usage(); 125 } 126 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) { 127 errx(1, "option requires an argument -- %s", name); 128 usage(); 129 } 130 kp->f(ip); 131 return (1); 132} 133 134void 135f_all(ip) 136 struct info *ip; 137{ 138 print(&ip->t, &ip->win, ip->ldisc, BSD); 139} 140 141void 142f_cbreak(ip) 143 struct info *ip; 144{ 145 146 if (ip->off) 147 f_sane(ip); 148 else { 149 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL; 150 ip->t.c_oflag |= OPOST; 151 ip->t.c_lflag |= ISIG|IEXTEN; 152 ip->t.c_lflag &= ~ICANON; 153 ip->set = 1; 154 } 155} 156 157void 158f_columns(ip) 159 struct info *ip; 160{ 161 162 ip->win.ws_col = atoi(ip->arg); 163 ip->wset = 1; 164} 165 166void 167f_dec(ip) 168 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 180void 181f_everything(ip) 182 struct info *ip; 183{ 184 185 print(&ip->t, &ip->win, ip->ldisc, BSD); 186} 187 188void 189f_extproc(ip) 190 struct info *ip; 191{ 192 193 if (ip->set) { 194 int tmp = 1; 195 (void)ioctl(ip->fd, TIOCEXT, &tmp); 196 } else { 197 int tmp = 0; 198 (void)ioctl(ip->fd, TIOCEXT, &tmp); 199 } 200} 201 202void 203f_ispeed(ip) 204 struct info *ip; 205{ 206 207 cfsetispeed(&ip->t, atoi(ip->arg)); 208 ip->set = 1; 209} 210 211void 212f_nl(ip) 213 struct info *ip; 214{ 215 216 if (ip->off) { 217 ip->t.c_iflag |= ICRNL; 218 ip->t.c_oflag |= ONLCR; 219 } else { 220 ip->t.c_iflag &= ~ICRNL; 221 ip->t.c_oflag &= ~ONLCR; 222 } 223 ip->set = 1; 224} 225 226void 227f_ospeed(ip) 228 struct info *ip; 229{ 230 231 cfsetospeed(&ip->t, atoi(ip->arg)); 232 ip->set = 1; 233} 234 235void 236f_raw(ip) 237 struct info *ip; 238{ 239 240 if (ip->off) 241 f_sane(ip); 242 else { 243 cfmakeraw(&ip->t); 244 ip->t.c_cflag &= ~(CSIZE|PARENB); 245 ip->t.c_cflag |= CS8; 246 ip->set = 1; 247 } 248} 249 250void 251f_rows(ip) 252 struct info *ip; 253{ 254 255 ip->win.ws_row = atoi(ip->arg); 256 ip->wset = 1; 257} 258 259void 260f_sane(ip) 261 struct info *ip; 262{ 263 264 ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL); 265 ip->t.c_iflag = TTYDEF_IFLAG; 266 ip->t.c_iflag |= ICRNL; 267 /* preserve user-preference flags in lflag */ 268#define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH) 269 ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP); 270 ip->t.c_oflag = TTYDEF_OFLAG; 271 ip->set = 1; 272} 273 274void 275f_size(ip) 276 struct info *ip; 277{ 278 279 (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col); 280} 281 282void 283f_speed(ip) 284 struct info *ip; 285{ 286 287 (void)printf("%d\n", cfgetospeed(&ip->t)); 288} 289 290void 291f_tty(ip) 292 struct info *ip; 293{ 294 int tmp; 295 296 tmp = TTYDISC; 297 if (ioctl(0, TIOCSETD, &tmp) < 0) 298 err(1, "TIOCSETD"); 299} 300