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