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