Module Name:    src
Committed By:   sjg
Date:           Sun Oct 11 04:51:24 UTC 2015

Modified Files:
        src/usr.bin/make: arch.c compat.c cond.c for.c job.c main.c make.c
            meta.c nonints.h parse.c suff.c var.c
        src/usr.bin/make/unit-tests: varmisc.exp varmisc.mk

Log Message:
Add Boolean wantit to Var_Parse and Var_Subst

wantit will be FALSE when we are just consuming to discard
in which case we skip "expensive" things like Cmd_Exec.

Reviewed by: christos


To generate a diff of this commit:
cvs rdiff -u -r1.63 -r1.64 src/usr.bin/make/arch.c
cvs rdiff -u -r1.100 -r1.101 src/usr.bin/make/compat.c
cvs rdiff -u -r1.68 -r1.69 src/usr.bin/make/cond.c src/usr.bin/make/nonints.h
cvs rdiff -u -r1.49 -r1.50 src/usr.bin/make/for.c
cvs rdiff -u -r1.180 -r1.181 src/usr.bin/make/job.c
cvs rdiff -u -r1.233 -r1.234 src/usr.bin/make/main.c
cvs rdiff -u -r1.91 -r1.92 src/usr.bin/make/make.c
cvs rdiff -u -r1.39 -r1.40 src/usr.bin/make/meta.c
cvs rdiff -u -r1.204 -r1.205 src/usr.bin/make/parse.c
cvs rdiff -u -r1.73 -r1.74 src/usr.bin/make/suff.c
cvs rdiff -u -r1.196 -r1.197 src/usr.bin/make/var.c
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/make/unit-tests/varmisc.exp
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/make/unit-tests/varmisc.mk

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/make/arch.c
diff -u src/usr.bin/make/arch.c:1.63 src/usr.bin/make/arch.c:1.64
--- src/usr.bin/make/arch.c:1.63	Tue Jun 12 19:21:50 2012
+++ src/usr.bin/make/arch.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $	*/
+/*	$NetBSD: arch.c,v 1.64 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $";
+static char rcsid[] = "$NetBSD: arch.c,v 1.64 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)arch.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: arch.c,v 1.63 2012/06/12 19:21:50 joerg Exp $");
+__RCSID("$NetBSD: arch.c,v 1.64 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -261,7 +261,7 @@ Arch_ParseArchive(char **linePtr, Lst no
 	    void	*freeIt;
 	    char	*result;
 
-	    result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
+	    result = Var_Parse(cp, ctxt, TRUE, TRUE, &length, &freeIt);
 	    if (freeIt)
 		free(freeIt);
 	    if (result == var_Error) {
@@ -276,7 +276,7 @@ Arch_ParseArchive(char **linePtr, Lst no
 
     *cp++ = '\0';
     if (subLibName) {
-	libName = Var_Subst(NULL, libName, ctxt, TRUE);
+	libName = Var_Subst(NULL, libName, ctxt, TRUE, TRUE);
     }
 
 
@@ -302,7 +302,7 @@ Arch_ParseArchive(char **linePtr, Lst no
 		void	*freeIt;
 		char	*result;
 
-		result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
+		result = Var_Parse(cp, ctxt, TRUE, TRUE, &length, &freeIt);
 		if (freeIt)
 		    free(freeIt);
 		if (result == var_Error) {
@@ -355,7 +355,7 @@ Arch_ParseArchive(char **linePtr, Lst no
 	    char    *oldMemName = memName;
 	    size_t   sz;
 
-	    memName = Var_Subst(NULL, memName, ctxt, TRUE);
+	    memName = Var_Subst(NULL, memName, ctxt, TRUE, TRUE);
 
 	    /*
 	     * Now form an archive spec and recurse to deal with nested

Index: src/usr.bin/make/compat.c
diff -u src/usr.bin/make/compat.c:1.100 src/usr.bin/make/compat.c:1.101
--- src/usr.bin/make/compat.c:1.100	Fri Jun 19 14:32:04 2015
+++ src/usr.bin/make/compat.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: compat.c,v 1.100 2015/06/19 14:32:04 christos Exp $	*/
+/*	$NetBSD: compat.c,v 1.101 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: compat.c,v 1.100 2015/06/19 14:32:04 christos Exp $";
+static char rcsid[] = "$NetBSD: compat.c,v 1.101 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)compat.c	8.2 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: compat.c,v 1.100 2015/06/19 14:32:04 christos Exp $");
+__RCSID("$NetBSD: compat.c,v 1.101 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -210,7 +210,7 @@ CompatRunCommand(void *cmdp, void *gnp)
     doIt = FALSE;
     
     cmdNode = Lst_Member(gn->commands, cmd);
-    cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
+    cmdStart = Var_Subst(NULL, cmd, gn, FALSE, TRUE);
 
     /*
      * brk_string will return an argv with a NULL in av[0], thus causing

Index: src/usr.bin/make/cond.c
diff -u src/usr.bin/make/cond.c:1.68 src/usr.bin/make/cond.c:1.69
--- src/usr.bin/make/cond.c:1.68	Tue May  5 21:51:09 2015
+++ src/usr.bin/make/cond.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.68 2015/05/05 21:51:09 sjg Exp $	*/
+/*	$NetBSD: cond.c,v 1.69 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: cond.c,v 1.68 2015/05/05 21:51:09 sjg Exp $";
+static char rcsid[] = "$NetBSD: cond.c,v 1.69 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: cond.c,v 1.68 2015/05/05 21:51:09 sjg Exp $");
+__RCSID("$NetBSD: cond.c,v 1.69 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -289,7 +289,7 @@ CondGetArg(char **linePtr, char **argPtr
 	    int		len;
 	    void	*freeIt;
 
-	    cp2 = Var_Parse(cp, VAR_CMD, TRUE, &len, &freeIt);
+	    cp2 = Var_Parse(cp, VAR_CMD, TRUE, TRUE, &len, &freeIt);
 	    Buf_AddBytes(&buf, strlen(cp2), cp2);
 	    if (freeIt)
 		free(freeIt);
@@ -571,7 +571,7 @@ CondGetString(Boolean doEval, Boolean *q
 	case '$':
 	    /* if we are in quotes, then an undefined variable is ok */
 	    str = Var_Parse(condExpr, VAR_CMD, (qt ? 0 : doEval),
-			    &len, freeIt);
+			    TRUE, &len, freeIt);
 	    if (str == var_Error) {
 		if (*freeIt) {
 		    free(*freeIt);
@@ -823,7 +823,7 @@ get_mpt_arg(char **linePtr, char **argPt
     /* We do all the work here and return the result as the length */
     *argPtr = NULL;
 
-    val = Var_Parse(cp - 1, VAR_CMD, FALSE, &length, &freeIt);
+    val = Var_Parse(cp - 1, VAR_CMD, FALSE, TRUE, &length, &freeIt);
     /*
      * Advance *linePtr to beyond the closing ). Note that
      * we subtract one because 'length' is calculated from 'cp - 1'.
Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.68 src/usr.bin/make/nonints.h:1.69
--- src/usr.bin/make/nonints.h:1.68	Tue May  5 21:51:09 2015
+++ src/usr.bin/make/nonints.h	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.68 2015/05/05 21:51:09 sjg Exp $	*/
+/*	$NetBSD: nonints.h,v 1.69 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -182,8 +182,8 @@ void Var_Set(const char *, const char *,
 void Var_Append(const char *, const char *, GNode *);
 Boolean Var_Exists(const char *, GNode *);
 char *Var_Value(const char *, GNode *, char **);
-char *Var_Parse(const char *, GNode *, Boolean, int *, void **);
-char *Var_Subst(const char *, const char *, GNode *, Boolean);
+char *Var_Parse(const char *, GNode *, Boolean, Boolean, int *, void **);
+char *Var_Subst(const char *, const char *, GNode *, Boolean, Boolean);
 char *Var_GetTail(const char *);
 char *Var_GetHead(const char *);
 void Var_Init(void);

Index: src/usr.bin/make/for.c
diff -u src/usr.bin/make/for.c:1.49 src/usr.bin/make/for.c:1.50
--- src/usr.bin/make/for.c:1.49	Sun Jun  3 04:29:40 2012
+++ src/usr.bin/make/for.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $	*/
+/*	$NetBSD: for.c,v 1.50 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1992, The Regents of the University of California.
@@ -30,14 +30,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $";
+static char rcsid[] = "$NetBSD: for.c,v 1.50 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)for.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: for.c,v 1.49 2012/06/03 04:29:40 sjg Exp $");
+__RCSID("$NetBSD: for.c,v 1.50 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -216,7 +216,7 @@ For_Eval(char *line)
      * We can't do the escapes here - because we don't know whether
      * we are substuting into ${...} or $(...).
      */
-    sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE);
+    sub = Var_Subst(NULL, ptr, VAR_GLOBAL, FALSE, TRUE);
 
     /*
      * Split into words allowing for quoted strings.

Index: src/usr.bin/make/job.c
diff -u src/usr.bin/make/job.c:1.180 src/usr.bin/make/job.c:1.181
--- src/usr.bin/make/job.c:1.180	Thu Apr 16 13:31:03 2015
+++ src/usr.bin/make/job.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: job.c,v 1.180 2015/04/16 13:31:03 joerg Exp $	*/
+/*	$NetBSD: job.c,v 1.181 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: job.c,v 1.180 2015/04/16 13:31:03 joerg Exp $";
+static char rcsid[] = "$NetBSD: job.c,v 1.181 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)job.c	8.2 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: job.c,v 1.180 2015/04/16 13:31:03 joerg Exp $");
+__RCSID("$NetBSD: job.c,v 1.181 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -702,7 +702,7 @@ JobPrintCommand(void *cmdp, void *jobp)
 
     numCommands += 1;
 
-    cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
+    cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE, TRUE);
 
     cmdTemplate = "%s\n";
 
@@ -890,7 +890,7 @@ JobPrintCommand(void *cmdp, void *jobp)
 static int
 JobSaveCommand(void *cmd, void *gn)
 {
-    cmd = Var_Subst(NULL, (char *)cmd, (GNode *)gn, FALSE);
+    cmd = Var_Subst(NULL, (char *)cmd, (GNode *)gn, FALSE, TRUE);
     (void)Lst_AtEnd(postCommands->commands, cmd);
     return(0);
 }
@@ -2178,7 +2178,8 @@ Job_SetPrefix(void)
 	Var_Set(MAKE_JOB_PREFIX, "---", VAR_GLOBAL, 0);
     }
 
-    targPrefix = Var_Subst(NULL, "${" MAKE_JOB_PREFIX "}", VAR_GLOBAL, 0);
+    targPrefix = Var_Subst(NULL, "${" MAKE_JOB_PREFIX "}",
+			   VAR_GLOBAL, FALSE, TRUE);
 }
 
 /*-

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.233 src/usr.bin/make/main.c:1.234
--- src/usr.bin/make/main.c:1.233	Thu Sep 10 17:15:11 2015
+++ src/usr.bin/make/main.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.233 2015/09/10 17:15:11 sjg Exp $	*/
+/*	$NetBSD: main.c,v 1.234 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.233 2015/09/10 17:15:11 sjg Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.234 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.233 2015/09/10 17:15:11 sjg Exp $");
+__RCSID("$NetBSD: main.c,v 1.234 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -692,7 +692,7 @@ Main_SetObjdir(const char *path)
 	/* expand variable substitutions */
 	if (strchr(path, '$') != 0) {
 		snprintf(buf, MAXPATHLEN, "%s", path);
-		path = p = Var_Subst(NULL, buf, VAR_GLOBAL, 0);
+		path = p = Var_Subst(NULL, buf, VAR_GLOBAL, FALSE, TRUE);
 	}
 
 	if (path[0] != '/') {
@@ -775,7 +775,8 @@ MakeMode(const char *mode)
     char *mp = NULL;
 
     if (!mode)
-	mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}", VAR_GLOBAL, 0);
+	mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}",
+			      VAR_GLOBAL, FALSE, TRUE);
 
     if (mode && *mode) {
 	if (strstr(mode, "compat")) {
@@ -1217,7 +1218,7 @@ main(int argc, char **argv)
 			    (char *)Lst_Datum(ln));
 	} else {
 	    p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}",
