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