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: merge duplicate code for expanding variab...



details:   https://anonhg.NetBSD.org/src/rev/a8d21ebecd62
branches:  trunk
changeset: 359656:a8d21ebecd62
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jan 15 18:34:41 2022 +0000

description:
make: merge duplicate code for expanding variable expressions

No functional change.

diffstat:

 usr.bin/make/arch.c    |  19 ++++---------
 usr.bin/make/main.c    |  13 ++------
 usr.bin/make/meta.c    |  15 ++-------
 usr.bin/make/nonints.h |   3 +-
 usr.bin/make/parse.c   |  20 ++-----------
 usr.bin/make/var.c     |  72 ++++++++++++++++++-------------------------------
 6 files changed, 46 insertions(+), 96 deletions(-)

diffs (truncated from 325 to 300 lines):

diff -r ae70f65ed0e1 -r a8d21ebecd62 usr.bin/make/arch.c
--- a/usr.bin/make/arch.c       Sat Jan 15 18:02:33 2022 +0000
+++ b/usr.bin/make/arch.c       Sat Jan 15 18:34:41 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arch.c,v 1.209 2021/12/15 12:58:01 rillig Exp $        */
+/*     $NetBSD: arch.c,v 1.210 2022/01/15 18:34:41 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -126,7 +126,7 @@
 #include "config.h"
 
 /*     "@(#)arch.c     8.2 (Berkeley) 1/2/94"  */
-MAKE_RCSID("$NetBSD: arch.c,v 1.209 2021/12/15 12:58:01 rillig Exp $");
+MAKE_RCSID("$NetBSD: arch.c,v 1.210 2022/01/15 18:34:41 rillig Exp $");
 
 typedef struct List ArchList;
 typedef struct ListNode ArchListNode;
@@ -236,12 +236,8 @@
        }
 
        spec[cp++ - spec] = '\0';
