1 /* $NetBSD: var.c,v 1.218 2018/02/18 00:52: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.218 2018/02/18 00:52: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.218 2018/02/18 00:52: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>
140 #include "metachar.h"
142 extern int makelevel;
144 * This lets us tell if we have replaced the original environ
145 * (which we cannot free).
147 char **savedEnv = NULL;
150 * This is a harmless return value for Var_Parse that can be used by Var_Subst
151 * to determine if there was an error in parsing -- easier than returning
152 * a flag, as things outside this module don't give a hoot.
154 char var_Error[] = "";
157 * Similar to var_Error, but returned when the 'VARF_UNDEFERR' flag for
158 * Var_Parse is not set. Why not just use a constant? Well, gcc likes
159 * to condense identical string instances...
161 static char varNoError[] = "";
164 * Traditionally we consume $$ during := like any other expansion.
165 * Other make's do not.
166 * This knob allows controlling the behavior.
167 * FALSE for old behavior.
168 * TRUE for new compatible.
170 #define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
171 static Boolean save_dollars = FALSE;
174 * Internally, variables are contained in four different contexts.
175 * 1) the environment. They may not be changed. If an environment
176 * variable is appended-to, the result is placed in the global
178 * 2) the global context. Variables set in the Makefile are located in
179 * the global context. It is the penultimate context searched when
181 * 3) the command-line context. All variables set on the command line
182 * are placed in this context. They are UNALTERABLE once placed here.
183 * 4) the local context. Each target has associated with it a context
184 * list. On this list are located the structures describing such
185 * local variables as $(@) and $(*)
186 * The four contexts are searched in the reverse order from which they are
189 GNode *VAR_INTERNAL; /* variables from make itself */
190 GNode *VAR_GLOBAL; /* variables from the makefile */
191 GNode *VAR_CMD; /* variables defined on the command-line */
193 #define FIND_CMD 0x1 /* look in VAR_CMD when searching */
194 #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
195 #define FIND_ENV 0x4 /* look in the environment also */
198 char *name; /* the variable's name */
199 Buffer val; /* its value */
200 int flags; /* miscellaneous status flags */
201 #define VAR_IN_USE 1 /* Variable's value currently being used.
202 * Used to avoid recursion */
203 #define VAR_FROM_ENV 2 /* Variable comes from the environment */
204 #define VAR_JUNK 4 /* Variable is a junk variable that
205 * should be destroyed when done with
206 * it. Used by Var_Parse for undefined,
207 * modified variables */
208 #define VAR_KEEP 8 /* Variable is VAR_JUNK, but we found
209 * a use for it in some modifier and
210 * the value is therefore valid */
211 #define VAR_EXPORTED 16 /* Variable is exported */
212 #define VAR_REEXPORT 32 /* Indicate if var needs re-export.
213 * This would be true if it contains $'s
215 #define VAR_FROM_CMD 64 /* Variable came from command line */
219 * Exporting vars is expensive so skip it if we can
221 #define VAR_EXPORTED_NONE 0
222 #define VAR_EXPORTED_YES 1
223 #define VAR_EXPORTED_ALL 2
224 static int var_exportedVars = VAR_EXPORTED_NONE;
226 * We pass this to Var_Export when doing the initial export
227 * or after updating an exported var.
229 #define VAR_EXPORT_PARENT 1
231 * We pass this to Var_Export1 to tell it to leave the value alone.
233 #define VAR_EXPORT_LITERAL 2
235 /* Var*Pattern flags */
236 #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */
237 #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */
238 #define VAR_SUB_MATCHED 0x04 /* There was a match */
239 #define VAR_MATCH_START 0x08 /* Match at start of word */
240 #define VAR_MATCH_END 0x10 /* Match at end of word */
241 #define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */
244 #define VAR_NO_EXPORT 0x01 /* do not export */
248 * The following fields are set by Var_Parse() when it
249 * encounters modifiers that need to keep state for use by
250 * subsequent modifiers within the same variable expansion.
252 Byte varSpace; /* Word separator in expansions */
253 Boolean oneBigWord; /* TRUE if we will treat the variable as a
254 * single big word, even if it contains
255 * embedded spaces (as opposed to the
256 * usual behaviour of treating it as
257 * several space-separated words). */
260 /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/",
261 * to VarSYSVMatch() for ":lhs=rhs". */
263 const char *lhs; /* String to match */
264 int leftLen; /* Length of string */
265 const char *rhs; /* Replacement string (w/ &'s removed) */
266 int rightLen; /* Length of replacement */
270 /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */
272 GNode *ctxt; /* variable context */
273 char *tvar; /* name of temp var */
275 char *str; /* string to expand */
277 int errnum; /* errnum for not defined */
281 /* struct passed as 'void *' to VarRESubstitute() for ":C///" */
291 /* struct passed to VarSelectWords() for ":[start..end]" */
293 int start; /* first word to select */
294 int end; /* last word to select */
297 static Var *VarFind(const char *, GNode *, int);
298 static void VarAdd(const char *, const char *, GNode *);
299 static Boolean VarHead(GNode *, Var_Parse_State *,
300 char *, Boolean, Buffer *, void *);
301 static Boolean VarTail(GNode *, Var_Parse_State *,
302 char *, Boolean, Buffer *, void *);
303 static Boolean VarSuffix(GNode *, Var_Parse_State *,
304 char *, Boolean, Buffer *, void *);
305 static Boolean VarRoot(GNode *, Var_Parse_State *,
306 char *, Boolean, Buffer *, void *);
307 static Boolean VarMatch(GNode *, Var_Parse_State *,
308 char *, Boolean, Buffer *, void *);
310 static Boolean VarSYSVMatch(GNode *, Var_Parse_State *,
311 char *, Boolean, Buffer *, void *);
313 static Boolean VarNoMatch(GNode *, Var_Parse_State *,
314 char *, Boolean, Buffer *, void *);
316 static void VarREError(int, regex_t *, const char *);
317 static Boolean VarRESubstitute(GNode *, Var_Parse_State *,
318 char *, Boolean, Buffer *, void *);
320 static Boolean VarSubstitute(GNode *, Var_Parse_State *,
321 char *, Boolean, Buffer *, void *);
322 static Boolean VarLoopExpand(GNode *, Var_Parse_State *,
323 char *, Boolean, Buffer *, void *);
324 static char *VarGetPattern(GNode *, Var_Parse_State *,
325 int, const char **, int, int *, int *,
327 static char *VarQuote(char *);
328 static char *VarHash(char *);
329 static char *VarModify(GNode *, Var_Parse_State *,
331 Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *),
333 static char *VarOrder(const char *, const char);
334 static char *VarUniq(const char *);
335 static int VarWordCompare(const void *, const void *);
336 static void VarPrintVar(void *);
344 *-----------------------------------------------------------------------
346 * Find the given variable in the given context and any other contexts
351 * ctxt context in which to find it
352 * flags FIND_GLOBAL set means to look in the
353 * VAR_GLOBAL context as well. FIND_CMD set means
354 * to look in the VAR_CMD context also. FIND_ENV
355 * set means to look in the environment
358 * A pointer to the structure describing the desired variable or
359 * NULL if the variable does not exist.
363 *-----------------------------------------------------------------------
366 VarFind(const char *name, GNode *ctxt, int flags)
372 * If the variable name begins with a '.', it could very well be one of
373 * the local ones. We check the name against all the local variables
374 * and substitute the short version in for 'name' if it matches one of
377 if (*name == '.' && isupper((unsigned char) name[1]))
380 if (!strcmp(name, ".ALLSRC"))
382 if (!strcmp(name, ".ARCHIVE"))
386 if (!strcmp(name, ".IMPSRC"))
390 if (!strcmp(name, ".MEMBER"))
394 if (!strcmp(name, ".OODATE"))
398 if (!strcmp(name, ".PREFIX"))
402 if (!strcmp(name, ".TARGET"))
407 /* for compatibility with gmake */
408 if (name[0] == '^' && name[1] == '\0')
413 * First look for the variable in the given context. If it's not there,
414 * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
415 * depending on the FIND_* flags in 'flags'
417 var = Hash_FindEntry(&ctxt->context, name);
419 if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) {
420 var = Hash_FindEntry(&VAR_CMD->context, name);
422 if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) &&
423 (ctxt != VAR_GLOBAL))
425 var = Hash_FindEntry(&VAR_GLOBAL->context, name);
426 if ((var == NULL) && (ctxt != VAR_INTERNAL)) {
427 /* VAR_INTERNAL is subordinate to VAR_GLOBAL */
428 var = Hash_FindEntry(&VAR_INTERNAL->context, name);
431 if ((var == NULL) && (flags & FIND_ENV)) {
434 if ((env = getenv(name)) != NULL) {
437 v = bmake_malloc(sizeof(Var));
438 v->name = bmake_strdup(name);
442 Buf_Init(&v->val, len + 1);
443 Buf_AddBytes(&v->val, len, env);
445 v->flags = VAR_FROM_ENV;
447 } else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
448 (ctxt != VAR_GLOBAL))
450 var = Hash_FindEntry(&VAR_GLOBAL->context, name);
451 if ((var == NULL) && (ctxt != VAR_INTERNAL)) {
452 var = Hash_FindEntry(&VAR_INTERNAL->context, name);
457 return ((Var *)Hash_GetValue(var));
462 } else if (var == NULL) {
465 return ((Var *)Hash_GetValue(var));
470 *-----------------------------------------------------------------------
472 * If the variable is an environment variable, free it
476 * destroy true if the value buffer should be destroyed.
479 * 1 if it is an environment variable 0 ow.
482 * The variable is free'ed if it is an environent variable.
483 *-----------------------------------------------------------------------
486 VarFreeEnv(Var *v, Boolean destroy)
488 if ((v->flags & VAR_FROM_ENV) == 0)
491 Buf_Destroy(&v->val, destroy);
497 *-----------------------------------------------------------------------
499 * Add a new variable of name name and value val to the given context
502 * name name of variable to add
503 * val value to set it to
504 * ctxt context in which to set it
510 * The new variable is placed at the front of the given context
511 * The name and val arguments are duplicated so they may
513 *-----------------------------------------------------------------------
516 VarAdd(const char *name, const char *val, GNode *ctxt)
522 v = bmake_malloc(sizeof(Var));
524 len = val ? strlen(val) : 0;
525 Buf_Init(&v->val, len+1);
526 Buf_AddBytes(&v->val, len, val);
530 h = Hash_CreateEntry(&ctxt->context, name, NULL);
533 if (DEBUG(VAR) && (ctxt->flags & INTERNAL) == 0) {
534 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
539 *-----------------------------------------------------------------------
541 * Remove a variable from a context.
547 * The Var structure is removed and freed.
549 *-----------------------------------------------------------------------
552 Var_Delete(const char *name, GNode *ctxt)
557 if (strchr(name, '$')) {
558 cp = Var_Subst(NULL, name, VAR_GLOBAL, VARF_WANTRES);
562 ln = Hash_FindEntry(&ctxt->context, cp);
564 fprintf(debug_file, "%s:delete %s%s\n",
565 ctxt->name, cp, ln ? "" : " (not found)");
573 v = (Var *)Hash_GetValue(ln);
574 if ((v->flags & VAR_EXPORTED)) {
577 if (strcmp(MAKE_EXPORTED, v->name) == 0) {
578 var_exportedVars = VAR_EXPORTED_NONE;
580 if (v->name != ln->name)
582 Hash_DeleteEntry(&ctxt->context, ln);
583 Buf_Destroy(&v->val, TRUE);
591 * We ignore make internal variables (those which start with '.')
592 * Also we jump through some hoops to avoid calling setenv
593 * more than necessary since it can leak.
594 * We only manipulate flags of vars if 'parent' is set.
597 Var_Export1(const char *name, int flags)
603 int parent = (flags & VAR_EXPORT_PARENT);
606 return 0; /* skip internals */
610 * If it is one of the vars that should only appear in
611 * local context, skip it, else we can get Var_Subst
622 v = VarFind(name, VAR_GLOBAL, 0);
627 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
628 return 0; /* nothing to do */
630 val = Buf_GetAll(&v->val, NULL);
631 if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) {
634 * Flag this as something we need to re-export.
635 * No point actually exporting it now though,
636 * the child can do it at the last minute.
638 v->flags |= (VAR_EXPORTED|VAR_REEXPORT);
641 if (v->flags & VAR_IN_USE) {
643 * We recursed while exporting in a child.
644 * This isn't going to end well, just skip it.
648 n = snprintf(tmp, sizeof(tmp), "${%s}", name);
649 if (n < (int)sizeof(tmp)) {
650 val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
651 setenv(name, val, 1);
656 v->flags &= ~VAR_REEXPORT; /* once will do */
658 if (parent || !(v->flags & VAR_EXPORTED)) {
659 setenv(name, val, 1);
663 * This is so Var_Set knows to call Var_Export again...
666 v->flags |= VAR_EXPORTED;
672 * This gets called from our children.
685 * Several make's support this sort of mechanism for tracking
686 * recursion - but each uses a different name.
687 * We allow the makefiles to update MAKELEVEL and ensure
688 * children see a correctly incremented value.
690 snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
691 setenv(MAKE_LEVEL_ENV, tmp, 1);
693 if (VAR_EXPORTED_NONE == var_exportedVars)
696 if (VAR_EXPORTED_ALL == var_exportedVars) {
698 * Ouch! This is crazy...
700 for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state);
702 var = Hash_EnumNext(&state)) {
703 v = (Var *)Hash_GetValue(var);
704 Var_Export1(v->name, 0);
709 * We have a number of exported vars,
711 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
712 if (n < (int)sizeof(tmp)) {
718 val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
720 av = brk_string(val, &ac, FALSE, &as);
721 for (i = 0; i < ac; i++) {
722 Var_Export1(av[i], 0);
732 * This is called when .export is seen or
733 * .MAKE.EXPORTED is modified.
734 * It is also called when any exported var is modified.
737 Var_Export(char *str, int isExport)
747 if (isExport && (!str || !str[0])) {
748 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
753 if (strncmp(str, "-env", 4) == 0) {
755 } else if (strncmp(str, "-literal", 8) == 0) {
757 flags |= VAR_EXPORT_LITERAL;
759 flags |= VAR_EXPORT_PARENT;
761 val = Var_Subst(NULL, str, VAR_GLOBAL, VARF_WANTRES);
763 av = brk_string(val, &ac, FALSE, &as);
764 for (i = 0; i < ac; i++) {
769 * If it is one of the vars that should only appear in
770 * local context, skip it, else we can get Var_Subst
781 if (Var_Export1(name, flags)) {
782 if (VAR_EXPORTED_ALL != var_exportedVars)
783 var_exportedVars = VAR_EXPORTED_YES;
784 if (isExport && (flags & VAR_EXPORT_PARENT)) {
785 Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
797 * This is called when .unexport[-env] is seen.
799 extern char **environ;
802 Var_UnExport(char *str)
807 Boolean unexport_env;
810 if (!str || !str[0]) {
811 return; /* assert? */
817 unexport_env = (strncmp(str, "-env", 4) == 0);
821 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */
822 if (environ == savedEnv) {
823 /* we have been here before! */
824 newenv = bmake_realloc(environ, 2 * sizeof(char *));
830 newenv = bmake_malloc(2 * sizeof(char *));
834 /* Note: we cannot safely free() the original environ. */
835 environ = savedEnv = newenv;
838 setenv(MAKE_LEVEL_ENV, cp, 1);
840 for (; *str != '\n' && isspace((unsigned char) *str); str++)
842 if (str[0] && str[0] != '\n') {
848 /* Using .MAKE.EXPORTED */
849 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
850 if (n < (int)sizeof(tmp)) {
851 vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
861 av = brk_string(vlist, &ac, FALSE, &as);
862 for (i = 0; i < ac; i++) {
863 v = VarFind(av[i], VAR_GLOBAL, 0);
867 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
870 v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
872 * If we are unexporting a list,
873 * remove each one from .MAKE.EXPORTED.
874 * If we are removing them all,
875 * just delete .MAKE.EXPORTED below.
878 n = snprintf(tmp, sizeof(tmp),
879 "${" MAKE_EXPORTED ":N%s}", v->name);
880 if (n < (int)sizeof(tmp)) {
881 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
882 Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0);
890 Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
897 *-----------------------------------------------------------------------
899 * Set the variable name to the value val in the given context.
902 * name name of variable to set
903 * val value to give to the variable
904 * ctxt context in which to set it
910 * If the variable doesn't yet exist, a new record is created for it.
911 * Else the old value is freed and the new one stuck in its place
914 * The variable is searched for only in its context before being
915 * created in that context. I.e. if the context is VAR_GLOBAL,
916 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
917 * VAR_CMD->context is searched. This is done to avoid the literally
918 * thousands of unnecessary strcmp's that used to be done to
919 * set, say, $(@) or $(<).
920 * If the context is VAR_GLOBAL though, we check if the variable
921 * was set in VAR_CMD from the command line and skip it if so.
922 *-----------------------------------------------------------------------
925 Var_Set(const char *name, const char *val, GNode *ctxt, int flags)
928 char *expanded_name = NULL;
931 * We only look for a variable in the given context since anything set
932 * here will override anything in a lower context, so there's not much
933 * point in searching them all just to save a bit of memory...
935 if (strchr(name, '$') != NULL) {
936 expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
937 if (expanded_name[0] == 0) {
939 fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
940 "name expands to empty string - ignored\n",
946 name = expanded_name;
948 if (ctxt == VAR_GLOBAL) {
949 v = VarFind(name, VAR_CMD, 0);
951 if ((v->flags & VAR_FROM_CMD)) {
953 fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val);
960 v = VarFind(name, ctxt, 0);
962 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
964 * This var would normally prevent the same name being added
965 * to VAR_GLOBAL, so delete it from there if needed.
966 * Otherwise -V name may show the wrong value.
968 Var_Delete(name, VAR_GLOBAL);
970 VarAdd(name, val, ctxt);
974 Buf_AddBytes(&v->val, strlen(val), val);
977 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
979 if ((v->flags & VAR_EXPORTED)) {
980 Var_Export1(name, VAR_EXPORT_PARENT);
984 * Any variables given on the command line are automatically exported
985 * to the environment (as per POSIX standard)
987 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
989 /* we just added it */
990 v = VarFind(name, ctxt, 0);
993 v->flags |= VAR_FROM_CMD;
995 * If requested, don't export these in the environment
996 * individually. We still put them in MAKEOVERRIDES so
997 * that the command-line settings continue to override
1000 if (varNoExportEnv != TRUE)
1001 setenv(name, val ? val : "", 1);
1003 Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
1006 if (strcmp(name, SAVE_DOLLARS) == 0)
1007 save_dollars = s2Boolean(val, save_dollars);
1011 free(expanded_name);
1013 VarFreeEnv(v, TRUE);
1017 *-----------------------------------------------------------------------
1019 * The variable of the given name has the given value appended to it in
1020 * the given context.
1023 * name name of variable to modify
1024 * val String to append to it
1025 * ctxt Context in which this should occur
1031 * If the variable doesn't exist, it is created. Else the strings
1032 * are concatenated (with a space in between).
1035 * Only if the variable is being sought in the global context is the
1036 * environment searched.
1037 * XXX: Knows its calling circumstances in that if called with ctxt
1038 * an actual target, it will only search that context since only
1039 * a local variable could be being appended to. This is actually
1040 * a big win and must be tolerated.
1041 *-----------------------------------------------------------------------
1044 Var_Append(const char *name, const char *val, GNode *ctxt)
1048 char *expanded_name = NULL;
1050 if (strchr(name, '$') != NULL) {
1051 expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1052 if (expanded_name[0] == 0) {
1054 fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
1055 "name expands to empty string - ignored\n",
1058 free(expanded_name);
1061 name = expanded_name;
1064 v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? (FIND_CMD|FIND_ENV) : 0);
1067 Var_Set(name, val, ctxt, 0);
1068 } else if (ctxt == VAR_CMD || !(v->flags & VAR_FROM_CMD)) {
1069 Buf_AddByte(&v->val, ' ');
1070 Buf_AddBytes(&v->val, strlen(val), val);
1073 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
1074 Buf_GetAll(&v->val, NULL));
1077 if (v->flags & VAR_FROM_ENV) {
1079 * If the original variable came from the environment, we
1080 * have to install it in the global context (we could place
1081 * it in the environment, but then we should provide a way to
1082 * export other variables...)
1084 v->flags &= ~VAR_FROM_ENV;
1085 h = Hash_CreateEntry(&ctxt->context, name, NULL);
1086 Hash_SetValue(h, v);
1089 free(expanded_name);
1093 *-----------------------------------------------------------------------
1095 * See if the given variable exists.
1098 * name Variable to find
1099 * ctxt Context in which to start search
1102 * TRUE if it does, FALSE if it doesn't
1107 *-----------------------------------------------------------------------
1110 Var_Exists(const char *name, GNode *ctxt)
1115 if ((cp = strchr(name, '$')) != NULL) {
1116 cp = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1118 v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
1123 (void)VarFreeEnv(v, TRUE);
1129 *-----------------------------------------------------------------------
1131 * Return the value of the named variable in the given context
1135 * ctxt context in which to search for it
1138 * The value if the variable exists, NULL if it doesn't
1142 *-----------------------------------------------------------------------
1145 Var_Value(const char *name, GNode *ctxt, char **frp)
1149 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1152 char *p = (Buf_GetAll(&v->val, NULL));
1153 if (VarFreeEnv(v, FALSE))
1162 *-----------------------------------------------------------------------
1164 * Remove the tail of the given word and place the result in the given
1169 * addSpace True if need to add a space to the buffer
1170 * before sticking in the head
1171 * buf Buffer in which to store it
1174 * TRUE if characters were added to the buffer (a space needs to be
1175 * added to the buffer before the next word).
1178 * The trimmed word is added to the buffer.
1180 *-----------------------------------------------------------------------
1183 VarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1184 char *word, Boolean addSpace, Buffer *buf,
1185 void *dummy MAKE_ATTR_UNUSED)
1189 slash = strrchr(word, '/');
1190 if (slash != NULL) {
1191 if (addSpace && vpstate->varSpace) {
1192 Buf_AddByte(buf, vpstate->varSpace);
1195 Buf_AddBytes(buf, strlen(word), word);
1200 * If no directory part, give . (q.v. the POSIX standard)
1202 if (addSpace && vpstate->varSpace)
1203 Buf_AddByte(buf, vpstate->varSpace);
1204 Buf_AddByte(buf, '.');
1210 *-----------------------------------------------------------------------
1212 * Remove the head of the given word and place the result in the given
1217 * addSpace True if need to add a space to the buffer
1218 * before adding the tail
1219 * buf Buffer in which to store it
1222 * TRUE if characters were added to the buffer (a space needs to be
1223 * added to the buffer before the next word).
1226 * The trimmed word is added to the buffer.
1228 *-----------------------------------------------------------------------
1231 VarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1232 char *word, Boolean addSpace, Buffer *buf,
1233 void *dummy MAKE_ATTR_UNUSED)
1237 if (addSpace && vpstate->varSpace) {
1238 Buf_AddByte(buf, vpstate->varSpace);
1241 slash = strrchr(word, '/');
1242 if (slash != NULL) {
1244 Buf_AddBytes(buf, strlen(slash), slash);
1247 Buf_AddBytes(buf, strlen(word), word);
1253 *-----------------------------------------------------------------------
1255 * Place the suffix of the given word in the given buffer.
1259 * addSpace TRUE if need to add a space before placing the
1260 * suffix in the buffer
1261 * buf Buffer in which to store it
1264 * TRUE if characters were added to the buffer (a space needs to be
1265 * added to the buffer before the next word).
1268 * The suffix from the word is placed in the buffer.
1270 *-----------------------------------------------------------------------
1273 VarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1274 char *word, Boolean addSpace, Buffer *buf,
1275 void *dummy MAKE_ATTR_UNUSED)
1279 dot = strrchr(word, '.');
1281 if (addSpace && vpstate->varSpace) {
1282 Buf_AddByte(buf, vpstate->varSpace);
1285 Buf_AddBytes(buf, strlen(dot), dot);
1293 *-----------------------------------------------------------------------
1295 * Remove the suffix of the given word and place the result in the
1300 * addSpace TRUE if need to add a space to the buffer
1301 * before placing the root in it
1302 * buf Buffer in which to store it
1305 * TRUE if characters were added to the buffer (a space needs to be
1306 * added to the buffer before the next word).
1309 * The trimmed word is added to the buffer.
1311 *-----------------------------------------------------------------------
1314 VarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1315 char *word, Boolean addSpace, Buffer *buf,
1316 void *dummy MAKE_ATTR_UNUSED)
1320 if (addSpace && vpstate->varSpace) {
1321 Buf_AddByte(buf, vpstate->varSpace);
1324 dot = strrchr(word, '.');
1327 Buf_AddBytes(buf, strlen(word), word);
1330 Buf_AddBytes(buf, strlen(word), word);
1336 *-----------------------------------------------------------------------
1338 * Place the word in the buffer if it matches the given pattern.
1339 * Callback function for VarModify to implement the :M modifier.
1342 * word Word to examine
1343 * addSpace TRUE if need to add a space to the buffer
1344 * before adding the word, if it matches
1345 * buf Buffer in which to store it
1346 * pattern Pattern the word must match
1349 * TRUE if a space should be placed in the buffer before the next
1353 * The word may be copied to the buffer.
1355 *-----------------------------------------------------------------------
1358 VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1359 char *word, Boolean addSpace, Buffer *buf,
1363 fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern);
1364 if (Str_Match(word, (char *)pattern)) {
1365 if (addSpace && vpstate->varSpace) {
1366 Buf_AddByte(buf, vpstate->varSpace);
1369 Buf_AddBytes(buf, strlen(word), word);
1376 *-----------------------------------------------------------------------
1378 * Place the word in the buffer if it matches the given pattern.
1379 * Callback function for VarModify to implement the System V %
1383 * word Word to examine
1384 * addSpace TRUE if need to add a space to the buffer
1385 * before adding the word, if it matches
1386 * buf Buffer in which to store it
1387 * patp Pattern the word must match
1390 * TRUE if a space should be placed in the buffer before the next
1394 * The word may be copied to the buffer.
1396 *-----------------------------------------------------------------------
1399 VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
1400 char *word, Boolean addSpace, Buffer *buf,
1405 VarPattern *pat = (VarPattern *)patp;
1408 if (addSpace && vpstate->varSpace)
1409 Buf_AddByte(buf, vpstate->varSpace);
1413 if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) {
1414 varexp = Var_Subst(NULL, pat->rhs, ctx, VARF_WANTRES);
1415 Str_SYSVSubst(buf, varexp, ptr, len);
1418 Buf_AddBytes(buf, strlen(word), word);
1427 *-----------------------------------------------------------------------
1429 * Place the word in the buffer if it doesn't match the given pattern.
1430 * Callback function for VarModify to implement the :N modifier.
1433 * word Word to examine
1434 * addSpace TRUE if need to add a space to the buffer
1435 * before adding the word, if it matches
1436 * buf Buffer in which to store it
1437 * pattern Pattern the word must match
1440 * TRUE if a space should be placed in the buffer before the next
1444 * The word may be copied to the buffer.
1446 *-----------------------------------------------------------------------
1449 VarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1450 char *word, Boolean addSpace, Buffer *buf,
1453 if (!Str_Match(word, (char *)pattern)) {
1454 if (addSpace && vpstate->varSpace) {
1455 Buf_AddByte(buf, vpstate->varSpace);
1458 Buf_AddBytes(buf, strlen(word), word);
1465 *-----------------------------------------------------------------------
1467 * Perform a string-substitution on the given word, placing the
1468 * result in the passed buffer.
1471 * word Word to modify
1472 * addSpace True if space should be added before
1474 * buf Buffer for result
1475 * patternp Pattern for substitution
1478 * TRUE if a space is needed before more characters are added.
1483 *-----------------------------------------------------------------------
1486 VarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1487 char *word, Boolean addSpace, Buffer *buf,
1490 int wordLen; /* Length of word */
1491 char *cp; /* General pointer */
1492 VarPattern *pattern = (VarPattern *)patternp;
1494 wordLen = strlen(word);
1495 if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
1496 (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1498 * Still substituting -- break it down into simple anchored cases
1499 * and if none of them fits, perform the general substitution case.
1501 if ((pattern->flags & VAR_MATCH_START) &&
1502 (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
1504 * Anchored at start and beginning of word matches pattern
1506 if ((pattern->flags & VAR_MATCH_END) &&
1507 (wordLen == pattern->leftLen)) {
1509 * Also anchored at end and matches to the end (word
1510 * is same length as pattern) add space and rhs only
1511 * if rhs is non-null.
1513 if (pattern->rightLen != 0) {
1514 if (addSpace && vpstate->varSpace) {
1515 Buf_AddByte(buf, vpstate->varSpace);
1518 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1520 pattern->flags |= VAR_SUB_MATCHED;
1521 } else if (pattern->flags & VAR_MATCH_END) {
1523 * Doesn't match to end -- copy word wholesale
1528 * Matches at start but need to copy in trailing characters
1530 if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
1531 if (addSpace && vpstate->varSpace) {
1532 Buf_AddByte(buf, vpstate->varSpace);
1536 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1537 Buf_AddBytes(buf, wordLen - pattern->leftLen,
1538 (word + pattern->leftLen));
1539 pattern->flags |= VAR_SUB_MATCHED;
1541 } else if (pattern->flags & VAR_MATCH_START) {
1543 * Had to match at start of word and didn't -- copy whole word.
1546 } else if (pattern->flags & VAR_MATCH_END) {
1548 * Anchored at end, Find only place match could occur (leftLen
1549 * characters from the end of the word) and see if it does. Note
1550 * that because the $ will be left at the end of the lhs, we have
1553 cp = word + (wordLen - pattern->leftLen);
1555 (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
1557 * Match found. If we will place characters in the buffer,
1558 * add a space before hand as indicated by addSpace, then
1559 * stuff in the initial, unmatched part of the word followed
1560 * by the right-hand-side.
1562 if (((cp - word) + pattern->rightLen) != 0) {
1563 if (addSpace && vpstate->varSpace) {
1564 Buf_AddByte(buf, vpstate->varSpace);
1568 Buf_AddBytes(buf, cp - word, word);
1569 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1570 pattern->flags |= VAR_SUB_MATCHED;
1573 * Had to match at end and didn't. Copy entire word.
1579 * Pattern is unanchored: search for the pattern in the word using
1580 * String_FindSubstring, copying unmatched portions and the
1581 * right-hand-side for each match found, handling non-global
1582 * substitutions correctly, etc. When the loop is done, any
1583 * remaining part of the word (word and wordLen are adjusted
1584 * accordingly through the loop) is copied straight into the
1586 * addSpace is set FALSE as soon as a space is added to the
1593 origSize = Buf_Size(buf);
1595 cp = Str_FindSubstring(word, pattern->lhs);
1597 if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
1598 Buf_AddByte(buf, vpstate->varSpace);
1601 Buf_AddBytes(buf, cp-word, word);
1602 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1603 wordLen -= (cp - word) + pattern->leftLen;
1604 word = cp + pattern->leftLen;
1608 if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
1611 pattern->flags |= VAR_SUB_MATCHED;
1617 if (addSpace && vpstate->varSpace) {
1618 Buf_AddByte(buf, vpstate->varSpace);
1620 Buf_AddBytes(buf, wordLen, word);
1623 * If added characters to the buffer, need to add a space
1624 * before we add any more. If we didn't add any, just return
1625 * the previous value of addSpace.
1627 return ((Buf_Size(buf) != origSize) || addSpace);
1632 if (addSpace && vpstate->varSpace) {
1633 Buf_AddByte(buf, vpstate->varSpace);
1635 Buf_AddBytes(buf, wordLen, word);
1641 *-----------------------------------------------------------------------
1643 * Print the error caused by a regcomp or regexec call.
1649 * An error gets printed.
1651 *-----------------------------------------------------------------------
1654 VarREError(int reerr, regex_t *pat, const char *str)
1659 errlen = regerror(reerr, pat, 0, 0);
1660 errbuf = bmake_malloc(errlen);
1661 regerror(reerr, pat, errbuf, errlen);
1662 Error("%s: %s", str, errbuf);
1668 *-----------------------------------------------------------------------
1669 * VarRESubstitute --
1670 * Perform a regex substitution on the given word, placing the
1671 * result in the passed buffer.
1674 * TRUE if a space is needed before more characters are added.
1679 *-----------------------------------------------------------------------
1682 VarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED,
1683 Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1684 char *word, Boolean addSpace, Buffer *buf,
1694 #define MAYBE_ADD_SPACE() \
1695 if (addSpace && !added) \
1696 Buf_AddByte(buf, ' '); \
1703 if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
1704 (VAR_SUB_ONE|VAR_SUB_MATCHED))
1708 xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
1713 pat->flags |= VAR_SUB_MATCHED;
1714 if (pat->matches[0].rm_so > 0) {
1716 Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
1719 for (rp = pat->replace; *rp; rp++) {
1720 if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
1722 Buf_AddByte(buf,rp[1]);
1725 else if ((*rp == '&') ||
1726 ((*rp == '\\') && isdigit((unsigned char)rp[1]))) {
1744 if (n > pat->nsub) {
1745 Error("No subexpression %s", &errstr[0]);
1748 } else if ((pat->matches[n].rm_so == -1) &&
1749 (pat->matches[n].rm_eo == -1)) {
1750 Error("No match for subexpression %s", &errstr[0]);
1754 subbuf = wp + pat->matches[n].rm_so;
1755 sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
1760 Buf_AddBytes(buf, sublen, subbuf);
1764 Buf_AddByte(buf, *rp);
1767 wp += pat->matches[0].rm_eo;
1768 if (pat->flags & VAR_SUB_GLOBAL) {
1769 flags |= REG_NOTBOL;
1770 if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
1772 Buf_AddByte(buf, *wp);
1781 Buf_AddBytes(buf, strlen(wp), wp);
1785 VarREError(xrv, &pat->re, "Unexpected regex error");
1790 Buf_AddBytes(buf,strlen(wp),wp);
1794 return(addSpace||added);
1801 *-----------------------------------------------------------------------
1803 * Implements the :@<temp>@<string>@ modifier of ODE make.
1804 * We set the temp variable named in pattern.lhs to word and expand
1805 * pattern.rhs storing the result in the passed buffer.
1808 * word Word to modify
1809 * addSpace True if space should be added before
1811 * buf Buffer for result
1812 * pattern Datafor substitution
1815 * TRUE if a space is needed before more characters are added.
1820 *-----------------------------------------------------------------------
1823 VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED,
1824 Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1825 char *word, Boolean addSpace, Buffer *buf,
1828 VarLoop_t *loop = (VarLoop_t *)loopp;
1832 if (word && *word) {
1833 Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
1834 s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum | VARF_WANTRES);
1835 if (s != NULL && *s != '\0') {
1836 if (addSpace && *s != '\n')
1837 Buf_AddByte(buf, ' ');
1838 Buf_AddBytes(buf, (slen = strlen(s)), s);
1839 addSpace = (slen > 0 && s[slen - 1] != '\n');
1848 *-----------------------------------------------------------------------
1850 * Implements the :[start..end] modifier.
1851 * This is a special case of VarModify since we want to be able
1852 * to scan the list backwards if start > end.
1855 * str String whose words should be trimmed
1856 * seldata words to select
1859 * A string of all the words selected.
1864 *-----------------------------------------------------------------------
1867 VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1868 const char *str, VarSelectWords_t *seldata)
1870 Buffer buf; /* Buffer for the new string */
1871 Boolean addSpace; /* TRUE if need to add a space to the
1872 * buffer before adding the trimmed
1874 char **av; /* word list */
1875 char *as; /* word list memory */
1877 int start, end, step;
1882 if (vpstate->oneBigWord) {
1883 /* fake what brk_string() would do if there were only one word */
1885 av = bmake_malloc((ac + 1) * sizeof(char *));
1886 as = bmake_strdup(str);
1890 av = brk_string(str, &ac, FALSE, &as);
1894 * Now sanitize seldata.
1895 * If seldata->start or seldata->end are negative, convert them to
1896 * the positive equivalents (-1 gets converted to argc, -2 gets
1897 * converted to (argc-1), etc.).
1899 if (seldata->start < 0)
1900 seldata->start = ac + seldata->start + 1;
1901 if (seldata->end < 0)
1902 seldata->end = ac + seldata->end + 1;
1905 * We avoid scanning more of the list than we need to.
1907 if (seldata->start > seldata->end) {
1908 start = MIN(ac, seldata->start) - 1;
1909 end = MAX(0, seldata->end - 1);
1912 start = MAX(0, seldata->start - 1);
1913 end = MIN(ac, seldata->end);
1918 (step < 0 && i >= end) || (step > 0 && i < end);
1920 if (av[i] && *av[i]) {
1921 if (addSpace && vpstate->varSpace) {
1922 Buf_AddByte(&buf, vpstate->varSpace);
1924 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1932 return Buf_Destroy(&buf, FALSE);
1938 * Replace each word with the result of realpath()
1942 VarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1943 char *word, Boolean addSpace, Buffer *buf,
1944 void *patternp MAKE_ATTR_UNUSED)
1947 char rbuf[MAXPATHLEN];
1950 if (addSpace && vpstate->varSpace) {
1951 Buf_AddByte(buf, vpstate->varSpace);
1954 rp = cached_realpath(word, rbuf);
1955 if (rp && *rp == '/' && stat(rp, &st) == 0)
1958 Buf_AddBytes(buf, strlen(word), word);
1963 *-----------------------------------------------------------------------
1965 * Modify each of the words of the passed string using the given
1966 * function. Used to implement all modifiers.
1969 * str String whose words should be trimmed
1970 * modProc Function to use to modify them
1971 * datum Datum to pass it
1974 * A string of all the words modified appropriately.
1979 *-----------------------------------------------------------------------
1982 VarModify(GNode *ctx, Var_Parse_State *vpstate,
1984 Boolean (*modProc)(GNode *, Var_Parse_State *, char *,
1985 Boolean, Buffer *, void *),
1988 Buffer buf; /* Buffer for the new string */
1989 Boolean addSpace; /* TRUE if need to add a space to the
1990 * buffer before adding the trimmed
1992 char **av; /* word list */
1993 char *as; /* word list memory */
1999 if (vpstate->oneBigWord) {
2000 /* fake what brk_string() would do if there were only one word */
2002 av = bmake_malloc((ac + 1) * sizeof(char *));
2003 as = bmake_strdup(str);
2007 av = brk_string(str, &ac, FALSE, &as);
2010 for (i = 0; i < ac; i++) {
2011 addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum);
2017 return Buf_Destroy(&buf, FALSE);
2022 VarWordCompare(const void *a, const void *b)
2024 int r = strcmp(*(const char * const *)a, *(const char * const *)b);
2029 *-----------------------------------------------------------------------
2031 * Order the words in the string.
2034 * str String whose words should be sorted.
2035 * otype How to order: s - sort, x - random.
2038 * A string containing the words ordered.
2043 *-----------------------------------------------------------------------
2046 VarOrder(const char *str, const char otype)
2048 Buffer buf; /* Buffer for the new string */
2049 char **av; /* word list [first word does not count] */
2050 char *as; /* word list memory */
2055 av = brk_string(str, &ac, FALSE, &as);
2059 case 's': /* sort alphabetically */
2060 qsort(av, ac, sizeof(char *), VarWordCompare);
2062 case 'x': /* randomize */
2068 * We will use [ac..2] range for mod factors. This will produce
2069 * random numbers in [(ac-1)..0] interval, and minimal
2070 * reasonable value for mod factor is 2 (the mod 1 will produce
2071 * 0 with probability 1).
2073 for (i = ac-1; i > 0; i--) {
2074 rndidx = random() % (i + 1);
2082 } /* end of switch */
2084 for (i = 0; i < ac; i++) {
2085 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
2087 Buf_AddByte(&buf, ' ');
2093 return Buf_Destroy(&buf, FALSE);
2098 *-----------------------------------------------------------------------
2100 * Remove adjacent duplicate words.
2103 * str String whose words should be sorted
2106 * A string containing the resulting words.
2111 *-----------------------------------------------------------------------
2114 VarUniq(const char *str)
2116 Buffer buf; /* Buffer for new string */
2117 char **av; /* List of words to affect */
2118 char *as; /* Word list memory */
2122 av = brk_string(str, &ac, FALSE, &as);
2125 for (j = 0, i = 1; i < ac; i++)
2126 if (strcmp(av[i], av[j]) != 0 && (++j != i))
2131 for (i = 0; i < ac; i++) {
2132 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
2134 Buf_AddByte(&buf, ' ');
2140 return Buf_Destroy(&buf, FALSE);
2144 *-----------------------------------------------------------------------
2146 * Return an integer sequence
2149 * str String whose words provide default range
2150 * ac range length, if 0 use str words
2155 *-----------------------------------------------------------------------
2158 VarRange(const char *str, int ac)
2160 Buffer buf; /* Buffer for new string */
2161 char tmp[32]; /* each element */
2162 char **av; /* List of words to affect */
2163 char *as; /* Word list memory */
2171 av = brk_string(str, &ac, FALSE, &as);
2173 for (i = 0; i < ac; i++) {
2174 n = snprintf(tmp, sizeof(tmp), "%d", 1 + i);
2175 if (n >= (int)sizeof(tmp))
2177 Buf_AddBytes(&buf, n, tmp);
2179 Buf_AddByte(&buf, ' ');
2185 return Buf_Destroy(&buf, FALSE);
2190 *-----------------------------------------------------------------------
2192 * Pass through the tstr looking for 1) escaped delimiters,
2193 * '$'s and backslashes (place the escaped character in
2194 * uninterpreted) and 2) unescaped $'s that aren't before
2195 * the delimiter (expand the variable substitution unless flags
2196 * has VAR_NOSUBST set).
2197 * Return the expanded string or NULL if the delimiter was missing
2198 * If pattern is specified, handle escaped ampersands, and replace
2199 * unescaped ampersands with the lhs of the pattern.
2202 * A string of all the words modified appropriately.
2203 * If length is specified, return the string length of the buffer
2204 * If flags is specified and the last character of the pattern is a
2205 * $ set the VAR_MATCH_END bit of flags.
2209 *-----------------------------------------------------------------------
2212 VarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
2213 int flags, const char **tstr, int delim, int *vflags,
2214 int *length, VarPattern *pattern)
2220 int errnum = flags & VARF_UNDEFERR;
2226 #define IS_A_MATCH(cp, delim) \
2227 ((cp[0] == '\\') && ((cp[1] == delim) || \
2228 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
2231 * Skim through until the matching delimiter is found;
2232 * pick up variable substitutions on the way. Also allow
2233 * backslashes to quote the delimiter, $, and \, but don't
2234 * touch other backslashes.
2236 for (cp = *tstr; *cp && (*cp != delim); cp++) {
2237 if (IS_A_MATCH(cp, delim)) {
2238 Buf_AddByte(&buf, cp[1]);
2240 } else if (*cp == '$') {
2241 if (cp[1] == delim) {
2243 Buf_AddByte(&buf, *cp);
2246 * Unescaped $ at end of pattern => anchor
2249 *vflags |= VAR_MATCH_END;
2251 if (vflags == NULL || (*vflags & VAR_NOSUBST) == 0) {
2257 * If unescaped dollar sign not before the
2258 * delimiter, assume it's a variable
2259 * substitution and recurse.
2261 cp2 = Var_Parse(cp, ctxt, errnum | VARF_WANTRES, &len,
2263 Buf_AddBytes(&buf, strlen(cp2), cp2);
2267 const char *cp2 = &cp[1];
2269 if (*cp2 == PROPEN || *cp2 == BROPEN) {
2271 * Find the end of this variable reference
2272 * and suck it in without further ado.
2273 * It will be interperated later.
2276 int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE;
2279 for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
2280 if (cp2[-1] != '\\') {
2287 Buf_AddBytes(&buf, cp2 - cp, cp);
2290 Buf_AddByte(&buf, *cp);
2294 else if (pattern && *cp == '&')
2295 Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs);
2297 Buf_AddByte(&buf, *cp);
2307 *length = Buf_Size(&buf);
2308 rstr = Buf_Destroy(&buf, FALSE);
2310 fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr);
2315 *-----------------------------------------------------------------------
2317 * Quote shell meta-characters and space characters in the string
2325 *-----------------------------------------------------------------------
2332 const char *newline;
2335 if ((newline = Shell_GetNewline()) == NULL)
2337 nlen = strlen(newline);
2341 for (; *str != '\0'; str++) {
2343 Buf_AddBytes(&buf, nlen, newline);
2346 if (isspace((unsigned char)*str) || ismeta((unsigned char)*str))
2347 Buf_AddByte(&buf, '\\');
2348 Buf_AddByte(&buf, *str);
2351 str = Buf_Destroy(&buf, FALSE);
2353 fprintf(debug_file, "QuoteMeta: [%s]\n", str);
2358 *-----------------------------------------------------------------------
2360 * Hash the string using the MurmurHash3 algorithm.
2361 * Output is computed using 32bit Little Endian arithmetic.
2364 * str String to modify
2367 * Hash value of str, encoded as 8 hex digits.
2372 *-----------------------------------------------------------------------
2377 static const char hexdigits[16] = "0123456789abcdef";
2380 unsigned char *ustr = (unsigned char *)str;
2381 unsigned int h, k, c1, c2;
2388 for (len = len2; len; ) {
2392 k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0];
2397 k |= (ustr[2] << 16);
2399 k |= (ustr[1] << 8);
2404 c1 = c1 * 5 + 0x7b7d159cU;
2405 c2 = c2 * 5 + 0x6bce6396U;
2407 k = (k << 11) ^ (k >> 21);
2409 h = (h << 13) ^ (h >> 19);
2410 h = h * 5 + 0x52dce729U;
2420 for (len = 0; len < 8; ++len) {
2421 Buf_AddByte(&buf, hexdigits[h & 15]);
2425 return Buf_Destroy(&buf, FALSE);
2429 VarStrftime(const char *fmt, int zulu, time_t utc)
2437 strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc));
2439 buf[sizeof(buf) - 1] = '\0';
2440 return bmake_strdup(buf);
2444 * Now we need to apply any modifiers the user wants applied.
2446 * :M<pattern> words which match the given <pattern>.
2447 * <pattern> is of the standard file
2449 * :N<pattern> words which do not match the given <pattern>.
2450 * :S<d><pat1><d><pat2><d>[1gW]
2451 * Substitute <pat2> for <pat1> in the value
2452 * :C<d><pat1><d><pat2><d>[1gW]
2453 * Substitute <pat2> for regex <pat1> in the value
2454 * :H Substitute the head of each word
2455 * :T Substitute the tail of each word
2456 * :E Substitute the extension (minus '.') of
2458 * :R Substitute the root of each word
2459 * (pathname minus the suffix).
2460 * :O ("Order") Alphabeticaly sort words in variable.
2461 * :Ox ("intermiX") Randomize words in variable.
2462 * :u ("uniq") Remove adjacent duplicate words.
2463 * :tu Converts the variable contents to uppercase.
2464 * :tl Converts the variable contents to lowercase.
2465 * :ts[c] Sets varSpace - the char used to
2466 * separate words to 'c'. If 'c' is
2467 * omitted then no separation is used.
2468 * :tW Treat the variable contents as a single
2469 * word, even if it contains spaces.
2470 * (Mnemonic: one big 'W'ord.)
2471 * :tw Treat the variable contents as multiple
2472 * space-separated words.
2473 * (Mnemonic: many small 'w'ords.)
2474 * :[index] Select a single word from the value.
2475 * :[start..end] Select multiple words from the value.
2476 * :[*] or :[0] Select the entire value, as a single
2477 * word. Equivalent to :tW.
2478 * :[@] Select the entire value, as multiple
2479 * words. Undoes the effect of :[*].
2480 * Equivalent to :tw.
2481 * :[#] Returns the number of words in the value.
2483 * :?<true-value>:<false-value>
2484 * If the variable evaluates to true, return
2485 * true value, else return the second value.
2486 * :lhs=rhs Like :S, but the rhs goes to the end of
2488 * :sh Treat the current value as a command
2489 * to be run, new value is its output.
2490 * The following added so we can handle ODE makefiles.
2491 * :@<tmpvar>@<newval>@
2492 * Assign a temporary local variable <tmpvar>
2493 * to the current value of each word in turn
2494 * and replace each word with the result of
2495 * evaluating <newval>
2496 * :D<newval> Use <newval> as value if variable defined
2497 * :U<newval> Use <newval> as value if variable undefined
2498 * :L Use the name of the variable as the value.
2499 * :P Use the path of the node that has the same
2500 * name as the variable as the value. This
2501 * basically includes an implied :L so that
2502 * the common method of refering to the path
2503 * of your dependent 'x' in a rule is to use
2504 * the form '${x:P}'.
2505 * :!<cmd>! Run cmd much the same as :sh run's the
2506 * current value of the variable.
2507 * The ::= modifiers, actually assign a value to the variable.
2508 * Their main purpose is in supporting modifiers of .for loop
2509 * iterators and other obscure uses. They always expand to
2510 * nothing. In a target rule that would otherwise expand to an
2511 * empty line they can be preceded with @: to keep make happy.
2515 * .for i in ${.TARGET} ${.TARGET:R}.gz
2520 * ::=<str> Assigns <str> as the new value of variable.
2521 * ::?=<str> Assigns <str> as value of variable if
2522 * it was not already set.
2523 * ::+=<str> Appends <str> to variable.
2524 * ::!=<cmd> Assigns output of <cmd> as the new value of
2528 /* we now have some modifiers with long names */
2529 #define STRMOD_MATCH(s, want, n) \
2530 (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':'))
2531 #define STRMOD_MATCHX(s, want, n) \
2532 (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':' || s[n] == '='))
2533 #define CHARMOD_MATCH(c) (c == endc || c == ':')
2536 ApplyModifiers(char *nstr, const char *tstr,
2537 int startc, int endc,
2538 Var *v, GNode *ctxt, int flags,
2539 int *lengthPtr, void **freePtr)
2542 const char *cp; /* Secondary pointer into str (place marker
2544 char *newStr; /* New value to return */
2546 char termc; /* Character which terminated scan */
2547 int cnt; /* Used to count brace pairs when variable in
2548 * in parens or braces */
2550 int modifier; /* that we are processing */
2551 Var_Parse_State parsestate; /* Flags passed to helper functions */
2552 time_t utc; /* for VarStrftime */
2555 parsestate.oneBigWord = FALSE;
2556 parsestate.varSpace = ' '; /* word separator */
2560 while (*tstr && *tstr != endc) {
2564 * We may have some complex modifiers in a variable.
2571 rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
2574 * If we have not parsed up to endc or ':',
2575 * we are not interested.
2577 if (rval != NULL && *rval &&
2578 (c = tstr[rlen]) != '\0' &&
2586 fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n",
2587 rval, rlen, tstr, rlen, tstr + rlen);
2592 if (rval != NULL && *rval) {
2595 nstr = ApplyModifiers(nstr, rval,
2596 0, 0, v, ctxt, flags, &used, freePtr);
2597 if (nstr == var_Error
2598 || (nstr == varNoError && (flags & VARF_UNDEFERR) == 0)
2599 || strlen(rval) != (size_t) used) {
2601 goto out; /* error already reported */
2607 else if (!*tstr && endc) {
2608 Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name);
2615 fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name,
2619 switch ((modifier = *tstr)) {
2622 if (tstr[1] == '=' ||
2624 (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) {
2626 * "::=", "::!=", "::+=", or "::?="
2628 GNode *v_ctxt; /* context where v belongs */
2635 if (v->name[0] == 0)
2641 if (v->flags & VAR_JUNK) {
2643 * We need to bmake_strdup() it incase
2644 * VarGetPattern() recurses.
2647 v->name = bmake_strdup(v->name);
2648 } else if (ctxt != VAR_GLOBAL) {
2649 Var *gv = VarFind(v->name, ctxt, 0);
2651 v_ctxt = VAR_GLOBAL;
2653 VarFreeEnv(gv, TRUE);
2656 switch ((how = *tstr)) {
2666 delim = startc == PROPEN ? PRCLOSE : BRCLOSE;
2669 vflags = (flags & VARF_WANTRES) ? 0 : VAR_NOSUBST;
2670 pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
2671 &cp, delim, &vflags,
2674 if (v->flags & VAR_JUNK) {
2675 /* restore original name */
2679 if (pattern.rhs == NULL)
2685 if (flags & VARF_WANTRES) {
2688 Var_Append(v->name, pattern.rhs, v_ctxt);
2691 newStr = Cmd_Exec(pattern.rhs, &emsg);
2695 Var_Set(v->name, newStr, v_ctxt, 0);
2699 if ((v->flags & VAR_JUNK) == 0)
2703 Var_Set(v->name, pattern.rhs, v_ctxt, 0);
2707 free(UNCONST(pattern.rhs));
2708 newStr = varNoError;
2711 goto default_case; /* "::<unrecognised>" */
2716 int vflags = VAR_NOSUBST;
2720 if ((loop.tvar = VarGetPattern(ctxt, &parsestate, flags,
2722 &vflags, &loop.tvarLen,
2726 if ((loop.str = VarGetPattern(ctxt, &parsestate, flags,
2728 &vflags, &loop.strLen,
2735 loop.errnum = flags & VARF_UNDEFERR;
2737 newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand,
2739 Var_Delete(loop.tvar, ctxt);
2744 case '_': /* remember current value */
2745 cp = tstr + 1; /* make sure it is set */
2746 if (STRMOD_MATCHX(tstr, "_", 1)) {
2747 if (tstr[1] == '=') {
2752 n = strcspn(cp, ":)}");
2753 np = bmake_strndup(cp, n+1);
2756 Var_Set(np, nstr, ctxt, 0);
2759 Var_Set("_", nstr, ctxt, 0);
2769 Buffer buf; /* Buffer for patterns */
2772 if (flags & VARF_WANTRES) {
2775 wantres = ((v->flags & VAR_JUNK) != 0);
2777 wantres = ((v->flags & VAR_JUNK) == 0);
2778 nflags = flags & ~VARF_WANTRES;
2780 nflags |= VARF_WANTRES;
2784 * Pass through tstr looking for 1) escaped delimiters,
2785 * '$'s and backslashes (place the escaped character in
2786 * uninterpreted) and 2) unescaped $'s that aren't before
2787 * the delimiter (expand the variable substitution).
2788 * The result is left in the Buffer buf.
2792 *cp != endc && *cp != ':' && *cp != '\0';
2794 if ((*cp == '\\') &&
2800 Buf_AddByte(&buf, cp[1]);
2802 } else if (*cp == '$') {
2804 * If unescaped dollar sign, assume it's a
2805 * variable substitution and recurse.
2811 cp2 = Var_Parse(cp, ctxt, nflags, &len, &freeIt);
2812 Buf_AddBytes(&buf, strlen(cp2), cp2);
2816 Buf_AddByte(&buf, *cp);
2822 if ((v->flags & VAR_JUNK) != 0)
2823 v->flags |= VAR_KEEP;
2824 if (nflags & VARF_WANTRES) {
2825 newStr = Buf_Destroy(&buf, FALSE);
2828 Buf_Destroy(&buf, TRUE);
2834 if ((v->flags & VAR_JUNK) != 0)
2835 v->flags |= VAR_KEEP;
2836 newStr = bmake_strdup(v->name);
2845 if ((v->flags & VAR_JUNK) != 0)
2846 v->flags |= VAR_KEEP;
2847 gn = Targ_FindNode(v->name, TARG_NOCREATE);
2848 if (gn == NULL || gn->type & OP_NOPATH) {
2850 } else if (gn->path) {
2851 newStr = bmake_strdup(gn->path);
2853 newStr = Dir_FindFile(v->name, Suff_FindPath(gn));
2856 newStr = bmake_strdup(v->name);
2871 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
2873 NULL, &pattern.rightLen,
2876 if (flags & VARF_WANTRES)
2877 newStr = Cmd_Exec(pattern.rhs, &emsg);
2879 newStr = varNoError;
2880 free(UNCONST(pattern.rhs));
2885 if (v->flags & VAR_JUNK) {
2886 v->flags |= VAR_KEEP;
2893 * Look for the closing ']', recursively
2894 * expanding any embedded variables.
2896 * estr is a pointer to the expanded result,
2897 * which we must free().
2901 cp = tstr+1; /* point to char after '[' */
2902 delim = ']'; /* look for closing ']' */
2903 estr = VarGetPattern(ctxt, &parsestate,
2907 goto cleanup; /* report missing ']' */
2908 /* now cp points just after the closing ']' */
2910 if (cp[0] != ':' && cp[0] != endc) {
2911 /* Found junk after ']' */
2915 if (estr[0] == '\0') {
2916 /* Found empty square brackets in ":[]". */
2919 } else if (estr[0] == '#' && estr[1] == '\0') {
2923 * We will need enough space for the decimal
2924 * representation of an int. We calculate the
2925 * space needed for the octal representation,
2926 * and add enough slop to cope with a '-' sign
2927 * (which should never be needed) and a '\0'
2928 * string terminator.
2931 (sizeof(int) * CHAR_BIT + 2) / 3 + 2;
2933 newStr = bmake_malloc(newStrSize);
2934 if (parsestate.oneBigWord) {
2935 strncpy(newStr, "1", newStrSize);
2937 /* XXX: brk_string() is a rather expensive
2938 * way of counting words. */
2943 av = brk_string(nstr, &ac, FALSE, &as);
2944 snprintf(newStr, newStrSize, "%d", ac);
2951 } else if (estr[0] == '*' && estr[1] == '\0') {
2953 parsestate.oneBigWord = TRUE;
2958 } else if (estr[0] == '@' && estr[1] == '\0') {
2960 parsestate.oneBigWord = FALSE;
2967 * We expect estr to contain a single
2968 * integer for :[N], or two integers
2969 * separated by ".." for :[start..end].
2971 VarSelectWords_t seldata = { 0, 0 };
2973 seldata.start = strtol(estr, &ep, 0);
2975 /* Found junk instead of a number */
2978 } else if (ep[0] == '\0') {
2979 /* Found only one integer in :[N] */
2980 seldata.end = seldata.start;
2981 } else if (ep[0] == '.' && ep[1] == '.' &&
2983 /* Expecting another integer after ".." */
2985 seldata.end = strtol(ep, &ep, 0);
2986 if (ep[0] != '\0') {
2987 /* Found junk after ".." */
2992 /* Found junk instead of ".." */
2997 * Now seldata is properly filled in,
2998 * but we still have to check for 0 as
3001 if (seldata.start == 0 && seldata.end == 0) {
3002 /* ":[0]" or perhaps ":[0..0]" */
3003 parsestate.oneBigWord = TRUE;
3008 } else if (seldata.start == 0 ||
3010 /* ":[0..N]" or ":[N..0]" */
3015 * Normal case: select the words
3016 * described by seldata.
3018 newStr = VarSelectWords(ctxt, &parsestate,
3028 cp = tstr + 1; /* make sure it is set */
3029 if (STRMOD_MATCHX(tstr, "gmtime", 6)) {
3030 if (tstr[6] == '=') {
3031 utc = strtoul(&tstr[7], &ep, 10);
3037 newStr = VarStrftime(nstr, 1, utc);
3044 cp = tstr + 1; /* make sure it is set */
3045 if (STRMOD_MATCH(tstr, "hash", 4)) {
3046 newStr = VarHash(nstr);
3054 cp = tstr + 1; /* make sure it is set */
3055 if (STRMOD_MATCHX(tstr, "localtime", 9)) {
3056 if (tstr[9] == '=') {
3057 utc = strtoul(&tstr[10], &ep, 10);
3063 newStr = VarStrftime(nstr, 0, utc);
3071 cp = tstr + 1; /* make sure it is set */
3072 if (tstr[1] != endc && tstr[1] != ':') {
3073 if (tstr[1] == 's') {
3075 * Use the char (if any) at tstr[2]
3076 * as the word separator.
3080 if (tstr[2] != endc &&
3081 (tstr[3] == endc || tstr[3] == ':')) {
3082 /* ":ts<unrecognised><endc>" or
3083 * ":ts<unrecognised>:" */
3084 parsestate.varSpace = tstr[2];
3086 } else if (tstr[2] == endc || tstr[2] == ':') {
3087 /* ":ts<endc>" or ":ts:" */
3088 parsestate.varSpace = 0; /* no separator */
3090 } else if (tstr[2] == '\\') {
3091 const char *xp = &tstr[3];
3092 int base = 8; /* assume octal */
3096 parsestate.varSpace = '\n';
3100 parsestate.varSpace = '\t';
3111 if (isdigit((unsigned char)tstr[3])) {
3114 parsestate.varSpace =
3115 strtoul(xp, &ep, base);
3116 if (*ep != ':' && *ep != endc)
3121 * ":ts<backslash><unrecognised>".
3129 * Found ":ts<unrecognised><unrecognised>".
3137 * We cannot be certain that VarModify
3138 * will be used - even if there is a
3139 * subsequent modifier, so do a no-op
3140 * VarSubstitute now to for str to be
3141 * re-expanded without the spaces.
3143 pattern.flags = VAR_SUB_ONE;
3144 pattern.lhs = pattern.rhs = "\032";
3145 pattern.leftLen = pattern.rightLen = 1;
3147 newStr = VarModify(ctxt, &parsestate, nstr,
3150 } else if (tstr[2] == endc || tstr[2] == ':') {
3152 * Check for two-character options:
3155 if (tstr[1] == 'A') { /* absolute path */
3156 newStr = VarModify(ctxt, &parsestate, nstr,
3160 } else if (tstr[1] == 'u') {
3161 char *dp = bmake_strdup(nstr);
3162 for (newStr = dp; *dp; dp++)
3163 *dp = toupper((unsigned char)*dp);
3166 } else if (tstr[1] == 'l') {
3167 char *dp = bmake_strdup(nstr);
3168 for (newStr = dp; *dp; dp++)
3169 *dp = tolower((unsigned char)*dp);
3172 } else if (tstr[1] == 'W' || tstr[1] == 'w') {
3173 parsestate.oneBigWord = (tstr[1] == 'W');
3178 /* Found ":t<unrecognised>:" or
3179 * ":t<unrecognised><endc>". */
3184 * Found ":t<unrecognised><unrecognised>".
3190 * Found ":t<endc>" or ":t:".
3200 const char *endpat; /* points just after end of pattern */
3202 Boolean copy; /* pattern should be, or has been, copied */
3210 * In the loop below, ignore ':' unless we are at
3211 * (or back to) the original brace level.
3212 * XXX This will likely not work right if $() and ${}
3216 *cp != '\0' && !(*cp == ':' && nest == 1);
3221 cp[1] == endc || cp[1] == startc)) {
3231 if (*cp == '(' || *cp == '{')
3233 if (*cp == ')' || *cp == '}') {
3243 * Need to compress the \:'s out of the pattern, so
3244 * allocate enough room to hold the uncompressed
3245 * pattern (note that cp started at tstr+1, so
3246 * cp - tstr takes the null byte into account) and
3247 * compress the pattern into the space.
3249 pattern = bmake_malloc(cp - tstr);
3250 for (cp2 = pattern, cp = tstr + 1;
3254 if ((*cp == '\\') && (cp+1 < endpat) &&
3255 (cp[1] == ':' || cp[1] == endc)) {
3264 * Either Var_Subst or VarModify will need a
3265 * nul-terminated string soon, so construct one now.
3267 pattern = bmake_strndup(tstr+1, endpat - (tstr + 1));
3271 * pattern contains embedded '$', so use Var_Subst to
3275 pattern = Var_Subst(NULL, cp2, ctxt, flags | VARF_WANTRES);
3279 fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n",
3280 v->name, nstr, pattern);
3282 newStr = VarModify(ctxt, &parsestate, nstr, VarMatch,
3285 newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch,
3294 Var_Parse_State tmpparsestate;
3297 tmpparsestate = parsestate;
3302 * If pattern begins with '^', it is anchored to the
3303 * start of the word -- skip over it and flag pattern.
3306 pattern.flags |= VAR_MATCH_START;
3311 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags,
3318 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
3325 * Check for global substitution. If 'g' after the final
3326 * delimiter, substitution is global and is marked that
3332 pattern.flags |= VAR_SUB_GLOBAL;
3335 pattern.flags |= VAR_SUB_ONE;
3338 tmpparsestate.oneBigWord = TRUE;
3345 newStr = VarModify(ctxt, &tmpparsestate, nstr,
3350 * Free the two strings.
3352 free(UNCONST(pattern.lhs));
3353 free(UNCONST(pattern.rhs));
3362 int lhs_flags, rhs_flags;
3364 /* find ':', and then substitute accordingly */
3365 if (flags & VARF_WANTRES) {
3366 cond_rc = Cond_EvalExpression(NULL, v->name, &value, 0, FALSE);
3367 if (cond_rc == COND_INVALID) {
3368 lhs_flags = rhs_flags = VAR_NOSUBST;
3371 rhs_flags = VAR_NOSUBST;
3373 lhs_flags = VAR_NOSUBST;
3377 /* we are just consuming and discarding */
3378 cond_rc = value = 0;
3379 lhs_flags = rhs_flags = VAR_NOSUBST;
3385 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags,
3386 &cp, delim, &lhs_flags,
3391 /* BROPEN or PROPEN */
3393 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
3394 &cp, delim, &rhs_flags,
3401 if (cond_rc == COND_INVALID) {
3402 Error("Bad conditional expression `%s' in %s?%s:%s",
3403 v->name, v->name, pattern.lhs, pattern.rhs);
3408 newStr = UNCONST(pattern.lhs);
3409 free(UNCONST(pattern.rhs));
3411 newStr = UNCONST(pattern.rhs);
3412 free(UNCONST(pattern.lhs));
3414 if (v->flags & VAR_JUNK) {
3415 v->flags |= VAR_KEEP;
3422 VarREPattern pattern;
3425 Var_Parse_State tmpparsestate;
3428 tmpparsestate = parsestate;
3434 if ((re = VarGetPattern(ctxt, &parsestate, flags, &cp, delim,
3435 NULL, NULL, NULL)) == NULL)
3438 if ((pattern.replace = VarGetPattern(ctxt, &parsestate,
3439 flags, &cp, delim, NULL,
3440 NULL, NULL)) == NULL){
3448 pattern.flags |= VAR_SUB_GLOBAL;
3451 pattern.flags |= VAR_SUB_ONE;
3454 tmpparsestate.oneBigWord = TRUE;
3462 error = regcomp(&pattern.re, re, REG_EXTENDED);
3465 *lengthPtr = cp - start + 1;
3466 VarREError(error, &pattern.re, "RE substitution error");
3467 free(pattern.replace);
3471 pattern.nsub = pattern.re.re_nsub + 1;
3472 if (pattern.nsub < 1)
3474 if (pattern.nsub > 10)
3476 pattern.matches = bmake_malloc(pattern.nsub *
3477 sizeof(regmatch_t));
3478 newStr = VarModify(ctxt, &tmpparsestate, nstr,
3481 regfree(&pattern.re);
3482 free(pattern.replace);
3483 free(pattern.matches);
3489 if (tstr[1] == endc || tstr[1] == ':') {
3490 newStr = VarQuote(nstr);
3497 if (tstr[1] == endc || tstr[1] == ':') {
3498 newStr = VarModify(ctxt, &parsestate, nstr, VarTail,
3506 if (tstr[1] == endc || tstr[1] == ':') {
3507 newStr = VarModify(ctxt, &parsestate, nstr, VarHead,
3515 if (tstr[1] == endc || tstr[1] == ':') {
3516 newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix,
3524 if (tstr[1] == endc || tstr[1] == ':') {
3525 newStr = VarModify(ctxt, &parsestate, nstr, VarRoot,
3533 cp = tstr + 1; /* make sure it is set */
3534 if (STRMOD_MATCHX(tstr, "range", 5)) {
3537 if (tstr[5] == '=') {
3538 n = strtoul(&tstr[6], &ep, 10);
3544 newStr = VarRange(nstr, n);
3553 cp = tstr + 1; /* skip to the rest in any case */
3554 if (tstr[1] == endc || tstr[1] == ':') {
3557 } else if ( (tstr[1] == 'x') &&
3558 (tstr[2] == endc || tstr[2] == ':') ) {
3565 newStr = VarOrder(nstr, otype);
3569 if (tstr[1] == endc || tstr[1] == ':') {
3570 newStr = VarUniq(nstr);
3578 if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
3580 if (flags & VARF_WANTRES) {
3581 newStr = Cmd_Exec(nstr, &emsg);
3585 newStr = varNoError;
3597 * This can either be a bogus modifier or a System-V
3598 * substitution command.
3606 * First we make a pass through the string trying
3607 * to verify it is a SYSV-make-style translation:
3608 * it must be: <string1>=<string2>)
3612 while (*cp != '\0' && cnt) {
3615 /* continue looking for endc */
3617 else if (*cp == endc)
3619 else if (*cp == startc)
3624 if (*cp == endc && eqFound) {
3627 * Now we break this sucker into the lhs and
3628 * rhs. We must null terminate them of course.
3632 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate,
3633 flags, &cp, delim, &pattern.flags,
3634 &pattern.leftLen, NULL)) == NULL)
3637 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate,
3638 flags, &cp, delim, NULL, &pattern.rightLen,
3643 * SYSV modifications happen through the whole
3644 * string. Note the pattern is anchored at the end.
3648 if (pattern.leftLen == 0 && *nstr == '\0') {
3649 newStr = nstr; /* special case */
3651 newStr = VarModify(ctxt, &parsestate, nstr,
3655 free(UNCONST(pattern.lhs));
3656 free(UNCONST(pattern.rhs));
3660 Error("Unknown modifier '%c'", *tstr);
3662 *cp != ':' && *cp != endc && *cp != '\0';
3671 fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n",
3672 v->name, modifier, newStr);
3675 if (newStr != nstr) {
3681 if (nstr != var_Error && nstr != varNoError) {
3685 if (termc == '\0' && endc != '\0') {
3686 Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier);
3687 } else if (termc == ':') {
3693 *lengthPtr = tstr - start;
3698 Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr,
3702 *lengthPtr = cp - start;
3704 Error("Unclosed substitution for %s (%c missing)",
3712 *-----------------------------------------------------------------------
3714 * Given the start of a variable invocation, extract the variable
3715 * name and find its value, then modify it according to the
3719 * str The string to parse
3720 * ctxt The context for the variable
3721 * flags VARF_UNDEFERR if undefineds are an error
3722 * VARF_WANTRES if we actually want the result
3723 * VARF_ASSIGN if we are in a := assignment
3724 * lengthPtr OUT: The length of the specification
3725 * freePtr OUT: Non-NULL if caller should free *freePtr
3728 * The (possibly-modified) value of the variable or var_Error if the
3729 * specification is invalid. The length of the specification is
3730 * placed in *lengthPtr (for invalid specifications, this is just
3732 * If *freePtr is non-NULL then it's a pointer that the caller
3733 * should pass to free() to free memory used by the result.
3738 *-----------------------------------------------------------------------
3740 /* coverity[+alloc : arg-*4] */
3742 Var_Parse(const char *str, GNode *ctxt, int flags,
3743 int *lengthPtr, void **freePtr)
3745 const char *tstr; /* Pointer into str */
3746 Var *v; /* Variable in invocation */
3747 Boolean haveModifier;/* TRUE if have modifiers for the variable */
3748 char endc; /* Ending character when variable in parens
3750 char startc; /* Starting character when variable in parens
3752 int vlen; /* Length of variable name */
3753 const char *start; /* Points to original start of str */
3754 char *nstr; /* New string, used during expansion */
3755 Boolean dynamic; /* TRUE if the variable is local and we're
3756 * expanding it in a non-local context. This
3757 * is done to support dynamic sources. The
3758 * result is just the invocation, unaltered */
3759 const char *extramodifiers; /* extra modifiers to apply first */
3763 extramodifiers = NULL;
3768 if (startc != PROPEN && startc != BROPEN) {
3770 * If it's not bounded by braces of some sort, life is much simpler.
3771 * We just need to check for the first character and return the
3772 * value if it exists.
3775 /* Error out some really stupid names */
3776 if (startc == '\0' || strchr(")}:$", startc)) {
3783 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3787 if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
3789 * If substituting a local variable in a non-local context,
3790 * assume it's for dynamic source stuff. We have to handle
3791 * this specially and return the longhand for the variable
3792 * with the dollar sign escaped so it makes it back to the
3793 * caller. Only four of the local variables are treated
3794 * specially as they are the only four that will be set
3795 * when dynamic sources are expanded.
3799 return UNCONST("$(.TARGET)");
3801 return UNCONST("$(.MEMBER)");
3803 return UNCONST("$(.PREFIX)");
3805 return UNCONST("$(.ARCHIVE)");
3811 return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3813 haveModifier = FALSE;
3818 Buffer buf; /* Holds the variable name */
3821 endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
3825 * Skip to the end character or a colon, whichever comes first.
3827 for (tstr = str + 2; *tstr != '\0'; tstr++)
3830 * Track depth so we can spot parse errors.
3832 if (*tstr == startc) {
3835 if (*tstr == endc) {
3839 if (depth == 1 && *tstr == ':') {
3843 * A variable inside a variable, expand
3848 char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
3850 Buf_AddBytes(&buf, strlen(rval), rval);
3856 Buf_AddByte(&buf, *tstr);
3859 haveModifier = TRUE;
3860 } else if (*tstr == endc) {
3861 haveModifier = FALSE;
3864 * If we never did find the end character, return NULL
3865 * right now, setting the length to be the distance to
3866 * the end of the string, since that's what make does.
3868 *lengthPtr = tstr - str;
3869 Buf_Destroy(&buf, TRUE);
3872 str = Buf_GetAll(&buf, &vlen);
3875 * At this point, str points into newly allocated memory from
3876 * buf, containing only the name of the variable.
3878 * start and tstr point into the const string that was pointed
3879 * to by the original value of the str parameter. start points
3880 * to the '$' at the beginning of the string, while tstr points
3881 * to the char just after the end of the variable name -- this
3882 * will be '\0', ':', PRCLOSE, or BRCLOSE.
3885 v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3887 * Check also for bogus D and F forms of local variables since we're
3888 * in a local context and the name is the right length.
3890 if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
3891 (vlen == 2) && (str[1] == 'F' || str[1] == 'D') &&
3892 strchr("@%?*!<>", str[0]) != NULL) {
3894 * Well, it's local -- go look for it.
3898 v = VarFind(name, ctxt, 0);
3901 if (str[1] == 'D') {
3902 extramodifiers = "H:";
3905 extramodifiers = "T:";
3912 (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) &&
3913 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3916 * If substituting a local variable in a non-local context,
3917 * assume it's for dynamic source stuff. We have to handle
3918 * this specially and return the longhand for the variable
3919 * with the dollar sign escaped so it makes it back to the
3920 * caller. Only four of the local variables are treated
3921 * specially as they are the only four that will be set
3922 * when dynamic sources are expanded.
3932 } else if ((vlen > 2) && (*str == '.') &&
3933 isupper((unsigned char) str[1]) &&
3934 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3939 if ((strncmp(str, ".TARGET", len) == 0) ||
3940 (strncmp(str, ".ARCHIVE", len) == 0) ||
3941 (strncmp(str, ".PREFIX", len) == 0) ||
3942 (strncmp(str, ".MEMBER", len) == 0))
3948 if (!haveModifier) {
3950 * No modifiers -- have specification length so we can return
3953 *lengthPtr = tstr - start + 1;
3955 char *pstr = bmake_strndup(start, *lengthPtr);
3957 Buf_Destroy(&buf, TRUE);
3960 Buf_Destroy(&buf, TRUE);
3961 return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3965 * Still need to get to the end of the variable specification,
3966 * so kludge up a Var structure for the modifications
3968 v = bmake_malloc(sizeof(Var));
3969 v->name = UNCONST(str);
3970 Buf_Init(&v->val, 1);
3971 v->flags = VAR_JUNK;
3972 Buf_Destroy(&buf, FALSE);
3975 Buf_Destroy(&buf, TRUE);
3978 if (v->flags & VAR_IN_USE) {
3979 Fatal("Variable %s is recursive.", v->name);
3982 v->flags |= VAR_IN_USE;
3985 * Before doing any modification, we have to make sure the value
3986 * has been fully expanded. If it looks like recursion might be
3987 * necessary (there's a dollar sign somewhere in the variable's value)
3988 * we just call Var_Subst to do any other substitutions that are
3989 * necessary. Note that the value returned by Var_Subst will have
3990 * been dynamically-allocated, so it will need freeing when we
3993 nstr = Buf_GetAll(&v->val, NULL);
3994 if (strchr(nstr, '$') != NULL) {
3995 nstr = Var_Subst(NULL, nstr, ctxt, flags);
3999 v->flags &= ~VAR_IN_USE;
4001 if ((nstr != NULL) && (haveModifier || extramodifiers != NULL)) {
4006 if (extramodifiers != NULL) {
4007 nstr = ApplyModifiers(nstr, extramodifiers, '(', ')',
4008 v, ctxt, flags, &used, &extraFree);
4012 /* Skip initial colon. */
4015 nstr = ApplyModifiers(nstr, tstr, startc, endc,
4016 v, ctxt, flags, &used, freePtr);
4020 *freePtr = extraFree;
4024 *lengthPtr = tstr - start + 1;
4026 *lengthPtr = tstr - start;
4029 if (v->flags & VAR_FROM_ENV) {
4030 Boolean destroy = FALSE;
4032 if (nstr != Buf_GetAll(&v->val, NULL)) {
4036 * Returning the value unmodified, so tell the caller to free
4041 VarFreeEnv(v, destroy);
4042 } else if (v->flags & VAR_JUNK) {
4044 * Perform any free'ing needed and set *freePtr to NULL so the caller
4045 * doesn't try to free a static pointer.
4046 * If VAR_KEEP is also set then we want to keep str as is.
4048 if (!(v->flags & VAR_KEEP)) {
4054 nstr = bmake_strndup(start, *lengthPtr);
4057 nstr = (flags & VARF_UNDEFERR) ? var_Error : varNoError;
4060 if (nstr != Buf_GetAll(&v->val, NULL))
4061 Buf_Destroy(&v->val, TRUE);
4069 *-----------------------------------------------------------------------
4071 * Substitute for all variables in the given string in the given context
4072 * If flags & VARF_UNDEFERR, Parse_Error will be called when an undefined
4073 * variable is encountered.
4076 * var Named variable || NULL for all
4077 * str the string which to substitute
4078 * ctxt the context wherein to find variables
4079 * flags VARF_UNDEFERR if undefineds are an error
4080 * VARF_WANTRES if we actually want the result
4081 * VARF_ASSIGN if we are in a := assignment
4084 * The resulting string.
4087 * None. The old string must be freed by the caller
4088 *-----------------------------------------------------------------------
4091 Var_Subst(const char *var, const char *str, GNode *ctxt, int flags)
4093 Buffer buf; /* Buffer for forming things */
4094 char *val; /* Value to substitute for a variable */
4095 int length; /* Length of the variable invocation */
4096 Boolean trailingBslash; /* variable ends in \ */
4097 void *freeIt = NULL; /* Set if it should be freed */
4098 static Boolean errorReported; /* Set true if an error has already
4099 * been reported to prevent a plethora
4100 * of messages when recursing */
4103 errorReported = FALSE;
4104 trailingBslash = FALSE;
4107 if (*str == '\n' && trailingBslash)
4108 Buf_AddByte(&buf, ' ');
4109 if (var == NULL && (*str == '$') && (str[1] == '$')) {
4111 * A dollar sign may be escaped either with another dollar sign.
4112 * In such a case, we skip over the escape character and store the
4113 * dollar sign into the buffer directly.
4115 if (save_dollars && (flags & VARF_ASSIGN))
4116 Buf_AddByte(&buf, *str);
4118 Buf_AddByte(&buf, *str);
4120 } else if (*str != '$') {
4122 * Skip as many characters as possible -- either to the end of
4123 * the string or to the next dollar sign (variable invocation).
4127 for (cp = str++; *str != '$' && *str != '\0'; str++)
4129 Buf_AddBytes(&buf, str - cp, cp);
4134 if (str[1] == '\0') {
4135 /* A trailing $ is kind of a special case */
4136 Buf_AddByte(&buf, str[0]);
4139 } else if (str[1] != PROPEN && str[1] != BROPEN) {
4140 if (str[1] != *var || strlen(var) > 1) {
4141 Buf_AddBytes(&buf, 2, str);
4153 * Scan up to the end of the variable name.
4155 for (p = &str[2]; *p &&
4156 *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++)
4160 * A variable inside the variable. We cannot expand
4161 * the external variable yet, so we try again with
4165 Buf_AddBytes(&buf, p - str, str);
4170 if (strncmp(var, str + 2, p - str - 2) != 0 ||
4171 var[p - str - 2] != '\0') {
4173 * Not the variable we want to expand, scan
4174 * until the next variable
4176 for (;*p != '$' && *p != '\0'; p++)
4178 Buf_AddBytes(&buf, p - str, str);
4191 val = Var_Parse(str, ctxt, flags, &length, &freeIt);
4194 * When we come down here, val should either point to the
4195 * value of this variable, suitably modified, or be NULL.
4196 * Length should be the total length of the potential
4197 * variable invocation (from $ to end character...)
4199 if (val == var_Error || val == varNoError) {
4201 * If performing old-time variable substitution, skip over
4202 * the variable and continue with the substitution. Otherwise,
4203 * store the dollar sign and advance str so we continue with
4208 } else if ((flags & VARF_UNDEFERR) || val == var_Error) {
4210 * If variable is undefined, complain and skip the
4211 * variable. The complaint will stop us from doing anything
4212 * when the file is parsed.
4214 if (!errorReported) {
4215 Parse_Error(PARSE_FATAL,
4216 "Undefined variable \"%.*s\"",length,str);
4219 errorReported = TRUE;
4221 Buf_AddByte(&buf, *str);
4226 * We've now got a variable structure to store in. But first,
4227 * advance the string pointer.
4232 * Copy all the characters from the variable value straight
4233 * into the new string.
4235 length = strlen(val);
4236 Buf_AddBytes(&buf, length, val);
4237 trailingBslash = length > 0 && val[length - 1] == '\\';
4244 return Buf_DestroyCompact(&buf);
4248 *-----------------------------------------------------------------------
4250 * Return the tail from each of a list of words. Used to set the
4251 * System V local variables.
4254 * file Filename to modify
4257 * The resulting string.
4262 *-----------------------------------------------------------------------
4266 Var_GetTail(char *file)
4268 return(VarModify(file, VarTail, NULL));
4272 *-----------------------------------------------------------------------
4274 * Find the leading components of a (list of) filename(s).
4275 * XXX: VarHead does not replace foo by ., as (sun) System V make
4279 * file Filename to manipulate
4282 * The leading components.
4287 *-----------------------------------------------------------------------
4290 Var_GetHead(char *file)
4292 return(VarModify(file, VarHead, NULL));
4297 *-----------------------------------------------------------------------
4299 * Initialize the module
4305 * The VAR_CMD and VAR_GLOBAL contexts are created
4306 *-----------------------------------------------------------------------
4311 VAR_INTERNAL = Targ_NewGN("Internal");
4312 VAR_GLOBAL = Targ_NewGN("Global");
4313 VAR_CMD = Targ_NewGN("Command");
4324 /****************** PRINT DEBUGGING INFO *****************/
4326 VarPrintVar(void *vp)
4329 fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
4333 *-----------------------------------------------------------------------
4335 * print all variables in a context
4336 *-----------------------------------------------------------------------
4339 Var_Dump(GNode *ctxt)
4344 for (h = Hash_EnumFirst(&ctxt->context, &search);
4346 h = Hash_EnumNext(&search)) {
4347 VarPrintVar(Hash_GetValue(h));