#include <sys/stat.h>
#include <sys/types.h>
#include <regex.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <time.h>
#include "make.h"
#include "dir.h"
#include "job.h"
#include "metachar.h"
MAKE_RCSID("$NetBSD: var.c,v 1.1182 2026/07/13 19:23:55 rillig Exp $");
typedef struct Var {
FStr name;
Buffer val;
bool fromCmd:1;
bool shortLived:1;
bool fromEnvironment:1;
bool readOnly:1;
bool readOnlyLoud:1;
bool inUse:1;
bool exported:1;
bool reexport:1;
} Var;
typedef enum VarExportedMode {
VAR_EXPORTED_NONE,
VAR_EXPORTED_SOME,
VAR_EXPORTED_ALL
} VarExportedMode;
typedef enum UnexportWhat {
UNEXPORT_NAMED,
UNEXPORT_ALL,
UNEXPORT_ENV
} UnexportWhat;
typedef struct PatternFlags {
bool subGlobal:1;
bool subOnce:1;
bool anchorStart:1;
bool anchorEnd:1;
} PatternFlags;
typedef struct SepBuf {
Buffer buf;
bool needSep;
char sep;
} SepBuf;
typedef enum {
VSK_MAKEFLAGS,
VSK_TARGET,
VSK_COMMAND,
VSK_VARNAME,
VSK_INDIRECT_MODIFIERS,
VSK_COND,
VSK_COND_THEN,
VSK_COND_ELSE,
VSK_EXPR,
VSK_EXPR_PARSE
} EvalStackElementKind;
typedef struct {
EvalStackElementKind kind;
const char *str;
const FStr *value;
} EvalStackElement;
typedef struct {
EvalStackElement *elems;
size_t len;
size_t cap;
} EvalStack;
char **savedEnv = NULL;
char var_Error[] = "";
static char varUndefined[] = "";
static bool save_dollars = true;
GNode *SCOPE_CMDLINE;
GNode *SCOPE_GLOBAL;
GNode *SCOPE_INTERNAL;
static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
static const char VarEvalMode_Name[][32] = {
"parse",
"parse-balanced",
"eval",
"eval-defined-loud",
"eval-defined",
"eval-keep-undefined",
"eval-keep-dollar-and-undefined",
};
static EvalStack evalStack;
static void
EvalStack_Push(EvalStackElementKind kind, const char *str, const FStr *value)
{
if (evalStack.len >= evalStack.cap) {
evalStack.cap = 16 + 2 * evalStack.cap;
evalStack.elems = bmake_realloc(evalStack.elems,
evalStack.cap * sizeof(*evalStack.elems));
}
evalStack.elems[evalStack.len].kind = kind;
evalStack.elems[evalStack.len].str = str;
evalStack.elems[evalStack.len].value = value;
evalStack.len++;
}
void
EvalStack_PushMakeflags(const char *makeflags)
{
EvalStack_Push(VSK_MAKEFLAGS, makeflags, NULL);
}
void
EvalStack_Pop(void)
{
assert(evalStack.len > 0);
evalStack.len--;
}
bool
EvalStack_Details(Buffer *buf)
{
size_t i;
for (i = evalStack.len; i > 0; i--) {
static const char descr[][42] = {
"while evaluating MAKEFLAGS",
"in target",
"in command",
"while evaluating variable",
"while evaluating indirect modifiers",
"while evaluating condition",
"while evaluating then-branch of condition",
"while evaluating else-branch of condition",
"while evaluating",
"while parsing",
};
EvalStackElement *elem = evalStack.elems + i - 1;
EvalStackElementKind kind = elem->kind;
const char* value = elem->value != NULL
&& (kind == VSK_VARNAME || kind == VSK_EXPR)
? elem->value->str : NULL;
const GNode *gn;
Buf_AddStr(buf, "\t");
Buf_AddStr(buf, descr[kind]);
Buf_AddStr(buf, " \"");
Buf_AddStr(buf, elem->str);
if (value != NULL) {
Buf_AddStr(buf, "\" with value \"");
Buf_AddStr(buf, value);
}
if (kind == VSK_TARGET
&& (gn = Targ_FindNode(elem->str)) != NULL
&& gn->fname != NULL) {
Buf_AddStr(buf, "\" from ");
Buf_AddStr(buf, gn->fname);
Buf_AddStr(buf, ":");
Buf_AddInt(buf, (int)gn->lineno);
Buf_AddStr(buf, "\n");
} else
Buf_AddStr(buf, "\"\n");
}
return evalStack.len > 0;
}
static Var *
VarNew(FStr name, const char *value,
bool shortLived, bool fromEnvironment, bool readOnly)
{
size_t value_len = strlen(value);
Var *var = bmake_malloc(sizeof *var);
var->name = name;
Buf_InitSize(&var->val, value_len + 1);
Buf_AddBytes(&var->val, value, value_len);
var->fromCmd = false;
var->shortLived = shortLived;
var->fromEnvironment = fromEnvironment;
var->readOnly = readOnly;
var->readOnlyLoud = false;
var->inUse = false;
var->exported = false;
var->reexport = false;
return var;
}
static Substring
CanonicalVarname(Substring name)
{
if (Substring_Equals(name, "^"))
return Substring_InitStr(ALLSRC);
if (!(Substring_Length(name) > 0 && name.start[0] == '.'))
return name;
if (Substring_Equals(name, ".ALLSRC"))
return Substring_InitStr(ALLSRC);
if (Substring_Equals(name, ".ARCHIVE"))
return Substring_InitStr(ARCHIVE);
if (Substring_Equals(name, ".IMPSRC"))
return Substring_InitStr(IMPSRC);
if (Substring_Equals(name, ".MEMBER"))
return Substring_InitStr(MEMBER);
if (Substring_Equals(name, ".OODATE"))
return Substring_InitStr(OODATE);
if (Substring_Equals(name, ".PREFIX"))
return Substring_InitStr(PREFIX);
if (Substring_Equals(name, ".TARGET"))
return Substring_InitStr(TARGET);
if (Substring_Equals(name, ".SHELL") && shellPath == NULL)
Shell_Init();
return name;
}
static Var *
GNode_FindVar(GNode *scope, Substring varname, unsigned hash)
{
return HashTable_FindValueBySubstringHash(&scope->vars, varname, hash);
}
static Var *
VarFindSubstring(Substring name, GNode *scope, bool elsewhere)
{
Var *var;
unsigned nameHash;
name = CanonicalVarname(name);
nameHash = Hash_Substring(name);
var = GNode_FindVar(scope, name, nameHash);
if (!elsewhere)
return var;
if (var == NULL && scope != SCOPE_CMDLINE)
var = GNode_FindVar(SCOPE_CMDLINE, name, nameHash);
if (!opts.checkEnvFirst && var == NULL && scope != SCOPE_GLOBAL) {
var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
if (var == NULL && scope != SCOPE_INTERNAL) {
var = GNode_FindVar(SCOPE_INTERNAL, name, nameHash);
}
}
if (var == NULL) {
FStr envName = Substring_Str(name);
const char *envValue = getenv(envName.str);
if (envValue != NULL)
return VarNew(envName, envValue, true, true, false);
FStr_Done(&envName);
if (opts.checkEnvFirst && scope != SCOPE_GLOBAL) {
var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
if (var == NULL && scope != SCOPE_INTERNAL)
var = GNode_FindVar(SCOPE_INTERNAL, name,
nameHash);
return var;
}
return NULL;
}
return var;
}
static Var *
VarFind(const char *name, GNode *scope, bool elsewhere)
{
return VarFindSubstring(Substring_InitStr(name), scope, elsewhere);
}
static void
VarFreeShortLived(Var *v)
{
if (!v->shortLived)
return;
FStr_Done(&v->name);
Buf_Done(&v->val);
free(v);
}
static const char *
ValueDescription(const char *value)
{
if (value[0] == '\0')
return "# (empty)";
if (ch_isspace(value[strlen(value) - 1]))
return "# (ends with space)";
return "";
}
static Var *
VarAdd(const char *name, const char *value, GNode *scope, VarSetFlags flags)
{
HashEntry *he = HashTable_CreateEntry(&scope->vars, name, NULL);
Var *v = VarNew(FStr_InitRefer( he->key), value,
false, false, (flags & VAR_SET_READONLY) != 0);
HashEntry_Set(he, v);
DEBUG4(VAR, "%s: %s = %s%s\n",
scope->name, name, value, ValueDescription(value));
return v;
}
void
Var_Delete(GNode *scope, const char *varname)
{
HashEntry *he = HashTable_FindEntry(&scope->vars, varname);
Var *v;
if (he == NULL) {
DEBUG2(VAR, "%s: ignoring delete '%s' as it is not found\n",
scope->name, varname);
return;
}
v = he->value;
if (v->readOnlyLoud) {
Parse_Error(PARSE_FATAL,
"Cannot delete \"%s\" as it is read-only",
v->name.str);
return;
}
if (v->readOnly) {
DEBUG2(VAR, "%s: ignoring delete '%s' as it is read-only\n",
scope->name, varname);
return;
}
if (v->inUse) {
Parse_Error(PARSE_FATAL,
"Cannot delete variable \"%s\" while it is used",
v->name.str);
return;
}
DEBUG2(VAR, "%s: delete %s\n", scope->name, varname);
if (v->exported)
unsetenv(v->name.str);
if (strcmp(v->name.str, ".MAKE.EXPORTED") == 0)
var_exportedVars = VAR_EXPORTED_NONE;
assert(v->name.freeIt == NULL);
HashTable_DeleteEntry(&scope->vars, he);
Buf_Done(&v->val);
free(v);
}
#ifdef CLEANUP
void
Var_DeleteAll(GNode *scope)
{
HashIter hi;
HashIter_Init(&hi, &scope->vars);
while (HashIter_Next(&hi)) {
Var *v = hi.entry->value;
Buf_Done(&v->val);
free(v);
}
}
#endif
void
Var_Undef(const char *arg)
{
char *expanded;
Words varnames;
size_t i;
if (arg[0] == '\0') {
Parse_Error(PARSE_FATAL,
"The .undef directive requires an argument");
return;
}
expanded = Var_Subst(arg, SCOPE_GLOBAL, VARE_EVAL);
if (expanded == var_Error) {
Parse_Error(PARSE_FATAL,
"Error in variable names to be undefined");
return;
}
varnames = Str_Words(expanded, false);
if (varnames.len == 1 && varnames.words[0][0] == '\0')
varnames.len = 0;
for (i = 0; i < varnames.len; i++) {
const char *varname = varnames.words[i];
Global_Delete(varname);
}
Words_Free(varnames);
free(expanded);
}
static bool
MayExport(const char *name)
{
if (name[0] == '.')
return false;
if (name[0] == '-')
return false;
if (name[1] == '\0') {
switch (name[0]) {
case '@':
case '%':
case '*':
case '!':
return false;
}
}
return true;
}
static bool
ExportVarEnv(Var *v, GNode *scope)
{
const char *name = v->name.str;
char *val = v->val.data;
char *expr;
if (v->exported && !v->reexport)
return false;
if (strchr(val, '$') == NULL) {
if (!v->exported)
setenv(name, val, 1);
return true;
}
if (v->inUse)
return false;
expr = str_concat3("${", name, "}");
val = Var_Subst(expr, scope, VARE_EVAL);
if (scope != SCOPE_GLOBAL) {
v = VarFind(name, SCOPE_GLOBAL, false);
if (v != NULL)
v->exported = false;
}
setenv(name, val, 1);
free(val);
free(expr);
return true;
}
static bool
ExportVarPlain(Var *v)
{
if (strchr(v->val.data, '$') == NULL) {
setenv(v->name.str, v->val.data, 1);
v->exported = true;
v->reexport = false;
return true;
}
v->exported = true;
v->reexport = true;
return true;
}
static bool
ExportVarLiteral(Var *v)
{
if (v->exported && !v->reexport)
return false;
if (!v->exported)
setenv(v->name.str, v->val.data, 1);
return true;
}
static bool
ExportVar(const char *name, GNode *scope, VarExportMode mode)
{
Var *v;
if (!MayExport(name))
return false;
v = VarFind(name, scope, false);
if (v == NULL && scope != SCOPE_GLOBAL)
v = VarFind(name, SCOPE_GLOBAL, false);
if (v == NULL)
return false;
if (mode == VEM_ENV)
return ExportVarEnv(v, scope);
else if (mode == VEM_PLAIN)
return ExportVarPlain(v);
else
return ExportVarLiteral(v);
}
void
Var_ReexportVars(GNode *scope)
{
char *xvarnames;
char level_buf[21];
snprintf(level_buf, sizeof level_buf, "%d", makelevel + 1);
setenv(MAKE_LEVEL_ENV, level_buf, 1);
if (var_exportedVars == VAR_EXPORTED_NONE)
return;
if (var_exportedVars == VAR_EXPORTED_ALL) {
HashIter hi;
HashIter_Init(&hi, &SCOPE_GLOBAL->vars);
while (HashIter_Next(&hi)) {
Var *var = hi.entry->value;
ExportVar(var->name.str, scope, VEM_ENV);
}
return;
}
xvarnames = Var_Subst("${.MAKE.EXPORTED:O:u}", SCOPE_GLOBAL,
VARE_EVAL);
if (xvarnames[0] != '\0') {
Words varnames = Str_Words(xvarnames, false);
size_t i;
for (i = 0; i < varnames.len; i++)
ExportVar(varnames.words[i], scope, VEM_ENV);
Words_Free(varnames);
}
free(xvarnames);
}
static void
ExportVars(const char *varnames, bool isExport, VarExportMode mode)
{
Words words = Str_Words(varnames, false);
size_t i;
if (words.len == 1 && words.words[0][0] == '\0')
words.len = 0;
for (i = 0; i < words.len; i++) {
const char *varname = words.words[i];
if (!ExportVar(varname, SCOPE_GLOBAL, mode))
continue;
if (var_exportedVars == VAR_EXPORTED_NONE)
var_exportedVars = VAR_EXPORTED_SOME;
if (isExport && mode == VEM_PLAIN)
Global_Append(".MAKE.EXPORTED", varname);
}
Words_Free(words);
}
static void
ExportVarsExpand(const char *uvarnames, bool isExport, VarExportMode mode)
{
char *xvarnames = Var_Subst(uvarnames, SCOPE_GLOBAL, VARE_EVAL);
ExportVars(xvarnames, isExport, mode);
free(xvarnames);
}
void
Var_Export(VarExportMode mode, const char *varnames)
{
if (mode == VEM_ALL) {
var_exportedVars = VAR_EXPORTED_ALL;
return;
} else if (mode == VEM_PLAIN && varnames[0] == '\0') {
Parse_Error(PARSE_WARNING, ".export requires an argument.");
return;
}
ExportVarsExpand(varnames, true, mode);
}
void
Var_ExportVars(const char *varnames)
{
ExportVarsExpand(varnames, false, VEM_PLAIN);
}
static void
ClearEnv(void)
{
const char *level;
char **newenv;
level = getenv(MAKE_LEVEL_ENV);
if (environ == savedEnv) {
newenv = bmake_realloc(environ, 2 * sizeof(char *));
} else {
if (savedEnv != NULL) {
free(savedEnv);
savedEnv = NULL;
}
newenv = bmake_malloc(2 * sizeof(char *));
}
environ = savedEnv = newenv;
newenv[0] = NULL;
newenv[1] = NULL;
if (level != NULL && *level != '\0')
setenv(MAKE_LEVEL_ENV, level, 1);
}
static void
GetVarnamesToUnexport(bool isEnv, const char *arg,
FStr *out_varnames, UnexportWhat *out_what)
{
UnexportWhat what;
FStr varnames = FStr_InitRefer("");
if (isEnv) {
if (arg[0] != '\0') {
Parse_Error(PARSE_FATAL,
"The directive .unexport-env does not take "
"arguments");
}
what = UNEXPORT_ENV;
} else {
what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL;
if (what == UNEXPORT_NAMED)
varnames = FStr_InitRefer(arg);
}
if (what != UNEXPORT_NAMED) {
char *expanded = Var_Subst("${.MAKE.EXPORTED:O:u}",
SCOPE_GLOBAL, VARE_EVAL);
varnames = FStr_InitOwn(expanded);
}
*out_varnames = varnames;
*out_what = what;
}
static void
UnexportVar(Substring varname, UnexportWhat what)
{
Var *v = VarFindSubstring(varname, SCOPE_GLOBAL, false);
if (v == NULL) {
DEBUG2(VAR, "Not unexporting \"%.*s\" (not found)\n",
(int)Substring_Length(varname), varname.start);
return;
}
DEBUG2(VAR, "Unexporting \"%.*s\"\n",
(int)Substring_Length(varname), varname.start);
if (what != UNEXPORT_ENV && v->exported && !v->reexport)
unsetenv(v->name.str);
v->exported = false;
v->reexport = false;
if (what == UNEXPORT_NAMED) {
char *expr = str_concat3(
"${.MAKE.EXPORTED:N", v->name.str, "}");
char *filtered = Var_Subst(expr, SCOPE_GLOBAL, VARE_EVAL);
Global_Set(".MAKE.EXPORTED", filtered);
free(filtered);
free(expr);
}
}
static void
UnexportVars(const char *varnames, UnexportWhat what)
{
size_t i;
SubstringWords words;
if (what == UNEXPORT_ENV)
ClearEnv();
words = Substring_Words(varnames, false);
for (i = 0; i < words.len; i++)
UnexportVar(words.words[i], what);
SubstringWords_Free(words);
if (what != UNEXPORT_NAMED)
Global_Delete(".MAKE.EXPORTED");
}
void
Var_UnExport(bool isEnv, const char *arg)
{
UnexportWhat what;
FStr varnames;
GetVarnamesToUnexport(isEnv, arg, &varnames, &what);
UnexportVars(varnames.str, what);
FStr_Done(&varnames);
}
void
Var_SetWithFlags(GNode *scope, const char *name, const char *val,
VarSetFlags flags)
{
Var *v;
assert(val != NULL);
if (name[0] == '\0') {
DEBUG3(VAR,
"%s: ignoring '%s = %s' as the variable name is empty\n",
scope->name, name, val);
return;
}
if (scope == SCOPE_GLOBAL
&& VarFind(name, SCOPE_CMDLINE, false) != NULL) {
DEBUG3(VAR,
"%s: ignoring '%s = %s' "
"due to a command line variable of the same name\n",
scope->name, name, val);
return;
}
v = VarFind(name, scope, false);
if (v == NULL) {
if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) {
Var *gl = VarFind(name, SCOPE_GLOBAL, false);
if (gl != NULL && strcmp(gl->val.data, val) == 0) {
DEBUG3(VAR,
"%s: ignoring to override the global "
"'%s = %s' from a command line variable "
"as the value wouldn't change\n",
scope->name, name, val);
} else if (gl != NULL && gl->readOnlyLoud)
Parse_Error(PARSE_FATAL,
"Cannot override "
"read-only global variable \"%s\" "
"with a command line variable", name);
else
Var_Delete(SCOPE_GLOBAL, name);
}
if (strcmp(name, ".SUFFIXES") == 0) {
DEBUG3(VAR,
"%s: ignoring '%s = %s' as it is read-only\n",
scope->name, name, val);
return;
}
v = VarAdd(name, val, scope, flags);
} else {
if (v->readOnlyLoud) {
Parse_Error(PARSE_FATAL,
"Cannot overwrite \"%s\" as it is read-only",
name);
return;
}
if (v->readOnly && !(flags & VAR_SET_READONLY)) {
DEBUG3(VAR,
"%s: ignoring '%s = %s' as it is read-only\n",
scope->name, name, val);
return;
}
Buf_Clear(&v->val);
Buf_AddStr(&v->val, val);
DEBUG4(VAR, "%s: %s = %s%s\n",
scope->name, name, val, ValueDescription(val));
if (v->exported)
ExportVar(name, scope, VEM_PLAIN);
}
if (scope == SCOPE_CMDLINE) {
v->fromCmd = true;
if (!(flags & VAR_SET_NO_EXPORT)) {
if (!opts.varNoExportEnv && name[0] != '.')
setenv(name, val, 1);
if (!(flags & VAR_SET_INTERNAL))
Global_Append(".MAKEOVERRIDES", name);
}
}
if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0)
save_dollars = ParseBoolean(val, save_dollars);
if (v != NULL)
VarFreeShortLived(v);
}
void
Var_Set(GNode *scope, const char *name, const char *val)
{
Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
}
void
Var_SetExpand(GNode *scope, const char *name, const char *val)
{
FStr varname = FStr_InitRefer(name);
assert(val != NULL);
Var_Expand(&varname, scope, VARE_EVAL);
if (varname.str[0] == '\0') {
DEBUG4(VAR,
"%s: ignoring '%s = %s' "
"as the variable name '%s' expands to empty\n",
scope->name, varname.str, val, name);
} else
Var_SetWithFlags(scope, varname.str, val, VAR_SET_NONE);
FStr_Done(&varname);
}
void
Global_Set(const char *name, const char *value)
{
Var_Set(SCOPE_GLOBAL, name, value);
}
void
Global_Delete(const char *name)
{
Var_Delete(SCOPE_GLOBAL, name);
}
void
Global_Set_ReadOnly(const char *name, const char *value)
{
Var_SetWithFlags(SCOPE_GLOBAL, name, value, VAR_SET_NONE);
VarFind(name, SCOPE_GLOBAL, false)->readOnlyLoud = true;
}
void
Var_Append(GNode *scope, const char *name, const char *val)
{
Var *v;
v = VarFind(name, scope, scope == SCOPE_GLOBAL);
if (v == NULL) {
Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
} else if (v->readOnlyLoud) {
Parse_Error(PARSE_FATAL,
"Cannot append to \"%s\" as it is read-only", name);
return;
} else if (v->readOnly) {
DEBUG3(VAR, "%s: ignoring '%s += %s' as it is read-only\n",
scope->name, name, val);
} else if (scope == SCOPE_CMDLINE || !v->fromCmd) {
Buf_AddByte(&v->val, ' ');
Buf_AddStr(&v->val, val);
DEBUG3(VAR, "%s: %s = %s\n", scope->name, name, v->val.data);
if (v->fromEnvironment) {
HashEntry *he =
HashTable_CreateEntry(&scope->vars, name, NULL);
HashEntry_Set(he, v);
FStr_Done(&v->name);
v->name = FStr_InitRefer( he->key);
v->shortLived = false;
v->fromEnvironment = false;
}
}
}
void
Var_AppendExpand(GNode *scope, const char *name, const char *val)
{
FStr xname = FStr_InitRefer(name);
assert(val != NULL);
Var_Expand(&xname, scope, VARE_EVAL);
if (xname.str != name && xname.str[0] == '\0')
DEBUG4(VAR,
"%s: ignoring '%s += %s' "
"as the variable name '%s' expands to empty\n",
scope->name, xname.str, val, name);
else
Var_Append(scope, xname.str, val);
FStr_Done(&xname);
}
void
Global_Append(const char *name, const char *value)
{
Var_Append(SCOPE_GLOBAL, name, value);
}
bool
Var_Exists(GNode *scope, const char *name)
{
Var *v = VarFind(name, scope, true);
if (v == NULL)
return false;
VarFreeShortLived(v);
return true;
}
bool
Var_ExistsExpand(GNode *scope, const char *name)
{
FStr varname = FStr_InitRefer(name);
bool exists;
Var_Expand(&varname, scope, VARE_EVAL);
exists = Var_Exists(scope, varname.str);
FStr_Done(&varname);
return exists;
}
FStr
Var_Value(GNode *scope, const char *name)
{
Var *v = VarFind(name, scope, true);
char *value;
if (v == NULL)
return FStr_InitRefer(NULL);
if (!v->shortLived)
return FStr_InitRefer(v->val.data);
value = v->val.data;
v->val.data = NULL;
VarFreeShortLived(v);
return FStr_InitOwn(value);
}
void
Var_ReadOnly(const char *name, bool bf)
{
Var *v;
v = VarFind(name, SCOPE_GLOBAL, false);
if (v == NULL) {
DEBUG1(VAR, "Var_ReadOnly: %s not found\n", name);
return;
}
v->readOnly = bf;
DEBUG2(VAR, "Var_ReadOnly: %s %s\n", name, bf ? "true" : "false");
}
const char *
GNode_ValueDirect(GNode *gn, const char *name)
{
Var *v = VarFind(name, gn, false);
return v != NULL ? v->val.data : NULL;
}
static VarEvalMode
VarEvalMode_WithoutKeepDollar(VarEvalMode emode)
{
return emode == VARE_EVAL_KEEP_DOLLAR_AND_UNDEFINED
? VARE_EVAL_KEEP_UNDEFINED : emode;
}
static bool
VarEvalMode_ShouldEval(VarEvalMode emode)
{
return emode != VARE_PARSE;
}
static bool
VarEvalMode_ShouldKeepUndef(VarEvalMode emode)
{
return emode == VARE_EVAL_KEEP_UNDEFINED ||
emode == VARE_EVAL_KEEP_DOLLAR_AND_UNDEFINED;
}
static bool
VarEvalMode_ShouldKeepDollar(VarEvalMode emode)
{
return emode == VARE_EVAL_KEEP_DOLLAR_AND_UNDEFINED;
}
static void
SepBuf_Init(SepBuf *buf, char sep)
{
Buf_InitSize(&buf->buf, 32);
buf->needSep = false;
buf->sep = sep;
}
static void
SepBuf_Sep(SepBuf *buf)
{
buf->needSep = true;
}
static void
SepBuf_AddBytes(SepBuf *buf, const char *mem, size_t mem_size)
{
if (mem_size == 0)
return;
if (buf->needSep && buf->sep != '\0') {
Buf_AddByte(&buf->buf, buf->sep);
buf->needSep = false;
}
Buf_AddBytes(&buf->buf, mem, mem_size);
}
static void
SepBuf_AddRange(SepBuf *buf, const char *start, const char *end)
{
SepBuf_AddBytes(buf, start, (size_t)(end - start));
}
static void
SepBuf_AddStr(SepBuf *buf, const char *str)
{
SepBuf_AddBytes(buf, str, strlen(str));
}
static void
SepBuf_AddSubstring(SepBuf *buf, Substring sub)
{
SepBuf_AddRange(buf, sub.start, sub.end);
}
static char *
SepBuf_DoneData(SepBuf *buf)
{
return Buf_DoneData(&buf->buf);
}
typedef void (*ModifyWordProc)(Substring word, SepBuf *buf, void *data);
static void
ModifyWord_Head(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
{
SepBuf_AddSubstring(buf, Substring_Dirname(word));
}
static void
ModifyWord_Tail(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
{
SepBuf_AddSubstring(buf, Substring_Basename(word));
}
static void
ModifyWord_Suffix(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
{
const char *lastDot = Substring_FindLast(word, '.');
if (lastDot != NULL)
SepBuf_AddRange(buf, lastDot + 1, word.end);
}
static void
ModifyWord_Root(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
{
const char *lastDot, *end;
lastDot = Substring_FindLast(word, '.');
end = lastDot != NULL ? lastDot : word.end;
SepBuf_AddRange(buf, word.start, end);
}
struct ModifyWord_SysVSubstArgs {
GNode *scope;
Substring lhsPrefix;
bool lhsPercent;
Substring lhsSuffix;
const char *rhs;
};
static void
ModifyWord_SysVSubst(Substring word, SepBuf *buf, void *data)
{
const struct ModifyWord_SysVSubstArgs *args = data;
FStr rhs;
const char *percent;
if (Substring_IsEmpty(word))
return;
if (!Substring_HasPrefix(word, args->lhsPrefix) ||
!Substring_HasSuffix(word, args->lhsSuffix)) {
SepBuf_AddSubstring(buf, word);
return;
}
rhs = FStr_InitRefer(args->rhs);
Var_Expand(&rhs, args->scope, VARE_EVAL);
percent = args->lhsPercent ? strchr(rhs.str, '%') : NULL;
if (percent != NULL)
SepBuf_AddRange(buf, rhs.str, percent);
if (percent != NULL || !args->lhsPercent)
SepBuf_AddRange(buf,
word.start + Substring_Length(args->lhsPrefix),
word.end - Substring_Length(args->lhsSuffix));
SepBuf_AddStr(buf, percent != NULL ? percent + 1 : rhs.str);
FStr_Done(&rhs);
}
static const char *
Substring_Find(Substring haystack, Substring needle)
{
size_t len, needleLen, i;
len = Substring_Length(haystack);
needleLen = Substring_Length(needle);
for (i = 0; i + needleLen <= len; i++)
if (memcmp(haystack.start + i, needle.start, needleLen) == 0)
return haystack.start + i;
return NULL;
}
struct ModifyWord_SubstArgs {
Substring lhs;
Substring rhs;
PatternFlags pflags;
bool matched;
};
static void
ModifyWord_Subst(Substring word, SepBuf *buf, void *data)
{
struct ModifyWord_SubstArgs *args = data;
size_t wordLen, lhsLen;
const char *match;
wordLen = Substring_Length(word);
if (args->pflags.subOnce && args->matched)
goto nosub;
lhsLen = Substring_Length(args->lhs);
if (args->pflags.anchorStart) {
if (wordLen < lhsLen ||
memcmp(word.start, args->lhs.start, lhsLen) != 0)
goto nosub;
if (args->pflags.anchorEnd && wordLen != lhsLen)
goto nosub;
SepBuf_AddSubstring(buf, args->rhs);
SepBuf_AddRange(buf, word.start + lhsLen, word.end);
args->matched = true;
return;
}
if (args->pflags.anchorEnd) {
if (wordLen < lhsLen)
goto nosub;
if (memcmp(word.end - lhsLen, args->lhs.start, lhsLen) != 0)
goto nosub;
SepBuf_AddRange(buf, word.start, word.end - lhsLen);
SepBuf_AddSubstring(buf, args->rhs);
args->matched = true;
return;
}
if (Substring_IsEmpty(args->lhs))
goto nosub;
while ((match = Substring_Find(word, args->lhs)) != NULL) {
SepBuf_AddRange(buf, word.start, match);
SepBuf_AddSubstring(buf, args->rhs);
args->matched = true;
word.start = match + lhsLen;
if (Substring_IsEmpty(word) || !args->pflags.subGlobal)
break;
}
nosub:
SepBuf_AddSubstring(buf, word);
}
static void
RegexError(int reerr, const regex_t *pat, const char *str)
{
size_t errlen = regerror(reerr, pat, NULL, 0);
char *errbuf = bmake_malloc(errlen);
regerror(reerr, pat, errbuf, errlen);
Parse_Error(PARSE_FATAL, "%s: %s", str, errbuf);
free(errbuf);
}
static void
RegexReplaceBackref(char ref, SepBuf *buf, const char *wp,
const regmatch_t *m, size_t nsub)
{
unsigned n = (unsigned)ref - '0';
if (n >= nsub)
Parse_Error(PARSE_FATAL, "No subexpression \\%u", n);
else if (m[n].rm_so == -1) {
if (opts.strict)
Error("No match for subexpression \\%u", n);
} else {
SepBuf_AddRange(buf,
wp + (size_t)m[n].rm_so,
wp + (size_t)m[n].rm_eo);
}
}
static void
RegexReplace(Substring replace, SepBuf *buf, const char *wp,
const regmatch_t *m, size_t nsub)
{
const char *rp;
for (rp = replace.start; rp != replace.end; rp++) {
if (*rp == '\\' && rp + 1 != replace.end &&
(rp[1] == '&' || rp[1] == '\\'))
SepBuf_AddBytes(buf, ++rp, 1);
else if (*rp == '\\' && rp + 1 != replace.end &&
ch_isdigit(rp[1]))
RegexReplaceBackref(*++rp, buf, wp, m, nsub);
else if (*rp == '&') {
SepBuf_AddRange(buf,
wp + (size_t)m[0].rm_so,
wp + (size_t)m[0].rm_eo);
} else
SepBuf_AddBytes(buf, rp, 1);
}
}
struct ModifyWord_SubstRegexArgs {
regex_t re;
size_t nsub;
Substring replace;
PatternFlags pflags;
bool matched;
};
static void
ModifyWord_SubstRegex(Substring word, SepBuf *buf, void *data)
{
struct ModifyWord_SubstRegexArgs *args = data;
int xrv;
const char *wp;
int flags = 0;
regmatch_t m[10];
assert(word.end[0] == '\0');
wp = word.start;
if (args->pflags.subOnce && args->matched)
goto no_match;
again:
xrv = regexec(&args->re, wp, args->nsub, m, flags);
if (xrv == 0)
goto ok;
if (xrv != REG_NOMATCH)
RegexError(xrv, &args->re, "Unexpected regex error");
no_match:
SepBuf_AddRange(buf, wp, word.end);
return;
ok:
args->matched = true;
SepBuf_AddBytes(buf, wp, (size_t)m[0].rm_so);
RegexReplace(args->replace, buf, wp, m, args->nsub);
wp += (size_t)m[0].rm_eo;
if (args->pflags.subGlobal) {
flags |= REG_NOTBOL;
if (m[0].rm_so == 0 && m[0].rm_eo == 0 && *wp != '\0') {
SepBuf_AddBytes(buf, wp, 1);
wp++;
}
if (*wp != '\0')
goto again;
}
if (*wp != '\0')
SepBuf_AddStr(buf, wp);
}
struct ModifyWord_LoopArgs {
GNode *scope;
const char *var;
const char *body;
VarEvalMode emode;
};
static void
ModifyWord_Loop(Substring word, SepBuf *buf, void *data)
{
const struct ModifyWord_LoopArgs *args;
char *s;
if (Substring_IsEmpty(word))
return;
args = data;
assert(word.end[0] == '\0');
Var_SetWithFlags(args->scope, args->var, word.start,
VAR_SET_NO_EXPORT);
s = Var_Subst(args->body, args->scope, args->emode);
DEBUG2(VAR, "ModifyWord_Loop: expand \"%s\" to \"%s\"\n",
args->body, s);
if (s[0] == '\n' || Buf_EndsWith(&buf->buf, '\n'))
buf->needSep = false;
SepBuf_AddStr(buf, s);
free(s);
}
static char *
VarSelectWords(const char *str, int first, int last,
char sep, bool oneBigWord)
{
SubstringWords words;
int len, start, end, step;
int i;
SepBuf buf;
SepBuf_Init(&buf, sep);
if (oneBigWord) {
words.len = 1;
words.words = bmake_malloc(sizeof(words.words[0]));
words.freeIt = NULL;
words.words[0] = Substring_InitStr(str);
} else {
words = Substring_Words(str, false);
}
len = (int)words.len;
if (first < 0)
first += len + 1;
if (last < 0)
last += len + 1;
if (first > last) {
start = (first > len ? len : first) - 1;
end = last < 1 ? 0 : last - 1;
step = -1;
} else {
start = first < 1 ? 0 : first - 1;
end = last > len ? len : last;
step = 1;
}
for (i = start; (step < 0) == (i >= end); i += step) {
SepBuf_AddSubstring(&buf, words.words[i]);
SepBuf_Sep(&buf);
}
SubstringWords_Free(words);
return SepBuf_DoneData(&buf);
}
static void
ModifyWord_Realpath(Substring word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
{
struct stat st;
char rbuf[MAXPATHLEN];
const char *rp;
assert(word.end[0] == '\0');
rp = cached_realpath(word.start, rbuf);
if (rp != NULL && *rp == '/' && stat(rp, &st) == 0)
SepBuf_AddStr(buf, rp);
else
SepBuf_AddSubstring(buf, word);
}
static char *
SubstringWords_JoinFree(SubstringWords words)
{
Buffer buf;
size_t i;
Buf_Init(&buf);
for (i = 0; i < words.len; i++) {
if (i != 0) {
Buf_AddByte(&buf, ' ');
}
Buf_AddRange(&buf, words.words[i].start, words.words[i].end);
}
SubstringWords_Free(words);
return Buf_DoneData(&buf);
}
static void
QuoteShell(const char *str, bool quoteDollar, LazyBuf *buf)
{
const char *p;
LazyBuf_Init(buf, str);
for (p = str; *p != '\0'; p++) {
if (*p == '\n') {
const char *newline = Shell_GetNewline();
if (newline == NULL)
newline = "\\\n";
LazyBuf_AddStr(buf, newline);
continue;
}
if (ch_isspace(*p) || ch_is_shell_meta(*p))
LazyBuf_Add(buf, '\\');
LazyBuf_Add(buf, *p);
if (quoteDollar && *p == '$')
LazyBuf_AddStr(buf, "\\$");
}
}
static char *
Hash(const char *str)
{
static const char hexdigits[] = "0123456789abcdef";
const unsigned char *ustr = (const unsigned char *)str;
uint32_t h = 0x971e137bU;
uint32_t c1 = 0x95543787U;
uint32_t c2 = 0x2ad7eb25U;
size_t len2 = strlen(str);
char *buf;
size_t i;
size_t len;
for (len = len2; len != 0;) {
uint32_t k = 0;
switch (len) {
default:
k = ((uint32_t)ustr[3] << 24) |
((uint32_t)ustr[2] << 16) |
((uint32_t)ustr[1] << 8) |
(uint32_t)ustr[0];
len -= 4;
ustr += 4;
break;
case 3:
k |= (uint32_t)ustr[2] << 16;
case 2:
k |= (uint32_t)ustr[1] << 8;
case 1:
k |= (uint32_t)ustr[0];
len = 0;
}
c1 = c1 * 5 + 0x7b7d159cU;
c2 = c2 * 5 + 0x6bce6396U;
k *= c1;
k = (k << 11) ^ (k >> 21);
k *= c2;
h = (h << 13) ^ (h >> 19);
h = h * 5 + 0x52dce729U;
h ^= k;
}
h ^= (uint32_t)len2;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
buf = bmake_malloc(9);
for (i = 0; i < 8; i++) {
buf[i] = hexdigits[h & 0x0f];
h >>= 4;
}
buf[8] = '\0';
return buf;
}
static char *
FormatTime(const char *fmt, time_t t, bool gmt)
{
char buf[BUFSIZ];
if (t == 0)
time(&t);
if (*fmt == '\0')
fmt = "%c";
if (gmt && strchr(fmt, 's') != NULL) {
const char *prev_tz_env = getenv("TZ");
char *prev_tz = prev_tz_env != NULL
? bmake_strdup(prev_tz_env) : NULL;
setenv("TZ", "UTC", 1);
strftime(buf, sizeof buf, fmt, localtime(&t));
if (prev_tz != NULL) {
setenv("TZ", prev_tz, 1);
free(prev_tz);
} else
unsetenv("TZ");
} else
strftime(buf, sizeof buf, fmt, (gmt ? gmtime : localtime)(&t));
buf[sizeof buf - 1] = '\0';
return bmake_strdup(buf);
}
typedef enum ExprDefined {
DEF_REGULAR,
DEF_UNDEF,
DEF_DEFINED
} ExprDefined;
static const char ExprDefined_Name[][10] = {
"regular",
"undefined",
"defined"
};
#if __STDC_VERSION__ >= 199901L
#define const_member const
#else
#define const_member
#endif
typedef struct Expr {
const char *name;
FStr value;
VarEvalMode const_member emode;
GNode *const_member scope;
ExprDefined defined;
} Expr;
typedef struct ModChain {
Expr *expr;
char const_member startc;
char const_member endc;
char sep;
bool oneBigWord;
} ModChain;
static void
Expr_Define(Expr *expr)
{
if (expr->defined == DEF_UNDEF)
expr->defined = DEF_DEFINED;
}
static const char *
Expr_Str(const Expr *expr)
{
return expr->value.str;
}
static SubstringWords
Expr_Words(const Expr *expr)
{
return Substring_Words(Expr_Str(expr), false);
}
static void
Expr_SetValue(Expr *expr, FStr value)
{
FStr_Done(&expr->value);
expr->value = value;
}
static void
Expr_SetValueOwn(Expr *expr, char *value)
{
Expr_SetValue(expr, FStr_InitOwn(value));
}
static void
Expr_SetValueRefer(Expr *expr, const char *value)
{
Expr_SetValue(expr, FStr_InitRefer(value));
}
static bool
Expr_ShouldEval(const Expr *expr)
{
return VarEvalMode_ShouldEval(expr->emode);
}
static bool
ModChain_ShouldEval(const ModChain *ch)
{
return Expr_ShouldEval(ch->expr);
}
typedef enum ApplyModifierResult {
AMR_OK,
AMR_UNKNOWN,
AMR_CLEANUP
} ApplyModifierResult;
static bool
IsEscapedModifierPart(const char *p, char delim1, char delim2,
struct ModifyWord_SubstArgs *subst)
{
if (p[0] != '\\' || p[1] == '\0')
return false;
if (p[1] == delim1 || p[1] == delim2 || p[1] == '\\' || p[1] == '$')
return true;
return p[1] == '&' && subst != NULL;
}
static void
ParseModifierPartExpr(const char **pp, LazyBuf *part, const ModChain *ch,
VarEvalMode emode)
{
const char *p = *pp;
FStr nested_val = Var_Parse(&p, ch->expr->scope,
VarEvalMode_WithoutKeepDollar(emode));
if (VarEvalMode_ShouldEval(emode))
LazyBuf_AddStr(part, nested_val.str);
else
LazyBuf_AddSubstring(part, Substring_Init(*pp, p));
FStr_Done(&nested_val);
*pp = p;
}
static void
ParseModifierPartBalanced(const char **pp, LazyBuf *part)
{
const char *p = *pp;
if (p[1] == '(' || p[1] == '{') {
char startc = p[1];
int endc = startc == '(' ? ')' : '}';
int depth = 1;
for (p += 2; *p != '\0' && depth > 0; p++) {
if (p[-1] != '\\') {
if (*p == startc)
depth++;
if (*p == endc)
depth--;
}
}
LazyBuf_AddSubstring(part, Substring_Init(*pp, p));
*pp = p;
} else {
LazyBuf_Add(part, *p);
*pp = p + 1;
}
}
static bool
ParseModifierPart(
const char **pp,
char end1,
char end2,
VarEvalMode emode,
ModChain *ch,
LazyBuf *part,
PatternFlags *pflags,
struct ModifyWord_SubstArgs *subst
)
{
const char *p = *pp;
LazyBuf_Init(part, p);
while (*p != '\0' && *p != end1 && *p != end2) {
if (IsEscapedModifierPart(p, end1, end2, subst)) {
LazyBuf_Add(part, p[1]);
p += 2;
} else if (*p != '$') {
if (subst != NULL && *p == '&')
LazyBuf_AddSubstring(part, subst->lhs);
else
LazyBuf_Add(part, *p);
p++;
} else if (p[1] == end2) {
if (pflags != NULL)
pflags->anchorEnd = true;
else
LazyBuf_Add(part, *p);
p++;
} else if (emode == VARE_PARSE_BALANCED)
ParseModifierPartBalanced(&p, part);
else
ParseModifierPartExpr(&p, part, ch, emode);
}
if (*p != end1 && *p != end2) {
Parse_Error(PARSE_FATAL,
"Unfinished modifier after \"%.*s\", expecting \"%c\"",
(int)(p - *pp), *pp, end2);
LazyBuf_Done(part);
*pp = p;
return false;
}
*pp = p;
if (end1 == end2)
(*pp)++;
{
Substring sub = LazyBuf_Get(part);
DEBUG2(VAR, "Modifier part: \"%.*s\"\n",
(int)Substring_Length(sub), sub.start);
}
return true;
}
MAKE_INLINE bool
IsDelimiter(char c, const ModChain *ch)
{
return c == ':' || c == ch->endc || c == '\0';
}
MAKE_INLINE bool
ModMatch(const char *mod, const char *modname, const ModChain *ch)
{
size_t n = strlen(modname);
return strncmp(mod, modname, n) == 0 && IsDelimiter(mod[n], ch);
}
MAKE_INLINE bool
ModMatchEq(const char *mod, const char *modname, const ModChain *ch)
{
size_t n = strlen(modname);
return strncmp(mod, modname, n) == 0 &&
(IsDelimiter(mod[n], ch) || mod[n] == '=');
}
static bool
TryParseIntBase0(const char **pp, int *out_num)
{
char *end;
long n;
errno = 0;
n = strtol(*pp, &end, 0);
if (end == *pp)
return false;
if ((n == LONG_MIN || n == LONG_MAX) && errno == ERANGE)
return false;
if (n < INT_MIN || n > INT_MAX)
return false;
*pp = end;
*out_num = (int)n;
return true;
}
static bool
TryParseSize(const char **pp, size_t *out_num)
{
char *end;
unsigned long n;
if (!ch_isdigit(**pp))
return false;
errno = 0;
n = strtoul(*pp, &end, 10);
if (n == ULONG_MAX && errno == ERANGE)
return false;
if (n > SIZE_MAX)
return false;
*pp = end;
*out_num = (size_t)n;
return true;
}
static bool
TryParseChar(const char **pp, int base, char *out_ch)
{
char *end;
unsigned long n;
if (!ch_isalnum(**pp))
return false;
errno = 0;
n = strtoul(*pp, &end, base);
if (n == ULONG_MAX && errno == ERANGE)
return false;
if (n > UCHAR_MAX)
return false;
*pp = end;
*out_ch = (char)n;
return true;
}
static void
ModifyWords(ModChain *ch,
ModifyWordProc modifyWord, void *modifyWord_args,
bool oneBigWord)
{
Expr *expr = ch->expr;
const char *val = Expr_Str(expr);
SepBuf result;
SubstringWords words;
size_t i;
Substring word;
if (!ModChain_ShouldEval(ch))
return;
if (oneBigWord) {
SepBuf_Init(&result, ch->sep);
word = Substring_InitStr(val);
modifyWord(word, &result, modifyWord_args);
goto done;
}
words = Substring_Words(val, false);
DEBUG3(VAR, "ModifyWords: split \"%s\" into %u %s\n",
val, (unsigned)words.len, words.len != 1 ? "words" : "word");
SepBuf_Init(&result, ch->sep);
for (i = 0; i < words.len; i++) {
modifyWord(words.words[i], &result, modifyWord_args);
if (result.buf.len > 0)
SepBuf_Sep(&result);
}
SubstringWords_Free(words);
done:
Expr_SetValueOwn(expr, SepBuf_DoneData(&result));
}
static ApplyModifierResult
ApplyModifier_Loop(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
struct ModifyWord_LoopArgs args;
char prev_sep;
LazyBuf tvarBuf, strBuf;
FStr tvar, str;
args.scope = expr->scope;
(*pp)++;
if (!ParseModifierPart(pp, '@', '@', VARE_PARSE,
ch, &tvarBuf, NULL, NULL))
return AMR_CLEANUP;
tvar = LazyBuf_DoneGet(&tvarBuf);
args.var = tvar.str;
if (strchr(args.var, '$') != NULL) {
Parse_Error(PARSE_FATAL,
"In the :@ modifier, the variable name \"%s\" "
"must not contain a dollar",
args.var);
goto cleanup_tvar;
}
if (!ParseModifierPart(pp, '@', '@', VARE_PARSE_BALANCED,
ch, &strBuf, NULL, NULL))
goto cleanup_tvar;
str = LazyBuf_DoneGet(&strBuf);
args.body = str.str;
if (!Expr_ShouldEval(expr))
goto done;
args.emode = VarEvalMode_WithoutKeepDollar(expr->emode);
prev_sep = ch->sep;
ch->sep = ' ';
ModifyWords(ch, ModifyWord_Loop, &args, ch->oneBigWord);
ch->sep = prev_sep;
Var_Delete(expr->scope, args.var);
done:
FStr_Done(&tvar);
FStr_Done(&str);
return AMR_OK;
cleanup_tvar:
FStr_Done(&tvar);
return AMR_CLEANUP;
}
static void
ParseModifier_Defined(const char **pp, ModChain *ch, bool shouldEval,
LazyBuf *buf)
{
const char *p;
p = *pp + 1;
LazyBuf_Init(buf, p);
while (!IsDelimiter(*p, ch)) {
if (*p == '\\') {
char c = p[1];
if ((IsDelimiter(c, ch) && c != '\0') ||
c == '$' || c == '\\') {
if (shouldEval)
LazyBuf_Add(buf, c);
p += 2;
continue;
}
}
if (*p == '$') {
FStr val = Var_Parse(&p, ch->expr->scope,
shouldEval ? ch->expr->emode : VARE_PARSE);
if (shouldEval)
LazyBuf_AddStr(buf, val.str);
FStr_Done(&val);
continue;
}
if (shouldEval)
LazyBuf_Add(buf, *p);
p++;
}
*pp = p;
}
static ApplyModifierResult
ApplyModifier_Defined(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
LazyBuf buf;
bool shouldEval =
Expr_ShouldEval(expr) &&
(**pp == 'D') == (expr->defined == DEF_REGULAR);
ParseModifier_Defined(pp, ch, shouldEval, &buf);
Expr_Define(expr);
if (shouldEval)
Expr_SetValue(expr, Substring_Str(LazyBuf_Get(&buf)));
LazyBuf_Done(&buf);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Literal(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
(*pp)++;
if (Expr_ShouldEval(expr)) {
Expr_Define(expr);
Expr_SetValueOwn(expr, bmake_strdup(expr->name));
}
return AMR_OK;
}
static bool
TryParseTime(const char **pp, time_t *out_time)
{
char *end;
unsigned long n;
if (!ch_isdigit(**pp))
return false;
errno = 0;
n = strtoul(*pp, &end, 10);
if (n == ULONG_MAX && errno == ERANGE)
return false;
*pp = end;
*out_time = (time_t)n;
return true;
}
static ApplyModifierResult
ApplyModifier_Time(const char **pp, ModChain *ch)
{
Expr *expr;
time_t t;
const char *args;
const char *mod = *pp;
bool gmt = mod[0] == 'g';
if (!ModMatchEq(mod, gmt ? "gmtime" : "localtime", ch))
return AMR_UNKNOWN;
args = mod + (gmt ? 6 : 9);
if (args[0] == '=') {
const char *p = args + 1;
LazyBuf buf;
FStr arg;
if (!ParseModifierPart(&p, ':', ch->endc, ch->expr->emode,
ch, &buf, NULL, NULL))
return AMR_CLEANUP;
arg = LazyBuf_DoneGet(&buf);
if (ModChain_ShouldEval(ch)) {
const char *arg_p = arg.str;
if (!TryParseTime(&arg_p, &t) || *arg_p != '\0') {
Parse_Error(PARSE_FATAL,
"Invalid time value \"%s\"", arg.str);
FStr_Done(&arg);
return AMR_CLEANUP;
}
} else
t = 0;
FStr_Done(&arg);
*pp = p;
} else {
t = 0;
*pp = args;
}
expr = ch->expr;
if (Expr_ShouldEval(expr))
Expr_SetValueOwn(expr, FormatTime(Expr_Str(expr), t, gmt));
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Hash(const char **pp, ModChain *ch)
{
if (!ModMatch(*pp, "hash", ch))
return AMR_UNKNOWN;
*pp += 4;
if (ModChain_ShouldEval(ch))
Expr_SetValueOwn(ch->expr, Hash(Expr_Str(ch->expr)));
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Path(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
GNode *gn;
char *path;
(*pp)++;
if (!Expr_ShouldEval(expr))
return AMR_OK;
Expr_Define(expr);
gn = Targ_FindNode(expr->name);
if (gn == NULL || gn->type & OP_NOPATH)
path = NULL;
else if (gn->path != NULL)
path = bmake_strdup(gn->path);
else {
SearchPath *searchPath = Suff_FindPath(gn);
path = Dir_FindFile(expr->name, searchPath);
}
if (path == NULL)
path = bmake_strdup(expr->name);
Expr_SetValueOwn(expr, path);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_ShellCommand(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
LazyBuf cmdBuf;
FStr cmd;
(*pp)++;
if (!ParseModifierPart(pp, '!', '!', expr->emode,
ch, &cmdBuf, NULL, NULL))
return AMR_CLEANUP;
cmd = LazyBuf_DoneGet(&cmdBuf);
if (Expr_ShouldEval(expr)) {
char *output, *error;
output = Cmd_Exec(cmd.str, &error);
Expr_SetValueOwn(expr, output);
if (error != NULL) {
Parse_Error(PARSE_WARNING, "%s", error);
free(error);
}
} else
Expr_SetValueRefer(expr, "");
FStr_Done(&cmd);
Expr_Define(expr);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Range(const char **pp, ModChain *ch)
{
size_t n;
Buffer buf;
size_t i;
const char *mod = *pp;
if (!ModMatchEq(mod, "range", ch))
return AMR_UNKNOWN;
if (mod[5] == '=') {
const char *p = mod + 6;
if (!TryParseSize(&p, &n)) {
Parse_Error(PARSE_FATAL,
"Invalid number \"%s\" for modifier \":range\"",
mod + 6);
return AMR_CLEANUP;
}
*pp = p;
} else {
n = 0;
*pp = mod + 5;
}
if (!ModChain_ShouldEval(ch))
return AMR_OK;
if (n == 0) {
SubstringWords words = Expr_Words(ch->expr);
n = words.len;
SubstringWords_Free(words);
}
Buf_Init(&buf);
for (i = 0; i < n; i++) {
if (i != 0) {
Buf_AddByte(&buf, ' ');
}
Buf_AddInt(&buf, 1 + (int)i);
}
Expr_SetValueOwn(ch->expr, Buf_DoneData(&buf));
return AMR_OK;
}
static char *
ParseModifier_Match(const char **pp, const ModChain *ch)
{
const char *mod = *pp;
Expr *expr = ch->expr;
bool copy = false;
bool needSubst = false;
const char *endpat;
char *pattern;
int depth = 0;
const char *p;
for (p = mod + 1; *p != '\0' && !(*p == ':' && depth == 0); p++) {
if (*p == '\\' && p[1] != '\0' &&
(IsDelimiter(p[1], ch) || p[1] == ch->startc)) {
if (!needSubst)
copy = true;
p++;
continue;
}
if (*p == '$')
needSubst = true;
if (*p == '(' || *p == '{')
depth++;
if (*p == ')' || *p == '}') {
depth--;
if (depth < 0)
break;
}
}
*pp = p;
endpat = p;
if (copy) {
char *dst;
const char *src;
pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1);
dst = pattern;
src = mod + 1;
for (; src < endpat; src++, dst++) {
if (src[0] == '\\' && src + 1 < endpat &&
IsDelimiter(src[1], ch))
src++;
*dst = *src;
}
*dst = '\0';
} else {
pattern = bmake_strsedup(mod + 1, endpat);
}
if (needSubst) {
char *old_pattern = pattern;
pattern = Var_Subst(pattern, expr->scope, expr->emode);
free(old_pattern);
}
DEBUG2(VAR, "Pattern for ':%c' is \"%s\"\n", mod[0], pattern);
return pattern;
}
struct ModifyWord_MatchArgs {
StringList patterns;
bool neg;
bool error_reported;
};
static void
ModifyWord_Match(Substring word, SepBuf *buf, void *data)
{
struct ModifyWord_MatchArgs *args = data;
StringListNode *ln;
StrMatchResult res;
const char *pattern;
assert(word.end[0] == '\0');
for (ln = args->patterns.first; ln != NULL; ln = ln->next) {
pattern = ln->datum;
res = Str_Match(word.start, pattern);
if (res.error != NULL && !args->error_reported) {
args->error_reported = true;
Parse_Error(PARSE_FATAL,
"%s in pattern \"%s\" of modifier \"%s\"",
res.error, pattern, args->neg ? ":N" : ":M");
}
if (res.matched != args->neg) {
SepBuf_AddSubstring(buf, word);
break;
}
}
}
static ApplyModifierResult
ApplyModifier_Match(const char **pp, ModChain *ch)
{
char mod = **pp;
char *pattern;
pattern = ParseModifier_Match(pp, ch);
if (ModChain_ShouldEval(ch)) {
struct ModifyWord_MatchArgs args;
const char *brace;
Lst_Init(&args.patterns);
if (mod == 'N')
brace = NULL;
else
for (brace = strchr(pattern, '{'); brace != NULL;
brace = strchr(++brace, '{')) {
if (brace == pattern
|| (brace[-1] != '\\' && brace[-1] != '$'))
break;
}
if (brace != NULL)
ExpandCurly(pattern, brace, NULL, &args.patterns);
else
Lst_Append(&args.patterns, pattern);
args.neg = mod == 'N';
args.error_reported = false;
ModifyWords(ch, ModifyWord_Match, &args, ch->oneBigWord);
if (brace != NULL)
Lst_DoneFree(&args.patterns);
else
Lst_Done(&args.patterns);
}
free(pattern);
return AMR_OK;
}
struct ModifyWord_MtimeArgs {
bool error;
bool use_fallback;
ApplyModifierResult rc;
time_t fallback;
};
static void
ModifyWord_Mtime(Substring word, SepBuf *buf, void *data)
{
struct ModifyWord_MtimeArgs *args = data;
struct stat st;
char tbuf[21];
if (Substring_IsEmpty(word))
return;
assert(word.end[0] == '\0');
if (stat(word.start, &st) < 0) {
if (args->error) {
Parse_Error(PARSE_FATAL,
"Cannot determine mtime for \"%s\": %s",
word.start, strerror(errno));
args->rc = AMR_CLEANUP;
return;
}
if (args->use_fallback)
st.st_mtime = args->fallback;
else
time(&st.st_mtime);
}
snprintf(tbuf, sizeof(tbuf), "%u", (unsigned)st.st_mtime);
SepBuf_AddStr(buf, tbuf);
}
static ApplyModifierResult
ApplyModifier_Mtime(const char **pp, ModChain *ch)
{
const char *p, *mod = *pp;
struct ModifyWord_MtimeArgs args;
if (!ModMatchEq(mod, "mtime", ch))
return AMR_UNKNOWN;
*pp += 5;
p = *pp;
args.error = false;
args.use_fallback = p[0] == '=';
args.rc = AMR_OK;
if (args.use_fallback) {
p++;
if (TryParseTime(&p, &args.fallback)) {
} else if (strncmp(p, "error", 5) == 0) {
p += 5;
args.error = true;
} else
goto invalid_argument;
if (!IsDelimiter(*p, ch))
goto invalid_argument;
*pp = p;
}
ModifyWords(ch, ModifyWord_Mtime, &args, ch->oneBigWord);
return args.rc;
invalid_argument:
Parse_Error(PARSE_FATAL,
"Invalid argument \"%.*s\" for modifier \":mtime\"",
(int)strcspn(*pp + 1, ":{}()"), *pp + 1);
return AMR_CLEANUP;
}
static void
ParsePatternFlags(const char **pp, PatternFlags *pflags, bool *oneBigWord)
{
for (;; (*pp)++) {
if (**pp == 'g')
pflags->subGlobal = true;
else if (**pp == '1')
pflags->subOnce = true;
else if (**pp == 'W')
*oneBigWord = true;
else
break;
}
}
MAKE_INLINE PatternFlags
PatternFlags_None(void)
{
PatternFlags pflags = { false, false, false, false };
return pflags;
}
static ApplyModifierResult
ApplyModifier_Subst(const char **pp, ModChain *ch)
{
struct ModifyWord_SubstArgs args;
bool oneBigWord;
LazyBuf lhsBuf, rhsBuf;
char delim = (*pp)[1];
if (delim == '\0') {
Parse_Error(PARSE_FATAL,
"Missing delimiter for modifier \":S\"");
(*pp)++;
return AMR_CLEANUP;
}
*pp += 2;
args.pflags = PatternFlags_None();
args.matched = false;
if (**pp == '^') {
args.pflags.anchorStart = true;
(*pp)++;
}
if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
ch, &lhsBuf, &args.pflags, NULL))
return AMR_CLEANUP;
args.lhs = LazyBuf_Get(&lhsBuf);
if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
ch, &rhsBuf, NULL, &args)) {
LazyBuf_Done(&lhsBuf);
return AMR_CLEANUP;
}
args.rhs = LazyBuf_Get(&rhsBuf);
oneBigWord = ch->oneBigWord;
ParsePatternFlags(pp, &args.pflags, &oneBigWord);
ModifyWords(ch, ModifyWord_Subst, &args, oneBigWord);
LazyBuf_Done(&lhsBuf);
LazyBuf_Done(&rhsBuf);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Regex(const char **pp, ModChain *ch)
{
struct ModifyWord_SubstRegexArgs args;
bool oneBigWord;
int error;
LazyBuf reBuf, replaceBuf;
FStr re;
char delim = (*pp)[1];
if (delim == '\0') {
Parse_Error(PARSE_FATAL,
"Missing delimiter for modifier \":C\"");
(*pp)++;
return AMR_CLEANUP;
}
*pp += 2;
if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
ch, &reBuf, NULL, NULL))
return AMR_CLEANUP;
re = LazyBuf_DoneGet(&reBuf);
if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
ch, &replaceBuf, NULL, NULL)) {
FStr_Done(&re);
return AMR_CLEANUP;
}
args.replace = LazyBuf_Get(&replaceBuf);
args.pflags = PatternFlags_None();
args.matched = false;
oneBigWord = ch->oneBigWord;
ParsePatternFlags(pp, &args.pflags, &oneBigWord);
if (!ModChain_ShouldEval(ch))
goto done;
if (re.str[0] == '\0') {
Parse_Error(PARSE_FATAL, "Regex compilation error: empty");
goto re_err;
}
error = regcomp(&args.re, re.str, REG_EXTENDED);
if (error != 0) {
RegexError(error, &args.re, "Regex compilation error");
re_err:
LazyBuf_Done(&replaceBuf);
FStr_Done(&re);
return AMR_CLEANUP;
}
args.nsub = args.re.re_nsub + 1;
if (args.nsub > 10)
args.nsub = 10;
ModifyWords(ch, ModifyWord_SubstRegex, &args, oneBigWord);
regfree(&args.re);
done:
LazyBuf_Done(&replaceBuf);
FStr_Done(&re);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Quote(const char **pp, ModChain *ch)
{
LazyBuf buf;
bool quoteDollar;
quoteDollar = **pp == 'q';
if (!IsDelimiter((*pp)[1], ch))
return AMR_UNKNOWN;
(*pp)++;
if (!ModChain_ShouldEval(ch))
return AMR_OK;
QuoteShell(Expr_Str(ch->expr), quoteDollar, &buf);
if (buf.data != NULL)
Expr_SetValue(ch->expr, LazyBuf_DoneGet(&buf));
else
LazyBuf_Done(&buf);
return AMR_OK;
}
static void
ModifyWord_Copy(Substring word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
{
SepBuf_AddSubstring(buf, word);
}
static ApplyModifierResult
ApplyModifier_ToSep(const char **pp, ModChain *ch)
{
const char *sep = *pp + 2;
if (sep[0] != ch->endc && IsDelimiter(sep[1], ch)) {
*pp = sep + 1;
ch->sep = sep[0];
goto ok;
}
if (IsDelimiter(sep[0], ch)) {
*pp = sep;
ch->sep = '\0';
goto ok;
}
if (sep[0] != '\\')
return AMR_UNKNOWN;
if (sep[1] == 'n') {
*pp = sep + 2;
ch->sep = '\n';
goto ok;
}
if (sep[1] == 't') {
*pp = sep + 2;
ch->sep = '\t';
goto ok;
}
{
const char *p = sep + 1;
int base = 8;
if (sep[1] == 'x') {
base = 16;
p++;
} else if (!ch_isdigit(sep[1]))
return AMR_UNKNOWN;
if (!TryParseChar(&p, base, &ch->sep)) {
Parse_Error(PARSE_FATAL,
"Invalid character number at \"%s\"", p);
return AMR_CLEANUP;
}
if (!IsDelimiter(*p, ch))
return AMR_UNKNOWN;
*pp = p;
}
ok:
ModifyWords(ch, ModifyWord_Copy, NULL, ch->oneBigWord);
return AMR_OK;
}
static char *
str_totitle(const char *str)
{
size_t i, n = strlen(str) + 1;
char *res = bmake_malloc(n);
for (i = 0; i < n; i++) {
if (i == 0 || ch_isspace(res[i - 1]))
res[i] = ch_toupper(str[i]);
else
res[i] = ch_tolower(str[i]);
}
return res;
}
static char *
str_toupper(const char *str)
{
size_t i, n = strlen(str) + 1;
char *res = bmake_malloc(n);
for (i = 0; i < n; i++)
res[i] = ch_toupper(str[i]);
return res;
}
static char *
str_tolower(const char *str)
{
size_t i, n = strlen(str) + 1;
char *res = bmake_malloc(n);
for (i = 0; i < n; i++)
res[i] = ch_tolower(str[i]);
return res;
}
static ApplyModifierResult
ApplyModifier_To(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
const char *mod = *pp;
assert(mod[0] == 't');
if (IsDelimiter(mod[1], ch))
return AMR_UNKNOWN;
if (mod[1] == 's')
return ApplyModifier_ToSep(pp, ch);
if (!IsDelimiter(mod[2], ch))
return AMR_UNKNOWN;
if (mod[1] == 'A') {
*pp = mod + 2;
ModifyWords(ch, ModifyWord_Realpath, NULL, ch->oneBigWord);
return AMR_OK;
}
if (mod[1] == 't') {
*pp = mod + 2;
if (Expr_ShouldEval(expr))
Expr_SetValueOwn(expr, str_totitle(Expr_Str(expr)));
return AMR_OK;
}
if (mod[1] == 'u') {
*pp = mod + 2;
if (Expr_ShouldEval(expr))
Expr_SetValueOwn(expr, str_toupper(Expr_Str(expr)));
return AMR_OK;
}
if (mod[1] == 'l') {
*pp = mod + 2;
if (Expr_ShouldEval(expr))
Expr_SetValueOwn(expr, str_tolower(Expr_Str(expr)));
return AMR_OK;
}
if (mod[1] == 'W' || mod[1] == 'w') {
*pp = mod + 2;
ch->oneBigWord = mod[1] == 'W';
return AMR_OK;
}
return AMR_UNKNOWN;
}
static ApplyModifierResult
ApplyModifier_Words(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
int first, last;
const char *p;
LazyBuf argBuf;
FStr arg;
(*pp)++;
if (!ParseModifierPart(pp, ']', ']', expr->emode,
ch, &argBuf, NULL, NULL))
return AMR_CLEANUP;
arg = LazyBuf_DoneGet(&argBuf);
p = arg.str;
if (!IsDelimiter(**pp, ch)) {
Parse_Error(PARSE_FATAL,
"Extra text after \"[%s]\"", arg.str);
FStr_Done(&arg);
return AMR_CLEANUP;
}
if (!ModChain_ShouldEval(ch))
goto ok;
if (p[0] == '\0')
goto bad_modifier;
if (strcmp(p, "#") == 0) {
if (ch->oneBigWord)
Expr_SetValueRefer(expr, "1");
else {
Buffer buf;
SubstringWords words = Expr_Words(expr);
size_t ac = words.len;
SubstringWords_Free(words);
Buf_Init(&buf);
Buf_AddInt(&buf, (int)ac);
Expr_SetValueOwn(expr, Buf_DoneData(&buf));
}
goto ok;
}
if (strcmp(p, "*") == 0) {
ch->oneBigWord = true;
goto ok;
}
if (strcmp(p, "@") == 0) {
ch->oneBigWord = false;
goto ok;
}
if (!TryParseIntBase0(&p, &first))
goto bad_modifier;
if (p[0] == '\0')
last = first;
else if (strncmp(p, "..", 2) == 0) {
p += 2;
if (!TryParseIntBase0(&p, &last) || *p != '\0')
goto bad_modifier;
} else
goto bad_modifier;
if (first == 0 && last == 0) {
ch->oneBigWord = true;
goto ok;
}
if (first == 0 || last == 0)
goto bad_modifier;
Expr_SetValueOwn(expr,
VarSelectWords(Expr_Str(expr), first, last,
ch->sep, ch->oneBigWord));
ok:
FStr_Done(&arg);
return AMR_OK;
bad_modifier:
Parse_Error(PARSE_FATAL, "Invalid modifier \":[%s]\"", arg.str);
FStr_Done(&arg);
return AMR_CLEANUP;
}
#if __STDC_VERSION__ >= 199901L
# define NUM_TYPE long long
# define PARSE_NUM_TYPE strtoll
#else
# define NUM_TYPE long
# define PARSE_NUM_TYPE strtol
#endif
static NUM_TYPE
num_val(Substring s)
{
NUM_TYPE val;
char *ep;
val = PARSE_NUM_TYPE(s.start, &ep, 0);
if (ep != s.start) {
switch (*ep) {
case 'K':
case 'k':
val <<= 10;
break;
case 'M':
case 'm':
val <<= 20;
break;
case 'G':
case 'g':
val <<= 30;
break;
}
}
return val;
}
static int
SubNumAsc(const void *sa, const void *sb)
{
NUM_TYPE a, b;
a = num_val(*(const Substring *)sa);
b = num_val(*(const Substring *)sb);
return a > b ? 1 : b > a ? -1 : 0;
}
static int
SubNumDesc(const void *sa, const void *sb)
{
return SubNumAsc(sb, sa);
}
static int
Substring_Cmp(Substring a, Substring b)
{
for (; a.start < a.end && b.start < b.end; a.start++, b.start++)
if (a.start[0] != b.start[0])
return (unsigned char)a.start[0]
- (unsigned char)b.start[0];
return (int)((a.end - a.start) - (b.end - b.start));
}
static int
SubStrAsc(const void *sa, const void *sb)
{
return Substring_Cmp(*(const Substring *)sa, *(const Substring *)sb);
}
static int
SubStrDesc(const void *sa, const void *sb)
{
return SubStrAsc(sb, sa);
}
static void
ShuffleSubstrings(Substring *strs, size_t n)
{
size_t i;
for (i = n - 1; i > 0; i--) {
size_t rndidx = (size_t)random() % (i + 1);
Substring t = strs[i];
strs[i] = strs[rndidx];
strs[rndidx] = t;
}
}
static ApplyModifierResult
ApplyModifier_Order(const char **pp, ModChain *ch)
{
const char *mod = *pp;
SubstringWords words;
int (*cmp)(const void *, const void *);
if (IsDelimiter(mod[1], ch)) {
cmp = SubStrAsc;
(*pp)++;
} else if (IsDelimiter(mod[2], ch)) {
if (mod[1] == 'n')
cmp = SubNumAsc;
else if (mod[1] == 'r')
cmp = SubStrDesc;
else if (mod[1] == 'x')
cmp = NULL;
else
return AMR_UNKNOWN;
*pp += 2;
} else if (IsDelimiter(mod[3], ch)) {
if ((mod[1] == 'n' && mod[2] == 'r') ||
(mod[1] == 'r' && mod[2] == 'n'))
cmp = SubNumDesc;
else
return AMR_UNKNOWN;
*pp += 3;
} else
return AMR_UNKNOWN;
if (!ModChain_ShouldEval(ch))
return AMR_OK;
words = Expr_Words(ch->expr);
if (cmp == NULL)
ShuffleSubstrings(words.words, words.len);
else {
assert(words.words[0].end[0] == '\0');
qsort(words.words, words.len, sizeof(words.words[0]), cmp);
}
Expr_SetValueOwn(ch->expr, SubstringWords_JoinFree(words));
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_IfElse(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
LazyBuf thenBuf;
LazyBuf elseBuf;
VarEvalMode then_emode = VARE_PARSE;
VarEvalMode else_emode = VARE_PARSE;
int parseErrorsBefore = parseErrors;
CondResult cond_rc = CR_TRUE;
if (Expr_ShouldEval(expr)) {
evalStack.elems[evalStack.len - 1].kind = VSK_COND;
cond_rc = Cond_EvalCondition(expr->name);
if (cond_rc == CR_TRUE)
then_emode = expr->emode;
else if (cond_rc == CR_FALSE)
else_emode = expr->emode;
else if (parseErrors == parseErrorsBefore)
Parse_Error(PARSE_FATAL, "Bad condition");
}
evalStack.elems[evalStack.len - 1].kind = VSK_COND_THEN;
(*pp)++;
if (!ParseModifierPart(pp, ':', ':', then_emode,
ch, &thenBuf, NULL, NULL))
return AMR_CLEANUP;
evalStack.elems[evalStack.len - 1].kind = VSK_COND_ELSE;
if (!ParseModifierPart(pp, ch->endc, ch->endc, else_emode,
ch, &elseBuf, NULL, NULL)) {
LazyBuf_Done(&thenBuf);
return AMR_CLEANUP;
}
(*pp)--;
if (cond_rc == CR_ERROR) {
LazyBuf_Done(&thenBuf);
LazyBuf_Done(&elseBuf);
return AMR_CLEANUP;
}
if (!Expr_ShouldEval(expr)) {
LazyBuf_Done(&thenBuf);
LazyBuf_Done(&elseBuf);
} else if (cond_rc == CR_TRUE) {
Expr_SetValue(expr, LazyBuf_DoneGet(&thenBuf));
LazyBuf_Done(&elseBuf);
} else {
LazyBuf_Done(&thenBuf);
Expr_SetValue(expr, LazyBuf_DoneGet(&elseBuf));
}
Expr_Define(expr);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Assign(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
GNode *scope;
FStr val;
LazyBuf buf;
const char *mod = *pp;
const char *op = mod + 1;
if (op[0] == '=')
goto found_op;
if ((op[0] == '+' || op[0] == '?' || op[0] == '!') && op[1] == '=')
goto found_op;
return AMR_UNKNOWN;
found_op:
if (expr->name[0] == '\0') {
const char *value = op[0] == '=' ? op + 1 : op + 2;
*pp = mod + 1;
Parse_Error(PARSE_FATAL,
"Invalid attempt to assign \"%.*s\" to variable \"\" "
"via modifier \":%.*s\"",
(int)strcspn(value, ":)}"), value,
(int)(value - mod), mod);
return AMR_CLEANUP;
}
*pp = mod + (op[0] != '=' ? 3 : 2);
if (!ParseModifierPart(pp, ch->endc, ch->endc, expr->emode,
ch, &buf, NULL, NULL))
return AMR_CLEANUP;
val = LazyBuf_DoneGet(&buf);
(*pp)--;
if (!Expr_ShouldEval(expr))
goto done;
scope = expr->scope;
if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL
&& VarFind(expr->name, expr->scope, false) == NULL)
scope = SCOPE_GLOBAL;
if (op[0] == '+')
Var_Append(scope, expr->name, val.str);
else if (op[0] == '!') {
char *output, *error;
output = Cmd_Exec(val.str, &error);
if (error != NULL) {
Parse_Error(PARSE_WARNING, "%s", error);
free(error);
} else
Var_Set(scope, expr->name, output);
free(output);
} else if (op[0] == '?' && expr->defined == DEF_REGULAR) {
} else
Var_Set(scope, expr->name, val.str);
Expr_SetValueRefer(expr, "");
done:
FStr_Done(&val);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Remember(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
const char *mod = *pp;
FStr name;
if (!ModMatchEq(mod, "_", ch))
return AMR_UNKNOWN;
name = FStr_InitRefer("_");
if (mod[1] == '=') {
const char *arg = mod + 2;
size_t argLen = strcspn(arg, ":)}");
*pp = arg + argLen;
name = FStr_InitOwn(bmake_strldup(arg, argLen));
} else
*pp = mod + 1;
if (Expr_ShouldEval(expr))
Var_Set(SCOPE_GLOBAL, name.str, Expr_Str(expr));
FStr_Done(&name);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_WordFunc(const char **pp, ModChain *ch,
ModifyWordProc modifyWord)
{
if (!IsDelimiter((*pp)[1], ch))
return AMR_UNKNOWN;
(*pp)++;
ModifyWords(ch, modifyWord, NULL, ch->oneBigWord);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_Unique(const char **pp, ModChain *ch)
{
SubstringWords words;
if (!IsDelimiter((*pp)[1], ch))
return AMR_UNKNOWN;
(*pp)++;
if (!ModChain_ShouldEval(ch))
return AMR_OK;
words = Expr_Words(ch->expr);
if (words.len > 1) {
size_t di, si;
for (di = 0, si = 1; si < words.len; si++)
if (!Substring_Eq(words.words[di], words.words[si]))
words.words[++di] = words.words[si];
words.len = di + 1;
}
Expr_SetValueOwn(ch->expr, SubstringWords_JoinFree(words));
return AMR_OK;
}
static bool
IsSysVModifier(const char *p, char startc, char endc)
{
bool eqFound = false;
int depth = 1;
while (*p != '\0') {
if (*p == '=')
eqFound = true;
else if (*p == endc) {
if (--depth == 0)
break;
} else if (*p == startc)
depth++;
p++;
}
return eqFound;
}
static ApplyModifierResult
ApplyModifier_SysV(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
LazyBuf lhsBuf, rhsBuf;
FStr rhs;
struct ModifyWord_SysVSubstArgs args;
Substring lhs;
const char *lhsSuffix;
const char *mod = *pp;
if (!IsSysVModifier(mod, ch->startc, ch->endc))
return AMR_UNKNOWN;
if (!ParseModifierPart(pp, '=', '=', expr->emode,
ch, &lhsBuf, NULL, NULL))
return AMR_CLEANUP;
if (!ParseModifierPart(pp, ch->endc, ch->endc, expr->emode,
ch, &rhsBuf, NULL, NULL)) {
LazyBuf_Done(&lhsBuf);
return AMR_CLEANUP;
}
rhs = LazyBuf_DoneGet(&rhsBuf);
(*pp)--;
if (lhsBuf.len == 0 && Expr_Str(expr)[0] == '\0')
goto done;
lhs = LazyBuf_Get(&lhsBuf);
lhsSuffix = Substring_SkipFirst(lhs, '%');
args.scope = expr->scope;
args.lhsPrefix = Substring_Init(lhs.start,
lhsSuffix != lhs.start ? lhsSuffix - 1 : lhs.start);
args.lhsPercent = lhsSuffix != lhs.start;
args.lhsSuffix = Substring_Init(lhsSuffix, lhs.end);
args.rhs = rhs.str;
ModifyWords(ch, ModifyWord_SysVSubst, &args, ch->oneBigWord);
done:
LazyBuf_Done(&lhsBuf);
FStr_Done(&rhs);
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_SunShell(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
const char *p = *pp;
if (!(p[1] == 'h' && IsDelimiter(p[2], ch)))
return AMR_UNKNOWN;
*pp = p + 2;
if (Expr_ShouldEval(expr)) {
char *output, *error;
output = Cmd_Exec(Expr_Str(expr), &error);
if (error != NULL) {
Parse_Error(PARSE_WARNING, "%s", error);
free(error);
}
Expr_SetValueOwn(expr, output);
}
return AMR_OK;
}
static ApplyModifierResult
ApplyModifier_SunShell1(const char **pp, ModChain *ch)
{
Expr *expr = ch->expr;
const char *p = *pp;
if (!(p[1] == 'h' && p[2] == '1' && IsDelimiter(p[3], ch)))
return AMR_UNKNOWN;
*pp = p + 3;
if (Expr_ShouldEval(expr)) {
char *cache_varname;
Var *v;
cache_varname = str_concat2(".MAKE.SH1.", expr->name);
v = VarFind(cache_varname, SCOPE_GLOBAL, false);
if (v == NULL) {
char *output, *error;
output = Cmd_Exec(Expr_Str(expr), &error);
if (error != NULL) {
Parse_Error(PARSE_WARNING, "%s", error);
free(error);
}
Var_SetWithFlags(SCOPE_GLOBAL, cache_varname, output,
VAR_SET_NO_EXPORT);
Expr_SetValueOwn(expr, output);
} else {
Expr_SetValueRefer(expr, v->val.data);
}
free(cache_varname);
}
return AMR_OK;
}
static bool
ShouldLogInSimpleFormat(const Expr *expr)
{
return (expr->emode == VARE_EVAL
|| expr->emode == VARE_EVAL_DEFINED
|| expr->emode == VARE_EVAL_DEFINED_LOUD)
&& expr->defined == DEF_REGULAR;
}
static void
LogBeforeApply(const ModChain *ch, const char *mod)
{
const Expr *expr = ch->expr;
bool is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], ch);
if (!Expr_ShouldEval(expr)) {
debug_printf("Parsing modifier ${%s:%c%s}\n",
expr->name, mod[0], is_single_char ? "" : "...");
return;
}
if (ShouldLogInSimpleFormat(expr)) {
debug_printf(
"Evaluating modifier ${%s:%c%s} on value \"%s\"\n",
expr->name, mod[0], is_single_char ? "" : "...",
Expr_Str(expr));
return;
}
debug_printf(
"Evaluating modifier ${%s:%c%s} on value \"%s\" (%s, %s)\n",
expr->name, mod[0], is_single_char ? "" : "...", Expr_Str(expr),
VarEvalMode_Name[expr->emode], ExprDefined_Name[expr->defined]);
}
static void
LogAfterApply(const ModChain *ch, const char *p, const char *mod)
{
const Expr *expr = ch->expr;
const char *value = Expr_Str(expr);
if (ShouldLogInSimpleFormat(expr)) {
debug_printf("Result of ${%s:%.*s} is \"%s\"\n",
expr->name, (int)(p - mod), mod, value);
return;
}
debug_printf("Result of ${%s:%.*s} is \"%s\" (%s, %s)\n",
expr->name, (int)(p - mod), mod, value,
VarEvalMode_Name[expr->emode],
ExprDefined_Name[expr->defined]);
}
static ApplyModifierResult
ApplyModifier(const char **pp, ModChain *ch)
{
switch (**pp) {
case '!':
return ApplyModifier_ShellCommand(pp, ch);
case ':':
return ApplyModifier_Assign(pp, ch);
case '?':
return ApplyModifier_IfElse(pp, ch);
case '@':
return ApplyModifier_Loop(pp, ch);
case '[':
return ApplyModifier_Words(pp, ch);
case '_':
return ApplyModifier_Remember(pp, ch);
case 'C':
return ApplyModifier_Regex(pp, ch);
case 'D':
case 'U':
return ApplyModifier_Defined(pp, ch);
case 'E':
return ApplyModifier_WordFunc(pp, ch, ModifyWord_Suffix);
case 'g':
case 'l':
return ApplyModifier_Time(pp, ch);
case 'H':
return ApplyModifier_WordFunc(pp, ch, ModifyWord_Head);
case 'h':
return ApplyModifier_Hash(pp, ch);
case 'L':
return ApplyModifier_Literal(pp, ch);
case 'M':
case 'N':
return ApplyModifier_Match(pp, ch);
case 'm':
return ApplyModifier_Mtime(pp, ch);
case 'O':
return ApplyModifier_Order(pp, ch);
case 'P':
return ApplyModifier_Path(pp, ch);
case 'Q':
case 'q':
return ApplyModifier_Quote(pp, ch);
case 'R':
return ApplyModifier_WordFunc(pp, ch, ModifyWord_Root);
case 'r':
return ApplyModifier_Range(pp, ch);
case 'S':
return ApplyModifier_Subst(pp, ch);
case 's':
if ((*pp)[1] == 'h' && (*pp)[2] == '1')
return ApplyModifier_SunShell1(pp, ch);
return ApplyModifier_SunShell(pp, ch);
case 'T':
return ApplyModifier_WordFunc(pp, ch, ModifyWord_Tail);
case 't':
return ApplyModifier_To(pp, ch);
case 'u':
return ApplyModifier_Unique(pp, ch);
default:
return AMR_UNKNOWN;
}
}
static void ApplyModifiers(Expr *, const char **, char, char);
typedef enum ApplyModifiersIndirectResult {
AMIR_CONTINUE,
AMIR_SYSV,
AMIR_OUT
} ApplyModifiersIndirectResult;
static ApplyModifiersIndirectResult
ApplyModifiersIndirect(ModChain *ch, const char **pp)
{
Expr *expr = ch->expr;
const char *p = *pp;
FStr mods = Var_Parse(&p, expr->scope, expr->emode);
if (mods.str[0] != '\0' && !IsDelimiter(*p, ch)) {
FStr_Done(&mods);
return AMIR_SYSV;
}
DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n",
mods.str, (int)(p - *pp), *pp);
if (ModChain_ShouldEval(ch) && mods.str[0] != '\0') {
const char *modsp = mods.str;
EvalStack_Push(VSK_INDIRECT_MODIFIERS, mods.str, NULL);
ApplyModifiers(expr, &modsp, '\0', '\0');
EvalStack_Pop();
if (Expr_Str(expr) == var_Error || *modsp != '\0') {
FStr_Done(&mods);
*pp = p;
return AMIR_OUT;
}
}
FStr_Done(&mods);
if (*p == ':')
p++;
else if (*p == '\0' && ch->endc != '\0') {
Parse_Error(PARSE_FATAL,
"Unclosed expression after indirect modifier, "
"expecting \"%c\"",
ch->endc);
*pp = p;
return AMIR_OUT;
}
*pp = p;
return AMIR_CONTINUE;
}
static ApplyModifierResult
ApplySingleModifier(const char **pp, ModChain *ch)
{
ApplyModifierResult res;
const char *mod = *pp;
const char *p = *pp;
if (DEBUG(VAR))
LogBeforeApply(ch, mod);
if (posix_state == PS_SET)
res = ApplyModifier_SysV(&p, ch);
else
res = AMR_UNKNOWN;
if (res == AMR_UNKNOWN)
res = ApplyModifier(&p, ch);
if (res == AMR_UNKNOWN && posix_state != PS_SET) {
assert(p == mod);
res = ApplyModifier_SysV(&p, ch);
}
if (res == AMR_UNKNOWN) {
for (p++; !IsDelimiter(*p, ch); p++)
continue;
Parse_Error(PARSE_FATAL, "Unknown modifier \":%.*s\"",
(int)(p - mod), mod);
Expr_SetValueRefer(ch->expr, var_Error);
res = AMR_CLEANUP;
}
if (res != AMR_OK) {
*pp = p;
return res;
}
if (DEBUG(VAR))
LogAfterApply(ch, p, mod);
if (*p == '\0' && ch->endc != '\0') {
Parse_Error(PARSE_FATAL,
"Unclosed expression, expecting \"%c\" for "
"modifier \"%.*s\"",
ch->endc, (int)(p - mod), mod);
} else if (*p == ':') {
p++;
} else if (opts.strict && *p != '\0' && *p != ch->endc) {
Parse_Error(PARSE_FATAL,
"Missing delimiter \":\" after modifier \"%.*s\"",
(int)(p - mod), mod);
}
*pp = p;
return AMR_OK;
}
#if __STDC_VERSION__ >= 199901L
#define ModChain_Init(expr, startc, endc, sep, oneBigWord) \
(ModChain) { expr, startc, endc, sep, oneBigWord }
#else
MAKE_INLINE ModChain
ModChain_Init(Expr *expr, char startc, char endc, char sep, bool oneBigWord)
{
ModChain ch;
ch.expr = expr;
ch.startc = startc;
ch.endc = endc;
ch.sep = sep;
ch.oneBigWord = oneBigWord;
return ch;
}
#endif
static void
ApplyModifiers(
Expr *expr,
const char **pp,
char startc,
char endc
)
{
ModChain ch = ModChain_Init(expr, startc, endc, ' ', false);
const char *p;
assert(startc == '(' || startc == '{' || startc == '\0');
assert(endc == ')' || endc == '}' || endc == '\0');
assert(Expr_Str(expr) != NULL);
p = *pp;
if (*p == '\0' && endc != '\0') {
Parse_Error(PARSE_FATAL,
"Unclosed expression, expecting \"%c\"", ch.endc);
goto cleanup;
}
while (*p != '\0' && *p != endc) {
ApplyModifierResult res;
if (*p == '$') {
ApplyModifiersIndirectResult amir =
ApplyModifiersIndirect(&ch, &p);
if (amir == AMIR_CONTINUE)
continue;
if (amir == AMIR_OUT)
break;
}
res = ApplySingleModifier(&p, &ch);
if (res == AMR_CLEANUP)
goto cleanup;
}
*pp = p;
assert(Expr_Str(expr) != NULL);
return;
cleanup:
*pp = p;
Expr_SetValueRefer(expr, var_Error);
}
static bool
VarnameIsDynamic(Substring varname)
{
const char *name;
size_t len;
name = varname.start;
len = Substring_Length(varname);
if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) {
switch (name[0]) {
case '@':
case '%':
case '*':
case '!':
return true;
}
return false;
}
if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) {
return Substring_Equals(varname, ".TARGET") ||
Substring_Equals(varname, ".ARCHIVE") ||
Substring_Equals(varname, ".PREFIX") ||
Substring_Equals(varname, ".MEMBER");
}
return false;
}
static const char *
UndefinedShortVarValue(char varname, const GNode *scope)
{
if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) {
switch (varname) {
case '@':
return "$(.TARGET)";
case '%':
return "$(.MEMBER)";
case '*':
return "$(.PREFIX)";
case '!':
return "$(.ARCHIVE)";
}
}
return NULL;
}
static void
ParseVarname(const char **pp, char startc, char endc,
GNode *scope, VarEvalMode emode,
LazyBuf *buf)
{
const char *p = *pp;
int depth = 0;
LazyBuf_Init(buf, p);
while (*p != '\0') {
if ((*p == endc || *p == ':') && depth == 0)
break;
if (*p == startc)
depth++;
if (*p == endc)
depth--;
if (*p == '$') {
FStr nested_val = Var_Parse(&p, scope, emode);
LazyBuf_AddStr(buf, nested_val.str);
FStr_Done(&nested_val);
} else {
LazyBuf_Add(buf, *p);
p++;
}
}
*pp = p;
}
static bool
IsShortVarnameValid(char varname, const char *start)
{
if (varname != '$' && varname != ':' && varname != '}' &&
varname != ')' && varname != '\0')
return true;
if (!opts.strict)
return false;
if (varname == '$' && save_dollars)
Parse_Error(PARSE_FATAL,
"To escape a dollar, use \\$, not $$, at \"%s\"", start);
else if (varname == '\0')
Parse_Error(PARSE_FATAL, "Dollar followed by nothing");
else if (save_dollars)
Parse_Error(PARSE_FATAL,
"Invalid variable name \"%c\", at \"%s\"", varname, start);
return false;
}
static bool
ParseVarnameShort(char varname, const char **pp, GNode *scope,
VarEvalMode emode,
const char **out_false_val,
Var **out_true_var)
{
char name[2];
Var *v;
const char *val;
if (!IsShortVarnameValid(varname, *pp)) {
(*pp)++;
*out_false_val = var_Error;
return false;
}
name[0] = varname;
name[1] = '\0';
v = VarFind(name, scope, true);
if (v != NULL) {
*out_true_var = v;
return true;
}
*pp += 2;
val = UndefinedShortVarValue(varname, scope);
if (val == NULL)
val = emode == VARE_EVAL_DEFINED
|| emode == VARE_EVAL_DEFINED_LOUD
? var_Error : varUndefined;
if ((opts.strict || emode == VARE_EVAL_DEFINED_LOUD)
&& val == var_Error) {
Parse_Error(PARSE_FATAL,
"Variable \"%s\" is undefined", name);
}
*out_false_val = val;
return false;
}
static Var *
FindLocalLegacyVar(Substring varname, GNode *scope,
const char **out_extraModifiers)
{
Var *v;
if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL)
return NULL;
if (Substring_Length(varname) != 2)
return NULL;
if (varname.start[1] != 'F' && varname.start[1] != 'D')
return NULL;
if (strchr("@%?*!<>^", varname.start[0]) == NULL)
return NULL;
v = VarFindSubstring(Substring_Init(varname.start, varname.start + 1),
scope, false);
if (v == NULL)
return NULL;
*out_extraModifiers = varname.start[1] == 'D' ? "H:" : "T:";
return v;
}
static FStr
EvalUndefined(bool dynamic, const char *start, const char *p,
Substring varname, VarEvalMode emode, int parseErrorsBefore)
{
if (dynamic)
return FStr_InitOwn(bmake_strsedup(start, p));
if (emode == VARE_EVAL_DEFINED_LOUD
|| (emode == VARE_EVAL_DEFINED && opts.strict)) {
if (parseErrors == parseErrorsBefore) {
Parse_Error(PARSE_FATAL,
"Variable \"%.*s\" is undefined",
(int) Substring_Length(varname), varname.start);
}
return FStr_InitRefer(var_Error);
}
return FStr_InitRefer(
emode == VARE_EVAL_DEFINED_LOUD || emode == VARE_EVAL_DEFINED
? var_Error : varUndefined);
}
static void
CheckVarname(Substring name)
{
const char *p;
for (p = name.start; p < name.end; p++) {
if (ch_isspace(*p))
break;
}
if (p < name.end) {
Parse_Error(PARSE_WARNING,
ch_isprint(*p)
? "Invalid character \"%c\" in variable name \"%.*s\""
: "Invalid character \"\\x%02x\" in variable name \"%.*s\"",
(int)*p,
(int)Substring_Length(name), name.start);
}
}
static bool
ParseVarnameLong(
const char **pp,
char startc,
GNode *scope,
VarEvalMode emode,
VarEvalMode nested_emode,
int parseErrorsBefore,
const char **out_false_pp,
FStr *out_false_val,
char *out_true_endc,
Var **out_true_v,
bool *out_true_haveModifier,
const char **out_true_extraModifiers,
bool *out_true_dynamic,
ExprDefined *out_true_exprDefined
)
{
LazyBuf varname;
Substring name;
Var *v;
bool haveModifier;
bool dynamic = false;
const char *p = *pp;
const char *start = p;
char endc = startc == '(' ? ')' : '}';
p += 2;
ParseVarname(&p, startc, endc, scope, nested_emode, &varname);
name = LazyBuf_Get(&varname);
if (*p == ':')
haveModifier = true;
else if (*p == endc)
haveModifier = false;
else {
Parse_Error(PARSE_FATAL, "Unclosed variable \"%.*s\"",
(int)Substring_Length(name), name.start);
LazyBuf_Done(&varname);
*out_false_pp = p;
*out_false_val = FStr_InitRefer(var_Error);
return false;
}
v = VarFindSubstring(name, scope, true);
if (v == NULL && Substring_Equals(name, ".SUFFIXES")) {
char *suffixes = Suff_NamesStr();
v = VarNew(FStr_InitRefer(".SUFFIXES"), suffixes,
true, false, true);
free(suffixes);
} else if (v == NULL)
v = FindLocalLegacyVar(name, scope, out_true_extraModifiers);
if (v == NULL) {
dynamic = VarnameIsDynamic(name) &&
(scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL);
if (!haveModifier) {
CheckVarname(name);
p++;
*out_false_pp = p;
*out_false_val = EvalUndefined(dynamic, start, p,
name, emode, parseErrorsBefore);
LazyBuf_Done(&varname);
return false;
}
v = VarNew(LazyBuf_DoneGet(&varname), "",
true, false, false);
*out_true_exprDefined = DEF_UNDEF;
} else
LazyBuf_Done(&varname);
*pp = p;
*out_true_endc = endc;
*out_true_v = v;
*out_true_haveModifier = haveModifier;
*out_true_dynamic = dynamic;
return true;
}
#if __STDC_VERSION__ >= 199901L
#define Expr_Init(name, value, emode, scope, defined) \
(Expr) { name, value, emode, scope, defined }
#else
MAKE_INLINE Expr
Expr_Init(const char *name, FStr value,
VarEvalMode emode, GNode *scope, ExprDefined defined)
{
Expr expr;
expr.name = name;
expr.value = value;
expr.emode = emode;
expr.scope = scope;
expr.defined = defined;
return expr;
}
#endif
static bool
Var_Parse_U(const char **pp, VarEvalMode emode, FStr *out_value)
{
const char *p;
p = *pp;
if (!(p[0] == '$' && p[1] == '{' && p[2] == ':' && p[3] == 'U'))
return false;
p += 4;
while (*p != '$' && *p != '{' && *p != ':' && *p != '\\' &&
*p != '}' && *p != '\0')
p++;
if (*p != '}')
return false;
*out_value = emode == VARE_PARSE
? FStr_InitRefer("")
: FStr_InitOwn(bmake_strsedup(*pp + 4, p));
*pp = p + 1;
return true;
}
FStr
Var_Parse(const char **pp, GNode *scope, VarEvalMode emode)
{
const char *start, *p;
bool haveModifier;
char startc;
char endc;
bool dynamic;
const char *extramodifiers;
Var *v;
Expr expr = Expr_Init(NULL, FStr_InitRefer(NULL),
emode == VARE_EVAL_DEFINED || emode == VARE_EVAL_DEFINED_LOUD
? VARE_EVAL : emode,
scope, DEF_REGULAR);
FStr val;
int parseErrorsBefore = parseErrors;
if (Var_Parse_U(pp, emode, &val))
return val;
p = *pp;
start = p;
DEBUG2(VAR, "Var_Parse: %s (%s)\n", start, VarEvalMode_Name[emode]);
val = FStr_InitRefer(NULL);
extramodifiers = NULL;
dynamic = false;
endc = '\0';
startc = p[1];
if (startc != '(' && startc != '{') {
if (!ParseVarnameShort(startc, pp, scope, emode, &val.str, &v))
return val;
haveModifier = false;
p++;
} else {
if (!ParseVarnameLong(&p, startc, scope, emode, expr.emode,
parseErrorsBefore,
pp, &val,
&endc, &v, &haveModifier, &extramodifiers,
&dynamic, &expr.defined))
return val;
}
expr.name = v->name.str;
if (v->inUse && VarEvalMode_ShouldEval(emode)) {
Parse_Error(PARSE_FATAL, "Variable %s is recursive.",
v->name.str);
FStr_Done(&val);
if (*p != '\0')
p++;
*pp = p;
return FStr_InitRefer(var_Error);
}
expr.value = FStr_InitRefer(v->val.data);
if (!VarEvalMode_ShouldEval(emode))
EvalStack_Push(VSK_EXPR_PARSE, start, NULL);
else if (expr.name[0] != '\0')
EvalStack_Push(VSK_VARNAME, expr.name, &expr.value);
else
EvalStack_Push(VSK_EXPR, start, &expr.value);
if (VarEvalMode_ShouldEval(emode) &&
strchr(Expr_Str(&expr), '$') != NULL) {
char *expanded;
v->inUse = true;
expanded = Var_Subst(Expr_Str(&expr), scope, expr.emode);
v->inUse = false;
Expr_SetValueOwn(&expr, expanded);
}
if (extramodifiers != NULL) {
const char *em = extramodifiers;
ApplyModifiers(&expr, &em, '\0', '\0');
}
if (haveModifier) {
p++;
ApplyModifiers(&expr, &p, startc, endc);
}
if (*p != '\0')
p++;
*pp = p;
if (expr.defined == DEF_UNDEF) {
Substring varname = Substring_InitStr(expr.name);
FStr value = EvalUndefined(dynamic, start, p, varname, emode,
parseErrorsBefore);
Expr_SetValue(&expr, value);
}
EvalStack_Pop();
if (v->shortLived) {
if (expr.value.str == v->val.data) {
expr.value.freeIt = v->val.data;
v->val.data = NULL;
}
VarFreeShortLived(v);
}
return expr.value;
}
static void
VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalMode emode)
{
if (save_dollars && VarEvalMode_ShouldKeepDollar(emode))
Buf_AddByte(res, '$');
Buf_AddByte(res, '$');
*pp += 2;
}
static void
VarSubstExpr(const char **pp, Buffer *buf, GNode *scope, VarEvalMode emode)
{
const char *p = *pp;
const char *nested_p = p;
FStr val = Var_Parse(&nested_p, scope, emode);
if (val.str == var_Error || val.str == varUndefined) {
if (!VarEvalMode_ShouldKeepUndef(emode)
|| val.str == var_Error) {
p = nested_p;
} else {
Buf_AddByte(buf, *p);
p++;
}
} else {
p = nested_p;
Buf_AddStr(buf, val.str);
}
FStr_Done(&val);
*pp = p;
}
static void
VarSubstPlain(const char **pp, Buffer *res)
{
const char *p = *pp;
const char *start = p;
for (p++; *p != '$' && *p != '\0'; p++)
continue;
Buf_AddRange(res, start, p);
*pp = p;
}
char *
Var_Subst(const char *str, GNode *scope, VarEvalMode emode)
{
const char *p = str;
Buffer res;
Buf_Init(&res);
while (*p != '\0') {
if (p[0] == '$' && p[1] == '$')
VarSubstDollarDollar(&p, &res, emode);
else if (p[0] == '$')
VarSubstExpr(&p, &res, scope, emode);
else
VarSubstPlain(&p, &res);
}
return Buf_DoneData(&res);
}
char *
Var_SubstInTarget(const char *str, GNode *scope)
{
char *res;
EvalStack_Push(VSK_TARGET, scope->name, NULL);
EvalStack_Push(VSK_COMMAND, str, NULL);
res = Var_Subst(str, scope, VARE_EVAL);
EvalStack_Pop();
EvalStack_Pop();
return res;
}
void
Var_ExportStackTrace(const char *target, const char *cmd)
{
char *stackTrace;
if (GetParentStackTrace() == NULL)
return;
if (target != NULL)
EvalStack_Push(VSK_TARGET, target, NULL);
if (cmd != NULL)
EvalStack_Push(VSK_COMMAND, cmd, NULL);
stackTrace = GetStackTrace(true);
(void)setenv("MAKE_STACK_TRACE", stackTrace, 1);
free(stackTrace);
if (cmd != NULL)
EvalStack_Pop();
if (target != NULL)
EvalStack_Pop();
}
void
Var_Expand(FStr *str, GNode *scope, VarEvalMode emode)
{
char *expanded;
if (strchr(str->str, '$') == NULL)
return;
expanded = Var_Subst(str->str, scope, emode);
FStr_Done(str);
*str = FStr_InitOwn(expanded);
}
void
Var_Stats(void)
{
HashTable_DebugStats(&SCOPE_GLOBAL->vars, "Global variables");
}
static int
StrAsc(const void *sa, const void *sb)
{
return strcmp(*(const char *const *)sa, *(const char *const *)sb);
}
void
Var_Dump(GNode *scope)
{
Vector vec;
HashIter hi;
size_t i;
const char **varnames;
Vector_Init(&vec, sizeof(const char *));
HashIter_Init(&hi, &scope->vars);
while (HashIter_Next(&hi))
*(const char **)Vector_Push(&vec) = hi.entry->key;
varnames = vec.items;
qsort(varnames, vec.len, sizeof varnames[0], StrAsc);
for (i = 0; i < vec.len; i++) {
const char *varname = varnames[i];
const Var *var = HashTable_FindValue(&scope->vars, varname);
debug_printf("%-16s = %s%s\n", varname,
var->val.data, ValueDescription(var->val.data));
}
Vector_Done(&vec);
}