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