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