var.c revision 1.528 1 /* $NetBSD: var.c,v 1.528 2020/09/22 06:23:33 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.528 2020/09/22 06:23:33 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 /*-
1589 * Parse a part of a modifier such as the "from" and "to" in :S/from/to/
1590 * or the "var" or "replacement" in :@var@replacement+${var}@, up to and
1591 * including the next unescaped delimiter. The delimiter, as well as the
1592 * backslash or the dollar, can be escaped with a backslash.
1593 *
1594 * Return the parsed (and possibly expanded) string, or NULL if no delimiter
1595 * was found. On successful return, the parsing position pp points right
1596 * after the delimiter. The delimiter is not included in the returned
1597 * value though.
1598 */
1599 static char *
1600 ParseModifierPart(
1601 const char **pp, /* The parsing position, updated upon return */
1602 int delim, /* Parsing stops at this delimiter */
1603 VarEvalFlags eflags, /* Flags for evaluating nested variables;
1604 * if VARE_WANTRES is not set, the text is
1605 * only parsed */
1606 GNode *ctxt, /* For looking up nested variables */
1607 size_t *out_length, /* Optionally stores the length of the returned
1608 * string, just to save another strlen call. */
1609 VarPatternFlags *out_pflags,/* For the first part of the :S modifier,
1610 * sets the VARP_ANCHOR_END flag if the last
1611 * character of the pattern is a $. */
1612 ModifyWord_SubstArgs *subst /* For the second part of the :S modifier,
1613 * allow ampersands to be escaped and replace
1614 * unescaped ampersands with subst->lhs. */
1615 ) {
1616 Buffer buf;
1617 const char *p;
1618 char *rstr;
1619
1620 Buf_Init(&buf, 0);
1621
1622 /*
1623 * Skim through until the matching delimiter is found;
1624 * pick up variable substitutions on the way. Also allow
1625 * backslashes to quote the delimiter, $, and \, but don't
1626 * touch other backslashes.
1627 */
1628 p = *pp;
1629 while (*p != '\0' && *p != delim) {
1630 const char *varstart;
1631
1632 Boolean is_escaped = p[0] == '\\' && (
1633 p[1] == delim || p[1] == '\\' || p[1] == '$' ||
1634 (p[1] == '&' && subst != NULL));
1635 if (is_escaped) {
1636 Buf_AddByte(&buf, p[1]);
1637 p += 2;
1638 continue;
1639 }
1640
1641 if (*p != '$') { /* Unescaped, simple text */
1642 if (subst != NULL && *p == '&')
1643 Buf_AddBytes(&buf, subst->lhs, subst->lhsLen);
1644 else
1645 Buf_AddByte(&buf, *p);
1646 p++;
1647 continue;
1648 }
1649
1650 if (p[1] == delim) { /* Unescaped $ at end of pattern */
1651 if (out_pflags != NULL)
1652 *out_pflags |= VARP_ANCHOR_END;
1653 else
1654 Buf_AddByte(&buf, *p);
1655 p++;
1656 continue;
1657 }
1658
1659 if (eflags & VARE_WANTRES) { /* Nested variable, evaluated */
1660 const char *nested_p = p;
1661 const char *nested_val;
1662 void *nested_val_freeIt;
1663 VarEvalFlags nested_eflags = eflags & ~(unsigned)VARE_ASSIGN;
1664
1665 (void)Var_Parse(&nested_p, ctxt, nested_eflags,
1666 &nested_val, &nested_val_freeIt);
1667 /* TODO: handle errors */
1668 Buf_AddStr(&buf, nested_val);
1669 free(nested_val_freeIt);
1670 p += nested_p - p;
1671 continue;
1672 }
1673
1674 /* XXX: This whole block is very similar to Var_Parse without
1675 * VARE_WANTRES. There may be subtle edge cases though that are
1676 * not yet covered in the unit tests and that are parsed differently,
1677 * depending on whether they are evaluated or not.
1678 *
1679 * This subtle difference is not documented in the manual page,
1680 * neither is the difference between parsing :D and :M documented.
1681 * No code should ever depend on these details, but who knows. */
1682
1683 varstart = p; /* Nested variable, only parsed */
1684 if (p[1] == '(' || p[1] == '{') {
1685 /*
1686 * Find the end of this variable reference
1687 * and suck it in without further ado.
1688 * It will be interpreted later.
1689 */
1690 int have = p[1];
1691 int want = have == '(' ? ')' : '}';
1692 int depth = 1;
1693
1694 for (p += 2; *p != '\0' && depth > 0; p++) {
1695 if (p[-1] != '\\') {
1696 if (*p == have)
1697 depth++;
1698 if (*p == want)
1699 depth--;
1700 }
1701 }
1702 Buf_AddBytesBetween(&buf, varstart, p);
1703 } else {
1704 Buf_AddByte(&buf, *varstart);
1705 p++;
1706 }
1707 }
1708
1709 if (*p != delim) {
1710 *pp = p;
1711 return NULL;
1712 }
1713
1714 *pp = ++p;
1715 if (out_length != NULL)
1716 *out_length = Buf_Size(&buf);
1717
1718 rstr = Buf_Destroy(&buf, FALSE);
1719 VAR_DEBUG("Modifier part: \"%s\"\n", rstr);
1720 return rstr;
1721 }
1722
1723 /* Quote shell meta-characters and space characters in the string.
1724 * If quoteDollar is set, also quote and double any '$' characters. */
1725 static char *
1726 VarQuote(const char *str, Boolean quoteDollar)
1727 {
1728 char *res;
1729 Buffer buf;
1730 Buf_Init(&buf, 0);
1731
1732 for (; *str != '\0'; str++) {
1733 if (*str == '\n') {
1734 const char *newline = Shell_GetNewline();
1735 if (newline == NULL)
1736 newline = "\\\n";
1737 Buf_AddStr(&buf, newline);
1738 continue;
1739 }
1740 if (ch_isspace(*str) || ismeta((unsigned char)*str))
1741 Buf_AddByte(&buf, '\\');
1742 Buf_AddByte(&buf, *str);
1743 if (quoteDollar && *str == '$')
1744 Buf_AddStr(&buf, "\\$");
1745 }
1746
1747 res = Buf_Destroy(&buf, FALSE);
1748 VAR_DEBUG("QuoteMeta: [%s]\n", res);
1749 return res;
1750 }
1751
1752 /* Compute the 32-bit hash of the given string, using the MurmurHash3
1753 * algorithm. Output is encoded as 8 hex digits, in Little Endian order. */
1754 static char *
1755 VarHash(const char *str)
1756 {
1757 static const char hexdigits[16] = "0123456789abcdef";
1758 const unsigned char *ustr = (const unsigned char *)str;
1759
1760 uint32_t h = 0x971e137bU;
1761 uint32_t c1 = 0x95543787U;
1762 uint32_t c2 = 0x2ad7eb25U;
1763 size_t len2 = strlen(str);
1764
1765 char *buf;
1766 size_t i;
1767
1768 size_t len;
1769 for (len = len2; len; ) {
1770 uint32_t k = 0;
1771 switch (len) {
1772 default:
1773 k = ((uint32_t)ustr[3] << 24) |
1774 ((uint32_t)ustr[2] << 16) |
1775 ((uint32_t)ustr[1] << 8) |
1776 (uint32_t)ustr[0];
1777 len -= 4;
1778 ustr += 4;
1779 break;
1780 case 3:
1781 k |= (uint32_t)ustr[2] << 16;
1782 /* FALLTHROUGH */
1783 case 2:
1784 k |= (uint32_t)ustr[1] << 8;
1785 /* FALLTHROUGH */
1786 case 1:
1787 k |= (uint32_t)ustr[0];
1788 len = 0;
1789 }
1790 c1 = c1 * 5 + 0x7b7d159cU;
1791 c2 = c2 * 5 + 0x6bce6396U;
1792 k *= c1;
1793 k = (k << 11) ^ (k >> 21);
1794 k *= c2;
1795 h = (h << 13) ^ (h >> 19);
1796 h = h * 5 + 0x52dce729U;
1797 h ^= k;
1798 }
1799 h ^= (uint32_t)len2;
1800 h *= 0x85ebca6b;
1801 h ^= h >> 13;
1802 h *= 0xc2b2ae35;
1803 h ^= h >> 16;
1804
1805 buf = bmake_malloc(9);
1806 for (i = 0; i < 8; i++) {
1807 buf[i] = hexdigits[h & 0x0f];
1808 h >>= 4;
1809 }
1810 buf[8] = '\0';
1811 return buf;
1812 }
1813
1814 static char *
1815 VarStrftime(const char *fmt, Boolean zulu, time_t tim)
1816 {
1817 char buf[BUFSIZ];
1818
1819 if (!tim)
1820 time(&tim);
1821 if (!*fmt)
1822 fmt = "%c";
1823 strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&tim) : localtime(&tim));
1824
1825 buf[sizeof(buf) - 1] = '\0';
1826 return bmake_strdup(buf);
1827 }
1828
1829 /* The ApplyModifier functions all work in the same way. They get the
1830 * current parsing position (pp) and parse the modifier from there. The
1831 * modifier typically lasts until the next ':', or a closing '}' or ')'
1832 * (taken from st->endc), or the end of the string (parse error).
1833 *
1834 * The high-level behavior of these functions is:
1835 *
1836 * 1. parse the modifier
1837 * 2. evaluate the modifier
1838 * 3. housekeeping
1839 *
1840 * Parsing the modifier
1841 *
1842 * If parsing succeeds, the parsing position *pp is updated to point to the
1843 * first character following the modifier, which typically is either ':' or
1844 * st->endc.
1845 *
1846 * If parsing fails because of a missing delimiter (as in the :S, :C or :@
1847 * modifiers), set st->missing_delim and return AMR_CLEANUP.
1848 *
1849 * If parsing fails because the modifier is unknown, return AMR_UNKNOWN to
1850 * try the SysV modifier ${VAR:from=to} as fallback. This should only be
1851 * done as long as there have been no side effects from evaluating nested
1852 * variables, to avoid evaluating them more than once. In this case, the
1853 * parsing position must not be updated. (XXX: Why not? The original parsing
1854 * position is well-known in ApplyModifiers.)
1855 *
1856 * If parsing fails and the SysV modifier ${VAR:from=to} should not be used
1857 * as a fallback, either issue an error message using Error or Parse_Error
1858 * and then return AMR_CLEANUP, or return AMR_BAD for the default error
1859 * message. Both of these return values will stop processing the variable
1860 * expression. (XXX: As of 2020-08-23, evaluation of the whole string
1861 * continues nevertheless after skipping a few bytes, which essentially is
1862 * undefined behavior. Not in the sense of C, but still it's impossible to
1863 * predict what happens in the parser.)
1864 *
1865 * Evaluating the modifier
1866 *
1867 * After parsing, the modifier is evaluated. The side effects from evaluating
1868 * nested variable expressions in the modifier text often already happen
1869 * during parsing though.
1870 *
1871 * Evaluating the modifier usually takes the current value of the variable
1872 * expression from st->val, or the variable name from st->v->name and stores
1873 * the result in st->newVal.
1874 *
1875 * If evaluating fails (as of 2020-08-23), an error message is printed using
1876 * Error. This function has no side-effects, it really just prints the error
1877 * message. Processing the expression continues as if everything were ok.
1878 * XXX: This should be fixed by adding proper error handling to Var_Subst,
1879 * Var_Parse, ApplyModifiers and ModifyWords.
1880 *
1881 * Housekeeping
1882 *
1883 * Some modifiers such as :D and :U turn undefined expressions into defined
1884 * expressions (see VEF_UNDEF, VEF_DEF).
1885 *
1886 * Some modifiers need to free some memory.
1887 */
1888
1889 typedef enum VarExprFlags {
1890 /* The variable expression is based on an undefined variable. */
1891 VEF_UNDEF = 0x01,
1892 /* The variable expression started as an undefined expression, but one
1893 * of the modifiers (such as :D or :U) has turned the expression from
1894 * undefined to defined. */
1895 VEF_DEF = 0x02
1896 } VarExprFlags;
1897
1898 ENUM_FLAGS_RTTI_2(VarExprFlags,
1899 VEF_UNDEF, VEF_DEF);
1900
1901
1902 typedef struct {
1903 const char startc; /* '\0' or '{' or '(' */
1904 const char endc; /* '\0' or '}' or ')' */
1905 Var * const v;
1906 GNode * const ctxt;
1907 const VarEvalFlags eflags;
1908
1909 char *val; /* The old value of the expression,
1910 * before applying the modifier, never NULL */
1911 char *newVal; /* The new value of the expression,
1912 * after applying the modifier, never NULL */
1913 char missing_delim; /* For error reporting */
1914
1915 char sep; /* Word separator in expansions
1916 * (see the :ts modifier) */
1917 Boolean oneBigWord; /* TRUE if some modifiers that otherwise split
1918 * the variable value into words, like :S and
1919 * :C, treat the variable value as a single big
1920 * word, possibly containing spaces. */
1921 VarExprFlags exprFlags;
1922 } ApplyModifiersState;
1923
1924 static void
1925 ApplyModifiersState_Define(ApplyModifiersState *st)
1926 {
1927 if (st->exprFlags & VEF_UNDEF)
1928 st->exprFlags |= VEF_DEF;
1929 }
1930
1931 typedef enum {
1932 AMR_OK, /* Continue parsing */
1933 AMR_UNKNOWN, /* Not a match, try other modifiers as well */
1934 AMR_BAD, /* Error out with "Bad modifier" message */
1935 AMR_CLEANUP /* Error out, with "Unfinished modifier"
1936 * if st->missing_delim is set. */
1937 } ApplyModifierResult;
1938
1939 /* Test whether mod starts with modname, followed by a delimiter. */
1940 static Boolean
1941 ModMatch(const char *mod, const char *modname, char endc)
1942 {
1943 size_t n = strlen(modname);
1944 return strncmp(mod, modname, n) == 0 &&
1945 (mod[n] == endc || mod[n] == ':');
1946 }
1947
1948 /* Test whether mod starts with modname, followed by a delimiter or '='. */
1949 static inline Boolean
1950 ModMatchEq(const char *mod, const char *modname, char endc)
1951 {
1952 size_t n = strlen(modname);
1953 return strncmp(mod, modname, n) == 0 &&
1954 (mod[n] == endc || mod[n] == ':' || mod[n] == '=');
1955 }
1956
1957 /* :@var (at) ...${var}...@ */
1958 static ApplyModifierResult
1959 ApplyModifier_Loop(const char **pp, ApplyModifiersState *st)
1960 {
1961 ModifyWord_LoopArgs args;
1962 char delim;
1963 char prev_sep;
1964 VarEvalFlags eflags = st->eflags & ~(unsigned)VARE_WANTRES;
1965
1966 args.ctx = st->ctxt;
1967
1968 (*pp)++; /* Skip the first '@' */
1969 delim = '@';
1970 args.tvar = ParseModifierPart(pp, delim, eflags,
1971 st->ctxt, NULL, NULL, NULL);
1972 if (args.tvar == NULL) {
1973 st->missing_delim = delim;
1974 return AMR_CLEANUP;
1975 }
1976 if (DEBUG(LINT) && strchr(args.tvar, '$') != NULL) {
1977 Parse_Error(PARSE_FATAL,
1978 "In the :@ modifier of \"%s\", the variable name \"%s\" "
1979 "must not contain a dollar.",
1980 st->v->name, args.tvar);
1981 return AMR_CLEANUP;
1982 }
1983
1984 args.str = ParseModifierPart(pp, delim, eflags,
1985 st->ctxt, NULL, NULL, NULL);
1986 if (args.str == NULL) {
1987 st->missing_delim = delim;
1988 return AMR_CLEANUP;
1989 }
1990
1991 args.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES);
1992 prev_sep = st->sep;
1993 st->sep = ' '; /* XXX: should be st->sep for consistency */
1994 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
1995 ModifyWord_Loop, &args);
1996 st->sep = prev_sep;
1997 Var_Delete(args.tvar, st->ctxt);
1998 free(args.tvar);
1999 free(args.str);
2000 return AMR_OK;
2001 }
2002
2003 /* :Ddefined or :Uundefined */
2004 static ApplyModifierResult
2005 ApplyModifier_Defined(const char **pp, ApplyModifiersState *st)
2006 {
2007 Buffer buf;
2008 const char *p;
2009
2010 VarEvalFlags eflags = st->eflags & ~(unsigned)VARE_WANTRES;
2011 if (st->eflags & VARE_WANTRES) {
2012 if ((**pp == 'D') == !(st->exprFlags & VEF_UNDEF))
2013 eflags |= VARE_WANTRES;
2014 }
2015
2016 Buf_Init(&buf, 0);
2017 p = *pp + 1;
2018 while (*p != st->endc && *p != ':' && *p != '\0') {
2019
2020 /* Escaped delimiter or other special character */
2021 if (*p == '\\') {
2022 char c = p[1];
2023 if (c == st->endc || c == ':' || c == '$' || c == '\\') {
2024 Buf_AddByte(&buf, c);
2025 p += 2;
2026 continue;
2027 }
2028 }
2029
2030 /* Nested variable expression */
2031 if (*p == '$') {
2032 const char *nested_val;
2033 void *nested_val_freeIt;
2034
2035 (void)Var_Parse(&p, st->ctxt, eflags,
2036 &nested_val, &nested_val_freeIt);
2037 /* TODO: handle errors */
2038 Buf_AddStr(&buf, nested_val);
2039 free(nested_val_freeIt);
2040 continue;
2041 }
2042
2043 /* Ordinary text */
2044 Buf_AddByte(&buf, *p);
2045 p++;
2046 }
2047 *pp = p;
2048
2049 ApplyModifiersState_Define(st);
2050
2051 if (eflags & VARE_WANTRES) {
2052 st->newVal = Buf_Destroy(&buf, FALSE);
2053 } else {
2054 st->newVal = st->val;
2055 Buf_Destroy(&buf, TRUE);
2056 }
2057 return AMR_OK;
2058 }
2059
2060 /* :gmtime */
2061 static ApplyModifierResult
2062 ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st)
2063 {
2064 time_t utc;
2065
2066 const char *mod = *pp;
2067 if (!ModMatchEq(mod, "gmtime", st->endc))
2068 return AMR_UNKNOWN;
2069
2070 if (mod[6] == '=') {
2071 char *ep;
2072 utc = (time_t)strtoul(mod + 7, &ep, 10);
2073 *pp = ep;
2074 } else {
2075 utc = 0;
2076 *pp = mod + 6;
2077 }
2078 st->newVal = VarStrftime(st->val, TRUE, utc);
2079 return AMR_OK;
2080 }
2081
2082 /* :localtime */
2083 static ApplyModifierResult
2084 ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st)
2085 {
2086 time_t utc;
2087
2088 const char *mod = *pp;
2089 if (!ModMatchEq(mod, "localtime", st->endc))
2090 return AMR_UNKNOWN;
2091
2092 if (mod[9] == '=') {
2093 char *ep;
2094 utc = (time_t)strtoul(mod + 10, &ep, 10);
2095 *pp = ep;
2096 } else {
2097 utc = 0;
2098 *pp = mod + 9;
2099 }
2100 st->newVal = VarStrftime(st->val, FALSE, utc);
2101 return AMR_OK;
2102 }
2103
2104 /* :hash */
2105 static ApplyModifierResult
2106 ApplyModifier_Hash(const char **pp, ApplyModifiersState *st)
2107 {
2108 if (!ModMatch(*pp, "hash", st->endc))
2109 return AMR_UNKNOWN;
2110
2111 st->newVal = VarHash(st->val);
2112 *pp += 4;
2113 return AMR_OK;
2114 }
2115
2116 /* :P */
2117 static ApplyModifierResult
2118 ApplyModifier_Path(const char **pp, ApplyModifiersState *st)
2119 {
2120 GNode *gn;
2121 char *path;
2122
2123 ApplyModifiersState_Define(st);
2124
2125 gn = Targ_FindNode(st->v->name, TARG_NOCREATE);
2126 if (gn == NULL || gn->type & OP_NOPATH) {
2127 path = NULL;
2128 } else if (gn->path) {
2129 path = bmake_strdup(gn->path);
2130 } else {
2131 SearchPath *searchPath = Suff_FindPath(gn);
2132 path = Dir_FindFile(st->v->name, searchPath);
2133 }
2134 if (path == NULL)
2135 path = bmake_strdup(st->v->name);
2136 st->newVal = path;
2137
2138 (*pp)++;
2139 return AMR_OK;
2140 }
2141
2142 /* :!cmd! */
2143 static ApplyModifierResult
2144 ApplyModifier_ShellCommand(const char **pp, ApplyModifiersState *st)
2145 {
2146 char delim;
2147 char *cmd;
2148 const char *errfmt;
2149
2150 (*pp)++;
2151 delim = '!';
2152 cmd = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2153 NULL, NULL, NULL);
2154 if (cmd == NULL) {
2155 st->missing_delim = delim;
2156 return AMR_CLEANUP;
2157 }
2158
2159 errfmt = NULL;
2160 if (st->eflags & VARE_WANTRES)
2161 st->newVal = Cmd_Exec(cmd, &errfmt);
2162 else
2163 st->newVal = varNoError;
2164 free(cmd);
2165
2166 if (errfmt != NULL)
2167 Error(errfmt, st->val); /* XXX: why still return AMR_OK? */
2168
2169 ApplyModifiersState_Define(st);
2170 return AMR_OK;
2171 }
2172
2173 /* The :range modifier generates an integer sequence as long as the words.
2174 * The :range=7 modifier generates an integer sequence from 1 to 7. */
2175 static ApplyModifierResult
2176 ApplyModifier_Range(const char **pp, ApplyModifiersState *st)
2177 {
2178 size_t n;
2179 Buffer buf;
2180 size_t i;
2181
2182 const char *mod = *pp;
2183 if (!ModMatchEq(mod, "range", st->endc))
2184 return AMR_UNKNOWN;
2185
2186 if (mod[5] == '=') {
2187 char *ep;
2188 n = (size_t)strtoul(mod + 6, &ep, 10);
2189 *pp = ep;
2190 } else {
2191 n = 0;
2192 *pp = mod + 5;
2193 }
2194
2195 if (n == 0) {
2196 Words words = Str_Words(st->val, FALSE);
2197 n = words.len;
2198 Words_Free(words);
2199 }
2200
2201 Buf_Init(&buf, 0);
2202
2203 for (i = 0; i < n; i++) {
2204 if (i != 0)
2205 Buf_AddByte(&buf, ' '); /* XXX: st->sep, for consistency */
2206 Buf_AddInt(&buf, 1 + (int)i);
2207 }
2208
2209 st->newVal = Buf_Destroy(&buf, FALSE);
2210 return AMR_OK;
2211 }
2212
2213 /* :Mpattern or :Npattern */
2214 static ApplyModifierResult
2215 ApplyModifier_Match(const char **pp, ApplyModifiersState *st)
2216 {
2217 const char *mod = *pp;
2218 Boolean copy = FALSE; /* pattern should be, or has been, copied */
2219 Boolean needSubst = FALSE;
2220 const char *endpat;
2221 char *pattern;
2222 ModifyWordsCallback callback;
2223
2224 /*
2225 * In the loop below, ignore ':' unless we are at (or back to) the
2226 * original brace level.
2227 * XXX This will likely not work right if $() and ${} are intermixed.
2228 */
2229 int nest = 0;
2230 const char *p;
2231 for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) {
2232 if (*p == '\\' &&
2233 (p[1] == ':' || p[1] == st->endc || p[1] == st->startc)) {
2234 if (!needSubst)
2235 copy = TRUE;
2236 p++;
2237 continue;
2238 }
2239 if (*p == '$')
2240 needSubst = TRUE;
2241 if (*p == '(' || *p == '{')
2242 nest++;
2243 if (*p == ')' || *p == '}') {
2244 nest--;
2245 if (nest < 0)
2246 break;
2247 }
2248 }
2249 *pp = p;
2250 endpat = p;
2251
2252 if (copy) {
2253 char *dst;
2254 const char *src;
2255
2256 /* Compress the \:'s out of the pattern. */
2257 pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1);
2258 dst = pattern;
2259 src = mod + 1;
2260 for (; src < endpat; src++, dst++) {
2261 if (src[0] == '\\' && src + 1 < endpat &&
2262 /* XXX: st->startc is missing here; see above */
2263 (src[1] == ':' || src[1] == st->endc))
2264 src++;
2265 *dst = *src;
2266 }
2267 *dst = '\0';
2268 endpat = dst;
2269 } else {
2270 pattern = bmake_strsedup(mod + 1, endpat);
2271 }
2272
2273 if (needSubst) {
2274 /* pattern contains embedded '$', so use Var_Subst to expand it. */
2275 char *old_pattern = pattern;
2276 pattern = Var_Subst(pattern, st->ctxt, st->eflags);
2277 free(old_pattern);
2278 }
2279
2280 VAR_DEBUG("Pattern[%s] for [%s] is [%s]\n", st->v->name, st->val, pattern);
2281
2282 callback = mod[0] == 'M' ? ModifyWord_Match : ModifyWord_NoMatch;
2283 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2284 callback, pattern);
2285 free(pattern);
2286 return AMR_OK;
2287 }
2288
2289 /* :S,from,to, */
2290 static ApplyModifierResult
2291 ApplyModifier_Subst(const char **pp, ApplyModifiersState *st)
2292 {
2293 ModifyWord_SubstArgs args;
2294 char *lhs, *rhs;
2295 Boolean oneBigWord;
2296
2297 char delim = (*pp)[1];
2298 if (delim == '\0') {
2299 Error("Missing delimiter for :S modifier");
2300 (*pp)++;
2301 return AMR_CLEANUP;
2302 }
2303
2304 *pp += 2;
2305
2306 args.pflags = 0;
2307 args.matched = FALSE;
2308
2309 /*
2310 * If pattern begins with '^', it is anchored to the
2311 * start of the word -- skip over it and flag pattern.
2312 */
2313 if (**pp == '^') {
2314 args.pflags |= VARP_ANCHOR_START;
2315 (*pp)++;
2316 }
2317
2318 lhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2319 &args.lhsLen, &args.pflags, NULL);
2320 if (lhs == NULL) {
2321 st->missing_delim = delim;
2322 return AMR_CLEANUP;
2323 }
2324 args.lhs = lhs;
2325
2326 rhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2327 &args.rhsLen, NULL, &args);
2328 if (rhs == NULL) {
2329 st->missing_delim = delim;
2330 return AMR_CLEANUP;
2331 }
2332 args.rhs = rhs;
2333
2334 oneBigWord = st->oneBigWord;
2335 for (;; (*pp)++) {
2336 switch (**pp) {
2337 case 'g':
2338 args.pflags |= VARP_SUB_GLOBAL;
2339 continue;
2340 case '1':
2341 args.pflags |= VARP_SUB_ONE;
2342 continue;
2343 case 'W':
2344 oneBigWord = TRUE;
2345 continue;
2346 }
2347 break;
2348 }
2349
2350 st->newVal = ModifyWords(st->ctxt, st->sep, oneBigWord, st->val,
2351 ModifyWord_Subst, &args);
2352
2353 free(lhs);
2354 free(rhs);
2355 return AMR_OK;
2356 }
2357
2358 #ifndef NO_REGEX
2359
2360 /* :C,from,to, */
2361 static ApplyModifierResult
2362 ApplyModifier_Regex(const char **pp, ApplyModifiersState *st)
2363 {
2364 char *re;
2365 ModifyWord_SubstRegexArgs args;
2366 Boolean oneBigWord;
2367 int error;
2368
2369 char delim = (*pp)[1];
2370 if (delim == '\0') {
2371 Error("Missing delimiter for :C modifier");
2372 (*pp)++;
2373 return AMR_CLEANUP;
2374 }
2375
2376 *pp += 2;
2377
2378 re = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2379 if (re == NULL) {
2380 st->missing_delim = delim;
2381 return AMR_CLEANUP;
2382 }
2383
2384 args.replace = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2385 NULL, NULL, NULL);
2386 if (args.replace == NULL) {
2387 free(re);
2388 st->missing_delim = delim;
2389 return AMR_CLEANUP;
2390 }
2391
2392 args.pflags = 0;
2393 args.matched = FALSE;
2394 oneBigWord = st->oneBigWord;
2395 for (;; (*pp)++) {
2396 switch (**pp) {
2397 case 'g':
2398 args.pflags |= VARP_SUB_GLOBAL;
2399 continue;
2400 case '1':
2401 args.pflags |= VARP_SUB_ONE;
2402 continue;
2403 case 'W':
2404 oneBigWord = TRUE;
2405 continue;
2406 }
2407 break;
2408 }
2409
2410 error = regcomp(&args.re, re, REG_EXTENDED);
2411 free(re);
2412 if (error) {
2413 VarREError(error, &args.re, "Regex compilation error");
2414 free(args.replace);
2415 return AMR_CLEANUP;
2416 }
2417
2418 args.nsub = args.re.re_nsub + 1;
2419 if (args.nsub > 10)
2420 args.nsub = 10;
2421 st->newVal = ModifyWords(st->ctxt, st->sep, oneBigWord, st->val,
2422 ModifyWord_SubstRegex, &args);
2423 regfree(&args.re);
2424 free(args.replace);
2425 return AMR_OK;
2426 }
2427 #endif
2428
2429 static void
2430 ModifyWord_Copy(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
2431 {
2432 SepBuf_AddStr(buf, word);
2433 }
2434
2435 /* :ts<separator> */
2436 static ApplyModifierResult
2437 ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st)
2438 {
2439 /* XXX: pp points to the 's', for historic reasons only.
2440 * Changing this will influence the error messages. */
2441 const char *sep = *pp + 1;
2442
2443 /* ":ts<any><endc>" or ":ts<any>:" */
2444 if (sep[0] != st->endc && (sep[1] == st->endc || sep[1] == ':')) {
2445 st->sep = sep[0];
2446 *pp = sep + 1;
2447 goto ok;
2448 }
2449
2450 /* ":ts<endc>" or ":ts:" */
2451 if (sep[0] == st->endc || sep[0] == ':') {
2452 st->sep = '\0'; /* no separator */
2453 *pp = sep;
2454 goto ok;
2455 }
2456
2457 /* ":ts<unrecognised><unrecognised>". */
2458 if (sep[0] != '\\')
2459 return AMR_BAD;
2460
2461 /* ":ts\n" */
2462 if (sep[1] == 'n') {
2463 st->sep = '\n';
2464 *pp = sep + 2;
2465 goto ok;
2466 }
2467
2468 /* ":ts\t" */
2469 if (sep[1] == 't') {
2470 st->sep = '\t';
2471 *pp = sep + 2;
2472 goto ok;
2473 }
2474
2475 /* ":ts\x40" or ":ts\100" */
2476 {
2477 const char *numStart = sep + 1;
2478 int base = 8; /* assume octal */
2479 char *end;
2480
2481 if (sep[1] == 'x') {
2482 base = 16;
2483 numStart++;
2484 } else if (!ch_isdigit(sep[1]))
2485 return AMR_BAD; /* ":ts<backslash><unrecognised>". */
2486
2487 st->sep = (char)strtoul(numStart, &end, base);
2488 if (*end != ':' && *end != st->endc)
2489 return AMR_BAD;
2490 *pp = end;
2491 }
2492
2493 ok:
2494 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2495 ModifyWord_Copy, NULL);
2496 return AMR_OK;
2497 }
2498
2499 /* :tA, :tu, :tl, :ts<separator>, etc. */
2500 static ApplyModifierResult
2501 ApplyModifier_To(const char **pp, ApplyModifiersState *st)
2502 {
2503 const char *mod = *pp;
2504 assert(mod[0] == 't');
2505
2506 *pp = mod + 1; /* make sure it is set */
2507 if (mod[1] == st->endc || mod[1] == ':' || mod[1] == '\0')
2508 return AMR_BAD; /* Found ":t<endc>" or ":t:". */
2509
2510 if (mod[1] == 's')
2511 return ApplyModifier_ToSep(pp, st);
2512
2513 if (mod[2] != st->endc && mod[2] != ':')
2514 return AMR_BAD; /* Found ":t<unrecognised><unrecognised>". */
2515
2516 /* Check for two-character options: ":tu", ":tl" */
2517 if (mod[1] == 'A') { /* absolute path */
2518 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2519 ModifyWord_Realpath, NULL);
2520 *pp = mod + 2;
2521 return AMR_OK;
2522 }
2523
2524 if (mod[1] == 'u') {
2525 size_t i;
2526 size_t len = strlen(st->val);
2527 st->newVal = bmake_malloc(len + 1);
2528 for (i = 0; i < len + 1; i++)
2529 st->newVal[i] = ch_toupper(st->val[i]);
2530 *pp = mod + 2;
2531 return AMR_OK;
2532 }
2533
2534 if (mod[1] == 'l') {
2535 size_t i;
2536 size_t len = strlen(st->val);
2537 st->newVal = bmake_malloc(len + 1);
2538 for (i = 0; i < len + 1; i++)
2539 st->newVal[i] = ch_tolower(st->val[i]);
2540 *pp = mod + 2;
2541 return AMR_OK;
2542 }
2543
2544 if (mod[1] == 'W' || mod[1] == 'w') {
2545 st->oneBigWord = mod[1] == 'W';
2546 st->newVal = st->val;
2547 *pp = mod + 2;
2548 return AMR_OK;
2549 }
2550
2551 /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */
2552 return AMR_BAD;
2553 }
2554
2555 /* :[#], :[1], etc. */
2556 static ApplyModifierResult
2557 ApplyModifier_Words(const char **pp, ApplyModifiersState *st)
2558 {
2559 char delim;
2560 char *estr;
2561 char *ep;
2562 int first, last;
2563
2564 (*pp)++; /* skip the '[' */
2565 delim = ']'; /* look for closing ']' */
2566 estr = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2567 NULL, NULL, NULL);
2568 if (estr == NULL) {
2569 st->missing_delim = delim;
2570 return AMR_CLEANUP;
2571 }
2572
2573 /* now *pp points just after the closing ']' */
2574 if (**pp != ':' && **pp != st->endc)
2575 goto bad_modifier; /* Found junk after ']' */
2576
2577 if (estr[0] == '\0')
2578 goto bad_modifier; /* empty square brackets in ":[]". */
2579
2580 if (estr[0] == '#' && estr[1] == '\0') { /* Found ":[#]" */
2581 if (st->oneBigWord) {
2582 st->newVal = bmake_strdup("1");
2583 } else {
2584 Buffer buf;
2585
2586 Words words = Str_Words(st->val, FALSE);
2587 size_t ac = words.len;
2588 Words_Free(words);
2589
2590 Buf_Init(&buf, 4); /* 3 digits + '\0' is usually enough */
2591 Buf_AddInt(&buf, (int)ac);
2592 st->newVal = Buf_Destroy(&buf, FALSE);
2593 }
2594 goto ok;
2595 }
2596
2597 if (estr[0] == '*' && estr[1] == '\0') {
2598 /* Found ":[*]" */
2599 st->oneBigWord = TRUE;
2600 st->newVal = st->val;
2601 goto ok;
2602 }
2603
2604 if (estr[0] == '@' && estr[1] == '\0') {
2605 /* Found ":[@]" */
2606 st->oneBigWord = FALSE;
2607 st->newVal = st->val;
2608 goto ok;
2609 }
2610
2611 /*
2612 * We expect estr to contain a single integer for :[N], or two integers
2613 * separated by ".." for :[start..end].
2614 */
2615 first = (int)strtol(estr, &ep, 0);
2616 if (ep == estr) /* Found junk instead of a number */
2617 goto bad_modifier;
2618
2619 if (ep[0] == '\0') { /* Found only one integer in :[N] */
2620 last = first;
2621 } else if (ep[0] == '.' && ep[1] == '.' && ep[2] != '\0') {
2622 /* Expecting another integer after ".." */
2623 ep += 2;
2624 last = (int)strtol(ep, &ep, 0);
2625 if (ep[0] != '\0') /* Found junk after ".." */
2626 goto bad_modifier;
2627 } else
2628 goto bad_modifier; /* Found junk instead of ".." */
2629
2630 /*
2631 * Now seldata is properly filled in, but we still have to check for 0 as
2632 * a special case.
2633 */
2634 if (first == 0 && last == 0) {
2635 /* ":[0]" or perhaps ":[0..0]" */
2636 st->oneBigWord = TRUE;
2637 st->newVal = st->val;
2638 goto ok;
2639 }
2640
2641 /* ":[0..N]" or ":[N..0]" */
2642 if (first == 0 || last == 0)
2643 goto bad_modifier;
2644
2645 /* Normal case: select the words described by seldata. */
2646 st->newVal = VarSelectWords(st->sep, st->oneBigWord, st->val, first, last);
2647
2648 ok:
2649 free(estr);
2650 return AMR_OK;
2651
2652 bad_modifier:
2653 free(estr);
2654 return AMR_BAD;
2655 }
2656
2657 static int
2658 str_cmp_asc(const void *a, const void *b)
2659 {
2660 return strcmp(*(const char * const *)a, *(const char * const *)b);
2661 }
2662
2663 static int
2664 str_cmp_desc(const void *a, const void *b)
2665 {
2666 return strcmp(*(const char * const *)b, *(const char * const *)a);
2667 }
2668
2669 /* :O (order ascending) or :Or (order descending) or :Ox (shuffle) */
2670 static ApplyModifierResult
2671 ApplyModifier_Order(const char **pp, ApplyModifiersState *st)
2672 {
2673 const char *mod = (*pp)++; /* skip past the 'O' in any case */
2674
2675 Words words = Str_Words(st->val, FALSE);
2676
2677 if (mod[1] == st->endc || mod[1] == ':') {
2678 /* :O sorts ascending */
2679 qsort(words.words, words.len, sizeof(char *), str_cmp_asc);
2680
2681 } else if ((mod[1] == 'r' || mod[1] == 'x') &&
2682 (mod[2] == st->endc || mod[2] == ':')) {
2683 (*pp)++;
2684
2685 if (mod[1] == 'r') {
2686 /* :Or sorts descending */
2687 qsort(words.words, words.len, sizeof(char *), str_cmp_desc);
2688
2689 } else {
2690 /* :Ox shuffles
2691 *
2692 * We will use [ac..2] range for mod factors. This will produce
2693 * random numbers in [(ac-1)..0] interval, and minimal
2694 * reasonable value for mod factor is 2 (the mod 1 will produce
2695 * 0 with probability 1).
2696 */
2697 size_t i;
2698 for (i = words.len - 1; i > 0; i--) {
2699 size_t rndidx = (size_t)random() % (i + 1);
2700 char *t = words.words[i];
2701 words.words[i] = words.words[rndidx];
2702 words.words[rndidx] = t;
2703 }
2704 }
2705 } else {
2706 Words_Free(words);
2707 return AMR_BAD;
2708 }
2709
2710 st->newVal = Words_JoinFree(words);
2711 return AMR_OK;
2712 }
2713
2714 /* :? then : else */
2715 static ApplyModifierResult
2716 ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st)
2717 {
2718 char delim;
2719 char *then_expr, *else_expr;
2720
2721 Boolean value = FALSE;
2722 VarEvalFlags then_eflags = st->eflags & ~(unsigned)VARE_WANTRES;
2723 VarEvalFlags else_eflags = st->eflags & ~(unsigned)VARE_WANTRES;
2724
2725 int cond_rc = COND_PARSE; /* anything other than COND_INVALID */
2726 if (st->eflags & VARE_WANTRES) {
2727 cond_rc = Cond_EvalCondition(st->v->name, &value);
2728 if (cond_rc != COND_INVALID && value)
2729 then_eflags |= VARE_WANTRES;
2730 if (cond_rc != COND_INVALID && !value)
2731 else_eflags |= VARE_WANTRES;
2732 }
2733
2734 (*pp)++; /* skip past the '?' */
2735 delim = ':';
2736 then_expr = ParseModifierPart(pp, delim, then_eflags, st->ctxt,
2737 NULL, NULL, NULL);
2738 if (then_expr == NULL) {
2739 st->missing_delim = delim;
2740 return AMR_CLEANUP;
2741 }
2742
2743 delim = st->endc; /* BRCLOSE or PRCLOSE */
2744 else_expr = ParseModifierPart(pp, delim, else_eflags, st->ctxt,
2745 NULL, NULL, NULL);
2746 if (else_expr == NULL) {
2747 st->missing_delim = delim;
2748 return AMR_CLEANUP;
2749 }
2750
2751 (*pp)--;
2752 if (cond_rc == COND_INVALID) {
2753 Error("Bad conditional expression `%s' in %s?%s:%s",
2754 st->v->name, st->v->name, then_expr, else_expr);
2755 return AMR_CLEANUP;
2756 }
2757
2758 if (value) {
2759 st->newVal = then_expr;
2760 free(else_expr);
2761 } else {
2762 st->newVal = else_expr;
2763 free(then_expr);
2764 }
2765 ApplyModifiersState_Define(st);
2766 return AMR_OK;
2767 }
2768
2769 /*
2770 * The ::= modifiers actually assign a value to the variable.
2771 * Their main purpose is in supporting modifiers of .for loop
2772 * iterators and other obscure uses. They always expand to
2773 * nothing. In a target rule that would otherwise expand to an
2774 * empty line they can be preceded with @: to keep make happy.
2775 * Eg.
2776 *
2777 * foo: .USE
2778 * .for i in ${.TARGET} ${.TARGET:R}.gz
2779 * @: ${t::=$i}
2780 * @echo blah ${t:T}
2781 * .endfor
2782 *
2783 * ::=<str> Assigns <str> as the new value of variable.
2784 * ::?=<str> Assigns <str> as value of variable if
2785 * it was not already set.
2786 * ::+=<str> Appends <str> to variable.
2787 * ::!=<cmd> Assigns output of <cmd> as the new value of
2788 * variable.
2789 */
2790 static ApplyModifierResult
2791 ApplyModifier_Assign(const char **pp, ApplyModifiersState *st)
2792 {
2793 GNode *v_ctxt;
2794 char *sv_name;
2795 char delim;
2796 char *val;
2797
2798 const char *mod = *pp;
2799 const char *op = mod + 1;
2800 if (!(op[0] == '=' ||
2801 (op[1] == '=' &&
2802 (op[0] == '!' || op[0] == '+' || op[0] == '?'))))
2803 return AMR_UNKNOWN; /* "::<unrecognised>" */
2804
2805
2806 if (st->v->name[0] == 0) {
2807 *pp = mod + 1;
2808 return AMR_BAD;
2809 }
2810
2811 v_ctxt = st->ctxt; /* context where v belongs */
2812 sv_name = NULL;
2813 if (st->exprFlags & VEF_UNDEF) {
2814 /*
2815 * We need to bmake_strdup() it in case ParseModifierPart() recurses.
2816 */
2817 sv_name = st->v->name;
2818 st->v->name = bmake_strdup(st->v->name);
2819 } else if (st->ctxt != VAR_GLOBAL) {
2820 Var *gv = VarFind(st->v->name, st->ctxt, 0);
2821 if (gv == NULL)
2822 v_ctxt = VAR_GLOBAL;
2823 else
2824 VarFreeEnv(gv, TRUE);
2825 }
2826
2827 switch (op[0]) {
2828 case '+':
2829 case '?':
2830 case '!':
2831 *pp = mod + 3;
2832 break;
2833 default:
2834 *pp = mod + 2;
2835 break;
2836 }
2837
2838 delim = st->startc == '(' ? ')' : '}';
2839 val = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2840 if (st->exprFlags & VEF_UNDEF) {
2841 /* restore original name */
2842 free(st->v->name);
2843 st->v->name = sv_name;
2844 }
2845 if (val == NULL) {
2846 st->missing_delim = delim;
2847 return AMR_CLEANUP;
2848 }
2849
2850 (*pp)--;
2851
2852 if (st->eflags & VARE_WANTRES) {
2853 switch (op[0]) {
2854 case '+':
2855 Var_Append(st->v->name, val, v_ctxt);
2856 break;
2857 case '!': {
2858 const char *errfmt;
2859 char *cmd_output = Cmd_Exec(val, &errfmt);
2860 if (errfmt)
2861 Error(errfmt, val);
2862 else
2863 Var_Set(st->v->name, cmd_output, v_ctxt);
2864 free(cmd_output);
2865 break;
2866 }
2867 case '?':
2868 if (!(st->exprFlags & VEF_UNDEF))
2869 break;
2870 /* FALLTHROUGH */
2871 default:
2872 Var_Set(st->v->name, val, v_ctxt);
2873 break;
2874 }
2875 }
2876 free(val);
2877 st->newVal = varNoError; /* XXX: varNoError is kind of an error,
2878 * the intention here is to just return
2879 * an empty string. */
2880 return AMR_OK;
2881 }
2882
2883 /* remember current value */
2884 static ApplyModifierResult
2885 ApplyModifier_Remember(const char **pp, ApplyModifiersState *st)
2886 {
2887 const char *mod = *pp;
2888 if (!ModMatchEq(mod, "_", st->endc))
2889 return AMR_UNKNOWN;
2890
2891 if (mod[1] == '=') {
2892 size_t n = strcspn(mod + 2, ":)}");
2893 char *name = bmake_strldup(mod + 2, n);
2894 Var_Set(name, st->val, st->ctxt);
2895 free(name);
2896 *pp = mod + 2 + n;
2897 } else {
2898 Var_Set("_", st->val, st->ctxt);
2899 *pp = mod + 1;
2900 }
2901 st->newVal = st->val;
2902 return AMR_OK;
2903 }
2904
2905 /* Apply the given function to each word of the variable value. */
2906 static ApplyModifierResult
2907 ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
2908 ModifyWordsCallback modifyWord)
2909 {
2910 char delim = (*pp)[1];
2911 if (delim != st->endc && delim != ':')
2912 return AMR_UNKNOWN;
2913
2914 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord,
2915 st->val, modifyWord, NULL);
2916 (*pp)++;
2917 return AMR_OK;
2918 }
2919
2920 #ifdef SYSVVARSUB
2921 /* :from=to */
2922 static ApplyModifierResult
2923 ApplyModifier_SysV(const char **pp, ApplyModifiersState *st)
2924 {
2925 char delim;
2926 char *lhs, *rhs;
2927
2928 const char *mod = *pp;
2929 Boolean eqFound = FALSE;
2930
2931 /*
2932 * First we make a pass through the string trying
2933 * to verify it is a SYSV-make-style translation:
2934 * it must be: <string1>=<string2>)
2935 */
2936 int nest = 1;
2937 const char *next = mod;
2938 while (*next != '\0' && nest > 0) {
2939 if (*next == '=') {
2940 eqFound = TRUE;
2941 /* continue looking for st->endc */
2942 } else if (*next == st->endc)
2943 nest--;
2944 else if (*next == st->startc)
2945 nest++;
2946 if (nest > 0)
2947 next++;
2948 }
2949 if (*next != st->endc || !eqFound)
2950 return AMR_UNKNOWN;
2951
2952 delim = '=';
2953 *pp = mod;
2954 lhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2955 if (lhs == NULL) {
2956 st->missing_delim = delim;
2957 return AMR_CLEANUP;
2958 }
2959
2960 delim = st->endc;
2961 rhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2962 if (rhs == NULL) {
2963 st->missing_delim = delim;
2964 return AMR_CLEANUP;
2965 }
2966
2967 /*
2968 * SYSV modifications happen through the whole
2969 * string. Note the pattern is anchored at the end.
2970 */
2971 (*pp)--;
2972 if (lhs[0] == '\0' && st->val[0] == '\0') {
2973 st->newVal = st->val; /* special case */
2974 } else {
2975 ModifyWord_SYSVSubstArgs args = {st->ctxt, lhs, rhs};
2976 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2977 ModifyWord_SYSVSubst, &args);
2978 }
2979 free(lhs);
2980 free(rhs);
2981 return AMR_OK;
2982 }
2983 #endif
2984
2985 /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */
2986 static char *
2987 ApplyModifiers(
2988 const char **pp, /* the parsing position, updated upon return */
2989 char *val, /* the current value of the variable */
2990 char const startc, /* '(' or '{', or '\0' for indirect modifiers */
2991 char const endc, /* ')' or '}', or '\0' for indirect modifiers */
2992 Var * const v,
2993 VarExprFlags *exprFlags,
2994 GNode * const ctxt, /* for looking up and modifying variables */
2995 VarEvalFlags const eflags,
2996 void ** const freePtr /* free this after using the return value */
2997 ) {
2998 ApplyModifiersState st = {
2999 startc, endc, v, ctxt, eflags, val,
3000 var_Error, /* .newVal */
3001 '\0', /* .missing_delim */
3002 ' ', /* .sep */
3003 FALSE, /* .oneBigWord */
3004 *exprFlags /* .exprFlags */
3005 };
3006 const char *p;
3007 const char *mod;
3008 ApplyModifierResult res;
3009
3010 assert(startc == '(' || startc == '{' || startc == '\0');
3011 assert(endc == ')' || endc == '}' || endc == '\0');
3012 assert(val != NULL);
3013
3014 p = *pp;
3015 while (*p != '\0' && *p != endc) {
3016
3017 if (*p == '$') {
3018 /*
3019 * We may have some complex modifiers in a variable.
3020 */
3021 const char *nested_p = p;
3022 void *freeIt;
3023 const char *rval;
3024 int c;
3025
3026 (void)Var_Parse(&nested_p, st.ctxt, st.eflags, &rval, &freeIt);
3027 /* TODO: handle errors */
3028
3029 /*
3030 * If we have not parsed up to st.endc or ':',
3031 * we are not interested.
3032 */
3033 if (rval[0] != '\0' &&
3034 (c = *nested_p) != '\0' && c != ':' && c != st.endc) {
3035 free(freeIt);
3036 /* XXX: apply_mods doesn't sound like "not interested". */
3037 goto apply_mods;
3038 }
3039
3040 VAR_DEBUG("Indirect modifier \"%s\" from \"%.*s\"\n",
3041 rval, (int)(size_t)(nested_p - p), p);
3042
3043 p = nested_p;
3044
3045 if (rval[0] != '\0') {
3046 const char *rval_pp = rval;
3047 st.val = ApplyModifiers(&rval_pp, st.val, '\0', '\0', v,
3048 exprFlags, ctxt, eflags, freePtr);
3049 if (st.val == var_Error
3050 || (st.val == varNoError && !(st.eflags & VARE_UNDEFERR))
3051 || *rval_pp != '\0') {
3052 free(freeIt);
3053 goto out; /* error already reported */
3054 }
3055 }
3056 free(freeIt);
3057 if (*p == ':')
3058 p++;
3059 else if (*p == '\0' && endc != '\0') {
3060 Error("Unclosed variable specification after complex "
3061 "modifier (expecting '%c') for %s", st.endc, st.v->name);
3062 goto out;
3063 }
3064 continue;
3065 }
3066 apply_mods:
3067 st.newVal = var_Error; /* default value, in case of errors */
3068 res = AMR_BAD; /* just a safe fallback */
3069 mod = p;
3070
3071 if (DEBUG(VAR)) {
3072 char eflags_str[VarEvalFlags_ToStringSize];
3073 char vflags_str[VarFlags_ToStringSize];
3074 char exprflags_str[VarExprFlags_ToStringSize];
3075 Boolean is_single_char = mod[0] != '\0' &&
3076 (mod[1] == endc || mod[1] == ':');
3077
3078 /* At this point, only the first character of the modifier can
3079 * be used since the end of the modifier is not yet known. */
3080 VAR_DEBUG("Applying ${%s:%c%s} to \"%s\" (%s, %s, %s)\n",
3081 st.v->name, mod[0], is_single_char ? "" : "...", st.val,
3082 Enum_FlagsToString(eflags_str, sizeof eflags_str,
3083 st.eflags, VarEvalFlags_ToStringSpecs),
3084 Enum_FlagsToString(vflags_str, sizeof vflags_str,
3085 st.v->flags, VarFlags_ToStringSpecs),
3086 Enum_FlagsToString(exprflags_str, sizeof exprflags_str,
3087 st.exprFlags,
3088 VarExprFlags_ToStringSpecs));
3089 }
3090
3091 switch (*mod) {
3092 case ':':
3093 res = ApplyModifier_Assign(&p, &st);
3094 break;
3095 case '@':
3096 res = ApplyModifier_Loop(&p, &st);
3097 break;
3098 case '_':
3099 res = ApplyModifier_Remember(&p, &st);
3100 break;
3101 case 'D':
3102 case 'U':
3103 res = ApplyModifier_Defined(&p, &st);
3104 break;
3105 case 'L':
3106 ApplyModifiersState_Define(&st);
3107 st.newVal = bmake_strdup(st.v->name);
3108 p++;
3109 res = AMR_OK;
3110 break;
3111 case 'P':
3112 res = ApplyModifier_Path(&p, &st);
3113 break;
3114 case '!':
3115 res = ApplyModifier_ShellCommand(&p, &st);
3116 break;
3117 case '[':
3118 res = ApplyModifier_Words(&p, &st);
3119 break;
3120 case 'g':
3121 res = ApplyModifier_Gmtime(&p, &st);
3122 break;
3123 case 'h':
3124 res = ApplyModifier_Hash(&p, &st);
3125 break;
3126 case 'l':
3127 res = ApplyModifier_Localtime(&p, &st);
3128 break;
3129 case 't':
3130 res = ApplyModifier_To(&p, &st);
3131 break;
3132 case 'N':
3133 case 'M':
3134 res = ApplyModifier_Match(&p, &st);
3135 break;
3136 case 'S':
3137 res = ApplyModifier_Subst(&p, &st);
3138 break;
3139 case '?':
3140 res = ApplyModifier_IfElse(&p, &st);
3141 break;
3142 #ifndef NO_REGEX
3143 case 'C':
3144 res = ApplyModifier_Regex(&p, &st);
3145 break;
3146 #endif
3147 case 'q':
3148 case 'Q':
3149 if (p[1] == st.endc || p[1] == ':') {
3150 st.newVal = VarQuote(st.val, *mod == 'q');
3151 p++;
3152 res = AMR_OK;
3153 } else
3154 res = AMR_UNKNOWN;
3155 break;
3156 case 'T':
3157 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Tail);
3158 break;
3159 case 'H':
3160 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Head);
3161 break;
3162 case 'E':
3163 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Suffix);
3164 break;
3165 case 'R':
3166 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Root);
3167 break;
3168 case 'r':
3169 res = ApplyModifier_Range(&p, &st);
3170 break;
3171 case 'O':
3172 res = ApplyModifier_Order(&p, &st);
3173 break;
3174 case 'u':
3175 if (p[1] == st.endc || p[1] == ':') {
3176 st.newVal = VarUniq(st.val);
3177 p++;
3178 res = AMR_OK;
3179 } else
3180 res = AMR_UNKNOWN;
3181 break;
3182 #ifdef SUNSHCMD
3183 case 's':
3184 if (p[1] == 'h' && (p[2] == st.endc || p[2] == ':')) {
3185 if (st.eflags & VARE_WANTRES) {
3186 const char *errfmt;
3187 st.newVal = Cmd_Exec(st.val, &errfmt);
3188 if (errfmt)
3189 Error(errfmt, st.val);
3190 } else
3191 st.newVal = varNoError;
3192 p += 2;
3193 res = AMR_OK;
3194 } else
3195 res = AMR_UNKNOWN;
3196 break;
3197 #endif
3198 default:
3199 res = AMR_UNKNOWN;
3200 }
3201
3202 #ifdef SYSVVARSUB
3203 if (res == AMR_UNKNOWN) {
3204 assert(p == mod);
3205 res = ApplyModifier_SysV(&p, &st);
3206 }
3207 #endif
3208
3209 if (res == AMR_UNKNOWN) {
3210 Error("Unknown modifier '%c'", *mod);
3211 for (p++; *p != ':' && *p != st.endc && *p != '\0'; p++)
3212 continue;
3213 st.newVal = var_Error;
3214 }
3215 if (res == AMR_CLEANUP)
3216 goto cleanup;
3217 if (res == AMR_BAD)
3218 goto bad_modifier;
3219
3220 if (DEBUG(VAR)) {
3221 char eflags_str[VarEvalFlags_ToStringSize];
3222 char vflags_str[VarFlags_ToStringSize];
3223 char exprflags_str[VarExprFlags_ToStringSize];
3224 const char *quot = st.newVal == var_Error ? "" : "\"";
3225 const char *newVal = st.newVal == var_Error ? "error" : st.newVal;
3226
3227 VAR_DEBUG("Result of ${%s:%.*s} is %s%s%s (%s, %s, %s)\n",
3228 st.v->name, (int)(p - mod), mod, quot, newVal, quot,
3229 Enum_FlagsToString(eflags_str, sizeof eflags_str,
3230 st.eflags, VarEvalFlags_ToStringSpecs),
3231 Enum_FlagsToString(vflags_str, sizeof vflags_str,
3232 st.v->flags, VarFlags_ToStringSpecs),
3233 Enum_FlagsToString(exprflags_str, sizeof exprflags_str,
3234 st.exprFlags,
3235 VarExprFlags_ToStringSpecs));
3236 }
3237
3238 if (st.newVal != st.val) {
3239 if (*freePtr) {
3240 free(st.val);
3241 *freePtr = NULL;
3242 }
3243 st.val = st.newVal;
3244 if (st.val != var_Error && st.val != varNoError) {
3245 *freePtr = st.val;
3246 }
3247 }
3248 if (*p == '\0' && st.endc != '\0') {
3249 Error("Unclosed variable specification (expecting '%c') "
3250 "for \"%s\" (value \"%s\") modifier %c",
3251 st.endc, st.v->name, st.val, *mod);
3252 } else if (*p == ':') {
3253 p++;
3254 }
3255 mod = p;
3256 }
3257 out:
3258 *pp = p;
3259 assert(st.val != NULL); /* Use var_Error or varNoError instead. */
3260 *exprFlags = st.exprFlags;
3261 return st.val;
3262
3263 bad_modifier:
3264 Error("Bad modifier `:%.*s' for %s",
3265 (int)strcspn(mod, ":)}"), mod, st.v->name);
3266
3267 cleanup:
3268 *pp = p;
3269 if (st.missing_delim != '\0')
3270 Error("Unfinished modifier for %s ('%c' missing)",
3271 st.v->name, st.missing_delim);
3272 free(*freePtr);
3273 *freePtr = NULL;
3274 *exprFlags = st.exprFlags;
3275 return var_Error;
3276 }
3277
3278 static Boolean
3279 VarIsDynamic(GNode *ctxt, const char *varname, size_t namelen)
3280 {
3281 if ((namelen == 1 ||
3282 (namelen == 2 && (varname[1] == 'F' || varname[1] == 'D'))) &&
3283 (ctxt == VAR_CMD || ctxt == VAR_GLOBAL))
3284 {
3285 /*
3286 * If substituting a local variable in a non-local context,
3287 * assume it's for dynamic source stuff. We have to handle
3288 * this specially and return the longhand for the variable
3289 * with the dollar sign escaped so it makes it back to the
3290 * caller. Only four of the local variables are treated
3291 * specially as they are the only four that will be set
3292 * when dynamic sources are expanded.
3293 */
3294 switch (varname[0]) {
3295 case '@':
3296 case '%':
3297 case '*':
3298 case '!':
3299 return TRUE;
3300 }
3301 return FALSE;
3302 }
3303
3304 if ((namelen == 7 || namelen == 8) && varname[0] == '.' &&
3305 ch_isupper(varname[1]) && (ctxt == VAR_CMD || ctxt == VAR_GLOBAL))
3306 {
3307 return strcmp(varname, ".TARGET") == 0 ||
3308 strcmp(varname, ".ARCHIVE") == 0 ||
3309 strcmp(varname, ".PREFIX") == 0 ||
3310 strcmp(varname, ".MEMBER") == 0;
3311 }
3312
3313 return FALSE;
3314 }
3315
3316 static const char *
3317 ShortVarValue(char varname, const GNode *ctxt, VarEvalFlags eflags)
3318 {
3319 if (ctxt == VAR_CMD || ctxt == VAR_GLOBAL) {
3320 /*
3321 * If substituting a local variable in a non-local context,
3322 * assume it's for dynamic source stuff. We have to handle
3323 * this specially and return the longhand for the variable
3324 * with the dollar sign escaped so it makes it back to the
3325 * caller. Only four of the local variables are treated
3326 * specially as they are the only four that will be set
3327 * when dynamic sources are expanded.
3328 */
3329 switch (varname) {
3330 case '@':
3331 return "$(.TARGET)";
3332 case '%':
3333 return "$(.MEMBER)";
3334 case '*':
3335 return "$(.PREFIX)";
3336 case '!':
3337 return "$(.ARCHIVE)";
3338 }
3339 }
3340 return eflags & VARE_UNDEFERR ? var_Error : varNoError;
3341 }
3342
3343 /* Parse a variable name, until the end character or a colon, whichever
3344 * comes first. */
3345 static char *
3346 ParseVarname(const char **pp, char startc, char endc,
3347 GNode *ctxt, VarEvalFlags eflags,
3348 size_t *out_varname_len)
3349 {
3350 Buffer buf;
3351 const char *p = *pp;
3352 int depth = 1;
3353
3354 Buf_Init(&buf, 0);
3355
3356 while (*p != '\0') {
3357 /* Track depth so we can spot parse errors. */
3358 if (*p == startc)
3359 depth++;
3360 if (*p == endc) {
3361 if (--depth == 0)
3362 break;
3363 }
3364 if (*p == ':' && depth == 1)
3365 break;
3366
3367 /* A variable inside a variable, expand. */
3368 if (*p == '$') {
3369 void *freeIt;
3370 const char *rval;
3371 (void)Var_Parse(&p, ctxt, eflags, &rval, &freeIt);
3372 /* TODO: handle errors */
3373 Buf_AddStr(&buf, rval);
3374 free(freeIt);
3375 } else {
3376 Buf_AddByte(&buf, *p);
3377 p++;
3378 }
3379 }
3380 *pp = p;
3381 *out_varname_len = Buf_Size(&buf);
3382 return Buf_Destroy(&buf, FALSE);
3383 }
3384
3385 static Boolean
3386 ValidShortVarname(char varname, const char *start)
3387 {
3388 switch (varname) {
3389 case '\0':
3390 case ')':
3391 case '}':
3392 case ':':
3393 case '$':
3394 break; /* and continue below */
3395 default:
3396 return TRUE;
3397 }
3398
3399 if (!DEBUG(LINT))
3400 return FALSE;
3401
3402 if (varname == '$')
3403 Parse_Error(PARSE_FATAL,
3404 "To escape a dollar, use \\$, not $$, at \"%s\"", start);
3405 else if (varname == '\0')
3406 Parse_Error(PARSE_FATAL, "Dollar followed by nothing");
3407 else
3408 Parse_Error(PARSE_FATAL,
3409 "Invalid variable name '%c', at \"%s\"", varname, start);
3410
3411 return FALSE;
3412 }
3413
3414 /*-
3415 *-----------------------------------------------------------------------
3416 * Var_Parse --
3417 * Given the start of a variable expression (such as $v, $(VAR),
3418 * ${VAR:Mpattern}), extract the variable name, possibly some
3419 * modifiers and find its value by applying the modifiers to the
3420 * original value.
3421 *
3422 * Input:
3423 * str The string to parse
3424 * ctxt The context for the variable
3425 * flags VARE_UNDEFERR if undefineds are an error
3426 * VARE_WANTRES if we actually want the result
3427 * VARE_ASSIGN if we are in a := assignment
3428 * lengthPtr OUT: The length of the specification
3429 * freePtr OUT: Non-NULL if caller should free *freePtr
3430 *
3431 * Results:
3432 * Returns the value of the variable expression, never NULL.
3433 * var_Error if there was a parse error and VARE_UNDEFERR was set.
3434 * varNoError if there was a parse error and VARE_UNDEFERR was not set.
3435 *
3436 * Parsing should continue at str + *lengthPtr.
3437 * TODO: Document the value of *lengthPtr on parse errors. It might be
3438 * 0, or +1, or the index of the parse error, or the guessed end of the
3439 * variable expression.
3440 *
3441 * If var_Error is returned, a diagnostic may or may not have been
3442 * printed. XXX: This is inconsistent.
3443 *
3444 * If varNoError is returned, a diagnostic may or may not have been
3445 * printed. XXX: This is inconsistent, and as of 2020-09-08, returning
3446 * varNoError is even used to return a regular, non-error empty string.
3447 *
3448 * After using the returned value, *freePtr must be freed, preferably
3449 * using bmake_free since it is NULL in most cases.
3450 *
3451 * Side Effects:
3452 * Any effects from the modifiers, such as :!cmd! or ::=value.
3453 *-----------------------------------------------------------------------
3454 */
3455 /* coverity[+alloc : arg-*4] */
3456 VarParseResult
3457 Var_Parse(const char **pp, GNode *ctxt, VarEvalFlags eflags,
3458 const char **out_val, void **freePtr)
3459 {
3460 const char *const start = *pp;
3461 const char *p;
3462 Boolean haveModifier; /* TRUE if have modifiers for the variable */
3463 char startc; /* Starting character if variable in parens
3464 * or braces */
3465 char endc; /* Ending character if variable in parens
3466 * or braces */
3467 Boolean dynamic; /* TRUE if the variable is local and we're
3468 * expanding it in a non-local context. This
3469 * is done to support dynamic sources. The
3470 * result is just the expression, unaltered */
3471 const char *extramodifiers;
3472 Var *v;
3473 char *nstr;
3474 char eflags_str[VarEvalFlags_ToStringSize];
3475 VarExprFlags exprFlags = 0;
3476
3477 VAR_DEBUG("%s: %s with %s\n", __func__, start,
3478 Enum_FlagsToString(eflags_str, sizeof eflags_str, eflags,
3479 VarEvalFlags_ToStringSpecs));
3480
3481 *freePtr = NULL;
3482 extramodifiers = NULL; /* extra modifiers to apply first */
3483 dynamic = FALSE;
3484
3485 /* Appease GCC, which thinks that the variable might not be
3486 * initialized. */
3487 endc = '\0';
3488
3489 startc = start[1];
3490 if (startc != '(' && startc != '{') {
3491 char name[2];
3492
3493 /*
3494 * If it's not bounded by braces of some sort, life is much simpler.
3495 * We just need to check for the first character and return the
3496 * value if it exists.
3497 */
3498
3499 if (!ValidShortVarname(startc, start)) {
3500 (*pp)++;
3501 *out_val = var_Error;
3502 return VPR_PARSE_MSG;
3503 }
3504
3505 name[0] = startc;
3506 name[1] = '\0';
3507 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3508 if (v == NULL) {
3509 *pp += 2;
3510
3511 *out_val = ShortVarValue(startc, ctxt, eflags);
3512 if (DEBUG(LINT) && *out_val == var_Error) {
3513 Parse_Error(PARSE_FATAL, "Variable \"%s\" is undefined", name);
3514 return VPR_UNDEF_MSG;
3515 }
3516 return eflags & VARE_UNDEFERR ? VPR_UNDEF_SILENT : VPR_OK;
3517 } else {
3518 haveModifier = FALSE;
3519 p = start + 1;
3520 }
3521 } else {
3522 size_t namelen;
3523 char *varname;
3524
3525 endc = startc == '(' ? ')' : '}';
3526
3527 p = start + 2;
3528 varname = ParseVarname(&p, startc, endc, ctxt, eflags, &namelen);
3529
3530 if (*p == ':') {
3531 haveModifier = TRUE;
3532 } else if (*p == endc) {
3533 haveModifier = FALSE;
3534 } else {
3535 Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"", varname);
3536 *pp = p;
3537 free(varname);
3538 *out_val = var_Error;
3539 return VPR_PARSE_MSG;
3540 }
3541
3542 v = VarFind(varname, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3543
3544 /* At this point, p points just after the variable name,
3545 * either at ':' or at endc. */
3546
3547 /*
3548 * Check also for bogus D and F forms of local variables since we're
3549 * in a local context and the name is the right length.
3550 */
3551 if (v == NULL && ctxt != VAR_CMD && ctxt != VAR_GLOBAL &&
3552 namelen == 2 && (varname[1] == 'F' || varname[1] == 'D') &&
3553 strchr("@%?*!<>", varname[0]) != NULL)
3554 {
3555 /*
3556 * Well, it's local -- go look for it.
3557 */
3558 char name[] = { varname[0], '\0' };
3559 v = VarFind(name, ctxt, 0);
3560
3561 if (v != NULL) {
3562 if (varname[1] == 'D') {
3563 extramodifiers = "H:";
3564 } else { /* F */
3565 extramodifiers = "T:";
3566 }
3567 }
3568 }
3569
3570 if (v == NULL) {
3571 dynamic = VarIsDynamic(ctxt, varname, namelen);
3572
3573 if (!haveModifier) {
3574 p++; /* skip endc */
3575 *pp = p;
3576 if (dynamic) {
3577 char *pstr = bmake_strsedup(start, p);
3578 *freePtr = pstr;
3579 free(varname);
3580 *out_val = pstr;
3581 return VPR_OK;
3582 }
3583
3584 if ((eflags & VARE_UNDEFERR) && (eflags & VARE_WANTRES) &&
3585 DEBUG(LINT))
3586 {
3587 Parse_Error(PARSE_FATAL, "Variable \"%s\" is undefined",
3588 varname);
3589 free(varname);
3590 *out_val = var_Error;
3591 return VPR_UNDEF_MSG;
3592 }
3593
3594 if (eflags & VARE_UNDEFERR) {
3595 free(varname);
3596 *out_val = var_Error;
3597 return VPR_UNDEF_SILENT;
3598 }
3599
3600 free(varname);
3601 *out_val = varNoError;
3602 return VPR_OK;
3603 }
3604
3605 /* The variable expression is based on an undefined variable.
3606 * Nevertheless it needs a Var, for modifiers that access the
3607 * variable name, such as :L or :?.
3608 *
3609 * Most modifiers leave this expression in the "undefined" state
3610 * (VEF_UNDEF), only a few modifiers like :D, :U, :L, :P turn this
3611 * undefined expression into a defined expression (VEF_DEF).
3612 *
3613 * At the end, after applying all modifiers, if the expression
3614 * is still undefined, Var_Parse will return an empty string
3615 * instead of the actually computed value. */
3616 v = bmake_malloc(sizeof(Var));
3617 v->name = varname;
3618 Buf_Init(&v->val, 1);
3619 v->flags = 0;
3620 exprFlags = VEF_UNDEF;
3621 } else
3622 free(varname);
3623 }
3624
3625 if (v->flags & VAR_IN_USE) {
3626 Fatal("Variable %s is recursive.", v->name);
3627 /*NOTREACHED*/
3628 } else {
3629 v->flags |= VAR_IN_USE;
3630 }
3631
3632 /*
3633 * Before doing any modification, we have to make sure the value
3634 * has been fully expanded. If it looks like recursion might be
3635 * necessary (there's a dollar sign somewhere in the variable's value)
3636 * we just call Var_Subst to do any other substitutions that are
3637 * necessary. Note that the value returned by Var_Subst will have
3638 * been dynamically-allocated, so it will need freeing when we
3639 * return.
3640 */
3641 nstr = Buf_GetAll(&v->val, NULL);
3642 if (strchr(nstr, '$') != NULL && (eflags & VARE_WANTRES)) {
3643 VarEvalFlags nested_eflags = eflags;
3644 if (DEBUG(LINT))
3645 nested_eflags &= ~(unsigned)VARE_UNDEFERR;
3646 nstr = Var_Subst(nstr, ctxt, nested_eflags);
3647 *freePtr = nstr;
3648 }
3649
3650 v->flags &= ~(unsigned)VAR_IN_USE;
3651
3652 if (haveModifier || extramodifiers != NULL) {
3653 void *extraFree;
3654
3655 extraFree = NULL;
3656 if (extramodifiers != NULL) {
3657 const char *em = extramodifiers;
3658 nstr = ApplyModifiers(&em, nstr, '(', ')',
3659 v, &exprFlags, ctxt, eflags, &extraFree);
3660 }
3661
3662 if (haveModifier) {
3663 /* Skip initial colon. */
3664 p++;
3665
3666 nstr = ApplyModifiers(&p, nstr, startc, endc,
3667 v, &exprFlags, ctxt, eflags, freePtr);
3668 free(extraFree);
3669 } else {
3670 *freePtr = extraFree;
3671 }
3672 }
3673
3674 if (*p != '\0') /* Skip past endc if possible. */
3675 p++;
3676
3677 *pp = p;
3678
3679 if (v->flags & VAR_FROM_ENV) {
3680 /* Free the environment variable now since we own it,
3681 * but don't free the variable value if it will be returned. */
3682 Boolean keepValue = nstr == Buf_GetAll(&v->val, NULL);
3683 if (keepValue)
3684 *freePtr = nstr;
3685 (void)VarFreeEnv(v, !keepValue);
3686
3687 } else if (exprFlags & VEF_UNDEF) {
3688 if (!(exprFlags & VEF_DEF)) {
3689 if (*freePtr != NULL) {
3690 free(*freePtr);
3691 *freePtr = NULL;
3692 }
3693 if (dynamic) {
3694 nstr = bmake_strsedup(start, p);
3695 *freePtr = nstr;
3696 } else {
3697 /* The expression is still undefined, therefore discard the
3698 * actual value and return an empty string instead. */
3699 nstr = (eflags & VARE_UNDEFERR) ? var_Error : varNoError;
3700 }
3701 }
3702 if (nstr != Buf_GetAll(&v->val, NULL))
3703 Buf_Destroy(&v->val, TRUE);
3704 free(v->name);
3705 free(v);
3706 }
3707 *out_val = nstr;
3708 return VPR_UNKNOWN;
3709 }
3710
3711 /* Substitute for all variables in the given string in the given context.
3712 *
3713 * If eflags & VARE_UNDEFERR, Parse_Error will be called when an undefined
3714 * variable is encountered.
3715 *
3716 * If eflags & VARE_WANTRES, any effects from the modifiers, such as ::=,
3717 * :sh or !cmd! take place.
3718 *
3719 * Input:
3720 * str the string which to substitute
3721 * ctxt the context wherein to find variables
3722 * eflags VARE_UNDEFERR if undefineds are an error
3723 * VARE_WANTRES if we actually want the result
3724 * VARE_ASSIGN if we are in a := assignment
3725 *
3726 * Results:
3727 * The resulting string.
3728 */
3729 char *
3730 Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags)
3731 {
3732 Buffer buf; /* Buffer for forming things */
3733 Boolean trailingBackslash;
3734
3735 /* Set true if an error has already been reported,
3736 * to prevent a plethora of messages when recursing */
3737 static Boolean errorReported;
3738
3739 Buf_Init(&buf, 0);
3740 errorReported = FALSE;
3741 trailingBackslash = FALSE; /* variable ends in \ */
3742
3743 while (*str) {
3744 if (*str == '\n' && trailingBackslash)
3745 Buf_AddByte(&buf, ' ');
3746
3747 if (*str == '$' && str[1] == '$') {
3748 /*
3749 * A dollar sign may be escaped with another dollar sign.
3750 * In such a case, we skip over the escape character and store the
3751 * dollar sign into the buffer directly.
3752 */
3753 if (save_dollars && (eflags & VARE_ASSIGN))
3754 Buf_AddByte(&buf, '$');
3755 Buf_AddByte(&buf, '$');
3756 str += 2;
3757 } else if (*str != '$') {
3758 /*
3759 * Skip as many characters as possible -- either to the end of
3760 * the string or to the next dollar sign (variable expression).
3761 */
3762 const char *cp;
3763
3764 for (cp = str++; *str != '$' && *str != '\0'; str++)
3765 continue;
3766 Buf_AddBytesBetween(&buf, cp, str);
3767 } else {
3768 const char *nested_str = str;
3769 void *freeIt;
3770 const char *val;
3771 (void)Var_Parse(&nested_str, ctxt, eflags, &val, &freeIt);
3772 /* TODO: handle errors */
3773
3774 if (val == var_Error || val == varNoError) {
3775 /*
3776 * If performing old-time variable substitution, skip over
3777 * the variable and continue with the substitution. Otherwise,
3778 * store the dollar sign and advance str so we continue with
3779 * the string...
3780 */
3781 if (oldVars) {
3782 str = nested_str;
3783 } else if ((eflags & VARE_UNDEFERR) || val == var_Error) {
3784 /*
3785 * If variable is undefined, complain and skip the
3786 * variable. The complaint will stop us from doing anything
3787 * when the file is parsed.
3788 */
3789 if (!errorReported) {
3790 Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"",
3791 (int)(size_t)(nested_str - str), str);
3792 }
3793 str = nested_str;
3794 errorReported = TRUE;
3795 } else {
3796 Buf_AddByte(&buf, *str);
3797 str++;
3798 }
3799 } else {
3800 size_t val_len;
3801
3802 str = nested_str;
3803
3804 val_len = strlen(val);
3805 Buf_AddBytes(&buf, val, val_len);
3806 trailingBackslash = val_len > 0 && val[val_len - 1] == '\\';
3807 }
3808 free(freeIt);
3809 freeIt = NULL;
3810 }
3811 }
3812
3813 return Buf_DestroyCompact(&buf);
3814 }
3815
3816 /* Initialize the module. */
3817 void
3818 Var_Init(void)
3819 {
3820 VAR_INTERNAL = Targ_NewGN("Internal");
3821 VAR_GLOBAL = Targ_NewGN("Global");
3822 VAR_CMD = Targ_NewGN("Command");
3823 }
3824
3825
3826 void
3827 Var_End(void)
3828 {
3829 Var_Stats();
3830 }
3831
3832 void
3833 Var_Stats(void)
3834 {
3835 Hash_DebugStats(&VAR_GLOBAL->context, "VAR_GLOBAL");
3836 }
3837
3838
3839 /****************** PRINT DEBUGGING INFO *****************/
3840 static void
3841 VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED)
3842 {
3843 Var *v = (Var *)vp;
3844 fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
3845 }
3846
3847 /* Print all variables in a context, unordered. */
3848 void
3849 Var_Dump(GNode *ctxt)
3850 {
3851 Hash_ForEach(&ctxt->context, VarPrintVar, NULL);
3852 }
3853