I liked the savecore echo (it explained the delay and funny messages);
[unix-history] / usr / src / bin / stty / key.c
CommitLineData
b21d5003
KB
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
9static char sccsid[] = "@(#)key.c 5.1 (Berkeley) %G%";
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
21int c_key __P((const void *, const void *));
22void f_all __P((struct info *));
23void f_cbreak __P((struct info *));
24void f_columns __P((struct info *));
25void f_dec __P((struct info *));
26void f_everything __P((struct info *));
27void f_extproc __P((struct info *));
28void f_ispeed __P((struct info *));
29void f_nl __P((struct info *));
30void f_ospeed __P((struct info *));
31void f_raw __P((struct info *));
32void f_rows __P((struct info *));
33void f_sane __P((struct info *));
34void f_size __P((struct info *));
35void f_speed __P((struct info *));
36void f_tty __P((struct info *));
37__END_DECLS
38
39static 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
61struct key *
62ksearch(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
73static
74c_key(a, b)
75 const void *a, *b;
76{
77 return(strcmp(((struct key *)a)->name, ((struct key *)b)->name));
78}
79
80void
81f_all(ip)
82 struct info *ip;
83{
84 print(&ip->t, &ip->win, ip->ldisc, BSD);
85}
86
87void
88f_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
102void
103f_columns(ip)
104 struct info *ip;
105{
106 ip->win.ws_col = atoi(ip->arg);
107 ip->wset = 1;
108}
109
110void
111f_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
123void
124f_everything(ip)
125 struct info *ip;
126{
127 print(&ip->t, &ip->win, ip->ldisc, BSD);
128}
129
130void
131f_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
145void
146f_ispeed(ip)
147 struct info *ip;
148{
149 cfsetispeed(&ip->t, atoi(ip->arg));
150 ip->set = 1;
151}
152
153void
154f_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
167void
168f_ospeed(ip)
169 struct info *ip;
170{
171 cfsetospeed(&ip->t, atoi(ip->arg));
172 ip->set = 1;
173}
174
175void
176f_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
189void
190f_rows(ip)
191 struct info *ip;
192{
193 ip->win.ws_row = atoi(ip->arg);
194 ip->wset = 1;
195}
196
197void
198f_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
211void
212f_size(ip)
213 struct info *ip;
214{
215 (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
216}
217
218void
219f_speed(ip)
220 struct info *ip;
221{
222 (void)printf("%d\n", cfgetospeed(&ip->t));
223}
224
225void
226f_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}