1 /* $NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
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.
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
72 static char rcsid
[] = "$NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid
[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
79 __RCSID("$NetBSD: var.c,v 1.167 2011/06/03 21:10:42 sjg Exp $");
86 * Variable-handling functions
89 * Var_Set Set the value of a variable in the given
90 * context. The variable is created if it doesn't
91 * yet exist. The value and variable name need not
94 * Var_Append Append more characters to an existing variable
95 * in the given context. The variable needn't
96 * exist already -- it will be created if it doesn't.
97 * A space is placed between the old value and the
100 * Var_Exists See if a variable exists.
102 * Var_Value Return the value of a variable in a context or
103 * NULL if the variable is undefined.
105 * Var_Subst Substitute named variable, or all variables if
106 * NULL in a string using
107 * the given context as the top-most one. If the
108 * third argument is non-zero, Parse_Error is
109 * called if any variables are undefined.
111 * Var_Parse Parse a variable expansion from a string and
112 * return the result and the number of characters
115 * Var_Delete Delete a variable in a context.
117 * Var_Init Initialize this module.
120 * Var_Dump Print out all variables defined in the given
123 * XXX: There's a lot of duplication in these functions.
126 #include <sys/stat.h>
128 #include <sys/types.h>
132 #include <inttypes.h>
143 * This is a harmless return value for Var_Parse that can be used by Var_Subst
144 * to determine if there was an error in parsing -- easier than returning
145 * a flag, as things outside this module don't give a hoot.
147 char var_Error
[] = "";
150 * Similar to var_Error, but returned when the 'errnum' flag for Var_Parse is
151 * set false. Why not just use a constant? Well, gcc likes to condense
152 * identical string instances...
154 static char varNoError
[] = "";
157 * Internally, variables are contained in four different contexts.
158 * 1) the environment. They may not be changed. If an environment
159 * variable is appended-to, the result is placed in the global
161 * 2) the global context. Variables set in the Makefile are located in
162 * the global context. It is the penultimate context searched when
164 * 3) the command-line context. All variables set on the command line
165 * are placed in this context. They are UNALTERABLE once placed here.
166 * 4) the local context. Each target has associated with it a context
167 * list. On this list are located the structures describing such
168 * local variables as $(@) and $(*)
169 * The four contexts are searched in the reverse order from which they are
172 GNode
*VAR_GLOBAL
; /* variables from the makefile */
173 GNode
*VAR_CMD
; /* variables defined on the command-line */
175 #define FIND_CMD 0x1 /* look in VAR_CMD when searching */
176 #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
177 #define FIND_ENV 0x4 /* look in the environment also */
180 char *name
; /* the variable's name */
181 Buffer val
; /* its value */
182 int flags
; /* miscellaneous status flags */
183 #define VAR_IN_USE 1 /* Variable's value currently being used.
184 * Used to avoid recursion */
185 #define VAR_FROM_ENV 2 /* Variable comes from the environment */
186 #define VAR_JUNK 4 /* Variable is a junk variable that
187 * should be destroyed when done with
188 * it. Used by Var_Parse for undefined,
189 * modified variables */
190 #define VAR_KEEP 8 /* Variable is VAR_JUNK, but we found
191 * a use for it in some modifier and
192 * the value is therefore valid */
193 #define VAR_EXPORTED 16 /* Variable is exported */
194 #define VAR_REEXPORT 32 /* Indicate if var needs re-export.
195 * This would be true if it contains $'s
197 #define VAR_FROM_CMD 64 /* Variable came from command line */
201 * Exporting vars is expensive so skip it if we can
203 #define VAR_EXPORTED_NONE 0
204 #define VAR_EXPORTED_YES 1
205 #define VAR_EXPORTED_ALL 2
206 static int var_exportedVars
= VAR_EXPORTED_NONE
;
208 * We pass this to Var_Export when doing the initial export
209 * or after updating an exported var.
211 #define VAR_EXPORT_PARENT 1
213 /* Var*Pattern flags */
214 #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */
215 #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */
216 #define VAR_SUB_MATCHED 0x04 /* There was a match */
217 #define VAR_MATCH_START 0x08 /* Match at start of word */
218 #define VAR_MATCH_END 0x10 /* Match at end of word */
219 #define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */
222 #define VAR_NO_EXPORT 0x01 /* do not export */
226 * The following fields are set by Var_Parse() when it
227 * encounters modifiers that need to keep state for use by
228 * subsequent modifiers within the same variable expansion.
230 Byte varSpace
; /* Word separator in expansions */
231 Boolean oneBigWord
; /* TRUE if we will treat the variable as a
232 * single big word, even if it contains
233 * embedded spaces (as opposed to the
234 * usual behaviour of treating it as
235 * several space-separated words). */
238 /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/",
239 * to VarSYSVMatch() for ":lhs=rhs". */
241 const char *lhs
; /* String to match */
242 int leftLen
; /* Length of string */
243 const char *rhs
; /* Replacement string (w/ &'s removed) */
244 int rightLen
; /* Length of replacement */
248 /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */
250 GNode
*ctxt
; /* variable context */
251 char *tvar
; /* name of temp var */
253 char *str
; /* string to expand */
255 int errnum
; /* errnum for not defined */
259 /* struct passed as 'void *' to VarRESubstitute() for ":C///" */
269 /* struct passed to VarSelectWords() for ":[start..end]" */
271 int start
; /* first word to select */
272 int end
; /* last word to select */
275 static Var
*VarFind(const char *, GNode
*, int);
276 static void VarAdd(const char *, const char *, GNode
*);
277 static Boolean
VarHead(GNode
*, Var_Parse_State
*,
278 char *, Boolean
, Buffer
*, void *);
279 static Boolean
VarTail(GNode
*, Var_Parse_State
*,
280 char *, Boolean
, Buffer
*, void *);
281 static Boolean
VarSuffix(GNode
*, Var_Parse_State
*,
282 char *, Boolean
, Buffer
*, void *);
283 static Boolean
VarRoot(GNode
*, Var_Parse_State
*,
284 char *, Boolean
, Buffer
*, void *);
285 static Boolean
VarMatch(GNode
*, Var_Parse_State
*,
286 char *, Boolean
, Buffer
*, void *);
288 static Boolean
VarSYSVMatch(GNode
*, Var_Parse_State
*,
289 char *, Boolean
, Buffer
*, void *);
291 static Boolean
VarNoMatch(GNode
*, Var_Parse_State
*,
292 char *, Boolean
, Buffer
*, void *);
294 static void VarREError(int, regex_t
*, const char *);
295 static Boolean
VarRESubstitute(GNode
*, Var_Parse_State
*,
296 char *, Boolean
, Buffer
*, void *);
298 static Boolean
VarSubstitute(GNode
*, Var_Parse_State
*,
299 char *, Boolean
, Buffer
*, void *);
300 static Boolean
VarLoopExpand(GNode
*, Var_Parse_State
*,
301 char *, Boolean
, Buffer
*, void *);
302 static char *VarGetPattern(GNode
*, Var_Parse_State
*,
303 int, const char **, int, int *, int *,
305 static char *VarQuote(char *);
306 static char *VarChangeCase(char *, int);
307 static char *VarHash(char *);
308 static char *VarModify(GNode
*, Var_Parse_State
*,
310 Boolean (*)(GNode
*, Var_Parse_State
*, char *, Boolean
, Buffer
*, void *),
312 static char *VarOrder(const char *, const char);
313 static char *VarUniq(const char *);
314 static int VarWordCompare(const void *, const void *);
315 static void VarPrintVar(void *);
323 *-----------------------------------------------------------------------
325 * Find the given variable in the given context and any other contexts
330 * ctxt context in which to find it
331 * flags FIND_GLOBAL set means to look in the
332 * VAR_GLOBAL context as well. FIND_CMD set means
333 * to look in the VAR_CMD context also. FIND_ENV
334 * set means to look in the environment
337 * A pointer to the structure describing the desired variable or
338 * NULL if the variable does not exist.
342 *-----------------------------------------------------------------------
345 VarFind(const char *name
, GNode
*ctxt
, int flags
)
351 * If the variable name begins with a '.', it could very well be one of
352 * the local ones. We check the name against all the local variables
353 * and substitute the short version in for 'name' if it matches one of
356 if (*name
== '.' && isupper((unsigned char) name
[1]))
359 if (!strcmp(name
, ".ALLSRC"))
361 if (!strcmp(name
, ".ARCHIVE"))
365 if (!strcmp(name
, ".IMPSRC"))
369 if (!strcmp(name
, ".MEMBER"))
373 if (!strcmp(name
, ".OODATE"))
377 if (!strcmp(name
, ".PREFIX"))
381 if (!strcmp(name
, ".TARGET"))
386 /* for compatibility with gmake */
387 if (name
[0] == '^' && name
[1] == '\0')
392 * First look for the variable in the given context. If it's not there,
393 * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
394 * depending on the FIND_* flags in 'flags'
396 var
= Hash_FindEntry(&ctxt
->context
, name
);
398 if ((var
== NULL
) && (flags
& FIND_CMD
) && (ctxt
!= VAR_CMD
)) {
399 var
= Hash_FindEntry(&VAR_CMD
->context
, name
);
401 if (!checkEnvFirst
&& (var
== NULL
) && (flags
& FIND_GLOBAL
) &&
402 (ctxt
!= VAR_GLOBAL
))
404 var
= Hash_FindEntry(&VAR_GLOBAL
->context
, name
);
406 if ((var
== NULL
) && (flags
& FIND_ENV
)) {
409 if ((env
= getenv(name
)) != NULL
) {
412 v
= bmake_malloc(sizeof(Var
));
413 v
->name
= bmake_strdup(name
);
417 Buf_Init(&v
->val
, len
+ 1);
418 Buf_AddBytes(&v
->val
, len
, env
);
420 v
->flags
= VAR_FROM_ENV
;
422 } else if (checkEnvFirst
&& (flags
& FIND_GLOBAL
) &&
423 (ctxt
!= VAR_GLOBAL
))
425 var
= Hash_FindEntry(&VAR_GLOBAL
->context
, name
);
429 return ((Var
*)Hash_GetValue(var
));
434 } else if (var
== NULL
) {
437 return ((Var
*)Hash_GetValue(var
));
442 *-----------------------------------------------------------------------
444 * If the variable is an environment variable, free it
448 * destroy true if the value buffer should be destroyed.
451 * 1 if it is an environment variable 0 ow.
454 * The variable is free'ed if it is an environent variable.
455 *-----------------------------------------------------------------------
458 VarFreeEnv(Var
*v
, Boolean destroy
)
460 if ((v
->flags
& VAR_FROM_ENV
) == 0)
463 Buf_Destroy(&v
->val
, destroy
);
469 *-----------------------------------------------------------------------
471 * Add a new variable of name name and value val to the given context
474 * name name of variable to add
475 * val value to set it to
476 * ctxt context in which to set it
482 * The new variable is placed at the front of the given context
483 * The name and val arguments are duplicated so they may
485 *-----------------------------------------------------------------------
488 VarAdd(const char *name
, const char *val
, GNode
*ctxt
)
494 v
= bmake_malloc(sizeof(Var
));
496 len
= val
? strlen(val
) : 0;
497 Buf_Init(&v
->val
, len
+1);
498 Buf_AddBytes(&v
->val
, len
, val
);
502 h
= Hash_CreateEntry(&ctxt
->context
, name
, NULL
);
506 fprintf(debug_file
, "%s:%s = %s\n", ctxt
->name
, name
, val
);
511 *-----------------------------------------------------------------------
513 * Remove a variable from a context.
519 * The Var structure is removed and freed.
521 *-----------------------------------------------------------------------
524 Var_Delete(const char *name
, GNode
*ctxt
)
528 ln
= Hash_FindEntry(&ctxt
->context
, name
);
530 fprintf(debug_file
, "%s:delete %s%s\n",
531 ctxt
->name
, name
, ln
? "" : " (not found)");
536 v
= (Var
*)Hash_GetValue(ln
);
537 if ((v
->flags
& VAR_EXPORTED
)) {
540 if (strcmp(MAKE_EXPORTED
, v
->name
) == 0) {
541 var_exportedVars
= VAR_EXPORTED_NONE
;
543 if (v
->name
!= ln
->name
)
545 Hash_DeleteEntry(&ctxt
->context
, ln
);
546 Buf_Destroy(&v
->val
, TRUE
);
554 * We ignore make internal variables (those which start with '.')
555 * Also we jump through some hoops to avoid calling setenv
556 * more than necessary since it can leak.
557 * We only manipulate flags of vars if 'parent' is set.
560 Var_Export1(const char *name
, int parent
)
568 return 0; /* skip internals */
572 * If it is one of the vars that should only appear in
573 * local context, skip it, else we can get Var_Subst
584 v
= VarFind(name
, VAR_GLOBAL
, 0);
589 (v
->flags
& (VAR_EXPORTED
|VAR_REEXPORT
)) == VAR_EXPORTED
) {
590 return 0; /* nothing to do */
592 val
= Buf_GetAll(&v
->val
, NULL
);
593 if (strchr(val
, '$')) {
596 * Flag this as something we need to re-export.
597 * No point actually exporting it now though,
598 * the child can do it at the last minute.
600 v
->flags
|= (VAR_EXPORTED
|VAR_REEXPORT
);
603 if (v
->flags
& VAR_IN_USE
) {
605 * We recursed while exporting in a child.
606 * This isn't going to end well, just skip it.
610 n
= snprintf(tmp
, sizeof(tmp
), "${%s}", name
);
611 if (n
< (int)sizeof(tmp
)) {
612 val
= Var_Subst(NULL
, tmp
, VAR_GLOBAL
, 0);
613 setenv(name
, val
, 1);
618 v
->flags
&= ~VAR_REEXPORT
; /* once will do */
620 if (parent
|| !(v
->flags
& VAR_EXPORTED
)) {
621 setenv(name
, val
, 1);
625 * This is so Var_Set knows to call Var_Export again...
628 v
->flags
|= VAR_EXPORTED
;
634 * This gets called from our children.
646 if (VAR_EXPORTED_NONE
== var_exportedVars
)
649 if (VAR_EXPORTED_ALL
== var_exportedVars
) {
651 * Ouch! This is crazy...
653 for (var
= Hash_EnumFirst(&VAR_GLOBAL
->context
, &state
);
655 var
= Hash_EnumNext(&state
)) {
656 v
= (Var
*)Hash_GetValue(var
);
657 Var_Export1(v
->name
, 0);
662 * We have a number of exported vars,
664 n
= snprintf(tmp
, sizeof(tmp
), "${" MAKE_EXPORTED
":O:u}");
665 if (n
< (int)sizeof(tmp
)) {
671 val
= Var_Subst(NULL
, tmp
, VAR_GLOBAL
, 0);
672 av
= brk_string(val
, &ac
, FALSE
, &as
);
673 for (i
= 0; i
< ac
; i
++) {
674 Var_Export1(av
[i
], 0);
683 * This is called when .export is seen or
684 * .MAKE.EXPORTED is modified.
685 * It is also called when any exported var is modified.
688 Var_Export(char *str
, int isExport
)
698 if (isExport
&& (!str
|| !str
[0])) {
699 var_exportedVars
= VAR_EXPORTED_ALL
; /* use with caution! */
703 if (strncmp(str
, "-env", 4) == 0) {
707 track
= VAR_EXPORT_PARENT
;
709 val
= Var_Subst(NULL
, str
, VAR_GLOBAL
, 0);
710 av
= brk_string(val
, &ac
, FALSE
, &as
);
711 for (i
= 0; i
< ac
; i
++) {
716 * If it is one of the vars that should only appear in
717 * local context, skip it, else we can get Var_Subst
728 if (Var_Export1(name
, track
)) {
729 if (VAR_EXPORTED_ALL
!= var_exportedVars
)
730 var_exportedVars
= VAR_EXPORTED_YES
;
731 if (isExport
&& track
) {
732 Var_Append(MAKE_EXPORTED
, name
, VAR_GLOBAL
);
743 * This is called when .unexport[-env] is seen.
746 Var_UnExport(char *str
)
751 Boolean unexport_env
;
754 if (!str
|| !str
[0]) {
755 return; /* assert? */
761 unexport_env
= (strncmp(str
, "-env", 4) == 0);
763 extern char **environ
;
764 static char **savenv
;
767 cp
= getenv(MAKE_LEVEL
); /* we should preserve this */
768 if (environ
== savenv
) {
769 /* we have been here before! */
770 newenv
= bmake_realloc(environ
, 2 * sizeof(char *));
776 newenv
= bmake_malloc(2 * sizeof(char *));
780 /* Note: we cannot safely free() the original environ. */
781 environ
= savenv
= newenv
;
784 setenv(MAKE_LEVEL
, cp
, 1);
786 for (; *str
!= '\n' && isspace((unsigned char) *str
); str
++)
788 if (str
[0] && str
[0] != '\n') {
794 /* Using .MAKE.EXPORTED */
795 n
= snprintf(tmp
, sizeof(tmp
), "${" MAKE_EXPORTED
":O:u}");
796 if (n
< (int)sizeof(tmp
)) {
797 vlist
= Var_Subst(NULL
, tmp
, VAR_GLOBAL
, 0);
807 av
= brk_string(vlist
, &ac
, FALSE
, &as
);
808 for (i
= 0; i
< ac
; i
++) {
809 v
= VarFind(av
[i
], VAR_GLOBAL
, 0);
813 (v
->flags
& (VAR_EXPORTED
|VAR_REEXPORT
)) == VAR_EXPORTED
) {
816 v
->flags
&= ~(VAR_EXPORTED
|VAR_REEXPORT
);
818 * If we are unexporting a list,
819 * remove each one from .MAKE.EXPORTED.
820 * If we are removing them all,
821 * just delete .MAKE.EXPORTED below.
824 n
= snprintf(tmp
, sizeof(tmp
),
825 "${" MAKE_EXPORTED
":N%s}", v
->name
);
826 if (n
< (int)sizeof(tmp
)) {
827 cp
= Var_Subst(NULL
, tmp
, VAR_GLOBAL
, 0);
828 Var_Set(MAKE_EXPORTED
, cp
, VAR_GLOBAL
, 0);
836 Var_Delete(MAKE_EXPORTED
, VAR_GLOBAL
);
843 *-----------------------------------------------------------------------
845 * Set the variable name to the value val in the given context.
848 * name name of variable to set
849 * val value to give to the variable
850 * ctxt context in which to set it
856 * If the variable doesn't yet exist, a new record is created for it.
857 * Else the old value is freed and the new one stuck in its place
860 * The variable is searched for only in its context before being
861 * created in that context. I.e. if the context is VAR_GLOBAL,
862 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
863 * VAR_CMD->context is searched. This is done to avoid the literally
864 * thousands of unnecessary strcmp's that used to be done to
865 * set, say, $(@) or $(<).
866 * If the context is VAR_GLOBAL though, we check if the variable
867 * was set in VAR_CMD from the command line and skip it if so.
868 *-----------------------------------------------------------------------
871 Var_Set(const char *name
, const char *val
, GNode
*ctxt
, int flags
)
874 char *expanded_name
= NULL
;
877 * We only look for a variable in the given context since anything set
878 * here will override anything in a lower context, so there's not much
879 * point in searching them all just to save a bit of memory...
881 if (strchr(name
, '$') != NULL
) {
882 expanded_name
= Var_Subst(NULL
, name
, ctxt
, 0);
883 if (expanded_name
[0] == 0) {
885 fprintf(debug_file
, "Var_Set(\"%s\", \"%s\", ...) "
886 "name expands to empty string - ignored\n",
892 name
= expanded_name
;
894 if (ctxt
== VAR_GLOBAL
) {
895 v
= VarFind(name
, VAR_CMD
, 0);
897 if ((v
->flags
& VAR_FROM_CMD
)) {
899 fprintf(debug_file
, "%s:%s = %s ignored!\n", ctxt
->name
, name
, val
);
906 v
= VarFind(name
, ctxt
, 0);
908 VarAdd(name
, val
, ctxt
);
911 Buf_AddBytes(&v
->val
, strlen(val
), val
);
914 fprintf(debug_file
, "%s:%s = %s\n", ctxt
->name
, name
, val
);
916 if ((v
->flags
& VAR_EXPORTED
)) {
917 Var_Export1(name
, VAR_EXPORT_PARENT
);
921 * Any variables given on the command line are automatically exported
922 * to the environment (as per POSIX standard)
924 if (ctxt
== VAR_CMD
&& (flags
& VAR_NO_EXPORT
) == 0) {
926 /* we just added it */
927 v
= VarFind(name
, ctxt
, 0);
930 v
->flags
|= VAR_FROM_CMD
;
932 * If requested, don't export these in the environment
933 * individually. We still put them in MAKEOVERRIDES so
934 * that the command-line settings continue to override
937 if (varNoExportEnv
!= TRUE
)
938 setenv(name
, val
, 1);
940 Var_Append(MAKEOVERRIDES
, name
, VAR_GLOBAL
);
943 * Another special case.
944 * Several make's support this sort of mechanism for tracking
945 * recursion - but each uses a different name.
946 * We allow the makefiles to update .MAKE.LEVEL and ensure
947 * children see a correctly incremented value.
949 if (ctxt
== VAR_GLOBAL
&& strcmp(MAKE_LEVEL
, name
) == 0) {
954 snprintf(tmp
, sizeof(tmp
), "%u", level
+ 1);
955 setenv(MAKE_LEVEL
, tmp
, 1);
960 if (expanded_name
!= NULL
)
967 *-----------------------------------------------------------------------
969 * The variable of the given name has the given value appended to it in
973 * name name of variable to modify
974 * val String to append to it
975 * ctxt Context in which this should occur
981 * If the variable doesn't exist, it is created. Else the strings
982 * are concatenated (with a space in between).
985 * Only if the variable is being sought in the global context is the
986 * environment searched.
987 * XXX: Knows its calling circumstances in that if called with ctxt
988 * an actual target, it will only search that context since only
989 * a local variable could be being appended to. This is actually
990 * a big win and must be tolerated.
991 *-----------------------------------------------------------------------
994 Var_Append(const char *name
, const char *val
, GNode
*ctxt
)
998 char *expanded_name
= NULL
;
1000 if (strchr(name
, '$') != NULL
) {
1001 expanded_name
= Var_Subst(NULL
, name
, ctxt
, 0);
1002 if (expanded_name
[0] == 0) {
1004 fprintf(debug_file
, "Var_Append(\"%s\", \"%s\", ...) "
1005 "name expands to empty string - ignored\n",
1008 free(expanded_name
);
1011 name
= expanded_name
;
1014 v
= VarFind(name
, ctxt
, (ctxt
== VAR_GLOBAL
) ? FIND_ENV
: 0);
1017 VarAdd(name
, val
, ctxt
);
1019 Buf_AddByte(&v
->val
, ' ');
1020 Buf_AddBytes(&v
->val
, strlen(val
), val
);
1023 fprintf(debug_file
, "%s:%s = %s\n", ctxt
->name
, name
,
1024 Buf_GetAll(&v
->val
, NULL
));
1027 if (v
->flags
& VAR_FROM_ENV
) {
1029 * If the original variable came from the environment, we
1030 * have to install it in the global context (we could place
1031 * it in the environment, but then we should provide a way to
1032 * export other variables...)
1034 v
->flags
&= ~VAR_FROM_ENV
;
1035 h
= Hash_CreateEntry(&ctxt
->context
, name
, NULL
);
1036 Hash_SetValue(h
, v
);
1039 if (expanded_name
!= NULL
)
1040 free(expanded_name
);
1044 *-----------------------------------------------------------------------
1046 * See if the given variable exists.
1049 * name Variable to find
1050 * ctxt Context in which to start search
1053 * TRUE if it does, FALSE if it doesn't
1058 *-----------------------------------------------------------------------
1061 Var_Exists(const char *name
, GNode
*ctxt
)
1066 if ((cp
= strchr(name
, '$')) != NULL
) {
1067 cp
= Var_Subst(NULL
, name
, ctxt
, FALSE
);
1069 v
= VarFind(cp
? cp
: name
, ctxt
, FIND_CMD
|FIND_GLOBAL
|FIND_ENV
);
1076 (void)VarFreeEnv(v
, TRUE
);
1082 *-----------------------------------------------------------------------
1084 * Return the value of the named variable in the given context
1088 * ctxt context in which to search for it
1091 * The value if the variable exists, NULL if it doesn't
1095 *-----------------------------------------------------------------------
1098 Var_Value(const char *name
, GNode
*ctxt
, char **frp
)
1102 v
= VarFind(name
, ctxt
, FIND_ENV
| FIND_GLOBAL
| FIND_CMD
);
1105 char *p
= (Buf_GetAll(&v
->val
, NULL
));
1106 if (VarFreeEnv(v
, FALSE
))
1115 *-----------------------------------------------------------------------
1117 * Remove the tail of the given word and place the result in the given
1122 * addSpace True if need to add a space to the buffer
1123 * before sticking in the head
1124 * buf Buffer in which to store it
1127 * TRUE if characters were added to the buffer (a space needs to be
1128 * added to the buffer before the next word).
1131 * The trimmed word is added to the buffer.
1133 *-----------------------------------------------------------------------
1136 VarHead(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1137 char *word
, Boolean addSpace
, Buffer
*buf
,
1142 slash
= strrchr(word
, '/');
1143 if (slash
!= NULL
) {
1144 if (addSpace
&& vpstate
->varSpace
) {
1145 Buf_AddByte(buf
, vpstate
->varSpace
);
1148 Buf_AddBytes(buf
, strlen(word
), word
);
1153 * If no directory part, give . (q.v. the POSIX standard)
1155 if (addSpace
&& vpstate
->varSpace
)
1156 Buf_AddByte(buf
, vpstate
->varSpace
);
1157 Buf_AddByte(buf
, '.');
1159 return(dummy
? TRUE
: TRUE
);
1163 *-----------------------------------------------------------------------
1165 * Remove the head of the given word and place the result in the given
1170 * addSpace True if need to add a space to the buffer
1171 * before adding the tail
1172 * buf Buffer in which to store it
1175 * TRUE if characters were added to the buffer (a space needs to be
1176 * added to the buffer before the next word).
1179 * The trimmed word is added to the buffer.
1181 *-----------------------------------------------------------------------
1184 VarTail(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1185 char *word
, Boolean addSpace
, Buffer
*buf
,
1190 if (addSpace
&& vpstate
->varSpace
) {
1191 Buf_AddByte(buf
, vpstate
->varSpace
);
1194 slash
= strrchr(word
, '/');
1195 if (slash
!= NULL
) {
1197 Buf_AddBytes(buf
, strlen(slash
), slash
);
1200 Buf_AddBytes(buf
, strlen(word
), word
);
1202 return (dummy
? TRUE
: TRUE
);
1206 *-----------------------------------------------------------------------
1208 * Place the suffix of the given word in the given buffer.
1212 * addSpace TRUE if need to add a space before placing the
1213 * suffix in the buffer
1214 * buf Buffer in which to store it
1217 * TRUE if characters were added to the buffer (a space needs to be
1218 * added to the buffer before the next word).
1221 * The suffix from the word is placed in the buffer.
1223 *-----------------------------------------------------------------------
1226 VarSuffix(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1227 char *word
, Boolean addSpace
, Buffer
*buf
,
1232 dot
= strrchr(word
, '.');
1234 if (addSpace
&& vpstate
->varSpace
) {
1235 Buf_AddByte(buf
, vpstate
->varSpace
);
1238 Buf_AddBytes(buf
, strlen(dot
), dot
);
1242 return (dummy
? addSpace
: addSpace
);
1246 *-----------------------------------------------------------------------
1248 * Remove the suffix of the given word and place the result in the
1253 * addSpace TRUE if need to add a space to the buffer
1254 * before placing the root in it
1255 * buf Buffer in which to store it
1258 * TRUE if characters were added to the buffer (a space needs to be
1259 * added to the buffer before the next word).
1262 * The trimmed word is added to the buffer.
1264 *-----------------------------------------------------------------------
1267 VarRoot(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1268 char *word
, Boolean addSpace
, Buffer
*buf
,
1273 if (addSpace
&& vpstate
->varSpace
) {
1274 Buf_AddByte(buf
, vpstate
->varSpace
);
1277 dot
= strrchr(word
, '.');
1280 Buf_AddBytes(buf
, strlen(word
), word
);
1283 Buf_AddBytes(buf
, strlen(word
), word
);
1285 return (dummy
? TRUE
: TRUE
);
1289 *-----------------------------------------------------------------------
1291 * Place the word in the buffer if it matches the given pattern.
1292 * Callback function for VarModify to implement the :M modifier.
1295 * word Word to examine
1296 * addSpace TRUE if need to add a space to the buffer
1297 * before adding the word, if it matches
1298 * buf Buffer in which to store it
1299 * pattern Pattern the word must match
1302 * TRUE if a space should be placed in the buffer before the next
1306 * The word may be copied to the buffer.
1308 *-----------------------------------------------------------------------
1311 VarMatch(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1312 char *word
, Boolean addSpace
, Buffer
*buf
,
1316 fprintf(debug_file
, "VarMatch [%s] [%s]\n", word
, (char *)pattern
);
1317 if (Str_Match(word
, (char *)pattern
)) {
1318 if (addSpace
&& vpstate
->varSpace
) {
1319 Buf_AddByte(buf
, vpstate
->varSpace
);
1322 Buf_AddBytes(buf
, strlen(word
), word
);
1329 *-----------------------------------------------------------------------
1331 * Place the word in the buffer if it matches the given pattern.
1332 * Callback function for VarModify to implement the System V %
1336 * word Word to examine
1337 * addSpace TRUE if need to add a space to the buffer
1338 * before adding the word, if it matches
1339 * buf Buffer in which to store it
1340 * patp Pattern the word must match
1343 * TRUE if a space should be placed in the buffer before the next
1347 * The word may be copied to the buffer.
1349 *-----------------------------------------------------------------------
1352 VarSYSVMatch(GNode
*ctx
, Var_Parse_State
*vpstate
,
1353 char *word
, Boolean addSpace
, Buffer
*buf
,
1358 VarPattern
*pat
= (VarPattern
*)patp
;
1361 if (addSpace
&& vpstate
->varSpace
)
1362 Buf_AddByte(buf
, vpstate
->varSpace
);
1366 if ((ptr
= Str_SYSVMatch(word
, pat
->lhs
, &len
)) != NULL
) {
1367 varexp
= Var_Subst(NULL
, pat
->rhs
, ctx
, 0);
1368 Str_SYSVSubst(buf
, varexp
, ptr
, len
);
1371 Buf_AddBytes(buf
, strlen(word
), word
);
1380 *-----------------------------------------------------------------------
1382 * Place the word in the buffer if it doesn't match the given pattern.
1383 * Callback function for VarModify to implement the :N modifier.
1386 * word Word to examine
1387 * addSpace TRUE if need to add a space to the buffer
1388 * before adding the word, if it matches
1389 * buf Buffer in which to store it
1390 * pattern Pattern the word must match
1393 * TRUE if a space should be placed in the buffer before the next
1397 * The word may be copied to the buffer.
1399 *-----------------------------------------------------------------------
1402 VarNoMatch(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1403 char *word
, Boolean addSpace
, Buffer
*buf
,
1406 if (!Str_Match(word
, (char *)pattern
)) {
1407 if (addSpace
&& vpstate
->varSpace
) {
1408 Buf_AddByte(buf
, vpstate
->varSpace
);
1411 Buf_AddBytes(buf
, strlen(word
), word
);
1418 *-----------------------------------------------------------------------
1420 * Perform a string-substitution on the given word, placing the
1421 * result in the passed buffer.
1424 * word Word to modify
1425 * addSpace True if space should be added before
1427 * buf Buffer for result
1428 * patternp Pattern for substitution
1431 * TRUE if a space is needed before more characters are added.
1436 *-----------------------------------------------------------------------
1439 VarSubstitute(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1440 char *word
, Boolean addSpace
, Buffer
*buf
,
1443 int wordLen
; /* Length of word */
1444 char *cp
; /* General pointer */
1445 VarPattern
*pattern
= (VarPattern
*)patternp
;
1447 wordLen
= strlen(word
);
1448 if ((pattern
->flags
& (VAR_SUB_ONE
|VAR_SUB_MATCHED
)) !=
1449 (VAR_SUB_ONE
|VAR_SUB_MATCHED
)) {
1451 * Still substituting -- break it down into simple anchored cases
1452 * and if none of them fits, perform the general substitution case.
1454 if ((pattern
->flags
& VAR_MATCH_START
) &&
1455 (strncmp(word
, pattern
->lhs
, pattern
->leftLen
) == 0)) {
1457 * Anchored at start and beginning of word matches pattern
1459 if ((pattern
->flags
& VAR_MATCH_END
) &&
1460 (wordLen
== pattern
->leftLen
)) {
1462 * Also anchored at end and matches to the end (word
1463 * is same length as pattern) add space and rhs only
1464 * if rhs is non-null.
1466 if (pattern
->rightLen
!= 0) {
1467 if (addSpace
&& vpstate
->varSpace
) {
1468 Buf_AddByte(buf
, vpstate
->varSpace
);
1471 Buf_AddBytes(buf
, pattern
->rightLen
, pattern
->rhs
);
1473 pattern
->flags
|= VAR_SUB_MATCHED
;
1474 } else if (pattern
->flags
& VAR_MATCH_END
) {
1476 * Doesn't match to end -- copy word wholesale
1481 * Matches at start but need to copy in trailing characters
1483 if ((pattern
->rightLen
+ wordLen
- pattern
->leftLen
) != 0){
1484 if (addSpace
&& vpstate
->varSpace
) {
1485 Buf_AddByte(buf
, vpstate
->varSpace
);
1489 Buf_AddBytes(buf
, pattern
->rightLen
, pattern
->rhs
);
1490 Buf_AddBytes(buf
, wordLen
- pattern
->leftLen
,
1491 (word
+ pattern
->leftLen
));
1492 pattern
->flags
|= VAR_SUB_MATCHED
;
1494 } else if (pattern
->flags
& VAR_MATCH_START
) {
1496 * Had to match at start of word and didn't -- copy whole word.
1499 } else if (pattern
->flags
& VAR_MATCH_END
) {
1501 * Anchored at end, Find only place match could occur (leftLen
1502 * characters from the end of the word) and see if it does. Note
1503 * that because the $ will be left at the end of the lhs, we have
1506 cp
= word
+ (wordLen
- pattern
->leftLen
);
1508 (strncmp(cp
, pattern
->lhs
, pattern
->leftLen
) == 0)) {
1510 * Match found. If we will place characters in the buffer,
1511 * add a space before hand as indicated by addSpace, then
1512 * stuff in the initial, unmatched part of the word followed
1513 * by the right-hand-side.
1515 if (((cp
- word
) + pattern
->rightLen
) != 0) {
1516 if (addSpace
&& vpstate
->varSpace
) {
1517 Buf_AddByte(buf
, vpstate
->varSpace
);
1521 Buf_AddBytes(buf
, cp
- word
, word
);
1522 Buf_AddBytes(buf
, pattern
->rightLen
, pattern
->rhs
);
1523 pattern
->flags
|= VAR_SUB_MATCHED
;
1526 * Had to match at end and didn't. Copy entire word.
1532 * Pattern is unanchored: search for the pattern in the word using
1533 * String_FindSubstring, copying unmatched portions and the
1534 * right-hand-side for each match found, handling non-global
1535 * substitutions correctly, etc. When the loop is done, any
1536 * remaining part of the word (word and wordLen are adjusted
1537 * accordingly through the loop) is copied straight into the
1539 * addSpace is set FALSE as soon as a space is added to the
1546 origSize
= Buf_Size(buf
);
1548 cp
= Str_FindSubstring(word
, pattern
->lhs
);
1550 if (addSpace
&& (((cp
- word
) + pattern
->rightLen
) != 0)){
1551 Buf_AddByte(buf
, vpstate
->varSpace
);
1554 Buf_AddBytes(buf
, cp
-word
, word
);
1555 Buf_AddBytes(buf
, pattern
->rightLen
, pattern
->rhs
);
1556 wordLen
-= (cp
- word
) + pattern
->leftLen
;
1557 word
= cp
+ pattern
->leftLen
;
1561 if ((pattern
->flags
& VAR_SUB_GLOBAL
) == 0) {
1564 pattern
->flags
|= VAR_SUB_MATCHED
;
1570 if (addSpace
&& vpstate
->varSpace
) {
1571 Buf_AddByte(buf
, vpstate
->varSpace
);
1573 Buf_AddBytes(buf
, wordLen
, word
);
1576 * If added characters to the buffer, need to add a space
1577 * before we add any more. If we didn't add any, just return
1578 * the previous value of addSpace.
1580 return ((Buf_Size(buf
) != origSize
) || addSpace
);
1585 if (addSpace
&& vpstate
->varSpace
) {
1586 Buf_AddByte(buf
, vpstate
->varSpace
);
1588 Buf_AddBytes(buf
, wordLen
, word
);
1594 *-----------------------------------------------------------------------
1596 * Print the error caused by a regcomp or regexec call.
1602 * An error gets printed.
1604 *-----------------------------------------------------------------------
1607 VarREError(int errnum
, regex_t
*pat
, const char *str
)
1612 errlen
= regerror(errnum
, pat
, 0, 0);
1613 errbuf
= bmake_malloc(errlen
);
1614 regerror(errnum
, pat
, errbuf
, errlen
);
1615 Error("%s: %s", str
, errbuf
);
1621 *-----------------------------------------------------------------------
1622 * VarRESubstitute --
1623 * Perform a regex substitution on the given word, placing the
1624 * result in the passed buffer.
1627 * TRUE if a space is needed before more characters are added.
1632 *-----------------------------------------------------------------------
1635 VarRESubstitute(GNode
*ctx __unused
, Var_Parse_State
*vpstate __unused
,
1636 char *word
, Boolean addSpace
, Buffer
*buf
,
1646 #define MAYBE_ADD_SPACE() \
1647 if (addSpace && !added) \
1648 Buf_AddByte(buf, ' '); \
1655 if ((pat
->flags
& (VAR_SUB_ONE
|VAR_SUB_MATCHED
)) ==
1656 (VAR_SUB_ONE
|VAR_SUB_MATCHED
))
1660 xrv
= regexec(&pat
->re
, wp
, pat
->nsub
, pat
->matches
, flags
);
1665 pat
->flags
|= VAR_SUB_MATCHED
;
1666 if (pat
->matches
[0].rm_so
> 0) {
1668 Buf_AddBytes(buf
, pat
->matches
[0].rm_so
, wp
);
1671 for (rp
= pat
->replace
; *rp
; rp
++) {
1672 if ((*rp
== '\\') && ((rp
[1] == '&') || (rp
[1] == '\\'))) {
1674 Buf_AddByte(buf
,rp
[1]);
1677 else if ((*rp
== '&') ||
1678 ((*rp
== '\\') && isdigit((unsigned char)rp
[1]))) {
1696 if (n
> pat
->nsub
) {
1697 Error("No subexpression %s", &errstr
[0]);
1700 } else if ((pat
->matches
[n
].rm_so
== -1) &&
1701 (pat
->matches
[n
].rm_eo
== -1)) {
1702 Error("No match for subexpression %s", &errstr
[0]);
1706 subbuf
= wp
+ pat
->matches
[n
].rm_so
;
1707 sublen
= pat
->matches
[n
].rm_eo
- pat
->matches
[n
].rm_so
;
1712 Buf_AddBytes(buf
, sublen
, subbuf
);
1716 Buf_AddByte(buf
, *rp
);
1719 wp
+= pat
->matches
[0].rm_eo
;
1720 if (pat
->flags
& VAR_SUB_GLOBAL
) {
1721 flags
|= REG_NOTBOL
;
1722 if (pat
->matches
[0].rm_so
== 0 && pat
->matches
[0].rm_eo
== 0) {
1724 Buf_AddByte(buf
, *wp
);
1733 Buf_AddBytes(buf
, strlen(wp
), wp
);
1737 VarREError(xrv
, &pat
->re
, "Unexpected regex error");
1742 Buf_AddBytes(buf
,strlen(wp
),wp
);
1746 return(addSpace
||added
);
1753 *-----------------------------------------------------------------------
1755 * Implements the :@<temp>@<string>@ modifier of ODE make.
1756 * We set the temp variable named in pattern.lhs to word and expand
1757 * pattern.rhs storing the result in the passed buffer.
1760 * word Word to modify
1761 * addSpace True if space should be added before
1763 * buf Buffer for result
1764 * pattern Datafor substitution
1767 * TRUE if a space is needed before more characters are added.
1772 *-----------------------------------------------------------------------
1775 VarLoopExpand(GNode
*ctx __unused
, Var_Parse_State
*vpstate __unused
,
1776 char *word
, Boolean addSpace
, Buffer
*buf
,
1779 VarLoop_t
*loop
= (VarLoop_t
*)loopp
;
1783 if (word
&& *word
) {
1784 Var_Set(loop
->tvar
, word
, loop
->ctxt
, VAR_NO_EXPORT
);
1785 s
= Var_Subst(NULL
, loop
->str
, loop
->ctxt
, loop
->errnum
);
1786 if (s
!= NULL
&& *s
!= '\0') {
1787 if (addSpace
&& *s
!= '\n')
1788 Buf_AddByte(buf
, ' ');
1789 Buf_AddBytes(buf
, (slen
= strlen(s
)), s
);
1790 addSpace
= (slen
> 0 && s
[slen
- 1] != '\n');
1799 *-----------------------------------------------------------------------
1801 * Implements the :[start..end] modifier.
1802 * This is a special case of VarModify since we want to be able
1803 * to scan the list backwards if start > end.
1806 * str String whose words should be trimmed
1807 * seldata words to select
1810 * A string of all the words selected.
1815 *-----------------------------------------------------------------------
1818 VarSelectWords(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1819 const char *str
, VarSelectWords_t
*seldata
)
1821 Buffer buf
; /* Buffer for the new string */
1822 Boolean addSpace
; /* TRUE if need to add a space to the
1823 * buffer before adding the trimmed
1825 char **av
; /* word list */
1826 char *as
; /* word list memory */
1828 int start
, end
, step
;
1833 if (vpstate
->oneBigWord
) {
1834 /* fake what brk_string() would do if there were only one word */
1836 av
= bmake_malloc((ac
+ 1) * sizeof(char *));
1837 as
= bmake_strdup(str
);
1841 av
= brk_string(str
, &ac
, FALSE
, &as
);
1845 * Now sanitize seldata.
1846 * If seldata->start or seldata->end are negative, convert them to
1847 * the positive equivalents (-1 gets converted to argc, -2 gets
1848 * converted to (argc-1), etc.).
1850 if (seldata
->start
< 0)
1851 seldata
->start
= ac
+ seldata
->start
+ 1;
1852 if (seldata
->end
< 0)
1853 seldata
->end
= ac
+ seldata
->end
+ 1;
1856 * We avoid scanning more of the list than we need to.
1858 if (seldata
->start
> seldata
->end
) {
1859 start
= MIN(ac
, seldata
->start
) - 1;
1860 end
= MAX(0, seldata
->end
- 1);
1863 start
= MAX(0, seldata
->start
- 1);
1864 end
= MIN(ac
, seldata
->end
);
1869 (step
< 0 && i
>= end
) || (step
> 0 && i
< end
);
1871 if (av
[i
] && *av
[i
]) {
1872 if (addSpace
&& vpstate
->varSpace
) {
1873 Buf_AddByte(&buf
, vpstate
->varSpace
);
1875 Buf_AddBytes(&buf
, strlen(av
[i
]), av
[i
]);
1883 return Buf_Destroy(&buf
, FALSE
);
1889 * Replace each word with the result of realpath()
1893 VarRealpath(GNode
*ctx __unused
, Var_Parse_State
*vpstate
,
1894 char *word
, Boolean addSpace
, Buffer
*buf
,
1895 void *patternp __unused
)
1898 char rbuf
[MAXPATHLEN
];
1901 if (addSpace
&& vpstate
->varSpace
) {
1902 Buf_AddByte(buf
, vpstate
->varSpace
);
1905 rp
= realpath(word
, rbuf
);
1906 if (rp
&& *rp
== '/' && stat(rp
, &st
) == 0)
1909 Buf_AddBytes(buf
, strlen(word
), word
);
1914 *-----------------------------------------------------------------------
1916 * Modify each of the words of the passed string using the given
1917 * function. Used to implement all modifiers.
1920 * str String whose words should be trimmed
1921 * modProc Function to use to modify them
1922 * datum Datum to pass it
1925 * A string of all the words modified appropriately.
1930 *-----------------------------------------------------------------------
1933 VarModify(GNode
*ctx
, Var_Parse_State
*vpstate
,
1935 Boolean (*modProc
)(GNode
*, Var_Parse_State
*, char *,
1936 Boolean
, Buffer
*, void *),
1939 Buffer buf
; /* Buffer for the new string */
1940 Boolean addSpace
; /* TRUE if need to add a space to the
1941 * buffer before adding the trimmed
1943 char **av
; /* word list */
1944 char *as
; /* word list memory */
1950 if (vpstate
->oneBigWord
) {
1951 /* fake what brk_string() would do if there were only one word */
1953 av
= bmake_malloc((ac
+ 1) * sizeof(char *));
1954 as
= bmake_strdup(str
);
1958 av
= brk_string(str
, &ac
, FALSE
, &as
);
1961 for (i
= 0; i
< ac
; i
++) {
1962 addSpace
= (*modProc
)(ctx
, vpstate
, av
[i
], addSpace
, &buf
, datum
);
1968 return Buf_Destroy(&buf
, FALSE
);
1973 VarWordCompare(const void *a
, const void *b
)
1975 int r
= strcmp(*(const char * const *)a
, *(const char * const *)b
);
1980 *-----------------------------------------------------------------------
1982 * Order the words in the string.
1985 * str String whose words should be sorted.
1986 * otype How to order: s - sort, x - random.
1989 * A string containing the words ordered.
1994 *-----------------------------------------------------------------------
1997 VarOrder(const char *str
, const char otype
)
1999 Buffer buf
; /* Buffer for the new string */
2000 char **av
; /* word list [first word does not count] */
2001 char *as
; /* word list memory */
2006 av
= brk_string(str
, &ac
, FALSE
, &as
);
2010 case 's': /* sort alphabetically */
2011 qsort(av
, ac
, sizeof(char *), VarWordCompare
);
2013 case 'x': /* randomize */
2019 * We will use [ac..2] range for mod factors. This will produce
2020 * random numbers in [(ac-1)..0] interval, and minimal
2021 * reasonable value for mod factor is 2 (the mod 1 will produce
2022 * 0 with probability 1).
2024 for (i
= ac
-1; i
> 0; i
--) {
2025 rndidx
= random() % (i
+ 1);
2033 } /* end of switch */
2035 for (i
= 0; i
< ac
; i
++) {
2036 Buf_AddBytes(&buf
, strlen(av
[i
]), av
[i
]);
2038 Buf_AddByte(&buf
, ' ');
2044 return Buf_Destroy(&buf
, FALSE
);
2049 *-----------------------------------------------------------------------
2051 * Remove adjacent duplicate words.
2054 * str String whose words should be sorted
2057 * A string containing the resulting words.
2062 *-----------------------------------------------------------------------
2065 VarUniq(const char *str
)
2067 Buffer buf
; /* Buffer for new string */
2068 char **av
; /* List of words to affect */
2069 char *as
; /* Word list memory */
2073 av
= brk_string(str
, &ac
, FALSE
, &as
);
2076 for (j
= 0, i
= 1; i
< ac
; i
++)
2077 if (strcmp(av
[i
], av
[j
]) != 0 && (++j
!= i
))
2082 for (i
= 0; i
< ac
; i
++) {
2083 Buf_AddBytes(&buf
, strlen(av
[i
]), av
[i
]);
2085 Buf_AddByte(&buf
, ' ');
2091 return Buf_Destroy(&buf
, FALSE
);
2096 *-----------------------------------------------------------------------
2098 * Pass through the tstr looking for 1) escaped delimiters,
2099 * '$'s and backslashes (place the escaped character in
2100 * uninterpreted) and 2) unescaped $'s that aren't before
2101 * the delimiter (expand the variable substitution unless flags
2102 * has VAR_NOSUBST set).
2103 * Return the expanded string or NULL if the delimiter was missing
2104 * If pattern is specified, handle escaped ampersands, and replace
2105 * unescaped ampersands with the lhs of the pattern.
2108 * A string of all the words modified appropriately.
2109 * If length is specified, return the string length of the buffer
2110 * If flags is specified and the last character of the pattern is a
2111 * $ set the VAR_MATCH_END bit of flags.
2115 *-----------------------------------------------------------------------
2118 VarGetPattern(GNode
*ctxt
, Var_Parse_State
*vpstate __unused
,
2119 int errnum
, const char **tstr
, int delim
, int *flags
,
2120 int *length
, VarPattern
*pattern
)
2131 #define IS_A_MATCH(cp, delim) \
2132 ((cp[0] == '\\') && ((cp[1] == delim) || \
2133 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
2136 * Skim through until the matching delimiter is found;
2137 * pick up variable substitutions on the way. Also allow
2138 * backslashes to quote the delimiter, $, and \, but don't
2139 * touch other backslashes.
2141 for (cp
= *tstr
; *cp
&& (*cp
!= delim
); cp
++) {
2142 if (IS_A_MATCH(cp
, delim
)) {
2143 Buf_AddByte(&buf
, cp
[1]);
2145 } else if (*cp
== '$') {
2146 if (cp
[1] == delim
) {
2148 Buf_AddByte(&buf
, *cp
);
2151 * Unescaped $ at end of pattern => anchor
2154 *flags
|= VAR_MATCH_END
;
2156 if (flags
== NULL
|| (*flags
& VAR_NOSUBST
) == 0) {
2162 * If unescaped dollar sign not before the
2163 * delimiter, assume it's a variable
2164 * substitution and recurse.
2166 cp2
= Var_Parse(cp
, ctxt
, errnum
, &len
, &freeIt
);
2167 Buf_AddBytes(&buf
, strlen(cp2
), cp2
);
2172 const char *cp2
= &cp
[1];
2174 if (*cp2
== PROPEN
|| *cp2
== BROPEN
) {
2176 * Find the end of this variable reference
2177 * and suck it in without further ado.
2178 * It will be interperated later.
2181 int want
= (*cp2
== PROPEN
) ? PRCLOSE
: BRCLOSE
;
2184 for (++cp2
; *cp2
!= '\0' && depth
> 0; ++cp2
) {
2185 if (cp2
[-1] != '\\') {
2192 Buf_AddBytes(&buf
, cp2
- cp
, cp
);
2195 Buf_AddByte(&buf
, *cp
);
2199 else if (pattern
&& *cp
== '&')
2200 Buf_AddBytes(&buf
, pattern
->leftLen
, pattern
->lhs
);
2202 Buf_AddByte(&buf
, *cp
);
2212 *length
= Buf_Size(&buf
);
2213 rstr
= Buf_Destroy(&buf
, FALSE
);
2215 fprintf(debug_file
, "Modifier pattern: \"%s\"\n", rstr
);
2220 *-----------------------------------------------------------------------
2222 * Quote shell meta-characters in the string
2230 *-----------------------------------------------------------------------
2237 /* This should cover most shells :-( */
2238 static const char meta
[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
2239 const char *newline
;
2242 if ((newline
= Shell_GetNewline()) == NULL
)
2244 nlen
= strlen(newline
);
2247 while (*str
!= '\0') {
2248 if ((len
= strcspn(str
, meta
)) != 0) {
2249 Buf_AddBytes(&buf
, len
, str
);
2251 } else if (*str
== '\n') {
2252 Buf_AddBytes(&buf
, nlen
, newline
);
2255 Buf_AddByte(&buf
, '\\');
2256 Buf_AddByte(&buf
, *str
);
2260 str
= Buf_Destroy(&buf
, FALSE
);
2262 fprintf(debug_file
, "QuoteMeta: [%s]\n", str
);
2267 *-----------------------------------------------------------------------
2269 * Hash the string using the MurmurHash3 algorithm.
2270 * Output is computed using 32bit Little Endian arithmetic.
2273 * str String to modify
2276 * Hash value of str, encoded as 8 hex digits.
2281 *-----------------------------------------------------------------------
2286 static const char hexdigits
[16] = "0123456789abcdef";
2289 unsigned char *ustr
= (unsigned char *)str
;
2290 uint32_t h
, k
, c1
, c2
;
2299 for (len
= len2
; len
; ) {
2303 k
= (ustr
[3] << 24) | (ustr
[2] << 16) | (ustr
[1] << 8) | ustr
[0];
2308 k
|= (ustr
[2] << 16);
2310 k
|= (ustr
[1] << 8);
2315 c1
= c1
* 5 + 0x7b7d159cU
;
2316 c2
= c2
* 5 + 0x6bce6396U
;
2318 k
= (k
<< 11) ^ (k
>> 21);
2320 h
= (h
<< 13) ^ (h
>> 19);
2321 h
= h
* 5 + 0x52dce729U
;
2331 for (len
= 0; len
< 8; ++len
) {
2332 Buf_AddByte(&buf
, hexdigits
[h
& 15]);
2336 return Buf_Destroy(&buf
, FALSE
);
2340 *-----------------------------------------------------------------------
2342 * Change the string to all uppercase or all lowercase
2345 * str String to modify
2346 * upper TRUE -> uppercase, else lowercase
2349 * The string with case changed
2354 *-----------------------------------------------------------------------
2357 VarChangeCase(char *str
, int upper
)
2360 int (*modProc
)(int);
2362 modProc
= (upper
? toupper
: tolower
);
2364 for (; *str
; str
++) {
2365 Buf_AddByte(&buf
, modProc(*str
));
2367 return Buf_Destroy(&buf
, FALSE
);
2371 VarStrftime(const char *fmt
, int zulu
)
2379 strftime(buf
, sizeof(buf
), fmt
, zulu
? gmtime(&utc
) : localtime(&utc
));
2381 buf
[sizeof(buf
) - 1] = '\0';
2382 return bmake_strdup(buf
);
2386 * Now we need to apply any modifiers the user wants applied.
2388 * :M<pattern> words which match the given <pattern>.
2389 * <pattern> is of the standard file
2391 * :N<pattern> words which do not match the given <pattern>.
2392 * :S<d><pat1><d><pat2><d>[1gW]
2393 * Substitute <pat2> for <pat1> in the value
2394 * :C<d><pat1><d><pat2><d>[1gW]
2395 * Substitute <pat2> for regex <pat1> in the value
2396 * :H Substitute the head of each word
2397 * :T Substitute the tail of each word
2398 * :E Substitute the extension (minus '.') of
2400 * :R Substitute the root of each word
2401 * (pathname minus the suffix).
2402 * :O ("Order") Alphabeticaly sort words in variable.
2403 * :Ox ("intermiX") Randomize words in variable.
2404 * :u ("uniq") Remove adjacent duplicate words.
2405 * :tu Converts the variable contents to uppercase.
2406 * :tl Converts the variable contents to lowercase.
2407 * :ts[c] Sets varSpace - the char used to
2408 * separate words to 'c'. If 'c' is
2409 * omitted then no separation is used.
2410 * :tW Treat the variable contents as a single
2411 * word, even if it contains spaces.
2412 * (Mnemonic: one big 'W'ord.)
2413 * :tw Treat the variable contents as multiple
2414 * space-separated words.
2415 * (Mnemonic: many small 'w'ords.)
2416 * :[index] Select a single word from the value.
2417 * :[start..end] Select multiple words from the value.
2418 * :[*] or :[0] Select the entire value, as a single
2419 * word. Equivalent to :tW.
2420 * :[@] Select the entire value, as multiple
2421 * words. Undoes the effect of :[*].
2422 * Equivalent to :tw.
2423 * :[#] Returns the number of words in the value.
2425 * :?<true-value>:<false-value>
2426 * If the variable evaluates to true, return
2427 * true value, else return the second value.
2428 * :lhs=rhs Like :S, but the rhs goes to the end of
2430 * :sh Treat the current value as a command
2431 * to be run, new value is its output.
2432 * The following added so we can handle ODE makefiles.
2433 * :@<tmpvar>@<newval>@
2434 * Assign a temporary local variable <tmpvar>
2435 * to the current value of each word in turn
2436 * and replace each word with the result of
2437 * evaluating <newval>
2438 * :D<newval> Use <newval> as value if variable defined
2439 * :U<newval> Use <newval> as value if variable undefined
2440 * :L Use the name of the variable as the value.
2441 * :P Use the path of the node that has the same
2442 * name as the variable as the value. This
2443 * basically includes an implied :L so that
2444 * the common method of refering to the path
2445 * of your dependent 'x' in a rule is to use
2446 * the form '${x:P}'.
2447 * :!<cmd>! Run cmd much the same as :sh run's the
2448 * current value of the variable.
2449 * The ::= modifiers, actually assign a value to the variable.
2450 * Their main purpose is in supporting modifiers of .for loop
2451 * iterators and other obscure uses. They always expand to
2452 * nothing. In a target rule that would otherwise expand to an
2453 * empty line they can be preceded with @: to keep make happy.
2457 * .for i in ${.TARGET} ${.TARGET:R}.gz
2462 * ::=<str> Assigns <str> as the new value of variable.
2463 * ::?=<str> Assigns <str> as value of variable if
2464 * it was not already set.
2465 * ::+=<str> Appends <str> to variable.
2466 * ::!=<cmd> Assigns output of <cmd> as the new value of
2470 /* we now have some modifiers with long names */
2471 #define STRMOD_MATCH(s, want, n) \
2472 (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':'))
2475 ApplyModifiers(char *nstr
, const char *tstr
,
2476 int startc
, int endc
,
2477 Var
*v
, GNode
*ctxt
, Boolean errnum
,
2478 int *lengthPtr
, void **freePtr
)
2481 const char *cp
; /* Secondary pointer into str (place marker
2483 char *newStr
; /* New value to return */
2484 char termc
; /* Character which terminated scan */
2485 int cnt
; /* Used to count brace pairs when variable in
2486 * in parens or braces */
2488 int modifier
; /* that we are processing */
2489 Var_Parse_State parsestate
; /* Flags passed to helper functions */
2492 parsestate
.oneBigWord
= FALSE
;
2493 parsestate
.varSpace
= ' '; /* word separator */
2497 while (*tstr
&& *tstr
!= endc
) {
2501 * We may have some complex modifiers in a variable.
2508 rval
= Var_Parse(tstr
, ctxt
, errnum
, &rlen
, &freeIt
);
2511 * If we have not parsed up to endc or ':',
2512 * we are not interested.
2514 if (rval
!= NULL
&& *rval
&&
2515 (c
= tstr
[rlen
]) != '\0' &&
2524 fprintf(debug_file
, "Got '%s' from '%.*s'%.*s\n",
2525 rval
, rlen
, tstr
, rlen
, tstr
+ rlen
);
2530 if (rval
!= NULL
&& *rval
) {
2533 nstr
= ApplyModifiers(nstr
, rval
,
2535 v
, ctxt
, errnum
, &used
, freePtr
);
2536 if (nstr
== var_Error
2537 || (nstr
== varNoError
&& errnum
== 0)
2538 || strlen(rval
) != (size_t) used
) {
2541 goto out
; /* error already reported */
2548 else if (!*tstr
&& endc
) {
2549 Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc
, v
->name
);
2556 fprintf(debug_file
, "Applying :%c to \"%s\"\n", *tstr
, nstr
);
2559 switch ((modifier
= *tstr
)) {
2562 if (tstr
[1] == '=' ||
2564 (tstr
[1] == '!' || tstr
[1] == '+' || tstr
[1] == '?'))) {
2566 * "::=", "::!=", "::+=", or "::?="
2568 GNode
*v_ctxt
; /* context where v belongs */
2574 if (v
->name
[0] == 0)
2580 if (v
->flags
& VAR_JUNK
) {
2582 * We need to bmake_strdup() it incase
2583 * VarGetPattern() recurses.
2586 v
->name
= bmake_strdup(v
->name
);
2587 } else if (ctxt
!= VAR_GLOBAL
) {
2588 Var
*gv
= VarFind(v
->name
, ctxt
, 0);
2590 v_ctxt
= VAR_GLOBAL
;
2592 VarFreeEnv(gv
, TRUE
);
2595 switch ((how
= *tstr
)) {
2605 delim
= startc
== PROPEN
? PRCLOSE
: BRCLOSE
;
2608 pattern
.rhs
= VarGetPattern(ctxt
, &parsestate
, errnum
,
2612 if (v
->flags
& VAR_JUNK
) {
2613 /* restore original name */
2617 if (pattern
.rhs
== NULL
)
2625 Var_Append(v
->name
, pattern
.rhs
, v_ctxt
);
2628 newStr
= Cmd_Exec(pattern
.rhs
, &emsg
);
2632 Var_Set(v
->name
, newStr
, v_ctxt
, 0);
2637 if ((v
->flags
& VAR_JUNK
) == 0)
2641 Var_Set(v
->name
, pattern
.rhs
, v_ctxt
, 0);
2644 free(UNCONST(pattern
.rhs
));
2648 goto default_case
; /* "::<unrecognised>" */
2653 int flags
= VAR_NOSUBST
;
2657 if ((loop
.tvar
= VarGetPattern(ctxt
, &parsestate
, errnum
,
2659 &flags
, &loop
.tvarLen
,
2663 if ((loop
.str
= VarGetPattern(ctxt
, &parsestate
, errnum
,
2665 &flags
, &loop
.strLen
,
2672 loop
.errnum
= errnum
;
2674 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarLoopExpand
,
2683 Buffer buf
; /* Buffer for patterns */
2684 int wantit
; /* want data in buffer */
2687 * Pass through tstr looking for 1) escaped delimiters,
2688 * '$'s and backslashes (place the escaped character in
2689 * uninterpreted) and 2) unescaped $'s that aren't before
2690 * the delimiter (expand the variable substitution).
2691 * The result is left in the Buffer buf.
2695 *cp
!= endc
&& *cp
!= ':' && *cp
!= '\0';
2697 if ((*cp
== '\\') &&
2703 Buf_AddByte(&buf
, cp
[1]);
2705 } else if (*cp
== '$') {
2707 * If unescaped dollar sign, assume it's a
2708 * variable substitution and recurse.
2714 cp2
= Var_Parse(cp
, ctxt
, errnum
, &len
, &freeIt
);
2715 Buf_AddBytes(&buf
, strlen(cp2
), cp2
);
2720 Buf_AddByte(&buf
, *cp
);
2727 wantit
= ((v
->flags
& VAR_JUNK
) != 0);
2729 wantit
= ((v
->flags
& VAR_JUNK
) == 0);
2730 if ((v
->flags
& VAR_JUNK
) != 0)
2731 v
->flags
|= VAR_KEEP
;
2733 newStr
= Buf_Destroy(&buf
, FALSE
);
2736 Buf_Destroy(&buf
, TRUE
);
2742 if ((v
->flags
& VAR_JUNK
) != 0)
2743 v
->flags
|= VAR_KEEP
;
2744 newStr
= bmake_strdup(v
->name
);
2753 if ((v
->flags
& VAR_JUNK
) != 0)
2754 v
->flags
|= VAR_KEEP
;
2755 gn
= Targ_FindNode(v
->name
, TARG_NOCREATE
);
2756 if (gn
== NULL
|| gn
->type
& OP_NOPATH
) {
2758 } else if (gn
->path
) {
2759 newStr
= bmake_strdup(gn
->path
);
2761 newStr
= Dir_FindFile(v
->name
, Suff_FindPath(gn
));
2764 newStr
= bmake_strdup(v
->name
);
2779 if ((pattern
.rhs
= VarGetPattern(ctxt
, &parsestate
, errnum
,
2781 NULL
, &pattern
.rightLen
,
2784 newStr
= Cmd_Exec(pattern
.rhs
, &emsg
);
2785 free(UNCONST(pattern
.rhs
));
2790 if (v
->flags
& VAR_JUNK
) {
2791 v
->flags
|= VAR_KEEP
;
2798 * Look for the closing ']', recursively
2799 * expanding any embedded variables.
2801 * estr is a pointer to the expanded result,
2802 * which we must free().
2806 cp
= tstr
+1; /* point to char after '[' */
2807 delim
= ']'; /* look for closing ']' */
2808 estr
= VarGetPattern(ctxt
, &parsestate
,
2812 goto cleanup
; /* report missing ']' */
2813 /* now cp points just after the closing ']' */
2815 if (cp
[0] != ':' && cp
[0] != endc
) {
2816 /* Found junk after ']' */
2820 if (estr
[0] == '\0') {
2821 /* Found empty square brackets in ":[]". */
2824 } else if (estr
[0] == '#' && estr
[1] == '\0') {
2828 * We will need enough space for the decimal
2829 * representation of an int. We calculate the
2830 * space needed for the octal representation,
2831 * and add enough slop to cope with a '-' sign
2832 * (which should never be needed) and a '\0'
2833 * string terminator.
2836 (sizeof(int) * CHAR_BIT
+ 2) / 3 + 2;
2838 newStr
= bmake_malloc(newStrSize
);
2839 if (parsestate
.oneBigWord
) {
2840 strncpy(newStr
, "1", newStrSize
);
2842 /* XXX: brk_string() is a rather expensive
2843 * way of counting words. */
2848 av
= brk_string(nstr
, &ac
, FALSE
, &as
);
2849 snprintf(newStr
, newStrSize
, "%d", ac
);
2856 } else if (estr
[0] == '*' && estr
[1] == '\0') {
2858 parsestate
.oneBigWord
= TRUE
;
2863 } else if (estr
[0] == '@' && estr
[1] == '\0') {
2865 parsestate
.oneBigWord
= FALSE
;
2872 * We expect estr to contain a single
2873 * integer for :[N], or two integers
2874 * separated by ".." for :[start..end].
2878 VarSelectWords_t seldata
= { 0, 0 };
2880 seldata
.start
= strtol(estr
, &ep
, 0);
2882 /* Found junk instead of a number */
2885 } else if (ep
[0] == '\0') {
2886 /* Found only one integer in :[N] */
2887 seldata
.end
= seldata
.start
;
2888 } else if (ep
[0] == '.' && ep
[1] == '.' &&
2890 /* Expecting another integer after ".." */
2892 seldata
.end
= strtol(ep
, &ep
, 0);
2893 if (ep
[0] != '\0') {
2894 /* Found junk after ".." */
2899 /* Found junk instead of ".." */
2904 * Now seldata is properly filled in,
2905 * but we still have to check for 0 as
2908 if (seldata
.start
== 0 && seldata
.end
== 0) {
2909 /* ":[0]" or perhaps ":[0..0]" */
2910 parsestate
.oneBigWord
= TRUE
;
2915 } else if (seldata
.start
== 0 ||
2917 /* ":[0..N]" or ":[N..0]" */
2922 * Normal case: select the words
2923 * described by seldata.
2925 newStr
= VarSelectWords(ctxt
, &parsestate
,
2935 cp
= tstr
+ 1; /* make sure it is set */
2936 if (STRMOD_MATCH(tstr
, "gmtime", 6)) {
2937 newStr
= VarStrftime(nstr
, 1);
2945 cp
= tstr
+ 1; /* make sure it is set */
2946 if (STRMOD_MATCH(tstr
, "hash", 4)) {
2947 newStr
= VarHash(nstr
);
2955 cp
= tstr
+ 1; /* make sure it is set */
2956 if (STRMOD_MATCH(tstr
, "localtime", 9)) {
2957 newStr
= VarStrftime(nstr
, 0);
2966 cp
= tstr
+ 1; /* make sure it is set */
2967 if (tstr
[1] != endc
&& tstr
[1] != ':') {
2968 if (tstr
[1] == 's') {
2970 * Use the char (if any) at tstr[2]
2971 * as the word separator.
2975 if (tstr
[2] != endc
&&
2976 (tstr
[3] == endc
|| tstr
[3] == ':')) {
2977 /* ":ts<unrecognised><endc>" or
2978 * ":ts<unrecognised>:" */
2979 parsestate
.varSpace
= tstr
[2];
2981 } else if (tstr
[2] == endc
|| tstr
[2] == ':') {
2982 /* ":ts<endc>" or ":ts:" */
2983 parsestate
.varSpace
= 0; /* no separator */
2985 } else if (tstr
[2] == '\\') {
2988 parsestate
.varSpace
= '\n';
2992 parsestate
.varSpace
= '\t';
2996 if (isdigit((unsigned char)tstr
[3])) {
2999 parsestate
.varSpace
=
3000 strtoul(&tstr
[3], &ep
, 0);
3001 if (*ep
!= ':' && *ep
!= endc
)
3006 * ":ts<backslash><unrecognised>".
3014 * Found ":ts<unrecognised><unrecognised>".
3022 * We cannot be certain that VarModify
3023 * will be used - even if there is a
3024 * subsequent modifier, so do a no-op
3025 * VarSubstitute now to for str to be
3026 * re-expanded without the spaces.
3028 pattern
.flags
= VAR_SUB_ONE
;
3029 pattern
.lhs
= pattern
.rhs
= "\032";
3030 pattern
.leftLen
= pattern
.rightLen
= 1;
3032 newStr
= VarModify(ctxt
, &parsestate
, nstr
,
3035 } else if (tstr
[2] == endc
|| tstr
[2] == ':') {
3037 * Check for two-character options:
3040 if (tstr
[1] == 'A') { /* absolute path */
3041 newStr
= VarModify(ctxt
, &parsestate
, nstr
,
3045 } else if (tstr
[1] == 'u' || tstr
[1] == 'l') {
3046 newStr
= VarChangeCase(nstr
, (tstr
[1] == 'u'));
3049 } else if (tstr
[1] == 'W' || tstr
[1] == 'w') {
3050 parsestate
.oneBigWord
= (tstr
[1] == 'W');
3055 /* Found ":t<unrecognised>:" or
3056 * ":t<unrecognised><endc>". */
3061 * Found ":t<unrecognised><unrecognised>".
3067 * Found ":t<endc>" or ":t:".
3077 const char *endpat
; /* points just after end of pattern */
3079 Boolean copy
; /* pattern should be, or has been, copied */
3087 * In the loop below, ignore ':' unless we are at
3088 * (or back to) the original brace level.
3089 * XXX This will likely not work right if $() and ${}
3093 *cp
!= '\0' && !(*cp
== ':' && nest
== 1);
3098 cp
[1] == endc
|| cp
[1] == startc
)) {
3108 if (*cp
== '(' || *cp
== '{')
3110 if (*cp
== ')' || *cp
== '}') {
3120 * Need to compress the \:'s out of the pattern, so
3121 * allocate enough room to hold the uncompressed
3122 * pattern (note that cp started at tstr+1, so
3123 * cp - tstr takes the null byte into account) and
3124 * compress the pattern into the space.
3126 pattern
= bmake_malloc(cp
- tstr
);
3127 for (cp2
= pattern
, cp
= tstr
+ 1;
3131 if ((*cp
== '\\') && (cp
+1 < endpat
) &&
3132 (cp
[1] == ':' || cp
[1] == endc
)) {
3141 * Either Var_Subst or VarModify will need a
3142 * nul-terminated string soon, so construct one now.
3144 pattern
= bmake_strndup(tstr
+1, endpat
- (tstr
+ 1));
3148 * pattern contains embedded '$', so use Var_Subst to
3152 pattern
= Var_Subst(NULL
, cp2
, ctxt
, errnum
);
3156 fprintf(debug_file
, "Pattern for [%s] is [%s]\n", nstr
,
3159 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarMatch
,
3162 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarNoMatch
,
3171 Var_Parse_State tmpparsestate
;
3174 tmpparsestate
= parsestate
;
3179 * If pattern begins with '^', it is anchored to the
3180 * start of the word -- skip over it and flag pattern.
3183 pattern
.flags
|= VAR_MATCH_START
;
3188 if ((pattern
.lhs
= VarGetPattern(ctxt
, &parsestate
, errnum
,
3195 if ((pattern
.rhs
= VarGetPattern(ctxt
, &parsestate
, errnum
,
3202 * Check for global substitution. If 'g' after the final
3203 * delimiter, substitution is global and is marked that
3209 pattern
.flags
|= VAR_SUB_GLOBAL
;
3212 pattern
.flags
|= VAR_SUB_ONE
;
3215 tmpparsestate
.oneBigWord
= TRUE
;
3222 newStr
= VarModify(ctxt
, &tmpparsestate
, nstr
,
3227 * Free the two strings.
3229 free(UNCONST(pattern
.lhs
));
3230 free(UNCONST(pattern
.rhs
));
3239 /* find ':', and then substitute accordingly */
3245 if ((pattern
.lhs
= VarGetPattern(ctxt
, &parsestate
, errnum
,
3251 /* BROPEN or PROPEN */
3253 if ((pattern
.rhs
= VarGetPattern(ctxt
, &parsestate
, errnum
,
3261 if (Cond_EvalExpression(NULL
, v
->name
, &value
, 0)
3263 Error("Bad conditional expression `%s' in %s?%s:%s",
3264 v
->name
, v
->name
, pattern
.lhs
, pattern
.rhs
);
3269 newStr
= UNCONST(pattern
.lhs
);
3270 free(UNCONST(pattern
.rhs
));
3272 newStr
= UNCONST(pattern
.rhs
);
3273 free(UNCONST(pattern
.lhs
));
3275 if (v
->flags
& VAR_JUNK
) {
3276 v
->flags
|= VAR_KEEP
;
3283 VarREPattern pattern
;
3286 Var_Parse_State tmpparsestate
;
3289 tmpparsestate
= parsestate
;
3295 if ((re
= VarGetPattern(ctxt
, &parsestate
, errnum
, &cp
, delim
,
3296 NULL
, NULL
, NULL
)) == NULL
)
3299 if ((pattern
.replace
= VarGetPattern(ctxt
, &parsestate
,
3300 errnum
, &cp
, delim
, NULL
,
3301 NULL
, NULL
)) == NULL
){
3309 pattern
.flags
|= VAR_SUB_GLOBAL
;
3312 pattern
.flags
|= VAR_SUB_ONE
;
3315 tmpparsestate
.oneBigWord
= TRUE
;
3323 error
= regcomp(&pattern
.re
, re
, REG_EXTENDED
);
3326 *lengthPtr
= cp
- start
+ 1;
3327 VarREError(error
, &pattern
.re
, "RE substitution error");
3328 free(pattern
.replace
);
3332 pattern
.nsub
= pattern
.re
.re_nsub
+ 1;
3333 if (pattern
.nsub
< 1)
3335 if (pattern
.nsub
> 10)
3337 pattern
.matches
= bmake_malloc(pattern
.nsub
*
3338 sizeof(regmatch_t
));
3339 newStr
= VarModify(ctxt
, &tmpparsestate
, nstr
,
3342 regfree(&pattern
.re
);
3343 free(pattern
.replace
);
3344 free(pattern
.matches
);
3350 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3351 newStr
= VarQuote(nstr
);
3358 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3359 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarTail
,
3367 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3368 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarHead
,
3376 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3377 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarSuffix
,
3385 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3386 newStr
= VarModify(ctxt
, &parsestate
, nstr
, VarRoot
,
3397 cp
= tstr
+ 1; /* skip to the rest in any case */
3398 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3401 } else if ( (tstr
[1] == 'x') &&
3402 (tstr
[2] == endc
|| tstr
[2] == ':') ) {
3409 newStr
= VarOrder(nstr
, otype
);
3413 if (tstr
[1] == endc
|| tstr
[1] == ':') {
3414 newStr
= VarUniq(nstr
);
3422 if (tstr
[1] == 'h' && (tstr
[2] == endc
|| tstr
[2] == ':')) {
3424 newStr
= Cmd_Exec(nstr
, &emsg
);
3438 * This can either be a bogus modifier or a System-V
3439 * substitution command.
3447 * First we make a pass through the string trying
3448 * to verify it is a SYSV-make-style translation:
3449 * it must be: <string1>=<string2>)
3453 while (*cp
!= '\0' && cnt
) {
3456 /* continue looking for endc */
3458 else if (*cp
== endc
)
3460 else if (*cp
== startc
)
3465 if (*cp
== endc
&& eqFound
) {
3468 * Now we break this sucker into the lhs and
3469 * rhs. We must null terminate them of course.
3473 if ((pattern
.lhs
= VarGetPattern(ctxt
, &parsestate
,
3474 errnum
, &cp
, delim
, &pattern
.flags
,
3475 &pattern
.leftLen
, NULL
)) == NULL
)
3478 if ((pattern
.rhs
= VarGetPattern(ctxt
, &parsestate
,
3479 errnum
, &cp
, delim
, NULL
, &pattern
.rightLen
,
3484 * SYSV modifications happen through the whole
3485 * string. Note the pattern is anchored at the end.
3489 if (pattern
.leftLen
== 0 && *nstr
== '\0') {
3490 newStr
= nstr
; /* special case */
3492 newStr
= VarModify(ctxt
, &parsestate
, nstr
,
3496 free(UNCONST(pattern
.lhs
));
3497 free(UNCONST(pattern
.rhs
));
3501 Error("Unknown modifier '%c'", *tstr
);
3503 *cp
!= ':' && *cp
!= endc
&& *cp
!= '\0';
3512 fprintf(debug_file
, "Result of :%c is \"%s\"\n", modifier
, newStr
);
3515 if (newStr
!= nstr
) {
3521 if (nstr
!= var_Error
&& nstr
!= varNoError
) {
3525 if (termc
== '\0' && endc
!= '\0') {
3526 Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc
, v
->name
, nstr
, modifier
);
3527 } else if (termc
== ':') {
3533 *lengthPtr
= tstr
- start
;
3538 Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr
, ":)}"), tstr
,
3542 *lengthPtr
= cp
- start
;
3544 Error("Unclosed substitution for %s (%c missing)",
3554 *-----------------------------------------------------------------------
3556 * Given the start of a variable invocation, extract the variable
3557 * name and find its value, then modify it according to the
3561 * str The string to parse
3562 * ctxt The context for the variable
3563 * errnum TRUE if undefined variables are an error
3564 * lengthPtr OUT: The length of the specification
3565 * freePtr OUT: Non-NULL if caller should free *freePtr
3568 * The (possibly-modified) value of the variable or var_Error if the
3569 * specification is invalid. The length of the specification is
3570 * placed in *lengthPtr (for invalid specifications, this is just
3572 * If *freePtr is non-NULL then it's a pointer that the caller
3573 * should pass to free() to free memory used by the result.
3578 *-----------------------------------------------------------------------
3580 /* coverity[+alloc : arg-*4] */
3582 Var_Parse(const char *str
, GNode
*ctxt
, Boolean errnum
, int *lengthPtr
,
3585 const char *tstr
; /* Pointer into str */
3586 Var
*v
; /* Variable in invocation */
3587 Boolean haveModifier
;/* TRUE if have modifiers for the variable */
3588 char endc
; /* Ending character when variable in parens
3590 char startc
; /* Starting character when variable in parens
3592 int vlen
; /* Length of variable name */
3593 const char *start
; /* Points to original start of str */
3594 char *nstr
; /* New string, used during expansion */
3595 Boolean dynamic
; /* TRUE if the variable is local and we're
3596 * expanding it in a non-local context. This
3597 * is done to support dynamic sources. The
3598 * result is just the invocation, unaltered */
3599 Var_Parse_State parsestate
; /* Flags passed to helper functions */
3605 parsestate
.oneBigWord
= FALSE
;
3606 parsestate
.varSpace
= ' '; /* word separator */
3609 if (startc
!= PROPEN
&& startc
!= BROPEN
) {
3611 * If it's not bounded by braces of some sort, life is much simpler.
3612 * We just need to check for the first character and return the
3613 * value if it exists.
3616 /* Error out some really stupid names */
3617 if (startc
== '\0' || strchr(")}:$", startc
)) {
3624 v
= VarFind(name
, ctxt
, FIND_ENV
| FIND_GLOBAL
| FIND_CMD
);
3628 if ((ctxt
== VAR_CMD
) || (ctxt
== VAR_GLOBAL
)) {
3630 * If substituting a local variable in a non-local context,
3631 * assume it's for dynamic source stuff. We have to handle
3632 * this specially and return the longhand for the variable
3633 * with the dollar sign escaped so it makes it back to the
3634 * caller. Only four of the local variables are treated
3635 * specially as they are the only four that will be set
3636 * when dynamic sources are expanded.
3640 return UNCONST("$(.TARGET)");
3642 return UNCONST("$(.ARCHIVE)");
3644 return UNCONST("$(.PREFIX)");
3646 return UNCONST("$(.MEMBER)");
3652 return (errnum
? var_Error
: varNoError
);
3654 haveModifier
= FALSE
;
3659 Buffer buf
; /* Holds the variable name */
3661 endc
= startc
== PROPEN
? PRCLOSE
: BRCLOSE
;
3665 * Skip to the end character or a colon, whichever comes first.
3667 for (tstr
= str
+ 2;
3668 *tstr
!= '\0' && *tstr
!= endc
&& *tstr
!= ':';
3672 * A variable inside a variable, expand
3677 char *rval
= Var_Parse(tstr
, ctxt
, errnum
, &rlen
, &freeIt
);
3679 Buf_AddBytes(&buf
, strlen(rval
), rval
);
3686 Buf_AddByte(&buf
, *tstr
);
3689 haveModifier
= TRUE
;
3690 } else if (*tstr
!= '\0') {
3691 haveModifier
= FALSE
;
3694 * If we never did find the end character, return NULL
3695 * right now, setting the length to be the distance to
3696 * the end of the string, since that's what make does.
3698 *lengthPtr
= tstr
- str
;
3699 Buf_Destroy(&buf
, TRUE
);
3702 str
= Buf_GetAll(&buf
, &vlen
);
3705 * At this point, str points into newly allocated memory from
3706 * buf, containing only the name of the variable.
3708 * start and tstr point into the const string that was pointed
3709 * to by the original value of the str parameter. start points
3710 * to the '$' at the beginning of the string, while tstr points
3711 * to the char just after the end of the variable name -- this
3712 * will be '\0', ':', PRCLOSE, or BRCLOSE.
3715 v
= VarFind(str
, ctxt
, FIND_ENV
| FIND_GLOBAL
| FIND_CMD
);
3717 * Check also for bogus D and F forms of local variables since we're
3718 * in a local context and the name is the right length.
3720 if ((v
== NULL
) && (ctxt
!= VAR_CMD
) && (ctxt
!= VAR_GLOBAL
) &&
3721 (vlen
== 2) && (str
[1] == 'F' || str
[1] == 'D') &&
3722 strchr("@%*!<>", str
[0]) != NULL
) {
3724 * Well, it's local -- go look for it.
3728 v
= VarFind(name
, ctxt
, 0);
3732 * No need for nested expansion or anything, as we're
3733 * the only one who sets these things and we sure don't
3734 * but nested invocations in them...
3736 nstr
= Buf_GetAll(&v
->val
, NULL
);
3738 if (str
[1] == 'D') {
3739 nstr
= VarModify(ctxt
, &parsestate
, nstr
, VarHead
,
3742 nstr
= VarModify(ctxt
, &parsestate
, nstr
, VarTail
,
3746 * Resulting string is dynamically allocated, so
3747 * tell caller to free it.
3750 *lengthPtr
= tstr
-start
+1;
3751 Buf_Destroy(&buf
, TRUE
);
3752 VarFreeEnv(v
, TRUE
);
3759 (((vlen
== 2) && (str
[1] == 'F' || str
[1] == 'D')))) &&
3760 ((ctxt
== VAR_CMD
) || (ctxt
== VAR_GLOBAL
)))
3763 * If substituting a local variable in a non-local context,
3764 * assume it's for dynamic source stuff. We have to handle
3765 * this specially and return the longhand for the variable
3766 * with the dollar sign escaped so it makes it back to the
3767 * caller. Only four of the local variables are treated
3768 * specially as they are the only four that will be set
3769 * when dynamic sources are expanded.
3779 } else if ((vlen
> 2) && (*str
== '.') &&
3780 isupper((unsigned char) str
[1]) &&
3781 ((ctxt
== VAR_CMD
) || (ctxt
== VAR_GLOBAL
)))
3786 if ((strncmp(str
, ".TARGET", len
) == 0) ||
3787 (strncmp(str
, ".ARCHIVE", len
) == 0) ||
3788 (strncmp(str
, ".PREFIX", len
) == 0) ||
3789 (strncmp(str
, ".MEMBER", len
) == 0))
3795 if (!haveModifier
) {
3797 * No modifiers -- have specification length so we can return
3800 *lengthPtr
= tstr
- start
+ 1;
3802 char *pstr
= bmake_strndup(start
, *lengthPtr
);
3804 Buf_Destroy(&buf
, TRUE
);
3807 Buf_Destroy(&buf
, TRUE
);
3808 return (errnum
? var_Error
: varNoError
);
3812 * Still need to get to the end of the variable specification,
3813 * so kludge up a Var structure for the modifications
3815 v
= bmake_malloc(sizeof(Var
));
3816 v
->name
= UNCONST(str
);
3817 Buf_Init(&v
->val
, 1);
3818 v
->flags
= VAR_JUNK
;
3819 Buf_Destroy(&buf
, FALSE
);
3822 Buf_Destroy(&buf
, TRUE
);
3825 if (v
->flags
& VAR_IN_USE
) {
3826 Fatal("Variable %s is recursive.", v
->name
);
3829 v
->flags
|= VAR_IN_USE
;
3832 * Before doing any modification, we have to make sure the value
3833 * has been fully expanded. If it looks like recursion might be
3834 * necessary (there's a dollar sign somewhere in the variable's value)
3835 * we just call Var_Subst to do any other substitutions that are
3836 * necessary. Note that the value returned by Var_Subst will have
3837 * been dynamically-allocated, so it will need freeing when we
3840 nstr
= Buf_GetAll(&v
->val
, NULL
);
3841 if (strchr(nstr
, '$') != NULL
) {
3842 nstr
= Var_Subst(NULL
, nstr
, ctxt
, errnum
);
3846 v
->flags
&= ~VAR_IN_USE
;
3848 if ((nstr
!= NULL
) && haveModifier
) {
3851 * Skip initial colon.
3855 nstr
= ApplyModifiers(nstr
, tstr
, startc
, endc
,
3856 v
, ctxt
, errnum
, &used
, freePtr
);
3860 *lengthPtr
= tstr
- start
+ 1;
3862 *lengthPtr
= tstr
- start
;
3865 if (v
->flags
& VAR_FROM_ENV
) {
3866 Boolean destroy
= FALSE
;
3868 if (nstr
!= Buf_GetAll(&v
->val
, NULL
)) {
3872 * Returning the value unmodified, so tell the caller to free
3877 VarFreeEnv(v
, destroy
);
3878 } else if (v
->flags
& VAR_JUNK
) {
3880 * Perform any free'ing needed and set *freePtr to NULL so the caller
3881 * doesn't try to free a static pointer.
3882 * If VAR_KEEP is also set then we want to keep str as is.
3884 if (!(v
->flags
& VAR_KEEP
)) {
3890 nstr
= bmake_strndup(start
, *lengthPtr
);
3893 nstr
= errnum
? var_Error
: varNoError
;
3896 if (nstr
!= Buf_GetAll(&v
->val
, NULL
))
3897 Buf_Destroy(&v
->val
, TRUE
);
3905 *-----------------------------------------------------------------------
3907 * Substitute for all variables in the given string in the given context
3908 * If undefErr is TRUE, Parse_Error will be called when an undefined
3909 * variable is encountered.
3912 * var Named variable || NULL for all
3913 * str the string which to substitute
3914 * ctxt the context wherein to find variables
3915 * undefErr TRUE if undefineds are an error
3918 * The resulting string.
3921 * None. The old string must be freed by the caller
3922 *-----------------------------------------------------------------------
3925 Var_Subst(const char *var
, const char *str
, GNode
*ctxt
, Boolean undefErr
)
3927 Buffer buf
; /* Buffer for forming things */
3928 char *val
; /* Value to substitute for a variable */
3929 int length
; /* Length of the variable invocation */
3930 Boolean trailingBslash
; /* variable ends in \ */
3931 void *freeIt
= NULL
; /* Set if it should be freed */
3932 static Boolean errorReported
; /* Set true if an error has already
3933 * been reported to prevent a plethora
3934 * of messages when recursing */
3937 errorReported
= FALSE
;
3938 trailingBslash
= FALSE
;
3941 if (*str
== '\n' && trailingBslash
)
3942 Buf_AddByte(&buf
, ' ');
3943 if (var
== NULL
&& (*str
== '$') && (str
[1] == '$')) {
3945 * A dollar sign may be escaped either with another dollar sign.
3946 * In such a case, we skip over the escape character and store the
3947 * dollar sign into the buffer directly.
3950 Buf_AddByte(&buf
, *str
);
3952 } else if (*str
!= '$') {
3954 * Skip as many characters as possible -- either to the end of
3955 * the string or to the next dollar sign (variable invocation).
3959 for (cp
= str
++; *str
!= '$' && *str
!= '\0'; str
++)
3961 Buf_AddBytes(&buf
, str
- cp
, cp
);
3966 if (str
[1] == '\0') {
3967 /* A trailing $ is kind of a special case */
3968 Buf_AddByte(&buf
, str
[0]);
3971 } else if (str
[1] != PROPEN
&& str
[1] != BROPEN
) {
3972 if (str
[1] != *var
|| strlen(var
) > 1) {
3973 Buf_AddBytes(&buf
, 2, str
);
3985 * Scan up to the end of the variable name.
3987 for (p
= &str
[2]; *p
&&
3988 *p
!= ':' && *p
!= PRCLOSE
&& *p
!= BRCLOSE
; p
++)
3992 * A variable inside the variable. We cannot expand
3993 * the external variable yet, so we try again with
3997 Buf_AddBytes(&buf
, p
- str
, str
);
4002 if (strncmp(var
, str
+ 2, p
- str
- 2) != 0 ||
4003 var
[p
- str
- 2] != '\0') {
4005 * Not the variable we want to expand, scan
4006 * until the next variable
4008 for (;*p
!= '$' && *p
!= '\0'; p
++)
4010 Buf_AddBytes(&buf
, p
- str
, str
);
4023 val
= Var_Parse(str
, ctxt
, undefErr
, &length
, &freeIt
);
4026 * When we come down here, val should either point to the
4027 * value of this variable, suitably modified, or be NULL.
4028 * Length should be the total length of the potential
4029 * variable invocation (from $ to end character...)
4031 if (val
== var_Error
|| val
== varNoError
) {
4033 * If performing old-time variable substitution, skip over
4034 * the variable and continue with the substitution. Otherwise,
4035 * store the dollar sign and advance str so we continue with
4040 } else if (undefErr
) {
4042 * If variable is undefined, complain and skip the
4043 * variable. The complaint will stop us from doing anything
4044 * when the file is parsed.
4046 if (!errorReported
) {
4047 Parse_Error(PARSE_FATAL
,
4048 "Undefined variable \"%.*s\"",length
,str
);
4051 errorReported
= TRUE
;
4053 Buf_AddByte(&buf
, *str
);
4058 * We've now got a variable structure to store in. But first,
4059 * advance the string pointer.
4064 * Copy all the characters from the variable value straight
4065 * into the new string.
4067 length
= strlen(val
);
4068 Buf_AddBytes(&buf
, length
, val
);
4069 trailingBslash
= length
> 0 && val
[length
- 1] == '\\';
4078 return Buf_Destroy(&buf
, FALSE
);
4082 *-----------------------------------------------------------------------
4084 * Return the tail from each of a list of words. Used to set the
4085 * System V local variables.
4088 * file Filename to modify
4091 * The resulting string.
4096 *-----------------------------------------------------------------------
4100 Var_GetTail(char *file
)
4102 return(VarModify(file
, VarTail
, NULL
));
4106 *-----------------------------------------------------------------------
4108 * Find the leading components of a (list of) filename(s).
4109 * XXX: VarHead does not replace foo by ., as (sun) System V make
4113 * file Filename to manipulate
4116 * The leading components.
4121 *-----------------------------------------------------------------------
4124 Var_GetHead(char *file
)
4126 return(VarModify(file
, VarHead
, NULL
));
4131 *-----------------------------------------------------------------------
4133 * Initialize the module
4139 * The VAR_CMD and VAR_GLOBAL contexts are created
4140 *-----------------------------------------------------------------------
4145 VAR_GLOBAL
= Targ_NewGN("Global");
4146 VAR_CMD
= Targ_NewGN("Command");
4157 /****************** PRINT DEBUGGING INFO *****************/
4159 VarPrintVar(void *vp
)
4162 fprintf(debug_file
, "%-16s = %s\n", v
->name
, Buf_GetAll(&v
->val
, NULL
));
4166 *-----------------------------------------------------------------------
4168 * print all variables in a context
4169 *-----------------------------------------------------------------------
4172 Var_Dump(GNode
*ctxt
)
4177 for (h
= Hash_EnumFirst(&ctxt
->context
, &search
);
4179 h
= Hash_EnumNext(&search
)) {
4180 VarPrintVar(Hash_GetValue(h
));