- Push more variables into local scope of the if else blocks. This
[dragonfly.git] / usr.bin / make / var.c
CommitLineData
edf6dd37 1/*-
984263bc
MD
2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
5 * 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
1de703da
MD
37 *
38 * @(#)var.c 8.3 (Berkeley) 3/19/94
1901cbd2 39 * $FreeBSD: src/usr.bin/make/var.c,v 1.83 2005/02/11 10:49:01 harti Exp $
88f444c9 40 * $DragonFly: src/usr.bin/make/var.c,v 1.98 2005/02/15 11:11:29 okumoto Exp $
984263bc
MD
41 */
42
984263bc
MD
43/*-
44 * var.c --
45 * Variable-handling functions
46 *
47 * Interface:
48 * Var_Set Set the value of a variable in the given
49 * context. The variable is created if it doesn't
50 * yet exist. The value and variable name need not
51 * be preserved.
52 *
53 * Var_Append Append more characters to an existing variable
54 * in the given context. The variable needn't
55 * exist already -- it will be created if it doesn't.
56 * A space is placed between the old value and the
57 * new one.
58 *
59 * Var_Exists See if a variable exists.
60 *
61 * Var_Value Return the value of a variable in a context or
62 * NULL if the variable is undefined.
63 *
64 * Var_Subst Substitute named variable, or all variables if
65 * NULL in a string using
66 * the given context as the top-most one. If the
67 * third argument is non-zero, Parse_Error is
68 * called if any variables are undefined.
69 *
70 * Var_Parse Parse a variable expansion from a string and
71 * return the result and the number of characters
72 * consumed.
73 *
74 * Var_Delete Delete a variable in a context.
75 *
76 * Var_Init Initialize this module.
77 *
78 * Debugging:
79 * Var_Dump Print out all variables defined in the given
80 * context.
81 *
82 * XXX: There's a lot of duplication in these functions.
83 */
84
9863ce62
MO
85#include <ctype.h>
86#include <stdlib.h>
87#include <string.h>
88
89#include "buf.h"
90#include "config.h"
91#include "globals.h"
92#include "GNode.h"
93#include "make.h"
89aa31f8 94#include "nonints.h"
9863ce62
MO
95#include "parse.h"
96#include "str.h"
97#include "targ.h"
98#include "util.h"
99#include "var.h"
984263bc
MD
100
101/*
102 * This is a harmless return value for Var_Parse that can be used by Var_Subst
103 * to determine if there was an error in parsing -- easier than returning
104 * a flag, as things outside this module don't give a hoot.
105 */
106char var_Error[] = "";
107
108/*
109 * Similar to var_Error, but returned when the 'err' flag for Var_Parse is
110 * set false. Why not just use a constant? Well, gcc likes to condense
111 * identical string instances...
112 */
113static char varNoError[] = "";
114
115/*
116 * Internally, variables are contained in four different contexts.
117 * 1) the environment. They may not be changed. If an environment
118 * variable is appended-to, the result is placed in the global
119 * context.
120 * 2) the global context. Variables set in the Makefile are located in
121 * the global context. It is the penultimate context searched when
122 * substituting.
123 * 3) the command-line context. All variables set on the command line
124 * are placed in this context. They are UNALTERABLE once placed here.
125 * 4) the local context. Each target has associated with it a context
126 * list. On this list are located the structures describing such
127 * local variables as $(@) and $(*)
128 * The four contexts are searched in the reverse order from which they are
129 * listed.
130 */
b83dbac0
MO
131GNode *VAR_GLOBAL; /* variables from the makefile */
132GNode *VAR_CMD; /* variables defined on the command-line */
984263bc 133
b83dbac0
MO
134#define FIND_CMD 0x1 /* look in VAR_CMD when searching */
135#define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
136#define FIND_ENV 0x4 /* look in the environment also */
984263bc 137
de3fa3f4
MO
138#define OPEN_PAREN '('
139#define CLOSE_PAREN ')'
9df78e4a
MO
140#define OPEN_BRACE '{'
141#define CLOSE_BRACE '}'
de3fa3f4 142
9cf296a4 143static char *VarGetPattern(GNode *, int, char **, int, int *, size_t *,
b83dbac0 144 VarPattern *);
9edd457e 145static int VarPrintVar(void *, void *);
984263bc 146
dd57762b
MO
147/*
148 * Create a Var object.
149 *
150 * @param name Name of variable.
151 * @param value Value of variable.
152 * @param flags Flags set on variable.
153 */
154static Var *
155VarCreate(const char name[], const char value[], int flags)
156{
157 Var *v;
158
159 v = emalloc(sizeof(Var));
160 v->name = estrdup(name);
161 v->val = Buf_Init(0);
162 v->flags = flags;
163
164 if (value != NULL) {
165 Buf_Append(v->val, value);
166 }
167 return (v);
168}
169
170/*
171 * Destroy a Var object.
172 *
173 * @param v Object to destroy.
174 * @param f true if internal buffer in Buffer object is to be
175 * removed.
176 */
177static void
178VarDestroy(Var *v, Boolean f)
179{
180
181 Buf_Destroy(v->val, f);
182 free(v->name);
183 free(v);
184}
185
984263bc
MD
186/*-
187 *-----------------------------------------------------------------------
188 * VarCmp --
189 * See if the given variable matches the named one. Called from
190 * Lst_Find when searching for a variable of a given name.
191 *
192 * Results:
193 * 0 if they match. non-zero otherwise.
194 *
195 * Side Effects:
196 * none
197 *-----------------------------------------------------------------------
198 */
199static int
d98710ab 200VarCmp(const void *v, const void *name)
984263bc 201{
8e4eb27a 202
d98710ab 203 return (strcmp(name, ((const Var *)v)->name));
984263bc
MD
204}
205
db29db6d
MD
206/*-
207 *-----------------------------------------------------------------------
208 * VarPossiblyExpand --
b83dbac0 209 * Expand a variable name's embedded variables in the given context.
db29db6d
MD
210 *
211 * Results:
b83dbac0 212 * The contents of name, possibly expanded.
db29db6d
MD
213 *-----------------------------------------------------------------------
214 */
605113f3
MO
215static char *
216VarPossiblyExpand(const char *name, GNode *ctxt)
db29db6d 217{
dc2b2c55 218 char *tmp;
69021aba
MO
219
220 /*
221 * XXX make a temporary copy of the name because Var_Subst insists
222 * on writing into the string.
223 */
224 tmp = estrdup(name);
225 if (strchr(name, '$') != NULL) {
226 Buffer *buf;
227 char *str;
228
229 buf = Var_Subst(NULL, tmp, ctxt, 0);
230 str = Buf_GetAll(buf, NULL);
231 Buf_Destroy(buf, FALSE);
232
233 return(str);
234 } else {
235 return(tmp);
236 }
db29db6d
MD
237}
238
984263bc
MD
239/*-
240 *-----------------------------------------------------------------------
241 * VarFind --
242 * Find the given variable in the given context and any other contexts
243 * indicated.
244 *
84de9e23
MD
245 * Flags:
246 * FIND_GLOBAL set means look in the VAR_GLOBAL context too
247 * FIND_CMD set means to look in the VAR_CMD context too
248 * FIND_ENV set means to look in the environment
249 *
984263bc
MD
250 * Results:
251 * A pointer to the structure describing the desired variable or
9edd457e 252 * NULL if the variable does not exist.
984263bc
MD
253 *
254 * Side Effects:
255 * None
256 *-----------------------------------------------------------------------
257 */
258static Var *
ea6d42d8 259VarFind(const char *name, GNode *ctxt, int flags)
984263bc 260{
af3dea45
MO
261 Boolean localCheckEnvFirst;
262 LstNode *var;
263 char *env;
984263bc 264
f6262316
MO
265 /*
266 * If the variable name begins with a '.', it could very well be one of
267 * the local ones. We check the name against all the local variables
268 * and substitute the short version in for 'name' if it matches one of
269 * them.
270 */
271 if (name[0] == '.') {
272 switch (name[1]) {
273 case 'A':
274 if (!strcmp(name, ".ALLSRC"))
275 name = ALLSRC;
276 if (!strcmp(name, ".ARCHIVE"))
277 name = ARCHIVE;
278 break;
279 case 'I':
280 if (!strcmp(name, ".IMPSRC"))
281 name = IMPSRC;
282 break;
283 case 'M':
284 if (!strcmp(name, ".MEMBER"))
285 name = MEMBER;
286 break;
287 case 'O':
288 if (!strcmp(name, ".OODATE"))
289 name = OODATE;
290 break;
291 case 'P':
292 if (!strcmp(name, ".PREFIX"))
293 name = PREFIX;
294 break;
295 case 'T':
296 if (!strcmp(name, ".TARGET"))
297 name = TARGET;
298 break;
299 default:
300 break;
301 }
275de3ca 302 }
984263bc 303
af3dea45
MO
304 /*
305 * Note whether this is one of the specific variables we were told
306 * through the -E flag to use environment-variable-override for.
307 */
308 if (Lst_Find(&envFirstVars, name, (CompareProc *)strcmp) != NULL) {
309 localCheckEnvFirst = TRUE;
310 } else {
311 localCheckEnvFirst = FALSE;
312 }
984263bc 313
af3dea45
MO
314 /*
315 * First look for the variable in the given context. If it's not there,
316 * look for it in VAR_CMD, VAR_GLOBAL and the environment,
317 * in that order, depending on the FIND_* flags in 'flags'
318 */
319 var = Lst_Find(&ctxt->context, name, VarCmp);
320 if (var != NULL) {
321 /* got it */
322 return (Lst_Datum(var));
323 }
984263bc 324
af3dea45
MO
325 /* not there - try command line context */
326 if ((flags & FIND_CMD) && (ctxt != VAR_CMD)) {
327 var = Lst_Find(&VAR_CMD->context, name, VarCmp);
328 if (var != NULL)
329 return (Lst_Datum(var));
330 }
331
332 /* not there - try global context, but only if not -e/-E */
333 if ((flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL) &&
334 !checkEnvFirst && !localCheckEnvFirst) {
335 var = Lst_Find(&VAR_GLOBAL->context, name, VarCmp);
336 if (var != NULL)
337 return (Lst_Datum(var));
338 }
339
340 if (!(flags & FIND_ENV))
341 /* we were not told to look into the environment */
342 return (NULL);
343
344 /* look in the environment */
345 if ((env = getenv(name)) != NULL) {
346 /* craft this variable from the environment value */
347 return (VarCreate(name, env, VAR_FROM_ENV));
984263bc 348 }
b632f142 349
af3dea45
MO
350 /* deferred check for the environment (in case of -e/-E) */
351 if ((checkEnvFirst || localCheckEnvFirst) &&
352 (flags & FIND_GLOBAL) && (ctxt != VAR_GLOBAL)) {
353 var = Lst_Find(&VAR_GLOBAL->context, name, VarCmp);
354 if (var != NULL)
355 return (Lst_Datum(var));
356 }
9a4c88c2 357 return (NULL);
984263bc
MD
358}
359
360/*-
361 *-----------------------------------------------------------------------
362 * VarAdd --
84de9e23 363 * Add a new variable of name name and value val to the given context.
984263bc
MD
364 *
365 * Results:
366 * None
367 *
368 * Side Effects:
369 * The new variable is placed at the front of the given context
370 * The name and val arguments are duplicated so they may
371 * safely be freed.
372 *-----------------------------------------------------------------------
373 */
374static void
ea6d42d8 375VarAdd(const char *name, const char *val, GNode *ctxt)
984263bc 376{
dd57762b 377
5cfd56df
MO
378 Lst_AtFront(&ctxt->context, VarCreate(name, val, 0));
379
380 DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, name, val));
381}
382
984263bc
MD
383/*-
384 *-----------------------------------------------------------------------
385 * Var_Delete --
386 * Remove a variable from a context.
387 *
388 * Results:
389 * None.
390 *
391 * Side Effects:
392 * The Var structure is removed and freed.
393 *
394 *-----------------------------------------------------------------------
395 */
396void
7034e6b9 397Var_Delete(const char *name, GNode *ctxt)
984263bc 398{
e035e56b 399 LstNode *ln;
984263bc 400
913800f5 401 DEBUGF(VAR, ("%s:delete %s\n", ctxt->name, name));
0a7e0b85 402 ln = Lst_Find(&ctxt->context, name, VarCmp);
9edd457e 403 if (ln != NULL) {
5cfd56df 404 VarDestroy(Lst_Datum(ln), TRUE);
0a7e0b85 405 Lst_Remove(&ctxt->context, ln);
984263bc
MD
406 }
407}
408
409/*-
410 *-----------------------------------------------------------------------
411 * Var_Set --
412 * Set the variable name to the value val in the given context.
413 *
414 * Results:
415 * None.
416 *
417 * Side Effects:
418 * If the variable doesn't yet exist, a new record is created for it.
419 * Else the old value is freed and the new one stuck in its place
420 *
421 * Notes:
422 * The variable is searched for only in its context before being
423 * created in that context. I.e. if the context is VAR_GLOBAL,
424 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
425 * VAR_CMD->context is searched. This is done to avoid the literally
426 * thousands of unnecessary strcmp's that used to be done to
427 * set, say, $(@) or $(<).
428 *-----------------------------------------------------------------------
429 */
430void
ea6d42d8 431Var_Set(const char *name, const char *val, GNode *ctxt)
984263bc 432{
605113f3
MO
433 Var *v;
434 char *n;
984263bc
MD
435
436 /*
437 * We only look for a variable in the given context since anything set
438 * here will override anything in a lower context, so there's not much
439 * point in searching them all just to save a bit of memory...
440 */
605113f3
MO
441 n = VarPossiblyExpand(name, ctxt);
442 v = VarFind(n, ctxt, 0);
9a4c88c2 443 if (v == NULL) {
605113f3 444 VarAdd(n, val, ctxt);
984263bc 445 } else {
0964826d 446 Buf_Clear(v->val);
fcf95e9c 447 Buf_Append(v->val, val);
984263bc 448
605113f3 449 DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n, val));
984263bc
MD
450 }
451 /*
452 * Any variables given on the command line are automatically exported
453 * to the environment (as per POSIX standard)
454 */
9edd457e 455 if (ctxt == VAR_CMD || (v != (Var *)NULL && (v->flags & VAR_TO_ENV))) {
605113f3 456 setenv(n, val, 1);
984263bc 457 }
605113f3 458 free(n);
984263bc
MD
459}
460
be7281c9
MD
461/*
462 * Var_SetEnv --
463 * Set the VAR_TO_ENV flag on a variable
464 */
465void
ea6d42d8 466Var_SetEnv(const char *name, GNode *ctxt)
be7281c9 467{
f72581d7 468 Var *v;
be7281c9
MD
469
470 v = VarFind(name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
471 if (v) {
472 if ((v->flags & VAR_TO_ENV) == 0) {
ea01430f 473 v->flags |= VAR_TO_ENV;
be7281c9
MD
474 setenv(v->name, Buf_GetAll(v->val, NULL), 1);
475 }
476 } else {
477 Error("Cannot set environment flag on non-existant variable %s", name);
478 }
479}
480
984263bc
MD
481/*-
482 *-----------------------------------------------------------------------
483 * Var_Append --
484 * The variable of the given name has the given value appended to it in
485 * the given context.
486 *
487 * Results:
488 * None
489 *
490 * Side Effects:
491 * If the variable doesn't exist, it is created. Else the strings
492 * are concatenated (with a space in between).
493 *
494 * Notes:
495 * Only if the variable is being sought in the global context is the
496 * environment searched.
497 * XXX: Knows its calling circumstances in that if called with ctxt
498 * an actual target, it will only search that context since only
499 * a local variable could be being appended to. This is actually
500 * a big win and must be tolerated.
501 *-----------------------------------------------------------------------
502 */
503void
ea6d42d8 504Var_Append(const char *name, const char *val, GNode *ctxt)
984263bc 505{
605113f3
MO
506 Var *v;
507 char *n;
984263bc 508
605113f3
MO
509 n = VarPossiblyExpand(name, ctxt);
510 v = VarFind(n, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
7034e6b9 511
9a4c88c2 512 if (v == NULL) {
605113f3 513 VarAdd(n, val, ctxt);
984263bc
MD
514 } else {
515 Buf_AddByte(v->val, (Byte)' ');
fcf95e9c 516 Buf_Append(v->val, val);
984263bc 517
605113f3 518 DEBUGF(VAR, ("%s:%s = %s\n", ctxt->name, n,
9cf296a4 519 (char *)Buf_GetAll(v->val, (size_t *)NULL)));
984263bc
MD
520
521 if (v->flags & VAR_FROM_ENV) {
522 /*
523 * If the original variable came from the environment, we
524 * have to install it in the global context (we could place
525 * it in the environment, but then we should provide a way to
526 * export other variables...)
527 */
528 v->flags &= ~VAR_FROM_ENV;
0a7e0b85 529 Lst_AtFront(&ctxt->context, v);
984263bc
MD
530 }
531 }
605113f3 532 free(n);
984263bc
MD
533}
534
535/*-
536 *-----------------------------------------------------------------------
537 * Var_Exists --
538 * See if the given variable exists.
539 *
540 * Results:
541 * TRUE if it does, FALSE if it doesn't
542 *
543 * Side Effects:
544 * None.
545 *
546 *-----------------------------------------------------------------------
547 */
548Boolean
605113f3 549Var_Exists(const char *name, GNode *ctxt)
984263bc 550{
605113f3
MO
551 Var *v;
552 char *n;
984263bc 553
605113f3
MO
554 n = VarPossiblyExpand(name, ctxt);
555 v = VarFind(n, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
556 free(n);
984263bc 557
9a4c88c2 558 if (v == NULL) {
fbfaa208 559 return (FALSE);
984263bc 560 } else if (v->flags & VAR_FROM_ENV) {
5cfd56df 561 VarDestroy(v, TRUE);
984263bc 562 }
fbfaa208 563 return (TRUE);
984263bc
MD
564}
565
566/*-
567 *-----------------------------------------------------------------------
568 * Var_Value --
569 * Return the value of the named variable in the given context
570 *
571 * Results:
572 * The value if the variable exists, NULL if it doesn't
573 *
574 * Side Effects:
575 * None
576 *-----------------------------------------------------------------------
577 */
578char *
605113f3 579Var_Value(const char *name, GNode *ctxt, char **frp)
984263bc 580{
605113f3
MO
581 Var *v;
582 char *n;
984263bc 583
605113f3
MO
584 n = VarPossiblyExpand(name, ctxt);
585 v = VarFind(n, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
586 free(n);
984263bc 587 *frp = NULL;
9a4c88c2 588 if (v != NULL) {
9cf296a4 589 char *p = (char *)Buf_GetAll(v->val, (size_t *)NULL);
9a4c88c2 590
984263bc 591 if (v->flags & VAR_FROM_ENV) {
5cfd56df 592 VarDestroy(v, FALSE);
984263bc
MD
593 *frp = p;
594 }
fbfaa208 595 return (p);
984263bc 596 } else {
9a4c88c2 597 return (NULL);
984263bc
MD
598 }
599}
600
984263bc
MD
601/*-
602 *-----------------------------------------------------------------------
603 * VarModify --
604 * Modify each of the words of the passed string using the given
605 * function. Used to implement all modifiers.
606 *
607 * Results:
608 * A string of all the words modified appropriately.
609 *
610 * Side Effects:
611 * None.
612 *
613 *-----------------------------------------------------------------------
614 */
615static char *
07d5ef71 616VarModify(char *str, VarModifyProc *modProc, void *datum)
984263bc 617{
6a3d9147 618 Buffer *buf; /* Buffer for the new string */
984263bc
MD
619 Boolean addSpace; /* TRUE if need to add a space to the
620 * buffer before adding the trimmed
621 * word */
622 char **av; /* word list [first word does not count] */
623 int ac, i;
624
fbfaa208 625 buf = Buf_Init(0);
984263bc
MD
626 addSpace = FALSE;
627
628 av = brk_string(str, &ac, FALSE);
629
630 for (i = 1; i < ac; i++)
631 addSpace = (*modProc)(av[i], addSpace, buf, datum);
632
8294fbb3 633 Buf_AddByte(buf, '\0');
9cf296a4 634 str = (char *)Buf_GetAll(buf, (size_t *)NULL);
fbfaa208 635 Buf_Destroy(buf, FALSE);
984263bc
MD
636 return (str);
637}
638
372b1ac6
JS
639/*-
640 *-----------------------------------------------------------------------
641 * VarSortWords --
642 * Sort the words in the string.
643 *
644 * Input:
645 * str String whose words should be sorted
646 * cmp A comparison function to control the ordering
647 *
648 * Results:
649 * A string containing the words sorted
650 *
651 * Side Effects:
652 * None.
653 *
654 *-----------------------------------------------------------------------
655 */
656static char *
657VarSortWords(char *str, int (*cmp)(const void *, const void *))
658{
6a3d9147 659 Buffer *buf;
372b1ac6
JS
660 char **av;
661 int ac, i;
662
663 buf = Buf_Init(0);
664 av = brk_string(str, &ac, FALSE);
9a4c88c2 665 qsort(av + 1, ac - 1, sizeof(char *), cmp);
372b1ac6 666 for (i = 1; i < ac; i++) {
fcf95e9c 667 Buf_Append(buf, av[i]);
372b1ac6
JS
668 Buf_AddByte(buf, (Byte)((i < ac - 1) ? ' ' : '\0'));
669 }
9cf296a4 670 str = (char *)Buf_GetAll(buf, (size_t *)NULL);
372b1ac6
JS
671 Buf_Destroy(buf, FALSE);
672 return (str);
673}
674
675static int
676SortIncreasing(const void *l, const void *r)
677{
8e4eb27a 678
372b1ac6
JS
679 return (strcmp(*(const char* const*)l, *(const char* const*)r));
680}
681
984263bc
MD
682/*-
683 *-----------------------------------------------------------------------
684 * VarGetPattern --
685 * Pass through the tstr looking for 1) escaped delimiters,
686 * '$'s and backslashes (place the escaped character in
687 * uninterpreted) and 2) unescaped $'s that aren't before
688 * the delimiter (expand the variable substitution unless flags
689 * has VAR_NOSUBST set).
690 * Return the expanded string or NULL if the delimiter was missing
691 * If pattern is specified, handle escaped ampersands, and replace
692 * unescaped ampersands with the lhs of the pattern.
693 *
694 * Results:
695 * A string of all the words modified appropriately.
696 * If length is specified, return the string length of the buffer
697 * If flags is specified and the last character of the pattern is a
698 * $ set the VAR_MATCH_END bit of flags.
699 *
700 * Side Effects:
701 * None.
702 *-----------------------------------------------------------------------
703 */
704static char *
84de9e23 705VarGetPattern(GNode *ctxt, int err, char **tstr, int delim, int *flags,
9cf296a4 706 size_t *length, VarPattern *pattern)
984263bc
MD
707{
708 char *cp;
6a3d9147 709 Buffer *buf = Buf_Init(0);
9cf296a4 710 size_t junk;
fbfaa208 711
984263bc
MD
712 if (length == NULL)
713 length = &junk;
714
913800f5 715#define IS_A_MATCH(cp, delim) \
984263bc
MD
716 ((cp[0] == '\\') && ((cp[1] == delim) || \
717 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
718
719 /*
720 * Skim through until the matching delimiter is found;
721 * pick up variable substitutions on the way. Also allow
722 * backslashes to quote the delimiter, $, and \, but don't
723 * touch other backslashes.
724 */
725 for (cp = *tstr; *cp && (*cp != delim); cp++) {
726 if (IS_A_MATCH(cp, delim)) {
fbfaa208 727 Buf_AddByte(buf, (Byte)cp[1]);
984263bc
MD
728 cp++;
729 } else if (*cp == '$') {
730 if (cp[1] == delim) {
731 if (flags == NULL)
fbfaa208 732 Buf_AddByte(buf, (Byte)*cp);
984263bc
MD
733 else
734 /*
735 * Unescaped $ at end of pattern => anchor
736 * pattern at end.
737 */
738 *flags |= VAR_MATCH_END;
739 } else {
740 if (flags == NULL || (*flags & VAR_NOSUBST) == 0) {
741 char *cp2;
9cf296a4 742 size_t len;
984263bc
MD
743 Boolean freeIt;
744
745 /*
746 * If unescaped dollar sign not before the
747 * delimiter, assume it's a variable
748 * substitution and recurse.
749 */
750 cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
fcf95e9c 751 Buf_Append(buf, cp2);
984263bc
MD
752 if (freeIt)
753 free(cp2);
754 cp += len - 1;
755 } else {
756 char *cp2 = &cp[1];
757
9df78e4a 758 if (*cp2 == OPEN_PAREN || *cp2 == OPEN_BRACE) {
984263bc
MD
759 /*
760 * Find the end of this variable reference
761 * and suck it in without further ado.
762 * It will be interperated later.
763 */
764 int have = *cp2;
9df78e4a 765 int want = (*cp2 == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
984263bc
MD
766 int depth = 1;
767
768 for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
769 if (cp2[-1] != '\\') {
770 if (*cp2 == have)
771 ++depth;
772 if (*cp2 == want)
773 --depth;
774 }
775 }
76b1c519 776 Buf_AppendRange(buf, cp, cp2);
984263bc
MD
777 cp = --cp2;
778 } else
fbfaa208 779 Buf_AddByte(buf, (Byte)*cp);
984263bc
MD
780 }
781 }
782 }
783 else if (pattern && *cp == '&')
784 Buf_AddBytes(buf, pattern->leftLen, (Byte *)pattern->lhs);
785 else
fbfaa208 786 Buf_AddByte(buf, (Byte)*cp);
984263bc
MD
787 }
788
8294fbb3
MO
789 Buf_AddByte(buf, (Byte)'\0');
790
984263bc
MD
791 if (*cp != delim) {
792 *tstr = cp;
793 *length = 0;
fbfaa208
MO
794 return (NULL);
795 } else {
984263bc 796 *tstr = ++cp;
fbfaa208 797 cp = (char *)Buf_GetAll(buf, length);
984263bc
MD
798 *length -= 1; /* Don't count the NULL */
799 Buf_Destroy(buf, FALSE);
fbfaa208 800 return (cp);
984263bc
MD
801 }
802}
803
984263bc
MD
804/*-
805 *-----------------------------------------------------------------------
c382ef3f 806 * Var_Quote --
984263bc
MD
807 * Quote shell meta-characters in the string
808 *
809 * Results:
810 * The quoted string
811 *
812 * Side Effects:
813 * None.
814 *
815 *-----------------------------------------------------------------------
816 */
c382ef3f
JS
817char *
818Var_Quote(const char *str)
984263bc 819{
6a3d9147 820 Buffer *buf;
984263bc
MD
821 /* This should cover most shells :-( */
822 static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
b839b89f 823 char *ret;
984263bc 824
fbfaa208 825 buf = Buf_Init(MAKE_BSIZE);
984263bc
MD
826 for (; *str; str++) {
827 if (strchr(meta, *str) != NULL)
828 Buf_AddByte(buf, (Byte)'\\');
829 Buf_AddByte(buf, (Byte)*str);
830 }
8294fbb3 831 Buf_AddByte(buf, (Byte)'\0');
b839b89f 832 ret = Buf_GetAll(buf, NULL);
fbfaa208 833 Buf_Destroy(buf, FALSE);
b839b89f 834 return (ret);
984263bc
MD
835}
836
422e5813
MO
837/*-
838 *-----------------------------------------------------------------------
839 * VarREError --
840 * Print the error caused by a regcomp or regexec call.
841 *
842 * Results:
843 * None.
844 *
845 * Side Effects:
846 * An error gets printed.
847 *
848 *-----------------------------------------------------------------------
849 */
850void
851VarREError(int err, regex_t *pat, const char *str)
852{
853 char *errbuf;
854 int errlen;
855
856 errlen = regerror(err, pat, 0, 0);
857 errbuf = emalloc(errlen);
858 regerror(err, pat, errbuf, errlen);
859 Error("%s: %s", str, errbuf);
860 free(errbuf);
861}
862
dc2b2c55
MO
863/*
864 * Make sure this variable is fully expanded.
865 */
866char *
867VarExpand(Var *v, GNode *ctxt, Boolean err)
868{
869 char *value;
870 char *result;
871
872 if (v->flags & VAR_IN_USE) {
873 Fatal("Variable %s is recursive.", v->name);
874 /* NOTREACHED */
875 }
876
877 v->flags |= VAR_IN_USE;
878
879 /*
880 * Before doing any modification, we have to make sure the
881 * value has been fully expanded. If it looks like recursion
882 * might be necessary (there's a dollar sign somewhere in the
883 * variable's value) we just call Var_Subst to do any other
884 * substitutions that are necessary. Note that the value
885 * returned by Var_Subst will have been
886 * dynamically-allocated, so it will need freeing when we
887 * return.
888 */
889 value = (char *)Buf_GetAll(v->val, (size_t *)NULL);
890 if (strchr(value, '$') == NULL) {
891 result = strdup(value);
892 } else {
893 Buffer *buf;
894
895 buf = Var_Subst(NULL, value, ctxt, err);
896 result = Buf_GetAll(buf, NULL);
897 Buf_Destroy(buf, FALSE);
898 }
899
900 v->flags &= ~VAR_IN_USE;
901
902 return (result);
903}
904
984263bc
MD
905/*-
906 *-----------------------------------------------------------------------
907 * Var_Parse --
908 * Given the start of a variable invocation, extract the variable
909 * name and find its value, then modify it according to the
910 * specification.
911 *
912 * Results:
913 * The (possibly-modified) value of the variable or var_Error if the
914 * specification is invalid. The length of the specification is
915 * placed in *lengthPtr (for invalid specifications, this is just
3b340ba1
JS
916 * 2 to skip the '$' and the following letter, or 1 if '$' was the
917 * last character in the string).
984263bc
MD
918 * A Boolean in *freePtr telling whether the returned string should
919 * be freed by the caller.
920 *
921 * Side Effects:
922 * None.
923 *
5223c049
MO
924 * Assumption:
925 * It is assumed that Var_Parse() is called with str[0] == '$'.
926 *
984263bc
MD
927 *-----------------------------------------------------------------------
928 */
929char *
7473c372 930Var_Parse(char *foo, GNode *ctxt, Boolean err, size_t *lengthPtr,
7034e6b9 931 Boolean *freePtr)
984263bc 932{
7473c372 933 const char *input = foo;
984263bc 934
7473c372 935 if (input[1] == '\0') {
4e473958
MO
936 /*
937 * Error, there is only a dollar sign in the input string.
938 */
962507cc 939 *freePtr = FALSE;
4e473958
MO
940 *lengthPtr = 1;
941 return (err ? var_Error : varNoError);
942
7473c372 943 } else if (input[1] == OPEN_PAREN || input[1] == OPEN_BRACE) {
4e473958
MO
944 /*
945 * Check if brackets contain a variable name.
946 */
88f444c9
MO
947 char *rw_str = foo;
948 Var *v; /* Variable in invocation */
2b58e883
MO
949 const char *vname;
950 size_t vlen; /* length of variable name, after embedded variable
95e5cd47 951 * expansion */
14f2263c
MO
952 Boolean haveModifier; /* TRUE if have modifiers for the variable */
953 char endc; /* Ending character when variable in parens
954 * or braces */
955 char startc; /* Starting character when variable in parens
956 * or braces */
957 char *tstr; /* Pointer into str */
88f444c9
MO
958 Boolean dynamic; /* TRUE if the variable is local and we're
959 * expanding it in a non-local context. This
960 * is done to support dynamic sources. The
961 * result is just the invocation, unaltered */
4e473958 962
984263bc 963 /* build up expanded variable name in this buffer */
6a3d9147 964 Buffer *buf = Buf_Init(MAKE_BSIZE);
984263bc 965
984263bc
MD
966 /*
967 * Skip to the end character or a colon, whichever comes first,
968 * replacing embedded variables as we go.
969 */
7473c372 970 startc = input[1];
9df78e4a 971 endc = (startc == OPEN_PAREN) ? CLOSE_PAREN : CLOSE_BRACE;
7473c372 972 tstr = rw_str + 2;
12fcaf94 973
12fcaf94
MO
974 while (*tstr != '\0' && *tstr != endc && *tstr != ':') {
975 if (*tstr == '$') {
976 size_t rlen;
977 Boolean rfree;
978 char *rval;
979
980 rval = Var_Parse(tstr, ctxt, err, &rlen, &rfree);
981 if (rval == var_Error) {
982 Fatal("Error expanding embedded variable.");
15f70477
MO
983 }
984 if (rval != NULL) {
12fcaf94
MO
985 Buf_Append(buf, rval);
986 if (rfree)
987 free(rval);
988 }
989 tstr += rlen - 1;
990 } else {
991 Buf_AddByte(buf, (Byte)*tstr);
992 }
993 tstr++;
994 }
fbfaa208 995
984263bc
MD
996 if (*tstr == '\0') {
997 /*
998 * If we never did find the end character, return NULL
999 * right now, setting the length to be the distance to
1000 * the end of the string, since that's what make does.
1001 */
962507cc 1002 *freePtr = FALSE;
7473c372 1003 *lengthPtr = tstr - input;
984263bc
MD
1004 return (var_Error);
1005 }
fbfaa208 1006
984263bc 1007 haveModifier = (*tstr == ':');
12fcaf94 1008 *tstr = '\0'; /* modify input string */
984263bc 1009
2b58e883
MO
1010 vname = Buf_GetAll(buf, (size_t *)NULL); /* REPLACE str */
1011 vlen = strlen(vname);
984263bc 1012
2b58e883 1013 v = VarFind(vname, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
962507cc 1014
55587644
MO
1015 if ((v == NULL) &&
1016 (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
2b58e883 1017 (vlen == 2) && (vname[1] == 'F' || vname[1] == 'D'))
984263bc
MD
1018 {
1019 /*
1020 * Check for bogus D and F forms of local variables since we're
1021 * in a local context and the name is the right length.
1022 */
2b58e883 1023 if (strchr("!%*<>@", vname[0]) != NULL) {
8a26c36f 1024 char name[2];
e8377aa5 1025 char *val;
984263bc 1026
e8377aa5
MO
1027 /*
1028 * Well, it's local -- go look for it.
1029 */
2b58e883 1030 name[0] = vname[0];
8a26c36f 1031 name[1] = '\0';
e8377aa5 1032
8a26c36f 1033 v = VarFind(name, ctxt, 0);
e8377aa5 1034 if (v != NULL && !haveModifier) {
984263bc 1035 /*
e8377aa5
MO
1036 * No need for nested expansion or anything, as we're
1037 * the only one who sets these things and we sure don't
1038 * put nested invocations in them...
984263bc 1039 */
e8377aa5 1040 val = (char *)Buf_GetAll(v->val, (size_t *)NULL);
984263bc 1041
2b58e883 1042 if (vname[1] == 'D') {
e8377aa5
MO
1043 val = VarModify(val, VarHead, (void *)NULL);
1044 } else {
1045 val = VarModify(val, VarTail, (void *)NULL);
984263bc 1046 }
e8377aa5
MO
1047 /*
1048 * Resulting string is dynamically allocated, so
1049 * tell caller to free it.
1050 */
1051 *freePtr = TRUE;
7473c372 1052 *lengthPtr = tstr - input + 1;
e8377aa5
MO
1053 *tstr = endc;
1054 Buf_Destroy(buf, TRUE);
1055 return (val);
984263bc
MD
1056 }
1057 }
1058 }
1059
55587644
MO
1060 if (v == NULL) {
1061 if (((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) &&
7473c372 1062 ((vlen == 1) ||
2b58e883 1063 ((vlen == 2) && (vname[1] == 'F' || vname[1] == 'D'))))
984263bc
MD
1064 {
1065 /*
1066 * If substituting a local variable in a non-local context,
1067 * assume it's for dynamic source stuff. We have to handle
1068 * this specially and return the longhand for the variable
1069 * with the dollar sign escaped so it makes it back to the
1070 * caller. Only four of the local variables are treated
1071 * specially as they are the only four that will be set
1072 * when dynamic sources are expanded.
1073 */
2b58e883 1074 if (strchr("!%*@", vname[0]) != NULL) {
e8377aa5
MO
1075 dynamic = TRUE;
1076 } else {
1077 dynamic = FALSE;
984263bc 1078 }
55587644
MO
1079 } else if (((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) &&
1080 (vlen > 2) &&
2b58e883
MO
1081 (vname[0] == '.') &&
1082 isupper((unsigned char)vname[1]))
984263bc
MD
1083 {
1084 int len;
1085
1086 len = vlen - 1;
2b58e883
MO
1087 if ((strncmp(vname, ".TARGET", len) == 0) ||
1088 (strncmp(vname, ".ARCHIVE", len) == 0) ||
1089 (strncmp(vname, ".PREFIX", len) == 0) ||
1090 (strncmp(vname, ".MEMBER", len) == 0))
984263bc
MD
1091 {
1092 dynamic = TRUE;
962507cc
MO
1093 } else {
1094 dynamic = FALSE;
984263bc 1095 }
962507cc
MO
1096 } else {
1097 dynamic = FALSE;
984263bc
MD
1098 }
1099
002d4cd8
MO
1100 if (haveModifier) {
1101 /*
1102 * Still need to get to the end of the variable specification,
1103 * so kludge up a Var structure for the modifications
1104 */
2b58e883 1105 v = VarCreate(vname, NULL, VAR_JUNK);
002d4cd8
MO
1106
1107 } else {
984263bc
MD
1108 /*
1109 * No modifiers -- have specification length so we can return
1110 * now.
1111 */
984263bc 1112 if (dynamic) {
27346dfe 1113 char *result;
7473c372 1114 size_t rlen = tstr - input + 1;
27346dfe
MO
1115
1116 result = emalloc(rlen + 1);
7473c372 1117 strncpy(result, input, rlen);
27346dfe
MO
1118 result[rlen] = '\0';
1119
984263bc 1120 *freePtr = TRUE;
27346dfe
MO
1121 *lengthPtr = rlen;
1122 *tstr = endc;
1123
984263bc 1124 Buf_Destroy(buf, TRUE);
27346dfe 1125 return (result);
984263bc 1126 } else {
962507cc 1127 *freePtr = FALSE;
7473c372 1128 *lengthPtr = tstr - input + 1;
27346dfe
MO
1129 *tstr = endc;
1130
984263bc
MD
1131 Buf_Destroy(buf, TRUE);
1132 return (err ? var_Error : varNoError);
1133 }
002d4cd8 1134 }
962507cc
MO
1135 } else {
1136 dynamic = FALSE;
002d4cd8 1137 }
962507cc 1138 *freePtr = FALSE;
002d4cd8
MO
1139 Buf_Destroy(buf, TRUE);
1140
14f2263c
MO
1141 rw_str = VarExpand(v, ctxt, err);
1142 *freePtr = TRUE;
984263bc 1143
984263bc 1144 /*
14f2263c
MO
1145 * Now we need to apply any modifiers the user wants applied.
1146 * These are:
1147 * :M<pattern>
1148 * words which match the given <pattern>.
1149 * <pattern> is of the standard file
1150 * wildcarding form.
1151 * :S<d><pat1><d><pat2><d>[g]
1152 * Substitute <pat2> for <pat1> in the value
1153 * :C<d><pat1><d><pat2><d>[g]
1154 * Substitute <pat2> for regex <pat1> in the value
1155 * :H Substitute the head of each word
1156 * :T Substitute the tail of each word
1157 * :E Substitute the extension (minus '.') of
1158 * each word
1159 * :R Substitute the root of each word
1160 * (pathname minus the suffix).
1161 * :lhs=rhs
1162 * Like :S, but the rhs goes to the end of
1163 * the invocation.
1164 * :U Converts variable to upper-case.
1165 * :L Converts variable to lower-case.
984263bc 1166 */
14f2263c
MO
1167 if (haveModifier) {
1168 char *cp;
1169 /*
1170 * Skip initial colon while putting it back.
1171 */
1172 *tstr++ = ':';
1173 while (*tstr != endc) {
1174 char *newStr; /* New value to return */
1175 char termc; /* Character which terminated scan */
1176
1177 DEBUGF(VAR, ("Applying :%c to \"%s\"\n", *tstr, rw_str));
1178 switch (*tstr) {
1179 case 'N':
1180 case 'M':
984263bc 1181 {
14f2263c
MO
1182 char *pattern;
1183 char *cp2;
1184 Boolean copy;
1185
1186 copy = FALSE;
1187 for (cp = tstr + 1;
1188 *cp != '\0' && *cp != ':' && *cp != endc;
1189 cp++)
1190 {
1191 if (*cp == '\\' && (cp[1] == ':' || cp[1] == endc)) {
1192 copy = TRUE;
1193 cp++;
1194 }
1195 }
1196 termc = *cp;
1197 *cp = '\0';
1198 if (copy) {
1199 /*
1200 * Need to compress the \:'s out of the pattern, so
1201 * allocate enough room to hold the uncompressed
1202 * pattern (note that cp started at tstr+1, so
1203 * cp - tstr takes the null byte into account) and
1204 * compress the pattern into the space.
1205 */
1206 pattern = emalloc(cp - tstr);
1207 for (cp2 = pattern, cp = tstr + 1;
1208 *cp != '\0';
1209 cp++, cp2++)
1210 {
1211 if ((*cp == '\\') &&
1212 (cp[1] == ':' || cp[1] == endc)) {
1213 cp++;
1214 }
1215 *cp2 = *cp;
1216 }
1217 *cp2 = '\0';
1218 } else {
1219 pattern = &tstr[1];
1220 }
1221 if (*tstr == 'M' || *tstr == 'm') {
1222 newStr = VarModify(rw_str, VarMatch, pattern);
1223 } else {
1224 newStr = VarModify(rw_str, VarNoMatch, pattern);
984263bc 1225 }
14f2263c
MO
1226 if (copy) {
1227 free(pattern);
1228 }
1229 break;
984263bc 1230 }
14f2263c
MO
1231 case 'S':
1232 {
1233 VarPattern pattern;
1234 char del;
1235 Buffer *buf; /* Buffer for patterns */
1236
1237 pattern.flags = 0;
1238 del = tstr[1];
1239 tstr += 2;
1240
984263bc 1241 /*
14f2263c
MO
1242 * If pattern begins with '^', it is anchored to the
1243 * start of the word -- skip over it and flag pattern.
984263bc 1244 */
14f2263c
MO
1245 if (*tstr == '^') {
1246 pattern.flags |= VAR_MATCH_START;
1247 tstr += 1;
1248 }
1249
1250 buf = Buf_Init(0);
1251
1252 /*
1253 * Pass through the lhs looking for 1) escaped delimiters,
1254 * '$'s and backslashes (place the escaped character in
1255 * uninterpreted) and 2) unescaped $'s that aren't before
1256 * the delimiter (expand the variable substitution).
1257 * The result is left in the Buffer buf.
1258 */
1259 for (cp = tstr; *cp != '\0' && *cp != del; cp++) {
984263bc 1260 if ((*cp == '\\') &&
14f2263c
MO
1261 ((cp[1] == del) ||
1262 (cp[1] == '$') ||
1263 (cp[1] == '\\')))
1264 {
1265 Buf_AddByte(buf, (Byte)cp[1]);
1266 cp++;
1267 } else if (*cp == '$') {
1268 if (cp[1] != del) {
1269 /*
1270 * If unescaped dollar sign not before the
1271 * delimiter, assume it's a variable
1272 * substitution and recurse.
1273 */
1274 char *cp2;
1275 size_t len;
1276 Boolean freeIt;
1277
1278 cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
1279 Buf_Append(buf, cp2);
1280 if (freeIt) {
1281 free(cp2);
1282 }
1283 cp += len - 1;
1284 } else {
1285 /*
1286 * Unescaped $ at end of pattern => anchor
1287 * pattern at end.
1288 */
1289 pattern.flags |= VAR_MATCH_END;
1290 }
1291 } else {
1292 Buf_AddByte(buf, (Byte)*cp);
984263bc 1293 }
984263bc 1294 }
984263bc 1295
14f2263c 1296 Buf_AddByte(buf, (Byte)'\0');
984263bc 1297
14f2263c
MO
1298 /*
1299 * If lhs didn't end with the delimiter, complain and
1300 * exit.
1301 */
1302 if (*cp != del) {
1303 Fatal("Unclosed substitution for %s (%c missing)",
1304 v->name, del);
1305 }
984263bc 1306
14f2263c
MO
1307 /*
1308 * Fetch pattern and destroy buffer, but preserve the data
1309 * in it, since that's our lhs. Note that Buf_GetAll
1310 * will return the actual number of bytes, which includes
1311 * the null byte, so we have to decrement the length by
1312 * one.
1313 */
1314 pattern.lhs = (char *)Buf_GetAll(buf, &pattern.leftLen);
1315 pattern.leftLen--;
1316 Buf_Destroy(buf, FALSE);
984263bc 1317
14f2263c
MO
1318 /*
1319 * Now comes the replacement string. Three things need to
1320 * be done here: 1) need to compress escaped delimiters and
1321 * ampersands and 2) need to replace unescaped ampersands
1322 * with the l.h.s. (since this isn't regexp, we can do
1323 * it right here) and 3) expand any variable substitutions.
1324 */
1325 buf = Buf_Init(0);
1326
1327 tstr = cp + 1;
1328 for (cp = tstr; *cp != '\0' && *cp != del; cp++) {
1329 if ((*cp == '\\') &&
1330 ((cp[1] == del) ||
1331 (cp[1] == '&') ||
1332 (cp[1] == '\\') ||
1333 (cp[1] == '$')))
1334 {
1335 Buf_AddByte(buf, (Byte)cp[1]);
1336 cp++;
1337 } else if ((*cp == '$') && (cp[1] != del)) {
1338 char *cp2;
9cf296a4 1339 size_t len;
14f2263c 1340 Boolean freeIt;
984263bc
MD
1341
1342 cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
fcf95e9c 1343 Buf_Append(buf, cp2);
14f2263c 1344 cp += len - 1;
984263bc
MD
1345 if (freeIt) {
1346 free(cp2);
1347 }
14f2263c
MO
1348 } else if (*cp == '&') {
1349 Buf_AddBytes(buf, pattern.leftLen,
1350 (Byte *)pattern.lhs);
984263bc 1351 } else {
14f2263c 1352 Buf_AddByte(buf, (Byte)*cp);
984263bc 1353 }
984263bc 1354 }
984263bc 1355
14f2263c 1356 Buf_AddByte(buf, (Byte)'\0');
8294fbb3 1357
14f2263c
MO
1358 /*
1359 * If didn't end in delimiter character, complain
1360 */
1361 if (*cp != del) {
1362 Fatal("Unclosed substitution for %s (%c missing)",
1363 v->name, del);
1364 }
984263bc 1365
14f2263c
MO
1366 pattern.rhs = (char *)Buf_GetAll(buf, &pattern.rightLen);
1367 pattern.rightLen--;
1368 Buf_Destroy(buf, FALSE);
984263bc 1369
14f2263c
MO
1370 /*
1371 * Check for global substitution. If 'g' after the final
1372 * delimiter, substitution is global and is marked that
1373 * way.
1374 */
1375 cp++;
1376 if (*cp == 'g') {
1377 pattern.flags |= VAR_SUB_GLOBAL;
984263bc 1378 cp++;
984263bc 1379 }
984263bc 1380
14f2263c
MO
1381 /*
1382 * Global substitution of the empty string causes an
1383 * infinite number of matches, unless anchored by '^'
1384 * (start of string) or '$' (end of string). Catch the
1385 * infinite substitution here.
1386 * Note that flags can only contain the 3 bits we're
1387 * interested in so we don't have to mask unrelated
1388 * bits. We can test for equality.
1389 */
1390 if (!pattern.leftLen && pattern.flags == VAR_SUB_GLOBAL)
1391 Fatal("Global substitution of the empty string");
984263bc 1392
14f2263c
MO
1393 termc = *cp;
1394 newStr = VarModify(rw_str, VarSubstitute, &pattern);
1395 /*
1396 * Free the two strings.
1397 */
1398 free(pattern.lhs);
1399 free(pattern.rhs);
1400 break;
984263bc 1401 }
14f2263c
MO
1402 case 'C':
1403 {
1404 int delim;
1405 VarREPattern pattern;
1406 char *re;
1407 int error;
984263bc 1408
14f2263c
MO
1409 pattern.flags = 0;
1410 delim = tstr[1];
1411 tstr += 2;
6065a492 1412
14f2263c 1413 cp = tstr;
984263bc 1414
14f2263c
MO
1415 if ((re = VarGetPattern(ctxt, err, &cp, delim, NULL,
1416 NULL, NULL)) == NULL) {
1417 /* was: goto cleanup */
1418 *lengthPtr = cp - input + 1;
1419 if (*freePtr)
1420 free(rw_str);
1421 if (delim != '\0')
1422 Fatal("Unclosed substitution for %s (%c missing)",
1423 v->name, delim);
1424 return (var_Error);
1425 }
984263bc 1426
14f2263c
MO
1427 if ((pattern.replace = VarGetPattern(ctxt, err, &cp,
1428 delim, NULL, NULL, NULL)) == NULL){
1429 free(re);
1430
1431 /* was: goto cleanup */
1432 *lengthPtr = cp - input + 1;
1433 if (*freePtr)
1434 free(rw_str);
1435 if (delim != '\0')
1436 Fatal("Unclosed substitution for %s (%c missing)",
1437 v->name, delim);
1438 return (var_Error);
1439 }
984263bc 1440
14f2263c
MO
1441 for (;; cp++) {
1442 switch (*cp) {
1443 case 'g':
1444 pattern.flags |= VAR_SUB_GLOBAL;
1445 continue;
1446 case '1':
1447 pattern.flags |= VAR_SUB_ONE;
1448 continue;
1449 default:
1450 break;
1451 }
454224cf 1452 break;
984263bc 1453 }
754c8e77 1454
984263bc 1455 termc = *cp;
754c8e77 1456
14f2263c
MO
1457 error = regcomp(&pattern.re, re, REG_EXTENDED);
1458 free(re);
1459 if (error) {
1460 *lengthPtr = cp - input + 1;
1461 VarREError(error, &pattern.re, "RE substitution error");
1462 free(pattern.replace);
1463 return (var_Error);
1464 }
754c8e77 1465
14f2263c
MO
1466 pattern.nsub = pattern.re.re_nsub + 1;
1467 if (pattern.nsub < 1)
1468 pattern.nsub = 1;
1469 if (pattern.nsub > 10)
1470 pattern.nsub = 10;
1471 pattern.matches = emalloc(pattern.nsub *
1472 sizeof(regmatch_t));
1473 newStr = VarModify(rw_str, VarRESubstitute, &pattern);
1474 regfree(&pattern.re);
1475 free(pattern.replace);
1476 free(pattern.matches);
984263bc
MD
1477 break;
1478 }
14f2263c
MO
1479 case 'L':
1480 if (tstr[1] == endc || tstr[1] == ':') {
1481 Buffer *buf;
1482 buf = Buf_Init(MAKE_BSIZE);
1483 for (cp = rw_str; *cp ; cp++)
1484 Buf_AddByte(buf, (Byte)tolower(*cp));
1485
1486 Buf_AddByte(buf, (Byte)'\0');
1487 newStr = (char *)Buf_GetAll(buf, (size_t *)NULL);
1488 Buf_Destroy(buf, FALSE);
1489
1490 cp = tstr + 1;
1491 termc = *cp;
1492 break;
1493 }
1494 /* FALLTHROUGH */
1495 case 'O':
1496 if (tstr[1] == endc || tstr[1] == ':') {
1497 newStr = VarSortWords(rw_str, SortIncreasing);
1498 cp = tstr + 1;
1499 termc = *cp;
1500 break;
1501 }
1502 /* FALLTHROUGH */
1503 case 'Q':
1504 if (tstr[1] == endc || tstr[1] == ':') {
1505 newStr = Var_Quote(rw_str);
1506 cp = tstr + 1;
1507 termc = *cp;
1508 break;
1509 }
1510 /*FALLTHRU*/
1511 case 'T':
1512 if (tstr[1] == endc || tstr[1] == ':') {
1513 newStr = VarModify(rw_str, VarTail, (void *)NULL);
1514 cp = tstr + 1;
1515 termc = *cp;
1516 break;
1517 }
1518 /*FALLTHRU*/
1519 case 'U':
1520 if (tstr[1] == endc || tstr[1] == ':') {
1521 Buffer *buf;
1522 buf = Buf_Init(MAKE_BSIZE);
1523 for (cp = rw_str; *cp ; cp++)
1524 Buf_AddByte(buf, (Byte)toupper(*cp));
1525
1526 Buf_AddByte(buf, (Byte)'\0');
1527 newStr = (char *)Buf_GetAll(buf, (size_t *)NULL);
1528 Buf_Destroy(buf, FALSE);
1529
1530 cp = tstr + 1;
1531 termc = *cp;
1532 break;
1533 }
1534 /* FALLTHROUGH */
1535 case 'H':
1536 if (tstr[1] == endc || tstr[1] == ':') {
1537 newStr = VarModify(rw_str, VarHead, (void *)NULL);
1538 cp = tstr + 1;
1539 termc = *cp;
1540 break;
1541 }
1542 /*FALLTHRU*/
1543 case 'E':
1544 if (tstr[1] == endc || tstr[1] == ':') {
1545 newStr = VarModify(rw_str, VarSuffix, (void *)NULL);
1546 cp = tstr + 1;
1547 termc = *cp;
1548 break;
1549 }
1550 /*FALLTHRU*/
1551 case 'R':
1552 if (tstr[1] == endc || tstr[1] == ':') {
1553 newStr = VarModify(rw_str, VarRoot, (void *)NULL);
1554 cp = tstr + 1;
1555 termc = *cp;
1556 break;
1557 }
1558 /*FALLTHRU*/
984263bc 1559#ifdef SUNSHCMD
14f2263c
MO
1560 case 's':
1561 if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
1562 const char *error;
1563 Buffer *buf;
1564
1565 buf = Cmd_Exec(rw_str, &error);
1566 newStr = Buf_GetAll(buf, NULL);
1567 Buf_Destroy(buf, FALSE);
1568
1569 if (error)
1570 Error(error, rw_str);
1571 cp = tstr + 2;
1572 termc = *cp;
1573 break;
1574 }
1575 /*FALLTHRU*/
984263bc 1576#endif
14f2263c
MO
1577 default:
1578 {
984263bc 1579#ifdef SYSVVARSUB
14f2263c
MO
1580 /*
1581 * This can either be a bogus modifier or a System-V
1582 * substitution command.
1583 */
1584 VarPattern pattern;
1585 Boolean eqFound;
1586 int cnt;
984263bc 1587
14f2263c
MO
1588 pattern.flags = 0;
1589 eqFound = FALSE;
984263bc 1590 /*
14f2263c
MO
1591 * First we make a pass through the string trying
1592 * to verify it is a SYSV-make-style translation:
1593 * it must be: <string1>=<string2>)
984263bc 1594 */
754c8e77 1595 cp = tstr;
14f2263c
MO
1596 cnt = 1;
1597 while (*cp != '\0' && cnt) {
1598 if (*cp == '=') {
1599 eqFound = TRUE;
1600 /* continue looking for endc */
1601 }
1602 else if (*cp == endc)
1603 cnt--;
1604 else if (*cp == startc)
1605 cnt++;
1606 if (cnt)
1607 cp++;
754c8e77 1608 }
14f2263c
MO
1609 if (*cp == endc && eqFound) {
1610 int delim;
1611
1612 /*
1613 * Now we break this sucker into the lhs and
1614 * rhs. We must null terminate them of course.
1615 */
1616 cp = tstr;
1617
1618 delim = '=';
1619 if ((pattern.lhs = VarGetPattern(ctxt,
1620 err, &cp, delim, &pattern.flags, &pattern.leftLen,
1621 NULL)) == NULL) {
1622 /* was: goto cleanup */
1623 *lengthPtr = cp - input + 1;
1624 if (*freePtr)
1625 free(rw_str);
1626 if (delim != '\0')
1627 Fatal("Unclosed substitution for %s (%c missing)",
1628 v->name, delim);
1629 return (var_Error);
1630 }
754c8e77 1631
14f2263c
MO
1632 delim = endc;
1633 if ((pattern.rhs = VarGetPattern(ctxt,
1634 err, &cp, delim, NULL, &pattern.rightLen,
1635 &pattern)) == NULL) {
1636 /* was: goto cleanup */
1637 *lengthPtr = cp - input + 1;
1638 if (*freePtr)
1639 free(rw_str);
1640 if (delim != '\0')
1641 Fatal("Unclosed substitution for %s (%c missing)",
1642 v->name, delim);
1643 return (var_Error);
1644 }
984263bc 1645
14f2263c
MO
1646 /*
1647 * SYSV modifications happen through the whole
1648 * string. Note the pattern is anchored at the end.
1649 */
1650 termc = *--cp;
1651 delim = '\0';
1652 newStr = VarModify(rw_str, VarSYSVMatch, &pattern);
984263bc 1653
14f2263c
MO
1654 free(pattern.lhs);
1655 free(pattern.rhs);
754c8e77 1656
14f2263c
MO
1657 termc = endc;
1658 } else
984263bc 1659#endif
14f2263c
MO
1660 {
1661 Error("Unknown modifier '%c'\n", *tstr);
1662 for (cp = tstr+1;
1663 *cp != ':' && *cp != endc && *cp != '\0';
1664 cp++)
1665 continue;
1666 termc = *cp;
1667 newStr = var_Error;
1668 }
984263bc
MD
1669 }
1670 }
14f2263c 1671 DEBUGF(VAR, ("Result is \"%s\"\n", newStr));
984263bc 1672
14f2263c
MO
1673 if (*freePtr) {
1674 free(rw_str);
1675 }
1676 rw_str = newStr;
1677 if (rw_str != var_Error) {
1678 *freePtr = TRUE;
1679 } else {
1680 *freePtr = FALSE;
1681 }
1682 if (termc == '\0') {
1683 Error("Unclosed variable specification for %s", v->name);
1684 } else if (termc == ':') {
1685 *cp++ = termc;
1686 } else {
1687 *cp = termc;
1688 }
1689 tstr = cp;
984263bc 1690 }
14f2263c
MO
1691 *lengthPtr = tstr - input + 1;
1692 } else {
1693 *lengthPtr = tstr - input + 1;
1694 *tstr = endc;
1695 }
1696
39631e41
MO
1697 if (v->flags & VAR_FROM_ENV) {
1698 if (rw_str == (char *)Buf_GetAll(v->val, (size_t *)NULL)) {
1699 /*
1700 * Returning the value unmodified, so tell the caller to free
1701 * the thing.
1702 */
1703 *freePtr = TRUE;
1704 VarDestroy(v, FALSE);
1705 return (rw_str);
1706 } else {
1707 VarDestroy(v, TRUE);
1708 return (rw_str);
1709 }
1710 } else if (v->flags & VAR_JUNK) {
1711 /*
1712 * Perform any free'ing needed and set *freePtr to FALSE so the caller
1713 * doesn't try to free a static pointer.
1714 */
1715 if (*freePtr) {
1716 free(rw_str);
1717 }
1718 if (dynamic) {
1719 *freePtr = FALSE;
1720 VarDestroy(v, TRUE);
1721 rw_str = emalloc(*lengthPtr + 1);
1722 strncpy(rw_str, input, *lengthPtr);
1723 rw_str[*lengthPtr] = '\0';
1724 *freePtr = TRUE;
1725 return (rw_str);
1726 } else {
1727 *freePtr = FALSE;
1728 VarDestroy(v, TRUE);
1729 return (err ? var_Error : varNoError);
1730 }
1731 } else {
1732 return (rw_str);
1733 }
14f2263c
MO
1734 } else {
1735 /*
1736 * If it's not bounded by braces of some sort, life is much simpler.
1737 * We just need to check for the first character and return the
1738 * value if it exists.
1739 */
88f444c9
MO
1740 char *result;
1741 Var *v; /* Variable in invocation */
1742 char name[2];
14f2263c
MO
1743
1744 name[0] = input[1];
1745 name[1] = '\0';
1746
1747 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1748 if (v == NULL) {
1749 if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
1750 /*
1751 * If substituting a local variable in a non-local context,
1752 * assume it's for dynamic source stuff. We have to handle
1753 * this specially and return the longhand for the variable
1754 * with the dollar sign escaped so it makes it back to the
1755 * caller. Only four of the local variables are treated
1756 * specially as they are the only four that will be set
1757 * when dynamic sources are expanded.
1758 */
1759 /* XXX: It looks like $% and $! are reversed here */
984263bc 1760 *freePtr = FALSE;
14f2263c
MO
1761 *lengthPtr = 2;
1762 switch (input[1]) {
1763 case '@':
1764 return ("$(.TARGET)");
1765 case '%':
1766 return ("$(.ARCHIVE)");
1767 case '*':
1768 return ("$(.PREFIX)");
1769 case '!':
1770 return ("$(.MEMBER)");
1771 default:
1772 return (err ? var_Error : varNoError);
1773 }
984263bc 1774 } else {
14f2263c
MO
1775 *freePtr = FALSE;
1776 *lengthPtr = 2;
1777 return (err ? var_Error : varNoError);
984263bc 1778 }
14f2263c 1779 } else {
715c736a 1780 if (v->flags & VAR_FROM_ENV) {
88f444c9
MO
1781 result = VarExpand(v, ctxt, err);
1782 if (result == (char *)Buf_GetAll(v->val, (size_t *)NULL)) {
715c736a
MO
1783 /*
1784 * Returning the value unmodified, so tell the caller to free
1785 * the thing.
1786 */
1787 *freePtr = TRUE;
1788 *lengthPtr = 2;
1789 VarDestroy(v, FALSE);
88f444c9 1790 return (result);
715c736a
MO
1791 } else {
1792 *freePtr = TRUE;
1793 *lengthPtr = 2;
1794 VarDestroy(v, TRUE);
88f444c9 1795 return (result);
715c736a 1796 }
39631e41 1797 } else {
39631e41 1798 *freePtr = TRUE;
715c736a
MO
1799 *lengthPtr = 2;
1800 return (VarExpand(v, ctxt, err));
39631e41 1801 }
984263bc 1802 }
984263bc 1803 }
984263bc
MD
1804}
1805
1806/*-
1807 *-----------------------------------------------------------------------
1808 * Var_Subst --
1809 * Substitute for all variables in the given string in the given context
1810 * If undefErr is TRUE, Parse_Error will be called when an undefined
1811 * variable is encountered.
1812 *
1813 * Results:
1814 * The resulting string.
1815 *
1816 * Side Effects:
1817 * None. The old string must be freed by the caller
1818 *-----------------------------------------------------------------------
1819 */
b0ef9278 1820Buffer *
ea6d42d8 1821Var_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr)
984263bc 1822{
b0ef9278
MO
1823 Boolean errorReported;
1824 Buffer *buf; /* Buffer for forming things */
984263bc 1825
b0ef9278
MO
1826 /*
1827 * Set TRUE if an error has already been reported to prevent a
1828 * plethora of messages when recursing.
1829 */
984263bc
MD
1830 errorReported = FALSE;
1831
b0ef9278 1832 buf = Buf_Init(0);
984263bc 1833 while (*str) {
f9402228 1834 if (var == NULL && (str[0] == '$') && (str[1] == '$')) {
984263bc
MD
1835 /*
1836 * A dollar sign may be escaped either with another dollar sign.
1837 * In such a case, we skip over the escape character and store the
1838 * dollar sign into the buffer directly.
1839 */
f9402228
MO
1840 Buf_AddByte(buf, (Byte)str[0]);
1841 str += 2;
1842
b0ef9278
MO
1843 } else if (str[0] == '$') {
1844 char *val; /* Value to substitute for a variable */
1845 size_t length; /* Length of the variable invocation */
1846 Boolean doFree; /* Set true if val should be freed */
de3fa3f4
MO
1847 /*
1848 * Variable invocation.
1849 */
984263bc
MD
1850 if (var != NULL) {
1851 int expand;
1852 for (;;) {
9df78e4a 1853 if (str[1] == OPEN_PAREN || str[1] == OPEN_BRACE) {
00776eaf 1854 size_t ln;
eb3f0099 1855 const char *p = str + 2;
984263bc
MD
1856
1857 /*
1858 * Scan up to the end of the variable name.
1859 */
de3fa3f4
MO
1860 while (*p != '\0' &&
1861 *p != ':' &&
1862 *p != CLOSE_PAREN &&
9df78e4a 1863 *p != CLOSE_BRACE &&
de3fa3f4
MO
1864 *p != '$') {
1865 ++p;
1866 }
b0ef9278 1867
984263bc
MD
1868 /*
1869 * A variable inside the variable. We cannot expand
1870 * the external variable yet, so we try again with
1871 * the nested one
1872 */
1873 if (*p == '$') {
76b1c519 1874 Buf_AppendRange(buf, str, p);
984263bc
MD
1875 str = p;
1876 continue;
1877 }
1878
00776eaf
MO
1879 ln = p - (str + 2);
1880 if (var[ln] == '\0' && strncmp(var, str + 2, ln) == 0) {
b0ef9278
MO
1881 expand = TRUE;
1882 } else {
984263bc
MD
1883 /*
1884 * Not the variable we want to expand, scan
1885 * until the next variable
1886 */
b0ef9278
MO
1887 while (*p != '$' && *p != '\0')
1888 ++p;
1889
76b1c519 1890 Buf_AppendRange(buf, str, p);
984263bc
MD
1891 str = p;
1892 expand = FALSE;
b0ef9278 1893 }
b0ef9278
MO
1894 } else {
1895 /*
72554e76 1896 * Single letter variable name
b0ef9278 1897 */
eb3f0099 1898 if (var[1] == '\0' && var[0] == str[1]) {
984263bc 1899 expand = TRUE;
b0ef9278
MO
1900 } else {
1901 Buf_AddBytes(buf, 2, (const Byte *)str);
1902 str += 2;
1903 expand = FALSE;
f9402228 1904 }
984263bc 1905 }
b0ef9278 1906 break;
984263bc
MD
1907 }
1908 if (!expand)
1909 continue;
1910 }
1911
fbfaa208 1912 val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
984263bc
MD
1913
1914 /*
1915 * When we come down here, val should either point to the
1916 * value of this variable, suitably modified, or be NULL.
1917 * Length should be the total length of the potential
1918 * variable invocation (from $ to end character...)
1919 */
1920 if (val == var_Error || val == varNoError) {
1921 /*
1922 * If performing old-time variable substitution, skip over
1923 * the variable and continue with the substitution. Otherwise,
1924 * store the dollar sign and advance str so we continue with
1925 * the string...
1926 */
1927 if (oldVars) {
1928 str += length;
1929 } else if (undefErr) {
1930 /*
1931 * If variable is undefined, complain and skip the
1932 * variable. The complaint will stop us from doing anything
1933 * when the file is parsed.
1934 */
1935 if (!errorReported) {
fbfaa208 1936 Parse_Error(PARSE_FATAL,
984263bc
MD
1937 "Undefined variable \"%.*s\"",length,str);
1938 }
1939 str += length;
1940 errorReported = TRUE;
1941 } else {
bf2bcf34 1942 Buf_AddByte(buf, (Byte)*str);
984263bc
MD
1943 str += 1;
1944 }
1945 } else {
1946 /*
1947 * We've now got a variable structure to store in. But first,
1948 * advance the string pointer.
1949 */
1950 str += length;
1951
1952 /*
1953 * Copy all the characters from the variable value straight
1954 * into the new string.
1955 */
fcf95e9c 1956 Buf_Append(buf, val);
984263bc 1957 if (doFree) {
fbfaa208 1958 free(val);
984263bc
MD
1959 }
1960 }
b0ef9278
MO
1961 } else {
1962 /*
1963 * Skip as many characters as possible -- either to the end of
1964 * the string or to the next dollar sign (variable invocation).
1965 */
72554e76 1966 const char *cp = str;
b0ef9278
MO
1967
1968 do {
1969 str++;
1970 } while (str[0] != '$' && str[0] != '\0');
1971
1972 Buf_AppendRange(buf, cp, str);
984263bc
MD
1973 }
1974 }
1975
b0ef9278 1976 return (buf);
984263bc
MD
1977}
1978
1979/*-
1980 *-----------------------------------------------------------------------
1981 * Var_GetTail --
1982 * Return the tail from each of a list of words. Used to set the
1983 * System V local variables.
1984 *
1985 * Results:
1986 * The resulting string.
1987 *
1988 * Side Effects:
1989 * None.
1990 *
1991 *-----------------------------------------------------------------------
1992 */
1993char *
84de9e23 1994Var_GetTail(char *file)
984263bc 1995{
8e4eb27a 1996
9a4c88c2 1997 return (VarModify(file, VarTail, (void *)NULL));
984263bc
MD
1998}
1999
2000/*-
2001 *-----------------------------------------------------------------------
2002 * Var_GetHead --
2003 * Find the leading components of a (list of) filename(s).
2004 * XXX: VarHead does not replace foo by ., as (sun) System V make
2005 * does.
2006 *
2007 * Results:
2008 * The leading components.
2009 *
2010 * Side Effects:
2011 * None.
2012 *
2013 *-----------------------------------------------------------------------
2014 */
2015char *
84de9e23 2016Var_GetHead(char *file)
984263bc 2017{
fbfaa208 2018
9a4c88c2 2019 return (VarModify(file, VarHead, (void *)NULL));
984263bc
MD
2020}
2021
2022/*-
2023 *-----------------------------------------------------------------------
2024 * Var_Init --
2025 * Initialize the module
2026 *
2027 * Results:
2028 * None
2029 *
2030 * Side Effects:
2031 * The VAR_CMD and VAR_GLOBAL contexts are created
2032 *-----------------------------------------------------------------------
2033 */
2034void
fbfaa208 2035Var_Init(void)
984263bc 2036{
8e4eb27a 2037
fbfaa208
MO
2038 VAR_GLOBAL = Targ_NewGN("Global");
2039 VAR_CMD = Targ_NewGN("Command");
984263bc
MD
2040}
2041
984263bc
MD
2042/****************** PRINT DEBUGGING INFO *****************/
2043static int
fbfaa208 2044VarPrintVar(void *vp, void *dummy __unused)
984263bc
MD
2045{
2046 Var *v = (Var *) vp;
fbfaa208 2047
9cf296a4 2048 printf("%-16s = %s\n", v->name, (char *)Buf_GetAll(v->val, (size_t *)NULL));
84de9e23 2049 return (0);
984263bc
MD
2050}
2051
2052/*-
2053 *-----------------------------------------------------------------------
2054 * Var_Dump --
2055 * print all variables in a context
2056 *-----------------------------------------------------------------------
2057 */
2058void
fbfaa208 2059Var_Dump(GNode *ctxt)
984263bc 2060{
09040c89 2061
0a7e0b85 2062 Lst_ForEach(&ctxt->context, VarPrintVar, (void *)NULL);
984263bc 2063}