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