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