replaced PNULL def with NULL
[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
14cc5c1c 9static char sccsid[] = "@(#)key.c 5.2 (Berkeley) %G%";
b21d5003
KB
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
b21d5003
KB
21void f_all __P((struct info *));
22void f_cbreak __P((struct info *));
23void f_columns __P((struct info *));
24void f_dec __P((struct info *));
25void f_everything __P((struct info *));
26void f_extproc __P((struct info *));
27void f_ispeed __P((struct info *));
28void f_nl __P((struct info *));
29void f_ospeed __P((struct info *));
30void f_raw __P((struct info *));
31void f_rows __P((struct info *));
32void f_sane __P((struct info *));
33void f_size __P((struct info *));
34void f_speed __P((struct info *));
35void f_tty __P((struct info *));
36__END_DECLS
37
38static 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
14cc5c1c
KB
60ksearch(argvp, ip)
61 char ***argvp;
62 struct info *ip;
b21d5003 63{
14cc5c1c
KB
64 register struct key *kp;
65 register char *name;
b21d5003 66 struct key tmp;
14cc5c1c
KB
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;
b21d5003
KB
75
76 tmp.name = name;
14cc5c1c
KB
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);
b21d5003
KB
86}
87
88static
89c_key(a, b)
90 const void *a, *b;
91{
92 return(strcmp(((struct key *)a)->name, ((struct key *)b)->name));
93}
94
95void
96f_all(ip)
97 struct info *ip;
98{
99 print(&ip->t, &ip->win, ip->ldisc, BSD);
100}
101
102void
103f_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
117void
118f_columns(ip)
119 struct info *ip;
120{
121 ip->win.ws_col = atoi(ip->arg);
122 ip->wset = 1;
123}
124
125void
126f_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
138void
139f_everything(ip)
140 struct info *ip;
141{
142 print(&ip->t, &ip->win, ip->ldisc, BSD);
143}
144
145void
146f_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
160void
161f_ispeed(ip)
162 struct info *ip;
163{
164 cfsetispeed(&ip->t, atoi(ip->arg));
165 ip->set = 1;
166}
167
168void
169f_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
182void
183f_ospeed(ip)
184 struct info *ip;
185{
186 cfsetospeed(&ip->t, atoi(ip->arg));
187 ip->set = 1;
188}
189
190void
191f_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
204void
205f_rows(ip)
206 struct info *ip;
207{
208 ip->win.ws_row = atoi(ip->arg);
209 ip->wset = 1;
210}
211
212void
213f_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
226void
227f_size(ip)
228 struct info *ip;
229{
230 (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
231}
232
233void
234f_speed(ip)
235 struct info *ip;
236{
237 (void)printf("%d\n", cfgetospeed(&ip->t));
238}
239
240void
241f_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}