Module Name:    src
Committed By:   dholland
Date:           Sat Aug 10 23:54:41 UTC 2013

Modified Files:
        src/usr.bin/tr: extern.h str.c tr.c

Log Message:
Expose less of the parser state outside str.c.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/tr/extern.h
cvs rdiff -u -r1.19 -r1.20 src/usr.bin/tr/str.c
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/tr/tr.c

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

Modified files:

Index: src/usr.bin/tr/extern.h
diff -u src/usr.bin/tr/extern.h:1.7 src/usr.bin/tr/extern.h:1.8
--- src/usr.bin/tr/extern.h:1.7	Tue Sep  6 18:33:46 2011
+++ src/usr.bin/tr/extern.h	Sat Aug 10 23:54:41 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: extern.h,v 1.7 2011/09/06 18:33:46 joerg Exp $	*/
+/*	$NetBSD: extern.h,v 1.8 2013/08/10 23:54:41 dholland Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -45,4 +45,4 @@ typedef struct {
 #define	NCHARS	(UCHAR_MAX + 1)		/* Number of possible characters. */
 #define	OOBCH	(UCHAR_MAX + 1)		/* Out of band character value. */
 
-int	 next(STR *);
+int	 next(STR *, int *);

Index: src/usr.bin/tr/str.c
diff -u src/usr.bin/tr/str.c:1.19 src/usr.bin/tr/str.c:1.20
--- src/usr.bin/tr/str.c:1.19	Thu Sep  8 12:00:26 2011
+++ src/usr.bin/tr/str.c	Sat Aug 10 23:54:41 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: str.c,v 1.19 2011/09/08 12:00:26 christos Exp $	*/
+/*	$NetBSD: str.c,v 1.20 2013/08/10 23:54:41 dholland Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)str.c	8.2 (Berkeley) 4/28/95";
 #endif
-__RCSID("$NetBSD: str.c,v 1.19 2011/09/08 12:00:26 christos Exp $");
+__RCSID("$NetBSD: str.c,v 1.20 2013/08/10 23:54:41 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -46,6 +46,7 @@ __RCSID("$NetBSD: str.c,v 1.19 2011/09/0
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <assert.h>
 
 #include "extern.h"
 
@@ -58,26 +59,29 @@ static int	genrange(STR *);
 static void	genseq(STR *);
 
 int
-next(STR *s)
+next(STR *s, int *ret)
 {
 	int ch;
 
 	switch (s->state) {
 	case EOS:
+		*ret = s->lastch;
 		return 0;
 	case INFINITE:
+		*ret = s->lastch;
 		return 1;
 	case NORMAL:
 		switch (ch = *s->str) {
 		case '\0':
 			s->state = EOS;
+			*ret = s->lastch;
 			return 0;
 		case '\\':
 			s->lastch = backslash(s);
 			break;
 		case '[':
 			if (bracket(s))
-				return next(s);
+				return next(s, ret);
 			/* FALLTHROUGH */
 		default:
 			++s->str;
@@ -86,30 +90,37 @@ next(STR *s)
 		}
 
 		/* We can start a range at any time. */
-		if (s->str[0] == '-' && genrange(s))
-			return next(s);
+		if (s->str[0] == '-' && genrange(s)) {
+			return next(s, ret);
+		}
+		*ret = s->lastch;
 		return 1;
 	case RANGE:
 		if (s->cnt-- == 0) {
 			s->state = NORMAL;
-			return next(s);
+			return next(s, ret);
 		}
 		++s->lastch;
+		*ret = s->lastch;
 		return 1;
 	case SEQUENCE:
 		if (s->cnt-- == 0) {
 			s->state = NORMAL;
-			return next(s);
+			return next(s, ret);
 		}
+		*ret = s->lastch;
 		return 1;
 	case SET:
 		if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
 			s->state = NORMAL;
-			return next(s);
+			return next(s, ret);
 		}
+		*ret = s->lastch;
 		return 1;
 	}
 	/* NOTREACHED */
+	assert(0);
+	*ret = s->lastch;
 	return 0;
 }
 

Index: src/usr.bin/tr/tr.c
diff -u src/usr.bin/tr/tr.c:1.9 src/usr.bin/tr/tr.c:1.10
--- src/usr.bin/tr/tr.c:1.9	Tue Sep  6 18:33:46 2011
+++ src/usr.bin/tr/tr.c	Sat Aug 10 23:54:41 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: tr.c,v 1.9 2011/09/06 18:33:46 joerg Exp $	*/
+/*	$NetBSD: tr.c,v 1.10 2013/08/10 23:54:41 dholland Exp $	*/
 
 /*
  * Copyright (c) 1988, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)tr.c	8.2 (Berkeley) 5/4/95";
 #endif
-__RCSID("$NetBSD: tr.c,v 1.9 2011/09/06 18:33:46 joerg Exp $");
+__RCSID("$NetBSD: tr.c,v 1.10 2013/08/10 23:54:41 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -96,12 +96,12 @@ __dead static void usage(void);
 int
 main(int argc, char **argv)
 {
-	int ch, cnt, lastch, *p;
+	int ch, ch2, cnt, lastch, *p;
 	int cflag, dflag, sflag, isstring2;
 
 	cflag = dflag = sflag = 0;
 	while ((ch = getopt(argc, argv, "cds")) != -1)
-		switch((char)ch) {
+		switch (ch) {
 		case 'c':
 			cflag = 1;
 			break;
@@ -198,20 +198,20 @@ main(int argc, char **argv)
 		for (cnt = NCHARS, p = string1; cnt--;)
 			*p++ = OOBCH;
 
-	if (!next(&s2))
+	if (!next(&s2, &ch2))
 		errx(1, "empty string2");
 
 	/* If string2 runs out of characters, use the last one specified. */
 	if (sflag)
-		while (next(&s1)) {
-			string1[s1.lastch] = ch = s2.lastch;
-			string2[ch] = 1;
-			(void)next(&s2);
+		while (next(&s1, &ch)) {
+			string1[ch] = ch2;
+			string2[ch2] = 1;
+			(void)next(&s2, &ch2);
 		}
 	else
-		while (next(&s1)) {
-			string1[s1.lastch] = ch = s2.lastch;
-			(void)next(&s2);
+		while (next(&s1, &ch)) {
+			string1[ch] = ch2;
+			(void)next(&s2, &ch2);
 		}
 
 	if (cflag)
@@ -236,11 +236,12 @@ static void
 setup(int *string, char *arg, STR *str, int cflag)
 {
 	int cnt, *p;
+	int ch;
 
 	str->str = arg;
 	memset(string, 0, NCHARS * sizeof(int));
-	while (next(str))
-		string[str->lastch] = 1;
+	while (next(str, &ch))
+		string[ch] = 1;
 	if (cflag)
 		for (p = string, cnt = NCHARS; cnt--; ++p)
 			*p = !*p;

Reply via email to