xref: /freebsd/usr.sbin/lpr/lpd/modes.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. 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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #endif
34 
35 #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
36 #include <stddef.h>
37 #include <string.h>
38 #include <termios.h>
39 #include "lp.local.h"
40 #include "extern.h"
41 
42 struct modes {
43 	const char *name;
44 	const long  set;
45 	const long  unset;
46 };
47 
48 /*
49  * The code in optlist() depends on minus options following regular
50  * options, i.e. "foo" must immediately precede "-foo".
51  */
52 struct modes cmodes[] = {
53 	{ "cs5",	CS5, CSIZE },
54 	{ "cs6",	CS6, CSIZE },
55 	{ "cs7",	CS7, CSIZE },
56 	{ "cs8",	CS8, CSIZE },
57 	{ "cstopb",	CSTOPB, 0 },
58 	{ "-cstopb",	0, CSTOPB },
59 	{ "cread",	CREAD, 0 },
60 	{ "-cread",	0, CREAD },
61 	{ "parenb",	PARENB, 0 },
62 	{ "-parenb",	0, PARENB },
63 	{ "parodd",	PARODD, 0 },
64 	{ "-parodd",	0, PARODD },
65 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
66 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
67 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
68 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
69 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
70 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
71 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
72 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
73 	{ "hupcl",	HUPCL, 0 },
74 	{ "-hupcl",	0, HUPCL },
75 	{ "hup",	HUPCL, 0 },
76 	{ "-hup",	0, HUPCL },
77 	{ "clocal",	CLOCAL, 0 },
78 	{ "-clocal",	0, CLOCAL },
79 	{ "crtscts",	CRTSCTS, 0 },
80 	{ "-crtscts",	0, CRTSCTS },
81 	{ "ctsflow",	CCTS_OFLOW, 0 },
82 	{ "-ctsflow",	0, CCTS_OFLOW },
83 	{ "dsrflow",	CDSR_OFLOW, 0 },
84 	{ "-dsrflow",	0, CDSR_OFLOW },
85 	{ "dtrflow",	CDTR_IFLOW, 0 },
86 	{ "-dtrflow",	0, CDTR_IFLOW },
87 	{ "rtsflow",	CRTS_IFLOW, 0 },
88 	{ "-rtsflow",	0, CRTS_IFLOW },
89 	{ "mdmbuf",	MDMBUF, 0 },
90 	{ "-mdmbuf",	0, MDMBUF },
91 	{ NULL, 0, 0},
92 };
93 
94 struct modes imodes[] = {
95 	{ "ignbrk",	IGNBRK, 0 },
96 	{ "-ignbrk",	0, IGNBRK },
97 	{ "brkint",	BRKINT, 0 },
98 	{ "-brkint",	0, BRKINT },
99 	{ "ignpar",	IGNPAR, 0 },
100 	{ "-ignpar",	0, IGNPAR },
101 	{ "parmrk",	PARMRK, 0 },
102 	{ "-parmrk",	0, PARMRK },
103 	{ "inpck",	INPCK, 0 },
104 	{ "-inpck",	0, INPCK },
105 	{ "istrip",	ISTRIP, 0 },
106 	{ "-istrip",	0, ISTRIP },
107 	{ "inlcr",	INLCR, 0 },
108 	{ "-inlcr",	0, INLCR },
109 	{ "igncr",	IGNCR, 0 },
110 	{ "-igncr",	0, IGNCR },
111 	{ "icrnl",	ICRNL, 0 },
112 	{ "-icrnl",	0, ICRNL },
113 	{ "ixon",	IXON, 0 },
114 	{ "-ixon",	0, IXON },
115 	{ "flow",	IXON, 0 },
116 	{ "-flow",	0, IXON },
117 	{ "ixoff",	IXOFF, 0 },
118 	{ "-ixoff",	0, IXOFF },
119 	{ "tandem",	IXOFF, 0 },
120 	{ "-tandem",	0, IXOFF },
121 	{ "ixany",	IXANY, 0 },
122 	{ "-ixany",	0, IXANY },
123 	{ "decctlq",	0, IXANY },
124 	{ "-decctlq",	IXANY, 0 },
125 	{ "imaxbel",	IMAXBEL, 0 },
126 	{ "-imaxbel",	0, IMAXBEL },
127 	{ NULL, 0, 0},
128 };
129 
130 struct modes lmodes[] = {
131 	{ "echo",	ECHO, 0 },
132 	{ "-echo",	0, ECHO },
133 	{ "echoe",	ECHOE, 0 },
134 	{ "-echoe",	0, ECHOE },
135 	{ "crterase",	ECHOE, 0 },
136 	{ "-crterase",	0, ECHOE },
137 	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
138 	{ "-crtbs",	0, ECHOE },
139 	{ "echok",	ECHOK, 0 },
140 	{ "-echok",	0, ECHOK },
141 	{ "echoke",	ECHOKE, 0 },
142 	{ "-echoke",	0, ECHOKE },
143 	{ "crtkill",	ECHOKE, 0 },
144 	{ "-crtkill",	0, ECHOKE },
145 	{ "altwerase",	ALTWERASE, 0 },
146 	{ "-altwerase",	0, ALTWERASE },
147 	{ "iexten",	IEXTEN, 0 },
148 	{ "-iexten",	0, IEXTEN },
149 	{ "echonl",	ECHONL, 0 },
150 	{ "-echonl",	0, ECHONL },
151 	{ "echoctl",	ECHOCTL, 0 },
152 	{ "-echoctl",	0, ECHOCTL },
153 	{ "ctlecho",	ECHOCTL, 0 },
154 	{ "-ctlecho",	0, ECHOCTL },
155 	{ "echoprt",	ECHOPRT, 0 },
156 	{ "-echoprt",	0, ECHOPRT },
157 	{ "prterase",	ECHOPRT, 0 },
158 	{ "-prterase",	0, ECHOPRT },
159 	{ "isig",	ISIG, 0 },
160 	{ "-isig",	0, ISIG },
161 	{ "icanon",	ICANON, 0 },
162 	{ "-icanon",	0, ICANON },
163 	{ "noflsh",	NOFLSH, 0 },
164 	{ "-noflsh",	0, NOFLSH },
165 	{ "tostop",	TOSTOP, 0 },
166 	{ "-tostop",	0, TOSTOP },
167 	{ "flusho",	FLUSHO, 0 },
168 	{ "-flusho",	0, FLUSHO },
169 	{ "pendin",	PENDIN, 0 },
170 	{ "-pendin",	0, PENDIN },
171 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
172 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
173 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
174 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
175 	{ "nokerninfo",	NOKERNINFO, 0 },
176 	{ "-nokerninfo",0, NOKERNINFO },
177 	{ "kerninfo",	0, NOKERNINFO },
178 	{ "-kerninfo",	NOKERNINFO, 0 },
179 	{ NULL, 0, 0},
180 };
181 
182 struct modes omodes[] = {
183 	{ "opost",	OPOST, 0 },
184 	{ "-opost",	0, OPOST },
185 	{ "litout",	0, OPOST },
186 	{ "-litout",	OPOST, 0 },
187 	{ "onlcr",	ONLCR, 0 },
188 	{ "-onlcr",	0, ONLCR },
189 	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
190 	{ "-tabs",	OXTABS, 0 },
191 	{ "oxtabs",	OXTABS, 0 },
192 	{ "-oxtabs",	0, OXTABS },
193 	{ NULL, 0, 0},
194 };
195 
196 #define	CHK(name, s)	(*name == s[0] && !strcmp(name, s))
197 
198 int
199 msearch(char *str, struct termios *ip)
200 {
201 	struct modes *mp;
202 
203 	for (mp = cmodes; mp->name; ++mp)
204 		if (CHK(str, mp->name)) {
205 			ip->c_cflag &= ~mp->unset;
206 			ip->c_cflag |= mp->set;
207 			return (1);
208 		}
209 	for (mp = imodes; mp->name; ++mp)
210 		if (CHK(str, mp->name)) {
211 			ip->c_iflag &= ~mp->unset;
212 			ip->c_iflag |= mp->set;
213 			return (1);
214 		}
215 	for (mp = lmodes; mp->name; ++mp)
216 		if (CHK(str, mp->name)) {
217 			ip->c_lflag &= ~mp->unset;
218 			ip->c_lflag |= mp->set;
219 			return (1);
220 		}
221 	for (mp = omodes; mp->name; ++mp)
222 		if (CHK(str, mp->name)) {
223 			ip->c_oflag &= ~mp->unset;
224 			ip->c_oflag |= mp->set;
225 			return (1);
226 		}
227 	return (0);
228 }
229