Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make make(1): inline Var_Parse_State into ApplyModif...



details:   https://anonhg.NetBSD.org/src/rev/ecacf7d3ee16
branches:  trunk
changeset: 974117:ecacf7d3ee16
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jul 24 07:52:44 2020 +0000

description:
make(1): inline Var_Parse_State into ApplyModifiersState

There was no need to have this as a separate struct.  Especially none of
the modifiers used by ModifyWords modify this state, therefore it was
not necessary to pass it by reference.  This makes the code easier to
understand since there's less modifiable state.

diffstat:

 usr.bin/make/var.c |  115 ++++++++++++++++++++++++----------------------------
 1 files changed, 53 insertions(+), 62 deletions(-)

diffs (truncated from 364 to 300 lines):

diff -r a606cf07f61c -r ecacf7d3ee16 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Fri Jul 24 07:29:18 2020 +0000
+++ b/usr.bin/make/var.c        Fri Jul 24 07:52:44 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.300 2020/07/24 07:29:18 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.301 2020/07/24 07:52:44 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.300 2020/07/24 07:29:18 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.301 2020/07/24 07:52:44 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.300 2020/07/24 07:29:18 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.301 2020/07/24 07:52:44 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -250,20 +250,6 @@
     VAR_NO_EXPORT      = 0x01  /* do not export */
 } VarSet_Flags;
 
-typedef struct {
-    /*
-     * The following fields are set by Var_Parse() when it
-     * encounters modifiers that need to keep state for use by
-     * subsequent modifiers within the same variable expansion.
-     */
-    Byte       varSpace;       /* Word separator in expansions */
-    Boolean    oneBigWord;     /* TRUE if we will treat the variable as a
-                                * single big word, even if it contains
-                                * embedded spaces (as opposed to the
-                                * usual behaviour of treating it as
-                                * several space-separated words). */
-} Var_Parse_State;
-
 #define BROPEN '{'
 #define BRCLOSE        '}'
 #define PROPEN '('
@@ -1521,7 +1507,8 @@
  * to scan the list backwards if first > last.
  */
 static char *
