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