xref: /dragonfly/bin/stty/modes.c (revision d50f9ae3)
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  */
32 
33 #include <sys/types.h>
34 #include <stddef.h>
35 #include <string.h>
36 #include "stty.h"
37 
38 int msearch (char ***, struct info *);
39 
40 struct modes {
41 	const char *name;
42 	long set;
43 	long unset;
44 };
45 
46 /*
47  * The code in optlist() depends on minus options following regular
48  * options, i.e. "foo" must immediately precede "-foo".
49  */
50 static struct modes cmodes[] = {
51 	{ "cs5",	CS5, CSIZE },
52 	{ "cs6",	CS6, CSIZE },
53 	{ "cs7",	CS7, CSIZE },
54 	{ "cs8",	CS8, CSIZE },
55 	{ "cstopb",	CSTOPB, 0 },
56 	{ "-cstopb",	0, CSTOPB },
57 	{ "cread",	CREAD, 0 },
58 	{ "-cread",	0, CREAD },
59 	{ "parenb",	PARENB, 0 },
60 	{ "-parenb",	0, PARENB },
61 	{ "parodd",	PARODD, 0 },
62 	{ "-parodd",	0, PARODD },
63 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
64 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
65 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
66 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
67 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
68 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
69 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
70 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
71 	{ "hupcl",	HUPCL, 0 },
72 	{ "-hupcl",	0, HUPCL },
73 	{ "hup",	HUPCL, 0 },
74 	{ "-hup",	0, HUPCL },
75 	{ "clocal",	CLOCAL, 0 },
76 	{ "-clocal",	0, CLOCAL },
77 	{ "crtscts",	CRTSCTS, 0 },
78 	{ "-crtscts",	0, CRTSCTS },
79 	{ "ctsflow",	CCTS_OFLOW, 0 },
80 	{ "-ctsflow",	0, CCTS_OFLOW },
81 	{ "dsrflow",	CDSR_OFLOW, 0 },
82 	{ "-dsrflow",	0, CDSR_OFLOW },
83 	{ "dtrflow",	CDTR_IFLOW, 0 },
84 	{ "-dtrflow",	0, CDTR_IFLOW },
85 	{ "rtsflow",	CRTS_IFLOW, 0 },
86 	{ "-rtsflow",	0, CRTS_IFLOW },
87 	{ "mdmbuf",	MDMBUF, 0 },
88 	{ "-mdmbuf",	0, MDMBUF },
89 	{ NULL,		0, 0 },
90 };
91 
92 static struct modes imodes[] = {
93 	{ "ignbrk",	IGNBRK, 0 },
94 	{ "-ignbrk",	0, IGNBRK },
95 	{ "brkint",	BRKINT, 0 },
96 	{ "-brkint",	0, BRKINT },
97 	{ "ignpar",	IGNPAR, 0 },
98 	{ "-ignpar",	0, IGNPAR },
99 	{ "parmrk",	PARMRK, 0 },
100 	{ "-parmrk",	0, PARMRK },
101 	{ "inpck",	INPCK, 0 },
102 	{ "-inpck",	0, INPCK },
103 	{ "istrip",	ISTRIP, 0 },
104 	{ "-istrip",	0, ISTRIP },
105 	{ "inlcr",	INLCR, 0 },
106 	{ "-inlcr",	0, INLCR },
107 	{ "igncr",	IGNCR, 0 },
108 	{ "-igncr",	0, IGNCR },
109 	{ "icrnl",	ICRNL, 0 },
110 	{ "-icrnl",	0, ICRNL },
111 	{ "ixon",	IXON, 0 },
112 	{ "-ixon",	0, IXON },
113 	{ "flow",	IXON, 0 },
114 	{ "-flow",	0, IXON },
115 	{ "ixoff",	IXOFF, 0 },
116 	{ "-ixoff",	0, IXOFF },
117 	{ "tandem",	IXOFF, 0 },
118 	{ "-tandem",	0, IXOFF },
119 	{ "ixany",	IXANY, 0 },
120 	{ "-ixany",	0, IXANY },
121 	{ "decctlq",	0, IXANY },
122 	{ "-decctlq",	IXANY, 0 },
123 	{ "imaxbel",	IMAXBEL, 0 },
124 	{ "-imaxbel",	0, IMAXBEL },
125 	{ NULL,		0, 0 },
126 };
127 
128 static 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 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
msearch(char *** argvp,struct info * ip)205 msearch(char ***argvp, struct info *ip)
206 {
207 	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