xref: /freebsd/contrib/bmake/var.c (revision 5dfe1956df4b37af4892aaff3fafe70c0d6d7466)
1 /*	$NetBSD: var.c,v 1.1181 2026/07/03 15:31:35 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.1181 2026/07/03 15:31:35 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 	StringList patterns;
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 	StringListNode *ln;
2835 	StrMatchResult res;
2836 	const char *pattern;
2837 
2838 	assert(word.end[0] == '\0');	/* assume null-terminated word */
2839 	for (ln = args->patterns.first; ln != NULL; ln = ln->next) {
2840 		pattern = ln->datum;
2841 		res = Str_Match(word.start, pattern);
2842 		if (res.error != NULL && !args->error_reported) {
2843 			args->error_reported = true;
2844 			Parse_Error(PARSE_FATAL,
2845 			    "%s in pattern \"%s\" of modifier \"%s\"",
2846 			    res.error, pattern, args->neg ? ":N" : ":M");
2847 		}
2848 		if (res.matched != args->neg) {
2849 			SepBuf_AddSubstring(buf, word);
2850 			break;
2851 		}
2852 	}
2853 }
2854 
2855 /* :Mpattern or :Npattern */
2856 static ApplyModifierResult
ApplyModifier_Match(const char ** pp,ModChain * ch)2857 ApplyModifier_Match(const char **pp, ModChain *ch)
2858 {
2859 	char mod = **pp;
2860 	char *pattern;
2861 
2862 	pattern = ParseModifier_Match(pp, ch);
2863 
2864 	if (ModChain_ShouldEval(ch)) {
2865 		struct ModifyWord_MatchArgs args;
2866 		const char *brace;
2867 
2868 		Lst_Init(&args.patterns);
2869 		if (mod == 'N')
2870 			brace = NULL;	/* not supported */
2871 		else
2872 			for (brace = strchr(pattern, '{'); brace != NULL;
2873 			     brace = strchr(++brace, '{')) {
2874 				if (brace == pattern
2875 				    || (brace[-1] != '\\' && brace[-1] != '$'))
2876 					break;
2877 			}
2878 
2879 		if (brace)
2880 			ExpandCurly(pattern, brace, NULL, &args.patterns);
2881 		else
2882 			Lst_Append(&args.patterns, pattern);
2883 		args.neg = mod == 'N';
2884 		args.error_reported = false;
2885 		ModifyWords(ch, ModifyWord_Match, &args, ch->oneBigWord);
2886 		if (brace)
2887 			Lst_DoneFree(&args.patterns);
2888 		else
2889 			Lst_Done(&args.patterns);
2890 	}
2891 
2892 	free(pattern);
2893 	return AMR_OK;
2894 }
2895 
2896 struct ModifyWord_MtimeArgs {
2897 	bool error;
2898 	bool use_fallback;
2899 	ApplyModifierResult rc;
2900 	time_t fallback;
2901 };
2902 
2903 static void
ModifyWord_Mtime(Substring word,SepBuf * buf,void * data)2904 ModifyWord_Mtime(Substring word, SepBuf *buf, void *data)
2905 {
2906 	struct ModifyWord_MtimeArgs *args = data;
2907 	struct stat st;
2908 	char tbuf[21];
2909 
2910 	if (Substring_IsEmpty(word))
2911 		return;
2912 	assert(word.end[0] == '\0');	/* assume null-terminated word */
2913 	if (stat(word.start, &st) < 0) {
2914 		if (args->error) {
2915 			Parse_Error(PARSE_FATAL,
2916 			    "Cannot determine mtime for \"%s\": %s",
2917 			    word.start, strerror(errno));
2918 			args->rc = AMR_CLEANUP;
2919 			return;
2920 		}
2921 		if (args->use_fallback)
2922 			st.st_mtime = args->fallback;
2923 		else
2924 			time(&st.st_mtime);
2925 	}
2926 	snprintf(tbuf, sizeof(tbuf), "%u", (unsigned)st.st_mtime);
2927 	SepBuf_AddStr(buf, tbuf);
2928 }
2929 
2930 /* :mtime */
2931 static ApplyModifierResult
ApplyModifier_Mtime(const char ** pp,ModChain * ch)2932 ApplyModifier_Mtime(const char **pp, ModChain *ch)
2933 {
2934 	const char *p, *mod = *pp;
2935 	struct ModifyWord_MtimeArgs args;
2936 
2937 	if (!ModMatchEq(mod, "mtime", ch))
2938 		return AMR_UNKNOWN;
2939 	*pp += 5;
2940 	p = *pp;
2941 	args.error = false;
2942 	args.use_fallback = p[0] == '=';
2943 	args.rc = AMR_OK;
2944 	if (args.use_fallback) {
2945 		p++;
2946 		if (TryParseTime(&p, &args.fallback)) {
2947 		} else if (strncmp(p, "error", 5) == 0) {
2948 			p += 5;
2949 			args.error = true;
2950 		} else
2951 			goto invalid_argument;
2952 		if (!IsDelimiter(*p, ch))
2953 			goto invalid_argument;
2954 		*pp = p;
2955 	}
2956 	ModifyWords(ch, ModifyWord_Mtime, &args, ch->oneBigWord);
2957 	return args.rc;
2958 
2959 invalid_argument:
2960 	Parse_Error(PARSE_FATAL,
2961 	    "Invalid argument \"%.*s\" for modifier \":mtime\"",
2962 	    (int)strcspn(*pp + 1, ":{}()"), *pp + 1);
2963 	return AMR_CLEANUP;
2964 }
2965 
2966 static void
ParsePatternFlags(const char ** pp,PatternFlags * pflags,bool * oneBigWord)2967 ParsePatternFlags(const char **pp, PatternFlags *pflags, bool *oneBigWord)
2968 {
2969 	for (;; (*pp)++) {
2970 		if (**pp == 'g')
2971 			pflags->subGlobal = true;
2972 		else if (**pp == '1')
2973 			pflags->subOnce = true;
2974 		else if (**pp == 'W')
2975 			*oneBigWord = true;
2976 		else
2977 			break;
2978 	}
2979 }
2980 
2981 MAKE_INLINE PatternFlags
PatternFlags_None(void)2982 PatternFlags_None(void)
2983 {
2984 	PatternFlags pflags = { false, false, false, false };
2985 	return pflags;
2986 }
2987 
2988 /* :S,from,to, */
2989 static ApplyModifierResult
ApplyModifier_Subst(const char ** pp,ModChain * ch)2990 ApplyModifier_Subst(const char **pp, ModChain *ch)
2991 {
2992 	struct ModifyWord_SubstArgs args;
2993 	bool oneBigWord;
2994 	LazyBuf lhsBuf, rhsBuf;
2995 
2996 	char delim = (*pp)[1];
2997 	if (delim == '\0') {
2998 		Parse_Error(PARSE_FATAL,
2999 		    "Missing delimiter for modifier \":S\"");
3000 		(*pp)++;
3001 		return AMR_CLEANUP;
3002 	}
3003 
3004 	*pp += 2;
3005 
3006 	args.pflags = PatternFlags_None();
3007 	args.matched = false;
3008 
3009 	if (**pp == '^') {
3010 		args.pflags.anchorStart = true;
3011 		(*pp)++;
3012 	}
3013 
3014 	if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
3015 	    ch, &lhsBuf, &args.pflags, NULL))
3016 		return AMR_CLEANUP;
3017 	args.lhs = LazyBuf_Get(&lhsBuf);
3018 
3019 	if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
3020 	    ch, &rhsBuf, NULL, &args)) {
3021 		LazyBuf_Done(&lhsBuf);
3022 		return AMR_CLEANUP;
3023 	}
3024 	args.rhs = LazyBuf_Get(&rhsBuf);
3025 
3026 	oneBigWord = ch->oneBigWord;
3027 	ParsePatternFlags(pp, &args.pflags, &oneBigWord);
3028 
3029 	ModifyWords(ch, ModifyWord_Subst, &args, oneBigWord);
3030 
3031 	LazyBuf_Done(&lhsBuf);
3032 	LazyBuf_Done(&rhsBuf);
3033 	return AMR_OK;
3034 }
3035 
3036 #ifdef HAVE_REGEX_H
3037 
3038 /* :C,from,to, */
3039 static ApplyModifierResult
ApplyModifier_Regex(const char ** pp,ModChain * ch)3040 ApplyModifier_Regex(const char **pp, ModChain *ch)
3041 {
3042 	struct ModifyWord_SubstRegexArgs args;
3043 	bool oneBigWord;
3044 	int error;
3045 	LazyBuf reBuf, replaceBuf;
3046 	FStr re;
3047 
3048 	char delim = (*pp)[1];
3049 	if (delim == '\0') {
3050 		Parse_Error(PARSE_FATAL,
3051 		    "Missing delimiter for modifier \":C\"");
3052 		(*pp)++;
3053 		return AMR_CLEANUP;
3054 	}
3055 
3056 	*pp += 2;
3057 
3058 	if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
3059 	    ch, &reBuf, NULL, NULL))
3060 		return AMR_CLEANUP;
3061 	re = LazyBuf_DoneGet(&reBuf);
3062 
3063 	if (!ParseModifierPart(pp, delim, delim, ch->expr->emode,
3064 	    ch, &replaceBuf, NULL, NULL)) {
3065 		FStr_Done(&re);
3066 		return AMR_CLEANUP;
3067 	}
3068 	args.replace = LazyBuf_Get(&replaceBuf);
3069 
3070 	args.pflags = PatternFlags_None();
3071 	args.matched = false;
3072 	oneBigWord = ch->oneBigWord;
3073 	ParsePatternFlags(pp, &args.pflags, &oneBigWord);
3074 
3075 	if (!ModChain_ShouldEval(ch))
3076 		goto done;
3077 
3078 	if (re.str[0] == '\0') {
3079 	    /* not all regcomp() fail on this */
3080 	    Parse_Error(PARSE_FATAL, "Regex compilation error: empty");
3081 	    goto re_err;
3082 	}
3083 
3084 	error = regcomp(&args.re, re.str, REG_EXTENDED);
3085 	if (error != 0) {
3086 		RegexError(error, &args.re, "Regex compilation error");
3087 	re_err:
3088 		LazyBuf_Done(&replaceBuf);
3089 		FStr_Done(&re);
3090 		return AMR_CLEANUP;
3091 	}
3092 
3093 	args.nsub = args.re.re_nsub + 1;
3094 	if (args.nsub > 10)
3095 		args.nsub = 10;
3096 
3097 	ModifyWords(ch, ModifyWord_SubstRegex, &args, oneBigWord);
3098 
3099 	regfree(&args.re);
3100 done:
3101 	LazyBuf_Done(&replaceBuf);
3102 	FStr_Done(&re);
3103 	return AMR_OK;
3104 }
3105 
3106 #endif
3107 
3108 /* :Q, :q */
3109 static ApplyModifierResult
ApplyModifier_Quote(const char ** pp,ModChain * ch)3110 ApplyModifier_Quote(const char **pp, ModChain *ch)
3111 {
3112 	LazyBuf buf;
3113 	bool quoteDollar;
3114 
3115 	quoteDollar = **pp == 'q';
3116 	if (!IsDelimiter((*pp)[1], ch))
3117 		return AMR_UNKNOWN;
3118 	(*pp)++;
3119 
3120 	if (!ModChain_ShouldEval(ch))
3121 		return AMR_OK;
3122 
3123 	QuoteShell(Expr_Str(ch->expr), quoteDollar, &buf);
3124 	if (buf.data != NULL)
3125 		Expr_SetValue(ch->expr, LazyBuf_DoneGet(&buf));
3126 	else
3127 		LazyBuf_Done(&buf);
3128 
3129 	return AMR_OK;
3130 }
3131 
3132 static void
ModifyWord_Copy(Substring word,SepBuf * buf,void * data MAKE_ATTR_UNUSED)3133 ModifyWord_Copy(Substring word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
3134 {
3135 	SepBuf_AddSubstring(buf, word);
3136 }
3137 
3138 /* :ts<separator> */
3139 static ApplyModifierResult
ApplyModifier_ToSep(const char ** pp,ModChain * ch)3140 ApplyModifier_ToSep(const char **pp, ModChain *ch)
3141 {
3142 	const char *sep = *pp + 2;
3143 
3144 	/*
3145 	 * Even in parse-only mode, apply the side effects, since the side
3146 	 * effects are neither observable nor is there a performance penalty.
3147 	 * Checking for VARE_EVAL for every single piece of code in here
3148 	 * would make the code in this function too hard to read.
3149 	 */
3150 
3151 	/* ":ts<any><endc>" or ":ts<any>:" */
3152 	if (sep[0] != ch->endc && IsDelimiter(sep[1], ch)) {
3153 		*pp = sep + 1;
3154 		ch->sep = sep[0];
3155 		goto ok;
3156 	}
3157 
3158 	/* ":ts<endc>" or ":ts:" */
3159 	if (IsDelimiter(sep[0], ch)) {
3160 		*pp = sep;
3161 		ch->sep = '\0';	/* no separator */
3162 		goto ok;
3163 	}
3164 
3165 	if (sep[0] != '\\')
3166 		return AMR_UNKNOWN;
3167 
3168 	/* ":ts\n" */
3169 	if (sep[1] == 'n') {
3170 		*pp = sep + 2;
3171 		ch->sep = '\n';
3172 		goto ok;
3173 	}
3174 
3175 	/* ":ts\t" */
3176 	if (sep[1] == 't') {
3177 		*pp = sep + 2;
3178 		ch->sep = '\t';
3179 		goto ok;
3180 	}
3181 
3182 	/* ":ts\x40" or ":ts\100" */
3183 	{
3184 		const char *p = sep + 1;
3185 		int base = 8;	/* assume octal */
3186 
3187 		if (sep[1] == 'x') {
3188 			base = 16;
3189 			p++;
3190 		} else if (!ch_isdigit(sep[1]))
3191 			return AMR_UNKNOWN;	/* ":ts\..." */
3192 
3193 		if (!TryParseChar(&p, base, &ch->sep)) {
3194 			Parse_Error(PARSE_FATAL,
3195 			    "Invalid character number at \"%s\"", p);
3196 			return AMR_CLEANUP;
3197 		}
3198 		if (!IsDelimiter(*p, ch))
3199 			return AMR_UNKNOWN;
3200 
3201 		*pp = p;
3202 	}
3203 
3204 ok:
3205 	ModifyWords(ch, ModifyWord_Copy, NULL, ch->oneBigWord);
3206 	return AMR_OK;
3207 }
3208 
3209 static char *
str_totitle(const char * str)3210 str_totitle(const char *str)
3211 {
3212 	size_t i, n = strlen(str) + 1;
3213 	char *res = bmake_malloc(n);
3214 	for (i = 0; i < n; i++) {
3215 		if (i == 0 || ch_isspace(res[i - 1]))
3216 			res[i] = ch_toupper(str[i]);
3217 		else
3218 			res[i] = ch_tolower(str[i]);
3219 	}
3220 	return res;
3221 }
3222 
3223 
3224 static char *
str_toupper(const char * str)3225 str_toupper(const char *str)
3226 {
3227 	size_t i, n = strlen(str) + 1;
3228 	char *res = bmake_malloc(n);
3229 	for (i = 0; i < n; i++)
3230 		res[i] = ch_toupper(str[i]);
3231 	return res;
3232 }
3233 
3234 static char *
str_tolower(const char * str)3235 str_tolower(const char *str)
3236 {
3237 	size_t i, n = strlen(str) + 1;
3238 	char *res = bmake_malloc(n);
3239 	for (i = 0; i < n; i++)
3240 		res[i] = ch_tolower(str[i]);
3241 	return res;
3242 }
3243 
3244 /* :tA, :tu, :tl, :ts<separator>, etc. */
3245 static ApplyModifierResult
ApplyModifier_To(const char ** pp,ModChain * ch)3246 ApplyModifier_To(const char **pp, ModChain *ch)
3247 {
3248 	Expr *expr = ch->expr;
3249 	const char *mod = *pp;
3250 	assert(mod[0] == 't');
3251 
3252 	if (IsDelimiter(mod[1], ch))
3253 		return AMR_UNKNOWN;		/* ":t<endc>" or ":t:" */
3254 
3255 	if (mod[1] == 's')
3256 		return ApplyModifier_ToSep(pp, ch);
3257 
3258 	if (!IsDelimiter(mod[2], ch))
3259 		return AMR_UNKNOWN;
3260 
3261 	if (mod[1] == 'A') {				/* :tA */
3262 		*pp = mod + 2;
3263 		ModifyWords(ch, ModifyWord_Realpath, NULL, ch->oneBigWord);
3264 		return AMR_OK;
3265 	}
3266 
3267 	if (mod[1] == 't') {				/* :tt */
3268 		*pp = mod + 2;
3269 		if (Expr_ShouldEval(expr))
3270 			Expr_SetValueOwn(expr, str_totitle(Expr_Str(expr)));
3271 		return AMR_OK;
3272 	}
3273 
3274 	if (mod[1] == 'u') {				/* :tu */
3275 		*pp = mod + 2;
3276 		if (Expr_ShouldEval(expr))
3277 			Expr_SetValueOwn(expr, str_toupper(Expr_Str(expr)));
3278 		return AMR_OK;
3279 	}
3280 
3281 	if (mod[1] == 'l') {				/* :tl */
3282 		*pp = mod + 2;
3283 		if (Expr_ShouldEval(expr))
3284 			Expr_SetValueOwn(expr, str_tolower(Expr_Str(expr)));
3285 		return AMR_OK;
3286 	}
3287 
3288 	if (mod[1] == 'W' || mod[1] == 'w') {		/* :tW, :tw */
3289 		*pp = mod + 2;
3290 		ch->oneBigWord = mod[1] == 'W';
3291 		return AMR_OK;
3292 	}
3293 
3294 	return AMR_UNKNOWN;		/* ":t<any>:" or ":t<any><endc>" */
3295 }
3296 
3297 /* :[#], :[1], :[-1..1], etc. */
3298 static ApplyModifierResult
ApplyModifier_Words(const char ** pp,ModChain * ch)3299 ApplyModifier_Words(const char **pp, ModChain *ch)
3300 {
3301 	Expr *expr = ch->expr;
3302 	int first, last;
3303 	const char *p;
3304 	LazyBuf argBuf;
3305 	FStr arg;
3306 
3307 	(*pp)++;		/* skip the '[' */
3308 	if (!ParseModifierPart(pp, ']', ']', expr->emode,
3309 	    ch, &argBuf, NULL, NULL))
3310 		return AMR_CLEANUP;
3311 	arg = LazyBuf_DoneGet(&argBuf);
3312 	p = arg.str;
3313 
3314 	if (!IsDelimiter(**pp, ch)) {
3315 		Parse_Error(PARSE_FATAL,
3316 		    "Extra text after \"[%s]\"", arg.str);
3317 		FStr_Done(&arg);
3318 		return AMR_CLEANUP;
3319 	}
3320 
3321 	if (!ModChain_ShouldEval(ch))
3322 		goto ok;
3323 
3324 	if (p[0] == '\0')
3325 		goto bad_modifier;		/* Found ":[]". */
3326 
3327 	if (strcmp(p, "#") == 0) {		/* Found ":[#]" */
3328 		if (ch->oneBigWord)
3329 			Expr_SetValueRefer(expr, "1");
3330 		else {
3331 			Buffer buf;
3332 
3333 			SubstringWords words = Expr_Words(expr);
3334 			size_t ac = words.len;
3335 			SubstringWords_Free(words);
3336 
3337 			Buf_Init(&buf);
3338 			Buf_AddInt(&buf, (int)ac);
3339 			Expr_SetValueOwn(expr, Buf_DoneData(&buf));
3340 		}
3341 		goto ok;
3342 	}
3343 
3344 	if (strcmp(p, "*") == 0) {		/* ":[*]" */
3345 		ch->oneBigWord = true;
3346 		goto ok;
3347 	}
3348 
3349 	if (strcmp(p, "@") == 0) {		/* ":[@]" */
3350 		ch->oneBigWord = false;
3351 		goto ok;
3352 	}
3353 
3354 	/* Expect ":[N]" or ":[start..end]" */
3355 	if (!TryParseIntBase0(&p, &first))
3356 		goto bad_modifier;
3357 
3358 	if (p[0] == '\0')			/* ":[N]" */
3359 		last = first;
3360 	else if (strncmp(p, "..", 2) == 0) {
3361 		p += 2;
3362 		if (!TryParseIntBase0(&p, &last) || *p != '\0')
3363 			goto bad_modifier;
3364 	} else
3365 		goto bad_modifier;
3366 
3367 	if (first == 0 && last == 0) {		/* ":[0]" or ":[0..0]" */
3368 		ch->oneBigWord = true;
3369 		goto ok;
3370 	}
3371 
3372 	if (first == 0 || last == 0)		/* ":[0..N]" or ":[N..0]" */
3373 		goto bad_modifier;
3374 
3375 	Expr_SetValueOwn(expr,
3376 	    VarSelectWords(Expr_Str(expr), first, last,
3377 		ch->sep, ch->oneBigWord));
3378 
3379 ok:
3380 	FStr_Done(&arg);
3381 	return AMR_OK;
3382 
3383 bad_modifier:
3384 	Parse_Error(PARSE_FATAL, "Invalid modifier \":[%s]\"", arg.str);
3385 	FStr_Done(&arg);
3386 	return AMR_CLEANUP;
3387 }
3388 
3389 #if __STDC_VERSION__ >= 199901L || defined(HAVE_LONG_LONG_INT)
3390 # define NUM_TYPE long long
3391 # define PARSE_NUM_TYPE strtoll
3392 #else
3393 # define NUM_TYPE long
3394 # define PARSE_NUM_TYPE strtol
3395 #endif
3396 
3397 static NUM_TYPE
num_val(Substring s)3398 num_val(Substring s)
3399 {
3400 	NUM_TYPE val;
3401 	char *ep;
3402 
3403 	val = PARSE_NUM_TYPE(s.start, &ep, 0);
3404 	if (ep != s.start) {
3405 		switch (*ep) {
3406 		case 'K':
3407 		case 'k':
3408 			val <<= 10;
3409 			break;
3410 		case 'M':
3411 		case 'm':
3412 			val <<= 20;
3413 			break;
3414 		case 'G':
3415 		case 'g':
3416 			val <<= 30;
3417 			break;
3418 		}
3419 	}
3420 	return val;
3421 }
3422 
3423 static int
SubNumAsc(const void * sa,const void * sb)3424 SubNumAsc(const void *sa, const void *sb)
3425 {
3426 	NUM_TYPE a, b;
3427 
3428 	a = num_val(*(const Substring *)sa);
3429 	b = num_val(*(const Substring *)sb);
3430 	return a > b ? 1 : b > a ? -1 : 0;
3431 }
3432 
3433 static int
SubNumDesc(const void * sa,const void * sb)3434 SubNumDesc(const void *sa, const void *sb)
3435 {
3436 	return SubNumAsc(sb, sa);
3437 }
3438 
3439 static int
Substring_Cmp(Substring a,Substring b)3440 Substring_Cmp(Substring a, Substring b)
3441 {
3442 	for (; a.start < a.end && b.start < b.end; a.start++, b.start++)
3443 		if (a.start[0] != b.start[0])
3444 			return (unsigned char)a.start[0]
3445 			    - (unsigned char)b.start[0];
3446 	return (int)((a.end - a.start) - (b.end - b.start));
3447 }
3448 
3449 static int
SubStrAsc(const void * sa,const void * sb)3450 SubStrAsc(const void *sa, const void *sb)
3451 {
3452 	return Substring_Cmp(*(const Substring *)sa, *(const Substring *)sb);
3453 }
3454 
3455 static int
SubStrDesc(const void * sa,const void * sb)3456 SubStrDesc(const void *sa, const void *sb)
3457 {
3458 	return SubStrAsc(sb, sa);
3459 }
3460 
3461 static void
ShuffleSubstrings(Substring * strs,size_t n)3462 ShuffleSubstrings(Substring *strs, size_t n)
3463 {
3464 	size_t i;
3465 
3466 	for (i = n - 1; i > 0; i--) {
3467 		size_t rndidx = (size_t)random() % (i + 1);
3468 		Substring t = strs[i];
3469 		strs[i] = strs[rndidx];
3470 		strs[rndidx] = t;
3471 	}
3472 }
3473 
3474 /*
3475  * :O		order ascending
3476  * :Or		order descending
3477  * :Ox		shuffle
3478  * :On		numeric ascending
3479  * :Onr, :Orn	numeric descending
3480  */
3481 static ApplyModifierResult
ApplyModifier_Order(const char ** pp,ModChain * ch)3482 ApplyModifier_Order(const char **pp, ModChain *ch)
3483 {
3484 	const char *mod = *pp;
3485 	SubstringWords words;
3486 	int (*cmp)(const void *, const void *);
3487 
3488 	if (IsDelimiter(mod[1], ch)) {
3489 		cmp = SubStrAsc;
3490 		(*pp)++;
3491 	} else if (IsDelimiter(mod[2], ch)) {
3492 		if (mod[1] == 'n')
3493 			cmp = SubNumAsc;
3494 		else if (mod[1] == 'r')
3495 			cmp = SubStrDesc;
3496 		else if (mod[1] == 'x')
3497 			cmp = NULL;
3498 		else
3499 			return AMR_UNKNOWN;
3500 		*pp += 2;
3501 	} else if (IsDelimiter(mod[3], ch)) {
3502 		if ((mod[1] == 'n' && mod[2] == 'r') ||
3503 		    (mod[1] == 'r' && mod[2] == 'n'))
3504 			cmp = SubNumDesc;
3505 		else
3506 			return AMR_UNKNOWN;
3507 		*pp += 3;
3508 	} else
3509 		return AMR_UNKNOWN;
3510 
3511 	if (!ModChain_ShouldEval(ch))
3512 		return AMR_OK;
3513 
3514 	words = Expr_Words(ch->expr);
3515 	if (cmp == NULL)
3516 		ShuffleSubstrings(words.words, words.len);
3517 	else {
3518 		assert(words.words[0].end[0] == '\0');
3519 		qsort(words.words, words.len, sizeof(words.words[0]), cmp);
3520 	}
3521 	Expr_SetValueOwn(ch->expr, SubstringWords_JoinFree(words));
3522 
3523 	return AMR_OK;
3524 }
3525 
3526 /* :? then : else */
3527 static ApplyModifierResult
ApplyModifier_IfElse(const char ** pp,ModChain * ch)3528 ApplyModifier_IfElse(const char **pp, ModChain *ch)
3529 {
3530 	Expr *expr = ch->expr;
3531 	LazyBuf thenBuf;
3532 	LazyBuf elseBuf;
3533 
3534 	VarEvalMode then_emode = VARE_PARSE;
3535 	VarEvalMode else_emode = VARE_PARSE;
3536 	int parseErrorsBefore = parseErrors;
3537 
3538 	CondResult cond_rc = CR_TRUE;	/* anything other than CR_ERROR */
3539 	if (Expr_ShouldEval(expr)) {
3540 		evalStack.elems[evalStack.len - 1].kind = VSK_COND;
3541 		cond_rc = Cond_EvalCondition(expr->name);
3542 		if (cond_rc == CR_TRUE)
3543 			then_emode = expr->emode;
3544 		else if (cond_rc == CR_FALSE)
3545 			else_emode = expr->emode;
3546 		else if (parseErrors == parseErrorsBefore)
3547 			Parse_Error(PARSE_FATAL, "Bad condition");
3548 	}
3549 
3550 	evalStack.elems[evalStack.len - 1].kind = VSK_COND_THEN;
3551 	(*pp)++;		/* skip past the '?' */
3552 	if (!ParseModifierPart(pp, ':', ':', then_emode,
3553 	    ch, &thenBuf, NULL, NULL))
3554 		return AMR_CLEANUP;
3555 
3556 	evalStack.elems[evalStack.len - 1].kind = VSK_COND_ELSE;
3557 	if (!ParseModifierPart(pp, ch->endc, ch->endc, else_emode,
3558 	    ch, &elseBuf, NULL, NULL)) {
3559 		LazyBuf_Done(&thenBuf);
3560 		return AMR_CLEANUP;
3561 	}
3562 
3563 	(*pp)--;		/* Go back to the ch->endc. */
3564 
3565 	if (cond_rc == CR_ERROR) {
3566 		LazyBuf_Done(&thenBuf);
3567 		LazyBuf_Done(&elseBuf);
3568 		return AMR_CLEANUP;
3569 	}
3570 
3571 	if (!Expr_ShouldEval(expr)) {
3572 		LazyBuf_Done(&thenBuf);
3573 		LazyBuf_Done(&elseBuf);
3574 	} else if (cond_rc == CR_TRUE) {
3575 		Expr_SetValue(expr, LazyBuf_DoneGet(&thenBuf));
3576 		LazyBuf_Done(&elseBuf);
3577 	} else {
3578 		LazyBuf_Done(&thenBuf);
3579 		Expr_SetValue(expr, LazyBuf_DoneGet(&elseBuf));
3580 	}
3581 	Expr_Define(expr);
3582 	return AMR_OK;
3583 }
3584 
3585 /*
3586  * The ::= modifiers are special in that they do not read the variable value
3587  * but instead assign to that variable.  They always expand to an empty
3588  * string.
3589  *
3590  * Their main purpose is in supporting .for loops that generate shell commands
3591  * since an ordinary variable assignment at that point would terminate the
3592  * dependency group for these targets.  For example:
3593  *
3594  * list-targets: .USE
3595  * .for i in ${.TARGET} ${.TARGET:R}.gz
3596  *	@${t::=$i}
3597  *	@echo 'The target is ${t:T}.'
3598  * .endfor
3599  *
3600  *	  ::=<str>	Assigns <str> as the new value of variable.
3601  *	  ::?=<str>	Assigns <str> as value of variable if
3602  *			it was not already set.
3603  *	  ::+=<str>	Appends <str> to variable.
3604  *	  ::!=<cmd>	Assigns output of <cmd> as the new value of
3605  *			variable.
3606  */
3607 static ApplyModifierResult
ApplyModifier_Assign(const char ** pp,ModChain * ch)3608 ApplyModifier_Assign(const char **pp, ModChain *ch)
3609 {
3610 	Expr *expr = ch->expr;
3611 	GNode *scope;
3612 	FStr val;
3613 	LazyBuf buf;
3614 
3615 	const char *mod = *pp;
3616 	const char *op = mod + 1;
3617 
3618 	if (op[0] == '=')
3619 		goto found_op;
3620 	if ((op[0] == '+' || op[0] == '?' || op[0] == '!') && op[1] == '=')
3621 		goto found_op;
3622 	return AMR_UNKNOWN;	/* "::<unrecognized>" */
3623 
3624 found_op:
3625 	if (expr->name[0] == '\0') {
3626 		const char *value = op[0] == '=' ? op + 1 : op + 2;
3627 		*pp = mod + 1;
3628 		/* Take a guess at where the modifier ends. */
3629 		Parse_Error(PARSE_FATAL,
3630 		    "Invalid attempt to assign \"%.*s\" to variable \"\" "
3631 		    "via modifier \":%.*s\"",
3632 		    (int)strcspn(value, ":)}"), value,
3633 		    (int)(value - mod), mod);
3634 		return AMR_CLEANUP;
3635 	}
3636 
3637 	*pp = mod + (op[0] != '=' ? 3 : 2);
3638 
3639 	if (!ParseModifierPart(pp, ch->endc, ch->endc, expr->emode,
3640 	    ch, &buf, NULL, NULL))
3641 		return AMR_CLEANUP;
3642 	val = LazyBuf_DoneGet(&buf);
3643 
3644 	(*pp)--;		/* Go back to the ch->endc. */
3645 
3646 	if (!Expr_ShouldEval(expr))
3647 		goto done;
3648 
3649 	scope = expr->scope;	/* scope where v belongs */
3650 	if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL
3651 	    && VarFind(expr->name, expr->scope, false) == NULL)
3652 		scope = SCOPE_GLOBAL;
3653 
3654 	if (op[0] == '+')
3655 		Var_Append(scope, expr->name, val.str);
3656 	else if (op[0] == '!') {
3657 		char *output, *error;
3658 		output = Cmd_Exec(val.str, &error);
3659 		if (error != NULL) {
3660 			Parse_Error(PARSE_WARNING, "%s", error);
3661 			free(error);
3662 		} else
3663 			Var_Set(scope, expr->name, output);
3664 		free(output);
3665 	} else if (op[0] == '?' && expr->defined == DEF_REGULAR) {
3666 		/* Do nothing. */
3667 	} else
3668 		Var_Set(scope, expr->name, val.str);
3669 
3670 	Expr_SetValueRefer(expr, "");
3671 
3672 done:
3673 	FStr_Done(&val);
3674 	return AMR_OK;
3675 }
3676 
3677 /*
3678  * :_=...
3679  * remember current value
3680  */
3681 static ApplyModifierResult
ApplyModifier_Remember(const char ** pp,ModChain * ch)3682 ApplyModifier_Remember(const char **pp, ModChain *ch)
3683 {
3684 	Expr *expr = ch->expr;
3685 	const char *mod = *pp;
3686 	FStr name;
3687 
3688 	if (!ModMatchEq(mod, "_", ch))
3689 		return AMR_UNKNOWN;
3690 
3691 	name = FStr_InitRefer("_");
3692 	if (mod[1] == '=') {
3693 		/*
3694 		 * XXX: This ad-hoc call to strcspn deviates from the usual
3695 		 * behavior defined in ParseModifierPart.  This creates an
3696 		 * unnecessary and undocumented inconsistency in make.
3697 		 */
3698 		const char *arg = mod + 2;
3699 		size_t argLen = strcspn(arg, ":)}");
3700 		*pp = arg + argLen;
3701 		name = FStr_InitOwn(bmake_strldup(arg, argLen));
3702 	} else
3703 		*pp = mod + 1;
3704 
3705 	if (Expr_ShouldEval(expr))
3706 		Var_Set(SCOPE_GLOBAL, name.str, Expr_Str(expr));
3707 	FStr_Done(&name);
3708 
3709 	return AMR_OK;
3710 }
3711 
3712 /*
3713  * Apply the given function to each word of the variable value,
3714  * for a single-letter modifier such as :H, :T.
3715  */
3716 static ApplyModifierResult
ApplyModifier_WordFunc(const char ** pp,ModChain * ch,ModifyWordProc modifyWord)3717 ApplyModifier_WordFunc(const char **pp, ModChain *ch,
3718 		       ModifyWordProc modifyWord)
3719 {
3720 	if (!IsDelimiter((*pp)[1], ch))
3721 		return AMR_UNKNOWN;
3722 	(*pp)++;
3723 
3724 	ModifyWords(ch, modifyWord, NULL, ch->oneBigWord);
3725 
3726 	return AMR_OK;
3727 }
3728 
3729 /* Remove adjacent duplicate words. */
3730 static ApplyModifierResult
ApplyModifier_Unique(const char ** pp,ModChain * ch)3731 ApplyModifier_Unique(const char **pp, ModChain *ch)
3732 {
3733 	SubstringWords words;
3734 
3735 	if (!IsDelimiter((*pp)[1], ch))
3736 		return AMR_UNKNOWN;
3737 	(*pp)++;
3738 
3739 	if (!ModChain_ShouldEval(ch))
3740 		return AMR_OK;
3741 
3742 	words = Expr_Words(ch->expr);
3743 
3744 	if (words.len > 1) {
3745 		size_t di, si;
3746 		for (di = 0, si = 1; si < words.len; si++)
3747 			if (!Substring_Eq(words.words[di], words.words[si]))
3748 				words.words[++di] = words.words[si];
3749 		words.len = di + 1;
3750 	}
3751 
3752 	Expr_SetValueOwn(ch->expr, SubstringWords_JoinFree(words));
3753 
3754 	return AMR_OK;
3755 }
3756 
3757 /* Test whether the modifier has the form '<lhs>=<rhs>'. */
3758 static bool
IsSysVModifier(const char * p,char startc,char endc)3759 IsSysVModifier(const char *p, char startc, char endc)
3760 {
3761 	bool eqFound = false;
3762 
3763 	int depth = 1;
3764 	while (*p != '\0') {
3765 		if (*p == '=')	/* XXX: should also test depth == 1 */
3766 			eqFound = true;
3767 		else if (*p == endc) {
3768 			if (--depth == 0)
3769 				break;
3770 		} else if (*p == startc)
3771 			depth++;
3772 		p++;
3773 	}
3774 	return eqFound;
3775 }
3776 
3777 /* :from=to */
3778 static ApplyModifierResult
ApplyModifier_SysV(const char ** pp,ModChain * ch)3779 ApplyModifier_SysV(const char **pp, ModChain *ch)
3780 {
3781 	Expr *expr = ch->expr;
3782 	LazyBuf lhsBuf, rhsBuf;
3783 	FStr rhs;
3784 	struct ModifyWord_SysVSubstArgs args;
3785 	Substring lhs;
3786 	const char *lhsSuffix;
3787 
3788 	const char *mod = *pp;
3789 
3790 	if (!IsSysVModifier(mod, ch->startc, ch->endc))
3791 		return AMR_UNKNOWN;
3792 
3793 	if (!ParseModifierPart(pp, '=', '=', expr->emode,
3794 	    ch, &lhsBuf, NULL, NULL))
3795 		return AMR_CLEANUP;
3796 
3797 	if (!ParseModifierPart(pp, ch->endc, ch->endc, expr->emode,
3798 	    ch, &rhsBuf, NULL, NULL)) {
3799 		LazyBuf_Done(&lhsBuf);
3800 		return AMR_CLEANUP;
3801 	}
3802 	rhs = LazyBuf_DoneGet(&rhsBuf);
3803 
3804 	(*pp)--;		/* Go back to the ch->endc. */
3805 
3806 	/* Do not turn an empty expression into non-empty. */
3807 	if (lhsBuf.len == 0 && Expr_Str(expr)[0] == '\0')
3808 		goto done;
3809 
3810 	lhs = LazyBuf_Get(&lhsBuf);
3811 	lhsSuffix = Substring_SkipFirst(lhs, '%');
3812 
3813 	args.scope = expr->scope;
3814 	args.lhsPrefix = Substring_Init(lhs.start,
3815 	    lhsSuffix != lhs.start ? lhsSuffix - 1 : lhs.start);
3816 	args.lhsPercent = lhsSuffix != lhs.start;
3817 	args.lhsSuffix = Substring_Init(lhsSuffix, lhs.end);
3818 	args.rhs = rhs.str;
3819 
3820 	ModifyWords(ch, ModifyWord_SysVSubst, &args, ch->oneBigWord);
3821 
3822 done:
3823 	LazyBuf_Done(&lhsBuf);
3824 	FStr_Done(&rhs);
3825 	return AMR_OK;
3826 }
3827 
3828 /* :sh */
3829 static ApplyModifierResult
ApplyModifier_SunShell(const char ** pp,ModChain * ch)3830 ApplyModifier_SunShell(const char **pp, ModChain *ch)
3831 {
3832 	Expr *expr = ch->expr;
3833 	const char *p = *pp;
3834 	if (!(p[1] == 'h' && IsDelimiter(p[2], ch)))
3835 		return AMR_UNKNOWN;
3836 	*pp = p + 2;
3837 
3838 	if (Expr_ShouldEval(expr)) {
3839 		char *output, *error;
3840 		output = Cmd_Exec(Expr_Str(expr), &error);
3841 		if (error != NULL) {
3842 			Parse_Error(PARSE_WARNING, "%s", error);
3843 			free(error);
3844 		}
3845 		Expr_SetValueOwn(expr, output);
3846 	}
3847 
3848 	return AMR_OK;
3849 }
3850 
3851 /* :sh1 */
3852 static ApplyModifierResult
ApplyModifier_SunShell1(const char ** pp,ModChain * ch)3853 ApplyModifier_SunShell1(const char **pp, ModChain *ch)
3854 {
3855 	Expr *expr = ch->expr;
3856 	const char *p = *pp;
3857 
3858 	if (!(p[1] == 'h' && p[2] == '1' && IsDelimiter(p[3], ch)))
3859 		return AMR_UNKNOWN;
3860 	*pp = p + 3;
3861 
3862 	if (Expr_ShouldEval(expr)) {
3863 		char *cache_varname;
3864 		Var *v;
3865 
3866 		cache_varname = str_concat2(".MAKE.SH1.", expr->name);
3867 		v = VarFind(cache_varname, SCOPE_GLOBAL, false);
3868 		if (v == NULL) {
3869 			char *output, *error;
3870 
3871 			output = Cmd_Exec(Expr_Str(expr), &error);
3872 			if (error != NULL) {
3873 				Parse_Error(PARSE_WARNING, "%s", error);
3874 				free(error);
3875 			}
3876 			Var_SetWithFlags(SCOPE_GLOBAL, cache_varname, output,
3877 			    VAR_SET_NO_EXPORT);
3878 			Expr_SetValueOwn(expr, output);
3879 		} else {
3880 			Expr_SetValueRefer(expr, v->val.data);
3881 		}
3882 		free(cache_varname);
3883 	}
3884 
3885 	return AMR_OK;
3886 }
3887 
3888 
3889 /*
3890  * In cases where the evaluation mode and the definedness are the "standard"
3891  * ones, don't log them, to keep the logs readable.
3892  */
3893 static bool
ShouldLogInSimpleFormat(const Expr * expr)3894 ShouldLogInSimpleFormat(const Expr *expr)
3895 {
3896 	return (expr->emode == VARE_EVAL
3897 		|| expr->emode == VARE_EVAL_DEFINED
3898 		|| expr->emode == VARE_EVAL_DEFINED_LOUD)
3899 	    && expr->defined == DEF_REGULAR;
3900 }
3901 
3902 static void
LogBeforeApply(const ModChain * ch,const char * mod)3903 LogBeforeApply(const ModChain *ch, const char *mod)
3904 {
3905 	const Expr *expr = ch->expr;
3906 	bool is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], ch);
3907 
3908 	/*
3909 	 * At this point, only the first character of the modifier can
3910 	 * be used since the end of the modifier is not yet known.
3911 	 */
3912 
3913 	if (!Expr_ShouldEval(expr)) {
3914 		debug_printf("Parsing modifier ${%s:%c%s}\n",
3915 		    expr->name, mod[0], is_single_char ? "" : "...");
3916 		return;
3917 	}
3918 
3919 	if (ShouldLogInSimpleFormat(expr)) {
3920 		debug_printf(
3921 		    "Evaluating modifier ${%s:%c%s} on value \"%s\"\n",
3922 		    expr->name, mod[0], is_single_char ? "" : "...",
3923 		    Expr_Str(expr));
3924 		return;
3925 	}
3926 
3927 	debug_printf(
3928 	    "Evaluating modifier ${%s:%c%s} on value \"%s\" (%s, %s)\n",
3929 	    expr->name, mod[0], is_single_char ? "" : "...", Expr_Str(expr),
3930 	    VarEvalMode_Name[expr->emode], ExprDefined_Name[expr->defined]);
3931 }
3932 
3933 static void
LogAfterApply(const ModChain * ch,const char * p,const char * mod)3934 LogAfterApply(const ModChain *ch, const char *p, const char *mod)
3935 {
3936 	const Expr *expr = ch->expr;
3937 	const char *value = Expr_Str(expr);
3938 
3939 	if (ShouldLogInSimpleFormat(expr)) {
3940 		debug_printf("Result of ${%s:%.*s} is \"%s\"\n",
3941 		    expr->name, (int)(p - mod), mod, value);
3942 		return;
3943 	}
3944 
3945 	debug_printf("Result of ${%s:%.*s} is \"%s\" (%s, %s)\n",
3946 	    expr->name, (int)(p - mod), mod, value,
3947 	    VarEvalMode_Name[expr->emode],
3948 	    ExprDefined_Name[expr->defined]);
3949 }
3950 
3951 static ApplyModifierResult
ApplyModifier(const char ** pp,ModChain * ch)3952 ApplyModifier(const char **pp, ModChain *ch)
3953 {
3954 	switch (**pp) {
3955 	case '!':
3956 		return ApplyModifier_ShellCommand(pp, ch);
3957 	case ':':
3958 		return ApplyModifier_Assign(pp, ch);
3959 	case '?':
3960 		return ApplyModifier_IfElse(pp, ch);
3961 	case '@':
3962 		return ApplyModifier_Loop(pp, ch);
3963 	case '[':
3964 		return ApplyModifier_Words(pp, ch);
3965 	case '_':
3966 		return ApplyModifier_Remember(pp, ch);
3967 #ifdef HAVE_REGEX_H
3968 	case 'C':
3969 		return ApplyModifier_Regex(pp, ch);
3970 #endif
3971 	case 'D':
3972 	case 'U':
3973 		return ApplyModifier_Defined(pp, ch);
3974 	case 'E':
3975 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Suffix);
3976 	case 'g':
3977 	case 'l':
3978 		return ApplyModifier_Time(pp, ch);
3979 	case 'H':
3980 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Head);
3981 	case 'h':
3982 		return ApplyModifier_Hash(pp, ch);
3983 	case 'L':
3984 		return ApplyModifier_Literal(pp, ch);
3985 	case 'M':
3986 	case 'N':
3987 		return ApplyModifier_Match(pp, ch);
3988 	case 'm':
3989 		return ApplyModifier_Mtime(pp, ch);
3990 	case 'O':
3991 		return ApplyModifier_Order(pp, ch);
3992 	case 'P':
3993 		return ApplyModifier_Path(pp, ch);
3994 	case 'Q':
3995 	case 'q':
3996 		return ApplyModifier_Quote(pp, ch);
3997 	case 'R':
3998 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Root);
3999 	case 'r':
4000 		return ApplyModifier_Range(pp, ch);
4001 	case 'S':
4002 		return ApplyModifier_Subst(pp, ch);
4003 	case 's':
4004 		if ((*pp)[1] == 'h' && (*pp)[2] == '1')
4005 			return ApplyModifier_SunShell1(pp, ch);
4006 		return ApplyModifier_SunShell(pp, ch);
4007 	case 'T':
4008 		return ApplyModifier_WordFunc(pp, ch, ModifyWord_Tail);
4009 	case 't':
4010 		return ApplyModifier_To(pp, ch);
4011 	case 'u':
4012 		return ApplyModifier_Unique(pp, ch);
4013 	default:
4014 		return AMR_UNKNOWN;
4015 	}
4016 }
4017 
4018 static void ApplyModifiers(Expr *, const char **, char, char);
4019 
4020 typedef enum ApplyModifiersIndirectResult {
4021 	/* The indirect modifiers have been applied successfully. */
4022 	AMIR_CONTINUE,
4023 	/* Fall back to the SysV modifier. */
4024 	AMIR_SYSV,
4025 	/* Error out. */
4026 	AMIR_OUT
4027 } ApplyModifiersIndirectResult;
4028 
4029 /*
4030  * While expanding an expression, expand and apply indirect modifiers,
4031  * such as in ${VAR:${M_indirect}}.
4032  *
4033  * All indirect modifiers of a group must come from a single
4034  * expression.  ${VAR:${M1}} is valid but ${VAR:${M1}${M2}} is not.
4035  *
4036  * Multiple groups of indirect modifiers can be chained by separating them
4037  * with colons.  ${VAR:${M1}:${M2}} contains 2 indirect modifiers.
4038  *
4039  * If the expression is not followed by ch->endc or ':', fall
4040  * back to trying the SysV modifier, such as in ${VAR:${FROM}=${TO}}.
4041  */
4042 static ApplyModifiersIndirectResult
ApplyModifiersIndirect(ModChain * ch,const char ** pp)4043 ApplyModifiersIndirect(ModChain *ch, const char **pp)
4044 {
4045 	Expr *expr = ch->expr;
4046 	const char *p = *pp;
4047 	FStr mods = Var_Parse(&p, expr->scope, expr->emode);
4048 	/* TODO: handle errors */
4049 
4050 	if (mods.str[0] != '\0' && !IsDelimiter(*p, ch)) {
4051 		FStr_Done(&mods);
4052 		return AMIR_SYSV;
4053 	}
4054 
4055 	DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n",
4056 	    mods.str, (int)(p - *pp), *pp);
4057 
4058 	if (ModChain_ShouldEval(ch) && mods.str[0] != '\0') {
4059 		const char *modsp = mods.str;
4060 		EvalStack_Push(VSK_INDIRECT_MODIFIERS, mods.str, NULL);
4061 		ApplyModifiers(expr, &modsp, '\0', '\0');
4062 		EvalStack_Pop();
4063 		if (Expr_Str(expr) == var_Error || *modsp != '\0') {
4064 			FStr_Done(&mods);
4065 			*pp = p;
4066 			return AMIR_OUT;	/* error already reported */
4067 		}
4068 	}
4069 	FStr_Done(&mods);
4070 
4071 	if (*p == ':')
4072 		p++;
4073 	else if (*p == '\0' && ch->endc != '\0') {
4074 		Parse_Error(PARSE_FATAL,
4075 		    "Unclosed expression after indirect modifier, "
4076 		    "expecting \"%c\"",
4077 		    ch->endc);
4078 		*pp = p;
4079 		return AMIR_OUT;
4080 	}
4081 
4082 	*pp = p;
4083 	return AMIR_CONTINUE;
4084 }
4085 
4086 static ApplyModifierResult
ApplySingleModifier(const char ** pp,ModChain * ch)4087 ApplySingleModifier(const char **pp, ModChain *ch)
4088 {
4089 	ApplyModifierResult res;
4090 	const char *mod = *pp;
4091 	const char *p = *pp;
4092 
4093 	if (DEBUG(VAR))
4094 		LogBeforeApply(ch, mod);
4095 
4096 	if (posix_state == PS_SET)
4097 		res = ApplyModifier_SysV(&p, ch);
4098 	else
4099 		res = AMR_UNKNOWN;
4100 	if (res == AMR_UNKNOWN)
4101 		res = ApplyModifier(&p, ch);
4102 
4103 	if (res == AMR_UNKNOWN && posix_state != PS_SET) {
4104 		assert(p == mod);
4105 		res = ApplyModifier_SysV(&p, ch);
4106 	}
4107 
4108 	if (res == AMR_UNKNOWN) {
4109 		/*
4110 		 * Guess the end of the current modifier.
4111 		 * XXX: Skipping the rest of the modifier hides
4112 		 * errors and leads to wrong results.
4113 		 * Parsing should rather stop here.
4114 		 */
4115 		for (p++; !IsDelimiter(*p, ch); p++)
4116 			continue;
4117 		Parse_Error(PARSE_FATAL, "Unknown modifier \":%.*s\"",
4118 		    (int)(p - mod), mod);
4119 		Expr_SetValueRefer(ch->expr, var_Error);
4120 		res = AMR_CLEANUP;
4121 	}
4122 	if (res != AMR_OK) {
4123 		*pp = p;
4124 		return res;
4125 	}
4126 
4127 	if (DEBUG(VAR))
4128 		LogAfterApply(ch, p, mod);
4129 
4130 	if (*p == '\0' && ch->endc != '\0') {
4131 		Parse_Error(PARSE_FATAL,
4132 		    "Unclosed expression, expecting \"%c\" for "
4133 		    "modifier \"%.*s\"",
4134 		    ch->endc, (int)(p - mod), mod);
4135 	} else if (*p == ':') {
4136 		p++;
4137 	} else if (opts.strict && *p != '\0' && *p != ch->endc) {
4138 		Parse_Error(PARSE_FATAL,
4139 		    "Missing delimiter \":\" after modifier \"%.*s\"",
4140 		    (int)(p - mod), mod);
4141 		/*
4142 		 * TODO: propagate parse error to the enclosing
4143 		 * expression
4144 		 */
4145 	}
4146 	*pp = p;
4147 	return AMR_OK;
4148 }
4149 
4150 #if __STDC_VERSION__ >= 199901L
4151 #define ModChain_Init(expr, startc, endc, sep, oneBigWord) \
4152 	(ModChain) { expr, startc, endc, sep, oneBigWord }
4153 #else
4154 MAKE_INLINE ModChain
ModChain_Init(Expr * expr,char startc,char endc,char sep,bool oneBigWord)4155 ModChain_Init(Expr *expr, char startc, char endc, char sep, bool oneBigWord)
4156 {
4157 	ModChain ch;
4158 	ch.expr = expr;
4159 	ch.startc = startc;
4160 	ch.endc = endc;
4161 	ch.sep = sep;
4162 	ch.oneBigWord = oneBigWord;
4163 	return ch;
4164 }
4165 #endif
4166 
4167 /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */
4168 static void
ApplyModifiers(Expr * expr,const char ** pp,char startc,char endc)4169 ApplyModifiers(
4170     Expr *expr,
4171     const char **pp,	/* the parsing position, updated upon return */
4172     char startc,	/* '(' or '{'; or '\0' for indirect modifiers */
4173     char endc		/* ')' or '}'; or '\0' for indirect modifiers */
4174 )
4175 {
4176 	ModChain ch = ModChain_Init(expr, startc, endc, ' ', false);
4177 	const char *p;
4178 
4179 	assert(startc == '(' || startc == '{' || startc == '\0');
4180 	assert(endc == ')' || endc == '}' || endc == '\0');
4181 	assert(Expr_Str(expr) != NULL);
4182 
4183 	p = *pp;
4184 
4185 	if (*p == '\0' && endc != '\0') {
4186 		Parse_Error(PARSE_FATAL,
4187 		    "Unclosed expression, expecting \"%c\"", ch.endc);
4188 		goto cleanup;
4189 	}
4190 
4191 	while (*p != '\0' && *p != endc) {
4192 		ApplyModifierResult res;
4193 
4194 		if (*p == '$') {
4195 			/*
4196 			 * TODO: Only evaluate the expression once, no matter
4197 			 * whether it's an indirect modifier or the initial
4198 			 * part of a SysV modifier.
4199 			 */
4200 			ApplyModifiersIndirectResult amir =
4201 			    ApplyModifiersIndirect(&ch, &p);
4202 			if (amir == AMIR_CONTINUE)
4203 				continue;
4204 			if (amir == AMIR_OUT)
4205 				break;
4206 		}
4207 
4208 		res = ApplySingleModifier(&p, &ch);
4209 		if (res == AMR_CLEANUP)
4210 			goto cleanup;
4211 	}
4212 
4213 	*pp = p;
4214 	assert(Expr_Str(expr) != NULL);	/* Use var_Error or varUndefined. */
4215 	return;
4216 
4217 cleanup:
4218 	/*
4219 	 * TODO: Use p + strlen(p) instead, to stop parsing immediately.
4220 	 *
4221 	 * In the unit tests, this generates a few shell commands with
4222 	 * unbalanced quotes.  Instead of producing these incomplete strings,
4223 	 * commands with evaluation errors should not be run at all.
4224 	 *
4225 	 * To make that happen, Var_Subst must report the actual errors
4226 	 * instead of returning the resulting string unconditionally.
4227 	 */
4228 	*pp = p;
4229 	Expr_SetValueRefer(expr, var_Error);
4230 }
4231 
4232 /*
4233  * Only 4 of the 7 built-in local variables are treated specially as they are
4234  * the only ones that will be set when dynamic sources are expanded.
4235  */
4236 static bool
VarnameIsDynamic(Substring varname)4237 VarnameIsDynamic(Substring varname)
4238 {
4239 	const char *name;
4240 	size_t len;
4241 
4242 	name = varname.start;
4243 	len = Substring_Length(varname);
4244 	if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) {
4245 		switch (name[0]) {
4246 		case '@':
4247 		case '%':
4248 		case '*':
4249 		case '!':
4250 			return true;
4251 		}
4252 		return false;
4253 	}
4254 
4255 	if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) {
4256 		return Substring_Equals(varname, ".TARGET") ||
4257 		       Substring_Equals(varname, ".ARCHIVE") ||
4258 		       Substring_Equals(varname, ".PREFIX") ||
4259 		       Substring_Equals(varname, ".MEMBER");
4260 	}
4261 
4262 	return false;
4263 }
4264 
4265 static const char *
UndefinedShortVarValue(char varname,const GNode * scope)4266 UndefinedShortVarValue(char varname, const GNode *scope)
4267 {
4268 	if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) {
4269 		/*
4270 		 * If substituting a local variable in a non-local scope,
4271 		 * assume it's for dynamic source stuff. We have to handle
4272 		 * this specially and return the longhand for the variable
4273 		 * with the dollar sign escaped so it makes it back to the
4274 		 * caller. Only four of the local variables are treated
4275 		 * specially as they are the only four that will be set
4276 		 * when dynamic sources are expanded.
4277 		 */
4278 		switch (varname) {
4279 		case '@':
4280 			return "$(.TARGET)";
4281 		case '%':
4282 			return "$(.MEMBER)";
4283 		case '*':
4284 			return "$(.PREFIX)";
4285 		case '!':
4286 			return "$(.ARCHIVE)";
4287 		}
4288 	}
4289 	return NULL;
4290 }
4291 
4292 /*
4293  * Parse a variable name, until the end character or a colon, whichever
4294  * comes first.
4295  */
4296 static void
ParseVarname(const char ** pp,char startc,char endc,GNode * scope,VarEvalMode emode,LazyBuf * buf)4297 ParseVarname(const char **pp, char startc, char endc,
4298 	     GNode *scope, VarEvalMode emode,
4299 	     LazyBuf *buf)
4300 {
4301 	const char *p = *pp;
4302 	int depth = 0;
4303 
4304 	LazyBuf_Init(buf, p);
4305 
4306 	while (*p != '\0') {
4307 		if ((*p == endc || *p == ':') && depth == 0)
4308 			break;
4309 		if (*p == startc)
4310 			depth++;
4311 		if (*p == endc)
4312 			depth--;
4313 
4314 		if (*p == '$') {
4315 			FStr nested_val = Var_Parse(&p, scope, emode);
4316 			/* TODO: handle errors */
4317 			LazyBuf_AddStr(buf, nested_val.str);
4318 			FStr_Done(&nested_val);
4319 		} else {
4320 			LazyBuf_Add(buf, *p);
4321 			p++;
4322 		}
4323 	}
4324 	*pp = p;
4325 }
4326 
4327 static bool
IsShortVarnameValid(char varname,const char * start)4328 IsShortVarnameValid(char varname, const char *start)
4329 {
4330 	if (varname != '$' && varname != ':' && varname != '}' &&
4331 	    varname != ')' && varname != '\0')
4332 		return true;
4333 
4334 	if (!opts.strict)
4335 		return false;	/* XXX: Missing error message */
4336 
4337 	if (varname == '$' && save_dollars)
4338 		Parse_Error(PARSE_FATAL,
4339 		    "To escape a dollar, use \\$, not $$, at \"%s\"", start);
4340 	else if (varname == '\0')
4341 		Parse_Error(PARSE_FATAL, "Dollar followed by nothing");
4342 	else if (save_dollars)
4343 		Parse_Error(PARSE_FATAL,
4344 		    "Invalid variable name \"%c\", at \"%s\"", varname, start);
4345 
4346 	return false;
4347 }
4348 
4349 /*
4350  * Parse a single-character variable name such as in $V or $@.
4351  * Return whether to continue parsing.
4352  */
4353 static bool
ParseVarnameShort(char varname,const char ** pp,GNode * scope,VarEvalMode emode,const char ** out_false_val,Var ** out_true_var)4354 ParseVarnameShort(char varname, const char **pp, GNode *scope,
4355 		  VarEvalMode emode,
4356 		  const char **out_false_val,
4357 		  Var **out_true_var)
4358 {
4359 	char name[2];
4360 	Var *v;
4361 	const char *val;
4362 
4363 	if (!IsShortVarnameValid(varname, *pp)) {
4364 		(*pp)++;	/* only skip the '$' */
4365 		*out_false_val = var_Error;
4366 		return false;
4367 	}
4368 
4369 	name[0] = varname;
4370 	name[1] = '\0';
4371 	v = VarFind(name, scope, true);
4372 	if (v != NULL) {
4373 		/* No need to advance *pp, the calling code handles this. */
4374 		*out_true_var = v;
4375 		return true;
4376 	}
4377 
4378 	*pp += 2;
4379 
4380 	val = UndefinedShortVarValue(varname, scope);
4381 	if (val == NULL)
4382 		val = emode == VARE_EVAL_DEFINED
4383 		    || emode == VARE_EVAL_DEFINED_LOUD
4384 		    ? var_Error : varUndefined;
4385 
4386 	if ((opts.strict || emode == VARE_EVAL_DEFINED_LOUD)
4387 	    && val == var_Error) {
4388 		Parse_Error(PARSE_FATAL,
4389 		    "Variable \"%s\" is undefined", name);
4390 	}
4391 
4392 	*out_false_val = val;
4393 	return false;
4394 }
4395 
4396 /* Find variables like @F or <D. */
4397 static Var *
FindLocalLegacyVar(Substring varname,GNode * scope,const char ** out_extraModifiers)4398 FindLocalLegacyVar(Substring varname, GNode *scope,
4399 		   const char **out_extraModifiers)
4400 {
4401 	Var *v;
4402 
4403 	/* Only resolve these variables if scope is a "real" target. */
4404 	if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL)
4405 		return NULL;
4406 
4407 	if (Substring_Length(varname) != 2)
4408 		return NULL;
4409 	if (varname.start[1] != 'F' && varname.start[1] != 'D')
4410 		return NULL;
4411 	if (strchr("@%?*!<>^", varname.start[0]) == NULL)
4412 		return NULL;
4413 
4414 	v = VarFindSubstring(Substring_Init(varname.start, varname.start + 1),
4415 	    scope, false);
4416 	if (v == NULL)
4417 		return NULL;
4418 
4419 	*out_extraModifiers = varname.start[1] == 'D' ? "H:" : "T:";
4420 	return v;
4421 }
4422 
4423 static FStr
EvalUndefined(bool dynamic,const char * start,const char * p,Substring varname,VarEvalMode emode,int parseErrorsBefore)4424 EvalUndefined(bool dynamic, const char *start, const char *p,
4425 	      Substring varname, VarEvalMode emode, int parseErrorsBefore)
4426 {
4427 	if (dynamic)
4428 		return FStr_InitOwn(bmake_strsedup(start, p));
4429 
4430 	if (emode == VARE_EVAL_DEFINED_LOUD
4431 	    || (emode == VARE_EVAL_DEFINED && opts.strict)) {
4432 		if (parseErrors == parseErrorsBefore) {
4433 			Parse_Error(PARSE_FATAL,
4434 			    "Variable \"%.*s\" is undefined",
4435 			    (int) Substring_Length(varname), varname.start);
4436 		}
4437 		return FStr_InitRefer(var_Error);
4438 	}
4439 
4440 	return FStr_InitRefer(
4441 	    emode == VARE_EVAL_DEFINED_LOUD || emode == VARE_EVAL_DEFINED
4442 		? var_Error : varUndefined);
4443 }
4444 
4445 static void
CheckVarname(Substring name)4446 CheckVarname(Substring name)
4447 {
4448 	const char *p;
4449 
4450 	for (p = name.start; p < name.end; p++) {
4451 		if (ch_isspace(*p))
4452 			break;
4453 	}
4454 	if (p < name.end) {
4455 		Parse_Error(PARSE_WARNING,
4456 		    ch_isprint(*p)
4457 		    ? "Invalid character \"%c\" in variable name \"%.*s\""
4458 		    : "Invalid character \"\\x%02x\" in variable name \"%.*s\"",
4459 		    (int)*p,
4460 		    (int)Substring_Length(name), name.start);
4461 	}
4462 }
4463 
4464 /*
4465  * Parse a long variable name enclosed in braces or parentheses such as $(VAR)
4466  * or ${VAR}, up to the closing brace or parenthesis, or in the case of
4467  * ${VAR:Modifiers}, up to the ':' that starts the modifiers.
4468  * Return whether to continue parsing.
4469  */
4470 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)4471 ParseVarnameLong(
4472 	const char **pp,
4473 	char startc,
4474 	GNode *scope,
4475 	VarEvalMode emode,
4476 	VarEvalMode nested_emode,
4477 	int parseErrorsBefore,
4478 
4479 	const char **out_false_pp,
4480 	FStr *out_false_val,
4481 
4482 	char *out_true_endc,
4483 	Var **out_true_v,
4484 	bool *out_true_haveModifier,
4485 	const char **out_true_extraModifiers,
4486 	bool *out_true_dynamic,
4487 	ExprDefined *out_true_exprDefined
4488 )
4489 {
4490 	LazyBuf varname;
4491 	Substring name;
4492 	Var *v;
4493 	bool haveModifier;
4494 	bool dynamic = false;
4495 
4496 	const char *p = *pp;
4497 	const char *start = p;
4498 	char endc = startc == '(' ? ')' : '}';
4499 
4500 	p += 2;			/* skip "${" or "$(" or "y(" */
4501 	ParseVarname(&p, startc, endc, scope, nested_emode, &varname);
4502 	name = LazyBuf_Get(&varname);
4503 
4504 	if (*p == ':')
4505 		haveModifier = true;
4506 	else if (*p == endc)
4507 		haveModifier = false;
4508 	else {
4509 		Parse_Error(PARSE_FATAL, "Unclosed variable \"%.*s\"",
4510 		    (int)Substring_Length(name), name.start);
4511 		LazyBuf_Done(&varname);
4512 		*out_false_pp = p;
4513 		*out_false_val = FStr_InitRefer(var_Error);
4514 		return false;
4515 	}
4516 
4517 	v = VarFindSubstring(name, scope, true);
4518 
4519 	/*
4520 	 * At this point, p points just after the variable name, either at
4521 	 * ':' or at endc.
4522 	 */
4523 
4524 	if (v == NULL && Substring_Equals(name, ".SUFFIXES")) {
4525 		char *suffixes = Suff_NamesStr();
4526 		v = VarNew(FStr_InitRefer(".SUFFIXES"), suffixes,
4527 		    true, false, true);
4528 		free(suffixes);
4529 	} else if (v == NULL)
4530 		v = FindLocalLegacyVar(name, scope, out_true_extraModifiers);
4531 
4532 	if (v == NULL) {
4533 		/*
4534 		 * Defer expansion of dynamic variables if they appear in
4535 		 * non-local scope since they are not defined there.
4536 		 */
4537 		dynamic = VarnameIsDynamic(name) &&
4538 			  (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL);
4539 
4540 		if (!haveModifier) {
4541 			CheckVarname(name);
4542 			p++;	/* skip endc */
4543 			*out_false_pp = p;
4544 			*out_false_val = EvalUndefined(dynamic, start, p,
4545 			    name, emode, parseErrorsBefore);
4546 			LazyBuf_Done(&varname);
4547 			return false;
4548 		}
4549 
4550 		/*
4551 		 * The expression is based on an undefined variable.
4552 		 * Nevertheless it needs a Var, for modifiers that access the
4553 		 * variable name, such as :L or :?.
4554 		 *
4555 		 * Most modifiers leave this expression in the "undefined"
4556 		 * state (DEF_UNDEF), only a few modifiers like :D, :U, :L,
4557 		 * :P turn this undefined expression into a defined
4558 		 * expression (DEF_DEFINED).
4559 		 *
4560 		 * In the end, after applying all modifiers, if the expression
4561 		 * is still undefined, Var_Parse will return an empty string
4562 		 * instead of the actually computed value.
4563 		 */
4564 		v = VarNew(LazyBuf_DoneGet(&varname), "",
4565 		    true, false, false);
4566 		*out_true_exprDefined = DEF_UNDEF;
4567 	} else
4568 		LazyBuf_Done(&varname);
4569 
4570 	*pp = p;
4571 	*out_true_endc = endc;
4572 	*out_true_v = v;
4573 	*out_true_haveModifier = haveModifier;
4574 	*out_true_dynamic = dynamic;
4575 	return true;
4576 }
4577 
4578 #if __STDC_VERSION__ >= 199901L
4579 #define Expr_Init(name, value, emode, scope, defined) \
4580 	(Expr) { name, value, emode, scope, defined }
4581 #else
4582 MAKE_INLINE Expr
Expr_Init(const char * name,FStr value,VarEvalMode emode,GNode * scope,ExprDefined defined)4583 Expr_Init(const char *name, FStr value,
4584 	  VarEvalMode emode, GNode *scope, ExprDefined defined)
4585 {
4586 	Expr expr;
4587 
4588 	expr.name = name;
4589 	expr.value = value;
4590 	expr.emode = emode;
4591 	expr.scope = scope;
4592 	expr.defined = defined;
4593 	return expr;
4594 }
4595 #endif
4596 
4597 /*
4598  * Expressions of the form ${:U...} with a trivial value are often generated
4599  * by .for loops and are boring, so evaluate them without debug logging.
4600  */
4601 static bool
Var_Parse_U(const char ** pp,VarEvalMode emode,FStr * out_value)4602 Var_Parse_U(const char **pp, VarEvalMode emode, FStr *out_value)
4603 {
4604 	const char *p;
4605 
4606 	p = *pp;
4607 	if (!(p[0] == '$' && p[1] == '{' && p[2] == ':' && p[3] == 'U'))
4608 		return false;
4609 
4610 	p += 4;
4611 	while (*p != '$' && *p != '{' && *p != ':' && *p != '\\' &&
4612 	       *p != '}' && *p != '\0')
4613 		p++;
4614 	if (*p != '}')
4615 		return false;
4616 
4617 	*out_value = emode == VARE_PARSE
4618 	    ? FStr_InitRefer("")
4619 	    : FStr_InitOwn(bmake_strsedup(*pp + 4, p));
4620 	*pp = p + 1;
4621 	return true;
4622 }
4623 
4624 /*
4625  * Given the start of an expression (such as $v, $(VAR), ${VAR:Mpattern}),
4626  * extract the variable name and the modifiers, if any.  While parsing, apply
4627  * the modifiers to the value of the expression.
4628  *
4629  * Input:
4630  *	*pp		The string to parse.
4631  *			When called from CondParser_FuncCallEmpty, it can
4632  *			also point to the "y" of "empty(VARNAME:Modifiers)".
4633  *	scope		The scope for finding variables.
4634  *	emode		Controls the exact details of parsing and evaluation.
4635  *
4636  * Output:
4637  *	*pp		The position where to continue parsing.
4638  *			TODO: After a parse error, the value of *pp is
4639  *			unspecified.  It may not have been updated at all,
4640  *			point to some random character in the string, to the
4641  *			location of the parse error, or at the end of the
4642  *			string.
4643  *	return		The value of the expression, never NULL.
4644  *	return		var_Error if there was a parse error.
4645  *	return		var_Error if the base variable of the expression was
4646  *			undefined, emode is VARE_EVAL_DEFINED, and none of
4647  *			the modifiers turned the undefined expression into a
4648  *			defined expression.
4649  *			XXX: It is not guaranteed that an error message has
4650  *			been printed.
4651  *	return		varUndefined if the base variable of the expression
4652  *			was undefined, emode was not VARE_EVAL_DEFINED,
4653  *			and none of the modifiers turned the undefined
4654  *			expression into a defined expression.
4655  */
4656 FStr
Var_Parse(const char ** pp,GNode * scope,VarEvalMode emode)4657 Var_Parse(const char **pp, GNode *scope, VarEvalMode emode)
4658 {
4659 	const char *start, *p;
4660 	bool haveModifier;	/* true for ${VAR:...}, false for ${VAR} */
4661 	char startc;		/* the actual '{' or '(' or '\0' */
4662 	char endc;		/* the expected '}' or ')' or '\0' */
4663 	/*
4664 	 * true if the expression is based on one of the 7 predefined
4665 	 * variables that are local to a target, and the expression is
4666 	 * expanded in a non-local scope.  The result is the text of the
4667 	 * expression, unaltered.  This is needed to support dynamic sources.
4668 	 */
4669 	bool dynamic;
4670 	const char *extramodifiers;
4671 	Var *v;
4672 	Expr expr = Expr_Init(NULL, FStr_InitRefer(NULL),
4673 	    emode == VARE_EVAL_DEFINED || emode == VARE_EVAL_DEFINED_LOUD
4674 		? VARE_EVAL : emode,
4675 	    scope, DEF_REGULAR);
4676 	FStr val;
4677 	int parseErrorsBefore = parseErrors;
4678 
4679 	if (Var_Parse_U(pp, emode, &val))
4680 		return val;
4681 
4682 	p = *pp;
4683 	start = p;
4684 	DEBUG2(VAR, "Var_Parse: %s (%s)\n", start, VarEvalMode_Name[emode]);
4685 
4686 	val = FStr_InitRefer(NULL);
4687 	extramodifiers = NULL;	/* extra modifiers to apply first */
4688 	dynamic = false;
4689 
4690 	endc = '\0';		/* Appease GCC. */
4691 
4692 	startc = p[1];
4693 	if (startc != '(' && startc != '{') {
4694 		if (!ParseVarnameShort(startc, pp, scope, emode, &val.str, &v))
4695 			return val;
4696 		haveModifier = false;
4697 		p++;
4698 	} else {
4699 		if (!ParseVarnameLong(&p, startc, scope, emode, expr.emode,
4700 		    parseErrorsBefore,
4701 		    pp, &val,
4702 		    &endc, &v, &haveModifier, &extramodifiers,
4703 		    &dynamic, &expr.defined))
4704 			return val;
4705 	}
4706 
4707 	expr.name = v->name.str;
4708 	if (v->inUse && VarEvalMode_ShouldEval(emode)) {
4709 		Parse_Error(PARSE_FATAL, "Variable %s is recursive.",
4710 		    v->name.str);
4711 		FStr_Done(&val);
4712 		if (*p != '\0')
4713 			p++;
4714 		*pp = p;
4715 		return FStr_InitRefer(var_Error);
4716 	}
4717 
4718 	/*
4719 	 * FIXME: This assignment creates an alias to the current value of the
4720 	 * variable.  This means that as long as the value of the expression
4721 	 * stays the same, the value of the variable must not change, and the
4722 	 * variable must not be deleted.  Using the ':@' modifier, it is
4723 	 * possible (since var.c 1.212 from 2017-02-01) to delete the variable
4724 	 * while its value is still being used:
4725 	 *
4726 	 *	VAR=	value
4727 	 *	_:=	${VAR:${:U:@VAR@@}:S,^,prefix,}
4728 	 *
4729 	 * The same effect might be achievable using the '::=' or the ':_'
4730 	 * modifiers.
4731 	 *
4732 	 * At the bottom of this function, the resulting value is compared to
4733 	 * the then-current value of the variable.  This might also invoke
4734 	 * undefined behavior.
4735 	 */
4736 	expr.value = FStr_InitRefer(v->val.data);
4737 
4738 	if (!VarEvalMode_ShouldEval(emode))
4739 		EvalStack_Push(VSK_EXPR_PARSE, start, NULL);
4740 	else if (expr.name[0] != '\0')
4741 		EvalStack_Push(VSK_VARNAME, expr.name, &expr.value);
4742 	else
4743 		EvalStack_Push(VSK_EXPR, start, &expr.value);
4744 
4745 	/*
4746 	 * Before applying any modifiers, expand any nested expressions from
4747 	 * the variable value.
4748 	 */
4749 	if (VarEvalMode_ShouldEval(emode) &&
4750 	    strchr(Expr_Str(&expr), '$') != NULL) {
4751 		char *expanded;
4752 		v->inUse = true;
4753 		expanded = Var_Subst(Expr_Str(&expr), scope, expr.emode);
4754 		v->inUse = false;
4755 		/* TODO: handle errors */
4756 		Expr_SetValueOwn(&expr, expanded);
4757 	}
4758 
4759 	if (extramodifiers != NULL) {
4760 		const char *em = extramodifiers;
4761 		ApplyModifiers(&expr, &em, '\0', '\0');
4762 	}
4763 
4764 	if (haveModifier) {
4765 		p++;		/* Skip initial colon. */
4766 		ApplyModifiers(&expr, &p, startc, endc);
4767 	}
4768 
4769 	if (*p != '\0')		/* Skip past endc if possible. */
4770 		p++;
4771 
4772 	*pp = p;
4773 
4774 	if (expr.defined == DEF_UNDEF) {
4775 		Substring varname = Substring_InitStr(expr.name);
4776 		FStr value = EvalUndefined(dynamic, start, p, varname, emode,
4777 		    parseErrorsBefore);
4778 		Expr_SetValue(&expr, value);
4779 	}
4780 
4781 	EvalStack_Pop();
4782 
4783 	if (v->shortLived) {
4784 		if (expr.value.str == v->val.data) {
4785 			/* move ownership */
4786 			expr.value.freeIt = v->val.data;
4787 			v->val.data = NULL;
4788 		}
4789 		VarFreeShortLived(v);
4790 	}
4791 
4792 	return expr.value;
4793 }
4794 
4795 static void
VarSubstDollarDollar(const char ** pp,Buffer * res,VarEvalMode emode)4796 VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalMode emode)
4797 {
4798 	/* A dollar sign may be escaped with another dollar sign. */
4799 	if (save_dollars && VarEvalMode_ShouldKeepDollar(emode))
4800 		Buf_AddByte(res, '$');
4801 	Buf_AddByte(res, '$');
4802 	*pp += 2;
4803 }
4804 
4805 static void
VarSubstExpr(const char ** pp,Buffer * buf,GNode * scope,VarEvalMode emode)4806 VarSubstExpr(const char **pp, Buffer *buf, GNode *scope, VarEvalMode emode)
4807 {
4808 	const char *p = *pp;
4809 	const char *nested_p = p;
4810 	FStr val = Var_Parse(&nested_p, scope, emode);
4811 	/* TODO: handle errors */
4812 
4813 	if (val.str == var_Error || val.str == varUndefined) {
4814 		if (!VarEvalMode_ShouldKeepUndef(emode)
4815 		    || val.str == var_Error) {
4816 			p = nested_p;
4817 		} else {
4818 			/*
4819 			 * Copy the initial '$' of the undefined expression,
4820 			 * thereby deferring expansion of the expression, but
4821 			 * expand nested expressions if already possible. See
4822 			 * unit-tests/varparse-undef-partial.mk.
4823 			 */
4824 			Buf_AddByte(buf, *p);
4825 			p++;
4826 		}
4827 	} else {
4828 		p = nested_p;
4829 		Buf_AddStr(buf, val.str);
4830 	}
4831 
4832 	FStr_Done(&val);
4833 
4834 	*pp = p;
4835 }
4836 
4837 /*
4838  * Skip as many characters as possible -- either to the end of the string,
4839  * or to the next dollar sign, which may start an expression.
4840  */
4841 static void
VarSubstPlain(const char ** pp,Buffer * res)4842 VarSubstPlain(const char **pp, Buffer *res)
4843 {
4844 	const char *p = *pp;
4845 	const char *start = p;
4846 
4847 	for (p++; *p != '$' && *p != '\0'; p++)
4848 		continue;
4849 	Buf_AddRange(res, start, p);
4850 	*pp = p;
4851 }
4852 
4853 /*
4854  * Expand all expressions like $V, ${VAR}, $(VAR:Modifiers) in the
4855  * given string.
4856  *
4857  * Input:
4858  *	str		The string in which the expressions are expanded.
4859  *	scope		The scope in which to start searching for variables.
4860  *			The other scopes are searched as well.
4861  *	emode		The mode for parsing or evaluating subexpressions.
4862  */
4863 char *
Var_Subst(const char * str,GNode * scope,VarEvalMode emode)4864 Var_Subst(const char *str, GNode *scope, VarEvalMode emode)
4865 {
4866 	const char *p = str;
4867 	Buffer res;
4868 
4869 	Buf_Init(&res);
4870 
4871 	while (*p != '\0') {
4872 		if (p[0] == '$' && p[1] == '$')
4873 			VarSubstDollarDollar(&p, &res, emode);
4874 		else if (p[0] == '$')
4875 			VarSubstExpr(&p, &res, scope, emode);
4876 		else
4877 			VarSubstPlain(&p, &res);
4878 	}
4879 
4880 	return Buf_DoneData(&res);
4881 }
4882 
4883 char *
Var_SubstInTarget(const char * str,GNode * scope)4884 Var_SubstInTarget(const char *str, GNode *scope)
4885 {
4886 	char *res;
4887 	EvalStack_Push(VSK_TARGET, scope->name, NULL);
4888 	EvalStack_Push(VSK_COMMAND, str, NULL);
4889 	res = Var_Subst(str, scope, VARE_EVAL);
4890 	EvalStack_Pop();
4891 	EvalStack_Pop();
4892 	return res;
4893 }
4894 
4895 void
Var_ExportStackTrace(const char * target,const char * cmd)4896 Var_ExportStackTrace(const char *target, const char *cmd)
4897 {
4898 	char *stackTrace;
4899 
4900 	if (GetParentStackTrace() == NULL)
4901 		return;
4902 
4903 	if (target != NULL)
4904 		EvalStack_Push(VSK_TARGET, target, NULL);
4905 	if (cmd != NULL)
4906 		EvalStack_Push(VSK_COMMAND, cmd, NULL);
4907 
4908 	stackTrace = GetStackTrace(true);
4909 	(void)setenv("MAKE_STACK_TRACE", stackTrace, 1);
4910 	free(stackTrace);
4911 
4912 	if (cmd != NULL)
4913 		EvalStack_Pop();
4914 	if (target != NULL)
4915 		EvalStack_Pop();
4916 }
4917 
4918 void
Var_Expand(FStr * str,GNode * scope,VarEvalMode emode)4919 Var_Expand(FStr *str, GNode *scope, VarEvalMode emode)
4920 {
4921 	char *expanded;
4922 
4923 	if (strchr(str->str, '$') == NULL)
4924 		return;
4925 	expanded = Var_Subst(str->str, scope, emode);
4926 	/* TODO: handle errors */
4927 	FStr_Done(str);
4928 	*str = FStr_InitOwn(expanded);
4929 }
4930 
4931 void
Var_Stats(void)4932 Var_Stats(void)
4933 {
4934 	HashTable_DebugStats(&SCOPE_GLOBAL->vars, "Global variables");
4935 }
4936 
4937 static int
StrAsc(const void * sa,const void * sb)4938 StrAsc(const void *sa, const void *sb)
4939 {
4940 	return strcmp(*(const char *const *)sa, *(const char *const *)sb);
4941 }
4942 
4943 
4944 /* Print all variables in a scope, sorted by name. */
4945 void
Var_Dump(GNode * scope)4946 Var_Dump(GNode *scope)
4947 {
4948 	Vector /* of const char * */ vec;
4949 	HashIter hi;
4950 	size_t i;
4951 	const char **varnames;
4952 
4953 	Vector_Init(&vec, sizeof(const char *));
4954 
4955 	HashIter_Init(&hi, &scope->vars);
4956 	while (HashIter_Next(&hi))
4957 		*(const char **)Vector_Push(&vec) = hi.entry->key;
4958 	varnames = vec.items;
4959 
4960 	qsort(varnames, vec.len, sizeof varnames[0], StrAsc);
4961 
4962 	for (i = 0; i < vec.len; i++) {
4963 		const char *varname = varnames[i];
4964 		const Var *var = HashTable_FindValue(&scope->vars, varname);
4965 		debug_printf("%-16s = %s%s\n", varname,
4966 		    var->val.data, ValueDescription(var->val.data));
4967 	}
4968 
4969 	Vector_Done(&vec);
4970 }
4971