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