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