var.c revision 250770
1/*	$NetBSD: var.c,v 1.173 2013/02/24 19:43:37 christos 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
72static char rcsid[] = "$NetBSD: var.c,v 1.173 2013/02/24 19:43:37 christos Exp $";
73#else
74#include <sys/cdefs.h>
75#ifndef lint
76#if 0
77static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
78#else
79__RCSID("$NetBSD: var.c,v 1.173 2013/02/24 19:43:37 christos 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. The value and variable name need not
92 *			    be preserved.
93 *
94 *	Var_Append	    Append more characters to an existing variable
95 *			    in the given context. The variable needn't
96 *			    exist already -- it will be created if it doesn't.
97 *			    A space is placed between the old value and the
98 *			    new one.
99 *
100 *	Var_Exists	    See if a variable exists.
101 *
102 *	Var_Value 	    Return the value of a variable in a context or
103 *			    NULL if the variable is undefined.
104 *
105 *	Var_Subst 	    Substitute named variable, or all variables if
106 *			    NULL in a string using
107 *			    the given context as the top-most one. If the
108 *			    third argument is non-zero, Parse_Error is
109 *			    called if any variables are undefined.
110 *
111 *	Var_Parse 	    Parse a variable expansion from a string and
112 *			    return the result and the number of characters
113 *			    consumed.
114 *
115 *	Var_Delete	    Delete a variable in a context.
116 *
117 *	Var_Init  	    Initialize this module.
118 *
119 * Debugging:
120 *	Var_Dump  	    Print out all variables defined in the given
121 *			    context.
122 *
123 * XXX: There's a lot of duplication in these functions.
124 */
125
126#include    <sys/stat.h>
127#ifndef NO_REGEX
128#include    <sys/types.h>
129#include    <regex.h>
130#endif
131#include    <ctype.h>
132#include    <inttypes.h>
133#include    <stdlib.h>
134#include    <limits.h>
135#include    <time.h>
136
137#include    "make.h"
138#include    "buf.h"
139#include    "dir.h"
140#include    "job.h"
141
142/*
143 * XXX transition hack for FreeBSD ports.
144 * bsd.port.mk can set .MAKE.FreeBSD_UL=yes
145 * to cause us to treat :[LU] as aliases for :t[lu]
146 * To be reverted when ports converts to :t[lu] (when 8.3 is EOL)
147 */
148#define MAKE_FREEBSD_UL ".MAKE.FreeBSD_UL"
149#ifdef MAKE_FREEBSD_UL
150static int FreeBSD_UL = FALSE;
151#endif
152
153/*
154 * This lets us tell if we have replaced the original environ
155 * (which we cannot free).
156 */
157char **savedEnv = NULL;
158
159/*
160 * This is a harmless return value for Var_Parse that can be used by Var_Subst
161 * to determine if there was an error in parsing -- easier than returning
162 * a flag, as things outside this module don't give a hoot.
163 */
164char 	var_Error[] = "";
165
166/*
167 * Similar to var_Error, but returned when the 'errnum' flag for Var_Parse is
168 * set false. Why not just use a constant? Well, gcc likes to condense
169 * identical string instances...
170 */
171static char	varNoError[] = "";
172
173/*
174 * Internally, variables are contained in four different contexts.
175 *	1) the environment. They may not be changed. If an environment
176 *	    variable is appended-to, the result is placed in the global
177 *	    context.
178 *	2) the global context. Variables set in the Makefile are located in
179 *	    the global context. It is the penultimate context searched when
180 *	    substituting.
181 *	3) the command-line context. All variables set on the command line
182 *	   are placed in this context. They are UNALTERABLE once placed here.
183 *	4) the local context. Each target has associated with it a context
184 *	   list. On this list are located the structures describing such
185 *	   local variables as $(@) and $(*)
186 * The four contexts are searched in the reverse order from which they are
187 * listed.
188 */
189GNode          *VAR_GLOBAL;   /* variables from the makefile */
190GNode          *VAR_CMD;      /* variables defined on the command-line */
191
192#define FIND_CMD	0x1   /* look in VAR_CMD when searching */
193#define FIND_GLOBAL	0x2   /* look in VAR_GLOBAL as well */
194#define FIND_ENV  	0x4   /* look in the environment also */
195
196typedef struct Var {
197    char          *name;	/* the variable's name */
198    Buffer	  val;		/* its value */
199    int		  flags;    	/* miscellaneous status flags */
200#define VAR_IN_USE	1   	    /* Variable's value currently being used.
201				     * Used to avoid recursion */
202#define VAR_FROM_ENV	2   	    /* Variable comes from the environment */
203#define VAR_JUNK  	4   	    /* Variable is a junk variable that
204				     * should be destroyed when done with
205				     * it. Used by Var_Parse for undefined,
206				     * modified variables */
207#define VAR_KEEP	8	    /* Variable is VAR_JUNK, but we found
208				     * a use for it in some modifier and
209				     * the value is therefore valid */
210#define VAR_EXPORTED	16 	    /* Variable is exported */
211#define VAR_REEXPORT	32	    /* Indicate if var needs re-export.
212				     * This would be true if it contains $'s
213				     */
214#define VAR_FROM_CMD	64 	    /* Variable came from command line */
215}  Var;
216
217/*
218 * Exporting vars is expensive so skip it if we can
219 */
220#define VAR_EXPORTED_NONE	0
221#define VAR_EXPORTED_YES	1
222#define VAR_EXPORTED_ALL	2
223static int var_exportedVars = VAR_EXPORTED_NONE;
224/*
225 * We pass this to Var_Export when doing the initial export
226 * or after updating an exported var.
227 */
228#define VAR_EXPORT_PARENT 1
229
230/* Var*Pattern flags */
231#define VAR_SUB_GLOBAL	0x01	/* Apply substitution globally */
232#define VAR_SUB_ONE	0x02	/* Apply substitution to one word */
233#define VAR_SUB_MATCHED	0x04	/* There was a match */
234#define VAR_MATCH_START	0x08	/* Match at start of word */
235#define VAR_MATCH_END	0x10	/* Match at end of word */
236#define VAR_NOSUBST	0x20	/* don't expand vars in VarGetPattern */
237
238/* Var_Set flags */
239#define VAR_NO_EXPORT	0x01	/* do not export */
240
241typedef struct {
242    /*
243     * The following fields are set by Var_Parse() when it
244     * encounters modifiers that need to keep state for use by
245     * subsequent modifiers within the same variable expansion.
246     */
247    Byte	varSpace;	/* Word separator in expansions */
248    Boolean	oneBigWord;	/* TRUE if we will treat the variable as a
249				 * single big word, even if it contains
250				 * embedded spaces (as opposed to the
251				 * usual behaviour of treating it as
252				 * several space-separated words). */
253} Var_Parse_State;
254
255/* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/",
256 * to VarSYSVMatch() for ":lhs=rhs". */
257typedef struct {
258    const char   *lhs;	    /* String to match */
259    int		  leftLen; /* Length of string */
260    const char   *rhs;	    /* Replacement string (w/ &'s removed) */
261    int		  rightLen; /* Length of replacement */
262    int		  flags;
263} VarPattern;
264
265/* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */
266typedef struct {
267    GNode	*ctxt;		/* variable context */
268    char	*tvar;		/* name of temp var */
269    int		tvarLen;
270    char	*str;		/* string to expand */
271    int		strLen;
272    int		errnum;		/* errnum for not defined */
273} VarLoop_t;
274
275#ifndef NO_REGEX
276/* struct passed as 'void *' to VarRESubstitute() for ":C///" */
277typedef struct {
278    regex_t	   re;
279    int		   nsub;
280    regmatch_t 	  *matches;
281    char 	  *replace;
282    int		   flags;
283} VarREPattern;
284#endif
285
286/* struct passed to VarSelectWords() for ":[start..end]" */
287typedef struct {
288    int		start;		/* first word to select */
289    int		end;		/* last word to select */
290} VarSelectWords_t;
291
292static Var *VarFind(const char *, GNode *, int);
293static void VarAdd(const char *, const char *, GNode *);
294static Boolean VarHead(GNode *, Var_Parse_State *,
295			char *, Boolean, Buffer *, void *);
296static Boolean VarTail(GNode *, Var_Parse_State *,
297			char *, Boolean, Buffer *, void *);
298static Boolean VarSuffix(GNode *, Var_Parse_State *,
299			char *, Boolean, Buffer *, void *);
300static Boolean VarRoot(GNode *, Var_Parse_State *,
301			char *, Boolean, Buffer *, void *);
302static Boolean VarMatch(GNode *, Var_Parse_State *,
303			char *, Boolean, Buffer *, void *);
304#ifdef SYSVVARSUB
305static Boolean VarSYSVMatch(GNode *, Var_Parse_State *,
306			char *, Boolean, Buffer *, void *);
307#endif
308static Boolean VarNoMatch(GNode *, Var_Parse_State *,
309			char *, Boolean, Buffer *, void *);
310#ifndef NO_REGEX
311static void VarREError(int, regex_t *, const char *);
312static Boolean VarRESubstitute(GNode *, Var_Parse_State *,
313			char *, Boolean, Buffer *, void *);
314#endif
315static Boolean VarSubstitute(GNode *, Var_Parse_State *,
316			char *, Boolean, Buffer *, void *);
317static Boolean VarLoopExpand(GNode *, Var_Parse_State *,
318			char *, Boolean, Buffer *, void *);
319static char *VarGetPattern(GNode *, Var_Parse_State *,
320			   int, const char **, int, int *, int *,
321			   VarPattern *);
322static char *VarQuote(char *);
323static char *VarHash(char *);
324static char *VarModify(GNode *, Var_Parse_State *,
325    const char *,
326    Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *),
327    void *);
328static char *VarOrder(const char *, const char);
329static char *VarUniq(const char *);
330static int VarWordCompare(const void *, const void *);
331static void VarPrintVar(void *);
332
333#define BROPEN	'{'
334#define BRCLOSE	'}'
335#define PROPEN	'('
336#define PRCLOSE	')'
337
338/*-
339 *-----------------------------------------------------------------------
340 * VarFind --
341 *	Find the given variable in the given context and any other contexts
342 *	indicated.
343 *
344 * Input:
345 *	name		name to find
346 *	ctxt		context in which to find it
347 *	flags		FIND_GLOBAL set means to look in the
348 *			VAR_GLOBAL context as well. FIND_CMD set means
349 *			to look in the VAR_CMD context also. FIND_ENV
350 *			set means to look in the environment
351 *
352 * Results:
353 *	A pointer to the structure describing the desired variable or
354 *	NULL if the variable does not exist.
355 *
356 * Side Effects:
357 *	None
358 *-----------------------------------------------------------------------
359 */
360static Var *
361VarFind(const char *name, GNode *ctxt, int flags)
362{
363    Hash_Entry         	*var;
364    Var			*v;
365
366	/*
367	 * If the variable name begins with a '.', it could very well be one of
368	 * the local ones.  We check the name against all the local variables
369	 * and substitute the short version in for 'name' if it matches one of
370	 * them.
371	 */
372	if (*name == '.' && isupper((unsigned char) name[1]))
373		switch (name[1]) {
374		case 'A':
375			if (!strcmp(name, ".ALLSRC"))
376				name = ALLSRC;
377			if (!strcmp(name, ".ARCHIVE"))
378				name = ARCHIVE;
379			break;
380		case 'I':
381			if (!strcmp(name, ".IMPSRC"))
382				name = IMPSRC;
383			break;
384		case 'M':
385			if (!strcmp(name, ".MEMBER"))
386				name = MEMBER;
387			break;
388		case 'O':
389			if (!strcmp(name, ".OODATE"))
390				name = OODATE;
391			break;
392		case 'P':
393			if (!strcmp(name, ".PREFIX"))
394				name = PREFIX;
395			break;
396		case 'T':
397			if (!strcmp(name, ".TARGET"))
398				name = TARGET;
399			break;
400		}
401#ifdef notyet
402    /* for compatibility with gmake */
403    if (name[0] == '^' && name[1] == '\0')
404	    name = ALLSRC;
405#endif
406
407    /*
408     * First look for the variable in the given context. If it's not there,
409     * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
410     * depending on the FIND_* flags in 'flags'
411     */
412    var = Hash_FindEntry(&ctxt->context, name);
413
414    if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) {
415	var = Hash_FindEntry(&VAR_CMD->context, name);
416    }
417    if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) &&
418	(ctxt != VAR_GLOBAL))
419    {
420	var = Hash_FindEntry(&VAR_GLOBAL->context, name);
421    }
422    if ((var == NULL) && (flags & FIND_ENV)) {
423	char *env;
424
425	if ((env = getenv(name)) != NULL) {
426	    int		len;
427
428	    v = bmake_malloc(sizeof(Var));
429	    v->name = bmake_strdup(name);
430
431	    len = strlen(env);
432
433	    Buf_Init(&v->val, len + 1);
434	    Buf_AddBytes(&v->val, len, env);
435
436	    v->flags = VAR_FROM_ENV;
437	    return (v);
438	} else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
439		   (ctxt != VAR_GLOBAL))
440	{
441	    var = Hash_FindEntry(&VAR_GLOBAL->context, name);
442	    if (var == NULL) {
443		return NULL;
444	    } else {
445		return ((Var *)Hash_GetValue(var));
446	    }
447	} else {
448	    return NULL;
449	}
450    } else if (var == NULL) {
451	return NULL;
452    } else {
453	return ((Var *)Hash_GetValue(var));
454    }
455}
456
457/*-
458 *-----------------------------------------------------------------------
459 * VarFreeEnv  --
460 *	If the variable is an environment variable, free it
461 *
462 * Input:
463 *	v		the variable
464 *	destroy		true if the value buffer should be destroyed.
465 *
466 * Results:
467 *	1 if it is an environment variable 0 ow.
468 *
469 * Side Effects:
470 *	The variable is free'ed if it is an environent variable.
471 *-----------------------------------------------------------------------
472 */
473static Boolean
474VarFreeEnv(Var *v, Boolean destroy)
475{
476    if ((v->flags & VAR_FROM_ENV) == 0)
477	return FALSE;
478    free(v->name);
479    Buf_Destroy(&v->val, destroy);
480    free(v);
481    return TRUE;
482}
483
484/*-
485 *-----------------------------------------------------------------------
486 * VarAdd  --
487 *	Add a new variable of name name and value val to the given context
488 *
489 * Input:
490 *	name		name of variable to add
491 *	val		value to set it to
492 *	ctxt		context in which to set it
493 *
494 * Results:
495 *	None
496 *
497 * Side Effects:
498 *	The new variable is placed at the front of the given context
499 *	The name and val arguments are duplicated so they may
500 *	safely be freed.
501 *-----------------------------------------------------------------------
502 */
503static void
504VarAdd(const char *name, const char *val, GNode *ctxt)
505{
506    Var   	  *v;
507    int		  len;
508    Hash_Entry    *h;
509
510    v = bmake_malloc(sizeof(Var));
511
512    len = val ? strlen(val) : 0;
513    Buf_Init(&v->val, len+1);
514    Buf_AddBytes(&v->val, len, val);
515
516    v->flags = 0;
517
518    h = Hash_CreateEntry(&ctxt->context, name, NULL);
519    Hash_SetValue(h, v);
520    v->name = h->name;
521    if (DEBUG(VAR)) {
522	fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
523    }
524}
525
526/*-
527 *-----------------------------------------------------------------------
528 * Var_Delete --
529 *	Remove a variable from a context.
530 *
531 * Results:
532 *	None.
533 *
534 * Side Effects:
535 *	The Var structure is removed and freed.
536 *
537 *-----------------------------------------------------------------------
538 */
539void
540Var_Delete(const char *name, GNode *ctxt)
541{
542    Hash_Entry 	  *ln;
543    char *cp;
544
545    if (strchr(name, '$')) {
546	cp = Var_Subst(NULL, name, VAR_GLOBAL, 0);
547    } else {
548	cp = name;
549    }
550    ln = Hash_FindEntry(&ctxt->context, cp);
551    if (DEBUG(VAR)) {
552	fprintf(debug_file, "%s:delete %s%s\n",
553	    ctxt->name, cp, ln ? "" : " (not found)");
554    }
555    if (cp != name) {
556	free(cp);
557    }
558    if (ln != NULL) {
559	Var 	  *v;
560
561	v = (Var *)Hash_GetValue(ln);
562	if ((v->flags & VAR_EXPORTED)) {
563	    unsetenv(v->name);
564	}
565	if (strcmp(MAKE_EXPORTED, v->name) == 0) {
566	    var_exportedVars = VAR_EXPORTED_NONE;
567	}
568	if (v->name != ln->name)
569		free(v->name);
570	Hash_DeleteEntry(&ctxt->context, ln);
571	Buf_Destroy(&v->val, TRUE);
572	free(v);
573    }
574}
575
576
577/*
578 * Export a var.
579 * We ignore make internal variables (those which start with '.')
580 * Also we jump through some hoops to avoid calling setenv
581 * more than necessary since it can leak.
582 * We only manipulate flags of vars if 'parent' is set.
583 */
584static int
585Var_Export1(const char *name, int parent)
586{
587    char tmp[BUFSIZ];
588    Var *v;
589    char *val = NULL;
590    int n;
591
592    if (*name == '.')
593	return 0;			/* skip internals */
594    if (!name[1]) {
595	/*
596	 * A single char.
597	 * If it is one of the vars that should only appear in
598	 * local context, skip it, else we can get Var_Subst
599	 * into a loop.
600	 */
601	switch (name[0]) {
602	case '@':
603	case '%':
604	case '*':
605	case '!':
606	    return 0;
607	}
608    }
609    v = VarFind(name, VAR_GLOBAL, 0);
610    if (v == NULL) {
611	return 0;
612    }
613    if (!parent &&
614	(v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
615	return 0;			/* nothing to do */
616    }
617    val = Buf_GetAll(&v->val, NULL);
618    if (strchr(val, '$')) {
619	if (parent) {
620	    /*
621	     * Flag this as something we need to re-export.
622	     * No point actually exporting it now though,
623	     * the child can do it at the last minute.
624	     */
625	    v->flags |= (VAR_EXPORTED|VAR_REEXPORT);
626	    return 1;
627	}
628	if (v->flags & VAR_IN_USE) {
629	    /*
630	     * We recursed while exporting in a child.
631	     * This isn't going to end well, just skip it.
632	     */
633	    return 0;
634	}
635	n = snprintf(tmp, sizeof(tmp), "${%s}", name);
636	if (n < (int)sizeof(tmp)) {
637	    val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
638	    setenv(name, val, 1);
639	    free(val);
640	}
641    } else {
642	if (parent) {
643	    v->flags &= ~VAR_REEXPORT;	/* once will do */
644	}
645	if (parent || !(v->flags & VAR_EXPORTED)) {
646	    setenv(name, val, 1);
647	}
648    }
649    /*
650     * This is so Var_Set knows to call Var_Export again...
651     */
652    if (parent) {
653	v->flags |= VAR_EXPORTED;
654    }
655    return 1;
656}
657
658/*
659 * This gets called from our children.
660 */
661void
662Var_ExportVars(void)
663{
664    char tmp[BUFSIZ];
665    Hash_Entry         	*var;
666    Hash_Search 	state;
667    Var *v;
668    char *val;
669    int n;
670
671    if (VAR_EXPORTED_NONE == var_exportedVars)
672	return;
673
674    if (VAR_EXPORTED_ALL == var_exportedVars) {
675	/*
676	 * Ouch! This is crazy...
677	 */
678	for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state);
679	     var != NULL;
680	     var = Hash_EnumNext(&state)) {
681	    v = (Var *)Hash_GetValue(var);
682	    Var_Export1(v->name, 0);
683	}
684	return;
685    }
686    /*
687     * We have a number of exported vars,
688     */
689    n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
690    if (n < (int)sizeof(tmp)) {
691	char **av;
692	char *as;
693	int ac;
694	int i;
695
696	val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
697	av = brk_string(val, &ac, FALSE, &as);
698	for (i = 0; i < ac; i++) {
699	    Var_Export1(av[i], 0);
700	}
701	free(val);
702	free(as);
703	free(av);
704    }
705}
706
707/*
708 * This is called when .export is seen or
709 * .MAKE.EXPORTED is modified.
710 * It is also called when any exported var is modified.
711 */
712void
713Var_Export(char *str, int isExport)
714{
715    char *name;
716    char *val;
717    char **av;
718    char *as;
719    int track;
720    int ac;
721    int i;
722
723    if (isExport && (!str || !str[0])) {
724	var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
725	return;
726    }
727
728    if (strncmp(str, "-env", 4) == 0) {
729	track = 0;
730	str += 4;
731    } else {
732	track = VAR_EXPORT_PARENT;
733    }
734    val = Var_Subst(NULL, str, VAR_GLOBAL, 0);
735    av = brk_string(val, &ac, FALSE, &as);
736    for (i = 0; i < ac; i++) {
737	name = av[i];
738	if (!name[1]) {
739	    /*
740	     * A single char.
741	     * If it is one of the vars that should only appear in
742	     * local context, skip it, else we can get Var_Subst
743	     * into a loop.
744	     */
745	    switch (name[0]) {
746	    case '@':
747	    case '%':
748	    case '*':
749	    case '!':
750		continue;
751	    }
752	}
753	if (Var_Export1(name, track)) {
754	    if (VAR_EXPORTED_ALL != var_exportedVars)
755		var_exportedVars = VAR_EXPORTED_YES;
756	    if (isExport && track) {
757		Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
758	    }
759	}
760    }
761    free(val);
762    free(as);
763    free(av);
764}
765
766
767/*
768 * This is called when .unexport[-env] is seen.
769 */
770extern char **environ;
771
772void
773Var_UnExport(char *str)
774{
775    char tmp[BUFSIZ];
776    char *vlist;
777    char *cp;
778    Boolean unexport_env;
779    int n;
780
781    if (!str || !str[0]) {
782	return; 			/* assert? */
783    }
784
785    vlist = NULL;
786
787    str += 8;
788    unexport_env = (strncmp(str, "-env", 4) == 0);
789    if (unexport_env) {
790	char **newenv;
791
792	cp = getenv(MAKE_LEVEL);	/* we should preserve this */
793	if (environ == savedEnv) {
794	    /* we have been here before! */
795	    newenv = bmake_realloc(environ, 2 * sizeof(char *));
796	} else {
797	    if (savedEnv) {
798		free(savedEnv);
799		savedEnv = NULL;
800	    }
801	    newenv = bmake_malloc(2 * sizeof(char *));
802	}
803	if (!newenv)
804	    return;
805	/* Note: we cannot safely free() the original environ. */
806	environ = savedEnv = newenv;
807	newenv[0] = NULL;
808	newenv[1] = NULL;
809	setenv(MAKE_LEVEL, cp, 1);
810#ifdef MAKE_LEVEL_SAFE
811	setenv(MAKE_LEVEL_SAFE, cp, 1);
812#endif
813    } else {
814	for (; *str != '\n' && isspace((unsigned char) *str); str++)
815	    continue;
816	if (str[0] && str[0] != '\n') {
817	    vlist = str;
818	}
819    }
820
821    if (!vlist) {
822	/* Using .MAKE.EXPORTED */
823	n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
824	if (n < (int)sizeof(tmp)) {
825	    vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
826	}
827    }
828    if (vlist) {
829	Var *v;
830	char **av;
831	char *as;
832	int ac;
833	int i;
834
835	av = brk_string(vlist, &ac, FALSE, &as);
836	for (i = 0; i < ac; i++) {
837	    v = VarFind(av[i], VAR_GLOBAL, 0);
838	    if (!v)
839		continue;
840	    if (!unexport_env &&
841		(v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
842		unsetenv(v->name);
843	    }
844	    v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
845	    /*
846	     * If we are unexporting a list,
847	     * remove each one from .MAKE.EXPORTED.
848	     * If we are removing them all,
849	     * just delete .MAKE.EXPORTED below.
850	     */
851	    if (vlist == str) {
852		n = snprintf(tmp, sizeof(tmp),
853			     "${" MAKE_EXPORTED ":N%s}", v->name);
854		if (n < (int)sizeof(tmp)) {
855		    cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
856		    Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0);
857		    free(cp);
858		}
859	    }
860	}
861	free(as);
862	free(av);
863	if (vlist != str) {
864	    Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
865	    free(vlist);
866	}
867    }
868}
869
870/*-
871 *-----------------------------------------------------------------------
872 * Var_Set --
873 *	Set the variable name to the value val in the given context.
874 *
875 * Input:
876 *	name		name of variable to set
877 *	val		value to give to the variable
878 *	ctxt		context in which to set it
879 *
880 * Results:
881 *	None.
882 *
883 * Side Effects:
884 *	If the variable doesn't yet exist, a new record is created for it.
885 *	Else the old value is freed and the new one stuck in its place
886 *
887 * Notes:
888 *	The variable is searched for only in its context before being
889 *	created in that context. I.e. if the context is VAR_GLOBAL,
890 *	only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
891 *	VAR_CMD->context is searched. This is done to avoid the literally
892 *	thousands of unnecessary strcmp's that used to be done to
893 *	set, say, $(@) or $(<).
894 *	If the context is VAR_GLOBAL though, we check if the variable
895 *	was set in VAR_CMD from the command line and skip it if so.
896 *-----------------------------------------------------------------------
897 */
898void
899Var_Set(const char *name, const char *val, GNode *ctxt, int flags)
900{
901    Var   *v;
902    char *expanded_name = NULL;
903
904    /*
905     * We only look for a variable in the given context since anything set
906     * here will override anything in a lower context, so there's not much
907     * point in searching them all just to save a bit of memory...
908     */
909    if (strchr(name, '$') != NULL) {
910	expanded_name = Var_Subst(NULL, name, ctxt, 0);
911	if (expanded_name[0] == 0) {
912	    if (DEBUG(VAR)) {
913		fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
914			"name expands to empty string - ignored\n",
915			name, val);
916	    }
917	    free(expanded_name);
918	    return;
919	}
920	name = expanded_name;
921    }
922    if (ctxt == VAR_GLOBAL) {
923	v = VarFind(name, VAR_CMD, 0);
924	if (v != NULL) {
925	    if ((v->flags & VAR_FROM_CMD)) {
926		if (DEBUG(VAR)) {
927		    fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val);
928		}
929		goto out;
930	    }
931	    VarFreeEnv(v, TRUE);
932	}
933    }
934    v = VarFind(name, ctxt, 0);
935    if (v == NULL) {
936	VarAdd(name, val, ctxt);
937    } else {
938	Buf_Empty(&v->val);
939	Buf_AddBytes(&v->val, strlen(val), val);
940
941	if (DEBUG(VAR)) {
942	    fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
943	}
944	if ((v->flags & VAR_EXPORTED)) {
945	    Var_Export1(name, VAR_EXPORT_PARENT);
946	}
947    }
948    /*
949     * Any variables given on the command line are automatically exported
950     * to the environment (as per POSIX standard)
951     */
952    if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
953	if (v == NULL) {
954	    /* we just added it */
955	    v = VarFind(name, ctxt, 0);
956	}
957	if (v != NULL)
958	    v->flags |= VAR_FROM_CMD;
959	/*
960	 * If requested, don't export these in the environment
961	 * individually.  We still put them in MAKEOVERRIDES so
962	 * that the command-line settings continue to override
963	 * Makefile settings.
964	 */
965	if (varNoExportEnv != TRUE)
966	    setenv(name, val, 1);
967
968	Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
969    }
970    /*
971     * Another special case.
972     * Several make's support this sort of mechanism for tracking
973     * recursion - but each uses a different name.
974     * We allow the makefiles to update .MAKE.LEVEL and ensure
975     * children see a correctly incremented value.
976     */
977    if (ctxt == VAR_GLOBAL && strcmp(MAKE_LEVEL, name) == 0) {
978	char tmp[64];
979	int level;
980
981	level = atoi(val);
982	snprintf(tmp, sizeof(tmp), "%u", level + 1);
983	setenv(MAKE_LEVEL, tmp, 1);
984#ifdef MAKE_LEVEL_SAFE
985	setenv(MAKE_LEVEL_SAFE, tmp, 1);
986#endif
987    }
988#ifdef MAKE_FREEBSD_UL
989    if (strcmp(MAKE_FREEBSD_UL, name) == 0) {
990	FreeBSD_UL = getBoolean(MAKE_FREEBSD_UL, FALSE);
991    }
992#endif
993
994
995 out:
996    if (expanded_name != NULL)
997	free(expanded_name);
998    if (v != NULL)
999	VarFreeEnv(v, TRUE);
1000}
1001
1002/*-
1003 *-----------------------------------------------------------------------
1004 * Var_Append --
1005 *	The variable of the given name has the given value appended to it in
1006 *	the given context.
1007 *
1008 * Input:
1009 *	name		name of variable to modify
1010 *	val		String to append to it
1011 *	ctxt		Context in which this should occur
1012 *
1013 * Results:
1014 *	None
1015 *
1016 * Side Effects:
1017 *	If the variable doesn't exist, it is created. Else the strings
1018 *	are concatenated (with a space in between).
1019 *
1020 * Notes:
1021 *	Only if the variable is being sought in the global context is the
1022 *	environment searched.
1023 *	XXX: Knows its calling circumstances in that if called with ctxt
1024 *	an actual target, it will only search that context since only
1025 *	a local variable could be being appended to. This is actually
1026 *	a big win and must be tolerated.
1027 *-----------------------------------------------------------------------
1028 */
1029void
1030Var_Append(const char *name, const char *val, GNode *ctxt)
1031{
1032    Var		   *v;
1033    Hash_Entry	   *h;
1034    char *expanded_name = NULL;
1035
1036    if (strchr(name, '$') != NULL) {
1037	expanded_name = Var_Subst(NULL, name, ctxt, 0);
1038	if (expanded_name[0] == 0) {
1039	    if (DEBUG(VAR)) {
1040		fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
1041			"name expands to empty string - ignored\n",
1042			name, val);
1043	    }
1044	    free(expanded_name);
1045	    return;
1046	}
1047	name = expanded_name;
1048    }
1049
1050    v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
1051
1052    if (v == NULL) {
1053	VarAdd(name, val, ctxt);
1054    } else {
1055	Buf_AddByte(&v->val, ' ');
1056	Buf_AddBytes(&v->val, strlen(val), val);
1057
1058	if (DEBUG(VAR)) {
1059	    fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
1060		   Buf_GetAll(&v->val, NULL));
1061	}
1062
1063	if (v->flags & VAR_FROM_ENV) {
1064	    /*
1065	     * If the original variable came from the environment, we
1066	     * have to install it in the global context (we could place
1067	     * it in the environment, but then we should provide a way to
1068	     * export other variables...)
1069	     */
1070	    v->flags &= ~VAR_FROM_ENV;
1071	    h = Hash_CreateEntry(&ctxt->context, name, NULL);
1072	    Hash_SetValue(h, v);
1073	}
1074    }
1075    if (expanded_name != NULL)
1076	free(expanded_name);
1077}
1078
1079/*-
1080 *-----------------------------------------------------------------------
1081 * Var_Exists --
1082 *	See if the given variable exists.
1083 *
1084 * Input:
1085 *	name		Variable to find
1086 *	ctxt		Context in which to start search
1087 *
1088 * Results:
1089 *	TRUE if it does, FALSE if it doesn't
1090 *
1091 * Side Effects:
1092 *	None.
1093 *
1094 *-----------------------------------------------------------------------
1095 */
1096Boolean
1097Var_Exists(const char *name, GNode *ctxt)
1098{
1099    Var		  *v;
1100    char          *cp;
1101
1102    if ((cp = strchr(name, '$')) != NULL) {
1103	cp = Var_Subst(NULL, name, ctxt, FALSE);
1104    }
1105    v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
1106    if (cp != NULL) {
1107	free(cp);
1108    }
1109    if (v == NULL) {
1110	return(FALSE);
1111    } else {
1112	(void)VarFreeEnv(v, TRUE);
1113    }
1114    return(TRUE);
1115}
1116
1117/*-
1118 *-----------------------------------------------------------------------
1119 * Var_Value --
1120 *	Return the value of the named variable in the given context
1121 *
1122 * Input:
1123 *	name		name to find
1124 *	ctxt		context in which to search for it
1125 *
1126 * Results:
1127 *	The value if the variable exists, NULL if it doesn't
1128 *
1129 * Side Effects:
1130 *	None
1131 *-----------------------------------------------------------------------
1132 */
1133char *
1134Var_Value(const char *name, GNode *ctxt, char **frp)
1135{
1136    Var            *v;
1137
1138    v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1139    *frp = NULL;
1140    if (v != NULL) {
1141	char *p = (Buf_GetAll(&v->val, NULL));
1142	if (VarFreeEnv(v, FALSE))
1143	    *frp = p;
1144	return p;
1145    } else {
1146	return NULL;
1147    }
1148}
1149
1150/*-
1151 *-----------------------------------------------------------------------
1152 * VarHead --
1153 *	Remove the tail of the given word and place the result in the given
1154 *	buffer.
1155 *
1156 * Input:
1157 *	word		Word to trim
1158 *	addSpace	True if need to add a space to the buffer
1159 *			before sticking in the head
1160 *	buf		Buffer in which to store it
1161 *
1162 * Results:
1163 *	TRUE if characters were added to the buffer (a space needs to be
1164 *	added to the buffer before the next word).
1165 *
1166 * Side Effects:
1167 *	The trimmed word is added to the buffer.
1168 *
1169 *-----------------------------------------------------------------------
1170 */
1171static Boolean
1172VarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1173	char *word, Boolean addSpace, Buffer *buf,
1174	void *dummy)
1175{
1176    char *slash;
1177
1178    slash = strrchr(word, '/');
1179    if (slash != NULL) {
1180	if (addSpace && vpstate->varSpace) {
1181	    Buf_AddByte(buf, vpstate->varSpace);
1182	}
1183	*slash = '\0';
1184	Buf_AddBytes(buf, strlen(word), word);
1185	*slash = '/';
1186	return (TRUE);
1187    } else {
1188	/*
1189	 * If no directory part, give . (q.v. the POSIX standard)
1190	 */
1191	if (addSpace && vpstate->varSpace)
1192	    Buf_AddByte(buf, vpstate->varSpace);
1193	Buf_AddByte(buf, '.');
1194    }
1195    return(dummy ? TRUE : TRUE);
1196}
1197
1198/*-
1199 *-----------------------------------------------------------------------
1200 * VarTail --
1201 *	Remove the head of the given word and place the result in the given
1202 *	buffer.
1203 *
1204 * Input:
1205 *	word		Word to trim
1206 *	addSpace	True if need to add a space to the buffer
1207 *			before adding the tail
1208 *	buf		Buffer in which to store it
1209 *
1210 * Results:
1211 *	TRUE if characters were added to the buffer (a space needs to be
1212 *	added to the buffer before the next word).
1213 *
1214 * Side Effects:
1215 *	The trimmed word is added to the buffer.
1216 *
1217 *-----------------------------------------------------------------------
1218 */
1219static Boolean
1220VarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1221	char *word, Boolean addSpace, Buffer *buf,
1222	void *dummy)
1223{
1224    char *slash;
1225
1226    if (addSpace && vpstate->varSpace) {
1227	Buf_AddByte(buf, vpstate->varSpace);
1228    }
1229
1230    slash = strrchr(word, '/');
1231    if (slash != NULL) {
1232	*slash++ = '\0';
1233	Buf_AddBytes(buf, strlen(slash), slash);
1234	slash[-1] = '/';
1235    } else {
1236	Buf_AddBytes(buf, strlen(word), word);
1237    }
1238    return (dummy ? TRUE : TRUE);
1239}
1240
1241/*-
1242 *-----------------------------------------------------------------------
1243 * VarSuffix --
1244 *	Place the suffix of the given word in the given buffer.
1245 *
1246 * Input:
1247 *	word		Word to trim
1248 *	addSpace	TRUE if need to add a space before placing the
1249 *			suffix in the buffer
1250 *	buf		Buffer in which to store it
1251 *
1252 * Results:
1253 *	TRUE if characters were added to the buffer (a space needs to be
1254 *	added to the buffer before the next word).
1255 *
1256 * Side Effects:
1257 *	The suffix from the word is placed in the buffer.
1258 *
1259 *-----------------------------------------------------------------------
1260 */
1261static Boolean
1262VarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1263	  char *word, Boolean addSpace, Buffer *buf,
1264	  void *dummy)
1265{
1266    char *dot;
1267
1268    dot = strrchr(word, '.');
1269    if (dot != NULL) {
1270	if (addSpace && vpstate->varSpace) {
1271	    Buf_AddByte(buf, vpstate->varSpace);
1272	}
1273	*dot++ = '\0';
1274	Buf_AddBytes(buf, strlen(dot), dot);
1275	dot[-1] = '.';
1276	addSpace = TRUE;
1277    }
1278    return (dummy ? addSpace : addSpace);
1279}
1280
1281/*-
1282 *-----------------------------------------------------------------------
1283 * VarRoot --
1284 *	Remove the suffix of the given word and place the result in the
1285 *	buffer.
1286 *
1287 * Input:
1288 *	word		Word to trim
1289 *	addSpace	TRUE if need to add a space to the buffer
1290 *			before placing the root in it
1291 *	buf		Buffer in which to store it
1292 *
1293 * Results:
1294 *	TRUE if characters were added to the buffer (a space needs to be
1295 *	added to the buffer before the next word).
1296 *
1297 * Side Effects:
1298 *	The trimmed word is added to the buffer.
1299 *
1300 *-----------------------------------------------------------------------
1301 */
1302static Boolean
1303VarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1304	char *word, Boolean addSpace, Buffer *buf,
1305	void *dummy)
1306{
1307    char *dot;
1308
1309    if (addSpace && vpstate->varSpace) {
1310	Buf_AddByte(buf, vpstate->varSpace);
1311    }
1312
1313    dot = strrchr(word, '.');
1314    if (dot != NULL) {
1315	*dot = '\0';
1316	Buf_AddBytes(buf, strlen(word), word);
1317	*dot = '.';
1318    } else {
1319	Buf_AddBytes(buf, strlen(word), word);
1320    }
1321    return (dummy ? TRUE : TRUE);
1322}
1323
1324/*-
1325 *-----------------------------------------------------------------------
1326 * VarMatch --
1327 *	Place the word in the buffer if it matches the given pattern.
1328 *	Callback function for VarModify to implement the :M modifier.
1329 *
1330 * Input:
1331 *	word		Word to examine
1332 *	addSpace	TRUE if need to add a space to the buffer
1333 *			before adding the word, if it matches
1334 *	buf		Buffer in which to store it
1335 *	pattern		Pattern the word must match
1336 *
1337 * Results:
1338 *	TRUE if a space should be placed in the buffer before the next
1339 *	word.
1340 *
1341 * Side Effects:
1342 *	The word may be copied to the buffer.
1343 *
1344 *-----------------------------------------------------------------------
1345 */
1346static Boolean
1347VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1348	 char *word, Boolean addSpace, Buffer *buf,
1349	 void *pattern)
1350{
1351    if (DEBUG(VAR))
1352	fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern);
1353    if (Str_Match(word, (char *)pattern)) {
1354	if (addSpace && vpstate->varSpace) {
1355	    Buf_AddByte(buf, vpstate->varSpace);
1356	}
1357	addSpace = TRUE;
1358	Buf_AddBytes(buf, strlen(word), word);
1359    }
1360    return(addSpace);
1361}
1362
1363#ifdef SYSVVARSUB
1364/*-
1365 *-----------------------------------------------------------------------
1366 * VarSYSVMatch --
1367 *	Place the word in the buffer if it matches the given pattern.
1368 *	Callback function for VarModify to implement the System V %
1369 *	modifiers.
1370 *
1371 * Input:
1372 *	word		Word to examine
1373 *	addSpace	TRUE if need to add a space to the buffer
1374 *			before adding the word, if it matches
1375 *	buf		Buffer in which to store it
1376 *	patp		Pattern the word must match
1377 *
1378 * Results:
1379 *	TRUE if a space should be placed in the buffer before the next
1380 *	word.
1381 *
1382 * Side Effects:
1383 *	The word may be copied to the buffer.
1384 *
1385 *-----------------------------------------------------------------------
1386 */
1387static Boolean
1388VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
1389	     char *word, Boolean addSpace, Buffer *buf,
1390	     void *patp)
1391{
1392    int len;
1393    char *ptr;
1394    VarPattern 	  *pat = (VarPattern *)patp;
1395    char *varexp;
1396
1397    if (addSpace && vpstate->varSpace)
1398	Buf_AddByte(buf, vpstate->varSpace);
1399
1400    addSpace = TRUE;
1401
1402    if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) {
1403        varexp = Var_Subst(NULL, pat->rhs, ctx, 0);
1404	Str_SYSVSubst(buf, varexp, ptr, len);
1405	free(varexp);
1406    } else {
1407	Buf_AddBytes(buf, strlen(word), word);
1408    }
1409
1410    return(addSpace);
1411}
1412#endif
1413
1414
1415/*-
1416 *-----------------------------------------------------------------------
1417 * VarNoMatch --
1418 *	Place the word in the buffer if it doesn't match the given pattern.
1419 *	Callback function for VarModify to implement the :N modifier.
1420 *
1421 * Input:
1422 *	word		Word to examine
1423 *	addSpace	TRUE if need to add a space to the buffer
1424 *			before adding the word, if it matches
1425 *	buf		Buffer in which to store it
1426 *	pattern		Pattern the word must match
1427 *
1428 * Results:
1429 *	TRUE if a space should be placed in the buffer before the next
1430 *	word.
1431 *
1432 * Side Effects:
1433 *	The word may be copied to the buffer.
1434 *
1435 *-----------------------------------------------------------------------
1436 */
1437static Boolean
1438VarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1439	   char *word, Boolean addSpace, Buffer *buf,
1440	   void *pattern)
1441{
1442    if (!Str_Match(word, (char *)pattern)) {
1443	if (addSpace && vpstate->varSpace) {
1444	    Buf_AddByte(buf, vpstate->varSpace);
1445	}
1446	addSpace = TRUE;
1447	Buf_AddBytes(buf, strlen(word), word);
1448    }
1449    return(addSpace);
1450}
1451
1452
1453/*-
1454 *-----------------------------------------------------------------------
1455 * VarSubstitute --
1456 *	Perform a string-substitution on the given word, placing the
1457 *	result in the passed buffer.
1458 *
1459 * Input:
1460 *	word		Word to modify
1461 *	addSpace	True if space should be added before
1462 *			other characters
1463 *	buf		Buffer for result
1464 *	patternp	Pattern for substitution
1465 *
1466 * Results:
1467 *	TRUE if a space is needed before more characters are added.
1468 *
1469 * Side Effects:
1470 *	None.
1471 *
1472 *-----------------------------------------------------------------------
1473 */
1474static Boolean
1475VarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1476	      char *word, Boolean addSpace, Buffer *buf,
1477	      void *patternp)
1478{
1479    int  	wordLen;    /* Length of word */
1480    char 	*cp;	    /* General pointer */
1481    VarPattern	*pattern = (VarPattern *)patternp;
1482
1483    wordLen = strlen(word);
1484    if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
1485	(VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1486	/*
1487	 * Still substituting -- break it down into simple anchored cases
1488	 * and if none of them fits, perform the general substitution case.
1489	 */
1490	if ((pattern->flags & VAR_MATCH_START) &&
1491	    (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
1492		/*
1493		 * Anchored at start and beginning of word matches pattern
1494		 */
1495		if ((pattern->flags & VAR_MATCH_END) &&
1496		    (wordLen == pattern->leftLen)) {
1497			/*
1498			 * Also anchored at end and matches to the end (word
1499			 * is same length as pattern) add space and rhs only
1500			 * if rhs is non-null.
1501			 */
1502			if (pattern->rightLen != 0) {
1503			    if (addSpace && vpstate->varSpace) {
1504				Buf_AddByte(buf, vpstate->varSpace);
1505			    }
1506			    addSpace = TRUE;
1507			    Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1508			}
1509			pattern->flags |= VAR_SUB_MATCHED;
1510		} else if (pattern->flags & VAR_MATCH_END) {
1511		    /*
1512		     * Doesn't match to end -- copy word wholesale
1513		     */
1514		    goto nosub;
1515		} else {
1516		    /*
1517		     * Matches at start but need to copy in trailing characters
1518		     */
1519		    if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
1520			if (addSpace && vpstate->varSpace) {
1521			    Buf_AddByte(buf, vpstate->varSpace);
1522			}
1523			addSpace = TRUE;
1524		    }
1525		    Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1526		    Buf_AddBytes(buf, wordLen - pattern->leftLen,
1527				 (word + pattern->leftLen));
1528		    pattern->flags |= VAR_SUB_MATCHED;
1529		}
1530	} else if (pattern->flags & VAR_MATCH_START) {
1531	    /*
1532	     * Had to match at start of word and didn't -- copy whole word.
1533	     */
1534	    goto nosub;
1535	} else if (pattern->flags & VAR_MATCH_END) {
1536	    /*
1537	     * Anchored at end, Find only place match could occur (leftLen
1538	     * characters from the end of the word) and see if it does. Note
1539	     * that because the $ will be left at the end of the lhs, we have
1540	     * to use strncmp.
1541	     */
1542	    cp = word + (wordLen - pattern->leftLen);
1543	    if ((cp >= word) &&
1544		(strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
1545		/*
1546		 * Match found. If we will place characters in the buffer,
1547		 * add a space before hand as indicated by addSpace, then
1548		 * stuff in the initial, unmatched part of the word followed
1549		 * by the right-hand-side.
1550		 */
1551		if (((cp - word) + pattern->rightLen) != 0) {
1552		    if (addSpace && vpstate->varSpace) {
1553			Buf_AddByte(buf, vpstate->varSpace);
1554		    }
1555		    addSpace = TRUE;
1556		}
1557		Buf_AddBytes(buf, cp - word, word);
1558		Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1559		pattern->flags |= VAR_SUB_MATCHED;
1560	    } else {
1561		/*
1562		 * Had to match at end and didn't. Copy entire word.
1563		 */
1564		goto nosub;
1565	    }
1566	} else {
1567	    /*
1568	     * Pattern is unanchored: search for the pattern in the word using
1569	     * String_FindSubstring, copying unmatched portions and the
1570	     * right-hand-side for each match found, handling non-global
1571	     * substitutions correctly, etc. When the loop is done, any
1572	     * remaining part of the word (word and wordLen are adjusted
1573	     * accordingly through the loop) is copied straight into the
1574	     * buffer.
1575	     * addSpace is set FALSE as soon as a space is added to the
1576	     * buffer.
1577	     */
1578	    Boolean done;
1579	    int origSize;
1580
1581	    done = FALSE;
1582	    origSize = Buf_Size(buf);
1583	    while (!done) {
1584		cp = Str_FindSubstring(word, pattern->lhs);
1585		if (cp != NULL) {
1586		    if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
1587			Buf_AddByte(buf, vpstate->varSpace);
1588			addSpace = FALSE;
1589		    }
1590		    Buf_AddBytes(buf, cp-word, word);
1591		    Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1592		    wordLen -= (cp - word) + pattern->leftLen;
1593		    word = cp + pattern->leftLen;
1594		    if (wordLen == 0) {
1595			done = TRUE;
1596		    }
1597		    if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
1598			done = TRUE;
1599		    }
1600		    pattern->flags |= VAR_SUB_MATCHED;
1601		} else {
1602		    done = TRUE;
1603		}
1604	    }
1605	    if (wordLen != 0) {
1606		if (addSpace && vpstate->varSpace) {
1607		    Buf_AddByte(buf, vpstate->varSpace);
1608		}
1609		Buf_AddBytes(buf, wordLen, word);
1610	    }
1611	    /*
1612	     * If added characters to the buffer, need to add a space
1613	     * before we add any more. If we didn't add any, just return
1614	     * the previous value of addSpace.
1615	     */
1616	    return ((Buf_Size(buf) != origSize) || addSpace);
1617	}
1618	return (addSpace);
1619    }
1620 nosub:
1621    if (addSpace && vpstate->varSpace) {
1622	Buf_AddByte(buf, vpstate->varSpace);
1623    }
1624    Buf_AddBytes(buf, wordLen, word);
1625    return(TRUE);
1626}
1627
1628#ifndef NO_REGEX
1629/*-
1630 *-----------------------------------------------------------------------
1631 * VarREError --
1632 *	Print the error caused by a regcomp or regexec call.
1633 *
1634 * Results:
1635 *	None.
1636 *
1637 * Side Effects:
1638 *	An error gets printed.
1639 *
1640 *-----------------------------------------------------------------------
1641 */
1642static void
1643VarREError(int errnum, regex_t *pat, const char *str)
1644{
1645    char *errbuf;
1646    int errlen;
1647
1648    errlen = regerror(errnum, pat, 0, 0);
1649    errbuf = bmake_malloc(errlen);
1650    regerror(errnum, pat, errbuf, errlen);
1651    Error("%s: %s", str, errbuf);
1652    free(errbuf);
1653}
1654
1655
1656/*-
1657 *-----------------------------------------------------------------------
1658 * VarRESubstitute --
1659 *	Perform a regex substitution on the given word, placing the
1660 *	result in the passed buffer.
1661 *
1662 * Results:
1663 *	TRUE if a space is needed before more characters are added.
1664 *
1665 * Side Effects:
1666 *	None.
1667 *
1668 *-----------------------------------------------------------------------
1669 */
1670static Boolean
1671VarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED,
1672		Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1673		char *word, Boolean addSpace, Buffer *buf,
1674		void *patternp)
1675{
1676    VarREPattern *pat;
1677    int xrv;
1678    char *wp;
1679    char *rp;
1680    int added;
1681    int flags = 0;
1682
1683#define MAYBE_ADD_SPACE()		\
1684	if (addSpace && !added)		\
1685	    Buf_AddByte(buf, ' ');	\
1686	added = 1
1687
1688    added = 0;
1689    wp = word;
1690    pat = patternp;
1691
1692    if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
1693	(VAR_SUB_ONE|VAR_SUB_MATCHED))
1694	xrv = REG_NOMATCH;
1695    else {
1696    tryagain:
1697	xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
1698    }
1699
1700    switch (xrv) {
1701    case 0:
1702	pat->flags |= VAR_SUB_MATCHED;
1703	if (pat->matches[0].rm_so > 0) {
1704	    MAYBE_ADD_SPACE();
1705	    Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
1706	}
1707
1708	for (rp = pat->replace; *rp; rp++) {
1709	    if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
1710		MAYBE_ADD_SPACE();
1711		Buf_AddByte(buf,rp[1]);
1712		rp++;
1713	    }
1714	    else if ((*rp == '&') ||
1715		((*rp == '\\') && isdigit((unsigned char)rp[1]))) {
1716		int n;
1717		const char *subbuf;
1718		int sublen;
1719		char errstr[3];
1720
1721		if (*rp == '&') {
1722		    n = 0;
1723		    errstr[0] = '&';
1724		    errstr[1] = '\0';
1725		} else {
1726		    n = rp[1] - '0';
1727		    errstr[0] = '\\';
1728		    errstr[1] = rp[1];
1729		    errstr[2] = '\0';
1730		    rp++;
1731		}
1732
1733		if (n > pat->nsub) {
1734		    Error("No subexpression %s", &errstr[0]);
1735		    subbuf = "";
1736		    sublen = 0;
1737		} else if ((pat->matches[n].rm_so == -1) &&
1738			   (pat->matches[n].rm_eo == -1)) {
1739		    Error("No match for subexpression %s", &errstr[0]);
1740		    subbuf = "";
1741		    sublen = 0;
1742	        } else {
1743		    subbuf = wp + pat->matches[n].rm_so;
1744		    sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
1745		}
1746
1747		if (sublen > 0) {
1748		    MAYBE_ADD_SPACE();
1749		    Buf_AddBytes(buf, sublen, subbuf);
1750		}
1751	    } else {
1752		MAYBE_ADD_SPACE();
1753		Buf_AddByte(buf, *rp);
1754	    }
1755	}
1756	wp += pat->matches[0].rm_eo;
1757	if (pat->flags & VAR_SUB_GLOBAL) {
1758	    flags |= REG_NOTBOL;
1759	    if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
1760		MAYBE_ADD_SPACE();
1761		Buf_AddByte(buf, *wp);
1762		wp++;
1763
1764	    }
1765	    if (*wp)
1766		goto tryagain;
1767	}
1768	if (*wp) {
1769	    MAYBE_ADD_SPACE();
1770	    Buf_AddBytes(buf, strlen(wp), wp);
1771	}
1772	break;
1773    default:
1774	VarREError(xrv, &pat->re, "Unexpected regex error");
1775       /* fall through */
1776    case REG_NOMATCH:
1777	if (*wp) {
1778	    MAYBE_ADD_SPACE();
1779	    Buf_AddBytes(buf,strlen(wp),wp);
1780	}
1781	break;
1782    }
1783    return(addSpace||added);
1784}
1785#endif
1786
1787
1788
1789/*-
1790 *-----------------------------------------------------------------------
1791 * VarLoopExpand --
1792 *	Implements the :@<temp>@<string>@ modifier of ODE make.
1793 *	We set the temp variable named in pattern.lhs to word and expand
1794 *	pattern.rhs storing the result in the passed buffer.
1795 *
1796 * Input:
1797 *	word		Word to modify
1798 *	addSpace	True if space should be added before
1799 *			other characters
1800 *	buf		Buffer for result
1801 *	pattern		Datafor substitution
1802 *
1803 * Results:
1804 *	TRUE if a space is needed before more characters are added.
1805 *
1806 * Side Effects:
1807 *	None.
1808 *
1809 *-----------------------------------------------------------------------
1810 */
1811static Boolean
1812VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED,
1813	      Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1814	      char *word, Boolean addSpace, Buffer *buf,
1815	      void *loopp)
1816{
1817    VarLoop_t	*loop = (VarLoop_t *)loopp;
1818    char *s;
1819    int slen;
1820
1821    if (word && *word) {
1822        Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
1823        s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum);
1824        if (s != NULL && *s != '\0') {
1825            if (addSpace && *s != '\n')
1826                Buf_AddByte(buf, ' ');
1827            Buf_AddBytes(buf, (slen = strlen(s)), s);
1828            addSpace = (slen > 0 && s[slen - 1] != '\n');
1829            free(s);
1830        }
1831    }
1832    return addSpace;
1833}
1834
1835
1836/*-
1837 *-----------------------------------------------------------------------
1838 * VarSelectWords --
1839 *	Implements the :[start..end] modifier.
1840 *	This is a special case of VarModify since we want to be able
1841 *	to scan the list backwards if start > end.
1842 *
1843 * Input:
1844 *	str		String whose words should be trimmed
1845 *	seldata		words to select
1846 *
1847 * Results:
1848 *	A string of all the words selected.
1849 *
1850 * Side Effects:
1851 *	None.
1852 *
1853 *-----------------------------------------------------------------------
1854 */
1855static char *
1856VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1857	       const char *str, VarSelectWords_t *seldata)
1858{
1859    Buffer  	  buf;		    /* Buffer for the new string */
1860    Boolean 	  addSpace; 	    /* TRUE if need to add a space to the
1861				     * buffer before adding the trimmed
1862				     * word */
1863    char **av;			    /* word list */
1864    char *as;			    /* word list memory */
1865    int ac, i;
1866    int start, end, step;
1867
1868    Buf_Init(&buf, 0);
1869    addSpace = FALSE;
1870
1871    if (vpstate->oneBigWord) {
1872	/* fake what brk_string() would do if there were only one word */
1873	ac = 1;
1874    	av = bmake_malloc((ac + 1) * sizeof(char *));
1875	as = bmake_strdup(str);
1876	av[0] = as;
1877	av[1] = NULL;
1878    } else {
1879	av = brk_string(str, &ac, FALSE, &as);
1880    }
1881
1882    /*
1883     * Now sanitize seldata.
1884     * If seldata->start or seldata->end are negative, convert them to
1885     * the positive equivalents (-1 gets converted to argc, -2 gets
1886     * converted to (argc-1), etc.).
1887     */
1888    if (seldata->start < 0)
1889	seldata->start = ac + seldata->start + 1;
1890    if (seldata->end < 0)
1891	seldata->end = ac + seldata->end + 1;
1892
1893    /*
1894     * We avoid scanning more of the list than we need to.
1895     */
1896    if (seldata->start > seldata->end) {
1897	start = MIN(ac, seldata->start) - 1;
1898	end = MAX(0, seldata->end - 1);
1899	step = -1;
1900    } else {
1901	start = MAX(0, seldata->start - 1);
1902	end = MIN(ac, seldata->end);
1903	step = 1;
1904    }
1905
1906    for (i = start;
1907	 (step < 0 && i >= end) || (step > 0 && i < end);
1908	 i += step) {
1909	if (av[i] && *av[i]) {
1910	    if (addSpace && vpstate->varSpace) {
1911		Buf_AddByte(&buf, vpstate->varSpace);
1912	    }
1913	    Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1914	    addSpace = TRUE;
1915	}
1916    }
1917
1918    free(as);
1919    free(av);
1920
1921    return Buf_Destroy(&buf, FALSE);
1922}
1923
1924
1925/*-
1926 * VarRealpath --
1927 *	Replace each word with the result of realpath()
1928 *	if successful.
1929 */
1930static Boolean
1931VarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1932	    char *word, Boolean addSpace, Buffer *buf,
1933	    void *patternp MAKE_ATTR_UNUSED)
1934{
1935	struct stat st;
1936	char rbuf[MAXPATHLEN];
1937	char *rp;
1938
1939	if (addSpace && vpstate->varSpace) {
1940	    Buf_AddByte(buf, vpstate->varSpace);
1941	}
1942	addSpace = TRUE;
1943	rp = realpath(word, rbuf);
1944	if (rp && *rp == '/' && stat(rp, &st) == 0)
1945		word = rp;
1946
1947	Buf_AddBytes(buf, strlen(word), word);
1948	return(addSpace);
1949}
1950
1951/*-
1952 *-----------------------------------------------------------------------
1953 * VarModify --
1954 *	Modify each of the words of the passed string using the given
1955 *	function. Used to implement all modifiers.
1956 *
1957 * Input:
1958 *	str		String whose words should be trimmed
1959 *	modProc		Function to use to modify them
1960 *	datum		Datum to pass it
1961 *
1962 * Results:
1963 *	A string of all the words modified appropriately.
1964 *
1965 * Side Effects:
1966 *	None.
1967 *
1968 *-----------------------------------------------------------------------
1969 */
1970static char *
1971VarModify(GNode *ctx, Var_Parse_State *vpstate,
1972    const char *str,
1973    Boolean (*modProc)(GNode *, Var_Parse_State *, char *,
1974		       Boolean, Buffer *, void *),
1975    void *datum)
1976{
1977    Buffer  	  buf;		    /* Buffer for the new string */
1978    Boolean 	  addSpace; 	    /* TRUE if need to add a space to the
1979				     * buffer before adding the trimmed
1980				     * word */
1981    char **av;			    /* word list */
1982    char *as;			    /* word list memory */
1983    int ac, i;
1984
1985    Buf_Init(&buf, 0);
1986    addSpace = FALSE;
1987
1988    if (vpstate->oneBigWord) {
1989	/* fake what brk_string() would do if there were only one word */
1990	ac = 1;
1991    	av = bmake_malloc((ac + 1) * sizeof(char *));
1992	as = bmake_strdup(str);
1993	av[0] = as;
1994	av[1] = NULL;
1995    } else {
1996	av = brk_string(str, &ac, FALSE, &as);
1997    }
1998
1999    for (i = 0; i < ac; i++) {
2000	addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum);
2001    }
2002
2003    free(as);
2004    free(av);
2005
2006    return Buf_Destroy(&buf, FALSE);
2007}
2008
2009
2010static int
2011VarWordCompare(const void *a, const void *b)
2012{
2013	int r = strcmp(*(const char * const *)a, *(const char * const *)b);
2014	return r;
2015}
2016
2017/*-
2018 *-----------------------------------------------------------------------
2019 * VarOrder --
2020 *	Order the words in the string.
2021 *
2022 * Input:
2023 *	str		String whose words should be sorted.
2024 *	otype		How to order: s - sort, x - random.
2025 *
2026 * Results:
2027 *	A string containing the words ordered.
2028 *
2029 * Side Effects:
2030 *	None.
2031 *
2032 *-----------------------------------------------------------------------
2033 */
2034static char *
2035VarOrder(const char *str, const char otype)
2036{
2037    Buffer  	  buf;		    /* Buffer for the new string */
2038    char **av;			    /* word list [first word does not count] */
2039    char *as;			    /* word list memory */
2040    int ac, i;
2041
2042    Buf_Init(&buf, 0);
2043
2044    av = brk_string(str, &ac, FALSE, &as);
2045
2046    if (ac > 0)
2047	switch (otype) {
2048	case 's':	/* sort alphabetically */
2049	    qsort(av, ac, sizeof(char *), VarWordCompare);
2050	    break;
2051	case 'x':	/* randomize */
2052	{
2053	    int rndidx;
2054	    char *t;
2055
2056	    /*
2057	     * We will use [ac..2] range for mod factors. This will produce
2058	     * random numbers in [(ac-1)..0] interval, and minimal
2059	     * reasonable value for mod factor is 2 (the mod 1 will produce
2060	     * 0 with probability 1).
2061	     */
2062	    for (i = ac-1; i > 0; i--) {
2063		rndidx = random() % (i + 1);
2064		if (i != rndidx) {
2065		    t = av[i];
2066		    av[i] = av[rndidx];
2067		    av[rndidx] = t;
2068		}
2069	    }
2070	}
2071	} /* end of switch */
2072
2073    for (i = 0; i < ac; i++) {
2074	Buf_AddBytes(&buf, strlen(av[i]), av[i]);
2075	if (i != ac - 1)
2076	    Buf_AddByte(&buf, ' ');
2077    }
2078
2079    free(as);
2080    free(av);
2081
2082    return Buf_Destroy(&buf, FALSE);
2083}
2084
2085
2086/*-
2087 *-----------------------------------------------------------------------
2088 * VarUniq --
2089 *	Remove adjacent duplicate words.
2090 *
2091 * Input:
2092 *	str		String whose words should be sorted
2093 *
2094 * Results:
2095 *	A string containing the resulting words.
2096 *
2097 * Side Effects:
2098 *	None.
2099 *
2100 *-----------------------------------------------------------------------
2101 */
2102static char *
2103VarUniq(const char *str)
2104{
2105    Buffer	  buf;		    /* Buffer for new string */
2106    char 	**av;		    /* List of words to affect */
2107    char 	 *as;		    /* Word list memory */
2108    int 	  ac, i, j;
2109
2110    Buf_Init(&buf, 0);
2111    av = brk_string(str, &ac, FALSE, &as);
2112
2113    if (ac > 1) {
2114	for (j = 0, i = 1; i < ac; i++)
2115	    if (strcmp(av[i], av[j]) != 0 && (++j != i))
2116		av[j] = av[i];
2117	ac = j + 1;
2118    }
2119
2120    for (i = 0; i < ac; i++) {
2121	Buf_AddBytes(&buf, strlen(av[i]), av[i]);
2122	if (i != ac - 1)
2123	    Buf_AddByte(&buf, ' ');
2124    }
2125
2126    free(as);
2127    free(av);
2128
2129    return Buf_Destroy(&buf, FALSE);
2130}
2131
2132
2133/*-
2134 *-----------------------------------------------------------------------
2135 * VarGetPattern --
2136 *	Pass through the tstr looking for 1) escaped delimiters,
2137 *	'$'s and backslashes (place the escaped character in
2138 *	uninterpreted) and 2) unescaped $'s that aren't before
2139 *	the delimiter (expand the variable substitution unless flags
2140 *	has VAR_NOSUBST set).
2141 *	Return the expanded string or NULL if the delimiter was missing
2142 *	If pattern is specified, handle escaped ampersands, and replace
2143 *	unescaped ampersands with the lhs of the pattern.
2144 *
2145 * Results:
2146 *	A string of all the words modified appropriately.
2147 *	If length is specified, return the string length of the buffer
2148 *	If flags is specified and the last character of the pattern is a
2149 *	$ set the VAR_MATCH_END bit of flags.
2150 *
2151 * Side Effects:
2152 *	None.
2153 *-----------------------------------------------------------------------
2154 */
2155static char *
2156VarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
2157	      int errnum, const char **tstr, int delim, int *flags,
2158	      int *length, VarPattern *pattern)
2159{
2160    const char *cp;
2161    char *rstr;
2162    Buffer buf;
2163    int junk;
2164
2165    Buf_Init(&buf, 0);
2166    if (length == NULL)
2167	length = &junk;
2168
2169#define IS_A_MATCH(cp, delim) \
2170    ((cp[0] == '\\') && ((cp[1] == delim) ||  \
2171     (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
2172
2173    /*
2174     * Skim through until the matching delimiter is found;
2175     * pick up variable substitutions on the way. Also allow
2176     * backslashes to quote the delimiter, $, and \, but don't
2177     * touch other backslashes.
2178     */
2179    for (cp = *tstr; *cp && (*cp != delim); cp++) {
2180	if (IS_A_MATCH(cp, delim)) {
2181	    Buf_AddByte(&buf, cp[1]);
2182	    cp++;
2183	} else if (*cp == '$') {
2184	    if (cp[1] == delim) {
2185		if (flags == NULL)
2186		    Buf_AddByte(&buf, *cp);
2187		else
2188		    /*
2189		     * Unescaped $ at end of pattern => anchor
2190		     * pattern at end.
2191		     */
2192		    *flags |= VAR_MATCH_END;
2193	    } else {
2194		if (flags == NULL || (*flags & VAR_NOSUBST) == 0) {
2195		    char   *cp2;
2196		    int     len;
2197		    void   *freeIt;
2198
2199		    /*
2200		     * If unescaped dollar sign not before the
2201		     * delimiter, assume it's a variable
2202		     * substitution and recurse.
2203		     */
2204		    cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt);
2205		    Buf_AddBytes(&buf, strlen(cp2), cp2);
2206		    if (freeIt)
2207			free(freeIt);
2208		    cp += len - 1;
2209		} else {
2210		    const char *cp2 = &cp[1];
2211
2212		    if (*cp2 == PROPEN || *cp2 == BROPEN) {
2213			/*
2214			 * Find the end of this variable reference
2215			 * and suck it in without further ado.
2216			 * It will be interperated later.
2217			 */
2218			int have = *cp2;
2219			int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE;
2220			int depth = 1;
2221
2222			for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
2223			    if (cp2[-1] != '\\') {
2224				if (*cp2 == have)
2225				    ++depth;
2226				if (*cp2 == want)
2227				    --depth;
2228			    }
2229			}
2230			Buf_AddBytes(&buf, cp2 - cp, cp);
2231			cp = --cp2;
2232		    } else
2233			Buf_AddByte(&buf, *cp);
2234		}
2235	    }
2236	}
2237	else if (pattern && *cp == '&')
2238	    Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs);
2239	else
2240	    Buf_AddByte(&buf, *cp);
2241    }
2242
2243    if (*cp != delim) {
2244	*tstr = cp;
2245	*length = 0;
2246	return NULL;
2247    }
2248
2249    *tstr = ++cp;
2250    *length = Buf_Size(&buf);
2251    rstr = Buf_Destroy(&buf, FALSE);
2252    if (DEBUG(VAR))
2253	fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr);
2254    return rstr;
2255}
2256
2257/*-
2258 *-----------------------------------------------------------------------
2259 * VarQuote --
2260 *	Quote shell meta-characters in the string
2261 *
2262 * Results:
2263 *	The quoted string
2264 *
2265 * Side Effects:
2266 *	None.
2267 *
2268 *-----------------------------------------------------------------------
2269 */
2270static char *
2271VarQuote(char *str)
2272{
2273
2274    Buffer  	  buf;
2275    /* This should cover most shells :-( */
2276    static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
2277    const char	*newline;
2278    size_t len, nlen;
2279
2280    if ((newline = Shell_GetNewline()) == NULL)
2281	    newline = "\\\n";
2282    nlen = strlen(newline);
2283
2284    Buf_Init(&buf, 0);
2285    while (*str != '\0') {
2286	if ((len = strcspn(str, meta)) != 0) {
2287	    Buf_AddBytes(&buf, len, str);
2288	    str += len;
2289	} else if (*str == '\n') {
2290	    Buf_AddBytes(&buf, nlen, newline);
2291	    ++str;
2292	} else {
2293	    Buf_AddByte(&buf, '\\');
2294	    Buf_AddByte(&buf, *str);
2295	    ++str;
2296	}
2297    }
2298    str = Buf_Destroy(&buf, FALSE);
2299    if (DEBUG(VAR))
2300	fprintf(debug_file, "QuoteMeta: [%s]\n", str);
2301    return str;
2302}
2303
2304/*-
2305 *-----------------------------------------------------------------------
2306 * VarHash --
2307 *      Hash the string using the MurmurHash3 algorithm.
2308 *      Output is computed using 32bit Little Endian arithmetic.
2309 *
2310 * Input:
2311 *	str		String to modify
2312 *
2313 * Results:
2314 *      Hash value of str, encoded as 8 hex digits.
2315 *
2316 * Side Effects:
2317 *      None.
2318 *
2319 *-----------------------------------------------------------------------
2320 */
2321static char *
2322VarHash(char *str)
2323{
2324    static const char    hexdigits[16] = "0123456789abcdef";
2325    Buffer         buf;
2326    size_t         len, len2;
2327    unsigned char  *ustr = (unsigned char *)str;
2328    uint32_t       h, k, c1, c2;
2329    int            done;
2330
2331    done = 1;
2332    h  = 0x971e137bU;
2333    c1 = 0x95543787U;
2334    c2 = 0x2ad7eb25U;
2335    len2 = strlen(str);
2336
2337    for (len = len2; len; ) {
2338	k = 0;
2339	switch (len) {
2340	default:
2341	    k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0];
2342	    len -= 4;
2343	    ustr += 4;
2344	    break;
2345	case 3:
2346	    k |= (ustr[2] << 16);
2347	case 2:
2348	    k |= (ustr[1] << 8);
2349	case 1:
2350	    k |= ustr[0];
2351	    len = 0;
2352	}
2353	c1 = c1 * 5 + 0x7b7d159cU;
2354	c2 = c2 * 5 + 0x6bce6396U;
2355	k *= c1;
2356	k = (k << 11) ^ (k >> 21);
2357	k *= c2;
2358	h = (h << 13) ^ (h >> 19);
2359	h = h * 5 + 0x52dce729U;
2360	h ^= k;
2361   } while (!done);
2362   h ^= len2;
2363   h *= 0x85ebca6b;
2364   h ^= h >> 13;
2365   h *= 0xc2b2ae35;
2366   h ^= h >> 16;
2367
2368   Buf_Init(&buf, 0);
2369   for (len = 0; len < 8; ++len) {
2370       Buf_AddByte(&buf, hexdigits[h & 15]);
2371       h >>= 4;
2372   }
2373
2374   return Buf_Destroy(&buf, FALSE);
2375}
2376
2377static char *
2378VarStrftime(const char *fmt, int zulu)
2379{
2380    char buf[BUFSIZ];
2381    time_t utc;
2382
2383    time(&utc);
2384    if (!*fmt)
2385	fmt = "%c";
2386    strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc));
2387
2388    buf[sizeof(buf) - 1] = '\0';
2389    return bmake_strdup(buf);
2390}
2391
2392/*
2393 * Now we need to apply any modifiers the user wants applied.
2394 * These are:
2395 *  	  :M<pattern>	words which match the given <pattern>.
2396 *  			<pattern> is of the standard file
2397 *  			wildcarding form.
2398 *  	  :N<pattern>	words which do not match the given <pattern>.
2399 *  	  :S<d><pat1><d><pat2><d>[1gW]
2400 *  			Substitute <pat2> for <pat1> in the value
2401 *  	  :C<d><pat1><d><pat2><d>[1gW]
2402 *  			Substitute <pat2> for regex <pat1> in the value
2403 *  	  :H		Substitute the head of each word
2404 *  	  :T		Substitute the tail of each word
2405 *  	  :E		Substitute the extension (minus '.') of
2406 *  			each word
2407 *  	  :R		Substitute the root of each word
2408 *  			(pathname minus the suffix).
2409 *	  :O		("Order") Alphabeticaly sort words in variable.
2410 *	  :Ox		("intermiX") Randomize words in variable.
2411 *	  :u		("uniq") Remove adjacent duplicate words.
2412 *	  :tu		Converts the variable contents to uppercase.
2413 *	  :tl		Converts the variable contents to lowercase.
2414 *	  :ts[c]	Sets varSpace - the char used to
2415 *			separate words to 'c'. If 'c' is
2416 *			omitted then no separation is used.
2417 *	  :tW		Treat the variable contents as a single
2418 *			word, even if it contains spaces.
2419 *			(Mnemonic: one big 'W'ord.)
2420 *	  :tw		Treat the variable contents as multiple
2421 *			space-separated words.
2422 *			(Mnemonic: many small 'w'ords.)
2423 *	  :[index]	Select a single word from the value.
2424 *	  :[start..end]	Select multiple words from the value.
2425 *	  :[*] or :[0]	Select the entire value, as a single
2426 *			word.  Equivalent to :tW.
2427 *	  :[@]		Select the entire value, as multiple
2428 *			words.	Undoes the effect of :[*].
2429 *			Equivalent to :tw.
2430 *	  :[#]		Returns the number of words in the value.
2431 *
2432 *	  :?<true-value>:<false-value>
2433 *			If the variable evaluates to true, return
2434 *			true value, else return the second value.
2435 *    	  :lhs=rhs  	Like :S, but the rhs goes to the end of
2436 *    			the invocation.
2437 *	  :sh		Treat the current value as a command
2438 *			to be run, new value is its output.
2439 * The following added so we can handle ODE makefiles.
2440 *	  :@<tmpvar>@<newval>@
2441 *			Assign a temporary local variable <tmpvar>
2442 *			to the current value of each word in turn
2443 *			and replace each word with the result of
2444 *			evaluating <newval>
2445 *	  :D<newval>	Use <newval> as value if variable defined
2446 *	  :U<newval>	Use <newval> as value if variable undefined
2447 *	  :L		Use the name of the variable as the value.
2448 *	  :P		Use the path of the node that has the same
2449 *			name as the variable as the value.  This
2450 *			basically includes an implied :L so that
2451 *			the common method of refering to the path
2452 *			of your dependent 'x' in a rule is to use
2453 *			the form '${x:P}'.
2454 *	  :!<cmd>!	Run cmd much the same as :sh run's the
2455 *			current value of the variable.
2456 * The ::= modifiers, actually assign a value to the variable.
2457 * Their main purpose is in supporting modifiers of .for loop
2458 * iterators and other obscure uses.  They always expand to
2459 * nothing.  In a target rule that would otherwise expand to an
2460 * empty line they can be preceded with @: to keep make happy.
2461 * Eg.
2462 *
2463 * foo:	.USE
2464 * .for i in ${.TARGET} ${.TARGET:R}.gz
2465 * 	@: ${t::=$i}
2466 *	@echo blah ${t:T}
2467 * .endfor
2468 *
2469 *	  ::=<str>	Assigns <str> as the new value of variable.
2470 *	  ::?=<str>	Assigns <str> as value of variable if
2471 *			it was not already set.
2472 *	  ::+=<str>	Appends <str> to variable.
2473 *	  ::!=<cmd>	Assigns output of <cmd> as the new value of
2474 *			variable.
2475 */
2476
2477/* we now have some modifiers with long names */
2478#define STRMOD_MATCH(s, want, n) \
2479    (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':'))
2480
2481static char *
2482ApplyModifiers(char *nstr, const char *tstr,
2483	       int startc, int endc,
2484	       Var *v, GNode *ctxt, Boolean errnum,
2485	       int *lengthPtr, void **freePtr)
2486{
2487    const char 	   *start;
2488    const char     *cp;    	/* Secondary pointer into str (place marker
2489				 * for tstr) */
2490    char	   *newStr;	/* New value to return */
2491    char	    termc;	/* Character which terminated scan */
2492    int             cnt;	/* Used to count brace pairs when variable in
2493				 * in parens or braces */
2494    char	delim;
2495    int		modifier;	/* that we are processing */
2496    Var_Parse_State parsestate; /* Flags passed to helper functions */
2497
2498    delim = '\0';
2499    parsestate.oneBigWord = FALSE;
2500    parsestate.varSpace = ' ';	/* word separator */
2501
2502    start = cp = tstr;
2503
2504    while (*tstr && *tstr != endc) {
2505
2506	if (*tstr == '$') {
2507	    /*
2508	     * We may have some complex modifiers in a variable.
2509	     */
2510	    void *freeIt;
2511	    char *rval;
2512	    int rlen;
2513	    int c;
2514
2515	    rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt);
2516
2517	    /*
2518	     * If we have not parsed up to endc or ':',
2519	     * we are not interested.
2520	     */
2521	    if (rval != NULL && *rval &&
2522		(c = tstr[rlen]) != '\0' &&
2523		c != ':' &&
2524		c != endc) {
2525		if (freeIt)
2526		    free(freeIt);
2527		goto apply_mods;
2528	    }
2529
2530	    if (DEBUG(VAR)) {
2531		fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n",
2532		       rval, rlen, tstr, rlen, tstr + rlen);
2533	    }
2534
2535	    tstr += rlen;
2536
2537	    if (rval != NULL && *rval) {
2538		int used;
2539
2540		nstr = ApplyModifiers(nstr, rval,
2541				      0, 0,
2542				      v, ctxt, errnum, &used, freePtr);
2543		if (nstr == var_Error
2544		    || (nstr == varNoError && errnum == 0)
2545		    || strlen(rval) != (size_t) used) {
2546		    if (freeIt)
2547			free(freeIt);
2548		    goto out;		/* error already reported */
2549		}
2550	    }
2551	    if (freeIt)
2552		free(freeIt);
2553	    if (*tstr == ':')
2554		tstr++;
2555	    else if (!*tstr && endc) {
2556		Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name);
2557		goto out;
2558	    }
2559	    continue;
2560	}
2561    apply_mods:
2562	if (DEBUG(VAR)) {
2563	    fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name,
2564		*tstr, nstr);
2565	}
2566	newStr = var_Error;
2567	switch ((modifier = *tstr)) {
2568	case ':':
2569	    {
2570		if (tstr[1] == '=' ||
2571		    (tstr[2] == '=' &&
2572		     (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) {
2573		    /*
2574		     * "::=", "::!=", "::+=", or "::?="
2575		     */
2576		    GNode *v_ctxt;		/* context where v belongs */
2577		    const char *emsg;
2578		    char *sv_name;
2579		    VarPattern	pattern;
2580		    int	how;
2581
2582		    if (v->name[0] == 0)
2583			goto bad_modifier;
2584
2585		    v_ctxt = ctxt;
2586		    sv_name = NULL;
2587		    ++tstr;
2588		    if (v->flags & VAR_JUNK) {
2589			/*
2590			 * We need to bmake_strdup() it incase
2591			 * VarGetPattern() recurses.
2592			 */
2593			sv_name = v->name;
2594			v->name = bmake_strdup(v->name);
2595		    } else if (ctxt != VAR_GLOBAL) {
2596			Var *gv = VarFind(v->name, ctxt, 0);
2597			if (gv == NULL)
2598			    v_ctxt = VAR_GLOBAL;
2599			else
2600			    VarFreeEnv(gv, TRUE);
2601		    }
2602
2603		    switch ((how = *tstr)) {
2604		    case '+':
2605		    case '?':
2606		    case '!':
2607			cp = &tstr[2];
2608			break;
2609		    default:
2610			cp = ++tstr;
2611			break;
2612		    }
2613		    delim = startc == PROPEN ? PRCLOSE : BRCLOSE;
2614		    pattern.flags = 0;
2615
2616		    pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum,
2617						&cp, delim, NULL,
2618						&pattern.rightLen,
2619						NULL);
2620		    if (v->flags & VAR_JUNK) {
2621			/* restore original name */
2622			free(v->name);
2623			v->name = sv_name;
2624		    }
2625		    if (pattern.rhs == NULL)
2626			goto cleanup;
2627
2628		    termc = *--cp;
2629		    delim = '\0';
2630
2631		    switch (how) {
2632		    case '+':
2633			Var_Append(v->name, pattern.rhs, v_ctxt);
2634			break;
2635		    case '!':
2636			newStr = Cmd_Exec(pattern.rhs, &emsg);
2637			if (emsg)
2638			    Error(emsg, nstr);
2639			else
2640			    Var_Set(v->name, newStr,  v_ctxt, 0);
2641			if (newStr)
2642			    free(newStr);
2643			break;
2644		    case '?':
2645			if ((v->flags & VAR_JUNK) == 0)
2646			    break;
2647			/* FALLTHROUGH */
2648		    default:
2649			Var_Set(v->name, pattern.rhs, v_ctxt, 0);
2650			break;
2651		    }
2652		    free(UNCONST(pattern.rhs));
2653		    newStr = var_Error;
2654		    break;
2655		}
2656		goto default_case; /* "::<unrecognised>" */
2657	    }
2658	case '@':
2659	    {
2660		VarLoop_t	loop;
2661		int flags = VAR_NOSUBST;
2662
2663		cp = ++tstr;
2664		delim = '@';
2665		if ((loop.tvar = VarGetPattern(ctxt, &parsestate, errnum,
2666					       &cp, delim,
2667					       &flags, &loop.tvarLen,
2668					       NULL)) == NULL)
2669		    goto cleanup;
2670
2671		if ((loop.str = VarGetPattern(ctxt, &parsestate, errnum,
2672					      &cp, delim,
2673					      &flags, &loop.strLen,
2674					      NULL)) == NULL)
2675		    goto cleanup;
2676
2677		termc = *cp;
2678		delim = '\0';
2679
2680		loop.errnum = errnum;
2681		loop.ctxt = ctxt;
2682		newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand,
2683				   &loop);
2684		free(loop.tvar);
2685		free(loop.str);
2686		break;
2687	    }
2688	case 'U':
2689#ifdef MAKE_FREEBSD_UL
2690	    if (FreeBSD_UL) {
2691		int nc = tstr[1];
2692
2693		/* we have to be careful, since :U is used internally */
2694		if (nc == ':' || nc == endc) {
2695		    char *dp = bmake_strdup(nstr);
2696		    for (newStr = dp; *dp; dp++)
2697			*dp = toupper((unsigned char)*dp);
2698		    cp = tstr + 1;
2699		    termc = *cp;
2700		    break;		/* yes inside the conditional */
2701		}
2702		/* FALLTHROUGH */
2703	    }
2704#endif
2705	case 'D':
2706	    {
2707		Buffer  buf;    	/* Buffer for patterns */
2708		int	    wantit;	/* want data in buffer */
2709
2710		/*
2711		 * Pass through tstr looking for 1) escaped delimiters,
2712		 * '$'s and backslashes (place the escaped character in
2713		 * uninterpreted) and 2) unescaped $'s that aren't before
2714		 * the delimiter (expand the variable substitution).
2715		 * The result is left in the Buffer buf.
2716		 */
2717		Buf_Init(&buf, 0);
2718		for (cp = tstr + 1;
2719		     *cp != endc && *cp != ':' && *cp != '\0';
2720		     cp++) {
2721		    if ((*cp == '\\') &&
2722			((cp[1] == ':') ||
2723			 (cp[1] == '$') ||
2724			 (cp[1] == endc) ||
2725			 (cp[1] == '\\')))
2726			{
2727			    Buf_AddByte(&buf, cp[1]);
2728			    cp++;
2729			} else if (*cp == '$') {
2730			    /*
2731			     * If unescaped dollar sign, assume it's a
2732			     * variable substitution and recurse.
2733			     */
2734			    char    *cp2;
2735			    int	    len;
2736			    void    *freeIt;
2737
2738			    cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt);
2739			    Buf_AddBytes(&buf, strlen(cp2), cp2);
2740			    if (freeIt)
2741				free(freeIt);
2742			    cp += len - 1;
2743			} else {
2744			    Buf_AddByte(&buf, *cp);
2745			}
2746		}
2747
2748		termc = *cp;
2749
2750		if (*tstr == 'U')
2751		    wantit = ((v->flags & VAR_JUNK) != 0);
2752		else
2753		    wantit = ((v->flags & VAR_JUNK) == 0);
2754		if ((v->flags & VAR_JUNK) != 0)
2755		    v->flags |= VAR_KEEP;
2756		if (wantit) {
2757		    newStr = Buf_Destroy(&buf, FALSE);
2758		} else {
2759		    newStr = nstr;
2760		    Buf_Destroy(&buf, TRUE);
2761		}
2762		break;
2763	    }
2764	case 'L':
2765#ifdef MAKE_FREEBSD_UL
2766	    if (FreeBSD_UL) {
2767		char *dp = bmake_strdup(nstr);
2768		for (newStr = dp; *dp; dp++)
2769		    *dp = tolower((unsigned char)*dp);
2770		cp = tstr + 1;
2771		termc = *cp;
2772		break;
2773	    }
2774	    /* FALLTHROUGH */
2775#endif
2776	    {
2777		if ((v->flags & VAR_JUNK) != 0)
2778		    v->flags |= VAR_KEEP;
2779		newStr = bmake_strdup(v->name);
2780		cp = ++tstr;
2781		termc = *tstr;
2782		break;
2783	    }
2784	case 'P':
2785	    {
2786		GNode *gn;
2787
2788		if ((v->flags & VAR_JUNK) != 0)
2789		    v->flags |= VAR_KEEP;
2790		gn = Targ_FindNode(v->name, TARG_NOCREATE);
2791		if (gn == NULL || gn->type & OP_NOPATH) {
2792		    newStr = NULL;
2793		} else if (gn->path) {
2794		    newStr = bmake_strdup(gn->path);
2795		} else {
2796		    newStr = Dir_FindFile(v->name, Suff_FindPath(gn));
2797		}
2798		if (!newStr) {
2799		    newStr = bmake_strdup(v->name);
2800		}
2801		cp = ++tstr;
2802		termc = *tstr;
2803		break;
2804	    }
2805	case '!':
2806	    {
2807		const char *emsg;
2808		VarPattern 	    pattern;
2809		pattern.flags = 0;
2810
2811		delim = '!';
2812
2813		cp = ++tstr;
2814		if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum,
2815						 &cp, delim,
2816						 NULL, &pattern.rightLen,
2817						 NULL)) == NULL)
2818		    goto cleanup;
2819		newStr = Cmd_Exec(pattern.rhs, &emsg);
2820		free(UNCONST(pattern.rhs));
2821		if (emsg)
2822		    Error(emsg, nstr);
2823		termc = *cp;
2824		delim = '\0';
2825		if (v->flags & VAR_JUNK) {
2826		    v->flags |= VAR_KEEP;
2827		}
2828		break;
2829	    }
2830	case '[':
2831	    {
2832		/*
2833		 * Look for the closing ']', recursively
2834		 * expanding any embedded variables.
2835		 *
2836		 * estr is a pointer to the expanded result,
2837		 * which we must free().
2838		 */
2839		char *estr;
2840
2841		cp = tstr+1; /* point to char after '[' */
2842		delim = ']'; /* look for closing ']' */
2843		estr = VarGetPattern(ctxt, &parsestate,
2844				     errnum, &cp, delim,
2845				     NULL, NULL, NULL);
2846		if (estr == NULL)
2847		    goto cleanup; /* report missing ']' */
2848		/* now cp points just after the closing ']' */
2849		delim = '\0';
2850		if (cp[0] != ':' && cp[0] != endc) {
2851		    /* Found junk after ']' */
2852		    free(estr);
2853		    goto bad_modifier;
2854		}
2855		if (estr[0] == '\0') {
2856		    /* Found empty square brackets in ":[]". */
2857		    free(estr);
2858		    goto bad_modifier;
2859		} else if (estr[0] == '#' && estr[1] == '\0') {
2860		    /* Found ":[#]" */
2861
2862		    /*
2863		     * We will need enough space for the decimal
2864		     * representation of an int.  We calculate the
2865		     * space needed for the octal representation,
2866		     * and add enough slop to cope with a '-' sign
2867		     * (which should never be needed) and a '\0'
2868		     * string terminator.
2869		     */
2870		    int newStrSize =
2871			(sizeof(int) * CHAR_BIT + 2) / 3 + 2;
2872
2873		    newStr = bmake_malloc(newStrSize);
2874		    if (parsestate.oneBigWord) {
2875			strncpy(newStr, "1", newStrSize);
2876		    } else {
2877			/* XXX: brk_string() is a rather expensive
2878			 * way of counting words. */
2879			char **av;
2880			char *as;
2881			int ac;
2882
2883			av = brk_string(nstr, &ac, FALSE, &as);
2884			snprintf(newStr, newStrSize,  "%d", ac);
2885			free(as);
2886			free(av);
2887		    }
2888		    termc = *cp;
2889		    free(estr);
2890		    break;
2891		} else if (estr[0] == '*' && estr[1] == '\0') {
2892		    /* Found ":[*]" */
2893		    parsestate.oneBigWord = TRUE;
2894		    newStr = nstr;
2895		    termc = *cp;
2896		    free(estr);
2897		    break;
2898		} else if (estr[0] == '@' && estr[1] == '\0') {
2899		    /* Found ":[@]" */
2900		    parsestate.oneBigWord = FALSE;
2901		    newStr = nstr;
2902		    termc = *cp;
2903		    free(estr);
2904		    break;
2905		} else {
2906		    /*
2907		     * We expect estr to contain a single
2908		     * integer for :[N], or two integers
2909		     * separated by ".." for :[start..end].
2910		     */
2911		    char *ep;
2912
2913		    VarSelectWords_t seldata = { 0, 0 };
2914
2915		    seldata.start = strtol(estr, &ep, 0);
2916		    if (ep == estr) {
2917			/* Found junk instead of a number */
2918			free(estr);
2919			goto bad_modifier;
2920		    } else if (ep[0] == '\0') {
2921			/* Found only one integer in :[N] */
2922			seldata.end = seldata.start;
2923		    } else if (ep[0] == '.' && ep[1] == '.' &&
2924			       ep[2] != '\0') {
2925			/* Expecting another integer after ".." */
2926			ep += 2;
2927			seldata.end = strtol(ep, &ep, 0);
2928			if (ep[0] != '\0') {
2929			    /* Found junk after ".." */
2930			    free(estr);
2931			    goto bad_modifier;
2932			}
2933		    } else {
2934			/* Found junk instead of ".." */
2935			free(estr);
2936			goto bad_modifier;
2937		    }
2938		    /*
2939		     * Now seldata is properly filled in,
2940		     * but we still have to check for 0 as
2941		     * a special case.
2942		     */
2943		    if (seldata.start == 0 && seldata.end == 0) {
2944			/* ":[0]" or perhaps ":[0..0]" */
2945			parsestate.oneBigWord = TRUE;
2946			newStr = nstr;
2947			termc = *cp;
2948			free(estr);
2949			break;
2950		    } else if (seldata.start == 0 ||
2951			       seldata.end == 0) {
2952			/* ":[0..N]" or ":[N..0]" */
2953			free(estr);
2954			goto bad_modifier;
2955		    }
2956		    /*
2957		     * Normal case: select the words
2958		     * described by seldata.
2959		     */
2960		    newStr = VarSelectWords(ctxt, &parsestate,
2961					    nstr, &seldata);
2962
2963		    termc = *cp;
2964		    free(estr);
2965		    break;
2966		}
2967
2968	    }
2969	case 'g':
2970	    cp = tstr + 1;	/* make sure it is set */
2971	    if (STRMOD_MATCH(tstr, "gmtime", 6)) {
2972		newStr = VarStrftime(nstr, 1);
2973		cp = tstr + 6;
2974		termc = *cp;
2975	    } else {
2976		goto default_case;
2977	    }
2978	    break;
2979	case 'h':
2980	    cp = tstr + 1;	/* make sure it is set */
2981	    if (STRMOD_MATCH(tstr, "hash", 4)) {
2982		newStr = VarHash(nstr);
2983		cp = tstr + 4;
2984		termc = *cp;
2985	    } else {
2986		goto default_case;
2987	    }
2988	    break;
2989	case 'l':
2990	    cp = tstr + 1;	/* make sure it is set */
2991	    if (STRMOD_MATCH(tstr, "localtime", 9)) {
2992		newStr = VarStrftime(nstr, 0);
2993		cp = tstr + 9;
2994		termc = *cp;
2995	    } else {
2996		goto default_case;
2997	    }
2998	    break;
2999	case 't':
3000	    {
3001		cp = tstr + 1;	/* make sure it is set */
3002		if (tstr[1] != endc && tstr[1] != ':') {
3003		    if (tstr[1] == 's') {
3004			/*
3005			 * Use the char (if any) at tstr[2]
3006			 * as the word separator.
3007			 */
3008			VarPattern pattern;
3009
3010			if (tstr[2] != endc &&
3011			    (tstr[3] == endc || tstr[3] == ':')) {
3012			    /* ":ts<unrecognised><endc>" or
3013			     * ":ts<unrecognised>:" */
3014			    parsestate.varSpace = tstr[2];
3015			    cp = tstr + 3;
3016			} else if (tstr[2] == endc || tstr[2] == ':') {
3017			    /* ":ts<endc>" or ":ts:" */
3018			    parsestate.varSpace = 0; /* no separator */
3019			    cp = tstr + 2;
3020			} else if (tstr[2] == '\\') {
3021			    switch (tstr[3]) {
3022			    case 'n':
3023				parsestate.varSpace = '\n';
3024				cp = tstr + 4;
3025				break;
3026			    case 't':
3027				parsestate.varSpace = '\t';
3028				cp = tstr + 4;
3029				break;
3030			    default:
3031				if (isdigit((unsigned char)tstr[3])) {
3032				    char *ep;
3033
3034				    parsestate.varSpace =
3035					strtoul(&tstr[3], &ep, 0);
3036				    if (*ep != ':' && *ep != endc)
3037					goto bad_modifier;
3038				    cp = ep;
3039				} else {
3040				    /*
3041				     * ":ts<backslash><unrecognised>".
3042				     */
3043				    goto bad_modifier;
3044				}
3045				break;
3046			    }
3047			} else {
3048			    /*
3049			     * Found ":ts<unrecognised><unrecognised>".
3050			     */
3051			    goto bad_modifier;
3052			}
3053
3054			termc = *cp;
3055
3056			/*
3057			 * We cannot be certain that VarModify
3058			 * will be used - even if there is a
3059			 * subsequent modifier, so do a no-op
3060			 * VarSubstitute now to for str to be
3061			 * re-expanded without the spaces.
3062			 */
3063			pattern.flags = VAR_SUB_ONE;
3064			pattern.lhs = pattern.rhs = "\032";
3065			pattern.leftLen = pattern.rightLen = 1;
3066
3067			newStr = VarModify(ctxt, &parsestate, nstr,
3068					   VarSubstitute,
3069					   &pattern);
3070		    } else if (tstr[2] == endc || tstr[2] == ':') {
3071			/*
3072			 * Check for two-character options:
3073			 * ":tu", ":tl"
3074			 */
3075			if (tstr[1] == 'A') { /* absolute path */
3076			    newStr = VarModify(ctxt, &parsestate, nstr,
3077					       VarRealpath, NULL);
3078			    cp = tstr + 2;
3079			    termc = *cp;
3080			} else if (tstr[1] == 'u') {
3081			    char *dp = bmake_strdup(nstr);
3082			    for (newStr = dp; *dp; dp++)
3083				*dp = toupper((unsigned char)*dp);
3084			    cp = tstr + 2;
3085			    termc = *cp;
3086			} else if (tstr[1] == 'l') {
3087			    char *dp = bmake_strdup(nstr);
3088			    for (newStr = dp; *dp; dp++)
3089				*dp = tolower((unsigned char)*dp);
3090			    cp = tstr + 2;
3091			    termc = *cp;
3092			} else if (tstr[1] == 'W' || tstr[1] == 'w') {
3093			    parsestate.oneBigWord = (tstr[1] == 'W');
3094			    newStr = nstr;
3095			    cp = tstr + 2;
3096			    termc = *cp;
3097			} else {
3098			    /* Found ":t<unrecognised>:" or
3099			     * ":t<unrecognised><endc>". */
3100			    goto bad_modifier;
3101			}
3102		    } else {
3103			/*
3104			 * Found ":t<unrecognised><unrecognised>".
3105			 */
3106			goto bad_modifier;
3107		    }
3108		} else {
3109		    /*
3110		     * Found ":t<endc>" or ":t:".
3111		     */
3112		    goto bad_modifier;
3113		}
3114		break;
3115	    }
3116	case 'N':
3117	case 'M':
3118	    {
3119		char    *pattern;
3120		const char *endpat; /* points just after end of pattern */
3121		char    *cp2;
3122		Boolean copy;	/* pattern should be, or has been, copied */
3123		Boolean needSubst;
3124		int nest;
3125
3126		copy = FALSE;
3127		needSubst = FALSE;
3128		nest = 1;
3129		/*
3130		 * In the loop below, ignore ':' unless we are at
3131		 * (or back to) the original brace level.
3132		 * XXX This will likely not work right if $() and ${}
3133		 * are intermixed.
3134		 */
3135		for (cp = tstr + 1;
3136		     *cp != '\0' && !(*cp == ':' && nest == 1);
3137		     cp++)
3138		    {
3139			if (*cp == '\\' &&
3140			    (cp[1] == ':' ||
3141			     cp[1] == endc || cp[1] == startc)) {
3142			    if (!needSubst) {
3143				copy = TRUE;
3144			    }
3145			    cp++;
3146			    continue;
3147			}
3148			if (*cp == '$') {
3149			    needSubst = TRUE;
3150			}
3151			if (*cp == '(' || *cp == '{')
3152			    ++nest;
3153			if (*cp == ')' || *cp == '}') {
3154			    --nest;
3155			    if (nest == 0)
3156				break;
3157			}
3158		    }
3159		termc = *cp;
3160		endpat = cp;
3161		if (copy) {
3162		    /*
3163		     * Need to compress the \:'s out of the pattern, so
3164		     * allocate enough room to hold the uncompressed
3165		     * pattern (note that cp started at tstr+1, so
3166		     * cp - tstr takes the null byte into account) and
3167		     * compress the pattern into the space.
3168		     */
3169		    pattern = bmake_malloc(cp - tstr);
3170		    for (cp2 = pattern, cp = tstr + 1;
3171			 cp < endpat;
3172			 cp++, cp2++)
3173			{
3174			    if ((*cp == '\\') && (cp+1 < endpat) &&
3175				(cp[1] == ':' || cp[1] == endc)) {
3176				cp++;
3177			    }
3178			    *cp2 = *cp;
3179			}
3180		    *cp2 = '\0';
3181		    endpat = cp2;
3182		} else {
3183		    /*
3184		     * Either Var_Subst or VarModify will need a
3185		     * nul-terminated string soon, so construct one now.
3186		     */
3187		    pattern = bmake_strndup(tstr+1, endpat - (tstr + 1));
3188		}
3189		if (needSubst) {
3190		    /*
3191		     * pattern contains embedded '$', so use Var_Subst to
3192		     * expand it.
3193		     */
3194		    cp2 = pattern;
3195		    pattern = Var_Subst(NULL, cp2, ctxt, errnum);
3196		    free(cp2);
3197		}
3198		if (DEBUG(VAR))
3199		    fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n",
3200			v->name, nstr, pattern);
3201		if (*tstr == 'M') {
3202		    newStr = VarModify(ctxt, &parsestate, nstr, VarMatch,
3203				       pattern);
3204		} else {
3205		    newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch,
3206				       pattern);
3207		}
3208		free(pattern);
3209		break;
3210	    }
3211	case 'S':
3212	    {
3213		VarPattern 	    pattern;
3214		Var_Parse_State tmpparsestate;
3215
3216		pattern.flags = 0;
3217		tmpparsestate = parsestate;
3218		delim = tstr[1];
3219		tstr += 2;
3220
3221		/*
3222		 * If pattern begins with '^', it is anchored to the
3223		 * start of the word -- skip over it and flag pattern.
3224		 */
3225		if (*tstr == '^') {
3226		    pattern.flags |= VAR_MATCH_START;
3227		    tstr += 1;
3228		}
3229
3230		cp = tstr;
3231		if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum,
3232						 &cp, delim,
3233						 &pattern.flags,
3234						 &pattern.leftLen,
3235						 NULL)) == NULL)
3236		    goto cleanup;
3237
3238		if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum,
3239						 &cp, delim, NULL,
3240						 &pattern.rightLen,
3241						 &pattern)) == NULL)
3242		    goto cleanup;
3243
3244		/*
3245		 * Check for global substitution. If 'g' after the final
3246		 * delimiter, substitution is global and is marked that
3247		 * way.
3248		 */
3249		for (;; cp++) {
3250		    switch (*cp) {
3251		    case 'g':
3252			pattern.flags |= VAR_SUB_GLOBAL;
3253			continue;
3254		    case '1':
3255			pattern.flags |= VAR_SUB_ONE;
3256			continue;
3257		    case 'W':
3258			tmpparsestate.oneBigWord = TRUE;
3259			continue;
3260		    }
3261		    break;
3262		}
3263
3264		termc = *cp;
3265		newStr = VarModify(ctxt, &tmpparsestate, nstr,
3266				   VarSubstitute,
3267				   &pattern);
3268
3269		/*
3270		 * Free the two strings.
3271		 */
3272		free(UNCONST(pattern.lhs));
3273		free(UNCONST(pattern.rhs));
3274		delim = '\0';
3275		break;
3276	    }
3277	case '?':
3278	    {
3279		VarPattern 	pattern;
3280		Boolean	value;
3281
3282		/* find ':', and then substitute accordingly */
3283
3284		pattern.flags = 0;
3285
3286		cp = ++tstr;
3287		delim = ':';
3288		if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum,
3289						 &cp, delim, NULL,
3290						 &pattern.leftLen,
3291						 NULL)) == NULL)
3292		    goto cleanup;
3293
3294		/* BROPEN or PROPEN */
3295		delim = endc;
3296		if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum,
3297						 &cp, delim, NULL,
3298						 &pattern.rightLen,
3299						 NULL)) == NULL)
3300		    goto cleanup;
3301
3302		termc = *--cp;
3303		delim = '\0';
3304		if (Cond_EvalExpression(NULL, v->name, &value, 0)
3305		    == COND_INVALID) {
3306		    Error("Bad conditional expression `%s' in %s?%s:%s",
3307			  v->name, v->name, pattern.lhs, pattern.rhs);
3308		    goto cleanup;
3309		}
3310
3311		if (value) {
3312		    newStr = UNCONST(pattern.lhs);
3313		    free(UNCONST(pattern.rhs));
3314		} else {
3315		    newStr = UNCONST(pattern.rhs);
3316		    free(UNCONST(pattern.lhs));
3317		}
3318		if (v->flags & VAR_JUNK) {
3319		    v->flags |= VAR_KEEP;
3320		}
3321		break;
3322	    }
3323#ifndef NO_REGEX
3324	case 'C':
3325	    {
3326		VarREPattern    pattern;
3327		char           *re;
3328		int             error;
3329		Var_Parse_State tmpparsestate;
3330
3331		pattern.flags = 0;
3332		tmpparsestate = parsestate;
3333		delim = tstr[1];
3334		tstr += 2;
3335
3336		cp = tstr;
3337
3338		if ((re = VarGetPattern(ctxt, &parsestate, errnum, &cp, delim,
3339					NULL, NULL, NULL)) == NULL)
3340		    goto cleanup;
3341
3342		if ((pattern.replace = VarGetPattern(ctxt, &parsestate,
3343						     errnum, &cp, delim, NULL,
3344						     NULL, NULL)) == NULL){
3345		    free(re);
3346		    goto cleanup;
3347		}
3348
3349		for (;; cp++) {
3350		    switch (*cp) {
3351		    case 'g':
3352			pattern.flags |= VAR_SUB_GLOBAL;
3353			continue;
3354		    case '1':
3355			pattern.flags |= VAR_SUB_ONE;
3356			continue;
3357		    case 'W':
3358			tmpparsestate.oneBigWord = TRUE;
3359			continue;
3360		    }
3361		    break;
3362		}
3363
3364		termc = *cp;
3365
3366		error = regcomp(&pattern.re, re, REG_EXTENDED);
3367		free(re);
3368		if (error)  {
3369		    *lengthPtr = cp - start + 1;
3370		    VarREError(error, &pattern.re, "RE substitution error");
3371		    free(pattern.replace);
3372		    goto cleanup;
3373		}
3374
3375		pattern.nsub = pattern.re.re_nsub + 1;
3376		if (pattern.nsub < 1)
3377		    pattern.nsub = 1;
3378		if (pattern.nsub > 10)
3379		    pattern.nsub = 10;
3380		pattern.matches = bmake_malloc(pattern.nsub *
3381					  sizeof(regmatch_t));
3382		newStr = VarModify(ctxt, &tmpparsestate, nstr,
3383				   VarRESubstitute,
3384				   &pattern);
3385		regfree(&pattern.re);
3386		free(pattern.replace);
3387		free(pattern.matches);
3388		delim = '\0';
3389		break;
3390	    }
3391#endif
3392	case 'Q':
3393	    if (tstr[1] == endc || tstr[1] == ':') {
3394		newStr = VarQuote(nstr);
3395		cp = tstr + 1;
3396		termc = *cp;
3397		break;
3398	    }
3399	    goto default_case;
3400	case 'T':
3401	    if (tstr[1] == endc || tstr[1] == ':') {
3402		newStr = VarModify(ctxt, &parsestate, nstr, VarTail,
3403				   NULL);
3404		cp = tstr + 1;
3405		termc = *cp;
3406		break;
3407	    }
3408	    goto default_case;
3409	case 'H':
3410	    if (tstr[1] == endc || tstr[1] == ':') {
3411		newStr = VarModify(ctxt, &parsestate, nstr, VarHead,
3412				   NULL);
3413		cp = tstr + 1;
3414		termc = *cp;
3415		break;
3416	    }
3417	    goto default_case;
3418	case 'E':
3419	    if (tstr[1] == endc || tstr[1] == ':') {
3420		newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix,
3421				   NULL);
3422		cp = tstr + 1;
3423		termc = *cp;
3424		break;
3425	    }
3426	    goto default_case;
3427	case 'R':
3428	    if (tstr[1] == endc || tstr[1] == ':') {
3429		newStr = VarModify(ctxt, &parsestate, nstr, VarRoot,
3430				   NULL);
3431		cp = tstr + 1;
3432		termc = *cp;
3433		break;
3434	    }
3435	    goto default_case;
3436	case 'O':
3437	    {
3438		char otype;
3439
3440		cp = tstr + 1;	/* skip to the rest in any case */
3441		if (tstr[1] == endc || tstr[1] == ':') {
3442		    otype = 's';
3443		    termc = *cp;
3444		} else if ( (tstr[1] == 'x') &&
3445			    (tstr[2] == endc || tstr[2] == ':') ) {
3446		    otype = tstr[1];
3447		    cp = tstr + 2;
3448		    termc = *cp;
3449		} else {
3450		    goto bad_modifier;
3451		}
3452		newStr = VarOrder(nstr, otype);
3453		break;
3454	    }
3455	case 'u':
3456	    if (tstr[1] == endc || tstr[1] == ':') {
3457		newStr = VarUniq(nstr);
3458		cp = tstr + 1;
3459		termc = *cp;
3460		break;
3461	    }
3462	    goto default_case;
3463#ifdef SUNSHCMD
3464	case 's':
3465	    if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
3466		const char *emsg;
3467		newStr = Cmd_Exec(nstr, &emsg);
3468		if (emsg)
3469		    Error(emsg, nstr);
3470		cp = tstr + 2;
3471		termc = *cp;
3472		break;
3473	    }
3474	    goto default_case;
3475#endif
3476	default:
3477	default_case:
3478	{
3479#ifdef SYSVVARSUB
3480	    /*
3481	     * This can either be a bogus modifier or a System-V
3482	     * substitution command.
3483	     */
3484	    VarPattern      pattern;
3485	    Boolean         eqFound;
3486
3487	    pattern.flags = 0;
3488	    eqFound = FALSE;
3489	    /*
3490	     * First we make a pass through the string trying
3491	     * to verify it is a SYSV-make-style translation:
3492	     * it must be: <string1>=<string2>)
3493	     */
3494	    cp = tstr;
3495	    cnt = 1;
3496	    while (*cp != '\0' && cnt) {
3497		if (*cp == '=') {
3498		    eqFound = TRUE;
3499		    /* continue looking for endc */
3500		}
3501		else if (*cp == endc)
3502		    cnt--;
3503		else if (*cp == startc)
3504		    cnt++;
3505		if (cnt)
3506		    cp++;
3507	    }
3508	    if (*cp == endc && eqFound) {
3509
3510		/*
3511		 * Now we break this sucker into the lhs and
3512		 * rhs. We must null terminate them of course.
3513		 */
3514		delim='=';
3515		cp = tstr;
3516		if ((pattern.lhs = VarGetPattern(ctxt, &parsestate,
3517						 errnum, &cp, delim, &pattern.flags,
3518						 &pattern.leftLen, NULL)) == NULL)
3519		    goto cleanup;
3520		delim = endc;
3521		if ((pattern.rhs = VarGetPattern(ctxt, &parsestate,
3522						 errnum, &cp, delim, NULL, &pattern.rightLen,
3523						 &pattern)) == NULL)
3524		    goto cleanup;
3525
3526		/*
3527		 * SYSV modifications happen through the whole
3528		 * string. Note the pattern is anchored at the end.
3529		 */
3530		termc = *--cp;
3531		delim = '\0';
3532		if (pattern.leftLen == 0 && *nstr == '\0') {
3533		    newStr = nstr;	/* special case */
3534		} else {
3535		    newStr = VarModify(ctxt, &parsestate, nstr,
3536				       VarSYSVMatch,
3537				       &pattern);
3538		}
3539		free(UNCONST(pattern.lhs));
3540		free(UNCONST(pattern.rhs));
3541	    } else
3542#endif
3543		{
3544		    Error("Unknown modifier '%c'", *tstr);
3545		    for (cp = tstr+1;
3546			 *cp != ':' && *cp != endc && *cp != '\0';
3547			 cp++)
3548			continue;
3549		    termc = *cp;
3550		    newStr = var_Error;
3551		}
3552	    }
3553	}
3554	if (DEBUG(VAR)) {
3555	    fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n",
3556		v->name, modifier, newStr);
3557	}
3558
3559	if (newStr != nstr) {
3560	    if (*freePtr) {
3561		free(nstr);
3562		*freePtr = NULL;
3563	    }
3564	    nstr = newStr;
3565	    if (nstr != var_Error && nstr != varNoError) {
3566		*freePtr = nstr;
3567	    }
3568	}
3569	if (termc == '\0' && endc != '\0') {
3570	    Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier);
3571	} else if (termc == ':') {
3572	    cp++;
3573	}
3574	tstr = cp;
3575    }
3576 out:
3577    *lengthPtr = tstr - start;
3578    return (nstr);
3579
3580 bad_modifier:
3581    /* "{(" */
3582    Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr,
3583	  v->name);
3584
3585 cleanup:
3586    *lengthPtr = cp - start;
3587    if (delim != '\0')
3588	Error("Unclosed substitution for %s (%c missing)",
3589	      v->name, delim);
3590    if (*freePtr) {
3591	free(*freePtr);
3592	*freePtr = NULL;
3593    }
3594    return (var_Error);
3595}
3596
3597/*-
3598 *-----------------------------------------------------------------------
3599 * Var_Parse --
3600 *	Given the start of a variable invocation, extract the variable
3601 *	name and find its value, then modify it according to the
3602 *	specification.
3603 *
3604 * Input:
3605 *	str		The string to parse
3606 *	ctxt		The context for the variable
3607 *	errnum		TRUE if undefined variables are an error
3608 *	lengthPtr	OUT: The length of the specification
3609 *	freePtr		OUT: Non-NULL if caller should free *freePtr
3610 *
3611 * Results:
3612 *	The (possibly-modified) value of the variable or var_Error if the
3613 *	specification is invalid. The length of the specification is
3614 *	placed in *lengthPtr (for invalid specifications, this is just
3615 *	2...?).
3616 *	If *freePtr is non-NULL then it's a pointer that the caller
3617 *	should pass to free() to free memory used by the result.
3618 *
3619 * Side Effects:
3620 *	None.
3621 *
3622 *-----------------------------------------------------------------------
3623 */
3624/* coverity[+alloc : arg-*4] */
3625char *
3626Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr,
3627	  void **freePtr)
3628{
3629    const char	   *tstr;    	/* Pointer into str */
3630    Var		   *v;		/* Variable in invocation */
3631    Boolean 	    haveModifier;/* TRUE if have modifiers for the variable */
3632    char	    endc;    	/* Ending character when variable in parens
3633				 * or braces */
3634    char	    startc;	/* Starting character when variable in parens
3635				 * or braces */
3636    int		    vlen;	/* Length of variable name */
3637    const char 	   *start;	/* Points to original start of str */
3638    char	   *nstr;	/* New string, used during expansion */
3639    Boolean 	    dynamic;	/* TRUE if the variable is local and we're
3640				 * expanding it in a non-local context. This
3641				 * is done to support dynamic sources. The
3642				 * result is just the invocation, unaltered */
3643    Var_Parse_State parsestate; /* Flags passed to helper functions */
3644    char	  name[2];
3645
3646    *freePtr = NULL;
3647    dynamic = FALSE;
3648    start = str;
3649    parsestate.oneBigWord = FALSE;
3650    parsestate.varSpace = ' ';	/* word separator */
3651
3652    startc = str[1];
3653    if (startc != PROPEN && startc != BROPEN) {
3654	/*
3655	 * If it's not bounded by braces of some sort, life is much simpler.
3656	 * We just need to check for the first character and return the
3657	 * value if it exists.
3658	 */
3659
3660	/* Error out some really stupid names */
3661	if (startc == '\0' || strchr(")}:$", startc)) {
3662	    *lengthPtr = 1;
3663	    return var_Error;
3664	}
3665	name[0] = startc;
3666	name[1] = '\0';
3667
3668	v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3669	if (v == NULL) {
3670	    *lengthPtr = 2;
3671
3672	    if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
3673		/*
3674		 * If substituting a local variable in a non-local context,
3675		 * assume it's for dynamic source stuff. We have to handle
3676		 * this specially and return the longhand for the variable
3677		 * with the dollar sign escaped so it makes it back to the
3678		 * caller. Only four of the local variables are treated
3679		 * specially as they are the only four that will be set
3680		 * when dynamic sources are expanded.
3681		 */
3682		switch (str[1]) {
3683		    case '@':
3684			return UNCONST("$(.TARGET)");
3685		    case '%':
3686			return UNCONST("$(.ARCHIVE)");
3687		    case '*':
3688			return UNCONST("$(.PREFIX)");
3689		    case '!':
3690			return UNCONST("$(.MEMBER)");
3691		}
3692	    }
3693	    /*
3694	     * Error
3695	     */
3696	    return (errnum ? var_Error : varNoError);
3697	} else {
3698	    haveModifier = FALSE;
3699	    tstr = &str[1];
3700	    endc = str[1];
3701	}
3702    } else {
3703	Buffer buf;	/* Holds the variable name */
3704
3705	endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
3706	Buf_Init(&buf, 0);
3707
3708	/*
3709	 * Skip to the end character or a colon, whichever comes first.
3710	 */
3711	for (tstr = str + 2;
3712	     *tstr != '\0' && *tstr != endc && *tstr != ':';
3713	     tstr++)
3714	{
3715	    /*
3716	     * A variable inside a variable, expand
3717	     */
3718	    if (*tstr == '$') {
3719		int rlen;
3720		void *freeIt;
3721		char *rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt);
3722		if (rval != NULL) {
3723		    Buf_AddBytes(&buf, strlen(rval), rval);
3724		}
3725		if (freeIt)
3726		    free(freeIt);
3727		tstr += rlen - 1;
3728	    }
3729	    else
3730		Buf_AddByte(&buf, *tstr);
3731	}
3732	if (*tstr == ':') {
3733	    haveModifier = TRUE;
3734	} else if (*tstr != '\0') {
3735	    haveModifier = FALSE;
3736	} else {
3737	    /*
3738	     * If we never did find the end character, return NULL
3739	     * right now, setting the length to be the distance to
3740	     * the end of the string, since that's what make does.
3741	     */
3742	    *lengthPtr = tstr - str;
3743	    Buf_Destroy(&buf, TRUE);
3744	    return (var_Error);
3745	}
3746	str = Buf_GetAll(&buf, &vlen);
3747
3748	/*
3749	 * At this point, str points into newly allocated memory from
3750	 * buf, containing only the name of the variable.
3751	 *
3752	 * start and tstr point into the const string that was pointed
3753	 * to by the original value of the str parameter.  start points
3754	 * to the '$' at the beginning of the string, while tstr points
3755	 * to the char just after the end of the variable name -- this
3756	 * will be '\0', ':', PRCLOSE, or BRCLOSE.
3757	 */
3758
3759	v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3760	/*
3761	 * Check also for bogus D and F forms of local variables since we're
3762	 * in a local context and the name is the right length.
3763	 */
3764	if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
3765		(vlen == 2) && (str[1] == 'F' || str[1] == 'D') &&
3766		strchr("@%*!<>", str[0]) != NULL) {
3767	    /*
3768	     * Well, it's local -- go look for it.
3769	     */
3770	    name[0] = *str;
3771	    name[1] = '\0';
3772	    v = VarFind(name, ctxt, 0);
3773
3774	    if (v != NULL) {
3775		/*
3776		 * No need for nested expansion or anything, as we're
3777		 * the only one who sets these things and we sure don't
3778		 * but nested invocations in them...
3779		 */
3780		nstr = Buf_GetAll(&v->val, NULL);
3781
3782		if (str[1] == 'D') {
3783		    nstr = VarModify(ctxt, &parsestate, nstr, VarHead,
3784				    NULL);
3785		} else {
3786		    nstr = VarModify(ctxt, &parsestate, nstr, VarTail,
3787				    NULL);
3788		}
3789		/*
3790		 * Resulting string is dynamically allocated, so
3791		 * tell caller to free it.
3792		 */
3793		*freePtr = nstr;
3794		*lengthPtr = tstr-start+1;
3795		Buf_Destroy(&buf, TRUE);
3796		VarFreeEnv(v, TRUE);
3797		return nstr;
3798	    }
3799	}
3800
3801	if (v == NULL) {
3802	    if (((vlen == 1) ||
3803		 (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) &&
3804		((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3805	    {
3806		/*
3807		 * If substituting a local variable in a non-local context,
3808		 * assume it's for dynamic source stuff. We have to handle
3809		 * this specially and return the longhand for the variable
3810		 * with the dollar sign escaped so it makes it back to the
3811		 * caller. Only four of the local variables are treated
3812		 * specially as they are the only four that will be set
3813		 * when dynamic sources are expanded.
3814		 */
3815		switch (*str) {
3816		    case '@':
3817		    case '%':
3818		    case '*':
3819		    case '!':
3820			dynamic = TRUE;
3821			break;
3822		}
3823	    } else if ((vlen > 2) && (*str == '.') &&
3824		       isupper((unsigned char) str[1]) &&
3825		       ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3826	    {
3827		int	len;
3828
3829		len = vlen - 1;
3830		if ((strncmp(str, ".TARGET", len) == 0) ||
3831		    (strncmp(str, ".ARCHIVE", len) == 0) ||
3832		    (strncmp(str, ".PREFIX", len) == 0) ||
3833		    (strncmp(str, ".MEMBER", len) == 0))
3834		{
3835		    dynamic = TRUE;
3836		}
3837	    }
3838
3839	    if (!haveModifier) {
3840		/*
3841		 * No modifiers -- have specification length so we can return
3842		 * now.
3843		 */
3844		*lengthPtr = tstr - start + 1;
3845		if (dynamic) {
3846		    char *pstr = bmake_strndup(start, *lengthPtr);
3847		    *freePtr = pstr;
3848		    Buf_Destroy(&buf, TRUE);
3849		    return(pstr);
3850		} else {
3851		    Buf_Destroy(&buf, TRUE);
3852		    return (errnum ? var_Error : varNoError);
3853		}
3854	    } else {
3855		/*
3856		 * Still need to get to the end of the variable specification,
3857		 * so kludge up a Var structure for the modifications
3858		 */
3859		v = bmake_malloc(sizeof(Var));
3860		v->name = UNCONST(str);
3861		Buf_Init(&v->val, 1);
3862		v->flags = VAR_JUNK;
3863		Buf_Destroy(&buf, FALSE);
3864	    }
3865	} else
3866	    Buf_Destroy(&buf, TRUE);
3867    }
3868
3869    if (v->flags & VAR_IN_USE) {
3870	Fatal("Variable %s is recursive.", v->name);
3871	/*NOTREACHED*/
3872    } else {
3873	v->flags |= VAR_IN_USE;
3874    }
3875    /*
3876     * Before doing any modification, we have to make sure the value
3877     * has been fully expanded. If it looks like recursion might be
3878     * necessary (there's a dollar sign somewhere in the variable's value)
3879     * we just call Var_Subst to do any other substitutions that are
3880     * necessary. Note that the value returned by Var_Subst will have
3881     * been dynamically-allocated, so it will need freeing when we
3882     * return.
3883     */
3884    nstr = Buf_GetAll(&v->val, NULL);
3885    if (strchr(nstr, '$') != NULL) {
3886	nstr = Var_Subst(NULL, nstr, ctxt, errnum);
3887	*freePtr = nstr;
3888    }
3889
3890    v->flags &= ~VAR_IN_USE;
3891
3892    if ((nstr != NULL) && haveModifier) {
3893	int used;
3894	/*
3895	 * Skip initial colon.
3896	 */
3897	tstr++;
3898
3899	nstr = ApplyModifiers(nstr, tstr, startc, endc,
3900			      v, ctxt, errnum, &used, freePtr);
3901	tstr += used;
3902    }
3903    if (*tstr) {
3904	*lengthPtr = tstr - start + 1;
3905    } else {
3906	*lengthPtr = tstr - start;
3907    }
3908
3909    if (v->flags & VAR_FROM_ENV) {
3910	Boolean	  destroy = FALSE;
3911
3912	if (nstr != Buf_GetAll(&v->val, NULL)) {
3913	    destroy = TRUE;
3914	} else {
3915	    /*
3916	     * Returning the value unmodified, so tell the caller to free
3917	     * the thing.
3918	     */
3919	    *freePtr = nstr;
3920	}
3921	VarFreeEnv(v, destroy);
3922    } else if (v->flags & VAR_JUNK) {
3923	/*
3924	 * Perform any free'ing needed and set *freePtr to NULL so the caller
3925	 * doesn't try to free a static pointer.
3926	 * If VAR_KEEP is also set then we want to keep str as is.
3927	 */
3928	if (!(v->flags & VAR_KEEP)) {
3929	    if (*freePtr) {
3930		free(nstr);
3931		*freePtr = NULL;
3932	    }
3933	    if (dynamic) {
3934		nstr = bmake_strndup(start, *lengthPtr);
3935		*freePtr = nstr;
3936	    } else {
3937		nstr = errnum ? var_Error : varNoError;
3938	    }
3939	}
3940	if (nstr != Buf_GetAll(&v->val, NULL))
3941	    Buf_Destroy(&v->val, TRUE);
3942	free(v->name);
3943	free(v);
3944    }
3945    return (nstr);
3946}
3947
3948/*-
3949 *-----------------------------------------------------------------------
3950 * Var_Subst  --
3951 *	Substitute for all variables in the given string in the given context
3952 *	If undefErr is TRUE, Parse_Error will be called when an undefined
3953 *	variable is encountered.
3954 *
3955 * Input:
3956 *	var		Named variable || NULL for all
3957 *	str		the string which to substitute
3958 *	ctxt		the context wherein to find variables
3959 *	undefErr	TRUE if undefineds are an error
3960 *
3961 * Results:
3962 *	The resulting string.
3963 *
3964 * Side Effects:
3965 *	None. The old string must be freed by the caller
3966 *-----------------------------------------------------------------------
3967 */
3968char *
3969Var_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr)
3970{
3971    Buffer  	  buf;		    /* Buffer for forming things */
3972    char    	  *val;		    /* Value to substitute for a variable */
3973    int		  length;   	    /* Length of the variable invocation */
3974    Boolean	  trailingBslash;   /* variable ends in \ */
3975    void 	  *freeIt = NULL;    /* Set if it should be freed */
3976    static Boolean errorReported;   /* Set true if an error has already
3977				     * been reported to prevent a plethora
3978				     * of messages when recursing */
3979
3980    Buf_Init(&buf, 0);
3981    errorReported = FALSE;
3982    trailingBslash = FALSE;
3983
3984    while (*str) {
3985	if (*str == '\n' && trailingBslash)
3986	    Buf_AddByte(&buf, ' ');
3987	if (var == NULL && (*str == '$') && (str[1] == '$')) {
3988	    /*
3989	     * A dollar sign may be escaped either with another dollar sign.
3990	     * In such a case, we skip over the escape character and store the
3991	     * dollar sign into the buffer directly.
3992	     */
3993	    str++;
3994	    Buf_AddByte(&buf, *str);
3995	    str++;
3996	} else if (*str != '$') {
3997	    /*
3998	     * Skip as many characters as possible -- either to the end of
3999	     * the string or to the next dollar sign (variable invocation).
4000	     */
4001	    const char  *cp;
4002
4003	    for (cp = str++; *str != '$' && *str != '\0'; str++)
4004		continue;
4005	    Buf_AddBytes(&buf, str - cp, cp);
4006	} else {
4007	    if (var != NULL) {
4008		int expand;
4009		for (;;) {
4010		    if (str[1] == '\0') {
4011			/* A trailing $ is kind of a special case */
4012			Buf_AddByte(&buf, str[0]);
4013			str++;
4014			expand = FALSE;
4015		    } else if (str[1] != PROPEN && str[1] != BROPEN) {
4016			if (str[1] != *var || strlen(var) > 1) {
4017			    Buf_AddBytes(&buf, 2, str);
4018			    str += 2;
4019			    expand = FALSE;
4020			}
4021			else
4022			    expand = TRUE;
4023			break;
4024		    }
4025		    else {
4026			const char *p;
4027
4028			/*
4029			 * Scan up to the end of the variable name.
4030			 */
4031			for (p = &str[2]; *p &&
4032			     *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++)
4033			    if (*p == '$')
4034				break;
4035			/*
4036			 * A variable inside the variable. We cannot expand
4037			 * the external variable yet, so we try again with
4038			 * the nested one
4039			 */
4040			if (*p == '$') {
4041			    Buf_AddBytes(&buf, p - str, str);
4042			    str = p;
4043			    continue;
4044			}
4045
4046			if (strncmp(var, str + 2, p - str - 2) != 0 ||
4047			    var[p - str - 2] != '\0') {
4048			    /*
4049			     * Not the variable we want to expand, scan
4050			     * until the next variable
4051			     */
4052			    for (;*p != '$' && *p != '\0'; p++)
4053				continue;
4054			    Buf_AddBytes(&buf, p - str, str);
4055			    str = p;
4056			    expand = FALSE;
4057			}
4058			else
4059			    expand = TRUE;
4060			break;
4061		    }
4062		}
4063		if (!expand)
4064		    continue;
4065	    }
4066
4067	    val = Var_Parse(str, ctxt, undefErr, &length, &freeIt);
4068
4069	    /*
4070	     * When we come down here, val should either point to the
4071	     * value of this variable, suitably modified, or be NULL.
4072	     * Length should be the total length of the potential
4073	     * variable invocation (from $ to end character...)
4074	     */
4075	    if (val == var_Error || val == varNoError) {
4076		/*
4077		 * If performing old-time variable substitution, skip over
4078		 * the variable and continue with the substitution. Otherwise,
4079		 * store the dollar sign and advance str so we continue with
4080		 * the string...
4081		 */
4082		if (oldVars) {
4083		    str += length;
4084		} else if (undefErr) {
4085		    /*
4086		     * If variable is undefined, complain and skip the
4087		     * variable. The complaint will stop us from doing anything
4088		     * when the file is parsed.
4089		     */
4090		    if (!errorReported) {
4091			Parse_Error(PARSE_FATAL,
4092				     "Undefined variable \"%.*s\"",length,str);
4093		    }
4094		    str += length;
4095		    errorReported = TRUE;
4096		} else {
4097		    Buf_AddByte(&buf, *str);
4098		    str += 1;
4099		}
4100	    } else {
4101		/*
4102		 * We've now got a variable structure to store in. But first,
4103		 * advance the string pointer.
4104		 */
4105		str += length;
4106
4107		/*
4108		 * Copy all the characters from the variable value straight
4109		 * into the new string.
4110		 */
4111		length = strlen(val);
4112		Buf_AddBytes(&buf, length, val);
4113		trailingBslash = length > 0 && val[length - 1] == '\\';
4114	    }
4115	    if (freeIt) {
4116		free(freeIt);
4117		freeIt = NULL;
4118	    }
4119	}
4120    }
4121
4122    return Buf_DestroyCompact(&buf);
4123}
4124
4125/*-
4126 *-----------------------------------------------------------------------
4127 * Var_GetTail --
4128 *	Return the tail from each of a list of words. Used to set the
4129 *	System V local variables.
4130 *
4131 * Input:
4132 *	file		Filename to modify
4133 *
4134 * Results:
4135 *	The resulting string.
4136 *
4137 * Side Effects:
4138 *	None.
4139 *
4140 *-----------------------------------------------------------------------
4141 */
4142#if 0
4143char *
4144Var_GetTail(char *file)
4145{
4146    return(VarModify(file, VarTail, NULL));
4147}
4148
4149/*-
4150 *-----------------------------------------------------------------------
4151 * Var_GetHead --
4152 *	Find the leading components of a (list of) filename(s).
4153 *	XXX: VarHead does not replace foo by ., as (sun) System V make
4154 *	does.
4155 *
4156 * Input:
4157 *	file		Filename to manipulate
4158 *
4159 * Results:
4160 *	The leading components.
4161 *
4162 * Side Effects:
4163 *	None.
4164 *
4165 *-----------------------------------------------------------------------
4166 */
4167char *
4168Var_GetHead(char *file)
4169{
4170    return(VarModify(file, VarHead, NULL));
4171}
4172#endif
4173
4174/*-
4175 *-----------------------------------------------------------------------
4176 * Var_Init --
4177 *	Initialize the module
4178 *
4179 * Results:
4180 *	None
4181 *
4182 * Side Effects:
4183 *	The VAR_CMD and VAR_GLOBAL contexts are created
4184 *-----------------------------------------------------------------------
4185 */
4186void
4187Var_Init(void)
4188{
4189    VAR_GLOBAL = Targ_NewGN("Global");
4190    VAR_CMD = Targ_NewGN("Command");
4191
4192}
4193
4194
4195void
4196Var_End(void)
4197{
4198}
4199
4200
4201/****************** PRINT DEBUGGING INFO *****************/
4202static void
4203VarPrintVar(void *vp)
4204{
4205    Var    *v = (Var *)vp;
4206    fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
4207}
4208
4209/*-
4210 *-----------------------------------------------------------------------
4211 * Var_Dump --
4212 *	print all variables in a context
4213 *-----------------------------------------------------------------------
4214 */
4215void
4216Var_Dump(GNode *ctxt)
4217{
4218    Hash_Search search;
4219    Hash_Entry *h;
4220
4221    for (h = Hash_EnumFirst(&ctxt->context, &search);
4222	 h != NULL;
4223	 h = Hash_EnumNext(&search)) {
4224	    VarPrintVar(Hash_GetValue(h));
4225    }
4226}
4227