-VarSelectWords(Var_Parse_State *vpstate, const char *str, int first, int last)
+VarSelectWords(Byte sep, Boolean oneBigWord, const char *str, int first,
+              int last)
 {
     SepBuf buf;
     char **av;                 /* word list */
@@ -1529,9 +1516,9 @@
     int ac, i;
     int start, end, step;
 
-    SepBuf_Init(&buf, vpstate->varSpace);
-
-    if (vpstate->oneBigWord) {
+    SepBuf_Init(&buf, sep);
+
+    if (oneBigWord) {
        /* fake what brk_string() would do if there were only one word */
        ac = 1;
        av = bmake_malloc((ac + 1) * sizeof(char *));
@@ -1609,7 +1596,7 @@
  *-----------------------------------------------------------------------
  */
 static char *
-ModifyWords(GNode *ctx, Var_Parse_State *vpstate,
+ModifyWords(GNode *ctx, Byte sep, Boolean oneBigWord,
            const char *str, ModifyWordsCallback modifyWord, void *data)
 {
     SepBuf result;
@@ -1617,9 +1604,9 @@
     char *as;                  /* word list memory */
     int ac, i;
 
-    SepBuf_Init(&result, vpstate->varSpace);
-
-    if (vpstate->oneBigWord) {
+    SepBuf_Init(&result, sep);
+
+    if (oneBigWord) {
        /* fake what brk_string() would do if there were only one word */
        ac = 1;
        av = bmake_malloc((ac + 1) * sizeof(char *));
@@ -1646,7 +1633,6 @@
     free(as);
     free(av);
 
-    vpstate->varSpace = result.sep;
     return SepBuf_Destroy(&result, FALSE);
 }
 
@@ -2087,7 +2073,13 @@
     char termc;                        /* Character which terminated scan */
     char missing_delim;                /* For error reporting */
     int modifier;              /* that we are processing */
-    Var_Parse_State parsestate;        /* Flags passed to helper functions */
+
+    Byte       sep;            /* Word separator in expansions */
+    Boolean    oneBigWord;     /* TRUE if we will treat the variable as a
+                                * single big word, even if it contains
+                                * embedded spaces (as opposed to the
+                                * usual behaviour of treating it as
+                                * several space-separated words). */
 
     /* result */
     char *newStr;              /* New value to return */
@@ -2128,11 +2120,11 @@
     st->termc = *st->cp;
 
     args.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES);
-    int prev_sep = st->parsestate.varSpace;
-    st->parsestate.varSpace = ' ';
-    st->newStr = ModifyWords(st->ctxt, &st->parsestate, st->nstr,
+    int prev_sep = st->sep;
+    st->sep = ' ';             /* XXX: this is inconsistent */
+    st->newStr = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->nstr,
                             ModifyWord_Loop, &args);
-    st->parsestate.varSpace = prev_sep;
+    st->sep = prev_sep;
     Var_Delete(args.tvar, st->ctxt);
     free(args.tvar);
     free(args.str);
@@ -2409,7 +2401,7 @@
            st->v->name, st->nstr, pattern);
     ModifyWordsCallback callback = mod[0] == 'M'
        ? ModifyWord_Match : ModifyWord_NoMatch;
-    st->newStr = ModifyWords(st->ctxt, &st->parsestate, st->nstr,
+    st->newStr = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->nstr,
                             callback, pattern);
     free(pattern);
 }
@@ -2419,7 +2411,7 @@
 ApplyModifier_Subst(const char * const mod, ApplyModifiersState *st)
 {
     ModifyWord_SubstArgs args;
-    Var_Parse_State tmpparsestate = st->parsestate;
+    Boolean oneBigWord = st->oneBigWord;
     char delim = mod[1];
 
     st->cp = mod + 2;
@@ -2464,14 +2456,14 @@
            args.pflags |= VARP_SUB_ONE;
            continue;
        case 'W':
-           tmpparsestate.oneBigWord = TRUE;
+           oneBigWord = TRUE;
            continue;
        }
        break;
     }
 
     st->termc = *st->cp;
-    st->newStr = ModifyWords(st->ctxt, &tmpparsestate, st->nstr,
+    st->newStr = ModifyWords(st->ctxt, st->sep, oneBigWord, st->nstr,
                             ModifyWord_Subst, &args);
 
     free(lhs);
@@ -2488,7 +2480,7 @@
     ModifyWord_SubstRegexArgs args;
 
     args.pflags = 0;
-    Var_Parse_State tmpparsestate = st->parsestate;
+    Boolean oneBigWord = st->oneBigWord;
     char delim = mod[1];
 
     st->cp = mod + 2;
@@ -2517,7 +2509,7 @@
            args.pflags |= VARP_SUB_ONE;
            continue;
        case 'W':
-           tmpparsestate.oneBigWord = TRUE;
+           oneBigWord = TRUE;
            continue;
        }
        break;
@@ -2540,7 +2532,7 @@
     if (args.nsub > 10)
        args.nsub = 10;
     args.matches = bmake_malloc(args.nsub * sizeof(regmatch_t));
-    st->newStr = ModifyWords(st->ctxt, &tmpparsestate, st->nstr,
+    st->newStr = ModifyWords(st->ctxt, st->sep, oneBigWord, st->nstr,
                             ModifyWord_SubstRegex, &args);
     regfree(&args.re);
     free(args.replace);
@@ -2561,11 +2553,11 @@
 {
     if (sep[0] != st->endc && (sep[1] == st->endc || sep[1] == ':')) {
        /* ":ts<unrecognised><endc>" or ":ts<unrecognised>:" */
-       st->parsestate.varSpace = sep[0];
+       st->sep = sep[0];
        st->cp = sep + 1;
     } else if (sep[0] == st->endc || sep[0] == ':') {
        /* ":ts<endc>" or ":ts:" */
-       st->parsestate.varSpace = 0;    /* no separator */
+       st->sep = '\0';         /* no separator */
        st->cp = sep;
     } else if (sep[0] == '\\') {
        const char *xp = sep + 1;
@@ -2573,11 +2565,11 @@
 
        switch (sep[1]) {
        case 'n':
-           st->parsestate.varSpace = '\n';
+           st->sep = '\n';
            st->cp = sep + 2;
            break;
        case 't':
-           st->parsestate.varSpace = '\t';
+           st->sep = '\t';
            st->cp = sep + 2;
            break;
        case 'x':
@@ -2593,8 +2585,7 @@
 
            char *end;
        get_numeric:
-           st->parsestate.varSpace = strtoul(sep + 1 + (sep[1] == 'x'),
-                                             &end, base);
+           st->sep = strtoul(sep + 1 + (sep[1] == 'x'), &end, base);
            if (*end != ':' && *end != st->endc)
                return FALSE;
            st->cp = end;
@@ -2605,7 +2596,7 @@
     }
 
     st->termc = *st->cp;
-    st->newStr = ModifyWords(st->ctxt, &st->parsestate, st->nstr,
+    st->newStr = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->nstr,
                             ModifyWord_Copy, NULL);
     return TRUE;
 }
@@ -2626,7 +2617,7 @@
 
     /* Check for two-character options: ":tu", ":tl" */
     if (mod[1] == 'A') {       /* absolute path */
-       st->newStr = ModifyWords(st->ctxt, &st->parsestate, st->nstr,
+       st->newStr = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->nstr,
                                 ModifyWord_Realpath, NULL);
        st->cp = mod + 2;
        st->termc = *st->cp;
@@ -2643,7 +2634,7 @@
        st->cp = mod + 2;
        st->termc = *st->cp;
     } else if (mod[1] == 'W' || mod[1] == 'w') {
-       st->parsestate.oneBigWord = (mod[1] == 'W');
+       st->oneBigWord = mod[1] == 'W';
        st->newStr = st->nstr;
        st->cp = mod + 2;
        st->termc = *st->cp;
@@ -2684,7 +2675,7 @@
        int newStrSize = (sizeof(int) * CHAR_BIT + 2) / 3 + 2;
 
        st->newStr = bmake_malloc(newStrSize);
-       if (st->parsestate.oneBigWord) {
+       if (st->oneBigWord) {
            strncpy(st->newStr, "1", newStrSize);
        } else {
            /* XXX: brk_string() is a rather expensive
@@ -2703,14 +2694,14 @@
 
     if (estr[0] == '*' && estr[1] == '\0') {
        /* Found ":[*]" */
-       st->parsestate.oneBigWord = TRUE;
+       st->oneBigWord = TRUE;
        st->newStr = st->nstr;
        goto ok;
     }
 
     if (estr[0] == '@' && estr[1] == '\0') {
        /* Found ":[@]" */
-       st->parsestate.oneBigWord = FALSE;
+       st->oneBigWord = FALSE;
        st->newStr = st->nstr;
        goto ok;
     }
@@ -2742,7 +2733,7 @@
      */
     if (first == 0 && last == 0) {
        /* ":[0]" or perhaps ":[0..0]" */
-       st->parsestate.oneBigWord = TRUE;
+       st->oneBigWord = TRUE;
        st->newStr = st->nstr;
        goto ok;
     }
@@ -2752,7 +2743,7 @@
        goto bad_modifier;
 
     /* Normal case: select the words described by seldata. */
-    st->newStr = VarSelectWords(&st->parsestate, st->nstr, first, last);
+    st->newStr = VarSelectWords(st->sep, st->oneBigWord, st->nstr, first, last);
 



Home | Main Index | Thread Index | Old Index