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