Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/tr Expose less of the parser state outside str.c.



details:   https://anonhg.NetBSD.org/src/rev/04f84d95b858
branches:  trunk
changeset: 789189:04f84d95b858
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sat Aug 10 23:54:41 2013 +0000

description:
Expose less of the parser state outside str.c.

diffstat:

 usr.bin/tr/extern.h |   4 ++--
 usr.bin/tr/str.c    |  29 ++++++++++++++++++++---------
 usr.bin/tr/tr.c     |  29 +++++++++++++++--------------
 3 files changed, 37 insertions(+), 25 deletions(-)

diffs (193 lines):

diff -r 24cb62b4b715 -r 04f84d95b858 usr.bin/tr/extern.h
--- a/usr.bin/tr/extern.h       Sat Aug 10 23:25:35 2013 +0000
+++ b/usr.bin/tr/extern.h       Sat Aug 10 23:54:41 2013 +0000
@@ -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 @@
 #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 *);
diff -r 24cb62b4b715 -r 04f84d95b858 usr.bin/tr/str.c
--- a/usr.bin/tr/str.c  Sat Aug 10 23:25:35 2013 +0000
+++ b/usr.bin/tr/str.c  Sat Aug 10 23:54:41 2013 +0000
@@ -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 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <assert.h>
 
 #include "extern.h"
 
@@ -58,26 +59,29 @@
 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 @@
                }
 
                /* 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;
 }
 
diff -r 24cb62b4b715 -r 04f84d95b858 usr.bin/tr/tr.c
--- a/usr.bin/tr/tr.c   Sat Aug 10 23:25:35 2013 +0000
+++ b/usr.bin/tr/tr.c   Sat Aug 10 23:54:41 2013 +0000
@@ -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 @@
 #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 @@
 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 @@
                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 @@
 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;



Home | Main Index | Thread Index | Old Index