-       if (expandLib) {
-               char *expanded;
-               (void)Var_Subst(lib.str, scope, VARE_UNDEFERR, &expanded);
-               /* TODO: handle errors */
-               lib = FStr_InitOwn(expanded);
-       }
+       if (expandLib)
+               Var_Expand(&lib, scope, VARE_UNDEFERR);
 
        for (;;) {
                /*
@@ -317,13 +313,10 @@
                 */
                if (doSubst) {
                        char *fullName;
-                       char *p, *expandedMem;
+                       char *p;
                        const char *unexpandedMem = mem.str;
 
-                       (void)Var_Subst(mem.str, scope, VARE_UNDEFERR,
-                           &expandedMem);
-                       /* TODO: handle errors */
-                       mem = FStr_InitOwn(expandedMem);
+                       Var_Expand(&mem, scope, VARE_UNDEFERR);
 
                        /*
                         * Now form an archive spec and recurse to deal with
diff -r ae70f65ed0e1 -r a8d21ebecd62 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Sat Jan 15 18:02:33 2022 +0000
+++ b/usr.bin/make/main.c       Sat Jan 15 18:34:41 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.569 2022/01/10 20:32:28 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.570 2022/01/15 18:34:41 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.569 2022/01/10 20:32:28 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.570 2022/01/15 18:34:41 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -743,14 +743,7 @@
                return false;
        }
 
-       /* expand variable substitutions */
-       if (strchr(path.str, '$') != 0) {
-               char *expanded;
-               (void)Var_Subst(path.str, SCOPE_GLOBAL, VARE_WANTRES, &expanded);
-               /* TODO: handle errors */
-               FStr_Done(&path);
-               path = FStr_InitOwn(expanded);
-       }
+       Var_Expand(&path, SCOPE_GLOBAL, VARE_WANTRES);
 
        (void)Main_SetObjdir(writable, "%s%s", path.str, suffix);
 
diff -r ae70f65ed0e1 -r a8d21ebecd62 usr.bin/make/meta.c
--- a/usr.bin/make/meta.c       Sat Jan 15 18:02:33 2022 +0000
+++ b/usr.bin/make/meta.c       Sat Jan 15 18:34:41 2022 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.189 2022/01/15 09:08:57 rillig Exp $ */
+/*      $NetBSD: meta.c,v 1.190 2022/01/15 18:34:41 rillig Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -327,15 +327,14 @@
     static const char *p_make = NULL;
     static size_t p_len;
     char *mp = NULL;
-    const char *cp, *cp2;
+    const char *cp2;
     bool rc = false;
 
     if (p_make == NULL) {
        p_make = Var_Value(gn, ".MAKE").str;
        p_len = strlen(p_make);
     }
-    cp = strchr(cmd, '$');
-    if (cp != NULL) {
+    if (strchr(cmd, '$') != NULL) {
        (void)Var_Subst(cmd, gn, VARE_WANTRES, &mp);
        /* TODO: handle errors */
        cmd = mp;
@@ -382,13 +381,7 @@
 {
     FStr xcmd = FStr_InitRefer(ucmd);
 
-    if (strchr(ucmd, '$') != NULL) {
-       char *expanded;
-       (void)Var_Subst(ucmd, gn, VARE_WANTRES, &expanded);
-       /* TODO: handle errors */
-       xcmd = FStr_InitOwn(expanded);
-    }
-
+    Var_Expand(&xcmd, gn, VARE_WANTRES);
     fprintf(fp, "CMD %s\n", xcmd.str);
     FStr_Done(&xcmd);
 }
diff -r ae70f65ed0e1 -r a8d21ebecd62 usr.bin/make/nonints.h
--- a/usr.bin/make/nonints.h    Sat Jan 15 18:02:33 2022 +0000
+++ b/usr.bin/make/nonints.h    Sat Jan 15 18:34:41 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: nonints.h,v 1.237 2022/01/09 18:49:28 rillig Exp $     */
+/*     $NetBSD: nonints.h,v 1.238 2022/01/15 18:34:41 rillig Exp $     */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -310,6 +310,7 @@
 const char *GNode_ValueDirect(GNode *, const char *) MAKE_ATTR_USE;
 VarParseResult Var_Parse(const char **, GNode *, VarEvalMode, FStr *);
 VarParseResult Var_Subst(const char *, GNode *, VarEvalMode, char **);
+void Var_Expand(FStr *, GNode *, VarEvalMode);
 void Var_Stats(void);
 void Var_Dump(GNode *);
 void Var_ReexportVars(void);
diff -r ae70f65ed0e1 -r a8d21ebecd62 usr.bin/make/parse.c
--- a/usr.bin/make/parse.c      Sat Jan 15 18:02:33 2022 +0000
+++ b/usr.bin/make/parse.c      Sat Jan 15 18:34:41 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parse.c,v 1.649 2022/01/09 19:57:14 rillig Exp $       */
+/*     $NetBSD: parse.c,v 1.650 2022/01/15 18:34:41 rillig Exp $       */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -106,7 +106,7 @@
 #include "pathnames.h"
 
 /*     "@(#)parse.c    8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: parse.c,v 1.649 2022/01/09 19:57:14 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.650 2022/01/15 18:34:41 rillig Exp $");
 
 /*
  * A file being read.
@@ -1621,13 +1621,7 @@
        char *output, *error;
 
        cmd = FStr_InitRefer(uvalue);
-       if (strchr(cmd.str, '$') != NULL) {
-               char *expanded;
-               (void)Var_Subst(cmd.str, SCOPE_CMDLINE, VARE_UNDEFERR,
-                   &expanded);
-               /* TODO: handle errors */
-               cmd = FStr_InitOwn(expanded);
-       }
+       Var_Expand(&cmd, SCOPE_CMDLINE, VARE_UNDEFERR);
 
        output = Cmd_Exec(cmd.str, &error);
        Var_SetExpand(scope, name, output);
@@ -1955,13 +1949,7 @@
 
        *p = '\0';
 
-       if (strchr(file.str, '$') != NULL) {
-               char *xfile;
-               Var_Subst(file.str, SCOPE_CMDLINE, VARE_WANTRES, &xfile);
-               /* TODO: handle errors */
-               file = FStr_InitOwn(xfile);
-       }
-
+       Var_Expand(&file, SCOPE_CMDLINE, VARE_WANTRES);
        IncludeFile(file.str, endc == '>', directive[0] == 'd', silent);
        FStr_Done(&file);
 }
