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