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