-		VAR_CMD, 0);
+	        VAR_CMD, FALSE, TRUE);
 	    if (p1) {
 		(void)str2Lst_Append(makefiles, p1, NULL);
 		(void)Lst_Find(makefiles, NULL, ReadMakefile);
@@ -1228,7 +1229,7 @@ main(int argc, char **argv)
 	/* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */
 	if (!noBuiltins || !printVars) {
 	    makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}",
-		VAR_CMD, 0);
+		VAR_CMD, FALSE, TRUE);
 	    doing_depend = TRUE;
 	    (void)ReadMakefile(makeDependfile, NULL);
 	    doing_depend = FALSE;
@@ -1267,7 +1268,7 @@ main(int argc, char **argv)
 		 */
 		static char VPATH[] = "${VPATH}";
 
-		vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
+		vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE, TRUE);
 		path = vpath;
 		do {
 			/* skip to end of directory */
@@ -1314,14 +1315,16 @@ main(int argc, char **argv)
 			char *value;
 			
 			if (strchr(var, '$')) {
-				value = p1 = Var_Subst(NULL, var, VAR_GLOBAL, 0);
+			    value = p1 = Var_Subst(NULL, var, VAR_GLOBAL,
+						   FALSE, TRUE);
 			} else if (expandVars) {
 				char tmp[128];
 								
 				if (snprintf(tmp, sizeof(tmp), "${%s}", var) >= (int)(sizeof(tmp)))
 					Fatal("%s: variable name too big: %s",
 					      progname, var);
-				value = p1 = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+				value = p1 = Var_Subst(NULL, tmp, VAR_GLOBAL,
+						       FALSE, TRUE);
 			} else {
 				value = Var_Value(var, VAR_GLOBAL, &p1);
 			}
@@ -1859,7 +1862,7 @@ PrintOnError(GNode *gn, const char *s)
     }
     strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
 	    sizeof(tmp) - 1);
