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