make: merge duplicate code for expanding variable expressions

No functional change.
This commit is contained in:
rillig 2022-01-15 18:34:41 +00:00
parent 03f9182e94
commit bbf0adc973
6 changed files with 46 additions and 96 deletions

View File

@ -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 *gns, GNode *scope)
}
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 *gns, GNode *scope)
*/
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

View File

@ -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 *var, const char *suffix)
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);

View File

@ -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, GNode *gn)
{
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);
}

View File

@ -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 *) MAKE_ATTR_USE;
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);

View File

@ -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, const char *uvalue, GNode *scope,
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);
}

View File

@ -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 char *name)
{
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, const char *name, const char *val,
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 char *name, const char *val)
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 char *name)
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, SepBuf *buf, void *data)
{
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, SepBuf *buf, void *data)
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, VarEvalMode emode, char **out_res)
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)