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