modes.c revision 50479
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35/*
36static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
37*/
38static const char rcsid[] =
39  "$FreeBSD: head/usr.sbin/lpr/lpd/modes.c 50479 1999-08-28 01:35:59Z peter $";
40#endif /* not lint */
41
42#include <stddef.h>
43#include <string.h>
44#include <termios.h>
45#include "lp.local.h"
46#include "extern.h"
47
48struct modes {
49	char *name;
50	long set;
51	long unset;
52};
53
54/*
55 * The code in optlist() depends on minus options following regular
56 * options, i.e. "foo" must immediately precede "-foo".
57 */
58struct modes cmodes[] = {
59	{ "cs5",	CS5, CSIZE },
60	{ "cs6",	CS6, CSIZE },
61	{ "cs7",	CS7, CSIZE },
62	{ "cs8",	CS8, CSIZE },
63	{ "cstopb",	CSTOPB, 0 },
64	{ "-cstopb",	0, CSTOPB },
65	{ "cread",	CREAD, 0 },
66	{ "-cread",	0, CREAD },
67	{ "parenb",	PARENB, 0 },
68	{ "-parenb",	0, PARENB },
69	{ "parodd",	PARODD, 0 },
70	{ "-parodd",	0, PARODD },
71	{ "parity",	PARENB | CS7, PARODD | CSIZE },
72	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
73	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
74	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
75	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
76	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
77	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
78	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
79	{ "hupcl",	HUPCL, 0 },
80	{ "-hupcl",	0, HUPCL },
81	{ "hup",	HUPCL, 0 },
82	{ "-hup",	0, HUPCL },
83	{ "clocal",	CLOCAL, 0 },
84	{ "-clocal",	0, CLOCAL },
85	{ "crtscts",	CRTSCTS, 0 },
86	{ "-crtscts",	0, CRTSCTS },
87	{ "ctsflow",	CCTS_OFLOW, 0 },
88	{ "-ctsflow",	0, CCTS_OFLOW },
89	{ "dsrflow",	CDSR_OFLOW, 0 },
90	{ "-dsrflow",	0, CDSR_OFLOW },
91	{ "dtrflow",	CDTR_IFLOW, 0 },
92	{ "-dtrflow",	0, CDTR_IFLOW },
93	{ "rtsflow",	CRTS_IFLOW, 0 },
94	{ "-rtsflow",	0, CRTS_IFLOW },
95	{ "mdmbuf",	MDMBUF, 0 },
96	{ "-mdmbuf",	0, MDMBUF },
97	{ NULL },
98};
99
100struct modes imodes[] = {
101	{ "ignbrk",	IGNBRK, 0 },
102	{ "-ignbrk",	0, IGNBRK },
103	{ "brkint",	BRKINT, 0 },
104	{ "-brkint",	0, BRKINT },
105	{ "ignpar",	IGNPAR, 0 },
106	{ "-ignpar",	0, IGNPAR },
107	{ "parmrk",	PARMRK, 0 },
108	{ "-parmrk",	0, PARMRK },
109	{ "inpck",	INPCK, 0 },
110	{ "-inpck",	0, INPCK },
111	{ "istrip",	ISTRIP, 0 },
112	{ "-istrip",	0, ISTRIP },
113	{ "inlcr",	INLCR, 0 },
114	{ "-inlcr",	0, INLCR },
115	{ "igncr",	IGNCR, 0 },
116	{ "-igncr",	0, IGNCR },
117	{ "icrnl",	ICRNL, 0 },
118	{ "-icrnl",	0, ICRNL },
119	{ "ixon",	IXON, 0 },
120	{ "-ixon",	0, IXON },
121	{ "flow",	IXON, 0 },
122	{ "-flow",	0, IXON },
123	{ "ixoff",	IXOFF, 0 },
124	{ "-ixoff",	0, IXOFF },
125	{ "tandem",	IXOFF, 0 },
126	{ "-tandem",	0, IXOFF },
127	{ "ixany",	IXANY, 0 },
128	{ "-ixany",	0, IXANY },
129	{ "decctlq",	0, IXANY },
130	{ "-decctlq",	IXANY, 0 },
131	{ "imaxbel",	IMAXBEL, 0 },
132	{ "-imaxbel",	0, IMAXBEL },
133	{ NULL },
134};
135
136struct modes lmodes[] = {
137	{ "echo",	ECHO, 0 },
138	{ "-echo",	0, ECHO },
139	{ "echoe",	ECHOE, 0 },
140	{ "-echoe",	0, ECHOE },
141	{ "crterase",	ECHOE, 0 },
142	{ "-crterase",	0, ECHOE },
143	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
144	{ "-crtbs",	0, ECHOE },
145	{ "echok",	ECHOK, 0 },
146	{ "-echok",	0, ECHOK },
147	{ "echoke",	ECHOKE, 0 },
148	{ "-echoke",	0, ECHOKE },
149	{ "crtkill",	ECHOKE, 0 },
150	{ "-crtkill",	0, ECHOKE },
151	{ "altwerase",	ALTWERASE, 0 },
152	{ "-altwerase",	0, ALTWERASE },
153	{ "iexten",	IEXTEN, 0 },
154	{ "-iexten",	0, IEXTEN },
155	{ "echonl",	ECHONL, 0 },
156	{ "-echonl",	0, ECHONL },
157	{ "echoctl",	ECHOCTL, 0 },
158	{ "-echoctl",	0, ECHOCTL },
159	{ "ctlecho",	ECHOCTL, 0 },
160	{ "-ctlecho",	0, ECHOCTL },
161	{ "echoprt",	ECHOPRT, 0 },
162	{ "-echoprt",	0, ECHOPRT },
163	{ "prterase",	ECHOPRT, 0 },
164	{ "-prterase",	0, ECHOPRT },
165	{ "isig",	ISIG, 0 },
166	{ "-isig",	0, ISIG },
167	{ "icanon",	ICANON, 0 },
168	{ "-icanon",	0, ICANON },
169	{ "noflsh",	NOFLSH, 0 },
170	{ "-noflsh",	0, NOFLSH },
171	{ "tostop",	TOSTOP, 0 },
172	{ "-tostop",	0, TOSTOP },
173	{ "flusho",	FLUSHO, 0 },
174	{ "-flusho",	0, FLUSHO },
175	{ "pendin",	PENDIN, 0 },
176	{ "-pendin",	0, PENDIN },
177	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
178	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
179	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
180	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
181	{ "nokerninfo",	NOKERNINFO, 0 },
182	{ "-nokerninfo",0, NOKERNINFO },
183	{ "kerninfo",	0, NOKERNINFO },
184	{ "-kerninfo",	NOKERNINFO, 0 },
185	{ NULL },
186};
187
188struct modes omodes[] = {
189	{ "opost",	OPOST, 0 },
190	{ "-opost",	0, OPOST },
191	{ "litout",	0, OPOST },
192	{ "-litout",	OPOST, 0 },
193	{ "onlcr",	ONLCR, 0 },
194	{ "-onlcr",	0, ONLCR },
195	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
196	{ "-tabs",	OXTABS, 0 },
197	{ "oxtabs",	OXTABS, 0 },
198	{ "-oxtabs",	0, OXTABS },
199	{ NULL },
200};
201
202#define	CHK(name, s)	(*name == s[0] && !strcmp(name, s))
203
204int
205msearch(str, ip)
206	char *str;
207	struct termios *ip;
208{
209	struct modes *mp;
210
211	for (mp = cmodes; mp->name; ++mp)
212		if (CHK(str, mp->name)) {
213			ip->c_cflag &= ~mp->unset;
214			ip->c_cflag |= mp->set;
215			return (1);
216		}
217	for (mp = imodes; mp->name; ++mp)
218		if (CHK(str, mp->name)) {
219			ip->c_iflag &= ~mp->unset;
220			ip->c_iflag |= mp->set;
221			return (1);
222		}
223	for (mp = lmodes; mp->name; ++mp)
224		if (CHK(str, mp->name)) {
225			ip->c_lflag &= ~mp->unset;
226			ip->c_lflag |= mp->set;
227			return (1);
228		}
229	for (mp = omodes; mp->name; ++mp)
230		if (CHK(str, mp->name)) {
231			ip->c_oflag &= ~mp->unset;
232			ip->c_oflag |= mp->set;
233			return (1);
234		}
235	return (0);
236}
237