1 /*	$OpenBSD: key.c,v 1.12 2003/06/11 23:42:12 deraadt Exp $	*/
2 /*	$NetBSD: key.c,v 1.11 1995/09/07 06:57:11 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)key.c	8.4 (Berkeley) 2/20/95";
36 #else
37 static char rcsid[] = "$OpenBSD: key.c,v 1.12 2003/06/11 23:42:12 deraadt Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 
49 #include "stty.h"
50 #include "extern.h"
51 
52 __BEGIN_DECLS
53 void	f_all(struct info *);
54 void	f_cbreak(struct info *);
55 void	f_columns(struct info *);
56 void	f_dec(struct info *);
57 void	f_ek(struct info *);
58 void	f_everything(struct info *);
59 void	f_extproc(struct info *);
60 void	f_ispeed(struct info *);
61 void	f_lcase(struct info *);
62 void	f_nl(struct info *);
63 void	f_ospeed(struct info *);
64 void	f_raw(struct info *);
65 void	f_rows(struct info *);
66 void	f_sane(struct info *);
67 void	f_size(struct info *);
68 void	f_speed(struct info *);
69 void	f_ostart(struct info *);
70 void	f_ostop(struct info *);
71 void	f_tty(struct info *);
72 __END_DECLS
73 
74 static struct key {
75 	char *name;				/* name */
76 	void (*f)(struct info *);		/* function */
77 #define	F_NEEDARG	0x01			/* needs an argument */
78 #define	F_OFFOK		0x02			/* can turn off */
79 	int flags;
80 } keys[] = {
81 	{ "all",	f_all,		0 },
82 	{ "cbreak",	f_cbreak,	F_OFFOK },
83 	{ "cols",	f_columns,	F_NEEDARG },
84 	{ "columns",	f_columns,	F_NEEDARG },
85 	{ "cooked", 	f_sane,		0 },
86 	{ "dec",	f_dec,		0 },
87 	{ "ek",		f_ek,		0 },
88 	{ "everything",	f_everything,	0 },
89 	{ "extproc",	f_extproc,	F_OFFOK },
90 	{ "ispeed",	f_ispeed,	F_NEEDARG },
91 	{ "lcase", 	f_lcase,	0 },
92 	{ "new",	f_tty,		0 },
93 	{ "nl",		f_nl,		F_OFFOK },
94 	{ "old",	f_tty,		0 },
95 	{ "ospeed",	f_ospeed,	F_NEEDARG },
96 	{ "ostart",	f_ostart,	0 },
97 	{ "ostop",	f_ostop,	0 },
98 	{ "raw",	f_raw,		F_OFFOK },
99 	{ "rows",	f_rows,		F_NEEDARG },
100 	{ "sane",	f_sane,		0 },
101 	{ "size",	f_size,		0 },
102 	{ "speed",	f_speed,	0 },
103 	{ "tty",	f_tty,		0 },
104 };
105 
106 static int
c_key(const void * a,const void * b)107 c_key(const void *a, const void *b)
108 {
109 
110 	return (strcmp(((struct key *)a)->name, ((struct key *)b)->name));
111 }
112 
113 int
ksearch(char *** argvp,struct info * ip)114 ksearch(char ***argvp, struct info *ip)
115 {
116 	char *name;
117 	struct key *kp, tmp;
118 
119 	name = **argvp;
120 	if (*name == '-') {
121 		ip->off = 1;
122 		++name;
123 	} else
124 		ip->off = 0;
125 
126 	tmp.name = name;
127 	if (!(kp = (struct key *)bsearch(&tmp, keys,
128 	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
129 		return (0);
130 	if (!(kp->flags & F_OFFOK) && ip->off) {
131 		warnx("illegal option -- -%s", name);
132 		usage();
133 	}
134 	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
135 		warnx("option requires an argument -- %s", name);
136 		usage();
137 	}
138 	kp->f(ip);
139 	return (1);
140 }
141 
142 void
f_all(struct info * ip)143 f_all(struct info *ip)
144 {
145 	print(&ip->t, &ip->win, ip->ldisc, BSD);
146 }
147 
148 void
f_cbreak(struct info * ip)149 f_cbreak(struct info *ip)
150 {
151 
152 	if (ip->off)
153 		f_sane(ip);
154 	else {
155 		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
156 		ip->t.c_oflag |= OPOST;
157 		ip->t.c_lflag |= ISIG|IEXTEN;
158 		ip->t.c_lflag &= ~ICANON;
159 		ip->set = 1;
160 	}
161 }
162 
163 void
f_columns(struct info * ip)164 f_columns(struct info *ip)
165 {
166 
167 	ip->win.ws_col = atoi(ip->arg);
168 	ip->wset = 1;
169 }
170 
171 void
f_dec(struct info * ip)172 f_dec(struct info *ip)
173 {
174 
175 	ip->t.c_cc[VERASE] = (u_char)0177;
176 	ip->t.c_cc[VKILL] = CTRL('u');
177 	ip->t.c_cc[VINTR] = CTRL('c');
178 	ip->t.c_lflag &= ~ECHOPRT;
179 	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
180 	ip->t.c_iflag &= ~IXANY;
181 	ip->set = 1;
182 }
183 
184 void
f_ek(struct info * ip)185 f_ek(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
f_everything(struct info * ip)194 f_everything(struct info *ip)
195 {
196 
197 	print(&ip->t, &ip->win, ip->ldisc, BSD);
198 }
199 
200 void
f_extproc(struct info * ip)201 f_extproc(struct info *ip)
202 {
203 
204 	if (ip->off) {
205 		int tmp = 0;
206 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
207 	} else {
208 		int tmp = 1;
209 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
210 	}
211 	ip->set = 1;
212 }
213 
214 void
f_ispeed(struct info * ip)215 f_ispeed(struct info *ip)
216 {
217 
218 	cfsetispeed(&ip->t, atoi(ip->arg));
219 	ip->set = 1;
220 }
221 
222 void
f_lcase(struct info * ip)223 f_lcase(struct info *ip)
224 {
225 	if (ip->off) {
226 		ip->t.c_iflag &= ~IUCLC;
227 		ip->t.c_oflag &= ~OLCUC;
228 		ip->t.c_lflag &= ~XCASE;
229 	} else {
230 		ip->t.c_iflag |= IUCLC;
231 		ip->t.c_oflag |= OLCUC;
232 		ip->t.c_lflag |= XCASE;
233 	}
234 	ip->set = 1;
235 }
236 
237 void
f_nl(struct info * ip)238 f_nl(struct info *ip)
239 {
240 
241 	if (ip->off) {
242 		ip->t.c_iflag |= ICRNL;
243 		ip->t.c_oflag |= ONLCR;
244 	} else {
245 		ip->t.c_iflag &= ~ICRNL;
246 		ip->t.c_oflag &= ~ONLCR;
247 	}
248 	ip->set = 1;
249 }
250 
251 void
f_ospeed(struct info * ip)252 f_ospeed(struct info *ip)
253 {
254 
255 	cfsetospeed(&ip->t, atoi(ip->arg));
256 	ip->set = 1;
257 }
258 
259 void
f_raw(struct info * ip)260 f_raw(struct info *ip)
261 {
262 
263 	if (ip->off)
264 		f_sane(ip);
265 	else {
266 		cfmakeraw(&ip->t);
267 		ip->t.c_cflag &= ~(CSIZE|PARENB);
268 		ip->t.c_cflag |= CS8;
269 		ip->set = 1;
270 	}
271 }
272 
273 void
f_rows(struct info * ip)274 f_rows(struct info *ip)
275 {
276 
277 	ip->win.ws_row = atoi(ip->arg);
278 	ip->wset = 1;
279 }
280 
281 void
f_sane(struct info * ip)282 f_sane(struct info *ip)
283 {
284 
285 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS));
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;
293 }
294 
295 void
f_size(struct info * ip)296 f_size(struct info *ip)
297 {
298 
299 	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
300 }
301 
302 void
f_speed(struct info * ip)303 f_speed(struct info *ip)
304 {
305 
306 	(void)printf("%d\n", cfgetospeed(&ip->t));
307 }
308 
309 void
f_tty(struct info * ip)310 f_tty(struct info *ip)
311 {
312 	int tmp;
313 
314 	tmp = TTYDISC;
315 	if (ioctl(ip->fd, TIOCSETD, &tmp) < 0)
316 		err(1, "TIOCSETD");
317 }
318 
319 void
f_ostart(struct info * ip)320 f_ostart(struct info *ip)
321 {
322 	if (ioctl (ip->fd, TIOCSTART) < 0)
323 		err(1, "TIOCSTART");
324 }
325 
326 void
f_ostop(struct info * ip)327 f_ostop(struct info *ip)
328 {
329 	if (ioctl (ip->fd, TIOCSTOP) < 0)
330 		err(1, "TIOCSTOP");
331 }
332