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