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