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#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
33#include <stddef.h>
34#include <string.h>
35#include <termios.h>
36#include "lp.local.h"
37#include "extern.h"
38
39struct modes {
40	const char *name;
41	const long  set;
42	const 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 */
49struct 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	{ "ctsflow",	CCTS_OFLOW, 0 },
79	{ "-ctsflow",	0, CCTS_OFLOW },
80	{ "dsrflow",	CDSR_OFLOW, 0 },
81	{ "-dsrflow",	0, CDSR_OFLOW },
82	{ "dtrflow",	CDTR_IFLOW, 0 },
83	{ "-dtrflow",	0, CDTR_IFLOW },
84	{ "rtsflow",	CRTS_IFLOW, 0 },
85	{ "-rtsflow",	0, CRTS_IFLOW },
86	{ "mdmbuf",	MDMBUF, 0 },
87	{ "-mdmbuf",	0, MDMBUF },
88	{ NULL, 0, 0},
89};
90
91struct modes imodes[] = {
92	{ "ignbrk",	IGNBRK, 0 },
93	{ "-ignbrk",	0, IGNBRK },
94	{ "brkint",	BRKINT, 0 },
95	{ "-brkint",	0, BRKINT },
96	{ "ignpar",	IGNPAR, 0 },
97	{ "-ignpar",	0, IGNPAR },
98	{ "parmrk",	PARMRK, 0 },
99	{ "-parmrk",	0, PARMRK },
100	{ "inpck",	INPCK, 0 },
101	{ "-inpck",	0, INPCK },
102	{ "istrip",	ISTRIP, 0 },
103	{ "-istrip",	0, ISTRIP },
104	{ "inlcr",	INLCR, 0 },
105	{ "-inlcr",	0, INLCR },
106	{ "igncr",	IGNCR, 0 },
107	{ "-igncr",	0, IGNCR },
108	{ "icrnl",	ICRNL, 0 },
109	{ "-icrnl",	0, ICRNL },
110	{ "ixon",	IXON, 0 },
111	{ "-ixon",	0, IXON },
112	{ "flow",	IXON, 0 },
113	{ "-flow",	0, IXON },
114	{ "ixoff",	IXOFF, 0 },
115	{ "-ixoff",	0, IXOFF },
116	{ "tandem",	IXOFF, 0 },
117	{ "-tandem",	0, IXOFF },
118	{ "ixany",	IXANY, 0 },
119	{ "-ixany",	0, IXANY },
120	{ "decctlq",	0, IXANY },
121	{ "-decctlq",	IXANY, 0 },
122	{ "imaxbel",	IMAXBEL, 0 },
123	{ "-imaxbel",	0, IMAXBEL },
124	{ NULL, 0, 0},
125};
126
127struct modes lmodes[] = {
128	{ "echo",	ECHO, 0 },
129	{ "-echo",	0, ECHO },
130	{ "echoe",	ECHOE, 0 },
131	{ "-echoe",	0, ECHOE },
132	{ "crterase",	ECHOE, 0 },
133	{ "-crterase",	0, ECHOE },
134	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
135	{ "-crtbs",	0, ECHOE },
136	{ "echok",	ECHOK, 0 },
137	{ "-echok",	0, ECHOK },
138	{ "echoke",	ECHOKE, 0 },
139	{ "-echoke",	0, ECHOKE },
140	{ "crtkill",	ECHOKE, 0 },
141	{ "-crtkill",	0, ECHOKE },
142	{ "altwerase",	ALTWERASE, 0 },
143	{ "-altwerase",	0, ALTWERASE },
144	{ "iexten",	IEXTEN, 0 },
145	{ "-iexten",	0, IEXTEN },
146	{ "echonl",	ECHONL, 0 },
147	{ "-echonl",	0, ECHONL },
148	{ "echoctl",	ECHOCTL, 0 },
149	{ "-echoctl",	0, ECHOCTL },
150	{ "ctlecho",	ECHOCTL, 0 },
151	{ "-ctlecho",	0, ECHOCTL },
152	{ "echoprt",	ECHOPRT, 0 },
153	{ "-echoprt",	0, ECHOPRT },
154	{ "prterase",	ECHOPRT, 0 },
155	{ "-prterase",	0, ECHOPRT },
156	{ "isig",	ISIG, 0 },
157	{ "-isig",	0, ISIG },
158	{ "icanon",	ICANON, 0 },
159	{ "-icanon",	0, ICANON },
160	{ "noflsh",	NOFLSH, 0 },
161	{ "-noflsh",	0, NOFLSH },
162	{ "tostop",	TOSTOP, 0 },
163	{ "-tostop",	0, TOSTOP },
164	{ "flusho",	FLUSHO, 0 },
165	{ "-flusho",	0, FLUSHO },
166	{ "pendin",	PENDIN, 0 },
167	{ "-pendin",	0, PENDIN },
168	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
169	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
170	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
171	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
172	{ "nokerninfo",	NOKERNINFO, 0 },
173	{ "-nokerninfo",0, NOKERNINFO },
174	{ "kerninfo",	0, NOKERNINFO },
175	{ "-kerninfo",	NOKERNINFO, 0 },
176	{ NULL, 0, 0},
177};
178
179struct modes omodes[] = {
180	{ "opost",	OPOST, 0 },
181	{ "-opost",	0, OPOST },
182	{ "litout",	0, OPOST },
183	{ "-litout",	OPOST, 0 },
184	{ "onlcr",	ONLCR, 0 },
185	{ "-onlcr",	0, ONLCR },
186	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
187	{ "-tabs",	OXTABS, 0 },
188	{ "oxtabs",	OXTABS, 0 },
189	{ "-oxtabs",	0, OXTABS },
190	{ NULL, 0, 0},
191};
192
193#define	CHK(name, s)	(*name == s[0] && !strcmp(name, s))
194
195int
196msearch(char *str, struct termios *ip)
197{
198	struct modes *mp;
199
200	for (mp = cmodes; mp->name; ++mp)
201		if (CHK(str, mp->name)) {
202			ip->c_cflag &= ~mp->unset;
203			ip->c_cflag |= mp->set;
204			return (1);
205		}
206	for (mp = imodes; mp->name; ++mp)
207		if (CHK(str, mp->name)) {
208			ip->c_iflag &= ~mp->unset;
209			ip->c_iflag |= mp->set;
210			return (1);
211		}
212	for (mp = lmodes; mp->name; ++mp)
213		if (CHK(str, mp->name)) {
214			ip->c_lflag &= ~mp->unset;
215			ip->c_lflag |= mp->set;
216			return (1);
217		}
218	for (mp = omodes; mp->name; ++mp)
219		if (CHK(str, mp->name)) {
220			ip->c_oflag &= ~mp->unset;
221			ip->c_oflag |= mp->set;
222			return (1);
223		}
224	return (0);
225}
226