xref: /original-bsd/bin/stty/key.c (revision dc4562f1)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)key.c	5.3 (Berkeley) 06/10/91";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include "stty.h"
18 #include "extern.h"
19 
20 __BEGIN_DECLS
21 void	f_all __P((struct info *));
22 void	f_cbreak __P((struct info *));
23 void	f_columns __P((struct info *));
24 void	f_dec __P((struct info *));
25 void	f_everything __P((struct info *));
26 void	f_extproc __P((struct info *));
27 void	f_ispeed __P((struct info *));
28 void	f_nl __P((struct info *));
29 void	f_ospeed __P((struct info *));
30 void	f_raw __P((struct info *));
31 void	f_rows __P((struct info *));
32 void	f_sane __P((struct info *));
33 void	f_size __P((struct info *));
34 void	f_speed __P((struct info *));
35 void	f_tty __P((struct info *));
36 __END_DECLS
37 
38 static struct key {
39 	char *name;				/* name */
40 	void (*f) __P((struct info *));		/* function */
41 #define	F_NEEDARG	0x01			/* needs an argument */
42 #define	F_OFFOK		0x02			/* can turn off */
43 	int flags;
44 } keys[] = {
45 	"all",		f_all,		0,
46 	"cbreak",	f_cbreak,	F_OFFOK,
47 	"cols",		f_columns,	F_NEEDARG,
48 	"columns",	f_columns,	F_NEEDARG,
49 	"cooked", 	f_sane,		0,
50 	"dec",		f_dec,		0,
51 	"everything",	f_everything,	0,
52 	"extproc",	f_extproc,	F_OFFOK,
53 	"ispeed",	f_ispeed,	0,
54 	"new",		f_tty,		0,
55 	"nl",		f_nl,		F_OFFOK,
56 	"old",		f_tty,		0,
57 	"ospeed",	f_ospeed,	F_NEEDARG,
58 	"raw",		f_raw,		F_OFFOK,
59 	"rows",		f_rows,		F_NEEDARG,
60 	"sane",		f_sane,		0,
61 	"size",		f_size,		0,
62 	"speed",	f_speed,	0,
63 	"tty",		f_tty,		0,
64 };
65 
66 ksearch(argvp, ip)
67 	char ***argvp;
68 	struct info *ip;
69 {
70 	register struct key *kp;
71 	register char *name;
72 	struct key tmp;
73 	static int c_key __P((const void *, const void *));
74 
75 	name = **argvp;
76 	if (*name == '-') {
77 		ip->off = 1;
78 		++name;
79 	} else
80 		ip->off = 0;
81 
82 	tmp.name = name;
83 	if (!(kp = (struct key *)bsearch(&tmp, keys,
84 	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
85 		return(0);
86 	if (!(kp->flags & F_OFFOK) && ip->off)
87 		err("illegal option -- %s\n%s", name, usage);
88 	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp))
89 		err("option requires an argument -- %s\n%s", name, usage);
90 	kp->f(ip);
91 	return(1);
92 }
93 
94 static
95 c_key(a, b)
96         const void *a, *b;
97 {
98         return(strcmp(((struct key *)a)->name, ((struct key *)b)->name));
99 }
100 
101 void
102 f_all(ip)
103 	struct info *ip;
104 {
105 	print(&ip->t, &ip->win, ip->ldisc, BSD);
106 }
107 
108 void
109 f_cbreak(ip)
110 	struct info *ip;
111 {
112 	if (ip->off)
113 		f_sane(ip);
114 	else {
115 		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
116 		ip->t.c_oflag |= OPOST;
117 		ip->t.c_lflag |= ISIG|IEXTEN;
118 		ip->t.c_lflag &= ~ICANON;
119 		ip->set = 1;
120 	}
121 }
122 
123 void
124 f_columns(ip)
125 	struct info *ip;
126 {
127 	ip->win.ws_col = atoi(ip->arg);
128 	ip->wset = 1;
129 }
130 
131 void
132 f_dec(ip)
133 	struct info *ip;
134 {
135 	ip->t.c_cc[VERASE] = (u_char)0177;
136 	ip->t.c_cc[VKILL] = CTRL('u');
137 	ip->t.c_cc[VINTR] = CTRL('c');
138 	ip->t.c_lflag &= ~ECHOPRT;
139 	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
140 	ip->t.c_iflag &= ~IXANY;
141 	ip->set = 1;
142 }
143 
144 void
145 f_everything(ip)
146 	struct info *ip;
147 {
148 	print(&ip->t, &ip->win, ip->ldisc, BSD);
149 }
150 
151 void
152 f_extproc(ip)
153 	struct info *ip;
154 {
155 	int tmp;
156 
157 	if (ip->set) {
158 		tmp = 1;
159 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
160 	} else {
161 		tmp = 0;
162 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
163 	}
164 }
165 
166 void
167 f_ispeed(ip)
168 	struct info *ip;
169 {
170 	cfsetispeed(&ip->t, atoi(ip->arg));
171 	ip->set = 1;
172 }
173 
174 void
175 f_nl(ip)
176 	struct info *ip;
177 {
178 	if (ip->off) {
179 		ip->t.c_iflag |= ICRNL;
180 		ip->t.c_oflag |= ONLCR;
181 	} else {
182 		ip->t.c_iflag &= ~ICRNL;
183 		ip->t.c_oflag &= ~ONLCR;
184 	}
185 	ip->set = 1;
186 }
187 
188 void
189 f_ospeed(ip)
190 	struct info *ip;
191 {
192 	cfsetospeed(&ip->t, atoi(ip->arg));
193 	ip->set = 1;
194 }
195 
196 void
197 f_raw(ip)
198 	struct info *ip;
199 {
200 	if (ip->off)
201 		f_sane(ip);
202 	else {
203 		cfmakeraw(&ip->t);
204 		ip->t.c_cflag &= ~(CSIZE|PARENB);
205 		ip->t.c_cflag |= CS8;
206 		ip->set = 1;
207 	}
208 }
209 
210 void
211 f_rows(ip)
212 	struct info *ip;
213 {
214 	ip->win.ws_row = atoi(ip->arg);
215 	ip->wset = 1;
216 }
217 
218 void
219 f_sane(ip)
220 	struct info *ip;
221 {
222 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL);
223 	ip->t.c_iflag = TTYDEF_IFLAG;
224 	ip->t.c_iflag |= ICRNL;
225 	/* preserve user-preference flags in lflag */
226 #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
227 	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
228 	ip->t.c_oflag = TTYDEF_OFLAG;
229 	ip->set = 1;
230 }
231 
232 void
233 f_size(ip)
234 	struct info *ip;
235 {
236 	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
237 }
238 
239 void
240 f_speed(ip)
241 	struct info *ip;
242 {
243 	(void)printf("%d\n", cfgetospeed(&ip->t));
244 }
245 
246 void
247 f_tty(ip)
248 	struct info *ip;
249 {
250 	int tmp;
251 
252 	tmp = TTYDISC;
253 	if (ioctl(0, TIOCSETD, &tmp) < 0)
254 		err("TIOCSETD: %s", strerror(errno));
255 }
256