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