Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/tr Simplify the parser handling.



details:   https://anonhg.NetBSD.org/src/rev/689f3c2ee1f5
branches:  trunk
changeset: 789197:689f3c2ee1f5
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Aug 11 00:39:22 2013 +0000

description:
Simplify the parser handling.

diffstat:

 usr.bin/tr/extern.h |   5 ++---
 usr.bin/tr/str.c    |  14 ++++----------
 usr.bin/tr/tr.c     |  40 ++++++++++++++++++----------------------
 3 files changed, 24 insertions(+), 35 deletions(-)

diffs (207 lines):

diff -r 27df733fb865 -r 689f3c2ee1f5 usr.bin/tr/extern.h
--- a/usr.bin/tr/extern.h       Sun Aug 11 00:34:09 2013 +0000
+++ b/usr.bin/tr/extern.h       Sun Aug 11 00:39:22 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.10 2013/08/11 00:05:49 dholland Exp $     */
+/*     $NetBSD: extern.h,v 1.11 2013/08/11 00:39:22 dholland Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -38,7 +38,6 @@
 #define        NCHARS  (UCHAR_MAX + 1)         /* Number of possible characters. */
 #define        OOBCH   (UCHAR_MAX + 1)         /* Out of band character value. */
 
-STR *str_create(int);
+STR *str_create(int, const char *);
 void str_destroy(STR *);
-void str_setstring(STR *s, const char *txt);
 int     next(STR *, int *);
diff -r 27df733fb865 -r 689f3c2ee1f5 usr.bin/tr/str.c
--- a/usr.bin/tr/str.c  Sun Aug 11 00:34:09 2013 +0000
+++ b/usr.bin/tr/str.c  Sun Aug 11 00:39:22 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: str.c,v 1.22 2013/08/11 00:05:49 dholland Exp $        */
+/*     $NetBSD: str.c,v 1.23 2013/08/11 00:39:22 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.22 2013/08/11 00:05:49 dholland Exp $");
+__RCSID("$NetBSD: str.c,v 1.23 2013/08/11 00:39:22 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -69,7 +69,7 @@
 static void    genseq(STR *);
 
 STR *
-str_create(int whichstring)
+str_create(int whichstring, const char *txt)
 {
        STR *s;
 
@@ -85,7 +85,7 @@
        s->equiv[0] = 0;
        s->equiv[1] = OOBCH;
        s->set = NULL;
-       s->str = NULL;
+       s->str = txt;
 
        return s;
 }
@@ -99,12 +99,6 @@
        free(s);
 }
 
-void
-str_setstring(STR *s, const char *txt)
-{
-       s->str = txt;
-}
-
 int
 next(STR *s, int *ret)
 {
diff -r 27df733fb865 -r 689f3c2ee1f5 usr.bin/tr/tr.c
--- a/usr.bin/tr/tr.c   Sun Aug 11 00:34:09 2013 +0000
+++ b/usr.bin/tr/tr.c   Sun Aug 11 00:39:22 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tr.c,v 1.16 2013/08/11 00:34:09 dholland Exp $ */
+/*     $NetBSD: tr.c,v 1.17 2013/08/11 00:39:22 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.16 2013/08/11 00:34:09 dholland Exp $");
+__RCSID("$NetBSD: tr.c,v 1.17 2013/08/11 00:39:22 dholland Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -54,7 +54,7 @@
 
 static int string1[NCHARS], string2[NCHARS];
 
-static void setup(int *, const char *, STR *, int);
+static void setup(int *, const char *, int, int);
 __dead static void usage(void);
 
 int
@@ -96,9 +96,6 @@
                break;
        }
 
-       s1 = str_create(1);
-       s2 = str_create(2);
-
        /*
         * tr -ds [-c] string1 string2
         * Delete all characters (or complemented characters) in string1.
@@ -108,16 +105,14 @@
                if (!isstring2)
                        usage();
 
-               setup(string1, argv[0], s1, cflag);
-               setup(string2, argv[1], s2, 0);
+               setup(string1, argv[0], 1, cflag);
+               setup(string2, argv[1], 2, 0);
                
                for (lastch = OOBCH; (ch = getchar()) != EOF;)
                        if (!string1[ch] && (!string2[ch] || lastch != ch)) {
                                lastch = ch;
                                (void)putchar(ch);
                        }
-               str_destroy(s1);
-               str_destroy(s2);
                exit(0);
        }
 
@@ -129,13 +124,11 @@
                if (isstring2)
                        usage();
 
-               setup(string1, argv[0], s1, cflag);
+               setup(string1, argv[0], 1, cflag);
 
                while ((ch = getchar()) != EOF)
                        if (!string1[ch])
                                (void)putchar(ch);
-               str_destroy(s1);
-               str_destroy(s2);
                exit(0);
        }
 
@@ -144,15 +137,13 @@
         * Squeeze all characters (or complemented characters) in string1.
         */
        if (sflag && !isstring2) {
-               setup(string1, argv[0], s1, cflag);
+               setup(string1, argv[0], 1, cflag);
 
                for (lastch = OOBCH; (ch = getchar()) != EOF;)
                        if (!string1[ch] || lastch != ch) {
                                lastch = ch;
                                (void)putchar(ch);
                        }
-               str_destroy(s1);
-               str_destroy(s2);
                exit(0);
        }
 
@@ -166,15 +157,16 @@
                usage();
 
        if (cflag) {
-               setup(string1, argv[0], s1, cflag);
+               setup(string1, argv[0], 1, cflag);
+               s1 = NULL; /* for safety */
                ch = -1;
        } else {
-               str_setstring(s1, argv[0]);
+               s1 = str_create(1, argv[0]);
                for (ch = 0; ch < NCHARS; ch++) {
                        string1[ch] = ch;
                }
        }
-       str_setstring(s2, argv[1]);
+       s2 = str_create(2, argv[1]);
 
        if (!next(s2, &ch2))
                errx(1, "empty string2");
@@ -218,24 +210,28 @@
                while ((ch = getchar()) != EOF)
                        (void)putchar(string1[ch]);
 
-       str_destroy(s1);
+       if (s1 != NULL) {
+               str_destroy(s1);
+       }
        str_destroy(s2);
        exit (0);
 }
 
 static void
-setup(int *string, const char *arg, STR *str, int cflag)
+setup(int *string, const char *arg, int whichstring, int cflag)
 {
        int cnt, *p;
        int ch;
+       STR *str;
 
-       str_setstring(str, arg);
+       str = str_create(whichstring, arg);
        memset(string, 0, NCHARS * sizeof(int));
        while (next(str, &ch))
                string[ch] = 1;
        if (cflag)
                for (p = string, cnt = NCHARS; cnt--; ++p)
                        *p = !*p;
+       str_destroy(str);
 }
 
 static void



Home | Main Index | Thread Index | Old Index