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 <sys/types.h>
33 #include <string.h>
34 #include "stty.h"
35
36 int msearch(char ***, struct info *);
37
38 struct modes {
39 const char *name;
40 long set;
41 long unset;
42 };
43
44 /*
45 * The code in optlist() depends on minus options following regular
46 * options, i.e. "foo" must immediately precede "-foo".
47 */
48 static const struct modes cmodes[] = {
49 { "cs5", CS5, CSIZE },
50 { "cs6", CS6, CSIZE },
51 { "cs7", CS7, CSIZE },
52 { "cs8", CS8, CSIZE },
53 { "cstopb", CSTOPB, 0 },
54 { "-cstopb", 0, CSTOPB },
55 { "cread", CREAD, 0 },
56 { "-cread", 0, CREAD },
57 { "parenb", PARENB, 0 },
58 { "-parenb", 0, PARENB },
59 { "parodd", PARODD, 0 },
60 { "-parodd", 0, PARODD },
61 { "parity", PARENB | CS7, PARODD | CSIZE },
62 { "-parity", CS8, PARODD | PARENB | CSIZE },
63 { "evenp", PARENB | CS7, PARODD | CSIZE },
64 { "-evenp", CS8, PARODD | PARENB | CSIZE },
65 { "oddp", PARENB | CS7 | PARODD, CSIZE },
66 { "-oddp", CS8, PARODD | PARENB | CSIZE },
67 { "pass8", CS8, PARODD | PARENB | CSIZE },
68 { "-pass8", PARENB | CS7, PARODD | CSIZE },
69 { "hupcl", HUPCL, 0 },
70 { "-hupcl", 0, HUPCL },
71 { "hup", HUPCL, 0 },
72 { "-hup", 0, HUPCL },
73 { "clocal", CLOCAL, 0 },
74 { "-clocal", 0, CLOCAL },
75 { "crtscts", CRTSCTS, 0 },
76 { "-crtscts", 0, CRTSCTS },
77 { "ctsflow", CCTS_OFLOW, 0 },
78 { "-ctsflow", 0, CCTS_OFLOW },
79 { "dsrflow", CDSR_OFLOW, 0 },
80 { "-dsrflow", 0, CDSR_OFLOW },
81 { "dtrflow", CDTR_IFLOW, 0 },
82 { "-dtrflow", 0, CDTR_IFLOW },
83 { "rtsflow", CRTS_IFLOW, 0 },
84 { "-rtsflow", 0, CRTS_IFLOW },
85 { "mdmbuf", MDMBUF, 0 },
86 { "-mdmbuf", 0, MDMBUF },
87 { "rtsdtr", 0, CNO_RTSDTR },
88 { "-rtsdtr", CNO_RTSDTR, 0 },
89 { NULL, 0, 0 },
90 };
91
92 static const struct modes imodes[] = {
93 { "ignbrk", IGNBRK, 0 },
94 { "-ignbrk", 0, IGNBRK },
95 { "brkint", BRKINT, 0 },
96 { "-brkint", 0, BRKINT },
97 { "ignpar", IGNPAR, 0 },
98 { "-ignpar", 0, IGNPAR },
99 { "parmrk", PARMRK, 0 },
100 { "-parmrk", 0, PARMRK },
101 { "inpck", INPCK, 0 },
102 { "-inpck", 0, INPCK },
103 { "istrip", ISTRIP, 0 },
104 { "-istrip", 0, ISTRIP },
105 { "inlcr", INLCR, 0 },
106 { "-inlcr", 0, INLCR },
107 { "igncr", IGNCR, 0 },
108 { "-igncr", 0, IGNCR },
109 { "icrnl", ICRNL, 0 },
110 { "-icrnl", 0, ICRNL },
111 { "ixon", IXON, 0 },
112 { "-ixon", 0, IXON },
113 { "flow", IXON, 0 },
114 { "-flow", 0, IXON },
115 { "ixoff", IXOFF, 0 },
116 { "-ixoff", 0, IXOFF },
117 { "tandem", IXOFF, 0 },
118 { "-tandem", 0, IXOFF },
119 { "ixany", IXANY, 0 },
120 { "-ixany", 0, IXANY },
121 { "decctlq", 0, IXANY },
122 { "-decctlq", IXANY, 0 },
123 { "imaxbel", IMAXBEL, 0 },
124 { "-imaxbel", 0, IMAXBEL },
125 { "iutf8", IUTF8, 0 },
126 { "-iutf8", 0, IUTF8 },
127 { NULL, 0, 0 },
128 };
129
130 static const 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 static const 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 { "ocrnl", OCRNL, 0 },
190 { "-ocrnl", 0, OCRNL },
191 { "tabs", TAB0, TABDLY }, /* "preserve" tabs */
192 { "-tabs", TAB3, TABDLY },
193 { "oxtabs", TAB3, TABDLY },
194 { "-oxtabs", TAB0, TABDLY },
195 { "tab0", TAB0, TABDLY },
196 { "tab3", TAB3, TABDLY },
197 { "onocr", ONOCR, 0 },
198 { "-onocr", 0, ONOCR },
199 { "onlret", ONLRET, 0 },
200 { "-onlret", 0, ONLRET },
201 { NULL, 0, 0 },
202 };
203
204 #define CHK(s) (*name == s[0] && !strcmp(name, s))
205
206 int
msearch(char *** argvp,struct info * ip)207 msearch(char ***argvp, struct info *ip)
208 {
209 const struct modes *mp;
210 char *name;
211
212 name = **argvp;
213
214 for (mp = cmodes; mp->name; ++mp)
215 if (CHK(mp->name)) {
216 ip->t.c_cflag &= ~mp->unset;
217 ip->t.c_cflag |= mp->set;
218 ip->set = 1;
219 return (1);
220 }
221 for (mp = imodes; mp->name; ++mp)
222 if (CHK(mp->name)) {
223 ip->t.c_iflag &= ~mp->unset;
224 ip->t.c_iflag |= mp->set;
225 ip->set = 1;
226 return (1);
227 }
228 for (mp = lmodes; mp->name; ++mp)
229 if (CHK(mp->name)) {
230 ip->t.c_lflag &= ~mp->unset;
231 ip->t.c_lflag |= mp->set;
232 ip->set = 1;
233 return (1);
234 }
235 for (mp = omodes; mp->name; ++mp)
236 if (CHK(mp->name)) {
237 ip->t.c_oflag &= ~mp->unset;
238 ip->t.c_oflag |= mp->set;
239 ip->set = 1;
240 return (1);
241 }
242 return (0);
243 }
244