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