key.c revision 1.1
1/*	$NetBSD: key.c,v 1.1 1995/10/03 15:02:41 hpeyerl Exp $	*/
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)key.c	8.3 (Berkeley) 4/2/94";
39#else
40static char rcsid[] = "$NetBSD: key.c,v 1.1 1995/10/03 15:02:41 hpeyerl Exp $";
41#endif
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/types.h>
46
47#include <errno.h>
48#include <stdlib.h>
49#include <stdio.h>
50#include <string.h>
51#include <syslog.h>
52#include <dirent.h>
53#include <termios.h>
54
55#include "lp.h"
56#include "extern.h"
57
58__BEGIN_DECLS
59void	f_cbreak __P((struct info *));
60void	f_columns __P((struct info *));
61void	f_dec __P((struct info *));
62void	f_extproc __P((struct info *));
63void	f_ispeed __P((struct info *));
64void	f_nl __P((struct info *));
65void	f_ospeed __P((struct info *));
66void	f_raw __P((struct info *));
67void	f_rows __P((struct info *));
68void	f_sane __P((struct info *));
69void	f_tty __P((struct info *));
70__END_DECLS
71
72static struct key {
73	char *name;				/* name */
74	void (*f) __P((struct info *));		/* function */
75#define	F_NEEDARG	0x01			/* needs an argument */
76#define	F_OFFOK		0x02			/* can turn off */
77	int flags;
78} keys[] = {
79	{ "cbreak",	f_cbreak,	F_OFFOK },
80	{ "cols",	f_columns,	F_NEEDARG },
81	{ "columns",	f_columns,	F_NEEDARG },
82	{ "cooked", 	f_sane,		0 },
83	{ "dec",	f_dec,		0 },
84	{ "extproc",	f_extproc,	F_OFFOK },
85	{ "ispeed",	f_ispeed,	F_NEEDARG },
86	{ "new",	f_tty,		0 },
87	{ "nl",		f_nl,		F_OFFOK },
88	{ "old",	f_tty,		0 },
89	{ "ospeed",	f_ospeed,	F_NEEDARG },
90	{ "raw",	f_raw,		F_OFFOK },
91	{ "rows",	f_rows,		F_NEEDARG },
92	{ "sane",	f_sane,		0 },
93	{ "tty",	f_tty,		0 },
94};
95
96static int
97c_key(a, b)
98        const void *a, *b;
99{
100
101        return (strcmp(((struct key *)a)->name, ((struct key *)b)->name));
102}
103
104int
105ksearch(argvp, ip)
106	char ***argvp;
107	struct info *ip;
108{
109	char *name;
110	struct key *kp, tmp;
111
112	name = **argvp;
113	if (*name == '-') {
114		ip->off = 1;
115		++name;
116	} else
117		ip->off = 0;
118
119	tmp.name = name;
120	if (!(kp = (struct key *)bsearch(&tmp, keys,
121	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
122		return (0);
123	if (!(kp->flags & F_OFFOK) && ip->off) {
124		syslog(LOG_INFO, "%s: illegal option: %s", printer, name);
125		return (1);
126	}
127	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
128		syslog(LOG_INFO, "%s: option requires an argument: %s",
129		       printer, name);
130		return (1);
131	}
132	kp->f(ip);
133	return (1);
134}
135
136void
137f_cbreak(ip)
138	struct info *ip;
139{
140
141	if (ip->off)
142		f_sane(ip);
143	else {
144		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
145		ip->t.c_oflag |= OPOST;
146		ip->t.c_lflag |= ISIG|IEXTEN;
147		ip->t.c_lflag &= ~ICANON;
148		ip->set = 1;
149	}
150}
151
152void
153f_columns(ip)
154	struct info *ip;
155{
156
157	ip->win.ws_col = atoi(ip->arg);
158	ip->wset = 1;
159}
160
161void
162f_dec(ip)
163	struct info *ip;
164{
165
166	ip->t.c_cc[VERASE] = (u_char)0177;
167	ip->t.c_cc[VKILL] = CTRL('u');
168	ip->t.c_cc[VINTR] = CTRL('c');
169	ip->t.c_lflag &= ~ECHOPRT;
170	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
171	ip->t.c_iflag &= ~IXANY;
172	ip->set = 1;
173}
174
175void
176f_extproc(ip)
177	struct info *ip;
178{
179
180	if (ip->set) {
181		int tmp = 1;
182		(void)ioctl(ip->fd, TIOCEXT, &tmp);
183	} else {
184		int tmp = 0;
185		(void)ioctl(ip->fd, TIOCEXT, &tmp);
186	}
187}
188
189void
190f_ispeed(ip)
191	struct info *ip;
192{
193
194	cfsetispeed(&ip->t, atoi(ip->arg));
195	ip->set = 1;
196}
197
198void
199f_nl(ip)
200	struct info *ip;
201{
202
203	if (ip->off) {
204		ip->t.c_iflag |= ICRNL;
205		ip->t.c_oflag |= ONLCR;
206	} else {
207		ip->t.c_iflag &= ~ICRNL;
208		ip->t.c_oflag &= ~ONLCR;
209	}
210	ip->set = 1;
211}
212
213void
214f_ospeed(ip)
215	struct info *ip;
216{
217
218	cfsetospeed(&ip->t, atoi(ip->arg));
219	ip->set = 1;
220}
221
222void
223f_raw(ip)
224	struct info *ip;
225{
226
227	if (ip->off)
228		f_sane(ip);
229	else {
230		cfmakeraw(&ip->t);
231		ip->t.c_cflag &= ~(CSIZE|PARENB);
232		ip->t.c_cflag |= CS8;
233		ip->set = 1;
234	}
235}
236
237void
238f_rows(ip)
239	struct info *ip;
240{
241
242	ip->win.ws_row = atoi(ip->arg);
243	ip->wset = 1;
244}
245
246void
247f_sane(ip)
248	struct info *ip;
249{
250
251	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS));
252	ip->t.c_iflag = TTYDEF_IFLAG;
253	ip->t.c_iflag |= ICRNL;
254	/* preserve user-preference flags in lflag */
255#define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
256	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
257	ip->t.c_oflag = TTYDEF_OFLAG;
258	ip->set = 1;
259}
260
261void
262f_tty(ip)
263	struct info *ip;
264{
265	int tmp;
266
267	tmp = TTYDISC;
268	if (ioctl(0, TIOCSETD, &tmp) < 0)
269		syslog(LOG_ERR, "%s: ioctl(TIOCSETD): %m", printer);
270}
271