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