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