-    cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+    cp = Var_Subst(NULL, tmp, VAR_GLOBAL, FALSE, TRUE);
     if (cp) {
 	if (*cp)
 	    printf("%s", cp);
@@ -1888,7 +1891,7 @@ Main_ExportMAKEFLAGS(Boolean first)
     
     strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
 	    sizeof(tmp));
-    s = Var_Subst(NULL, tmp, VAR_CMD, 0);
+    s = Var_Subst(NULL, tmp, VAR_CMD, FALSE, TRUE);
     if (s && *s) {
 #ifdef POSIX
 	setenv("MAKEFLAGS", s, 1);
@@ -1910,7 +1913,8 @@ getTmpdir(void)
 	 * Honor $TMPDIR but only if it is valid.
 	 * Ensure it ends with /.
 	 */
-	tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL, 0);
+	tmpdir = Var_Subst(NULL, "${TMPDIR:tA:U" _PATH_TMP "}/", VAR_GLOBAL,
+			   FALSE, TRUE);
 	if (stat(tmpdir, &st) < 0 || !S_ISDIR(st.st_mode)) {
 	    free(tmpdir);
 	    tmpdir = bmake_strdup(_PATH_TMP);
@@ -1965,7 +1969,7 @@ getBoolean(const char *name, Boolean bf)
     char *cp;
 
     if (snprintf(tmp, sizeof(tmp), "${%s:tl}", name) < (int)(sizeof(tmp))) {
-	cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+	cp = Var_Subst(NULL, tmp, VAR_GLOBAL, FALSE, TRUE);
 
 	if (cp) {
 	    switch(*cp) {

Index: src/usr.bin/make/make.c
diff -u src/usr.bin/make/make.c:1.91 src/usr.bin/make/make.c:1.92
--- src/usr.bin/make/make.c:1.91	Sat Oct 18 08:33:30 2014
+++ src/usr.bin/make/make.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.c,v 1.91 2014/10/18 08:33:30 snj Exp $	*/
+/*	$NetBSD: make.c,v 1.92 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: make.c,v 1.91 2014/10/18 08:33:30 snj Exp $";
+static char rcsid[] = "$NetBSD: make.c,v 1.92 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)make.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: make.c,v 1.91 2014/10/18 08:33:30 snj Exp $");
+__RCSID("$NetBSD: make.c,v 1.92 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -485,7 +485,7 @@ Make_HandleUse(GNode *cgn, GNode *pgn)
 		if (gn->name)
 		    free(gn->name);
 	    }
-	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE);
+	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE, TRUE);
 	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
 		/* See if we have a target for this node. */
 		tgn = Targ_FindNode(gn->name, TARG_NOCREATE);

Index: src/usr.bin/make/meta.c
diff -u src/usr.bin/make/meta.c:1.39 src/usr.bin/make/meta.c:1.40
--- src/usr.bin/make/meta.c:1.39	Sat Oct 10 03:58:59 2015
+++ src/usr.bin/make/meta.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*      $NetBSD: meta.c,v 1.39 2015/10/10 03:58:59 sjg Exp $ */
+/*      $NetBSD: meta.c,v 1.40 2015/10/11 04:51:24 sjg Exp $ */
 
 /*
  * Implement 'meta' mode.
@@ -324,7 +324,7 @@ is_submake(void *cmdp, void *gnp)
     }
     cp = strchr(cmd, '$');
     if ((cp)) {
-	mp = Var_Subst(NULL, cmd, gn, FALSE);
+	mp = Var_Subst(NULL, cmd, gn, FALSE, TRUE);
 	cmd = mp;
     }
     cp2 = strstr(cmd, p_make);
@@ -367,7 +367,7 @@ printCMD(void *cmdp, void *mfpp)
     char *cp = NULL;
 
     if (strchr(cmd, '$')) {
-	cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE);
+	cmd = cp = Var_Subst(NULL, cmd, mfp->gn, FALSE, TRUE);
     }
     fprintf(mfp->fp, "CMD %s\n", cmd);
     if (cp)
@@ -462,7 +462,7 @@ meta_create(BuildMon *pbm, GNode *gn)
 	char *mp;
 
 	/* Describe the target we are building */
-	mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, 0);
+	mp = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", gn, FALSE, TRUE);
 	if (*mp)
 	    fprintf(stdout, "%s\n", mp);
 	free(mp);
@@ -605,7 +605,8 @@ meta_mode_init(const char *make_mode)
      * We consider ourselves master of all within ${.MAKE.META.BAILIWICK}
      */
     metaBailiwick = Lst_Init(FALSE);
-    cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL, 0);
+    cp = Var_Subst(NULL, "${.MAKE.META.BAILIWICK:O:u:tA}", VAR_GLOBAL,
+		   FALSE, TRUE);
     if (cp) {
 	str2Lst_Append(metaBailiwick, cp, NULL);
     }
@@ -616,7 +617,8 @@ meta_mode_init(const char *make_mode)
     Var_Append(MAKE_META_IGNORE_PATHS,
 	       "/dev /etc /proc /tmp /var/run /var/tmp ${TMPDIR}", VAR_GLOBAL);
     cp = Var_Subst(NULL,
-		   "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL, 0);
+		   "${" MAKE_META_IGNORE_PATHS ":O:u:tA}", VAR_GLOBAL,
+		   FALSE, TRUE);
     if (cp) {
 	str2Lst_Append(metaIgnorePaths, cp, NULL);
     }
@@ -727,7 +729,8 @@ meta_job_output(Job *job, char *cp, cons
 	    if (!meta_prefix) {
 		char *cp2;
 
-		meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}", VAR_GLOBAL, 0);
+		meta_prefix = Var_Subst(NULL, "${" MAKE_META_PREFIX "}",
+					VAR_GLOBAL, FALSE, TRUE);
 		if ((cp2 = strchr(meta_prefix, '$')))
 		    meta_prefix_len = cp2 - meta_prefix;
 		else
@@ -1312,7 +1315,7 @@ meta_oodate(GNode *gn, Boolean oodate)
 			if (DEBUG(META))
 			    fprintf(debug_file, "%s: %d: cannot compare command using .OODATE\n", fname, lineno);
 		    }
-		    cmd = Var_Subst(NULL, cmd, gn, TRUE);
+		    cmd = Var_Subst(NULL, cmd, gn, TRUE, TRUE);
 
 		    if ((cp = strchr(cmd, '\n'))) {
 			int n;

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.204 src/usr.bin/make/parse.c:1.205
--- src/usr.bin/make/parse.c:1.204	Thu Sep 18 08:06:13 2014
+++ src/usr.bin/make/parse.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.204 2014/09/18 08:06:13 dholland Exp $	*/
+/*	$NetBSD: parse.c,v 1.205 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: parse.c,v 1.204 2014/09/18 08:06:13 dholland Exp $";
+static char rcsid[] = "$NetBSD: parse.c,v 1.205 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)parse.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: parse.c,v 1.204 2014/09/18 08:06:13 dholland Exp $");
+__RCSID("$NetBSD: parse.c,v 1.205 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -802,7 +802,7 @@ ParseMessage(char *line)
     while (isspace((u_char)*line))
 	line++;
 
-    line = Var_Subst(NULL, line, VAR_CMD, 0);
+    line = Var_Subst(NULL, line, VAR_CMD, FALSE, TRUE);
     Parse_Error(mtype, "%s", line);
     free(line);
 
@@ -1219,7 +1219,7 @@ ParseDoDependency(char *line)
 		int 	length;
 		void    *freeIt;
 
-		(void)Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
+		(void)Var_Parse(cp, VAR_CMD, TRUE, TRUE, &length, &freeIt);
 		if (freeIt)
 		    free(freeIt);
 		cp += length-1;
@@ -1934,7 +1934,7 @@ Parse_DoVar(char *line, GNode *ctxt)
 	if (!Var_Exists(line, ctxt))
 	    Var_Set(line, "", ctxt, 0);
 
-	cp = Var_Subst(NULL, cp, ctxt, FALSE);
+	cp = Var_Subst(NULL, cp, ctxt, FALSE, TRUE);
 	oldVars = oldOldVars;
 	freeCp = TRUE;
 
@@ -1949,7 +1949,7 @@ Parse_DoVar(char *line, GNode *ctxt)
 	     * expansion on the whole thing. The resulting string will need
 	     * freeing when we're done, so set freeCmd to TRUE.
 	     */
-	    cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
+	    cp = Var_Subst(NULL, cp, VAR_CMD, TRUE, TRUE);
 	    freeCp = TRUE;
 	}
 
@@ -2288,7 +2288,7 @@ ParseDoInclude(char *line)
      * Substitute for any variables in the file name before trying to
      * find the thing.
      */
-    file = Var_Subst(NULL, file, VAR_CMD, FALSE);
+    file = Var_Subst(NULL, file, VAR_CMD, FALSE, TRUE);
 
     Parse_include_file(file, endc == '>', silent);
     free(file);
@@ -2514,7 +2514,7 @@ ParseTraditionalInclude(char *line)
      * Substitute for any variables in the file name before trying to
      * find the thing.
      */
-    all_files = Var_Subst(NULL, file, VAR_CMD, FALSE);
+    all_files = Var_Subst(NULL, file, VAR_CMD, FALSE, TRUE);
 
     if (*file == '\0') {
 	Parse_Error(PARSE_FATAL,
@@ -2582,7 +2582,7 @@ ParseGmakeExport(char *line)
     /*
      * Expand the value before putting it in the environment.
      */
-    value = Var_Subst(NULL, value, VAR_CMD, FALSE);
+    value = Var_Subst(NULL, value, VAR_CMD, FALSE, TRUE);
     setenv(variable, value, 1);
 }
 #endif
@@ -3131,7 +3131,7 @@ Parse_File(const char *name, int fd)
 	     * variables expanded before being parsed. Tell the variable
 	     * module to complain if some variable is undefined...
 	     */
-	    line = Var_Subst(NULL, line, VAR_CMD, TRUE);
+	    line = Var_Subst(NULL, line, VAR_CMD, TRUE, TRUE);
 
 	    /*
 	     * Need a non-circular list for the target nodes

Index: src/usr.bin/make/suff.c
diff -u src/usr.bin/make/suff.c:1.73 src/usr.bin/make/suff.c:1.74
--- src/usr.bin/make/suff.c:1.73	Sun Sep  7 20:55:34 2014
+++ src/usr.bin/make/suff.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: suff.c,v 1.73 2014/09/07 20:55:34 joerg Exp $	*/
+/*	$NetBSD: suff.c,v 1.74 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: suff.c,v 1.73 2014/09/07 20:55:34 joerg Exp $";
+static char rcsid[] = "$NetBSD: suff.c,v 1.74 2015/10/11 04:51:24 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)suff.c	8.4 (Berkeley) 3/21/94";
 #else
-__RCSID("$NetBSD: suff.c,v 1.73 2014/09/07 20:55:34 joerg Exp $");
+__RCSID("$NetBSD: suff.c,v 1.74 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1543,7 +1543,7 @@ SuffExpandChildren(LstNode cln, GNode *p
     if (DEBUG(SUFF)) {
 	fprintf(debug_file, "Expanding \"%s\"...", cgn->name);
     }
-    cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
+    cp = Var_Subst(NULL, cgn->name, pgn, TRUE, TRUE);
 
     if (cp != NULL) {
 	Lst	    members = Lst_Init(FALSE);
@@ -1596,7 +1596,7 @@ SuffExpandChildren(LstNode cln, GNode *p
 		    int 	len;
 		    void	*freeIt;
 
-		    junk = Var_Parse(cp, pgn, TRUE, &len, &freeIt);
+		    junk = Var_Parse(cp, pgn, TRUE, TRUE, &len, &freeIt);
 		    if (junk != var_Error) {
 			cp += len - 1;
 		    }

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.196 src/usr.bin/make/var.c:1.197
--- src/usr.bin/make/var.c:1.196	Tue Oct  6 17:36:25 2015
+++ src/usr.bin/make/var.c	Sun Oct 11 04:51:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.196 2015/10/06 17:36:25 christos Exp $	*/
+/*	$NetBSD: var.c,v 1.197 2015/10/11 04:51:24 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.196 2015/10/06 17:36:25 christos Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.197 2015/10/11 04:51:24 sjg 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.196 2015/10/06 17:36:25 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.197 2015/10/11 04:51:24 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -542,7 +542,7 @@ Var_Delete(const char *name, GNode *ctxt
     char *cp;
     
     if (strchr(name, '$')) {
-	cp = Var_Subst(NULL, name, VAR_GLOBAL, 0);
+	cp = Var_Subst(NULL, name, VAR_GLOBAL, FALSE, TRUE);
     } else {
 	cp = (char *)name;
     }
@@ -633,7 +633,7 @@ Var_Export1(const char *name, int parent
 	}
 	n = snprintf(tmp, sizeof(tmp), "${%s}", name);
 	if (n < (int)sizeof(tmp)) {
-	    val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+	    val = Var_Subst(NULL, tmp, VAR_GLOBAL, FALSE, TRUE);
 	    setenv(name, val, 1);
 	    free(val);
 	}
@@ -701,7 +701,7 @@ Var_ExportVars(void)
 	int ac;
 	int i;
 
-	val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+	val = Var_Subst(NULL, tmp, VAR_GLOBAL, FALSE, TRUE);
 	av = brk_string(val, &ac, FALSE, &as);
 	for (i = 0; i < ac; i++) {
 	    Var_Export1(av[i], 0);
@@ -739,7 +739,7 @@ Var_Export(char *str, int isExport)
     } else {
 	track = VAR_EXPORT_PARENT;
     }
-    val = Var_Subst(NULL, str, VAR_GLOBAL, 0);
+    val = Var_Subst(NULL, str, VAR_GLOBAL, FALSE, TRUE);
     av = brk_string(val, &ac, FALSE, &as);
     for (i = 0; i < ac; i++) {
 	name = av[i];
@@ -827,7 +827,7 @@ Var_UnExport(char *str)
 	/* Using .MAKE.EXPORTED */
 	n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
 	if (n < (int)sizeof(tmp)) {
-	    vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+	    vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, FALSE, TRUE);
 	}
     }
     if (vlist) {
@@ -857,7 +857,7 @@ Var_UnExport(char *str)
 		n = snprintf(tmp, sizeof(tmp),
 			     "${" MAKE_EXPORTED ":N%s}", v->name);
 		if (n < (int)sizeof(tmp)) {
-		    cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+		    cp = Var_Subst(NULL, tmp, VAR_GLOBAL, FALSE, TRUE);
 		    Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0);
 		    free(cp);
 		}
@@ -912,7 +912,7 @@ Var_Set(const char *name, const char *va
      * point in searching them all just to save a bit of memory...
      */
     if (strchr(name, '$') != NULL) {
-	expanded_name = Var_Subst(NULL, name, ctxt, 0);
+	expanded_name = Var_Subst(NULL, name, ctxt, FALSE, TRUE);
 	if (expanded_name[0] == 0) {
 	    if (DEBUG(VAR)) {
 		fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
@@ -1022,7 +1022,7 @@ Var_Append(const char *name, const char 
     char *expanded_name = NULL;
 
     if (strchr(name, '$') != NULL) {
-	expanded_name = Var_Subst(NULL, name, ctxt, 0);
+	expanded_name = Var_Subst(NULL, name, ctxt, FALSE, TRUE);
 	if (expanded_name[0] == 0) {
 	    if (DEBUG(VAR)) {
 		fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
@@ -1087,7 +1087,7 @@ Var_Exists(const char *name, GNode *ctxt
     char          *cp;
 
     if ((cp = strchr(name, '$')) != NULL) {
-	cp = Var_Subst(NULL, name, ctxt, FALSE);
+	cp = Var_Subst(NULL, name, ctxt, FALSE, TRUE);
     }
     v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
     free(cp);
@@ -1385,7 +1385,7 @@ VarSYSVMatch(GNode *ctx, Var_Parse_State
     addSpace = TRUE;
 
     if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) {
-        varexp = Var_Subst(NULL, pat->rhs, ctx, 0);
+        varexp = Var_Subst(NULL, pat->rhs, ctx, FALSE, TRUE);
 	Str_SYSVSubst(buf, varexp, ptr, len);
 	free(varexp);
     } else {
@@ -1805,7 +1805,7 @@ VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSE
 
     if (word && *word) {
         Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
-        s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum);
+        s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum, TRUE);
         if (s != NULL && *s != '\0') {
             if (addSpace && *s != '\n')
                 Buf_AddByte(buf, ' ');
@@ -2186,7 +2186,7 @@ VarGetPattern(GNode *ctxt, Var_Parse_Sta
 		     * delimiter, assume it's a variable
 		     * substitution and recurse.
 		     */
-		    cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt);
+		    cp2 = Var_Parse(cp, ctxt, errnum, TRUE, &len, &freeIt);
 		    Buf_AddBytes(&buf, strlen(cp2), cp2);
 		    free(freeIt);
 		    cp += len - 1;
@@ -2459,7 +2459,7 @@ VarStrftime(const char *fmt, int zulu)
 static char *
 ApplyModifiers(char *nstr, const char *tstr,
 	       int startc, int endc,
-	       Var *v, GNode *ctxt, Boolean errnum,
+	       Var *v, GNode *ctxt, Boolean errnum, Boolean wantit,
 	       int *lengthPtr, void **freePtr)
 {
     const char 	   *start;
@@ -2490,7 +2490,7 @@ ApplyModifiers(char *nstr, const char *t
 	    int rlen;
 	    int c;
 
-	    rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt);
+	    rval = Var_Parse(tstr, ctxt, errnum, wantit, &rlen, &freeIt);
 
 	    /*
 	     * If we have not parsed up to endc or ':',
@@ -2516,7 +2516,7 @@ ApplyModifiers(char *nstr, const char *t
 
 		nstr = ApplyModifiers(nstr, rval,
 				      0, 0,
-				      v, ctxt, errnum, &used, freePtr);
+				      v, ctxt, errnum, wantit, &used, freePtr);
 		if (nstr == var_Error
 		    || (nstr == varNoError && errnum == 0)
 		    || strlen(rval) != (size_t) used) {
@@ -2553,6 +2553,7 @@ ApplyModifiers(char *nstr, const char *t
 		    char *sv_name;
 		    VarPattern	pattern;
 		    int	how;
+		    int flags;
 
 		    if (v->name[0] == 0)
 			goto bad_modifier;
@@ -2588,8 +2589,9 @@ ApplyModifiers(char *nstr, const char *t
 		    delim = startc == PROPEN ? PRCLOSE : BRCLOSE;
 		    pattern.flags = 0;
 
+		    flags = (wantit) ? 0 : VAR_NOSUBST;
 		    pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum,
-						&cp, delim, NULL,
+						&cp, delim, &flags,
 						&pattern.rightLen,
 						NULL);
 		    if (v->flags & VAR_JUNK) {
@@ -2603,25 +2605,27 @@ ApplyModifiers(char *nstr, const char *t
 		    termc = *--cp;
 		    delim = '\0';
 
-		    switch (how) {
-		    case '+':
-			Var_Append(v->name, pattern.rhs, v_ctxt);
-			break;
-		    case '!':
-			newStr = Cmd_Exec(pattern.rhs, &emsg);
-			if (emsg)
-			    Error(emsg, nstr);
-			else
-			    Var_Set(v->name, newStr,  v_ctxt, 0);
-			free(newStr);
-			break;
-		    case '?':
-			if ((v->flags & VAR_JUNK) == 0)
+		    if (wantit) {
+			switch (how) {
+			case '+':
+			    Var_Append(v->name, pattern.rhs, v_ctxt);
 			    break;
-			/* FALLTHROUGH */
-		    default:
-			Var_Set(v->name, pattern.rhs, v_ctxt, 0);
-			break;
+			case '!':
+			    newStr = Cmd_Exec(pattern.rhs, &emsg);
+			    if (emsg)
+				Error(emsg, nstr);
+			    else
+				Var_Set(v->name, newStr,  v_ctxt, 0);
+			    free(newStr);
+			    break;
+			case '?':
+			    if ((v->flags & VAR_JUNK) == 0)
+				break;
+			    /* FALLTHROUGH */
+			default:
+			    Var_Set(v->name, pattern.rhs, v_ctxt, 0);
+			    break;
+			}
 		    }
 		    free(UNCONST(pattern.rhs));
 		    newStr = varNoError;
@@ -2663,8 +2667,15 @@ ApplyModifiers(char *nstr, const char *t
 	case 'U':
 	    {
 		Buffer  buf;    	/* Buffer for patterns */
-		int	    wantit;	/* want data in buffer */
+		int	    wantit_;	/* want data in buffer */
 
+		if (wantit) {
+		    if (*tstr == 'U')
+			wantit_ = ((v->flags & VAR_JUNK) != 0);
+		    else
+			wantit_ = ((v->flags & VAR_JUNK) == 0);
+		} else
+		    wantit_ = wantit;
 		/*
 		 * Pass through tstr looking for 1) escaped delimiters,
 		 * '$'s and backslashes (place the escaped character in
@@ -2693,7 +2704,7 @@ ApplyModifiers(char *nstr, const char *t
 			    int	    len;
 			    void    *freeIt;
 
-			    cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt);
+			    cp2 = Var_Parse(cp, ctxt, errnum, wantit_, &len, &freeIt);
 			    Buf_AddBytes(&buf, strlen(cp2), cp2);
 			    free(freeIt);
 			    cp += len - 1;
@@ -2704,13 +2715,9 @@ ApplyModifiers(char *nstr, const char *t
 
 		termc = *cp;
 
-		if (*tstr == 'U')
-		    wantit = ((v->flags & VAR_JUNK) != 0);
-		else
-		    wantit = ((v->flags & VAR_JUNK) == 0);
 		if ((v->flags & VAR_JUNK) != 0)
 		    v->flags |= VAR_KEEP;
-		if (wantit) {
+		if (wantit_) {
 		    newStr = Buf_Destroy(&buf, FALSE);
 		} else {
 		    newStr = nstr;
@@ -2762,7 +2769,10 @@ ApplyModifiers(char *nstr, const char *t
 						 NULL, &pattern.rightLen,
 						 NULL)) == NULL)
 		    goto cleanup;
-		newStr = Cmd_Exec(pattern.rhs, &emsg);
+		if (wantit)
+		    newStr = Cmd_Exec(pattern.rhs, &emsg);
+		else
+		    newStr = varNoError;
 		free(UNCONST(pattern.rhs));
 		if (emsg)
 		    Error(emsg, nstr);
@@ -3138,7 +3148,7 @@ ApplyModifiers(char *nstr, const char *t
 		     * expand it.
 		     */
 		    cp2 = pattern;
-		    pattern = Var_Subst(NULL, cp2, ctxt, errnum);
+		    pattern = Var_Subst(NULL, cp2, ctxt, errnum, TRUE);
 		    free(cp2);
 		}
 		if (DEBUG(VAR))
@@ -3224,15 +3234,27 @@ ApplyModifiers(char *nstr, const char *t
 	    {
 		VarPattern 	pattern;
 		Boolean	value;
-
+		int cond_rc;
+		int lhs_flags, rhs_flags;
+		
 		/* find ':', and then substitute accordingly */
+		cond_rc = Cond_EvalExpression(NULL, v->name, &value, 0, FALSE);
+		if (cond_rc == COND_INVALID) {
+		    lhs_flags = rhs_flags = VAR_NOSUBST;
+		} else if (value) {
+		    lhs_flags = 0;
+		    rhs_flags = VAR_NOSUBST;
+		} else {
+		    lhs_flags = VAR_NOSUBST;
+		    rhs_flags = 0;
+		}
 
 		pattern.flags = 0;
 
 		cp = ++tstr;
 		delim = ':';
 		if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum,
-						 &cp, delim, NULL,
+						 &cp, delim, &lhs_flags,
 						 &pattern.leftLen,
 						 NULL)) == NULL)
 		    goto cleanup;
@@ -3240,15 +3262,14 @@ ApplyModifiers(char *nstr, const char *t
 		/* BROPEN or PROPEN */
 		delim = endc;
 		if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum,
-						 &cp, delim, NULL,
+						 &cp, delim, &rhs_flags,
 						 &pattern.rightLen,
 						 NULL)) == NULL)
 		    goto cleanup;
 
 		termc = *--cp;
 		delim = '\0';
-		if (Cond_EvalExpression(NULL, v->name, &value, 0, FALSE)
-		    == COND_INVALID) {
+		if (cond_rc == COND_INVALID) {
 		    Error("Bad conditional expression `%s' in %s?%s:%s",
 			  v->name, v->name, pattern.lhs, pattern.rhs);
 		    goto cleanup;
@@ -3410,9 +3431,12 @@ ApplyModifiers(char *nstr, const char *t
 	case 's':
 	    if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
 		const char *emsg;
-		newStr = Cmd_Exec(nstr, &emsg);
-		if (emsg)
-		    Error(emsg, nstr);
+		if (wantit) {
+		    newStr = Cmd_Exec(nstr, &emsg);
+		    if (emsg)
+			Error(emsg, nstr);
+		} else
+		    newStr = varNoError;
 		cp = tstr + 2;
 		termc = *cp;
 		break;
@@ -3549,6 +3573,7 @@ ApplyModifiers(char *nstr, const char *t
  *	str		The string to parse
  *	ctxt		The context for the variable
  *	errnum		TRUE if undefined variables are an error
+ *	wantit		TRUE if we actually want the result
  *	lengthPtr	OUT: The length of the specification
  *	freePtr		OUT: Non-NULL if caller should free *freePtr
  *
@@ -3567,8 +3592,9 @@ ApplyModifiers(char *nstr, const char *t
  */
 /* coverity[+alloc : arg-*4] */
 char *
-Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr,
-	  void **freePtr)
+Var_Parse(const char *str, GNode *ctxt,
+	   Boolean errnum, Boolean wantit,
+	   int *lengthPtr, void **freePtr)
 {
     const char	   *tstr;    	/* Pointer into str */
     Var		   *v;		/* Variable in invocation */
@@ -3673,7 +3699,7 @@ Var_Parse(const char *str, GNode *ctxt, 
 	    if (*tstr == '$') {
 		int rlen;
 		void *freeIt;
-		char *rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt);
+		char *rval = Var_Parse(tstr, ctxt, errnum, wantit, &rlen, &freeIt);
 		if (rval != NULL) {
 		    Buf_AddBytes(&buf, strlen(rval), rval);
 		}
@@ -3820,7 +3846,7 @@ Var_Parse(const char *str, GNode *ctxt, 
      */
     nstr = Buf_GetAll(&v->val, NULL);
     if (strchr(nstr, '$') != NULL) {
-	nstr = Var_Subst(NULL, nstr, ctxt, errnum);
+	nstr = Var_Subst(NULL, nstr, ctxt, errnum, wantit);
 	*freePtr = nstr;
     }
 
@@ -3833,7 +3859,7 @@ Var_Parse(const char *str, GNode *ctxt, 
 	extraFree = NULL;
 	if (extramodifiers != NULL) {
 		nstr = ApplyModifiers(nstr, extramodifiers, '(', ')',
-				      v, ctxt, errnum, &used, &extraFree);
+				      v, ctxt, errnum, wantit, &used, &extraFree);
 	}
 
 	if (haveModifier) {
@@ -3841,7 +3867,7 @@ Var_Parse(const char *str, GNode *ctxt, 
 		tstr++;
 
 		nstr = ApplyModifiers(nstr, tstr, startc, endc,
-				      v, ctxt, errnum, &used, freePtr);
+				      v, ctxt, errnum, wantit, &used, freePtr);
 		tstr += used;
 		free(extraFree);
 	} else {
@@ -3905,6 +3931,7 @@ Var_Parse(const char *str, GNode *ctxt, 
  *	str		the string which to substitute
  *	ctxt		the context wherein to find variables
  *	undefErr	TRUE if undefineds are an error
+ *	wantit		TRUE if we actually want the result
  *
  * Results:
  *	The resulting string.
@@ -3914,7 +3941,8 @@ Var_Parse(const char *str, GNode *ctxt, 
  *-----------------------------------------------------------------------
  */
 char *
-Var_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr)
+Var_Subst(const char *var, const char *str, GNode *ctxt,
+	   Boolean undefErr, Boolean wantit)
 {
     Buffer  	  buf;		    /* Buffer for forming things */
     char    	  *val;		    /* Value to substitute for a variable */
@@ -4012,7 +4040,7 @@ Var_Subst(const char *var, const char *s
 		    continue;
 	    }
 
-	    val = Var_Parse(str, ctxt, undefErr, &length, &freeIt);
+	    val = Var_Parse(str, ctxt, undefErr, wantit, &length, &freeIt);
 
 	    /*
 	     * When we come down here, val should either point to the

Index: src/usr.bin/make/unit-tests/varmisc.exp
diff -u src/usr.bin/make/unit-tests/varmisc.exp:1.1 src/usr.bin/make/unit-tests/varmisc.exp:1.2
--- src/usr.bin/make/unit-tests/varmisc.exp:1.1	Thu Aug 21 15:37:13 2014
+++ src/usr.bin/make/unit-tests/varmisc.exp	Sun Oct 11 04:51:24 2015
@@ -1,2 +1,14 @@
 
+:D expanded when var set
+true,TRUE
+:U expanded when var undef
+true,TRUE
+:D skipped if var undef
+
+:U skipped when var set
+is set
+:? only lhs when value true
+true,TRUE
+:? only rhs when value false
+false,FALSE
 exit status 0

Index: src/usr.bin/make/unit-tests/varmisc.mk
diff -u src/usr.bin/make/unit-tests/varmisc.mk:1.2 src/usr.bin/make/unit-tests/varmisc.mk:1.3
--- src/usr.bin/make/unit-tests/varmisc.mk:1.2	Sat Aug 30 22:21:08 2014
+++ src/usr.bin/make/unit-tests/varmisc.mk	Sun Oct 11 04:51:24 2015
@@ -1,8 +1,38 @@
-# $Id: varmisc.mk,v 1.2 2014/08/30 22:21:08 sjg Exp $
+# $Id: varmisc.mk,v 1.3 2015/10/11 04:51:24 sjg Exp $
 #
 # Miscellaneous variable tests.
 
-all: unmatched_var_paren
+all: unmatched_var_paren D_true U_true D_false U_false Q_lhs Q_rhs
 
 unmatched_var_paren:
 	@echo ${foo::=foo-text}
+
+True = ${echo -n true,>&2:L:sh}TRUE
+False= ${echo -n false,>&2:L:sh}FALSE
+
+VSET= is set
+.undef UNDEF
+
+U_false:
+	@echo :U skipped when var set
+	@echo ${VSET:U${False}}
+
+D_false:
+	@echo :D skipped if var undef
+	@echo ${UNDEF:D${False}}
+
+U_true:
+	@echo :U expanded when var undef
+	@echo ${UNDEF:U${True}}
+
+D_true:
+	@echo :D expanded when var set
+	@echo ${VSET:D${True}}
+
+Q_lhs:
+	@echo :? only lhs when value true
+	@echo ${1:L:?${True}:${False}}
+
+Q_rhs:
+	@echo :? only rhs when value false
+	@echo ${0:L:?${True}:${False}}

Reply via email to