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