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