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