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