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