1 /* $NetBSD: var.c,v 1.484 2020/09/02 06:25:48 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.484 2020/09/02 06:25:48 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.484 2020/09/02 06:25:48 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
93 * Var_Append Append more characters to an existing variable
94 * in the given context. The variable needn't
95 * exist already -- it will be created if it doesn't.
96 * A space is placed between the old value and the
99 * Var_Exists See if a variable exists.
101 * Var_Value Return the unexpanded value of a variable in a
102 * context or NULL if the variable is undefined.
104 * Var_Subst Substitute either a single variable or all
105 * variables in a string, using the given context.
107 * Var_Parse Parse a variable expansion from a string and
108 * return the result and the number of characters
111 * Var_Delete Delete a variable in a context.
113 * Var_Init Initialize this module.
116 * Var_Dump Print out all variables defined in the given
119 * XXX: There's a lot of duplication in these functions.
122 #include <sys/stat.h>
123 #include <sys/types.h>
131 #ifdef HAVE_INTTYPES_H
132 #include <inttypes.h>
133 #elif defined(HAVE_STDINT_H)
140 #include "metachar.h"
142 #define VAR_DEBUG_IF(cond, fmt, ...) \
143 if (!(DEBUG(VAR) && (cond))) \
146 fprintf(debug_file, fmt, __VA_ARGS__)
148 #define VAR_DEBUG(fmt, ...) VAR_DEBUG_IF(TRUE, fmt, __VA_ARGS__)
150 ENUM_FLAGS_RTTI_3(VarEvalFlags,
151 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN);
154 * This lets us tell if we have replaced the original environ
155 * (which we cannot free).
157 char **savedEnv = NULL;
160 * This is a harmless return value for Var_Parse that can be used by Var_Subst
161 * to determine if there was an error in parsing -- easier than returning
162 * a flag, as things outside this module don't give a hoot.
164 char var_Error[] = "";
167 * Similar to var_Error, but returned when the 'VARE_UNDEFERR' flag for
168 * Var_Parse is not set.
170 * Why not just use a constant? Well, GCC likes to condense identical string
173 static char varNoError[] = "";
176 * Traditionally we consume $$ during := like any other expansion.
177 * Other make's do not.
178 * This knob allows controlling the behavior.
179 * FALSE to consume $$ during := assignment.
180 * TRUE to preserve $$ during := assignment.
182 #define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
183 static Boolean save_dollars = FALSE;
186 * Internally, variables are contained in four different contexts.
187 * 1) the environment. They cannot be changed. If an environment
188 * variable is appended to, the result is placed in the global
190 * 2) the global context. Variables set in the Makefile are located in
191 * the global context.
192 * 3) the command-line context. All variables set on the command line
193 * are placed in this context. They are UNALTERABLE once placed here.
194 * 4) the local context. Each target has associated with it a context
195 * list. On this list are located the structures describing such
196 * local variables as $(@) and $(*)
197 * The four contexts are searched in the reverse order from which they are
198 * listed (but see checkEnvFirst).
200 GNode *VAR_INTERNAL; /* variables from make itself */
201 GNode *VAR_GLOBAL; /* variables from the makefile */
202 GNode *VAR_CMD; /* variables defined on the command-line */
205 FIND_CMD = 0x01, /* look in VAR_CMD when searching */
206 FIND_GLOBAL = 0x02, /* look in VAR_GLOBAL as well */
207 FIND_ENV = 0x04 /* look in the environment also */
211 /* The variable's value is currently being used by Var_Parse or Var_Subst.
212 * This marker is used to avoid endless recursion. */
214 /* The variable comes from the environment.
215 * These variables are not registered in any GNode, therefore they must
216 * be freed as soon as they are not used anymore. */
218 /* The variable is a junk variable that should be destroyed when done with
219 * it. Used by Var_Parse for undefined, modified variables. */
221 /* Variable is VAR_JUNK, but we found a use for it in some modifier and
222 * the value is therefore valid. */
224 /* The variable is exported to the environment, to be used by child
227 /* At the point where this variable was exported, it contained an
228 * unresolved reference to another variable. Before any child process is
229 * started, it needs to be exported again, in the hope that the referenced
230 * variable can then be resolved. */
232 /* The variable came from command line. */
237 ENUM_FLAGS_RTTI_8(VarFlags,
238 VAR_IN_USE, VAR_FROM_ENV, VAR_JUNK, VAR_KEEP,
239 VAR_EXPORTED, VAR_REEXPORT, VAR_FROM_CMD, VAR_READONLY);
242 char *name; /* the variable's name; it is allocated for
243 * environment variables and aliased to the
244 * Hash_Entry name for all other variables,
245 * and thus must not be modified */
246 Buffer val; /* its value */
247 VarFlags flags; /* miscellaneous status flags */
251 * Exporting vars is expensive so skip it if we can
259 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
263 * We pass this to Var_Export when doing the initial export
264 * or after updating an exported var.
266 VAR_EXPORT_PARENT = 0x01,
268 * We pass this to Var_Export1 to tell it to leave the value alone.
270 VAR_EXPORT_LITERAL = 0x02
273 /* Flags for pattern matching in the :S and :C modifiers */
275 VARP_SUB_GLOBAL = 0x01, /* Apply substitution globally */
276 VARP_SUB_ONE = 0x02, /* Apply substitution to one word */
277 VARP_ANCHOR_START = 0x04, /* Match at start of word */
278 VARP_ANCHOR_END = 0x08 /* Match at end of word */
287 *-----------------------------------------------------------------------
289 * Find the given variable in the given context and any other contexts
294 * ctxt context in which to find it
295 * flags FIND_GLOBAL look in VAR_GLOBAL as well
296 * FIND_CMD look in VAR_CMD as well
297 * FIND_ENV look in the environment as well
300 * A pointer to the structure describing the desired variable or
301 * NULL if the variable does not exist.
302 *-----------------------------------------------------------------------
305 VarFind(const char *name, GNode *ctxt, VarFindFlags flags)
310 * If the variable name begins with a '.', it could very well be one of
311 * the local ones. We check the name against all the local variables
312 * and substitute the short version in for 'name' if it matches one of
315 if (*name == '.' && isupper((unsigned char)name[1])) {
318 if (strcmp(name, ".ALLSRC") == 0)
320 if (strcmp(name, ".ARCHIVE") == 0)
324 if (strcmp(name, ".IMPSRC") == 0)
328 if (strcmp(name, ".MEMBER") == 0)
332 if (strcmp(name, ".OODATE") == 0)
336 if (strcmp(name, ".PREFIX") == 0)
340 if (strcmp(name, ".SHELL") == 0 ) {
346 if (strcmp(name, ".TARGET") == 0)
353 /* for compatibility with gmake */
354 if (name[0] == '^' && name[1] == '\0')
359 * First look for the variable in the given context. If it's not there,
360 * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
361 * depending on the FIND_* flags in 'flags'
363 var = Hash_FindEntry(&ctxt->context, name);
365 if (var == NULL && (flags & FIND_CMD) && ctxt != VAR_CMD)
366 var = Hash_FindEntry(&VAR_CMD->context, name);
368 if (!checkEnvFirst && var == NULL && (flags & FIND_GLOBAL) &&
371 var = Hash_FindEntry(&VAR_GLOBAL->context, name);
372 if (var == NULL && ctxt != VAR_INTERNAL) {
373 /* VAR_INTERNAL is subordinate to VAR_GLOBAL */
374 var = Hash_FindEntry(&VAR_INTERNAL->context, name);
378 if (var == NULL && (flags & FIND_ENV)) {
381 if ((env = getenv(name)) != NULL) {
382 Var *v = bmake_malloc(sizeof(Var));
384 v->name = bmake_strdup(name);
387 Buf_Init(&v->val, len + 1);
388 Buf_AddBytes(&v->val, env, len);
390 v->flags = VAR_FROM_ENV;
394 if (checkEnvFirst && (flags & FIND_GLOBAL) && ctxt != VAR_GLOBAL) {
395 var = Hash_FindEntry(&VAR_GLOBAL->context, name);
396 if (var == NULL && ctxt != VAR_INTERNAL)
397 var = Hash_FindEntry(&VAR_INTERNAL->context, name);
401 return (Var *)Hash_GetValue(var);
410 return (Var *)Hash_GetValue(var);
414 *-----------------------------------------------------------------------
416 * If the variable is an environment variable, free it
420 * destroy true if the value buffer should be destroyed.
423 * TRUE if it is an environment variable, FALSE otherwise.
424 *-----------------------------------------------------------------------
427 VarFreeEnv(Var *v, Boolean destroy)
429 if (!(v->flags & VAR_FROM_ENV))
432 Buf_Destroy(&v->val, destroy);
437 /* Add a new variable of the given name and value to the given context.
438 * The name and val arguments are duplicated so they may safely be freed. */
440 VarAdd(const char *name, const char *val, GNode *ctxt, VarSet_Flags flags)
442 Var *v = bmake_malloc(sizeof(Var));
443 size_t len = strlen(val);
446 Buf_Init(&v->val, len + 1);
447 Buf_AddBytes(&v->val, val, len);
450 if (flags & VAR_SET_READONLY)
451 v->flags |= VAR_READONLY;
453 he = Hash_CreateEntry(&ctxt->context, name, NULL);
454 Hash_SetValue(he, v);
456 VAR_DEBUG_IF(!(ctxt->flags & INTERNAL),
457 "%s:%s = %s\n", ctxt->name, name, val);
460 /* Remove a variable from a context, freeing the Var structure as well. */
462 Var_Delete(const char *name, GNode *ctxt)
464 char *name_freeIt = NULL;
467 if (strchr(name, '$') != NULL)
468 name = name_freeIt = Var_Subst(name, VAR_GLOBAL, VARE_WANTRES);
469 he = Hash_FindEntry(&ctxt->context, name);
470 VAR_DEBUG("%s:delete %s%s\n",
471 ctxt->name, name, he != NULL ? "" : " (not found)");
475 Var *v = (Var *)Hash_GetValue(he);
476 if (v->flags & VAR_EXPORTED)
478 if (strcmp(v->name, MAKE_EXPORTED) == 0)
479 var_exportedVars = VAR_EXPORTED_NONE;
480 if (v->name != he->name)
482 Hash_DeleteEntry(&ctxt->context, he);
483 Buf_Destroy(&v->val, TRUE);
490 * Export a single variable.
491 * We ignore make internal variables (those which start with '.').
492 * Also we jump through some hoops to avoid calling setenv
493 * more than necessary since it can leak.
494 * We only manipulate flags of vars if 'parent' is set.
497 Var_Export1(const char *name, VarExportFlags flags)
499 VarExportFlags parent = flags & VAR_EXPORT_PARENT;
504 return FALSE; /* skip internals */
505 if (name[1] == '\0') {
508 * If it is one of the vars that should only appear in
509 * local context, skip it, else we can get Var_Subst
521 v = VarFind(name, VAR_GLOBAL, 0);
525 if (!parent && (v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
526 return FALSE; /* nothing to do */
528 val = Buf_GetAll(&v->val, NULL);
529 if (!(flags & VAR_EXPORT_LITERAL) && strchr(val, '$') != NULL) {
534 * Flag this as something we need to re-export.
535 * No point actually exporting it now though,
536 * the child can do it at the last minute.
538 v->flags |= VAR_EXPORTED | VAR_REEXPORT;
541 if (v->flags & VAR_IN_USE) {
543 * We recursed while exporting in a child.
544 * This isn't going to end well, just skip it.
549 expr = str_concat3("${", name, "}");
550 val = Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES);
551 setenv(name, val, 1);
556 v->flags &= ~(unsigned)VAR_REEXPORT; /* once will do */
557 if (parent || !(v->flags & VAR_EXPORTED))
558 setenv(name, val, 1);
561 * This is so Var_Set knows to call Var_Export again...
564 v->flags |= VAR_EXPORTED;
570 Var_ExportVars_callback(void *entry, void *unused MAKE_ATTR_UNUSED)
573 Var_Export1(var->name, 0);
577 * This gets called from our children.
585 * Several make's support this sort of mechanism for tracking
586 * recursion - but each uses a different name.
587 * We allow the makefiles to update MAKELEVEL and ensure
588 * children see a correctly incremented value.
591 snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
592 setenv(MAKE_LEVEL_ENV, tmp, 1);
594 if (var_exportedVars == VAR_EXPORTED_NONE)
597 if (var_exportedVars == VAR_EXPORTED_ALL) {
598 /* Ouch! This is crazy... */
599 Hash_ForEach(&VAR_GLOBAL->context, Var_ExportVars_callback, NULL);
603 val = Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, VARE_WANTRES);
605 Words words = Str_Words(val, FALSE);
608 for (i = 0; i < words.len; i++)
609 Var_Export1(words.words[i], 0);
616 * This is called when .export is seen or .MAKE.EXPORTED is modified.
618 * It is also called when any exported variable is modified.
621 * str has the format "[-env|-literal] varname...".
624 Var_Export(const char *str, Boolean isExport)
626 VarExportFlags flags;
629 if (isExport && str[0] == '\0') {
630 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
635 if (strncmp(str, "-env", 4) == 0) {
637 } else if (strncmp(str, "-literal", 8) == 0) {
639 flags |= VAR_EXPORT_LITERAL;
641 flags |= VAR_EXPORT_PARENT;
644 val = Var_Subst(str, VAR_GLOBAL, VARE_WANTRES);
645 if (val[0] != '\0') {
646 Words words = Str_Words(val, FALSE);
649 for (i = 0; i < words.len; i++) {
650 const char *name = words.words[i];
651 if (Var_Export1(name, flags)) {
652 if (var_exportedVars != VAR_EXPORTED_ALL)
653 var_exportedVars = VAR_EXPORTED_YES;
654 if (isExport && (flags & VAR_EXPORT_PARENT)) {
655 Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL);
665 extern char **environ;
668 * This is called when .unexport[-env] is seen.
670 * str must have the form "unexport[-env] varname...".
673 Var_UnExport(const char *str)
675 const char *varnames;
676 char *varnames_freeIt;
677 Boolean unexport_env;
680 varnames_freeIt = NULL;
682 str += strlen("unexport");
683 unexport_env = strncmp(str, "-env", 4) == 0;
688 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */
689 if (environ == savedEnv) {
690 /* we have been here before! */
691 newenv = bmake_realloc(environ, 2 * sizeof(char *));
697 newenv = bmake_malloc(2 * sizeof(char *));
700 /* Note: we cannot safely free() the original environ. */
701 environ = savedEnv = newenv;
705 setenv(MAKE_LEVEL_ENV, cp, 1);
707 for (; isspace((unsigned char)*str); str++)
713 if (varnames == NULL) {
714 /* Using .MAKE.EXPORTED */
715 varnames = varnames_freeIt = Var_Subst("${" MAKE_EXPORTED ":O:u}",
716 VAR_GLOBAL, VARE_WANTRES);
723 Words words = Str_Words(varnames, FALSE);
724 for (i = 0; i < words.len; i++) {
725 const char *varname = words.words[i];
726 v = VarFind(varname, VAR_GLOBAL, 0);
728 VAR_DEBUG("Not unexporting \"%s\" (not found)\n", varname);
732 VAR_DEBUG("Unexporting \"%s\"\n", varname);
733 if (!unexport_env && (v->flags & VAR_EXPORTED) &&
734 !(v->flags & VAR_REEXPORT))
736 v->flags &= ~(unsigned)(VAR_EXPORTED | VAR_REEXPORT);
739 * If we are unexporting a list,
740 * remove each one from .MAKE.EXPORTED.
741 * If we are removing them all,
742 * just delete .MAKE.EXPORTED below.
744 if (varnames == str) {
745 char *expr = str_concat3("${" MAKE_EXPORTED ":N", v->name, "}");
746 char *cp = Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES);
747 Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL);
753 if (varnames != str) {
754 Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
755 free(varnames_freeIt);
760 /* See Var_Set for documentation. */
762 Var_Set_with_flags(const char *name, const char *val, GNode *ctxt,
765 const char *unexpanded_name = name;
766 char *name_freeIt = NULL;
772 * We only look for a variable in the given context since anything set
773 * here will override anything in a lower context, so there's not much
774 * point in searching them all just to save a bit of memory...
776 if (strchr(name, '$') != NULL)
777 name = name_freeIt = Var_Subst(name, ctxt, VARE_WANTRES);
779 if (name[0] == '\0') {
780 VAR_DEBUG("Var_Set(\"%s\", \"%s\", ...) "
781 "name expands to empty string - ignored\n",
782 unexpanded_name, val);
787 if (ctxt == VAR_GLOBAL) {
788 v = VarFind(name, VAR_CMD, 0);
790 if (v->flags & VAR_FROM_CMD) {
791 VAR_DEBUG("%s:%s = %s ignored!\n", ctxt->name, name, val);
798 v = VarFind(name, ctxt, 0);
800 if (ctxt == VAR_CMD && !(flags & VAR_NO_EXPORT)) {
802 * This var would normally prevent the same name being added
803 * to VAR_GLOBAL, so delete it from there if needed.
804 * Otherwise -V name may show the wrong value.
806 Var_Delete(name, VAR_GLOBAL);
808 VarAdd(name, val, ctxt, flags);
810 if ((v->flags & VAR_READONLY) && !(flags & VAR_SET_READONLY)) {
811 VAR_DEBUG("%s:%s = %s ignored (read-only)\n",
812 ctxt->name, name, val);
817 Buf_AddStr(&v->val, val);
819 VAR_DEBUG("%s:%s = %s\n", ctxt->name, name, val);
820 if (v->flags & VAR_EXPORTED) {
821 Var_Export1(name, VAR_EXPORT_PARENT);
825 * Any variables given on the command line are automatically exported
826 * to the environment (as per POSIX standard)
827 * Other than internals.
829 if (ctxt == VAR_CMD && !(flags & VAR_NO_EXPORT) && name[0] != '.') {
831 /* we just added it */
832 v = VarFind(name, ctxt, 0);
835 v->flags |= VAR_FROM_CMD;
837 * If requested, don't export these in the environment
838 * individually. We still put them in MAKEOVERRIDES so
839 * that the command-line settings continue to override
843 setenv(name, val ? val : "", 1);
845 Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
847 if (name[0] == '.' && strcmp(name, SAVE_DOLLARS) == 0)
848 save_dollars = s2Boolean(val, save_dollars);
857 *-----------------------------------------------------------------------
859 * Set the variable name to the value val in the given context.
861 * If the variable doesn't yet exist, it is created.
862 * Otherwise the new value overwrites and replaces the old value.
865 * name name of variable to set
866 * val value to give to the variable
867 * ctxt context in which to set it
870 * The variable is searched for only in its context before being
871 * created in that context. I.e. if the context is VAR_GLOBAL,
872 * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
873 * VAR_CMD->context is searched. This is done to avoid the literally
874 * thousands of unnecessary strcmp's that used to be done to
875 * set, say, $(@) or $(<).
876 * If the context is VAR_GLOBAL though, we check if the variable
877 * was set in VAR_CMD from the command line and skip it if so.
878 *-----------------------------------------------------------------------
881 Var_Set(const char *name, const char *val, GNode *ctxt)
883 Var_Set_with_flags(name, val, ctxt, 0);
887 *-----------------------------------------------------------------------
889 * The variable of the given name has the given value appended to it in
892 * If the variable doesn't exist, it is created. Otherwise the strings
893 * are concatenated, with a space in between.
896 * name name of variable to modify
897 * val string to append to it
898 * ctxt context in which this should occur
901 * Only if the variable is being sought in the global context is the
902 * environment searched.
903 * XXX: Knows its calling circumstances in that if called with ctxt
904 * an actual target, it will only search that context since only
905 * a local variable could be being appended to. This is actually
906 * a big win and must be tolerated.
907 *-----------------------------------------------------------------------
910 Var_Append(const char *name, const char *val, GNode *ctxt)
912 char *name_freeIt = NULL;
917 if (strchr(name, '$') != NULL) {
918 const char *unexpanded_name = name;
919 name = name_freeIt = Var_Subst(name, ctxt, VARE_WANTRES);
920 if (name[0] == '\0') {
921 VAR_DEBUG("Var_Append(\"%s\", \"%s\", ...) "
922 "name expands to empty string - ignored\n",
923 unexpanded_name, val);
929 v = VarFind(name, ctxt, ctxt == VAR_GLOBAL ? (FIND_CMD | FIND_ENV) : 0);
932 Var_Set(name, val, ctxt);
933 } else if (ctxt == VAR_CMD || !(v->flags & VAR_FROM_CMD)) {
934 Buf_AddByte(&v->val, ' ');
935 Buf_AddStr(&v->val, val);
937 VAR_DEBUG("%s:%s = %s\n", ctxt->name, name,
938 Buf_GetAll(&v->val, NULL));
940 if (v->flags & VAR_FROM_ENV) {
944 * If the original variable came from the environment, we
945 * have to install it in the global context (we could place
946 * it in the environment, but then we should provide a way to
947 * export other variables...)
949 v->flags &= ~(unsigned)VAR_FROM_ENV;
950 h = Hash_CreateEntry(&ctxt->context, name, NULL);
957 /* See if the given variable exists, in the given context or in other
961 * name Variable to find
962 * ctxt Context in which to start search
965 Var_Exists(const char *name, GNode *ctxt)
967 char *name_freeIt = NULL;
970 if (strchr(name, '$') != NULL)
971 name = name_freeIt = Var_Subst(name, ctxt, VARE_WANTRES);
973 v = VarFind(name, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV);
978 (void)VarFreeEnv(v, TRUE);
983 *-----------------------------------------------------------------------
985 * Return the unexpanded value of the given variable in the given
986 * context, or the usual contexts.
990 * ctxt context in which to search for it
993 * The value if the variable exists, NULL if it doesn't.
994 * If the returned value is not NULL, the caller must free *freeIt
995 * as soon as the returned value is no longer needed.
996 *-----------------------------------------------------------------------
999 Var_Value(const char *name, GNode *ctxt, char **freeIt)
1001 Var *v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1008 p = Buf_GetAll(&v->val, NULL);
1009 if (VarFreeEnv(v, FALSE))
1015 /* SepBuf is a string being built from "words", interleaved with separators. */
1019 char sep; /* usually ' ', but see the :ts modifier */
1023 SepBuf_Init(SepBuf *buf, char sep)
1025 Buf_Init(&buf->buf, 32 /* bytes */);
1026 buf->needSep = FALSE;
1031 SepBuf_Sep(SepBuf *buf)
1033 buf->needSep = TRUE;
1037 SepBuf_AddBytes(SepBuf *buf, const char *mem, size_t mem_size)
1041 if (buf->needSep && buf->sep != '\0') {
1042 Buf_AddByte(&buf->buf, buf->sep);
1043 buf->needSep = FALSE;
1045 Buf_AddBytes(&buf->buf, mem, mem_size);
1049 SepBuf_AddBytesBetween(SepBuf *buf, const char *start, const char *end)
1051 SepBuf_AddBytes(buf, start, (size_t)(end - start));
1055 SepBuf_AddStr(SepBuf *buf, const char *str)
1057 SepBuf_AddBytes(buf, str, strlen(str));
1061 SepBuf_Destroy(SepBuf *buf, Boolean free_buf)
1063 return Buf_Destroy(&buf->buf, free_buf);
1067 /* This callback for ModifyWords gets a single word from an expression and
1068 * typically adds a modification of this word to the buffer. It may also do
1069 * nothing or add several words. */
1070 typedef void (*ModifyWordsCallback)(const char *word, SepBuf *buf, void *data);
1073 /* Callback for ModifyWords to implement the :H modifier.
1074 * Add the dirname of the given word to the buffer. */
1076 ModifyWord_Head(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1078 const char *slash = strrchr(word, '/');
1080 SepBuf_AddBytesBetween(buf, word, slash);
1082 SepBuf_AddStr(buf, ".");
1085 /* Callback for ModifyWords to implement the :T modifier.
1086 * Add the basename of the given word to the buffer. */
1088 ModifyWord_Tail(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1090 const char *slash = strrchr(word, '/');
1091 const char *base = slash != NULL ? slash + 1 : word;
1092 SepBuf_AddStr(buf, base);
1095 /* Callback for ModifyWords to implement the :E modifier.
1096 * Add the filename suffix of the given word to the buffer, if it exists. */
1098 ModifyWord_Suffix(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1100 const char *dot = strrchr(word, '.');
1102 SepBuf_AddStr(buf, dot + 1);
1105 /* Callback for ModifyWords to implement the :R modifier.
1106 * Add the basename of the given word to the buffer. */
1108 ModifyWord_Root(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1110 const char *dot = strrchr(word, '.');
1111 size_t len = dot != NULL ? (size_t)(dot - word) : strlen(word);
1112 SepBuf_AddBytes(buf, word, len);
1115 /* Callback for ModifyWords to implement the :M modifier.
1116 * Place the word in the buffer if it matches the given pattern. */
1118 ModifyWord_Match(const char *word, SepBuf *buf, void *data)
1120 const char *pattern = data;
1121 VAR_DEBUG("VarMatch [%s] [%s]\n", word, pattern);
1122 if (Str_Match(word, pattern))
1123 SepBuf_AddStr(buf, word);
1126 /* Callback for ModifyWords to implement the :N modifier.
1127 * Place the word in the buffer if it doesn't match the given pattern. */
1129 ModifyWord_NoMatch(const char *word, SepBuf *buf, void *data)
1131 const char *pattern = data;
1132 if (!Str_Match(word, pattern))
1133 SepBuf_AddStr(buf, word);
1138 *-----------------------------------------------------------------------
1140 * Check word against pattern for a match (% is wild),
1143 * word Word to examine
1144 * pattern Pattern to examine against
1147 * Returns the start of the match, or NULL.
1148 * *match_len returns the length of the match, if any.
1149 * *hasPercent returns whether the pattern contains a percent.
1150 *-----------------------------------------------------------------------
1153 Str_SYSVMatch(const char *word, const char *pattern, size_t *match_len,
1154 Boolean *hasPercent)
1156 const char *p = pattern;
1157 const char *w = word;
1158 const char *percent;
1163 *hasPercent = FALSE;
1164 if (*p == '\0') { /* ${VAR:=suffix} */
1165 *match_len = strlen(w); /* Null pattern is the whole string */
1169 percent = strchr(p, '%');
1170 if (percent != NULL) { /* ${VAR:...%...=...} */
1173 return NULL; /* empty word does not match pattern */
1175 /* check that the prefix matches */
1176 for (; p != percent && *w != '\0' && *w == *p; w++, p++)
1179 return NULL; /* No match */
1181 p++; /* Skip the percent */
1183 /* No more pattern, return the rest of the string */
1184 *match_len = strlen(w);
1189 /* Test whether the tail matches */
1195 w_tail = w + w_len - p_len;
1196 if (memcmp(p, w_tail, p_len) != 0)
1199 *match_len = (size_t)(w_tail - w);
1207 } ModifyWord_SYSVSubstArgs;
1209 /* Callback for ModifyWords to implement the :%.from=%.to modifier. */
1211 ModifyWord_SYSVSubst(const char *word, SepBuf *buf, void *data)
1213 const ModifyWord_SYSVSubstArgs *args = data;
1216 const char *percent;
1220 const char *match = Str_SYSVMatch(word, args->lhs, &match_len, &lhsPercent);
1221 if (match == NULL) {
1222 SepBuf_AddStr(buf, word);
1226 /* Append rhs to the buffer, substituting the first '%' with the
1227 * match, but only if the lhs had a '%' as well. */
1229 rhs_expanded = Var_Subst(args->rhs, args->ctx, VARE_WANTRES);
1232 percent = strchr(rhs, '%');
1234 if (percent != NULL && lhsPercent) {
1235 /* Copy the prefix of the replacement pattern */
1236 SepBuf_AddBytesBetween(buf, rhs, percent);
1239 if (percent != NULL || !lhsPercent)
1240 SepBuf_AddBytes(buf, match, match_len);
1242 /* Append the suffix of the replacement pattern */
1243 SepBuf_AddStr(buf, rhs);
1255 VarPatternFlags pflags;
1257 } ModifyWord_SubstArgs;
1259 /* Callback for ModifyWords to implement the :S,from,to, modifier.
1260 * Perform a string substitution on the given word. */
1262 ModifyWord_Subst(const char *word, SepBuf *buf, void *data)
1264 size_t wordLen = strlen(word);
1265 ModifyWord_SubstArgs *args = data;
1268 if ((args->pflags & VARP_SUB_ONE) && args->matched)
1271 if (args->pflags & VARP_ANCHOR_START) {
1272 if (wordLen < args->lhsLen ||
1273 memcmp(word, args->lhs, args->lhsLen) != 0)
1276 if (args->pflags & VARP_ANCHOR_END) {
1277 if (wordLen != args->lhsLen)
1280 /* :S,^whole$,replacement, */
1281 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1282 args->matched = TRUE;
1284 /* :S,^prefix,replacement, */
1285 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1286 SepBuf_AddBytes(buf, word + args->lhsLen, wordLen - args->lhsLen);
1287 args->matched = TRUE;
1292 if (args->pflags & VARP_ANCHOR_END) {
1295 if (wordLen < args->lhsLen)
1298 start = word + (wordLen - args->lhsLen);
1299 if (memcmp(start, args->lhs, args->lhsLen) != 0)
1302 /* :S,suffix$,replacement, */
1303 SepBuf_AddBytesBetween(buf, word, start);
1304 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1305 args->matched = TRUE;
1309 /* unanchored case, may match more than once */
1310 while ((match = Str_FindSubstring(word, args->lhs)) != NULL) {
1311 SepBuf_AddBytesBetween(buf, word, match);
1312 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1313 args->matched = TRUE;
1314 wordLen -= (size_t)(match - word) + args->lhsLen;
1315 word += (size_t)(match - word) + args->lhsLen;
1316 if (wordLen == 0 || !(args->pflags & VARP_SUB_GLOBAL))
1320 SepBuf_AddBytes(buf, word, wordLen);
1324 /* Print the error caused by a regcomp or regexec call. */
1326 VarREError(int reerr, regex_t *pat, const char *str)
1328 size_t errlen = regerror(reerr, pat, 0, 0);
1329 char *errbuf = bmake_malloc(errlen);
1330 regerror(reerr, pat, errbuf, errlen);
1331 Error("%s: %s", str, errbuf);
1339 VarPatternFlags pflags;
1341 } ModifyWord_SubstRegexArgs;
1343 /* Callback for ModifyWords to implement the :C/from/to/ modifier.
1344 * Perform a regex substitution on the given word. */
1346 ModifyWord_SubstRegex(const char *word, SepBuf *buf, void *data)
1348 ModifyWord_SubstRegexArgs *args = data;
1350 const char *wp = word;
1355 if ((args->pflags & VARP_SUB_ONE) && args->matched)
1359 xrv = regexec(&args->re, wp, args->nsub, m, flags);
1363 args->matched = TRUE;
1364 SepBuf_AddBytes(buf, wp, (size_t)m[0].rm_so);
1366 for (rp = args->replace; *rp; rp++) {
1367 if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) {
1368 SepBuf_AddBytes(buf, rp + 1, 1);
1374 SepBuf_AddBytesBetween(buf, wp + m[0].rm_so, wp + m[0].rm_eo);
1378 if (*rp != '\\' || !isdigit((unsigned char)rp[1])) {
1379 SepBuf_AddBytes(buf, rp, 1);
1383 { /* \0 to \9 backreference */
1384 size_t n = (size_t)(rp[1] - '0');
1387 if (n >= args->nsub) {
1388 Error("No subexpression \\%zu", n);
1389 } else if (m[n].rm_so == -1 && m[n].rm_eo == -1) {
1390 Error("No match for subexpression \\%zu", n);
1392 SepBuf_AddBytesBetween(buf, wp + m[n].rm_so,
1399 if (args->pflags & VARP_SUB_GLOBAL) {
1400 flags |= REG_NOTBOL;
1401 if (m[0].rm_so == 0 && m[0].rm_eo == 0) {
1402 SepBuf_AddBytes(buf, wp, 1);
1409 SepBuf_AddStr(buf, wp);
1413 VarREError(xrv, &args->re, "Unexpected regex error");
1417 SepBuf_AddStr(buf, wp);
1426 char *tvar; /* name of temporary variable */
1427 char *str; /* string to expand */
1428 VarEvalFlags eflags;
1429 } ModifyWord_LoopArgs;
1431 /* Callback for ModifyWords to implement the :@var@...@ modifier of ODE make. */
1433 ModifyWord_Loop(const char *word, SepBuf *buf, void *data)
1435 const ModifyWord_LoopArgs *args;
1438 if (word[0] == '\0')
1442 Var_Set_with_flags(args->tvar, word, args->ctx, VAR_NO_EXPORT);
1443 s = Var_Subst(args->str, args->ctx, args->eflags);
1445 VAR_DEBUG("ModifyWord_Loop: in \"%s\", replace \"%s\" with \"%s\" "
1447 word, args->tvar, args->str, s);
1449 if (s[0] == '\n' || (buf->buf.count > 0 &&
1450 buf->buf.buffer[buf->buf.count - 1] == '\n'))
1451 buf->needSep = FALSE;
1452 SepBuf_AddStr(buf, s);
1458 * Implements the :[first..last] modifier.
1459 * This is a special case of ModifyWords since we want to be able
1460 * to scan the list backwards if first > last.
1463 VarSelectWords(char sep, Boolean oneBigWord, const char *str, int first,
1467 int start, end, step;
1471 SepBuf_Init(&buf, sep);
1474 /* fake what Str_Words() would do if there were only one word */
1476 words.words = bmake_malloc((words.len + 1) * sizeof(char *));
1477 words.freeIt = bmake_strdup(str);
1478 words.words[0] = words.freeIt;
1479 words.words[1] = NULL;
1481 words = Str_Words(str, FALSE);
1485 * Now sanitize the given range.
1486 * If first or last are negative, convert them to the positive equivalents
1487 * (-1 gets converted to ac, -2 gets converted to (ac - 1), etc.).
1490 first += (int)words.len + 1;
1492 last += (int)words.len + 1;
1495 * We avoid scanning more of the list than we need to.
1498 start = MIN((int)words.len, first) - 1;
1499 end = MAX(0, last - 1);
1502 start = MAX(0, first - 1);
1503 end = MIN((int)words.len, last);
1507 for (i = start; (step < 0) == (i >= end); i += step) {
1508 SepBuf_AddStr(&buf, words.words[i]);
1514 return SepBuf_Destroy(&buf, FALSE);
1518 /* Callback for ModifyWords to implement the :tA modifier.
1519 * Replace each word with the result of realpath() if successful. */
1521 ModifyWord_Realpath(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
1524 char rbuf[MAXPATHLEN];
1526 const char *rp = cached_realpath(word, rbuf);
1527 if (rp != NULL && *rp == '/' && stat(rp, &st) == 0)
1530 SepBuf_AddStr(buf, word);
1534 *-----------------------------------------------------------------------
1535 * Modify each of the words of the passed string using the given function.
1538 * str String whose words should be modified
1539 * modifyWord Function that modifies a single word
1540 * modifyWord_args Custom arguments for modifyWord
1543 * A string of all the words modified appropriately.
1544 *-----------------------------------------------------------------------
1547 ModifyWords(GNode *ctx, char sep, Boolean oneBigWord, const char *str,
1548 ModifyWordsCallback modifyWord, void *modifyWord_args)
1555 SepBuf_Init(&result, sep);
1556 modifyWord(str, &result, modifyWord_args);
1557 return SepBuf_Destroy(&result, FALSE);
1560 SepBuf_Init(&result, sep);
1562 words = Str_Words(str, FALSE);
1564 VAR_DEBUG("ModifyWords: split \"%s\" into %zu words\n", str, words.len);
1566 for (i = 0; i < words.len; i++) {
1567 modifyWord(words.words[i], &result, modifyWord_args);
1568 if (result.buf.count > 0)
1569 SepBuf_Sep(&result);
1574 return SepBuf_Destroy(&result, FALSE);
1579 Words_JoinFree(Words words)
1586 for (i = 0; i < words.len; i++) {
1588 Buf_AddByte(&buf, ' '); /* XXX: st->sep, for consistency */
1589 Buf_AddStr(&buf, words.words[i]);
1594 return Buf_Destroy(&buf, FALSE);
1597 /* Remove adjacent duplicate words. */
1599 VarUniq(const char *str)
1601 Words words = Str_Words(str, FALSE);
1603 if (words.len > 1) {
1605 for (j = 0, i = 1; i < words.len; i++)
1606 if (strcmp(words.words[i], words.words[j]) != 0 && (++j != i))
1607 words.words[j] = words.words[i];
1611 return Words_JoinFree(words);
1616 * Parse a part of a modifier such as the "from" and "to" in :S/from/to/
1617 * or the "var" or "replacement" in :@var@replacement+${var}@, up to and
1618 * including the next unescaped delimiter. The delimiter, as well as the
1619 * backslash or the dollar, can be escaped with a backslash.
1621 * Return the parsed (and possibly expanded) string, or NULL if no delimiter
1622 * was found. On successful return, the parsing position pp points right
1623 * after the delimiter. The delimiter is not included in the returned
1628 const char **pp, /* The parsing position, updated upon return */
1629 int delim, /* Parsing stops at this delimiter */
1630 VarEvalFlags eflags, /* Flags for evaluating nested variables;
1631 * if VARE_WANTRES is not set, the text is
1633 GNode *ctxt, /* For looking up nested variables */
1634 size_t *out_length, /* Optionally stores the length of the returned
1635 * string, just to save another strlen call. */
1636 VarPatternFlags *out_pflags,/* For the first part of the :S modifier,
1637 * sets the VARP_ANCHOR_END flag if the last
1638 * character of the pattern is a $. */
1639 ModifyWord_SubstArgs *subst /* For the second part of the :S modifier,
1640 * allow ampersands to be escaped and replace
1641 * unescaped ampersands with subst->lhs. */
1650 * Skim through until the matching delimiter is found;
1651 * pick up variable substitutions on the way. Also allow
1652 * backslashes to quote the delimiter, $, and \, but don't
1653 * touch other backslashes.
1656 while (*p != '\0' && *p != delim) {
1657 const char *varstart;
1659 Boolean is_escaped = p[0] == '\\' && (
1660 p[1] == delim || p[1] == '\\' || p[1] == '$' ||
1661 (p[1] == '&' && subst != NULL));
1663 Buf_AddByte(&buf, p[1]);
1668 if (*p != '$') { /* Unescaped, simple text */
1669 if (subst != NULL && *p == '&')
1670 Buf_AddBytes(&buf, subst->lhs, subst->lhsLen);
1672 Buf_AddByte(&buf, *p);
1677 if (p[1] == delim) { /* Unescaped $ at end of pattern */
1678 if (out_pflags != NULL)
1679 *out_pflags |= VARP_ANCHOR_END;
1681 Buf_AddByte(&buf, *p);
1686 if (eflags & VARE_WANTRES) { /* Nested variable, evaluated */
1690 VarEvalFlags nested_eflags = eflags & ~(unsigned)VARE_ASSIGN;
1692 cp2 = Var_Parse(p, ctxt, nested_eflags, &len, &freeIt);
1693 Buf_AddStr(&buf, cp2);
1699 /* XXX: This whole block is very similar to Var_Parse without
1700 * VARE_WANTRES. There may be subtle edge cases though that are
1701 * not yet covered in the unit tests and that are parsed differently,
1702 * depending on whether they are evaluated or not.
1704 * This subtle difference is not documented in the manual page,
1705 * neither is the difference between parsing :D and :M documented.
1706 * No code should ever depend on these details, but who knows. */
1708 varstart = p; /* Nested variable, only parsed */
1709 if (p[1] == PROPEN || p[1] == BROPEN) {
1711 * Find the end of this variable reference
1712 * and suck it in without further ado.
1713 * It will be interpreted later.
1716 int want = have == PROPEN ? PRCLOSE : BRCLOSE;
1719 for (p += 2; *p != '\0' && depth > 0; p++) {
1720 if (p[-1] != '\\') {
1727 Buf_AddBytesBetween(&buf, varstart, p);
1729 Buf_AddByte(&buf, *varstart);
1740 if (out_length != NULL)
1741 *out_length = Buf_Size(&buf);
1743 rstr = Buf_Destroy(&buf, FALSE);
1744 VAR_DEBUG("Modifier part: \"%s\"\n", rstr);
1748 /* Quote shell meta-characters and space characters in the string.
1749 * If quoteDollar is set, also quote and double any '$' characters. */
1751 VarQuote(const char *str, Boolean quoteDollar)
1757 for (; *str != '\0'; str++) {
1759 const char *newline = Shell_GetNewline();
1760 if (newline == NULL)
1762 Buf_AddStr(&buf, newline);
1765 if (isspace((unsigned char)*str) || ismeta((unsigned char)*str))
1766 Buf_AddByte(&buf, '\\');
1767 Buf_AddByte(&buf, *str);
1768 if (quoteDollar && *str == '$')
1769 Buf_AddStr(&buf, "\\$");
1772 res = Buf_Destroy(&buf, FALSE);
1773 VAR_DEBUG("QuoteMeta: [%s]\n", res);
1777 /* Compute the 32-bit hash of the given string, using the MurmurHash3
1778 * algorithm. Output is encoded as 8 hex digits, in Little Endian order. */
1780 VarHash(const char *str)
1782 static const char hexdigits[16] = "0123456789abcdef";
1783 const unsigned char *ustr = (const unsigned char *)str;
1785 uint32_t h = 0x971e137bU;
1786 uint32_t c1 = 0x95543787U;
1787 uint32_t c2 = 0x2ad7eb25U;
1788 size_t len2 = strlen(str);
1794 for (len = len2; len; ) {
1798 k = ((uint32_t)ustr[3] << 24) |
1799 ((uint32_t)ustr[2] << 16) |
1800 ((uint32_t)ustr[1] << 8) |
1806 k |= (uint32_t)ustr[2] << 16;
1809 k |= (uint32_t)ustr[1] << 8;
1812 k |= (uint32_t)ustr[0];
1815 c1 = c1 * 5 + 0x7b7d159cU;
1816 c2 = c2 * 5 + 0x6bce6396U;
1818 k = (k << 11) ^ (k >> 21);
1820 h = (h << 13) ^ (h >> 19);
1821 h = h * 5 + 0x52dce729U;
1824 h ^= (uint32_t)len2;
1830 buf = bmake_malloc(9);
1831 for (i = 0; i < 8; i++) {
1832 buf[i] = hexdigits[h & 0x0f];
1840 VarStrftime(const char *fmt, Boolean zulu, time_t tim)
1848 strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&tim) : localtime(&tim));
1850 buf[sizeof(buf) - 1] = '\0';
1851 return bmake_strdup(buf);
1854 /* The ApplyModifier functions all work in the same way. They get the
1855 * current parsing position (pp) and parse the modifier from there. The
1856 * modifier typically lasts until the next ':', or a closing '}' or ')'
1857 * (taken from st->endc), or the end of the string (parse error).
1859 * The high-level behavior of these functions is:
1861 * 1. parse the modifier
1862 * 2. evaluate the modifier
1865 * Parsing the modifier
1867 * If parsing succeeds, the parsing position *pp is updated to point to the
1868 * first character following the modifier, which typically is either ':' or
1871 * If parsing fails because of a missing delimiter (as in the :S, :C or :@
1872 * modifiers), set st->missing_delim and return AMR_CLEANUP.
1874 * If parsing fails because the modifier is unknown, return AMR_UNKNOWN to
1875 * try the SysV modifier ${VAR:from=to} as fallback. This should only be
1876 * done as long as there have been no side effects from evaluating nested
1877 * variables, to avoid evaluating them more than once. In this case, the
1878 * parsing position must not be updated. (XXX: Why not? The original parsing
1879 * position is well-known in ApplyModifiers.)
1881 * If parsing fails and the SysV modifier ${VAR:from=to} should not be used
1882 * as a fallback, either issue an error message using Error or Parse_Error
1883 * and then return AMR_CLEANUP, or return AMR_BAD for the default error
1884 * message. Both of these return values will stop processing the variable
1885 * expression. (XXX: As of 2020-08-23, evaluation of the whole string
1886 * continues nevertheless after skipping a few bytes, which essentially is
1887 * undefined behavior. Not in the sense of C, but still it's impossible to
1888 * predict what happens in the parser.)
1890 * Evaluating the modifier
1892 * After parsing, the modifier is evaluated. The side effects from evaluating
1893 * nested variable expressions in the modifier text often already happen
1894 * during parsing though.
1896 * Evaluating the modifier usually takes the current value of the variable
1897 * expression from st->val, or the variable name from st->v->name and stores
1898 * the result in st->newVal.
1900 * If evaluating fails (as of 2020-08-23), an error message is printed using
1901 * Error. This function has no side-effects, it really just prints the error
1902 * message. Processing the expression continues as if everything were ok.
1903 * XXX: This should be fixed by adding proper error handling to Var_Subst,
1904 * Var_Parse, ApplyModifiers and ModifyWords.
1908 * Some modifiers such as :D and :U turn undefined variables into useful
1909 * variables (VAR_JUNK, VAR_KEEP).
1911 * Some modifiers need to free some memory.
1915 const char startc; /* '\0' or '{' or '(' */
1916 const char endc; /* '\0' or '}' or ')' */
1919 const VarEvalFlags eflags;
1921 char *val; /* The old value of the expression,
1922 * before applying the modifier, never NULL */
1923 char *newVal; /* The new value of the expression,
1924 * after applying the modifier, never NULL */
1925 char missing_delim; /* For error reporting */
1927 char sep; /* Word separator in expansions
1928 * (see the :ts modifier) */
1929 Boolean oneBigWord; /* TRUE if some modifiers that otherwise split
1930 * the variable value into words, like :S and
1931 * :C, treat the variable value as a single big
1932 * word, possibly containing spaces. */
1933 } ApplyModifiersState;
1936 AMR_OK, /* Continue parsing */
1937 AMR_UNKNOWN, /* Not a match, try other modifiers as well */
1938 AMR_BAD, /* Error out with "Bad modifier" message */
1939 AMR_CLEANUP /* Error out, with "Unfinished modifier"
1940 * if st->missing_delim is set. */
1941 } ApplyModifierResult;
1943 /* Test whether mod starts with modname, followed by a delimiter. */
1945 ModMatch(const char *mod, const char *modname, char endc)
1947 size_t n = strlen(modname);
1948 return strncmp(mod, modname, n) == 0 &&
1949 (mod[n] == endc || mod[n] == ':');
1952 /* Test whether mod starts with modname, followed by a delimiter or '='. */
1953 static inline Boolean
1954 ModMatchEq(const char *mod, const char *modname, char endc)
1956 size_t n = strlen(modname);
1957 return strncmp(mod, modname, n) == 0 &&
1958 (mod[n] == endc || mod[n] == ':' || mod[n] == '=');
1961 /* :@var@...${var}...@ */
1962 static ApplyModifierResult
1963 ApplyModifier_Loop(const char **pp, ApplyModifiersState *st)
1965 ModifyWord_LoopArgs args;
1968 VarEvalFlags eflags = st->eflags & ~(unsigned)VARE_WANTRES;
1970 args.ctx = st->ctxt;
1972 (*pp)++; /* Skip the first '@' */
1974 args.tvar = ParseModifierPart(pp, delim, eflags,
1975 st->ctxt, NULL, NULL, NULL);
1976 if (args.tvar == NULL) {
1977 st->missing_delim = delim;
1980 if (DEBUG(LINT) && strchr(args.tvar, '$') != NULL) {
1981 Parse_Error(PARSE_FATAL,
1982 "In the :@ modifier of \"%s\", the variable name \"%s\" "
1983 "must not contain a dollar.",
1984 st->v->name, args.tvar);
1988 args.str = ParseModifierPart(pp, delim, eflags,
1989 st->ctxt, NULL, NULL, NULL);
1990 if (args.str == NULL) {
1991 st->missing_delim = delim;
1995 args.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES);
1997 st->sep = ' '; /* XXX: should be st->sep for consistency */
1998 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
1999 ModifyWord_Loop, &args);
2001 Var_Delete(args.tvar, st->ctxt);
2007 /* :Ddefined or :Uundefined */
2008 static ApplyModifierResult
2009 ApplyModifier_Defined(const char **pp, ApplyModifiersState *st)
2014 VarEvalFlags eflags = st->eflags & ~(unsigned)VARE_WANTRES;
2015 if (st->eflags & VARE_WANTRES) {
2016 if ((**pp == 'D') == !(st->v->flags & VAR_JUNK))
2017 eflags |= VARE_WANTRES;
2022 while (*p != st->endc && *p != ':' && *p != '\0') {
2024 /* Escaped delimiter or other special character */
2027 if (c == st->endc || c == ':' || c == '$' || c == '\\') {
2028 Buf_AddByte(&buf, c);
2034 /* Nested variable expression */
2040 cp2 = Var_Parse(p, st->ctxt, eflags, &len, &freeIt);
2041 Buf_AddStr(&buf, cp2);
2048 Buf_AddByte(&buf, *p);
2053 if (st->v->flags & VAR_JUNK)
2054 st->v->flags |= VAR_KEEP;
2055 if (eflags & VARE_WANTRES) {
2056 st->newVal = Buf_Destroy(&buf, FALSE);
2058 st->newVal = st->val;
2059 Buf_Destroy(&buf, TRUE);
2065 static ApplyModifierResult
2066 ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st)
2070 const char *mod = *pp;
2071 if (!ModMatchEq(mod, "gmtime", st->endc))
2074 if (mod[6] == '=') {
2076 utc = (time_t)strtoul(mod + 7, &ep, 10);
2082 st->newVal = VarStrftime(st->val, TRUE, utc);
2088 ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st)
2092 const char *mod = *pp;
2093 if (!ModMatchEq(mod, "localtime", st->endc))
2096 if (mod[9] == '=') {
2098 utc = (time_t)strtoul(mod + 10, &ep, 10);
2104 st->newVal = VarStrftime(st->val, FALSE, utc);
2109 static ApplyModifierResult
2110 ApplyModifier_Hash(const char **pp, ApplyModifiersState *st)
2112 if (!ModMatch(*pp, "hash", st->endc))
2115 st->newVal = VarHash(st->val);
2121 static ApplyModifierResult
2122 ApplyModifier_Path(const char **pp, ApplyModifiersState *st)
2127 if (st->v->flags & VAR_JUNK)
2128 st->v->flags |= VAR_KEEP;
2130 gn = Targ_FindNode(st->v->name, TARG_NOCREATE);
2131 if (gn == NULL || gn->type & OP_NOPATH) {
2133 } else if (gn->path) {
2134 path = bmake_strdup(gn->path);
2136 Lst searchPath = Suff_FindPath(gn);
2137 path = Dir_FindFile(st->v->name, searchPath);
2140 path = bmake_strdup(st->v->name);
2148 static ApplyModifierResult
2149 ApplyModifier_Exclam(const char **pp, ApplyModifiersState *st)
2157 cmd = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2160 st->missing_delim = delim;
2165 if (st->eflags & VARE_WANTRES)
2166 st->newVal = Cmd_Exec(cmd, &errfmt);
2168 st->newVal = varNoError;
2172 Error(errfmt, st->val); /* XXX: why still return AMR_OK? */
2174 if (st->v->flags & VAR_JUNK)
2175 st->v->flags |= VAR_KEEP;
2179 /* The :range modifier generates an integer sequence as long as the words.
2180 * The :range=7 modifier generates an integer sequence from 1 to 7. */
2181 static ApplyModifierResult
2182 ApplyModifier_Range(const char **pp, ApplyModifiersState *st)
2188 const char *mod = *pp;
2189 if (!ModMatchEq(mod, "range", st->endc))
2192 if (mod[5] == '=') {
2194 n = (size_t)strtoul(mod + 6, &ep, 10);
2202 Words words = Str_Words(st->val, FALSE);
2209 for (i = 0; i < n; i++) {
2211 Buf_AddByte(&buf, ' '); /* XXX: st->sep, for consistency */
2212 Buf_AddInt(&buf, 1 + (int)i);
2215 st->newVal = Buf_Destroy(&buf, FALSE);
2219 /* :Mpattern or :Npattern */
2220 static ApplyModifierResult
2221 ApplyModifier_Match(const char **pp, ApplyModifiersState *st)
2223 const char *mod = *pp;
2224 Boolean copy = FALSE; /* pattern should be, or has been, copied */
2225 Boolean needSubst = FALSE;
2228 ModifyWordsCallback callback;
2231 * In the loop below, ignore ':' unless we are at (or back to) the
2232 * original brace level.
2233 * XXX This will likely not work right if $() and ${} are intermixed.
2237 for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) {
2239 (p[1] == ':' || p[1] == st->endc || p[1] == st->startc)) {
2247 if (*p == '(' || *p == '{')
2249 if (*p == ')' || *p == '}') {
2262 /* Compress the \:'s out of the pattern. */
2263 pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1);
2266 for (; src < endpat; src++, dst++) {
2267 if (src[0] == '\\' && src + 1 < endpat &&
2268 /* XXX: st->startc is missing here; see above */
2269 (src[1] == ':' || src[1] == st->endc))
2276 pattern = bmake_strsedup(mod + 1, endpat);
2280 /* pattern contains embedded '$', so use Var_Subst to expand it. */
2281 char *old_pattern = pattern;
2282 pattern = Var_Subst(pattern, st->ctxt, st->eflags);
2286 VAR_DEBUG("Pattern[%s] for [%s] is [%s]\n", st->v->name, st->val, pattern);
2288 callback = mod[0] == 'M' ? ModifyWord_Match : ModifyWord_NoMatch;
2289 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2296 static ApplyModifierResult
2297 ApplyModifier_Subst(const char **pp, ApplyModifiersState *st)
2299 ModifyWord_SubstArgs args;
2303 char delim = (*pp)[1];
2304 if (delim == '\0') {
2305 Error("Missing delimiter for :S modifier");
2313 args.matched = FALSE;
2316 * If pattern begins with '^', it is anchored to the
2317 * start of the word -- skip over it and flag pattern.
2320 args.pflags |= VARP_ANCHOR_START;
2324 lhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2325 &args.lhsLen, &args.pflags, NULL);
2327 st->missing_delim = delim;
2332 rhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2333 &args.rhsLen, NULL, &args);
2335 st->missing_delim = delim;
2340 oneBigWord = st->oneBigWord;
2344 args.pflags |= VARP_SUB_GLOBAL;
2347 args.pflags |= VARP_SUB_ONE;
2356 st->newVal = ModifyWords(st->ctxt, st->sep, oneBigWord, st->val,
2357 ModifyWord_Subst, &args);
2367 static ApplyModifierResult
2368 ApplyModifier_Regex(const char **pp, ApplyModifiersState *st)
2371 ModifyWord_SubstRegexArgs args;
2375 char delim = (*pp)[1];
2376 if (delim == '\0') {
2377 Error("Missing delimiter for :C modifier");
2384 re = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2386 st->missing_delim = delim;
2390 args.replace = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2392 if (args.replace == NULL) {
2394 st->missing_delim = delim;
2399 args.matched = FALSE;
2400 oneBigWord = st->oneBigWord;
2404 args.pflags |= VARP_SUB_GLOBAL;
2407 args.pflags |= VARP_SUB_ONE;
2416 error = regcomp(&args.re, re, REG_EXTENDED);
2419 VarREError(error, &args.re, "Regex compilation error");
2424 args.nsub = args.re.re_nsub + 1;
2427 st->newVal = ModifyWords(st->ctxt, st->sep, oneBigWord, st->val,
2428 ModifyWord_SubstRegex, &args);
2436 ModifyWord_Copy(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
2438 SepBuf_AddStr(buf, word);
2441 /* :ts<separator> */
2442 static ApplyModifierResult
2443 ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st)
2445 /* XXX: pp points to the 's', for historic reasons only.
2446 * Changing this will influence the error messages. */
2447 const char *sep = *pp + 1;
2449 /* ":ts<any><endc>" or ":ts<any>:" */
2450 if (sep[0] != st->endc && (sep[1] == st->endc || sep[1] == ':')) {
2456 /* ":ts<endc>" or ":ts:" */
2457 if (sep[0] == st->endc || sep[0] == ':') {
2458 st->sep = '\0'; /* no separator */
2463 /* ":ts<unrecognised><unrecognised>". */
2468 if (sep[1] == 'n') {
2475 if (sep[1] == 't') {
2481 /* ":ts\x40" or ":ts\100" */
2483 const char *numStart = sep + 1;
2484 int base = 8; /* assume octal */
2487 if (sep[1] == 'x') {
2490 } else if (!isdigit((unsigned char)sep[1]))
2491 return AMR_BAD; /* ":ts<backslash><unrecognised>". */
2493 st->sep = (char)strtoul(numStart, &end, base);
2494 if (*end != ':' && *end != st->endc)
2500 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2501 ModifyWord_Copy, NULL);
2505 /* :tA, :tu, :tl, :ts<separator>, etc. */
2506 static ApplyModifierResult
2507 ApplyModifier_To(const char **pp, ApplyModifiersState *st)
2509 const char *mod = *pp;
2510 assert(mod[0] == 't');
2512 *pp = mod + 1; /* make sure it is set */
2513 if (mod[1] == st->endc || mod[1] == ':' || mod[1] == '\0')
2514 return AMR_BAD; /* Found ":t<endc>" or ":t:". */
2517 return ApplyModifier_ToSep(pp, st);
2519 if (mod[2] != st->endc && mod[2] != ':')
2520 return AMR_BAD; /* Found ":t<unrecognised><unrecognised>". */
2522 /* Check for two-character options: ":tu", ":tl" */
2523 if (mod[1] == 'A') { /* absolute path */
2524 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2525 ModifyWord_Realpath, NULL);
2530 if (mod[1] == 'u') {
2532 size_t len = strlen(st->val);
2533 st->newVal = bmake_malloc(len + 1);
2534 for (i = 0; i < len + 1; i++)
2535 st->newVal[i] = (char)toupper((unsigned char)st->val[i]);
2540 if (mod[1] == 'l') {
2542 size_t len = strlen(st->val);
2543 st->newVal = bmake_malloc(len + 1);
2544 for (i = 0; i < len + 1; i++)
2545 st->newVal[i] = (char)tolower((unsigned char)st->val[i]);
2550 if (mod[1] == 'W' || mod[1] == 'w') {
2551 st->oneBigWord = mod[1] == 'W';
2552 st->newVal = st->val;
2557 /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */
2561 /* :[#], :[1], etc. */
2562 static ApplyModifierResult
2563 ApplyModifier_Words(const char **pp, ApplyModifiersState *st)
2570 (*pp)++; /* skip the '[' */
2571 delim = ']'; /* look for closing ']' */
2572 estr = ParseModifierPart(pp, delim, st->eflags, st->ctxt,
2575 st->missing_delim = delim;
2579 /* now *pp points just after the closing ']' */
2580 if (**pp != ':' && **pp != st->endc)
2581 goto bad_modifier; /* Found junk after ']' */
2583 if (estr[0] == '\0')
2584 goto bad_modifier; /* empty square brackets in ":[]". */
2586 if (estr[0] == '#' && estr[1] == '\0') { /* Found ":[#]" */
2587 if (st->oneBigWord) {
2588 st->newVal = bmake_strdup("1");
2592 Words words = Str_Words(st->val, FALSE);
2593 size_t ac = words.len;
2596 Buf_Init(&buf, 4); /* 3 digits + '\0' is usually enough */
2597 Buf_AddInt(&buf, (int)ac);
2598 st->newVal = Buf_Destroy(&buf, FALSE);
2603 if (estr[0] == '*' && estr[1] == '\0') {
2605 st->oneBigWord = TRUE;
2606 st->newVal = st->val;
2610 if (estr[0] == '@' && estr[1] == '\0') {
2612 st->oneBigWord = FALSE;
2613 st->newVal = st->val;
2618 * We expect estr to contain a single integer for :[N], or two integers
2619 * separated by ".." for :[start..end].
2621 first = (int)strtol(estr, &ep, 0);
2622 if (ep == estr) /* Found junk instead of a number */
2625 if (ep[0] == '\0') { /* Found only one integer in :[N] */
2627 } else if (ep[0] == '.' && ep[1] == '.' && ep[2] != '\0') {
2628 /* Expecting another integer after ".." */
2630 last = (int)strtol(ep, &ep, 0);
2631 if (ep[0] != '\0') /* Found junk after ".." */
2634 goto bad_modifier; /* Found junk instead of ".." */
2637 * Now seldata is properly filled in, but we still have to check for 0 as
2640 if (first == 0 && last == 0) {
2641 /* ":[0]" or perhaps ":[0..0]" */
2642 st->oneBigWord = TRUE;
2643 st->newVal = st->val;
2647 /* ":[0..N]" or ":[N..0]" */
2648 if (first == 0 || last == 0)
2651 /* Normal case: select the words described by seldata. */
2652 st->newVal = VarSelectWords(st->sep, st->oneBigWord, st->val, first, last);
2664 str_cmp_asc(const void *a, const void *b)
2666 return strcmp(*(const char * const *)a, *(const char * const *)b);
2670 str_cmp_desc(const void *a, const void *b)
2672 return strcmp(*(const char * const *)b, *(const char * const *)a);
2675 /* :O (order ascending) or :Or (order descending) or :Ox (shuffle) */
2676 static ApplyModifierResult
2677 ApplyModifier_Order(const char **pp, ApplyModifiersState *st)
2679 const char *mod = (*pp)++; /* skip past the 'O' in any case */
2681 Words words = Str_Words(st->val, FALSE);
2683 if (mod[1] == st->endc || mod[1] == ':') {
2684 /* :O sorts ascending */
2685 qsort(words.words, words.len, sizeof(char *), str_cmp_asc);
2687 } else if ((mod[1] == 'r' || mod[1] == 'x') &&
2688 (mod[2] == st->endc || mod[2] == ':')) {
2691 if (mod[1] == 'r') {
2692 /* :Or sorts descending */
2693 qsort(words.words, words.len, sizeof(char *), str_cmp_desc);
2698 * We will use [ac..2] range for mod factors. This will produce
2699 * random numbers in [(ac-1)..0] interval, and minimal
2700 * reasonable value for mod factor is 2 (the mod 1 will produce
2701 * 0 with probability 1).
2704 for (i = words.len - 1; i > 0; i--) {
2705 size_t rndidx = (size_t)random() % (i + 1);
2706 char *t = words.words[i];
2707 words.words[i] = words.words[rndidx];
2708 words.words[rndidx] = t;
2716 st->newVal = Words_JoinFree(words);
2720 /* :? then : else */
2721 static ApplyModifierResult
2722 ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st)
2725 char *then_expr, *else_expr;
2727 Boolean value = FALSE;
2728 VarEvalFlags then_eflags = st->eflags & ~(unsigned)VARE_WANTRES;
2729 VarEvalFlags else_eflags = st->eflags & ~(unsigned)VARE_WANTRES;
2731 int cond_rc = COND_PARSE; /* anything other than COND_INVALID */
2732 if (st->eflags & VARE_WANTRES) {
2733 cond_rc = Cond_EvalExpression(NULL, st->v->name, &value, 0, FALSE);
2734 if (cond_rc != COND_INVALID && value)
2735 then_eflags |= VARE_WANTRES;
2736 if (cond_rc != COND_INVALID && !value)
2737 else_eflags |= VARE_WANTRES;
2740 (*pp)++; /* skip past the '?' */
2742 then_expr = ParseModifierPart(pp, delim, then_eflags, st->ctxt,
2744 if (then_expr == NULL) {
2745 st->missing_delim = delim;
2749 delim = st->endc; /* BRCLOSE or PRCLOSE */
2750 else_expr = ParseModifierPart(pp, delim, else_eflags, st->ctxt,
2752 if (else_expr == NULL) {
2753 st->missing_delim = delim;
2758 if (cond_rc == COND_INVALID) {
2759 Error("Bad conditional expression `%s' in %s?%s:%s",
2760 st->v->name, st->v->name, then_expr, else_expr);
2765 st->newVal = then_expr;
2768 st->newVal = else_expr;
2771 if (st->v->flags & VAR_JUNK)
2772 st->v->flags |= VAR_KEEP;
2777 * The ::= modifiers actually assign a value to the variable.
2778 * Their main purpose is in supporting modifiers of .for loop
2779 * iterators and other obscure uses. They always expand to
2780 * nothing. In a target rule that would otherwise expand to an
2781 * empty line they can be preceded with @: to keep make happy.
2785 * .for i in ${.TARGET} ${.TARGET:R}.gz
2790 * ::=<str> Assigns <str> as the new value of variable.
2791 * ::?=<str> Assigns <str> as value of variable if
2792 * it was not already set.
2793 * ::+=<str> Appends <str> to variable.
2794 * ::!=<cmd> Assigns output of <cmd> as the new value of
2797 static ApplyModifierResult
2798 ApplyModifier_Assign(const char **pp, ApplyModifiersState *st)
2805 const char *mod = *pp;
2806 const char *op = mod + 1;
2807 if (!(op[0] == '=' ||
2809 (op[0] == '!' || op[0] == '+' || op[0] == '?'))))
2810 return AMR_UNKNOWN; /* "::<unrecognised>" */
2813 if (st->v->name[0] == 0) {
2818 v_ctxt = st->ctxt; /* context where v belongs */
2820 if (st->v->flags & VAR_JUNK) {
2822 * We need to bmake_strdup() it in case ParseModifierPart() recurses.
2824 sv_name = st->v->name;
2825 st->v->name = bmake_strdup(st->v->name);
2826 } else if (st->ctxt != VAR_GLOBAL) {
2827 Var *gv = VarFind(st->v->name, st->ctxt, 0);
2829 v_ctxt = VAR_GLOBAL;
2831 VarFreeEnv(gv, TRUE);
2845 delim = st->startc == PROPEN ? PRCLOSE : BRCLOSE;
2846 val = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2847 if (st->v->flags & VAR_JUNK) {
2848 /* restore original name */
2850 st->v->name = sv_name;
2853 st->missing_delim = delim;
2859 if (st->eflags & VARE_WANTRES) {
2862 Var_Append(st->v->name, val, v_ctxt);
2866 char *cmd_output = Cmd_Exec(val, &errfmt);
2870 Var_Set(st->v->name, cmd_output, v_ctxt);
2875 if (!(st->v->flags & VAR_JUNK))
2879 Var_Set(st->v->name, val, v_ctxt);
2884 st->newVal = varNoError; /* XXX: varNoError is kind of an error,
2885 * the intention here is to just return
2886 * an empty string. */
2890 /* remember current value */
2891 static ApplyModifierResult
2892 ApplyModifier_Remember(const char **pp, ApplyModifiersState *st)
2894 const char *mod = *pp;
2895 if (!ModMatchEq(mod, "_", st->endc))
2898 if (mod[1] == '=') {
2899 size_t n = strcspn(mod + 2, ":)}");
2900 char *name = bmake_strldup(mod + 2, n);
2901 Var_Set(name, st->val, st->ctxt);
2905 Var_Set("_", st->val, st->ctxt);
2908 st->newVal = st->val;
2912 /* Apply the given function to each word of the variable value. */
2913 static ApplyModifierResult
2914 ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
2915 ModifyWordsCallback modifyWord)
2917 char delim = (*pp)[1];
2918 if (delim != st->endc && delim != ':')
2921 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord,
2922 st->val, modifyWord, NULL);
2929 static ApplyModifierResult
2930 ApplyModifier_SysV(const char **pp, ApplyModifiersState *st)
2935 const char *mod = *pp;
2936 Boolean eqFound = FALSE;
2939 * First we make a pass through the string trying
2940 * to verify it is a SYSV-make-style translation:
2941 * it must be: <string1>=<string2>)
2944 const char *next = mod;
2945 while (*next != '\0' && nest > 0) {
2948 /* continue looking for st->endc */
2949 } else if (*next == st->endc)
2951 else if (*next == st->startc)
2956 if (*next != st->endc || !eqFound)
2961 lhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2963 st->missing_delim = delim;
2968 rhs = ParseModifierPart(pp, delim, st->eflags, st->ctxt, NULL, NULL, NULL);
2970 st->missing_delim = delim;
2975 * SYSV modifications happen through the whole
2976 * string. Note the pattern is anchored at the end.
2979 if (lhs[0] == '\0' && *st->val == '\0') {
2980 st->newVal = st->val; /* special case */
2982 ModifyWord_SYSVSubstArgs args = {st->ctxt, lhs, rhs};
2983 st->newVal = ModifyWords(st->ctxt, st->sep, st->oneBigWord, st->val,
2984 ModifyWord_SYSVSubst, &args);
2992 /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */
2995 const char **pp, /* the parsing position, updated upon return */
2996 char *val, /* the current value of the variable */
2997 char const startc, /* '(' or '{', or '\0' for indirect modifiers */
2998 char const endc, /* ')' or '}', or '\0' for indirect modifiers */
2999 Var * const v, /* the variable may have its flags changed */
3000 GNode * const ctxt, /* for looking up and modifying variables */
3001 VarEvalFlags const eflags,
3002 void ** const freePtr /* free this after using the return value */
3004 ApplyModifiersState st = {
3005 startc, endc, v, ctxt, eflags, val,
3006 var_Error, /* .newVal */
3007 '\0', /* .missing_delim */
3009 FALSE /* .oneBigWord */
3013 ApplyModifierResult res;
3015 assert(startc == '(' || startc == '{' || startc == '\0');
3016 assert(endc == ')' || endc == '}' || endc == '\0');
3017 assert(val != NULL);
3020 while (*p != '\0' && *p != endc) {
3024 * We may have some complex modifiers in a variable.
3028 const char *rval = Var_Parse(p, st.ctxt, st.eflags, &rlen, &freeIt);
3031 * If we have not parsed up to st.endc or ':',
3032 * we are not interested.
3035 if (rval[0] != '\0' &&
3036 (c = p[rlen]) != '\0' && c != ':' && c != st.endc) {
3041 VAR_DEBUG("Indirect modifier \"%s\" from \"%.*s\"\n",
3046 if (rval[0] != '\0') {
3047 const char *rval_pp = rval;
3048 st.val = ApplyModifiers(&rval_pp, st.val, '\0', '\0', v,
3049 ctxt, eflags, freePtr);
3050 if (st.val == var_Error
3051 || (st.val == varNoError && !(st.eflags & VARE_UNDEFERR))
3052 || *rval_pp != '\0') {
3054 goto out; /* error already reported */
3060 else if (*p == '\0' && endc != '\0') {
3061 Error("Unclosed variable specification after complex "
3062 "modifier (expecting '%c') for %s", st.endc, st.v->name);
3068 st.newVal = var_Error; /* default value, in case of errors */
3069 res = AMR_BAD; /* just a safe fallback */
3073 char eflags_str[VarEvalFlags_ToStringSize];
3074 char vflags_str[VarFlags_ToStringSize];
3075 Boolean is_single_char = mod[0] != '\0' &&
3076 (mod[1] == endc || mod[1] == ':');
3078 /* At this point, only the first character of the modifier can
3079 * be used since the end of the modifier is not yet known. */
3080 VAR_DEBUG("Applying ${%s:%c%s} to \"%s\" "
3081 "(eflags = %s, vflags = %s)\n",
3082 st.v->name, mod[0], is_single_char ? "" : "...", st.val,
3083 Enum_FlagsToString(eflags_str, sizeof eflags_str,
3084 st.eflags, VarEvalFlags_ToStringSpecs),
3085 Enum_FlagsToString(vflags_str, sizeof vflags_str,
3086 st.v->flags, VarFlags_ToStringSpecs));
3091 res = ApplyModifier_Assign(&p, &st);
3094 res = ApplyModifier_Loop(&p, &st);
3097 res = ApplyModifier_Remember(&p, &st);
3101 res = ApplyModifier_Defined(&p, &st);
3104 if (st.v->flags & VAR_JUNK)
3105 st.v->flags |= VAR_KEEP;
3106 st.newVal = bmake_strdup(st.v->name);
3111 res = ApplyModifier_Path(&p, &st);
3114 res = ApplyModifier_Exclam(&p, &st);
3117 res = ApplyModifier_Words(&p, &st);
3120 res = ApplyModifier_Gmtime(&p, &st);
3123 res = ApplyModifier_Hash(&p, &st);
3126 res = ApplyModifier_Localtime(&p, &st);
3129 res = ApplyModifier_To(&p, &st);
3133 res = ApplyModifier_Match(&p, &st);
3136 res = ApplyModifier_Subst(&p, &st);
3139 res = ApplyModifier_IfElse(&p, &st);
3143 res = ApplyModifier_Regex(&p, &st);
3148 if (p[1] == st.endc || p[1] == ':') {
3149 st.newVal = VarQuote(st.val, *mod == 'q');
3156 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Tail);
3159 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Head);
3162 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Suffix);
3165 res = ApplyModifier_WordFunc(&p, &st, ModifyWord_Root);
3168 res = ApplyModifier_Range(&p, &st);
3171 res = ApplyModifier_Order(&p, &st);
3174 if (p[1] == st.endc || p[1] == ':') {
3175 st.newVal = VarUniq(st.val);
3183 if (p[1] == 'h' && (p[2] == st.endc || p[2] == ':')) {
3184 if (st.eflags & VARE_WANTRES) {
3186 st.newVal = Cmd_Exec(st.val, &errfmt);
3188 Error(errfmt, st.val);
3190 st.newVal = varNoError;
3202 if (res == AMR_UNKNOWN) {
3204 res = ApplyModifier_SysV(&p, &st);
3208 if (res == AMR_UNKNOWN) {
3209 Error("Unknown modifier '%c'", *mod);
3210 for (p++; *p != ':' && *p != st.endc && *p != '\0'; p++)
3212 st.newVal = var_Error;
3214 if (res == AMR_CLEANUP)
3220 char eflags_str[VarEvalFlags_ToStringSize];
3221 char vflags_str[VarFlags_ToStringSize];
3222 const char *quot = st.newVal == var_Error ? "" : "\"";
3223 const char *newVal = st.newVal == var_Error ? "error" : st.newVal;
3225 VAR_DEBUG("Result of ${%s:%.*s} is %s%s%s "
3226 "(eflags = %s, vflags = %s)\n",
3227 st.v->name, (int)(p - mod), mod, quot, newVal, quot,
3228 Enum_FlagsToString(eflags_str, sizeof eflags_str,
3229 st.eflags, VarEvalFlags_ToStringSpecs),
3230 Enum_FlagsToString(vflags_str, sizeof vflags_str,
3231 st.v->flags, VarFlags_ToStringSpecs));
3234 if (st.newVal != st.val) {
3240 if (st.val != var_Error && st.val != varNoError) {
3244 if (*p == '\0' && st.endc != '\0') {
3245 Error("Unclosed variable specification (expecting '%c') "
3246 "for \"%s\" (value \"%s\") modifier %c",
3247 st.endc, st.v->name, st.val, *mod);
3248 } else if (*p == ':') {
3255 assert(st.val != NULL); /* Use var_Error or varNoError instead. */
3259 Error("Bad modifier `:%.*s' for %s",
3260 (int)strcspn(mod, ":)}"), mod, st.v->name);
3264 if (st.missing_delim != '\0')
3265 Error("Unfinished modifier for %s ('%c' missing)",
3266 st.v->name, st.missing_delim);
3273 VarIsDynamic(GNode *ctxt, const char *varname, size_t namelen)
3275 if ((namelen == 1 ||
3276 (namelen == 2 && (varname[1] == 'F' || varname[1] == 'D'))) &&
3277 (ctxt == VAR_CMD || ctxt == VAR_GLOBAL))
3280 * If substituting a local variable in a non-local context,
3281 * assume it's for dynamic source stuff. We have to handle
3282 * this specially and return the longhand for the variable
3283 * with the dollar sign escaped so it makes it back to the
3284 * caller. Only four of the local variables are treated
3285 * specially as they are the only four that will be set
3286 * when dynamic sources are expanded.
3288 switch (varname[0]) {
3298 if ((namelen == 7 || namelen == 8) && varname[0] == '.' &&
3299 isupper((unsigned char)varname[1]) &&
3300 (ctxt == VAR_CMD || ctxt == VAR_GLOBAL))
3302 return strcmp(varname, ".TARGET") == 0 ||
3303 strcmp(varname, ".ARCHIVE") == 0 ||
3304 strcmp(varname, ".PREFIX") == 0 ||
3305 strcmp(varname, ".MEMBER") == 0;
3312 *-----------------------------------------------------------------------
3314 * Given the start of a variable invocation (such as $v, $(VAR),
3315 * ${VAR:Mpattern}), extract the variable name, possibly some
3316 * modifiers and find its value by applying the modifiers to the
3320 * str The string to parse
3321 * ctxt The context for the variable
3322 * flags VARE_UNDEFERR if undefineds are an error
3323 * VARE_WANTRES if we actually want the result
3324 * VARE_ASSIGN if we are in a := assignment
3325 * lengthPtr OUT: The length of the specification
3326 * freePtr OUT: Non-NULL if caller should free *freePtr
3329 * Returns the value of the variable expression, never NULL.
3330 * var_Error if there was a parse error and VARE_UNDEFERR was set.
3331 * varNoError if there was a parse error and VARE_UNDEFERR was not set.
3333 * Parsing should continue at str + *lengthPtr.
3335 * After using the returned value, *freePtr must be freed, preferably
3336 * using bmake_free since it is NULL in most cases.
3339 * Any effects from the modifiers, such as :!cmd! or ::=value.
3340 *-----------------------------------------------------------------------
3342 /* coverity[+alloc : arg-*4] */
3344 Var_Parse(const char * const str, GNode *ctxt, VarEvalFlags eflags,
3345 int *lengthPtr, void **freePtr)
3347 const char *tstr; /* Pointer into str */
3348 Boolean haveModifier; /* TRUE if have modifiers for the variable */
3349 char startc; /* Starting character if variable in parens
3351 char endc; /* Ending character if variable in parens
3353 Boolean dynamic; /* TRUE if the variable is local and we're
3354 * expanding it in a non-local context. This
3355 * is done to support dynamic sources. The
3356 * result is just the invocation, unaltered */
3357 const char *extramodifiers;
3360 char eflags_str[VarEvalFlags_ToStringSize];
3362 VAR_DEBUG("%s: %s with %s\n", __func__, str,
3363 Enum_FlagsToString(eflags_str, sizeof eflags_str, eflags,
3364 VarEvalFlags_ToStringSpecs));
3367 extramodifiers = NULL; /* extra modifiers to apply first */
3370 #ifdef USE_DOUBLE_BOOLEAN
3371 /* Appease GCC 5.5.0, which thinks that the variable might not be
3377 if (startc != PROPEN && startc != BROPEN) {
3381 * If it's not bounded by braces of some sort, life is much simpler.
3382 * We just need to check for the first character and return the
3383 * value if it exists.
3386 /* Error out some really stupid names */
3387 if (startc == '\0' || strchr(")}:$", startc)) {
3394 v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3398 if (ctxt == VAR_CMD || ctxt == VAR_GLOBAL) {
3400 * If substituting a local variable in a non-local context,
3401 * assume it's for dynamic source stuff. We have to handle
3402 * this specially and return the longhand for the variable
3403 * with the dollar sign escaped so it makes it back to the
3404 * caller. Only four of the local variables are treated
3405 * specially as they are the only four that will be set
3406 * when dynamic sources are expanded.
3410 return "$(.TARGET)";
3412 return "$(.MEMBER)";
3414 return "$(.PREFIX)";
3416 return "$(.ARCHIVE)";
3419 return (eflags & VARE_UNDEFERR) ? var_Error : varNoError;
3421 haveModifier = FALSE;
3425 Buffer namebuf; /* Holds the variable name */
3430 endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
3432 Buf_Init(&namebuf, 0);
3435 * Skip to the end character or a colon, whichever comes first.
3438 for (tstr = str + 2; *tstr != '\0'; tstr++) {
3439 /* Track depth so we can spot parse errors. */
3440 if (*tstr == startc)
3442 if (*tstr == endc) {
3446 if (*tstr == ':' && depth == 1)
3448 /* A variable inside a variable, expand. */
3452 const char *rval = Var_Parse(tstr, ctxt, eflags, &rlen,
3454 Buf_AddStr(&namebuf, rval);
3458 Buf_AddByte(&namebuf, *tstr);
3461 haveModifier = TRUE;
3462 } else if (*tstr == endc) {
3463 haveModifier = FALSE;
3465 Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"",
3466 Buf_GetAll(&namebuf, NULL));
3468 * If we never did find the end character, return NULL
3469 * right now, setting the length to be the distance to
3470 * the end of the string, since that's what make does.
3472 *lengthPtr = (int)(size_t)(tstr - str);
3473 Buf_Destroy(&namebuf, TRUE);
3477 varname = Buf_GetAll(&namebuf, &namelen);
3480 * At this point, varname points into newly allocated memory from
3481 * namebuf, containing only the name of the variable.
3483 * start and tstr point into the const string that was pointed
3484 * to by the original value of the str parameter. start points
3485 * to the '$' at the beginning of the string, while tstr points
3486 * to the char just after the end of the variable name -- this
3487 * will be '\0', ':', PRCLOSE, or BRCLOSE.
3490 v = VarFind(varname, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
3492 * Check also for bogus D and F forms of local variables since we're
3493 * in a local context and the name is the right length.
3495 if (v == NULL && ctxt != VAR_CMD && ctxt != VAR_GLOBAL &&
3496 namelen == 2 && (varname[1] == 'F' || varname[1] == 'D') &&
3497 strchr("@%?*!<>", varname[0]) != NULL)
3500 * Well, it's local -- go look for it.
3502 char name[] = { varname[0], '\0' };
3503 v = VarFind(name, ctxt, 0);
3506 if (varname[1] == 'D') {
3507 extramodifiers = "H:";
3509 extramodifiers = "T:";
3515 dynamic = VarIsDynamic(ctxt, varname, namelen);
3517 if (!haveModifier) {
3519 * No modifiers -- have specification length so we can return
3522 *lengthPtr = (int)(size_t)(tstr - str) + 1;
3524 char *pstr = bmake_strldup(str, (size_t)*lengthPtr);
3526 Buf_Destroy(&namebuf, TRUE);
3529 Buf_Destroy(&namebuf, TRUE);
3530 return (eflags & VARE_UNDEFERR) ? var_Error : varNoError;
3534 * Still need to get to the end of the variable specification,
3535 * so kludge up a Var structure for the modifications
3537 v = bmake_malloc(sizeof(Var));
3539 Buf_Init(&v->val, 1);
3540 v->flags = VAR_JUNK;
3541 Buf_Destroy(&namebuf, FALSE);
3544 Buf_Destroy(&namebuf, TRUE);
3547 if (v->flags & VAR_IN_USE) {
3548 Fatal("Variable %s is recursive.", v->name);
3551 v->flags |= VAR_IN_USE;
3555 * Before doing any modification, we have to make sure the value
3556 * has been fully expanded. If it looks like recursion might be
3557 * necessary (there's a dollar sign somewhere in the variable's value)
3558 * we just call Var_Subst to do any other substitutions that are
3559 * necessary. Note that the value returned by Var_Subst will have
3560 * been dynamically-allocated, so it will need freeing when we
3563 nstr = Buf_GetAll(&v->val, NULL);
3564 if (strchr(nstr, '$') != NULL && (eflags & VARE_WANTRES) != 0) {
3565 nstr = Var_Subst(nstr, ctxt, eflags);
3569 v->flags &= ~(unsigned)VAR_IN_USE;
3571 if (haveModifier || extramodifiers != NULL) {
3575 if (extramodifiers != NULL) {
3576 const char *em = extramodifiers;
3577 nstr = ApplyModifiers(&em, nstr, '(', ')',
3578 v, ctxt, eflags, &extraFree);
3582 /* Skip initial colon. */
3585 nstr = ApplyModifiers(&tstr, nstr, startc, endc,
3586 v, ctxt, eflags, freePtr);
3589 *freePtr = extraFree;
3593 /* Skip past endc if possible. */
3594 *lengthPtr = (int)(size_t)(tstr + (*tstr ? 1 : 0) - str);
3596 if (v->flags & VAR_FROM_ENV) {
3597 Boolean destroy = nstr != Buf_GetAll(&v->val, NULL);
3600 * Returning the value unmodified, so tell the caller to free
3605 (void)VarFreeEnv(v, destroy);
3606 } else if (v->flags & VAR_JUNK) {
3608 * Perform any freeing needed and set *freePtr to NULL so the caller
3609 * doesn't try to free a static pointer.
3610 * If VAR_KEEP is also set then we want to keep str(?) as is.
3612 if (!(v->flags & VAR_KEEP)) {
3613 if (*freePtr != NULL) {
3618 nstr = bmake_strldup(str, (size_t)*lengthPtr);
3621 nstr = (eflags & VARE_UNDEFERR) ? var_Error : varNoError;
3624 if (nstr != Buf_GetAll(&v->val, NULL))
3625 Buf_Destroy(&v->val, TRUE);
3632 /* Substitute for all variables in the given string in the given context.
3634 * If eflags & VARE_UNDEFERR, Parse_Error will be called when an undefined
3635 * variable is encountered.
3637 * If eflags & VARE_WANTRES, any effects from the modifiers, such as ::=,
3638 * :sh or !cmd! take place.
3641 * str the string which to substitute
3642 * ctxt the context wherein to find variables
3643 * eflags VARE_UNDEFERR if undefineds are an error
3644 * VARE_WANTRES if we actually want the result
3645 * VARE_ASSIGN if we are in a := assignment
3648 * The resulting string.
3651 Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags)
3653 Buffer buf; /* Buffer for forming things */
3654 Boolean trailingBslash;
3656 /* Set true if an error has already been reported,
3657 * to prevent a plethora of messages when recursing */
3658 static Boolean errorReported;
3661 errorReported = FALSE;
3662 trailingBslash = FALSE; /* variable ends in \ */
3665 if (*str == '\n' && trailingBslash)
3666 Buf_AddByte(&buf, ' ');
3668 if (*str == '$' && str[1] == '$') {
3670 * A dollar sign may be escaped with another dollar sign.
3671 * In such a case, we skip over the escape character and store the
3672 * dollar sign into the buffer directly.
3674 if (save_dollars && (eflags & VARE_ASSIGN))
3675 Buf_AddByte(&buf, '$');
3676 Buf_AddByte(&buf, '$');
3678 } else if (*str != '$') {
3680 * Skip as many characters as possible -- either to the end of
3681 * the string or to the next dollar sign (variable invocation).
3685 for (cp = str++; *str != '$' && *str != '\0'; str++)
3687 Buf_AddBytesBetween(&buf, cp, str);
3691 const char *val = Var_Parse(str, ctxt, eflags, &length, &freeIt);
3693 if (val == var_Error || val == varNoError) {
3695 * If performing old-time variable substitution, skip over
3696 * the variable and continue with the substitution. Otherwise,
3697 * store the dollar sign and advance str so we continue with
3702 } else if ((eflags & VARE_UNDEFERR) || val == var_Error) {
3704 * If variable is undefined, complain and skip the
3705 * variable. The complaint will stop us from doing anything
3706 * when the file is parsed.
3708 if (!errorReported) {
3709 Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"",
3713 errorReported = TRUE;
3715 Buf_AddByte(&buf, *str);
3723 val_len = strlen(val);
3724 Buf_AddBytes(&buf, val, val_len);
3725 trailingBslash = val_len > 0 && val[val_len - 1] == '\\';
3732 return Buf_DestroyCompact(&buf);
3735 /* Initialize the module. */
3739 VAR_INTERNAL = Targ_NewGN("Internal");
3740 VAR_GLOBAL = Targ_NewGN("Global");
3741 VAR_CMD = Targ_NewGN("Command");
3754 Hash_DebugStats(&VAR_GLOBAL->context, "VAR_GLOBAL");
3758 /****************** PRINT DEBUGGING INFO *****************/
3760 VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED)
3763 fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
3766 /* Print all variables in a context, unordered. */
3768 Var_Dump(GNode *ctxt)
3770 Hash_ForEach(&ctxt->context, VarPrintVar, NULL);