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