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