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