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