1 /* $NetBSD: key.c,v 1.6 2003/08/07 11:25:27 agc Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)key.c 8.3 (Berkeley) 4/2/94";
37 __RCSID("$NetBSD: key.c,v 1.6 2003/08/07 11:25:27 agc Exp $");
41 #include <sys/param.h>
42 #include <sys/types.h>
57 c_key(const void *, const void *);
58 void f_cbreak(struct info
*);
59 void f_columns(struct info
*);
60 void f_dec(struct info
*);
61 void f_extproc(struct info
*);
62 void f_ispeed(struct info
*);
63 void f_nl(struct info
*);
64 void f_ospeed(struct info
*);
65 void f_raw(struct info
*);
66 void f_rows(struct info
*);
67 void f_sane(struct info
*);
68 void f_tty(struct info
*);
72 const char *name
; /* name */
73 void (*f
)(struct info
*); /* function */
74 #define F_NEEDARG 0x01 /* needs an argument */
75 #define F_OFFOK 0x02 /* can turn off */
78 { "cbreak", f_cbreak
, F_OFFOK
},
79 { "cols", f_columns
, F_NEEDARG
},
80 { "columns", f_columns
, F_NEEDARG
},
81 { "cooked", f_sane
, 0 },
83 { "extproc", f_extproc
, F_OFFOK
},
84 { "ispeed", f_ispeed
, F_NEEDARG
},
86 { "nl", f_nl
, F_OFFOK
},
88 { "ospeed", f_ospeed
, F_NEEDARG
},
89 { "raw", f_raw
, F_OFFOK
},
90 { "rows", f_rows
, F_NEEDARG
},
91 { "sane", f_sane
, 0 },
96 c_key(const void *a
, const void *b
)
99 return (strcmp(((const struct key
*)a
)->name
,
100 ((const struct key
*)b
)->name
));
104 ksearch(char ***argvp
, struct info
*ip
)
117 if (!(kp
= (struct key
*)bsearch(&tmp
, keys
,
118 sizeof(keys
)/sizeof(struct key
), sizeof(struct key
), c_key
)))
120 if (!(kp
->flags
& F_OFFOK
) && ip
->off
) {
121 syslog(LOG_INFO
, "%s: illegal option: %s", printer
, name
);
124 if (kp
->flags
& F_NEEDARG
&& !(ip
->arg
= *++*argvp
)) {
125 syslog(LOG_INFO
, "%s: option requires an argument: %s",
134 f_cbreak(struct info
*ip
)
140 ip
->t
.c_iflag
|= BRKINT
|IXON
|IMAXBEL
;
141 ip
->t
.c_oflag
|= OPOST
;
142 ip
->t
.c_lflag
|= ISIG
|IEXTEN
;
143 ip
->t
.c_lflag
&= ~ICANON
;
149 f_columns(struct info
*ip
)
152 ip
->win
.ws_col
= atoi(ip
->arg
);
157 f_dec(struct info
*ip
)
160 ip
->t
.c_cc
[VERASE
] = (u_char
)0177;
161 ip
->t
.c_cc
[VKILL
] = CTRL('u');
162 ip
->t
.c_cc
[VINTR
] = CTRL('c');
163 ip
->t
.c_lflag
&= ~ECHOPRT
;
164 ip
->t
.c_lflag
|= ECHOE
|ECHOKE
|ECHOCTL
;
165 ip
->t
.c_iflag
&= ~IXANY
;
170 f_extproc(struct info
*ip
)
175 (void)ioctl(ip
->fd
, TIOCEXT
, &tmp
);
178 (void)ioctl(ip
->fd
, TIOCEXT
, &tmp
);
183 f_ispeed(struct info
*ip
)
186 cfsetispeed(&ip
->t
, atoi(ip
->arg
));
191 f_nl(struct info
*ip
)
195 ip
->t
.c_iflag
|= ICRNL
;
196 ip
->t
.c_oflag
|= ONLCR
;
198 ip
->t
.c_iflag
&= ~ICRNL
;
199 ip
->t
.c_oflag
&= ~ONLCR
;
205 f_ospeed(struct info
*ip
)
208 cfsetospeed(&ip
->t
, atoi(ip
->arg
));
213 f_raw(struct info
*ip
)
220 ip
->t
.c_cflag
&= ~(CSIZE
|PARENB
);
221 ip
->t
.c_cflag
|= CS8
;
227 f_rows(struct info
*ip
)
230 ip
->win
.ws_row
= atoi(ip
->arg
);
235 f_sane(struct info
*ip
)
238 ip
->t
.c_cflag
= TTYDEF_CFLAG
| (ip
->t
.c_cflag
& (CLOCAL
|CRTSCTS
|CDTRCTS
));
239 ip
->t
.c_iflag
= TTYDEF_IFLAG
;
240 ip
->t
.c_iflag
|= ICRNL
;
241 /* preserve user-preference flags in lflag */
242 #define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
243 ip
->t
.c_lflag
= TTYDEF_LFLAG
| (ip
->t
.c_lflag
& LKEEP
);
244 ip
->t
.c_oflag
= TTYDEF_OFLAG
;
249 f_tty(struct info
*ip
)
254 if (ioctl(0, TIOCSETD
, &tmp
) < 0)
255 syslog(LOG_ERR
, "%s: ioctl(TIOCSETD): %m", printer
);