Home | History | Annotate | Line # | Download | only in make
var.c revision 1.1110
      1 /*	$NetBSD: var.c,v 1.1110 2024/05/24 23:02:46 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1989 by Berkeley Softworks
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Adam de Boor.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Handling of variables and the expressions formed from them.
     73  *
     74  * Variables are set using lines of the form VAR=value.  Both the variable
     75  * name and the value can contain references to other variables, by using
     76  * expressions like ${VAR}, ${VAR:Modifiers}, ${${VARNAME}} or ${VAR:${MODS}}.
     77  *
     78  * Interface:
     79  *	Var_Init	Initialize this module.
     80  *
     81  *	Var_End		Clean up the module.
     82  *
     83  *	Var_Set
     84  *	Var_SetExpand	Set the value of the variable, creating it if
     85  *			necessary.
     86  *
     87  *	Var_Append
     88  *	Var_AppendExpand
     89  *			Append more characters to the variable, creating it if
     90  *			necessary. A space is placed between the old value and
     91  *			the new one.
     92  *
     93  *	Var_Exists
     94  *	Var_ExistsExpand
     95  *			See if a variable exists.
     96  *
     97  *	Var_Value	Return the unexpanded value of a variable, or NULL if
     98  *			the variable is undefined.
     99  *
    100  *	Var_Subst	Substitute all expressions in a string.
    101  *
    102  *	Var_Parse	Parse an expression such as ${VAR:Mpattern}.
    103  *
    104  *	Var_Delete	Delete a variable.
    105  *
    106  *	Var_ReexportVars
    107  *			Export some or even all variables to the environment
    108  *			of this process and its child processes.
    109  *
    110  *	Var_Export	Export the variable to the environment of this process
    111  *			and its child processes.
    112  *
    113  *	Var_UnExport	Don't export the variable anymore.
    114  *
    115  * Debugging:
    116  *	Var_Stats	Print out hashing statistics if in -dh mode.
    117  *
    118  *	Var_Dump	Print out all variables defined in the given scope.
    119  */
    120 
    121 #include <sys/stat.h>
    122 #include <sys/types.h>
    123 #include <regex.h>
    124 #include <errno.h>
    125 #include <inttypes.h>
    126 #include <limits.h>
    127 #include <time.h>
    128 
    129 #include "make.h"
    130 #include "dir.h"
    131 #include "job.h"
    132 #include "metachar.h"
    133 
    134 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
    135 MAKE_RCSID("$NetBSD: var.c,v 1.1110 2024/05/24 23:02:46 rillig Exp $");
    136 
    137 /*
    138  * Variables are defined using one of the VAR=value assignments.  Their
    139  * value can be queried by expressions such as $V, ${VAR}, or with modifiers
    140  * such as ${VAR:S,from,to,g:Q}.
    141  *
    142  * There are 3 kinds of variables: scope variables, environment variables,
    143  * undefined variables.
    144  *
    145  * Scope variables are stored in GNode.vars.  The only way to undefine
    146  * a scope variable is using the .undef directive.  In particular, it must
    147  * not be possible to undefine a variable during the evaluation of an
    148  * expression, or Var.name might point nowhere.  (There is another,
    149  * unintended way to undefine a scope variable, see varmod-loop-delete.mk.)
    150  *
    151  * Environment variables are short-lived.  They are returned by VarFind, and
    152  * after using them, they must be freed using VarFreeShortLived.
    153  *
    154  * Undefined variables occur during evaluation of expressions such
    155  * as ${UNDEF:Ufallback} in Var_Parse and ApplyModifiers.
    156  */
    157 typedef struct Var {
    158 	/*
    159 	 * The name of the variable, once set, doesn't change anymore.
    160 	 * For scope variables, it aliases the corresponding HashEntry name.
    161 	 * For environment and undefined variables, it is allocated.
    162 	 */
    163 	FStr name;
    164 
    165 	/* The unexpanded value of the variable. */
    166 	Buffer val;
    167 
    168 	/* The variable came from the command line. */
    169 	bool fromCmd:1;
    170 
    171 	/*
    172 	 * The variable is short-lived.
    173 	 * These variables are not registered in any GNode, therefore they
    174 	 * must be freed after use.
    175 	 */
    176 	bool shortLived:1;
    177 
    178 	/*
    179 	 * The variable comes from the environment.
    180 	 * Appending to its value depends on the scope, see var-op-append.mk.
    181 	 */
    182 	bool fromEnvironment:1;
    183 
    184 	/*
    185 	 * The variable value cannot be changed anymore, and the variable
    186 	 * cannot be deleted.  Any attempts to do so are silently ignored,
    187 	 * they are logged with -dv though.
    188 	 * Use .[NO]READONLY: to adjust.
    189 	 *
    190 	 * See VAR_SET_READONLY.
    191 	 */
    192 	bool readOnly:1;
    193 
    194 	/*
    195 	 * The variable is currently being accessed by Var_Parse or Var_Subst.
    196 	 * This temporary marker is used to avoid endless recursion.
    197 	 */
    198 	bool inUse:1;
    199 
    200 	/*
    201 	 * The variable is exported to the environment, to be used by child
    202 	 * processes.
    203 	 */
    204 	bool exported:1;
    205 
    206 	/*
    207 	 * At the point where this variable was exported, it contained an
    208 	 * unresolved reference to another variable.  Before any child
    209 	 * process is started, it needs to be actually exported, resolving
    210 	 * the referenced variable just in time.
    211 	 */
    212 	bool reexport:1;
    213 } Var;
    214 
    215 /*
    216  * Exporting variables is expensive and may leak memory, so skip it if we
    217  * can.
    218  */
    219 typedef enum VarExportedMode {
    220 	VAR_EXPORTED_NONE,
    221 	VAR_EXPORTED_SOME,
    222 	VAR_EXPORTED_ALL
    223 } VarExportedMode;
    224 
    225 typedef enum UnexportWhat {
    226 	/* Unexport the variables given by name. */
    227 	UNEXPORT_NAMED,
    228 	/*
    229 	 * Unexport all globals previously exported, but keep the environment
    230 	 * inherited from the parent.
    231 	 */
    232 	UNEXPORT_ALL,
    233 	/*
    234 	 * Unexport all globals previously exported and clear the environment
    235 	 * inherited from the parent.
    236 	 */
    237 	UNEXPORT_ENV
    238 } UnexportWhat;
    239 
    240 /* Flags for pattern matching in the :S and :C modifiers */
    241 typedef struct PatternFlags {
    242 	bool subGlobal:1;	/* 'g': replace as often as possible */
    243 	bool subOnce:1;		/* '1': replace only once */
    244 	bool anchorStart:1;	/* '^': match only at start of word */
    245 	bool anchorEnd:1;	/* '$': match only at end of word */
    246 } PatternFlags;
    247 
    248 /* SepBuf builds a string from words interleaved with separators. */
    249 typedef struct SepBuf {
    250 	Buffer buf;
    251 	bool needSep;
    252 	/* Usually ' ', but see the ':ts' modifier. */
    253 	char sep;
    254 } SepBuf;
    255 
    256 typedef struct {
    257 	const char *target;
    258 	const char *varname;
    259 	const char *expr;
    260 } EvalStackElement;
    261 
    262 typedef struct {
    263 	EvalStackElement *elems;
    264 	size_t len;
    265 	size_t cap;
    266 	Buffer details;
    267 } EvalStack;
    268 
    269 /* Whether we have replaced the original environ (which we cannot free). */
    270 char **savedEnv = NULL;
    271 
    272 /*
    273  * Special return value for Var_Parse, indicating a parse error.  It may be
    274  * caused by an undefined variable, a syntax error in a modifier or
    275  * something entirely different.
    276  */
    277 char var_Error[] = "";
    278 
    279 /*
    280  * Special return value for Var_Parse, indicating an undefined variable in
    281  * a case where VARE_UNDEFERR is not set.  This undefined variable is
    282  * typically a dynamic variable such as ${.TARGET}, whose expansion needs to
    283  * be deferred until it is defined in an actual target.
    284  *
    285  * See VARE_EVAL_KEEP_UNDEF.
    286  */
    287 static char varUndefined[] = "";
    288 
    289 /*
    290  * Traditionally this make consumed $$ during := like any other expansion.
    291  * Other make's do not, and this make follows straight since 2016-01-09.
    292  *
    293  * This knob allows controlling the behavior:
    294  *	false to consume $$ during := assignment.
    295  *	true to preserve $$ during := assignment.
    296  */
    297 #define MAKE_SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
    298 static bool save_dollars = true;
    299 
    300 /*
    301  * A scope collects variable names and their values.
    302  *
    303  * The main scope is SCOPE_GLOBAL, which contains the variables that are set
    304  * in the makefiles.  SCOPE_INTERNAL acts as a fallback for SCOPE_GLOBAL and
    305  * contains some internal make variables.  These internal variables can thus
    306  * be overridden, they can also be restored by undefining the overriding
    307  * variable.
    308  *
    309  * SCOPE_CMDLINE contains variables from the command line arguments.  These
    310  * override variables from SCOPE_GLOBAL.
    311  *
    312  * There is no scope for environment variables, these are generated on-the-fly
    313  * whenever they are referenced.
    314  *
    315  * Each target has its own scope, containing the 7 target-local variables
    316  * .TARGET, .ALLSRC, etc.  Variables set on dependency lines also go in
    317  * this scope.
    318  */
    319 
    320 GNode *SCOPE_CMDLINE;
    321 GNode *SCOPE_GLOBAL;
    322 GNode *SCOPE_INTERNAL;
    323 
    324 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
    325 
    326 static const char VarEvalMode_Name[][32] = {
    327 	"parse-only",
    328 	"parse-balanced",
    329 	"eval",
    330 	"eval-defined",
    331 	"eval-keep-dollar",
    332 	"eval-keep-undefined",
    333 	"eval-keep-dollar-and-undefined",
    334 };
    335 
    336 static EvalStack evalStack;
    337 
    338 
    339 void
    340 EvalStack_Push(const char *target, const char *expr, const char *varname)
    341 {
    342 	if (evalStack.len >= evalStack.cap) {
    343 		evalStack.cap = 16 + 2 * evalStack.cap;
    344 		evalStack.elems = bmake_realloc(evalStack.elems,
    345 		    evalStack.cap * sizeof(*evalStack.elems));
    346 	}
    347 	evalStack.elems[evalStack.len].target = target;
    348 	evalStack.elems[evalStack.len].expr = expr;
    349 	evalStack.elems[evalStack.len].varname = varname;
    350 	evalStack.len++;
    351 }
    352 
    353 void
    354 EvalStack_Pop(void)
    355 {
    356 	assert(evalStack.len > 0);
    357 	evalStack.len--;
    358 }
    359 
    360 const char *
    361 EvalStack_Details(void)
    362 {
    363 	size_t i;
    364 	Buffer *buf = &evalStack.details;
    365 
    366 
    367 	buf->len = 0;
    368 	for (i = 0; i < evalStack.len; i++) {
    369 		EvalStackElement *elem = evalStack.elems + i;
    370 		if (elem->target != NULL) {
    371 			Buf_AddStr(buf, "in target \"");
    372 			Buf_AddStr(buf, elem->target);
    373 			Buf_AddStr(buf, "\": ");
    374 		}
    375 		if (elem->expr != NULL) {
    376 			Buf_AddStr(buf, "while evaluating \"");
    377 			Buf_AddStr(buf, elem->expr);
    378 			Buf_AddStr(buf, "\": ");
    379 		}
    380 		if (elem->varname != NULL) {
    381 			Buf_AddStr(buf, "while evaluating variable \"");
    382 			Buf_AddStr(buf, elem->varname);
    383 			Buf_AddStr(buf, "\": ");
    384 		}
    385 	}
    386 	return buf->len > 0 ? buf->data : "";
    387 }
    388 
    389 static Var *
    390 VarNew(FStr name, const char *value,
    391        bool shortLived, bool fromEnvironment, bool readOnly)
    392 {
    393 	size_t value_len = strlen(value);
    394 	Var *var = bmake_malloc(sizeof *var);
    395 	var->name = name;
    396 	Buf_InitSize(&var->val, value_len + 1);
    397 	Buf_AddBytes(&var->val, value, value_len);
    398 	var->fromCmd = false;
    399 	var->shortLived = shortLived;
    400 	var->fromEnvironment = fromEnvironment;
    401 	var->readOnly = readOnly;
    402 	var->inUse = false;
    403 	var->exported = false;
    404 	var->reexport = false;
    405 	return var;
    406 }
    407 
    408 static Substring
    409 CanonicalVarname(Substring name)
    410 {
    411 
    412 	if (!(Substring_Length(name) > 0 && name.start[0] == '.'))
    413 		return name;
    414 
    415 	if (Substring_Equals(name, ".ALLSRC"))
    416 		return Substring_InitStr(ALLSRC);
    417 	if (Substring_Equals(name, ".ARCHIVE"))
    418 		return Substring_InitStr(ARCHIVE);
    419 	if (Substring_Equals(name, ".IMPSRC"))
    420 		return Substring_InitStr(IMPSRC);
    421 	if (Substring_Equals(name, ".MEMBER"))
    422 		return Substring_InitStr(MEMBER);
    423 	if (Substring_Equals(name, ".OODATE"))
    424 		return Substring_InitStr(OODATE);
    425 	if (Substring_Equals(name, ".PREFIX"))
    426 		return Substring_InitStr(PREFIX);
    427 	if (Substring_Equals(name, ".TARGET"))
    428 		return Substring_InitStr(TARGET);
    429 
    430 	/* GNU make has an additional alias $^ == ${.ALLSRC}. */
    431 
    432 	if (Substring_Equals(name, ".SHELL") && shellPath == NULL)
    433 		Shell_Init();
    434 
    435 	return name;
    436 }
    437 
    438 static Var *
    439 GNode_FindVar(GNode *scope, Substring varname, unsigned int hash)
    440 {
    441 	return HashTable_FindValueBySubstringHash(&scope->vars, varname, hash);
    442 }
    443 
    444 /*
    445  * Find the variable in the scope, and maybe in other scopes as well.
    446  *
    447  * Input:
    448  *	name		name to find, is not expanded any further
    449  *	scope		scope in which to look first
    450  *	elsewhere	true to look in other scopes as well
    451  *
    452  * Results:
    453  *	The found variable, or NULL if the variable does not exist.
    454  *	If the variable is short-lived (such as environment variables), it
    455  *	must be freed using VarFreeShortLived after use.
    456  */
    457 static Var *
    458 VarFindSubstring(Substring name, GNode *scope, bool elsewhere)
    459 {
    460 	Var *var;
    461 	unsigned int nameHash;
    462 
    463 	/* Replace '.TARGET' with '@', likewise for other local variables. */
    464 	name = CanonicalVarname(name);
    465 	nameHash = Hash_Substring(name);
    466 
    467 	var = GNode_FindVar(scope, name, nameHash);
    468 	if (!elsewhere)
    469 		return var;
    470 
    471 	if (var == NULL && scope != SCOPE_CMDLINE)
    472 		var = GNode_FindVar(SCOPE_CMDLINE, name, nameHash);
    473 
    474 	if (!opts.checkEnvFirst && var == NULL && scope != SCOPE_GLOBAL) {
    475 		var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
    476 		if (var == NULL && scope != SCOPE_INTERNAL) {
    477 			/* SCOPE_INTERNAL is subordinate to SCOPE_GLOBAL */
    478 			var = GNode_FindVar(SCOPE_INTERNAL, name, nameHash);
    479 		}
    480 	}
    481 
    482 	if (var == NULL) {
    483 		FStr envName = Substring_Str(name);
    484 		const char *envValue = getenv(envName.str);
    485 		if (envValue != NULL)
    486 			return VarNew(envName, envValue, true, true, false);
    487 		FStr_Done(&envName);
    488 
    489 		if (opts.checkEnvFirst && scope != SCOPE_GLOBAL) {
    490 			var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
    491 			if (var == NULL && scope != SCOPE_INTERNAL)
    492 				var = GNode_FindVar(SCOPE_INTERNAL, name,
    493 				    nameHash);
    494 			return var;
    495 		}
    496 
    497 		return NULL;
    498 	}
    499 
    500 	return var;
    501 }
    502 
    503 static Var *
    504 VarFind(const char *name, GNode *scope, bool elsewhere)
    505 {
    506 	return VarFindSubstring(Substring_InitStr(name), scope, elsewhere);
    507 }
    508 
    509 /* If the variable is short-lived, free it, including its value. */
    510 static void
    511 VarFreeShortLived(Var *v)
    512 {
    513 	if (!v->shortLived)
    514 		return;
    515 
    516 	FStr_Done(&v->name);
    517 	Buf_Done(&v->val);
    518 	free(v);
    519 }
    520 
    521 static const char *
    522 ValueDescription(const char *value)
    523 {
    524 	if (value[0] == '\0')
    525 		return "# (empty)";
    526 	if (ch_isspace(value[strlen(value) - 1]))
    527 		return "# (ends with space)";
    528 	return "";
    529 }
    530 
    531 /* Add a new variable of the given name and value to the given scope. */
    532 static Var *
    533 VarAdd(const char *name, const char *value, GNode *scope, VarSetFlags flags)
    534 {
    535 	HashEntry *he = HashTable_CreateEntry(&scope->vars, name, NULL);
    536 	Var *v = VarNew(FStr_InitRefer(/* aliased to */ he->key), value,
    537 	    false, false, (flags & VAR_SET_READONLY) != 0);
    538 	HashEntry_Set(he, v);
    539 	DEBUG4(VAR, "%s: %s = %s%s\n",
    540 	    scope->name, name, value, ValueDescription(value));
    541 	return v;
    542 }
    543 
    544 /*
    545  * Remove a variable from a scope, freeing all related memory as well.
    546  * The variable name is kept as-is, it is not expanded.
    547  */
    548 void
    549 Var_Delete(GNode *scope, const char *varname)
    550 {
    551 	HashEntry *he = HashTable_FindEntry(&scope->vars, varname);
    552 	Var *v;
    553 
    554 	if (he == NULL) {
    555 		DEBUG2(VAR, "%s: ignoring delete '%s' as it is not found\n",
    556 		    scope->name, varname);
    557 		return;
    558 	}
    559 
    560 	v = he->value;
    561 	if (v->readOnly) {
    562 		DEBUG2(VAR, "%s: ignoring delete '%s' as it is read-only\n",
    563 		    scope->name, varname);
    564 		return;
    565 	}
    566 	if (v->inUse) {
    567 		Parse_Error(PARSE_FATAL,
    568 		    "Cannot delete variable \"%s\" while it is used",
    569 		    v->name.str);
    570 		return;
    571 	}
    572 
    573 	DEBUG2(VAR, "%s: delete %s\n", scope->name, varname);
    574 	if (v->exported)
    575 		unsetenv(v->name.str);
    576 	if (strcmp(v->name.str, ".MAKE.EXPORTED") == 0)
    577 		var_exportedVars = VAR_EXPORTED_NONE;
    578 
    579 	assert(v->name.freeIt == NULL);
    580 	HashTable_DeleteEntry(&scope->vars, he);
    581 	Buf_Done(&v->val);
    582 	free(v);
    583 }
    584 
    585 /*
    586  * Undefine one or more variables from the global scope.
    587  * The argument is expanded exactly once and then split into words.
    588  */
    589 void
    590 Var_Undef(const char *arg)
    591 {
    592 	char *expanded;
    593 	Words varnames;
    594 	size_t i;
    595 
    596 	if (arg[0] == '\0') {
    597 		Parse_Error(PARSE_FATAL,
    598 		    "The .undef directive requires an argument");
    599 		return;
    600 	}
    601 
    602 	expanded = Var_Subst(arg, SCOPE_GLOBAL, VARE_WANTRES);
    603 	if (expanded == var_Error) {
    604 		/* TODO: Make this part of the code reachable. */
    605 		Parse_Error(PARSE_FATAL,
    606 		    "Error in variable names to be undefined");
    607 		return;
    608 	}
    609 
    610 	varnames = Str_Words(expanded, false);
    611 	if (varnames.len == 1 && varnames.words[0][0] == '\0')
    612 		varnames.len = 0;
    613 
    614 	for (i = 0; i < varnames.len; i++) {
    615 		const char *varname = varnames.words[i];
    616 		Global_Delete(varname);
    617 	}
    618 
    619 	Words_Free(varnames);
    620 	free(expanded);
    621 }
    622 
    623 static bool
    624 MayExport(const char *name)
    625 {
    626 	if (name[0] == '.')
    627 		return false;	/* skip internals */
    628 	if (name[0] == '-')
    629 		return false;	/* skip misnamed variables */
    630 	if (name[1] == '\0') {
    631 		/*
    632 		 * A single char.
    633 		 * If it is one of the variables that should only appear in
    634 		 * local scope, skip it, else we can get Var_Subst
    635 		 * into a loop.
    636 		 */
    637 		switch (name[0]) {
    638 		case '@':
    639 		case '%':
    640 		case '*':
    641 		case '!':
    642 			return false;
    643 		}
    644 	}
    645 	return true;
    646 }
    647 
    648 static bool
    649 ExportVarEnv(Var *v, GNode *scope)
    650 {
    651 	const char *name = v->name.str;
    652 	char *val = v->val.data;
    653 	char *expr;
    654 
    655 	if (v->exported && !v->reexport)
    656 		return false;	/* nothing to do */
    657 
    658 	if (strchr(val, '$') == NULL) {
    659 		if (!v->exported)
    660 			setenv(name, val, 1);
    661 		return true;
    662 	}
    663 
    664 	if (v->inUse)
    665 		return false;	/* see EMPTY_SHELL in directive-export.mk */
    666 
    667 	/* XXX: name is injected without escaping it */
    668 	expr = str_concat3("${", name, "}");
    669 	val = Var_Subst(expr, scope, VARE_WANTRES);
    670 	if (scope != SCOPE_GLOBAL) {
    671 		/* we will need to re-export the global version */
    672 		v = VarFind(name, SCOPE_GLOBAL, false);
    673 		if (v != NULL)
    674 			v->exported = false;
    675 	}
    676 	/* TODO: handle errors */
    677 	setenv(name, val, 1);
    678 	free(val);
    679 	free(expr);
    680 	return true;
    681 }
    682 
    683 static bool
    684 ExportVarPlain(Var *v)
    685 {
    686 	if (strchr(v->val.data, '$') == NULL) {
    687 		setenv(v->name.str, v->val.data, 1);
    688 		v->exported = true;
    689 		v->reexport = false;
    690 		return true;
    691 	}
    692 
    693 	/*
    694 	 * Flag the variable as something we need to re-export.
    695 	 * No point actually exporting it now though,
    696 	 * the child process can do it at the last minute.
    697 	 * Avoid calling setenv more often than necessary since it can leak.
    698 	 */
    699 	v->exported = true;
    700 	v->reexport = true;
    701 	return true;
    702 }
    703 
    704 static bool
    705 ExportVarLiteral(Var *v)
    706 {
    707 	if (v->exported && !v->reexport)
    708 		return false;
    709 
    710 	if (!v->exported)
    711 		setenv(v->name.str, v->val.data, 1);
    712 
    713 	return true;
    714 }
    715 
    716 /*
    717  * Mark a single variable to be exported later for subprocesses.
    718  *
    719  * Internal variables are not exported.
    720  */
    721 static bool
    722 ExportVar(const char *name, GNode *scope, VarExportMode mode)
    723 {
    724 	Var *v;
    725 
    726 	if (!MayExport(name))
    727 		return false;
    728 
    729 	v = VarFind(name, scope, false);
    730 	if (v == NULL && scope != SCOPE_GLOBAL)
    731 		v = VarFind(name, SCOPE_GLOBAL, false);
    732 	if (v == NULL)
    733 		return false;
    734 
    735 	if (mode == VEM_ENV)
    736 		return ExportVarEnv(v, scope);
    737 	else if (mode == VEM_PLAIN)
    738 		return ExportVarPlain(v);
    739 	else
    740 		return ExportVarLiteral(v);
    741 }
    742 
    743 /*
    744  * Actually export the variables that have been marked as needing to be
    745  * re-exported.
    746  */
    747 void
    748 Var_ReexportVars(GNode *scope)
    749 {
    750 	char *xvarnames;
    751 
    752 	/*
    753 	 * Several make implementations support this sort of mechanism for
    754 	 * tracking recursion - but each uses a different name.
    755 	 * We allow the makefiles to update MAKELEVEL and ensure
    756 	 * children see a correctly incremented value.
    757 	 */
    758 	char level_buf[21];
    759 	snprintf(level_buf, sizeof level_buf, "%d", makelevel + 1);
    760 	setenv(MAKE_LEVEL_ENV, level_buf, 1);
    761 
    762 	if (var_exportedVars == VAR_EXPORTED_NONE)
    763 		return;
    764 
    765 	if (var_exportedVars == VAR_EXPORTED_ALL) {
    766 		HashIter hi;
    767 
    768 		/* Ouch! Exporting all variables at once is crazy. */
    769 		HashIter_Init(&hi, &SCOPE_GLOBAL->vars);
    770 		while (HashIter_Next(&hi) != NULL) {
    771 			Var *var = hi.entry->value;
    772 			ExportVar(var->name.str, scope, VEM_ENV);
    773 		}
    774 		return;
    775 	}
    776 
    777 	xvarnames = Var_Subst("${.MAKE.EXPORTED:O:u}", SCOPE_GLOBAL,
    778 	    VARE_WANTRES);
    779 	/* TODO: handle errors */
    780 	if (xvarnames[0] != '\0') {
    781 		Words varnames = Str_Words(xvarnames, false);
    782 		size_t i;
    783 
    784 		for (i = 0; i < varnames.len; i++)
    785 			ExportVar(varnames.words[i], scope, VEM_ENV);
    786 		Words_Free(varnames);
    787 	}
    788 	free(xvarnames);
    789 }
    790 
    791 static void
    792 ExportVars(const char *varnames, bool isExport, VarExportMode mode)
    793 /* TODO: try to combine the parameters 'isExport' and 'mode'. */
    794 {
    795 	Words words = Str_Words(varnames, false);
    796 	size_t i;
    797 
    798 	if (words.len == 1 && words.words[0][0] == '\0')
    799 		words.len = 0;
    800 
    801 	for (i = 0; i < words.len; i++) {
    802 		const char *varname = words.words[i];
    803 		if (!ExportVar(varname, SCOPE_GLOBAL, mode))
    804 			continue;
    805 
    806 		if (var_exportedVars == VAR_EXPORTED_NONE)
    807 			var_exportedVars = VAR_EXPORTED_SOME;
    808 
    809 		if (isExport && mode == VEM_PLAIN)
    810 			Global_Append(".MAKE.EXPORTED", varname);
    811 	}
    812 	Words_Free(words);
    813 }
    814 
    815 static void
    816 ExportVarsExpand(const char *uvarnames, bool isExport, VarExportMode mode)
    817 {
    818 	char *xvarnames = Var_Subst(uvarnames, SCOPE_GLOBAL, VARE_WANTRES);
    819 	/* TODO: handle errors */
    820 	ExportVars(xvarnames, isExport, mode);
    821 	free(xvarnames);
    822 }
    823 
    824 /* Export the named variables, or all variables. */
    825 void
    826 Var_Export(VarExportMode mode, const char *varnames)
    827 {
    828 	if (mode == VEM_PLAIN && varnames[0] == '\0') {
    829 		var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
    830 		return;
    831 	}
    832 
    833 	ExportVarsExpand(varnames, true, mode);
    834 }
    835 
    836 void
    837 Var_ExportVars(const char *varnames)
    838 {
    839 	ExportVarsExpand(varnames, false, VEM_PLAIN);
    840 }
    841 
    842 
    843 static void
    844 ClearEnv(void)
    845 {
    846 	const char *level;
    847 	char **newenv;
    848 
    849 	level = getenv(MAKE_LEVEL_ENV);	/* we should preserve this */
    850 	if (environ == savedEnv) {
    851 		/* we have been here before! */
    852 		newenv = bmake_realloc(environ, 2 * sizeof(char *));
    853 	} else {
    854 		if (savedEnv != NULL) {
    855 			free(savedEnv);
    856 			savedEnv = NULL;
    857 		}
    858 		newenv = bmake_malloc(2 * sizeof(char *));
    859 	}
    860 
    861 	/* Note: we cannot safely free() the original environ. */
    862 	environ = savedEnv = newenv;
    863 	newenv[0] = NULL;
    864 	newenv[1] = NULL;
    865 	if (level != NULL && *level != '\0')
    866 		setenv(MAKE_LEVEL_ENV, level, 1);
    867 }
    868 
    869 static void
    870 GetVarnamesToUnexport(bool isEnv, const char *arg,
    871 		      FStr *out_varnames, UnexportWhat *out_what)
    872 {
    873 	UnexportWhat what;
    874 	FStr varnames = FStr_InitRefer("");
    875 
    876 	if (isEnv) {
    877 		if (arg[0] != '\0') {
    878 			Parse_Error(PARSE_FATAL,
    879 			    "The directive .unexport-env does not take "
    880 			    "arguments");
    881 			/* continue anyway */
    882 		}
    883 		what = UNEXPORT_ENV;
    884 
    885 	} else {
    886 		what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL;
    887 		if (what == UNEXPORT_NAMED)
    888 			varnames = FStr_InitRefer(arg);
    889 	}
    890 
    891 	if (what != UNEXPORT_NAMED) {
    892 		char *expanded = Var_Subst("${.MAKE.EXPORTED:O:u}",
    893 		    SCOPE_GLOBAL, VARE_WANTRES);
    894 		/* TODO: handle errors */
    895 		varnames = FStr_InitOwn(expanded);
    896 	}
    897 
    898 	*out_varnames = varnames;
    899 	*out_what = what;
    900 }
    901 
    902 static void
    903 UnexportVar(Substring varname, UnexportWhat what)
    904 {
    905 	Var *v = VarFindSubstring(varname, SCOPE_GLOBAL, false);
    906 	if (v == NULL) {
    907 		DEBUG2(VAR, "Not unexporting \"%.*s\" (not found)\n",
    908 		    (int)Substring_Length(varname), varname.start);
    909 		return;
    910 	}
    911 
    912 	DEBUG2(VAR, "Unexporting \"%.*s\"\n",
    913 	    (int)Substring_Length(varname), varname.start);
    914 	if (what != UNEXPORT_ENV && v->exported && !v->reexport)
    915 		unsetenv(v->name.str);
    916 	v->exported = false;
    917 	v->reexport = false;
    918 
    919 	if (what == UNEXPORT_NAMED) {
    920 		/* Remove the variable names from .MAKE.EXPORTED. */
    921 		/* XXX: v->name is injected without escaping it */
    922 		char *expr = str_concat3(
    923 		    "${.MAKE.EXPORTED:N", v->name.str, "}");
    924 		char *filtered = Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES);
    925 		/* TODO: handle errors */
    926 		Global_Set(".MAKE.EXPORTED", filtered);
    927 		free(filtered);
    928 		free(expr);
    929 	}
    930 }
    931 
    932 static void
    933 UnexportVars(FStr *varnames, UnexportWhat what)
    934 {
    935 	size_t i;
    936 	SubstringWords words;
    937 
    938 	if (what == UNEXPORT_ENV)
    939 		ClearEnv();
    940 
    941 	words = Substring_Words(varnames->str, false);
    942 	for (i = 0; i < words.len; i++)
    943 		UnexportVar(words.words[i], what);
    944 	SubstringWords_Free(words);
    945 
    946 	if (what != UNEXPORT_NAMED)
    947 		Global_Delete(".MAKE.EXPORTED");
    948 }
    949 
    950 /* Handle the .unexport and .unexport-env directives. */
    951 void
    952 Var_UnExport(bool isEnv, const char *arg)
    953 {
    954 	UnexportWhat what;
    955 	FStr varnames;
    956 
    957 	GetVarnamesToUnexport(isEnv, arg, &varnames, &what);
    958 	UnexportVars(&varnames, what);
    959 	FStr_Done(&varnames);
    960 }
    961 
    962 /* Set the variable to the value; the name is not expanded. */
    963 void
    964 Var_SetWithFlags(GNode *scope, const char *name, const char *val,
    965 		 VarSetFlags flags)
    966 {
    967 	Var *v;
    968 
    969 	assert(val != NULL);
    970 	if (name[0] == '\0') {
    971 		DEBUG3(VAR,
    972 		    "%s: ignoring '%s = %s' as the variable name is empty\n",
    973 		    scope->name, name, val);
    974 		return;
    975 	}
    976 
    977 	if (scope == SCOPE_GLOBAL
    978 	    && VarFind(name, SCOPE_CMDLINE, false) != NULL) {
    979 		/*
    980 		 * The global variable would not be visible anywhere.
    981 		 * Therefore, there is no point in setting it at all.
    982 		 */
    983 		DEBUG3(VAR,
    984 		    "%s: ignoring '%s = %s' "
    985 		    "due to a command line variable of the same name\n",
    986 		    scope->name, name, val);
    987 		return;
    988 	}
    989 
    990 	/*
    991 	 * Only look for a variable in the given scope since anything set
    992 	 * here will override anything in a lower scope, so there's not much
    993 	 * point in searching them all.
    994 	 */
    995 	v = VarFind(name, scope, false);
    996 	if (v == NULL) {
    997 		if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) {
    998 			/*
    999 			 * This variable would normally prevent the same name
   1000 			 * being added to SCOPE_GLOBAL, so delete it from
   1001 			 * there if needed. Otherwise -V name may show the
   1002 			 * wrong value.
   1003 			 *
   1004 			 * See ExistsInCmdline.
   1005 			 */
   1006 			Var_Delete(SCOPE_GLOBAL, name);
   1007 		}
   1008 		if (strcmp(name, ".SUFFIXES") == 0) {
   1009 			/* special: treat as read-only */
   1010 			DEBUG3(VAR,
   1011 			    "%s: ignoring '%s = %s' as it is read-only\n",
   1012 			    scope->name, name, val);
   1013 			return;
   1014 		}
   1015 		v = VarAdd(name, val, scope, flags);
   1016 	} else {
   1017 		if (v->readOnly && !(flags & VAR_SET_READONLY)) {
   1018 			DEBUG3(VAR,
   1019 			    "%s: ignoring '%s = %s' as it is read-only\n",
   1020 			    scope->name, name, val);
   1021 			return;
   1022 		}
   1023 		Buf_Clear(&v->val);
   1024 		Buf_AddStr(&v->val, val);
   1025 
   1026 		DEBUG4(VAR, "%s: %s = %s%s\n",
   1027 		    scope->name, name, val, ValueDescription(val));
   1028 		if (v->exported)
   1029 			ExportVar(name, scope, VEM_PLAIN);
   1030 	}
   1031 
   1032 	if (scope == SCOPE_CMDLINE) {
   1033 		v->fromCmd = true;
   1034 
   1035 		/*
   1036 		 * Any variables given on the command line are automatically
   1037 		 * exported to the environment (as per POSIX standard), except
   1038 		 * for internals.
   1039 		 */
   1040 		if (!(flags & VAR_SET_NO_EXPORT)) {
   1041 
   1042 			/*
   1043 			 * If requested, don't export these in the
   1044 			 * environment individually.  We still put
   1045 			 * them in .MAKEOVERRIDES so that the
   1046 			 * command-line settings continue to override
   1047 			 * Makefile settings.
   1048 			 */
   1049 			if (!opts.varNoExportEnv && name[0] != '.')
   1050 				setenv(name, val, 1);
   1051 
   1052 			if (!(flags & VAR_SET_INTERNAL))
   1053 				Global_Append(".MAKEOVERRIDES", name);
   1054 		}
   1055 	}
   1056 
   1057 	if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0)
   1058 		save_dollars = ParseBoolean(val, save_dollars);
   1059 
   1060 	if (v != NULL)
   1061 		VarFreeShortLived(v);
   1062 }
   1063 
   1064 void
   1065 Var_Set(GNode *scope, const char *name, const char *val)
   1066 {
   1067 	Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
   1068 }
   1069 
   1070 /*
   1071  * In the scope, expand the variable name once, then create the variable or
   1072  * replace its value.
   1073  */
   1074 void
   1075 Var_SetExpand(GNode *scope, const char *name, const char *val)
   1076 {
   1077 	FStr varname = FStr_InitRefer(name);
   1078 
   1079 	assert(val != NULL);
   1080 
   1081 	Var_Expand(&varname, scope, VARE_WANTRES);
   1082 
   1083 	if (varname.str[0] == '\0') {
   1084 		DEBUG4(VAR,
   1085 		    "%s: ignoring '%s = %s' "
   1086 		    "as the variable name '%s' expands to empty\n",
   1087 		    scope->name, varname.str, val, name);
   1088 	} else
   1089 		Var_SetWithFlags(scope, varname.str, val, VAR_SET_NONE);
   1090 
   1091 	FStr_Done(&varname);
   1092 }
   1093 
   1094 void
   1095 Global_Set(const char *name, const char *value)
   1096 {
   1097 	Var_Set(SCOPE_GLOBAL, name, value);
   1098 }
   1099 
   1100 void
   1101 Global_Delete(const char *name)
   1102 {
   1103 	Var_Delete(SCOPE_GLOBAL, name);
   1104 }
   1105 
   1106 void
   1107 Global_Set_ReadOnly(const char *name, const char *value)
   1108 {
   1109 	Var_SetWithFlags(SCOPE_GLOBAL, name, value, VAR_SET_READONLY);
   1110 }
   1111 
   1112 /*
   1113  * Append the value to the named variable.
   1114  *
   1115  * If the variable doesn't exist, it is created.  Otherwise a single space
   1116  * and the given value are appended.
   1117  */
   1118 void
   1119 Var_Append(GNode *scope, const char *name, const char *val)
   1120 {
   1121 	Var *v;
   1122 
   1123 	v = VarFind(name, scope, scope == SCOPE_GLOBAL);
   1124 
   1125 	if (v == NULL) {
   1126 		Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
   1127 	} else if (v->readOnly) {
   1128 		DEBUG3(VAR, "%s: ignoring '%s += %s' as it is read-only\n",
   1129 		    scope->name, name, val);
   1130 	} else if (scope == SCOPE_CMDLINE || !v->fromCmd) {
   1131 		Buf_AddByte(&v->val, ' ');
   1132 		Buf_AddStr(&v->val, val);
   1133 
   1134 		DEBUG3(VAR, "%s: %s = %s\n", scope->name, name, v->val.data);
   1135 
   1136 		if (v->fromEnvironment) {
   1137 			/* See VarAdd. */
   1138 			HashEntry *he =
   1139 			    HashTable_CreateEntry(&scope->vars, name, NULL);
   1140 			HashEntry_Set(he, v);
   1141 			FStr_Done(&v->name);
   1142 			v->name = FStr_InitRefer(/* aliased to */ he->key);
   1143 			v->shortLived = false;
   1144 			v->fromEnvironment = false;
   1145 		}
   1146 	}
   1147 }
   1148 
   1149 /*
   1150  * In the scope, expand the variable name once.  If the variable exists in the
   1151  * scope, add a space and the value, otherwise set the variable to the value.
   1152  *
   1153  * Appending to an environment variable only works in the global scope, that
   1154  * is, for variable assignments in makefiles, but not inside conditions or the
   1155  * commands of a target.
   1156  */
   1157 void
   1158 Var_AppendExpand(GNode *scope, const char *name, const char *val)
   1159 {
   1160 	FStr xname = FStr_InitRefer(name);
   1161 
   1162 	assert(val != NULL);
   1163 
   1164 	Var_Expand(&xname, scope, VARE_WANTRES);
   1165 	if (xname.str != name && xname.str[0] == '\0')
   1166 		DEBUG4(VAR,
   1167 		    "%s: ignoring '%s += %s' "
   1168 		    "as the variable name '%s' expands to empty\n",
   1169 		    scope->name, xname.str, val, name);
   1170 	else
   1171 		Var_Append(scope, xname.str, val);
   1172 
   1173 	FStr_Done(&xname);
   1174 }
   1175 
   1176 void
   1177 Global_Append(const char *name, const char *value)
   1178 {
   1179 	Var_Append(SCOPE_GLOBAL, name, value);
   1180 }
   1181 
   1182 bool
   1183 Var_Exists(GNode *scope, const char *name)
   1184 {
   1185 	Var *v = VarFind(name, scope, true);
   1186 	if (v == NULL)
   1187 		return false;
   1188 
   1189 	VarFreeShortLived(v);
   1190 	return true;
   1191 }
   1192 
   1193 /*
   1194  * See if the given variable exists, in the given scope or in other
   1195  * fallback scopes.
   1196  *
   1197  * Input:
   1198  *	scope		scope in which to start search
   1199  *	name		name of the variable to find, is expanded once
   1200  */
   1201 bool
   1202 Var_ExistsExpand(GNode *scope, const char *name)
   1203 {
   1204 	FStr varname = FStr_InitRefer(name);
   1205 	bool exists;
   1206 
   1207 	Var_Expand(&varname, scope, VARE_WANTRES);
   1208 	exists = Var_Exists(scope, varname.str);
   1209 	FStr_Done(&varname);
   1210 	return exists;
   1211 }
   1212 
   1213 /*
   1214  * Return the unexpanded value of the given variable in the given scope,
   1215  * falling back to the command, global and environment scopes, in this order,
   1216  * but see the -e option.
   1217  *
   1218  * Input:
   1219  *	name		the name to find, is not expanded any further
   1220  *
   1221  * Results:
   1222  *	The value if the variable exists, NULL if it doesn't.
   1223  *	The value is valid until the next modification to any variable.
   1224  */
   1225 FStr
   1226 Var_Value(GNode *scope, const char *name)
   1227 {
   1228 	Var *v = VarFind(name, scope, true);
   1229 	char *value;
   1230 
   1231 	if (v == NULL)
   1232 		return FStr_InitRefer(NULL);
   1233 
   1234 	if (!v->shortLived)
   1235 		return FStr_InitRefer(v->val.data);
   1236 
   1237 	value = v->val.data;
   1238 	v->val.data = NULL;
   1239 	VarFreeShortLived(v);
   1240 
   1241 	return FStr_InitOwn(value);
   1242 }
   1243 
   1244 /* Set or clear the read-only attribute of the variable if it exists. */
   1245 void
   1246 Var_ReadOnly(const char *name, bool bf)
   1247 {
   1248 	Var *v;
   1249 
   1250 	v = VarFind(name, SCOPE_GLOBAL, false);
   1251 	if (v == NULL) {
   1252 		DEBUG1(VAR, "Var_ReadOnly: %s not found\n", name);
   1253 		return;
   1254 	}
   1255 	v->readOnly = bf;
   1256 	DEBUG2(VAR, "Var_ReadOnly: %s %s\n", name, bf ? "true" : "false");
   1257 }
   1258 
   1259 /*
   1260  * Return the unexpanded variable value from this node, without trying to look
   1261  * up the variable in any other scope.
   1262  */
   1263 const char *
   1264 GNode_ValueDirect(GNode *gn, const char *name)
   1265 {
   1266 	Var *v = VarFind(name, gn, false);
   1267 	return v != NULL ? v->val.data : NULL;
   1268 }
   1269 
   1270 static VarEvalMode
   1271 VarEvalMode_WithoutKeepDollar(VarEvalMode emode)
   1272 {
   1273 	if (emode == VARE_KEEP_DOLLAR_UNDEF)
   1274 		return VARE_EVAL_KEEP_UNDEF;
   1275 	if (emode == VARE_EVAL_KEEP_DOLLAR)
   1276 		return VARE_WANTRES;
   1277 	return emode;
   1278 }
   1279 
   1280 static VarEvalMode
   1281 VarEvalMode_UndefOk(VarEvalMode emode)
   1282 {
   1283 	return emode == VARE_UNDEFERR ? VARE_WANTRES : emode;
   1284 }
   1285 
   1286 static bool
   1287 VarEvalMode_ShouldEval(VarEvalMode emode)
   1288 {
   1289 	return emode != VARE_PARSE_ONLY;
   1290 }
   1291 
   1292 static bool
   1293 VarEvalMode_ShouldKeepUndef(VarEvalMode emode)
   1294 {
   1295 	return emode == VARE_EVAL_KEEP_UNDEF ||
   1296 	       emode == VARE_KEEP_DOLLAR_UNDEF;
   1297 }
   1298 
   1299 static bool
   1300 VarEvalMode_ShouldKeepDollar(VarEvalMode emode)
   1301 {
   1302 	return emode == VARE_EVAL_KEEP_DOLLAR ||
   1303 	       emode == VARE_KEEP_DOLLAR_UNDEF;
   1304 }
   1305 
   1306 
   1307 static void
   1308 SepBuf_Init(SepBuf *buf, char sep)
   1309 {
   1310 	Buf_InitSize(&buf->buf, 32);
   1311 	buf->needSep = false;
   1312 	buf->sep = sep;
   1313 }
   1314 
   1315 static void
   1316 SepBuf_Sep(SepBuf *buf)
   1317 {
   1318 	buf->needSep = true;
   1319 }
   1320 
   1321 static void
   1322 SepBuf_AddBytes(SepBuf *buf, const char *mem, size_t mem_size)
   1323 {
   1324 	if (mem_size == 0)
   1325 		return;
   1326 	if (buf->needSep && buf->sep != '\0') {
   1327 		Buf_AddByte(&buf->buf, buf->sep);
   1328 		buf->needSep = false;
   1329 	}
   1330 	Buf_AddBytes(&buf->buf, mem, mem_size);
   1331 }
   1332 
   1333 static void
   1334 SepBuf_AddRange(SepBuf *buf, const char *start, const char *end)
   1335 {
   1336 	SepBuf_AddBytes(buf, start, (size_t)(end - start));
   1337 }
   1338 
   1339 static void
   1340 SepBuf_AddStr(SepBuf *buf, const char *str)
   1341 {
   1342 	SepBuf_AddBytes(buf, str, strlen(str));
   1343 }
   1344 
   1345 static void
   1346 SepBuf_AddSubstring(SepBuf *buf, Substring sub)
   1347 {
   1348 	SepBuf_AddRange(buf, sub.start, sub.end);
   1349 }
   1350 
   1351 static char *
   1352 SepBuf_DoneData(SepBuf *buf)
   1353 {
   1354 	return Buf_DoneData(&buf->buf);
   1355 }
   1356 
   1357 
   1358 /*
   1359  * This callback for ModifyWords gets a single word from an expression
   1360  * and typically adds a modification of this word to the buffer. It may also
   1361  * do nothing or add several words.
   1362  *
   1363  * For example, when evaluating the modifier ':M*b' in ${:Ua b c:M*b}, the
   1364  * callback is called 3 times, once for "a", "b" and "c".
   1365  *
   1366  * Some ModifyWord functions assume that they are always passed a
   1367  * null-terminated substring, which is currently guaranteed but may change in
   1368  * the future.
   1369  */
   1370 typedef void (*ModifyWordProc)(Substring word, SepBuf *buf, void *data);
   1371 
   1372 
   1373 /*ARGSUSED*/
   1374 static void
   1375 ModifyWord_Head(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
   1376 {
   1377 	SepBuf_AddSubstring(buf, Substring_Dirname(word));
   1378 }
   1379 
   1380 /*ARGSUSED*/
   1381 static void
   1382 ModifyWord_Tail(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
   1383 {
   1384 	SepBuf_AddSubstring(buf, Substring_Basename(word));
   1385 }
   1386 
   1387 /*ARGSUSED*/
   1388 static void
   1389 ModifyWord_Suffix(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
   1390 {
   1391 	const char *lastDot = Substring_FindLast(word, '.');
   1392 	if (lastDot != NULL)
   1393 		SepBuf_AddRange(buf, lastDot + 1, word.end);
   1394 }
   1395 
   1396 /*ARGSUSED*/
   1397 static void
   1398 ModifyWord_Root(Substring word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
   1399 {
   1400 	const char *lastDot, *end;
   1401 
   1402 	lastDot = Substring_FindLast(word, '.');
   1403 	end = lastDot != NULL ? lastDot : word.end;
   1404 	SepBuf_AddRange(buf, word.start, end);
   1405 }
   1406 
   1407 struct ModifyWord_SysVSubstArgs {
   1408 	GNode *scope;
   1409 	Substring lhsPrefix;
   1410 	bool lhsPercent;
   1411 	Substring lhsSuffix;
   1412 	const char *rhs;
   1413 };
   1414 
   1415 static void
   1416 ModifyWord_SysVSubst(Substring word, SepBuf *buf, void *data)
   1417 {
   1418 	const struct ModifyWord_SysVSubstArgs *args = data;
   1419 	FStr rhs;
   1420 	const char *percent;
   1421 
   1422 	if (Substring_IsEmpty(word))
   1423 		return;
   1424 
   1425 	if (!Substring_HasPrefix(word, args->lhsPrefix) ||
   1426 	    !Substring_HasSuffix(word, args->lhsSuffix)) {
   1427 		SepBuf_AddSubstring(buf, word);
   1428 		return;
   1429 	}
   1430 
   1431 	rhs = FStr_InitRefer(args->rhs);
   1432 	Var_Expand(&rhs, args->scope, VARE_WANTRES);
   1433 
   1434 	percent = args->lhsPercent ? strchr(rhs.str, '%') : NULL;
   1435 
   1436 	if (percent != NULL)
   1437 		SepBuf_AddRange(buf, rhs.str, percent);
   1438 	if (percent != NULL || !args->lhsPercent)
   1439 		SepBuf_AddRange(buf,
   1440 		    word.start + Substring_Length(args->lhsPrefix),
   1441 		    word.end - Substring_Length(args->lhsSuffix));
   1442 	SepBuf_AddStr(buf, percent != NULL ? percent + 1 : rhs.str);
   1443 
   1444 	FStr_Done(&rhs);
   1445 }
   1446 
   1447 static const char *
   1448 Substring_Find(Substring haystack, Substring needle)
   1449 {
   1450 	size_t len, needleLen, i;
   1451 
   1452 	len = Substring_Length(haystack);
   1453 	needleLen = Substring_Length(needle);
   1454 	for (i = 0; i + needleLen <= len; i++)
   1455 		if (memcmp(haystack.start + i, needle.start, needleLen) == 0)
   1456 			return haystack.start + i;
   1457 	return NULL;
   1458 }
   1459 
   1460 struct ModifyWord_SubstArgs {
   1461 	Substring lhs;
   1462 	Substring rhs;
   1463 	PatternFlags pflags;
   1464 	bool matched;
   1465 };
   1466 
   1467 static void
   1468 ModifyWord_Subst(Substring word, SepBuf *buf, void *data)
   1469 {
   1470 	struct ModifyWord_SubstArgs *args = data;
   1471 	size_t wordLen, lhsLen;
   1472 	const char *match;
   1473 
   1474 	wordLen = Substring_Length(word);
   1475 	if (args->pflags.subOnce && args->matched)
   1476 		goto nosub;
   1477 
   1478 	lhsLen = Substring_Length(args->lhs);
   1479 	if (args->pflags.anchorStart) {
   1480 		if (wordLen < lhsLen ||
   1481 		    memcmp(word.start, args->lhs.start, lhsLen) != 0)
   1482 			goto nosub;
   1483 
   1484 		if (args->pflags.anchorEnd && wordLen != lhsLen)
   1485 			goto nosub;
   1486 
   1487 		/* :S,^prefix,replacement, or :S,^whole$,replacement, */
   1488 		SepBuf_AddSubstring(buf, args->rhs);
   1489 		SepBuf_AddRange(buf, word.start + lhsLen, word.end);
   1490 		args->matched = true;
   1491 		return;
   1492 	}
   1493 
   1494 	if (args->pflags.anchorEnd) {
   1495 		if (wordLen < lhsLen)
   1496 			goto nosub;
   1497 		if (memcmp(word.end - lhsLen, args->lhs.start, lhsLen) != 0)
   1498 			goto nosub;
   1499 
   1500 		/* :S,suffix$,replacement, */
   1501 		SepBuf_AddRange(buf, word.start, word.end - lhsLen);
   1502 		SepBuf_AddSubstring(buf, args->rhs);
   1503 		args->matched = true;
   1504 		return;
   1505 	}
   1506 
   1507 	if (Substring_IsEmpty(args->lhs))
   1508 		goto nosub;
   1509 
   1510 	/* unanchored case, may match more than once */
   1511 	while ((match = Substring_Find(word, args->lhs)) != NULL) {
   1512 		SepBuf_AddRange(buf, word.start, match);
   1513 		SepBuf_AddSubstring(buf, args->rhs);
   1514 		args->matched = true;
   1515 		word.start = match + lhsLen;
   1516 		if (Substring_IsEmpty(word) || !args->pflags.subGlobal)
   1517 			break;
   1518 	}
   1519 nosub:
   1520 	SepBuf_AddSubstring(buf, word);
   1521 }
   1522 
   1523 /* Print the error caused by a regcomp or regexec call. */
   1524 static void
   1525 RegexError(int reerr, const regex_t *pat, const char *str)
   1526 {
   1527 	size_t errlen = regerror(reerr, pat, NULL, 0);
   1528 	char *errbuf = bmake_malloc(errlen);
   1529 	regerror(reerr, pat, errbuf, errlen);
   1530 	Error("%s: %s", str, errbuf);
   1531 	free(errbuf);
   1532 }
   1533 
   1534 /* In the modifier ':C', replace a backreference from \0 to \9. */
   1535 static void
   1536 RegexReplaceBackref(char ref, SepBuf *buf, const char *wp,
   1537 		    const regmatch_t *m, size_t nsub)
   1538 {
   1539 	unsigned int n = (unsigned)ref - '0';
   1540 
   1541 	if (n >= nsub)
   1542 		Error("No subexpression \\%u", n);
   1543 	else if (m[n].rm_so == -1) {
   1544 		if (opts.strict)
   1545 			Error("No match for subexpression \\%u", n);
   1546 	} else {
   1547 		SepBuf_AddRange(buf,
   1548 		    wp + (size_t)m[n].rm_so,
   1549 		    wp + (size_t)m[n].rm_eo);
   1550 	}
   1551 }
   1552 
   1553 /*
   1554  * The regular expression matches the word; now add the replacement to the
   1555  * buffer, taking back-references from 'wp'.
   1556  */
   1557 static void
   1558 RegexReplace(Substring replace, SepBuf *buf, const char *wp,
   1559 	     const regmatch_t *m, size_t nsub)
   1560 {
   1561 	const char *rp;
   1562 
   1563 	for (rp = replace.start; rp != replace.end; rp++) {
   1564 		if (*rp == '\\' && rp + 1 != replace.end &&
   1565 		    (rp[1] == '&' || rp[1] == '\\'))
   1566 			SepBuf_AddBytes(buf, ++rp, 1);
   1567 		else if (*rp == '\\' && rp + 1 != replace.end &&
   1568 			 ch_isdigit(rp[1]))
   1569 			RegexReplaceBackref(*++rp, buf, wp, m, nsub);
   1570 		else if (*rp == '&') {
   1571 			SepBuf_AddRange(buf,
   1572 			    wp + (size_t)m[0].rm_so,
   1573 			    wp + (size_t)m[0].rm_eo);
   1574 		} else
   1575 			SepBuf_AddBytes(buf, rp, 1);
   1576 	}
   1577 }
   1578 
   1579 struct ModifyWord_SubstRegexArgs {
   1580 	regex_t re;
   1581 	size_t nsub;
   1582 	Substring replace;
   1583 	PatternFlags pflags;
   1584 	bool matched;
   1585 };
   1586 
   1587 static void
   1588 ModifyWord_SubstRegex(Substring word, SepBuf *buf, void *data)
   1589 {
   1590 	struct ModifyWord_SubstRegexArgs *args = data;
   1591 	int xrv;
   1592 	const char *wp;
   1593 	int flags = 0;
   1594 	regmatch_t m[10];
   1595 
   1596 	assert(word.end[0] == '\0');	/* assume null-terminated word */
   1597 	wp = word.start;
   1598 	if (args->pflags.subOnce && args->matched)
   1599 		goto no_match;
   1600 
   1601 again:
   1602 	xrv = regexec(&args->re, wp, args->nsub, m, flags);
   1603 	if (xrv == 0)
   1604 		goto ok;
   1605 	if (xrv != REG_NOMATCH)
   1606 		RegexError(xrv, &args->re, "Unexpected regex error");
   1607 no_match:
   1608 	SepBuf_AddRange(buf, wp, word.end);
   1609 	return;
   1610 
   1611 ok:
   1612 	args->matched = true;
   1613 	SepBuf_AddBytes(buf, wp, (size_t)m[0].rm_so);
   1614 
   1615 	RegexReplace(args->replace, buf, wp, m, args->nsub);
   1616 
   1617 	wp += (size_t)m[0].rm_eo;
   1618 	if (args->pflags.subGlobal) {
   1619 		flags |= REG_NOTBOL;
   1620 		if (m[0].rm_so == 0 && m[0].rm_eo == 0 && *wp != '\0') {
   1621 			SepBuf_AddBytes(buf, wp, 1);
   1622 			wp++;
   1623 		}
   1624 		if (*wp != '\0')
   1625 			goto again;
   1626 	}
   1627 	if (*wp != '\0')
   1628 		SepBuf_AddStr(buf, wp);
   1629 }
   1630 
   1631 
   1632 struct ModifyWord_LoopArgs {
   1633 	GNode *scope;
   1634 	const char *var;	/* name of the temporary variable */
   1635 	const char *body;	/* string to expand */
   1636 	VarEvalMode emode;
   1637 };
   1638 
   1639 static void
   1640 ModifyWord_Loop(Substring word, SepBuf *buf, void *data)
   1641 {
   1642 	const struct ModifyWord_LoopArgs *args;
   1643 	char *s;
   1644 
   1645 	if (Substring_IsEmpty(word))
   1646 		return;
   1647 
   1648 	args = data;
   1649 	assert(word.end[0] == '\0');	/* assume null-terminated word */
   1650 	Var_SetWithFlags(args->scope, args->var, word.start,
   1651 	    VAR_SET_NO_EXPORT);
   1652 	s = Var_Subst(args->body, args->scope, args->emode);
   1653 	/* TODO: handle errors */
   1654 
   1655 	DEBUG2(VAR, "ModifyWord_Loop: expand \"%s\" to \"%s\"\n",
   1656 	    args->body, s);
   1657 
   1658 	if (s[0] == '\n' || Buf_EndsWith(&buf->buf, '\n'))
   1659 		buf->needSep = false;
   1660 	SepBuf_AddStr(buf, s);
   1661 	free(s);
   1662 }
   1663 
   1664 
   1665 /*
   1666  * The :[first..last] modifier selects words from the expression.
   1667  * It can also reverse the words.
   1668  */
   1669 static char *
   1670 VarSelectWords(const char *str, int first, int last,
   1671 	       char sep, bool oneBigWord)
   1672 {
   1673 	SubstringWords words;
   1674 	int len, start, end, step;
   1675 	int i;
   1676 
   1677 	SepBuf buf;
   1678 	SepBuf_Init(&buf, sep);
   1679 
   1680 	if (oneBigWord) {
   1681 		/* fake what Substring_Words() would do */
   1682 		words.len = 1;
   1683 		words.words = bmake_malloc(sizeof(words.words[0]));
   1684 		words.freeIt = NULL;
   1685 		words.words[0] = Substring_InitStr(str); /* no need to copy */
   1686 	} else {
   1687 		words = Substring_Words(str, false);
   1688 	}
   1689 
   1690 	/* Convert -1 to len, -2 to (len - 1), etc. */
   1691 	len = (int)words.len;
   1692 	if (first < 0)
   1693 		first += len + 1;
   1694 	if (last < 0)
   1695 		last += len + 1;
   1696 
   1697 	if (first > last) {
   1698 		start = (first > len ? len : first) - 1;
   1699 		end = last < 1 ? 0 : last - 1;
   1700 		step = -1;
   1701 	} else {
   1702 		start = first < 1 ? 0 : first - 1;
   1703 		end = last > len ? len : last;
   1704 		step = 1;
   1705 	}
   1706 
   1707 	for (i = start; (step < 0) == (i >= end); i += step) {
   1708 		SepBuf_AddSubstring(&buf, words.words[i]);
   1709 		SepBuf_Sep(&buf);
   1710 	}
   1711 
   1712 	SubstringWords_Free(words);
   1713 
   1714 	return SepBuf_DoneData(&buf);
   1715 }
   1716 
   1717 
   1718 /*ARGSUSED*/
   1719 static void
   1720 ModifyWord_Realpath(Substring word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
   1721 {
   1722 	struct stat st;
   1723 	char rbuf[MAXPATHLEN];
   1724 	const char *rp;
   1725 
   1726 	assert(word.end[0] == '\0');	/* assume null-terminated word */
   1727 	rp = cached_realpath(word.start, rbuf);
   1728 	if (rp != NULL && *rp == '/' && stat(rp, &st) == 0)
   1729 		SepBuf_AddStr(buf, rp);
   1730 	else
   1731 		SepBuf_AddSubstring(buf, word);
   1732 }
   1733 
   1734 
   1735 static char *
   1736 SubstringWords_JoinFree(SubstringWords words)
   1737 {
   1738 	Buffer buf;
   1739 	size_t i;
   1740 
   1741 	Buf_Init(&buf);
   1742 
   1743 	for (i = 0; i < words.len; i++) {
   1744 		if (i != 0) {
   1745 			/*
   1746 			 * XXX: Use ch->sep instead of ' ', for consistency.
   1747 			 */
   1748 			Buf_AddByte(&buf, ' ');
   1749 		}
   1750 		Buf_AddRange(&buf, words.words[i].start, words.words[i].end);
   1751 	}
   1752 
   1753 	SubstringWords_Free(words);
   1754 
   1755 	return Buf_DoneData(&buf);
   1756 }
   1757 
   1758 
   1759 /*
   1760  * Quote shell meta-characters and space characters in the string.
   1761  * If quoteDollar is set, also quote and double any '$' characters.
   1762  */
   1763 static void
   1764 QuoteShell(const char *str, bool quoteDollar, LazyBuf *buf)
   1765 {
   1766 	const char *p;
   1767 
   1768 	LazyBuf_Init(buf, str);
   1769 	for (p = str; *p != '\0'; p++) {
   1770 		if (*p == '\n') {
   1771 			const char *newline = Shell_GetNewline();
   1772 			if (newline == NULL)
   1773 				newline = "\\\n";
   1774 			LazyBuf_AddStr(buf, newline);
   1775 			continue;
   1776 		}
   1777 		if (ch_isspace(*p) || ch_is_shell_meta(*p))
   1778 			LazyBuf_Add(buf, '\\');
   1779 		LazyBuf_Add(buf, *p);
   1780 		if (quoteDollar && *p == '$')
   1781 			LazyBuf_AddStr(buf, "\\$");
   1782 	}
   1783 }
   1784 
   1785 /*
   1786  * Compute the 32-bit hash of the given string, using the MurmurHash3
   1787  * algorithm. Output is encoded as 8 hex digits, in Little Endian order.
   1788  */
   1789 static char *
   1790 Hash(const char *str)
   1791 {
   1792 	static const char hexdigits[16] = "0123456789abcdef";
   1793 	const unsigned char *ustr = (const unsigned char *)str;
   1794 
   1795 	uint32_t h = 0x971e137bU;
   1796 	uint32_t c1 = 0x95543787U;
   1797 	uint32_t c2 = 0x2ad7eb25U;
   1798 	size_t len2 = strlen(str);
   1799 
   1800 	char *buf;
   1801 	size_t i;
   1802 
   1803 	size_t len;
   1804 	for (len = len2; len != 0;) {
   1805 		uint32_t k = 0;
   1806 		switch (len) {
   1807 		default:
   1808 			k = ((uint32_t)ustr[3] << 24) |
   1809 			    ((uint32_t)ustr[2] << 16) |
   1810 			    ((uint32_t)ustr[1] << 8) |
   1811 			    (uint32_t)ustr[0];
   1812 			len -= 4;
   1813 			ustr += 4;
   1814 			break;
   1815 		case 3:
   1816 			k |= (uint32_t)ustr[2] << 16;
   1817 			/* FALLTHROUGH */
   1818 		case 2:
   1819 			k |= (uint32_t)ustr[1] << 8;
   1820 			/* FALLTHROUGH */
   1821 		case 1:
   1822 			k |= (uint32_t)ustr[0];
   1823 			len = 0;
   1824 		}
   1825 		c1 = c1 * 5 + 0x7b7d159cU;
   1826 		c2 = c2 * 5 + 0x6bce6396U;
   1827 		k *= c1;
   1828 		k = (k << 11) ^ (k >> 21);
   1829 		k *= c2;
   1830 		h = (h << 13) ^ (h >> 19);
   1831 		h = h * 5 + 0x52dce729U;
   1832 		h ^= k;
   1833 	}
   1834 	h ^= (uint32_t)len2;
   1835 	h *= 0x85ebca6b;
   1836 	h ^= h >> 13;
   1837 	h *= 0xc2b2ae35;
   1838 	h ^= h >> 16;
   1839 
   1840 	buf = bmake_malloc(9);
   1841 	for (i = 0; i < 8; i++) {
   1842 		buf[i] = hexdigits[h & 0x0f];
   1843 		h >>= 4;
   1844 	}
   1845 	buf[8] = '\0';
   1846 	return buf;
   1847 }
   1848 
   1849 static char *
   1850 FormatTime(const char *fmt, time_t t, bool gmt)
   1851 {
   1852 	char buf[BUFSIZ];
   1853 
   1854 	if (t == 0)
   1855 		time(&t);
   1856 	if (*fmt == '\0')
   1857 		fmt = "%c";
   1858 	if (gmt && strchr(fmt, 's') != NULL) {
   1859 		/* strftime "%s" only works with localtime, not with gmtime. */
   1860 		const char *prev_tz_env = getenv("TZ");
   1861 		char *prev_tz = prev_tz_env != NULL
   1862 		    ? bmake_strdup(prev_tz_env) : NULL;
   1863 		setenv("TZ", "UTC", 1);
   1864 		strftime(buf, sizeof buf, fmt, localtime(&t));
   1865 		if (prev_tz != NULL) {
   1866 			setenv("TZ", prev_tz, 1);
   1867 			free(prev_tz);
   1868 		} else
   1869 			unsetenv("TZ");
   1870 	} else
   1871 		strftime(buf, sizeof buf, fmt, (gmt ? gmtime : localtime)(&t));
   1872 
   1873 	buf[sizeof buf - 1] = '\0';
   1874 	return bmake_strdup(buf);
   1875 }
   1876 
   1877 /*
   1878  * The ApplyModifier functions take an expression that is being evaluated.
   1879  * Their task is to apply a single modifier to the expression.  This involves
   1880  * parsing the modifier, evaluating it and finally updating the value of the
   1881  * expression.
   1882  *
   1883  * Parsing the modifier
   1884  *
   1885  * If parsing succeeds, the parsing position *pp is updated to point to the
   1886  * first character following the modifier, which typically is either ':' or
   1887  * ch->endc.  The modifier doesn't have to check for this delimiter character,
   1888  * this is done by ApplyModifiers.
   1889  *
   1890  * XXX: As of 2020-11-15, some modifiers such as :S, :C, :P, :L do not
   1891  * need to be followed by a ':' or endc; this was an unintended mistake.
   1892  *
   1893  * If parsing fails because of a missing delimiter after a modifier part (as
   1894  * in the :S, :C or :@ modifiers), return AMR_CLEANUP.
   1895  *
   1896  * If parsing fails because the modifier is unknown, return AMR_UNKNOWN to
   1897  * try the SysV modifier ':from=to' as fallback.  This should only be
   1898  * done as long as there have been no side effects from evaluating nested
   1899  * variables, to avoid evaluating them more than once.  In this case, the
   1900  * parsing position may or may not be updated.  (XXX: Why not? The original
   1901  * parsing position is well-known in ApplyModifiers.)
   1902  *
   1903  * If parsing fails and the SysV modifier ${VAR:from=to} should not be used
   1904  * as a fallback, issue an error message using Parse_Error (preferred over
   1905  * Error) and then return AMR_CLEANUP, which stops processing the expression.
   1906  * (XXX: As of 2020-08-23, evaluation of the string continues nevertheless
   1907  * after skipping a few bytes, which results in garbage.)
   1908  *
   1909  * Evaluating the modifier
   1910  *
   1911  * After parsing, the modifier is evaluated.  The side effects from evaluating
   1912  * nested expressions in the modifier text often already happen
   1913  * during parsing though.  For most modifiers this doesn't matter since their
   1914  * only noticeable effect is that they update the value of the expression.
   1915  * Some modifiers such as ':sh' or '::=' have noticeable side effects though.
   1916  *
   1917  * Evaluating the modifier usually takes the current value of the
   1918  * expression from ch->expr->value, or the variable name from ch->var->name,
   1919  * and stores the result back in ch->expr->value via Expr_SetValueOwn or
   1920  * Expr_SetValueRefer.
   1921  *
   1922  * If evaluating fails, the fallback error message "Bad modifier" is printed
   1923  * using Error.  This function has no side effects, it really just prints the
   1924  * error message, continuing as if nothing had happened.  TODO: This should be
   1925  * fixed by adding proper error handling to Var_Subst, Var_Parse,
   1926  * ApplyModifiers and ModifyWords.
   1927  *
   1928  * Some modifiers such as :D and :U turn undefined expressions into defined
   1929  * expressions using Expr_Define.
   1930  */
   1931 
   1932 typedef enum ExprDefined {
   1933 	/* The expression is based on a regular, defined variable. */
   1934 	DEF_REGULAR,
   1935 	/* The expression is based on an undefined variable. */
   1936 	DEF_UNDEF,
   1937 	/*
   1938 	 * The expression started as an undefined expression, but one
   1939 	 * of the modifiers (such as ':D' or ':U') has turned the expression
   1940 	 * from undefined to defined.
   1941 	 */
   1942 	DEF_DEFINED
   1943 } ExprDefined;
   1944 
   1945 static const char ExprDefined_Name[][10] = {
   1946 	"regular",
   1947 	"undefined",
   1948 	"defined"
   1949 };
   1950 
   1951 #if __STDC_VERSION__ >= 199901L
   1952 #define const_member		const
   1953 #else
   1954 #define const_member		/* no const possible */
   1955 #endif
   1956 
   1957 /* An expression based on a variable, such as $@ or ${VAR:Mpattern:Q}. */
   1958 typedef struct Expr {
   1959 	const char *name;
   1960 	FStr value;
   1961 	VarEvalMode const_member emode;
   1962 	GNode *const_member scope;
   1963 	ExprDefined defined;
   1964 } Expr;
   1965 
   1966 /*
   1967  * The status of applying a chain of modifiers to an expression.
   1968  *
   1969  * The modifiers of an expression are broken into chains of modifiers,
   1970  * starting a new nested chain whenever an indirect modifier starts.  There
   1971  * are at most 2 nesting levels: the outer one for the direct modifiers, and
   1972  * the inner one for the indirect modifiers.
   1973  *
   1974  * For example, the expression ${VAR:M*:${IND1}:${IND2}:O:u} has 3 chains of
   1975  * modifiers:
   1976  *
   1977  *	Chain 1 starts with the single modifier ':M*'.
   1978  *	  Chain 2 starts with all modifiers from ${IND1}.
   1979  *	  Chain 2 ends at the ':' between ${IND1} and ${IND2}.
   1980  *	  Chain 3 starts with all modifiers from ${IND2}.
   1981  *	  Chain 3 ends at the ':' after ${IND2}.
   1982  *	Chain 1 continues with the 2 modifiers ':O' and ':u'.
   1983  *	Chain 1 ends at the final '}' of the expression.
   1984  *
   1985  * After such a chain ends, its properties no longer have any effect.
   1986  *
   1987  * See varmod-indirect.mk.
   1988  */
   1989 typedef struct ModChain {
   1990 	Expr *expr;
   1991 	/* '\0' or '{' or '(' */
   1992 	char const_member startc;
   1993 	/* '\0' or '}' or ')' */
   1994 	char const_member endc;
   1995 	/* Separator when joining words (see the :ts modifier). */
   1996 	char sep;
   1997 	/*
   1998 	 * Whether some modifiers that otherwise split the variable value
   1999 	 * into words, like :S and :C, treat the variable value as a single
   2000 	 * big word, possibly containing spaces.
   2001 	 */
   2002 	bool oneBigWord;
   2003 } ModChain;
   2004 
   2005 static void
   2006 Expr_Define(Expr *expr)
   2007 {
   2008 	if (expr->defined == DEF_UNDEF)
   2009 		expr->defined = DEF_DEFINED;
   2010 }
   2011 
   2012 static const char *
   2013 Expr_Str(const Expr *expr)
   2014 {
   2015 	return expr->value.str;
   2016 }
   2017 
   2018 static SubstringWords
   2019 Expr_Words(const Expr *expr)
   2020 {
   2021 	return Substring_Words(Expr_Str(expr), false);
   2022 }
   2023 
   2024 static void
   2025 Expr_SetValue(Expr *expr, FStr value)
   2026 {
   2027 	FStr_Done(&expr->value);
   2028 	expr->value = value;
   2029 }
   2030 
   2031 static void
   2032 Expr_SetValueOwn(Expr *expr, char *value)
   2033 {
   2034 	Expr_SetValue(expr, FStr_InitOwn(value));
   2035 }
   2036 
   2037 static void
   2038 Expr_SetValueRefer(Expr *expr, const char *value)
   2039 {
   2040 	Expr_SetValue(expr, FStr_InitRefer(value));
   2041 }
   2042 
   2043 static bool
   2044 Expr_ShouldEval(const Expr *expr)
   2045 {
   2046 	return VarEvalMode_ShouldEval(expr->emode);
   2047 }
   2048 
   2049 static bool
   2050 ModChain_ShouldEval(const ModChain *ch)
   2051 {
   2052 	return Expr_ShouldEval(ch->expr);
   2053 }
   2054 
   2055 
   2056 typedef enum ApplyModifierResult {
   2057 	/* Continue parsing */
   2058 	AMR_OK,
   2059 	/* Not a match, try the ':from=to' modifier as well. */
   2060 	AMR_UNKNOWN,
   2061 	/* Error out with "Bad modifier" message. */
   2062 	AMR_BAD,
   2063 	/* Error out without the standard error message. */
   2064 	AMR_CLEANUP
   2065 } ApplyModifierResult;
   2066 
   2067 /*
   2068  * Allow backslashes to escape the delimiter, $, and \, but don't touch other
   2069  * backslashes.
   2070  */
   2071 static bool
   2072 IsEscapedModifierPart(const char *p, char delim,
   2073 		      struct ModifyWord_SubstArgs *subst)
   2074 {
   2075 	if (p[0] != '\\')
   2076 		return false;
   2077 	if (p[1] == delim || p[1] == '\\' || p[1] == '$')
   2078 		return true;
   2079 	return p[1] == '&' && subst != NULL;
   2080 }
   2081 
   2082 /*
   2083  * In a part of a modifier, parse a subexpression and evaluate it.
   2084  */
   2085 static void
   2086 ParseModifierPartExpr(const char **pp, LazyBuf *part, const ModChain *ch,
   2087 		      VarEvalMode emode)
   2088 {
   2089 	const char *p = *pp;
   2090 	FStr nested_val = Var_Parse(&p, ch->expr->scope,
   2091 	    VarEvalMode_WithoutKeepDollar(emode));
   2092 	/* TODO: handle errors */
   2093 	if (VarEvalMode_ShouldEval(emode))
   2094 		LazyBuf_AddStr(part, nested_val.str);
   2095 	else
   2096 		LazyBuf_AddSubstring(part, Substring_Init(*pp, p));
   2097 	FStr_Done(&nested_val);
   2098 	*pp = p;
   2099 }
   2100 
   2101 /*
   2102  * In a part of a modifier, parse some text that looks like a subexpression.
   2103  * If the text starts with '$(', any '(' and ')' must be balanced.
   2104  * If the text starts with '${', any '{' and '}' must be balanced.
   2105  * If the text starts with '$', that '$' is copied verbatim, it is not parsed
   2106  * as a short-name expression.
   2107  */
   2108 static void
   2109 ParseModifierPartBalanced(const char **pp, LazyBuf *part)
   2110 {
   2111 	const char *p = *pp;
   2112 
   2113 	if (p[1] == '(' || p[1] == '{') {
   2114 		char startc = p[1];
   2115 		int endc = startc == '(' ? ')' : '}';
   2116 		int depth = 1;
   2117 
   2118 		for (p += 2; *p != '\0' && depth > 0; p++) {
   2119 			if (p[-1] != '\\') {
   2120 				if (*p == startc)
   2121 					depth++;
   2122 				if (*p == endc)
   2123 					depth--;
   2124 			}
   2125 		}
   2126 		LazyBuf_AddSubstring(part, Substring_Init(*pp, p));
   2127 		*pp = p;
   2128 	} else {
   2129 		LazyBuf_Add(part, *p);
   2130 		*pp = p + 1;
   2131 	}
   2132 }
   2133 
   2134 /* See ParseModifierPart for the documentation. */
   2135 static bool
   2136 ParseModifierPartSubst(
   2137     const char **pp,
   2138     /* If true, parse up to but excluding the next ':' or ch->endc. */
   2139     bool whole,
   2140     char delim,
   2141     VarEvalMode emode,
   2142     ModChain *ch,
   2143     LazyBuf *part,
   2144     /*
   2145      * For the first part of the ':S' modifier, set anchorEnd if the last
   2146      * character of the pattern is a $.
   2147      */
   2148     PatternFlags *out_pflags,
   2149     /*
   2150      * For the second part of the ':S' modifier, allow ampersands to be
   2151      * escaped and replace unescaped ampersands with subst->lhs.
   2152      */
   2153     struct ModifyWord_SubstArgs *subst
   2154 )
   2155 {
   2156 	const char *p;
   2157 	char end1, end2;
   2158 
   2159 	p = *pp;
   2160 	LazyBuf_Init(part, p);
   2161 
   2162 	end1 = whole ? ':' : delim;
   2163 	end2 = whole ? ch->endc : delim;
   2164 	while (*p != '\0' && *p != end1 && *p != end2) {
   2165 		if (IsEscapedModifierPart(p, delim, subst)) {
   2166 			LazyBuf_Add(part, p[1]);
   2167 			p += 2;
   2168 		} else if (*p != '$') {	/* Unescaped, simple text */
   2169 			if (subst != NULL && *p == '&')
   2170 				LazyBuf_AddSubstring(part, subst->lhs);
   2171 			else
   2172 				LazyBuf_Add(part, *p);
   2173 			p++;
   2174 		} else if (p[1] == delim) {	/* Unescaped '$' at end */
   2175 			if (out_pflags != NULL)
   2176 				out_pflags->anchorEnd = true;
   2177 			else
   2178 				LazyBuf_Add(part, *p);
   2179 			p++;
   2180 		} else if (emode == VARE_PARSE_BALANCED)
   2181 			ParseModifierPartBalanced(&p, part);
   2182 		else
   2183 			ParseModifierPartExpr(&p, part, ch, emode);
   2184 	}
   2185 
   2186 	*pp = p;
   2187 	if (*p != end1 && *p != end2) {
   2188 		Error("Unfinished modifier for \"%s\" ('%c' missing)",
   2189 		    ch->expr->name, end2);
   2190 		LazyBuf_Done(part);
   2191 		return false;
   2192 	}
   2193 	if (!whole)
   2194 		(*pp)++;
   2195 
   2196 	{
   2197 		Substring sub = LazyBuf_Get(part);
   2198 		DEBUG2(VAR, "Modifier part: \"%.*s\"\n",
   2199 		    (int)Substring_Length(sub), sub.start);
   2200 	}
   2201 
   2202 	return true;
   2203 }
   2204 
   2205 /*
   2206  * Parse a part of a modifier such as the "from" and "to" in :S/from/to/ or
   2207  * the "var" or "replacement ${var}" in :@var@replacement ${var}@, up to and
   2208  * including the next unescaped delimiter.  The delimiter, as well as the
   2209  * backslash or the dollar, can be escaped with a backslash.
   2210  *
   2211  * Return true if parsing succeeded, together with the parsed (and possibly
   2212  * expanded) part.  In that case, pp points right after the delimiter.  The
   2213  * delimiter is not included in the part though.
   2214  */
   2215 static bool
   2216 ParseModifierPart(
   2217     /* The parsing position, updated upon return */
   2218     const char **pp,
   2219     /* Parsing stops at this delimiter */
   2220     char delim,
   2221     /* Mode for evaluating nested expressions. */
   2222     VarEvalMode emode,
   2223     ModChain *ch,
   2224     LazyBuf *part
   2225 )
   2226 {
   2227 	return ParseModifierPartSubst(pp, false, delim, emode, ch, part,
   2228 	    NULL, NULL);
   2229 }
   2230 
   2231 MAKE_INLINE bool
   2232 IsDelimiter(char c, const ModChain *ch)
   2233 {
   2234 	return c == ':' || c == ch->endc || c == '\0';
   2235 }
   2236 
   2237 /* Test whether mod starts with modname, followed by a delimiter. */
   2238 MAKE_INLINE bool
   2239 ModMatch(const char *mod, const char *modname, const ModChain *ch)
   2240 {
   2241 	size_t n = strlen(modname);
   2242 	return strncmp(mod, modname, n) == 0 && IsDelimiter(mod[n], ch);
   2243 }
   2244 
   2245 /* Test whether mod starts with modname, followed by a delimiter or '='. */
   2246 MAKE_INLINE bool
   2247 ModMatchEq(const char *mod, const char *modname, const ModChain *ch)
   2248 {
   2249 	size_t n = strlen(modname);
   2250 	return strncmp(mod, modname, n) == 0 &&
   2251 	       (IsDelimiter(mod[n], ch) || mod[n] == '=');
   2252 }
   2253 
   2254 static bool
   2255 TryParseIntBase0(const char **pp, int *out_num)
   2256 {
   2257 	char *end;
   2258 	long n;
   2259 
   2260 	errno = 0;
   2261 	n = strtol(*pp, &end, 0);
   2262 
   2263 	if (end == *pp)
   2264 		return false;
   2265 	if ((n == LONG_MIN || n == LONG_MAX) && errno == ERANGE)
   2266 		return false;
   2267 	if (n < INT_MIN || n > INT_MAX)
   2268 		return false;
   2269 
   2270 	*pp = end;
   2271 	*out_num = (int)n;
   2272 	return true;
   2273 }
   2274 
   2275 static bool
   2276 TryParseSize(const char **pp, size_t *out_num)
   2277 {
   2278 	char *end;
   2279 	unsigned long n;
   2280 
   2281 	if (!ch_isdigit(**pp))
   2282 		return false;
   2283 
   2284 	errno = 0;
   2285 	n = strtoul(*pp, &end, 10);
   2286 	if (n == ULONG_MAX && errno == ERANGE)
   2287 		return false;
   2288 	if (n > SIZE_MAX)
   2289 		return false;
   2290 
   2291 	*pp = end;
   2292 	*out_num = (size_t)n;
   2293 	return true;
   2294 }
   2295 
   2296 static bool
   2297 TryParseChar(const char **pp, int base, char *out_ch)
   2298 {
   2299 	char *end;
   2300 	unsigned long n;
   2301 
   2302 	if (!ch_isalnum(**pp))
   2303 		return false;
   2304 
   2305 	errno = 0;
   2306 	n = strtoul(*pp, &end, base);
   2307 	if (n == ULONG_MAX && errno == ERANGE)
   2308 		return false;
   2309 	if (n > UCHAR_MAX)
   2310 		return false;
   2311 
   2312 	*pp = end;
   2313 	*out_ch = (char)n;
   2314 	return true;
   2315 }
   2316 
   2317 /*
   2318  * Modify each word of the expression using the given function and place the
   2319  * result back in the expression.
   2320  */
   2321 static void
   2322 ModifyWords(ModChain *ch,
   2323 	    ModifyWordProc modifyWord, void *modifyWord_args,
   2324 	    bool oneBigWord)
   2325 {
   2326 	Expr *expr = ch->expr;
   2327 	const char *val = Expr_Str(expr);
   2328 	SepBuf result;
   2329 	SubstringWords words;
   2330 	size_t i;
   2331 	Substring word;
   2332 
   2333 	if (!ModChain_ShouldEval(ch))
   2334 		return;
   2335 
   2336 	if (oneBigWord) {
   2337 		SepBuf_Init(&result, ch->sep);
   2338 		/* XXX: performance: Substring_InitStr calls strlen */
   2339 		word = Substring_InitStr(val);
   2340 		modifyWord(word, &result, modifyWord_args);
   2341 		goto done;
   2342 	}
   2343 
   2344 	words = Substring_Words(val, false);
   2345 
   2346 	DEBUG3(VAR, "ModifyWords: split \"%s\" into %u %s\n",
   2347 	    val, (unsigned)words.len, words.len != 1 ? "words" : "word");
   2348 
   2349 	SepBuf_Init(&result, ch->sep);
   2350 	for (i = 0; i < words.len; i++) {
   2351 		modifyWord(words.words[i], &result, modifyWord_args);
   2352 		if (result.buf.len > 0)
   2353 			SepBuf_Sep(&result);
   2354 	}
   2355 
   2356 	SubstringWords_Free(words);
   2357 
   2358 done:
   2359 	Expr_SetValueOwn(expr, SepBuf_DoneData(&result));
   2360 }
   2361 
   2362 /* :@var (at) ...${var}...@ */
   2363 static ApplyModifierResult
   2364 ApplyModifier_Loop(const char **pp, ModChain *ch)
   2365 {
   2366 	Expr *expr = ch->expr;
   2367 	struct ModifyWord_LoopArgs args;
   2368 	char prev_sep;
   2369 	LazyBuf tvarBuf, strBuf;
   2370 	FStr tvar, str;
   2371 
   2372 	args.scope = expr->scope;
   2373 
   2374 	(*pp)++;		/* Skip the first '@' */
   2375 	if (!ParseModifierPart(pp, '@', VARE_PARSE_ONLY, ch, &tvarBuf))
   2376 		return AMR_CLEANUP;
   2377 	tvar = LazyBuf_DoneGet(&tvarBuf);
   2378 	args.var = tvar.str;
   2379 	if (strchr(args.var, '$') != NULL) {
   2380 		Parse_Error(PARSE_FATAL,
   2381 		    "In the :@ modifier, the variable name \"%s\" "
   2382 		    "must not contain a dollar",
   2383 		    args.var);
   2384 		return AMR_CLEANUP;
   2385 	}
   2386 
   2387 	if (!ParseModifierPart(pp, '@', VARE_PARSE_BALANCED, ch, &strBuf))
   2388 		return AMR_CLEANUP;
   2389 	str = LazyBuf_DoneGet(&strBuf);
   2390 	args.body = str.str;
   2391 
   2392 	if (!Expr_ShouldEval(expr))
   2393 		goto done;
   2394 
   2395 	args.emode = VarEvalMode_WithoutKeepDollar(expr->emode);
   2396 	prev_sep = ch->sep;
   2397 	ch->sep = ' ';		/* XXX: should be ch->sep for consistency */
   2398 	ModifyWords(ch, ModifyWord_Loop, &args, ch->oneBigWord);
   2399 	ch->sep = prev_sep;
   2400 	/* XXX: Consider restoring the previous value instead of deleting. */
   2401 	Var_Delete(expr->scope, args.var);
   2402 
   2403 done:
   2404 	FStr_Done(&tvar);
   2405 	FStr_Done(&str);
   2406 	return AMR_OK;
   2407 }
   2408 
   2409 static void
   2410 ParseModifier_Defined(const char **pp, ModChain *ch, bool shouldEval,
   2411 		      LazyBuf *buf)
   2412 {
   2413 	const char *p;
   2414 
   2415 	p = *pp + 1;
   2416 	LazyBuf_Init(buf, p);
   2417 	while (!IsDelimiter(*p, ch)) {
   2418 
   2419 		/*
   2420 		 * XXX: This code is similar to the one in Var_Parse. See if
   2421 		 * the code can be merged. See also ParseModifier_Match and
   2422 		 * ParseModifierPart.
   2423 		 */
   2424 
   2425 		/* See Buf_AddEscaped in for.c for the counterpart. */
   2426 		if (*p == '\\') {
   2427 			char c = p[1];
   2428 			if ((IsDelimiter(c, ch) && c != '\0') ||
   2429 			    c == '$' || c == '\\') {
   2430 				if (shouldEval)
   2431 					LazyBuf_Add(buf, c);
   2432 				p += 2;
   2433 				continue;
   2434 			}
   2435 		}
   2436 
   2437 		if (*p == '$') {
   2438 			FStr val = Var_Parse(&p, ch->expr->scope,
   2439 			    shouldEval ? ch->expr->emode : VARE_PARSE_ONLY);
   2440 			/* TODO: handle errors */
   2441 			if (shouldEval)
   2442 				LazyBuf_AddStr(buf, val.str);
   2443 			FStr_Done(&val);
   2444 			continue;
   2445 		}
   2446 
   2447 		if (shouldEval)
   2448 			LazyBuf_Add(buf, *p);
   2449 		p++;
   2450 	}
   2451 	*pp = p;
   2452 }
   2453 
   2454 /* :Ddefined or :Uundefined */
   2455 static ApplyModifierResult
   2456 ApplyModifier_Defined(const char **pp, ModChain *ch)
   2457 {
   2458 	Expr *expr = ch->expr;
   2459 	LazyBuf buf;
   2460 	bool shouldEval =
   2461 	    Expr_ShouldEval(expr) &&
   2462 	    (**pp == 'D') == (expr->defined == DEF_REGULAR);
   2463 
   2464 	ParseModifier_Defined(pp, ch, shouldEval, &buf);
   2465 
   2466 	Expr_Define(expr);
   2467 	if (shouldEval)
   2468 		Expr_SetValue(expr, Substring_Str(LazyBuf_Get(&buf)));
   2469 
   2470 	return AMR_OK;
   2471 }
   2472 
   2473 /* :L */
   2474 static ApplyModifierResult
   2475 ApplyModifier_Literal(const char **pp, ModChain *ch)
   2476 {
   2477 	Expr *expr = ch->expr;
   2478 
   2479 	(*pp)++;
   2480 
   2481 	if (Expr_ShouldEval(expr)) {
   2482 		Expr_Define(expr);
   2483 		Expr_SetValueOwn(expr, bmake_strdup(expr->name));
   2484 	}
   2485 
   2486 	return AMR_OK;
   2487 }
   2488 
   2489 static bool
   2490 TryParseTime(const char **pp, time_t *out_time)
   2491 {
   2492 	char *end;
   2493 	unsigned long n;
   2494 
   2495 	if (!ch_isdigit(**pp))
   2496 		return false;
   2497 
   2498 	errno = 0;
   2499 	n = strtoul(*pp, &end, 10);
   2500 	if (n == ULONG_MAX && errno == ERANGE)
   2501 		return false;
   2502 
   2503 	*pp = end;
   2504 	*out_time = (time_t)n;	/* ignore possible truncation for now */
   2505 	return true;
   2506 }
   2507 
   2508 /* :gmtime and :localtime */
   2509 static ApplyModifierResult
   2510 ApplyModifier_Time(const char **pp, ModChain *ch)
   2511 {
   2512 	Expr *expr;
   2513 	time_t t;
   2514 	const char *args;
   2515 	const char *mod = *pp;
   2516 	bool gmt = mod[0] == 'g';
   2517 
   2518 	if (!ModMatchEq(mod, gmt ? "gmtime" : "localtime", ch))
   2519 		return AMR_UNKNOWN;
   2520 	args = mod + (gmt ? 6 : 9);
   2521 
   2522 	if (args[0] == '=') {
   2523 		const char *p = args + 1;
   2524 		LazyBuf buf;
   2525 		FStr arg;
   2526 		if (!ParseModifierPartSubst(&p, true, '\0', ch->expr->emode,
   2527 		    ch, &buf, NULL, NULL))
   2528 			return AMR_CLEANUP;
   2529 		arg = LazyBuf_DoneGet(&buf);
   2530 		if (ModChain_ShouldEval(ch)) {
   2531 			const char *arg_p = arg.str;
   2532 			if (!TryParseTime(&arg_p, &t) || *arg_p != '\0') {
   2533 				Parse_Error(PARSE_FATAL,
   2534 				    "Invalid time value \"%s\"", arg.str);
   2535 				FStr_Done(&arg);
   2536 				return AMR_CLEANUP;
   2537 			}
   2538 		} else
   2539 			t = 0;
   2540 		FStr_Done(&arg);
   2541 		*pp = p;
   2542 	} else {
   2543 		t = 0;
   2544 		*pp = args;
   2545 	}
   2546 
   2547 	expr = ch->expr;
   2548 	if (Expr_ShouldEval(expr))
   2549 		Expr_SetValueOwn(expr, FormatTime(Expr_Str(expr), t, gmt));
   2550 
   2551 	return AMR_OK;
   2552 }
   2553 
   2554 /* :hash */
   2555 static ApplyModifierResult
   2556 ApplyModifier_Hash(const char **pp, ModChain *ch)
   2557 {
   2558 	if (!ModMatch(*pp, "hash", ch))
   2559 		return AMR_UNKNOWN;
   2560 	*pp += 4;
   2561 
   2562 	if (ModChain_ShouldEval(ch))
   2563 		Expr_SetValueOwn(ch->expr, Hash(Expr_Str(ch->expr)));
   2564 
   2565 	return AMR_OK;
   2566 }
   2567 
   2568 /* :P */
   2569 static ApplyModifierResult
   2570 ApplyModifier_Path(const char **pp, ModChain *ch)
   2571 {
   2572 	Expr *expr = ch->expr;
   2573 	GNode *gn;
   2574 	char *path;
   2575 
   2576 	(*pp)++;
   2577 
   2578 	if (!Expr_ShouldEval(expr))
   2579 		return AMR_OK;
   2580 
   2581 	Expr_Define(expr);
   2582 
   2583 	gn = Targ_FindNode(expr->name);
   2584 	if (gn == NULL || gn->type & OP_NOPATH)
   2585 		path = NULL;
   2586 	else if (gn->path != NULL)
   2587 		path = bmake_strdup(gn->path);
   2588 	else {
   2589 		SearchPath *searchPath = Suff_FindPath(gn);
   2590 		path = Dir_FindFile(expr->name, searchPath);
   2591 	}
   2592 	if (path == NULL)
   2593 		path = bmake_strdup(expr->name);
   2594 	Expr_SetValueOwn(expr, path);
   2595 
   2596 	return AMR_OK;
   2597 }
   2598 
   2599 /* :!cmd! */
   2600 static ApplyModifierResult
   2601 ApplyModifier_ShellCommand(const char **pp, ModChain *ch)
   2602 {
   2603 	Expr *expr = ch->expr;
   2604 	LazyBuf cmdBuf;
   2605 	FStr cmd;
   2606 
   2607 	(*pp)++;
   2608 	if (!ParseModifierPart(pp, '!', expr->emode, ch, &cmdBuf))
   2609 		return AMR_CLEANUP;
   2610 	cmd = LazyBuf_DoneGet(&cmdBuf);
   2611 
   2612 	if (Expr_ShouldEval(expr)) {
   2613 		char *output, *error;
   2614 		output = Cmd_Exec(cmd.str, &error);
   2615 		Expr_SetValueOwn(expr, output);
   2616 		if (error != NULL) {
   2617 			/* XXX: why still return AMR_OK? */
   2618 			Error("%s", error);
   2619 			free(error);
   2620 		}
   2621 	} else
   2622 		Expr_SetValueRefer(expr, "");
   2623 
   2624 	FStr_Done(&cmd);
   2625 	Expr_Define(expr);
   2626 
   2627 	return AMR_OK;
   2628 }
   2629 
   2630 /*
   2631  * The :range modifier generates an integer sequence as long as the words.
   2632  * The :range=7 modifier generates an integer sequence from 1 to 7.
   2633  */
   2634 static ApplyModifierResult
   2635 ApplyModifier_Range(const char **pp, ModChain *ch)
   2636 {
   2637 	size_t n;
   2638 	Buffer buf;
   2639 	size_t i;
   2640 
   2641 	const char *mod = *pp;
   2642 	if (!ModMatchEq(mod, "range", ch))
   2643 		return AMR_UNKNOWN;
   2644 
   2645 	if (mod[5] == '=') {
   2646 		const char *p = mod + 6;
   2647 		if (!TryParseSize(&p, &n)) {
   2648 			Parse_Error(PARSE_FATAL,
   2649 			    "Invalid number \"%s\" for ':range' modifier",
   2650 			    mod + 6);
   2651 			return AMR_CLEANUP;
   2652 		}
   2653 		*pp = p;
   2654 	} else {
   2655 		n = 0;
   2656 		*pp = mod + 5;
   2657 	}
   2658 
   2659 	if (!ModChain_ShouldEval(ch))
   2660 		return AMR_OK;
   2661 
   2662 	if (n == 0) {
   2663 		SubstringWords words = Expr_Words(ch->expr);
   2664 		n = words.len;
   2665 		SubstringWords_Free(words);
   2666 	}
   2667 
   2668 	Buf_Init(&buf);
   2669 
   2670 	for (i = 0; i < n; i++) {
   2671 		if (i != 0) {
   2672 			/*
   2673 			 * XXX: Use ch->sep instead of ' ', for consistency.
   2674 			 */
   2675 			Buf_AddByte(&buf, ' ');
   2676 		}
   2677 		Buf_AddInt(&buf, 1 + (int)i);
   2678 	}
   2679 
   2680 	Expr_SetValueOwn(ch->expr, Buf_DoneData(&buf));
   2681 	return AMR_OK;
   2682 }
   2683 
   2684 /* Parse a ':M' or ':N' modifier. */
   2685 static char *
   2686 ParseModifier_Match(const char **pp, const ModChain *ch)
   2687 {
   2688 	const char *mod = *pp;
   2689 	Expr *expr = ch->expr;
   2690 	bool copy = false;	/* pattern should be, or has been, copied */
   2691 	bool needSubst = false;
   2692 	const char *endpat;
   2693 	char *pattern;
   2694 
   2695 	/*
   2696 	 * In the loop below, ignore ':' unless we are at (or back to) the
   2697 	 * original brace level.
   2698 	 * XXX: This will likely not work right if $() and ${} are intermixed.
   2699 	 */
   2700 	/*
   2701 	 * XXX: This code is similar to the one in Var_Parse.
   2702 	 * See if the code can be merged.
   2703 	 * See also ApplyModifier_Defined.
   2704 	 */
   2705 	int depth = 0;
   2706 	const char *p;
   2707 	for (p = mod + 1; *p != '\0' && !(*p == ':' && depth == 0); p++) {
   2708 		if (*p == '\\' && p[1] != '\0' &&
   2709 		    (IsDelimiter(p[1], ch) || p[1] == ch->startc)) {
   2710 			if (!needSubst)
   2711 				copy = true;
   2712 			p++;
   2713 			continue;
   2714 		}
   2715 		if (*p == '$')
   2716 			needSubst = true;
   2717 		if (*p == '(' || *p == '{')
   2718 			depth++;
   2719 		if (*p == ')' || *p == '}') {
   2720 			depth--;
   2721 			if (depth < 0)
   2722 				break;
   2723 		}
   2724 	}
   2725 	*pp = p;
   2726 	endpat = p;
   2727 
   2728 	if (copy) {
   2729 		char *dst;
   2730 		const char *src;
   2731 
   2732 		/* Compress the \:'s out of the pattern. */
   2733 		pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1);
   2734 		dst = pattern;
   2735 		src = mod + 1;
   2736 		for (; src < endpat; src++, dst++) {
   2737 			if (src[0] == '\\' && src + 1 < endpat &&
   2738 			    /* XXX: ch->startc is missing here; see above */
   2739 			    IsDelimiter(src[1], ch))
   2740 				src++;
   2741 			*dst = *src;
   2742 		}
   2743 		*dst = '\0';
   2744 	} else {
   2745 		pattern = bmake_strsedup(mod + 1, endpat);
   2746 	}
   2747 
   2748 	if (needSubst) {
   2749 		char *old_pattern = pattern;
   2750 		/*
   2751 		 * XXX: Contrary to ParseModifierPart, a dollar in a ':M' or
   2752 		 * ':N' modifier must be escaped as '$$', not as '\$'.
   2753 		 */
   2754 		pattern = Var_Subst(pattern, expr->scope, expr->emode);
   2755 		/* TODO: handle errors */
   2756 		free(old_pattern);
   2757 	}
   2758 
   2759 	DEBUG2(VAR, "Pattern for ':%c' is \"%s\"\n", mod[0], pattern);
   2760 
   2761 	return pattern;
   2762 }
   2763 
   2764 struct ModifyWord_MatchArgs {
   2765 	const char *pattern;
   2766 	bool neg;
   2767 	bool error_reported;
   2768 };
   2769 
   2770 static void
   2771 ModifyWord_Match(Substring word, SepBuf *buf, void *data)
   2772 {
   2773 	struct ModifyWord_MatchArgs *args = data;
   2774 	StrMatchResult res;
   2775 	assert(word.end[0] == '\0');	/* assume null-terminated word */
   2776 	res = Str_Match(word.start, args->pattern);
   2777 	if (res.error != NULL && !args->error_reported) {
   2778 		args->error_reported = true;
   2779 		Parse_Error(PARSE_WARNING,
   2780 		    "%s in pattern '%s' of modifier '%s'",
   2781 		    res.error, args->pattern, args->neg ? ":N" : ":M");
   2782 	}
   2783 	if (res.matched != args->neg)
   2784 		SepBuf_AddSubstring(buf, word);
   2785 }
   2786 
   2787 /* :Mpattern or :Npattern */
   2788 static ApplyModifierResult
   2789 ApplyModifier_Match(const char **pp, ModChain *ch)
   2790 {
   2791 	char mod = **pp;
   2792 	char *pattern;
   2793 
   2794 	pattern = ParseModifier_Match(pp, ch);
   2795 
   2796 	if (ModChain_ShouldEval(ch)) {
   2797 		struct ModifyWord_MatchArgs args;
   2798 		args.pattern = pattern;
   2799 		args.neg = mod == 'N';
   2800 		args.error_reported = false;
   2801 		ModifyWords(ch, ModifyWord_Match, &args, ch->oneBigWord);
   2802 	}
   2803 
   2804 	free(pattern);
   2805 	return AMR_OK;
   2806 }
   2807 
   2808 struct ModifyWord_MtimeArgs {
   2809 	bool error;
   2810 	bool use_fallback;
   2811 	ApplyModifierResult rc;
   2812 	time_t fallback;
   2813 };
   2814 
   2815 static void
   2816 ModifyWord_Mtime(Substring word, SepBuf *buf, void *data)
   2817 {
   2818 	struct ModifyWord_MtimeArgs *args = data;
   2819 	struct stat st;
   2820 	char tbuf[21];
   2821 
   2822 	if (Substring_IsEmpty(word))
   2823 		return;
   2824 	assert(word.end[0] == '\0');	/* assume null-terminated word */
   2825 	if (stat(word.start, &st) < 0) {
   2826 		if (args->error) {
   2827 			Parse_Error(PARSE_FATAL,
   2828 			    "Cannot determine mtime for '%s': %s",
   2829 			    word.start, strerror(errno));
   2830 			args->rc = AMR_CLEANUP;
   2831 			return;
   2832 		}
   2833 		if (args->use_fallback)
   2834 			st.st_mtime = args->fallback;
   2835 		else
   2836 			time(&st.st_mtime);
   2837 	}
   2838 	snprintf(tbuf, sizeof(tbuf), "%u", (unsigned)st.st_mtime);
   2839 	SepBuf_AddStr(buf, tbuf);
   2840 }
   2841 
   2842 /* :mtime */
   2843 static ApplyModifierResult
   2844 ApplyModifier_Mtime(const char **pp, ModChain *ch)
   2845 {
   2846 	const char *p, *mod = *pp;
   2847 	struct ModifyWord_MtimeArgs args;
   2848 
   2849 	if (!ModMatchEq(mod, "mtime", ch))
   2850 		return AMR_UNKNOWN;
   2851 	*pp += 5;
   2852 	p = *pp;
   2853 	args.error = false;
   2854 	args.use_fallback = p[0] == '=';
   2855 	args.rc = AMR_OK;
   2856 	if (args.use_fallback) {
   2857 		p++;
   2858 		if (TryParseTime(&p, &args.fallback)) {
   2859 		} else if (strncmp(p, "error", 5) == 0) {
   2860 			p += 5;
   2861 			args.error = true;
   2862 		} else
   2863 			goto invalid_argument;
   2864 		if (!IsDelimiter(*p, ch))
   2865 			goto invalid_argument;
   2866 		*pp = p;
   2867 	}
   2868 	ModifyWords(ch, ModifyWord_Mtime, &args, ch->oneBigWord);
   2869 	return args.rc;
   2870 
   2871 invalid_argument:
   2872 	Parse_Error(PARSE_FATAL,
   2873 	    "Invalid argument '%.*s' for modifier ':mtime'",
   2874 	    (int)strcspn(*pp + 1, ":{}()"), *pp + 1);
   2875 	return AMR_CLEANUP;
   2876 }
   2877 
   2878 static void
   2879 ParsePatternFlags(const char **pp, PatternFlags *pflags, bool *oneBigWord)
   2880 {
   2881 	for (;; (*pp)++) {
   2882 		if (**pp == 'g')
   2883 			pflags->subGlobal = true;
   2884 		else if (**pp == '1')
   2885 			pflags->subOnce = true;
   2886 		else if (**pp == 'W')
   2887 			*oneBigWord = true;
   2888 		else
   2889 			break;
   2890 	}
   2891 }
   2892 
   2893 MAKE_INLINE PatternFlags
   2894 PatternFlags_None(void)
   2895 {
   2896 	PatternFlags pflags = { false, false, false, false };
   2897 	return pflags;
   2898 }
   2899 
   2900 /* :S,from,to, */
   2901 static ApplyModifierResult
   2902 ApplyModifier_Subst(const char **pp, ModChain *ch)
   2903 {
   2904 	struct ModifyWord_SubstArgs args;
   2905 	bool oneBigWord;
   2906 	LazyBuf lhsBuf, rhsBuf;
   2907 
   2908 	char delim = (*pp)[1];
   2909 	if (delim == '\0') {
   2910 		Error("Missing delimiter for modifier ':S'");
   2911 		(*pp)++;
   2912 		return AMR_CLEANUP;
   2913 	}
   2914 
   2915 	*pp += 2;
   2916 
   2917 	args.pflags = PatternFlags_None();
   2918 	args.matched = false;
   2919 
   2920 	if (**pp == '^') {
   2921 		args.pflags.anchorStart = true;
   2922 		(*pp)++;
   2923 	}
   2924 
   2925 	if (!ParseModifierPartSubst(pp,
   2926 	    false, delim, ch->expr->emode, ch, &lhsBuf, &args.pflags, NULL))
   2927 		return AMR_CLEANUP;
   2928 	args.lhs = LazyBuf_Get(&lhsBuf);
   2929 
   2930 	if (!ParseModifierPartSubst(pp,
   2931 	    false, delim, ch->expr->emode, ch, &rhsBuf, NULL, &args)) {
   2932 		LazyBuf_Done(&lhsBuf);
   2933 		return AMR_CLEANUP;
   2934 	}
   2935 	args.rhs = LazyBuf_Get(&rhsBuf);
   2936 
   2937 	oneBigWord = ch->oneBigWord;
   2938 	ParsePatternFlags(pp, &args.pflags, &oneBigWord);
   2939 
   2940 	ModifyWords(ch, ModifyWord_Subst, &args, oneBigWord);
   2941 
   2942 	LazyBuf_Done(&lhsBuf);
   2943 	LazyBuf_Done(&rhsBuf);
   2944 	return AMR_OK;
   2945 }
   2946 
   2947 /* :C,from,to, */
   2948 static ApplyModifierResult
   2949 ApplyModifier_Regex(const char **pp, ModChain *ch)
   2950 {
   2951 	struct ModifyWord_SubstRegexArgs args;
   2952 	bool oneBigWord;
   2953 	int error;
   2954 	LazyBuf reBuf, replaceBuf;
   2955 	FStr re;
   2956 
   2957 	char delim = (*pp)[1];
   2958 	if (delim == '\0') {
   2959 		Error("Missing delimiter for :C modifier");
   2960 		(*pp)++;
   2961 		return AMR_CLEANUP;
   2962 	}
   2963 
   2964 	*pp += 2;
   2965 
   2966 	if (!ParseModifierPart(pp, delim, ch->expr->emode, ch, &reBuf))
   2967 		return AMR_CLEANUP;
   2968 	re = LazyBuf_DoneGet(&reBuf);
   2969 
   2970 	if (!ParseModifierPart(pp, delim, ch->expr->emode, ch, &replaceBuf)) {
   2971 		FStr_Done(&re);
   2972 		return AMR_CLEANUP;
   2973 	}
   2974 	args.replace = LazyBuf_Get(&replaceBuf);
   2975 
   2976 	args.pflags = PatternFlags_None();
   2977 	args.matched = false;
   2978 	oneBigWord = ch->oneBigWord;
   2979 	ParsePatternFlags(pp, &args.pflags, &oneBigWord);
   2980 
   2981 	if (!ModChain_ShouldEval(ch))
   2982 		goto done;
   2983 
   2984 	error = regcomp(&args.re, re.str, REG_EXTENDED);
   2985 	if (error != 0) {
   2986 		RegexError(error, &args.re, "Regex compilation error");
   2987 		LazyBuf_Done(&replaceBuf);
   2988 		FStr_Done(&re);
   2989 		return AMR_CLEANUP;
   2990 	}
   2991 
   2992 	args.nsub = args.re.re_nsub + 1;
   2993 	if (args.nsub > 10)
   2994 		args.nsub = 10;
   2995 
   2996 	ModifyWords(ch, ModifyWord_SubstRegex, &args, oneBigWord);
   2997 
   2998 	regfree(&args.re);
   2999 done:
   3000 	LazyBuf_Done(&replaceBuf);
   3001 	FStr_Done(&re);
   3002 	return AMR_OK;
   3003 }
   3004 
   3005 /* :Q, :q */
   3006 static ApplyModifierResult
   3007 ApplyModifier_Quote(const char **pp, ModChain *ch)
   3008 {
   3009 	LazyBuf buf;
   3010 	bool quoteDollar;
   3011 
   3012 	quoteDollar = **pp == 'q';
   3013 	if (!IsDelimiter((*pp)[1], ch))
   3014 		return AMR_UNKNOWN;
   3015 	(*pp)++;
   3016 
   3017 	if (!ModChain_ShouldEval(ch))
   3018 		return AMR_OK;
   3019 
   3020 	QuoteShell(Expr_Str(ch->expr), quoteDollar, &buf);
   3021 	if (buf.data != NULL)
   3022 		Expr_SetValue(ch->expr, LazyBuf_DoneGet(&buf));
   3023 	else
   3024 		LazyBuf_Done(&buf);
   3025 
   3026 	return AMR_OK;
   3027 }
   3028 
   3029 /*ARGSUSED*/
   3030 static void
   3031 ModifyWord_Copy(Substring word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
   3032 {
   3033 	SepBuf_AddSubstring(buf, word);
   3034 }
   3035 
   3036 /* :ts<separator> */
   3037 static ApplyModifierResult
   3038 ApplyModifier_ToSep(const char **pp, ModChain *ch)
   3039 {
   3040 	const char *sep = *pp + 2;
   3041 
   3042 	/*
   3043 	 * Even in parse-only mode, apply the side effects, since the side
   3044 	 * effects are neither observable nor is there a performance penalty.
   3045 	 * Checking for wantRes for every single piece of code in here
   3046 	 * would make the code in this function too hard to read.
   3047 	 */
   3048 
   3049 	/* ":ts<any><endc>" or ":ts<any>:" */
   3050 	if (sep[0] != ch->endc && IsDelimiter(sep[1], ch)) {
   3051 		*pp = sep + 1;
   3052 		ch->sep = sep[0];
   3053 		goto ok;
   3054 	}
   3055 
   3056 	/* ":ts<endc>" or ":ts:" */
   3057 	if (IsDelimiter(sep[0], ch)) {
   3058 		*pp = sep;
   3059 		ch->sep = '\0';	/* no separator */
   3060 		goto ok;
   3061 	}
   3062 
   3063 	/* ":ts<unrecognized><unrecognized>". */
   3064 	if (sep[0] != '\\') {
   3065 		(*pp)++;	/* just for backwards compatibility */
   3066 		return AMR_BAD;
   3067 	}
   3068 
   3069 	/* ":ts\n" */
   3070 	if (sep[1] == 'n') {
   3071 		*pp = sep + 2;
   3072 		ch->sep = '\n';
   3073 		goto ok;
   3074 	}
   3075 
   3076 	/* ":ts\t" */
   3077 	if (sep[1] == 't') {
   3078 		*pp = sep + 2;
   3079 		ch->sep = '\t';
   3080 		goto ok;
   3081 	}
   3082 
   3083 	/* ":ts\x40" or ":ts\100" */
   3084 	{
   3085 		const char *p = sep + 1;
   3086 		int base = 8;	/* assume octal */
   3087 
   3088 		if (sep[1] == 'x') {
   3089 			base = 16;
   3090 			p++;
   3091 		} else if (!ch_isdigit(sep[1])) {
   3092 			(*pp)++;	/* just for backwards compatibility */
   3093 			return AMR_BAD;	/* ":ts<backslash><unrecognized>". */
   3094 		}
   3095 
   3096 		if (!TryParseChar(&p, base, &ch->sep)) {
   3097 			Parse_Error(PARSE_FATAL,
   3098 			    "Invalid character number at \"%s\"", p);
   3099 			return AMR_CLEANUP;
   3100 		}
   3101 		if (!IsDelimiter(*p, ch)) {
   3102 			(*pp)++;	/* just for backwards compatibility */
   3103 			return AMR_BAD;
   3104 		}
   3105 
   3106 		*pp = p;
   3107 	}
   3108 
   3109 ok:
   3110 	ModifyWords(ch, ModifyWord_Copy, NULL, ch->oneBigWord);
   3111 	return AMR_OK;
   3112 }
   3113 
   3114 static char *
   3115 str_toupper(const char *str)
   3116 {
   3117 	size_t i, n = strlen(str) + 1;
   3118 	char *res = bmake_malloc(n);
   3119 	for (i = 0; i < n; i++)
   3120 		res[i] = ch_toupper(str[i]);
   3121 	return res;
   3122 }
   3123 
   3124 static char *
   3125 str_tolower(const char *str)
   3126 {
   3127 	size_t i, n = strlen(str) + 1;
   3128 	char *res = bmake_malloc(n);
   3129 	for (i = 0; i < n; i++)
   3130 		res[i] = ch_tolower(str[i]);
   3131 	return res;
   3132 }
   3133 
   3134 /* :tA, :tu, :tl, :ts<separator>, etc. */
   3135 static ApplyModifierResult
   3136 ApplyModifier_To(const char **pp, ModChain *ch)
   3137 {
   3138 	Expr *expr = ch->expr;
   3139 	const char *mod = *pp;
   3140 	assert(mod[0] == 't');
   3141 
   3142 	if (IsDelimiter(mod[1], ch)) {
   3143 		*pp = mod + 1;
   3144 		return AMR_BAD;	/* Found ":t<endc>" or ":t:". */
   3145 	}
   3146 
   3147 	if (mod[1] == 's')
   3148 		return ApplyModifier_ToSep(pp, ch);
   3149 
   3150 	if (!IsDelimiter(mod[2], ch)) {			/* :t<any><any> */
   3151 		*pp = mod + 1;
   3152 		return AMR_BAD;
   3153 	}
   3154 
   3155 	if (mod[1] == 'A') {				/* :tA */
   3156 		*pp = mod + 2;
   3157 		ModifyWords(ch, ModifyWord_Realpath, NULL, ch->oneBigWord);
   3158 		return AMR_OK;
   3159 	}
   3160 
   3161 	if (mod[1] == 'u') {				/* :tu */
   3162 		*pp = mod + 2;
   3163 		if (Expr_ShouldEval(expr))
   3164 			Expr_SetValueOwn(expr, str_toupper(Expr_Str(expr)));
   3165 		return AMR_OK;
   3166 	}
   3167 
   3168 	if (mod[1] == 'l') {				/* :tl */
   3169 		*pp = mod + 2;
   3170 		if (Expr_ShouldEval(expr))
   3171 			Expr_SetValueOwn(expr, str_tolower(Expr_Str(expr)));
   3172 		return AMR_OK;
   3173 	}
   3174 
   3175 	if (mod[1] == 'W' || mod[1] == 'w') {		/* :tW, :tw */
   3176 		*pp = mod + 2;
   3177 		ch->oneBigWord = mod[1] == 'W';
   3178 		return AMR_OK;
   3179 	}
   3180 
   3181 	/* Found ":t<unrecognized>:" or ":t<unrecognized><endc>". */
   3182 	*pp = mod + 1;		/* XXX: unnecessary but observable */
   3183 	return AMR_BAD;
   3184 }
   3185 
   3186 /* :[#], :[1], :[-1..1], etc. */
   3187 static ApplyModifierResult
   3188 ApplyModifier_Words(const char **pp, ModChain *ch)
   3189 {
   3190 	Expr *expr = ch->expr;
   3191 	int first, last;
   3192 	const char *p;
   3193 	LazyBuf argBuf;
   3194 	FStr arg;
   3195 
   3196 	(*pp)++;		/* skip the '[' */
   3197 	if (!ParseModifierPart(pp, ']', expr->emode, ch, &argBuf))
   3198 		return AMR_CLEANUP;
   3199 	arg = LazyBuf_DoneGet(&argBuf);
   3200 	p = arg.str;
   3201 
   3202 	if (!IsDelimiter(**pp, ch))
   3203 		goto bad_modifier;		/* Found junk after ']' */
   3204 
   3205 	if (!ModChain_ShouldEval(ch))
   3206 		goto ok;
   3207 
   3208 	if (p[0] == '\0')
   3209 		goto bad_modifier;		/* Found ":[]". */
   3210 
   3211 	if (strcmp(p, "#") == 0) {		/* Found ":[#]" */
   3212 		if (ch->oneBigWord)
   3213 			Expr_SetValueRefer(expr, "1");
   3214 		else {
   3215 			Buffer buf;
   3216 
   3217 			SubstringWords words = Expr_Words(expr);
   3218 			size_t ac = words.len;
   3219 			SubstringWords_Free(words);
   3220 
   3221 			Buf_Init(&buf);
   3222 			Buf_AddInt(&buf, (int)ac);
   3223 			Expr_SetValueOwn(expr, Buf_DoneData(&buf));
   3224 		}
   3225 		goto ok;
   3226 	}
   3227 
   3228 	if (strcmp(p, "*") == 0) {		/* ":[*]" */
   3229 		ch->oneBigWord = true;
   3230 		goto ok;
   3231 	}
   3232 
   3233 	if (strcmp(p, "@") == 0) {		/* ":[@]" */
   3234 		ch->oneBigWord = false;
   3235 		goto ok;
   3236 	}
   3237 
   3238 	/* Expect ":[N]" or ":[start..end]" */
   3239 	if (!TryParseIntBase0(&p, &first))
   3240 		goto bad_modifier;
   3241 
   3242 	if (p[0] == '\0')			/* ":[N]" */
   3243 		last = first;
   3244 	else if (strncmp(p, "..", 2) == 0) {
   3245 		p += 2;
   3246 		if (!TryParseIntBase0(&p, &last) || *p != '\0')
   3247 			goto bad_modifier;
   3248 	} else
   3249 		goto bad_modifier;
   3250 
   3251 	if (first == 0 && last == 0) {		/* ":[0]" or ":[0..0]" */
   3252 		ch->oneBigWord = true;
   3253 		goto ok;
   3254 	}
   3255 
   3256 	if (first == 0 || last == 0)		/* ":[0..N]" or ":[N..0]" */
   3257 		goto bad_modifier;
   3258 
   3259 	Expr_SetValueOwn(expr,
   3260 	    VarSelectWords(Expr_Str(expr), first, last,
   3261 		ch->sep, ch->oneBigWord));
   3262 
   3263 ok:
   3264 	FStr_Done(&arg);
   3265 	return AMR_OK;
   3266 
   3267 bad_modifier:
   3268 	FStr_Done(&arg);
   3269 	return AMR_BAD;
   3270 }
   3271 
   3272 #if __STDC_VERSION__ >= 199901L
   3273 # define NUM_TYPE long long
   3274 # define PARSE_NUM_TYPE strtoll
   3275 #else
   3276 # define NUM_TYPE long
   3277 # define PARSE_NUM_TYPE strtol
   3278 #endif
   3279 
   3280 static NUM_TYPE
   3281 num_val(Substring s)
   3282 {
   3283 	NUM_TYPE val;
   3284 	char *ep;
   3285 
   3286 	val = PARSE_NUM_TYPE(s.start, &ep, 0);
   3287 	if (ep != s.start) {
   3288 		switch (*ep) {
   3289 		case 'K':
   3290 		case 'k':
   3291 			val <<= 10;
   3292 			break;
   3293 		case 'M':
   3294 		case 'm':
   3295 			val <<= 20;
   3296 			break;
   3297 		case 'G':
   3298 		case 'g':
   3299 			val <<= 30;
   3300 			break;
   3301 		}
   3302 	}
   3303 	return val;
   3304 }
   3305 
   3306 static int
   3307 SubNumAsc(const void *sa, const void *sb)
   3308 {
   3309 	NUM_TYPE a, b;
   3310 
   3311 	a = num_val(*((const Substring *)sa));
   3312 	b = num_val(*((const Substring *)sb));
   3313 	return a > b ? 1 : b > a ? -1 : 0;
   3314 }
   3315 
   3316 static int
   3317 SubNumDesc(const void *sa, const void *sb)
   3318 {
   3319 	return SubNumAsc(sb, sa);
   3320 }
   3321 
   3322 static int
   3323 Substring_Cmp(Substring a, Substring b)
   3324 {
   3325 	for (; a.start < a.end && b.start < b.end; a.start++, b.start++)
   3326 		if (a.start[0] != b.start[0])
   3327 			return (unsigned char)a.start[0]
   3328 			    - (unsigned char)b.start[0];
   3329 	return (int)((a.end - a.start) - (b.end - b.start));
   3330 }
   3331 
   3332 static int
   3333 SubStrAsc(const void *sa, const void *sb)
   3334 {
   3335 	return Substring_Cmp(*(const Substring *)sa, *(const Substring *)sb);
   3336 }
   3337 
   3338 static int
   3339 SubStrDesc(const void *sa, const void *sb)
   3340 {
   3341 	return SubStrAsc(sb, sa);
   3342 }
   3343 
   3344 static void
   3345 ShuffleSubstrings(Substring *strs, size_t n)
   3346 {
   3347 	size_t i;
   3348 
   3349 	for (i = n - 1; i > 0; i--) {
   3350 		size_t rndidx = (size_t)random() % (i + 1);
   3351 		Substring t = strs[i];
   3352 		strs[i] = strs[rndidx];
   3353 		strs[rndidx] = t;
   3354 	}
   3355 }
   3356 
   3357 /*
   3358  * :O		order ascending
   3359  * :Or		order descending
   3360  * :Ox		shuffle
   3361  * :On		numeric ascending
   3362  * :Onr, :Orn	numeric descending
   3363  */
   3364 static ApplyModifierResult
   3365 ApplyModifier_Order(const char **pp, ModChain *ch)
   3366 {
   3367 	const char *mod = *pp;
   3368 	SubstringWords words;
   3369 	int (*cmp)(const void *, const void *);
   3370 
   3371 	if (IsDelimiter(mod[1], ch)) {
   3372 		cmp = SubStrAsc;
   3373 		(*pp)++;
   3374 	} else if (IsDelimiter(mod[2], ch)) {
   3375 		if (mod[1] == 'n')
   3376 			cmp = SubNumAsc;
   3377 		else if (mod[1] == 'r')
   3378 			cmp = SubStrDesc;
   3379 		else if (mod[1] == 'x')
   3380 			cmp = NULL;
   3381 		else
   3382 			goto bad;
   3383 		*pp += 2;
   3384 	} else if (IsDelimiter(mod[3], ch)) {
   3385 		if ((mod[1] == 'n' && mod[2] == 'r') ||
   3386 		    (mod[1] == 'r' && mod[2] == 'n'))
   3387 			cmp = SubNumDesc;
   3388 		else
   3389 			goto bad;
   3390 		*pp += 3;
   3391 	} else
   3392 		goto bad;
   3393 
   3394 	if (!ModChain_ShouldEval(ch))
   3395 		return AMR_OK;
   3396 
   3397 	words = Expr_Words(ch->expr);
   3398 	if (cmp == NULL)
   3399 		ShuffleSubstrings(words.words, words.len);
   3400 	else {
   3401 		assert(words.words[0].end[0] == '\0');
   3402 		qsort(words.words, words.len, sizeof(words.words[0]), cmp);
   3403 	}
   3404 	Expr_SetValueOwn(ch->expr, SubstringWords_JoinFree(words));
   3405 
   3406 	return AMR_OK;
   3407 
   3408 bad:
   3409 	(*pp)++;
   3410 	return AMR_BAD;
   3411 }
   3412 
   3413 /* :? then : else */
   3414 static ApplyModifierResult
   3415 ApplyModifier_IfElse(const char **pp, ModChain *ch)
   3416 {
   3417 	Expr *expr = ch->expr;
   3418 	LazyBuf thenBuf;
   3419 	LazyBuf elseBuf;
   3420 
   3421 	VarEvalMode then_emode = VARE_PARSE_ONLY;
   3422 	VarEvalMode else_emode = VARE_PARSE_ONLY;
   3423 
   3424 	CondResult cond_rc = CR_TRUE;	/* just not CR_ERROR */
   3425 	if (Expr_ShouldEval(expr)) {
   3426 		cond_rc = Cond_EvalCondition(expr->name);
   3427 		if (cond_rc == CR_TRUE)
   3428 			then_emode = expr->emode;
   3429 		if (cond_rc == CR_FALSE)
   3430 			else_emode = expr->emode;
   3431 	}
   3432 
   3433 	(*pp)++;		/* skip past the '?' */
   3434 	if (!ParseModifierPart(pp, ':', then_emode, ch, &thenBuf))
   3435 		return AMR_CLEANUP;
   3436 
   3437 	if (!ParseModifierPart(pp, ch->endc, else_emode, ch, &elseBuf)) {
   3438 		LazyBuf_Done(&thenBuf);
   3439 		return AMR_CLEANUP;
   3440 	}
   3441 
   3442 	(*pp)--;		/* Go back to the ch->endc. */
   3443 
   3444 	if (cond_rc == CR_ERROR) {
   3445 		Substring thenExpr = LazyBuf_Get(&thenBuf);
   3446 		Substring elseExpr = LazyBuf_Get(&elseBuf);
   3447 		Error("Bad conditional expression '%s' before '?%.*s:%.*s'",
   3448 		    expr->name,
   3449 		    (int)Substring_Length(thenExpr), thenExpr.start,
   3450 		    (int)Substring_Length(elseExpr), elseExpr.start);
   3451 		LazyBuf_Done(&thenBuf);
   3452 		LazyBuf_Done(&elseBuf);
   3453 		return AMR_CLEANUP;
   3454 	}
   3455 
   3456 	if (!Expr_ShouldEval(expr)) {
   3457 		LazyBuf_Done(&thenBuf);
   3458 		LazyBuf_Done(&elseBuf);
   3459 	} else if (cond_rc == CR_TRUE) {
   3460 		Expr_SetValue(expr, LazyBuf_DoneGet(&thenBuf));
   3461 		LazyBuf_Done(&elseBuf);
   3462 	} else {
   3463 		LazyBuf_Done(&thenBuf);
   3464 		Expr_SetValue(expr, LazyBuf_DoneGet(&elseBuf));
   3465 	}
   3466 	Expr_Define(expr);
   3467 	return AMR_OK;
   3468 }
   3469 
   3470 /*
   3471  * The ::= modifiers are special in that they do not read the variable value
   3472  * but instead assign to that variable.  They always expand to an empty
   3473  * string.
   3474  *
   3475  * Their main purpose is in supporting .for loops that generate shell commands
   3476  * since an ordinary variable assignment at that point would terminate the
   3477  * dependency group for these targets.  For example:
   3478  *
   3479  * list-targets: .USE
   3480  * .for i in ${.TARGET} ${.TARGET:R}.gz
   3481  *	@${t::=$i}
   3482  *	@echo 'The target is ${t:T}.'
   3483  * .endfor
   3484  *
   3485  *	  ::=<str>	Assigns <str> as the new value of variable.
   3486  *	  ::?=<str>	Assigns <str> as value of variable if
   3487  *			it was not already set.
   3488  *	  ::+=<str>	Appends <str> to variable.
   3489  *	  ::!=<cmd>	Assigns output of <cmd> as the new value of
   3490  *			variable.
   3491  */
   3492 static ApplyModifierResult
   3493 ApplyModifier_Assign(const char **pp, ModChain *ch)
   3494 {
   3495 	Expr *expr = ch->expr;
   3496 	GNode *scope;
   3497 	FStr val;
   3498 	LazyBuf buf;
   3499 
   3500 	const char *mod = *pp;
   3501 	const char *op = mod + 1;
   3502 
   3503 	if (op[0] == '=')
   3504 		goto found_op;
   3505 	if ((op[0] == '+' || op[0] == '?' || op[0] == '!') && op[1] == '=')
   3506 		goto found_op;
   3507 	return AMR_UNKNOWN;	/* "::<unrecognized>" */
   3508 
   3509 found_op:
   3510 	if (expr->name[0] == '\0') {
   3511 		*pp = mod + 1;
   3512 		return AMR_BAD;
   3513 	}
   3514 
   3515 	*pp = mod + (op[0] != '=' ? 3 : 2);
   3516 
   3517 	if (!ParseModifierPart(pp, ch->endc, expr->emode, ch, &buf))
   3518 		return AMR_CLEANUP;
   3519 	val = LazyBuf_DoneGet(&buf);
   3520 
   3521 	(*pp)--;		/* Go back to the ch->endc. */
   3522 
   3523 	if (!Expr_ShouldEval(expr))
   3524 		goto done;
   3525 
   3526 	scope = expr->scope;	/* scope where v belongs */
   3527 	if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL
   3528 	    && VarFind(expr->name, expr->scope, false) == NULL)
   3529 		scope = SCOPE_GLOBAL;
   3530 
   3531 	if (op[0] == '+')
   3532 		Var_Append(scope, expr->name, val.str);
   3533 	else if (op[0] == '!') {
   3534 		char *output, *error;
   3535 		output = Cmd_Exec(val.str, &error);
   3536 		if (error != NULL) {
   3537 			Error("%s", error);
   3538 			free(error);
   3539 		} else
   3540 			Var_Set(scope, expr->name, output);
   3541 		free(output);
   3542 	} else if (op[0] == '?' && expr->defined == DEF_REGULAR) {
   3543 		/* Do nothing. */
   3544 	} else
   3545 		Var_Set(scope, expr->name, val.str);
   3546 
   3547 	Expr_SetValueRefer(expr, "");
   3548 
   3549 done:
   3550 	FStr_Done(&val);
   3551 	return AMR_OK;
   3552 }
   3553 
   3554 /*
   3555  * :_=...
   3556  * remember current value
   3557  */
   3558 static ApplyModifierResult
   3559 ApplyModifier_Remember(const char **pp, ModChain *ch)
   3560 {
   3561 	Expr *expr = ch->expr;
   3562 	const char *mod = *pp;
   3563 	FStr name;
   3564 
   3565 	if (!ModMatchEq(mod, "_", ch))
   3566 		return AMR_UNKNOWN;
   3567 
   3568 	name = FStr_InitRefer("_");
   3569 	if (mod[1] == '=') {
   3570 		/*
   3571 		 * XXX: This ad-hoc call to strcspn deviates from the usual
   3572 		 * behavior defined in ParseModifierPart.  This creates an
   3573 		 * unnecessary and undocumented inconsistency in make.
   3574 		 */
   3575 		const char *arg = mod + 2;
   3576 		size_t argLen = strcspn(arg, ":)}");
   3577 		*pp = arg + argLen;
   3578 		name = FStr_InitOwn(bmake_strldup(arg, argLen));
   3579 	} else
   3580 		*pp = mod + 1;
   3581 
   3582 	if (Expr_ShouldEval(expr))
   3583 		Var_Set(SCOPE_GLOBAL, name.str, Expr_Str(expr));
   3584 	FStr_Done(&name);
   3585 
   3586 	return AMR_OK;
   3587 }
   3588 
   3589 /*
   3590  * Apply the given function to each word of the variable value,
   3591  * for a single-letter modifier such as :H, :T.
   3592  */
   3593 static ApplyModifierResult
   3594 ApplyModifier_WordFunc(const char **pp, ModChain *ch,
   3595 		       ModifyWordProc modifyWord)
   3596 {
   3597 	if (!IsDelimiter((*pp)[1], ch))
   3598 		return AMR_UNKNOWN;
   3599 	(*pp)++;
   3600 
   3601 	ModifyWords(ch, modifyWord, NULL, ch->oneBigWord);
   3602 
   3603 	return AMR_OK;
   3604 }
   3605 
   3606 /* Remove adjacent duplicate words. */
   3607 static ApplyModifierResult
   3608 ApplyModifier_Unique(const char **pp, ModChain *ch)
   3609 {
   3610 	SubstringWords words;
   3611 
   3612 	if (!IsDelimiter((*pp)[1], ch))
   3613 		return AMR_UNKNOWN;
   3614 	(*pp)++;
   3615 
   3616 	if (!ModChain_ShouldEval(ch))
   3617 		return AMR_OK;
   3618 
   3619 	words = Expr_Words(ch->expr);
   3620 
   3621 	if (words.len > 1) {
   3622 		size_t di, si;
   3623 
   3624 		di = 0;
   3625 		for (si = 1; si < words.len; si++) {
   3626 			if (!Substring_Eq(words.words[si], words.words[di])) {
   3627 				di++;
   3628 				if (di != si)
   3629 					words.words[di] = words.words[si];
   3630 			}
   3631 		}
   3632 		words.len = di + 1;
   3633 	}
   3634 
   3635 	Expr_SetValueOwn(ch->expr, SubstringWords_JoinFree(words));
   3636 
   3637 	return AMR_OK;
   3638 }
   3639 
   3640 /* Test whether the modifier has the form '<lhs>=<rhs>'. */
   3641 static bool
   3642 IsSysVModifier(const char *p, char startc, char endc)
   3643 {
   3644 	bool eqFound = false;
   3645 
   3646 	int depth = 1;
   3647 	while (*p != '\0' && depth > 0) {
   3648 		if (*p == '=')	/* XXX: should also test depth == 1 */
   3649 			eqFound = true;
   3650 		else if (*p == endc)
   3651 			depth--;
   3652 		else if (*p == startc)
   3653 			depth++;
   3654 		if (depth > 0)
   3655 			p++;
   3656 	}
   3657 	return *p == endc && eqFound;
   3658 }
   3659 
   3660 /* :from=to */
   3661 static ApplyModifierResult
   3662 ApplyModifier_SysV(const char **pp, ModChain *ch)
   3663 {
   3664 	Expr *expr = ch->expr;
   3665 	LazyBuf lhsBuf, rhsBuf;
   3666 	FStr rhs;
   3667 	struct ModifyWord_SysVSubstArgs args;
   3668 	Substring lhs;
   3669 	const char *lhsSuffix;
   3670 
   3671 	const char *mod = *pp;
   3672 
   3673 	if (!IsSysVModifier(mod, ch->startc, ch->endc))
   3674 		return AMR_UNKNOWN;
   3675 
   3676 	if (!ParseModifierPart(pp, '=', expr->emode, ch, &lhsBuf))
   3677 		return AMR_CLEANUP;
   3678 
   3679 	/*
   3680 	 * The SysV modifier lasts until the end of the expression.
   3681 	 */
   3682 	if (!ParseModifierPart(pp, ch->endc, expr->emode, ch, &rhsBuf)) {
   3683 		LazyBuf_Done(&lhsBuf);
   3684 		return AMR_CLEANUP;
   3685 	}
   3686 	rhs = LazyBuf_DoneGet(&rhsBuf);
   3687 
   3688 	(*pp)--;		/* Go back to the ch->endc. */
   3689 
   3690 	/* Do not turn an empty expression into non-empty. */
   3691 	if (lhsBuf.len == 0 && Expr_Str(expr)[0] == '\0')
   3692 		goto done;
   3693 
   3694 	lhs = LazyBuf_Get(&lhsBuf);
   3695 	lhsSuffix = Substring_SkipFirst(lhs, '%');
   3696 
   3697 	args.scope = expr->scope;
   3698 	args.lhsPrefix = Substring_Init(lhs.start,
   3699 	    lhsSuffix != lhs.start ? lhsSuffix - 1 : lhs.start);
   3700 	args.lhsPercent = lhsSuffix != lhs.start;
   3701 	args.lhsSuffix = Substring_Init(lhsSuffix, lhs.end);
   3702 	args.rhs = rhs.str;
   3703 
   3704 	ModifyWords(ch, ModifyWord_SysVSubst, &args, ch->oneBigWord);
   3705 
   3706 done:
   3707 	LazyBuf_Done(&lhsBuf);
   3708 	FStr_Done(&rhs);
   3709 	return AMR_OK;
   3710 }
   3711 
   3712 /* :sh */
   3713 static ApplyModifierResult
   3714 ApplyModifier_SunShell(const char **pp, ModChain *ch)
   3715 {
   3716 	Expr *expr = ch->expr;
   3717 	const char *p = *pp;
   3718 	if (!(p[1] == 'h' && IsDelimiter(p[2], ch)))
   3719 		return AMR_UNKNOWN;
   3720 	*pp = p + 2;
   3721 
   3722 	if (Expr_ShouldEval(expr)) {
   3723 		char *output, *error;
   3724 		output = Cmd_Exec(Expr_Str(expr), &error);
   3725 		if (error != NULL) {
   3726 			Error("%s", error);
   3727 			free(error);
   3728 		}
   3729 		Expr_SetValueOwn(expr, output);
   3730 	}
   3731 
   3732 	return AMR_OK;
   3733 }
   3734 
   3735 /*
   3736  * In cases where the evaluation mode and the definedness are the "standard"
   3737  * ones, don't log them, to keep the logs readable.
   3738  */
   3739 static bool
   3740 ShouldLogInSimpleFormat(const Expr *expr)
   3741 {
   3742 	return (expr->emode == VARE_WANTRES ||
   3743 		expr->emode == VARE_UNDEFERR) &&
   3744 	       expr->defined == DEF_REGULAR;
   3745 }
   3746 
   3747 static void
   3748 LogBeforeApply(const ModChain *ch, const char *mod)
   3749 {
   3750 	const Expr *expr = ch->expr;
   3751 	bool is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], ch);
   3752 
   3753 	/*
   3754 	 * At this point, only the first character of the modifier can
   3755 	 * be used since the end of the modifier is not yet known.
   3756 	 */
   3757 
   3758 	if (!Expr_ShouldEval(expr)) {
   3759 		debug_printf("Parsing modifier ${%s:%c%s}\n",
   3760 		    expr->name, mod[0], is_single_char ? "" : "...");
   3761 		return;
   3762 	}
   3763 
   3764 	if (ShouldLogInSimpleFormat(expr)) {
   3765 		debug_printf(
   3766 		    "Evaluating modifier ${%s:%c%s} on value \"%s\"\n",
   3767 		    expr->name, mod[0], is_single_char ? "" : "...",
   3768 		    Expr_Str(expr));
   3769 		return;
   3770 	}
   3771 
   3772 	debug_printf(
   3773 	    "Evaluating modifier ${%s:%c%s} on value \"%s\" (%s, %s)\n",
   3774 	    expr->name, mod[0], is_single_char ? "" : "...", Expr_Str(expr),
   3775 	    VarEvalMode_Name[expr->emode], ExprDefined_Name[expr->defined]);
   3776 }
   3777 
   3778 static void
   3779 LogAfterApply(const ModChain *ch, const char *p, const char *mod)
   3780 {
   3781 	const Expr *expr = ch->expr;
   3782 	const char *value = Expr_Str(expr);
   3783 	const char *quot = value == var_Error ? "" : "\"";
   3784 
   3785 	if (ShouldLogInSimpleFormat(expr)) {
   3786 		debug_printf("Result of ${%s:%.*s} is %s%s%s\n",
   3787 		    expr->name, (int)(p - mod), mod,
   3788 		    quot, value == var_Error ? "error" : value, quot);
   3789 		return;
   3790 	}
   3791 
   3792 	debug_printf("Result of ${%s:%.*s} is %s%s%s (%s, %s)\n",
   3793 	    expr->name, (int)(p - mod), mod,
   3794 	    quot, value == var_Error ? "error" : value, quot,
   3795 	    VarEvalMode_Name[expr->emode],
   3796 	    ExprDefined_Name[expr->defined]);
   3797 }
   3798 
   3799 static ApplyModifierResult
   3800 ApplyModifier(const char **pp, ModChain *ch)
   3801 {
   3802 	switch (**pp) {
   3803 	case '!':
   3804 		return ApplyModifier_ShellCommand(pp, ch);
   3805 	case ':':
   3806 		return ApplyModifier_Assign(pp, ch);
   3807 	case '?':
   3808 		return ApplyModifier_IfElse(pp, ch);
   3809 	case '@':
   3810 		return ApplyModifier_Loop(pp, ch);
   3811 	case '[':
   3812 		return ApplyModifier_Words(pp, ch);
   3813 	case '_':
   3814 		return ApplyModifier_Remember(pp, ch);
   3815 	case 'C':
   3816 		return ApplyModifier_Regex(pp, ch);
   3817 	case 'D':
   3818 	case 'U':
   3819 		return ApplyModifier_Defined(pp, ch);
   3820 	case 'E':
   3821 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Suffix);
   3822 	case 'g':
   3823 	case 'l':
   3824 		return ApplyModifier_Time(pp, ch);
   3825 	case 'H':
   3826 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Head);
   3827 	case 'h':
   3828 		return ApplyModifier_Hash(pp, ch);
   3829 	case 'L':
   3830 		return ApplyModifier_Literal(pp, ch);
   3831 	case 'M':
   3832 	case 'N':
   3833 		return ApplyModifier_Match(pp, ch);
   3834 	case 'm':
   3835 		return ApplyModifier_Mtime(pp, ch);
   3836 	case 'O':
   3837 		return ApplyModifier_Order(pp, ch);
   3838 	case 'P':
   3839 		return ApplyModifier_Path(pp, ch);
   3840 	case 'Q':
   3841 	case 'q':
   3842 		return ApplyModifier_Quote(pp, ch);
   3843 	case 'R':
   3844 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Root);
   3845 	case 'r':
   3846 		return ApplyModifier_Range(pp, ch);
   3847 	case 'S':
   3848 		return ApplyModifier_Subst(pp, ch);
   3849 	case 's':
   3850 		return ApplyModifier_SunShell(pp, ch);
   3851 	case 'T':
   3852 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Tail);
   3853 	case 't':
   3854 		return ApplyModifier_To(pp, ch);
   3855 	case 'u':
   3856 		return ApplyModifier_Unique(pp, ch);
   3857 	default:
   3858 		return AMR_UNKNOWN;
   3859 	}
   3860 }
   3861 
   3862 static void ApplyModifiers(Expr *, const char **, char, char);
   3863 
   3864 typedef enum ApplyModifiersIndirectResult {
   3865 	/* The indirect modifiers have been applied successfully. */
   3866 	AMIR_CONTINUE,
   3867 	/* Fall back to the SysV modifier. */
   3868 	AMIR_SYSV,
   3869 	/* Error out. */
   3870 	AMIR_OUT
   3871 } ApplyModifiersIndirectResult;
   3872 
   3873 /*
   3874  * While expanding an expression, expand and apply indirect modifiers,
   3875  * such as in ${VAR:${M_indirect}}.
   3876  *
   3877  * All indirect modifiers of a group must come from a single
   3878  * expression.  ${VAR:${M1}} is valid but ${VAR:${M1}${M2}} is not.
   3879  *
   3880  * Multiple groups of indirect modifiers can be chained by separating them
   3881  * with colons.  ${VAR:${M1}:${M2}} contains 2 indirect modifiers.
   3882  *
   3883  * If the expression is not followed by ch->endc or ':', fall
   3884  * back to trying the SysV modifier, such as in ${VAR:${FROM}=${TO}}.
   3885  */
   3886 static ApplyModifiersIndirectResult
   3887 ApplyModifiersIndirect(ModChain *ch, const char **pp)
   3888 {
   3889 	Expr *expr = ch->expr;
   3890 	const char *p = *pp;
   3891 	FStr mods = Var_Parse(&p, expr->scope, expr->emode);
   3892 	/* TODO: handle errors */
   3893 
   3894 	if (mods.str[0] != '\0' && !IsDelimiter(*p, ch)) {
   3895 		FStr_Done(&mods);
   3896 		return AMIR_SYSV;
   3897 	}
   3898 
   3899 	DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n",
   3900 	    mods.str, (int)(p - *pp), *pp);
   3901 
   3902 	if (ModChain_ShouldEval(ch) && mods.str[0] != '\0') {
   3903 		const char *modsp = mods.str;
   3904 		ApplyModifiers(expr, &modsp, '\0', '\0');
   3905 		if (Expr_Str(expr) == var_Error || *modsp != '\0') {
   3906 			FStr_Done(&mods);
   3907 			*pp = p;
   3908 			return AMIR_OUT;	/* error already reported */
   3909 		}
   3910 	}
   3911 	FStr_Done(&mods);
   3912 
   3913 	if (*p == ':')
   3914 		p++;
   3915 	else if (*p == '\0' && ch->endc != '\0') {
   3916 		Error("Unclosed expression after indirect modifier, "
   3917 		      "expecting '%c' for variable \"%s\"",
   3918 		    ch->endc, expr->name);
   3919 		*pp = p;
   3920 		return AMIR_OUT;
   3921 	}
   3922 
   3923 	*pp = p;
   3924 	return AMIR_CONTINUE;
   3925 }
   3926 
   3927 static ApplyModifierResult
   3928 ApplySingleModifier(const char **pp, ModChain *ch)
   3929 {
   3930 	ApplyModifierResult res;
   3931 	const char *mod = *pp;
   3932 	const char *p = *pp;
   3933 
   3934 	if (DEBUG(VAR))
   3935 		LogBeforeApply(ch, mod);
   3936 
   3937 	res = ApplyModifier(&p, ch);
   3938 
   3939 	if (res == AMR_UNKNOWN) {
   3940 		assert(p == mod);
   3941 		res = ApplyModifier_SysV(&p, ch);
   3942 	}
   3943 
   3944 	if (res == AMR_UNKNOWN) {
   3945 		/*
   3946 		 * Guess the end of the current modifier.
   3947 		 * XXX: Skipping the rest of the modifier hides
   3948 		 * errors and leads to wrong results.
   3949 		 * Parsing should rather stop here.
   3950 		 */
   3951 		for (p++; !IsDelimiter(*p, ch); p++)
   3952 			continue;
   3953 		Parse_Error(PARSE_FATAL, "Unknown modifier \"%.*s\"",
   3954 		    (int)(p - mod), mod);
   3955 		Expr_SetValueRefer(ch->expr, var_Error);
   3956 	}
   3957 	if (res == AMR_CLEANUP || res == AMR_BAD) {
   3958 		*pp = p;
   3959 		return res;
   3960 	}
   3961 
   3962 	if (DEBUG(VAR))
   3963 		LogAfterApply(ch, p, mod);
   3964 
   3965 	if (*p == '\0' && ch->endc != '\0') {
   3966 		Error(
   3967 		    "Unclosed expression, expecting '%c' for "
   3968 		    "modifier \"%.*s\" of variable \"%s\" with value \"%s\"",
   3969 		    ch->endc,
   3970 		    (int)(p - mod), mod,
   3971 		    ch->expr->name, Expr_Str(ch->expr));
   3972 	} else if (*p == ':') {
   3973 		p++;
   3974 	} else if (opts.strict && *p != '\0' && *p != ch->endc) {
   3975 		Parse_Error(PARSE_FATAL,
   3976 		    "Missing delimiter ':' after modifier \"%.*s\"",
   3977 		    (int)(p - mod), mod);
   3978 		/*
   3979 		 * TODO: propagate parse error to the enclosing
   3980 		 * expression
   3981 		 */
   3982 	}
   3983 	*pp = p;
   3984 	return AMR_OK;
   3985 }
   3986 
   3987 #if __STDC_VERSION__ >= 199901L
   3988 #define ModChain_Init(expr, startc, endc, sep, oneBigWord) \
   3989 	(ModChain) { expr, startc, endc, sep, oneBigWord }
   3990 #else
   3991 MAKE_INLINE ModChain
   3992 ModChain_Init(Expr *expr, char startc, char endc, char sep, bool oneBigWord)
   3993 {
   3994 	ModChain ch;
   3995 	ch.expr = expr;
   3996 	ch.startc = startc;
   3997 	ch.endc = endc;
   3998 	ch.sep = sep;
   3999 	ch.oneBigWord = oneBigWord;
   4000 	return ch;
   4001 }
   4002 #endif
   4003 
   4004 /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */
   4005 static void
   4006 ApplyModifiers(
   4007     Expr *expr,
   4008     const char **pp,	/* the parsing position, updated upon return */
   4009     char startc,	/* '(' or '{'; or '\0' for indirect modifiers */
   4010     char endc		/* ')' or '}'; or '\0' for indirect modifiers */
   4011 )
   4012 {
   4013 	ModChain ch = ModChain_Init(expr, startc, endc, ' ', false);
   4014 	const char *p;
   4015 	const char *mod;
   4016 
   4017 	assert(startc == '(' || startc == '{' || startc == '\0');
   4018 	assert(endc == ')' || endc == '}' || endc == '\0');
   4019 	assert(Expr_Str(expr) != NULL);
   4020 
   4021 	p = *pp;
   4022 
   4023 	if (*p == '\0' && endc != '\0') {
   4024 		Error(
   4025 		    "Unclosed expression, expecting '%c' for \"%s\"",
   4026 		    ch.endc, expr->name);
   4027 		goto cleanup;
   4028 	}
   4029 
   4030 	while (*p != '\0' && *p != endc) {
   4031 		ApplyModifierResult res;
   4032 
   4033 		if (*p == '$') {
   4034 			/*
   4035 			 * TODO: Only evaluate the expression once, no matter
   4036 			 * whether it's an indirect modifier or the initial
   4037 			 * part of a SysV modifier.
   4038 			 */
   4039 			ApplyModifiersIndirectResult amir =
   4040 			    ApplyModifiersIndirect(&ch, &p);
   4041 			if (amir == AMIR_CONTINUE)
   4042 				continue;
   4043 			if (amir == AMIR_OUT)
   4044 				break;
   4045 		}
   4046 
   4047 		mod = p;
   4048 
   4049 		res = ApplySingleModifier(&p, &ch);
   4050 		if (res == AMR_CLEANUP)
   4051 			goto cleanup;
   4052 		if (res == AMR_BAD)
   4053 			goto bad_modifier;
   4054 	}
   4055 
   4056 	*pp = p;
   4057 	assert(Expr_Str(expr) != NULL);	/* Use var_Error or varUndefined. */
   4058 	return;
   4059 
   4060 bad_modifier:
   4061 	/* Take a guess at where the modifier ends. */
   4062 	Error("Bad modifier \":%.*s\" for variable \"%s\"",
   4063 	    (int)strcspn(mod, ":)}"), mod, expr->name);
   4064 
   4065 cleanup:
   4066 	/*
   4067 	 * TODO: Use p + strlen(p) instead, to stop parsing immediately.
   4068 	 *
   4069 	 * In the unit tests, this generates a few shell commands with
   4070 	 * unbalanced quotes.  Instead of producing these incomplete strings,
   4071 	 * commands with evaluation errors should not be run at all.
   4072 	 *
   4073 	 * To make that happen, Var_Subst must report the actual errors
   4074 	 * instead of returning the resulting string unconditionally.
   4075 	 */
   4076 	*pp = p;
   4077 	Expr_SetValueRefer(expr, var_Error);
   4078 }
   4079 
   4080 /*
   4081  * Only 4 of the 7 built-in local variables are treated specially as they are
   4082  * the only ones that will be set when dynamic sources are expanded.
   4083  */
   4084 static bool
   4085 VarnameIsDynamic(Substring varname)
   4086 {
   4087 	const char *name;
   4088 	size_t len;
   4089 
   4090 	name = varname.start;
   4091 	len = Substring_Length(varname);
   4092 	if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) {
   4093 		switch (name[0]) {
   4094 		case '@':
   4095 		case '%':
   4096 		case '*':
   4097 		case '!':
   4098 			return true;
   4099 		}
   4100 		return false;
   4101 	}
   4102 
   4103 	if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) {
   4104 		return Substring_Equals(varname, ".TARGET") ||
   4105 		       Substring_Equals(varname, ".ARCHIVE") ||
   4106 		       Substring_Equals(varname, ".PREFIX") ||
   4107 		       Substring_Equals(varname, ".MEMBER");
   4108 	}
   4109 
   4110 	return false;
   4111 }
   4112 
   4113 static const char *
   4114 UndefinedShortVarValue(char varname, const GNode *scope)
   4115 {
   4116 	if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) {
   4117 		/*
   4118 		 * If substituting a local variable in a non-local scope,
   4119 		 * assume it's for dynamic source stuff. We have to handle
   4120 		 * this specially and return the longhand for the variable
   4121 		 * with the dollar sign escaped so it makes it back to the
   4122 		 * caller. Only four of the local variables are treated
   4123 		 * specially as they are the only four that will be set
   4124 		 * when dynamic sources are expanded.
   4125 		 */
   4126 		switch (varname) {
   4127 		case '@':
   4128 			return "$(.TARGET)";
   4129 		case '%':
   4130 			return "$(.MEMBER)";
   4131 		case '*':
   4132 			return "$(.PREFIX)";
   4133 		case '!':
   4134 			return "$(.ARCHIVE)";
   4135 		}
   4136 	}
   4137 	return NULL;
   4138 }
   4139 
   4140 /*
   4141  * Parse a variable name, until the end character or a colon, whichever
   4142  * comes first.
   4143  */
   4144 static void
   4145 ParseVarname(const char **pp, char startc, char endc,
   4146 	     GNode *scope, VarEvalMode emode,
   4147 	     LazyBuf *buf)
   4148 {
   4149 	const char *p = *pp;
   4150 	int depth = 0;
   4151 
   4152 	LazyBuf_Init(buf, p);
   4153 
   4154 	while (*p != '\0') {
   4155 		if ((*p == endc || *p == ':') && depth == 0)
   4156 			break;
   4157 		if (*p == startc)
   4158 			depth++;
   4159 		if (*p == endc)
   4160 			depth--;
   4161 
   4162 		if (*p == '$') {
   4163 			FStr nested_val = Var_Parse(&p, scope, emode);
   4164 			/* TODO: handle errors */
   4165 			LazyBuf_AddStr(buf, nested_val.str);
   4166 			FStr_Done(&nested_val);
   4167 		} else {
   4168 			LazyBuf_Add(buf, *p);
   4169 			p++;
   4170 		}
   4171 	}
   4172 	*pp = p;
   4173 }
   4174 
   4175 static bool
   4176 IsShortVarnameValid(char varname, const char *start)
   4177 {
   4178 	if (varname != '$' && varname != ':' && varname != '}' &&
   4179 	    varname != ')' && varname != '\0')
   4180 		return true;
   4181 
   4182 	if (!opts.strict)
   4183 		return false;	/* XXX: Missing error message */
   4184 
   4185 	if (varname == '$' && save_dollars)
   4186 		Parse_Error(PARSE_FATAL,
   4187 		    "To escape a dollar, use \\$, not $$, at \"%s\"", start);
   4188 	else if (varname == '\0')
   4189 		Parse_Error(PARSE_FATAL, "Dollar followed by nothing");
   4190 	else if (save_dollars)
   4191 		Parse_Error(PARSE_FATAL,
   4192 		    "Invalid variable name '%c', at \"%s\"", varname, start);
   4193 
   4194 	return false;
   4195 }
   4196 
   4197 /*
   4198  * Parse a single-character variable name such as in $V or $@.
   4199  * Return whether to continue parsing.
   4200  */
   4201 static bool
   4202 ParseVarnameShort(char varname, const char **pp, GNode *scope,
   4203 		  VarEvalMode emode,
   4204 		  const char **out_false_val,
   4205 		  Var **out_true_var)
   4206 {
   4207 	char name[2];
   4208 	Var *v;
   4209 	const char *val;
   4210 
   4211 	if (!IsShortVarnameValid(varname, *pp)) {
   4212 		(*pp)++;	/* only skip the '$' */
   4213 		*out_false_val = var_Error;
   4214 		return false;
   4215 	}
   4216 
   4217 	name[0] = varname;
   4218 	name[1] = '\0';
   4219 	v = VarFind(name, scope, true);
   4220 	if (v != NULL) {
   4221 		/* No need to advance *pp, the calling code handles this. */
   4222 		*out_true_var = v;
   4223 		return true;
   4224 	}
   4225 
   4226 	*pp += 2;
   4227 
   4228 	val = UndefinedShortVarValue(varname, scope);
   4229 	if (val == NULL)
   4230 		val = emode == VARE_UNDEFERR ? var_Error : varUndefined;
   4231 
   4232 	if (opts.strict && val == var_Error) {
   4233 		Parse_Error(PARSE_FATAL,
   4234 		    "Variable \"%s\" is undefined", name);
   4235 	}
   4236 
   4237 	*out_false_val = val;
   4238 	return false;
   4239 }
   4240 
   4241 /* Find variables like @F or <D. */
   4242 static Var *
   4243 FindLocalLegacyVar(Substring varname, GNode *scope,
   4244 		   const char **out_extraModifiers)
   4245 {
   4246 	Var *v;
   4247 
   4248 	/* Only resolve these variables if scope is a "real" target. */
   4249 	if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL)
   4250 		return NULL;
   4251 
   4252 	if (Substring_Length(varname) != 2)
   4253 		return NULL;
   4254 	if (varname.start[1] != 'F' && varname.start[1] != 'D')
   4255 		return NULL;
   4256 	if (strchr("@%?*!<>", varname.start[0]) == NULL)
   4257 		return NULL;
   4258 
   4259 	v = VarFindSubstring(Substring_Init(varname.start, varname.start + 1),
   4260 	    scope, false);
   4261 	if (v == NULL)
   4262 		return NULL;
   4263 
   4264 	*out_extraModifiers = varname.start[1] == 'D' ? "H:" : "T:";
   4265 	return v;
   4266 }
   4267 
   4268 static FStr
   4269 EvalUndefined(bool dynamic, const char *start, const char *p,
   4270 	      Substring varname, VarEvalMode emode)
   4271 {
   4272 	if (dynamic)
   4273 		return FStr_InitOwn(bmake_strsedup(start, p));
   4274 
   4275 	if (emode == VARE_UNDEFERR && opts.strict) {
   4276 		Parse_Error(PARSE_FATAL,
   4277 		    "Variable \"%.*s\" is undefined",
   4278 		    (int)Substring_Length(varname), varname.start);
   4279 		return FStr_InitRefer(var_Error);
   4280 	}
   4281 
   4282 	return FStr_InitRefer(
   4283 	    emode == VARE_UNDEFERR ? var_Error : varUndefined);
   4284 }
   4285 
   4286 /*
   4287  * Parse a long variable name enclosed in braces or parentheses such as $(VAR)
   4288  * or ${VAR}, up to the closing brace or parenthesis, or in the case of
   4289  * ${VAR:Modifiers}, up to the ':' that starts the modifiers.
   4290  * Return whether to continue parsing.
   4291  */
   4292 static bool
   4293 ParseVarnameLong(
   4294 	const char **pp,
   4295 	char startc,
   4296 	GNode *scope,
   4297 	VarEvalMode emode,
   4298 
   4299 	const char **out_false_pp,
   4300 	FStr *out_false_val,
   4301 
   4302 	char *out_true_endc,
   4303 	Var **out_true_v,
   4304 	bool *out_true_haveModifier,
   4305 	const char **out_true_extraModifiers,
   4306 	bool *out_true_dynamic,
   4307 	ExprDefined *out_true_exprDefined
   4308 )
   4309 {
   4310 	LazyBuf varname;
   4311 	Substring name;
   4312 	Var *v;
   4313 	bool haveModifier;
   4314 	bool dynamic = false;
   4315 
   4316 	const char *p = *pp;
   4317 	const char *start = p;
   4318 	char endc = startc == '(' ? ')' : '}';
   4319 
   4320 	p += 2;			/* skip "${" or "$(" or "y(" */
   4321 	ParseVarname(&p, startc, endc, scope, emode, &varname);
   4322 	name = LazyBuf_Get(&varname);
   4323 
   4324 	if (*p == ':')
   4325 		haveModifier = true;
   4326 	else if (*p == endc)
   4327 		haveModifier = false;
   4328 	else {
   4329 		Parse_Error(PARSE_FATAL, "Unclosed variable \"%.*s\"",
   4330 		    (int)Substring_Length(name), name.start);
   4331 		LazyBuf_Done(&varname);
   4332 		*out_false_pp = p;
   4333 		*out_false_val = FStr_InitRefer(var_Error);
   4334 		return false;
   4335 	}
   4336 
   4337 	v = VarFindSubstring(name, scope, true);
   4338 
   4339 	/*
   4340 	 * At this point, p points just after the variable name, either at
   4341 	 * ':' or at endc.
   4342 	 */
   4343 
   4344 	if (v == NULL && Substring_Equals(name, ".SUFFIXES")) {
   4345 		char *suffixes = Suff_NamesStr();
   4346 		v = VarNew(FStr_InitRefer(".SUFFIXES"), suffixes,
   4347 		    true, false, true);
   4348 		free(suffixes);
   4349 	} else if (v == NULL)
   4350 		v = FindLocalLegacyVar(name, scope, out_true_extraModifiers);
   4351 
   4352 	if (v == NULL) {
   4353 		/*
   4354 		 * Defer expansion of dynamic variables if they appear in
   4355 		 * non-local scope since they are not defined there.
   4356 		 */
   4357 		dynamic = VarnameIsDynamic(name) &&
   4358 			  (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL);
   4359 
   4360 		if (!haveModifier) {
   4361 			p++;	/* skip endc */
   4362 			*out_false_pp = p;
   4363 			*out_false_val = EvalUndefined(dynamic, start, p,
   4364 			    name, emode);
   4365 			LazyBuf_Done(&varname);
   4366 			return false;
   4367 		}
   4368 
   4369 		/*
   4370 		 * The expression is based on an undefined variable.
   4371 		 * Nevertheless it needs a Var, for modifiers that access the
   4372 		 * variable name, such as :L or :?.
   4373 		 *
   4374 		 * Most modifiers leave this expression in the "undefined"
   4375 		 * state (DEF_UNDEF), only a few modifiers like :D, :U, :L,
   4376 		 * :P turn this undefined expression into a defined
   4377 		 * expression (DEF_DEFINED).
   4378 		 *
   4379 		 * In the end, after applying all modifiers, if the expression
   4380 		 * is still undefined, Var_Parse will return an empty string
   4381 		 * instead of the actually computed value.
   4382 		 */
   4383 		v = VarNew(LazyBuf_DoneGet(&varname), "",
   4384 		    true, false, false);
   4385 		*out_true_exprDefined = DEF_UNDEF;
   4386 	} else
   4387 		LazyBuf_Done(&varname);
   4388 
   4389 	*pp = p;
   4390 	*out_true_endc = endc;
   4391 	*out_true_v = v;
   4392 	*out_true_haveModifier = haveModifier;
   4393 	*out_true_dynamic = dynamic;
   4394 	return true;
   4395 }
   4396 
   4397 #if __STDC_VERSION__ >= 199901L
   4398 #define Expr_Init(name, value, emode, scope, defined) \
   4399 	(Expr) { name, value, emode, scope, defined }
   4400 #else
   4401 MAKE_INLINE Expr
   4402 Expr_Init(const char *name, FStr value,
   4403 	  VarEvalMode emode, GNode *scope, ExprDefined defined)
   4404 {
   4405 	Expr expr;
   4406 
   4407 	expr.name = name;
   4408 	expr.value = value;
   4409 	expr.emode = emode;
   4410 	expr.scope = scope;
   4411 	expr.defined = defined;
   4412 	return expr;
   4413 }
   4414 #endif
   4415 
   4416 /*
   4417  * Expressions of the form ${:U...} with a trivial value are often generated
   4418  * by .for loops and are boring, so evaluate them without debug logging.
   4419  */
   4420 static bool
   4421 Var_Parse_U(const char **pp, VarEvalMode emode, FStr *out_value)
   4422 {
   4423 	const char *p;
   4424 
   4425 	p = *pp;
   4426 	if (!(p[0] == '$' && p[1] == '{' && p[2] == ':' && p[3] == 'U'))
   4427 		return false;
   4428 
   4429 	p += 4;
   4430 	while (*p != '$' && *p != '{' && *p != ':' && *p != '\\' &&
   4431 	       *p != '}' && *p != '\0')
   4432 		p++;
   4433 	if (*p != '}')
   4434 		return false;
   4435 
   4436 	*out_value = emode == VARE_PARSE_ONLY
   4437 	    ? FStr_InitRefer("")
   4438 	    : FStr_InitOwn(bmake_strsedup(*pp + 4, p));
   4439 	*pp = p + 1;
   4440 	return true;
   4441 }
   4442 
   4443 /*
   4444  * Given the start of an expression (such as $v, $(VAR), ${VAR:Mpattern}),
   4445  * extract the variable name and the modifiers, if any.  While parsing, apply
   4446  * the modifiers to the value of the expression.
   4447  *
   4448  * Input:
   4449  *	*pp		The string to parse.
   4450  *			When called from CondParser_FuncCallEmpty, it can
   4451  *			also point to the "y" of "empty(VARNAME:Modifiers)".
   4452  *	scope		The scope for finding variables.
   4453  *	emode		Controls the exact details of parsing and evaluation.
   4454  *
   4455  * Output:
   4456  *	*pp		The position where to continue parsing.
   4457  *			TODO: After a parse error, the value of *pp is
   4458  *			unspecified.  It may not have been updated at all,
   4459  *			point to some random character in the string, to the
   4460  *			location of the parse error, or at the end of the
   4461  *			string.
   4462  *	return		The value of the expression, never NULL.
   4463  *	return		var_Error if there was a parse error.
   4464  *	return		var_Error if the base variable of the expression was
   4465  *			undefined, emode is VARE_UNDEFERR, and none of
   4466  *			the modifiers turned the undefined expression into a
   4467  *			defined expression.
   4468  *			XXX: It is not guaranteed that an error message has
   4469  *			been printed.
   4470  *	return		varUndefined if the base variable of the expression
   4471  *			was undefined, emode was not VARE_UNDEFERR,
   4472  *			and none of the modifiers turned the undefined
   4473  *			expression into a defined expression.
   4474  *			XXX: It is not guaranteed that an error message has
   4475  *			been printed.
   4476  */
   4477 FStr
   4478 Var_Parse(const char **pp, GNode *scope, VarEvalMode emode)
   4479 {
   4480 	const char *start, *p;
   4481 	bool haveModifier;	/* true for ${VAR:...}, false for ${VAR} */
   4482 	char startc;		/* the actual '{' or '(' or '\0' */
   4483 	char endc;		/* the expected '}' or ')' or '\0' */
   4484 	/*
   4485 	 * true if the expression is based on one of the 7 predefined
   4486 	 * variables that are local to a target, and the expression is
   4487 	 * expanded in a non-local scope.  The result is the text of the
   4488 	 * expression, unaltered.  This is needed to support dynamic sources.
   4489 	 */
   4490 	bool dynamic;
   4491 	const char *extramodifiers;
   4492 	Var *v;
   4493 	Expr expr = Expr_Init(NULL, FStr_InitRefer(NULL), emode,
   4494 	    scope, DEF_REGULAR);
   4495 	FStr val;
   4496 
   4497 	if (Var_Parse_U(pp, emode, &val))
   4498 		return val;
   4499 
   4500 	p = *pp;
   4501 	start = p;
   4502 	DEBUG2(VAR, "Var_Parse: %s (%s)\n", start, VarEvalMode_Name[emode]);
   4503 
   4504 	val = FStr_InitRefer(NULL);
   4505 	extramodifiers = NULL;	/* extra modifiers to apply first */
   4506 	dynamic = false;
   4507 
   4508 	endc = '\0';		/* Appease GCC. */
   4509 
   4510 	startc = p[1];
   4511 	if (startc != '(' && startc != '{') {
   4512 		if (!ParseVarnameShort(startc, pp, scope, emode, &val.str, &v))
   4513 			return val;
   4514 		haveModifier = false;
   4515 		p++;
   4516 	} else {
   4517 		if (!ParseVarnameLong(&p, startc, scope, emode,
   4518 		    pp, &val,
   4519 		    &endc, &v, &haveModifier, &extramodifiers,
   4520 		    &dynamic, &expr.defined))
   4521 			return val;
   4522 	}
   4523 
   4524 	expr.name = v->name.str;
   4525 	if (v->inUse && VarEvalMode_ShouldEval(emode)) {
   4526 		if (scope->fname != NULL) {
   4527 			fprintf(stderr, "In a command near ");
   4528 			PrintLocation(stderr, false, scope);
   4529 		}
   4530 		Fatal("Variable %s is recursive.", v->name.str);
   4531 	}
   4532 
   4533 	/*
   4534 	 * FIXME: This assignment creates an alias to the current value of the
   4535 	 * variable.  This means that as long as the value of the expression
   4536 	 * stays the same, the value of the variable must not change, and the
   4537 	 * variable must not be deleted.  Using the ':@' modifier, it is
   4538 	 * possible (since var.c 1.212 from 2017-02-01) to delete the variable
   4539 	 * while its value is still being used:
   4540 	 *
   4541 	 *	VAR=	value
   4542 	 *	_:=	${VAR:${:U:@VAR@@}:S,^,prefix,}
   4543 	 *
   4544 	 * The same effect might be achievable using the '::=' or the ':_'
   4545 	 * modifiers.
   4546 	 *
   4547 	 * At the bottom of this function, the resulting value is compared to
   4548 	 * the then-current value of the variable.  This might also invoke
   4549 	 * undefined behavior.
   4550 	 */
   4551 	expr.value = FStr_InitRefer(v->val.data);
   4552 
   4553 	if (expr.name[0] != '\0')
   4554 		EvalStack_Push(NULL, NULL, expr.name);
   4555 	else
   4556 		EvalStack_Push(NULL, start, NULL);
   4557 
   4558 	/*
   4559 	 * Before applying any modifiers, expand any nested expressions from
   4560 	 * the variable value.
   4561 	 */
   4562 	if (VarEvalMode_ShouldEval(emode) &&
   4563 	    strchr(Expr_Str(&expr), '$') != NULL) {
   4564 		char *expanded;
   4565 		VarEvalMode nested_emode = emode;
   4566 		if (opts.strict)
   4567 			nested_emode = VarEvalMode_UndefOk(nested_emode);
   4568 		v->inUse = true;
   4569 		expanded = Var_Subst(Expr_Str(&expr), scope, nested_emode);
   4570 		v->inUse = false;
   4571 		/* TODO: handle errors */
   4572 		Expr_SetValueOwn(&expr, expanded);
   4573 	}
   4574 
   4575 	if (extramodifiers != NULL) {
   4576 		const char *em = extramodifiers;
   4577 		ApplyModifiers(&expr, &em, '\0', '\0');
   4578 	}
   4579 
   4580 	if (haveModifier) {
   4581 		p++;		/* Skip initial colon. */
   4582 		ApplyModifiers(&expr, &p, startc, endc);
   4583 	}
   4584 
   4585 	if (*p != '\0')		/* Skip past endc if possible. */
   4586 		p++;
   4587 
   4588 	*pp = p;
   4589 
   4590 	if (expr.defined == DEF_UNDEF) {
   4591 		if (dynamic)
   4592 			Expr_SetValueOwn(&expr, bmake_strsedup(start, p));
   4593 		else {
   4594 			/*
   4595 			 * The expression is still undefined, therefore
   4596 			 * discard the actual value and return an error marker
   4597 			 * instead.
   4598 			 */
   4599 			Expr_SetValueRefer(&expr,
   4600 			    emode == VARE_UNDEFERR
   4601 				? var_Error : varUndefined);
   4602 		}
   4603 	}
   4604 
   4605 	if (v->shortLived) {
   4606 		if (expr.value.str == v->val.data) {
   4607 			/* move ownership */
   4608 			expr.value.freeIt = v->val.data;
   4609 			v->val.data = NULL;
   4610 		}
   4611 		VarFreeShortLived(v);
   4612 	}
   4613 
   4614 	EvalStack_Pop();
   4615 	return expr.value;
   4616 }
   4617 
   4618 static void
   4619 VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalMode emode)
   4620 {
   4621 	/* A dollar sign may be escaped with another dollar sign. */
   4622 	if (save_dollars && VarEvalMode_ShouldKeepDollar(emode))
   4623 		Buf_AddByte(res, '$');
   4624 	Buf_AddByte(res, '$');
   4625 	*pp += 2;
   4626 }
   4627 
   4628 static void
   4629 VarSubstExpr(const char **pp, Buffer *buf, GNode *scope,
   4630 	     VarEvalMode emode, bool *inout_errorReported)
   4631 {
   4632 	const char *p = *pp;
   4633 	const char *nested_p = p;
   4634 	FStr val = Var_Parse(&nested_p, scope, emode);
   4635 	/* TODO: handle errors */
   4636 
   4637 	if (val.str == var_Error || val.str == varUndefined) {
   4638 		if (!VarEvalMode_ShouldKeepUndef(emode)) {
   4639 			p = nested_p;
   4640 		} else if (val.str == var_Error) {
   4641 
   4642 			/*
   4643 			 * FIXME: The condition 'val.str == var_Error' doesn't
   4644 			 * mean there was an undefined variable.  It could
   4645 			 * equally well be a parse error; see
   4646 			 * unit-tests/varmod-order.mk.
   4647 			 */
   4648 
   4649 			/*
   4650 			 * If variable is undefined, complain and skip the
   4651 			 * variable. The complaint will stop us from doing
   4652 			 * anything when the file is parsed.
   4653 			 */
   4654 			if (!*inout_errorReported) {
   4655 				Parse_Error(PARSE_FATAL,
   4656 				    "Undefined variable \"%.*s\"",
   4657 				    (int)(nested_p - p), p);
   4658 				*inout_errorReported = true;
   4659 			}
   4660 			p = nested_p;
   4661 		} else {
   4662 			/*
   4663 			 * Copy the initial '$' of the undefined expression,
   4664 			 * thereby deferring expansion of the expression, but
   4665 			 * expand nested expressions if already possible. See
   4666 			 * unit-tests/varparse-undef-partial.mk.
   4667 			 */
   4668 			Buf_AddByte(buf, *p);
   4669 			p++;
   4670 		}
   4671 	} else {
   4672 		p = nested_p;
   4673 		Buf_AddStr(buf, val.str);
   4674 	}
   4675 
   4676 	FStr_Done(&val);
   4677 
   4678 	*pp = p;
   4679 }
   4680 
   4681 /*
   4682  * Skip as many characters as possible -- either to the end of the string,
   4683  * or to the next dollar sign, which may start an expression.
   4684  */
   4685 static void
   4686 VarSubstPlain(const char **pp, Buffer *res)
   4687 {
   4688 	const char *p = *pp;
   4689 	const char *start = p;
   4690 
   4691 	for (p++; *p != '$' && *p != '\0'; p++)
   4692 		continue;
   4693 	Buf_AddRange(res, start, p);
   4694 	*pp = p;
   4695 }
   4696 
   4697 /*
   4698  * Expand all expressions like $V, ${VAR}, $(VAR:Modifiers) in the
   4699  * given string.
   4700  *
   4701  * Input:
   4702  *	str		The string in which the expressions are expanded.
   4703  *	scope		The scope in which to start searching for variables.
   4704  *			The other scopes are searched as well.
   4705  *	emode		The mode for parsing or evaluating subexpressions.
   4706  */
   4707 char *
   4708 Var_Subst(const char *str, GNode *scope, VarEvalMode emode)
   4709 {
   4710 	const char *p = str;
   4711 	Buffer res;
   4712 
   4713 	/*
   4714 	 * Set true if an error has already been reported, to prevent a
   4715 	 * plethora of messages when recursing
   4716 	 */
   4717 	static bool errorReported;
   4718 
   4719 	Buf_Init(&res);
   4720 	errorReported = false;
   4721 
   4722 	while (*p != '\0') {
   4723 		if (p[0] == '$' && p[1] == '$')
   4724 			VarSubstDollarDollar(&p, &res, emode);
   4725 		else if (p[0] == '$')
   4726 			VarSubstExpr(&p, &res, scope, emode, &errorReported);
   4727 		else
   4728 			VarSubstPlain(&p, &res);
   4729 	}
   4730 
   4731 	return Buf_DoneData(&res);
   4732 }
   4733 
   4734 void
   4735 Var_Expand(FStr *str, GNode *scope, VarEvalMode emode)
   4736 {
   4737 	char *expanded;
   4738 
   4739 	if (strchr(str->str, '$') == NULL)
   4740 		return;
   4741 	expanded = Var_Subst(str->str, scope, emode);
   4742 	/* TODO: handle errors */
   4743 	FStr_Done(str);
   4744 	*str = FStr_InitOwn(expanded);
   4745 }
   4746 
   4747 /* Initialize the variables module. */
   4748 void
   4749 Var_Init(void)
   4750 {
   4751 	SCOPE_INTERNAL = GNode_New("Internal");
   4752 	SCOPE_GLOBAL = GNode_New("Global");
   4753 	SCOPE_CMDLINE = GNode_New("Command");
   4754 }
   4755 
   4756 #ifdef CLEANUP
   4757 static void
   4758 Var_DeleteAll(GNode *scope)
   4759 {
   4760 	for (;;) {
   4761 		HashIter hi;
   4762 		HashIter_Init(&hi, &scope->vars);
   4763 		if (!HashIter_Next(&hi))
   4764 			return;
   4765 		((Var *)hi.entry->value)->readOnly = false;
   4766 		Var_Delete(scope, hi.entry->key);
   4767 	}
   4768 }
   4769 
   4770 #endif
   4771 
   4772 /* Clean up the variables module. */
   4773 void
   4774 Var_End(void)
   4775 {
   4776 	Var_Stats();
   4777 #ifdef CLEANUP
   4778 	Var_DeleteAll(SCOPE_CMDLINE);
   4779 	Var_DeleteAll(SCOPE_GLOBAL);
   4780 	Var_DeleteAll(SCOPE_INTERNAL);
   4781 #endif
   4782 }
   4783 
   4784 void
   4785 Var_Stats(void)
   4786 {
   4787 	HashTable_DebugStats(&SCOPE_GLOBAL->vars, "Global variables");
   4788 }
   4789 
   4790 static int
   4791 StrAsc(const void *sa, const void *sb)
   4792 {
   4793 	return strcmp(
   4794 	    *((const char *const *)sa), *((const char *const *)sb));
   4795 }
   4796 
   4797 
   4798 /* Print all variables in a scope, sorted by name. */
   4799 void
   4800 Var_Dump(GNode *scope)
   4801 {
   4802 	Vector /* of const char * */ vec;
   4803 	HashIter hi;
   4804 	size_t i;
   4805 	const char **varnames;
   4806 
   4807 	Vector_Init(&vec, sizeof(const char *));
   4808 
   4809 	HashIter_Init(&hi, &scope->vars);
   4810 	while (HashIter_Next(&hi) != NULL)
   4811 		*(const char **)Vector_Push(&vec) = hi.entry->key;
   4812 	varnames = vec.items;
   4813 
   4814 	qsort(varnames, vec.len, sizeof varnames[0], StrAsc);
   4815 
   4816 	for (i = 0; i < vec.len; i++) {
   4817 		const char *varname = varnames[i];
   4818 		const Var *var = HashTable_FindValue(&scope->vars, varname);
   4819 		debug_printf("%-16s = %s%s\n", varname,
   4820 		    var->val.data, ValueDescription(var->val.data));
   4821 	}
   4822 
   4823 	Vector_Done(&vec);
   4824 }
   4825