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