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