1 /* $NetBSD: var.c,v 1.221 2018/12/21 05:50:19 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.221 2018/12/21 05:50:19 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.221 2018/12/21 05:50:19 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 *, Boolean);
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;
839 setenv(MAKE_LEVEL_ENV, cp, 1);
841 for (; *str != '\n' && isspace((unsigned char) *str); str++)
843 if (str[0] && str[0] != '\n') {
849 /* Using .MAKE.EXPORTED */
850 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
851 if (n < (int)sizeof(tmp)) {
852 vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
862 av = brk_string(vlist, &ac, FALSE, &as);
863 for (i = 0; i < ac; i++) {
864 v = VarFind(av[i], VAR_GLOBAL, 0);
868 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
871 v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
873 * If we are unexporting a list,
874 * remove each one from .MAKE.EXPORTED.
875 * If we are removing them all,
876 * just delete .MAKE.EXPORTED below.
879 n = snprintf(tmp, sizeof(tmp),
880 "${" MAKE_EXPORTED ":N%s}", v->name);
881 if (n < (int)sizeof(tmp)) {
882 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
883 Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0);
891 Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
898 *-----------------------------------------------------------------------
900 * Set the variable name to the value val in the given context.
903 * name name of variable to set
904 * val value to give to the variable
905 * ctxt context in which to set it
911 * If the variable doesn't yet exist, a new record is created for it.
912 * Else the old value is freed and the new one stuck in its place
915 * The variable is searched for only in its context before being
916 * created in that context. I.e. if the context is VAR_GLOBAL,
917 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
918 * VAR_CMD->context is searched. This is done to avoid the literally
919 * thousands of unnecessary strcmp's that used to be done to
920 * set, say, $(@) or $(<).
921 * If the context is VAR_GLOBAL though, we check if the variable
922 * was set in VAR_CMD from the command line and skip it if so.
923 *-----------------------------------------------------------------------
926 Var_Set(const char *name, const char *val, GNode *ctxt, int flags)
929 char *expanded_name = NULL;
932 * We only look for a variable in the given context since anything set
933 * here will override anything in a lower context, so there's not much
934 * point in searching them all just to save a bit of memory...
936 if (strchr(name, '$') != NULL) {
937 expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
938 if (expanded_name[0] == 0) {
940 fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
941 "name expands to empty string - ignored\n",
947 name = expanded_name;
949 if (ctxt == VAR_GLOBAL) {
950 v = VarFind(name, VAR_CMD, 0);
952 if ((v->flags & VAR_FROM_CMD)) {
954 fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val);
961 v = VarFind(name, ctxt, 0);
963 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
965 * This var would normally prevent the same name being added
966 * to VAR_GLOBAL, so delete it from there if needed.
967 * Otherwise -V name may show the wrong value.
969 Var_Delete(name, VAR_GLOBAL);
971 VarAdd(name, val, ctxt);
975 Buf_AddBytes(&v->val, strlen(val), val);
978 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
980 if ((v->flags & VAR_EXPORTED)) {
981 Var_Export1(name, VAR_EXPORT_PARENT);
985 * Any variables given on the command line are automatically exported
986 * to the environment (as per POSIX standard)
988 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
990 /* we just added it */
991 v = VarFind(name, ctxt, 0);
994 v->flags |= VAR_FROM_CMD;
996 * If requested, don't export these in the environment
997 * individually. We still put them in MAKEOVERRIDES so
998 * that the command-line settings continue to override
1001 if (varNoExportEnv != TRUE)
1002 setenv(name, val ? val : "", 1);
1004 Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
1007 if (strcmp(name, SAVE_DOLLARS) == 0)
1008 save_dollars = s2Boolean(val, save_dollars);
1012 free(expanded_name);
1014 VarFreeEnv(v, TRUE);
1018 *-----------------------------------------------------------------------
1020 * The variable of the given name has the given value appended to it in
1021 * the given context.
1024 * name name of variable to modify
1025 * val String to append to it
1026 * ctxt Context in which this should occur
1032 * If the variable doesn't exist, it is created. Else the strings
1033 * are concatenated (with a space in between).
1036 * Only if the variable is being sought in the global context is the
1037 * environment searched.
1038 * XXX: Knows its calling circumstances in that if called with ctxt
1039 * an actual target, it will only search that context since only
1040 * a local variable could be being appended to. This is actually
1041 * a big win and must be tolerated.
1042 *-----------------------------------------------------------------------
1045 Var_Append(const char *name, const char *val, GNode *ctxt)
1049 char *expanded_name = NULL;
1051 if (strchr(name, '$') != NULL) {
1052 expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1053 if (expanded_name[0] == 0) {
1055 fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
1056 "name expands to empty string - ignored\n",
1059 free(expanded_name);
1062 name = expanded_name;
1065 v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? (FIND_CMD|FIND_ENV) : 0);
1068 Var_Set(name, val, ctxt, 0);
1069 } else if (ctxt == VAR_CMD || !(v->flags & VAR_FROM_CMD)) {
1070 Buf_AddByte(&v->val, ' ');
1071 Buf_AddBytes(&v->val, strlen(val), val);
1074 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
1075 Buf_GetAll(&v->val, NULL));
1078 if (v->flags & VAR_FROM_ENV) {
1080 * If the original variable came from the environment, we
1081 * have to install it in the global context (we could place
1082 * it in the environment, but then we should provide a way to
1083 * export other variables...)
1085 v->flags &= ~VAR_FROM_ENV;
1086 h = Hash_CreateEntry(&ctxt->context, name, NULL);
1087 Hash_SetValue(h, v);
1090 free(expanded_name);
1094 *-----------------------------------------------------------------------
1096 * See if the given variable exists.
1099 * name Variable to find
1100 * ctxt Context in which to start search
1103 * TRUE if it does, FALSE if it doesn't
1108 *-----------------------------------------------------------------------
1111 Var_Exists(const char *name, GNode *ctxt)
1116 if ((cp = strchr(name, '$')) != NULL) {
1117 cp = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1119 v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
1124 (void)VarFreeEnv(v, TRUE);
1130 *-----------------------------------------------------------------------
1132 * Return the value of the named variable in the given context
1136 * ctxt context in which to search for it
1139 * The value if the variable exists, NULL if it doesn't
1143 *-----------------------------------------------------------------------
1146 Var_Value(const char *name, GNode *ctxt, char **frp)
1150 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1153 char *p = (Buf_GetAll(&v->val, NULL));
1154 if (VarFreeEnv(v, FALSE))
1163 *-----------------------------------------------------------------------
1165 * Remove the tail 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 sticking in the head
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 VarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1185 char *word, Boolean addSpace, Buffer *buf,
1186 void *dummy MAKE_ATTR_UNUSED)
1190 slash = strrchr(word, '/');
1191 if (slash != NULL) {
1192 if (addSpace && vpstate->varSpace) {
1193 Buf_AddByte(buf, vpstate->varSpace);
1196 Buf_AddBytes(buf, strlen(word), word);
1201 * If no directory part, give . (q.v. the POSIX standard)
1203 if (addSpace && vpstate->varSpace)
1204 Buf_AddByte(buf, vpstate->varSpace);
1205 Buf_AddByte(buf, '.');
1211 *-----------------------------------------------------------------------
1213 * Remove the head of the given word and place the result in the given
1218 * addSpace True if need to add a space to the buffer
1219 * before adding the tail
1220 * buf Buffer in which to store it
1223 * TRUE if characters were added to the buffer (a space needs to be
1224 * added to the buffer before the next word).
1227 * The trimmed word is added to the buffer.
1229 *-----------------------------------------------------------------------
1232 VarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1233 char *word, Boolean addSpace, Buffer *buf,
1234 void *dummy MAKE_ATTR_UNUSED)
1238 if (addSpace && vpstate->varSpace) {
1239 Buf_AddByte(buf, vpstate->varSpace);
1242 slash = strrchr(word, '/');
1243 if (slash != NULL) {
1245 Buf_AddBytes(buf, strlen(slash), slash);
1248 Buf_AddBytes(buf, strlen(word), word);
1254 *-----------------------------------------------------------------------
1256 * Place the suffix of the given word in the given buffer.
1260 * addSpace TRUE if need to add a space before placing the
1261 * suffix in the buffer
1262 * buf Buffer in which to store it
1265 * TRUE if characters were added to the buffer (a space needs to be
1266 * added to the buffer before the next word).
1269 * The suffix from the word is placed in the buffer.
1271 *-----------------------------------------------------------------------
1274 VarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1275 char *word, Boolean addSpace, Buffer *buf,
1276 void *dummy MAKE_ATTR_UNUSED)
1280 dot = strrchr(word, '.');
1282 if (addSpace && vpstate->varSpace) {
1283 Buf_AddByte(buf, vpstate->varSpace);
1286 Buf_AddBytes(buf, strlen(dot), dot);
1294 *-----------------------------------------------------------------------
1296 * Remove the suffix of the given word and place the result in the
1301 * addSpace TRUE if need to add a space to the buffer
1302 * before placing the root in it
1303 * buf Buffer in which to store it
1306 * TRUE if characters were added to the buffer (a space needs to be
1307 * added to the buffer before the next word).
1310 * The trimmed word is added to the buffer.
1312 *-----------------------------------------------------------------------
1315 VarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1316 char *word, Boolean addSpace, Buffer *buf,
1317 void *dummy MAKE_ATTR_UNUSED)
1321 if (addSpace && vpstate->varSpace) {
1322 Buf_AddByte(buf, vpstate->varSpace);
1325 dot = strrchr(word, '.');
1328 Buf_AddBytes(buf, strlen(word), word);
1331 Buf_AddBytes(buf, strlen(word), word);
1337 *-----------------------------------------------------------------------
1339 * Place the word in the buffer if it matches the given pattern.
1340 * Callback function for VarModify to implement the :M modifier.
1343 * word Word to examine
1344 * addSpace TRUE if need to add a space to the buffer
1345 * before adding the word, if it matches
1346 * buf Buffer in which to store it
1347 * pattern Pattern the word must match
1350 * TRUE if a space should be placed in the buffer before the next
1354 * The word may be copied to the buffer.
1356 *-----------------------------------------------------------------------
1359 VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1360 char *word, Boolean addSpace, Buffer *buf,
1364 fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern);
1365 if (Str_Match(word, (char *)pattern)) {
1366 if (addSpace && vpstate->varSpace) {
1367 Buf_AddByte(buf, vpstate->varSpace);
1370 Buf_AddBytes(buf, strlen(word), word);
1377 *-----------------------------------------------------------------------
1379 * Place the word in the buffer if it matches the given pattern.
1380 * Callback function for VarModify to implement the System V %
1384 * word Word to examine
1385 * addSpace TRUE if need to add a space to the buffer
1386 * before adding the word, if it matches
1387 * buf Buffer in which to store it
1388 * patp Pattern the word must match
1391 * TRUE if a space should be placed in the buffer before the next
1395 * The word may be copied to the buffer.
1397 *-----------------------------------------------------------------------
1400 VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
1401 char *word, Boolean addSpace, Buffer *buf,
1406 VarPattern *pat = (VarPattern *)patp;
1409 if (addSpace && vpstate->varSpace)
1410 Buf_AddByte(buf, vpstate->varSpace);
1414 if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) {
1415 varexp = Var_Subst(NULL, pat->rhs, ctx, VARF_WANTRES);
1416 Str_SYSVSubst(buf, varexp, ptr, len);
1419 Buf_AddBytes(buf, strlen(word), word);
1428 *-----------------------------------------------------------------------
1430 * Place the word in the buffer if it doesn't match the given pattern.
1431 * Callback function for VarModify to implement the :N modifier.
1434 * word Word to examine
1435 * addSpace TRUE if need to add a space to the buffer
1436 * before adding the word, if it matches
1437 * buf Buffer in which to store it
1438 * pattern Pattern the word must match
1441 * TRUE if a space should be placed in the buffer before the next
1445 * The word may be copied to the buffer.
1447 *-----------------------------------------------------------------------
1450 VarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1451 char *word, Boolean addSpace, Buffer *buf,
1454 if (!Str_Match(word, (char *)pattern)) {
1455 if (addSpace && vpstate->varSpace) {
1456 Buf_AddByte(buf, vpstate->varSpace);
1459 Buf_AddBytes(buf, strlen(word), word);
1466 *-----------------------------------------------------------------------
1468 * Perform a string-substitution on the given word, placing the
1469 * result in the passed buffer.
1472 * word Word to modify
1473 * addSpace True if space should be added before
1475 * buf Buffer for result
1476 * patternp Pattern for substitution
1479 * TRUE if a space is needed before more characters are added.
1484 *-----------------------------------------------------------------------
1487 VarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1488 char *word, Boolean addSpace, Buffer *buf,
1491 int wordLen; /* Length of word */
1492 char *cp; /* General pointer */
1493 VarPattern *pattern = (VarPattern *)patternp;
1495 wordLen = strlen(word);
1496 if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
1497 (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1499 * Still substituting -- break it down into simple anchored cases
1500 * and if none of them fits, perform the general substitution case.
1502 if ((pattern->flags & VAR_MATCH_START) &&
1503 (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
1505 * Anchored at start and beginning of word matches pattern
1507 if ((pattern->flags & VAR_MATCH_END) &&
1508 (wordLen == pattern->leftLen)) {
1510 * Also anchored at end and matches to the end (word
1511 * is same length as pattern) add space and rhs only
1512 * if rhs is non-null.
1514 if (pattern->rightLen != 0) {
1515 if (addSpace && vpstate->varSpace) {
1516 Buf_AddByte(buf, vpstate->varSpace);
1519 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1521 pattern->flags |= VAR_SUB_MATCHED;
1522 } else if (pattern->flags & VAR_MATCH_END) {
1524 * Doesn't match to end -- copy word wholesale
1529 * Matches at start but need to copy in trailing characters
1531 if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
1532 if (addSpace && vpstate->varSpace) {
1533 Buf_AddByte(buf, vpstate->varSpace);
1537 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1538 Buf_AddBytes(buf, wordLen - pattern->leftLen,
1539 (word + pattern->leftLen));
1540 pattern->flags |= VAR_SUB_MATCHED;
1542 } else if (pattern->flags & VAR_MATCH_START) {
1544 * Had to match at start of word and didn't -- copy whole word.
1547 } else if (pattern->flags & VAR_MATCH_END) {
1549 * Anchored at end, Find only place match could occur (leftLen
1550 * characters from the end of the word) and see if it does. Note
1551 * that because the $ will be left at the end of the lhs, we have
1554 cp = word + (wordLen - pattern->leftLen);
1556 (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
1558 * Match found. If we will place characters in the buffer,
1559 * add a space before hand as indicated by addSpace, then
1560 * stuff in the initial, unmatched part of the word followed
1561 * by the right-hand-side.
1563 if (((cp - word) + pattern->rightLen) != 0) {
1564 if (addSpace && vpstate->varSpace) {
1565 Buf_AddByte(buf, vpstate->varSpace);
1569 Buf_AddBytes(buf, cp - word, word);
1570 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1571 pattern->flags |= VAR_SUB_MATCHED;
1574 * Had to match at end and didn't. Copy entire word.
1580 * Pattern is unanchored: search for the pattern in the word using
1581 * String_FindSubstring, copying unmatched portions and the
1582 * right-hand-side for each match found, handling non-global
1583 * substitutions correctly, etc. When the loop is done, any
1584 * remaining part of the word (word and wordLen are adjusted
1585 * accordingly through the loop) is copied straight into the
1587 * addSpace is set FALSE as soon as a space is added to the
1594 origSize = Buf_Size(buf);
1596 cp = Str_FindSubstring(word, pattern->lhs);
1598 if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
1599 Buf_AddByte(buf, vpstate->varSpace);
1602 Buf_AddBytes(buf, cp-word, word);
1603 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1604 wordLen -= (cp - word) + pattern->leftLen;
1605 word = cp + pattern->leftLen;
1609 if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
1612 pattern->flags |= VAR_SUB_MATCHED;
1618 if (addSpace && vpstate->varSpace) {
1619 Buf_AddByte(buf, vpstate->varSpace);
1621 Buf_AddBytes(buf, wordLen, word);
1624 * If added characters to the buffer, need to add a space
1625 * before we add any more. If we didn't add any, just return
1626 * the previous value of addSpace.
1628 return ((Buf_Size(buf) != origSize) || addSpace);
1633 if (addSpace && vpstate->varSpace) {
1634 Buf_AddByte(buf, vpstate->varSpace);
1636 Buf_AddBytes(buf, wordLen, word);
1642 *-----------------------------------------------------------------------
1644 * Print the error caused by a regcomp or regexec call.
1650 * An error gets printed.
1652 *-----------------------------------------------------------------------
1655 VarREError(int reerr, regex_t *pat, const char *str)
1660 errlen = regerror(reerr, pat, 0, 0);
1661 errbuf = bmake_malloc(errlen);
1662 regerror(reerr, pat, errbuf, errlen);
1663 Error("%s: %s", str, errbuf);
1669 *-----------------------------------------------------------------------
1670 * VarRESubstitute --
1671 * Perform a regex substitution on the given word, placing the
1672 * result in the passed buffer.
1675 * TRUE if a space is needed before more characters are added.
1680 *-----------------------------------------------------------------------
1683 VarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED,
1684 Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1685 char *word, Boolean addSpace, Buffer *buf,
1695 #define MAYBE_ADD_SPACE() \
1696 if (addSpace && !added) \
1697 Buf_AddByte(buf, ' '); \
1704 if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
1705 (VAR_SUB_ONE|VAR_SUB_MATCHED))
1709 xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
1714 pat->flags |= VAR_SUB_MATCHED;
1715 if (pat->matches[0].rm_so > 0) {
1717 Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
1720 for (rp = pat->replace; *rp; rp++) {
1721 if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
1723 Buf_AddByte(buf,rp[1]);
1726 else if ((*rp == '&') ||
1727 ((*rp == '\\') && isdigit((unsigned char)rp[1]))) {
1745 if (n > pat->nsub) {
1746 Error("No subexpression %s", &errstr[0]);
1749 } else if ((pat->matches[n].rm_so == -1) &&
1750 (pat->matches[n].rm_eo == -1)) {
1751 Error("No match for subexpression %s", &errstr[0]);
1755 subbuf = wp + pat->matches[n].rm_so;
1756 sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
1761 Buf_AddBytes(buf, sublen, subbuf);
1765 Buf_AddByte(buf, *rp);
1768 wp += pat->matches[0].rm_eo;
1769 if (pat->flags & VAR_SUB_GLOBAL) {
1770 flags |= REG_NOTBOL;
1771 if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
1773 Buf_AddByte(buf, *wp);
1782 Buf_AddBytes(buf, strlen(wp), wp);
1786 VarREError(xrv, &pat->re, "Unexpected regex error");
1791 Buf_AddBytes(buf,strlen(wp),wp);
1795 return(addSpace||added);
1802 *-----------------------------------------------------------------------
1804 * Implements the :@<temp>@<string>@ modifier of ODE make.
1805 * We set the temp variable named in pattern.lhs to word and expand
1806 * pattern.rhs storing the result in the passed buffer.
1809 * word Word to modify
1810 * addSpace True if space should be added before
1812 * buf Buffer for result
1813 * pattern Datafor substitution
1816 * TRUE if a space is needed before more characters are added.
1821 *-----------------------------------------------------------------------
1824 VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED,
1825 Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1826 char *word, Boolean addSpace, Buffer *buf,
1829 VarLoop_t *loop = (VarLoop_t *)loopp;
1833 if (word && *word) {
1834 Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
1835 s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum | VARF_WANTRES);
1836 if (s != NULL && *s != '\0') {
1837 if (addSpace && *s != '\n')
1838 Buf_AddByte(buf, ' ');
1839 Buf_AddBytes(buf, (slen = strlen(s)), s);
1840 addSpace = (slen > 0 && s[slen - 1] != '\n');
1849 *-----------------------------------------------------------------------
1851 * Implements the :[start..end] modifier.
1852 * This is a special case of VarModify since we want to be able
1853 * to scan the list backwards if start > end.
1856 * str String whose words should be trimmed
1857 * seldata words to select
1860 * A string of all the words selected.
1865 *-----------------------------------------------------------------------
1868 VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1869 const char *str, VarSelectWords_t *seldata)
1871 Buffer buf; /* Buffer for the new string */
1872 Boolean addSpace; /* TRUE if need to add a space to the
1873 * buffer before adding the trimmed
1875 char **av; /* word list */
1876 char *as; /* word list memory */
1878 int start, end, step;
1883 if (vpstate->oneBigWord) {
1884 /* fake what brk_string() would do if there were only one word */
1886 av = bmake_malloc((ac + 1) * sizeof(char *));
1887 as = bmake_strdup(str);
1891 av = brk_string(str, &ac, FALSE, &as);
1895 * Now sanitize seldata.
1896 * If seldata->start or seldata->end are negative, convert them to
1897 * the positive equivalents (-1 gets converted to argc, -2 gets
1898 * converted to (argc-1), etc.).
1900 if (seldata->start < 0)
1901 seldata->start = ac + seldata->start + 1;
1902 if (seldata->end < 0)
1903 seldata->end = ac + seldata->end + 1;
1906 * We avoid scanning more of the list than we need to.
1908 if (seldata->start > seldata->end) {
1909 start = MIN(ac, seldata->start) - 1;
1910 end = MAX(0, seldata->end - 1);
1913 start = MAX(0, seldata->start - 1);
1914 end = MIN(ac, seldata->end);
1919 (step < 0 && i >= end) || (step > 0 && i < end);
1921 if (av[i] && *av[i]) {
1922 if (addSpace && vpstate->varSpace) {
1923 Buf_AddByte(&buf, vpstate->varSpace);
1925 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1933 return Buf_Destroy(&buf, FALSE);
1939 * Replace each word with the result of realpath()
1943 VarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1944 char *word, Boolean addSpace, Buffer *buf,
1945 void *patternp MAKE_ATTR_UNUSED)
1948 char rbuf[MAXPATHLEN];
1951 if (addSpace && vpstate->varSpace) {
1952 Buf_AddByte(buf, vpstate->varSpace);
1955 rp = cached_realpath(word, rbuf);
1956 if (rp && *rp == '/' && stat(rp, &st) == 0)
1959 Buf_AddBytes(buf, strlen(word), word);
1964 *-----------------------------------------------------------------------
1966 * Modify each of the words of the passed string using the given
1967 * function. Used to implement all modifiers.
1970 * str String whose words should be trimmed
1971 * modProc Function to use to modify them
1972 * datum Datum to pass it
1975 * A string of all the words modified appropriately.
1980 *-----------------------------------------------------------------------
1983 VarModify(GNode *ctx, Var_Parse_State *vpstate,
1985 Boolean (*modProc)(GNode *, Var_Parse_State *, char *,
1986 Boolean, Buffer *, void *),
1989 Buffer buf; /* Buffer for the new string */
1990 Boolean addSpace; /* TRUE if need to add a space to the
1991 * buffer before adding the trimmed
1993 char **av; /* word list */
1994 char *as; /* word list memory */
2000 if (vpstate->oneBigWord) {
2001 /* fake what brk_string() would do if there were only one word */
2003 av = bmake_malloc((ac + 1) * sizeof(char *));
2004 as = bmake_strdup(str);
2008 av = brk_string(str, &ac, FALSE, &as);
2011 for (i = 0; i < ac; i++) {
2012 addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum);
2018 return Buf_Destroy(&buf, FALSE);
2023 VarWordCompare(const void *a, const void *b)
2025 int r = strcmp(*(const char * const *)a, *(const char * const *)b);
2030 *-----------------------------------------------------------------------
2032 * Order the words in the string.
2035 * str String whose words should be sorted.
2036 * otype How to order: s - sort, x - random.
2039 * A string containing the words ordered.
2044 *-----------------------------------------------------------------------
2047 VarOrder(const char *str, const char otype)
2049 Buffer buf; /* Buffer for the new string */
2050 char **av; /* word list [first word does not count] */
2051 char *as; /* word list memory */
2056 av = brk_string(str, &ac, FALSE, &as);
2060 case 's': /* sort alphabetically */
2061 qsort(av, ac, sizeof(char *), VarWordCompare);
2063 case 'x': /* randomize */
2069 * We will use [ac..2] range for mod factors. This will produce
2070 * random numbers in [(ac-1)..0] interval, and minimal
2071 * reasonable value for mod factor is 2 (the mod 1 will produce
2072 * 0 with probability 1).
2074 for (i = ac-1; i > 0; i--) {
2075 rndidx = random() % (i + 1);
2083 } /* end of switch */
2085 for (i = 0; i < ac; i++) {
2086 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
2088 Buf_AddByte(&buf, ' ');
2094 return Buf_Destroy(&buf, FALSE);
2099 *-----------------------------------------------------------------------
2101 * Remove adjacent duplicate words.
2104 * str String whose words should be sorted
2107 * A string containing the resulting words.
2112 *-----------------------------------------------------------------------
2115 VarUniq(const char *str)
2117 Buffer buf; /* Buffer for new string */
2118 char **av; /* List of words to affect */
2119 char *as; /* Word list memory */
2123 av = brk_string(str, &ac, FALSE, &as);
2126 for (j = 0, i = 1; i < ac; i++)
2127 if (strcmp(av[i], av[j]) != 0 && (++j != i))
2132 for (i = 0; i < ac; i++) {
2133 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
2135 Buf_AddByte(&buf, ' ');
2141 return Buf_Destroy(&buf, FALSE);
2145 *-----------------------------------------------------------------------
2147 * Return an integer sequence
2150 * str String whose words provide default range
2151 * ac range length, if 0 use str words
2156 *-----------------------------------------------------------------------
2159 VarRange(const char *str, int ac)
2161 Buffer buf; /* Buffer for new string */
2162 char tmp[32]; /* each element */
2163 char **av; /* List of words to affect */
2164 char *as; /* Word list memory */
2172 av = brk_string(str, &ac, FALSE, &as);
2174 for (i = 0; i < ac; i++) {
2175 n = snprintf(tmp, sizeof(tmp), "%d", 1 + i);
2176 if (n >= (int)sizeof(tmp))
2178 Buf_AddBytes(&buf, n, tmp);
2180 Buf_AddByte(&buf, ' ');
2186 return Buf_Destroy(&buf, FALSE);
2191 *-----------------------------------------------------------------------
2193 * Pass through the tstr looking for 1) escaped delimiters,
2194 * '$'s and backslashes (place the escaped character in
2195 * uninterpreted) and 2) unescaped $'s that aren't before
2196 * the delimiter (expand the variable substitution unless flags
2197 * has VAR_NOSUBST set).
2198 * Return the expanded string or NULL if the delimiter was missing
2199 * If pattern is specified, handle escaped ampersands, and replace
2200 * unescaped ampersands with the lhs of the pattern.
2203 * A string of all the words modified appropriately.
2204 * If length is specified, return the string length of the buffer
2205 * If flags is specified and the last character of the pattern is a
2206 * $ set the VAR_MATCH_END bit of flags.
2210 *-----------------------------------------------------------------------
2213 VarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
2214 int flags, const char **tstr, int delim, int *vflags,
2215 int *length, VarPattern *pattern)
2221 int errnum = flags & VARF_UNDEFERR;
2227 #define IS_A_MATCH(cp, delim) \
2228 ((cp[0] == '\\') && ((cp[1] == delim) || \
2229 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
2232 * Skim through until the matching delimiter is found;
2233 * pick up variable substitutions on the way. Also allow
2234 * backslashes to quote the delimiter, $, and \, but don't
2235 * touch other backslashes.
2237 for (cp = *tstr; *cp && (*cp != delim); cp++) {
2238 if (IS_A_MATCH(cp, delim)) {
2239 Buf_AddByte(&buf, cp[1]);
2241 } else if (*cp == '$') {
2242 if (cp[1] == delim) {
2244 Buf_AddByte(&buf, *cp);
2247 * Unescaped $ at end of pattern => anchor
2250 *vflags |= VAR_MATCH_END;
2252 if (vflags == NULL || (*vflags & VAR_NOSUBST) == 0) {
2258 * If unescaped dollar sign not before the
2259 * delimiter, assume it's a variable
2260 * substitution and recurse.
2262 cp2 = Var_Parse(cp, ctxt, errnum | VARF_WANTRES, &len,
2264 Buf_AddBytes(&buf, strlen(cp2), cp2);
2268 const char *cp2 = &cp[1];
2270 if (*cp2 == PROPEN || *cp2 == BROPEN) {
2272 * Find the end of this variable reference
2273 * and suck it in without further ado.
2274 * It will be interperated later.
2277 int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE;
2280 for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
2281 if (cp2[-1] != '\\') {
2288 Buf_AddBytes(&buf, cp2 - cp, cp);
2291 Buf_AddByte(&buf, *cp);
2295 else if (pattern && *cp == '&')
2296 Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs);
2298 Buf_AddByte(&buf, *cp);
2308 *length = Buf_Size(&buf);
2309 rstr = Buf_Destroy(&buf, FALSE);
2311 fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr);
2316 *-----------------------------------------------------------------------
2318 * Quote shell meta-characters and space characters in the string
2319 * if quoteDollar is set, also quote and double any '$' characters.
2327 *-----------------------------------------------------------------------
2330 VarQuote(char *str, Boolean quoteDollar)
2334 const char *newline;
2337 if ((newline = Shell_GetNewline()) == NULL)
2339 nlen = strlen(newline);
2343 for (; *str != '\0'; str++) {
2345 Buf_AddBytes(&buf, nlen, newline);
2348 if (isspace((unsigned char)*str) || ismeta((unsigned char)*str))
2349 Buf_AddByte(&buf, '\\');
2350 Buf_AddByte(&buf, *str);
2351 if (quoteDollar && *str == '$')
2352 Buf_AddBytes(&buf, 2, "\\$");
2355 str = Buf_Destroy(&buf, FALSE);
2357 fprintf(debug_file, "QuoteMeta: [%s]\n", str);
2362 *-----------------------------------------------------------------------
2364 * Hash the string using the MurmurHash3 algorithm.
2365 * Output is computed using 32bit Little Endian arithmetic.
2368 * str String to modify
2371 * Hash value of str, encoded as 8 hex digits.
2376 *-----------------------------------------------------------------------
2381 static const char hexdigits[16] = "0123456789abcdef";
2384 unsigned char *ustr = (unsigned char *)str;
2385 unsigned int h, k, c1, c2;
2392 for (len = len2; len; ) {
2396 k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0];
2401 k |= (ustr[2] << 16);
2403 k |= (ustr[1] << 8);
2408 c1 = c1 * 5 + 0x7b7d159cU;
2409 c2 = c2 * 5 + 0x6bce6396U;
2411 k = (k << 11) ^ (k >> 21);
2413 h = (h << 13) ^ (h >> 19);
2414 h = h * 5 + 0x52dce729U;
2424 for (len = 0; len < 8; ++len) {
2425 Buf_AddByte(&buf, hexdigits[h & 15]);
2429 return Buf_Destroy(&buf, FALSE);
2433 VarStrftime(const char *fmt, int zulu, time_t utc)
2441 strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc));
2443 buf[sizeof(buf) - 1] = '\0';
2444 return bmake_strdup(buf);
2448 * Now we need to apply any modifiers the user wants applied.
2450 * :M<pattern> words which match the given <pattern>.
2451 * <pattern> is of the standard file
2453 * :N<pattern> words which do not match the given <pattern>.
2454 * :S<d><pat1><d><pat2><d>[1gW]
2455 * Substitute <pat2> for <pat1> in the value
2456 * :C<d><pat1><d><pat2><d>[1gW]
2457 * Substitute <pat2> for regex <pat1> in the value
2458 * :H Substitute the head of each word
2459 * :T Substitute the tail of each word
2460 * :E Substitute the extension (minus '.') of
2462 * :R Substitute the root of each word
2463 * (pathname minus the suffix).
2464 * :O ("Order") Alphabeticaly sort words in variable.
2465 * :Ox ("intermiX") Randomize words in variable.
2466 * :u ("uniq") Remove adjacent duplicate words.
2467 * :tu Converts the variable contents to uppercase.
2468 * :tl Converts the variable contents to lowercase.
2469 * :ts[c] Sets varSpace - the char used to
2470 * separate words to 'c'. If 'c' is
2471 * omitted then no separation is used.
2472 * :tW Treat the variable contents as a single
2473 * word, even if it contains spaces.
2474 * (Mnemonic: one big 'W'ord.)
2475 * :tw Treat the variable contents as multiple
2476 * space-separated words.
2477 * (Mnemonic: many small 'w'ords.)
2478 * :[index] Select a single word from the value.
2479 * :[start..end] Select multiple words from the value.
2480 * :[*] or :[0] Select the entire value, as a single
2481 * word. Equivalent to :tW.
2482 * :[@] Select the entire value, as multiple
2483 * words. Undoes the effect of :[*].
2484 * Equivalent to :tw.
2485 * :[#] Returns the number of words in the value.
2487 * :?<true-value>:<false-value>
2488 * If the variable evaluates to true, return
2489 * true value, else return the second value.
2490 * :lhs=rhs Like :S, but the rhs goes to the end of
2492 * :sh Treat the current value as a command
2493 * to be run, new value is its output.
2494 * The following added so we can handle ODE makefiles.
2495 * :@<tmpvar>@<newval>@
2496 * Assign a temporary local variable <tmpvar>
2497 * to the current value of each word in turn
2498 * and replace each word with the result of
2499 * evaluating <newval>
2500 * :D<newval> Use <newval> as value if variable defined
2501 * :U<newval> Use <newval> as value if variable undefined
2502 * :L Use the name of the variable as the value.
2503 * :P Use the path of the node that has the same
2504 * name as the variable as the value. This
2505 * basically includes an implied :L so that
2506 * the common method of refering to the path
2507 * of your dependent 'x' in a rule is to use
2508 * the form '${x:P}'.
2509 * :!<cmd>! Run cmd much the same as :sh run's the
2510 * current value of the variable.
2511 * The ::= modifiers, actually assign a value to the variable.
2512 * Their main purpose is in supporting modifiers of .for loop
2513 * iterators and other obscure uses. They always expand to
2514 * nothing. In a target rule that would otherwise expand to an
2515 * empty line they can be preceded with @: to keep make happy.
2519 * .for i in ${.TARGET} ${.TARGET:R}.gz
2524 * ::=<str> Assigns <str> as the new value of variable.
2525 * ::?=<str> Assigns <str> as value of variable if
2526 * it was not already set.
2527 * ::+=<str> Appends <str> to variable.
2528 * ::!=<cmd> Assigns output of <cmd> as the new value of
2532 /* we now have some modifiers with long names */
2533 #define STRMOD_MATCH(s, want, n) \
2534 (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':'))
2535 #define STRMOD_MATCHX(s, want, n) \
2536 (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':' || s[n] == '='))
2537 #define CHARMOD_MATCH(c) (c == endc || c == ':')
2540 ApplyModifiers(char *nstr, const char *tstr,
2541 int startc, int endc,
2542 Var *v, GNode *ctxt, int flags,
2543 int *lengthPtr, void **freePtr)
2546 const char *cp; /* Secondary pointer into str (place marker
2548 char *newStr; /* New value to return */
2550 char termc; /* Character which terminated scan */
2551 int cnt; /* Used to count brace pairs when variable in
2552 * in parens or braces */
2554 int modifier; /* that we are processing */
2555 Var_Parse_State parsestate; /* Flags passed to helper functions */
2556 time_t utc; /* for VarStrftime */
2559 parsestate.oneBigWord = FALSE;
2560 parsestate.varSpace = ' '; /* word separator */
2564 while (*tstr && *tstr != endc) {
2568 * We may have some complex modifiers in a variable.
2575 rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
2578 * If we have not parsed up to endc or ':',
2579 * we are not interested.
2581 if (rval != NULL && *rval &&
2582 (c = tstr[rlen]) != '\0' &&
2590 fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n",
2591 rval, rlen, tstr, rlen, tstr + rlen);
2596 if (rval != NULL && *rval) {
2599 nstr = ApplyModifiers(nstr, rval,
2600 0, 0, v, ctxt, flags, &used, freePtr);
2601 if (nstr == var_Error
2602 || (nstr == varNoError && (flags & VARF_UNDEFERR) == 0)
2603 || strlen(rval) != (size_t) used) {
2605 goto out; /* error already reported */
2611 else if (!*tstr && endc) {
2612 Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name);
2619 fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name,
2623 switch ((modifier = *tstr)) {
2626 if (tstr[1] == '=' ||
2628 (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) {
2630 * "::=", "::!=", "::+=", or "::?="
2632 GNode *v_ctxt; /* context where v belongs */
2639 if (v->name[0] == 0)
2645 if (v->flags & VAR_JUNK) {
2647 * We need to bmake_strdup() it incase
2648 * VarGetPattern() recurses.
2651 v->name = bmake_strdup(v->name);
2652 } else if (ctxt != VAR_GLOBAL) {
2653 Var *gv = VarFind(v->name, ctxt, 0);
2655 v_ctxt = VAR_GLOBAL;
2657 VarFreeEnv(gv, TRUE);
2660 switch ((how = *tstr)) {
2670 delim = startc == PROPEN ? PRCLOSE : BRCLOSE;
2673 vflags = (flags & VARF_WANTRES) ? 0 : VAR_NOSUBST;
2674 pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
2675 &cp, delim, &vflags,
2678 if (v->flags & VAR_JUNK) {
2679 /* restore original name */
2683 if (pattern.rhs == NULL)
2689 if (flags & VARF_WANTRES) {
2692 Var_Append(v->name, pattern.rhs, v_ctxt);
2695 newStr = Cmd_Exec(pattern.rhs, &emsg);
2699 Var_Set(v->name, newStr, v_ctxt, 0);
2703 if ((v->flags & VAR_JUNK) == 0)
2707 Var_Set(v->name, pattern.rhs, v_ctxt, 0);
2711 free(UNCONST(pattern.rhs));
2712 newStr = varNoError;
2715 goto default_case; /* "::<unrecognised>" */
2720 int vflags = VAR_NOSUBST;
2724 if ((loop.tvar = VarGetPattern(ctxt, &parsestate, flags,
2726 &vflags, &loop.tvarLen,
2730 if ((loop.str = VarGetPattern(ctxt, &parsestate, flags,
2732 &vflags, &loop.strLen,
2739 loop.errnum = flags & VARF_UNDEFERR;
2741 newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand,
2743 Var_Delete(loop.tvar, ctxt);
2748 case '_': /* remember current value */
2749 cp = tstr + 1; /* make sure it is set */
2750 if (STRMOD_MATCHX(tstr, "_", 1)) {
2751 if (tstr[1] == '=') {
2756 n = strcspn(cp, ":)}");
2757 np = bmake_strndup(cp, n+1);
2760 Var_Set(np, nstr, ctxt, 0);
2763 Var_Set("_", nstr, ctxt, 0);
2773 Buffer buf; /* Buffer for patterns */
2776 if (flags & VARF_WANTRES) {
2779 wantres = ((v->flags & VAR_JUNK) != 0);
2781 wantres = ((v->flags & VAR_JUNK) == 0);
2782 nflags = flags & ~VARF_WANTRES;
2784 nflags |= VARF_WANTRES;
2788 * Pass through tstr looking for 1) escaped delimiters,
2789 * '$'s and backslashes (place the escaped character in
2790 * uninterpreted) and 2) unescaped $'s that aren't before
2791 * the delimiter (expand the variable substitution).
2792 * The result is left in the Buffer buf.
2796 *cp != endc && *cp != ':' && *cp != '\0';
2798 if ((*cp == '\\') &&
2804 Buf_AddByte(&buf, cp[1]);
2806 } else if (*cp == '$') {
2808 * If unescaped dollar sign, assume it's a
2809 * variable substitution and recurse.
2815 cp2 = Var_Parse(cp, ctxt, nflags, &len, &freeIt);
2816 Buf_AddBytes(&buf, strlen(cp2), cp2);
2820 Buf_AddByte(&buf, *cp);
2826 if ((v->flags & VAR_JUNK) != 0)
2827 v->flags |= VAR_KEEP;
2828 if (nflags & VARF_WANTRES) {
2829 newStr = Buf_Destroy(&buf, FALSE);
2832 Buf_Destroy(&buf, TRUE);
2838 if ((v->flags & VAR_JUNK) != 0)
2839 v->flags |= VAR_KEEP;
2840 newStr = bmake_strdup(v->name);
2849 if ((v->flags & VAR_JUNK) != 0)
2850 v->flags |= VAR_KEEP;
2851 gn = Targ_FindNode(v->name, TARG_NOCREATE);
2852 if (gn == NULL || gn->type & OP_NOPATH) {
2854 } else if (gn->path) {
2855 newStr = bmake_strdup(gn->path);
2857 newStr = Dir_FindFile(v->name, Suff_FindPath(gn));
2860 newStr = bmake_strdup(v->name);
2875 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
2877 NULL, &pattern.rightLen,
2880 if (flags & VARF_WANTRES)
2881 newStr = Cmd_Exec(pattern.rhs, &emsg);
2883 newStr = varNoError;
2884 free(UNCONST(pattern.rhs));
2889 if (v->flags & VAR_JUNK) {
2890 v->flags |= VAR_KEEP;
2897 * Look for the closing ']', recursively
2898 * expanding any embedded variables.
2900 * estr is a pointer to the expanded result,
2901 * which we must free().
2905 cp = tstr+1; /* point to char after '[' */
2906 delim = ']'; /* look for closing ']' */
2907 estr = VarGetPattern(ctxt, &parsestate,
2911 goto cleanup; /* report missing ']' */
2912 /* now cp points just after the closing ']' */
2914 if (cp[0] != ':' && cp[0] != endc) {
2915 /* Found junk after ']' */
2919 if (estr[0] == '\0') {
2920 /* Found empty square brackets in ":[]". */
2923 } else if (estr[0] == '#' && estr[1] == '\0') {
2927 * We will need enough space for the decimal
2928 * representation of an int. We calculate the
2929 * space needed for the octal representation,
2930 * and add enough slop to cope with a '-' sign
2931 * (which should never be needed) and a '\0'
2932 * string terminator.
2935 (sizeof(int) * CHAR_BIT + 2) / 3 + 2;
2937 newStr = bmake_malloc(newStrSize);
2938 if (parsestate.oneBigWord) {
2939 strncpy(newStr, "1", newStrSize);
2941 /* XXX: brk_string() is a rather expensive
2942 * way of counting words. */
2947 av = brk_string(nstr, &ac, FALSE, &as);
2948 snprintf(newStr, newStrSize, "%d", ac);
2955 } else if (estr[0] == '*' && estr[1] == '\0') {
2957 parsestate.oneBigWord = TRUE;
2962 } else if (estr[0] == '@' && estr[1] == '\0') {
2964 parsestate.oneBigWord = FALSE;
2971 * We expect estr to contain a single
2972 * integer for :[N], or two integers
2973 * separated by ".." for :[start..end].
2975 VarSelectWords_t seldata = { 0, 0 };
2977 seldata.start = strtol(estr, &ep, 0);
2979 /* Found junk instead of a number */
2982 } else if (ep[0] == '\0') {
2983 /* Found only one integer in :[N] */
2984 seldata.end = seldata.start;
2985 } else if (ep[0] == '.' && ep[1] == '.' &&
2987 /* Expecting another integer after ".." */
2989 seldata.end = strtol(ep, &ep, 0);
2990 if (ep[0] != '\0') {
2991 /* Found junk after ".." */
2996 /* Found junk instead of ".." */
3001 * Now seldata is properly filled in,
3002 * but we still have to check for 0 as
3005 if (seldata.start == 0 && seldata.end == 0) {
3006 /* ":[0]" or perhaps ":[0..0]" */
3007 parsestate.oneBigWord = TRUE;
3012 } else if (seldata.start == 0 ||
3014 /* ":[0..N]" or ":[N..0]" */
3019 * Normal case: select the words
3020 * described by seldata.
3022 newStr = VarSelectWords(ctxt, &parsestate,
3032 cp = tstr + 1; /* make sure it is set */
3033 if (STRMOD_MATCHX(tstr, "gmtime", 6)) {
3034 if (tstr[6] == '=') {
3035 utc = strtoul(&tstr[7], &ep, 10);
3041 newStr = VarStrftime(nstr, 1, utc);
3048 cp = tstr + 1; /* make sure it is set */
3049 if (STRMOD_MATCH(tstr, "hash", 4)) {
3050 newStr = VarHash(nstr);
3058 cp = tstr + 1; /* make sure it is set */
3059 if (STRMOD_MATCHX(tstr, "localtime", 9)) {
3060 if (tstr[9] == '=') {
3061 utc = strtoul(&tstr[10], &ep, 10);
3067 newStr = VarStrftime(nstr, 0, utc);
3075 cp = tstr + 1; /* make sure it is set */
3076 if (tstr[1] != endc && tstr[1] != ':') {
3077 if (tstr[1] == 's') {
3079 * Use the char (if any) at tstr[2]
3080 * as the word separator.
3084 if (tstr[2] != endc &&
3085 (tstr[3] == endc || tstr[3] == ':')) {
3086 /* ":ts<unrecognised><endc>" or
3087 * ":ts<unrecognised>:" */
3088 parsestate.varSpace = tstr[2];
3090 } else if (tstr[2] == endc || tstr[2] == ':') {
3091 /* ":ts<endc>" or ":ts:" */
3092 parsestate.varSpace = 0; /* no separator */
3094 } else if (tstr[2] == '\\') {
3095 const char *xp = &tstr[3];
3096 int base = 8; /* assume octal */
3100 parsestate.varSpace = '\n';
3104 parsestate.varSpace = '\t';
3115 if (isdigit((unsigned char)tstr[3])) {
3118 parsestate.varSpace =
3119 strtoul(xp, &ep, base);
3120 if (*ep != ':' && *ep != endc)
3125 * ":ts<backslash><unrecognised>".
3133 * Found ":ts<unrecognised><unrecognised>".
3141 * We cannot be certain that VarModify
3142 * will be used - even if there is a
3143 * subsequent modifier, so do a no-op
3144 * VarSubstitute now to for str to be
3145 * re-expanded without the spaces.
3147 pattern.flags = VAR_SUB_ONE;
3148 pattern.lhs = pattern.rhs = "\032";
3149 pattern.leftLen = pattern.rightLen = 1;
3151 newStr = VarModify(ctxt, &parsestate, nstr,
3154 } else if (tstr[2] == endc || tstr[2] == ':') {
3156 * Check for two-character options:
3159 if (tstr[1] == 'A') { /* absolute path */
3160 newStr = VarModify(ctxt, &parsestate, nstr,
3164 } else if (tstr[1] == 'u') {
3165 char *dp = bmake_strdup(nstr);
3166 for (newStr = dp; *dp; dp++)
3167 *dp = toupper((unsigned char)*dp);
3170 } else if (tstr[1] == 'l') {
3171 char *dp = bmake_strdup(nstr);
3172 for (newStr = dp; *dp; dp++)
3173 *dp = tolower((unsigned char)*dp);
3176 } else if (tstr[1] == 'W' || tstr[1] == 'w') {
3177 parsestate.oneBigWord = (tstr[1] == 'W');
3182 /* Found ":t<unrecognised>:" or
3183 * ":t<unrecognised><endc>". */
3188 * Found ":t<unrecognised><unrecognised>".
3194 * Found ":t<endc>" or ":t:".
3204 const char *endpat; /* points just after end of pattern */
3206 Boolean copy; /* pattern should be, or has been, copied */
3214 * In the loop below, ignore ':' unless we are at
3215 * (or back to) the original brace level.
3216 * XXX This will likely not work right if $() and ${}
3220 *cp != '\0' && !(*cp == ':' && nest == 1);
3225 cp[1] == endc || cp[1] == startc)) {
3235 if (*cp == '(' || *cp == '{')
3237 if (*cp == ')' || *cp == '}') {
3247 * Need to compress the \:'s out of the pattern, so
3248 * allocate enough room to hold the uncompressed
3249 * pattern (note that cp started at tstr+1, so
3250 * cp - tstr takes the null byte into account) and
3251 * compress the pattern into the space.
3253 pattern = bmake_malloc(cp - tstr);
3254 for (cp2 = pattern, cp = tstr + 1;
3258 if ((*cp == '\\') && (cp+1 < endpat) &&
3259 (cp[1] == ':' || cp[1] == endc)) {
3268 * Either Var_Subst or VarModify will need a
3269 * nul-terminated string soon, so construct one now.
3271 pattern = bmake_strndup(tstr+1, endpat - (tstr + 1));
3275 * pattern contains embedded '$', so use Var_Subst to
3279 pattern = Var_Subst(NULL, cp2, ctxt, flags | VARF_WANTRES);
3283 fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n",
3284 v->name, nstr, pattern);
3286 newStr = VarModify(ctxt, &parsestate, nstr, VarMatch,
3289 newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch,
3298 Var_Parse_State tmpparsestate;
3301 tmpparsestate = parsestate;
3306 * If pattern begins with '^', it is anchored to the
3307 * start of the word -- skip over it and flag pattern.
3310 pattern.flags |= VAR_MATCH_START;
3315 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags,
3322 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
3329 * Check for global substitution. If 'g' after the final
3330 * delimiter, substitution is global and is marked that
3336 pattern.flags |= VAR_SUB_GLOBAL;
3339 pattern.flags |= VAR_SUB_ONE;
3342 tmpparsestate.oneBigWord = TRUE;
3349 newStr = VarModify(ctxt, &tmpparsestate, nstr,
3354 * Free the two strings.
3356 free(UNCONST(pattern.lhs));
3357 free(UNCONST(pattern.rhs));
3366 int lhs_flags, rhs_flags;
3368 /* find ':', and then substitute accordingly */
3369 if (flags & VARF_WANTRES) {
3370 cond_rc = Cond_EvalExpression(NULL, v->name, &value, 0, FALSE);
3371 if (cond_rc == COND_INVALID) {
3372 lhs_flags = rhs_flags = VAR_NOSUBST;
3375 rhs_flags = VAR_NOSUBST;
3377 lhs_flags = VAR_NOSUBST;
3381 /* we are just consuming and discarding */
3382 cond_rc = value = 0;
3383 lhs_flags = rhs_flags = VAR_NOSUBST;
3389 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags,
3390 &cp, delim, &lhs_flags,
3395 /* BROPEN or PROPEN */
3397 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags,
3398 &cp, delim, &rhs_flags,
3405 if (cond_rc == COND_INVALID) {
3406 Error("Bad conditional expression `%s' in %s?%s:%s",
3407 v->name, v->name, pattern.lhs, pattern.rhs);
3412 newStr = UNCONST(pattern.lhs);
3413 free(UNCONST(pattern.rhs));
3415 newStr = UNCONST(pattern.rhs);
3416 free(UNCONST(pattern.lhs));
3418 if (v->flags & VAR_JUNK) {
3419 v->flags |= VAR_KEEP;
3426 VarREPattern pattern;
3429 Var_Parse_State tmpparsestate;
3432 tmpparsestate = parsestate;
3438 if ((re = VarGetPattern(ctxt, &parsestate, flags, &cp, delim,
3439 NULL, NULL, NULL)) == NULL)
3442 if ((pattern.replace = VarGetPattern(ctxt, &parsestate,
3443 flags, &cp, delim, NULL,
3444 NULL, NULL)) == NULL){
3452 pattern.flags |= VAR_SUB_GLOBAL;
3455 pattern.flags |= VAR_SUB_ONE;
3458 tmpparsestate.oneBigWord = TRUE;
3466 error = regcomp(&pattern.re, re, REG_EXTENDED);
3469 *lengthPtr = cp - start + 1;
3470 VarREError(error, &pattern.re, "RE substitution error");
3471 free(pattern.replace);
3475 pattern.nsub = pattern.re.re_nsub + 1;
3476 if (pattern.nsub < 1)
3478 if (pattern.nsub > 10)
3480 pattern.matches = bmake_malloc(pattern.nsub *
3481 sizeof(regmatch_t));
3482 newStr = VarModify(ctxt, &tmpparsestate, nstr,
3485 regfree(&pattern.re);
3486 free(pattern.replace);
3487 free(pattern.matches);
3494 if (tstr[1] == endc || tstr[1] == ':') {
3495 newStr = VarQuote(nstr, modifier == 'q');
3502 if (tstr[1] == endc || tstr[1] == ':') {
3503 newStr = VarModify(ctxt, &parsestate, nstr, VarTail,
3511 if (tstr[1] == endc || tstr[1] == ':') {
3512 newStr = VarModify(ctxt, &parsestate, nstr, VarHead,
3520 if (tstr[1] == endc || tstr[1] == ':') {
3521 newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix,
3529 if (tstr[1] == endc || tstr[1] == ':') {
3530 newStr = VarModify(ctxt, &parsestate, nstr, VarRoot,
3538 cp = tstr + 1; /* make sure it is set */
3539 if (STRMOD_MATCHX(tstr, "range", 5)) {
3542 if (tstr[5] == '=') {
3543 n = strtoul(&tstr[6], &ep, 10);
3549 newStr = VarRange(nstr, n);
3558 cp = tstr + 1; /* skip to the rest in any case */
3559 if (tstr[1] == endc || tstr[1] == ':') {
3562 } else if ( (tstr[1] == 'x') &&
3563 (tstr[2] == endc || tstr[2] == ':') ) {
3570 newStr = VarOrder(nstr, otype);
3574 if (tstr[1] == endc || tstr[1] == ':') {
3575 newStr = VarUniq(nstr);
3583 if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
3585 if (flags & VARF_WANTRES) {
3586 newStr = Cmd_Exec(nstr, &emsg);
3590 newStr = varNoError;
3602 * This can either be a bogus modifier or a System-V
3603 * substitution command.
3611 * First we make a pass through the string trying
3612 * to verify it is a SYSV-make-style translation:
3613 * it must be: <string1>=<string2>)
3617 while (*cp != '\0' && cnt) {
3620 /* continue looking for endc */
3622 else if (*cp == endc)
3624 else if (*cp == startc)
3629 if (*cp == endc && eqFound) {
3632 * Now we break this sucker into the lhs and
3633 * rhs. We must null terminate them of course.
3637 if ((pattern.lhs = VarGetPattern(ctxt, &parsestate,
3638 flags, &cp, delim, &pattern.flags,
3639 &pattern.leftLen, NULL)) == NULL)
3642 if ((pattern.rhs = VarGetPattern(ctxt, &parsestate,
3643 flags, &cp, delim, NULL, &pattern.rightLen,
3648 * SYSV modifications happen through the whole
3649 * string. Note the pattern is anchored at the end.
3653 if (pattern.leftLen == 0 && *nstr == '\0') {
3654 newStr = nstr; /* special case */
3656 newStr = VarModify(ctxt, &parsestate, nstr,
3660 free(UNCONST(pattern.lhs));
3661 free(UNCONST(pattern.rhs));
3665 Error("Unknown modifier '%c'", *tstr);
3667 *cp != ':' && *cp != endc && *cp != '\0';
3676 fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n",
3677 v->name, modifier, newStr);
3680 if (newStr != nstr) {
3686 if (nstr != var_Error && nstr != varNoError) {
3690 if (termc == '\0' && endc != '\0') {
3691 Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier);
3692 } else if (termc == ':') {
3698 *lengthPtr = tstr - start;
3703 Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr,
3707 *lengthPtr = cp - start;
3709 Error("Unclosed substitution for %s (%c missing)",
3717 *-----------------------------------------------------------------------
3719 * Given the start of a variable invocation, extract the variable
3720 * name and find its value, then modify it according to the
3724 * str The string to parse
3725 * ctxt The context for the variable
3726 * flags VARF_UNDEFERR if undefineds are an error
3727 * VARF_WANTRES if we actually want the result
3728 * VARF_ASSIGN if we are in a := assignment
3729 * lengthPtr OUT: The length of the specification
3730 * freePtr OUT: Non-NULL if caller should free *freePtr
3733 * The (possibly-modified) value of the variable or var_Error if the
3734 * specification is invalid. The length of the specification is
3735 * placed in *lengthPtr (for invalid specifications, this is just
3737 * If *freePtr is non-NULL then it's a pointer that the caller
3738 * should pass to free() to free memory used by the result.
3743 *-----------------------------------------------------------------------
3745 /* coverity[+alloc : arg-*4] */
3747 Var_Parse(const char *str, GNode *ctxt, int flags,
3748 int *lengthPtr, void **freePtr)
3750 const char *tstr; /* Pointer into str */
3751 Var *v; /* Variable in invocation */
3752 Boolean haveModifier;/* TRUE if have modifiers for the variable */
3753 char endc; /* Ending character when variable in parens
3755 char startc; /* Starting character when variable in parens
3757 int vlen; /* Length of variable name */
3758 const char *start; /* Points to original start of str */
3759 char *nstr; /* New string, used during expansion */
3760 Boolean dynamic; /* TRUE if the variable is local and we're
3761 * expanding it in a non-local context. This
3762 * is done to support dynamic sources. The
3763 * result is just the invocation, unaltered */
3764 const char *extramodifiers; /* extra modifiers to apply first */
3768 extramodifiers = NULL;
3773 if (startc != PROPEN && startc != BROPEN) {
3775 * If it's not bounded by braces of some sort, life is much simpler.
3776 * We just need to check for the first character and return the
3777 * value if it exists.
3780 /* Error out some really stupid names */
3781 if (startc == '\0' || strchr(")}:$", startc)) {
3788 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3792 if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
3794 * If substituting a local variable in a non-local context,
3795 * assume it's for dynamic source stuff. We have to handle
3796 * this specially and return the longhand for the variable
3797 * with the dollar sign escaped so it makes it back to the
3798 * caller. Only four of the local variables are treated
3799 * specially as they are the only four that will be set
3800 * when dynamic sources are expanded.
3804 return UNCONST("$(.TARGET)");
3806 return UNCONST("$(.MEMBER)");
3808 return UNCONST("$(.PREFIX)");
3810 return UNCONST("$(.ARCHIVE)");
3816 return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3818 haveModifier = FALSE;
3823 Buffer buf; /* Holds the variable name */
3826 endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
3830 * Skip to the end character or a colon, whichever comes first.
3832 for (tstr = str + 2; *tstr != '\0'; tstr++)
3835 * Track depth so we can spot parse errors.
3837 if (*tstr == startc) {
3840 if (*tstr == endc) {
3844 if (depth == 1 && *tstr == ':') {
3848 * A variable inside a variable, expand
3853 char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
3855 Buf_AddBytes(&buf, strlen(rval), rval);
3861 Buf_AddByte(&buf, *tstr);
3864 haveModifier = TRUE;
3865 } else if (*tstr == endc) {
3866 haveModifier = FALSE;
3869 * If we never did find the end character, return NULL
3870 * right now, setting the length to be the distance to
3871 * the end of the string, since that's what make does.
3873 *lengthPtr = tstr - str;
3874 Buf_Destroy(&buf, TRUE);
3877 str = Buf_GetAll(&buf, &vlen);
3880 * At this point, str points into newly allocated memory from
3881 * buf, containing only the name of the variable.
3883 * start and tstr point into the const string that was pointed
3884 * to by the original value of the str parameter. start points
3885 * to the '$' at the beginning of the string, while tstr points
3886 * to the char just after the end of the variable name -- this
3887 * will be '\0', ':', PRCLOSE, or BRCLOSE.
3890 v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3892 * Check also for bogus D and F forms of local variables since we're
3893 * in a local context and the name is the right length.
3895 if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
3896 (vlen == 2) && (str[1] == 'F' || str[1] == 'D') &&
3897 strchr("@%?*!<>", str[0]) != NULL) {
3899 * Well, it's local -- go look for it.
3903 v = VarFind(name, ctxt, 0);
3906 if (str[1] == 'D') {
3907 extramodifiers = "H:";
3910 extramodifiers = "T:";
3917 (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) &&
3918 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3921 * If substituting a local variable in a non-local context,
3922 * assume it's for dynamic source stuff. We have to handle
3923 * this specially and return the longhand for the variable
3924 * with the dollar sign escaped so it makes it back to the
3925 * caller. Only four of the local variables are treated
3926 * specially as they are the only four that will be set
3927 * when dynamic sources are expanded.
3937 } else if ((vlen > 2) && (*str == '.') &&
3938 isupper((unsigned char) str[1]) &&
3939 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3944 if ((strncmp(str, ".TARGET", len) == 0) ||
3945 (strncmp(str, ".ARCHIVE", len) == 0) ||
3946 (strncmp(str, ".PREFIX", len) == 0) ||
3947 (strncmp(str, ".MEMBER", len) == 0))
3953 if (!haveModifier) {
3955 * No modifiers -- have specification length so we can return
3958 *lengthPtr = tstr - start + 1;
3960 char *pstr = bmake_strndup(start, *lengthPtr);
3962 Buf_Destroy(&buf, TRUE);
3965 Buf_Destroy(&buf, TRUE);
3966 return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3970 * Still need to get to the end of the variable specification,
3971 * so kludge up a Var structure for the modifications
3973 v = bmake_malloc(sizeof(Var));
3974 v->name = UNCONST(str);
3975 Buf_Init(&v->val, 1);
3976 v->flags = VAR_JUNK;
3977 Buf_Destroy(&buf, FALSE);
3980 Buf_Destroy(&buf, TRUE);
3983 if (v->flags & VAR_IN_USE) {
3984 Fatal("Variable %s is recursive.", v->name);
3987 v->flags |= VAR_IN_USE;
3990 * Before doing any modification, we have to make sure the value
3991 * has been fully expanded. If it looks like recursion might be
3992 * necessary (there's a dollar sign somewhere in the variable's value)
3993 * we just call Var_Subst to do any other substitutions that are
3994 * necessary. Note that the value returned by Var_Subst will have
3995 * been dynamically-allocated, so it will need freeing when we
3998 nstr = Buf_GetAll(&v->val, NULL);
3999 if (strchr(nstr, '$') != NULL) {
4000 nstr = Var_Subst(NULL, nstr, ctxt, flags);
4004 v->flags &= ~VAR_IN_USE;
4006 if ((nstr != NULL) && (haveModifier || extramodifiers != NULL)) {
4011 if (extramodifiers != NULL) {
4012 nstr = ApplyModifiers(nstr, extramodifiers, '(', ')',
4013 v, ctxt, flags, &used, &extraFree);
4017 /* Skip initial colon. */
4020 nstr = ApplyModifiers(nstr, tstr, startc, endc,
4021 v, ctxt, flags, &used, freePtr);
4025 *freePtr = extraFree;
4029 *lengthPtr = tstr - start + 1;
4031 *lengthPtr = tstr - start;
4034 if (v->flags & VAR_FROM_ENV) {
4035 Boolean destroy = FALSE;
4037 if (nstr != Buf_GetAll(&v->val, NULL)) {
4041 * Returning the value unmodified, so tell the caller to free
4046 VarFreeEnv(v, destroy);
4047 } else if (v->flags & VAR_JUNK) {
4049 * Perform any free'ing needed and set *freePtr to NULL so the caller
4050 * doesn't try to free a static pointer.
4051 * If VAR_KEEP is also set then we want to keep str as is.
4053 if (!(v->flags & VAR_KEEP)) {
4059 nstr = bmake_strndup(start, *lengthPtr);
4062 nstr = (flags & VARF_UNDEFERR) ? var_Error : varNoError;
4065 if (nstr != Buf_GetAll(&v->val, NULL))
4066 Buf_Destroy(&v->val, TRUE);
4074 *-----------------------------------------------------------------------
4076 * Substitute for all variables in the given string in the given context
4077 * If flags & VARF_UNDEFERR, Parse_Error will be called when an undefined
4078 * variable is encountered.
4081 * var Named variable || NULL for all
4082 * str the string which to substitute
4083 * ctxt the context wherein to find variables
4084 * flags VARF_UNDEFERR if undefineds are an error
4085 * VARF_WANTRES if we actually want the result
4086 * VARF_ASSIGN if we are in a := assignment
4089 * The resulting string.
4092 * None. The old string must be freed by the caller
4093 *-----------------------------------------------------------------------
4096 Var_Subst(const char *var, const char *str, GNode *ctxt, int flags)
4098 Buffer buf; /* Buffer for forming things */
4099 char *val; /* Value to substitute for a variable */
4100 int length; /* Length of the variable invocation */
4101 Boolean trailingBslash; /* variable ends in \ */
4102 void *freeIt = NULL; /* Set if it should be freed */
4103 static Boolean errorReported; /* Set true if an error has already
4104 * been reported to prevent a plethora
4105 * of messages when recursing */
4108 errorReported = FALSE;
4109 trailingBslash = FALSE;
4112 if (*str == '\n' && trailingBslash)
4113 Buf_AddByte(&buf, ' ');
4114 if (var == NULL && (*str == '$') && (str[1] == '$')) {
4116 * A dollar sign may be escaped either with another dollar sign.
4117 * In such a case, we skip over the escape character and store the
4118 * dollar sign into the buffer directly.
4120 if (save_dollars && (flags & VARF_ASSIGN))
4121 Buf_AddByte(&buf, *str);
4123 Buf_AddByte(&buf, *str);
4125 } else if (*str != '$') {
4127 * Skip as many characters as possible -- either to the end of
4128 * the string or to the next dollar sign (variable invocation).
4132 for (cp = str++; *str != '$' && *str != '\0'; str++)
4134 Buf_AddBytes(&buf, str - cp, cp);
4139 if (str[1] == '\0') {
4140 /* A trailing $ is kind of a special case */
4141 Buf_AddByte(&buf, str[0]);
4144 } else if (str[1] != PROPEN && str[1] != BROPEN) {
4145 if (str[1] != *var || strlen(var) > 1) {
4146 Buf_AddBytes(&buf, 2, str);
4158 * Scan up to the end of the variable name.
4160 for (p = &str[2]; *p &&
4161 *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++)
4165 * A variable inside the variable. We cannot expand
4166 * the external variable yet, so we try again with
4170 Buf_AddBytes(&buf, p - str, str);
4175 if (strncmp(var, str + 2, p - str - 2) != 0 ||
4176 var[p - str - 2] != '\0') {
4178 * Not the variable we want to expand, scan
4179 * until the next variable
4181 for (;*p != '$' && *p != '\0'; p++)
4183 Buf_AddBytes(&buf, p - str, str);
4196 val = Var_Parse(str, ctxt, flags, &length, &freeIt);
4199 * When we come down here, val should either point to the
4200 * value of this variable, suitably modified, or be NULL.
4201 * Length should be the total length of the potential
4202 * variable invocation (from $ to end character...)
4204 if (val == var_Error || val == varNoError) {
4206 * If performing old-time variable substitution, skip over
4207 * the variable and continue with the substitution. Otherwise,
4208 * store the dollar sign and advance str so we continue with
4213 } else if ((flags & VARF_UNDEFERR) || val == var_Error) {
4215 * If variable is undefined, complain and skip the
4216 * variable. The complaint will stop us from doing anything
4217 * when the file is parsed.
4219 if (!errorReported) {
4220 Parse_Error(PARSE_FATAL,
4221 "Undefined variable \"%.*s\"",length,str);
4224 errorReported = TRUE;
4226 Buf_AddByte(&buf, *str);
4231 * We've now got a variable structure to store in. But first,
4232 * advance the string pointer.
4237 * Copy all the characters from the variable value straight
4238 * into the new string.
4240 length = strlen(val);
4241 Buf_AddBytes(&buf, length, val);
4242 trailingBslash = length > 0 && val[length - 1] == '\\';
4249 return Buf_DestroyCompact(&buf);
4253 *-----------------------------------------------------------------------
4255 * Return the tail from each of a list of words. Used to set the
4256 * System V local variables.
4259 * file Filename to modify
4262 * The resulting string.
4267 *-----------------------------------------------------------------------
4271 Var_GetTail(char *file)
4273 return(VarModify(file, VarTail, NULL));
4277 *-----------------------------------------------------------------------
4279 * Find the leading components of a (list of) filename(s).
4280 * XXX: VarHead does not replace foo by ., as (sun) System V make
4284 * file Filename to manipulate
4287 * The leading components.
4292 *-----------------------------------------------------------------------
4295 Var_GetHead(char *file)
4297 return(VarModify(file, VarHead, NULL));
4302 *-----------------------------------------------------------------------
4304 * Initialize the module
4310 * The VAR_CMD and VAR_GLOBAL contexts are created
4311 *-----------------------------------------------------------------------
4316 VAR_INTERNAL = Targ_NewGN("Internal");
4317 VAR_GLOBAL = Targ_NewGN("Global");
4318 VAR_CMD = Targ_NewGN("Command");
4329 /****************** PRINT DEBUGGING INFO *****************/
4331 VarPrintVar(void *vp)
4334 fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
4338 *-----------------------------------------------------------------------
4340 * print all variables in a context
4341 *-----------------------------------------------------------------------
4344 Var_Dump(GNode *ctxt)
4349 for (h = Hash_EnumFirst(&ctxt->context, &search);
4351 h = Hash_EnumNext(&search)) {
4352 VarPrintVar(Hash_GetValue(h));