Module Name:    src
Committed By:   christos
Date:           Fri May  1 17:01:08 UTC 2015

Modified Files:
        src/bin/stty: modes.c

Log Message:
Simplify by splitting the "simple" mode setting and the "special" ones.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/bin/stty/modes.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/stty/modes.c
diff -u src/bin/stty/modes.c:1.17 src/bin/stty/modes.c:1.18
--- src/bin/stty/modes.c:1.17	Sun Oct 15 20:37:55 2006
+++ src/bin/stty/modes.c	Fri May  1 13:01:08 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: modes.c,v 1.17 2006/10/16 00:37:55 christos Exp $ */
+/* $NetBSD: modes.c,v 1.18 2015/05/01 17:01:08 christos Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)modes.c	8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: modes.c,v 1.17 2006/10/16 00:37:55 christos Exp $");
+__RCSID("$NetBSD: modes.c,v 1.18 2015/05/01 17:01:08 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -42,12 +42,18 @@ __RCSID("$NetBSD: modes.c,v 1.17 2006/10
 
 #include <stddef.h>
 #include <string.h>
+#include <stdbool.h>
 
 #include "stty.h"
 #include "extern.h"
 
 struct modes {
 	const char *name;
+	tcflag_t flag;
+};
+
+struct specialmodes {
+	const char *name;
 	tcflag_t set;
 	tcflag_t unset;
 };
@@ -57,18 +63,24 @@ struct modes {
  * options, i.e. "foo" must immediately precede "-foo".
  */
 const struct modes cmodes[] = {
+	{ "cstopb",	CSTOPB },
+	{ "cread",	CREAD },
+	{ "parenb",	PARENB },
+	{ "parodd",	PARODD },
+	{ "hupcl",	HUPCL },
+	{ "hup",	HUPCL },
+	{ "clocal",	CLOCAL },
+	{ "crtscts",	CRTSCTS },
+	{ "mdmbuf",	MDMBUF },
+	{ "cdtrcts",	CDTRCTS },
+	{ .name = NULL },
+};
+
+const struct specialmodes cspecialmodes[] = {
 	{ "cs5",	CS5, CSIZE },
 	{ "cs6",	CS6, CSIZE },
 	{ "cs7",	CS7, CSIZE },
 	{ "cs8",	CS8, CSIZE },
-	{ "cstopb",	CSTOPB, 0 },
-	{ "-cstopb",	0, CSTOPB },
-	{ "cread",	CREAD, 0 },
-	{ "-cread",	0, CREAD },
-	{ "parenb",	PARENB, 0 },
-	{ "-parenb",	0, PARENB },
-	{ "parodd",	PARODD, 0 },
-	{ "-parodd",	0, PARODD },
 	{ "parity",	PARENB | CS7, PARODD | CSIZE },
 	{ "-parity",	CS8, PARODD | PARENB | CSIZE },
 	{ "evenp",	PARENB | CS7, PARODD | CSIZE },
@@ -77,166 +89,136 @@ const struct modes cmodes[] = {
 	{ "-oddp",	CS8, PARODD | PARENB | CSIZE },
 	{ "pass8",	CS8, PARODD | PARENB | CSIZE },
 	{ "-pass8",	PARENB | CS7, PARODD | CSIZE },
-	{ "hupcl",	HUPCL, 0 },
-	{ "-hupcl",	0, HUPCL },
-	{ "hup",	HUPCL, 0 },
-	{ "-hup",	0, HUPCL },
-	{ "clocal",	CLOCAL, 0 },
-	{ "-clocal",	0, CLOCAL },
-	{ "crtscts",	CRTSCTS, 0 },
-	{ "-crtscts",	0, CRTSCTS },
-	{ "mdmbuf",	MDMBUF, 0 },
-	{ "-mdmbuf",	0, MDMBUF },
-	{ "cdtrcts",	CDTRCTS, 0 },
-	{ "-cdtrcts",	0, CDTRCTS },
 	{ .name = NULL },
 };
 
 const struct modes imodes[] = {
-	{ "ignbrk",	IGNBRK, 0 },
-	{ "-ignbrk",	0, IGNBRK },
-	{ "brkint",	BRKINT, 0 },
-	{ "-brkint",	0, BRKINT },
-	{ "ignpar",	IGNPAR, 0 },
-	{ "-ignpar",	0, IGNPAR },
-	{ "parmrk",	PARMRK, 0 },
-	{ "-parmrk",	0, PARMRK },
-	{ "inpck",	INPCK, 0 },
-	{ "-inpck",	0, INPCK },
-	{ "istrip",	ISTRIP, 0 },
-	{ "-istrip",	0, ISTRIP },
-	{ "inlcr",	INLCR, 0 },
-	{ "-inlcr",	0, INLCR },
-	{ "igncr",	IGNCR, 0 },
-	{ "-igncr",	0, IGNCR },
-	{ "icrnl",	ICRNL, 0 },
-	{ "-icrnl",	0, ICRNL },
-	{ "ixon",	IXON, 0 },
-	{ "-ixon",	0, IXON },
-	{ "flow",	IXON, 0 },
-	{ "-flow",	0, IXON },
-	{ "ixoff",	IXOFF, 0 },
-	{ "-ixoff",	0, IXOFF },
-	{ "tandem",	IXOFF, 0 },
-	{ "-tandem",	0, IXOFF },
-	{ "ixany",	IXANY, 0 },
-	{ "-ixany",	0, IXANY },
+	{ "ignbrk",	IGNBRK },
+	{ "brkint",	BRKINT },
+	{ "ignpar",	IGNPAR },
+	{ "parmrk",	PARMRK },
+	{ "inpck",	INPCK },
+	{ "istrip",	ISTRIP },
+	{ "inlcr",	INLCR },
+	{ "igncr",	IGNCR },
+	{ "icrnl",	ICRNL },
+	{ "ixon",	IXON },
+	{ "flow",	IXON },
+	{ "ixoff",	IXOFF },
+	{ "tandem",	IXOFF },
+	{ "ixany",	IXANY },
+	{ "imaxbel",	IMAXBEL },
+	{ .name = NULL },
+};
+
+const struct specialmodes ispecialmodes[] = {
 	{ "decctlq",	0, IXANY },
 	{ "-decctlq",	IXANY, 0 },
-	{ "imaxbel",	IMAXBEL, 0 },
-	{ "-imaxbel",	0, IMAXBEL },
 	{ .name = NULL },
 };
 
 const struct modes lmodes[] = {
-	{ "echo",	ECHO, 0 },
-	{ "-echo",	0, ECHO },
-	{ "echoe",	ECHOE, 0 },
-	{ "-echoe",	0, ECHOE },
-	{ "crterase",	ECHOE, 0 },
-	{ "-crterase",	0, ECHOE },
-	{ "crtbs",	ECHOE, 0 },	/* crtbs not supported, close enough */
-	{ "-crtbs",	0, ECHOE },
-	{ "echok",	ECHOK, 0 },
-	{ "-echok",	0, ECHOK },
-	{ "echoke",	ECHOKE, 0 },
-	{ "-echoke",	0, ECHOKE },
-	{ "crtkill",	ECHOKE, 0 },
-	{ "-crtkill",	0, ECHOKE },
-	{ "altwerase",	ALTWERASE, 0 },
-	{ "-altwerase",	0, ALTWERASE },
-	{ "iexten",	IEXTEN, 0 },
-	{ "-iexten",	0, IEXTEN },
-	{ "echonl",	ECHONL, 0 },
-	{ "-echonl",	0, ECHONL },
-	{ "echoctl",	ECHOCTL, 0 },
-	{ "-echoctl",	0, ECHOCTL },
-	{ "ctlecho",	ECHOCTL, 0 },
-	{ "-ctlecho",	0, ECHOCTL },
-	{ "echoprt",	ECHOPRT, 0 },
-	{ "-echoprt",	0, ECHOPRT },
-	{ "prterase",	ECHOPRT, 0 },
-	{ "-prterase",	0, ECHOPRT },
-	{ "isig",	ISIG, 0 },
-	{ "-isig",	0, ISIG },
-	{ "icanon",	ICANON, 0 },
-	{ "-icanon",	0, ICANON },
-	{ "noflsh",	NOFLSH, 0 },
-	{ "-noflsh",	0, NOFLSH },
-	{ "tostop",	TOSTOP, 0 },
-	{ "-tostop",	0, TOSTOP },
-	{ "flusho",	FLUSHO, 0 },
-	{ "-flusho",	0, FLUSHO },
-	{ "pendin",	PENDIN, 0 },
-	{ "-pendin",	0, PENDIN },
+	{ "echo",	ECHO },
+	{ "echoe",	ECHOE },
+	{ "crterase",	ECHOE },
+	{ "crtbs",	ECHOE },	/* crtbs not supported, close enough */
+	{ "echok",	ECHOK },
+	{ "echoke",	ECHOKE },
+	{ "crtkill",	ECHOKE },
+	{ "altwerase",	ALTWERASE },
+	{ "iexten",	IEXTEN },
+	{ "echonl",	ECHONL },
+	{ "echoctl",	ECHOCTL },
+	{ "ctlecho",	ECHOCTL },
+	{ "echoprt",	ECHOPRT },
+	{ "prterase",	ECHOPRT },
+	{ "isig",	ISIG },
+	{ "icanon",	ICANON },
+	{ "noflsh",	NOFLSH },
+	{ "tostop",	TOSTOP },
+	{ "flusho",	FLUSHO },
+	{ "pendin",	PENDIN },
+	{ "nokerninfo",	NOKERNINFO },
+	{ .name = NULL },
+};
+
+const struct specialmodes lspecialmodes[] = {
 	{ "crt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
 	{ "-crt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
 	{ "newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
 	{ "-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL },
-	{ "nokerninfo",	NOKERNINFO, 0 },
-	{ "-nokerninfo",0, NOKERNINFO },
 	{ "kerninfo",	0, NOKERNINFO },
 	{ "-kerninfo",	NOKERNINFO, 0 },
 	{ .name = NULL },
 };
 
 const struct modes omodes[] = {
-	{ "opost",	OPOST, 0 },
-	{ "-opost",	0, OPOST },
+	{ "opost",	OPOST },
+	{ "onlcr",	ONLCR },
+	{ "ocrnl",	OCRNL },
+	{ "oxtabs",	OXTABS },
+	{ "onocr",	ONOCR },
+	{ "onlret",	ONLRET },
+	{ .name = NULL },
+};
+
+const struct specialmodes ospecialmodes[] = {
 	{ "litout",	0, OPOST },
 	{ "-litout",	OPOST, 0 },
-	{ "onlcr",	ONLCR, 0 },
-	{ "-onlcr",	0, ONLCR },
-	{ "ocrnl",	OCRNL, 0 },
-	{ "-ocrnl",	0, OCRNL },
 	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
 	{ "-tabs",	OXTABS, 0 },
-	{ "oxtabs",	OXTABS, 0 },
-	{ "-oxtabs",	0, OXTABS },
-	{ "onocr",	ONOCR, 0 },
-	{ "-onocr",	0, ONOCR },
-	{ "onlret",	ONLRET, 0 },
-	{ "-onlret",	0, ONLRET },
 	{ .name = NULL },
 };
 
 #define	CHK(s)	(!strcmp(name, s))
 
-int
-msearch(char ***argvp, struct info *ip)
+static int
+modeset(const char *name, const struct modes *mp,
+    const struct specialmodes *smp, tcflag_t *f)
 {
-	const struct modes *mp;
-	char *name;
-
-	name = **argvp;
+	bool neg;
 
-	for (mp = cmodes; mp->name; ++mp)
-		if (CHK(mp->name)) {
-			ip->t.c_cflag &= ~mp->unset;
-			ip->t.c_cflag |= mp->set;
-			ip->set = 1;
-			return (1);
-		}
-	for (mp = imodes; mp->name; ++mp)
-		if (CHK(mp->name)) {
-			ip->t.c_iflag &= ~mp->unset;
-			ip->t.c_iflag |= mp->set;
-			ip->set = 1;
-			return (1);
-		}
-	for (mp = lmodes; mp->name; ++mp)
-		if (CHK(mp->name)) {
-			ip->t.c_lflag &= ~mp->unset;
-			ip->t.c_lflag |= mp->set;
-			ip->set = 1;
-			return (1);
+	for (; smp->name; ++smp)
+		if (CHK(smp->name)) {
+			*f &= ~smp->unset;
+			*f |= smp->set;
+			return 1;
 		}
-	for (mp = omodes; mp->name; ++mp)
+
+	if ((neg = (*name == '-')))
+		name++;
+
+	for (; mp->name; ++mp)
 		if (CHK(mp->name)) {
-			ip->t.c_oflag &= ~mp->unset;
-			ip->t.c_oflag |= mp->set;
-			ip->set = 1;
-			return (1);
+			if (neg)
+				*f &= ~mp->flag;
+			else
+				*f |= mp->flag;
+			return 1;
 		}
-	return (0);
+
+	return 0;
+}
+
+int
+msearch(char ***argvp, struct info *ip)
+{
+	const char *name = **argvp;
+
+	if (modeset(name, cmodes, cspecialmodes, &ip->t.c_cflag))
+		goto out;
+		
+	if (modeset(name, imodes, ispecialmodes, &ip->t.c_iflag))
+		goto out;
+
+	if (modeset(name, lmodes, lspecialmodes, &ip->t.c_lflag))
+		goto out;
+
+	if (modeset(name, omodes, ospecialmodes, &ip->t.c_oflag))
+		goto out;
+
+	return 0;
+out:
+	ip->set = 1;
+	return 1;
 }

Reply via email to