1 /* $NetBSD: var.c,v 1.255 2020/07/04 17:41:04 rillig 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.255 2020/07/04 17:41:04 rillig 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.255 2020/07/04 17:41:04 rillig 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 either a single variable or all
106 * variables in a string, using the given context as
109 * Var_Parse Parse a variable expansion from a string and
110 * return the result and the number of characters
113 * Var_Delete Delete a variable in a context.
115 * Var_Init Initialize this module.
118 * Var_Dump Print out all variables defined in the given
121 * XXX: There's a lot of duplication in these functions.
124 #include <sys/stat.h>
125 #include <sys/types.h>
143 #include "metachar.h"
145 extern int makelevel;
147 * This lets us tell if we have replaced the original environ
148 * (which we cannot free).
150 char **savedEnv = NULL;
153 * This is a harmless return value for Var_Parse that can be used by Var_Subst
154 * to determine if there was an error in parsing -- easier than returning
155 * a flag, as things outside this module don't give a hoot.
157 char var_Error[] = "";
160 * Similar to var_Error, but returned when the 'VARF_UNDEFERR' flag for
161 * Var_Parse is not set. Why not just use a constant? Well, gcc likes
162 * to condense identical string instances...
164 static char varNoError[] = "";
167 * Traditionally we consume $$ during := like any other expansion.
168 * Other make's do not.
169 * This knob allows controlling the behavior.
170 * FALSE for old behavior.
171 * TRUE for new compatible.
173 #define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
174 static Boolean save_dollars = FALSE;
177 * Internally, variables are contained in four different contexts.
178 * 1) the environment. They may not be changed. If an environment
179 * variable is appended-to, the result is placed in the global
181 * 2) the global context. Variables set in the Makefile are located in
182 * the global context. It is the penultimate context searched when
184 * 3) the command-line context. All variables set on the command line
185 * are placed in this context. They are UNALTERABLE once placed here.
186 * 4) the local context. Each target has associated with it a context
187 * list. On this list are located the structures describing such
188 * local variables as $(@) and $(*)
189 * The four contexts are searched in the reverse order from which they are
192 GNode *VAR_INTERNAL; /* variables from make itself */
193 GNode *VAR_GLOBAL; /* variables from the makefile */
194 GNode *VAR_CMD; /* variables defined on the command-line */
196 #define FIND_CMD 0x1 /* look in VAR_CMD when searching */
197 #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
198 #define FIND_ENV 0x4 /* look in the environment also */
201 VAR_IN_USE = 0x01, /* Variable's value is currently being used.
202 * Used to avoid endless recursion */
203 VAR_FROM_ENV = 0x02, /* Variable comes from the environment */
204 VAR_JUNK = 0x04, /* 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 VAR_KEEP = 0x08, /* Variable is VAR_JUNK, but we found
209 * a use for it in some modifier and
210 * the value is therefore valid */
211 VAR_EXPORTED = 0x10, /* Variable is exported */
212 VAR_REEXPORT = 0x20, /* Indicate if var needs re-export.
213 * This would be true if it contains $'s */
214 VAR_FROM_CMD = 0x40 /* Variable came from command line */
218 char *name; /* the variable's name */
219 Buffer val; /* its value */
220 Var_Flags flags; /* miscellaneous status flags */
224 * Exporting vars is expensive so skip it if we can
226 #define VAR_EXPORTED_NONE 0
227 #define VAR_EXPORTED_YES 1
228 #define VAR_EXPORTED_ALL 2
229 static int var_exportedVars = VAR_EXPORTED_NONE;
231 * We pass this to Var_Export when doing the initial export
232 * or after updating an exported var.
234 #define VAR_EXPORT_PARENT 1
236 * We pass this to Var_Export1 to tell it to leave the value alone.
238 #define VAR_EXPORT_LITERAL 2
241 VAR_SUB_GLOBAL = 0x01, /* Apply substitution globally */
242 VAR_SUB_ONE = 0x02, /* Apply substitution to one word */
243 VAR_SUB_MATCHED = 0x04, /* There was a match */
244 VAR_MATCH_START = 0x08, /* Match at start of word */
245 VAR_MATCH_END = 0x10, /* Match at end of word */
246 VAR_NOSUBST = 0x20 /* don't expand vars in VarGetPattern */
250 VAR_NO_EXPORT = 0x01 /* do not export */
255 * The following fields are set by Var_Parse() when it
256 * encounters modifiers that need to keep state for use by
257 * subsequent modifiers within the same variable expansion.
259 Byte varSpace; /* Word separator in expansions */
260 Boolean oneBigWord; /* TRUE if we will treat the variable as a
261 * single big word, even if it contains
262 * embedded spaces (as opposed to the
263 * usual behaviour of treating it as
264 * several space-separated words). */
267 /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/",
268 * to VarSYSVMatch() for ":lhs=rhs". */
270 const char *lhs; /* String to match */
271 int leftLen; /* Length of string */
272 const char *rhs; /* Replacement string (w/ &'s removed) */
273 int rightLen; /* Length of replacement */
274 VarPattern_Flags flags;
277 /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */
279 GNode *ctxt; /* variable context */
280 char *tvar; /* name of temp var */
282 char *str; /* string to expand */
288 /* struct passed as 'void *' to VarRESubstitute() for ":C///" */
298 /* struct passed to VarSelectWords() for ":[start..end]" */
300 int start; /* first word to select */
301 int end; /* last word to select */
310 *-----------------------------------------------------------------------
312 * Find the given variable in the given context and any other contexts
317 * ctxt context in which to find it
318 * flags FIND_GLOBAL set means to look in the
319 * VAR_GLOBAL context as well. FIND_CMD set means
320 * to look in the VAR_CMD context also. FIND_ENV
321 * set means to look in the environment
324 * A pointer to the structure describing the desired variable or
325 * NULL if the variable does not exist.
329 *-----------------------------------------------------------------------
332 VarFind(const char *name, GNode *ctxt, int flags)
338 * If the variable name begins with a '.', it could very well be one of
339 * the local ones. We check the name against all the local variables
340 * and substitute the short version in for 'name' if it matches one of
343 if (*name == '.' && isupper((unsigned char) name[1])) {
346 if (!strcmp(name, ".ALLSRC"))
348 if (!strcmp(name, ".ARCHIVE"))
352 if (!strcmp(name, ".IMPSRC"))
356 if (!strcmp(name, ".MEMBER"))
360 if (!strcmp(name, ".OODATE"))
364 if (!strcmp(name, ".PREFIX"))
368 if (!strcmp(name, ".TARGET"))
375 /* for compatibility with gmake */
376 if (name[0] == '^' && name[1] == '\0')
381 * First look for the variable in the given context. If it's not there,
382 * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
383 * depending on the FIND_* flags in 'flags'
385 var = Hash_FindEntry(&ctxt->context, name);
387 if (var == NULL && (flags & FIND_CMD) && ctxt != VAR_CMD) {
388 var = Hash_FindEntry(&VAR_CMD->context, name);
390 if (!checkEnvFirst && var == NULL && (flags & FIND_GLOBAL) &&
393 var = Hash_FindEntry(&VAR_GLOBAL->context, name);
394 if (var == NULL && ctxt != VAR_INTERNAL) {
395 /* VAR_INTERNAL is subordinate to VAR_GLOBAL */
396 var = Hash_FindEntry(&VAR_INTERNAL->context, name);
399 if (var == NULL && (flags & FIND_ENV)) {
402 if ((env = getenv(name)) != NULL) {
405 v = bmake_malloc(sizeof(Var));
406 v->name = bmake_strdup(name);
410 Buf_Init(&v->val, len + 1);
411 Buf_AddBytes(&v->val, len, env);
413 v->flags = VAR_FROM_ENV;
415 } else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
418 var = Hash_FindEntry(&VAR_GLOBAL->context, name);
419 if (var == NULL && ctxt != VAR_INTERNAL) {
420 var = Hash_FindEntry(&VAR_INTERNAL->context, name);
425 return (Var *)Hash_GetValue(var);
430 } else if (var == NULL) {
433 return (Var *)Hash_GetValue(var);
438 *-----------------------------------------------------------------------
440 * If the variable is an environment variable, free it
444 * destroy true if the value buffer should be destroyed.
447 * 1 if it is an environment variable 0 ow.
450 * The variable is free'ed if it is an environent variable.
451 *-----------------------------------------------------------------------
454 VarFreeEnv(Var *v, Boolean destroy)
456 if ((v->flags & VAR_FROM_ENV) == 0)
459 Buf_Destroy(&v->val, destroy);
465 *-----------------------------------------------------------------------
467 * Add a new variable of name name and value val to the given context
470 * name name of variable to add
471 * val value to set it to
472 * ctxt context in which to set it
475 * The new variable is placed at the front of the given context
476 * The name and val arguments are duplicated so they may
478 *-----------------------------------------------------------------------
481 VarAdd(const char *name, const char *val, GNode *ctxt)
487 v = bmake_malloc(sizeof(Var));
489 len = val ? strlen(val) : 0;
490 Buf_Init(&v->val, len + 1);
491 Buf_AddBytes(&v->val, len, val);
495 h = Hash_CreateEntry(&ctxt->context, name, NULL);
498 if (DEBUG(VAR) && (ctxt->flags & INTERNAL) == 0) {
499 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
504 *-----------------------------------------------------------------------
506 * Remove a variable from a context.
509 * The Var structure is removed and freed.
511 *-----------------------------------------------------------------------
514 Var_Delete(const char *name, GNode *ctxt)
519 if (strchr(name, '$')) {
520 cp = Var_Subst(NULL, name, VAR_GLOBAL, VARF_WANTRES);
522 cp = __DECONST(char *,name); /* XXX */
524 ln = Hash_FindEntry(&ctxt->context, cp);
526 fprintf(debug_file, "%s:delete %s%s\n",
527 ctxt->name, cp, ln ? "" : " (not found)");
535 v = (Var *)Hash_GetValue(ln);
536 if ((v->flags & VAR_EXPORTED)) {
539 if (strcmp(MAKE_EXPORTED, v->name) == 0) {
540 var_exportedVars = VAR_EXPORTED_NONE;
542 if (v->name != ln->name)
544 Hash_DeleteEntry(&ctxt->context, ln);
545 Buf_Destroy(&v->val, TRUE);
553 * We ignore make internal variables (those which start with '.')
554 * Also we jump through some hoops to avoid calling setenv
555 * more than necessary since it can leak.
556 * We only manipulate flags of vars if 'parent' is set.
559 Var_Export1(const char *name, int flags)
565 int parent = (flags & VAR_EXPORT_PARENT);
568 return 0; /* skip internals */
572 * If it is one of the vars that should only appear in
573 * local context, skip it, else we can get Var_Subst
584 v = VarFind(name, VAR_GLOBAL, 0);
589 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
590 return 0; /* nothing to do */
592 val = Buf_GetAll(&v->val, NULL);
593 if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) {
596 * Flag this as something we need to re-export.
597 * No point actually exporting it now though,
598 * the child can do it at the last minute.
600 v->flags |= (VAR_EXPORTED|VAR_REEXPORT);
603 if (v->flags & VAR_IN_USE) {
605 * We recursed while exporting in a child.
606 * This isn't going to end well, just skip it.
610 n = snprintf(tmp, sizeof(tmp), "${%s}", name);
611 if (n < (int)sizeof(tmp)) {
612 val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
613 setenv(name, val, 1);
618 v->flags &= ~VAR_REEXPORT; /* once will do */
620 if (parent || !(v->flags & VAR_EXPORTED)) {
621 setenv(name, val, 1);
625 * This is so Var_Set knows to call Var_Export again...
628 v->flags |= VAR_EXPORTED;
634 Var_ExportVars_callback(void *entry, void *unused MAKE_ATTR_UNUSED)
637 Var_Export1(var->name, 0);
641 * This gets called from our children.
651 * Several make's support this sort of mechanism for tracking
652 * recursion - but each uses a different name.
653 * We allow the makefiles to update MAKELEVEL and ensure
654 * children see a correctly incremented value.
656 snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
657 setenv(MAKE_LEVEL_ENV, tmp, 1);
659 if (VAR_EXPORTED_NONE == var_exportedVars)
662 if (VAR_EXPORTED_ALL == var_exportedVars) {
663 /* Ouch! This is crazy... */
664 Hash_ForEach(&VAR_GLOBAL->context, Var_ExportVars_callback, NULL);
668 * We have a number of exported vars,
670 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
671 if (n < (int)sizeof(tmp)) {
677 val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
679 av = brk_string(val, &ac, FALSE, &as);
680 for (i = 0; i < ac; i++) {
681 Var_Export1(av[i], 0);
691 * This is called when .export is seen or
692 * .MAKE.EXPORTED is modified.
693 * It is also called when any exported var is modified.
696 Var_Export(char *str, int isExport)
706 if (isExport && (!str || !str[0])) {
707 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
712 if (strncmp(str, "-env", 4) == 0) {
714 } else if (strncmp(str, "-literal", 8) == 0) {
716 flags |= VAR_EXPORT_LITERAL;
718 flags |= VAR_EXPORT_PARENT;
720 val = Var_Subst(NULL, str, VAR_GLOBAL, VARF_WANTRES);
722 av = brk_string(val, &ac, FALSE, &as);
723 for (i = 0; i < ac; i++) {
728 * If it is one of the vars that should only appear in
729 * local context, skip it, else we can get Var_Subst
740 if (Var_Export1(name, flags)) {
741 if (VAR_EXPORTED_ALL != var_exportedVars)
742 var_exportedVars = VAR_EXPORTED_YES;
743 if (isExport && (flags & VAR_EXPORT_PARENT)) {
744 Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
756 * This is called when .unexport[-env] is seen.
758 extern char **environ;
761 Var_UnExport(char *str)
766 Boolean unexport_env;
769 if (!str || !str[0]) {
770 return; /* assert? */
776 unexport_env = (strncmp(str, "-env", 4) == 0);
780 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */
781 if (environ == savedEnv) {
782 /* we have been here before! */
783 newenv = bmake_realloc(environ, 2 * sizeof(char *));
789 newenv = bmake_malloc(2 * sizeof(char *));
793 /* Note: we cannot safely free() the original environ. */
794 environ = savedEnv = newenv;
798 setenv(MAKE_LEVEL_ENV, cp, 1);
800 for (; *str != '\n' && isspace((unsigned char) *str); str++)
802 if (str[0] && str[0] != '\n') {
808 /* Using .MAKE.EXPORTED */
809 n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}");
810 if (n < (int)sizeof(tmp)) {
811 vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
821 av = brk_string(vlist, &ac, FALSE, &as);
822 for (i = 0; i < ac; i++) {
823 v = VarFind(av[i], VAR_GLOBAL, 0);
827 (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) {
830 v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT);
832 * If we are unexporting a list,
833 * remove each one from .MAKE.EXPORTED.
834 * If we are removing them all,
835 * just delete .MAKE.EXPORTED below.
838 n = snprintf(tmp, sizeof(tmp),
839 "${" MAKE_EXPORTED ":N%s}", v->name);
840 if (n < (int)sizeof(tmp)) {
841 cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES);
842 Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL);
850 Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
857 Var_Set_with_flags(const char *name, const char *val, GNode *ctxt,
861 char *expanded_name = NULL;
864 * We only look for a variable in the given context since anything set
865 * here will override anything in a lower context, so there's not much
866 * point in searching them all just to save a bit of memory...
868 if (strchr(name, '$') != NULL) {
869 expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
870 if (expanded_name[0] == 0) {
872 fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) "
873 "name expands to empty string - ignored\n",
879 name = expanded_name;
881 if (ctxt == VAR_GLOBAL) {
882 v = VarFind(name, VAR_CMD, 0);
884 if ((v->flags & VAR_FROM_CMD)) {
886 fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val);
893 v = VarFind(name, ctxt, 0);
895 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
897 * This var would normally prevent the same name being added
898 * to VAR_GLOBAL, so delete it from there if needed.
899 * Otherwise -V name may show the wrong value.
901 Var_Delete(name, VAR_GLOBAL);
903 VarAdd(name, val, ctxt);
907 Buf_AddBytes(&v->val, strlen(val), val);
910 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val);
912 if ((v->flags & VAR_EXPORTED)) {
913 Var_Export1(name, VAR_EXPORT_PARENT);
917 * Any variables given on the command line are automatically exported
918 * to the environment (as per POSIX standard)
920 if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) {
922 /* we just added it */
923 v = VarFind(name, ctxt, 0);
926 v->flags |= VAR_FROM_CMD;
928 * If requested, don't export these in the environment
929 * individually. We still put them in MAKEOVERRIDES so
930 * that the command-line settings continue to override
933 if (varNoExportEnv != TRUE)
934 setenv(name, val ? val : "", 1);
936 Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
939 if (strcmp(name, SAVE_DOLLARS) == 0)
940 save_dollars = s2Boolean(val, save_dollars);
950 *-----------------------------------------------------------------------
952 * Set the variable name to the value val in the given context.
955 * name name of variable to set
956 * val value to give to the variable
957 * ctxt context in which to set it
960 * If the variable doesn't yet exist, a new record is created for it.
961 * Else the old value is freed and the new one stuck in its place
964 * The variable is searched for only in its context before being
965 * created in that context. I.e. if the context is VAR_GLOBAL,
966 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
967 * VAR_CMD->context is searched. This is done to avoid the literally
968 * thousands of unnecessary strcmp's that used to be done to
969 * set, say, $(@) or $(<).
970 * If the context is VAR_GLOBAL though, we check if the variable
971 * was set in VAR_CMD from the command line and skip it if so.
972 *-----------------------------------------------------------------------
975 Var_Set(const char *name, const char *val, GNode *ctxt)
977 Var_Set_with_flags(name, val, ctxt, 0);
981 *-----------------------------------------------------------------------
983 * The variable of the given name has the given value appended to it in
987 * name name of variable to modify
988 * val String to append to it
989 * ctxt Context in which this should occur
992 * If the variable doesn't exist, it is created. Else the strings
993 * are concatenated (with a space in between).
996 * Only if the variable is being sought in the global context is the
997 * environment searched.
998 * XXX: Knows its calling circumstances in that if called with ctxt
999 * an actual target, it will only search that context since only
1000 * a local variable could be being appended to. This is actually
1001 * a big win and must be tolerated.
1002 *-----------------------------------------------------------------------
1005 Var_Append(const char *name, const char *val, GNode *ctxt)
1009 char *expanded_name = NULL;
1011 if (strchr(name, '$') != NULL) {
1012 expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1013 if (expanded_name[0] == 0) {
1015 fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) "
1016 "name expands to empty string - ignored\n",
1019 free(expanded_name);
1022 name = expanded_name;
1025 v = VarFind(name, ctxt, ctxt == VAR_GLOBAL ? (FIND_CMD|FIND_ENV) : 0);
1028 Var_Set(name, val, ctxt);
1029 } else if (ctxt == VAR_CMD || !(v->flags & VAR_FROM_CMD)) {
1030 Buf_AddByte(&v->val, ' ');
1031 Buf_AddBytes(&v->val, strlen(val), val);
1034 fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
1035 Buf_GetAll(&v->val, NULL));
1038 if (v->flags & VAR_FROM_ENV) {
1040 * If the original variable came from the environment, we
1041 * have to install it in the global context (we could place
1042 * it in the environment, but then we should provide a way to
1043 * export other variables...)
1045 v->flags &= ~VAR_FROM_ENV;
1046 h = Hash_CreateEntry(&ctxt->context, name, NULL);
1047 Hash_SetValue(h, v);
1050 free(expanded_name);
1054 *-----------------------------------------------------------------------
1056 * See if the given variable exists.
1059 * name Variable to find
1060 * ctxt Context in which to start search
1063 * TRUE if it does, FALSE if it doesn't
1068 *-----------------------------------------------------------------------
1071 Var_Exists(const char *name, GNode *ctxt)
1076 if ((cp = strchr(name, '$')) != NULL) {
1077 cp = Var_Subst(NULL, name, ctxt, VARF_WANTRES);
1079 v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
1085 (void)VarFreeEnv(v, TRUE);
1090 *-----------------------------------------------------------------------
1092 * Return the value of the named variable in the given context
1096 * ctxt context in which to search for it
1099 * The value if the variable exists, NULL if it doesn't
1103 *-----------------------------------------------------------------------
1106 Var_Value(const char *name, GNode *ctxt, char **frp)
1110 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1115 char *p = (Buf_GetAll(&v->val, NULL));
1116 if (VarFreeEnv(v, FALSE))
1122 /* This callback for VarModify gets a single word from an expression and
1123 * typically adds a modification of this word to the buffer. It may also do
1124 * nothing or add several words.
1126 * If addSpaces is TRUE, it must add a space before adding anything else to
1129 * It returns the addSpace value for the next call of this callback. Typical
1130 * return values are the current addSpaces or TRUE. */
1131 typedef Boolean (*VarModifyCallback)(GNode *ctxt, Var_Parse_State *vpstate,
1132 const char *word, Boolean addSpace, Buffer *buf, void *data);
1135 /* Callback function for VarModify to implement the :H modifier.
1136 * Add the dirname of the given word to the buffer. */
1138 VarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1139 const char *word, Boolean addSpace, Buffer *buf,
1140 void *dummy MAKE_ATTR_UNUSED)
1142 const char *slash = strrchr(word, '/');
1144 if (addSpace && vpstate->varSpace)
1145 Buf_AddByte(buf, vpstate->varSpace);
1147 Buf_AddBytes(buf, slash - word, word);
1149 Buf_AddByte(buf, '.');
1154 /* Callback function for VarModify to implement the :T modifier.
1155 * Add the basename of the given word to the buffer. */
1157 VarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1158 const char *word, Boolean addSpace, Buffer *buf,
1159 void *dummy MAKE_ATTR_UNUSED)
1161 const char *slash = strrchr(word, '/');
1162 const char *base = slash != NULL ? slash + 1 : word;
1164 if (addSpace && vpstate->varSpace)
1165 Buf_AddByte(buf, vpstate->varSpace);
1166 Buf_AddBytes(buf, strlen(base), base);
1170 /* Callback function for VarModify to implement the :E modifier.
1171 * Add the filename suffix of the given word to the buffer, if it exists. */
1173 VarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1174 const char *word, Boolean addSpace, Buffer *buf,
1175 void *dummy MAKE_ATTR_UNUSED)
1177 const char *dot = strrchr(word, '.');
1181 if (addSpace && vpstate->varSpace)
1182 Buf_AddByte(buf, vpstate->varSpace);
1183 Buf_AddBytes(buf, strlen(dot + 1), dot + 1);
1187 /* Callback function for VarModify to implement the :R modifier.
1188 * Add the filename basename of the given word to the buffer. */
1190 VarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1191 const char *word, Boolean addSpace, Buffer *buf,
1192 void *dummy MAKE_ATTR_UNUSED)
1194 char *dot = strrchr(word, '.');
1195 size_t len = dot != NULL ? (size_t)(dot - word) : strlen(word);
1197 if (addSpace && vpstate->varSpace)
1198 Buf_AddByte(buf, vpstate->varSpace);
1199 Buf_AddBytes(buf, len, word);
1203 /* Callback function for VarModify to implement the :M modifier.
1204 * Place the word in the buffer if it matches the given pattern. */
1206 VarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1207 const char *word, Boolean addSpace, Buffer *buf,
1210 const char *pattern = data;
1212 fprintf(debug_file, "VarMatch [%s] [%s]\n", word, pattern);
1213 if (!Str_Match(word, pattern))
1215 if (addSpace && vpstate->varSpace)
1216 Buf_AddByte(buf, vpstate->varSpace);
1217 Buf_AddBytes(buf, strlen(word), word);
1222 /* Callback function for VarModify to implement the :%.from=%.to modifier. */
1224 VarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate,
1225 const char *word, Boolean addSpace, Buffer *buf,
1231 VarPattern *pat = data;
1233 if (addSpace && vpstate->varSpace)
1234 Buf_AddByte(buf, vpstate->varSpace);
1236 if ((ptr = Str_SYSVMatch(word, pat->lhs, &len, &hasPercent)) != NULL) {
1237 char *varexp = Var_Subst(NULL, pat->rhs, ctx, VARF_WANTRES);
1238 Str_SYSVSubst(buf, varexp, ptr, len, hasPercent);
1241 Buf_AddBytes(buf, strlen(word), word);
1248 /* Callback function for VarModify to implement the :N modifier.
1249 * Place the word in the buffer if it doesn't match the given pattern. */
1251 VarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1252 const char *word, Boolean addSpace, Buffer *buf,
1255 const char *pattern = data;
1256 if (Str_Match(word, pattern))
1258 if (addSpace && vpstate->varSpace)
1259 Buf_AddByte(buf, vpstate->varSpace);
1260 Buf_AddBytes(buf, strlen(word), word);
1264 /* Callback function for VarModify to implement the :S,from,to, modifier.
1265 * Perform a string substitution on the given word. */
1267 VarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1268 const char *word, Boolean addSpace, Buffer *buf,
1271 int wordLen = strlen(word);
1272 const char *cp; /* General pointer */
1273 VarPattern *pattern = data;
1275 if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
1276 (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1278 * Still substituting -- break it down into simple anchored cases
1279 * and if none of them fits, perform the general substitution case.
1281 if ((pattern->flags & VAR_MATCH_START) &&
1282 (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
1284 * Anchored at start and beginning of word matches pattern
1286 if ((pattern->flags & VAR_MATCH_END) &&
1287 (wordLen == pattern->leftLen)) {
1289 * Also anchored at end and matches to the end (word
1290 * is same length as pattern) add space and rhs only
1291 * if rhs is non-null.
1293 if (pattern->rightLen != 0) {
1294 if (addSpace && vpstate->varSpace) {
1295 Buf_AddByte(buf, vpstate->varSpace);
1298 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1300 pattern->flags |= VAR_SUB_MATCHED;
1301 } else if (pattern->flags & VAR_MATCH_END) {
1303 * Doesn't match to end -- copy word wholesale
1308 * Matches at start but need to copy in trailing characters
1310 if ((pattern->rightLen + wordLen - pattern->leftLen) != 0) {
1311 if (addSpace && vpstate->varSpace) {
1312 Buf_AddByte(buf, vpstate->varSpace);
1316 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1317 Buf_AddBytes(buf, wordLen - pattern->leftLen,
1318 (word + pattern->leftLen));
1319 pattern->flags |= VAR_SUB_MATCHED;
1321 } else if (pattern->flags & VAR_MATCH_START) {
1323 * Had to match at start of word and didn't -- copy whole word.
1326 } else if (pattern->flags & VAR_MATCH_END) {
1328 * Anchored at end, Find only place match could occur (leftLen
1329 * characters from the end of the word) and see if it does. Note
1330 * that because the $ will be left at the end of the lhs, we have
1333 cp = word + (wordLen - pattern->leftLen);
1335 (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
1337 * Match found. If we will place characters in the buffer,
1338 * add a space before hand as indicated by addSpace, then
1339 * stuff in the initial, unmatched part of the word followed
1340 * by the right-hand-side.
1342 if (((cp - word) + pattern->rightLen) != 0) {
1343 if (addSpace && vpstate->varSpace) {
1344 Buf_AddByte(buf, vpstate->varSpace);
1348 Buf_AddBytes(buf, cp - word, word);
1349 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1350 pattern->flags |= VAR_SUB_MATCHED;
1353 * Had to match at end and didn't. Copy entire word.
1359 * Pattern is unanchored: search for the pattern in the word using
1360 * String_FindSubstring, copying unmatched portions and the
1361 * right-hand-side for each match found, handling non-global
1362 * substitutions correctly, etc. When the loop is done, any
1363 * remaining part of the word (word and wordLen are adjusted
1364 * accordingly through the loop) is copied straight into the
1366 * addSpace is set FALSE as soon as a space is added to the
1373 origSize = Buf_Size(buf);
1375 cp = Str_FindSubstring(word, pattern->lhs);
1377 if (addSpace && (((cp - word) + pattern->rightLen) != 0)) {
1378 Buf_AddByte(buf, vpstate->varSpace);
1381 Buf_AddBytes(buf, cp - word, word);
1382 Buf_AddBytes(buf, pattern->rightLen, pattern->rhs);
1383 wordLen -= (cp - word) + pattern->leftLen;
1384 word = cp + pattern->leftLen;
1388 if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {
1391 pattern->flags |= VAR_SUB_MATCHED;
1397 if (addSpace && vpstate->varSpace) {
1398 Buf_AddByte(buf, vpstate->varSpace);
1400 Buf_AddBytes(buf, wordLen, word);
1403 * If added characters to the buffer, need to add a space
1404 * before we add any more. If we didn't add any, just return
1405 * the previous value of addSpace.
1407 return (Buf_Size(buf) != origSize) || addSpace;
1412 if (addSpace && vpstate->varSpace) {
1413 Buf_AddByte(buf, vpstate->varSpace);
1415 Buf_AddBytes(buf, wordLen, word);
1421 *-----------------------------------------------------------------------
1423 * Print the error caused by a regcomp or regexec call.
1426 * An error gets printed.
1428 *-----------------------------------------------------------------------
1431 VarREError(int reerr, regex_t *pat, const char *str)
1436 errlen = regerror(reerr, pat, 0, 0);
1437 errbuf = bmake_malloc(errlen);
1438 regerror(reerr, pat, errbuf, errlen);
1439 Error("%s: %s", str, errbuf);
1443 /* Callback function for VarModify to implement the :C/from/to/ modifier.
1444 * Perform a regex substitution on the given word. */
1446 VarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED,
1447 Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1448 const char *word, Boolean addSpace, Buffer *buf,
1451 VarREPattern *pat = data;
1453 const char *wp = word;
1458 #define MAYBE_ADD_SPACE() \
1459 if (addSpace && !added) \
1460 Buf_AddByte(buf, ' '); \
1463 if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
1464 (VAR_SUB_ONE|VAR_SUB_MATCHED))
1468 xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags);
1473 pat->flags |= VAR_SUB_MATCHED;
1474 if (pat->matches[0].rm_so > 0) {
1476 Buf_AddBytes(buf, pat->matches[0].rm_so, wp);
1479 for (rp = pat->replace; *rp; rp++) {
1480 if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
1482 Buf_AddByte(buf, rp[1]);
1484 } else if ((*rp == '&') ||
1485 ((*rp == '\\') && isdigit((unsigned char)rp[1]))) {
1503 if (n > pat->nsub) {
1504 Error("No subexpression %s", &errstr[0]);
1507 } else if ((pat->matches[n].rm_so == -1) &&
1508 (pat->matches[n].rm_eo == -1)) {
1509 Error("No match for subexpression %s", &errstr[0]);
1513 subbuf = wp + pat->matches[n].rm_so;
1514 sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
1519 Buf_AddBytes(buf, sublen, subbuf);
1523 Buf_AddByte(buf, *rp);
1526 wp += pat->matches[0].rm_eo;
1527 if (pat->flags & VAR_SUB_GLOBAL) {
1528 flags |= REG_NOTBOL;
1529 if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) {
1531 Buf_AddByte(buf, *wp);
1540 Buf_AddBytes(buf, strlen(wp), wp);
1544 VarREError(xrv, &pat->re, "Unexpected regex error");
1549 Buf_AddBytes(buf, strlen(wp), wp);
1553 return addSpace || added;
1558 /* Callback function for VarModify to implement the :@var@...@ modifier of
1559 * ODE make. We set the temp variable named in pattern.lhs to word and
1560 * expand pattern.rhs. */
1562 VarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED,
1563 Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1564 const char *word, Boolean addSpace, Buffer *buf,
1567 VarLoop *loop = data;
1572 Var_Set_with_flags(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT);
1573 s = Var_Subst(NULL, loop->str, loop->ctxt, loop->flags);
1576 "VarLoopExpand: in \"%s\", replace \"%s\" with \"%s\" "
1578 word, loop->tvar, loop->str, s ? s : "(null)");
1580 if (s != NULL && *s != '\0') {
1581 if (addSpace && *s != '\n')
1582 Buf_AddByte(buf, ' ');
1583 Buf_AddBytes(buf, (slen = strlen(s)), s);
1584 addSpace = (slen > 0 && s[slen - 1] != '\n');
1593 *-----------------------------------------------------------------------
1595 * Implements the :[start..end] modifier.
1596 * This is a special case of VarModify since we want to be able
1597 * to scan the list backwards if start > end.
1600 * str String whose words should be trimmed
1601 * seldata words to select
1604 * A string of all the words selected.
1609 *-----------------------------------------------------------------------
1612 VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1613 const char *str, VarSelectWords_t *seldata)
1615 Buffer buf; /* Buffer for the new string */
1616 Boolean addSpace; /* TRUE if need to add a space to the
1617 * buffer before adding the trimmed
1619 char **av; /* word list */
1620 char *as; /* word list memory */
1622 int start, end, step;
1627 if (vpstate->oneBigWord) {
1628 /* fake what brk_string() would do if there were only one word */
1630 av = bmake_malloc((ac + 1) * sizeof(char *));
1631 as = bmake_strdup(str);
1635 av = brk_string(str, &ac, FALSE, &as);
1639 * Now sanitize seldata.
1640 * If seldata->start or seldata->end are negative, convert them to
1641 * the positive equivalents (-1 gets converted to argc, -2 gets
1642 * converted to (argc-1), etc.).
1644 if (seldata->start < 0)
1645 seldata->start = ac + seldata->start + 1;
1646 if (seldata->end < 0)
1647 seldata->end = ac + seldata->end + 1;
1650 * We avoid scanning more of the list than we need to.
1652 if (seldata->start > seldata->end) {
1653 start = MIN(ac, seldata->start) - 1;
1654 end = MAX(0, seldata->end - 1);
1657 start = MAX(0, seldata->start - 1);
1658 end = MIN(ac, seldata->end);
1663 (step < 0 && i >= end) || (step > 0 && i < end);
1665 if (av[i] && *av[i]) {
1666 if (addSpace && vpstate->varSpace) {
1667 Buf_AddByte(&buf, vpstate->varSpace);
1669 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1677 return Buf_Destroy(&buf, FALSE);
1681 /* Callback function for VarModify to implement the :tA modifier.
1682 * Replace each word with the result of realpath() if successful. */
1684 VarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
1685 const char *word, Boolean addSpace, Buffer *buf,
1686 void *patternp MAKE_ATTR_UNUSED)
1689 char rbuf[MAXPATHLEN];
1692 if (addSpace && vpstate->varSpace)
1693 Buf_AddByte(buf, vpstate->varSpace);
1694 rp = cached_realpath(word, rbuf);
1695 if (rp && *rp == '/' && stat(rp, &st) == 0)
1698 Buf_AddBytes(buf, strlen(word), word);
1703 *-----------------------------------------------------------------------
1704 * Modify each of the words of the passed string using the given function.
1707 * str String whose words should be trimmed
1708 * modProc Function to use to modify them
1709 * data Custom data for the modProc
1712 * A string of all the words modified appropriately.
1717 *-----------------------------------------------------------------------
1720 VarModify(GNode *ctx, Var_Parse_State *vpstate,
1721 const char *str, VarModifyCallback modProc, void *datum)
1723 Buffer buf; /* Buffer for the new string */
1724 Boolean addSpace; /* TRUE if need to add a space to the
1725 * buffer before adding the trimmed word */
1726 char **av; /* word list */
1727 char *as; /* word list memory */
1733 if (vpstate->oneBigWord) {
1734 /* fake what brk_string() would do if there were only one word */
1736 av = bmake_malloc((ac + 1) * sizeof(char *));
1737 as = bmake_strdup(str);
1741 av = brk_string(str, &ac, FALSE, &as);
1745 fprintf(debug_file, "VarModify: split \"%s\" into %d words\n",
1749 for (i = 0; i < ac; i++)
1750 addSpace = modProc(ctx, vpstate, av[i], addSpace, &buf, datum);
1755 return Buf_Destroy(&buf, FALSE);
1760 VarWordCompare(const void *a, const void *b)
1762 int r = strcmp(*(const char * const *)a, *(const char * const *)b);
1767 VarWordCompareReverse(const void *a, const void *b)
1769 int r = strcmp(*(const char * const *)b, *(const char * const *)a);
1774 *-----------------------------------------------------------------------
1776 * Order the words in the string.
1779 * str String whose words should be sorted.
1780 * otype How to order: s - sort, x - random.
1783 * A string containing the words ordered.
1788 *-----------------------------------------------------------------------
1791 VarOrder(const char *str, const char otype)
1793 Buffer buf; /* Buffer for the new string */
1794 char **av; /* word list [first word does not count] */
1795 char *as; /* word list memory */
1800 av = brk_string(str, &ac, FALSE, &as);
1804 case 'r': /* reverse sort alphabetically */
1805 qsort(av, ac, sizeof(char *), VarWordCompareReverse);
1807 case 's': /* sort alphabetically */
1808 qsort(av, ac, sizeof(char *), VarWordCompare);
1810 case 'x': /* randomize */
1813 * We will use [ac..2] range for mod factors. This will produce
1814 * random numbers in [(ac-1)..0] interval, and minimal
1815 * reasonable value for mod factor is 2 (the mod 1 will produce
1816 * 0 with probability 1).
1818 for (i = ac - 1; i > 0; i--) {
1819 int rndidx = random() % (i + 1);
1828 for (i = 0; i < ac; i++) {
1829 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1831 Buf_AddByte(&buf, ' ');
1837 return Buf_Destroy(&buf, FALSE);
1842 *-----------------------------------------------------------------------
1844 * Remove adjacent duplicate words.
1847 * str String whose words should be sorted
1850 * A string containing the resulting words.
1855 *-----------------------------------------------------------------------
1858 VarUniq(const char *str)
1860 Buffer buf; /* Buffer for new string */
1861 char **av; /* List of words to affect */
1862 char *as; /* Word list memory */
1866 av = brk_string(str, &ac, FALSE, &as);
1869 for (j = 0, i = 1; i < ac; i++)
1870 if (strcmp(av[i], av[j]) != 0 && (++j != i))
1875 for (i = 0; i < ac; i++) {
1876 Buf_AddBytes(&buf, strlen(av[i]), av[i]);
1878 Buf_AddByte(&buf, ' ');
1884 return Buf_Destroy(&buf, FALSE);
1888 *-----------------------------------------------------------------------
1890 * Return an integer sequence
1893 * str String whose words provide default range
1894 * ac range length, if 0 use str words
1899 *-----------------------------------------------------------------------
1902 VarRange(const char *str, int ac)
1904 Buffer buf; /* Buffer for new string */
1905 char tmp[32]; /* each element */
1906 char **av; /* List of words to affect */
1907 char *as; /* Word list memory */
1915 av = brk_string(str, &ac, FALSE, &as);
1917 for (i = 0; i < ac; i++) {
1918 n = snprintf(tmp, sizeof(tmp), "%d", 1 + i);
1919 if (n >= (int)sizeof(tmp))
1921 Buf_AddBytes(&buf, n, tmp);
1923 Buf_AddByte(&buf, ' ');
1929 return Buf_Destroy(&buf, FALSE);
1934 *-----------------------------------------------------------------------
1936 * During the parsing of a part of a modifier such as :S or :@,
1937 * pass through the tstr looking for 1) escaped delimiters,
1938 * '$'s and backslashes (place the escaped character in
1939 * uninterpreted) and 2) unescaped $'s that aren't before
1940 * the delimiter (expand the variable substitution unless flags
1941 * has VAR_NOSUBST set).
1942 * Return the expanded string or NULL if the delimiter was missing
1943 * If pattern is specified, handle escaped ampersands, and replace
1944 * unescaped ampersands with the lhs of the pattern.
1947 * A string of all the words modified appropriately.
1948 * If length is specified, return the string length of the buffer
1949 * If flags is specified and the last character of the pattern is a
1950 * $ set the VAR_MATCH_END bit of flags.
1954 *-----------------------------------------------------------------------
1957 VarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED,
1958 VarPattern_Flags flags, const char **tstr, int delim,
1959 VarPattern_Flags *vflags, int *length, VarPattern *pattern)
1965 int errnum = flags & VARF_UNDEFERR;
1971 #define IS_A_MATCH(cp, delim) \
1972 ((cp[0] == '\\') && ((cp[1] == delim) || \
1973 (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
1976 * Skim through until the matching delimiter is found;
1977 * pick up variable substitutions on the way. Also allow
1978 * backslashes to quote the delimiter, $, and \, but don't
1979 * touch other backslashes.
1981 for (cp = *tstr; *cp && (*cp != delim); cp++) {
1982 if (IS_A_MATCH(cp, delim)) {
1983 Buf_AddByte(&buf, cp[1]);
1985 } else if (*cp == '$') {
1986 if (cp[1] == delim) {
1988 Buf_AddByte(&buf, *cp);
1991 * Unescaped $ at end of pattern => anchor
1994 *vflags |= VAR_MATCH_END;
1996 if (vflags == NULL || (*vflags & VAR_NOSUBST) == 0) {
2002 * If unescaped dollar sign not before the
2003 * delimiter, assume it's a variable
2004 * substitution and recurse.
2006 cp2 = Var_Parse(cp, ctxt, errnum | (flags & VARF_WANTRES),
2008 Buf_AddBytes(&buf, strlen(cp2), cp2);
2012 const char *cp2 = &cp[1];
2014 if (*cp2 == PROPEN || *cp2 == BROPEN) {
2016 * Find the end of this variable reference
2017 * and suck it in without further ado.
2018 * It will be interpreted later.
2021 int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE;
2024 for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) {
2025 if (cp2[-1] != '\\') {
2032 Buf_AddBytes(&buf, cp2 - cp, cp);
2035 Buf_AddByte(&buf, *cp);
2038 } else if (pattern && *cp == '&')
2039 Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs);
2041 Buf_AddByte(&buf, *cp);
2051 *length = Buf_Size(&buf);
2052 rstr = Buf_Destroy(&buf, FALSE);
2054 fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr);
2059 *-----------------------------------------------------------------------
2061 * Quote shell meta-characters and space characters in the string
2062 * if quoteDollar is set, also quote and double any '$' characters.
2070 *-----------------------------------------------------------------------
2073 VarQuote(char *str, Boolean quoteDollar)
2077 const char *newline;
2080 if ((newline = Shell_GetNewline()) == NULL)
2082 nlen = strlen(newline);
2086 for (; *str != '\0'; str++) {
2088 Buf_AddBytes(&buf, nlen, newline);
2091 if (isspace((unsigned char)*str) || ismeta((unsigned char)*str))
2092 Buf_AddByte(&buf, '\\');
2093 Buf_AddByte(&buf, *str);
2094 if (quoteDollar && *str == '$')
2095 Buf_AddBytes(&buf, 2, "\\$");
2098 str = Buf_Destroy(&buf, FALSE);
2100 fprintf(debug_file, "QuoteMeta: [%s]\n", str);
2105 *-----------------------------------------------------------------------
2107 * Hash the string using the MurmurHash3 algorithm.
2108 * Output is computed using 32bit Little Endian arithmetic.
2111 * str String to modify
2114 * Hash value of str, encoded as 8 hex digits.
2119 *-----------------------------------------------------------------------
2122 VarHash(const char *str)
2124 static const char hexdigits[16] = "0123456789abcdef";
2127 const unsigned char *ustr = (const unsigned char *)str;
2128 uint32_t h, k, c1, c2;
2135 for (len = len2; len; ) {
2139 k = ((uint32_t)ustr[3] << 24) |
2140 ((uint32_t)ustr[2] << 16) |
2141 ((uint32_t)ustr[1] << 8) |
2147 k |= (uint32_t)ustr[2] << 16;
2150 k |= (uint32_t)ustr[1] << 8;
2153 k |= (uint32_t)ustr[0];
2156 c1 = c1 * 5 + 0x7b7d159cU;
2157 c2 = c2 * 5 + 0x6bce6396U;
2159 k = (k << 11) ^ (k >> 21);
2161 h = (h << 13) ^ (h >> 19);
2162 h = h * 5 + 0x52dce729U;
2172 for (len = 0; len < 8; ++len) {
2173 Buf_AddByte(&buf, hexdigits[h & 15]);
2177 return Buf_Destroy(&buf, FALSE);
2181 VarStrftime(const char *fmt, int zulu, time_t utc)
2189 strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc));
2191 buf[sizeof(buf) - 1] = '\0';
2192 return bmake_strdup(buf);
2196 /* const parameters */
2209 const char *cp; /* Secondary pointer into str (place marker
2211 char termc; /* Character which terminated scan */
2212 int cnt; /* Used to count brace pairs when variable in
2213 * in parens or braces */
2215 int modifier; /* that we are processing */
2216 Var_Parse_State parsestate; /* Flags passed to helper functions */
2219 char *newStr; /* New value to return */
2220 } ApplyModifiersState;
2222 /* we now have some modifiers with long names */
2223 #define STRMOD_MATCH(s, want, n) \
2224 (strncmp(s, want, n) == 0 && (s[n] == st->endc || s[n] == ':'))
2225 #define STRMOD_MATCHX(s, want, n) \
2226 (strncmp(s, want, n) == 0 && \
2227 (s[n] == st->endc || s[n] == ':' || s[n] == '='))
2228 #define CHARMOD_MATCH(c) (c == st->endc || c == ':')
2230 /* :@var@...${var}...@ */
2232 ApplyModifier_At(ApplyModifiersState *st) {
2234 VarPattern_Flags vflags = VAR_NOSUBST;
2236 st->cp = ++(st->tstr);
2238 loop.tvar = VarGetPattern(
2239 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2240 &vflags, &loop.tvarLen, NULL);
2241 if (loop.tvar == NULL)
2244 loop.str = VarGetPattern(
2245 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2246 &vflags, &loop.strLen, NULL);
2247 if (loop.str == NULL)
2250 st->termc = *st->cp;
2253 loop.flags = st->flags & (VARF_UNDEFERR | VARF_WANTRES);
2254 loop.ctxt = st->ctxt;
2255 st->newStr = VarModify(
2256 st->ctxt, &st->parsestate, st->nstr, VarLoopExpand, &loop);
2257 Var_Delete(loop.tvar, st->ctxt);
2263 /* :Ddefined or :Uundefined */
2265 ApplyModifier_Defined(ApplyModifiersState *st)
2267 Buffer buf; /* Buffer for patterns */
2270 if (st->flags & VARF_WANTRES) {
2272 if (*st->tstr == 'U')
2273 wantres = ((st->v->flags & VAR_JUNK) != 0);
2275 wantres = ((st->v->flags & VAR_JUNK) == 0);
2276 nflags = st->flags & ~VARF_WANTRES;
2278 nflags |= VARF_WANTRES;
2283 * Pass through tstr looking for 1) escaped delimiters,
2284 * '$'s and backslashes (place the escaped character in
2285 * uninterpreted) and 2) unescaped $'s that aren't before
2286 * the delimiter (expand the variable substitution).
2287 * The result is left in the Buffer buf.
2290 for (st->cp = st->tstr + 1;
2291 *st->cp != st->endc && *st->cp != ':' && *st->cp != '\0';
2293 if (*st->cp == '\\' &&
2294 (st->cp[1] == ':' || st->cp[1] == '$' || st->cp[1] == st->endc ||
2295 st->cp[1] == '\\')) {
2296 Buf_AddByte(&buf, st->cp[1]);
2298 } else if (*st->cp == '$') {
2300 * If unescaped dollar sign, assume it's a
2301 * variable substitution and recurse.
2307 cp2 = Var_Parse(st->cp, st->ctxt, nflags, &len, &freeIt);
2308 Buf_AddBytes(&buf, strlen(cp2), cp2);
2312 Buf_AddByte(&buf, *st->cp);
2316 st->termc = *st->cp;
2318 if ((st->v->flags & VAR_JUNK) != 0)
2319 st->v->flags |= VAR_KEEP;
2320 if (nflags & VARF_WANTRES) {
2321 st->newStr = Buf_Destroy(&buf, FALSE);
2323 st->newStr = st->nstr;
2324 Buf_Destroy(&buf, TRUE);
2330 ApplyModifier_Gmtime(ApplyModifiersState *st)
2335 st->cp = st->tstr + 1; /* make sure it is set */
2336 if (!STRMOD_MATCHX(st->tstr, "gmtime", 6))
2338 if (st->tstr[6] == '=') {
2339 utc = strtoul(&st->tstr[7], &ep, 10);
2343 st->cp = st->tstr + 6;
2345 st->newStr = VarStrftime(st->nstr, 1, utc);
2346 st->termc = *st->cp;
2352 ApplyModifier_Localtime(ApplyModifiersState *st)
2357 st->cp = st->tstr + 1; /* make sure it is set */
2358 if (!STRMOD_MATCHX(st->tstr, "localtime", 9))
2361 if (st->tstr[9] == '=') {
2362 utc = strtoul(&st->tstr[10], &ep, 10);
2366 st->cp = st->tstr + 9;
2368 st->newStr = VarStrftime(st->nstr, 0, utc);
2369 st->termc = *st->cp;
2375 ApplyModifier_Hash(ApplyModifiersState *st)
2377 st->cp = st->tstr + 1; /* make sure it is set */
2378 if (!STRMOD_MATCH(st->tstr, "hash", 4))
2380 st->newStr = VarHash(st->nstr);
2381 st->cp = st->tstr + 4;
2382 st->termc = *st->cp;
2388 ApplyModifier_Path(ApplyModifiersState *st)
2392 if ((st->v->flags & VAR_JUNK) != 0)
2393 st->v->flags |= VAR_KEEP;
2394 gn = Targ_FindNode(st->v->name, TARG_NOCREATE);
2395 if (gn == NULL || gn->type & OP_NOPATH) {
2397 } else if (gn->path) {
2398 st->newStr = bmake_strdup(gn->path);
2400 st->newStr = Dir_FindFile(st->v->name, Suff_FindPath(gn));
2403 st->newStr = bmake_strdup(st->v->name);
2404 st->cp = ++st->tstr;
2405 st->termc = *st->tstr;
2410 ApplyModifier_Exclam(ApplyModifiersState *st)
2419 st->cp = ++st->tstr;
2420 pattern.rhs = VarGetPattern(
2421 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2422 NULL, &pattern.rightLen, NULL);
2423 if (pattern.rhs == NULL)
2425 if (st->flags & VARF_WANTRES)
2426 st->newStr = Cmd_Exec(pattern.rhs, &emsg);
2428 st->newStr = varNoError;
2429 free(UNCONST(pattern.rhs));
2431 Error(emsg, st->nstr);
2432 st->termc = *st->cp;
2434 if (st->v->flags & VAR_JUNK)
2435 st->v->flags |= VAR_KEEP;
2441 ApplyModifier_Range(ApplyModifiersState *st)
2446 st->cp = st->tstr + 1; /* make sure it is set */
2447 if (!STRMOD_MATCHX(st->tstr, "range", 5))
2450 if (st->tstr[5] == '=') {
2451 n = strtoul(&st->tstr[6], &ep, 10);
2455 st->cp = st->tstr + 5;
2457 st->newStr = VarRange(st->nstr, n);
2458 st->termc = *st->cp;
2462 /* :Mpattern or :Npattern */
2464 ApplyModifier_Match(ApplyModifiersState *st)
2467 const char *endpat; /* points just after end of pattern */
2469 Boolean copy; /* pattern should be, or has been, copied */
2477 * In the loop below, ignore ':' unless we are at
2478 * (or back to) the original brace level.
2479 * XXX This will likely not work right if $() and ${}
2482 for (st->cp = st->tstr + 1;
2483 *st->cp != '\0' && !(*st->cp == ':' && nest == 1);
2485 if (*st->cp == '\\' &&
2486 (st->cp[1] == ':' || st->cp[1] == st->endc ||
2487 st->cp[1] == st->startc)) {
2495 if (*st->cp == '(' || *st->cp == '{')
2497 if (*st->cp == ')' || *st->cp == '}') {
2503 st->termc = *st->cp;
2507 * Need to compress the \:'s out of the pattern, so
2508 * allocate enough room to hold the uncompressed
2509 * pattern (note that st->cp started at st->tstr+1, so
2510 * st->cp - st->tstr takes the null byte into account) and
2511 * compress the pattern into the space.
2513 pattern = bmake_malloc(st->cp - st->tstr);
2514 for (cp2 = pattern, st->cp = st->tstr + 1;
2517 if ((*st->cp == '\\') && (st->cp+1 < endpat) &&
2518 (st->cp[1] == ':' || st->cp[1] == st->endc))
2526 * Either Var_Subst or VarModify will need a
2527 * nul-terminated string soon, so construct one now.
2529 pattern = bmake_strndup(st->tstr+1, endpat - (st->tstr + 1));
2532 /* pattern contains embedded '$', so use Var_Subst to expand it. */
2534 pattern = Var_Subst(NULL, cp2, st->ctxt, st->flags);
2538 fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n",
2539 st->v->name, st->nstr, pattern);
2540 if (*st->tstr == 'M') {
2541 st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, VarMatch,
2544 st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, VarNoMatch,
2552 ApplyModifier_Subst(ApplyModifiersState *st)
2555 Var_Parse_State tmpparsestate;
2558 tmpparsestate = st->parsestate;
2559 st->delim = st->tstr[1];
2563 * If pattern begins with '^', it is anchored to the
2564 * start of the word -- skip over it and flag pattern.
2566 if (*st->tstr == '^') {
2567 pattern.flags |= VAR_MATCH_START;
2572 pattern.lhs = VarGetPattern(
2573 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2574 &pattern.flags, &pattern.leftLen, NULL);
2575 if (pattern.lhs == NULL)
2578 pattern.rhs = VarGetPattern(
2579 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2580 NULL, &pattern.rightLen, &pattern);
2581 if (pattern.rhs == NULL)
2585 * Check for global substitution. If 'g' after the final
2586 * delimiter, substitution is global and is marked that
2592 pattern.flags |= VAR_SUB_GLOBAL;
2595 pattern.flags |= VAR_SUB_ONE;
2598 tmpparsestate.oneBigWord = TRUE;
2604 st->termc = *st->cp;
2605 st->newStr = VarModify(
2606 st->ctxt, &tmpparsestate, st->nstr, VarSubstitute, &pattern);
2608 /* Free the two strings. */
2609 free(UNCONST(pattern.lhs));
2610 free(UNCONST(pattern.rhs));
2618 ApplyModifier_Regex(ApplyModifiersState *st)
2620 VarREPattern pattern;
2623 Var_Parse_State tmpparsestate;
2626 tmpparsestate = st->parsestate;
2627 st->delim = st->tstr[1];
2633 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2638 pattern.replace = VarGetPattern(
2639 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2641 if (pattern.replace == NULL) {
2649 pattern.flags |= VAR_SUB_GLOBAL;
2652 pattern.flags |= VAR_SUB_ONE;
2655 tmpparsestate.oneBigWord = TRUE;
2661 st->termc = *st->cp;
2663 error = regcomp(&pattern.re, re, REG_EXTENDED);
2666 *st->lengthPtr = st->cp - st->start + 1;
2667 VarREError(error, &pattern.re, "RE substitution error");
2668 free(pattern.replace);
2672 pattern.nsub = pattern.re.re_nsub + 1;
2673 if (pattern.nsub < 1)
2675 if (pattern.nsub > 10)
2677 pattern.matches = bmake_malloc(pattern.nsub * sizeof(regmatch_t));
2678 st->newStr = VarModify(
2679 st->ctxt, &tmpparsestate, st->nstr, VarRESubstitute, &pattern);
2680 regfree(&pattern.re);
2681 free(pattern.replace);
2682 free(pattern.matches);
2688 /* :tA, :tu, :tl, etc. */
2690 ApplyModifier_To(ApplyModifiersState *st)
2692 st->cp = st->tstr + 1; /* make sure it is set */
2693 if (st->tstr[1] != st->endc && st->tstr[1] != ':') {
2694 if (st->tstr[1] == 's') {
2695 /* Use the char (if any) at st->tstr[2] as the word separator. */
2698 if (st->tstr[2] != st->endc &&
2699 (st->tstr[3] == st->endc || st->tstr[3] == ':')) {
2700 /* ":ts<unrecognised><endc>" or
2701 * ":ts<unrecognised>:" */
2702 st->parsestate.varSpace = st->tstr[2];
2703 st->cp = st->tstr + 3;
2704 } else if (st->tstr[2] == st->endc || st->tstr[2] == ':') {
2705 /* ":ts<endc>" or ":ts:" */
2706 st->parsestate.varSpace = 0; /* no separator */
2707 st->cp = st->tstr + 2;
2708 } else if (st->tstr[2] == '\\') {
2709 const char *xp = &st->tstr[3];
2710 int base = 8; /* assume octal */
2712 switch (st->tstr[3]) {
2714 st->parsestate.varSpace = '\n';
2715 st->cp = st->tstr + 4;
2718 st->parsestate.varSpace = '\t';
2719 st->cp = st->tstr + 4;
2729 if (isdigit((unsigned char)st->tstr[3])) {
2732 st->parsestate.varSpace = strtoul(xp, &ep, base);
2733 if (*ep != ':' && *ep != st->endc)
2737 /* ":ts<backslash><unrecognised>". */
2743 /* Found ":ts<unrecognised><unrecognised>". */
2747 st->termc = *st->cp;
2750 * We cannot be certain that VarModify will be used - even if there
2751 * is a subsequent modifier, so do a no-op VarSubstitute now to for
2752 * str to be re-expanded without the spaces.
2754 pattern.flags = VAR_SUB_ONE;
2755 pattern.lhs = pattern.rhs = "\032";
2756 pattern.leftLen = pattern.rightLen = 1;
2758 st->newStr = VarModify(
2759 st->ctxt, &st->parsestate, st->nstr, VarSubstitute, &pattern);
2760 } else if (st->tstr[2] == st->endc || st->tstr[2] == ':') {
2761 /* Check for two-character options: ":tu", ":tl" */
2762 if (st->tstr[1] == 'A') { /* absolute path */
2763 st->newStr = VarModify(
2764 st->ctxt, &st->parsestate, st->nstr, VarRealpath, NULL);
2765 st->cp = st->tstr + 2;
2766 st->termc = *st->cp;
2767 } else if (st->tstr[1] == 'u') {
2768 char *dp = bmake_strdup(st->nstr);
2769 for (st->newStr = dp; *dp; dp++)
2770 *dp = toupper((unsigned char)*dp);
2771 st->cp = st->tstr + 2;
2772 st->termc = *st->cp;
2773 } else if (st->tstr[1] == 'l') {
2774 char *dp = bmake_strdup(st->nstr);
2775 for (st->newStr = dp; *dp; dp++)
2776 *dp = tolower((unsigned char)*dp);
2777 st->cp = st->tstr + 2;
2778 st->termc = *st->cp;
2779 } else if (st->tstr[1] == 'W' || st->tstr[1] == 'w') {
2780 st->parsestate.oneBigWord = (st->tstr[1] == 'W');
2781 st->newStr = st->nstr;
2782 st->cp = st->tstr + 2;
2783 st->termc = *st->cp;
2785 /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */
2789 /* Found ":t<unrecognised><unrecognised>". */
2793 /* Found ":t<endc>" or ":t:". */
2799 /* :[#], :[1], etc. */
2801 ApplyModifier_Words(ApplyModifiersState *st)
2804 * Look for the closing ']', recursively
2805 * expanding any embedded variables.
2807 * estr is a pointer to the expanded result,
2808 * which we must free().
2812 st->cp = st->tstr + 1; /* point to char after '[' */
2813 st->delim = ']'; /* look for closing ']' */
2814 estr = VarGetPattern(
2815 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2818 return 'c'; /* report missing ']' */
2819 /* now st->cp points just after the closing ']' */
2821 if (st->cp[0] != ':' && st->cp[0] != st->endc) {
2822 /* Found junk after ']' */
2826 if (estr[0] == '\0') {
2827 /* Found empty square brackets in ":[]". */
2830 } else if (estr[0] == '#' && estr[1] == '\0') {
2834 * We will need enough space for the decimal
2835 * representation of an int. We calculate the
2836 * space needed for the octal representation,
2837 * and add enough slop to cope with a '-' sign
2838 * (which should never be needed) and a '\0'
2839 * string terminator.
2841 int newStrSize = (sizeof(int) * CHAR_BIT + 2) / 3 + 2;
2843 st->newStr = bmake_malloc(newStrSize);
2844 if (st->parsestate.oneBigWord) {
2845 strncpy(st->newStr, "1", newStrSize);
2847 /* XXX: brk_string() is a rather expensive
2848 * way of counting words. */
2853 av = brk_string(st->nstr, &ac, FALSE, &as);
2854 snprintf(st->newStr, newStrSize, "%d", ac);
2858 st->termc = *st->cp;
2861 } else if (estr[0] == '*' && estr[1] == '\0') {
2863 st->parsestate.oneBigWord = TRUE;
2864 st->newStr = st->nstr;
2865 st->termc = *st->cp;
2868 } else if (estr[0] == '@' && estr[1] == '\0') {
2870 st->parsestate.oneBigWord = FALSE;
2871 st->newStr = st->nstr;
2872 st->termc = *st->cp;
2878 * We expect estr to contain a single
2879 * integer for :[N], or two integers
2880 * separated by ".." for :[start..end].
2882 VarSelectWords_t seldata = { 0, 0 };
2884 seldata.start = strtol(estr, &ep, 0);
2886 /* Found junk instead of a number */
2889 } else if (ep[0] == '\0') {
2890 /* Found only one integer in :[N] */
2891 seldata.end = seldata.start;
2892 } else if (ep[0] == '.' && ep[1] == '.' && ep[2] != '\0') {
2893 /* Expecting another integer after ".." */
2895 seldata.end = strtol(ep, &ep, 0);
2896 if (ep[0] != '\0') {
2897 /* Found junk after ".." */
2902 /* Found junk instead of ".." */
2907 * Now seldata is properly filled in,
2908 * but we still have to check for 0 as
2911 if (seldata.start == 0 && seldata.end == 0) {
2912 /* ":[0]" or perhaps ":[0..0]" */
2913 st->parsestate.oneBigWord = TRUE;
2914 st->newStr = st->nstr;
2915 st->termc = *st->cp;
2918 } else if (seldata.start == 0 || seldata.end == 0) {
2919 /* ":[0..N]" or ":[N..0]" */
2923 /* Normal case: select the words described by seldata. */
2924 st->newStr = VarSelectWords(
2925 st->ctxt, &st->parsestate, st->nstr, &seldata);
2927 st->termc = *st->cp;
2935 ApplyModifier_Order(ApplyModifiersState *st)
2939 st->cp = st->tstr + 1; /* skip to the rest in any case */
2940 if (st->tstr[1] == st->endc || st->tstr[1] == ':') {
2942 st->termc = *st->cp;
2943 } else if ((st->tstr[1] == 'r' || st->tstr[1] == 'x') &&
2944 (st->tstr[2] == st->endc || st->tstr[2] == ':')) {
2945 otype = st->tstr[1];
2946 st->cp = st->tstr + 2;
2947 st->termc = *st->cp;
2951 st->newStr = VarOrder(st->nstr, otype);
2955 /* :? then : else */
2957 ApplyModifier_IfElse(ApplyModifiersState *st)
2962 VarPattern_Flags lhs_flags, rhs_flags;
2964 /* find ':', and then substitute accordingly */
2965 if (st->flags & VARF_WANTRES) {
2966 cond_rc = Cond_EvalExpression(NULL, st->v->name, &value, 0, FALSE);
2967 if (cond_rc == COND_INVALID) {
2968 lhs_flags = rhs_flags = VAR_NOSUBST;
2971 rhs_flags = VAR_NOSUBST;
2973 lhs_flags = VAR_NOSUBST;
2977 /* we are just consuming and discarding */
2978 cond_rc = value = 0;
2979 lhs_flags = rhs_flags = VAR_NOSUBST;
2983 st->cp = ++st->tstr;
2985 pattern.lhs = VarGetPattern(
2986 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2987 &lhs_flags, &pattern.leftLen, NULL);
2988 if (pattern.lhs == NULL)
2991 /* BROPEN or PROPEN */
2992 st->delim = st->endc;
2993 pattern.rhs = VarGetPattern(
2994 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
2995 &rhs_flags, &pattern.rightLen, NULL);
2996 if (pattern.rhs == NULL)
2999 st->termc = *--st->cp;
3001 if (cond_rc == COND_INVALID) {
3002 Error("Bad conditional expression `%s' in %s?%s:%s",
3003 st->v->name, st->v->name, pattern.lhs, pattern.rhs);
3008 st->newStr = UNCONST(pattern.lhs);
3009 free(UNCONST(pattern.rhs));
3011 st->newStr = UNCONST(pattern.rhs);
3012 free(UNCONST(pattern.lhs));
3014 if (st->v->flags & VAR_JUNK)
3015 st->v->flags |= VAR_KEEP;
3019 /* "::=", "::!=", "::+=", or "::?=" */
3021 ApplyModifier_Assign(ApplyModifiersState *st)
3023 if (st->tstr[1] == '=' ||
3024 (st->tstr[2] == '=' &&
3025 (st->tstr[1] == '!' || st->tstr[1] == '+' || st->tstr[1] == '?'))) {
3026 GNode *v_ctxt; /* context where v belongs */
3031 VarPattern_Flags vflags;
3033 if (st->v->name[0] == 0)
3039 if (st->v->flags & VAR_JUNK) {
3041 * We need to bmake_strdup() it incase
3042 * VarGetPattern() recurses.
3044 sv_name = st->v->name;
3045 st->v->name = bmake_strdup(st->v->name);
3046 } else if (st->ctxt != VAR_GLOBAL) {
3047 Var *gv = VarFind(st->v->name, st->ctxt, 0);
3049 v_ctxt = VAR_GLOBAL;
3051 VarFreeEnv(gv, TRUE);
3054 switch ((how = *st->tstr)) {
3058 st->cp = &st->tstr[2];
3061 st->cp = ++st->tstr;
3064 st->delim = st->startc == PROPEN ? PRCLOSE : BRCLOSE;
3067 vflags = (st->flags & VARF_WANTRES) ? 0 : VAR_NOSUBST;
3068 pattern.rhs = VarGetPattern(
3069 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
3070 &vflags, &pattern.rightLen, NULL);
3071 if (st->v->flags & VAR_JUNK) {
3072 /* restore original name */
3074 st->v->name = sv_name;
3076 if (pattern.rhs == NULL)
3079 st->termc = *--st->cp;
3082 if (st->flags & VARF_WANTRES) {
3085 Var_Append(st->v->name, pattern.rhs, v_ctxt);
3088 st->newStr = Cmd_Exec(pattern.rhs, &emsg);
3090 Error(emsg, st->nstr);
3092 Var_Set(st->v->name, st->newStr, v_ctxt);
3096 if ((st->v->flags & VAR_JUNK) == 0)
3100 Var_Set(st->v->name, pattern.rhs, v_ctxt);
3104 free(UNCONST(pattern.rhs));
3105 st->newStr = varNoError;
3108 return 'd'; /* "::<unrecognised>" */
3111 /* remember current value */
3113 ApplyModifier_Remember(ApplyModifiersState *st)
3115 st->cp = st->tstr + 1; /* make sure it is set */
3116 if (!STRMOD_MATCHX(st->tstr, "_", 1))
3119 if (st->tstr[1] == '=') {
3124 n = strcspn(st->cp, ":)}");
3125 np = bmake_strndup(st->cp, n + 1);
3127 st->cp = st->tstr + 2 + n;
3128 Var_Set(np, st->nstr, st->ctxt);
3131 Var_Set("_", st->nstr, st->ctxt);
3133 st->newStr = st->nstr;
3134 st->termc = *st->cp;
3141 ApplyModifier_SysV(ApplyModifiersState *st)
3144 * This can either be a bogus modifier or a System-V
3145 * substitution command.
3148 Boolean eqFound = FALSE;
3153 * First we make a pass through the string trying
3154 * to verify it is a SYSV-make-style translation:
3155 * it must be: <string1>=<string2>)
3159 while (*st->cp != '\0' && st->cnt) {
3160 if (*st->cp == '=') {
3162 /* continue looking for st->endc */
3163 } else if (*st->cp == st->endc)
3165 else if (*st->cp == st->startc)
3170 if (*st->cp != st->endc || !eqFound)
3175 pattern.lhs = VarGetPattern(
3176 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
3177 &pattern.flags, &pattern.leftLen, NULL);
3178 if (pattern.lhs == NULL)
3181 st->delim = st->endc;
3182 pattern.rhs = VarGetPattern(
3183 st->ctxt, &st->parsestate, st->flags, &st->cp, st->delim,
3184 NULL, &pattern.rightLen, &pattern);
3185 if (pattern.rhs == NULL)
3189 * SYSV modifications happen through the whole
3190 * string. Note the pattern is anchored at the end.
3192 st->termc = *--st->cp;
3194 if (pattern.leftLen == 0 && *st->nstr == '\0') {
3195 st->newStr = st->nstr; /* special case */
3197 st->newStr = VarModify(
3198 st->ctxt, &st->parsestate, st->nstr, VarSYSVMatch, &pattern);
3200 free(UNCONST(pattern.lhs));
3201 free(UNCONST(pattern.rhs));
3207 * Now we need to apply any modifiers the user wants applied.
3209 * :M<pattern> words which match the given <pattern>.
3210 * <pattern> is of the standard file
3212 * :N<pattern> words which do not match the given <pattern>.
3213 * :S<d><pat1><d><pat2><d>[1gW]
3214 * Substitute <pat2> for <pat1> in the value
3215 * :C<d><pat1><d><pat2><d>[1gW]
3216 * Substitute <pat2> for regex <pat1> in the value
3217 * :H Substitute the head of each word
3218 * :T Substitute the tail of each word
3219 * :E Substitute the extension (minus '.') of
3221 * :R Substitute the root of each word
3222 * (pathname minus the suffix).
3223 * :O ("Order") Alphabeticaly sort words in variable.
3224 * :Ox ("intermiX") Randomize words in variable.
3225 * :u ("uniq") Remove adjacent duplicate words.
3226 * :tu Converts the variable contents to uppercase.
3227 * :tl Converts the variable contents to lowercase.
3228 * :ts[c] Sets varSpace - the char used to
3229 * separate words to 'c'. If 'c' is
3230 * omitted then no separation is used.
3231 * :tW Treat the variable contents as a single
3232 * word, even if it contains spaces.
3233 * (Mnemonic: one big 'W'ord.)
3234 * :tw Treat the variable contents as multiple
3235 * space-separated words.
3236 * (Mnemonic: many small 'w'ords.)
3237 * :[index] Select a single word from the value.
3238 * :[start..end] Select multiple words from the value.
3239 * :[*] or :[0] Select the entire value, as a single
3240 * word. Equivalent to :tW.
3241 * :[@] Select the entire value, as multiple
3242 * words. Undoes the effect of :[*].
3243 * Equivalent to :tw.
3244 * :[#] Returns the number of words in the value.
3246 * :?<true-value>:<false-value>
3247 * If the variable evaluates to true, return
3248 * true value, else return the second value.
3249 * :lhs=rhs Like :S, but the rhs goes to the end of
3251 * :sh Treat the current value as a command
3252 * to be run, new value is its output.
3253 * The following added so we can handle ODE makefiles.
3254 * :@<tmpvar>@<newval>@
3255 * Assign a temporary local variable <tmpvar>
3256 * to the current value of each word in turn
3257 * and replace each word with the result of
3258 * evaluating <newval>
3259 * :D<newval> Use <newval> as value if variable defined
3260 * :U<newval> Use <newval> as value if variable undefined
3261 * :L Use the name of the variable as the value.
3262 * :P Use the path of the node that has the same
3263 * name as the variable as the value. This
3264 * basically includes an implied :L so that
3265 * the common method of refering to the path
3266 * of your dependent 'x' in a rule is to use
3267 * the form '${x:P}'.
3268 * :!<cmd>! Run cmd much the same as :sh run's the
3269 * current value of the variable.
3270 * The ::= modifiers, actually assign a value to the variable.
3271 * Their main purpose is in supporting modifiers of .for loop
3272 * iterators and other obscure uses. They always expand to
3273 * nothing. In a target rule that would otherwise expand to an
3274 * empty line they can be preceded with @: to keep make happy.
3278 * .for i in ${.TARGET} ${.TARGET:R}.gz
3283 * ::=<str> Assigns <str> as the new value of variable.
3284 * ::?=<str> Assigns <str> as value of variable if
3285 * it was not already set.
3286 * ::+=<str> Appends <str> to variable.
3287 * ::!=<cmd> Assigns output of <cmd> as the new value of
3291 ApplyModifiers(char *nstr, const char *tstr,
3292 int const startc, int const endc,
3293 Var * const v, GNode * const ctxt, int const flags,
3294 int * const lengthPtr, void ** const freePtr)
3296 ApplyModifiersState st = {
3297 startc, endc, v, ctxt, flags, lengthPtr, freePtr,
3298 nstr, tstr, tstr, tstr,
3299 '\0', 0, '\0', 0, {' ', FALSE}, NULL
3302 while (*st.tstr && *st.tstr != st.endc) {
3304 if (*st.tstr == '$') {
3306 * We may have some complex modifiers in a variable.
3313 rval = Var_Parse(st.tstr, st.ctxt, st.flags, &rlen, &freeIt);
3316 * If we have not parsed up to st.endc or ':',
3317 * we are not interested.
3319 if (rval != NULL && *rval &&
3320 (c = st.tstr[rlen]) != '\0' &&
3328 fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n",
3329 rval, rlen, st.tstr, rlen, st.tstr + rlen);
3334 if (rval != NULL && *rval) {
3337 st.nstr = ApplyModifiers(st.nstr, rval, 0, 0, st.v,
3338 st.ctxt, st.flags, &used, st.freePtr);
3339 if (st.nstr == var_Error
3340 || (st.nstr == varNoError && (st.flags & VARF_UNDEFERR) == 0)
3341 || strlen(rval) != (size_t) used) {
3343 goto out; /* error already reported */
3347 if (*st.tstr == ':')
3349 else if (!*st.tstr && st.endc) {
3350 Error("Unclosed variable specification after complex "
3351 "modifier (expecting '%c') for %s", st.endc, st.v->name);
3358 fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", st.v->name,
3361 st.newStr = var_Error;
3362 switch ((st.modifier = *st.tstr)) {
3365 int res = ApplyModifier_Assign(&st);
3375 ApplyModifier_At(&st);
3378 if (!ApplyModifier_Remember(&st))
3383 ApplyModifier_Defined(&st);
3387 if ((st.v->flags & VAR_JUNK) != 0)
3388 st.v->flags |= VAR_KEEP;
3389 st.newStr = bmake_strdup(st.v->name);
3391 st.termc = *st.tstr;
3395 ApplyModifier_Path(&st);
3398 if (!ApplyModifier_Exclam(&st))
3403 int res = ApplyModifier_Words(&st);
3411 if (!ApplyModifier_Gmtime(&st))
3415 if (!ApplyModifier_Hash(&st))
3419 if (!ApplyModifier_Localtime(&st))
3423 if (!ApplyModifier_To(&st))
3428 ApplyModifier_Match(&st);
3431 if (!ApplyModifier_Subst(&st))
3435 if (!ApplyModifier_IfElse(&st))
3440 if (!ApplyModifier_Regex(&st))
3446 if (st.tstr[1] == st.endc || st.tstr[1] == ':') {
3447 st.newStr = VarQuote(st.nstr, st.modifier == 'q');
3448 st.cp = st.tstr + 1;
3454 if (st.tstr[1] == st.endc || st.tstr[1] == ':') {
3455 st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarTail,
3457 st.cp = st.tstr + 1;
3463 if (st.tstr[1] == st.endc || st.tstr[1] == ':') {
3464 st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarHead,
3466 st.cp = st.tstr + 1;
3472 if (st.tstr[1] == st.endc || st.tstr[1] == ':') {
3473 st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarSuffix,
3475 st.cp = st.tstr + 1;
3481 if (st.tstr[1] == st.endc || st.tstr[1] == ':') {
3482 st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarRoot,
3484 st.cp = st.tstr + 1;
3490 if (!ApplyModifier_Range(&st))
3494 if (!ApplyModifier_Order(&st))
3498 if (st.tstr[1] == st.endc || st.tstr[1] == ':') {
3499 st.newStr = VarUniq(st.nstr);
3500 st.cp = st.tstr + 1;
3507 if (st.tstr[1] == 'h' && (st.tstr[2] == st.endc || st.tstr[2] == ':')) {
3509 if (st.flags & VARF_WANTRES) {
3510 st.newStr = Cmd_Exec(st.nstr, &emsg);
3512 Error(emsg, st.nstr);
3514 st.newStr = varNoError;
3515 st.cp = st.tstr + 2;
3525 int res = ApplyModifier_SysV(&st);
3531 Error("Unknown modifier '%c'", *st.tstr);
3532 for (st.cp = st.tstr+1;
3533 *st.cp != ':' && *st.cp != st.endc && *st.cp != '\0';
3537 st.newStr = var_Error;
3542 fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n",
3543 st.v->name, st.modifier, st.newStr);
3546 if (st.newStr != st.nstr) {
3551 st.nstr = st.newStr;
3552 if (st.nstr != var_Error && st.nstr != varNoError) {
3553 *st.freePtr = st.nstr;
3556 if (st.termc == '\0' && st.endc != '\0') {
3557 Error("Unclosed variable specification (expecting '%c') "
3558 "for \"%s\" (value \"%s\") modifier %c",
3559 st.endc, st.v->name, st.nstr, st.modifier);
3560 } else if (st.termc == ':') {
3566 *st.lengthPtr = st.tstr - st.start;
3571 Error("Bad modifier `:%.*s' for %s", (int)strcspn(st.tstr, ":)}"), st.tstr,
3575 *st.lengthPtr = st.cp - st.start;
3576 if (st.delim != '\0')
3577 Error("Unclosed substitution for %s (%c missing)",
3578 st.v->name, st.delim);
3585 *-----------------------------------------------------------------------
3587 * Given the start of a variable invocation, extract the variable
3588 * name and find its value, then modify it according to the
3592 * str The string to parse
3593 * ctxt The context for the variable
3594 * flags VARF_UNDEFERR if undefineds are an error
3595 * VARF_WANTRES if we actually want the result
3596 * VARF_ASSIGN if we are in a := assignment
3597 * lengthPtr OUT: The length of the specification
3598 * freePtr OUT: Non-NULL if caller should free *freePtr
3601 * The (possibly-modified) value of the variable or var_Error if the
3602 * specification is invalid. The length of the specification is
3603 * placed in *lengthPtr (for invalid specifications, this is just
3605 * If *freePtr is non-NULL then it's a pointer that the caller
3606 * should pass to free() to free memory used by the result.
3611 *-----------------------------------------------------------------------
3613 /* coverity[+alloc : arg-*4] */
3615 Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
3616 int *lengthPtr, void **freePtr)
3618 const char *tstr; /* Pointer into str */
3619 Var *v; /* Variable in invocation */
3620 Boolean haveModifier; /* TRUE if have modifiers for the variable */
3621 char endc; /* Ending character when variable in parens
3623 char startc; /* Starting character when variable in parens
3625 int vlen; /* Length of variable name */
3626 const char *start; /* Points to original start of str */
3627 char *nstr; /* New string, used during expansion */
3628 Boolean dynamic; /* TRUE if the variable is local and we're
3629 * expanding it in a non-local context. This
3630 * is done to support dynamic sources. The
3631 * result is just the invocation, unaltered */
3632 const char *extramodifiers; /* extra modifiers to apply first */
3636 extramodifiers = NULL;
3641 if (startc != PROPEN && startc != BROPEN) {
3643 * If it's not bounded by braces of some sort, life is much simpler.
3644 * We just need to check for the first character and return the
3645 * value if it exists.
3648 /* Error out some really stupid names */
3649 if (startc == '\0' || strchr(")}:$", startc)) {
3656 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3660 if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
3662 * If substituting a local variable in a non-local context,
3663 * assume it's for dynamic source stuff. We have to handle
3664 * this specially and return the longhand for the variable
3665 * with the dollar sign escaped so it makes it back to the
3666 * caller. Only four of the local variables are treated
3667 * specially as they are the only four that will be set
3668 * when dynamic sources are expanded.
3672 return UNCONST("$(.TARGET)");
3674 return UNCONST("$(.MEMBER)");
3676 return UNCONST("$(.PREFIX)");
3678 return UNCONST("$(.ARCHIVE)");
3681 return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3683 haveModifier = FALSE;
3688 Buffer buf; /* Holds the variable name */
3691 endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
3695 * Skip to the end character or a colon, whichever comes first.
3697 for (tstr = str + 2; *tstr != '\0'; tstr++) {
3698 /* Track depth so we can spot parse errors. */
3699 if (*tstr == startc)
3701 if (*tstr == endc) {
3705 if (depth == 1 && *tstr == ':')
3707 /* A variable inside a variable, expand. */
3711 char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
3713 Buf_AddBytes(&buf, strlen(rval), rval);
3717 Buf_AddByte(&buf, *tstr);
3720 haveModifier = TRUE;
3721 } else if (*tstr == endc) {
3722 haveModifier = FALSE;
3725 * If we never did find the end character, return NULL
3726 * right now, setting the length to be the distance to
3727 * the end of the string, since that's what make does.
3729 *lengthPtr = tstr - str;
3730 Buf_Destroy(&buf, TRUE);
3733 str = Buf_GetAll(&buf, &vlen);
3736 * At this point, str points into newly allocated memory from
3737 * buf, containing only the name of the variable.
3739 * start and tstr point into the const string that was pointed
3740 * to by the original value of the str parameter. start points
3741 * to the '$' at the beginning of the string, while tstr points
3742 * to the char just after the end of the variable name -- this
3743 * will be '\0', ':', PRCLOSE, or BRCLOSE.
3746 v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3748 * Check also for bogus D and F forms of local variables since we're
3749 * in a local context and the name is the right length.
3751 if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
3752 (vlen == 2) && (str[1] == 'F' || str[1] == 'D') &&
3753 strchr("@%?*!<>", str[0]) != NULL) {
3755 * Well, it's local -- go look for it.
3759 v = VarFind(name, ctxt, 0);
3762 if (str[1] == 'D') {
3763 extramodifiers = "H:";
3765 extramodifiers = "T:";
3772 (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) &&
3773 ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
3776 * If substituting a local variable in a non-local context,
3777 * assume it's for dynamic source stuff. We have to handle
3778 * this specially and return the longhand for the variable
3779 * with the dollar sign escaped so it makes it back to the
3780 * caller. Only four of the local variables are treated
3781 * specially as they are the only four that will be set
3782 * when dynamic sources are expanded.
3792 } else if (vlen > 2 && *str == '.' &&
3793 isupper((unsigned char) str[1]) &&
3794 (ctxt == VAR_CMD || ctxt == VAR_GLOBAL))
3797 if ((strncmp(str, ".TARGET", len) == 0) ||
3798 (strncmp(str, ".ARCHIVE", len) == 0) ||
3799 (strncmp(str, ".PREFIX", len) == 0) ||
3800 (strncmp(str, ".MEMBER", len) == 0))
3806 if (!haveModifier) {
3808 * No modifiers -- have specification length so we can return
3811 *lengthPtr = tstr - start + 1;
3813 char *pstr = bmake_strndup(start, *lengthPtr);
3815 Buf_Destroy(&buf, TRUE);
3818 Buf_Destroy(&buf, TRUE);
3819 return (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3823 * Still need to get to the end of the variable specification,
3824 * so kludge up a Var structure for the modifications
3826 v = bmake_malloc(sizeof(Var));
3827 v->name = UNCONST(str);
3828 Buf_Init(&v->val, 1);
3829 v->flags = VAR_JUNK;
3830 Buf_Destroy(&buf, FALSE);
3833 Buf_Destroy(&buf, TRUE);
3836 if (v->flags & VAR_IN_USE) {
3837 Fatal("Variable %s is recursive.", v->name);
3840 v->flags |= VAR_IN_USE;
3843 * Before doing any modification, we have to make sure the value
3844 * has been fully expanded. If it looks like recursion might be
3845 * necessary (there's a dollar sign somewhere in the variable's value)
3846 * we just call Var_Subst to do any other substitutions that are
3847 * necessary. Note that the value returned by Var_Subst will have
3848 * been dynamically-allocated, so it will need freeing when we
3851 nstr = Buf_GetAll(&v->val, NULL);
3852 if (strchr(nstr, '$') != NULL && (flags & VARF_WANTRES) != 0) {
3853 nstr = Var_Subst(NULL, nstr, ctxt, flags);
3857 v->flags &= ~VAR_IN_USE;
3859 if (nstr != NULL && (haveModifier || extramodifiers != NULL)) {
3864 if (extramodifiers != NULL) {
3865 nstr = ApplyModifiers(nstr, extramodifiers, '(', ')',
3866 v, ctxt, flags, &used, &extraFree);
3870 /* Skip initial colon. */
3873 nstr = ApplyModifiers(nstr, tstr, startc, endc,
3874 v, ctxt, flags, &used, freePtr);
3878 *freePtr = extraFree;
3881 *lengthPtr = tstr - start + (*tstr ? 1 : 0);
3883 if (v->flags & VAR_FROM_ENV) {
3884 Boolean destroy = FALSE;
3886 if (nstr != Buf_GetAll(&v->val, NULL)) {
3890 * Returning the value unmodified, so tell the caller to free
3895 VarFreeEnv(v, destroy);
3896 } else if (v->flags & VAR_JUNK) {
3898 * Perform any free'ing needed and set *freePtr to NULL so the caller
3899 * doesn't try to free a static pointer.
3900 * If VAR_KEEP is also set then we want to keep str as is.
3902 if (!(v->flags & VAR_KEEP)) {
3908 nstr = bmake_strndup(start, *lengthPtr);
3911 nstr = (flags & VARF_UNDEFERR) ? var_Error : varNoError;
3914 if (nstr != Buf_GetAll(&v->val, NULL))
3915 Buf_Destroy(&v->val, TRUE);
3923 *-----------------------------------------------------------------------
3925 * Substitute for all variables in the given string in the given context.
3926 * If flags & VARF_UNDEFERR, Parse_Error will be called when an undefined
3927 * variable is encountered.
3930 * var Named variable || NULL for all
3931 * str the string which to substitute
3932 * ctxt the context wherein to find variables
3933 * flags VARF_UNDEFERR if undefineds are an error
3934 * VARF_WANTRES if we actually want the result
3935 * VARF_ASSIGN if we are in a := assignment
3938 * The resulting string.
3942 *-----------------------------------------------------------------------
3945 Var_Subst(const char *var, const char *str, GNode *ctxt, Varf_Flags flags)
3947 Buffer buf; /* Buffer for forming things */
3948 char *val; /* Value to substitute for a variable */
3949 int length; /* Length of the variable invocation */
3950 Boolean trailingBslash; /* variable ends in \ */
3951 void *freeIt = NULL; /* Set if it should be freed */
3952 static Boolean errorReported; /* Set true if an error has already
3953 * been reported to prevent a plethora
3954 * of messages when recursing */
3957 errorReported = FALSE;
3958 trailingBslash = FALSE;
3961 if (*str == '\n' && trailingBslash)
3962 Buf_AddByte(&buf, ' ');
3963 if (var == NULL && (*str == '$') && (str[1] == '$')) {
3965 * A dollar sign may be escaped either with another dollar sign.
3966 * In such a case, we skip over the escape character and store the
3967 * dollar sign into the buffer directly.
3969 if (save_dollars && (flags & VARF_ASSIGN))
3970 Buf_AddByte(&buf, *str);
3972 Buf_AddByte(&buf, *str);
3974 } else if (*str != '$') {
3976 * Skip as many characters as possible -- either to the end of
3977 * the string or to the next dollar sign (variable invocation).
3981 for (cp = str++; *str != '$' && *str != '\0'; str++)
3983 Buf_AddBytes(&buf, str - cp, cp);
3988 if (str[1] == '\0') {
3989 /* A trailing $ is kind of a special case */
3990 Buf_AddByte(&buf, str[0]);
3993 } else if (str[1] != PROPEN && str[1] != BROPEN) {
3994 if (str[1] != *var || strlen(var) > 1) {
3995 Buf_AddBytes(&buf, 2, str);
4004 /* Scan up to the end of the variable name. */
4005 for (p = &str[2]; *p &&
4006 *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++)
4010 * A variable inside the variable. We cannot expand
4011 * the external variable yet, so we try again with
4015 Buf_AddBytes(&buf, p - str, str);
4020 if (strncmp(var, str + 2, p - str - 2) != 0 ||
4021 var[p - str - 2] != '\0') {
4023 * Not the variable we want to expand, scan
4024 * until the next variable
4026 for (; *p != '$' && *p != '\0'; p++)
4028 Buf_AddBytes(&buf, p - str, str);
4040 val = Var_Parse(str, ctxt, flags, &length, &freeIt);
4043 * When we come down here, val should either point to the
4044 * value of this variable, suitably modified, or be NULL.
4045 * Length should be the total length of the potential
4046 * variable invocation (from $ to end character...)
4048 if (val == var_Error || val == varNoError) {
4050 * If performing old-time variable substitution, skip over
4051 * the variable and continue with the substitution. Otherwise,
4052 * store the dollar sign and advance str so we continue with
4057 } else if ((flags & VARF_UNDEFERR) || val == var_Error) {
4059 * If variable is undefined, complain and skip the
4060 * variable. The complaint will stop us from doing anything
4061 * when the file is parsed.
4063 if (!errorReported) {
4064 Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"",
4068 errorReported = TRUE;
4070 Buf_AddByte(&buf, *str);
4075 * We've now got a variable structure to store in. But first,
4076 * advance the string pointer.
4081 * Copy all the characters from the variable value straight
4082 * into the new string.
4084 length = strlen(val);
4085 Buf_AddBytes(&buf, length, val);
4086 trailingBslash = length > 0 && val[length - 1] == '\\';
4093 return Buf_DestroyCompact(&buf);
4096 /* Initialize the module. */
4100 VAR_INTERNAL = Targ_NewGN("Internal");
4101 VAR_GLOBAL = Targ_NewGN("Global");
4102 VAR_CMD = Targ_NewGN("Command");
4112 /****************** PRINT DEBUGGING INFO *****************/
4114 VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED)
4117 fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
4121 *-----------------------------------------------------------------------
4123 * print all variables in a context
4124 *-----------------------------------------------------------------------
4127 Var_Dump(GNode *ctxt)
4129 Hash_ForEach(&ctxt->context, VarPrintVar, NULL);