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