diff -r ae70f65ed0e1 -r a8d21ebecd62 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Jan 15 18:02:33 2022 +0000
+++ b/usr.bin/make/var.c        Sat Jan 15 18:34:41 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.1000 2022/01/09 18:49:28 rillig Exp $        */
+/*     $NetBSD: var.c,v 1.1001 2022/01/15 18:34:41 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1000 2022/01/09 18:49:28 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1001 2022/01/15 18:34:41 rillig Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -530,14 +530,7 @@
 {
        FStr varname = FStr_InitRefer(name);
 
-       if (strchr(varname.str, '$') != NULL) {
-               char *expanded;
-               (void)Var_Subst(varname.str, SCOPE_GLOBAL, VARE_WANTRES,
-                   &expanded);
-               /* TODO: handle errors */
-               varname = FStr_InitOwn(expanded);
-       }
-
+       Var_Expand(&varname, SCOPE_GLOBAL, VARE_WANTRES);
        Var_Delete(scope, varname.str);
        FStr_Done(&varname);
 }
@@ -1049,12 +1042,7 @@
 
        assert(val != NULL);
 
-       if (strchr(varname.str, '$') != NULL) {
-               char *expanded;
-               (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded);
-               /* TODO: handle errors */
-               varname = FStr_InitOwn(expanded);
-       }
+       Var_Expand(&varname, scope, VARE_WANTRES);
 
        if (varname.str[0] == '\0') {
                DEBUG2(VAR,
@@ -1178,22 +1166,14 @@
 
        assert(val != NULL);
 
-       if (strchr(name, '$') != NULL) {
-               char *expanded;
-               (void)Var_Subst(name, scope, VARE_WANTRES, &expanded);
-               /* TODO: handle errors */
-               xname = FStr_InitOwn(expanded);
-               if (expanded[0] == '\0') {
-                       DEBUG2(VAR,
-                           "Var_AppendExpand: variable name \"%s\" expands "
-                           "to empty string, with value \"%s\" - ignored\n",
-                           name, val);
-                       FStr_Done(&xname);
-                       return;
-               }
-       }
-
-       Var_Append(scope, xname.str, val);
+       Var_Expand(&xname, scope, VARE_WANTRES);
+       if (xname.str != name && xname.str[0] == '\0')
+               DEBUG2(VAR,
+                   "Var_AppendExpand: variable name \"%s\" expands "
+                   "to empty string, with value \"%s\" - ignored\n",
+                   name, val);
+       else
+               Var_Append(scope, xname.str, val);
 
        FStr_Done(&xname);
 }
@@ -1229,13 +1209,7 @@
        FStr varname = FStr_InitRefer(name);
        bool exists;
 
-       if (strchr(varname.str, '$') != NULL) {
-               char *expanded;
-               (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded);
-               /* TODO: handle errors */
-               varname = FStr_InitOwn(expanded);
-       }
-
+       Var_Expand(&varname, scope, VARE_WANTRES);
        exists = Var_Exists(scope, varname.str);
        FStr_Done(&varname);
        return exists;
@@ -1479,7 +1453,6 @@
 {
        const struct ModifyWord_SysVSubstArgs *args = data;
        FStr rhs;
-       char *rhsExp;
        const char *percent;
 
        if (Substring_IsEmpty(word))
@@ -1491,11 +1464,7 @@
                goto no_match;
 
        rhs = FStr_InitRefer(args->rhs);
-       if (strchr(rhs.str, '$') != NULL) {
-               (void)Var_Subst(args->rhs, args->scope, VARE_WANTRES, &rhsExp);
-               /* TODO: handle errors */
-               rhs = FStr_InitOwn(rhsExp);



Home | Main Index | Thread Index | Old Index