Merge branch 'vendor/WPA_SUPPLICANT'
[dragonfly.git] / contrib / bmake / var.c
1 /*      $NetBSD: var.c,v 1.883 2021/03/14 20:23:29 rillig Exp $ */
2
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 1989 by Berkeley Softworks
37  * All rights reserved.
38  *
39  * This code is derived from software contributed to Berkeley by
40  * Adam de Boor.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *      This product includes software developed by the University of
53  *      California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  */
70
71 /*
72  * Handling of variables and the expressions formed from them.
73  *
74  * Variables are set using lines of the form VAR=value.  Both the variable
75  * name and the value can contain references to other variables, by using
76  * expressions like ${VAR}, ${VAR:Modifiers}, ${${VARNAME}} or ${VAR:${MODS}}.
77  *
78  * Interface:
79  *      Var_Init        Initialize this module.
80  *
81  *      Var_End         Clean up the module.
82  *
83  *      Var_Set
84  *      Var_SetExpand
85  *                      Set the value of the variable, creating it if
86  *                      necessary.
87  *
88  *      Var_Append
89  *      Var_AppendExpand
90  *                      Append more characters to the variable, creating it if
91  *                      necessary. A space is placed between the old value and
92  *                      the new one.
93  *
94  *      Var_Exists
95  *      Var_ExistsExpand
96  *                      See if a variable exists.
97  *
98  *      Var_Value       Return the unexpanded value of a variable, or NULL if
99  *                      the variable is undefined.
100  *
101  *      Var_Subst       Substitute all variable expressions in a string.
102  *
103  *      Var_Parse       Parse a variable expression such as ${VAR:Mpattern}.
104  *
105  *      Var_Delete
106  *      Var_DeleteExpand
107  *                      Delete a variable.
108  *
109  *      Var_ReexportVars
110  *                      Export some or even all variables to the environment
111  *                      of this process and its child processes.
112  *
113  *      Var_Export      Export the variable to the environment of this process
114  *                      and its child processes.
115  *
116  *      Var_UnExport    Don't export the variable anymore.
117  *
118  * Debugging:
119  *      Var_Stats       Print out hashing statistics if in -dh mode.
120  *
121  *      Var_Dump        Print out all variables defined in the given scope.
122  *
123  * XXX: There's a lot of almost duplicate code in these functions that only
124  *  differs in subtle details that are not mentioned in the manual page.
125  */
126
127 #include <sys/stat.h>
128 #include <sys/types.h>
129 #ifndef NO_REGEX
130 #include <regex.h>
131 #endif
132
133 #include "make.h"
134
135 #include <errno.h>
136 #ifdef HAVE_INTTYPES_H
137 #include <inttypes.h>
138 #elif defined(HAVE_STDINT_H)
139 #include <stdint.h>
140 #endif
141 #ifdef HAVE_LIMITS_H
142 #include <limits.h>
143 #endif
144 #include <time.h>
145
146 #include "dir.h"
147 #include "job.h"
148 #include "metachar.h"
149
150 /*      "@(#)var.c      8.3 (Berkeley) 3/19/94" */
151 MAKE_RCSID("$NetBSD: var.c,v 1.883 2021/03/14 20:23:29 rillig Exp $");
152
153 typedef enum VarFlags {
154         VFL_NONE        = 0,
155
156         /*
157          * The variable's value is currently being used by Var_Parse or
158          * Var_Subst.  This marker is used to avoid endless recursion.
159          */
160         VFL_IN_USE      = 1 << 0,
161
162         /*
163          * The variable comes from the environment.
164          * These variables are not registered in any GNode, therefore they
165          * must be freed as soon as they are not used anymore.
166          */
167         VFL_FROM_ENV    = 1 << 1,
168
169         /*
170          * The variable is exported to the environment, to be used by child
171          * processes.
172          */
173         VFL_EXPORTED    = 1 << 2,
174
175         /*
176          * At the point where this variable was exported, it contained an
177          * unresolved reference to another variable.  Before any child
178          * process is started, it needs to be exported again, in the hope
179          * that the referenced variable can then be resolved.
180          */
181         VFL_REEXPORT    = 1 << 3,
182
183         /* The variable came from the command line. */
184         VFL_FROM_CMD    = 1 << 4,
185
186         /*
187          * The variable value cannot be changed anymore, and the variable
188          * cannot be deleted.  Any attempts to do so are silently ignored,
189          * they are logged with -dv though.
190          *
191          * See VAR_SET_READONLY.
192          */
193         VFL_READONLY    = 1 << 5
194 } VarFlags;
195
196 /*
197  * Variables are defined using one of the VAR=value assignments.  Their
198  * value can be queried by expressions such as $V, ${VAR}, or with modifiers
199  * such as ${VAR:S,from,to,g:Q}.
200  *
201  * There are 3 kinds of variables: scope variables, environment variables,
202  * undefined variables.
203  *
204  * Scope variables are stored in a GNode.scope.  The only way to undefine
205  * a scope variable is using the .undef directive.  In particular, it must
206  * not be possible to undefine a variable during the evaluation of an
207  * expression, or Var.name might point nowhere.
208  *
209  * Environment variables are temporary.  They are returned by VarFind, and
210  * after using them, they must be freed using VarFreeEnv.
211  *
212  * Undefined variables occur during evaluation of variable expressions such
213  * as ${UNDEF:Ufallback} in Var_Parse and ApplyModifiers.
214  */
215 typedef struct Var {
216         /*
217          * The name of the variable, once set, doesn't change anymore.
218          * For scope variables, it aliases the corresponding HashEntry name.
219          * For environment and undefined variables, it is allocated.
220          */
221         FStr name;
222
223         /* The unexpanded value of the variable. */
224         Buffer val;
225         /* Miscellaneous status flags. */
226         VarFlags flags;
227 } Var;
228
229 /*
230  * Exporting variables is expensive and may leak memory, so skip it if we
231  * can.
232  *
233  * To avoid this, it might be worth encapsulating the environment variables
234  * in a separate data structure called EnvVars.
235  */
236 typedef enum VarExportedMode {
237         VAR_EXPORTED_NONE,
238         VAR_EXPORTED_SOME,
239         VAR_EXPORTED_ALL
240 } VarExportedMode;
241
242 typedef enum UnexportWhat {
243         /* Unexport the variables given by name. */
244         UNEXPORT_NAMED,
245         /*
246          * Unexport all globals previously exported, but keep the environment
247          * inherited from the parent.
248          */
249         UNEXPORT_ALL,
250         /*
251          * Unexport all globals previously exported and clear the environment
252          * inherited from the parent.
253          */
254         UNEXPORT_ENV
255 } UnexportWhat;
256
257 /* Flags for pattern matching in the :S and :C modifiers */
258 typedef struct VarPatternFlags {
259
260         /* Replace as often as possible ('g') */
261         Boolean subGlobal: 1;
262         /* Replace only once ('1') */
263         Boolean subOnce: 1;
264         /* Match at start of word ('^') */
265         Boolean anchorStart: 1;
266         /* Match at end of word ('$') */
267         Boolean anchorEnd: 1;
268 } VarPatternFlags;
269
270 /* SepBuf builds a string from words interleaved with separators. */
271 typedef struct SepBuf {
272         Buffer buf;
273         Boolean needSep;
274         /* Usually ' ', but see the ':ts' modifier. */
275         char sep;
276 } SepBuf;
277
278
279 ENUM_FLAGS_RTTI_4(VarEvalFlags,
280                   VARE_UNDEFERR, VARE_WANTRES, VARE_KEEP_DOLLAR,
281                   VARE_KEEP_UNDEF);
282
283 /*
284  * This lets us tell if we have replaced the original environ
285  * (which we cannot free).
286  */
287 char **savedEnv = NULL;
288
289 /*
290  * Special return value for Var_Parse, indicating a parse error.  It may be
291  * caused by an undefined variable, a syntax error in a modifier or
292  * something entirely different.
293  */
294 char var_Error[] = "";
295
296 /*
297  * Special return value for Var_Parse, indicating an undefined variable in
298  * a case where VARE_UNDEFERR is not set.  This undefined variable is
299  * typically a dynamic variable such as ${.TARGET}, whose expansion needs to
300  * be deferred until it is defined in an actual target.
301  *
302  * See VARE_KEEP_UNDEF.
303  */
304 static char varUndefined[] = "";
305
306 /*
307  * Traditionally this make consumed $$ during := like any other expansion.
308  * Other make's do not, and this make follows straight since 2016-01-09.
309  *
310  * This knob allows controlling the behavior.
311  * FALSE to consume $$ during := assignment.
312  * TRUE to preserve $$ during := assignment.
313  */
314 #define MAKE_SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
315 static Boolean save_dollars = FALSE;
316
317 /*
318  * A scope collects variable names and their values.
319  *
320  * The main scope is SCOPE_GLOBAL, which contains the variables that are set
321  * in the makefiles.  SCOPE_INTERNAL acts as a fallback for SCOPE_GLOBAL and
322  * contains some internal make variables.  These internal variables can thus
323  * be overridden, they can also be restored by undefining the overriding
324  * variable.
325  *
326  * SCOPE_CMDLINE contains variables from the command line arguments.  These
327  * override variables from SCOPE_GLOBAL.
328  *
329  * There is no scope for environment variables, these are generated on-the-fly
330  * whenever they are referenced.  If there were such a scope, each change to
331  * environment variables would have to be reflected in that scope, which may
332  * be simpler or more complex than the current implementation.
333  *
334  * Each target has its own scope, containing the 7 target-local variables
335  * .TARGET, .ALLSRC, etc.  No other variables are in these scopes.
336  */
337
338 GNode *SCOPE_CMDLINE;
339 GNode *SCOPE_GLOBAL;
340 GNode *SCOPE_INTERNAL;
341
342 ENUM_FLAGS_RTTI_6(VarFlags,
343                   VFL_IN_USE, VFL_FROM_ENV,
344                   VFL_EXPORTED, VFL_REEXPORT, VFL_FROM_CMD, VFL_READONLY);
345
346 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
347
348
349 static Var *
350 VarNew(FStr name, const char *value, VarFlags flags)
351 {
352         size_t value_len = strlen(value);
353         Var *var = bmake_malloc(sizeof *var);
354         var->name = name;
355         Buf_InitSize(&var->val, value_len + 1);
356         Buf_AddBytes(&var->val, value, value_len);
357         var->flags = flags;
358         return var;
359 }
360
361 static const char *
362 CanonicalVarname(const char *name)
363 {
364         if (*name == '.' && ch_isupper(name[1])) {
365                 switch (name[1]) {
366                 case 'A':
367                         if (strcmp(name, ".ALLSRC") == 0)
368                                 name = ALLSRC;
369                         if (strcmp(name, ".ARCHIVE") == 0)
370                                 name = ARCHIVE;
371                         break;
372                 case 'I':
373                         if (strcmp(name, ".IMPSRC") == 0)
374                                 name = IMPSRC;
375                         break;
376                 case 'M':
377                         if (strcmp(name, ".MEMBER") == 0)
378                                 name = MEMBER;
379                         break;
380                 case 'O':
381                         if (strcmp(name, ".OODATE") == 0)
382                                 name = OODATE;
383                         break;
384                 case 'P':
385                         if (strcmp(name, ".PREFIX") == 0)
386                                 name = PREFIX;
387                         break;
388                 case 'S':
389                         if (strcmp(name, ".SHELL") == 0) {
390                                 if (shellPath == NULL)
391                                         Shell_Init();
392                         }
393                         break;
394                 case 'T':
395                         if (strcmp(name, ".TARGET") == 0)
396                                 name = TARGET;
397                         break;
398                 }
399         }
400
401         /* GNU make has an additional alias $^ == ${.ALLSRC}. */
402
403         return name;
404 }
405
406 static Var *
407 GNode_FindVar(GNode *scope, const char *varname, unsigned int hash)
408 {
409         return HashTable_FindValueHash(&scope->vars, varname, hash);
410 }
411
412 /*
413  * Find the variable in the scope, and maybe in other scopes as well.
414  *
415  * Input:
416  *      name            name to find, is not expanded any further
417  *      scope           scope in which to look first
418  *      elsewhere       TRUE to look in other scopes as well
419  *
420  * Results:
421  *      The found variable, or NULL if the variable does not exist.
422  *      If the variable is an environment variable, it must be freed using
423  *      VarFreeEnv after use.
424  */
425 static Var *
426 VarFind(const char *name, GNode *scope, Boolean elsewhere)
427 {
428         Var *var;
429         unsigned int nameHash;
430
431         /* Replace '.TARGET' with '@', likewise for other local variables. */
432         name = CanonicalVarname(name);
433         nameHash = Hash_Hash(name);
434
435         var = GNode_FindVar(scope, name, nameHash);
436         if (!elsewhere)
437                 return var;
438
439         if (var == NULL && scope != SCOPE_CMDLINE)
440                 var = GNode_FindVar(SCOPE_CMDLINE, name, nameHash);
441
442         if (!opts.checkEnvFirst && var == NULL && scope != SCOPE_GLOBAL) {
443                 var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
444                 if (var == NULL && scope != SCOPE_INTERNAL) {
445                         /* SCOPE_INTERNAL is subordinate to SCOPE_GLOBAL */
446                         var = GNode_FindVar(SCOPE_INTERNAL, name, nameHash);
447                 }
448         }
449
450         if (var == NULL) {
451                 char *env;
452
453                 if ((env = getenv(name)) != NULL) {
454                         char *varname = bmake_strdup(name);
455                         return VarNew(FStr_InitOwn(varname), env, VFL_FROM_ENV);
456                 }
457
458                 if (opts.checkEnvFirst && scope != SCOPE_GLOBAL) {
459                         var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
460                         if (var == NULL && scope != SCOPE_INTERNAL)
461                                 var = GNode_FindVar(SCOPE_INTERNAL, name,
462                                     nameHash);
463                         return var;
464                 }
465
466                 return NULL;
467         }
468
469         return var;
470 }
471
472 /* If the variable is an environment variable, free it, including its value. */
473 static void
474 VarFreeEnv(Var *v)
475 {
476         if (!(v->flags & VFL_FROM_ENV))
477                 return;
478
479         FStr_Done(&v->name);
480         Buf_Done(&v->val);
481         free(v);
482 }
483
484 /* Add a new variable of the given name and value to the given scope. */
485 static Var *
486 VarAdd(const char *name, const char *value, GNode *scope, VarSetFlags flags)
487 {
488         HashEntry *he = HashTable_CreateEntry(&scope->vars, name, NULL);
489         Var *v = VarNew(FStr_InitRefer(/* aliased to */ he->key), value,
490             flags & VAR_SET_READONLY ? VFL_READONLY : VFL_NONE);
491         HashEntry_Set(he, v);
492         DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, value);
493         return v;
494 }
495
496 /*
497  * Remove a variable from a scope, freeing all related memory as well.
498  * The variable name is kept as-is, it is not expanded.
499  */
500 void
501 Var_Delete(GNode *scope, const char *varname)
502 {
503         HashEntry *he = HashTable_FindEntry(&scope->vars, varname);
504         Var *v;
505
506         if (he == NULL) {
507                 DEBUG2(VAR, "%s:delete %s (not found)\n", scope->name, varname);
508                 return;
509         }
510
511         DEBUG2(VAR, "%s:delete %s\n", scope->name, varname);
512         v = he->value;
513         if (v->flags & VFL_EXPORTED)
514                 unsetenv(v->name.str);
515         if (strcmp(v->name.str, MAKE_EXPORTED) == 0)
516                 var_exportedVars = VAR_EXPORTED_NONE;
517         assert(v->name.freeIt == NULL);
518         HashTable_DeleteEntry(&scope->vars, he);
519         Buf_Done(&v->val);
520         free(v);
521 }
522
523 /*
524  * Remove a variable from a scope, freeing all related memory as well.
525  * The variable name is expanded once.
526  */
527 void
528 Var_DeleteExpand(GNode *scope, const char *name)
529 {
530         FStr varname = FStr_InitRefer(name);
531
532         if (strchr(varname.str, '$') != NULL) {
533                 char *expanded;
534                 (void)Var_Subst(varname.str, SCOPE_GLOBAL, VARE_WANTRES,
535                     &expanded);
536                 /* TODO: handle errors */
537                 varname = FStr_InitOwn(expanded);
538         }
539
540         Var_Delete(scope, varname.str);
541         FStr_Done(&varname);
542 }
543
544 /*
545  * Undefine one or more variables from the global scope.
546  * The argument is expanded exactly once and then split into words.
547  */
548 void
549 Var_Undef(const char *arg)
550 {
551         VarParseResult vpr;
552         char *expanded;
553         Words varnames;
554         size_t i;
555
556         if (arg[0] == '\0') {
557                 Parse_Error(PARSE_FATAL,
558                     "The .undef directive requires an argument");
559                 return;
560         }
561
562         vpr = Var_Subst(arg, SCOPE_GLOBAL, VARE_WANTRES, &expanded);
563         if (vpr != VPR_OK) {
564                 Parse_Error(PARSE_FATAL,
565                     "Error in variable names to be undefined");
566                 return;
567         }
568
569         varnames = Str_Words(expanded, FALSE);
570         if (varnames.len == 1 && varnames.words[0][0] == '\0')
571                 varnames.len = 0;
572
573         for (i = 0; i < varnames.len; i++) {
574                 const char *varname = varnames.words[i];
575                 Global_Delete(varname);
576         }
577
578         Words_Free(varnames);
579         free(expanded);
580 }
581
582 static Boolean
583 MayExport(const char *name)
584 {
585         if (name[0] == '.')
586                 return FALSE;   /* skip internals */
587         if (name[0] == '-')
588                 return FALSE;   /* skip misnamed variables */
589         if (name[1] == '\0') {
590                 /*
591                  * A single char.
592                  * If it is one of the variables that should only appear in
593                  * local scope, skip it, else we can get Var_Subst
594                  * into a loop.
595                  */
596                 switch (name[0]) {
597                 case '@':
598                 case '%':
599                 case '*':
600                 case '!':
601                         return FALSE;
602                 }
603         }
604         return TRUE;
605 }
606
607 static Boolean
608 ExportVarEnv(Var *v)
609 {
610         const char *name = v->name.str;
611         char *val = v->val.data;
612         char *expr;
613
614         if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT))
615                 return FALSE;   /* nothing to do */
616
617         if (strchr(val, '$') == NULL) {
618                 if (!(v->flags & VFL_EXPORTED))
619                         setenv(name, val, 1);
620                 return TRUE;
621         }
622
623         if (v->flags & VFL_IN_USE) {
624                 /*
625                  * We recursed while exporting in a child.
626                  * This isn't going to end well, just skip it.
627                  */
628                 return FALSE;
629         }
630
631         /* XXX: name is injected without escaping it */
632         expr = str_concat3("${", name, "}");
633         (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &val);
634         /* TODO: handle errors */
635         setenv(name, val, 1);
636         free(val);
637         free(expr);
638         return TRUE;
639 }
640
641 static Boolean
642 ExportVarPlain(Var *v)
643 {
644         if (strchr(v->val.data, '$') == NULL) {
645                 setenv(v->name.str, v->val.data, 1);
646                 v->flags |= VFL_EXPORTED;
647                 v->flags &= ~(unsigned)VFL_REEXPORT;
648                 return TRUE;
649         }
650
651         /*
652          * Flag the variable as something we need to re-export.
653          * No point actually exporting it now though,
654          * the child process can do it at the last minute.
655          * Avoid calling setenv more often than necessary since it can leak.
656          */
657         v->flags |= VFL_EXPORTED | VFL_REEXPORT;
658         return TRUE;
659 }
660
661 static Boolean
662 ExportVarLiteral(Var *v)
663 {
664         if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT))
665                 return FALSE;
666
667         if (!(v->flags & VFL_EXPORTED))
668                 setenv(v->name.str, v->val.data, 1);
669
670         return TRUE;
671 }
672
673 /*
674  * Mark a single variable to be exported later for subprocesses.
675  *
676  * Internal variables (those starting with '.') are not exported.
677  */
678 static Boolean
679 ExportVar(const char *name, VarExportMode mode)
680 {
681         Var *v;
682
683         if (!MayExport(name))
684                 return FALSE;
685
686         v = VarFind(name, SCOPE_GLOBAL, FALSE);
687         if (v == NULL)
688                 return FALSE;
689
690         if (mode == VEM_ENV)
691                 return ExportVarEnv(v);
692         else if (mode == VEM_PLAIN)
693                 return ExportVarPlain(v);
694         else
695                 return ExportVarLiteral(v);
696 }
697
698 /*
699  * Actually export the variables that have been marked as needing to be
700  * re-exported.
701  */
702 void
703 Var_ReexportVars(void)
704 {
705         char *xvarnames;
706
707         /*
708          * Several make implementations support this sort of mechanism for
709          * tracking recursion - but each uses a different name.
710          * We allow the makefiles to update MAKELEVEL and ensure
711          * children see a correctly incremented value.
712          */
713         char tmp[21];
714         snprintf(tmp, sizeof tmp, "%d", makelevel + 1);
715         setenv(MAKE_LEVEL_ENV, tmp, 1);
716
717         if (var_exportedVars == VAR_EXPORTED_NONE)
718                 return;
719
720         if (var_exportedVars == VAR_EXPORTED_ALL) {
721                 HashIter hi;
722
723                 /* Ouch! Exporting all variables at once is crazy. */
724                 HashIter_Init(&hi, &SCOPE_GLOBAL->vars);
725                 while (HashIter_Next(&hi) != NULL) {
726                         Var *var = hi.entry->value;
727                         ExportVar(var->name.str, VEM_ENV);
728                 }
729                 return;
730         }
731
732         (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL, VARE_WANTRES,
733             &xvarnames);
734         /* TODO: handle errors */
735         if (xvarnames[0] != '\0') {
736                 Words varnames = Str_Words(xvarnames, FALSE);
737                 size_t i;
738
739                 for (i = 0; i < varnames.len; i++)
740                         ExportVar(varnames.words[i], VEM_ENV);
741                 Words_Free(varnames);
742         }
743         free(xvarnames);
744 }
745
746 static void
747 ExportVars(const char *varnames, Boolean isExport, VarExportMode mode)
748 /* TODO: try to combine the parameters 'isExport' and 'mode'. */
749 {
750         Words words = Str_Words(varnames, FALSE);
751         size_t i;
752
753         if (words.len == 1 && words.words[0][0] == '\0')
754                 words.len = 0;
755
756         for (i = 0; i < words.len; i++) {
757                 const char *varname = words.words[i];
758                 if (!ExportVar(varname, mode))
759                         continue;
760
761                 if (var_exportedVars == VAR_EXPORTED_NONE)
762                         var_exportedVars = VAR_EXPORTED_SOME;
763
764                 if (isExport && mode == VEM_PLAIN)
765                         Global_Append(MAKE_EXPORTED, varname);
766         }
767         Words_Free(words);
768 }
769
770 static void
771 ExportVarsExpand(const char *uvarnames, Boolean isExport, VarExportMode mode)
772 {
773         char *xvarnames;
774
775         (void)Var_Subst(uvarnames, SCOPE_GLOBAL, VARE_WANTRES, &xvarnames);
776         /* TODO: handle errors */
777         ExportVars(xvarnames, isExport, mode);
778         free(xvarnames);
779 }
780
781 /* Export the named variables, or all variables. */
782 void
783 Var_Export(VarExportMode mode, const char *varnames)
784 {
785         if (mode == VEM_PLAIN && varnames[0] == '\0') {
786                 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
787                 return;
788         }
789
790         ExportVarsExpand(varnames, TRUE, mode);
791 }
792
793 void
794 Var_ExportVars(const char *varnames)
795 {
796         ExportVarsExpand(varnames, FALSE, VEM_PLAIN);
797 }
798
799
800 extern char **environ;
801
802 static void
803 ClearEnv(void)
804 {
805         const char *cp;
806         char **newenv;
807
808         cp = getenv(MAKE_LEVEL_ENV);    /* we should preserve this */
809         if (environ == savedEnv) {
810                 /* we have been here before! */
811                 newenv = bmake_realloc(environ, 2 * sizeof(char *));
812         } else {
813                 if (savedEnv != NULL) {
814                         free(savedEnv);
815                         savedEnv = NULL;
816                 }
817                 newenv = bmake_malloc(2 * sizeof(char *));
818         }
819
820         /* Note: we cannot safely free() the original environ. */
821         environ = savedEnv = newenv;
822         newenv[0] = NULL;
823         newenv[1] = NULL;
824         if (cp != NULL && *cp != '\0')
825                 setenv(MAKE_LEVEL_ENV, cp, 1);
826 }
827
828 static void
829 GetVarnamesToUnexport(Boolean isEnv, const char *arg,
830                       FStr *out_varnames, UnexportWhat *out_what)
831 {
832         UnexportWhat what;
833         FStr varnames = FStr_InitRefer("");
834
835         if (isEnv) {
836                 if (arg[0] != '\0') {
837                         Parse_Error(PARSE_FATAL,
838                             "The directive .unexport-env does not take "
839                             "arguments");
840                         /* continue anyway */
841                 }
842                 what = UNEXPORT_ENV;
843
844         } else {
845                 what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL;
846                 if (what == UNEXPORT_NAMED)
847                         varnames = FStr_InitRefer(arg);
848         }
849
850         if (what != UNEXPORT_NAMED) {
851                 char *expanded;
852                 /* Using .MAKE.EXPORTED */
853                 (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL,
854                     VARE_WANTRES, &expanded);
855                 /* TODO: handle errors */
856                 varnames = FStr_InitOwn(expanded);
857         }
858
859         *out_varnames = varnames;
860         *out_what = what;
861 }
862
863 static void
864 UnexportVar(const char *varname, UnexportWhat what)
865 {
866         Var *v = VarFind(varname, SCOPE_GLOBAL, FALSE);
867         if (v == NULL) {
868                 DEBUG1(VAR, "Not unexporting \"%s\" (not found)\n", varname);
869                 return;
870         }
871
872         DEBUG1(VAR, "Unexporting \"%s\"\n", varname);
873         if (what != UNEXPORT_ENV &&
874             (v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT))
875                 unsetenv(v->name.str);
876         v->flags &= ~(unsigned)(VFL_EXPORTED | VFL_REEXPORT);
877
878         if (what == UNEXPORT_NAMED) {
879                 /* Remove the variable names from .MAKE.EXPORTED. */
880                 /* XXX: v->name is injected without escaping it */
881                 char *expr = str_concat3("${" MAKE_EXPORTED ":N",
882                     v->name.str, "}");
883                 char *cp;
884                 (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &cp);
885                 /* TODO: handle errors */
886                 Global_Set(MAKE_EXPORTED, cp);
887                 free(cp);
888                 free(expr);
889         }
890 }
891
892 static void
893 UnexportVars(FStr *varnames, UnexportWhat what)
894 {
895         size_t i;
896         Words words;
897
898         if (what == UNEXPORT_ENV)
899                 ClearEnv();
900
901         words = Str_Words(varnames->str, FALSE);
902         for (i = 0; i < words.len; i++) {
903                 const char *varname = words.words[i];
904                 UnexportVar(varname, what);
905         }
906         Words_Free(words);
907
908         if (what != UNEXPORT_NAMED)
909                 Global_Delete(MAKE_EXPORTED);
910 }
911
912 /*
913  * This is called when .unexport[-env] is seen.
914  *
915  * str must have the form "unexport[-env] varname...".
916  */
917 void
918 Var_UnExport(Boolean isEnv, const char *arg)
919 {
920         UnexportWhat what;
921         FStr varnames;
922
923         GetVarnamesToUnexport(isEnv, arg, &varnames, &what);
924         UnexportVars(&varnames, what);
925         FStr_Done(&varnames);
926 }
927
928 /*
929  * When there is a variable of the same name in the command line scope, the
930  * global variable would not be visible anywhere.  Therefore there is no
931  * point in setting it at all.
932  *
933  * See 'scope == SCOPE_CMDLINE' in Var_SetWithFlags.
934  */
935 static Boolean
936 ExistsInCmdline(const char *name, const char *val)
937 {
938         Var *v;
939
940         v = VarFind(name, SCOPE_CMDLINE, FALSE);
941         if (v == NULL)
942                 return FALSE;
943
944         if (v->flags & VFL_FROM_CMD) {
945                 DEBUG3(VAR, "%s:%s = %s ignored!\n",
946                     SCOPE_GLOBAL->name, name, val);
947                 return TRUE;
948         }
949
950         VarFreeEnv(v);
951         return FALSE;
952 }
953
954 /* Set the variable to the value; the name is not expanded. */
955 void
956 Var_SetWithFlags(GNode *scope, const char *name, const char *val,
957                  VarSetFlags flags)
958 {
959         Var *v;
960
961         assert(val != NULL);
962         if (name[0] == '\0') {
963                 DEBUG0(VAR, "SetVar: variable name is empty - ignored\n");
964                 return;
965         }
966
967         if (scope == SCOPE_GLOBAL && ExistsInCmdline(name, val))
968                 return;
969
970         /*
971          * Only look for a variable in the given scope since anything set
972          * here will override anything in a lower scope, so there's not much
973          * point in searching them all.
974          */
975         v = VarFind(name, scope, FALSE);
976         if (v == NULL) {
977                 if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) {
978                         /*
979                          * This var would normally prevent the same name being
980                          * added to SCOPE_GLOBAL, so delete it from there if
981                          * needed. Otherwise -V name may show the wrong value.
982                          *
983                          * See ExistsInCmdline.
984                          */
985                         Var_Delete(SCOPE_GLOBAL, name);
986                 }
987                 v = VarAdd(name, val, scope, flags);
988         } else {
989                 if ((v->flags & VFL_READONLY) && !(flags & VAR_SET_READONLY)) {
990                         DEBUG3(VAR, "%s:%s = %s ignored (read-only)\n",
991                             scope->name, name, val);
992                         return;
993                 }
994                 Buf_Empty(&v->val);
995                 Buf_AddStr(&v->val, val);
996
997                 DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, val);
998                 if (v->flags & VFL_EXPORTED)
999                         ExportVar(name, VEM_PLAIN);
1000         }
1001
1002         /*
1003          * Any variables given on the command line are automatically exported
1004          * to the environment (as per POSIX standard), except for internals.
1005          */
1006         if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT) &&
1007             name[0] != '.') {
1008                 v->flags |= VFL_FROM_CMD;
1009
1010                 /*
1011                  * If requested, don't export these in the environment
1012                  * individually.  We still put them in MAKEOVERRIDES so
1013                  * that the command-line settings continue to override
1014                  * Makefile settings.
1015                  */
1016                 if (!opts.varNoExportEnv)
1017                         setenv(name, val, 1);
1018                 /* XXX: What about .MAKE.EXPORTED? */
1019                 /* XXX: Why not just mark the variable for needing export,
1020                  *  as in ExportVarPlain? */
1021
1022                 Global_Append(MAKEOVERRIDES, name);
1023         }
1024
1025         if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0)
1026                 save_dollars = ParseBoolean(val, save_dollars);
1027
1028         if (v != NULL)
1029                 VarFreeEnv(v);
1030 }
1031
1032 /* See Var_Set for documentation. */
1033 void
1034 Var_SetExpandWithFlags(GNode *scope, const char *name, const char *val,
1035                        VarSetFlags flags)
1036 {
1037         const char *unexpanded_name = name;
1038         FStr varname = FStr_InitRefer(name);
1039
1040         assert(val != NULL);
1041
1042         if (strchr(varname.str, '$') != NULL) {
1043                 char *expanded;
1044                 (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded);
1045                 /* TODO: handle errors */
1046                 varname = FStr_InitOwn(expanded);
1047         }
1048
1049         if (varname.str[0] == '\0') {
1050                 DEBUG2(VAR, "Var_Set(\"%s\", \"%s\", ...) "
1051                             "name expands to empty string - ignored\n",
1052                     unexpanded_name, val);
1053         } else
1054                 Var_SetWithFlags(scope, varname.str, val, flags);
1055
1056         FStr_Done(&varname);
1057 }
1058
1059 void
1060 Var_Set(GNode *scope, const char *name, const char *val)
1061 {
1062         Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
1063 }
1064
1065 /*
1066  * Set the variable name to the value val in the given scope.
1067  *
1068  * If the variable doesn't yet exist, it is created.
1069  * Otherwise the new value overwrites and replaces the old value.
1070  *
1071  * Input:
1072  *      name            name of the variable to set, is expanded once
1073  *      val             value to give to the variable
1074  *      scope           scope in which to set it
1075  */
1076 void
1077 Var_SetExpand(GNode *scope, const char *name, const char *val)
1078 {
1079         Var_SetExpandWithFlags(scope, name, val, VAR_SET_NONE);
1080 }
1081
1082 void
1083 Global_Set(const char *name, const char *value)
1084 {
1085         Var_Set(SCOPE_GLOBAL, name, value);
1086 }
1087
1088 void
1089 Global_SetExpand(const char *name, const char *value)
1090 {
1091         Var_SetExpand(SCOPE_GLOBAL, name, value);
1092 }
1093
1094 void
1095 Global_Delete(const char *name)
1096 {
1097         Var_Delete(SCOPE_GLOBAL, name);
1098 }
1099
1100 /*
1101  * Append the value to the named variable.
1102  *
1103  * If the variable doesn't exist, it is created.  Otherwise a single space
1104  * and the given value are appended.
1105  */
1106 void
1107 Var_Append(GNode *scope, const char *name, const char *val)
1108 {
1109         Var *v;
1110
1111         v = VarFind(name, scope, scope == SCOPE_GLOBAL);
1112
1113         if (v == NULL) {
1114                 Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
1115         } else if (v->flags & VFL_READONLY) {
1116                 DEBUG1(VAR, "Ignoring append to %s since it is read-only\n",
1117                     name);
1118         } else if (scope == SCOPE_CMDLINE || !(v->flags & VFL_FROM_CMD)) {
1119                 Buf_AddByte(&v->val, ' ');
1120                 Buf_AddStr(&v->val, val);
1121
1122                 DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, v->val.data);
1123
1124                 if (v->flags & VFL_FROM_ENV) {
1125                         /*
1126                          * If the original variable came from the environment,
1127                          * we have to install it in the global scope (we
1128                          * could place it in the environment, but then we
1129                          * should provide a way to export other variables...)
1130                          */
1131                         v->flags &= ~(unsigned)VFL_FROM_ENV;
1132                         /*
1133                          * This is the only place where a variable is
1134                          * created whose v->name is not the same as
1135                          * scope->vars->key.
1136                          */
1137                         HashTable_Set(&scope->vars, name, v);
1138                 }
1139         }
1140 }
1141
1142 /*
1143  * The variable of the given name has the given value appended to it in the
1144  * given scope.
1145  *
1146  * If the variable doesn't exist, it is created. Otherwise the strings are
1147  * concatenated, with a space in between.
1148  *
1149  * Input:
1150  *      name            name of the variable to modify, is expanded once
1151  *      val             string to append to it
1152  *      scope           scope in which this should occur
1153  *
1154  * Notes:
1155  *      Only if the variable is being sought in the global scope is the
1156  *      environment searched.
1157  *      XXX: Knows its calling circumstances in that if called with scope
1158  *      an actual target, it will only search that scope since only
1159  *      a local variable could be being appended to. This is actually
1160  *      a big win and must be tolerated.
1161  */
1162 void
1163 Var_AppendExpand(GNode *scope, const char *name, const char *val)
1164 {
1165         FStr xname = FStr_InitRefer(name);
1166
1167         assert(val != NULL);
1168
1169         if (strchr(name, '$') != NULL) {
1170                 char *expanded;
1171                 (void)Var_Subst(name, scope, VARE_WANTRES, &expanded);
1172                 /* TODO: handle errors */
1173                 xname = FStr_InitOwn(expanded);
1174                 if (expanded[0] == '\0') {
1175                         /* TODO: update function name in the debug message */
1176                         DEBUG2(VAR, "Var_Append(\"%s\", \"%s\", ...) "
1177                                     "name expands to empty string - ignored\n",
1178                             name, val);
1179                         FStr_Done(&xname);
1180                         return;
1181                 }
1182         }
1183
1184         Var_Append(scope, xname.str, val);
1185
1186         FStr_Done(&xname);
1187 }
1188
1189 void
1190 Global_Append(const char *name, const char *value)
1191 {
1192         Var_Append(SCOPE_GLOBAL, name, value);
1193 }
1194
1195 Boolean
1196 Var_Exists(GNode *scope, const char *name)
1197 {
1198         Var *v = VarFind(name, scope, TRUE);
1199         if (v == NULL)
1200                 return FALSE;
1201
1202         VarFreeEnv(v);
1203         return TRUE;
1204 }
1205
1206 /*
1207  * See if the given variable exists, in the given scope or in other
1208  * fallback scopes.
1209  *
1210  * Input:
1211  *      name            Variable to find, is expanded once
1212  *      scope           Scope in which to start search
1213  */
1214 Boolean
1215 Var_ExistsExpand(GNode *scope, const char *name)
1216 {
1217         FStr varname = FStr_InitRefer(name);
1218         Boolean exists;
1219
1220         if (strchr(varname.str, '$') != NULL) {
1221                 char *expanded;
1222                 (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded);
1223                 /* TODO: handle errors */
1224                 varname = FStr_InitOwn(expanded);
1225         }
1226
1227         exists = Var_Exists(scope, varname.str);
1228         FStr_Done(&varname);
1229         return exists;
1230 }
1231
1232 /*
1233  * Return the unexpanded value of the given variable in the given scope,
1234  * or the usual scopes.
1235  *
1236  * Input:
1237  *      name            name to find, is not expanded any further
1238  *      scope           scope in which to search for it
1239  *
1240  * Results:
1241  *      The value if the variable exists, NULL if it doesn't.
1242  *      The value is valid until the next modification to any variable.
1243  */
1244 FStr
1245 Var_Value(GNode *scope, const char *name)
1246 {
1247         Var *v = VarFind(name, scope, TRUE);
1248         char *value;
1249
1250         if (v == NULL)
1251                 return FStr_InitRefer(NULL);
1252
1253         if (!(v->flags & VFL_FROM_ENV))
1254                 return FStr_InitRefer(v->val.data);
1255
1256         /* Since environment variables are short-lived, free it now. */
1257         FStr_Done(&v->name);
1258         value = Buf_DoneData(&v->val);
1259         free(v);
1260         return FStr_InitOwn(value);
1261 }
1262
1263 /*
1264  * Return the unexpanded variable value from this node, without trying to look
1265  * up the variable in any other scope.
1266  */
1267 const char *
1268 GNode_ValueDirect(GNode *gn, const char *name)
1269 {
1270         Var *v = VarFind(name, gn, FALSE);
1271         return v != NULL ? v->val.data : NULL;
1272 }
1273
1274
1275 static void
1276 SepBuf_Init(SepBuf *buf, char sep)
1277 {
1278         Buf_InitSize(&buf->buf, 32);
1279         buf->needSep = FALSE;
1280         buf->sep = sep;
1281 }
1282
1283 static void
1284 SepBuf_Sep(SepBuf *buf)
1285 {
1286         buf->needSep = TRUE;
1287 }
1288
1289 static void
1290 SepBuf_AddBytes(SepBuf *buf, const char *mem, size_t mem_size)
1291 {
1292         if (mem_size == 0)
1293                 return;
1294         if (buf->needSep && buf->sep != '\0') {
1295                 Buf_AddByte(&buf->buf, buf->sep);
1296                 buf->needSep = FALSE;
1297         }
1298         Buf_AddBytes(&buf->buf, mem, mem_size);
1299 }
1300
1301 static void
1302 SepBuf_AddBytesBetween(SepBuf *buf, const char *start, const char *end)
1303 {
1304         SepBuf_AddBytes(buf, start, (size_t)(end - start));
1305 }
1306
1307 static void
1308 SepBuf_AddStr(SepBuf *buf, const char *str)
1309 {
1310         SepBuf_AddBytes(buf, str, strlen(str));
1311 }
1312
1313 static char *
1314 SepBuf_DoneData(SepBuf *buf)
1315 {
1316         return Buf_DoneData(&buf->buf);
1317 }
1318
1319
1320 /*
1321  * This callback for ModifyWords gets a single word from a variable expression
1322  * and typically adds a modification of this word to the buffer. It may also
1323  * do nothing or add several words.
1324  *
1325  * For example, when evaluating the modifier ':M*b' in ${:Ua b c:M*b}, the
1326  * callback is called 3 times, once for "a", "b" and "c".
1327  */
1328 typedef void (*ModifyWordProc)(const char *word, SepBuf *buf, void *data);
1329
1330
1331 /*
1332  * Callback for ModifyWords to implement the :H modifier.
1333  * Add the dirname of the given word to the buffer.
1334  */
1335 /*ARGSUSED*/
1336 static void
1337 ModifyWord_Head(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1338 {
1339         const char *slash = strrchr(word, '/');
1340         if (slash != NULL)
1341                 SepBuf_AddBytesBetween(buf, word, slash);
1342         else
1343                 SepBuf_AddStr(buf, ".");
1344 }
1345
1346 /*
1347  * Callback for ModifyWords to implement the :T modifier.
1348  * Add the basename of the given word to the buffer.
1349  */
1350 /*ARGSUSED*/
1351 static void
1352 ModifyWord_Tail(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1353 {
1354         SepBuf_AddStr(buf, str_basename(word));
1355 }
1356
1357 /*
1358  * Callback for ModifyWords to implement the :E modifier.
1359  * Add the filename suffix of the given word to the buffer, if it exists.
1360  */
1361 /*ARGSUSED*/
1362 static void
1363 ModifyWord_Suffix(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1364 {
1365         const char *lastDot = strrchr(word, '.');
1366         if (lastDot != NULL)
1367                 SepBuf_AddStr(buf, lastDot + 1);
1368 }
1369
1370 /*
1371  * Callback for ModifyWords to implement the :R modifier.
1372  * Add the filename without extension of the given word to the buffer.
1373  */
1374 /*ARGSUSED*/
1375 static void
1376 ModifyWord_Root(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1377 {
1378         const char *lastDot = strrchr(word, '.');
1379         size_t len = lastDot != NULL ? (size_t)(lastDot - word) : strlen(word);
1380         SepBuf_AddBytes(buf, word, len);
1381 }
1382
1383 /*
1384  * Callback for ModifyWords to implement the :M modifier.
1385  * Place the word in the buffer if it matches the given pattern.
1386  */
1387 static void
1388 ModifyWord_Match(const char *word, SepBuf *buf, void *data)
1389 {
1390         const char *pattern = data;
1391         DEBUG2(VAR, "VarMatch [%s] [%s]\n", word, pattern);
1392         if (Str_Match(word, pattern))
1393                 SepBuf_AddStr(buf, word);
1394 }
1395
1396 /*
1397  * Callback for ModifyWords to implement the :N modifier.
1398  * Place the word in the buffer if it doesn't match the given pattern.
1399  */
1400 static void
1401 ModifyWord_NoMatch(const char *word, SepBuf *buf, void *data)
1402 {
1403         const char *pattern = data;
1404         if (!Str_Match(word, pattern))
1405                 SepBuf_AddStr(buf, word);
1406 }
1407
1408 #ifdef SYSVVARSUB
1409
1410 /*
1411  * Check word against pattern for a match (% is a wildcard).
1412  *
1413  * Input:
1414  *      word            Word to examine
1415  *      pattern         Pattern to examine against
1416  *
1417  * Results:
1418  *      Returns the start of the match, or NULL.
1419  *      out_match_len returns the length of the match, if any.
1420  *      out_hasPercent returns whether the pattern contains a percent.
1421  */
1422 static const char *
1423 SysVMatch(const char *word, const char *pattern,
1424           size_t *out_match_len, Boolean *out_hasPercent)
1425 {
1426         const char *p = pattern;
1427         const char *w = word;
1428         const char *percent;
1429         size_t w_len;
1430         size_t p_len;
1431         const char *w_tail;
1432
1433         *out_hasPercent = FALSE;
1434         percent = strchr(p, '%');
1435         if (percent != NULL) {          /* ${VAR:...%...=...} */
1436                 *out_hasPercent = TRUE;
1437                 if (w[0] == '\0')
1438                         return NULL;    /* empty word does not match pattern */
1439
1440                 /* check that the prefix matches */
1441                 for (; p != percent && *w != '\0' && *w == *p; w++, p++)
1442                         continue;
1443                 if (p != percent)
1444                         return NULL;    /* No match */
1445
1446                 p++;            /* Skip the percent */
1447                 if (*p == '\0') {
1448                         /* No more pattern, return the rest of the string */
1449                         *out_match_len = strlen(w);
1450                         return w;
1451                 }
1452         }
1453
1454         /* Test whether the tail matches */
1455         w_len = strlen(w);
1456         p_len = strlen(p);
1457         if (w_len < p_len)
1458                 return NULL;
1459
1460         w_tail = w + w_len - p_len;
1461         if (memcmp(p, w_tail, p_len) != 0)
1462                 return NULL;
1463
1464         *out_match_len = (size_t)(w_tail - w);
1465         return w;
1466 }
1467
1468 struct ModifyWord_SYSVSubstArgs {
1469         GNode *scope;
1470         const char *lhs;
1471         const char *rhs;
1472 };
1473
1474 /* Callback for ModifyWords to implement the :%.from=%.to modifier. */
1475 static void
1476 ModifyWord_SYSVSubst(const char *word, SepBuf *buf, void *data)
1477 {
1478         const struct ModifyWord_SYSVSubstArgs *args = data;
1479         char *rhs_expanded;
1480         const char *rhs;
1481         const char *percent;
1482
1483         size_t match_len;
1484         Boolean lhsPercent;
1485         const char *match = SysVMatch(word, args->lhs, &match_len, &lhsPercent);
1486         if (match == NULL) {
1487                 SepBuf_AddStr(buf, word);
1488                 return;
1489         }
1490
1491         /*
1492          * Append rhs to the buffer, substituting the first '%' with the
1493          * match, but only if the lhs had a '%' as well.
1494          */
1495
1496         (void)Var_Subst(args->rhs, args->scope, VARE_WANTRES, &rhs_expanded);
1497         /* TODO: handle errors */
1498
1499         rhs = rhs_expanded;
1500         percent = strchr(rhs, '%');
1501
1502         if (percent != NULL && lhsPercent) {
1503                 /* Copy the prefix of the replacement pattern */
1504                 SepBuf_AddBytesBetween(buf, rhs, percent);
1505                 rhs = percent + 1;
1506         }
1507         if (percent != NULL || !lhsPercent)
1508                 SepBuf_AddBytes(buf, match, match_len);
1509
1510         /* Append the suffix of the replacement pattern */
1511         SepBuf_AddStr(buf, rhs);
1512
1513         free(rhs_expanded);
1514 }
1515 #endif
1516
1517
1518 struct ModifyWord_SubstArgs {
1519         const char *lhs;
1520         size_t lhsLen;
1521         const char *rhs;
1522         size_t rhsLen;
1523         VarPatternFlags pflags;
1524         Boolean matched;
1525 };
1526
1527 /*
1528  * Callback for ModifyWords to implement the :S,from,to, modifier.
1529  * Perform a string substitution on the given word.
1530  */
1531 static void
1532 ModifyWord_Subst(const char *word, SepBuf *buf, void *data)
1533 {
1534         size_t wordLen = strlen(word);
1535         struct ModifyWord_SubstArgs *args = data;
1536         const char *match;
1537
1538         if (args->pflags.subOnce && args->matched)
1539                 goto nosub;
1540
1541         if (args->pflags.anchorStart) {
1542                 if (wordLen < args->lhsLen ||
1543                     memcmp(word, args->lhs, args->lhsLen) != 0)
1544                         goto nosub;
1545
1546                 if (args->pflags.anchorEnd && wordLen != args->lhsLen)
1547                         goto nosub;
1548
1549                 /* :S,^prefix,replacement, or :S,^whole$,replacement, */
1550                 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1551                 SepBuf_AddBytesBetween(buf,
1552                     word + args->lhsLen, word + wordLen);
1553                 args->matched = TRUE;
1554                 return;
1555         }
1556
1557         if (args->pflags.anchorEnd) {
1558                 const char *start;
1559
1560                 if (wordLen < args->lhsLen)
1561                         goto nosub;
1562
1563                 start = word + (wordLen - args->lhsLen);
1564                 if (memcmp(start, args->lhs, args->lhsLen) != 0)
1565                         goto nosub;
1566
1567                 /* :S,suffix$,replacement, */
1568                 SepBuf_AddBytesBetween(buf, word, start);
1569                 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1570                 args->matched = TRUE;
1571                 return;
1572         }
1573
1574         if (args->lhs[0] == '\0')
1575                 goto nosub;
1576
1577         /* unanchored case, may match more than once */
1578         while ((match = strstr(word, args->lhs)) != NULL) {
1579                 SepBuf_AddBytesBetween(buf, word, match);
1580                 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1581                 args->matched = TRUE;
1582                 wordLen -= (size_t)(match - word) + args->lhsLen;
1583                 word += (size_t)(match - word) + args->lhsLen;
1584                 if (wordLen == 0 || !args->pflags.subGlobal)
1585                         break;
1586         }
1587 nosub:
1588         SepBuf_AddBytes(buf, word, wordLen);
1589 }
1590
1591 #ifndef NO_REGEX
1592 /* Print the error caused by a regcomp or regexec call. */
1593 static void
1594 VarREError(int reerr, const regex_t *pat, const char *str)
1595 {
1596         size_t errlen = regerror(reerr, pat, NULL, 0);
1597         char *errbuf = bmake_malloc(errlen);
1598         regerror(reerr, pat, errbuf, errlen);
1599         Error("%s: %s", str, errbuf);
1600         free(errbuf);
1601 }
1602
1603 struct ModifyWord_SubstRegexArgs {
1604         regex_t re;
1605         size_t nsub;
1606         char *replace;
1607         VarPatternFlags pflags;
1608         Boolean matched;
1609 };
1610
1611 /*
1612  * Callback for ModifyWords to implement the :C/from/to/ modifier.
1613  * Perform a regex substitution on the given word.
1614  */
1615 static void
1616 ModifyWord_SubstRegex(const char *word, SepBuf *buf, void *data)
1617 {
1618         struct ModifyWord_SubstRegexArgs *args = data;
1619         int xrv;
1620         const char *wp = word;
1621         char *rp;
1622         int flags = 0;
1623         regmatch_t m[10];
1624
1625         if (args->pflags.subOnce && args->matched)
1626                 goto nosub;
1627
1628 tryagain:
1629         xrv = regexec(&args->re, wp, args->nsub, m, flags);
1630
1631         switch (xrv) {
1632         case 0:
1633                 args->matched = TRUE;
1634                 SepBuf_AddBytes(buf, wp, (size_t)m[0].rm_so);
1635
1636                 /*
1637                  * Replacement of regular expressions is not specified by
1638                  * POSIX, therefore implement it here.
1639                  */
1640
1641                 for (rp = args->replace; *rp != '\0'; rp++) {
1642                         if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) {
1643                                 SepBuf_AddBytes(buf, rp + 1, 1);
1644                                 rp++;
1645                                 continue;
1646                         }
1647
1648                         if (*rp == '&') {
1649                                 SepBuf_AddBytesBetween(buf,
1650                                     wp + m[0].rm_so, wp + m[0].rm_eo);
1651                                 continue;
1652                         }
1653
1654                         if (*rp != '\\' || !ch_isdigit(rp[1])) {
1655                                 SepBuf_AddBytes(buf, rp, 1);
1656                                 continue;
1657                         }
1658
1659                         {       /* \0 to \9 backreference */
1660                                 size_t n = (size_t)(rp[1] - '0');
1661                                 rp++;
1662
1663                                 if (n >= args->nsub) {
1664                                         Error("No subexpression \\%u",
1665                                             (unsigned)n);
1666                                 } else if (m[n].rm_so == -1) {
1667                                         Error(
1668                                             "No match for subexpression \\%u",
1669                                             (unsigned)n);
1670                                 } else {
1671                                         SepBuf_AddBytesBetween(buf,
1672                                             wp + m[n].rm_so, wp + m[n].rm_eo);
1673                                 }
1674                         }
1675                 }
1676
1677                 wp += m[0].rm_eo;
1678                 if (args->pflags.subGlobal) {
1679                         flags |= REG_NOTBOL;
1680                         if (m[0].rm_so == 0 && m[0].rm_eo == 0) {
1681                                 SepBuf_AddBytes(buf, wp, 1);
1682                                 wp++;
1683                         }
1684                         if (*wp != '\0')
1685                                 goto tryagain;
1686                 }
1687                 if (*wp != '\0')
1688                         SepBuf_AddStr(buf, wp);
1689                 break;
1690         default:
1691                 VarREError(xrv, &args->re, "Unexpected regex error");
1692                 /* FALLTHROUGH */
1693         case REG_NOMATCH:
1694         nosub:
1695                 SepBuf_AddStr(buf, wp);
1696                 break;
1697         }
1698 }
1699 #endif
1700
1701
1702 struct ModifyWord_LoopArgs {
1703         GNode *scope;
1704         char *tvar;             /* name of temporary variable */
1705         char *str;              /* string to expand */
1706         VarEvalFlags eflags;
1707 };
1708
1709 /* Callback for ModifyWords to implement the :@var@...@ modifier of ODE make. */
1710 static void
1711 ModifyWord_Loop(const char *word, SepBuf *buf, void *data)
1712 {
1713         const struct ModifyWord_LoopArgs *args;
1714         char *s;
1715
1716         if (word[0] == '\0')
1717                 return;
1718
1719         args = data;
1720         /* XXX: The variable name should not be expanded here. */
1721         Var_SetExpandWithFlags(args->scope, args->tvar, word,
1722             VAR_SET_NO_EXPORT);
1723         (void)Var_Subst(args->str, args->scope, args->eflags, &s);
1724         /* TODO: handle errors */
1725
1726         DEBUG4(VAR, "ModifyWord_Loop: "
1727                     "in \"%s\", replace \"%s\" with \"%s\" to \"%s\"\n",
1728             word, args->tvar, args->str, s);
1729
1730         if (s[0] == '\n' || Buf_EndsWith(&buf->buf, '\n'))
1731                 buf->needSep = FALSE;
1732         SepBuf_AddStr(buf, s);
1733         free(s);
1734 }
1735
1736
1737 /*
1738  * The :[first..last] modifier selects words from the expression.
1739  * It can also reverse the words.
1740  */
1741 static char *
1742 VarSelectWords(const char *str, int first, int last,
1743                char sep, Boolean oneBigWord)
1744 {
1745         Words words;
1746         int len, start, end, step;
1747         int i;
1748
1749         SepBuf buf;
1750         SepBuf_Init(&buf, sep);
1751
1752         if (oneBigWord) {
1753                 /* fake what Str_Words() would do if there were only one word */
1754                 words.len = 1;
1755                 words.words = bmake_malloc(
1756                     (words.len + 1) * sizeof(words.words[0]));
1757                 words.freeIt = bmake_strdup(str);
1758                 words.words[0] = words.freeIt;
1759                 words.words[1] = NULL;
1760         } else {
1761                 words = Str_Words(str, FALSE);
1762         }
1763
1764         /*
1765          * Now sanitize the given range.  If first or last are negative,
1766          * convert them to the positive equivalents (-1 gets converted to len,
1767          * -2 gets converted to (len - 1), etc.).
1768          */
1769         len = (int)words.len;
1770         if (first < 0)
1771                 first += len + 1;
1772         if (last < 0)
1773                 last += len + 1;
1774
1775         /* We avoid scanning more of the list than we need to. */
1776         if (first > last) {
1777                 start = (first > len ? len : first) - 1;
1778                 end = last < 1 ? 0 : last - 1;
1779                 step = -1;
1780         } else {
1781                 start = first < 1 ? 0 : first - 1;
1782                 end = last > len ? len : last;
1783                 step = 1;
1784         }
1785
1786         for (i = start; (step < 0) == (i >= end); i += step) {
1787                 SepBuf_AddStr(&buf, words.words[i]);
1788                 SepBuf_Sep(&buf);
1789         }
1790
1791         Words_Free(words);
1792
1793         return SepBuf_DoneData(&buf);
1794 }
1795
1796
1797 /*
1798  * Callback for ModifyWords to implement the :tA modifier.
1799  * Replace each word with the result of realpath() if successful.
1800  */
1801 /*ARGSUSED*/
1802 static void
1803 ModifyWord_Realpath(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
1804 {
1805         struct stat st;
1806         char rbuf[MAXPATHLEN];
1807
1808         const char *rp = cached_realpath(word, rbuf);
1809         if (rp != NULL && *rp == '/' && stat(rp, &st) == 0)
1810                 word = rp;
1811
1812         SepBuf_AddStr(buf, word);
1813 }
1814
1815
1816 static char *
1817 Words_JoinFree(Words words)
1818 {
1819         Buffer buf;
1820         size_t i;
1821
1822         Buf_Init(&buf);
1823
1824         for (i = 0; i < words.len; i++) {
1825                 if (i != 0) {
1826                         /* XXX: Use st->sep instead of ' ', for consistency. */
1827                         Buf_AddByte(&buf, ' ');
1828                 }
1829                 Buf_AddStr(&buf, words.words[i]);
1830         }
1831
1832         Words_Free(words);
1833
1834         return Buf_DoneData(&buf);
1835 }
1836
1837 /* Remove adjacent duplicate words. */
1838 static char *
1839 VarUniq(const char *str)
1840 {
1841         Words words = Str_Words(str, FALSE);
1842
1843         if (words.len > 1) {
1844                 size_t i, j;
1845                 for (j = 0, i = 1; i < words.len; i++)
1846                         if (strcmp(words.words[i], words.words[j]) != 0 &&
1847                             (++j != i))
1848                                 words.words[j] = words.words[i];
1849                 words.len = j + 1;
1850         }
1851
1852         return Words_JoinFree(words);
1853 }
1854
1855
1856 /*
1857  * Quote shell meta-characters and space characters in the string.
1858  * If quoteDollar is set, also quote and double any '$' characters.
1859  */
1860 static char *
1861 VarQuote(const char *str, Boolean quoteDollar)
1862 {
1863         Buffer buf;
1864         Buf_Init(&buf);
1865
1866         for (; *str != '\0'; str++) {
1867                 if (*str == '\n') {
1868                         const char *newline = Shell_GetNewline();
1869                         if (newline == NULL)
1870                                 newline = "\\\n";
1871                         Buf_AddStr(&buf, newline);
1872                         continue;
1873                 }
1874                 if (ch_isspace(*str) || is_shell_metachar((unsigned char)*str))
1875                         Buf_AddByte(&buf, '\\');
1876                 Buf_AddByte(&buf, *str);
1877                 if (quoteDollar && *str == '$')
1878                         Buf_AddStr(&buf, "\\$");
1879         }
1880
1881         return Buf_DoneData(&buf);
1882 }
1883
1884 /*
1885  * Compute the 32-bit hash of the given string, using the MurmurHash3
1886  * algorithm. Output is encoded as 8 hex digits, in Little Endian order.
1887  */
1888 static char *
1889 VarHash(const char *str)
1890 {
1891         static const char hexdigits[16] = "0123456789abcdef";
1892         const unsigned char *ustr = (const unsigned char *)str;
1893
1894         uint32_t h = 0x971e137bU;
1895         uint32_t c1 = 0x95543787U;
1896         uint32_t c2 = 0x2ad7eb25U;
1897         size_t len2 = strlen(str);
1898
1899         char *buf;
1900         size_t i;
1901
1902         size_t len;
1903         for (len = len2; len != 0;) {
1904                 uint32_t k = 0;
1905                 switch (len) {
1906                 default:
1907                         k = ((uint32_t)ustr[3] << 24) |
1908                             ((uint32_t)ustr[2] << 16) |
1909                             ((uint32_t)ustr[1] << 8) |
1910                             (uint32_t)ustr[0];
1911                         len -= 4;
1912                         ustr += 4;
1913                         break;
1914                 case 3:
1915                         k |= (uint32_t)ustr[2] << 16;
1916                         /* FALLTHROUGH */
1917                 case 2:
1918                         k |= (uint32_t)ustr[1] << 8;
1919                         /* FALLTHROUGH */
1920                 case 1:
1921                         k |= (uint32_t)ustr[0];
1922                         len = 0;
1923                 }
1924                 c1 = c1 * 5 + 0x7b7d159cU;
1925                 c2 = c2 * 5 + 0x6bce6396U;
1926                 k *= c1;
1927                 k = (k << 11) ^ (k >> 21);
1928                 k *= c2;
1929                 h = (h << 13) ^ (h >> 19);
1930                 h = h * 5 + 0x52dce729U;
1931                 h ^= k;
1932         }
1933         h ^= (uint32_t)len2;
1934         h *= 0x85ebca6b;
1935         h ^= h >> 13;
1936         h *= 0xc2b2ae35;
1937         h ^= h >> 16;
1938
1939         buf = bmake_malloc(9);
1940         for (i = 0; i < 8; i++) {
1941                 buf[i] = hexdigits[h & 0x0f];
1942                 h >>= 4;
1943         }
1944         buf[8] = '\0';
1945         return buf;
1946 }
1947
1948 static char *
1949 VarStrftime(const char *fmt, Boolean zulu, time_t tim)
1950 {
1951         char buf[BUFSIZ];
1952
1953         if (tim == 0)
1954                 time(&tim);
1955         if (*fmt == '\0')
1956                 fmt = "%c";
1957         strftime(buf, sizeof buf, fmt, zulu ? gmtime(&tim) : localtime(&tim));
1958
1959         buf[sizeof buf - 1] = '\0';
1960         return bmake_strdup(buf);
1961 }
1962
1963 /*
1964  * The ApplyModifier functions take an expression that is being evaluated.
1965  * Their task is to apply a single modifier to the expression.  This involves
1966  * parsing the modifier, evaluating it and finally updating the value of the
1967  * expression.
1968  *
1969  * Parsing the modifier
1970  *
1971  * If parsing succeeds, the parsing position *pp is updated to point to the
1972  * first character following the modifier, which typically is either ':' or
1973  * st->endc.  The modifier doesn't have to check for this delimiter character,
1974  * this is done by ApplyModifiers.
1975  *
1976  * XXX: As of 2020-11-15, some modifiers such as :S, :C, :P, :L do not
1977  * need to be followed by a ':' or endc; this was an unintended mistake.
1978  *
1979  * If parsing fails because of a missing delimiter (as in the :S, :C or :@
1980  * modifiers), return AMR_CLEANUP.
1981  *
1982  * If parsing fails because the modifier is unknown, return AMR_UNKNOWN to
1983  * try the SysV modifier ${VAR:from=to} as fallback.  This should only be
1984  * done as long as there have been no side effects from evaluating nested
1985  * variables, to avoid evaluating them more than once.  In this case, the
1986  * parsing position may or may not be updated.  (XXX: Why not? The original
1987  * parsing position is well-known in ApplyModifiers.)
1988  *
1989  * If parsing fails and the SysV modifier ${VAR:from=to} should not be used
1990  * as a fallback, either issue an error message using Error or Parse_Error
1991  * and then return AMR_CLEANUP, or return AMR_BAD for the default error
1992  * message.  Both of these return values will stop processing the variable
1993  * expression.  (XXX: As of 2020-08-23, evaluation of the whole string
1994  * continues nevertheless after skipping a few bytes, which essentially is
1995  * undefined behavior.  Not in the sense of C, but still the resulting string
1996  * is garbage.)
1997  *
1998  * Evaluating the modifier
1999  *
2000  * After parsing, the modifier is evaluated.  The side effects from evaluating
2001  * nested variable expressions in the modifier text often already happen
2002  * during parsing though.  For most modifiers this doesn't matter since their
2003  * only noticeable effect is that the update the value of the expression.
2004  * Some modifiers such as ':sh' or '::=' have noticeable side effects though.
2005  *
2006  * Evaluating the modifier usually takes the current value of the variable
2007  * expression from st->expr->value, or the variable name from st->var->name
2008  * and stores the result back in expr->value via Expr_SetValueOwn or
2009  * Expr_SetValueRefer.
2010  *
2011  * If evaluating fails (as of 2020-08-23), an error message is printed using
2012  * Error.  This function has no side-effects, it really just prints the error
2013  * message.  Processing the expression continues as if everything were ok.
2014  * XXX: This should be fixed by adding proper error handling to Var_Subst,
2015  * Var_Parse, ApplyModifiers and ModifyWords.
2016  *
2017  * Housekeeping
2018  *
2019  * Some modifiers such as :D and :U turn undefined expressions into defined
2020  * expressions (see Expr_Define).
2021  *
2022  * Some modifiers need to free some memory.
2023  */
2024
2025 typedef enum ExprDefined {
2026         /* The variable expression is based on a regular, defined variable. */
2027         DEF_REGULAR,
2028         /* The variable expression is based on an undefined variable. */
2029         DEF_UNDEF,
2030         /*
2031          * The variable expression started as an undefined expression, but one
2032          * of the modifiers (such as ':D' or ':U') has turned the expression
2033          * from undefined to defined.
2034          */
2035         DEF_DEFINED
2036 } ExprDefined;
2037
2038 static const char *const ExprDefined_Name[] = {
2039         "regular",
2040         "undefined",
2041         "defined"
2042 };
2043
2044 /* A variable expression such as $@ or ${VAR:Mpattern:Q}. */
2045 typedef struct Expr {
2046         Var *var;
2047         FStr value;
2048         VarEvalFlags const eflags;
2049         GNode *const scope;
2050         ExprDefined defined;
2051 } Expr;
2052
2053 /*
2054  * Data that is used when applying a chain of modifiers to an expression.
2055  * For indirect modifiers, the effects of this data stops after the indirect
2056  * modifiers have been applies.
2057  *
2058  * It may or may not be intended that 'status' has scope Expr while 'sep' and
2059  * 'oneBigWord' have smaller scope, terminating at the end of a chain of
2060  * indirect modifiers.
2061  *
2062  * See varmod-indirect.mk.
2063  */
2064 typedef struct ApplyModifiersState {
2065         Expr *expr;
2066         /* '\0' or '{' or '(' */
2067         const char startc;
2068         /* '\0' or '}' or ')' */
2069         const char endc;
2070         /* Word separator in expansions (see the :ts modifier). */
2071         char sep;
2072         /*
2073          * TRUE if some modifiers that otherwise split the variable value
2074          * into words, like :S and :C, treat the variable value as a single
2075          * big word, possibly containing spaces.
2076          */
2077         Boolean oneBigWord;
2078 } ApplyModifiersState;
2079
2080 static void
2081 Expr_Define(Expr *expr)
2082 {
2083         if (expr->defined == DEF_UNDEF)
2084                 expr->defined = DEF_DEFINED;
2085 }
2086
2087 static void
2088 Expr_SetValueOwn(Expr *expr, char *value)
2089 {
2090         FStr_Done(&expr->value);
2091         expr->value = FStr_InitOwn(value);
2092 }
2093
2094 static void
2095 Expr_SetValueRefer(Expr *expr, const char *value)
2096 {
2097         FStr_Done(&expr->value);
2098         expr->value = FStr_InitRefer(value);
2099 }
2100
2101 typedef enum ApplyModifierResult {
2102         /* Continue parsing */
2103         AMR_OK,
2104         /* Not a match, try other modifiers as well. */
2105         AMR_UNKNOWN,
2106         /* Error out with "Bad modifier" message. */
2107         AMR_BAD,
2108         /* Error out without the standard error message. */
2109         AMR_CLEANUP
2110 } ApplyModifierResult;
2111
2112 /*
2113  * Allow backslashes to escape the delimiter, $, and \, but don't touch other
2114  * backslashes.
2115  */
2116 static Boolean
2117 IsEscapedModifierPart(const char *p, char delim,
2118                       struct ModifyWord_SubstArgs *subst)
2119 {
2120         if (p[0] != '\\')
2121                 return FALSE;
2122         if (p[1] == delim || p[1] == '\\' || p[1] == '$')
2123                 return TRUE;
2124         return p[1] == '&' && subst != NULL;
2125 }
2126
2127 /* See ParseModifierPart */
2128 static VarParseResult
2129 ParseModifierPartSubst(
2130     const char **pp,
2131     char delim,
2132     VarEvalFlags eflags,
2133     ApplyModifiersState *st,
2134     char **out_part,
2135     /* Optionally stores the length of the returned string, just to save
2136      * another strlen call. */
2137     size_t *out_length,
2138     /* For the first part of the :S modifier, sets the VARP_ANCHOR_END flag
2139      * if the last character of the pattern is a $. */
2140     VarPatternFlags *out_pflags,
2141     /* For the second part of the :S modifier, allow ampersands to be
2142      * escaped and replace unescaped ampersands with subst->lhs. */
2143     struct ModifyWord_SubstArgs *subst
2144 )
2145 {
2146         Buffer buf;
2147         const char *p;
2148
2149         Buf_Init(&buf);
2150
2151         /*
2152          * Skim through until the matching delimiter is found; pick up
2153          * variable expressions on the way.
2154          */
2155         p = *pp;
2156         while (*p != '\0' && *p != delim) {
2157                 const char *varstart;
2158
2159                 if (IsEscapedModifierPart(p, delim, subst)) {
2160                         Buf_AddByte(&buf, p[1]);
2161                         p += 2;
2162                         continue;
2163                 }
2164
2165                 if (*p != '$') {        /* Unescaped, simple text */
2166                         if (subst != NULL && *p == '&')
2167                                 Buf_AddBytes(&buf, subst->lhs, subst->lhsLen);
2168                         else
2169                                 Buf_AddByte(&buf, *p);
2170                         p++;
2171                         continue;
2172                 }
2173
2174                 if (p[1] == delim) {    /* Unescaped $ at end of pattern */
2175                         if (out_pflags != NULL)
2176                                 out_pflags->anchorEnd = TRUE;
2177                         else
2178                                 Buf_AddByte(&buf, *p);
2179                         p++;
2180                         continue;
2181                 }
2182
2183                 if (eflags & VARE_WANTRES) { /* Nested variable, evaluated */
2184                         const char *nested_p = p;
2185                         FStr nested_val;
2186                         VarEvalFlags nested_eflags =
2187                             eflags & ~(unsigned)VARE_KEEP_DOLLAR;
2188
2189                         (void)Var_Parse(&nested_p, st->expr->scope,
2190                             nested_eflags, &nested_val);
2191                         /* TODO: handle errors */
2192                         Buf_AddStr(&buf, nested_val.str);
2193                         FStr_Done(&nested_val);
2194                         p += nested_p - p;
2195                         continue;
2196                 }
2197
2198                 /*
2199                  * XXX: This whole block is very similar to Var_Parse without
2200                  * VARE_WANTRES.  There may be subtle edge cases though that
2201                  * are not yet covered in the unit tests and that are parsed
2202                  * differently, depending on whether they are evaluated or
2203                  * not.
2204                  *
2205                  * This subtle difference is not documented in the manual
2206                  * page, neither is the difference between parsing :D and
2207                  * :M documented. No code should ever depend on these
2208                  * details, but who knows.
2209                  */
2210
2211                 varstart = p;   /* Nested variable, only parsed */
2212                 if (p[1] == '(' || p[1] == '{') {
2213                         /*
2214                          * Find the end of this variable reference
2215                          * and suck it in without further ado.
2216                          * It will be interpreted later.
2217                          */
2218                         char startc = p[1];
2219                         int endc = startc == '(' ? ')' : '}';
2220                         int depth = 1;
2221
2222                         for (p += 2; *p != '\0' && depth > 0; p++) {
2223                                 if (p[-1] != '\\') {
2224                                         if (*p == startc)
2225                                                 depth++;
2226                                         if (*p == endc)
2227                                                 depth--;
2228                                 }
2229                         }
2230                         Buf_AddBytesBetween(&buf, varstart, p);
2231                 } else {
2232                         Buf_AddByte(&buf, *varstart);
2233                         p++;
2234                 }
2235         }
2236
2237         if (*p != delim) {
2238                 *pp = p;
2239                 Error("Unfinished modifier for \"%s\" ('%c' missing)",
2240                     st->expr->var->name.str, delim);
2241                 *out_part = NULL;
2242                 return VPR_ERR;
2243         }
2244
2245         *pp = p + 1;
2246         if (out_length != NULL)
2247                 *out_length = buf.len;
2248
2249         *out_part = Buf_DoneData(&buf);
2250         DEBUG1(VAR, "Modifier part: \"%s\"\n", *out_part);
2251         return VPR_OK;
2252 }
2253
2254 /*
2255  * Parse a part of a modifier such as the "from" and "to" in :S/from/to/ or
2256  * the "var" or "replacement ${var}" in :@var@replacement ${var}@, up to and
2257  * including the next unescaped delimiter.  The delimiter, as well as the
2258  * backslash or the dollar, can be escaped with a backslash.
2259  *
2260  * Return the parsed (and possibly expanded) string, or NULL if no delimiter
2261  * was found.  On successful return, the parsing position pp points right
2262  * after the delimiter.  The delimiter is not included in the returned
2263  * value though.
2264  */
2265 static VarParseResult
2266 ParseModifierPart(
2267     /* The parsing position, updated upon return */
2268     const char **pp,
2269     /* Parsing stops at this delimiter */
2270     char delim,
2271     /* Flags for evaluating nested variables; if VARE_WANTRES is not set,
2272      * the text is only parsed. */
2273     VarEvalFlags eflags,
2274     ApplyModifiersState *st,
2275     char **out_part
2276 )
2277 {
2278         return ParseModifierPartSubst(pp, delim, eflags, st, out_part,
2279             NULL, NULL, NULL);
2280 }
2281
2282 MAKE_INLINE Boolean
2283 IsDelimiter(char ch, const ApplyModifiersState *st)
2284 {
2285         return ch == ':' || ch == st->endc;
2286 }
2287
2288 /* Test whether mod starts with modname, followed by a delimiter. */
2289 MAKE_INLINE Boolean
2290 ModMatch(const char *mod, const char *modname, const ApplyModifiersState *st)
2291 {
2292         size_t n = strlen(modname);
2293         return strncmp(mod, modname, n) == 0 && IsDelimiter(mod[n], st);
2294 }
2295
2296 /* Test whether mod starts with modname, followed by a delimiter or '='. */
2297 MAKE_INLINE Boolean
2298 ModMatchEq(const char *mod, const char *modname, const ApplyModifiersState *st)
2299 {
2300         size_t n = strlen(modname);
2301         return strncmp(mod, modname, n) == 0 &&
2302                (IsDelimiter(mod[n], st) || mod[n] == '=');
2303 }
2304
2305 static Boolean
2306 TryParseIntBase0(const char **pp, int *out_num)
2307 {
2308         char *end;
2309         long n;
2310
2311         errno = 0;
2312         n = strtol(*pp, &end, 0);
2313
2314         if (end == *pp)
2315                 return FALSE;
2316         if ((n == LONG_MIN || n == LONG_MAX) && errno == ERANGE)
2317                 return FALSE;
2318         if (n < INT_MIN || n > INT_MAX)
2319                 return FALSE;
2320
2321         *pp = end;
2322         *out_num = (int)n;
2323         return TRUE;
2324 }
2325
2326 static Boolean
2327 TryParseSize(const char **pp, size_t *out_num)
2328 {
2329         char *end;
2330         unsigned long n;
2331
2332         if (!ch_isdigit(**pp))
2333                 return FALSE;
2334
2335         errno = 0;
2336         n = strtoul(*pp, &end, 10);
2337         if (n == ULONG_MAX && errno == ERANGE)
2338                 return FALSE;
2339         if (n > SIZE_MAX)
2340                 return FALSE;
2341
2342         *pp = end;
2343         *out_num = (size_t)n;
2344         return TRUE;
2345 }
2346
2347 static Boolean
2348 TryParseChar(const char **pp, int base, char *out_ch)
2349 {
2350         char *end;
2351         unsigned long n;
2352
2353         if (!ch_isalnum(**pp))
2354                 return FALSE;
2355
2356         errno = 0;
2357         n = strtoul(*pp, &end, base);
2358         if (n == ULONG_MAX && errno == ERANGE)
2359                 return FALSE;
2360         if (n > UCHAR_MAX)
2361                 return FALSE;
2362
2363         *pp = end;
2364         *out_ch = (char)n;
2365         return TRUE;
2366 }
2367
2368 /*
2369  * Modify each word of the expression using the given function and place the
2370  * result back in the expression.
2371  */
2372 static void
2373 ModifyWords(ApplyModifiersState *st,
2374             ModifyWordProc modifyWord, void *modifyWord_args,
2375             Boolean oneBigWord)
2376 {
2377         Expr *expr = st->expr;
2378         const char *val = expr->value.str;
2379         SepBuf result;
2380         Words words;
2381         size_t i;
2382
2383         if (oneBigWord) {
2384                 SepBuf_Init(&result, st->sep);
2385                 modifyWord(val, &result, modifyWord_args);
2386                 goto done;
2387         }
2388
2389         words = Str_Words(val, FALSE);
2390
2391         DEBUG2(VAR, "ModifyWords: split \"%s\" into %u words\n",
2392             val, (unsigned)words.len);
2393
2394         SepBuf_Init(&result, st->sep);
2395         for (i = 0; i < words.len; i++) {
2396                 modifyWord(words.words[i], &result, modifyWord_args);
2397                 if (result.buf.len > 0)
2398                         SepBuf_Sep(&result);
2399         }
2400
2401         Words_Free(words);
2402
2403 done:
2404         Expr_SetValueOwn(expr, SepBuf_DoneData(&result));
2405 }
2406
2407 /* :@var@...${var}...@ */
2408 static ApplyModifierResult
2409 ApplyModifier_Loop(const char **pp, ApplyModifiersState *st)
2410 {
2411         Expr *expr = st->expr;
2412         struct ModifyWord_LoopArgs args;
2413         char prev_sep;
2414         VarParseResult res;
2415
2416         args.scope = expr->scope;
2417
2418         (*pp)++;                /* Skip the first '@' */
2419         res = ParseModifierPart(pp, '@', VARE_NONE, st, &args.tvar);
2420         if (res != VPR_OK)
2421                 return AMR_CLEANUP;
2422         if (opts.strict && strchr(args.tvar, '$') != NULL) {
2423                 Parse_Error(PARSE_FATAL,
2424                     "In the :@ modifier of \"%s\", the variable name \"%s\" "
2425                     "must not contain a dollar.",
2426                     expr->var->name.str, args.tvar);
2427                 return AMR_CLEANUP;
2428         }
2429
2430         res = ParseModifierPart(pp, '@', VARE_NONE, st, &args.str);
2431         if (res != VPR_OK)
2432                 return AMR_CLEANUP;
2433
2434         if (!(expr->eflags & VARE_WANTRES))
2435                 goto done;
2436
2437         args.eflags = expr->eflags & ~(unsigned)VARE_KEEP_DOLLAR;
2438         prev_sep = st->sep;
2439         st->sep = ' ';          /* XXX: should be st->sep for consistency */
2440         ModifyWords(st, ModifyWord_Loop, &args, st->oneBigWord);
2441         st->sep = prev_sep;
2442         /* XXX: Consider restoring the previous variable instead of deleting. */
2443         /*
2444          * XXX: The variable name should not be expanded here, see
2445          * ModifyWord_Loop.
2446          */
2447         Var_DeleteExpand(expr->scope, args.tvar);
2448
2449 done:
2450         free(args.tvar);
2451         free(args.str);
2452         return AMR_OK;
2453 }
2454
2455 /* :Ddefined or :Uundefined */
2456 static ApplyModifierResult
2457 ApplyModifier_Defined(const char **pp, ApplyModifiersState *st)
2458 {
2459         Expr *expr = st->expr;
2460         Buffer buf;
2461         const char *p;
2462
2463         VarEvalFlags eflags = VARE_NONE;
2464         if (expr->eflags & VARE_WANTRES)
2465                 if ((**pp == 'D') == (expr->defined == DEF_REGULAR))
2466                         eflags = expr->eflags;
2467
2468         Buf_Init(&buf);
2469         p = *pp + 1;
2470         while (!IsDelimiter(*p, st) && *p != '\0') {
2471
2472                 /* XXX: This code is similar to the one in Var_Parse.
2473                  * See if the code can be merged.
2474                  * See also ApplyModifier_Match and ParseModifierPart. */
2475
2476                 /* Escaped delimiter or other special character */
2477                 /* See Buf_AddEscaped in for.c. */
2478                 if (*p == '\\') {
2479                         char c = p[1];
2480                         if (IsDelimiter(c, st) || c == '$' || c == '\\') {
2481                                 Buf_AddByte(&buf, c);
2482                                 p += 2;
2483                                 continue;
2484                         }
2485                 }
2486
2487                 /* Nested variable expression */
2488                 if (*p == '$') {
2489                         FStr nested_val;
2490
2491                         (void)Var_Parse(&p, expr->scope, eflags, &nested_val);
2492                         /* TODO: handle errors */
2493                         if (expr->eflags & VARE_WANTRES)
2494                                 Buf_AddStr(&buf, nested_val.str);
2495                         FStr_Done(&nested_val);
2496                         continue;
2497                 }
2498
2499                 /* Ordinary text */
2500                 Buf_AddByte(&buf, *p);
2501                 p++;
2502         }
2503         *pp = p;
2504
2505         Expr_Define(expr);
2506
2507         if (eflags & VARE_WANTRES)
2508                 Expr_SetValueOwn(expr, Buf_DoneData(&buf));
2509         else
2510                 Buf_Done(&buf);
2511
2512         return AMR_OK;
2513 }
2514
2515 /* :L */
2516 static ApplyModifierResult
2517 ApplyModifier_Literal(const char **pp, ApplyModifiersState *st)
2518 {
2519         Expr *expr = st->expr;
2520
2521         (*pp)++;
2522
2523         if (expr->eflags & VARE_WANTRES) {
2524                 Expr_Define(expr);
2525                 Expr_SetValueOwn(expr, bmake_strdup(expr->var->name.str));
2526         }
2527
2528         return AMR_OK;
2529 }
2530
2531 static Boolean
2532 TryParseTime(const char **pp, time_t *out_time)
2533 {
2534         char *end;
2535         unsigned long n;
2536
2537         if (!ch_isdigit(**pp))
2538                 return FALSE;
2539
2540         errno = 0;
2541         n = strtoul(*pp, &end, 10);
2542         if (n == ULONG_MAX && errno == ERANGE)
2543                 return FALSE;
2544
2545         *pp = end;
2546         *out_time = (time_t)n;  /* ignore possible truncation for now */
2547         return TRUE;
2548 }
2549
2550 /* :gmtime */
2551 static ApplyModifierResult
2552 ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st)
2553 {
2554         time_t utc;
2555
2556         const char *mod = *pp;
2557         if (!ModMatchEq(mod, "gmtime", st))
2558                 return AMR_UNKNOWN;
2559
2560         if (mod[6] == '=') {
2561                 const char *p = mod + 7;
2562                 if (!TryParseTime(&p, &utc)) {
2563                         Parse_Error(PARSE_FATAL,
2564                             "Invalid time value: %s", mod + 7);
2565                         return AMR_CLEANUP;
2566                 }
2567                 *pp = p;
2568         } else {
2569                 utc = 0;
2570                 *pp = mod + 6;
2571         }
2572
2573         if (st->expr->eflags & VARE_WANTRES)
2574                 Expr_SetValueOwn(st->expr,
2575                     VarStrftime(st->expr->value.str, TRUE, utc));
2576
2577         return AMR_OK;
2578 }
2579
2580 /* :localtime */
2581 static ApplyModifierResult
2582 ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st)
2583 {
2584         time_t utc;
2585
2586         const char *mod = *pp;
2587         if (!ModMatchEq(mod, "localtime", st))
2588                 return AMR_UNKNOWN;
2589
2590         if (mod[9] == '=') {
2591                 const char *p = mod + 10;
2592                 if (!TryParseTime(&p, &utc)) {
2593                         Parse_Error(PARSE_FATAL,
2594                             "Invalid time value: %s", mod + 10);
2595                         return AMR_CLEANUP;
2596                 }
2597                 *pp = p;
2598         } else {
2599                 utc = 0;
2600                 *pp = mod + 9;
2601         }
2602
2603         if (st->expr->eflags & VARE_WANTRES)
2604                 Expr_SetValueOwn(st->expr,
2605                     VarStrftime(st->expr->value.str, FALSE, utc));
2606
2607         return AMR_OK;
2608 }
2609
2610 /* :hash */
2611 static ApplyModifierResult
2612 ApplyModifier_Hash(const char **pp, ApplyModifiersState *st)
2613 {
2614         if (!ModMatch(*pp, "hash", st))
2615                 return AMR_UNKNOWN;
2616         *pp += 4;
2617
2618         if (st->expr->eflags & VARE_WANTRES)
2619                 Expr_SetValueOwn(st->expr, VarHash(st->expr->value.str));
2620
2621         return AMR_OK;
2622 }
2623
2624 /* :P */
2625 static ApplyModifierResult
2626 ApplyModifier_Path(const char **pp, ApplyModifiersState *st)
2627 {
2628         Expr *expr = st->expr;
2629         GNode *gn;
2630         char *path;
2631
2632         (*pp)++;
2633
2634         if (!(st->expr->eflags & VARE_WANTRES))
2635                 return AMR_OK;
2636
2637         Expr_Define(expr);
2638
2639         gn = Targ_FindNode(expr->var->name.str);
2640         if (gn == NULL || gn->type & OP_NOPATH) {
2641                 path = NULL;
2642         } else if (gn->path != NULL) {
2643                 path = bmake_strdup(gn->path);
2644         } else {
2645                 SearchPath *searchPath = Suff_FindPath(gn);
2646                 path = Dir_FindFile(expr->var->name.str, searchPath);
2647         }
2648         if (path == NULL)
2649                 path = bmake_strdup(expr->var->name.str);
2650         Expr_SetValueOwn(expr, path);
2651
2652         return AMR_OK;
2653 }
2654
2655 /* :!cmd! */
2656 static ApplyModifierResult
2657 ApplyModifier_ShellCommand(const char **pp, ApplyModifiersState *st)
2658 {
2659         Expr *expr = st->expr;
2660         char *cmd;
2661         const char *errfmt;
2662         VarParseResult res;
2663
2664         (*pp)++;
2665         res = ParseModifierPart(pp, '!', expr->eflags, st, &cmd);
2666         if (res != VPR_OK)
2667                 return AMR_CLEANUP;
2668
2669         errfmt = NULL;
2670         if (expr->eflags & VARE_WANTRES)
2671                 Expr_SetValueOwn(expr, Cmd_Exec(cmd, &errfmt));
2672         else
2673                 Expr_SetValueRefer(expr, "");
2674         if (errfmt != NULL)
2675                 Error(errfmt, cmd);     /* XXX: why still return AMR_OK? */
2676         free(cmd);
2677         Expr_Define(expr);
2678
2679         return AMR_OK;
2680 }
2681
2682 /*
2683  * The :range modifier generates an integer sequence as long as the words.
2684  * The :range=7 modifier generates an integer sequence from 1 to 7.
2685  */
2686 static ApplyModifierResult
2687 ApplyModifier_Range(const char **pp, ApplyModifiersState *st)
2688 {
2689         size_t n;
2690         Buffer buf;
2691         size_t i;
2692
2693         const char *mod = *pp;
2694         if (!ModMatchEq(mod, "range", st))
2695                 return AMR_UNKNOWN;
2696
2697         if (mod[5] == '=') {
2698                 const char *p = mod + 6;
2699                 if (!TryParseSize(&p, &n)) {
2700                         Parse_Error(PARSE_FATAL,
2701                             "Invalid number \"%s\" for ':range' modifier",
2702                             mod + 6);
2703                         return AMR_CLEANUP;
2704                 }
2705                 *pp = p;
2706         } else {
2707                 n = 0;
2708                 *pp = mod + 5;
2709         }
2710
2711         if (!(st->expr->eflags & VARE_WANTRES))
2712                 return AMR_OK;
2713
2714         if (n == 0) {
2715                 Words words = Str_Words(st->expr->value.str, FALSE);
2716                 n = words.len;
2717                 Words_Free(words);
2718         }
2719
2720         Buf_Init(&buf);
2721
2722         for (i = 0; i < n; i++) {
2723                 if (i != 0) {
2724                         /* XXX: Use st->sep instead of ' ', for consistency. */
2725                         Buf_AddByte(&buf, ' ');
2726                 }
2727                 Buf_AddInt(&buf, 1 + (int)i);
2728         }
2729
2730         Expr_SetValueOwn(st->expr, Buf_DoneData(&buf));
2731         return AMR_OK;
2732 }
2733
2734 /* Parse a ':M' or ':N' modifier. */
2735 static void
2736 ParseModifier_Match(const char **pp, const ApplyModifiersState *st,
2737                     char **out_pattern)
2738 {
2739         const char *mod = *pp;
2740         Expr *expr = st->expr;
2741         Boolean copy = FALSE;   /* pattern should be, or has been, copied */
2742         Boolean needSubst = FALSE;
2743         const char *endpat;
2744         char *pattern;
2745
2746         /*
2747          * In the loop below, ignore ':' unless we are at (or back to) the
2748          * original brace level.
2749          * XXX: This will likely not work right if $() and ${} are intermixed.
2750          */
2751         /*
2752          * XXX: This code is similar to the one in Var_Parse.
2753          * See if the code can be merged.
2754          * See also ApplyModifier_Defined.
2755          */
2756         int nest = 0;
2757         const char *p;
2758         for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) {
2759                 if (*p == '\\' &&
2760                     (IsDelimiter(p[1], st) || p[1] == st->startc)) {
2761                         if (!needSubst)
2762                                 copy = TRUE;
2763                         p++;
2764                         continue;
2765                 }
2766                 if (*p == '$')
2767                         needSubst = TRUE;
2768                 if (*p == '(' || *p == '{')
2769                         nest++;
2770                 if (*p == ')' || *p == '}') {
2771                         nest--;
2772                         if (nest < 0)
2773                                 break;
2774                 }
2775         }
2776         *pp = p;
2777         endpat = p;
2778
2779         if (copy) {
2780                 char *dst;
2781                 const char *src;
2782
2783                 /* Compress the \:'s out of the pattern. */
2784                 pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1);
2785                 dst = pattern;
2786                 src = mod + 1;
2787                 for (; src < endpat; src++, dst++) {
2788                         if (src[0] == '\\' && src + 1 < endpat &&
2789                             /* XXX: st->startc is missing here; see above */
2790                             IsDelimiter(src[1], st))
2791                                 src++;
2792                         *dst = *src;
2793                 }
2794                 *dst = '\0';
2795         } else {
2796                 pattern = bmake_strsedup(mod + 1, endpat);
2797         }
2798
2799         if (needSubst) {
2800                 char *old_pattern = pattern;
2801                 (void)Var_Subst(pattern, expr->scope, expr->eflags, &pattern);
2802                 /* TODO: handle errors */
2803                 free(old_pattern);
2804         }
2805
2806         DEBUG3(VAR, "Pattern[%s] for [%s] is [%s]\n",
2807             expr->var->name.str, expr->value.str, pattern);
2808
2809         *out_pattern = pattern;
2810 }
2811
2812 /* :Mpattern or :Npattern */
2813 static ApplyModifierResult
2814 ApplyModifier_Match(const char **pp, ApplyModifiersState *st)
2815 {
2816         const char mod = **pp;
2817         char *pattern;
2818
2819         ParseModifier_Match(pp, st, &pattern);
2820
2821         if (st->expr->eflags & VARE_WANTRES) {
2822                 ModifyWordProc modifyWord =
2823                     mod == 'M' ? ModifyWord_Match : ModifyWord_NoMatch;
2824                 ModifyWords(st, modifyWord, pattern, st->oneBigWord);
2825         }
2826
2827         free(pattern);
2828         return AMR_OK;
2829 }
2830
2831 static void
2832 ParsePatternFlags(const char **pp, VarPatternFlags *pflags, Boolean *oneBigWord)
2833 {
2834         for (;; (*pp)++) {
2835                 if (**pp == 'g')
2836                         pflags->subGlobal = TRUE;
2837                 else if (**pp == '1')
2838                         pflags->subOnce = TRUE;
2839                 else if (**pp == 'W')
2840                         *oneBigWord = TRUE;
2841                 else
2842                         break;
2843         }
2844 }
2845
2846 /* :S,from,to, */
2847 static ApplyModifierResult
2848 ApplyModifier_Subst(const char **pp, ApplyModifiersState *st)
2849 {
2850         struct ModifyWord_SubstArgs args;
2851         char *lhs, *rhs;
2852         Boolean oneBigWord;
2853         VarParseResult res;
2854
2855         char delim = (*pp)[1];
2856         if (delim == '\0') {
2857                 Error("Missing delimiter for modifier ':S'");
2858                 (*pp)++;
2859                 return AMR_CLEANUP;
2860         }
2861
2862         *pp += 2;
2863
2864         args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE };
2865         args.matched = FALSE;
2866
2867         if (**pp == '^') {
2868                 args.pflags.anchorStart = TRUE;
2869                 (*pp)++;
2870         }
2871
2872         res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &lhs,
2873             &args.lhsLen, &args.pflags, NULL);
2874         if (res != VPR_OK)
2875                 return AMR_CLEANUP;
2876         args.lhs = lhs;
2877
2878         res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &rhs,
2879             &args.rhsLen, NULL, &args);
2880         if (res != VPR_OK)
2881                 return AMR_CLEANUP;
2882         args.rhs = rhs;
2883
2884         oneBigWord = st->oneBigWord;
2885         ParsePatternFlags(pp, &args.pflags, &oneBigWord);
2886
2887         ModifyWords(st, ModifyWord_Subst, &args, oneBigWord);
2888
2889         free(lhs);
2890         free(rhs);
2891         return AMR_OK;
2892 }
2893
2894 #ifndef NO_REGEX
2895
2896 /* :C,from,to, */
2897 static ApplyModifierResult
2898 ApplyModifier_Regex(const char **pp, ApplyModifiersState *st)
2899 {
2900         char *re;
2901         struct ModifyWord_SubstRegexArgs args;
2902         Boolean oneBigWord;
2903         int error;
2904         VarParseResult res;
2905
2906         char delim = (*pp)[1];
2907         if (delim == '\0') {
2908                 Error("Missing delimiter for :C modifier");
2909                 (*pp)++;
2910                 return AMR_CLEANUP;
2911         }
2912
2913         *pp += 2;
2914
2915         res = ParseModifierPart(pp, delim, st->expr->eflags, st, &re);
2916         if (res != VPR_OK)
2917                 return AMR_CLEANUP;
2918
2919         res = ParseModifierPart(pp, delim, st->expr->eflags, st, &args.replace);
2920         if (args.replace == NULL) {
2921                 free(re);
2922                 return AMR_CLEANUP;
2923         }
2924
2925         args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE };
2926         args.matched = FALSE;
2927         oneBigWord = st->oneBigWord;
2928         ParsePatternFlags(pp, &args.pflags, &oneBigWord);
2929
2930         if (!(st->expr->eflags & VARE_WANTRES)) {
2931                 free(args.replace);
2932                 free(re);
2933                 return AMR_OK;
2934         }
2935
2936         error = regcomp(&args.re, re, REG_EXTENDED);
2937         free(re);
2938         if (error != 0) {
2939                 VarREError(error, &args.re, "Regex compilation error");
2940                 free(args.replace);
2941                 return AMR_CLEANUP;
2942         }
2943
2944         args.nsub = args.re.re_nsub + 1;
2945         if (args.nsub > 10)
2946                 args.nsub = 10;
2947
2948         ModifyWords(st, ModifyWord_SubstRegex, &args, oneBigWord);
2949
2950         regfree(&args.re);
2951         free(args.replace);
2952         return AMR_OK;
2953 }
2954
2955 #endif
2956
2957 /* :Q, :q */
2958 static ApplyModifierResult
2959 ApplyModifier_Quote(const char **pp, ApplyModifiersState *st)
2960 {
2961         Boolean quoteDollar = **pp == 'q';
2962         if (!IsDelimiter((*pp)[1], st))
2963                 return AMR_UNKNOWN;
2964         (*pp)++;
2965
2966         if (st->expr->eflags & VARE_WANTRES)
2967                 Expr_SetValueOwn(st->expr,
2968                     VarQuote(st->expr->value.str, quoteDollar));
2969
2970         return AMR_OK;
2971 }
2972
2973 /*ARGSUSED*/
2974 static void
2975 ModifyWord_Copy(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
2976 {
2977         SepBuf_AddStr(buf, word);
2978 }
2979
2980 /* :ts<separator> */
2981 static ApplyModifierResult
2982 ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st)
2983 {
2984         const char *sep = *pp + 2;
2985
2986         /*
2987          * Even if VARE_WANTRES is not set, proceed as normal since there is
2988          * neither any observable side effect nor a performance penalty.
2989          * Checking for VARE_WANTRES for every single piece of code in here
2990          * would make the code in this function too hard to read.
2991          */
2992
2993         /* ":ts<any><endc>" or ":ts<any>:" */
2994         if (sep[0] != st->endc && IsDelimiter(sep[1], st)) {
2995                 *pp = sep + 1;
2996                 st->sep = sep[0];
2997                 goto ok;
2998         }
2999
3000         /* ":ts<endc>" or ":ts:" */
3001         if (IsDelimiter(sep[0], st)) {
3002                 *pp = sep;
3003                 st->sep = '\0'; /* no separator */
3004                 goto ok;
3005         }
3006
3007         /* ":ts<unrecognised><unrecognised>". */
3008         if (sep[0] != '\\') {
3009                 (*pp)++;        /* just for backwards compatibility */
3010                 return AMR_BAD;
3011         }
3012
3013         /* ":ts\n" */
3014         if (sep[1] == 'n') {
3015                 *pp = sep + 2;
3016                 st->sep = '\n';
3017                 goto ok;
3018         }
3019
3020         /* ":ts\t" */
3021         if (sep[1] == 't') {
3022                 *pp = sep + 2;
3023                 st->sep = '\t';
3024                 goto ok;
3025         }
3026
3027         /* ":ts\x40" or ":ts\100" */
3028         {
3029                 const char *p = sep + 1;
3030                 int base = 8;   /* assume octal */
3031
3032                 if (sep[1] == 'x') {
3033                         base = 16;
3034                         p++;
3035                 } else if (!ch_isdigit(sep[1])) {
3036                         (*pp)++;        /* just for backwards compatibility */
3037                         return AMR_BAD; /* ":ts<backslash><unrecognised>". */
3038                 }
3039
3040                 if (!TryParseChar(&p, base, &st->sep)) {
3041                         Parse_Error(PARSE_FATAL,
3042                             "Invalid character number: %s", p);
3043                         return AMR_CLEANUP;
3044                 }
3045                 if (!IsDelimiter(*p, st)) {
3046                         (*pp)++;        /* just for backwards compatibility */
3047                         return AMR_BAD;
3048                 }
3049
3050                 *pp = p;
3051         }
3052
3053 ok:
3054         ModifyWords(st, ModifyWord_Copy, NULL, st->oneBigWord);
3055         return AMR_OK;
3056 }
3057
3058 static char *
3059 str_toupper(const char *str)
3060 {
3061         char *res;
3062         size_t i, len;
3063
3064         len = strlen(str);
3065         res = bmake_malloc(len + 1);
3066         for (i = 0; i < len + 1; i++)
3067                 res[i] = ch_toupper(str[i]);
3068
3069         return res;
3070 }
3071
3072 static char *
3073 str_tolower(const char *str)
3074 {
3075         char *res;
3076         size_t i, len;
3077
3078         len = strlen(str);
3079         res = bmake_malloc(len + 1);
3080         for (i = 0; i < len + 1; i++)
3081                 res[i] = ch_tolower(str[i]);
3082
3083         return res;
3084 }
3085
3086 /* :tA, :tu, :tl, :ts<separator>, etc. */
3087 static ApplyModifierResult
3088 ApplyModifier_To(const char **pp, ApplyModifiersState *st)
3089 {
3090         Expr *expr = st->expr;
3091         const char *mod = *pp;
3092         assert(mod[0] == 't');
3093
3094         if (IsDelimiter(mod[1], st) || mod[1] == '\0') {
3095                 *pp = mod + 1;
3096                 return AMR_BAD; /* Found ":t<endc>" or ":t:". */
3097         }
3098
3099         if (mod[1] == 's')
3100                 return ApplyModifier_ToSep(pp, st);
3101
3102         if (!IsDelimiter(mod[2], st)) {                 /* :t<unrecognized> */
3103                 *pp = mod + 1;
3104                 return AMR_BAD;
3105         }
3106
3107         if (mod[1] == 'A') {                            /* :tA */
3108                 *pp = mod + 2;
3109                 ModifyWords(st, ModifyWord_Realpath, NULL, st->oneBigWord);
3110                 return AMR_OK;
3111         }
3112
3113         if (mod[1] == 'u') {                            /* :tu */
3114                 *pp = mod + 2;
3115                 if (st->expr->eflags & VARE_WANTRES)
3116                         Expr_SetValueOwn(expr, str_toupper(expr->value.str));
3117                 return AMR_OK;
3118         }
3119
3120         if (mod[1] == 'l') {                            /* :tl */
3121                 *pp = mod + 2;
3122                 if (st->expr->eflags & VARE_WANTRES)
3123                         Expr_SetValueOwn(expr, str_tolower(expr->value.str));
3124                 return AMR_OK;
3125         }
3126
3127         if (mod[1] == 'W' || mod[1] == 'w') {           /* :tW, :tw */
3128                 *pp = mod + 2;
3129                 st->oneBigWord = mod[1] == 'W';
3130                 return AMR_OK;
3131         }
3132
3133         /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */
3134         *pp = mod + 1;          /* XXX: unnecessary but observable */
3135         return AMR_BAD;
3136 }
3137
3138 /* :[#], :[1], :[-1..1], etc. */
3139 static ApplyModifierResult
3140 ApplyModifier_Words(const char **pp, ApplyModifiersState *st)
3141 {
3142         Expr *expr = st->expr;
3143         char *estr;
3144         int first, last;
3145         VarParseResult res;
3146         const char *p;
3147
3148         (*pp)++;                /* skip the '[' */
3149         res = ParseModifierPart(pp, ']', expr->eflags, st, &estr);
3150         if (res != VPR_OK)
3151                 return AMR_CLEANUP;
3152
3153         if (!IsDelimiter(**pp, st))
3154                 goto bad_modifier;              /* Found junk after ']' */
3155
3156         if (!(expr->eflags & VARE_WANTRES))
3157                 goto ok;
3158
3159         if (estr[0] == '\0')
3160                 goto bad_modifier;                      /* Found ":[]". */
3161
3162         if (estr[0] == '#' && estr[1] == '\0') {        /* Found ":[#]" */
3163                 if (st->oneBigWord) {
3164                         Expr_SetValueRefer(expr, "1");
3165                 } else {
3166                         Buffer buf;
3167
3168                         Words words = Str_Words(expr->value.str, FALSE);
3169                         size_t ac = words.len;
3170                         Words_Free(words);
3171
3172                         /* 3 digits + '\0' is usually enough */
3173                         Buf_InitSize(&buf, 4);
3174                         Buf_AddInt(&buf, (int)ac);
3175                         Expr_SetValueOwn(expr, Buf_DoneData(&buf));
3176                 }
3177                 goto ok;
3178         }
3179
3180         if (estr[0] == '*' && estr[1] == '\0') {        /* Found ":[*]" */
3181                 st->oneBigWord = TRUE;
3182                 goto ok;
3183         }
3184
3185         if (estr[0] == '@' && estr[1] == '\0') {        /* Found ":[@]" */
3186                 st->oneBigWord = FALSE;
3187                 goto ok;
3188         }
3189
3190         /*
3191          * We expect estr to contain a single integer for :[N], or two
3192          * integers separated by ".." for :[start..end].
3193          */
3194         p = estr;
3195         if (!TryParseIntBase0(&p, &first))
3196                 goto bad_modifier;      /* Found junk instead of a number */
3197
3198         if (p[0] == '\0') {             /* Found only one integer in :[N] */
3199                 last = first;
3200         } else if (p[0] == '.' && p[1] == '.' && p[2] != '\0') {
3201                 /* Expecting another integer after ".." */
3202                 p += 2;
3203                 if (!TryParseIntBase0(&p, &last) || *p != '\0')
3204                         goto bad_modifier; /* Found junk after ".." */
3205         } else
3206                 goto bad_modifier;      /* Found junk instead of ".." */
3207
3208         /*
3209          * Now first and last are properly filled in, but we still have to
3210          * check for 0 as a special case.
3211          */
3212         if (first == 0 && last == 0) {
3213                 /* ":[0]" or perhaps ":[0..0]" */
3214                 st->oneBigWord = TRUE;
3215                 goto ok;
3216         }
3217
3218         /* ":[0..N]" or ":[N..0]" */
3219         if (first == 0 || last == 0)
3220                 goto bad_modifier;
3221
3222         /* Normal case: select the words described by first and last. */
3223         Expr_SetValueOwn(expr,
3224             VarSelectWords(expr->value.str, first, last,
3225                 st->sep, st->oneBigWord));
3226
3227 ok:
3228         free(estr);
3229         return AMR_OK;
3230
3231 bad_modifier:
3232         free(estr);
3233         return AMR_BAD;
3234 }
3235
3236 static int
3237 str_cmp_asc(const void *a, const void *b)
3238 {
3239         return strcmp(*(const char *const *)a, *(const char *const *)b);
3240 }
3241
3242 static int
3243 str_cmp_desc(const void *a, const void *b)
3244 {
3245         return strcmp(*(const char *const *)b, *(const char *const *)a);
3246 }
3247
3248 static void
3249 ShuffleStrings(char **strs, size_t n)
3250 {
3251         size_t i;
3252
3253         for (i = n - 1; i > 0; i--) {
3254                 size_t rndidx = (size_t)random() % (i + 1);
3255                 char *t = strs[i];
3256                 strs[i] = strs[rndidx];
3257                 strs[rndidx] = t;
3258         }
3259 }
3260
3261 /* :O (order ascending) or :Or (order descending) or :Ox (shuffle) */
3262 static ApplyModifierResult
3263 ApplyModifier_Order(const char **pp, ApplyModifiersState *st)
3264 {
3265         const char *mod = (*pp)++;      /* skip past the 'O' in any case */
3266         Words words;
3267         enum SortMode {
3268                 ASC, DESC, SHUFFLE
3269         } mode;
3270
3271         if (IsDelimiter(mod[1], st)) {
3272                 mode = ASC;
3273         } else if ((mod[1] == 'r' || mod[1] == 'x') &&
3274             IsDelimiter(mod[2], st)) {
3275                 (*pp)++;
3276                 mode = mod[1] == 'r' ? DESC : SHUFFLE;
3277         } else
3278                 return AMR_BAD;
3279
3280         if (!(st->expr->eflags & VARE_WANTRES))
3281                 return AMR_OK;
3282
3283         words = Str_Words(st->expr->value.str, FALSE);
3284         if (mode == SHUFFLE)
3285                 ShuffleStrings(words.words, words.len);
3286         else
3287                 qsort(words.words, words.len, sizeof words.words[0],
3288                     mode == ASC ? str_cmp_asc : str_cmp_desc);
3289         Expr_SetValueOwn(st->expr, Words_JoinFree(words));
3290
3291         return AMR_OK;
3292 }
3293
3294 /* :? then : else */
3295 static ApplyModifierResult
3296 ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st)
3297 {
3298         Expr *expr = st->expr;
3299         char *then_expr, *else_expr;
3300         VarParseResult res;
3301
3302         Boolean value = FALSE;
3303         VarEvalFlags then_eflags = VARE_NONE;
3304         VarEvalFlags else_eflags = VARE_NONE;
3305
3306         int cond_rc = COND_PARSE;       /* anything other than COND_INVALID */
3307         if (expr->eflags & VARE_WANTRES) {
3308                 cond_rc = Cond_EvalCondition(expr->var->name.str, &value);
3309                 if (cond_rc != COND_INVALID && value)
3310                         then_eflags = expr->eflags;
3311                 if (cond_rc != COND_INVALID && !value)
3312                         else_eflags = expr->eflags;
3313         }
3314
3315         (*pp)++;                        /* skip past the '?' */
3316         res = ParseModifierPart(pp, ':', then_eflags, st, &then_expr);
3317         if (res != VPR_OK)
3318                 return AMR_CLEANUP;
3319
3320         res = ParseModifierPart(pp, st->endc, else_eflags, st, &else_expr);
3321         if (res != VPR_OK)
3322                 return AMR_CLEANUP;
3323
3324         (*pp)--;                /* Go back to the st->endc. */
3325
3326         if (cond_rc == COND_INVALID) {
3327                 Error("Bad conditional expression `%s' in %s?%s:%s",
3328                     expr->var->name.str, expr->var->name.str,
3329                     then_expr, else_expr);
3330                 return AMR_CLEANUP;
3331         }
3332
3333         if (!(expr->eflags & VARE_WANTRES)) {
3334                 free(then_expr);
3335                 free(else_expr);
3336         } else if (value) {
3337                 Expr_SetValueOwn(expr, then_expr);
3338                 free(else_expr);
3339         } else {
3340                 Expr_SetValueOwn(expr, else_expr);
3341                 free(then_expr);
3342         }
3343         Expr_Define(expr);
3344         return AMR_OK;
3345 }
3346
3347 /*
3348  * The ::= modifiers are special in that they do not read the variable value
3349  * but instead assign to that variable.  They always expand to an empty
3350  * string.
3351  *
3352  * Their main purpose is in supporting .for loops that generate shell commands
3353  * since an ordinary variable assignment at that point would terminate the
3354  * dependency group for these targets.  For example:
3355  *
3356  * list-targets: .USE
3357  * .for i in ${.TARGET} ${.TARGET:R}.gz
3358  *      @${t::=$i}
3359  *      @echo 'The target is ${t:T}.'
3360  * .endfor
3361  *
3362  *        ::=<str>      Assigns <str> as the new value of variable.
3363  *        ::?=<str>     Assigns <str> as value of variable if
3364  *                      it was not already set.
3365  *        ::+=<str>     Appends <str> to variable.
3366  *        ::!=<cmd>     Assigns output of <cmd> as the new value of
3367  *                      variable.
3368  */
3369 static ApplyModifierResult
3370 ApplyModifier_Assign(const char **pp, ApplyModifiersState *st)
3371 {
3372         Expr *expr = st->expr;
3373         GNode *scope;
3374         char *val;
3375         VarParseResult res;
3376
3377         const char *mod = *pp;
3378         const char *op = mod + 1;
3379
3380         if (op[0] == '=')
3381                 goto ok;
3382         if ((op[0] == '!' || op[0] == '+' || op[0] == '?') && op[1] == '=')
3383                 goto ok;
3384         return AMR_UNKNOWN;     /* "::<unrecognised>" */
3385
3386 ok:
3387         if (expr->var->name.str[0] == '\0') {
3388                 *pp = mod + 1;
3389                 return AMR_BAD;
3390         }
3391
3392         switch (op[0]) {
3393         case '+':
3394         case '?':
3395         case '!':
3396                 *pp = mod + 3;
3397                 break;
3398         default:
3399                 *pp = mod + 2;
3400                 break;
3401         }
3402
3403         res = ParseModifierPart(pp, st->endc, expr->eflags, st, &val);
3404         if (res != VPR_OK)
3405                 return AMR_CLEANUP;
3406
3407         (*pp)--;                /* Go back to the st->endc. */
3408
3409         if (!(expr->eflags & VARE_WANTRES))
3410                 goto done;
3411
3412         scope = expr->scope;    /* scope where v belongs */
3413         if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL) {
3414                 Var *gv = VarFind(expr->var->name.str, expr->scope, FALSE);
3415                 if (gv == NULL)
3416                         scope = SCOPE_GLOBAL;
3417                 else
3418                         VarFreeEnv(gv);
3419         }
3420
3421         /* XXX: Expanding the variable name at this point sounds wrong. */
3422         switch (op[0]) {
3423         case '+':
3424                 Var_AppendExpand(scope, expr->var->name.str, val);
3425                 break;
3426         case '!': {
3427                 const char *errfmt;
3428                 char *cmd_output = Cmd_Exec(val, &errfmt);
3429                 if (errfmt != NULL)
3430                         Error(errfmt, val);
3431                 else
3432                         Var_SetExpand(scope, expr->var->name.str, cmd_output);
3433                 free(cmd_output);
3434                 break;
3435         }
3436         case '?':
3437                 if (expr->defined == DEF_REGULAR)
3438                         break;
3439                 /* FALLTHROUGH */
3440         default:
3441                 Var_SetExpand(scope, expr->var->name.str, val);
3442                 break;
3443         }
3444         Expr_SetValueRefer(expr, "");
3445
3446 done:
3447         free(val);
3448         return AMR_OK;
3449 }
3450
3451 /*
3452  * :_=...
3453  * remember current value
3454  */
3455 static ApplyModifierResult
3456 ApplyModifier_Remember(const char **pp, ApplyModifiersState *st)
3457 {
3458         Expr *expr = st->expr;
3459         const char *mod = *pp;
3460         FStr name;
3461
3462         if (!ModMatchEq(mod, "_", st))
3463                 return AMR_UNKNOWN;
3464
3465         name = FStr_InitRefer("_");
3466         if (mod[1] == '=') {
3467                 /*
3468                  * XXX: This ad-hoc call to strcspn deviates from the usual
3469                  * behavior defined in ParseModifierPart.  This creates an
3470                  * unnecessary, undocumented inconsistency in make.
3471                  */
3472                 const char *arg = mod + 2;
3473                 size_t argLen = strcspn(arg, ":)}");
3474                 *pp = arg + argLen;
3475                 name = FStr_InitOwn(bmake_strldup(arg, argLen));
3476         } else
3477                 *pp = mod + 1;
3478
3479         if (expr->eflags & VARE_WANTRES)
3480                 Var_Set(expr->scope, name.str, expr->value.str);
3481         FStr_Done(&name);
3482
3483         return AMR_OK;
3484 }
3485
3486 /*
3487  * Apply the given function to each word of the variable value,
3488  * for a single-letter modifier such as :H, :T.
3489  */
3490 static ApplyModifierResult
3491 ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
3492                        ModifyWordProc modifyWord)
3493 {
3494         if (!IsDelimiter((*pp)[1], st))
3495                 return AMR_UNKNOWN;
3496         (*pp)++;
3497
3498         if (st->expr->eflags & VARE_WANTRES)
3499                 ModifyWords(st, modifyWord, NULL, st->oneBigWord);
3500
3501         return AMR_OK;
3502 }
3503
3504 static ApplyModifierResult
3505 ApplyModifier_Unique(const char **pp, ApplyModifiersState *st)
3506 {
3507         if (!IsDelimiter((*pp)[1], st))
3508                 return AMR_UNKNOWN;
3509         (*pp)++;
3510
3511         if (st->expr->eflags & VARE_WANTRES)
3512                 Expr_SetValueOwn(st->expr, VarUniq(st->expr->value.str));
3513
3514         return AMR_OK;
3515 }
3516
3517 #ifdef SYSVVARSUB
3518 /* :from=to */
3519 static ApplyModifierResult
3520 ApplyModifier_SysV(const char **pp, ApplyModifiersState *st)
3521 {
3522         Expr *expr = st->expr;
3523         char *lhs, *rhs;
3524         VarParseResult res;
3525
3526         const char *mod = *pp;
3527         Boolean eqFound = FALSE;
3528
3529         /*
3530          * First we make a pass through the string trying to verify it is a
3531          * SysV-make-style translation. It must be: <lhs>=<rhs>
3532          */
3533         int depth = 1;
3534         const char *p = mod;
3535         while (*p != '\0' && depth > 0) {
3536                 if (*p == '=') {        /* XXX: should also test depth == 1 */
3537                         eqFound = TRUE;
3538                         /* continue looking for st->endc */
3539                 } else if (*p == st->endc)
3540                         depth--;
3541                 else if (*p == st->startc)
3542                         depth++;
3543                 if (depth > 0)
3544                         p++;
3545         }
3546         if (*p != st->endc || !eqFound)
3547                 return AMR_UNKNOWN;
3548
3549         res = ParseModifierPart(pp, '=', expr->eflags, st, &lhs);
3550         if (res != VPR_OK)
3551                 return AMR_CLEANUP;
3552
3553         /* The SysV modifier lasts until the end of the variable expression. */
3554         res = ParseModifierPart(pp, st->endc, expr->eflags, st, &rhs);
3555         if (res != VPR_OK)
3556                 return AMR_CLEANUP;
3557
3558         (*pp)--;                /* Go back to the st->endc. */
3559
3560         if (lhs[0] == '\0' && expr->value.str[0] == '\0') {
3561                 /* Do not turn an empty expression into non-empty. */
3562         } else {
3563                 struct ModifyWord_SYSVSubstArgs args = {
3564                     expr->scope, lhs, rhs
3565                 };
3566                 ModifyWords(st, ModifyWord_SYSVSubst, &args, st->oneBigWord);
3567         }
3568         free(lhs);
3569         free(rhs);
3570         return AMR_OK;
3571 }
3572 #endif
3573
3574 #ifdef SUNSHCMD
3575 /* :sh */
3576 static ApplyModifierResult
3577 ApplyModifier_SunShell(const char **pp, ApplyModifiersState *st)
3578 {
3579         Expr *expr = st->expr;
3580         const char *p = *pp;
3581         if (!(p[1] == 'h' && IsDelimiter(p[2], st)))
3582                 return AMR_UNKNOWN;
3583         *pp = p + 2;
3584
3585         if (expr->eflags & VARE_WANTRES) {
3586                 const char *errfmt;
3587                 char *output = Cmd_Exec(expr->value.str, &errfmt);
3588                 if (errfmt != NULL)
3589                         Error(errfmt, expr->value.str);
3590                 Expr_SetValueOwn(expr, output);
3591         }
3592
3593         return AMR_OK;
3594 }
3595 #endif
3596
3597 static void
3598 LogBeforeApply(const ApplyModifiersState *st, const char *mod)
3599 {
3600         const Expr *expr = st->expr;
3601         char eflags_str[VarEvalFlags_ToStringSize];
3602         char vflags_str[VarFlags_ToStringSize];
3603         Boolean is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], st);
3604
3605         /* At this point, only the first character of the modifier can
3606          * be used since the end of the modifier is not yet known. */
3607         debug_printf("Applying ${%s:%c%s} to \"%s\" (%s, %s, %s)\n",
3608             expr->var->name.str, mod[0], is_single_char ? "" : "...",
3609             expr->value.str,
3610             VarEvalFlags_ToString(eflags_str, expr->eflags),
3611             VarFlags_ToString(vflags_str, expr->var->flags),
3612             ExprDefined_Name[expr->defined]);
3613 }
3614
3615 static void
3616 LogAfterApply(const ApplyModifiersState *st, const char *p, const char *mod)
3617 {
3618         const Expr *expr = st->expr;
3619         const char *value = expr->value.str;
3620         char eflags_str[VarEvalFlags_ToStringSize];
3621         char vflags_str[VarFlags_ToStringSize];
3622         const char *quot = value == var_Error ? "" : "\"";
3623
3624         debug_printf("Result of ${%s:%.*s} is %s%s%s (%s, %s, %s)\n",
3625             expr->var->name.str, (int)(p - mod), mod,
3626             quot, value == var_Error ? "error" : value, quot,
3627             VarEvalFlags_ToString(eflags_str, expr->eflags),
3628             VarFlags_ToString(vflags_str, expr->var->flags),
3629             ExprDefined_Name[expr->defined]);
3630 }
3631
3632 static ApplyModifierResult
3633 ApplyModifier(const char **pp, ApplyModifiersState *st)
3634 {
3635         switch (**pp) {
3636         case '!':
3637                 return ApplyModifier_ShellCommand(pp, st);
3638         case ':':
3639                 return ApplyModifier_Assign(pp, st);
3640         case '?':
3641                 return ApplyModifier_IfElse(pp, st);
3642         case '@':
3643                 return ApplyModifier_Loop(pp, st);
3644         case '[':
3645                 return ApplyModifier_Words(pp, st);
3646         case '_':
3647                 return ApplyModifier_Remember(pp, st);
3648 #ifndef NO_REGEX
3649         case 'C':
3650                 return ApplyModifier_Regex(pp, st);
3651 #endif
3652         case 'D':
3653                 return ApplyModifier_Defined(pp, st);
3654         case 'E':
3655                 return ApplyModifier_WordFunc(pp, st, ModifyWord_Suffix);
3656         case 'g':
3657                 return ApplyModifier_Gmtime(pp, st);
3658         case 'H':
3659                 return ApplyModifier_WordFunc(pp, st, ModifyWord_Head);
3660         case 'h':
3661                 return ApplyModifier_Hash(pp, st);
3662         case 'L':
3663                 return ApplyModifier_Literal(pp, st);
3664         case 'l':
3665                 return ApplyModifier_Localtime(pp, st);
3666         case 'M':
3667         case 'N':
3668                 return ApplyModifier_Match(pp, st);
3669         case 'O':
3670                 return ApplyModifier_Order(pp, st);
3671         case 'P':
3672                 return ApplyModifier_Path(pp, st);
3673         case 'Q':
3674         case 'q':
3675                 return ApplyModifier_Quote(pp, st);
3676         case 'R':
3677                 return ApplyModifier_WordFunc(pp, st, ModifyWord_Root);
3678         case 'r':
3679                 return ApplyModifier_Range(pp, st);
3680         case 'S':
3681                 return ApplyModifier_Subst(pp, st);
3682 #ifdef SUNSHCMD
3683         case 's':
3684                 return ApplyModifier_SunShell(pp, st);
3685 #endif
3686         case 'T':
3687                 return ApplyModifier_WordFunc(pp, st, ModifyWord_Tail);
3688         case 't':
3689                 return ApplyModifier_To(pp, st);
3690         case 'U':
3691                 return ApplyModifier_Defined(pp, st);
3692         case 'u':
3693                 return ApplyModifier_Unique(pp, st);
3694         default:
3695                 return AMR_UNKNOWN;
3696         }
3697 }
3698
3699 static void ApplyModifiers(Expr *, const char **, char, char);
3700
3701 typedef enum ApplyModifiersIndirectResult {
3702         /* The indirect modifiers have been applied successfully. */
3703         AMIR_CONTINUE,
3704         /* Fall back to the SysV modifier. */
3705         AMIR_SYSV,
3706         /* Error out. */
3707         AMIR_OUT
3708 } ApplyModifiersIndirectResult;
3709
3710 /*
3711  * While expanding a variable expression, expand and apply indirect modifiers,
3712  * such as in ${VAR:${M_indirect}}.
3713  *
3714  * All indirect modifiers of a group must come from a single variable
3715  * expression.  ${VAR:${M1}} is valid but ${VAR:${M1}${M2}} is not.
3716  *
3717  * Multiple groups of indirect modifiers can be chained by separating them
3718  * with colons.  ${VAR:${M1}:${M2}} contains 2 indirect modifiers.
3719  *
3720  * If the variable expression is not followed by st->endc or ':', fall
3721  * back to trying the SysV modifier, such as in ${VAR:${FROM}=${TO}}.
3722  */
3723 static ApplyModifiersIndirectResult
3724 ApplyModifiersIndirect(ApplyModifiersState *st, const char **pp)
3725 {
3726         Expr *expr = st->expr;
3727         const char *p = *pp;
3728         FStr mods;
3729
3730         (void)Var_Parse(&p, expr->scope, expr->eflags, &mods);
3731         /* TODO: handle errors */
3732
3733         if (mods.str[0] != '\0' && *p != '\0' && !IsDelimiter(*p, st)) {
3734                 FStr_Done(&mods);
3735                 return AMIR_SYSV;
3736         }
3737
3738         DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n",
3739             mods.str, (int)(p - *pp), *pp);
3740
3741         if (mods.str[0] != '\0') {
3742                 const char *modsp = mods.str;
3743                 ApplyModifiers(expr, &modsp, '\0', '\0');
3744                 if (expr->value.str == var_Error || *modsp != '\0') {
3745                         FStr_Done(&mods);
3746                         *pp = p;
3747                         return AMIR_OUT;        /* error already reported */
3748                 }
3749         }
3750         FStr_Done(&mods);
3751
3752         if (*p == ':')
3753                 p++;
3754         else if (*p == '\0' && st->endc != '\0') {
3755                 Error("Unclosed variable expression after indirect "
3756                       "modifier, expecting '%c' for variable \"%s\"",
3757                     st->endc, expr->var->name.str);
3758                 *pp = p;
3759                 return AMIR_OUT;
3760         }
3761
3762         *pp = p;
3763         return AMIR_CONTINUE;
3764 }
3765
3766 static ApplyModifierResult
3767 ApplySingleModifier(const char **pp, ApplyModifiersState *st)
3768 {
3769         ApplyModifierResult res;
3770         const char *mod = *pp;
3771         const char *p = *pp;
3772
3773         if (DEBUG(VAR))
3774                 LogBeforeApply(st, mod);
3775
3776         res = ApplyModifier(&p, st);
3777
3778 #ifdef SYSVVARSUB
3779         if (res == AMR_UNKNOWN) {
3780                 assert(p == mod);
3781                 res = ApplyModifier_SysV(&p, st);
3782         }
3783 #endif
3784
3785         if (res == AMR_UNKNOWN) {
3786                 /*
3787                  * Guess the end of the current modifier.
3788                  * XXX: Skipping the rest of the modifier hides
3789                  * errors and leads to wrong results.
3790                  * Parsing should rather stop here.
3791                  */
3792                 for (p++; !IsDelimiter(*p, st) && *p != '\0'; p++)
3793                         continue;
3794                 Parse_Error(PARSE_FATAL, "Unknown modifier \"%.*s\"",
3795                     (int)(p - mod), mod);
3796                 Expr_SetValueRefer(st->expr, var_Error);
3797         }
3798         if (res == AMR_CLEANUP || res == AMR_BAD) {
3799                 *pp = p;
3800                 return res;
3801         }
3802
3803         if (DEBUG(VAR))
3804                 LogAfterApply(st, p, mod);
3805
3806         if (*p == '\0' && st->endc != '\0') {
3807                 Error(
3808                     "Unclosed variable expression, expecting '%c' for "
3809                     "modifier \"%.*s\" of variable \"%s\" with value \"%s\"",
3810                     st->endc,
3811                     (int)(p - mod), mod,
3812                     st->expr->var->name.str, st->expr->value.str);
3813         } else if (*p == ':') {
3814                 p++;
3815         } else if (opts.strict && *p != '\0' && *p != st->endc) {
3816                 Parse_Error(PARSE_FATAL,
3817                     "Missing delimiter ':' after modifier \"%.*s\"",
3818                     (int)(p - mod), mod);
3819                 /*
3820                  * TODO: propagate parse error to the enclosing
3821                  * expression
3822                  */
3823         }
3824         *pp = p;
3825         return AMR_OK;
3826 }
3827
3828 /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */
3829 static void
3830 ApplyModifiers(
3831     Expr *expr,
3832     const char **pp,    /* the parsing position, updated upon return */
3833     char startc,        /* '(' or '{'; or '\0' for indirect modifiers */
3834     char endc           /* ')' or '}'; or '\0' for indirect modifiers */
3835 )
3836 {
3837         ApplyModifiersState st = {
3838             expr,
3839             startc,
3840             endc,
3841             ' ',                /* .sep */
3842             FALSE               /* .oneBigWord */
3843         };
3844         const char *p;
3845         const char *mod;
3846
3847         assert(startc == '(' || startc == '{' || startc == '\0');
3848         assert(endc == ')' || endc == '}' || endc == '\0');
3849         assert(expr->value.str != NULL);
3850
3851         p = *pp;
3852
3853         if (*p == '\0' && endc != '\0') {
3854                 Error(
3855                     "Unclosed variable expression (expecting '%c') for \"%s\"",
3856                     st.endc, expr->var->name.str);
3857                 goto cleanup;
3858         }
3859
3860         while (*p != '\0' && *p != endc) {
3861                 ApplyModifierResult res;
3862
3863                 if (*p == '$') {
3864                         ApplyModifiersIndirectResult amir =
3865                             ApplyModifiersIndirect(&st, &p);
3866                         if (amir == AMIR_CONTINUE)
3867                                 continue;
3868                         if (amir == AMIR_OUT)
3869                                 break;
3870                         /*
3871                          * It's neither '${VAR}:' nor '${VAR}}'.  Try to parse
3872                          * it as a SysV modifier, as that is the only modifier
3873                          * that can start with '$'.
3874                          */
3875                 }
3876
3877                 mod = p;
3878
3879                 res = ApplySingleModifier(&p, &st);
3880                 if (res == AMR_CLEANUP)
3881                         goto cleanup;
3882                 if (res == AMR_BAD)
3883                         goto bad_modifier;
3884         }
3885
3886         *pp = p;
3887         assert(expr->value.str != NULL); /* Use var_Error or varUndefined. */
3888         return;
3889
3890 bad_modifier:
3891         /* XXX: The modifier end is only guessed. */
3892         Error("Bad modifier \":%.*s\" for variable \"%s\"",
3893             (int)strcspn(mod, ":)}"), mod, expr->var->name.str);
3894
3895 cleanup:
3896         /*
3897          * TODO: Use p + strlen(p) instead, to stop parsing immediately.
3898          *
3899          * In the unit tests, this generates a few unterminated strings in the
3900          * shell commands though.  Instead of producing these unfinished
3901          * strings, commands with evaluation errors should not be run at all.
3902          *
3903          * To make that happen, Var_Subst must report the actual errors
3904          * instead of returning VPR_OK unconditionally.
3905          */
3906         *pp = p;
3907         Expr_SetValueRefer(expr, var_Error);
3908 }
3909
3910 /*
3911  * Only four of the local variables are treated specially as they are the
3912  * only four that will be set when dynamic sources are expanded.
3913  */
3914 static Boolean
3915 VarnameIsDynamic(const char *name, size_t len)
3916 {
3917         if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) {
3918                 switch (name[0]) {
3919                 case '@':
3920                 case '%':
3921                 case '*':
3922                 case '!':
3923                         return TRUE;
3924                 }
3925                 return FALSE;
3926         }
3927
3928         if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) {
3929                 return strcmp(name, ".TARGET") == 0 ||
3930                        strcmp(name, ".ARCHIVE") == 0 ||
3931                        strcmp(name, ".PREFIX") == 0 ||
3932                        strcmp(name, ".MEMBER") == 0;
3933         }
3934
3935         return FALSE;
3936 }
3937
3938 static const char *
3939 UndefinedShortVarValue(char varname, const GNode *scope)
3940 {
3941         if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) {
3942                 /*
3943                  * If substituting a local variable in a non-local scope,
3944                  * assume it's for dynamic source stuff. We have to handle
3945                  * this specially and return the longhand for the variable
3946                  * with the dollar sign escaped so it makes it back to the
3947                  * caller. Only four of the local variables are treated
3948                  * specially as they are the only four that will be set
3949                  * when dynamic sources are expanded.
3950                  */
3951                 switch (varname) {
3952                 case '@':
3953                         return "$(.TARGET)";
3954                 case '%':
3955                         return "$(.MEMBER)";
3956                 case '*':
3957                         return "$(.PREFIX)";
3958                 case '!':
3959                         return "$(.ARCHIVE)";
3960                 }
3961         }
3962         return NULL;
3963 }
3964
3965 /*
3966  * Parse a variable name, until the end character or a colon, whichever
3967  * comes first.
3968  */
3969 static char *
3970 ParseVarname(const char **pp, char startc, char endc,
3971              GNode *scope, VarEvalFlags eflags,
3972              size_t *out_varname_len)
3973 {
3974         Buffer buf;
3975         const char *p = *pp;
3976         int depth = 0;          /* Track depth so we can spot parse errors. */
3977
3978         Buf_Init(&buf);
3979
3980         while (*p != '\0') {
3981                 if ((*p == endc || *p == ':') && depth == 0)
3982                         break;
3983                 if (*p == startc)
3984                         depth++;
3985                 if (*p == endc)
3986                         depth--;
3987
3988                 /* A variable inside a variable, expand. */
3989                 if (*p == '$') {
3990                         FStr nested_val;
3991                         (void)Var_Parse(&p, scope, eflags, &nested_val);
3992                         /* TODO: handle errors */
3993                         Buf_AddStr(&buf, nested_val.str);
3994                         FStr_Done(&nested_val);
3995                 } else {
3996                         Buf_AddByte(&buf, *p);
3997                         p++;
3998                 }
3999         }
4000         *pp = p;
4001         *out_varname_len = buf.len;
4002         return Buf_DoneData(&buf);
4003 }
4004
4005 static VarParseResult
4006 ValidShortVarname(char varname, const char *start)
4007 {
4008         if (varname != '$' && varname != ':' && varname != '}' &&
4009             varname != ')' && varname != '\0')
4010                 return VPR_OK;
4011
4012         if (!opts.strict)
4013                 return VPR_ERR; /* XXX: Missing error message */
4014
4015         if (varname == '$')
4016                 Parse_Error(PARSE_FATAL,
4017                     "To escape a dollar, use \\$, not $$, at \"%s\"", start);
4018         else if (varname == '\0')
4019                 Parse_Error(PARSE_FATAL, "Dollar followed by nothing");
4020         else
4021                 Parse_Error(PARSE_FATAL,
4022                     "Invalid variable name '%c', at \"%s\"", varname, start);
4023
4024         return VPR_ERR;
4025 }
4026
4027 /*
4028  * Parse a single-character variable name such as in $V or $@.
4029  * Return whether to continue parsing.
4030  */
4031 static Boolean
4032 ParseVarnameShort(char varname, const char **pp, GNode *scope,
4033                   VarEvalFlags eflags,
4034                   VarParseResult *out_FALSE_res, const char **out_FALSE_val,
4035                   Var **out_TRUE_var)
4036 {
4037         char name[2];
4038         Var *v;
4039         VarParseResult vpr;
4040
4041         vpr = ValidShortVarname(varname, *pp);
4042         if (vpr != VPR_OK) {
4043                 (*pp)++;
4044                 *out_FALSE_res = vpr;
4045                 *out_FALSE_val = var_Error;
4046                 return FALSE;
4047         }
4048
4049         name[0] = varname;
4050         name[1] = '\0';
4051         v = VarFind(name, scope, TRUE);
4052         if (v == NULL) {
4053                 const char *val;
4054                 *pp += 2;
4055
4056                 val = UndefinedShortVarValue(varname, scope);
4057                 if (val == NULL)
4058                         val = eflags & VARE_UNDEFERR ? var_Error : varUndefined;
4059
4060                 if (opts.strict && val == var_Error) {
4061                         Parse_Error(PARSE_FATAL,
4062                             "Variable \"%s\" is undefined", name);
4063                         *out_FALSE_res = VPR_ERR;
4064                         *out_FALSE_val = val;
4065                         return FALSE;
4066                 }
4067
4068                 /*
4069                  * XXX: This looks completely wrong.
4070                  *
4071                  * If undefined expressions are not allowed, this should
4072                  * rather be VPR_ERR instead of VPR_UNDEF, together with an
4073                  * error message.
4074                  *
4075                  * If undefined expressions are allowed, this should rather
4076                  * be VPR_UNDEF instead of VPR_OK.
4077                  */
4078                 *out_FALSE_res = eflags & VARE_UNDEFERR ? VPR_UNDEF : VPR_OK;
4079                 *out_FALSE_val = val;
4080                 return FALSE;
4081         }
4082
4083         *out_TRUE_var = v;
4084         return TRUE;
4085 }
4086
4087 /* Find variables like @F or <D. */
4088 static Var *
4089 FindLocalLegacyVar(const char *varname, size_t namelen, GNode *scope,
4090                    const char **out_extraModifiers)
4091 {
4092         /* Only resolve these variables if scope is a "real" target. */
4093         if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL)
4094                 return NULL;
4095
4096         if (namelen != 2)
4097                 return NULL;
4098         if (varname[1] != 'F' && varname[1] != 'D')
4099                 return NULL;
4100         if (strchr("@%?*!<>", varname[0]) == NULL)
4101                 return NULL;
4102
4103         {
4104                 char name[] = { varname[0], '\0' };
4105                 Var *v = VarFind(name, scope, FALSE);
4106
4107                 if (v != NULL) {
4108                         if (varname[1] == 'D') {
4109                                 *out_extraModifiers = "H:";
4110                         } else { /* F */
4111                                 *out_extraModifiers = "T:";
4112                         }
4113                 }
4114                 return v;
4115         }
4116 }
4117
4118 static VarParseResult
4119 EvalUndefined(Boolean dynamic, const char *start, const char *p, char *varname,
4120               VarEvalFlags eflags,
4121               FStr *out_val)
4122 {
4123         if (dynamic) {
4124                 *out_val = FStr_InitOwn(bmake_strsedup(start, p));
4125                 free(varname);
4126                 return VPR_OK;
4127         }
4128
4129         if ((eflags & VARE_UNDEFERR) && opts.strict) {
4130                 Parse_Error(PARSE_FATAL,
4131                     "Variable \"%s\" is undefined", varname);
4132                 free(varname);
4133                 *out_val = FStr_InitRefer(var_Error);
4134                 return VPR_ERR;
4135         }
4136
4137         if (eflags & VARE_UNDEFERR) {
4138                 free(varname);
4139                 *out_val = FStr_InitRefer(var_Error);
4140                 return VPR_UNDEF;       /* XXX: Should be VPR_ERR instead. */
4141         }
4142
4143         free(varname);
4144         *out_val = FStr_InitRefer(varUndefined);
4145         return VPR_OK;
4146 }
4147
4148 /*
4149  * Parse a long variable name enclosed in braces or parentheses such as $(VAR)
4150  * or ${VAR}, up to the closing brace or parenthesis, or in the case of
4151  * ${VAR:Modifiers}, up to the ':' that starts the modifiers.
4152  * Return whether to continue parsing.
4153  */
4154 static Boolean
4155 ParseVarnameLong(
4156         const char *p,
4157         char startc,
4158         GNode *scope,
4159         VarEvalFlags eflags,
4160
4161         const char **out_FALSE_pp,
4162         VarParseResult *out_FALSE_res,
4163         FStr *out_FALSE_val,
4164
4165         char *out_TRUE_endc,
4166         const char **out_TRUE_p,
4167         Var **out_TRUE_v,
4168         Boolean *out_TRUE_haveModifier,
4169         const char **out_TRUE_extraModifiers,
4170         Boolean *out_TRUE_dynamic,
4171         ExprDefined *out_TRUE_exprDefined
4172 )
4173 {
4174         size_t namelen;
4175         char *varname;
4176         Var *v;
4177         Boolean haveModifier;
4178         Boolean dynamic = FALSE;
4179
4180         const char *const start = p;
4181         char endc = startc == '(' ? ')' : '}';
4182
4183         p += 2;                 /* skip "${" or "$(" or "y(" */
4184         varname = ParseVarname(&p, startc, endc, scope, eflags, &namelen);
4185
4186         if (*p == ':') {
4187                 haveModifier = TRUE;
4188         } else if (*p == endc) {
4189                 haveModifier = FALSE;
4190         } else {
4191                 Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"", varname);
4192                 free(varname);
4193                 *out_FALSE_pp = p;
4194                 *out_FALSE_val = FStr_InitRefer(var_Error);
4195                 *out_FALSE_res = VPR_ERR;
4196                 return FALSE;
4197         }
4198
4199         v = VarFind(varname, scope, TRUE);
4200
4201         /* At this point, p points just after the variable name,
4202          * either at ':' or at endc. */
4203
4204         if (v == NULL) {
4205                 v = FindLocalLegacyVar(varname, namelen, scope,
4206                     out_TRUE_extraModifiers);
4207         }
4208
4209         if (v == NULL) {
4210                 /*
4211                  * Defer expansion of dynamic variables if they appear in
4212                  * non-local scope since they are not defined there.
4213                  */
4214                 dynamic = VarnameIsDynamic(varname, namelen) &&
4215                           (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL);
4216
4217                 if (!haveModifier) {
4218                         p++;    /* skip endc */
4219                         *out_FALSE_pp = p;
4220                         *out_FALSE_res = EvalUndefined(dynamic, start, p,
4221                             varname, eflags, out_FALSE_val);
4222                         return FALSE;
4223                 }
4224
4225                 /*
4226                  * The variable expression is based on an undefined variable.
4227                  * Nevertheless it needs a Var, for modifiers that access the
4228                  * variable name, such as :L or :?.
4229                  *
4230                  * Most modifiers leave this expression in the "undefined"
4231                  * state (VES_UNDEF), only a few modifiers like :D, :U, :L,
4232                  * :P turn this undefined expression into a defined
4233                  * expression (VES_DEF).
4234                  *
4235                  * In the end, after applying all modifiers, if the expression
4236                  * is still undefined, Var_Parse will return an empty string
4237                  * instead of the actually computed value.
4238                  */
4239                 v = VarNew(FStr_InitOwn(varname), "", VFL_NONE);
4240                 *out_TRUE_exprDefined = DEF_UNDEF;
4241         } else
4242                 free(varname);
4243
4244         *out_TRUE_endc = endc;
4245         *out_TRUE_p = p;
4246         *out_TRUE_v = v;
4247         *out_TRUE_haveModifier = haveModifier;
4248         *out_TRUE_dynamic = dynamic;
4249         return TRUE;
4250 }
4251
4252 /* Free the environment variable now since we own it. */
4253 static void
4254 FreeEnvVar(Var *v, FStr *inout_val)
4255 {
4256         char *varValue = Buf_DoneData(&v->val);
4257         if (inout_val->str == varValue)
4258                 inout_val->freeIt = varValue;
4259         else
4260                 free(varValue);
4261
4262         FStr_Done(&v->name);
4263         free(v);
4264 }
4265
4266 /*
4267  * Given the start of a variable expression (such as $v, $(VAR),
4268  * ${VAR:Mpattern}), extract the variable name and value, and the modifiers,
4269  * if any.  While doing that, apply the modifiers to the value of the
4270  * expression, forming its final value.  A few of the modifiers such as :!cmd!
4271  * or ::= have side effects.
4272  *
4273  * Input:
4274  *      *pp             The string to parse.
4275  *                      When parsing a condition in ParseEmptyArg, it may also
4276  *                      point to the "y" of "empty(VARNAME:Modifiers)", which
4277  *                      is syntactically the same.
4278  *      scope           The scope for finding variables
4279  *      eflags          Control the exact details of parsing
4280  *
4281  * Output:
4282  *      *pp             The position where to continue parsing.
4283  *                      TODO: After a parse error, the value of *pp is
4284  *                      unspecified.  It may not have been updated at all,
4285  *                      point to some random character in the string, to the
4286  *                      location of the parse error, or at the end of the
4287  *                      string.
4288  *      *out_val        The value of the variable expression, never NULL.
4289  *      *out_val        var_Error if there was a parse error.
4290  *      *out_val        var_Error if the base variable of the expression was
4291  *                      undefined, eflags contains VARE_UNDEFERR, and none of
4292  *                      the modifiers turned the undefined expression into a
4293  *                      defined expression.
4294  *                      XXX: It is not guaranteed that an error message has
4295  *                      been printed.
4296  *      *out_val        varUndefined if the base variable of the expression
4297  *                      was undefined, eflags did not contain VARE_UNDEFERR,
4298  *                      and none of the modifiers turned the undefined
4299  *                      expression into a defined expression.
4300  *                      XXX: It is not guaranteed that an error message has
4301  *                      been printed.
4302  */
4303 VarParseResult
4304 Var_Parse(const char **pp, GNode *scope, VarEvalFlags eflags, FStr *out_val)
4305 {
4306         const char *p = *pp;
4307         const char *const start = p;
4308         /* TRUE if have modifiers for the variable. */
4309         Boolean haveModifier;
4310         /* Starting character if variable in parens or braces. */
4311         char startc;
4312         /* Ending character if variable in parens or braces. */
4313         char endc;
4314         /*
4315          * TRUE if the variable is local and we're expanding it in a
4316          * non-local scope. This is done to support dynamic sources.
4317          * The result is just the expression, unaltered.
4318          */
4319         Boolean dynamic;
4320         const char *extramodifiers;
4321         char eflags_str[VarEvalFlags_ToStringSize];
4322         Var *v;
4323
4324         Expr expr = {
4325                 NULL,
4326 #if defined(lint)
4327                 /* NetBSD lint cannot fully parse C99 struct initializers. */
4328                 { NULL, NULL },
4329 #else
4330                 FStr_InitRefer(NULL),
4331 #endif
4332                 eflags,
4333                 scope,
4334                 DEF_REGULAR
4335         };
4336
4337         DEBUG2(VAR, "Var_Parse: %s with %s\n", start,
4338             VarEvalFlags_ToString(eflags_str, eflags));
4339
4340         *out_val = FStr_InitRefer(NULL);
4341         extramodifiers = NULL;  /* extra modifiers to apply first */
4342         dynamic = FALSE;
4343
4344         /*
4345          * Appease GCC, which thinks that the variable might not be
4346          * initialized.
4347          */
4348         endc = '\0';
4349
4350         startc = p[1];
4351         if (startc != '(' && startc != '{') {
4352                 VarParseResult res;
4353                 if (!ParseVarnameShort(startc, pp, scope, eflags, &res,
4354                     &out_val->str, &expr.var))
4355                         return res;
4356                 haveModifier = FALSE;
4357                 p++;
4358         } else {
4359                 VarParseResult res;
4360                 if (!ParseVarnameLong(p, startc, scope, eflags,
4361                     pp, &res, out_val,
4362                     &endc, &p, &expr.var, &haveModifier, &extramodifiers,
4363                     &dynamic, &expr.defined))
4364                         return res;
4365         }
4366
4367         v = expr.var;
4368         if (v->flags & VFL_IN_USE)
4369                 Fatal("Variable %s is recursive.", v->name.str);
4370
4371         /*
4372          * XXX: This assignment creates an alias to the current value of the
4373          * variable.  This means that as long as the value of the expression
4374          * stays the same, the value of the variable must not change.
4375          * Using the '::=' modifier, it could be possible to do exactly this.
4376          * At the bottom of this function, the resulting value is compared to
4377          * the then-current value of the variable.  This might also invoke
4378          * undefined behavior.
4379          */
4380         expr.value = FStr_InitRefer(v->val.data);
4381
4382         /*
4383          * Before applying any modifiers, expand any nested expressions from
4384          * the variable value.
4385          */
4386         if (strchr(expr.value.str, '$') != NULL && (eflags & VARE_WANTRES)) {
4387                 char *expanded;
4388                 VarEvalFlags nested_eflags = eflags;
4389                 if (opts.strict)
4390                         nested_eflags &= ~(unsigned)VARE_UNDEFERR;
4391                 v->flags |= VFL_IN_USE;
4392                 (void)Var_Subst(expr.value.str, scope, nested_eflags,
4393                     &expanded);
4394                 v->flags &= ~(unsigned)VFL_IN_USE;
4395                 /* TODO: handle errors */
4396                 Expr_SetValueOwn(&expr, expanded);
4397         }
4398
4399         if (extramodifiers != NULL) {
4400                 const char *em = extramodifiers;
4401                 ApplyModifiers(&expr, &em, '\0', '\0');
4402         }
4403
4404         if (haveModifier) {
4405                 p++;    /* Skip initial colon. */
4406                 ApplyModifiers(&expr, &p, startc, endc);
4407         }
4408
4409         if (*p != '\0')         /* Skip past endc if possible. */
4410                 p++;
4411
4412         *pp = p;
4413
4414         if (v->flags & VFL_FROM_ENV) {
4415                 FreeEnvVar(v, &expr.value);
4416
4417         } else if (expr.defined != DEF_REGULAR) {
4418                 if (expr.defined == DEF_UNDEF) {
4419                         if (dynamic) {
4420                                 Expr_SetValueOwn(&expr,
4421                                     bmake_strsedup(start, p));
4422                         } else {
4423                                 /*
4424                                  * The expression is still undefined,
4425                                  * therefore discard the actual value and
4426                                  * return an error marker instead.
4427                                  */
4428                                 Expr_SetValueRefer(&expr,
4429                                     eflags & VARE_UNDEFERR
4430                                         ? var_Error : varUndefined);
4431                         }
4432                 }
4433                 /* XXX: This is not standard memory management. */
4434                 if (expr.value.str != v->val.data)
4435                         Buf_Done(&v->val);
4436                 FStr_Done(&v->name);
4437                 free(v);
4438         }
4439         *out_val = expr.value;
4440         return VPR_OK;          /* XXX: Is not correct in all cases */
4441 }
4442
4443 static void
4444 VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalFlags eflags)
4445 {
4446         /* A dollar sign may be escaped with another dollar sign. */
4447         if (save_dollars && (eflags & VARE_KEEP_DOLLAR))
4448                 Buf_AddByte(res, '$');
4449         Buf_AddByte(res, '$');
4450         *pp += 2;
4451 }
4452
4453 static void
4454 VarSubstExpr(const char **pp, Buffer *buf, GNode *scope,
4455              VarEvalFlags eflags, Boolean *inout_errorReported)
4456 {
4457         const char *p = *pp;
4458         const char *nested_p = p;
4459         FStr val;
4460
4461         (void)Var_Parse(&nested_p, scope, eflags, &val);
4462         /* TODO: handle errors */
4463
4464         if (val.str == var_Error || val.str == varUndefined) {
4465                 if (!(eflags & VARE_KEEP_UNDEF)) {
4466                         p = nested_p;
4467                 } else if ((eflags & VARE_UNDEFERR) || val.str == var_Error) {
4468
4469                         /*
4470                          * XXX: This condition is wrong.  If val == var_Error,
4471                          * this doesn't necessarily mean there was an undefined
4472                          * variable.  It could equally well be a parse error;
4473                          * see unit-tests/varmod-order.exp.
4474                          */
4475
4476                         /*
4477                          * If variable is undefined, complain and skip the
4478                          * variable. The complaint will stop us from doing
4479                          * anything when the file is parsed.
4480                          */
4481                         if (!*inout_errorReported) {
4482                                 Parse_Error(PARSE_FATAL,
4483                                     "Undefined variable \"%.*s\"",
4484                                     (int)(size_t)(nested_p - p), p);
4485                         }
4486                         p = nested_p;
4487                         *inout_errorReported = TRUE;
4488                 } else {
4489                         /* Copy the initial '$' of the undefined expression,
4490                          * thereby deferring expansion of the expression, but
4491                          * expand nested expressions if already possible.
4492                          * See unit-tests/varparse-undef-partial.mk. */
4493                         Buf_AddByte(buf, *p);
4494                         p++;
4495                 }
4496         } else {
4497                 p = nested_p;
4498                 Buf_AddStr(buf, val.str);
4499         }
4500
4501         FStr_Done(&val);
4502
4503         *pp = p;
4504 }
4505
4506 /*
4507  * Skip as many characters as possible -- either to the end of the string
4508  * or to the next dollar sign (variable expression).
4509  */
4510 static void
4511 VarSubstPlain(const char **pp, Buffer *res)
4512 {
4513         const char *p = *pp;
4514         const char *start = p;
4515
4516         for (p++; *p != '$' && *p != '\0'; p++)
4517                 continue;
4518         Buf_AddBytesBetween(res, start, p);
4519         *pp = p;
4520 }
4521
4522 /*
4523  * Expand all variable expressions like $V, ${VAR}, $(VAR:Modifiers) in the
4524  * given string.
4525  *
4526  * Input:
4527  *      str             The string in which the variable expressions are
4528  *                      expanded.
4529  *      scope           The scope in which to start searching for
4530  *                      variables.  The other scopes are searched as well.
4531  *      eflags          Special effects during expansion.
4532  */
4533 VarParseResult
4534 Var_Subst(const char *str, GNode *scope, VarEvalFlags eflags, char **out_res)
4535 {
4536         const char *p = str;
4537         Buffer res;
4538
4539         /* Set true if an error has already been reported,
4540          * to prevent a plethora of messages when recursing */
4541         /* XXX: Why is the 'static' necessary here? */
4542         static Boolean errorReported;
4543
4544         Buf_Init(&res);
4545         errorReported = FALSE;
4546
4547         while (*p != '\0') {
4548                 if (p[0] == '$' && p[1] == '$')
4549                         VarSubstDollarDollar(&p, &res, eflags);
4550                 else if (p[0] == '$')
4551                         VarSubstExpr(&p, &res, scope, eflags, &errorReported);
4552                 else
4553                         VarSubstPlain(&p, &res);
4554         }
4555
4556         *out_res = Buf_DoneDataCompact(&res);
4557         return VPR_OK;
4558 }
4559
4560 /* Initialize the variables module. */
4561 void
4562 Var_Init(void)
4563 {
4564         SCOPE_INTERNAL = GNode_New("Internal");
4565         SCOPE_GLOBAL = GNode_New("Global");
4566         SCOPE_CMDLINE = GNode_New("Command");
4567 }
4568
4569 /* Clean up the variables module. */
4570 void
4571 Var_End(void)
4572 {
4573         Var_Stats();
4574 }
4575
4576 void
4577 Var_Stats(void)
4578 {
4579         HashTable_DebugStats(&SCOPE_GLOBAL->vars, "Global variables");
4580 }
4581
4582 /* Print all variables in a scope, sorted by name. */
4583 void
4584 Var_Dump(GNode *scope)
4585 {
4586         Vector /* of const char * */ vec;
4587         HashIter hi;
4588         size_t i;
4589         const char **varnames;
4590
4591         Vector_Init(&vec, sizeof(const char *));
4592
4593         HashIter_Init(&hi, &scope->vars);
4594         while (HashIter_Next(&hi) != NULL)
4595                 *(const char **)Vector_Push(&vec) = hi.entry->key;
4596         varnames = vec.items;
4597
4598         qsort(varnames, vec.len, sizeof varnames[0], str_cmp_asc);
4599
4600         for (i = 0; i < vec.len; i++) {
4601                 const char *varname = varnames[i];
4602                 Var *var = HashTable_FindValue(&scope->vars, varname);
4603                 debug_printf("%-16s = %s\n", varname, var->val.data);
4604         }
4605
4606         Vector_Done(&vec);
4607 }