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