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