xref: /freebsd/bin/stty/modes.c (revision 5e5a566754837a6015072f28964c2583b68ea63f)
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  * 3. All advertising materials mentioning features or use of this software
144b88c807SRodney W. Grimes  *    must display the following acknowledgement:
154b88c807SRodney W. Grimes  *	This product includes software developed by the University of
164b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
344b88c807SRodney W. Grimes #ifndef lint
359ba8bd65SPhilippe Charnier #if 0
369ba8bd65SPhilippe Charnier static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
379ba8bd65SPhilippe Charnier #endif
389ba8bd65SPhilippe Charnier static const char rcsid[] =
392a456239SPeter Wemm   "$FreeBSD$";
404b88c807SRodney W. Grimes #endif /* not lint */
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include <sys/types.h>
434b88c807SRodney W. Grimes #include <stddef.h>
444b88c807SRodney W. Grimes #include <string.h>
454b88c807SRodney W. Grimes #include "stty.h"
464b88c807SRodney W. Grimes 
475e5a5667SKris Kennaway int msearch __P((char ***, struct info *));
485e5a5667SKris Kennaway 
494b88c807SRodney W. Grimes struct modes {
505e5a5667SKris Kennaway 	const char *name;
514b88c807SRodney W. Grimes 	long set;
524b88c807SRodney W. Grimes 	long unset;
534b88c807SRodney W. Grimes };
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes /*
564b88c807SRodney W. Grimes  * The code in optlist() depends on minus options following regular
574b88c807SRodney W. Grimes  * options, i.e. "foo" must immediately precede "-foo".
584b88c807SRodney W. Grimes  */
594b88c807SRodney W. Grimes struct modes cmodes[] = {
604b88c807SRodney W. Grimes 	{ "cs5",	CS5, CSIZE },
614b88c807SRodney W. Grimes 	{ "cs6",	CS6, CSIZE },
624b88c807SRodney W. Grimes 	{ "cs7",	CS7, CSIZE },
634b88c807SRodney W. Grimes 	{ "cs8",	CS8, CSIZE },
644b88c807SRodney W. Grimes 	{ "cstopb",	CSTOPB, 0 },
654b88c807SRodney W. Grimes 	{ "-cstopb",	0, CSTOPB },
664b88c807SRodney W. Grimes 	{ "cread",	CREAD, 0 },
674b88c807SRodney W. Grimes 	{ "-cread",	0, CREAD },
684b88c807SRodney W. Grimes 	{ "parenb",	PARENB, 0 },
694b88c807SRodney W. Grimes 	{ "-parenb",	0, PARENB },
704b88c807SRodney W. Grimes 	{ "parodd",	PARODD, 0 },
714b88c807SRodney W. Grimes 	{ "-parodd",	0, PARODD },
724b88c807SRodney W. Grimes 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
734b88c807SRodney W. Grimes 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
744b88c807SRodney W. Grimes 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
754b88c807SRodney W. Grimes 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
764b88c807SRodney W. Grimes 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
774b88c807SRodney W. Grimes 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
784b88c807SRodney W. Grimes 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
794b88c807SRodney W. Grimes 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
804b88c807SRodney W. Grimes 	{ "hupcl",	HUPCL, 0 },
814b88c807SRodney W. Grimes 	{ "-hupcl",	0, HUPCL },
824b88c807SRodney W. Grimes 	{ "hup",	HUPCL, 0 },
834b88c807SRodney W. Grimes 	{ "-hup",	0, HUPCL },
844b88c807SRodney W. Grimes 	{ "clocal",	CLOCAL, 0 },
854b88c807SRodney W. Grimes 	{ "-clocal",	0, CLOCAL },
864b88c807SRodney W. Grimes 	{ "crtscts",	CRTSCTS, 0 },
874b88c807SRodney W. Grimes 	{ "-crtscts",	0, CRTSCTS },
882bf5814aSBruce Evans 	{ "ctsflow",	CCTS_OFLOW, 0 },
892bf5814aSBruce Evans 	{ "-ctsflow",	0, CCTS_OFLOW },
902bf5814aSBruce Evans 	{ "dsrflow",	CDSR_OFLOW, 0 },
912bf5814aSBruce Evans 	{ "-dsrflow",	0, CDSR_OFLOW },
922bf5814aSBruce Evans 	{ "dtrflow",	CDTR_IFLOW, 0 },
932bf5814aSBruce Evans 	{ "-dtrflow",	0, CDTR_IFLOW },
942bf5814aSBruce Evans 	{ "rtsflow",	CRTS_IFLOW, 0 },
952bf5814aSBruce Evans 	{ "-rtsflow",	0, CRTS_IFLOW },
964b88c807SRodney W. Grimes 	{ "mdmbuf",	MDMBUF, 0 },
974b88c807SRodney W. Grimes 	{ "-mdmbuf",	0, MDMBUF },
985e5a5667SKris Kennaway 	{ NULL,		0, 0 },
994b88c807SRodney W. Grimes };
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes struct modes imodes[] = {
1024b88c807SRodney W. Grimes 	{ "ignbrk",	IGNBRK, 0 },
1034b88c807SRodney W. Grimes 	{ "-ignbrk",	0, IGNBRK },
1044b88c807SRodney W. Grimes 	{ "brkint",	BRKINT, 0 },
1054b88c807SRodney W. Grimes 	{ "-brkint",	0, BRKINT },
1064b88c807SRodney W. Grimes 	{ "ignpar",	IGNPAR, 0 },
1074b88c807SRodney W. Grimes 	{ "-ignpar",	0, IGNPAR },
1084b88c807SRodney W. Grimes 	{ "parmrk",	PARMRK, 0 },
1094b88c807SRodney W. Grimes 	{ "-parmrk",	0, PARMRK },
1104b88c807SRodney W. Grimes 	{ "inpck",	INPCK, 0 },
1114b88c807SRodney W. Grimes 	{ "-inpck",	0, INPCK },
1124b88c807SRodney W. Grimes 	{ "istrip",	ISTRIP, 0 },
1134b88c807SRodney W. Grimes 	{ "-istrip",	0, ISTRIP },
1144b88c807SRodney W. Grimes 	{ "inlcr",	INLCR, 0 },
1154b88c807SRodney W. Grimes 	{ "-inlcr",	0, INLCR },
1164b88c807SRodney W. Grimes 	{ "igncr",	IGNCR, 0 },
1174b88c807SRodney W. Grimes 	{ "-igncr",	0, IGNCR },
1184b88c807SRodney W. Grimes 	{ "icrnl",	ICRNL, 0 },
1194b88c807SRodney W. Grimes 	{ "-icrnl",	0, ICRNL },
1204b88c807SRodney W. Grimes 	{ "ixon",	IXON, 0 },
1214b88c807SRodney W. Grimes 	{ "-ixon",	0, IXON },
1224b88c807SRodney W. Grimes 	{ "flow",	IXON, 0 },
1234b88c807SRodney W. Grimes 	{ "-flow",	0, IXON },
1244b88c807SRodney W. Grimes 	{ "ixoff",	IXOFF, 0 },
1254b88c807SRodney W. Grimes 	{ "-ixoff",	0, IXOFF },
1264b88c807SRodney W. Grimes 	{ "tandem",	IXOFF, 0 },
1274b88c807SRodney W. Grimes 	{ "-tandem",	0, IXOFF },
1284b88c807SRodney W. Grimes 	{ "ixany",	IXANY, 0 },
1294b88c807SRodney W. Grimes 	{ "-ixany",	0, IXANY },
1304b88c807SRodney W. Grimes 	{ "decctlq",	0, IXANY },
1314b88c807SRodney W. Grimes 	{ "-decctlq",	IXANY, 0 },
1324b88c807SRodney W. Grimes 	{ "imaxbel",	IMAXBEL, 0 },
1334b88c807SRodney W. Grimes 	{ "-imaxbel",	0, IMAXBEL },
1345e5a5667SKris Kennaway 	{ NULL,		0, 0 },
1354b88c807SRodney W. Grimes };
1364b88c807SRodney W. Grimes 
1374b88c807SRodney W. Grimes struct modes lmodes[] = {
1384b88c807SRodney W. Grimes 	{ "echo",	ECHO, 0 },
1394b88c807SRodney W. Grimes 	{ "-echo",	0, ECHO },
1404b88c807SRodney W. Grimes 	{ "echoe",	ECHOE, 0 },
1414b88c807SRodney W. Grimes 	{ "-echoe",	0, ECHOE },
1424b88c807SRodney W. Grimes 	{ "crterase",	ECHOE, 0 },
1434b88c807SRodney W. Grimes 	{ "-crterase",	0, ECHOE },
1444b88c807SRodney W. Grimes 	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
1454b88c807SRodney W. Grimes 	{ "-crtbs",	0, ECHOE },
1464b88c807SRodney W. Grimes 	{ "echok",	ECHOK, 0 },
1474b88c807SRodney W. Grimes 	{ "-echok",	0, ECHOK },
1484b88c807SRodney W. Grimes 	{ "echoke",	ECHOKE, 0 },
1494b88c807SRodney W. Grimes 	{ "-echoke",	0, ECHOKE },
1504b88c807SRodney W. Grimes 	{ "crtkill",	ECHOKE, 0 },
1514b88c807SRodney W. Grimes 	{ "-crtkill",	0, ECHOKE },
1524b88c807SRodney W. Grimes 	{ "altwerase",	ALTWERASE, 0 },
1534b88c807SRodney W. Grimes 	{ "-altwerase",	0, ALTWERASE },
1544b88c807SRodney W. Grimes 	{ "iexten",	IEXTEN, 0 },
1554b88c807SRodney W. Grimes 	{ "-iexten",	0, IEXTEN },
1564b88c807SRodney W. Grimes 	{ "echonl",	ECHONL, 0 },
1574b88c807SRodney W. Grimes 	{ "-echonl",	0, ECHONL },
1584b88c807SRodney W. Grimes 	{ "echoctl",	ECHOCTL, 0 },
1594b88c807SRodney W. Grimes 	{ "-echoctl",	0, ECHOCTL },
1604b88c807SRodney W. Grimes 	{ "ctlecho",	ECHOCTL, 0 },
1614b88c807SRodney W. Grimes 	{ "-ctlecho",	0, ECHOCTL },
1624b88c807SRodney W. Grimes 	{ "echoprt",	ECHOPRT, 0 },
1634b88c807SRodney W. Grimes 	{ "-echoprt",	0, ECHOPRT },
1644b88c807SRodney W. Grimes 	{ "prterase",	ECHOPRT, 0 },
1654b88c807SRodney W. Grimes 	{ "-prterase",	0, ECHOPRT },
1664b88c807SRodney W. Grimes 	{ "isig",	ISIG, 0 },
1674b88c807SRodney W. Grimes 	{ "-isig",	0, ISIG },
1684b88c807SRodney W. Grimes 	{ "icanon",	ICANON, 0 },
1694b88c807SRodney W. Grimes 	{ "-icanon",	0, ICANON },
1704b88c807SRodney W. Grimes 	{ "noflsh",	NOFLSH, 0 },
1714b88c807SRodney W. Grimes 	{ "-noflsh",	0, NOFLSH },
1724b88c807SRodney W. Grimes 	{ "tostop",	TOSTOP, 0 },
1734b88c807SRodney W. Grimes 	{ "-tostop",	0, TOSTOP },
1744b88c807SRodney W. Grimes 	{ "flusho",	FLUSHO, 0 },
1754b88c807SRodney W. Grimes 	{ "-flusho",	0, FLUSHO },
1764b88c807SRodney W. Grimes 	{ "pendin",	PENDIN, 0 },
1774b88c807SRodney W. Grimes 	{ "-pendin",	0, PENDIN },
1784b88c807SRodney W. Grimes 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
1794b88c807SRodney W. Grimes 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
1804b88c807SRodney W. Grimes 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
1814b88c807SRodney W. Grimes 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
1824b88c807SRodney W. Grimes 	{ "nokerninfo",	NOKERNINFO, 0 },
1834b88c807SRodney W. Grimes 	{ "-nokerninfo",0, NOKERNINFO },
1844b88c807SRodney W. Grimes 	{ "kerninfo",	0, NOKERNINFO },
1854b88c807SRodney W. Grimes 	{ "-kerninfo",	NOKERNINFO, 0 },
1865e5a5667SKris Kennaway 	{ NULL,		0, 0 },
1874b88c807SRodney W. Grimes };
1884b88c807SRodney W. Grimes 
1894b88c807SRodney W. Grimes struct modes omodes[] = {
1904b88c807SRodney W. Grimes 	{ "opost",	OPOST, 0 },
1914b88c807SRodney W. Grimes 	{ "-opost",	0, OPOST },
1924b88c807SRodney W. Grimes 	{ "litout",	0, OPOST },
1934b88c807SRodney W. Grimes 	{ "-litout",	OPOST, 0 },
1944b88c807SRodney W. Grimes 	{ "onlcr",	ONLCR, 0 },
1954b88c807SRodney W. Grimes 	{ "-onlcr",	0, ONLCR },
1963617ddfcSAssar Westerlund 	{ "ocrnl",	OCRNL, 0 },
1973617ddfcSAssar Westerlund 	{ "-ocrnl",	0, OCRNL },
1984b88c807SRodney W. Grimes 	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
1994b88c807SRodney W. Grimes 	{ "-tabs",	OXTABS, 0 },
2004b88c807SRodney W. Grimes 	{ "oxtabs",	OXTABS, 0 },
2014b88c807SRodney W. Grimes 	{ "-oxtabs",	0, OXTABS },
2023617ddfcSAssar Westerlund 	{ "onocr",	ONOCR, 0 },
2033617ddfcSAssar Westerlund 	{ "-onocr",	0, ONOCR },
2043617ddfcSAssar Westerlund 	{ "onlret",	ONLRET, 0 },
2053617ddfcSAssar Westerlund 	{ "-onlret",	0, ONLRET },
2065e5a5667SKris Kennaway 	{ NULL,		0, 0 },
2074b88c807SRodney W. Grimes };
2084b88c807SRodney W. Grimes 
2094b88c807SRodney W. Grimes #define	CHK(s)	(*name == s[0] && !strcmp(name, s))
2104b88c807SRodney W. Grimes 
2114b88c807SRodney W. Grimes int
2124b88c807SRodney W. Grimes msearch(argvp, ip)
2134b88c807SRodney W. Grimes 	char ***argvp;
2144b88c807SRodney W. Grimes 	struct info *ip;
2154b88c807SRodney W. Grimes {
2164b88c807SRodney W. Grimes 	struct modes *mp;
2174b88c807SRodney W. Grimes 	char *name;
2184b88c807SRodney W. Grimes 
2194b88c807SRodney W. Grimes 	name = **argvp;
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes 	for (mp = cmodes; mp->name; ++mp)
2224b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2234b88c807SRodney W. Grimes 			ip->t.c_cflag &= ~mp->unset;
2244b88c807SRodney W. Grimes 			ip->t.c_cflag |= mp->set;
2254b88c807SRodney W. Grimes 			ip->set = 1;
2264b88c807SRodney W. Grimes 			return (1);
2274b88c807SRodney W. Grimes 		}
2284b88c807SRodney W. Grimes 	for (mp = imodes; mp->name; ++mp)
2294b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2304b88c807SRodney W. Grimes 			ip->t.c_iflag &= ~mp->unset;
2314b88c807SRodney W. Grimes 			ip->t.c_iflag |= mp->set;
2324b88c807SRodney W. Grimes 			ip->set = 1;
2334b88c807SRodney W. Grimes 			return (1);
2344b88c807SRodney W. Grimes 		}
2354b88c807SRodney W. Grimes 	for (mp = lmodes; mp->name; ++mp)
2364b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2374b88c807SRodney W. Grimes 			ip->t.c_lflag &= ~mp->unset;
2384b88c807SRodney W. Grimes 			ip->t.c_lflag |= mp->set;
2394b88c807SRodney W. Grimes 			ip->set = 1;
2404b88c807SRodney W. Grimes 			return (1);
2414b88c807SRodney W. Grimes 		}
2424b88c807SRodney W. Grimes 	for (mp = omodes; mp->name; ++mp)
2434b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2444b88c807SRodney W. Grimes 			ip->t.c_oflag &= ~mp->unset;
2454b88c807SRodney W. Grimes 			ip->t.c_oflag |= mp->set;
2464b88c807SRodney W. Grimes 			ip->set = 1;
2474b88c807SRodney W. Grimes 			return (1);
2484b88c807SRodney W. Grimes 		}
2494b88c807SRodney W. Grimes 	return (0);
2504b88c807SRodney W. Grimes }
251