xref: /freebsd/bin/stty/modes.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
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.
13fbbd9655SWarner Losh  * 3. 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 #include <sys/types.h>
314b88c807SRodney W. Grimes #include <string.h>
324b88c807SRodney W. Grimes #include "stty.h"
334b88c807SRodney W. Grimes 
345134c3f7SWarner Losh int msearch(char ***, struct info *);
355e5a5667SKris Kennaway 
364b88c807SRodney W. Grimes struct modes {
375e5a5667SKris Kennaway 	const char *name;
384b88c807SRodney W. Grimes 	long set;
394b88c807SRodney W. Grimes 	long unset;
404b88c807SRodney W. Grimes };
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes /*
434b88c807SRodney W. Grimes  * The code in optlist() depends on minus options following regular
444b88c807SRodney W. Grimes  * options, i.e. "foo" must immediately precede "-foo".
454b88c807SRodney W. Grimes  */
467b44b809SEd Schouten static const struct modes cmodes[] = {
474b88c807SRodney W. Grimes 	{ "cs5",	CS5, CSIZE },
484b88c807SRodney W. Grimes 	{ "cs6",	CS6, CSIZE },
494b88c807SRodney W. Grimes 	{ "cs7",	CS7, CSIZE },
504b88c807SRodney W. Grimes 	{ "cs8",	CS8, CSIZE },
514b88c807SRodney W. Grimes 	{ "cstopb",	CSTOPB, 0 },
524b88c807SRodney W. Grimes 	{ "-cstopb",	0, CSTOPB },
534b88c807SRodney W. Grimes 	{ "cread",	CREAD, 0 },
544b88c807SRodney W. Grimes 	{ "-cread",	0, CREAD },
554b88c807SRodney W. Grimes 	{ "parenb",	PARENB, 0 },
564b88c807SRodney W. Grimes 	{ "-parenb",	0, PARENB },
574b88c807SRodney W. Grimes 	{ "parodd",	PARODD, 0 },
584b88c807SRodney W. Grimes 	{ "-parodd",	0, PARODD },
594b88c807SRodney W. Grimes 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
604b88c807SRodney W. Grimes 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
614b88c807SRodney W. Grimes 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
624b88c807SRodney W. Grimes 	{ "-evenp",	CS8, PARODD | PARENB | CSIZE },
634b88c807SRodney W. Grimes 	{ "oddp",	PARENB | CS7 | PARODD, CSIZE },
644b88c807SRodney W. Grimes 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
654b88c807SRodney W. Grimes 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
664b88c807SRodney W. Grimes 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
674b88c807SRodney W. Grimes 	{ "hupcl",	HUPCL, 0 },
684b88c807SRodney W. Grimes 	{ "-hupcl",	0, HUPCL },
694b88c807SRodney W. Grimes 	{ "hup",	HUPCL, 0 },
704b88c807SRodney W. Grimes 	{ "-hup",	0, HUPCL },
714b88c807SRodney W. Grimes 	{ "clocal",	CLOCAL, 0 },
724b88c807SRodney W. Grimes 	{ "-clocal",	0, CLOCAL },
734b88c807SRodney W. Grimes 	{ "crtscts",	CRTSCTS, 0 },
744b88c807SRodney W. Grimes 	{ "-crtscts",	0, CRTSCTS },
752bf5814aSBruce Evans 	{ "ctsflow",	CCTS_OFLOW, 0 },
762bf5814aSBruce Evans 	{ "-ctsflow",	0, CCTS_OFLOW },
772bf5814aSBruce Evans 	{ "dsrflow",	CDSR_OFLOW, 0 },
782bf5814aSBruce Evans 	{ "-dsrflow",	0, CDSR_OFLOW },
792bf5814aSBruce Evans 	{ "dtrflow",	CDTR_IFLOW, 0 },
802bf5814aSBruce Evans 	{ "-dtrflow",	0, CDTR_IFLOW },
812bf5814aSBruce Evans 	{ "rtsflow",	CRTS_IFLOW, 0 },
822bf5814aSBruce Evans 	{ "-rtsflow",	0, CRTS_IFLOW },
834b88c807SRodney W. Grimes 	{ "mdmbuf",	MDMBUF, 0 },
844b88c807SRodney W. Grimes 	{ "-mdmbuf",	0, MDMBUF },
85705aad98SStephen Hurd 	{ "rtsdtr",	0, CNO_RTSDTR },
86705aad98SStephen Hurd 	{ "-rtsdtr",	CNO_RTSDTR, 0 },
875e5a5667SKris Kennaway 	{ NULL,		0, 0 },
884b88c807SRodney W. Grimes };
894b88c807SRodney W. Grimes 
907b44b809SEd Schouten static const struct modes imodes[] = {
914b88c807SRodney W. Grimes 	{ "ignbrk",	IGNBRK, 0 },
924b88c807SRodney W. Grimes 	{ "-ignbrk",	0, IGNBRK },
934b88c807SRodney W. Grimes 	{ "brkint",	BRKINT, 0 },
944b88c807SRodney W. Grimes 	{ "-brkint",	0, BRKINT },
954b88c807SRodney W. Grimes 	{ "ignpar",	IGNPAR, 0 },
964b88c807SRodney W. Grimes 	{ "-ignpar",	0, IGNPAR },
974b88c807SRodney W. Grimes 	{ "parmrk",	PARMRK, 0 },
984b88c807SRodney W. Grimes 	{ "-parmrk",	0, PARMRK },
994b88c807SRodney W. Grimes 	{ "inpck",	INPCK, 0 },
1004b88c807SRodney W. Grimes 	{ "-inpck",	0, INPCK },
1014b88c807SRodney W. Grimes 	{ "istrip",	ISTRIP, 0 },
1024b88c807SRodney W. Grimes 	{ "-istrip",	0, ISTRIP },
1034b88c807SRodney W. Grimes 	{ "inlcr",	INLCR, 0 },
1044b88c807SRodney W. Grimes 	{ "-inlcr",	0, INLCR },
1054b88c807SRodney W. Grimes 	{ "igncr",	IGNCR, 0 },
1064b88c807SRodney W. Grimes 	{ "-igncr",	0, IGNCR },
1074b88c807SRodney W. Grimes 	{ "icrnl",	ICRNL, 0 },
1084b88c807SRodney W. Grimes 	{ "-icrnl",	0, ICRNL },
1094b88c807SRodney W. Grimes 	{ "ixon",	IXON, 0 },
1104b88c807SRodney W. Grimes 	{ "-ixon",	0, IXON },
1114b88c807SRodney W. Grimes 	{ "flow",	IXON, 0 },
1124b88c807SRodney W. Grimes 	{ "-flow",	0, IXON },
1134b88c807SRodney W. Grimes 	{ "ixoff",	IXOFF, 0 },
1144b88c807SRodney W. Grimes 	{ "-ixoff",	0, IXOFF },
1154b88c807SRodney W. Grimes 	{ "tandem",	IXOFF, 0 },
1164b88c807SRodney W. Grimes 	{ "-tandem",	0, IXOFF },
1174b88c807SRodney W. Grimes 	{ "ixany",	IXANY, 0 },
1184b88c807SRodney W. Grimes 	{ "-ixany",	0, IXANY },
1194b88c807SRodney W. Grimes 	{ "decctlq",	0, IXANY },
1204b88c807SRodney W. Grimes 	{ "-decctlq",	IXANY, 0 },
1214b88c807SRodney W. Grimes 	{ "imaxbel",	IMAXBEL, 0 },
1224b88c807SRodney W. Grimes 	{ "-imaxbel",	0, IMAXBEL },
123*128f63ceSBojan Novković 	{ "iutf8",	IUTF8, 0 },
124*128f63ceSBojan Novković 	{ "-iutf8",	0, IUTF8 },
1255e5a5667SKris Kennaway 	{ NULL,		0, 0 },
1264b88c807SRodney W. Grimes };
1274b88c807SRodney W. Grimes 
1287b44b809SEd Schouten static const struct modes lmodes[] = {
1294b88c807SRodney W. Grimes 	{ "echo",	ECHO, 0 },
1304b88c807SRodney W. Grimes 	{ "-echo",	0, ECHO },
1314b88c807SRodney W. Grimes 	{ "echoe",	ECHOE, 0 },
1324b88c807SRodney W. Grimes 	{ "-echoe",	0, ECHOE },
1334b88c807SRodney W. Grimes 	{ "crterase",	ECHOE, 0 },
1344b88c807SRodney W. Grimes 	{ "-crterase",	0, ECHOE },
1354b88c807SRodney W. Grimes 	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
1364b88c807SRodney W. Grimes 	{ "-crtbs",	0, ECHOE },
1374b88c807SRodney W. Grimes 	{ "echok",	ECHOK, 0 },
1384b88c807SRodney W. Grimes 	{ "-echok",	0, ECHOK },
1394b88c807SRodney W. Grimes 	{ "echoke",	ECHOKE, 0 },
1404b88c807SRodney W. Grimes 	{ "-echoke",	0, ECHOKE },
1414b88c807SRodney W. Grimes 	{ "crtkill",	ECHOKE, 0 },
1424b88c807SRodney W. Grimes 	{ "-crtkill",	0, ECHOKE },
1434b88c807SRodney W. Grimes 	{ "altwerase",	ALTWERASE, 0 },
1444b88c807SRodney W. Grimes 	{ "-altwerase",	0, ALTWERASE },
1454b88c807SRodney W. Grimes 	{ "iexten",	IEXTEN, 0 },
1464b88c807SRodney W. Grimes 	{ "-iexten",	0, IEXTEN },
1474b88c807SRodney W. Grimes 	{ "echonl",	ECHONL, 0 },
1484b88c807SRodney W. Grimes 	{ "-echonl",	0, ECHONL },
1494b88c807SRodney W. Grimes 	{ "echoctl",	ECHOCTL, 0 },
1504b88c807SRodney W. Grimes 	{ "-echoctl",	0, ECHOCTL },
1514b88c807SRodney W. Grimes 	{ "ctlecho",	ECHOCTL, 0 },
1524b88c807SRodney W. Grimes 	{ "-ctlecho",	0, ECHOCTL },
1534b88c807SRodney W. Grimes 	{ "echoprt",	ECHOPRT, 0 },
1544b88c807SRodney W. Grimes 	{ "-echoprt",	0, ECHOPRT },
1554b88c807SRodney W. Grimes 	{ "prterase",	ECHOPRT, 0 },
1564b88c807SRodney W. Grimes 	{ "-prterase",	0, ECHOPRT },
1574b88c807SRodney W. Grimes 	{ "isig",	ISIG, 0 },
1584b88c807SRodney W. Grimes 	{ "-isig",	0, ISIG },
1594b88c807SRodney W. Grimes 	{ "icanon",	ICANON, 0 },
1604b88c807SRodney W. Grimes 	{ "-icanon",	0, ICANON },
1614b88c807SRodney W. Grimes 	{ "noflsh",	NOFLSH, 0 },
1624b88c807SRodney W. Grimes 	{ "-noflsh",	0, NOFLSH },
1634b88c807SRodney W. Grimes 	{ "tostop",	TOSTOP, 0 },
1644b88c807SRodney W. Grimes 	{ "-tostop",	0, TOSTOP },
1654b88c807SRodney W. Grimes 	{ "flusho",	FLUSHO, 0 },
1664b88c807SRodney W. Grimes 	{ "-flusho",	0, FLUSHO },
1674b88c807SRodney W. Grimes 	{ "pendin",	PENDIN, 0 },
1684b88c807SRodney W. Grimes 	{ "-pendin",	0, PENDIN },
1694b88c807SRodney W. Grimes 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
1704b88c807SRodney W. Grimes 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
1714b88c807SRodney W. Grimes 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
1724b88c807SRodney W. Grimes 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
1734b88c807SRodney W. Grimes 	{ "nokerninfo",	NOKERNINFO, 0 },
1744b88c807SRodney W. Grimes 	{ "-nokerninfo",0, NOKERNINFO },
1754b88c807SRodney W. Grimes 	{ "kerninfo",	0, NOKERNINFO },
1764b88c807SRodney W. Grimes 	{ "-kerninfo",	NOKERNINFO, 0 },
1775e5a5667SKris Kennaway 	{ NULL,		0, 0 },
1784b88c807SRodney W. Grimes };
1794b88c807SRodney W. Grimes 
1807b44b809SEd Schouten static const struct modes omodes[] = {
1814b88c807SRodney W. Grimes 	{ "opost",	OPOST, 0 },
1824b88c807SRodney W. Grimes 	{ "-opost",	0, OPOST },
1834b88c807SRodney W. Grimes 	{ "litout",	0, OPOST },
1844b88c807SRodney W. Grimes 	{ "-litout",	OPOST, 0 },
1854b88c807SRodney W. Grimes 	{ "onlcr",	ONLCR, 0 },
1864b88c807SRodney W. Grimes 	{ "-onlcr",	0, ONLCR },
1873617ddfcSAssar Westerlund 	{ "ocrnl",	OCRNL, 0 },
1883617ddfcSAssar Westerlund 	{ "-ocrnl",	0, OCRNL },
189f8f8c9f0SEd Schouten 	{ "tabs",	TAB0, TABDLY },		/* "preserve" tabs */
190f8f8c9f0SEd Schouten 	{ "-tabs",	TAB3, TABDLY },
191f8f8c9f0SEd Schouten 	{ "oxtabs",	TAB3, TABDLY },
192f8f8c9f0SEd Schouten 	{ "-oxtabs",	TAB0, TABDLY },
193f8f8c9f0SEd Schouten 	{ "tab0",	TAB0, TABDLY },
194f8f8c9f0SEd Schouten 	{ "tab3",	TAB3, TABDLY },
1953617ddfcSAssar Westerlund 	{ "onocr",	ONOCR, 0 },
1963617ddfcSAssar Westerlund 	{ "-onocr",	0, ONOCR },
1973617ddfcSAssar Westerlund 	{ "onlret",	ONLRET, 0 },
1983617ddfcSAssar Westerlund 	{ "-onlret",	0, ONLRET },
1995e5a5667SKris Kennaway 	{ NULL,		0, 0 },
2004b88c807SRodney W. Grimes };
2014b88c807SRodney W. Grimes 
2024b88c807SRodney W. Grimes #define	CHK(s)	(*name == s[0] && !strcmp(name, s))
2034b88c807SRodney W. Grimes 
2044b88c807SRodney W. Grimes int
msearch(char *** argvp,struct info * ip)2055134c3f7SWarner Losh msearch(char ***argvp, struct info *ip)
2064b88c807SRodney W. Grimes {
2077b44b809SEd Schouten 	const struct modes *mp;
2084b88c807SRodney W. Grimes 	char *name;
2094b88c807SRodney W. Grimes 
2104b88c807SRodney W. Grimes 	name = **argvp;
2114b88c807SRodney W. Grimes 
2124b88c807SRodney W. Grimes 	for (mp = cmodes; mp->name; ++mp)
2134b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2144b88c807SRodney W. Grimes 			ip->t.c_cflag &= ~mp->unset;
2154b88c807SRodney W. Grimes 			ip->t.c_cflag |= mp->set;
2164b88c807SRodney W. Grimes 			ip->set = 1;
2174b88c807SRodney W. Grimes 			return (1);
2184b88c807SRodney W. Grimes 		}
2194b88c807SRodney W. Grimes 	for (mp = imodes; mp->name; ++mp)
2204b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2214b88c807SRodney W. Grimes 			ip->t.c_iflag &= ~mp->unset;
2224b88c807SRodney W. Grimes 			ip->t.c_iflag |= mp->set;
2234b88c807SRodney W. Grimes 			ip->set = 1;
2244b88c807SRodney W. Grimes 			return (1);
2254b88c807SRodney W. Grimes 		}
2264b88c807SRodney W. Grimes 	for (mp = lmodes; mp->name; ++mp)
2274b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2284b88c807SRodney W. Grimes 			ip->t.c_lflag &= ~mp->unset;
2294b88c807SRodney W. Grimes 			ip->t.c_lflag |= mp->set;
2304b88c807SRodney W. Grimes 			ip->set = 1;
2314b88c807SRodney W. Grimes 			return (1);
2324b88c807SRodney W. Grimes 		}
2334b88c807SRodney W. Grimes 	for (mp = omodes; mp->name; ++mp)
2344b88c807SRodney W. Grimes 		if (CHK(mp->name)) {
2354b88c807SRodney W. Grimes 			ip->t.c_oflag &= ~mp->unset;
2364b88c807SRodney W. Grimes 			ip->t.c_oflag |= mp->set;
2374b88c807SRodney W. Grimes 			ip->set = 1;
2384b88c807SRodney W. Grimes 			return (1);
2394b88c807SRodney W. Grimes 		}
2404b88c807SRodney W. Grimes 	return (0);
2414b88c807SRodney W. Grimes }
242