Drop main() prototype. Syncs with NetBSD-8
[minix.git] / bin / stty / key.c
blob0094d3b354c45f63ac4558f4d9643e5fbd09b80e
1 /* $NetBSD: key.c,v 1.22 2017/01/10 20:44:05 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.22 2017/01/10 20:44:05 christos Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/types.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <time.h>
47 #include <paths.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
53 #include "stty.h"
54 #include "extern.h"
56 __BEGIN_DECLS
57 void f_all(struct info *);
58 void f_cbreak(struct info *);
59 void f_columns(struct info *);
60 void f_dec(struct info *);
61 void f_everything(struct info *);
62 void f_extproc(struct info *);
63 void f_insane(struct info *);
64 void f_ispeed(struct info *);
65 void f_nl(struct info *);
66 void f_ospeed(struct info *);
67 void f_raw(struct info *);
68 void f_rows(struct info *);
69 void f_sane(struct info *);
70 void f_size(struct info *);
71 void f_speed(struct info *);
72 void f_ostart(struct info *);
73 void f_ostop(struct info *);
74 void f_tty(struct info *);
75 __END_DECLS
77 static const struct key {
78 const char *name; /* name */
79 void (*f)(struct info *); /* function */
80 #define F_NEEDARG 0x01 /* needs an argument */
81 #define F_OFFOK 0x02 /* can turn off */
82 int flags;
83 } keys[] = {
84 { "all", f_all, 0 },
85 { "cbreak", f_cbreak, F_OFFOK },
86 { "cols", f_columns, F_NEEDARG },
87 { "columns", f_columns, F_NEEDARG },
88 { "cooked", f_sane, 0 },
89 { "dec", f_dec, 0 },
90 { "everything", f_everything, 0 },
91 { "extproc", f_extproc, F_OFFOK },
92 { "insane", f_insane, 0 },
93 { "ispeed", f_ispeed, F_NEEDARG },
94 { "new", f_tty, 0 },
95 { "nl", f_nl, F_OFFOK },
96 { "old", f_tty, 0 },
97 { "ospeed", f_ospeed, F_NEEDARG },
98 { "ostart", f_ostart, 0 },
99 { "ostop", f_ostop, 0 },
100 { "raw", f_raw, F_OFFOK },
101 { "rows", f_rows, F_NEEDARG },
102 { "sane", f_sane, 0 },
103 { "size", f_size, 0 },
104 { "speed", f_speed, 0 },
105 { "tty", f_tty, 0 },
108 static int c_key(const void *, const void *);
110 static int
111 c_key(const void *a, const void *b)
114 return (strcmp(((const struct key *)a)->name,
115 ((const struct key *)b)->name));
119 ksearch(char ***argvp, struct info *ip)
121 char *name;
122 struct key *kp, tmp;
124 name = **argvp;
125 if (*name == '-') {
126 ip->off = 1;
127 ++name;
128 } else
129 ip->off = 0;
131 tmp.name = name;
132 if (!(kp = (struct key *)bsearch(&tmp, keys,
133 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
134 return (0);
135 if (!(kp->flags & F_OFFOK) && ip->off) {
136 warnx("illegal option -- %s", name);
137 usage();
139 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
140 warnx("option requires an argument -- %s", name);
141 usage();
143 kp->f(ip);
144 return (1);
147 void
148 f_all(struct info *ip)
150 print(&ip->t, &ip->win, ip->queue, ip->ldisc, STTY_BSD);
153 void
154 f_cbreak(struct info *ip)
156 if (ip->off)
157 f_sane(ip);
158 else {
159 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
160 ip->t.c_oflag |= OPOST;
161 ip->t.c_lflag |= ISIG|IEXTEN;
162 ip->t.c_lflag &= ~ICANON;
163 ip->set = 1;
167 void
168 f_columns(struct info *ip)
170 ip->win.ws_col = atoi(ip->arg);
171 ip->wset = 1;
174 void
175 f_dec(struct info *ip)
177 ip->t.c_cc[VERASE] = (u_char)0177;
178 ip->t.c_cc[VKILL] = CTRL('u');
179 ip->t.c_cc[VINTR] = CTRL('c');
180 ip->t.c_lflag &= ~ECHOPRT;
181 ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
182 ip->t.c_iflag &= ~IXANY;
183 ip->set = 1;
186 void
187 f_everything(struct info *ip)
189 print(&ip->t, &ip->win, ip->queue, ip->ldisc, STTY_BSD);
192 void
193 f_extproc(struct info *ip)
195 #ifdef TIOCEXT
196 if (ip->off) {
197 int tmp = 0;
198 (void)ioctl(ip->fd, TIOCEXT, &tmp);
199 } else {
200 int tmp = 1;
201 (void)ioctl(ip->fd, TIOCEXT, &tmp);
203 ip->set = 1;
204 #endif
207 void
208 f_insane(struct info *ip)
210 int f, r;
212 r = f = open(_PATH_URANDOM, O_RDONLY, 0);
213 if (f >= 0) {
214 r = read(f, &(ip->t), sizeof(struct termios));
215 close(f);
217 if (r < 0) {
218 /* XXX not cryptographically secure! */
220 srandom(time(NULL));
221 ip->t.c_iflag = random();
222 ip->t.c_oflag = random();
223 ip->t.c_cflag = random();
224 ip->t.c_lflag = random();
225 for (f = 0; f < NCCS; f++) {
226 ip->t.c_cc[f] = random() & 0xFF;
228 ip->t.c_ispeed = random();
229 ip->t.c_ospeed = random();
232 ip->set = 1;
235 void
236 f_ispeed(struct info *ip)
238 cfsetispeed(&ip->t, atoi(ip->arg));
239 ip->set = 1;
242 void
243 f_nl(struct info *ip)
245 if (ip->off) {
246 ip->t.c_iflag |= ICRNL;
247 ip->t.c_oflag |= ONLCR;
248 } else {
249 ip->t.c_iflag &= ~ICRNL;
250 ip->t.c_oflag &= ~ONLCR;
252 ip->set = 1;
255 void
256 f_ospeed(struct info *ip)
258 cfsetospeed(&ip->t, atoi(ip->arg));
259 ip->set = 1;
262 void
263 f_raw(struct info *ip)
265 if (ip->off)
266 f_sane(ip);
267 else {
268 cfmakeraw(&ip->t);
269 ip->t.c_cflag &= ~(CSIZE|PARENB);
270 ip->t.c_cflag |= CS8;
271 ip->set = 1;
275 void
276 f_rows(struct info *ip)
278 ip->win.ws_row = atoi(ip->arg);
279 ip->wset = 1;
282 void
283 f_sane(struct info *ip)
285 ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS|CDTRCTS));
286 ip->t.c_iflag = TTYDEF_IFLAG;
287 ip->t.c_iflag |= ICRNL;
288 /* preserve user-preference flags in lflag */
289 #define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
290 ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
291 ip->t.c_oflag = TTYDEF_OFLAG;
292 ip->set = 1;
295 void
296 f_size(struct info *ip)
298 (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
301 void
302 f_speed(struct info *ip)
304 (void)printf("%d\n", cfgetospeed(&ip->t));
307 /* ARGSUSED */
308 void
309 f_tty(struct info *ip)
311 #ifdef TTYDISC
312 int tmp;
314 tmp = TTYDISC;
315 if (ioctl(0, TIOCSETD, &tmp) < 0)
316 err(1, "TIOCSETD");
317 #endif
320 void
321 f_ostart(struct info *ip)
323 if (ioctl (0, TIOCSTART) < 0)
324 err(1, "TIOCSTART");
327 void
328 f_ostop(struct info *ip)
330 if (ioctl (0, TIOCSTOP) < 0)
331 err(1, "TIOCSTOP");