Module Name:    src
Committed By:   rillig
Date:           Sat Jan 15 18:34:41 UTC 2022

Modified Files:
        src/usr.bin/make: arch.c main.c meta.c nonints.h parse.c var.c

Log Message:
make: merge duplicate code for expanding variable expressions

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.209 -r1.210 src/usr.bin/make/arch.c
cvs rdiff -u -r1.569 -r1.570 src/usr.bin/make/main.c
cvs rdiff -u -r1.189 -r1.190 src/usr.bin/make/meta.c
cvs rdiff -u -r1.237 -r1.238 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.649 -r1.650 src/usr.bin/make/parse.c
cvs rdiff -u -r1.1000 -r1.1001 src/usr.bin/make/var.c

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.209 src/usr.bin/make/arch.c:1.210
--- src/usr.bin/make/arch.c:1.209	Wed Dec 15 12:58:01 2021
+++ src/usr.bin/make/arch.c	Sat Jan 15 18:34:41 2022
@@ -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 @@ Arch_ParseArchive(char **pp, GNodeList *
 	}
 
 	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 @@ Arch_ParseArchive(char **pp, GNodeList *
 		 */
 		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

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.569 src/usr.bin/make/main.c:1.570
--- src/usr.bin/make/main.c:1.569	Mon Jan 10 20:32:28 2022
+++ src/usr.bin/make/main.c	Sat Jan 15 18:34:41 2022
@@ -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 @@ SetVarObjdir(bool writable, const char *
 		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);
 

Index: src/usr.bin/make/meta.c
diff -u src/usr.bin/make/meta.c:1.189 src/usr.bin/make/meta.c:1.190
--- src/usr.bin/make/meta.c:1.189	Sat Jan 15 09:08:57 2022
+++ src/usr.bin/make/meta.c	Sat Jan 15 18:34:41 2022
@@ -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 @@ is_submake(const char *cmd, GNode *gn)
     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 @@ printCMD(const char *ucmd, FILE *fp, GNo
 {
     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);
 }

Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.237 src/usr.bin/make/nonints.h:1.238
--- src/usr.bin/make/nonints.h:1.237	Sun Jan  9 18:49:28 2022
+++ src/usr.bin/make/nonints.h	Sat Jan 15 18:34:41 2022
@@ -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 @@ FStr Var_Value(GNode *, const char *) MA
 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);

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.649 src/usr.bin/make/parse.c:1.650
--- src/usr.bin/make/parse.c:1.649	Sun Jan  9 19:57:14 2022
+++ src/usr.bin/make/parse.c	Sat Jan 15 18:34:41 2022
@@ -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 @@ VarAssign_EvalShell(const char *name, co
 	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 @@ ParseInclude(char *directive)
 
 	*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);
 }

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1000 src/usr.bin/make/var.c:1.1001
--- src/usr.bin/make/var.c:1.1000	Sun Jan  9 18:49:28 2022
+++ src/usr.bin/make/var.c	Sat Jan 15 18:34:41 2022
@@ -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 @@ Var_DeleteExpand(GNode *scope, const cha
 {
 	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 @@ Var_SetExpandWithFlags(GNode *scope, con
 
 	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 @@ Var_AppendExpand(GNode *scope, const cha
 
 	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 @@ Var_ExistsExpand(GNode *scope, const cha
 	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 @@ ModifyWord_SysVSubst(Substring word, Sep
 {
 	const struct ModifyWord_SysVSubstArgs *args = data;
 	FStr rhs;
-	char *rhsExp;
 	const char *percent;
 
 	if (Substring_IsEmpty(word))
@@ -1491,11 +1464,7 @@ ModifyWord_SysVSubst(Substring word, Sep
 		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);
-	}
+	Var_Expand(&rhs, args->scope, VARE_WANTRES);
 
 	percent = args->lhsPercent ? strchr(rhs.str, '%') : NULL;
 
@@ -4775,6 +4744,19 @@ Var_Subst(const char *str, GNode *scope,
 	return VPR_OK;
 }
 
+void
+Var_Expand(FStr *str, GNode *scope, VarEvalMode emode)
+{
+	char *expanded;
+
+	if (strchr(str->str, '$') == NULL)
+		return;
+	(void)Var_Subst(str->str, scope, emode, &expanded);
+	/* TODO: handle errors */
+	FStr_Done(str);
+	*str = FStr_InitOwn(expanded);
+}
+
 /* Initialize the variables module. */
 void
 Var_Init(void)

Reply via email to