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