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