2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)key.c 8.3 (Berkeley) 4/2/94";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
50 static int c_key(const void *, const void *);
51 void f_all(struct info *);
52 void f_cbreak(struct info *);
53 void f_columns(struct info *);
54 void f_dec(struct info *);
55 void f_ek(struct info *);
56 void f_everything(struct info *);
57 void f_extproc(struct info *);
58 void f_ispeed(struct info *);
59 void f_nl(struct info *);
60 void f_ospeed(struct info *);
61 void f_raw(struct info *);
62 void f_rows(struct info *);
63 void f_sane(struct info *);
64 void f_size(struct info *);
65 void f_speed(struct info *);
66 void f_tty(struct info *);
70 const char *name; /* name */
71 void (*f)(struct info *); /* function */
72 #define F_NEEDARG 0x01 /* needs an argument */
73 #define F_OFFOK 0x02 /* can turn off */
77 { "cbreak", f_cbreak, F_OFFOK },
78 { "cols", f_columns, F_NEEDARG },
79 { "columns", f_columns, F_NEEDARG },
80 { "cooked", f_sane, 0 },
83 { "everything", f_everything, 0 },
84 { "extproc", f_extproc, F_OFFOK },
85 { "ispeed", f_ispeed, F_NEEDARG },
87 { "nl", f_nl, F_OFFOK },
89 { "ospeed", f_ospeed, F_NEEDARG },
90 { "raw", f_raw, F_OFFOK },
91 { "rows", f_rows, F_NEEDARG },
92 { "sane", f_sane, 0 },
93 { "size", f_size, 0 },
94 { "speed", f_speed, 0 },
99 c_key(const void *a, const void *b)
102 return (strcmp(((const struct key *)a)->name, ((const struct key *)b)->name));
106 ksearch(char ***argvp, struct info *ip)
119 if (!(kp = (struct key *)bsearch(&tmp, keys,
120 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
122 if (!(kp->flags & F_OFFOK) && ip->off) {
123 warnx("illegal option -- -%s", name);
126 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
127 warnx("option requires an argument -- %s", name);
135 f_all(struct info *ip)
137 print(&ip->t, &ip->win, ip->ldisc, BSD);
141 f_cbreak(struct info *ip)
147 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
148 ip->t.c_oflag |= OPOST;
149 ip->t.c_lflag |= ISIG|IEXTEN;
150 ip->t.c_lflag &= ~ICANON;
156 f_columns(struct info *ip)
159 ip->win.ws_col = atoi(ip->arg);
164 f_dec(struct info *ip)
167 ip->t.c_cc[VERASE] = (u_char)0177;
168 ip->t.c_cc[VKILL] = CTRL('u');
169 ip->t.c_cc[VINTR] = CTRL('c');
170 ip->t.c_lflag &= ~ECHOPRT;
171 ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
172 ip->t.c_iflag &= ~IXANY;
177 f_ek(struct info *ip)
180 ip->t.c_cc[VERASE] = CERASE;
181 ip->t.c_cc[VKILL] = CKILL;
186 f_everything(struct info *ip)
189 print(&ip->t, &ip->win, ip->ldisc, BSD);
193 f_extproc(struct info *ip)
198 (void)ioctl(ip->fd, TIOCEXT, &tmp);
201 (void)ioctl(ip->fd, TIOCEXT, &tmp);
206 f_ispeed(struct info *ip)
209 cfsetispeed(&ip->t, (speed_t)atoi(ip->arg));
214 f_nl(struct info *ip)
218 ip->t.c_iflag |= ICRNL;
219 ip->t.c_oflag |= ONLCR;
221 ip->t.c_iflag &= ~ICRNL;
222 ip->t.c_oflag &= ~ONLCR;
228 f_ospeed(struct info *ip)
231 cfsetospeed(&ip->t, (speed_t)atoi(ip->arg));
236 f_raw(struct info *ip)
243 ip->t.c_cflag &= ~(CSIZE|PARENB);
244 ip->t.c_cflag |= CS8;
250 f_rows(struct info *ip)
253 ip->win.ws_row = atoi(ip->arg);
258 f_sane(struct info *ip)
263 ip->t.c_cflag = def.c_cflag | (ip->t.c_cflag & CLOCAL);
264 ip->t.c_iflag = def.c_iflag;
265 /* preserve user-preference flags in lflag */
266 #define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
267 ip->t.c_lflag = def.c_lflag | (ip->t.c_lflag & LKEEP);
268 ip->t.c_oflag = def.c_oflag;
273 f_size(struct info *ip)
276 (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
280 f_speed(struct info *ip)
283 (void)printf("%lu\n", (u_long)cfgetospeed(&ip->t));
287 f_tty(struct info *ip)
292 if (ioctl(ip->fd, TIOCSETD, &tmp) < 0)