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