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