xref: /freebsd/bin/stty/modes.c (revision 7b44b809619b96eae54d596e01b859752bcd3733)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
134b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
144b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
154b88c807SRodney W. Grimes  *    without specific prior written permission.
164b88c807SRodney W. Grimes  *
174b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
184b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
214b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274b88c807SRodney W. Grimes  * SUCH DAMAGE.
284b88c807SRodney W. Grimes  */
294b88c807SRodney W. Grimes 
304b88c807SRodney W. Grimes #ifndef lint
319ba8bd65SPhilippe Charnier #if 0
329ba8bd65SPhilippe Charnier static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
339ba8bd65SPhilippe Charnier #endif
344b88c807SRodney W. Grimes #endif /* not lint */
352749b141SDavid E. O'Brien #include <sys/cdefs.h>
362749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
374b88c807SRodney W. Grimes 
384b88c807SRodney W. Grimes #include <sys/types.h>
394b88c807SRodney W. Grimes #include <stddef.h>
404b88c807SRodney W. Grimes #include <string.h>
414b88c807SRodney W. Grimes #include "stty.h"
424b88c807SRodney W. Grimes 
435134c3f7SWarner Losh int msearch(char ***, struct info *);
445e5a5667SKris Kennaway 
454b88c807SRodney W. Grimes struct modes {
465e5a5667SKris Kennaway 	const char *name;
474b88c807SRodney W. Grimes 	long set;
484b88c807SRodney W. Grimes 	long unset;
494b88c807SRodney W. Grimes };
504b88c807SRodney W. Grimes 
514b88c807SRodney W. Grimes /*
524b88c807SRodney W. Grimes  * The code in optlist() depends on minus options following regular
534b88c807SRodney W. Grimes  * options, i.e. "foo" must immediately precede "-foo".
544b88c807SRodney W. Grimes  */
55*7b44b809SEd Schouten static const struct modes cmodes[] = {
564b88c807SRodney W. Grimes 	{ "cs5",	CS5, CSIZE },
574b88c807SRodney W. Grimes 	{ "cs6",	CS6, CSIZE },
584b88c807SRodney W. Grimes 	{ "cs7",	CS7, CSIZE },
594b88c807SRodney W. Grimes 	{ "cs8",	CS8, CSIZE },
604b88c807SRodney W. Grimes 	{ "cstopb",	CSTOPB, 0 },
614b88c807SRodney W. Grimes 	{ "-cstopb",	0, CSTOPB },
624b88c807SRodney W. Grimes 	{ "cread",	CREAD, 0 },
634b88c807SRodney W. Grimes 	{ "-cread",	0, CREAD },
644b88c807SRodney W. Grimes 	{ "parenb",	PARENB, 0 },
654b88c807SRodney W. Grimes 	{ "-parenb",	0, PARENB },
664b88c807SRodney W. Grimes 	{ "parodd",	PARODD, 0 },
674b88c807SRodney W. Grimes 	{ "-parodd",	0, PARODD },
684b88c807SRodney W. Grimes 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
694b88c807SRodney W. Grimes 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
704b88c807SRodney W. Grimes 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
714b88c807SRodney W. Grimes 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
724b88c807SRodney W. Grimes 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
734b88c807SRodney W. Grimes 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
744b88c807SRodney W. Grimes 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
754b88c807SRodney W. Grimes 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
764b88c807SRodney W. Grimes 	{ "hupcl",	HUPCL, 0 },
774b88c807SRodney W. Grimes 	{ "-hupcl",	0, HUPCL },
784b88c807SRodney W. Grimes 	{ "hup",	HUPCL, 0 },
794b88c807SRodney W. Grimes 	{ "-hup",	0, HUPCL },
804b88c807SRodney W. Grimes 	{ "clocal",	CLOCAL, 0 },
814b88c807SRodney W. Grimes 	{ "-clocal",	0, CLOCAL },
824b88c807SRodney W. Grimes 	{ "crtscts",	CRTSCTS, 0 },
834b88c807SRodney W. Grimes 	{ "-crtscts",	0, CRTSCTS },
842bf5814aSBruce Evans 	{ "ctsflow",	CCTS_OFLOW, 0 },
852bf5814aSBruce Evans 	{ "-ctsflow",	0, CCTS_OFLOW },
862bf5814aSBruce Evans 	{ "dsrflow",	CDSR_OFLOW, 0 },
872bf5814aSBruce Evans 	{ "-dsrflow",	0, CDSR_OFLOW },
882bf5814aSBruce Evans 	{ "dtrflow",	CDTR_IFLOW, 0 },
892bf5814aSBruce Evans 	{ "-dtrflow",	0, CDTR_IFLOW },
902bf5814aSBruce Evans 	{ "rtsflow",	CRTS_IFLOW, 0 },
912bf5814aSBruce Evans 	{ "-rtsflow",	0, CRTS_IFLOW },
924b88c807SRodney W. Grimes 	{ "mdmbuf",	MDMBUF, 0 },
934b88c807SRodney W. Grimes 	{ "-mdmbuf",	0, MDMBUF },
945e5a5667SKris Kennaway 	{ NULL,		0, 0 },
954b88c807SRodney W. Grimes };
964b88c807SRodney W. Grimes 
97*7b44b809SEd Schouten static const struct modes imodes[] = {
984b88c807SRodney W. Grimes 	{ "ignbrk",	IGNBRK, 0 },
994b88c807SRodney W. Grimes 	{ "-ignbrk",	0, IGNBRK },
1004b88c807SRodney W. Grimes 	{ "brkint",	BRKINT, 0 },
1014b88c807SRodney W. Grimes 	{ "-brkint",	0, BRKINT },
1024b88c807SRodney W. Grimes 	{ "ignpar",	IGNPAR, 0 },
1034b88c807SRodney W. Grimes 	{ "-ignpar",	0, IGNPAR },
1044b88c807SRodney W. Grimes 	{ "parmrk",	PARMRK, 0 },
1054b88c807SRodney W. Grimes 	{ "-parmrk",	0, PARMRK },
1064b88c807SRodney W. Grimes 	{ "inpck",	INPCK, 0 },
1074b88c807SRodney W. Grimes 	{ "-inpck",	0, INPCK },
1084b88c807SRodney W. Grimes 	{ "istrip",	ISTRIP, 0 },
1094b88c807SRodney W. Grimes 	{ "-istrip",	0, ISTRIP },
1104b88c807SRodney W. Grimes 	{ "inlcr",	INLCR, 0 },
1114b88c807SRodney W. Grimes 	{ "-inlcr",	0, INLCR },
1124b88c807SRodney W. Grimes 	{ "igncr",	IGNCR, 0 },
1134b88c807SRodney W. Grimes 	{ "-igncr",	0, IGNCR },
1144b88c807SRodney W. Grimes 	{ "icrnl",	ICRNL, 0 },
1154b88c807SRodney W. Grimes 	{ "-icrnl",	0, ICRNL },
1164b88c807SRodney W. Grimes 	{ "ixon",	IXON, 0 },
1174b88c807SRodney W. Grimes 	{ "-ixon",	0, IXON },
1184b88c807SRodney W. Grimes 	{ "flow",	IXON, 0 },
1194b88c807SRodney W. Grimes 	{ "-flow",	0, IXON },
1204b88c807SRodney W. Grimes 	{ "ixoff",	IXOFF, 0 },
1214b88c807SRodney W. Grimes 	{ "-ixoff",	0, IXOFF },
1224b88c807SRodney W. Grimes 	{ "tandem",	IXOFF, 0 },
1234b88c807SRodney W. Grimes 	{ "-tandem",	0, IXOFF },
1244b88c807SRodney W. Grimes 	{ "ixany",	IXANY, 0 },
1254b88c807SRodney W. Grimes 	{ "-ixany",	0, IXANY },
1264b88c807SRodney W. Grimes 	{ "decctlq",	0, IXANY },
1274b88c807SRodney W. Grimes 	{ "-decctlq",	IXANY, 0 },
1284b88c807SRodney W. Grimes 	{ "imaxbel",	IMAXBEL, 0 },
1294b88c807SRodney W. Grimes 	{ "-imaxbel",	0, IMAXBEL },
1305e5a5667SKris Kennaway 	{ NULL,		0, 0 },
1314b88c807SRodney W. Grimes };
1324b88c807SRodney W. Grimes 
133*7b44b809SEd Schouten static const struct modes lmodes[] = {
1344b88c807SRodney W. Grimes 	{ "echo",	ECHO, 0 },
1354b88c807SRodney W. Grimes 	{ "-echo",	0, ECHO },
1364b88c807SRodney W. Grimes 	{ "echoe",	ECHOE, 0 },
1374b88c807SRodney W. Grimes 	{ "-echoe",	0, ECHOE },
1384b88c807SRodney W. Grimes 	{ "crterase",	ECHOE, 0 },
1394b88c807SRodney W. Grimes 	{ "-crterase",	0, ECHOE },
1404b88c807SRodney W. Grimes 	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
1414b88c807SRodney W. Grimes 	{ "-crtbs",	0, ECHOE },
1424b88c807SRodney W. Grimes 	{ "echok",	ECHOK, 0 },
1434b88c807SRodney W. Grimes 	{ "-echok",	0, ECHOK },
1444b88c807SRodney W. Grimes 	{ "echoke",	ECHOKE, 0 },
1454b88c807SRodney W. Grimes 	{ "-echoke",	0, ECHOKE },
1464b88c807SRodney W. Grimes 	{ "crtkill",	ECHOKE, 0 },
1474b88c807SRodney W. Grimes 	{ "-crtkill",	0, ECHOKE },
1484b88c807SRodney W. Grimes 	{ "altwerase",	ALTWERASE, 0 },
1494b88c807SRodney W. Grimes 	{ "-altwerase",	0, ALTWERASE },
1504b88c807SRodney W. Grimes 	{ "iexten",	IEXTEN, 0 },
1514b88c807SRodney W. Grimes 	{ "-iexten",	0, IEXTEN },
1524b88c807SRodney W. Grimes 	{ "echonl",	ECHONL, 0 },
1534b88c807SRodney W. Grimes 	{ "-echonl",	0, ECHONL },
1544b88c807SRodney W. Grimes 	{ "echoctl",	ECHOCTL, 0 },
1554b88c807SRodney W. Grimes 	{ "-echoctl",	0, ECHOCTL },
1564b88c807SRodney W. Grimes 	{ "ctlecho",	ECHOCTL, 0 },
1574b88c807SRodney W. Grimes 	{ "-ctlecho",	0, ECHOCTL },
1584b88c807SRodney W. Grimes 	{ "echoprt",	ECHOPRT, 0 },
1594b88c807SRodney W. Grimes 	{ "-echoprt",	0, ECHOPRT },
1604b88c807SRodney W. Grimes 	{ "prterase",	ECHOPRT, 0 },
1614b88c807SRodney W. Grimes 	{ "-prterase",	0, ECHOPRT },
1624b88c807SRodney W. Grimes 	{ "isig",	ISIG, 0 },
1634b88c807SRodney W. Grimes 	{ "-isig",	0, ISIG },
1644b88c807SRodney W. Grimes 	{ "icanon",	ICANON, 0 },
1654b88c807SRodney W. Grimes 	{ "-icanon",	0, ICANON },
1664b88c807SRodney W. Grimes 	{ "noflsh",	NOFLSH, 0 },
1674b88c807SRodney W. Grimes 	{ "-noflsh",	0, NOFLSH },
1684b88c807SRodney W. Grimes 	{ "tostop",	TOSTOP, 0 },
1694b88c807SRodney W. Grimes 	{ "-tostop",	0, TOSTOP },
1704b88c807SRodney W. Grimes 	{ "flusho",	FLUSHO, 0 },
1714b88c807SRodney W. Grimes 	{ "-flusho",	0, FLUSHO },
1724b88c807SRodney W. Grimes 	{ "pendin",	PENDIN, 0 },
1734b88c807SRodney W. Grimes 	{ "-pendin",	0, PENDIN },
1744b88c807SRodney W. Grimes 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
1754b88c807SRodney W. Grimes 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
1764b88c807SRodney W. Grimes 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
1774b88c807SRodney W. Grimes 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
1784b88c807SRodney W. Grimes 	{ "nokerninfo",	NOKERNINFO, 0 },
1794b88c807SRodney W. Grimes 	{ "-nokerninfo",0, NOKERNINFO },
1804b88c807SRodney W. Grimes 	{ "kerninfo",	0, NOKERNINFO },
1814b88c807SRodney W. Grimes 	{ "-kerninfo",	NOKERNINFO, 0 },
1825e5a5667SKris Kennaway 	{ NULL,		0, 0 },
1834b88c807SRodney W. Grimes };
1844b88c807SRodney W. Grimes 
185*7b44b809SEd Schouten static const struct modes omodes[] = {
1864b88c807SRodney W. Grimes 	{ "opost",	OPOST, 0 },
1874b88c807SRodney W. Grimes 	{ "-opost",	0, OPOST },
1884b88c807SRodney W. Grimes 	{ "litout",	0, OPOST },
1894b88c807SRodney W. Grimes 	{ "-litout",	OPOST, 0 },
1904b88c807SRodney W. Grimes 	{ "onlcr",	ONLCR, 0 },
1914b88c807SRodney W. Grimes 	{ "-onlcr",	0, ONLCR },
1923617ddfcSAssar Westerlund 	{ "ocrnl",	OCRNL, 0 },
1933617ddfcSAssar Westerlund 	{ "-ocrnl",	0, OCRNL },
194f8f8c9f0SEd Schouten 	{ "tabs",	TAB0, TABDLY },		/* "preserve" tabs */
195f8f8c9f0SEd Schouten 	{ "-tabs",	TAB3, TABDLY },
196f8f8c9f0SEd Schouten 	{ "oxtabs",	TAB3, TABDLY },
197f8f8c9f0SEd Schouten 	{ "-oxtabs",	TAB0, TABDLY },
198f8f8c9f0SEd Schouten 	{ "tab0",	TAB0, TABDLY },
199f8f8c9f0SEd Schouten 	{ "tab3",	TAB3, TABDLY },
2003617ddfcSAssar Westerlund 	{ "onocr",	ONOCR, 0 },
2013617ddfcSAssar Westerlund 	{ "-onocr",	0, ONOCR },
2023617ddfcSAssar Westerlund 	{ "onlret",	ONLRET, 0 },
2033617ddfcSAssar Westerlund 	{ "-onlret",	0, ONLRET },
2045e5a5667SKris Kennaway 	{ NULL,		0, 0 },
2054b88c807SRodney W. Grimes };
2064b88c807SRodney W. Grimes 
2074b88c807SRodney W. Grimes #define	CHK(s)	(*name == s[0] && !strcmp(name, s))
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes int
2105134c3f7SWarner Losh msearch(char ***argvp, struct info *ip)
2114b88c807SRodney W. Grimes {
212*7b44b809SEd Schouten 	const struct modes *mp;
2134b88c807SRodney W. Grimes 	char *name;
2144b88c807SRodney W. Grimes 
2154b88c807SRodney W. Grimes 	name = **argvp;
2164b88c807SRodney W. Grimes 
2174b88c807SRodney W. Grimes 	for (mp = cmodes; mp->name; ++mp)
2184b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2194b88c807SRodney W. Grimes 			ip->t.c_cflag &= ~mp->unset;
2204b88c807SRodney W. Grimes 			ip->t.c_cflag |= mp->set;
2214b88c807SRodney W. Grimes 			ip->set = 1;
2224b88c807SRodney W. Grimes 			return (1);
2234b88c807SRodney W. Grimes 		}
2244b88c807SRodney W. Grimes 	for (mp = imodes; mp->name; ++mp)
2254b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2264b88c807SRodney W. Grimes 			ip->t.c_iflag &= ~mp->unset;
2274b88c807SRodney W. Grimes 			ip->t.c_iflag |= mp->set;
2284b88c807SRodney W. Grimes 			ip->set = 1;
2294b88c807SRodney W. Grimes 			return (1);
2304b88c807SRodney W. Grimes 		}
2314b88c807SRodney W. Grimes 	for (mp = lmodes; mp->name; ++mp)
2324b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2334b88c807SRodney W. Grimes 			ip->t.c_lflag &= ~mp->unset;
2344b88c807SRodney W. Grimes 			ip->t.c_lflag |= mp->set;
2354b88c807SRodney W. Grimes 			ip->set = 1;
2364b88c807SRodney W. Grimes 			return (1);
2374b88c807SRodney W. Grimes 		}
2384b88c807SRodney W. Grimes 	for (mp = omodes; mp->name; ++mp)
2394b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2404b88c807SRodney W. Grimes 			ip->t.c_oflag &= ~mp->unset;
2414b88c807SRodney W. Grimes 			ip->t.c_oflag |= mp->set;
2424b88c807SRodney W. Grimes 			ip->set = 1;
2434b88c807SRodney W. Grimes 			return (1);
2444b88c807SRodney W. Grimes 		}
2454b88c807SRodney W. Grimes 	return (0);
2464b88c807SRodney W. Grimes }
247