Remove not needed void casts.
[dragonfly.git] / bin / stty / key.c
CommitLineData
984263bc
MD
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.
1de703da
MD
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 $
57fed2af 35 * $DragonFly: src/bin/stty/key.c,v 1.5 2004/11/07 20:54:52 eirikn Exp $
984263bc
MD
36 */
37
984263bc
MD
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
9dbf638f
DR
50static int c_key (const void *, const void *);
51void f_all (struct info *);
52void f_cbreak (struct info *);
53void f_columns (struct info *);
54void f_dec (struct info *);
55void f_ek (struct info *);
56void f_everything (struct info *);
57void f_extproc (struct info *);
58void f_ispeed (struct info *);
59void f_nl (struct info *);
60void f_ospeed (struct info *);
61void f_raw (struct info *);
62void f_rows (struct info *);
63void f_sane (struct info *);
64void f_size (struct info *);
65void f_speed (struct info *);
66void f_tty (struct info *);
984263bc
MD
67__END_DECLS
68
69static struct key {
70 const char *name; /* name */
9dbf638f 71 void (*f) (struct info *); /* function */
984263bc
MD
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
98static int
b5744197 99c_key(const void *a, const void *b)
984263bc
MD
100{
101
102 return (strcmp(((const struct key *)a)->name, ((const struct key *)b)->name));
103}
104
105int
b5744197 106ksearch(char ***argvp, struct info *ip)
984263bc
MD
107{
108 char *name;
109 struct key *kp, tmp;
110
111 name = **argvp;
112 if (*name == '-') {
113 ip->off = 1;
114 ++name;
115 } else
116 ip->off = 0;
117
118 tmp.name = name;
119 if (!(kp = (struct key *)bsearch(&tmp, keys,
120 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
121 return (0);
122 if (!(kp->flags & F_OFFOK) && ip->off) {
123 warnx("illegal option -- -%s", name);
124 usage();
125 }
126 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
127 warnx("option requires an argument -- %s", name);
128 usage();
129 }
130 kp->f(ip);
131 return (1);
132}
133
134void
b5744197 135f_all(struct info *ip)
984263bc
MD
136{
137 print(&ip->t, &ip->win, ip->ldisc, BSD);
138}
139
140void
b5744197 141f_cbreak(struct info *ip)
984263bc
MD
142{
143
144 if (ip->off)
145 f_sane(ip);
146 else {
147 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
148 ip->t.c_oflag |= OPOST;
149 ip->t.c_lflag |= ISIG|IEXTEN;
150 ip->t.c_lflag &= ~ICANON;
151 ip->set = 1;
152 }
153}
154
155void
b5744197 156f_columns(struct info *ip)
984263bc
MD
157{
158
159 ip->win.ws_col = atoi(ip->arg);
160 ip->wset = 1;
161}
162
163void
b5744197 164f_dec(struct info *ip)
984263bc
MD
165{
166
167 ip->t.c_cc[VERASE] = (u_char)0177;
168 ip->t.c_cc[VKILL] = CTRL('u');
169 ip->t.c_cc[VINTR] = CTRL('c');
170 ip->t.c_lflag &= ~ECHOPRT;
171 ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
172 ip->t.c_iflag &= ~IXANY;
173 ip->set = 1;
174}
175
176void
b5744197 177f_ek(struct info *ip)
984263bc
MD
178{
179
180 ip->t.c_cc[VERASE] = CERASE;
181 ip->t.c_cc[VKILL] = CKILL;
182 ip->set = 1;
183}
184
185void
b5744197 186f_everything(struct info *ip)
984263bc
MD
187{
188
189 print(&ip->t, &ip->win, ip->ldisc, BSD);
190}
191
192void
b5744197 193f_extproc(struct info *ip)
984263bc
MD
194{
195
196 if (ip->off) {
197 int tmp = 0;
57fed2af 198 ioctl(ip->fd, TIOCEXT, &tmp);
984263bc
MD
199 } else {
200 int tmp = 1;
57fed2af 201 ioctl(ip->fd, TIOCEXT, &tmp);
984263bc
MD
202 }
203}
204
205void
b5744197 206f_ispeed(struct info *ip)
984263bc
MD
207{
208
209 cfsetispeed(&ip->t, (speed_t)atoi(ip->arg));
210 ip->set = 1;
211}
212
213void
b5744197 214f_nl(struct info *ip)
984263bc
MD
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
227void
b5744197 228f_ospeed(struct info *ip)
984263bc
MD
229{
230
231 cfsetospeed(&ip->t, (speed_t)atoi(ip->arg));
232 ip->set = 1;
233}
234
235void
b5744197 236f_raw(struct info *ip)
984263bc
MD
237{
238
239 if (ip->off)
240 f_sane(ip);
241 else {
242 cfmakeraw(&ip->t);
243 ip->t.c_cflag &= ~(CSIZE|PARENB);
244 ip->t.c_cflag |= CS8;
245 ip->set = 1;
246 }
247}
248
249void
b5744197 250f_rows(struct info *ip)
984263bc
MD
251{
252
253 ip->win.ws_row = atoi(ip->arg);
254 ip->wset = 1;
255}
256
257void
b5744197 258f_sane(struct info *ip)
984263bc
MD
259{
260
261 ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL);
262 ip->t.c_iflag = TTYDEF_IFLAG;
263 ip->t.c_iflag |= ICRNL;
264 /* preserve user-preference flags in lflag */
265#define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
266 ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
267 ip->t.c_oflag = TTYDEF_OFLAG;
268 ip->set = 1;
269}
270
271void
b5744197 272f_size(struct info *ip)
984263bc
MD
273{
274
57fed2af 275 printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
984263bc
MD
276}
277
278void
b5744197 279f_speed(struct info *ip)
984263bc
MD
280{
281
57fed2af 282 printf("%lu\n", (u_long)cfgetospeed(&ip->t));
984263bc
MD
283}
284
285void
b5744197 286f_tty(struct info *ip)
984263bc
MD
287{
288 int tmp;
289
290 tmp = TTYDISC;
291 if (ioctl(ip->fd, TIOCSETD, &tmp) < 0)
292 err(1, "TIOCSETD");
293}