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