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