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