Home | History | Annotate | Line # | Download | only in make
var.c revision 1.27
      1 /*	$NetBSD: var.c,v 1.27 1998/09/18 20:35:12 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * Copyright (c) 1989 by Berkeley Softworks
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Adam de Boor.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  */
     40 
     41 #ifdef MAKE_BOOTSTRAP
     42 static char rcsid[] = "$NetBSD: var.c,v 1.27 1998/09/18 20:35:12 christos Exp $";
     43 #else
     44 #include <sys/cdefs.h>
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
     48 #else
     49 __RCSID("$NetBSD: var.c,v 1.27 1998/09/18 20:35:12 christos Exp $");
     50 #endif
     51 #endif /* not lint */
     52 #endif
     53 
     54 /*-
     55  * var.c --
     56  *	Variable-handling functions
     57  *
     58  * Interface:
     59  *	Var_Set	  	    Set the value of a variable in the given
     60  *	    	  	    context. The variable is created if it doesn't
     61  *	    	  	    yet exist. The value and variable name need not
     62  *	    	  	    be preserved.
     63  *
     64  *	Var_Append	    Append more characters to an existing variable
     65  *	    	  	    in the given context. The variable needn't
     66  *	    	  	    exist already -- it will be created if it doesn't.
     67  *	    	  	    A space is placed between the old value and the
     68  *	    	  	    new one.
     69  *
     70  *	Var_Exists	    See if a variable exists.
     71  *
     72  *	Var_Value 	    Return the value of a variable in a context or
     73  *	    	  	    NULL if the variable is undefined.
     74  *
     75  *	Var_Subst 	    Substitute named variable, or all variables if
     76  *			    NULL in a string using
     77  *	    	  	    the given context as the top-most one. If the
     78  *	    	  	    third argument is non-zero, Parse_Error is
     79  *	    	  	    called if any variables are undefined.
     80  *
     81  *	Var_Parse 	    Parse a variable expansion from a string and
     82  *	    	  	    return the result and the number of characters
     83  *	    	  	    consumed.
     84  *
     85  *	Var_Delete	    Delete a variable in a context.
     86  *
     87  *	Var_Init  	    Initialize this module.
     88  *
     89  * Debugging:
     90  *	Var_Dump  	    Print out all variables defined in the given
     91  *	    	  	    context.
     92  *
     93  * XXX: There's a lot of duplication in these functions.
     94  */
     95 
     96 #include    <ctype.h>
     97 #ifndef MAKE_BOOTSTRAP
     98 #include    <regex.h>
     99 #endif
    100 #include    <stdlib.h>
    101 #include    "make.h"
    102 #include    "buf.h"
    103 
    104 /*
    105  * This is a harmless return value for Var_Parse that can be used by Var_Subst
    106  * to determine if there was an error in parsing -- easier than returning
    107  * a flag, as things outside this module don't give a hoot.
    108  */
    109 char 	var_Error[] = "";
    110 
    111 /*
    112  * Similar to var_Error, but returned when the 'err' flag for Var_Parse is
    113  * set false. Why not just use a constant? Well, gcc likes to condense
    114  * identical string instances...
    115  */
    116 static char	varNoError[] = "";
    117 
    118 /*
    119  * Internally, variables are contained in four different contexts.
    120  *	1) the environment. They may not be changed. If an environment
    121  *	    variable is appended-to, the result is placed in the global
    122  *	    context.
    123  *	2) the global context. Variables set in the Makefile are located in
    124  *	    the global context. It is the penultimate context searched when
    125  *	    substituting.
    126  *	3) the command-line context. All variables set on the command line
    127  *	   are placed in this context. They are UNALTERABLE once placed here.
    128  *	4) the local context. Each target has associated with it a context
    129  *	   list. On this list are located the structures describing such
    130  *	   local variables as $(@) and $(*)
    131  * The four contexts are searched in the reverse order from which they are
    132  * listed.
    133  */
    134 GNode          *VAR_GLOBAL;   /* variables from the makefile */
    135 GNode          *VAR_CMD;      /* variables defined on the command-line */
    136 
    137 static Lst	allVars;      /* List of all variables */
    138 
    139 #define FIND_CMD	0x1   /* look in VAR_CMD when searching */
    140 #define FIND_GLOBAL	0x2   /* look in VAR_GLOBAL as well */
    141 #define FIND_ENV  	0x4   /* look in the environment also */
    142 
    143 typedef struct Var {
    144     char          *name;	/* the variable's name */
    145     Buffer	  val;	    	/* its value */
    146     int	    	  flags;    	/* miscellaneous status flags */
    147 #define VAR_IN_USE	1   	    /* Variable's value currently being used.
    148 				     * Used to avoid recursion */
    149 #define VAR_FROM_ENV	2   	    /* Variable comes from the environment */
    150 #define VAR_JUNK  	4   	    /* Variable is a junk variable that
    151 				     * should be destroyed when done with
    152 				     * it. Used by Var_Parse for undefined,
    153 				     * modified variables */
    154 }  Var;
    155 
    156 
    157 /* Var*Pattern flags */
    158 #define VAR_SUB_GLOBAL	0x01	/* Apply substitution globally */
    159 #define VAR_SUB_ONE	0x02	/* Apply substitution to one word */
    160 #define VAR_SUB_MATCHED	0x04	/* There was a match */
    161 #define VAR_MATCH_START	0x08	/* Match at start of word */
    162 #define VAR_MATCH_END	0x10	/* Match at end of word */
    163 
    164 typedef struct {
    165     char    	  *lhs;	    /* String to match */
    166     int	    	   leftLen; /* Length of string */
    167     char    	  *rhs;	    /* Replacement string (w/ &'s removed) */
    168     int	    	  rightLen; /* Length of replacement */
    169     int	    	  flags;
    170 } VarPattern;
    171 
    172 #ifndef MAKE_BOOTSTRAP
    173 typedef struct {
    174     regex_t	   re;
    175     int		   nsub;
    176     regmatch_t 	  *matches;
    177     char 	  *replace;
    178     int		   flags;
    179 } VarREPattern;
    180 #endif
    181 
    182 static int VarCmp __P((ClientData, ClientData));
    183 static Var *VarFind __P((char *, GNode *, int));
    184 static void VarAdd __P((char *, char *, GNode *));
    185 static void VarDelete __P((ClientData));
    186 static Boolean VarHead __P((char *, Boolean, Buffer, ClientData));
    187 static Boolean VarTail __P((char *, Boolean, Buffer, ClientData));
    188 static Boolean VarSuffix __P((char *, Boolean, Buffer, ClientData));
    189 static Boolean VarRoot __P((char *, Boolean, Buffer, ClientData));
    190 static Boolean VarMatch __P((char *, Boolean, Buffer, ClientData));
    191 #ifdef SYSVVARSUB
    192 static Boolean VarSYSVMatch __P((char *, Boolean, Buffer, ClientData));
    193 #endif
    194 static Boolean VarNoMatch __P((char *, Boolean, Buffer, ClientData));
    195 #ifndef MAKE_BOOTSTRAP
    196 static void VarREError __P((int, regex_t *, const char *));
    197 static Boolean VarRESubstitute __P((char *, Boolean, Buffer, ClientData));
    198 #endif
    199 static Boolean VarSubstitute __P((char *, Boolean, Buffer, ClientData));
    200 static char *VarGetPattern __P((GNode *, int, char **, int, int *, int *,
    201 				VarPattern *));
    202 static char *VarQuote __P((char *));
    203 static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer,
    204 						ClientData),
    205 			    ClientData));
    206 static int VarPrintVar __P((ClientData, ClientData));
    207 
    208 /*-
    209  *-----------------------------------------------------------------------
    210  * VarCmp  --
    211  *	See if the given variable matches the named one. Called from
    212  *	Lst_Find when searching for a variable of a given name.
    213  *
    214  * Results:
    215  *	0 if they match. non-zero otherwise.
    216  *
    217  * Side Effects:
    218  *	none
    219  *-----------------------------------------------------------------------
    220  */
    221 static int
    222 VarCmp (v, name)
    223     ClientData     v;		/* VAR structure to compare */
    224     ClientData     name;	/* name to look for */
    225 {
    226     return (strcmp ((char *) name, ((Var *) v)->name));
    227 }
    228 
    229 /*-
    230  *-----------------------------------------------------------------------
    231  * VarFind --
    232  *	Find the given variable in the given context and any other contexts
    233  *	indicated.
    234  *
    235  * Results:
    236  *	A pointer to the structure describing the desired variable or
    237  *	NIL if the variable does not exist.
    238  *
    239  * Side Effects:
    240  *	None
    241  *-----------------------------------------------------------------------
    242  */
    243 static Var *
    244 VarFind (name, ctxt, flags)
    245     char           	*name;	/* name to find */
    246     GNode          	*ctxt;	/* context in which to find it */
    247     int             	flags;	/* FIND_GLOBAL set means to look in the
    248 				 * VAR_GLOBAL context as well.
    249 				 * FIND_CMD set means to look in the VAR_CMD
    250 				 * context also.
    251 				 * FIND_ENV set means to look in the
    252 				 * environment */
    253 {
    254     LstNode         	var;
    255     Var		  	*v;
    256 
    257 	/*
    258 	 * If the variable name begins with a '.', it could very well be one of
    259 	 * the local ones.  We check the name against all the local variables
    260 	 * and substitute the short version in for 'name' if it matches one of
    261 	 * them.
    262 	 */
    263 	if (*name == '.' && isupper((unsigned char) name[1]))
    264 		switch (name[1]) {
    265 		case 'A':
    266 			if (!strcmp(name, ".ALLSRC"))
    267 				name = ALLSRC;
    268 			if (!strcmp(name, ".ARCHIVE"))
    269 				name = ARCHIVE;
    270 			break;
    271 		case 'I':
    272 			if (!strcmp(name, ".IMPSRC"))
    273 				name = IMPSRC;
    274 			break;
    275 		case 'M':
    276 			if (!strcmp(name, ".MEMBER"))
    277 				name = MEMBER;
    278 			break;
    279 		case 'O':
    280 			if (!strcmp(name, ".OODATE"))
    281 				name = OODATE;
    282 			break;
    283 		case 'P':
    284 			if (!strcmp(name, ".PREFIX"))
    285 				name = PREFIX;
    286 			break;
    287 		case 'T':
    288 			if (!strcmp(name, ".TARGET"))
    289 				name = TARGET;
    290 			break;
    291 		}
    292     /*
    293      * First look for the variable in the given context. If it's not there,
    294      * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
    295      * depending on the FIND_* flags in 'flags'
    296      */
    297     var = Lst_Find (ctxt->context, (ClientData)name, VarCmp);
    298 
    299     if ((var == NILLNODE) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) {
    300 	var = Lst_Find (VAR_CMD->context, (ClientData)name, VarCmp);
    301     }
    302     if (!checkEnvFirst && (var == NILLNODE) && (flags & FIND_GLOBAL) &&
    303 	(ctxt != VAR_GLOBAL))
    304     {
    305 	var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp);
    306     }
    307     if ((var == NILLNODE) && (flags & FIND_ENV)) {
    308 	char *env;
    309 
    310 	if ((env = getenv (name)) != NULL) {
    311 	    int	  	len;
    312 
    313 	    v = (Var *) emalloc(sizeof(Var));
    314 	    v->name = estrdup(name);
    315 
    316 	    len = strlen(env);
    317 
    318 	    v->val = Buf_Init(len);
    319 	    Buf_AddBytes(v->val, len, (Byte *)env);
    320 
    321 	    v->flags = VAR_FROM_ENV;
    322 	    return (v);
    323 	} else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
    324 		   (ctxt != VAR_GLOBAL))
    325 	{
    326 	    var = Lst_Find (VAR_GLOBAL->context, (ClientData)name, VarCmp);
    327 	    if (var == NILLNODE) {
    328 		return ((Var *) NIL);
    329 	    } else {
    330 		return ((Var *)Lst_Datum(var));
    331 	    }
    332 	} else {
    333 	    return((Var *)NIL);
    334 	}
    335     } else if (var == NILLNODE) {
    336 	return ((Var *) NIL);
    337     } else {
    338 	return ((Var *) Lst_Datum (var));
    339     }
    340 }
    341 
    342 /*-
    343  *-----------------------------------------------------------------------
    344  * VarAdd  --
    345  *	Add a new variable of name name and value val to the given context
    346  *
    347  * Results:
    348  *	None
    349  *
    350  * Side Effects:
    351  *	The new variable is placed at the front of the given context
    352  *	The name and val arguments are duplicated so they may
    353  *	safely be freed.
    354  *-----------------------------------------------------------------------
    355  */
    356 static void
    357 VarAdd (name, val, ctxt)
    358     char           *name;	/* name of variable to add */
    359     char           *val;	/* value to set it to */
    360     GNode          *ctxt;	/* context in which to set it */
    361 {
    362     register Var   *v;
    363     int	    	  len;
    364 
    365     v = (Var *) emalloc (sizeof (Var));
    366 
    367     v->name = estrdup (name);
    368 
    369     len = val ? strlen(val) : 0;
    370     v->val = Buf_Init(len+1);
    371     Buf_AddBytes(v->val, len, (Byte *)val);
    372 
    373     v->flags = 0;
    374 
    375     (void) Lst_AtFront (ctxt->context, (ClientData)v);
    376     (void) Lst_AtEnd (allVars, (ClientData) v);
    377     if (DEBUG(VAR)) {
    378 	printf("%s:%s = %s\n", ctxt->name, name, val);
    379     }
    380 }
    381 
    382 
    383 /*-
    384  *-----------------------------------------------------------------------
    385  * VarDelete  --
    386  *	Delete a variable and all the space associated with it.
    387  *
    388  * Results:
    389  *	None
    390  *
    391  * Side Effects:
    392  *	None
    393  *-----------------------------------------------------------------------
    394  */
    395 static void
    396 VarDelete(vp)
    397     ClientData vp;
    398 {
    399     Var *v = (Var *) vp;
    400     free(v->name);
    401     Buf_Destroy(v->val, TRUE);
    402     free((Address) v);
    403 }
    404 
    405 
    406 
    407 /*-
    408  *-----------------------------------------------------------------------
    409  * Var_Delete --
    410  *	Remove a variable from a context.
    411  *
    412  * Results:
    413  *	None.
    414  *
    415  * Side Effects:
    416  *	The Var structure is removed and freed.
    417  *
    418  *-----------------------------------------------------------------------
    419  */
    420 void
    421 Var_Delete(name, ctxt)
    422     char    	  *name;
    423     GNode	  *ctxt;
    424 {
    425     LstNode 	  ln;
    426 
    427     if (DEBUG(VAR)) {
    428 	printf("%s:delete %s\n", ctxt->name, name);
    429     }
    430     ln = Lst_Find(ctxt->context, (ClientData)name, VarCmp);
    431     if (ln != NILLNODE) {
    432 	register Var 	  *v;
    433 
    434 	v = (Var *)Lst_Datum(ln);
    435 	Lst_Remove(ctxt->context, ln);
    436 	ln = Lst_Member(allVars, v);
    437 	Lst_Remove(allVars, ln);
    438 	VarDelete((ClientData) v);
    439     }
    440 }
    441 
    442 /*-
    443  *-----------------------------------------------------------------------
    444  * Var_Set --
    445  *	Set the variable name to the value val in the given context.
    446  *
    447  * Results:
    448  *	None.
    449  *
    450  * Side Effects:
    451  *	If the variable doesn't yet exist, a new record is created for it.
    452  *	Else the old value is freed and the new one stuck in its place
    453  *
    454  * Notes:
    455  *	The variable is searched for only in its context before being
    456  *	created in that context. I.e. if the context is VAR_GLOBAL,
    457  *	only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
    458  *	VAR_CMD->context is searched. This is done to avoid the literally
    459  *	thousands of unnecessary strcmp's that used to be done to
    460  *	set, say, $(@) or $(<).
    461  *-----------------------------------------------------------------------
    462  */
    463 void
    464 Var_Set (name, val, ctxt)
    465     char           *name;	/* name of variable to set */
    466     char           *val;	/* value to give to the variable */
    467     GNode          *ctxt;	/* context in which to set it */
    468 {
    469     register Var   *v;
    470 
    471     /*
    472      * We only look for a variable in the given context since anything set
    473      * here will override anything in a lower context, so there's not much
    474      * point in searching them all just to save a bit of memory...
    475      */
    476     v = VarFind (name, ctxt, 0);
    477     if (v == (Var *) NIL) {
    478 	VarAdd (name, val, ctxt);
    479     } else {
    480 	Buf_Discard(v->val, Buf_Size(v->val));
    481 	Buf_AddBytes(v->val, strlen(val), (Byte *)val);
    482 
    483 	if (DEBUG(VAR)) {
    484 	    printf("%s:%s = %s\n", ctxt->name, name, val);
    485 	}
    486     }
    487     /*
    488      * Any variables given on the command line are automatically exported
    489      * to the environment (as per POSIX standard)
    490      */
    491     if (ctxt == VAR_CMD) {
    492 	setenv(name, val, 1);
    493     }
    494 }
    495 
    496 /*-
    497  *-----------------------------------------------------------------------
    498  * Var_Append --
    499  *	The variable of the given name has the given value appended to it in
    500  *	the given context.
    501  *
    502  * Results:
    503  *	None
    504  *
    505  * Side Effects:
    506  *	If the variable doesn't exist, it is created. Else the strings
    507  *	are concatenated (with a space in between).
    508  *
    509  * Notes:
    510  *	Only if the variable is being sought in the global context is the
    511  *	environment searched.
    512  *	XXX: Knows its calling circumstances in that if called with ctxt
    513  *	an actual target, it will only search that context since only
    514  *	a local variable could be being appended to. This is actually
    515  *	a big win and must be tolerated.
    516  *-----------------------------------------------------------------------
    517  */
    518 void
    519 Var_Append (name, val, ctxt)
    520     char           *name;	/* Name of variable to modify */
    521     char           *val;	/* String to append to it */
    522     GNode          *ctxt;	/* Context in which this should occur */
    523 {
    524     register Var   *v;
    525 
    526     v = VarFind (name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
    527 
    528     if (v == (Var *) NIL) {
    529 	VarAdd (name, val, ctxt);
    530     } else {
    531 	Buf_AddByte(v->val, (Byte)' ');
    532 	Buf_AddBytes(v->val, strlen(val), (Byte *)val);
    533 
    534 	if (DEBUG(VAR)) {
    535 	    printf("%s:%s = %s\n", ctxt->name, name,
    536 		   (char *) Buf_GetAll(v->val, (int *)NULL));
    537 	}
    538 
    539 	if (v->flags & VAR_FROM_ENV) {
    540 	    /*
    541 	     * If the original variable came from the environment, we
    542 	     * have to install it in the global context (we could place
    543 	     * it in the environment, but then we should provide a way to
    544 	     * export other variables...)
    545 	     */
    546 	    v->flags &= ~VAR_FROM_ENV;
    547 	    Lst_AtFront(ctxt->context, (ClientData)v);
    548 	}
    549     }
    550 }
    551 
    552 /*-
    553  *-----------------------------------------------------------------------
    554  * Var_Exists --
    555  *	See if the given variable exists.
    556  *
    557  * Results:
    558  *	TRUE if it does, FALSE if it doesn't
    559  *
    560  * Side Effects:
    561  *	None.
    562  *
    563  *-----------------------------------------------------------------------
    564  */
    565 Boolean
    566 Var_Exists(name, ctxt)
    567     char	  *name;    	/* Variable to find */
    568     GNode	  *ctxt;    	/* Context in which to start search */
    569 {
    570     Var	    	  *v;
    571 
    572     v = VarFind(name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
    573 
    574     if (v == (Var *)NIL) {
    575 	return(FALSE);
    576     } else if (v->flags & VAR_FROM_ENV) {
    577 	free(v->name);
    578 	Buf_Destroy(v->val, TRUE);
    579 	free((char *)v);
    580     }
    581     return(TRUE);
    582 }
    583 
    584 /*-
    585  *-----------------------------------------------------------------------
    586  * Var_Value --
    587  *	Return the value of the named variable in the given context
    588  *
    589  * Results:
    590  *	The value if the variable exists, NULL if it doesn't
    591  *
    592  * Side Effects:
    593  *	None
    594  *-----------------------------------------------------------------------
    595  */
    596 char *
    597 Var_Value (name, ctxt, frp)
    598     char           *name;	/* name to find */
    599     GNode          *ctxt;	/* context in which to search for it */
    600     char	   **frp;
    601 {
    602     Var            *v;
    603 
    604     v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
    605     *frp = NULL;
    606     if (v != (Var *) NIL) {
    607 	char *p = ((char *)Buf_GetAll(v->val, (int *)NULL));
    608 	if (v->flags & VAR_FROM_ENV) {
    609 	    Buf_Destroy(v->val, FALSE);
    610 	    free((Address) v);
    611 	    *frp = p;
    612 	}
    613 	return p;
    614     } else {
    615 	return ((char *) NULL);
    616     }
    617 }
    618 
    619 /*-
    620  *-----------------------------------------------------------------------
    621  * VarHead --
    622  *	Remove the tail of the given word and place the result in the given
    623  *	buffer.
    624  *
    625  * Results:
    626  *	TRUE if characters were added to the buffer (a space needs to be
    627  *	added to the buffer before the next word).
    628  *
    629  * Side Effects:
    630  *	The trimmed word is added to the buffer.
    631  *
    632  *-----------------------------------------------------------------------
    633  */
    634 static Boolean
    635 VarHead (word, addSpace, buf, dummy)
    636     char    	  *word;    	/* Word to trim */
    637     Boolean 	  addSpace; 	/* True if need to add a space to the buffer
    638 				 * before sticking in the head */
    639     Buffer  	  buf;	    	/* Buffer in which to store it */
    640     ClientData	  dummy;
    641 {
    642     register char *slash;
    643 
    644     slash = strrchr (word, '/');
    645     if (slash != (char *)NULL) {
    646 	if (addSpace) {
    647 	    Buf_AddByte (buf, (Byte)' ');
    648 	}
    649 	*slash = '\0';
    650 	Buf_AddBytes (buf, strlen (word), (Byte *)word);
    651 	*slash = '/';
    652 	return (TRUE);
    653     } else {
    654 	/*
    655 	 * If no directory part, give . (q.v. the POSIX standard)
    656 	 */
    657 	if (addSpace) {
    658 	    Buf_AddBytes(buf, 2, (Byte *)" .");
    659 	} else {
    660 	    Buf_AddByte(buf, (Byte)'.');
    661 	}
    662     }
    663     return(dummy ? TRUE : TRUE);
    664 }
    665 
    666 /*-
    667  *-----------------------------------------------------------------------
    668  * VarTail --
    669  *	Remove the head of the given word and place the result in the given
    670  *	buffer.
    671  *
    672  * Results:
    673  *	TRUE if characters were added to the buffer (a space needs to be
    674  *	added to the buffer before the next word).
    675  *
    676  * Side Effects:
    677  *	The trimmed word is added to the buffer.
    678  *
    679  *-----------------------------------------------------------------------
    680  */
    681 static Boolean
    682 VarTail (word, addSpace, buf, dummy)
    683     char    	  *word;    	/* Word to trim */
    684     Boolean 	  addSpace; 	/* TRUE if need to stick a space in the
    685 				 * buffer before adding the tail */
    686     Buffer  	  buf;	    	/* Buffer in which to store it */
    687     ClientData	  dummy;
    688 {
    689     register char *slash;
    690 
    691     if (addSpace) {
    692 	Buf_AddByte (buf, (Byte)' ');
    693     }
    694 
    695     slash = strrchr (word, '/');
    696     if (slash != (char *)NULL) {
    697 	*slash++ = '\0';
    698 	Buf_AddBytes (buf, strlen(slash), (Byte *)slash);
    699 	slash[-1] = '/';
    700     } else {
    701 	Buf_AddBytes (buf, strlen(word), (Byte *)word);
    702     }
    703     return (dummy ? TRUE : TRUE);
    704 }
    705 
    706 /*-
    707  *-----------------------------------------------------------------------
    708  * VarSuffix --
    709  *	Place the suffix of the given word in the given buffer.
    710  *
    711  * Results:
    712  *	TRUE if characters were added to the buffer (a space needs to be
    713  *	added to the buffer before the next word).
    714  *
    715  * Side Effects:
    716  *	The suffix from the word is placed in the buffer.
    717  *
    718  *-----------------------------------------------------------------------
    719  */
    720 static Boolean
    721 VarSuffix (word, addSpace, buf, dummy)
    722     char    	  *word;    	/* Word to trim */
    723     Boolean 	  addSpace; 	/* TRUE if need to add a space before placing
    724 				 * the suffix in the buffer */
    725     Buffer  	  buf;	    	/* Buffer in which to store it */
    726     ClientData	  dummy;
    727 {
    728     register char *dot;
    729 
    730     dot = strrchr (word, '.');
    731     if (dot != (char *)NULL) {
    732 	if (addSpace) {
    733 	    Buf_AddByte (buf, (Byte)' ');
    734 	}
    735 	*dot++ = '\0';
    736 	Buf_AddBytes (buf, strlen (dot), (Byte *)dot);
    737 	dot[-1] = '.';
    738 	addSpace = TRUE;
    739     }
    740     return (dummy ? addSpace : addSpace);
    741 }
    742 
    743 /*-
    744  *-----------------------------------------------------------------------
    745  * VarRoot --
    746  *	Remove the suffix of the given word and place the result in the
    747  *	buffer.
    748  *
    749  * Results:
    750  *	TRUE if characters were added to the buffer (a space needs to be
    751  *	added to the buffer before the next word).
    752  *
    753  * Side Effects:
    754  *	The trimmed word is added to the buffer.
    755  *
    756  *-----------------------------------------------------------------------
    757  */
    758 static Boolean
    759 VarRoot (word, addSpace, buf, dummy)
    760     char    	  *word;    	/* Word to trim */
    761     Boolean 	  addSpace; 	/* TRUE if need to add a space to the buffer
    762 				 * before placing the root in it */
    763     Buffer  	  buf;	    	/* Buffer in which to store it */
    764     ClientData	  dummy;
    765 {
    766     register char *dot;
    767 
    768     if (addSpace) {
    769 	Buf_AddByte (buf, (Byte)' ');
    770     }
    771 
    772     dot = strrchr (word, '.');
    773     if (dot != (char *)NULL) {
    774 	*dot = '\0';
    775 	Buf_AddBytes (buf, strlen (word), (Byte *)word);
    776 	*dot = '.';
    777     } else {
    778 	Buf_AddBytes (buf, strlen(word), (Byte *)word);
    779     }
    780     return (dummy ? TRUE : TRUE);
    781 }
    782 
    783 /*-
    784  *-----------------------------------------------------------------------
    785  * VarMatch --
    786  *	Place the word in the buffer if it matches the given pattern.
    787  *	Callback function for VarModify to implement the :M modifier.
    788  *
    789  * Results:
    790  *	TRUE if a space should be placed in the buffer before the next
    791  *	word.
    792  *
    793  * Side Effects:
    794  *	The word may be copied to the buffer.
    795  *
    796  *-----------------------------------------------------------------------
    797  */
    798 static Boolean
    799 VarMatch (word, addSpace, buf, pattern)
    800     char    	  *word;    	/* Word to examine */
    801     Boolean 	  addSpace; 	/* TRUE if need to add a space to the
    802 				 * buffer before adding the word, if it
    803 				 * matches */
    804     Buffer  	  buf;	    	/* Buffer in which to store it */
    805     ClientData    pattern; 	/* Pattern the word must match */
    806 {
    807     if (Str_Match(word, (char *) pattern)) {
    808 	if (addSpace) {
    809 	    Buf_AddByte(buf, (Byte)' ');
    810 	}
    811 	addSpace = TRUE;
    812 	Buf_AddBytes(buf, strlen(word), (Byte *)word);
    813     }
    814     return(addSpace);
    815 }
    816 
    817 #ifdef SYSVVARSUB
    818 /*-
    819  *-----------------------------------------------------------------------
    820  * VarSYSVMatch --
    821  *	Place the word in the buffer if it matches the given pattern.
    822  *	Callback function for VarModify to implement the System V %
    823  *	modifiers.
    824  *
    825  * Results:
    826  *	TRUE if a space should be placed in the buffer before the next
    827  *	word.
    828  *
    829  * Side Effects:
    830  *	The word may be copied to the buffer.
    831  *
    832  *-----------------------------------------------------------------------
    833  */
    834 static Boolean
    835 VarSYSVMatch (word, addSpace, buf, patp)
    836     char    	  *word;    	/* Word to examine */
    837     Boolean 	  addSpace; 	/* TRUE if need to add a space to the
    838 				 * buffer before adding the word, if it
    839 				 * matches */
    840     Buffer  	  buf;	    	/* Buffer in which to store it */
    841     ClientData 	  patp; 	/* Pattern the word must match */
    842 {
    843     int len;
    844     char *ptr;
    845     VarPattern 	  *pat = (VarPattern *) patp;
    846 
    847     if (addSpace)
    848 	Buf_AddByte(buf, (Byte)' ');
    849 
    850     addSpace = TRUE;
    851 
    852     if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL)
    853 	Str_SYSVSubst(buf, pat->rhs, ptr, len);
    854     else
    855 	Buf_AddBytes(buf, strlen(word), (Byte *) word);
    856 
    857     return(addSpace);
    858 }
    859 #endif
    860 
    861 
    862 /*-
    863  *-----------------------------------------------------------------------
    864  * VarNoMatch --
    865  *	Place the word in the buffer if it doesn't match the given pattern.
    866  *	Callback function for VarModify to implement the :N modifier.
    867  *
    868  * Results:
    869  *	TRUE if a space should be placed in the buffer before the next
    870  *	word.
    871  *
    872  * Side Effects:
    873  *	The word may be copied to the buffer.
    874  *
    875  *-----------------------------------------------------------------------
    876  */
    877 static Boolean
    878 VarNoMatch (word, addSpace, buf, pattern)
    879     char    	  *word;    	/* Word to examine */
    880     Boolean 	  addSpace; 	/* TRUE if need to add a space to the
    881 				 * buffer before adding the word, if it
    882 				 * matches */
    883     Buffer  	  buf;	    	/* Buffer in which to store it */
    884     ClientData    pattern; 	/* Pattern the word must match */
    885 {
    886     if (!Str_Match(word, (char *) pattern)) {
    887 	if (addSpace) {
    888 	    Buf_AddByte(buf, (Byte)' ');
    889 	}
    890 	addSpace = TRUE;
    891 	Buf_AddBytes(buf, strlen(word), (Byte *)word);
    892     }
    893     return(addSpace);
    894 }
    895 
    896 
    897 /*-
    898  *-----------------------------------------------------------------------
    899  * VarSubstitute --
    900  *	Perform a string-substitution on the given word, placing the
    901  *	result in the passed buffer.
    902  *
    903  * Results:
    904  *	TRUE if a space is needed before more characters are added.
    905  *
    906  * Side Effects:
    907  *	None.
    908  *
    909  *-----------------------------------------------------------------------
    910  */
    911 static Boolean
    912 VarSubstitute (word, addSpace, buf, patternp)
    913     char    	  	*word;	    /* Word to modify */
    914     Boolean 	  	addSpace;   /* True if space should be added before
    915 				     * other characters */
    916     Buffer  	  	buf;	    /* Buffer for result */
    917     ClientData	        patternp;   /* Pattern for substitution */
    918 {
    919     register int  	wordLen;    /* Length of word */
    920     register char 	*cp;	    /* General pointer */
    921     VarPattern	*pattern = (VarPattern *) patternp;
    922 
    923     wordLen = strlen(word);
    924     if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
    925 	(VAR_SUB_ONE|VAR_SUB_MATCHED)) {
    926 	/*
    927 	 * Still substituting -- break it down into simple anchored cases
    928 	 * and if none of them fits, perform the general substitution case.
    929 	 */
    930 	if ((pattern->flags & VAR_MATCH_START) &&
    931 	    (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
    932 		/*
    933 		 * Anchored at start and beginning of word matches pattern
    934 		 */
    935 		if ((pattern->flags & VAR_MATCH_END) &&
    936 		    (wordLen == pattern->leftLen)) {
    937 			/*
    938 			 * Also anchored at end and matches to the end (word
    939 			 * is same length as pattern) add space and rhs only
    940 			 * if rhs is non-null.
    941 			 */
    942 			if (pattern->rightLen != 0) {
    943 			    if (addSpace) {
    944 				Buf_AddByte(buf, (Byte)' ');
    945 			    }
    946 			    addSpace = TRUE;
    947 			    Buf_AddBytes(buf, pattern->rightLen,
    948 					 (Byte *)pattern->rhs);
    949 			}
    950 			pattern->flags |= VAR_SUB_MATCHED;
    951 		} else if (pattern->flags & VAR_MATCH_END) {
    952 		    /*
    953 		     * Doesn't match to end -- copy word wholesale
    954 		     */
    955 		    goto nosub;
    956 		} else {
    957 		    /*
    958 		     * Matches at start but need to copy in trailing characters
    959 		     */
    960 		    if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
    961 			if (addSpace) {
    962 			    Buf_AddByte(buf, (Byte)' ');
    963 			}
    964 			addSpace = TRUE;
    965 		    }
    966 		    Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
    967 		    Buf_AddBytes(buf, wordLen - pattern->leftLen,
    968 				 (Byte *)(word + pattern->leftLen));
    969 		    pattern->flags |= VAR_SUB_MATCHED;
    970 		}
    971 	} else if (pattern->flags & VAR_MATCH_START) {
    972 	    /*
    973 	     * Had to match at start of word and didn't -- copy whole word.
    974 	     */
    975 	    goto nosub;
    976 	} else if (pattern->flags & VAR_MATCH_END) {
    977 	    /*
    978 	     * Anchored at end, Find only place match could occur (leftLen
    979 	     * characters from the end of the word) and see if it does. Note
    980 	     * that because the $ will be left at the end of the lhs, we have
    981 	     * to use strncmp.
    982 	     */
    983 	    cp = word + (wordLen - pattern->leftLen);
    984 	    if ((cp >= word) &&
    985 		(strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
    986 		/*
    987 		 * Match found. If we will place characters in the buffer,
    988 		 * add a space before hand as indicated by addSpace, then
    989 		 * stuff in the initial, unmatched part of the word followed
    990 		 * by the right-hand-side.
    991 		 */
    992 		if (((cp - word) + pattern->rightLen) != 0) {
    993 		    if (addSpace) {
    994 			Buf_AddByte(buf, (Byte)' ');
    995 		    }
    996 		    addSpace = TRUE;
    997 		}
    998 		Buf_AddBytes(buf, cp - word, (Byte *)word);
    999 		Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
   1000 		pattern->flags |= VAR_SUB_MATCHED;
   1001 	    } else {
   1002 		/*
   1003 		 * Had to match at end and didn't. Copy entire word.
   1004 		 */
   1005 		goto nosub;
   1006 	    }
   1007 	} else {
   1008 	    /*
   1009 	     * Pattern is unanchored: search for the pattern in the word using
   1010 	     * String_FindSubstring, copying unmatched portions and the
   1011 	     * right-hand-side for each match found, handling non-global
   1012 	     * substitutions correctly, etc. When the loop is done, any
   1013 	     * remaining part of the word (word and wordLen are adjusted
   1014 	     * accordingly through the loop) is copied straight into the
   1015 	     * buffer.
   1016 	     * addSpace is set FALSE as soon as a space is added to the
   1017 	     * buffer.
   1018 	     */
   1019 	    register Boolean done;
   1020 	    int origSize;
   1021 
   1022 	    done = FALSE;
   1023 	    origSize = Buf_Size(buf);
   1024 	    while (!done) {
   1025 		cp = Str_FindSubstring(word, pattern->lhs);
   1026 		if (cp != (char *)NULL) {
   1027 		    if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
   1028 			Buf_AddByte(buf, (Byte)' ');
   1029 			addSpace = FALSE;
   1030 		    }
   1031 		    Buf_AddBytes(buf, cp-word, (Byte *)word);
   1032 		    Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
   1033 		    wordLen -= (cp - word) + pattern->leftLen;
   1034 		    word = cp + pattern->leftLen;
   1035 		    if (wordLen == 0) {
   1036 			done = TRUE;
   1037 		    }
   1038 		    if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
   1039 			done = TRUE;
   1040 		    }
   1041 		    pattern->flags |= VAR_SUB_MATCHED;
   1042 		} else {
   1043 		    done = TRUE;
   1044 		}
   1045 	    }
   1046 	    if (wordLen != 0) {
   1047 		if (addSpace) {
   1048 		    Buf_AddByte(buf, (Byte)' ');
   1049 		}
   1050 		Buf_AddBytes(buf, wordLen, (Byte *)word);
   1051 	    }
   1052 	    /*
   1053 	     * If added characters to the buffer, need to add a space
   1054 	     * before we add any more. If we didn't add any, just return
   1055 	     * the previous value of addSpace.
   1056 	     */
   1057 	    return ((Buf_Size(buf) != origSize) || addSpace);
   1058 	}
   1059 	return (addSpace);
   1060     }
   1061  nosub:
   1062     if (addSpace) {
   1063 	Buf_AddByte(buf, (Byte)' ');
   1064     }
   1065     Buf_AddBytes(buf, wordLen, (Byte *)word);
   1066     return(TRUE);
   1067 }
   1068 
   1069 #ifndef MAKE_BOOTSTRAP
   1070 /*-
   1071  *-----------------------------------------------------------------------
   1072  * VarREError --
   1073  *	Print the error caused by a regcomp or regexec call.
   1074  *
   1075  * Results:
   1076  *	None.
   1077  *
   1078  * Side Effects:
   1079  *	An error gets printed.
   1080  *
   1081  *-----------------------------------------------------------------------
   1082  */
   1083 static void
   1084 VarREError(err, pat, str)
   1085     int err;
   1086     regex_t *pat;
   1087     const char *str;
   1088 {
   1089     char *errbuf;
   1090     int errlen;
   1091 
   1092     errlen = regerror(err, pat, 0, 0);
   1093     errbuf = emalloc(errlen);
   1094     regerror(err, pat, errbuf, errlen);
   1095     Error("%s: %s", str, errbuf);
   1096     free(errbuf);
   1097 }
   1098 
   1099 
   1100 /*-
   1101  *-----------------------------------------------------------------------
   1102  * VarRESubstitute --
   1103  *	Perform a regex substitution on the given word, placing the
   1104  *	result in the passed buffer.
   1105  *
   1106  * Results:
   1107  *	TRUE if a space is needed before more characters are added.
   1108  *
   1109  * Side Effects:
   1110  *	None.
   1111  *
   1112  *-----------------------------------------------------------------------
   1113  */
   1114 static Boolean
   1115 VarRESubstitute(word, addSpace, buf, patternp)
   1116     char *word;
   1117     Boolean addSpace;
   1118     Buffer buf;
   1119     ClientData patternp;
   1120 {
   1121     VarREPattern *pat;
   1122     int xrv;
   1123     char *wp;
   1124     char *rp;
   1125     int added;
   1126     int flags = 0;
   1127 
   1128 #define MAYBE_ADD_SPACE()		\
   1129 	if (addSpace && !added)		\
   1130 	    Buf_AddByte(buf, ' ');	\
   1131 	added = 1
   1132 
   1133     added = 0;
   1134     wp = word;
   1135     pat = patternp;
   1136 
   1137     if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
   1138 	(VAR_SUB_ONE|VAR_SUB_MATCHED))
   1139 	xrv = REG_NOMATCH;
   1140     else {
   1141     tryagain:
   1142 	xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
   1143     }
   1144 
   1145     switch (xrv) {
   1146     case 0:
   1147 	pat->flags |= VAR_SUB_MATCHED;
   1148 	if (pat->matches[0].rm_so > 0) {
   1149 	    MAYBE_ADD_SPACE();
   1150 	    Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
   1151 	}
   1152 
   1153 	for (rp = pat->replace; *rp; rp++) {
   1154 	    if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
   1155 		MAYBE_ADD_SPACE();
   1156 		Buf_AddByte(buf,rp[1]);
   1157 		rp++;
   1158 	    }
   1159 	    else if ((*rp == '&') || ((*rp == '\\') && isdigit(rp[1]))) {
   1160 		int n;
   1161 		char *subbuf;
   1162 		int sublen;
   1163 		char errstr[3];
   1164 
   1165 		if (*rp == '&') {
   1166 		    n = 0;
   1167 		    errstr[0] = '&';
   1168 		    errstr[1] = '\0';
   1169 		} else {
   1170 		    n = rp[1] - '0';
   1171 		    errstr[0] = '\\';
   1172 		    errstr[1] = rp[1];
   1173 		    errstr[2] = '\0';
   1174 		    rp++;
   1175 		}
   1176 
   1177 		if (n > pat->nsub) {
   1178 		    Error("No subexpression %s", &errstr[0]);
   1179 		    subbuf = "";
   1180 		    sublen = 0;
   1181 		} else if ((pat->matches[n].rm_so == -1) &&
   1182 			   (pat->matches[n].rm_eo == -1)) {
   1183 		    Error("No match for subexpression %s", &errstr[0]);
   1184 		    subbuf = "";
   1185 		    sublen = 0;
   1186 	        } else {
   1187 		    subbuf = wp + pat->matches[n].rm_so;
   1188 		    sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
   1189 		}
   1190 
   1191 		if (sublen > 0) {
   1192 		    MAYBE_ADD_SPACE();
   1193 		    Buf_AddBytes(buf, sublen, subbuf);
   1194 		}
   1195 	    } else {
   1196 		MAYBE_ADD_SPACE();
   1197 		Buf_AddByte(buf, *rp);
   1198 	    }
   1199 	}
   1200 	wp += pat->matches[0].rm_eo;
   1201 	if (pat->flags & VAR_SUB_GLOBAL) {
   1202 	    flags |= REG_NOTBOL;
   1203 	    if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
   1204 		MAYBE_ADD_SPACE();
   1205 		Buf_AddByte(buf, *wp);
   1206 		wp++;
   1207 
   1208 	    }
   1209 	    if (*wp)
   1210 		goto tryagain;
   1211 	}
   1212 	if (*wp) {
   1213 	    MAYBE_ADD_SPACE();
   1214 	    Buf_AddBytes(buf, strlen(wp), wp);
   1215 	}
   1216 	break;
   1217     default:
   1218 	VarREError(xrv, &pat->re, "Unexpected regex error");
   1219        /* fall through */
   1220     case REG_NOMATCH:
   1221 	if (*wp) {
   1222 	    MAYBE_ADD_SPACE();
   1223 	    Buf_AddBytes(buf,strlen(wp),wp);
   1224 	}
   1225 	break;
   1226     }
   1227     return(addSpace||added);
   1228 }
   1229 #endif
   1230 
   1231 
   1232 /*-
   1233  *-----------------------------------------------------------------------
   1234  * VarModify --
   1235  *	Modify each of the words of the passed string using the given
   1236  *	function. Used to implement all modifiers.
   1237  *
   1238  * Results:
   1239  *	A string of all the words modified appropriately.
   1240  *
   1241  * Side Effects:
   1242  *	None.
   1243  *
   1244  *-----------------------------------------------------------------------
   1245  */
   1246 static char *
   1247 VarModify (str, modProc, datum)
   1248     char    	  *str;	    	    /* String whose words should be trimmed */
   1249 				    /* Function to use to modify them */
   1250     Boolean    	  (*modProc) __P((char *, Boolean, Buffer, ClientData));
   1251     ClientData	  datum;    	    /* Datum to pass it */
   1252 {
   1253     Buffer  	  buf;	    	    /* Buffer for the new string */
   1254     Boolean 	  addSpace; 	    /* TRUE if need to add a space to the
   1255 				     * buffer before adding the trimmed
   1256 				     * word */
   1257     char **av;			    /* word list [first word does not count] */
   1258     char *as;			    /* word list memory */
   1259     int ac, i;
   1260 
   1261     buf = Buf_Init (0);
   1262     addSpace = FALSE;
   1263 
   1264     av = brk_string(str, &ac, FALSE, &as);
   1265 
   1266     for (i = 0; i < ac; i++)
   1267 	addSpace = (*modProc)(av[i], addSpace, buf, datum);
   1268 
   1269     free(as);
   1270     free(av);
   1271 
   1272     Buf_AddByte (buf, '\0');
   1273     str = (char *)Buf_GetAll (buf, (int *)NULL);
   1274     Buf_Destroy (buf, FALSE);
   1275     return (str);
   1276 }
   1277 
   1278 /*-
   1279  *-----------------------------------------------------------------------
   1280  * VarGetPattern --
   1281  *	Pass through the tstr looking for 1) escaped delimiters,
   1282  *	'$'s and backslashes (place the escaped character in
   1283  *	uninterpreted) and 2) unescaped $'s that aren't before
   1284  *	the delimiter (expand the variable substitution).
   1285  *	Return the expanded string or NULL if the delimiter was missing
   1286  *	If pattern is specified, handle escaped ampersands, and replace
   1287  *	unescaped ampersands with the lhs of the pattern.
   1288  *
   1289  * Results:
   1290  *	A string of all the words modified appropriately.
   1291  *	If length is specified, return the string length of the buffer
   1292  *	If flags is specified and the last character of the pattern is a
   1293  *	$ set the VAR_MATCH_END bit of flags.
   1294  *
   1295  * Side Effects:
   1296  *	None.
   1297  *-----------------------------------------------------------------------
   1298  */
   1299 static char *
   1300 VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern)
   1301     GNode *ctxt;
   1302     int err;
   1303     char **tstr;
   1304     int delim;
   1305     int *flags;
   1306     int *length;
   1307     VarPattern *pattern;
   1308 {
   1309     char *cp;
   1310     Buffer buf = Buf_Init(0);
   1311     int junk;
   1312     if (length == NULL)
   1313 	length = &junk;
   1314 
   1315 #define IS_A_MATCH(cp, delim) \
   1316     ((cp[0] == '\\') && ((cp[1] == delim) ||  \
   1317      (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
   1318 
   1319     /*
   1320      * Skim through until the matching delimiter is found;
   1321      * pick up variable substitutions on the way. Also allow
   1322      * backslashes to quote the delimiter, $, and \, but don't
   1323      * touch other backslashes.
   1324      */
   1325     for (cp = *tstr; *cp && (*cp != delim); cp++) {
   1326 	if (IS_A_MATCH(cp, delim)) {
   1327 	    Buf_AddByte(buf, (Byte) cp[1]);
   1328 	    cp++;
   1329 	} else if (*cp == '$') {
   1330 	    if (cp[1] == delim) {
   1331 		if (flags == NULL)
   1332 		    Buf_AddByte(buf, (Byte) *cp);
   1333 		else
   1334 		    /*
   1335 		     * Unescaped $ at end of pattern => anchor
   1336 		     * pattern at end.
   1337 		     */
   1338 		    *flags |= VAR_MATCH_END;
   1339 	    }
   1340 	    else {
   1341 		char   *cp2;
   1342 		int     len;
   1343 		Boolean freeIt;
   1344 
   1345 		/*
   1346 		 * If unescaped dollar sign not before the
   1347 		 * delimiter, assume it's a variable
   1348 		 * substitution and recurse.
   1349 		 */
   1350 		cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
   1351 		Buf_AddBytes(buf, strlen(cp2), (Byte *) cp2);
   1352 		if (freeIt)
   1353 		    free(cp2);
   1354 		cp += len - 1;
   1355 	    }
   1356 	}
   1357 	else if (pattern && *cp == '&')
   1358 	    Buf_AddBytes(buf, pattern->leftLen, (Byte *)pattern->lhs);
   1359 	else
   1360 	    Buf_AddByte(buf, (Byte) *cp);
   1361     }
   1362 
   1363     Buf_AddByte(buf, (Byte) '\0');
   1364 
   1365     if (*cp != delim) {
   1366 	*tstr = cp;
   1367 	*length = 0;
   1368 	return NULL;
   1369     }
   1370     else {
   1371 	*tstr = ++cp;
   1372 	cp = (char *) Buf_GetAll(buf, length);
   1373 	*length -= 1;	/* Don't count the NULL */
   1374 	Buf_Destroy(buf, FALSE);
   1375 	return cp;
   1376     }
   1377 }
   1378 
   1379 /*-
   1380  *-----------------------------------------------------------------------
   1381  * VarQuote --
   1382  *	Quote shell meta-characters in the string
   1383  *
   1384  * Results:
   1385  *	The quoted string
   1386  *
   1387  * Side Effects:
   1388  *	None.
   1389  *
   1390  *-----------------------------------------------------------------------
   1391  */
   1392 static char *
   1393 VarQuote(str)
   1394 	char *str;
   1395 {
   1396 
   1397     Buffer  	  buf;
   1398     /* This should cover most shells :-( */
   1399     static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
   1400 
   1401     buf = Buf_Init (MAKE_BSIZE);
   1402     for (; *str; str++) {
   1403 	if (strchr(meta, *str) != NULL)
   1404 	    Buf_AddByte(buf, (Byte)'\\');
   1405 	Buf_AddByte(buf, (Byte)*str);
   1406     }
   1407     Buf_AddByte(buf, (Byte) '\0');
   1408     str = (char *)Buf_GetAll (buf, (int *)NULL);
   1409     Buf_Destroy (buf, FALSE);
   1410     return str;
   1411 }
   1412 
   1413 
   1414 /*-
   1415  *-----------------------------------------------------------------------
   1416  * Var_Parse --
   1417  *	Given the start of a variable invocation, extract the variable
   1418  *	name and find its value, then modify it according to the
   1419  *	specification.
   1420  *
   1421  * Results:
   1422  *	The (possibly-modified) value of the variable or var_Error if the
   1423  *	specification is invalid. The length of the specification is
   1424  *	placed in *lengthPtr (for invalid specifications, this is just
   1425  *	2...?).
   1426  *	A Boolean in *freePtr telling whether the returned string should
   1427  *	be freed by the caller.
   1428  *
   1429  * Side Effects:
   1430  *	None.
   1431  *
   1432  *-----------------------------------------------------------------------
   1433  */
   1434 char *
   1435 Var_Parse (str, ctxt, err, lengthPtr, freePtr)
   1436     char    	  *str;	    	/* The string to parse */
   1437     GNode   	  *ctxt;    	/* The context for the variable */
   1438     Boolean 	    err;    	/* TRUE if undefined variables are an error */
   1439     int	    	    *lengthPtr;	/* OUT: The length of the specification */
   1440     Boolean 	    *freePtr; 	/* OUT: TRUE if caller should free result */
   1441 {
   1442     register char   *tstr;    	/* Pointer into str */
   1443     Var	    	    *v;	    	/* Variable in invocation */
   1444     char   	    *cp;    	/* Secondary pointer into str (place marker
   1445 				 * for tstr) */
   1446     Boolean 	    haveModifier;/* TRUE if have modifiers for the variable */
   1447     register char   endc;    	/* Ending character when variable in parens
   1448 				 * or braces */
   1449     register char   startc=0;	/* Starting character when variable in parens
   1450 				 * or braces */
   1451     int             cnt;	/* Used to count brace pairs when variable in
   1452 				 * in parens or braces */
   1453     int		    vlen;	/* Length of variable name */
   1454     char    	    *start;
   1455     char	     delim;
   1456     Boolean 	    dynamic;	/* TRUE if the variable is local and we're
   1457 				 * expanding it in a non-local context. This
   1458 				 * is done to support dynamic sources. The
   1459 				 * result is just the invocation, unaltered */
   1460 
   1461     *freePtr = FALSE;
   1462     dynamic = FALSE;
   1463     start = str;
   1464 
   1465     if (str[1] != '(' && str[1] != '{') {
   1466 	/*
   1467 	 * If it's not bounded by braces of some sort, life is much simpler.
   1468 	 * We just need to check for the first character and return the
   1469 	 * value if it exists.
   1470 	 */
   1471 	char	  name[2];
   1472 
   1473 	name[0] = str[1];
   1474 	name[1] = '\0';
   1475 
   1476 	v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
   1477 	if (v == (Var *)NIL) {
   1478 	    *lengthPtr = 2;
   1479 
   1480 	    if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
   1481 		/*
   1482 		 * If substituting a local variable in a non-local context,
   1483 		 * assume it's for dynamic source stuff. We have to handle
   1484 		 * this specially and return the longhand for the variable
   1485 		 * with the dollar sign escaped so it makes it back to the
   1486 		 * caller. Only four of the local variables are treated
   1487 		 * specially as they are the only four that will be set
   1488 		 * when dynamic sources are expanded.
   1489 		 */
   1490 		switch (str[1]) {
   1491 		    case '@':
   1492 			return("$(.TARGET)");
   1493 		    case '%':
   1494 			return("$(.ARCHIVE)");
   1495 		    case '*':
   1496 			return("$(.PREFIX)");
   1497 		    case '!':
   1498 			return("$(.MEMBER)");
   1499 		}
   1500 	    }
   1501 	    /*
   1502 	     * Error
   1503 	     */
   1504 	    return (err ? var_Error : varNoError);
   1505 	} else {
   1506 	    haveModifier = FALSE;
   1507 	    tstr = &str[1];
   1508 	    endc = str[1];
   1509 	}
   1510     } else {
   1511 	Buffer buf;	/* Holds the variable name */
   1512 
   1513 	startc = str[1];
   1514 	endc = startc == '(' ? ')' : '}';
   1515 	buf = Buf_Init (MAKE_BSIZE);
   1516 
   1517 	/*
   1518 	 * Skip to the end character or a colon, whichever comes first.
   1519 	 */
   1520 	for (tstr = str + 2;
   1521 	     *tstr != '\0' && *tstr != endc && *tstr != ':';
   1522 	     tstr++)
   1523 	{
   1524 	    /*
   1525 	     * A variable inside a variable, expand
   1526 	     */
   1527 	    if (*tstr == '$') {
   1528 		int rlen;
   1529 		Boolean rfree;
   1530 		char *rval = Var_Parse(tstr, ctxt, err, &rlen, &rfree);
   1531 		if (rval != NULL) {
   1532 		    Buf_AddBytes(buf, strlen(rval), (Byte *) rval);
   1533 		    if (rfree)
   1534 			free(rval);
   1535 		}
   1536 		tstr += rlen - 1;
   1537 	    }
   1538 	    else
   1539 		Buf_AddByte(buf, (Byte) *tstr);
   1540 	}
   1541 	if (*tstr == ':') {
   1542 	    haveModifier = TRUE;
   1543 	} else if (*tstr != '\0') {
   1544 	    haveModifier = FALSE;
   1545 	} else {
   1546 	    /*
   1547 	     * If we never did find the end character, return NULL
   1548 	     * right now, setting the length to be the distance to
   1549 	     * the end of the string, since that's what make does.
   1550 	     */
   1551 	    *lengthPtr = tstr - str;
   1552 	    return (var_Error);
   1553 	}
   1554 	*tstr = '\0';
   1555 	Buf_AddByte(buf, (Byte) '\0');
   1556 	str = Buf_GetAll(buf, (int *) NULL);
   1557 	vlen = strlen(str);
   1558 
   1559 	v = VarFind (str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
   1560 	if ((v == (Var *)NIL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
   1561 	    (vlen == 2) && (str[1] == 'F' || str[1] == 'D'))
   1562 	{
   1563 	    /*
   1564 	     * Check for bogus D and F forms of local variables since we're
   1565 	     * in a local context and the name is the right length.
   1566 	     */
   1567 	    switch(*str) {
   1568 		case '@':
   1569 		case '%':
   1570 		case '*':
   1571 		case '!':
   1572 		case '>':
   1573 		case '<':
   1574 		{
   1575 		    char    vname[2];
   1576 		    char    *val;
   1577 
   1578 		    /*
   1579 		     * Well, it's local -- go look for it.
   1580 		     */
   1581 		    vname[0] = *str;
   1582 		    vname[1] = '\0';
   1583 		    v = VarFind(vname, ctxt, 0);
   1584 
   1585 		    if (v != (Var *)NIL) {
   1586 			/*
   1587 			 * No need for nested expansion or anything, as we're
   1588 			 * the only one who sets these things and we sure don't
   1589 			 * but nested invocations in them...
   1590 			 */
   1591 			val = (char *)Buf_GetAll(v->val, (int *)NULL);
   1592 
   1593 			if (str[1] == 'D') {
   1594 			    val = VarModify(val, VarHead, (ClientData)0);
   1595 			} else {
   1596 			    val = VarModify(val, VarTail, (ClientData)0);
   1597 			}
   1598 			/*
   1599 			 * Resulting string is dynamically allocated, so
   1600 			 * tell caller to free it.
   1601 			 */
   1602 			*freePtr = TRUE;
   1603 			*lengthPtr = tstr-start+1;
   1604 			*tstr = endc;
   1605 			Buf_Destroy (buf, TRUE);
   1606 			return(val);
   1607 		    }
   1608 		    break;
   1609 		}
   1610 	    }
   1611 	}
   1612 
   1613 	if (v == (Var *)NIL) {
   1614 	    if (((vlen == 1) ||
   1615 		 (((vlen == 2) && (str[1] == 'F' ||
   1616 					 str[1] == 'D')))) &&
   1617 		((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
   1618 	    {
   1619 		/*
   1620 		 * If substituting a local variable in a non-local context,
   1621 		 * assume it's for dynamic source stuff. We have to handle
   1622 		 * this specially and return the longhand for the variable
   1623 		 * with the dollar sign escaped so it makes it back to the
   1624 		 * caller. Only four of the local variables are treated
   1625 		 * specially as they are the only four that will be set
   1626 		 * when dynamic sources are expanded.
   1627 		 */
   1628 		switch (*str) {
   1629 		    case '@':
   1630 		    case '%':
   1631 		    case '*':
   1632 		    case '!':
   1633 			dynamic = TRUE;
   1634 			break;
   1635 		}
   1636 	    } else if ((vlen > 2) && (*str == '.') &&
   1637 		       isupper((unsigned char) str[1]) &&
   1638 		       ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
   1639 	    {
   1640 		int	len;
   1641 
   1642 		len = vlen - 1;
   1643 		if ((strncmp(str, ".TARGET", len) == 0) ||
   1644 		    (strncmp(str, ".ARCHIVE", len) == 0) ||
   1645 		    (strncmp(str, ".PREFIX", len) == 0) ||
   1646 		    (strncmp(str, ".MEMBER", len) == 0))
   1647 		{
   1648 		    dynamic = TRUE;
   1649 		}
   1650 	    }
   1651 
   1652 	    if (!haveModifier) {
   1653 		/*
   1654 		 * No modifiers -- have specification length so we can return
   1655 		 * now.
   1656 		 */
   1657 		*lengthPtr = tstr - start + 1;
   1658 		*tstr = endc;
   1659 		if (dynamic) {
   1660 		    str = emalloc(*lengthPtr + 1);
   1661 		    strncpy(str, start, *lengthPtr);
   1662 		    str[*lengthPtr] = '\0';
   1663 		    *freePtr = TRUE;
   1664 		    Buf_Destroy (buf, TRUE);
   1665 		    return(str);
   1666 		} else {
   1667 		    Buf_Destroy (buf, TRUE);
   1668 		    return (err ? var_Error : varNoError);
   1669 		}
   1670 	    } else {
   1671 		/*
   1672 		 * Still need to get to the end of the variable specification,
   1673 		 * so kludge up a Var structure for the modifications
   1674 		 */
   1675 		v = (Var *) emalloc(sizeof(Var));
   1676 		v->name = &str[1];
   1677 		v->val = Buf_Init(1);
   1678 		v->flags = VAR_JUNK;
   1679 	    }
   1680 	}
   1681 	Buf_Destroy (buf, TRUE);
   1682     }
   1683 
   1684 
   1685     if (v->flags & VAR_IN_USE) {
   1686 	Fatal("Variable %s is recursive.", v->name);
   1687 	/*NOTREACHED*/
   1688     } else {
   1689 	v->flags |= VAR_IN_USE;
   1690     }
   1691     /*
   1692      * Before doing any modification, we have to make sure the value
   1693      * has been fully expanded. If it looks like recursion might be
   1694      * necessary (there's a dollar sign somewhere in the variable's value)
   1695      * we just call Var_Subst to do any other substitutions that are
   1696      * necessary. Note that the value returned by Var_Subst will have
   1697      * been dynamically-allocated, so it will need freeing when we
   1698      * return.
   1699      */
   1700     str = (char *)Buf_GetAll(v->val, (int *)NULL);
   1701     if (strchr (str, '$') != (char *)NULL) {
   1702 	str = Var_Subst(NULL, str, ctxt, err);
   1703 	*freePtr = TRUE;
   1704     }
   1705 
   1706     v->flags &= ~VAR_IN_USE;
   1707 
   1708     /*
   1709      * Now we need to apply any modifiers the user wants applied.
   1710      * These are:
   1711      *  	  :M<pattern>	words which match the given <pattern>.
   1712      *  	  	    	<pattern> is of the standard file
   1713      *  	  	    	wildcarding form.
   1714      *  	  :S<d><pat1><d><pat2><d>[g]
   1715      *  	  	    	Substitute <pat2> for <pat1> in the value
   1716      *  	  :C<d><pat1><d><pat2><d>[g]
   1717      *  	  	    	Substitute <pat2> for regex <pat1> in the value
   1718      *  	  :H	    	Substitute the head of each word
   1719      *  	  :T	    	Substitute the tail of each word
   1720      *  	  :E	    	Substitute the extension (minus '.') of
   1721      *  	  	    	each word
   1722      *  	  :R	    	Substitute the root of each word
   1723      *  	  	    	(pathname minus the suffix).
   1724      *		  :?<true-value>:<false-value>
   1725      *				If the variable evaluates to true, return
   1726      *				true value, else return the second value.
   1727      *	    	  :lhs=rhs  	Like :S, but the rhs goes to the end of
   1728      *	    	    	    	the invocation.
   1729      */
   1730     if ((str != (char *)NULL) && haveModifier) {
   1731 	/*
   1732 	 * Skip initial colon while putting it back.
   1733 	 */
   1734 	*tstr++ = ':';
   1735 	while (*tstr != endc) {
   1736 	    char	*newStr;    /* New value to return */
   1737 	    char	termc;	    /* Character which terminated scan */
   1738 
   1739 	    if (DEBUG(VAR)) {
   1740 		printf("Applying :%c to \"%s\"\n", *tstr, str);
   1741 	    }
   1742 	    switch (*tstr) {
   1743 		case 'N':
   1744 		case 'M':
   1745 		{
   1746 		    char    *pattern;
   1747 		    char    *cp2;
   1748 		    Boolean copy;
   1749 
   1750 		    copy = FALSE;
   1751 		    for (cp = tstr + 1;
   1752 			 *cp != '\0' && *cp != ':' && *cp != endc;
   1753 			 cp++)
   1754 		    {
   1755 			if (*cp == '\\' && (cp[1] == ':' || cp[1] == endc)){
   1756 			    copy = TRUE;
   1757 			    cp++;
   1758 			}
   1759 		    }
   1760 		    termc = *cp;
   1761 		    *cp = '\0';
   1762 		    if (copy) {
   1763 			/*
   1764 			 * Need to compress the \:'s out of the pattern, so
   1765 			 * allocate enough room to hold the uncompressed
   1766 			 * pattern (note that cp started at tstr+1, so
   1767 			 * cp - tstr takes the null byte into account) and
   1768 			 * compress the pattern into the space.
   1769 			 */
   1770 			pattern = emalloc(cp - tstr);
   1771 			for (cp2 = pattern, cp = tstr + 1;
   1772 			     *cp != '\0';
   1773 			     cp++, cp2++)
   1774 			{
   1775 			    if ((*cp == '\\') &&
   1776 				(cp[1] == ':' || cp[1] == endc)) {
   1777 				    cp++;
   1778 			    }
   1779 			    *cp2 = *cp;
   1780 			}
   1781 			*cp2 = '\0';
   1782 		    } else {
   1783 			pattern = &tstr[1];
   1784 		    }
   1785 		    if (*tstr == 'M' || *tstr == 'm') {
   1786 			newStr = VarModify(str, VarMatch, (ClientData)pattern);
   1787 		    } else {
   1788 			newStr = VarModify(str, VarNoMatch,
   1789 					   (ClientData)pattern);
   1790 		    }
   1791 		    if (copy) {
   1792 			free(pattern);
   1793 		    }
   1794 		    break;
   1795 		}
   1796 		case 'S':
   1797 		{
   1798 		    VarPattern 	    pattern;
   1799 
   1800 		    pattern.flags = 0;
   1801 		    delim = tstr[1];
   1802 		    tstr += 2;
   1803 
   1804 		    /*
   1805 		     * If pattern begins with '^', it is anchored to the
   1806 		     * start of the word -- skip over it and flag pattern.
   1807 		     */
   1808 		    if (*tstr == '^') {
   1809 			pattern.flags |= VAR_MATCH_START;
   1810 			tstr += 1;
   1811 		    }
   1812 
   1813 		    cp = tstr;
   1814 		    if ((pattern.lhs = VarGetPattern(ctxt, err, &cp, delim,
   1815 			&pattern.flags, &pattern.leftLen, NULL)) == NULL)
   1816 			goto cleanup;
   1817 
   1818 		    if ((pattern.rhs = VarGetPattern(ctxt, err, &cp, delim,
   1819 			NULL, &pattern.rightLen, &pattern)) == NULL)
   1820 			goto cleanup;
   1821 
   1822 		    /*
   1823 		     * Check for global substitution. If 'g' after the final
   1824 		     * delimiter, substitution is global and is marked that
   1825 		     * way.
   1826 		     */
   1827 		    for (;; cp++) {
   1828 			switch (*cp) {
   1829 			case 'g':
   1830 			    pattern.flags |= VAR_SUB_GLOBAL;
   1831 			    continue;
   1832 			case '1':
   1833 			    pattern.flags |= VAR_SUB_ONE;
   1834 			    continue;
   1835 			}
   1836 			break;
   1837 		    }
   1838 
   1839 		    termc = *cp;
   1840 		    newStr = VarModify(str, VarSubstitute,
   1841 				       (ClientData)&pattern);
   1842 
   1843 		    /*
   1844 		     * Free the two strings.
   1845 		     */
   1846 		    free(pattern.lhs);
   1847 		    free(pattern.rhs);
   1848 		    break;
   1849 		}
   1850 		case '?':
   1851 		{
   1852 		    VarPattern 	pattern;
   1853 		    Boolean	value;
   1854 
   1855 		    /* find ':', and then substitute accordingly */
   1856 
   1857 		    pattern.flags = 0;
   1858 
   1859 		    cp = ++tstr;
   1860 		    delim = ':';
   1861 		    if ((pattern.lhs = VarGetPattern(ctxt, err, &cp, delim,
   1862 			NULL, &pattern.leftLen, NULL)) == NULL)
   1863 			goto cleanup;
   1864 
   1865 		    delim = '}';
   1866 		    if ((pattern.rhs = VarGetPattern(ctxt, err, &cp, delim,
   1867 			NULL, &pattern.rightLen, NULL)) == NULL)
   1868 			goto cleanup;
   1869 
   1870 		    termc = *--cp;
   1871 		    delim = '\0';
   1872 		    if (Cond_EvalExpression(1, str, &value, 0) == COND_INVALID){
   1873 			Error("Bad conditional expression `%s' in %s?%s:%s",
   1874 			      str, str, pattern.lhs, pattern.rhs);
   1875 			goto cleanup;
   1876 		    }
   1877 
   1878 		    if (value) {
   1879 			newStr = pattern.lhs;
   1880 			free(pattern.rhs);
   1881 		    } else {
   1882 			newStr = pattern.rhs;
   1883 			free(pattern.lhs);
   1884 		    }
   1885 		    break;
   1886 		}
   1887 #ifndef MAKE_BOOTSTRAP
   1888 		case 'C':
   1889 		{
   1890 		    VarREPattern    pattern;
   1891 		    char           *re;
   1892 		    int             error;
   1893 
   1894 		    pattern.flags = 0;
   1895 		    delim = tstr[1];
   1896 		    tstr += 2;
   1897 
   1898 		    cp = tstr;
   1899 
   1900 		    if ((re = VarGetPattern(ctxt, err, &cp, delim, NULL,
   1901 			NULL, NULL)) == NULL)
   1902 			goto cleanup;
   1903 
   1904 		    if ((pattern.replace = VarGetPattern(ctxt, err, &cp,
   1905 			delim, NULL, NULL, NULL)) == NULL){
   1906 			free(re);
   1907 			goto cleanup;
   1908 		    }
   1909 
   1910 		    for (;; cp++) {
   1911 			switch (*cp) {
   1912 			case 'g':
   1913 			    pattern.flags |= VAR_SUB_GLOBAL;
   1914 			    continue;
   1915 			case '1':
   1916 			    pattern.flags |= VAR_SUB_ONE;
   1917 			    continue;
   1918 			}
   1919 			break;
   1920 		    }
   1921 
   1922 		    termc = *cp;
   1923 
   1924 		    error = regcomp(&pattern.re, re, REG_EXTENDED);
   1925 		    free(re);
   1926 		    if (error)  {
   1927 			*lengthPtr = cp - start + 1;
   1928 			VarREError(error, &pattern.re, "RE substitution error");
   1929 			free(pattern.replace);
   1930 			return (var_Error);
   1931 		    }
   1932 
   1933 		    pattern.nsub = pattern.re.re_nsub + 1;
   1934 		    if (pattern.nsub < 1)
   1935 			pattern.nsub = 1;
   1936 		    if (pattern.nsub > 10)
   1937 			pattern.nsub = 10;
   1938 		    pattern.matches = emalloc(pattern.nsub *
   1939 					      sizeof(regmatch_t));
   1940 		    newStr = VarModify(str, VarRESubstitute,
   1941 				       (ClientData) &pattern);
   1942 		    regfree(&pattern.re);
   1943 		    free(pattern.replace);
   1944 		    free(pattern.matches);
   1945 		    break;
   1946 		}
   1947 #endif
   1948 		case 'Q':
   1949 		    if (tstr[1] == endc || tstr[1] == ':') {
   1950 			newStr = VarQuote (str);
   1951 			cp = tstr + 1;
   1952 			termc = *cp;
   1953 			break;
   1954 		    }
   1955 		    /*FALLTHRU*/
   1956 		case 'T':
   1957 		    if (tstr[1] == endc || tstr[1] == ':') {
   1958 			newStr = VarModify (str, VarTail, (ClientData)0);
   1959 			cp = tstr + 1;
   1960 			termc = *cp;
   1961 			break;
   1962 		    }
   1963 		    /*FALLTHRU*/
   1964 		case 'H':
   1965 		    if (tstr[1] == endc || tstr[1] == ':') {
   1966 			newStr = VarModify (str, VarHead, (ClientData)0);
   1967 			cp = tstr + 1;
   1968 			termc = *cp;
   1969 			break;
   1970 		    }
   1971 		    /*FALLTHRU*/
   1972 		case 'E':
   1973 		    if (tstr[1] == endc || tstr[1] == ':') {
   1974 			newStr = VarModify (str, VarSuffix, (ClientData)0);
   1975 			cp = tstr + 1;
   1976 			termc = *cp;
   1977 			break;
   1978 		    }
   1979 		    /*FALLTHRU*/
   1980 		case 'R':
   1981 		    if (tstr[1] == endc || tstr[1] == ':') {
   1982 			newStr = VarModify (str, VarRoot, (ClientData)0);
   1983 			cp = tstr + 1;
   1984 			termc = *cp;
   1985 			break;
   1986 		    }
   1987 		    /*FALLTHRU*/
   1988 #ifdef SUNSHCMD
   1989 		case 's':
   1990 		    if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
   1991 			char *err;
   1992 			newStr = Cmd_Exec (str, &err);
   1993 			if (err)
   1994 			    Error (err, str);
   1995 			cp = tstr + 2;
   1996 			termc = *cp;
   1997 			break;
   1998 		    }
   1999 		    /*FALLTHRU*/
   2000 #endif
   2001 		default:
   2002 		{
   2003 #ifdef SYSVVARSUB
   2004 		    /*
   2005 		     * This can either be a bogus modifier or a System-V
   2006 		     * substitution command.
   2007 		     */
   2008 		    VarPattern      pattern;
   2009 		    Boolean         eqFound;
   2010 
   2011 		    pattern.flags = 0;
   2012 		    eqFound = FALSE;
   2013 		    /*
   2014 		     * First we make a pass through the string trying
   2015 		     * to verify it is a SYSV-make-style translation:
   2016 		     * it must be: <string1>=<string2>)
   2017 		     */
   2018 		    cp = tstr;
   2019 		    cnt = 1;
   2020 		    while (*cp != '\0' && cnt) {
   2021 			if (*cp == '=') {
   2022 			    eqFound = TRUE;
   2023 			    /* continue looking for endc */
   2024 			}
   2025 			else if (*cp == endc)
   2026 			    cnt--;
   2027 			else if (*cp == startc)
   2028 			    cnt++;
   2029 			if (cnt)
   2030 			    cp++;
   2031 		    }
   2032 		    if (*cp == endc && eqFound) {
   2033 
   2034 			/*
   2035 			 * Now we break this sucker into the lhs and
   2036 			 * rhs. We must null terminate them of course.
   2037 			 */
   2038 			for (cp = tstr; *cp != '='; cp++)
   2039 			    continue;
   2040 			pattern.lhs = tstr;
   2041 			pattern.leftLen = cp - tstr;
   2042 			*cp++ = '\0';
   2043 
   2044 			pattern.rhs = cp;
   2045 			cnt = 1;
   2046 			while (cnt) {
   2047 			    if (*cp == endc)
   2048 				cnt--;
   2049 			    else if (*cp == startc)
   2050 				cnt++;
   2051 			    if (cnt)
   2052 				cp++;
   2053 			}
   2054 			pattern.rightLen = cp - pattern.rhs;
   2055 			*cp = '\0';
   2056 
   2057 			/*
   2058 			 * SYSV modifications happen through the whole
   2059 			 * string. Note the pattern is anchored at the end.
   2060 			 */
   2061 			newStr = VarModify(str, VarSYSVMatch,
   2062 					   (ClientData)&pattern);
   2063 
   2064 			/*
   2065 			 * Restore the nulled characters
   2066 			 */
   2067 			pattern.lhs[pattern.leftLen] = '=';
   2068 			pattern.rhs[pattern.rightLen] = endc;
   2069 			termc = endc;
   2070 		    } else
   2071 #endif
   2072 		    {
   2073 			Error ("Unknown modifier '%c'\n", *tstr);
   2074 			for (cp = tstr+1;
   2075 			     *cp != ':' && *cp != endc && *cp != '\0';
   2076 			     cp++)
   2077 				 continue;
   2078 			termc = *cp;
   2079 			newStr = var_Error;
   2080 		    }
   2081 		}
   2082 	    }
   2083 	    if (DEBUG(VAR)) {
   2084 		printf("Result is \"%s\"\n", newStr);
   2085 	    }
   2086 
   2087 	    if (*freePtr) {
   2088 		free (str);
   2089 	    }
   2090 	    str = newStr;
   2091 	    if (str != var_Error) {
   2092 		*freePtr = TRUE;
   2093 	    } else {
   2094 		*freePtr = FALSE;
   2095 	    }
   2096 	    if (termc == '\0') {
   2097 		Error("Unclosed variable specification for %s", v->name);
   2098 	    } else if (termc == ':') {
   2099 		*cp++ = termc;
   2100 	    } else {
   2101 		*cp = termc;
   2102 	    }
   2103 	    tstr = cp;
   2104 	}
   2105 	*lengthPtr = tstr - start + 1;
   2106     } else {
   2107 	*lengthPtr = tstr - start + 1;
   2108 	*tstr = endc;
   2109     }
   2110 
   2111     if (v->flags & VAR_FROM_ENV) {
   2112 	Boolean	  destroy = FALSE;
   2113 
   2114 	if (str != (char *)Buf_GetAll(v->val, (int *)NULL)) {
   2115 	    destroy = TRUE;
   2116 	} else {
   2117 	    /*
   2118 	     * Returning the value unmodified, so tell the caller to free
   2119 	     * the thing.
   2120 	     */
   2121 	    *freePtr = TRUE;
   2122 	}
   2123 	Buf_Destroy(v->val, destroy);
   2124 	free((Address)v);
   2125     } else if (v->flags & VAR_JUNK) {
   2126 	/*
   2127 	 * Perform any free'ing needed and set *freePtr to FALSE so the caller
   2128 	 * doesn't try to free a static pointer.
   2129 	 */
   2130 	if (*freePtr) {
   2131 	    free(str);
   2132 	}
   2133 	*freePtr = FALSE;
   2134 	Buf_Destroy(v->val, TRUE);
   2135 	free((Address)v);
   2136 	if (dynamic) {
   2137 	    str = emalloc(*lengthPtr + 1);
   2138 	    strncpy(str, start, *lengthPtr);
   2139 	    str[*lengthPtr] = '\0';
   2140 	    *freePtr = TRUE;
   2141 	} else {
   2142 	    str = var_Error;
   2143 	}
   2144     }
   2145     return (str);
   2146 
   2147 cleanup:
   2148     *lengthPtr = cp - start + 1;
   2149     if (*freePtr)
   2150 	free(str);
   2151     if (delim != '\0')
   2152 	Error("Unclosed substitution for %s (%c missing)",
   2153 	      v->name, delim);
   2154     return (var_Error);
   2155 }
   2156 
   2157 /*-
   2158  *-----------------------------------------------------------------------
   2159  * Var_Subst  --
   2160  *	Substitute for all variables in the given string in the given context
   2161  *	If undefErr is TRUE, Parse_Error will be called when an undefined
   2162  *	variable is encountered.
   2163  *
   2164  * Results:
   2165  *	The resulting string.
   2166  *
   2167  * Side Effects:
   2168  *	None. The old string must be freed by the caller
   2169  *-----------------------------------------------------------------------
   2170  */
   2171 char *
   2172 Var_Subst (var, str, ctxt, undefErr)
   2173     char	  *var;		    /* Named variable || NULL for all */
   2174     char 	  *str;	    	    /* the string in which to substitute */
   2175     GNode         *ctxt;	    /* the context wherein to find variables */
   2176     Boolean 	  undefErr; 	    /* TRUE if undefineds are an error */
   2177 {
   2178     Buffer  	  buf;	    	    /* Buffer for forming things */
   2179     char    	  *val;		    /* Value to substitute for a variable */
   2180     int	    	  length;   	    /* Length of the variable invocation */
   2181     Boolean 	  doFree;   	    /* Set true if val should be freed */
   2182     static Boolean errorReported;   /* Set true if an error has already
   2183 				     * been reported to prevent a plethora
   2184 				     * of messages when recursing */
   2185 
   2186     buf = Buf_Init (MAKE_BSIZE);
   2187     errorReported = FALSE;
   2188 
   2189     while (*str) {
   2190 	if (var == NULL && (*str == '$') && (str[1] == '$')) {
   2191 	    /*
   2192 	     * A dollar sign may be escaped either with another dollar sign.
   2193 	     * In such a case, we skip over the escape character and store the
   2194 	     * dollar sign into the buffer directly.
   2195 	     */
   2196 	    str++;
   2197 	    Buf_AddByte(buf, (Byte)*str);
   2198 	    str++;
   2199 	} else if (*str != '$') {
   2200 	    /*
   2201 	     * Skip as many characters as possible -- either to the end of
   2202 	     * the string or to the next dollar sign (variable invocation).
   2203 	     */
   2204 	    char  *cp;
   2205 
   2206 	    for (cp = str++; *str != '$' && *str != '\0'; str++)
   2207 		continue;
   2208 	    Buf_AddBytes(buf, str - cp, (Byte *)cp);
   2209 	} else {
   2210 	    if (var != NULL) {
   2211 		int expand;
   2212 		for (;;) {
   2213 		    if (str[1] != '(' && str[1] != '{') {
   2214 			if (str[1] != *var) {
   2215 			    Buf_AddBytes(buf, 2, (Byte *) str);
   2216 			    str += 2;
   2217 			    expand = FALSE;
   2218 			}
   2219 			else
   2220 			    expand = TRUE;
   2221 			break;
   2222 		    }
   2223 		    else {
   2224 			char *p;
   2225 
   2226 			/*
   2227 			 * Scan up to the end of the variable name.
   2228 			 */
   2229 			for (p = &str[2]; *p &&
   2230 			     *p != ':' && *p != ')' && *p != '}'; p++)
   2231 			    if (*p == '$')
   2232 				break;
   2233 			/*
   2234 			 * A variable inside the variable. We cannot expand
   2235 			 * the external variable yet, so we try again with
   2236 			 * the nested one
   2237 			 */
   2238 			if (*p == '$') {
   2239 			    Buf_AddBytes(buf, p - str, (Byte *) str);
   2240 			    str = p;
   2241 			    continue;
   2242 			}
   2243 
   2244 			if (strncmp(var, str + 2, p - str - 2) != 0 ||
   2245 			    var[p - str - 2] != '\0') {
   2246 			    /*
   2247 			     * Not the variable we want to expand, scan
   2248 			     * until the next variable
   2249 			     */
   2250 			    for (;*p != '$' && *p != '\0'; p++)
   2251 				continue;
   2252 			    Buf_AddBytes(buf, p - str, (Byte *) str);
   2253 			    str = p;
   2254 			    expand = FALSE;
   2255 			}
   2256 			else
   2257 			    expand = TRUE;
   2258 			break;
   2259 		    }
   2260 		}
   2261 		if (!expand)
   2262 		    continue;
   2263 	    }
   2264 
   2265 	    val = Var_Parse (str, ctxt, undefErr, &length, &doFree);
   2266 
   2267 	    /*
   2268 	     * When we come down here, val should either point to the
   2269 	     * value of this variable, suitably modified, or be NULL.
   2270 	     * Length should be the total length of the potential
   2271 	     * variable invocation (from $ to end character...)
   2272 	     */
   2273 	    if (val == var_Error || val == varNoError) {
   2274 		/*
   2275 		 * If performing old-time variable substitution, skip over
   2276 		 * the variable and continue with the substitution. Otherwise,
   2277 		 * store the dollar sign and advance str so we continue with
   2278 		 * the string...
   2279 		 */
   2280 		if (oldVars) {
   2281 		    str += length;
   2282 		} else if (undefErr) {
   2283 		    /*
   2284 		     * If variable is undefined, complain and skip the
   2285 		     * variable. The complaint will stop us from doing anything
   2286 		     * when the file is parsed.
   2287 		     */
   2288 		    if (!errorReported) {
   2289 			Parse_Error (PARSE_FATAL,
   2290 				     "Undefined variable \"%.*s\"",length,str);
   2291 		    }
   2292 		    str += length;
   2293 		    errorReported = TRUE;
   2294 		} else {
   2295 		    Buf_AddByte (buf, (Byte)*str);
   2296 		    str += 1;
   2297 		}
   2298 	    } else {
   2299 		/*
   2300 		 * We've now got a variable structure to store in. But first,
   2301 		 * advance the string pointer.
   2302 		 */
   2303 		str += length;
   2304 
   2305 		/*
   2306 		 * Copy all the characters from the variable value straight
   2307 		 * into the new string.
   2308 		 */
   2309 		Buf_AddBytes (buf, strlen (val), (Byte *)val);
   2310 		if (doFree) {
   2311 		    free ((Address)val);
   2312 		}
   2313 	    }
   2314 	}
   2315     }
   2316 
   2317     Buf_AddByte (buf, '\0');
   2318     str = (char *)Buf_GetAll (buf, (int *)NULL);
   2319     Buf_Destroy (buf, FALSE);
   2320     return (str);
   2321 }
   2322 
   2323 /*-
   2324  *-----------------------------------------------------------------------
   2325  * Var_GetTail --
   2326  *	Return the tail from each of a list of words. Used to set the
   2327  *	System V local variables.
   2328  *
   2329  * Results:
   2330  *	The resulting string.
   2331  *
   2332  * Side Effects:
   2333  *	None.
   2334  *
   2335  *-----------------------------------------------------------------------
   2336  */
   2337 char *
   2338 Var_GetTail(file)
   2339     char    	*file;	    /* Filename to modify */
   2340 {
   2341     return(VarModify(file, VarTail, (ClientData)0));
   2342 }
   2343 
   2344 /*-
   2345  *-----------------------------------------------------------------------
   2346  * Var_GetHead --
   2347  *	Find the leading components of a (list of) filename(s).
   2348  *	XXX: VarHead does not replace foo by ., as (sun) System V make
   2349  *	does.
   2350  *
   2351  * Results:
   2352  *	The leading components.
   2353  *
   2354  * Side Effects:
   2355  *	None.
   2356  *
   2357  *-----------------------------------------------------------------------
   2358  */
   2359 char *
   2360 Var_GetHead(file)
   2361     char    	*file;	    /* Filename to manipulate */
   2362 {
   2363     return(VarModify(file, VarHead, (ClientData)0));
   2364 }
   2365 
   2366 /*-
   2367  *-----------------------------------------------------------------------
   2368  * Var_Init --
   2369  *	Initialize the module
   2370  *
   2371  * Results:
   2372  *	None
   2373  *
   2374  * Side Effects:
   2375  *	The VAR_CMD and VAR_GLOBAL contexts are created
   2376  *-----------------------------------------------------------------------
   2377  */
   2378 void
   2379 Var_Init ()
   2380 {
   2381     VAR_GLOBAL = Targ_NewGN ("Global");
   2382     VAR_CMD = Targ_NewGN ("Command");
   2383     allVars = Lst_Init(FALSE);
   2384 
   2385 }
   2386 
   2387 
   2388 void
   2389 Var_End ()
   2390 {
   2391     Lst_Destroy(allVars, VarDelete);
   2392 }
   2393 
   2394 
   2395 /****************** PRINT DEBUGGING INFO *****************/
   2396 static int
   2397 VarPrintVar (vp, dummy)
   2398     ClientData vp;
   2399     ClientData dummy;
   2400 {
   2401     Var    *v = (Var *) vp;
   2402     printf ("%-16s = %s\n", v->name, (char *) Buf_GetAll(v->val, (int *)NULL));
   2403     return (dummy ? 0 : 0);
   2404 }
   2405 
   2406 /*-
   2407  *-----------------------------------------------------------------------
   2408  * Var_Dump --
   2409  *	print all variables in a context
   2410  *-----------------------------------------------------------------------
   2411  */
   2412 void
   2413 Var_Dump (ctxt)
   2414     GNode          *ctxt;
   2415 {
   2416     Lst_ForEach (ctxt->context, VarPrintVar, (ClientData) 0);
   2417 }
   2418