xref: /netbsd/bin/stty/modes.c (revision bf9ec67e)
1 /*	$NetBSD: modes.c,v 1.14 1998/07/28 11:40:59 mycroft Exp $	*/
2 
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
40 #else
41 __RCSID("$NetBSD: modes.c,v 1.14 1998/07/28 11:40:59 mycroft Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <stddef.h>
47 #include <string.h>
48 #include "stty.h"
49 #include "extern.h"
50 
51 struct modes {
52 	const char *name;
53 	tcflag_t set;
54 	tcflag_t unset;
55 };
56 
57 /*
58  * The code in optlist() depends on minus options following regular
59  * options, i.e. "foo" must immediately precede "-foo".
60  */
61 const struct modes cmodes[] = {
62 	{ "cs5",	CS5, CSIZE },
63 	{ "cs6",	CS6, CSIZE },
64 	{ "cs7",	CS7, CSIZE },
65 	{ "cs8",	CS8, CSIZE },
66 	{ "cstopb",	CSTOPB, 0 },
67 	{ "-cstopb",	0, CSTOPB },
68 	{ "cread",	CREAD, 0 },
69 	{ "-cread",	0, CREAD },
70 	{ "parenb",	PARENB, 0 },
71 	{ "-parenb",	0, PARENB },
72 	{ "parodd",	PARODD, 0 },
73 	{ "-parodd",	0, PARODD },
74 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
75 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
76 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
77 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
78 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
79 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
80 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
81 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
82 	{ "hupcl",	HUPCL, 0 },
83 	{ "-hupcl",	0, HUPCL },
84 	{ "hup",	HUPCL, 0 },
85 	{ "-hup",	0, HUPCL },
86 	{ "clocal",	CLOCAL, 0 },
87 	{ "-clocal",	0, CLOCAL },
88 	{ "crtscts",	CRTSCTS, 0 },
89 	{ "-crtscts",	0, CRTSCTS },
90 	{ "mdmbuf",	MDMBUF, 0 },
91 	{ "-mdmbuf",	0, MDMBUF },
92 	{ "cdtrcts",	CDTRCTS, 0 },
93 	{ "-cdtrcts",	0, CDTRCTS },
94 	{ NULL },
95 };
96 
97 const struct modes imodes[] = {
98 	{ "ignbrk",	IGNBRK, 0 },
99 	{ "-ignbrk",	0, IGNBRK },
100 	{ "brkint",	BRKINT, 0 },
101 	{ "-brkint",	0, BRKINT },
102 	{ "ignpar",	IGNPAR, 0 },
103 	{ "-ignpar",	0, IGNPAR },
104 	{ "parmrk",	PARMRK, 0 },
105 	{ "-parmrk",	0, PARMRK },
106 	{ "inpck",	INPCK, 0 },
107 	{ "-inpck",	0, INPCK },
108 	{ "istrip",	ISTRIP, 0 },
109 	{ "-istrip",	0, ISTRIP },
110 	{ "inlcr",	INLCR, 0 },
111 	{ "-inlcr",	0, INLCR },
112 	{ "igncr",	IGNCR, 0 },
113 	{ "-igncr",	0, IGNCR },
114 	{ "icrnl",	ICRNL, 0 },
115 	{ "-icrnl",	0, ICRNL },
116 	{ "ixon",	IXON, 0 },
117 	{ "-ixon",	0, IXON },
118 	{ "flow",	IXON, 0 },
119 	{ "-flow",	0, IXON },
120 	{ "ixoff",	IXOFF, 0 },
121 	{ "-ixoff",	0, IXOFF },
122 	{ "tandem",	IXOFF, 0 },
123 	{ "-tandem",	0, IXOFF },
124 	{ "ixany",	IXANY, 0 },
125 	{ "-ixany",	0, IXANY },
126 	{ "decctlq",	0, IXANY },
127 	{ "-decctlq",	IXANY, 0 },
128 	{ "imaxbel",	IMAXBEL, 0 },
129 	{ "-imaxbel",	0, IMAXBEL },
130 	{ NULL },
131 };
132 
133 const struct modes lmodes[] = {
134 	{ "echo",	ECHO, 0 },
135 	{ "-echo",	0, ECHO },
136 	{ "echoe",	ECHOE, 0 },
137 	{ "-echoe",	0, ECHOE },
138 	{ "crterase",	ECHOE, 0 },
139 	{ "-crterase",	0, ECHOE },
140 	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
141 	{ "-crtbs",	0, ECHOE },
142 	{ "echok",	ECHOK, 0 },
143 	{ "-echok",	0, ECHOK },
144 	{ "echoke",	ECHOKE, 0 },
145 	{ "-echoke",	0, ECHOKE },
146 	{ "crtkill",	ECHOKE, 0 },
147 	{ "-crtkill",	0, ECHOKE },
148 	{ "altwerase",	ALTWERASE, 0 },
149 	{ "-altwerase",	0, ALTWERASE },
150 	{ "iexten",	IEXTEN, 0 },
151 	{ "-iexten",	0, IEXTEN },
152 	{ "echonl",	ECHONL, 0 },
153 	{ "-echonl",	0, ECHONL },
154 	{ "echoctl",	ECHOCTL, 0 },
155 	{ "-echoctl",	0, ECHOCTL },
156 	{ "ctlecho",	ECHOCTL, 0 },
157 	{ "-ctlecho",	0, ECHOCTL },
158 	{ "echoprt",	ECHOPRT, 0 },
159 	{ "-echoprt",	0, ECHOPRT },
160 	{ "prterase",	ECHOPRT, 0 },
161 	{ "-prterase",	0, ECHOPRT },
162 	{ "isig",	ISIG, 0 },
163 	{ "-isig",	0, ISIG },
164 	{ "icanon",	ICANON, 0 },
165 	{ "-icanon",	0, ICANON },
166 	{ "noflsh",	NOFLSH, 0 },
167 	{ "-noflsh",	0, NOFLSH },
168 	{ "tostop",	TOSTOP, 0 },
169 	{ "-tostop",	0, TOSTOP },
170 	{ "flusho",	FLUSHO, 0 },
171 	{ "-flusho",	0, FLUSHO },
172 	{ "pendin",	PENDIN, 0 },
173 	{ "-pendin",	0, PENDIN },
174 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
175 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
176 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
177 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
178 	{ "nokerninfo",	NOKERNINFO, 0 },
179 	{ "-nokerninfo",0, NOKERNINFO },
180 	{ "kerninfo",	0, NOKERNINFO },
181 	{ "-kerninfo",	NOKERNINFO, 0 },
182 	{ NULL },
183 };
184 
185 const struct modes omodes[] = {
186 	{ "opost",	OPOST, 0 },
187 	{ "-opost",	0, OPOST },
188 	{ "litout",	0, OPOST },
189 	{ "-litout",	OPOST, 0 },
190 	{ "onlcr",	ONLCR, 0 },
191 	{ "-onlcr",	0, ONLCR },
192 	{ "ocrnl",	OCRNL, 0 },
193 	{ "-ocrnl",	0, OCRNL },
194 	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
195 	{ "-tabs",	OXTABS, 0 },
196 	{ "oxtabs",	OXTABS, 0 },
197 	{ "-oxtabs",	0, OXTABS },
198 	{ "onocr",	ONOCR, 0 },
199 	{ "-onocr",	0, ONOCR },
200 	{ "onlret",	ONLRET, 0 },
201 	{ "-onlret",	0, ONLRET },
202 	{ NULL },
203 };
204 
205 #define	CHK(s)	(!strcmp(name, s))
206 
207 int
208 msearch(argvp, ip)
209 	char ***argvp;
210 	struct info *ip;
211 {
212 	const struct modes *mp;
213 	char *name;
214 
215 	name = **argvp;
216 
217 	for (mp = cmodes; mp->name; ++mp)
218 		if (CHK(mp->name)) {
219 			ip->t.c_cflag &= ~mp->unset;
220 			ip->t.c_cflag |= mp->set;
221 			ip->set = 1;
222 			return (1);
223 		}
224 	for (mp = imodes; mp->name; ++mp)
225 		if (CHK(mp->name)) {
226 			ip->t.c_iflag &= ~mp->unset;
227 			ip->t.c_iflag |= mp->set;
228 			ip->set = 1;
229 			return (1);
230 		}
231 	for (mp = lmodes; mp->name; ++mp)
232 		if (CHK(mp->name)) {
233 			ip->t.c_lflag &= ~mp->unset;
234 			ip->t.c_lflag |= mp->set;
235 			ip->set = 1;
236 			return (1);
237 		}
238 	for (mp = omodes; mp->name; ++mp)
239 		if (CHK(mp->name)) {
240 			ip->t.c_oflag &= ~mp->unset;
241 			ip->t.c_oflag |= mp->set;
242 			ip->set = 1;
243 			return (1);
244 		}
245 	return (0);
246 }
247