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