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