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