modes.c revision 15032
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. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	$Id: modes.c,v 1.3 1995/04/29 15:17:17 bde Exp $
34 */
35
36#ifndef lint
37static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
38#endif /* not lint */
39
40#include <stddef.h>
41#include <string.h>
42#include <termios.h>
43
44struct modes {
45	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 */
54struct 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	{ NULL },
94};
95
96struct 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 },
130};
131
132struct 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 },
182};
183
184struct 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	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
192	{ "-tabs",	OXTABS, 0 },
193	{ "oxtabs",	OXTABS, 0 },
194	{ "-oxtabs",	0, OXTABS },
195	{ NULL },
196};
197
198#define	CHK(name, s)	(*name == s[0] && !strcmp(name, s))
199
200int
201msearch(str, ip)
202	char *str;
203	struct termios *ip;
204{
205	struct modes *mp;
206
207	for (mp = cmodes; mp->name; ++mp)
208		if (CHK(str, mp->name)) {
209			ip->c_cflag &= ~mp->unset;
210			ip->c_cflag |= mp->set;
211			return (1);
212		}
213	for (mp = imodes; mp->name; ++mp)
214		if (CHK(str, mp->name)) {
215			ip->c_iflag &= ~mp->unset;
216			ip->c_iflag |= mp->set;
217			return (1);
218		}
219	for (mp = lmodes; mp->name; ++mp)
220		if (CHK(str, mp->name)) {
221			ip->c_lflag &= ~mp->unset;
222			ip->c_lflag |= mp->set;
223			return (1);
224		}
225	for (mp = omodes; mp->name; ++mp)
226		if (CHK(str, mp->name)) {
227			ip->c_oflag &= ~mp->unset;
228			ip->c_oflag |= mp->set;
229			return (1);
230		}
231	return (0);
232}
233