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