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