1 /* $NetBSD: var.c,v 1.883 2021/03/14 20:23:29 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 * Handling of variables and the expressions formed from them.
74 * Variables are set using lines of the form VAR=value. Both the variable
75 * name and the value can contain references to other variables, by using
76 * expressions like ${VAR}, ${VAR:Modifiers}, ${${VARNAME}} or ${VAR:${MODS}}.
79 * Var_Init Initialize this module.
81 * Var_End Clean up the module.
85 * Set the value of the variable, creating it if
90 * Append more characters to the variable, creating it if
91 * necessary. A space is placed between the old value and
96 * See if a variable exists.
98 * Var_Value Return the unexpanded value of a variable, or NULL if
99 * the variable is undefined.
101 * Var_Subst Substitute all variable expressions in a string.
103 * Var_Parse Parse a variable expression such as ${VAR:Mpattern}.
110 * Export some or even all variables to the environment
111 * of this process and its child processes.
113 * Var_Export Export the variable to the environment of this process
114 * and its child processes.
116 * Var_UnExport Don't export the variable anymore.
119 * Var_Stats Print out hashing statistics if in -dh mode.
121 * Var_Dump Print out all variables defined in the given scope.
123 * XXX: There's a lot of almost duplicate code in these functions that only
124 * differs in subtle details that are not mentioned in the manual page.
127 #include <sys/stat.h>
128 #include <sys/types.h>
136 #ifdef HAVE_INTTYPES_H
137 #include <inttypes.h>
138 #elif defined(HAVE_STDINT_H)
148 #include "metachar.h"
150 /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
151 MAKE_RCSID("$NetBSD: var.c,v 1.883 2021/03/14 20:23:29 rillig Exp $");
153 typedef enum VarFlags {
157 * The variable's value is currently being used by Var_Parse or
158 * Var_Subst. This marker is used to avoid endless recursion.
163 * The variable comes from the environment.
164 * These variables are not registered in any GNode, therefore they
165 * must be freed as soon as they are not used anymore.
167 VFL_FROM_ENV = 1 << 1,
170 * The variable is exported to the environment, to be used by child
173 VFL_EXPORTED = 1 << 2,
176 * At the point where this variable was exported, it contained an
177 * unresolved reference to another variable. Before any child
178 * process is started, it needs to be exported again, in the hope
179 * that the referenced variable can then be resolved.
181 VFL_REEXPORT = 1 << 3,
183 /* The variable came from the command line. */
184 VFL_FROM_CMD = 1 << 4,
187 * The variable value cannot be changed anymore, and the variable
188 * cannot be deleted. Any attempts to do so are silently ignored,
189 * they are logged with -dv though.
191 * See VAR_SET_READONLY.
193 VFL_READONLY = 1 << 5
197 * Variables are defined using one of the VAR=value assignments. Their
198 * value can be queried by expressions such as $V, ${VAR}, or with modifiers
199 * such as ${VAR:S,from,to,g:Q}.
201 * There are 3 kinds of variables: scope variables, environment variables,
202 * undefined variables.
204 * Scope variables are stored in a GNode.scope. The only way to undefine
205 * a scope variable is using the .undef directive. In particular, it must
206 * not be possible to undefine a variable during the evaluation of an
207 * expression, or Var.name might point nowhere.
209 * Environment variables are temporary. They are returned by VarFind, and
210 * after using them, they must be freed using VarFreeEnv.
212 * Undefined variables occur during evaluation of variable expressions such
213 * as ${UNDEF:Ufallback} in Var_Parse and ApplyModifiers.
217 * The name of the variable, once set, doesn't change anymore.
218 * For scope variables, it aliases the corresponding HashEntry name.
219 * For environment and undefined variables, it is allocated.
223 /* The unexpanded value of the variable. */
225 /* Miscellaneous status flags. */
230 * Exporting variables is expensive and may leak memory, so skip it if we
233 * To avoid this, it might be worth encapsulating the environment variables
234 * in a separate data structure called EnvVars.
236 typedef enum VarExportedMode {
242 typedef enum UnexportWhat {
243 /* Unexport the variables given by name. */
246 * Unexport all globals previously exported, but keep the environment
247 * inherited from the parent.
251 * Unexport all globals previously exported and clear the environment
252 * inherited from the parent.
257 /* Flags for pattern matching in the :S and :C modifiers */
258 typedef struct VarPatternFlags {
260 /* Replace as often as possible ('g') */
261 Boolean subGlobal: 1;
262 /* Replace only once ('1') */
264 /* Match at start of word ('^') */
265 Boolean anchorStart: 1;
266 /* Match at end of word ('$') */
267 Boolean anchorEnd: 1;
270 /* SepBuf builds a string from words interleaved with separators. */
271 typedef struct SepBuf {
274 /* Usually ' ', but see the ':ts' modifier. */
279 ENUM_FLAGS_RTTI_4(VarEvalFlags,
280 VARE_UNDEFERR, VARE_WANTRES, VARE_KEEP_DOLLAR,
284 * This lets us tell if we have replaced the original environ
285 * (which we cannot free).
287 char **savedEnv = NULL;
290 * Special return value for Var_Parse, indicating a parse error. It may be
291 * caused by an undefined variable, a syntax error in a modifier or
292 * something entirely different.
294 char var_Error[] = "";
297 * Special return value for Var_Parse, indicating an undefined variable in
298 * a case where VARE_UNDEFERR is not set. This undefined variable is
299 * typically a dynamic variable such as ${.TARGET}, whose expansion needs to
300 * be deferred until it is defined in an actual target.
302 * See VARE_KEEP_UNDEF.
304 static char varUndefined[] = "";
307 * Traditionally this make consumed $$ during := like any other expansion.
308 * Other make's do not, and this make follows straight since 2016-01-09.
310 * This knob allows controlling the behavior.
311 * FALSE to consume $$ during := assignment.
312 * TRUE to preserve $$ during := assignment.
314 #define MAKE_SAVE_DOLLARS ".MAKE.SAVE_DOLLARS"
315 static Boolean save_dollars = FALSE;
318 * A scope collects variable names and their values.
320 * The main scope is SCOPE_GLOBAL, which contains the variables that are set
321 * in the makefiles. SCOPE_INTERNAL acts as a fallback for SCOPE_GLOBAL and
322 * contains some internal make variables. These internal variables can thus
323 * be overridden, they can also be restored by undefining the overriding
326 * SCOPE_CMDLINE contains variables from the command line arguments. These
327 * override variables from SCOPE_GLOBAL.
329 * There is no scope for environment variables, these are generated on-the-fly
330 * whenever they are referenced. If there were such a scope, each change to
331 * environment variables would have to be reflected in that scope, which may
332 * be simpler or more complex than the current implementation.
334 * Each target has its own scope, containing the 7 target-local variables
335 * .TARGET, .ALLSRC, etc. No other variables are in these scopes.
338 GNode *SCOPE_CMDLINE;
340 GNode *SCOPE_INTERNAL;
342 ENUM_FLAGS_RTTI_6(VarFlags,
343 VFL_IN_USE, VFL_FROM_ENV,
344 VFL_EXPORTED, VFL_REEXPORT, VFL_FROM_CMD, VFL_READONLY);
346 static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE;
350 VarNew(FStr name, const char *value, VarFlags flags)
352 size_t value_len = strlen(value);
353 Var *var = bmake_malloc(sizeof *var);
355 Buf_InitSize(&var->val, value_len + 1);
356 Buf_AddBytes(&var->val, value, value_len);
362 CanonicalVarname(const char *name)
364 if (*name == '.' && ch_isupper(name[1])) {
367 if (strcmp(name, ".ALLSRC") == 0)
369 if (strcmp(name, ".ARCHIVE") == 0)
373 if (strcmp(name, ".IMPSRC") == 0)
377 if (strcmp(name, ".MEMBER") == 0)
381 if (strcmp(name, ".OODATE") == 0)
385 if (strcmp(name, ".PREFIX") == 0)
389 if (strcmp(name, ".SHELL") == 0) {
390 if (shellPath == NULL)
395 if (strcmp(name, ".TARGET") == 0)
401 /* GNU make has an additional alias $^ == ${.ALLSRC}. */
407 GNode_FindVar(GNode *scope, const char *varname, unsigned int hash)
409 return HashTable_FindValueHash(&scope->vars, varname, hash);
413 * Find the variable in the scope, and maybe in other scopes as well.
416 * name name to find, is not expanded any further
417 * scope scope in which to look first
418 * elsewhere TRUE to look in other scopes as well
421 * The found variable, or NULL if the variable does not exist.
422 * If the variable is an environment variable, it must be freed using
423 * VarFreeEnv after use.
426 VarFind(const char *name, GNode *scope, Boolean elsewhere)
429 unsigned int nameHash;
431 /* Replace '.TARGET' with '@', likewise for other local variables. */
432 name = CanonicalVarname(name);
433 nameHash = Hash_Hash(name);
435 var = GNode_FindVar(scope, name, nameHash);
439 if (var == NULL && scope != SCOPE_CMDLINE)
440 var = GNode_FindVar(SCOPE_CMDLINE, name, nameHash);
442 if (!opts.checkEnvFirst && var == NULL && scope != SCOPE_GLOBAL) {
443 var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
444 if (var == NULL && scope != SCOPE_INTERNAL) {
445 /* SCOPE_INTERNAL is subordinate to SCOPE_GLOBAL */
446 var = GNode_FindVar(SCOPE_INTERNAL, name, nameHash);
453 if ((env = getenv(name)) != NULL) {
454 char *varname = bmake_strdup(name);
455 return VarNew(FStr_InitOwn(varname), env, VFL_FROM_ENV);
458 if (opts.checkEnvFirst && scope != SCOPE_GLOBAL) {
459 var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash);
460 if (var == NULL && scope != SCOPE_INTERNAL)
461 var = GNode_FindVar(SCOPE_INTERNAL, name,
472 /* If the variable is an environment variable, free it, including its value. */
476 if (!(v->flags & VFL_FROM_ENV))
484 /* Add a new variable of the given name and value to the given scope. */
486 VarAdd(const char *name, const char *value, GNode *scope, VarSetFlags flags)
488 HashEntry *he = HashTable_CreateEntry(&scope->vars, name, NULL);
489 Var *v = VarNew(FStr_InitRefer(/* aliased to */ he->key), value,
490 flags & VAR_SET_READONLY ? VFL_READONLY : VFL_NONE);
491 HashEntry_Set(he, v);
492 DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, value);
497 * Remove a variable from a scope, freeing all related memory as well.
498 * The variable name is kept as-is, it is not expanded.
501 Var_Delete(GNode *scope, const char *varname)
503 HashEntry *he = HashTable_FindEntry(&scope->vars, varname);
507 DEBUG2(VAR, "%s:delete %s (not found)\n", scope->name, varname);
511 DEBUG2(VAR, "%s:delete %s\n", scope->name, varname);
513 if (v->flags & VFL_EXPORTED)
514 unsetenv(v->name.str);
515 if (strcmp(v->name.str, MAKE_EXPORTED) == 0)
516 var_exportedVars = VAR_EXPORTED_NONE;
517 assert(v->name.freeIt == NULL);
518 HashTable_DeleteEntry(&scope->vars, he);
524 * Remove a variable from a scope, freeing all related memory as well.
525 * The variable name is expanded once.
528 Var_DeleteExpand(GNode *scope, const char *name)
530 FStr varname = FStr_InitRefer(name);
532 if (strchr(varname.str, '$') != NULL) {
534 (void)Var_Subst(varname.str, SCOPE_GLOBAL, VARE_WANTRES,
536 /* TODO: handle errors */
537 varname = FStr_InitOwn(expanded);
540 Var_Delete(scope, varname.str);
545 * Undefine one or more variables from the global scope.
546 * The argument is expanded exactly once and then split into words.
549 Var_Undef(const char *arg)
556 if (arg[0] == '\0') {
557 Parse_Error(PARSE_FATAL,
558 "The .undef directive requires an argument");
562 vpr = Var_Subst(arg, SCOPE_GLOBAL, VARE_WANTRES, &expanded);
564 Parse_Error(PARSE_FATAL,
565 "Error in variable names to be undefined");
569 varnames = Str_Words(expanded, FALSE);
570 if (varnames.len == 1 && varnames.words[0][0] == '\0')
573 for (i = 0; i < varnames.len; i++) {
574 const char *varname = varnames.words[i];
575 Global_Delete(varname);
578 Words_Free(varnames);
583 MayExport(const char *name)
586 return FALSE; /* skip internals */
588 return FALSE; /* skip misnamed variables */
589 if (name[1] == '\0') {
592 * If it is one of the variables that should only appear in
593 * local scope, skip it, else we can get Var_Subst
610 const char *name = v->name.str;
611 char *val = v->val.data;
614 if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT))
615 return FALSE; /* nothing to do */
617 if (strchr(val, '$') == NULL) {
618 if (!(v->flags & VFL_EXPORTED))
619 setenv(name, val, 1);
623 if (v->flags & VFL_IN_USE) {
625 * We recursed while exporting in a child.
626 * This isn't going to end well, just skip it.
631 /* XXX: name is injected without escaping it */
632 expr = str_concat3("${", name, "}");
633 (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &val);
634 /* TODO: handle errors */
635 setenv(name, val, 1);
642 ExportVarPlain(Var *v)
644 if (strchr(v->val.data, '$') == NULL) {
645 setenv(v->name.str, v->val.data, 1);
646 v->flags |= VFL_EXPORTED;
647 v->flags &= ~(unsigned)VFL_REEXPORT;
652 * Flag the variable as something we need to re-export.
653 * No point actually exporting it now though,
654 * the child process can do it at the last minute.
655 * Avoid calling setenv more often than necessary since it can leak.
657 v->flags |= VFL_EXPORTED | VFL_REEXPORT;
662 ExportVarLiteral(Var *v)
664 if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT))
667 if (!(v->flags & VFL_EXPORTED))
668 setenv(v->name.str, v->val.data, 1);
674 * Mark a single variable to be exported later for subprocesses.
676 * Internal variables (those starting with '.') are not exported.
679 ExportVar(const char *name, VarExportMode mode)
683 if (!MayExport(name))
686 v = VarFind(name, SCOPE_GLOBAL, FALSE);
691 return ExportVarEnv(v);
692 else if (mode == VEM_PLAIN)
693 return ExportVarPlain(v);
695 return ExportVarLiteral(v);
699 * Actually export the variables that have been marked as needing to be
703 Var_ReexportVars(void)
708 * Several make implementations support this sort of mechanism for
709 * tracking recursion - but each uses a different name.
710 * We allow the makefiles to update MAKELEVEL and ensure
711 * children see a correctly incremented value.
714 snprintf(tmp, sizeof tmp, "%d", makelevel + 1);
715 setenv(MAKE_LEVEL_ENV, tmp, 1);
717 if (var_exportedVars == VAR_EXPORTED_NONE)
720 if (var_exportedVars == VAR_EXPORTED_ALL) {
723 /* Ouch! Exporting all variables at once is crazy. */
724 HashIter_Init(&hi, &SCOPE_GLOBAL->vars);
725 while (HashIter_Next(&hi) != NULL) {
726 Var *var = hi.entry->value;
727 ExportVar(var->name.str, VEM_ENV);
732 (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL, VARE_WANTRES,
734 /* TODO: handle errors */
735 if (xvarnames[0] != '\0') {
736 Words varnames = Str_Words(xvarnames, FALSE);
739 for (i = 0; i < varnames.len; i++)
740 ExportVar(varnames.words[i], VEM_ENV);
741 Words_Free(varnames);
747 ExportVars(const char *varnames, Boolean isExport, VarExportMode mode)
748 /* TODO: try to combine the parameters 'isExport' and 'mode'. */
750 Words words = Str_Words(varnames, FALSE);
753 if (words.len == 1 && words.words[0][0] == '\0')
756 for (i = 0; i < words.len; i++) {
757 const char *varname = words.words[i];
758 if (!ExportVar(varname, mode))
761 if (var_exportedVars == VAR_EXPORTED_NONE)
762 var_exportedVars = VAR_EXPORTED_SOME;
764 if (isExport && mode == VEM_PLAIN)
765 Global_Append(MAKE_EXPORTED, varname);
771 ExportVarsExpand(const char *uvarnames, Boolean isExport, VarExportMode mode)
775 (void)Var_Subst(uvarnames, SCOPE_GLOBAL, VARE_WANTRES, &xvarnames);
776 /* TODO: handle errors */
777 ExportVars(xvarnames, isExport, mode);
781 /* Export the named variables, or all variables. */
783 Var_Export(VarExportMode mode, const char *varnames)
785 if (mode == VEM_PLAIN && varnames[0] == '\0') {
786 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
790 ExportVarsExpand(varnames, TRUE, mode);
794 Var_ExportVars(const char *varnames)
796 ExportVarsExpand(varnames, FALSE, VEM_PLAIN);
800 extern char **environ;
808 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */
809 if (environ == savedEnv) {
810 /* we have been here before! */
811 newenv = bmake_realloc(environ, 2 * sizeof(char *));
813 if (savedEnv != NULL) {
817 newenv = bmake_malloc(2 * sizeof(char *));
820 /* Note: we cannot safely free() the original environ. */
821 environ = savedEnv = newenv;
824 if (cp != NULL && *cp != '\0')
825 setenv(MAKE_LEVEL_ENV, cp, 1);
829 GetVarnamesToUnexport(Boolean isEnv, const char *arg,
830 FStr *out_varnames, UnexportWhat *out_what)
833 FStr varnames = FStr_InitRefer("");
836 if (arg[0] != '\0') {
837 Parse_Error(PARSE_FATAL,
838 "The directive .unexport-env does not take "
840 /* continue anyway */
845 what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL;
846 if (what == UNEXPORT_NAMED)
847 varnames = FStr_InitRefer(arg);
850 if (what != UNEXPORT_NAMED) {
852 /* Using .MAKE.EXPORTED */
853 (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL,
854 VARE_WANTRES, &expanded);
855 /* TODO: handle errors */
856 varnames = FStr_InitOwn(expanded);
859 *out_varnames = varnames;
864 UnexportVar(const char *varname, UnexportWhat what)
866 Var *v = VarFind(varname, SCOPE_GLOBAL, FALSE);
868 DEBUG1(VAR, "Not unexporting \"%s\" (not found)\n", varname);
872 DEBUG1(VAR, "Unexporting \"%s\"\n", varname);
873 if (what != UNEXPORT_ENV &&
874 (v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT))
875 unsetenv(v->name.str);
876 v->flags &= ~(unsigned)(VFL_EXPORTED | VFL_REEXPORT);
878 if (what == UNEXPORT_NAMED) {
879 /* Remove the variable names from .MAKE.EXPORTED. */
880 /* XXX: v->name is injected without escaping it */
881 char *expr = str_concat3("${" MAKE_EXPORTED ":N",
884 (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &cp);
885 /* TODO: handle errors */
886 Global_Set(MAKE_EXPORTED, cp);
893 UnexportVars(FStr *varnames, UnexportWhat what)
898 if (what == UNEXPORT_ENV)
901 words = Str_Words(varnames->str, FALSE);
902 for (i = 0; i < words.len; i++) {
903 const char *varname = words.words[i];
904 UnexportVar(varname, what);
908 if (what != UNEXPORT_NAMED)
909 Global_Delete(MAKE_EXPORTED);
913 * This is called when .unexport[-env] is seen.
915 * str must have the form "unexport[-env] varname...".
918 Var_UnExport(Boolean isEnv, const char *arg)
923 GetVarnamesToUnexport(isEnv, arg, &varnames, &what);
924 UnexportVars(&varnames, what);
925 FStr_Done(&varnames);
929 * When there is a variable of the same name in the command line scope, the
930 * global variable would not be visible anywhere. Therefore there is no
931 * point in setting it at all.
933 * See 'scope == SCOPE_CMDLINE' in Var_SetWithFlags.
936 ExistsInCmdline(const char *name, const char *val)
940 v = VarFind(name, SCOPE_CMDLINE, FALSE);
944 if (v->flags & VFL_FROM_CMD) {
945 DEBUG3(VAR, "%s:%s = %s ignored!\n",
946 SCOPE_GLOBAL->name, name, val);
954 /* Set the variable to the value; the name is not expanded. */
956 Var_SetWithFlags(GNode *scope, const char *name, const char *val,
962 if (name[0] == '\0') {
963 DEBUG0(VAR, "SetVar: variable name is empty - ignored\n");
967 if (scope == SCOPE_GLOBAL && ExistsInCmdline(name, val))
971 * Only look for a variable in the given scope since anything set
972 * here will override anything in a lower scope, so there's not much
973 * point in searching them all.
975 v = VarFind(name, scope, FALSE);
977 if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) {
979 * This var would normally prevent the same name being
980 * added to SCOPE_GLOBAL, so delete it from there if
981 * needed. Otherwise -V name may show the wrong value.
983 * See ExistsInCmdline.
985 Var_Delete(SCOPE_GLOBAL, name);
987 v = VarAdd(name, val, scope, flags);
989 if ((v->flags & VFL_READONLY) && !(flags & VAR_SET_READONLY)) {
990 DEBUG3(VAR, "%s:%s = %s ignored (read-only)\n",
991 scope->name, name, val);
995 Buf_AddStr(&v->val, val);
997 DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, val);
998 if (v->flags & VFL_EXPORTED)
999 ExportVar(name, VEM_PLAIN);
1003 * Any variables given on the command line are automatically exported
1004 * to the environment (as per POSIX standard), except for internals.
1006 if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT) &&
1008 v->flags |= VFL_FROM_CMD;
1011 * If requested, don't export these in the environment
1012 * individually. We still put them in MAKEOVERRIDES so
1013 * that the command-line settings continue to override
1014 * Makefile settings.
1016 if (!opts.varNoExportEnv)
1017 setenv(name, val, 1);
1018 /* XXX: What about .MAKE.EXPORTED? */
1019 /* XXX: Why not just mark the variable for needing export,
1020 * as in ExportVarPlain? */
1022 Global_Append(MAKEOVERRIDES, name);
1025 if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0)
1026 save_dollars = ParseBoolean(val, save_dollars);
1032 /* See Var_Set for documentation. */
1034 Var_SetExpandWithFlags(GNode *scope, const char *name, const char *val,
1037 const char *unexpanded_name = name;
1038 FStr varname = FStr_InitRefer(name);
1040 assert(val != NULL);
1042 if (strchr(varname.str, '$') != NULL) {
1044 (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded);
1045 /* TODO: handle errors */
1046 varname = FStr_InitOwn(expanded);
1049 if (varname.str[0] == '\0') {
1050 DEBUG2(VAR, "Var_Set(\"%s\", \"%s\", ...) "
1051 "name expands to empty string - ignored\n",
1052 unexpanded_name, val);
1054 Var_SetWithFlags(scope, varname.str, val, flags);
1056 FStr_Done(&varname);
1060 Var_Set(GNode *scope, const char *name, const char *val)
1062 Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
1066 * Set the variable name to the value val in the given scope.
1068 * If the variable doesn't yet exist, it is created.
1069 * Otherwise the new value overwrites and replaces the old value.
1072 * name name of the variable to set, is expanded once
1073 * val value to give to the variable
1074 * scope scope in which to set it
1077 Var_SetExpand(GNode *scope, const char *name, const char *val)
1079 Var_SetExpandWithFlags(scope, name, val, VAR_SET_NONE);
1083 Global_Set(const char *name, const char *value)
1085 Var_Set(SCOPE_GLOBAL, name, value);
1089 Global_SetExpand(const char *name, const char *value)
1091 Var_SetExpand(SCOPE_GLOBAL, name, value);
1095 Global_Delete(const char *name)
1097 Var_Delete(SCOPE_GLOBAL, name);
1101 * Append the value to the named variable.
1103 * If the variable doesn't exist, it is created. Otherwise a single space
1104 * and the given value are appended.
1107 Var_Append(GNode *scope, const char *name, const char *val)
1111 v = VarFind(name, scope, scope == SCOPE_GLOBAL);
1114 Var_SetWithFlags(scope, name, val, VAR_SET_NONE);
1115 } else if (v->flags & VFL_READONLY) {
1116 DEBUG1(VAR, "Ignoring append to %s since it is read-only\n",
1118 } else if (scope == SCOPE_CMDLINE || !(v->flags & VFL_FROM_CMD)) {
1119 Buf_AddByte(&v->val, ' ');
1120 Buf_AddStr(&v->val, val);
1122 DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, v->val.data);
1124 if (v->flags & VFL_FROM_ENV) {
1126 * If the original variable came from the environment,
1127 * we have to install it in the global scope (we
1128 * could place it in the environment, but then we
1129 * should provide a way to export other variables...)
1131 v->flags &= ~(unsigned)VFL_FROM_ENV;
1133 * This is the only place where a variable is
1134 * created whose v->name is not the same as
1137 HashTable_Set(&scope->vars, name, v);
1143 * The variable of the given name has the given value appended to it in the
1146 * If the variable doesn't exist, it is created. Otherwise the strings are
1147 * concatenated, with a space in between.
1150 * name name of the variable to modify, is expanded once
1151 * val string to append to it
1152 * scope scope in which this should occur
1155 * Only if the variable is being sought in the global scope is the
1156 * environment searched.
1157 * XXX: Knows its calling circumstances in that if called with scope
1158 * an actual target, it will only search that scope since only
1159 * a local variable could be being appended to. This is actually
1160 * a big win and must be tolerated.
1163 Var_AppendExpand(GNode *scope, const char *name, const char *val)
1165 FStr xname = FStr_InitRefer(name);
1167 assert(val != NULL);
1169 if (strchr(name, '$') != NULL) {
1171 (void)Var_Subst(name, scope, VARE_WANTRES, &expanded);
1172 /* TODO: handle errors */
1173 xname = FStr_InitOwn(expanded);
1174 if (expanded[0] == '\0') {
1175 /* TODO: update function name in the debug message */
1176 DEBUG2(VAR, "Var_Append(\"%s\", \"%s\", ...) "
1177 "name expands to empty string - ignored\n",
1184 Var_Append(scope, xname.str, val);
1190 Global_Append(const char *name, const char *value)
1192 Var_Append(SCOPE_GLOBAL, name, value);
1196 Var_Exists(GNode *scope, const char *name)
1198 Var *v = VarFind(name, scope, TRUE);
1207 * See if the given variable exists, in the given scope or in other
1211 * name Variable to find, is expanded once
1212 * scope Scope in which to start search
1215 Var_ExistsExpand(GNode *scope, const char *name)
1217 FStr varname = FStr_InitRefer(name);
1220 if (strchr(varname.str, '$') != NULL) {
1222 (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded);
1223 /* TODO: handle errors */
1224 varname = FStr_InitOwn(expanded);
1227 exists = Var_Exists(scope, varname.str);
1228 FStr_Done(&varname);
1233 * Return the unexpanded value of the given variable in the given scope,
1234 * or the usual scopes.
1237 * name name to find, is not expanded any further
1238 * scope scope in which to search for it
1241 * The value if the variable exists, NULL if it doesn't.
1242 * The value is valid until the next modification to any variable.
1245 Var_Value(GNode *scope, const char *name)
1247 Var *v = VarFind(name, scope, TRUE);
1251 return FStr_InitRefer(NULL);
1253 if (!(v->flags & VFL_FROM_ENV))
1254 return FStr_InitRefer(v->val.data);
1256 /* Since environment variables are short-lived, free it now. */
1257 FStr_Done(&v->name);
1258 value = Buf_DoneData(&v->val);
1260 return FStr_InitOwn(value);
1264 * Return the unexpanded variable value from this node, without trying to look
1265 * up the variable in any other scope.
1268 GNode_ValueDirect(GNode *gn, const char *name)
1270 Var *v = VarFind(name, gn, FALSE);
1271 return v != NULL ? v->val.data : NULL;
1276 SepBuf_Init(SepBuf *buf, char sep)
1278 Buf_InitSize(&buf->buf, 32);
1279 buf->needSep = FALSE;
1284 SepBuf_Sep(SepBuf *buf)
1286 buf->needSep = TRUE;
1290 SepBuf_AddBytes(SepBuf *buf, const char *mem, size_t mem_size)
1294 if (buf->needSep && buf->sep != '\0') {
1295 Buf_AddByte(&buf->buf, buf->sep);
1296 buf->needSep = FALSE;
1298 Buf_AddBytes(&buf->buf, mem, mem_size);
1302 SepBuf_AddBytesBetween(SepBuf *buf, const char *start, const char *end)
1304 SepBuf_AddBytes(buf, start, (size_t)(end - start));
1308 SepBuf_AddStr(SepBuf *buf, const char *str)
1310 SepBuf_AddBytes(buf, str, strlen(str));
1314 SepBuf_DoneData(SepBuf *buf)
1316 return Buf_DoneData(&buf->buf);
1321 * This callback for ModifyWords gets a single word from a variable expression
1322 * and typically adds a modification of this word to the buffer. It may also
1323 * do nothing or add several words.
1325 * For example, when evaluating the modifier ':M*b' in ${:Ua b c:M*b}, the
1326 * callback is called 3 times, once for "a", "b" and "c".
1328 typedef void (*ModifyWordProc)(const char *word, SepBuf *buf, void *data);
1332 * Callback for ModifyWords to implement the :H modifier.
1333 * Add the dirname of the given word to the buffer.
1337 ModifyWord_Head(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1339 const char *slash = strrchr(word, '/');
1341 SepBuf_AddBytesBetween(buf, word, slash);
1343 SepBuf_AddStr(buf, ".");
1347 * Callback for ModifyWords to implement the :T modifier.
1348 * Add the basename of the given word to the buffer.
1352 ModifyWord_Tail(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1354 SepBuf_AddStr(buf, str_basename(word));
1358 * Callback for ModifyWords to implement the :E modifier.
1359 * Add the filename suffix of the given word to the buffer, if it exists.
1363 ModifyWord_Suffix(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1365 const char *lastDot = strrchr(word, '.');
1366 if (lastDot != NULL)
1367 SepBuf_AddStr(buf, lastDot + 1);
1371 * Callback for ModifyWords to implement the :R modifier.
1372 * Add the filename without extension of the given word to the buffer.
1376 ModifyWord_Root(const char *word, SepBuf *buf, void *dummy MAKE_ATTR_UNUSED)
1378 const char *lastDot = strrchr(word, '.');
1379 size_t len = lastDot != NULL ? (size_t)(lastDot - word) : strlen(word);
1380 SepBuf_AddBytes(buf, word, len);
1384 * Callback for ModifyWords to implement the :M modifier.
1385 * Place the word in the buffer if it matches the given pattern.
1388 ModifyWord_Match(const char *word, SepBuf *buf, void *data)
1390 const char *pattern = data;
1391 DEBUG2(VAR, "VarMatch [%s] [%s]\n", word, pattern);
1392 if (Str_Match(word, pattern))
1393 SepBuf_AddStr(buf, word);
1397 * Callback for ModifyWords to implement the :N modifier.
1398 * Place the word in the buffer if it doesn't match the given pattern.
1401 ModifyWord_NoMatch(const char *word, SepBuf *buf, void *data)
1403 const char *pattern = data;
1404 if (!Str_Match(word, pattern))
1405 SepBuf_AddStr(buf, word);
1411 * Check word against pattern for a match (% is a wildcard).
1414 * word Word to examine
1415 * pattern Pattern to examine against
1418 * Returns the start of the match, or NULL.
1419 * out_match_len returns the length of the match, if any.
1420 * out_hasPercent returns whether the pattern contains a percent.
1423 SysVMatch(const char *word, const char *pattern,
1424 size_t *out_match_len, Boolean *out_hasPercent)
1426 const char *p = pattern;
1427 const char *w = word;
1428 const char *percent;
1433 *out_hasPercent = FALSE;
1434 percent = strchr(p, '%');
1435 if (percent != NULL) { /* ${VAR:...%...=...} */
1436 *out_hasPercent = TRUE;
1438 return NULL; /* empty word does not match pattern */
1440 /* check that the prefix matches */
1441 for (; p != percent && *w != '\0' && *w == *p; w++, p++)
1444 return NULL; /* No match */
1446 p++; /* Skip the percent */
1448 /* No more pattern, return the rest of the string */
1449 *out_match_len = strlen(w);
1454 /* Test whether the tail matches */
1460 w_tail = w + w_len - p_len;
1461 if (memcmp(p, w_tail, p_len) != 0)
1464 *out_match_len = (size_t)(w_tail - w);
1468 struct ModifyWord_SYSVSubstArgs {
1474 /* Callback for ModifyWords to implement the :%.from=%.to modifier. */
1476 ModifyWord_SYSVSubst(const char *word, SepBuf *buf, void *data)
1478 const struct ModifyWord_SYSVSubstArgs *args = data;
1481 const char *percent;
1485 const char *match = SysVMatch(word, args->lhs, &match_len, &lhsPercent);
1486 if (match == NULL) {
1487 SepBuf_AddStr(buf, word);
1492 * Append rhs to the buffer, substituting the first '%' with the
1493 * match, but only if the lhs had a '%' as well.
1496 (void)Var_Subst(args->rhs, args->scope, VARE_WANTRES, &rhs_expanded);
1497 /* TODO: handle errors */
1500 percent = strchr(rhs, '%');
1502 if (percent != NULL && lhsPercent) {
1503 /* Copy the prefix of the replacement pattern */
1504 SepBuf_AddBytesBetween(buf, rhs, percent);
1507 if (percent != NULL || !lhsPercent)
1508 SepBuf_AddBytes(buf, match, match_len);
1510 /* Append the suffix of the replacement pattern */
1511 SepBuf_AddStr(buf, rhs);
1518 struct ModifyWord_SubstArgs {
1523 VarPatternFlags pflags;
1528 * Callback for ModifyWords to implement the :S,from,to, modifier.
1529 * Perform a string substitution on the given word.
1532 ModifyWord_Subst(const char *word, SepBuf *buf, void *data)
1534 size_t wordLen = strlen(word);
1535 struct ModifyWord_SubstArgs *args = data;
1538 if (args->pflags.subOnce && args->matched)
1541 if (args->pflags.anchorStart) {
1542 if (wordLen < args->lhsLen ||
1543 memcmp(word, args->lhs, args->lhsLen) != 0)
1546 if (args->pflags.anchorEnd && wordLen != args->lhsLen)
1549 /* :S,^prefix,replacement, or :S,^whole$,replacement, */
1550 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1551 SepBuf_AddBytesBetween(buf,
1552 word + args->lhsLen, word + wordLen);
1553 args->matched = TRUE;
1557 if (args->pflags.anchorEnd) {
1560 if (wordLen < args->lhsLen)
1563 start = word + (wordLen - args->lhsLen);
1564 if (memcmp(start, args->lhs, args->lhsLen) != 0)
1567 /* :S,suffix$,replacement, */
1568 SepBuf_AddBytesBetween(buf, word, start);
1569 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1570 args->matched = TRUE;
1574 if (args->lhs[0] == '\0')
1577 /* unanchored case, may match more than once */
1578 while ((match = strstr(word, args->lhs)) != NULL) {
1579 SepBuf_AddBytesBetween(buf, word, match);
1580 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1581 args->matched = TRUE;
1582 wordLen -= (size_t)(match - word) + args->lhsLen;
1583 word += (size_t)(match - word) + args->lhsLen;
1584 if (wordLen == 0 || !args->pflags.subGlobal)
1588 SepBuf_AddBytes(buf, word, wordLen);
1592 /* Print the error caused by a regcomp or regexec call. */
1594 VarREError(int reerr, const regex_t *pat, const char *str)
1596 size_t errlen = regerror(reerr, pat, NULL, 0);
1597 char *errbuf = bmake_malloc(errlen);
1598 regerror(reerr, pat, errbuf, errlen);
1599 Error("%s: %s", str, errbuf);
1603 struct ModifyWord_SubstRegexArgs {
1607 VarPatternFlags pflags;
1612 * Callback for ModifyWords to implement the :C/from/to/ modifier.
1613 * Perform a regex substitution on the given word.
1616 ModifyWord_SubstRegex(const char *word, SepBuf *buf, void *data)
1618 struct ModifyWord_SubstRegexArgs *args = data;
1620 const char *wp = word;
1625 if (args->pflags.subOnce && args->matched)
1629 xrv = regexec(&args->re, wp, args->nsub, m, flags);
1633 args->matched = TRUE;
1634 SepBuf_AddBytes(buf, wp, (size_t)m[0].rm_so);
1637 * Replacement of regular expressions is not specified by
1638 * POSIX, therefore implement it here.
1641 for (rp = args->replace; *rp != '\0'; rp++) {
1642 if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) {
1643 SepBuf_AddBytes(buf, rp + 1, 1);
1649 SepBuf_AddBytesBetween(buf,
1650 wp + m[0].rm_so, wp + m[0].rm_eo);
1654 if (*rp != '\\' || !ch_isdigit(rp[1])) {
1655 SepBuf_AddBytes(buf, rp, 1);
1659 { /* \0 to \9 backreference */
1660 size_t n = (size_t)(rp[1] - '0');
1663 if (n >= args->nsub) {
1664 Error("No subexpression \\%u",
1666 } else if (m[n].rm_so == -1) {
1668 "No match for subexpression \\%u",
1671 SepBuf_AddBytesBetween(buf,
1672 wp + m[n].rm_so, wp + m[n].rm_eo);
1678 if (args->pflags.subGlobal) {
1679 flags |= REG_NOTBOL;
1680 if (m[0].rm_so == 0 && m[0].rm_eo == 0) {
1681 SepBuf_AddBytes(buf, wp, 1);
1688 SepBuf_AddStr(buf, wp);
1691 VarREError(xrv, &args->re, "Unexpected regex error");
1695 SepBuf_AddStr(buf, wp);
1702 struct ModifyWord_LoopArgs {
1704 char *tvar; /* name of temporary variable */
1705 char *str; /* string to expand */
1706 VarEvalFlags eflags;
1709 /* Callback for ModifyWords to implement the :@var@...@ modifier of ODE make. */
1711 ModifyWord_Loop(const char *word, SepBuf *buf, void *data)
1713 const struct ModifyWord_LoopArgs *args;
1716 if (word[0] == '\0')
1720 /* XXX: The variable name should not be expanded here. */
1721 Var_SetExpandWithFlags(args->scope, args->tvar, word,
1723 (void)Var_Subst(args->str, args->scope, args->eflags, &s);
1724 /* TODO: handle errors */
1726 DEBUG4(VAR, "ModifyWord_Loop: "
1727 "in \"%s\", replace \"%s\" with \"%s\" to \"%s\"\n",
1728 word, args->tvar, args->str, s);
1730 if (s[0] == '\n' || Buf_EndsWith(&buf->buf, '\n'))
1731 buf->needSep = FALSE;
1732 SepBuf_AddStr(buf, s);
1738 * The :[first..last] modifier selects words from the expression.
1739 * It can also reverse the words.
1742 VarSelectWords(const char *str, int first, int last,
1743 char sep, Boolean oneBigWord)
1746 int len, start, end, step;
1750 SepBuf_Init(&buf, sep);
1753 /* fake what Str_Words() would do if there were only one word */
1755 words.words = bmake_malloc(
1756 (words.len + 1) * sizeof(words.words[0]));
1757 words.freeIt = bmake_strdup(str);
1758 words.words[0] = words.freeIt;
1759 words.words[1] = NULL;
1761 words = Str_Words(str, FALSE);
1765 * Now sanitize the given range. If first or last are negative,
1766 * convert them to the positive equivalents (-1 gets converted to len,
1767 * -2 gets converted to (len - 1), etc.).
1769 len = (int)words.len;
1775 /* We avoid scanning more of the list than we need to. */
1777 start = (first > len ? len : first) - 1;
1778 end = last < 1 ? 0 : last - 1;
1781 start = first < 1 ? 0 : first - 1;
1782 end = last > len ? len : last;
1786 for (i = start; (step < 0) == (i >= end); i += step) {
1787 SepBuf_AddStr(&buf, words.words[i]);
1793 return SepBuf_DoneData(&buf);
1798 * Callback for ModifyWords to implement the :tA modifier.
1799 * Replace each word with the result of realpath() if successful.
1803 ModifyWord_Realpath(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
1806 char rbuf[MAXPATHLEN];
1808 const char *rp = cached_realpath(word, rbuf);
1809 if (rp != NULL && *rp == '/' && stat(rp, &st) == 0)
1812 SepBuf_AddStr(buf, word);
1817 Words_JoinFree(Words words)
1824 for (i = 0; i < words.len; i++) {
1826 /* XXX: Use st->sep instead of ' ', for consistency. */
1827 Buf_AddByte(&buf, ' ');
1829 Buf_AddStr(&buf, words.words[i]);
1834 return Buf_DoneData(&buf);
1837 /* Remove adjacent duplicate words. */
1839 VarUniq(const char *str)
1841 Words words = Str_Words(str, FALSE);
1843 if (words.len > 1) {
1845 for (j = 0, i = 1; i < words.len; i++)
1846 if (strcmp(words.words[i], words.words[j]) != 0 &&
1848 words.words[j] = words.words[i];
1852 return Words_JoinFree(words);
1857 * Quote shell meta-characters and space characters in the string.
1858 * If quoteDollar is set, also quote and double any '$' characters.
1861 VarQuote(const char *str, Boolean quoteDollar)
1866 for (; *str != '\0'; str++) {
1868 const char *newline = Shell_GetNewline();
1869 if (newline == NULL)
1871 Buf_AddStr(&buf, newline);
1874 if (ch_isspace(*str) || is_shell_metachar((unsigned char)*str))
1875 Buf_AddByte(&buf, '\\');
1876 Buf_AddByte(&buf, *str);
1877 if (quoteDollar && *str == '$')
1878 Buf_AddStr(&buf, "\\$");
1881 return Buf_DoneData(&buf);
1885 * Compute the 32-bit hash of the given string, using the MurmurHash3
1886 * algorithm. Output is encoded as 8 hex digits, in Little Endian order.
1889 VarHash(const char *str)
1891 static const char hexdigits[16] = "0123456789abcdef";
1892 const unsigned char *ustr = (const unsigned char *)str;
1894 uint32_t h = 0x971e137bU;
1895 uint32_t c1 = 0x95543787U;
1896 uint32_t c2 = 0x2ad7eb25U;
1897 size_t len2 = strlen(str);
1903 for (len = len2; len != 0;) {
1907 k = ((uint32_t)ustr[3] << 24) |
1908 ((uint32_t)ustr[2] << 16) |
1909 ((uint32_t)ustr[1] << 8) |
1915 k |= (uint32_t)ustr[2] << 16;
1918 k |= (uint32_t)ustr[1] << 8;
1921 k |= (uint32_t)ustr[0];
1924 c1 = c1 * 5 + 0x7b7d159cU;
1925 c2 = c2 * 5 + 0x6bce6396U;
1927 k = (k << 11) ^ (k >> 21);
1929 h = (h << 13) ^ (h >> 19);
1930 h = h * 5 + 0x52dce729U;
1933 h ^= (uint32_t)len2;
1939 buf = bmake_malloc(9);
1940 for (i = 0; i < 8; i++) {
1941 buf[i] = hexdigits[h & 0x0f];
1949 VarStrftime(const char *fmt, Boolean zulu, time_t tim)
1957 strftime(buf, sizeof buf, fmt, zulu ? gmtime(&tim) : localtime(&tim));
1959 buf[sizeof buf - 1] = '\0';
1960 return bmake_strdup(buf);
1964 * The ApplyModifier functions take an expression that is being evaluated.
1965 * Their task is to apply a single modifier to the expression. This involves
1966 * parsing the modifier, evaluating it and finally updating the value of the
1969 * Parsing the modifier
1971 * If parsing succeeds, the parsing position *pp is updated to point to the
1972 * first character following the modifier, which typically is either ':' or
1973 * st->endc. The modifier doesn't have to check for this delimiter character,
1974 * this is done by ApplyModifiers.
1976 * XXX: As of 2020-11-15, some modifiers such as :S, :C, :P, :L do not
1977 * need to be followed by a ':' or endc; this was an unintended mistake.
1979 * If parsing fails because of a missing delimiter (as in the :S, :C or :@
1980 * modifiers), return AMR_CLEANUP.
1982 * If parsing fails because the modifier is unknown, return AMR_UNKNOWN to
1983 * try the SysV modifier ${VAR:from=to} as fallback. This should only be
1984 * done as long as there have been no side effects from evaluating nested
1985 * variables, to avoid evaluating them more than once. In this case, the
1986 * parsing position may or may not be updated. (XXX: Why not? The original
1987 * parsing position is well-known in ApplyModifiers.)
1989 * If parsing fails and the SysV modifier ${VAR:from=to} should not be used
1990 * as a fallback, either issue an error message using Error or Parse_Error
1991 * and then return AMR_CLEANUP, or return AMR_BAD for the default error
1992 * message. Both of these return values will stop processing the variable
1993 * expression. (XXX: As of 2020-08-23, evaluation of the whole string
1994 * continues nevertheless after skipping a few bytes, which essentially is
1995 * undefined behavior. Not in the sense of C, but still the resulting string
1998 * Evaluating the modifier
2000 * After parsing, the modifier is evaluated. The side effects from evaluating
2001 * nested variable expressions in the modifier text often already happen
2002 * during parsing though. For most modifiers this doesn't matter since their
2003 * only noticeable effect is that the update the value of the expression.
2004 * Some modifiers such as ':sh' or '::=' have noticeable side effects though.
2006 * Evaluating the modifier usually takes the current value of the variable
2007 * expression from st->expr->value, or the variable name from st->var->name
2008 * and stores the result back in expr->value via Expr_SetValueOwn or
2009 * Expr_SetValueRefer.
2011 * If evaluating fails (as of 2020-08-23), an error message is printed using
2012 * Error. This function has no side-effects, it really just prints the error
2013 * message. Processing the expression continues as if everything were ok.
2014 * XXX: This should be fixed by adding proper error handling to Var_Subst,
2015 * Var_Parse, ApplyModifiers and ModifyWords.
2019 * Some modifiers such as :D and :U turn undefined expressions into defined
2020 * expressions (see Expr_Define).
2022 * Some modifiers need to free some memory.
2025 typedef enum ExprDefined {
2026 /* The variable expression is based on a regular, defined variable. */
2028 /* The variable expression is based on an undefined variable. */
2031 * The variable expression started as an undefined expression, but one
2032 * of the modifiers (such as ':D' or ':U') has turned the expression
2033 * from undefined to defined.
2038 static const char *const ExprDefined_Name[] = {
2044 /* A variable expression such as $@ or ${VAR:Mpattern:Q}. */
2045 typedef struct Expr {
2048 VarEvalFlags const eflags;
2050 ExprDefined defined;
2054 * Data that is used when applying a chain of modifiers to an expression.
2055 * For indirect modifiers, the effects of this data stops after the indirect
2056 * modifiers have been applies.
2058 * It may or may not be intended that 'status' has scope Expr while 'sep' and
2059 * 'oneBigWord' have smaller scope, terminating at the end of a chain of
2060 * indirect modifiers.
2062 * See varmod-indirect.mk.
2064 typedef struct ApplyModifiersState {
2066 /* '\0' or '{' or '(' */
2068 /* '\0' or '}' or ')' */
2070 /* Word separator in expansions (see the :ts modifier). */
2073 * TRUE if some modifiers that otherwise split the variable value
2074 * into words, like :S and :C, treat the variable value as a single
2075 * big word, possibly containing spaces.
2078 } ApplyModifiersState;
2081 Expr_Define(Expr *expr)
2083 if (expr->defined == DEF_UNDEF)
2084 expr->defined = DEF_DEFINED;
2088 Expr_SetValueOwn(Expr *expr, char *value)
2090 FStr_Done(&expr->value);
2091 expr->value = FStr_InitOwn(value);
2095 Expr_SetValueRefer(Expr *expr, const char *value)
2097 FStr_Done(&expr->value);
2098 expr->value = FStr_InitRefer(value);
2101 typedef enum ApplyModifierResult {
2102 /* Continue parsing */
2104 /* Not a match, try other modifiers as well. */
2106 /* Error out with "Bad modifier" message. */
2108 /* Error out without the standard error message. */
2110 } ApplyModifierResult;
2113 * Allow backslashes to escape the delimiter, $, and \, but don't touch other
2117 IsEscapedModifierPart(const char *p, char delim,
2118 struct ModifyWord_SubstArgs *subst)
2122 if (p[1] == delim || p[1] == '\\' || p[1] == '$')
2124 return p[1] == '&' && subst != NULL;
2127 /* See ParseModifierPart */
2128 static VarParseResult
2129 ParseModifierPartSubst(
2132 VarEvalFlags eflags,
2133 ApplyModifiersState *st,
2135 /* Optionally stores the length of the returned string, just to save
2136 * another strlen call. */
2138 /* For the first part of the :S modifier, sets the VARP_ANCHOR_END flag
2139 * if the last character of the pattern is a $. */
2140 VarPatternFlags *out_pflags,
2141 /* For the second part of the :S modifier, allow ampersands to be
2142 * escaped and replace unescaped ampersands with subst->lhs. */
2143 struct ModifyWord_SubstArgs *subst
2152 * Skim through until the matching delimiter is found; pick up
2153 * variable expressions on the way.
2156 while (*p != '\0' && *p != delim) {
2157 const char *varstart;
2159 if (IsEscapedModifierPart(p, delim, subst)) {
2160 Buf_AddByte(&buf, p[1]);
2165 if (*p != '$') { /* Unescaped, simple text */
2166 if (subst != NULL && *p == '&')
2167 Buf_AddBytes(&buf, subst->lhs, subst->lhsLen);
2169 Buf_AddByte(&buf, *p);
2174 if (p[1] == delim) { /* Unescaped $ at end of pattern */
2175 if (out_pflags != NULL)
2176 out_pflags->anchorEnd = TRUE;
2178 Buf_AddByte(&buf, *p);
2183 if (eflags & VARE_WANTRES) { /* Nested variable, evaluated */
2184 const char *nested_p = p;
2186 VarEvalFlags nested_eflags =
2187 eflags & ~(unsigned)VARE_KEEP_DOLLAR;
2189 (void)Var_Parse(&nested_p, st->expr->scope,
2190 nested_eflags, &nested_val);
2191 /* TODO: handle errors */
2192 Buf_AddStr(&buf, nested_val.str);
2193 FStr_Done(&nested_val);
2199 * XXX: This whole block is very similar to Var_Parse without
2200 * VARE_WANTRES. There may be subtle edge cases though that
2201 * are not yet covered in the unit tests and that are parsed
2202 * differently, depending on whether they are evaluated or
2205 * This subtle difference is not documented in the manual
2206 * page, neither is the difference between parsing :D and
2207 * :M documented. No code should ever depend on these
2208 * details, but who knows.
2211 varstart = p; /* Nested variable, only parsed */
2212 if (p[1] == '(' || p[1] == '{') {
2214 * Find the end of this variable reference
2215 * and suck it in without further ado.
2216 * It will be interpreted later.
2219 int endc = startc == '(' ? ')' : '}';
2222 for (p += 2; *p != '\0' && depth > 0; p++) {
2223 if (p[-1] != '\\') {
2230 Buf_AddBytesBetween(&buf, varstart, p);
2232 Buf_AddByte(&buf, *varstart);
2239 Error("Unfinished modifier for \"%s\" ('%c' missing)",
2240 st->expr->var->name.str, delim);
2246 if (out_length != NULL)
2247 *out_length = buf.len;
2249 *out_part = Buf_DoneData(&buf);
2250 DEBUG1(VAR, "Modifier part: \"%s\"\n", *out_part);
2255 * Parse a part of a modifier such as the "from" and "to" in :S/from/to/ or
2256 * the "var" or "replacement ${var}" in :@var@replacement ${var}@, up to and
2257 * including the next unescaped delimiter. The delimiter, as well as the
2258 * backslash or the dollar, can be escaped with a backslash.
2260 * Return the parsed (and possibly expanded) string, or NULL if no delimiter
2261 * was found. On successful return, the parsing position pp points right
2262 * after the delimiter. The delimiter is not included in the returned
2265 static VarParseResult
2267 /* The parsing position, updated upon return */
2269 /* Parsing stops at this delimiter */
2271 /* Flags for evaluating nested variables; if VARE_WANTRES is not set,
2272 * the text is only parsed. */
2273 VarEvalFlags eflags,
2274 ApplyModifiersState *st,
2278 return ParseModifierPartSubst(pp, delim, eflags, st, out_part,
2283 IsDelimiter(char ch, const ApplyModifiersState *st)
2285 return ch == ':' || ch == st->endc;
2288 /* Test whether mod starts with modname, followed by a delimiter. */
2290 ModMatch(const char *mod, const char *modname, const ApplyModifiersState *st)
2292 size_t n = strlen(modname);
2293 return strncmp(mod, modname, n) == 0 && IsDelimiter(mod[n], st);
2296 /* Test whether mod starts with modname, followed by a delimiter or '='. */
2298 ModMatchEq(const char *mod, const char *modname, const ApplyModifiersState *st)
2300 size_t n = strlen(modname);
2301 return strncmp(mod, modname, n) == 0 &&
2302 (IsDelimiter(mod[n], st) || mod[n] == '=');
2306 TryParseIntBase0(const char **pp, int *out_num)
2312 n = strtol(*pp, &end, 0);
2316 if ((n == LONG_MIN || n == LONG_MAX) && errno == ERANGE)
2318 if (n < INT_MIN || n > INT_MAX)
2327 TryParseSize(const char **pp, size_t *out_num)
2332 if (!ch_isdigit(**pp))
2336 n = strtoul(*pp, &end, 10);
2337 if (n == ULONG_MAX && errno == ERANGE)
2343 *out_num = (size_t)n;
2348 TryParseChar(const char **pp, int base, char *out_ch)
2353 if (!ch_isalnum(**pp))
2357 n = strtoul(*pp, &end, base);
2358 if (n == ULONG_MAX && errno == ERANGE)
2369 * Modify each word of the expression using the given function and place the
2370 * result back in the expression.
2373 ModifyWords(ApplyModifiersState *st,
2374 ModifyWordProc modifyWord, void *modifyWord_args,
2377 Expr *expr = st->expr;
2378 const char *val = expr->value.str;
2384 SepBuf_Init(&result, st->sep);
2385 modifyWord(val, &result, modifyWord_args);
2389 words = Str_Words(val, FALSE);
2391 DEBUG2(VAR, "ModifyWords: split \"%s\" into %u words\n",
2392 val, (unsigned)words.len);
2394 SepBuf_Init(&result, st->sep);
2395 for (i = 0; i < words.len; i++) {
2396 modifyWord(words.words[i], &result, modifyWord_args);
2397 if (result.buf.len > 0)
2398 SepBuf_Sep(&result);
2404 Expr_SetValueOwn(expr, SepBuf_DoneData(&result));
2407 /* :@var@...${var}...@ */
2408 static ApplyModifierResult
2409 ApplyModifier_Loop(const char **pp, ApplyModifiersState *st)
2411 Expr *expr = st->expr;
2412 struct ModifyWord_LoopArgs args;
2416 args.scope = expr->scope;
2418 (*pp)++; /* Skip the first '@' */
2419 res = ParseModifierPart(pp, '@', VARE_NONE, st, &args.tvar);
2422 if (opts.strict && strchr(args.tvar, '$') != NULL) {
2423 Parse_Error(PARSE_FATAL,
2424 "In the :@ modifier of \"%s\", the variable name \"%s\" "
2425 "must not contain a dollar.",
2426 expr->var->name.str, args.tvar);
2430 res = ParseModifierPart(pp, '@', VARE_NONE, st, &args.str);
2434 if (!(expr->eflags & VARE_WANTRES))
2437 args.eflags = expr->eflags & ~(unsigned)VARE_KEEP_DOLLAR;
2439 st->sep = ' '; /* XXX: should be st->sep for consistency */
2440 ModifyWords(st, ModifyWord_Loop, &args, st->oneBigWord);
2442 /* XXX: Consider restoring the previous variable instead of deleting. */
2444 * XXX: The variable name should not be expanded here, see
2447 Var_DeleteExpand(expr->scope, args.tvar);
2455 /* :Ddefined or :Uundefined */
2456 static ApplyModifierResult
2457 ApplyModifier_Defined(const char **pp, ApplyModifiersState *st)
2459 Expr *expr = st->expr;
2463 VarEvalFlags eflags = VARE_NONE;
2464 if (expr->eflags & VARE_WANTRES)
2465 if ((**pp == 'D') == (expr->defined == DEF_REGULAR))
2466 eflags = expr->eflags;
2470 while (!IsDelimiter(*p, st) && *p != '\0') {
2472 /* XXX: This code is similar to the one in Var_Parse.
2473 * See if the code can be merged.
2474 * See also ApplyModifier_Match and ParseModifierPart. */
2476 /* Escaped delimiter or other special character */
2477 /* See Buf_AddEscaped in for.c. */
2480 if (IsDelimiter(c, st) || c == '$' || c == '\\') {
2481 Buf_AddByte(&buf, c);
2487 /* Nested variable expression */
2491 (void)Var_Parse(&p, expr->scope, eflags, &nested_val);
2492 /* TODO: handle errors */
2493 if (expr->eflags & VARE_WANTRES)
2494 Buf_AddStr(&buf, nested_val.str);
2495 FStr_Done(&nested_val);
2500 Buf_AddByte(&buf, *p);
2507 if (eflags & VARE_WANTRES)
2508 Expr_SetValueOwn(expr, Buf_DoneData(&buf));
2516 static ApplyModifierResult
2517 ApplyModifier_Literal(const char **pp, ApplyModifiersState *st)
2519 Expr *expr = st->expr;
2523 if (expr->eflags & VARE_WANTRES) {
2525 Expr_SetValueOwn(expr, bmake_strdup(expr->var->name.str));
2532 TryParseTime(const char **pp, time_t *out_time)
2537 if (!ch_isdigit(**pp))
2541 n = strtoul(*pp, &end, 10);
2542 if (n == ULONG_MAX && errno == ERANGE)
2546 *out_time = (time_t)n; /* ignore possible truncation for now */
2551 static ApplyModifierResult
2552 ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st)
2556 const char *mod = *pp;
2557 if (!ModMatchEq(mod, "gmtime", st))
2560 if (mod[6] == '=') {
2561 const char *p = mod + 7;
2562 if (!TryParseTime(&p, &utc)) {
2563 Parse_Error(PARSE_FATAL,
2564 "Invalid time value: %s", mod + 7);
2573 if (st->expr->eflags & VARE_WANTRES)
2574 Expr_SetValueOwn(st->expr,
2575 VarStrftime(st->expr->value.str, TRUE, utc));
2581 static ApplyModifierResult
2582 ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st)
2586 const char *mod = *pp;
2587 if (!ModMatchEq(mod, "localtime", st))
2590 if (mod[9] == '=') {
2591 const char *p = mod + 10;
2592 if (!TryParseTime(&p, &utc)) {
2593 Parse_Error(PARSE_FATAL,
2594 "Invalid time value: %s", mod + 10);
2603 if (st->expr->eflags & VARE_WANTRES)
2604 Expr_SetValueOwn(st->expr,
2605 VarStrftime(st->expr->value.str, FALSE, utc));
2611 static ApplyModifierResult
2612 ApplyModifier_Hash(const char **pp, ApplyModifiersState *st)
2614 if (!ModMatch(*pp, "hash", st))
2618 if (st->expr->eflags & VARE_WANTRES)
2619 Expr_SetValueOwn(st->expr, VarHash(st->expr->value.str));
2625 static ApplyModifierResult
2626 ApplyModifier_Path(const char **pp, ApplyModifiersState *st)
2628 Expr *expr = st->expr;
2634 if (!(st->expr->eflags & VARE_WANTRES))
2639 gn = Targ_FindNode(expr->var->name.str);
2640 if (gn == NULL || gn->type & OP_NOPATH) {
2642 } else if (gn->path != NULL) {
2643 path = bmake_strdup(gn->path);
2645 SearchPath *searchPath = Suff_FindPath(gn);
2646 path = Dir_FindFile(expr->var->name.str, searchPath);
2649 path = bmake_strdup(expr->var->name.str);
2650 Expr_SetValueOwn(expr, path);
2656 static ApplyModifierResult
2657 ApplyModifier_ShellCommand(const char **pp, ApplyModifiersState *st)
2659 Expr *expr = st->expr;
2665 res = ParseModifierPart(pp, '!', expr->eflags, st, &cmd);
2670 if (expr->eflags & VARE_WANTRES)
2671 Expr_SetValueOwn(expr, Cmd_Exec(cmd, &errfmt));
2673 Expr_SetValueRefer(expr, "");
2675 Error(errfmt, cmd); /* XXX: why still return AMR_OK? */
2683 * The :range modifier generates an integer sequence as long as the words.
2684 * The :range=7 modifier generates an integer sequence from 1 to 7.
2686 static ApplyModifierResult
2687 ApplyModifier_Range(const char **pp, ApplyModifiersState *st)
2693 const char *mod = *pp;
2694 if (!ModMatchEq(mod, "range", st))
2697 if (mod[5] == '=') {
2698 const char *p = mod + 6;
2699 if (!TryParseSize(&p, &n)) {
2700 Parse_Error(PARSE_FATAL,
2701 "Invalid number \"%s\" for ':range' modifier",
2711 if (!(st->expr->eflags & VARE_WANTRES))
2715 Words words = Str_Words(st->expr->value.str, FALSE);
2722 for (i = 0; i < n; i++) {
2724 /* XXX: Use st->sep instead of ' ', for consistency. */
2725 Buf_AddByte(&buf, ' ');
2727 Buf_AddInt(&buf, 1 + (int)i);
2730 Expr_SetValueOwn(st->expr, Buf_DoneData(&buf));
2734 /* Parse a ':M' or ':N' modifier. */
2736 ParseModifier_Match(const char **pp, const ApplyModifiersState *st,
2739 const char *mod = *pp;
2740 Expr *expr = st->expr;
2741 Boolean copy = FALSE; /* pattern should be, or has been, copied */
2742 Boolean needSubst = FALSE;
2747 * In the loop below, ignore ':' unless we are at (or back to) the
2748 * original brace level.
2749 * XXX: This will likely not work right if $() and ${} are intermixed.
2752 * XXX: This code is similar to the one in Var_Parse.
2753 * See if the code can be merged.
2754 * See also ApplyModifier_Defined.
2758 for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) {
2760 (IsDelimiter(p[1], st) || p[1] == st->startc)) {
2768 if (*p == '(' || *p == '{')
2770 if (*p == ')' || *p == '}') {
2783 /* Compress the \:'s out of the pattern. */
2784 pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1);
2787 for (; src < endpat; src++, dst++) {
2788 if (src[0] == '\\' && src + 1 < endpat &&
2789 /* XXX: st->startc is missing here; see above */
2790 IsDelimiter(src[1], st))
2796 pattern = bmake_strsedup(mod + 1, endpat);
2800 char *old_pattern = pattern;
2801 (void)Var_Subst(pattern, expr->scope, expr->eflags, &pattern);
2802 /* TODO: handle errors */
2806 DEBUG3(VAR, "Pattern[%s] for [%s] is [%s]\n",
2807 expr->var->name.str, expr->value.str, pattern);
2809 *out_pattern = pattern;
2812 /* :Mpattern or :Npattern */
2813 static ApplyModifierResult
2814 ApplyModifier_Match(const char **pp, ApplyModifiersState *st)
2816 const char mod = **pp;
2819 ParseModifier_Match(pp, st, &pattern);
2821 if (st->expr->eflags & VARE_WANTRES) {
2822 ModifyWordProc modifyWord =
2823 mod == 'M' ? ModifyWord_Match : ModifyWord_NoMatch;
2824 ModifyWords(st, modifyWord, pattern, st->oneBigWord);
2832 ParsePatternFlags(const char **pp, VarPatternFlags *pflags, Boolean *oneBigWord)
2836 pflags->subGlobal = TRUE;
2837 else if (**pp == '1')
2838 pflags->subOnce = TRUE;
2839 else if (**pp == 'W')
2847 static ApplyModifierResult
2848 ApplyModifier_Subst(const char **pp, ApplyModifiersState *st)
2850 struct ModifyWord_SubstArgs args;
2855 char delim = (*pp)[1];
2856 if (delim == '\0') {
2857 Error("Missing delimiter for modifier ':S'");
2864 args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE };
2865 args.matched = FALSE;
2868 args.pflags.anchorStart = TRUE;
2872 res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &lhs,
2873 &args.lhsLen, &args.pflags, NULL);
2878 res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &rhs,
2879 &args.rhsLen, NULL, &args);
2884 oneBigWord = st->oneBigWord;
2885 ParsePatternFlags(pp, &args.pflags, &oneBigWord);
2887 ModifyWords(st, ModifyWord_Subst, &args, oneBigWord);
2897 static ApplyModifierResult
2898 ApplyModifier_Regex(const char **pp, ApplyModifiersState *st)
2901 struct ModifyWord_SubstRegexArgs args;
2906 char delim = (*pp)[1];
2907 if (delim == '\0') {
2908 Error("Missing delimiter for :C modifier");
2915 res = ParseModifierPart(pp, delim, st->expr->eflags, st, &re);
2919 res = ParseModifierPart(pp, delim, st->expr->eflags, st, &args.replace);
2920 if (args.replace == NULL) {
2925 args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE };
2926 args.matched = FALSE;
2927 oneBigWord = st->oneBigWord;
2928 ParsePatternFlags(pp, &args.pflags, &oneBigWord);
2930 if (!(st->expr->eflags & VARE_WANTRES)) {
2936 error = regcomp(&args.re, re, REG_EXTENDED);
2939 VarREError(error, &args.re, "Regex compilation error");
2944 args.nsub = args.re.re_nsub + 1;
2948 ModifyWords(st, ModifyWord_SubstRegex, &args, oneBigWord);
2958 static ApplyModifierResult
2959 ApplyModifier_Quote(const char **pp, ApplyModifiersState *st)
2961 Boolean quoteDollar = **pp == 'q';
2962 if (!IsDelimiter((*pp)[1], st))
2966 if (st->expr->eflags & VARE_WANTRES)
2967 Expr_SetValueOwn(st->expr,
2968 VarQuote(st->expr->value.str, quoteDollar));
2975 ModifyWord_Copy(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED)
2977 SepBuf_AddStr(buf, word);
2980 /* :ts<separator> */
2981 static ApplyModifierResult
2982 ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st)
2984 const char *sep = *pp + 2;
2987 * Even if VARE_WANTRES is not set, proceed as normal since there is
2988 * neither any observable side effect nor a performance penalty.
2989 * Checking for VARE_WANTRES for every single piece of code in here
2990 * would make the code in this function too hard to read.
2993 /* ":ts<any><endc>" or ":ts<any>:" */
2994 if (sep[0] != st->endc && IsDelimiter(sep[1], st)) {
3000 /* ":ts<endc>" or ":ts:" */
3001 if (IsDelimiter(sep[0], st)) {
3003 st->sep = '\0'; /* no separator */
3007 /* ":ts<unrecognised><unrecognised>". */
3008 if (sep[0] != '\\') {
3009 (*pp)++; /* just for backwards compatibility */
3014 if (sep[1] == 'n') {
3021 if (sep[1] == 't') {
3027 /* ":ts\x40" or ":ts\100" */
3029 const char *p = sep + 1;
3030 int base = 8; /* assume octal */
3032 if (sep[1] == 'x') {
3035 } else if (!ch_isdigit(sep[1])) {
3036 (*pp)++; /* just for backwards compatibility */
3037 return AMR_BAD; /* ":ts<backslash><unrecognised>". */
3040 if (!TryParseChar(&p, base, &st->sep)) {
3041 Parse_Error(PARSE_FATAL,
3042 "Invalid character number: %s", p);
3045 if (!IsDelimiter(*p, st)) {
3046 (*pp)++; /* just for backwards compatibility */
3054 ModifyWords(st, ModifyWord_Copy, NULL, st->oneBigWord);
3059 str_toupper(const char *str)
3065 res = bmake_malloc(len + 1);
3066 for (i = 0; i < len + 1; i++)
3067 res[i] = ch_toupper(str[i]);
3073 str_tolower(const char *str)
3079 res = bmake_malloc(len + 1);
3080 for (i = 0; i < len + 1; i++)
3081 res[i] = ch_tolower(str[i]);
3086 /* :tA, :tu, :tl, :ts<separator>, etc. */
3087 static ApplyModifierResult
3088 ApplyModifier_To(const char **pp, ApplyModifiersState *st)
3090 Expr *expr = st->expr;
3091 const char *mod = *pp;
3092 assert(mod[0] == 't');
3094 if (IsDelimiter(mod[1], st) || mod[1] == '\0') {
3096 return AMR_BAD; /* Found ":t<endc>" or ":t:". */
3100 return ApplyModifier_ToSep(pp, st);
3102 if (!IsDelimiter(mod[2], st)) { /* :t<unrecognized> */
3107 if (mod[1] == 'A') { /* :tA */
3109 ModifyWords(st, ModifyWord_Realpath, NULL, st->oneBigWord);
3113 if (mod[1] == 'u') { /* :tu */
3115 if (st->expr->eflags & VARE_WANTRES)
3116 Expr_SetValueOwn(expr, str_toupper(expr->value.str));
3120 if (mod[1] == 'l') { /* :tl */
3122 if (st->expr->eflags & VARE_WANTRES)
3123 Expr_SetValueOwn(expr, str_tolower(expr->value.str));
3127 if (mod[1] == 'W' || mod[1] == 'w') { /* :tW, :tw */
3129 st->oneBigWord = mod[1] == 'W';
3133 /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */
3134 *pp = mod + 1; /* XXX: unnecessary but observable */
3138 /* :[#], :[1], :[-1..1], etc. */
3139 static ApplyModifierResult
3140 ApplyModifier_Words(const char **pp, ApplyModifiersState *st)
3142 Expr *expr = st->expr;
3148 (*pp)++; /* skip the '[' */
3149 res = ParseModifierPart(pp, ']', expr->eflags, st, &estr);
3153 if (!IsDelimiter(**pp, st))
3154 goto bad_modifier; /* Found junk after ']' */
3156 if (!(expr->eflags & VARE_WANTRES))
3159 if (estr[0] == '\0')
3160 goto bad_modifier; /* Found ":[]". */
3162 if (estr[0] == '#' && estr[1] == '\0') { /* Found ":[#]" */
3163 if (st->oneBigWord) {
3164 Expr_SetValueRefer(expr, "1");
3168 Words words = Str_Words(expr->value.str, FALSE);
3169 size_t ac = words.len;
3172 /* 3 digits + '\0' is usually enough */
3173 Buf_InitSize(&buf, 4);
3174 Buf_AddInt(&buf, (int)ac);
3175 Expr_SetValueOwn(expr, Buf_DoneData(&buf));
3180 if (estr[0] == '*' && estr[1] == '\0') { /* Found ":[*]" */
3181 st->oneBigWord = TRUE;
3185 if (estr[0] == '@' && estr[1] == '\0') { /* Found ":[@]" */
3186 st->oneBigWord = FALSE;
3191 * We expect estr to contain a single integer for :[N], or two
3192 * integers separated by ".." for :[start..end].
3195 if (!TryParseIntBase0(&p, &first))
3196 goto bad_modifier; /* Found junk instead of a number */
3198 if (p[0] == '\0') { /* Found only one integer in :[N] */
3200 } else if (p[0] == '.' && p[1] == '.' && p[2] != '\0') {
3201 /* Expecting another integer after ".." */
3203 if (!TryParseIntBase0(&p, &last) || *p != '\0')
3204 goto bad_modifier; /* Found junk after ".." */
3206 goto bad_modifier; /* Found junk instead of ".." */
3209 * Now first and last are properly filled in, but we still have to
3210 * check for 0 as a special case.
3212 if (first == 0 && last == 0) {
3213 /* ":[0]" or perhaps ":[0..0]" */
3214 st->oneBigWord = TRUE;
3218 /* ":[0..N]" or ":[N..0]" */
3219 if (first == 0 || last == 0)
3222 /* Normal case: select the words described by first and last. */
3223 Expr_SetValueOwn(expr,
3224 VarSelectWords(expr->value.str, first, last,
3225 st->sep, st->oneBigWord));
3237 str_cmp_asc(const void *a, const void *b)
3239 return strcmp(*(const char *const *)a, *(const char *const *)b);
3243 str_cmp_desc(const void *a, const void *b)
3245 return strcmp(*(const char *const *)b, *(const char *const *)a);
3249 ShuffleStrings(char **strs, size_t n)
3253 for (i = n - 1; i > 0; i--) {
3254 size_t rndidx = (size_t)random() % (i + 1);
3256 strs[i] = strs[rndidx];
3261 /* :O (order ascending) or :Or (order descending) or :Ox (shuffle) */
3262 static ApplyModifierResult
3263 ApplyModifier_Order(const char **pp, ApplyModifiersState *st)
3265 const char *mod = (*pp)++; /* skip past the 'O' in any case */
3271 if (IsDelimiter(mod[1], st)) {
3273 } else if ((mod[1] == 'r' || mod[1] == 'x') &&
3274 IsDelimiter(mod[2], st)) {
3276 mode = mod[1] == 'r' ? DESC : SHUFFLE;
3280 if (!(st->expr->eflags & VARE_WANTRES))
3283 words = Str_Words(st->expr->value.str, FALSE);
3284 if (mode == SHUFFLE)
3285 ShuffleStrings(words.words, words.len);
3287 qsort(words.words, words.len, sizeof words.words[0],
3288 mode == ASC ? str_cmp_asc : str_cmp_desc);
3289 Expr_SetValueOwn(st->expr, Words_JoinFree(words));
3294 /* :? then : else */
3295 static ApplyModifierResult
3296 ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st)
3298 Expr *expr = st->expr;
3299 char *then_expr, *else_expr;
3302 Boolean value = FALSE;
3303 VarEvalFlags then_eflags = VARE_NONE;
3304 VarEvalFlags else_eflags = VARE_NONE;
3306 int cond_rc = COND_PARSE; /* anything other than COND_INVALID */
3307 if (expr->eflags & VARE_WANTRES) {
3308 cond_rc = Cond_EvalCondition(expr->var->name.str, &value);
3309 if (cond_rc != COND_INVALID && value)
3310 then_eflags = expr->eflags;
3311 if (cond_rc != COND_INVALID && !value)
3312 else_eflags = expr->eflags;
3315 (*pp)++; /* skip past the '?' */
3316 res = ParseModifierPart(pp, ':', then_eflags, st, &then_expr);
3320 res = ParseModifierPart(pp, st->endc, else_eflags, st, &else_expr);
3324 (*pp)--; /* Go back to the st->endc. */
3326 if (cond_rc == COND_INVALID) {
3327 Error("Bad conditional expression `%s' in %s?%s:%s",
3328 expr->var->name.str, expr->var->name.str,
3329 then_expr, else_expr);
3333 if (!(expr->eflags & VARE_WANTRES)) {
3337 Expr_SetValueOwn(expr, then_expr);
3340 Expr_SetValueOwn(expr, else_expr);
3348 * The ::= modifiers are special in that they do not read the variable value
3349 * but instead assign to that variable. They always expand to an empty
3352 * Their main purpose is in supporting .for loops that generate shell commands
3353 * since an ordinary variable assignment at that point would terminate the
3354 * dependency group for these targets. For example:
3356 * list-targets: .USE
3357 * .for i in ${.TARGET} ${.TARGET:R}.gz
3359 * @echo 'The target is ${t:T}.'
3362 * ::=<str> Assigns <str> as the new value of variable.
3363 * ::?=<str> Assigns <str> as value of variable if
3364 * it was not already set.
3365 * ::+=<str> Appends <str> to variable.
3366 * ::!=<cmd> Assigns output of <cmd> as the new value of
3369 static ApplyModifierResult
3370 ApplyModifier_Assign(const char **pp, ApplyModifiersState *st)
3372 Expr *expr = st->expr;
3377 const char *mod = *pp;
3378 const char *op = mod + 1;
3382 if ((op[0] == '!' || op[0] == '+' || op[0] == '?') && op[1] == '=')
3384 return AMR_UNKNOWN; /* "::<unrecognised>" */
3387 if (expr->var->name.str[0] == '\0') {
3403 res = ParseModifierPart(pp, st->endc, expr->eflags, st, &val);
3407 (*pp)--; /* Go back to the st->endc. */
3409 if (!(expr->eflags & VARE_WANTRES))
3412 scope = expr->scope; /* scope where v belongs */
3413 if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL) {
3414 Var *gv = VarFind(expr->var->name.str, expr->scope, FALSE);
3416 scope = SCOPE_GLOBAL;
3421 /* XXX: Expanding the variable name at this point sounds wrong. */
3424 Var_AppendExpand(scope, expr->var->name.str, val);
3428 char *cmd_output = Cmd_Exec(val, &errfmt);
3432 Var_SetExpand(scope, expr->var->name.str, cmd_output);
3437 if (expr->defined == DEF_REGULAR)
3441 Var_SetExpand(scope, expr->var->name.str, val);
3444 Expr_SetValueRefer(expr, "");
3453 * remember current value
3455 static ApplyModifierResult
3456 ApplyModifier_Remember(const char **pp, ApplyModifiersState *st)
3458 Expr *expr = st->expr;
3459 const char *mod = *pp;
3462 if (!ModMatchEq(mod, "_", st))
3465 name = FStr_InitRefer("_");
3466 if (mod[1] == '=') {
3468 * XXX: This ad-hoc call to strcspn deviates from the usual
3469 * behavior defined in ParseModifierPart. This creates an
3470 * unnecessary, undocumented inconsistency in make.
3472 const char *arg = mod + 2;
3473 size_t argLen = strcspn(arg, ":)}");
3475 name = FStr_InitOwn(bmake_strldup(arg, argLen));
3479 if (expr->eflags & VARE_WANTRES)
3480 Var_Set(expr->scope, name.str, expr->value.str);
3487 * Apply the given function to each word of the variable value,
3488 * for a single-letter modifier such as :H, :T.
3490 static ApplyModifierResult
3491 ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
3492 ModifyWordProc modifyWord)
3494 if (!IsDelimiter((*pp)[1], st))
3498 if (st->expr->eflags & VARE_WANTRES)
3499 ModifyWords(st, modifyWord, NULL, st->oneBigWord);
3504 static ApplyModifierResult
3505 ApplyModifier_Unique(const char **pp, ApplyModifiersState *st)
3507 if (!IsDelimiter((*pp)[1], st))
3511 if (st->expr->eflags & VARE_WANTRES)
3512 Expr_SetValueOwn(st->expr, VarUniq(st->expr->value.str));
3519 static ApplyModifierResult
3520 ApplyModifier_SysV(const char **pp, ApplyModifiersState *st)
3522 Expr *expr = st->expr;
3526 const char *mod = *pp;
3527 Boolean eqFound = FALSE;
3530 * First we make a pass through the string trying to verify it is a
3531 * SysV-make-style translation. It must be: <lhs>=<rhs>
3534 const char *p = mod;
3535 while (*p != '\0' && depth > 0) {
3536 if (*p == '=') { /* XXX: should also test depth == 1 */
3538 /* continue looking for st->endc */
3539 } else if (*p == st->endc)
3541 else if (*p == st->startc)
3546 if (*p != st->endc || !eqFound)
3549 res = ParseModifierPart(pp, '=', expr->eflags, st, &lhs);
3553 /* The SysV modifier lasts until the end of the variable expression. */
3554 res = ParseModifierPart(pp, st->endc, expr->eflags, st, &rhs);
3558 (*pp)--; /* Go back to the st->endc. */
3560 if (lhs[0] == '\0' && expr->value.str[0] == '\0') {
3561 /* Do not turn an empty expression into non-empty. */
3563 struct ModifyWord_SYSVSubstArgs args = {
3564 expr->scope, lhs, rhs
3566 ModifyWords(st, ModifyWord_SYSVSubst, &args, st->oneBigWord);
3576 static ApplyModifierResult
3577 ApplyModifier_SunShell(const char **pp, ApplyModifiersState *st)
3579 Expr *expr = st->expr;
3580 const char *p = *pp;
3581 if (!(p[1] == 'h' && IsDelimiter(p[2], st)))
3585 if (expr->eflags & VARE_WANTRES) {
3587 char *output = Cmd_Exec(expr->value.str, &errfmt);
3589 Error(errfmt, expr->value.str);
3590 Expr_SetValueOwn(expr, output);
3598 LogBeforeApply(const ApplyModifiersState *st, const char *mod)
3600 const Expr *expr = st->expr;
3601 char eflags_str[VarEvalFlags_ToStringSize];
3602 char vflags_str[VarFlags_ToStringSize];
3603 Boolean is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], st);
3605 /* At this point, only the first character of the modifier can
3606 * be used since the end of the modifier is not yet known. */
3607 debug_printf("Applying ${%s:%c%s} to \"%s\" (%s, %s, %s)\n",
3608 expr->var->name.str, mod[0], is_single_char ? "" : "...",
3610 VarEvalFlags_ToString(eflags_str, expr->eflags),
3611 VarFlags_ToString(vflags_str, expr->var->flags),
3612 ExprDefined_Name[expr->defined]);
3616 LogAfterApply(const ApplyModifiersState *st, const char *p, const char *mod)
3618 const Expr *expr = st->expr;
3619 const char *value = expr->value.str;
3620 char eflags_str[VarEvalFlags_ToStringSize];
3621 char vflags_str[VarFlags_ToStringSize];
3622 const char *quot = value == var_Error ? "" : "\"";
3624 debug_printf("Result of ${%s:%.*s} is %s%s%s (%s, %s, %s)\n",
3625 expr->var->name.str, (int)(p - mod), mod,
3626 quot, value == var_Error ? "error" : value, quot,
3627 VarEvalFlags_ToString(eflags_str, expr->eflags),
3628 VarFlags_ToString(vflags_str, expr->var->flags),
3629 ExprDefined_Name[expr->defined]);
3632 static ApplyModifierResult
3633 ApplyModifier(const char **pp, ApplyModifiersState *st)
3637 return ApplyModifier_ShellCommand(pp, st);
3639 return ApplyModifier_Assign(pp, st);
3641 return ApplyModifier_IfElse(pp, st);
3643 return ApplyModifier_Loop(pp, st);
3645 return ApplyModifier_Words(pp, st);
3647 return ApplyModifier_Remember(pp, st);
3650 return ApplyModifier_Regex(pp, st);
3653 return ApplyModifier_Defined(pp, st);
3655 return ApplyModifier_WordFunc(pp, st, ModifyWord_Suffix);
3657 return ApplyModifier_Gmtime(pp, st);
3659 return ApplyModifier_WordFunc(pp, st, ModifyWord_Head);
3661 return ApplyModifier_Hash(pp, st);
3663 return ApplyModifier_Literal(pp, st);
3665 return ApplyModifier_Localtime(pp, st);
3668 return ApplyModifier_Match(pp, st);
3670 return ApplyModifier_Order(pp, st);
3672 return ApplyModifier_Path(pp, st);
3675 return ApplyModifier_Quote(pp, st);
3677 return ApplyModifier_WordFunc(pp, st, ModifyWord_Root);
3679 return ApplyModifier_Range(pp, st);
3681 return ApplyModifier_Subst(pp, st);
3684 return ApplyModifier_SunShell(pp, st);
3687 return ApplyModifier_WordFunc(pp, st, ModifyWord_Tail);
3689 return ApplyModifier_To(pp, st);
3691 return ApplyModifier_Defined(pp, st);
3693 return ApplyModifier_Unique(pp, st);
3699 static void ApplyModifiers(Expr *, const char **, char, char);
3701 typedef enum ApplyModifiersIndirectResult {
3702 /* The indirect modifiers have been applied successfully. */
3704 /* Fall back to the SysV modifier. */
3708 } ApplyModifiersIndirectResult;
3711 * While expanding a variable expression, expand and apply indirect modifiers,
3712 * such as in ${VAR:${M_indirect}}.
3714 * All indirect modifiers of a group must come from a single variable
3715 * expression. ${VAR:${M1}} is valid but ${VAR:${M1}${M2}} is not.
3717 * Multiple groups of indirect modifiers can be chained by separating them
3718 * with colons. ${VAR:${M1}:${M2}} contains 2 indirect modifiers.
3720 * If the variable expression is not followed by st->endc or ':', fall
3721 * back to trying the SysV modifier, such as in ${VAR:${FROM}=${TO}}.
3723 static ApplyModifiersIndirectResult
3724 ApplyModifiersIndirect(ApplyModifiersState *st, const char **pp)
3726 Expr *expr = st->expr;
3727 const char *p = *pp;
3730 (void)Var_Parse(&p, expr->scope, expr->eflags, &mods);
3731 /* TODO: handle errors */
3733 if (mods.str[0] != '\0' && *p != '\0' && !IsDelimiter(*p, st)) {
3738 DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n",
3739 mods.str, (int)(p - *pp), *pp);
3741 if (mods.str[0] != '\0') {
3742 const char *modsp = mods.str;
3743 ApplyModifiers(expr, &modsp, '\0', '\0');
3744 if (expr->value.str == var_Error || *modsp != '\0') {
3747 return AMIR_OUT; /* error already reported */
3754 else if (*p == '\0' && st->endc != '\0') {
3755 Error("Unclosed variable expression after indirect "
3756 "modifier, expecting '%c' for variable \"%s\"",
3757 st->endc, expr->var->name.str);
3763 return AMIR_CONTINUE;
3766 static ApplyModifierResult
3767 ApplySingleModifier(const char **pp, ApplyModifiersState *st)
3769 ApplyModifierResult res;
3770 const char *mod = *pp;
3771 const char *p = *pp;
3774 LogBeforeApply(st, mod);
3776 res = ApplyModifier(&p, st);
3779 if (res == AMR_UNKNOWN) {
3781 res = ApplyModifier_SysV(&p, st);
3785 if (res == AMR_UNKNOWN) {
3787 * Guess the end of the current modifier.
3788 * XXX: Skipping the rest of the modifier hides
3789 * errors and leads to wrong results.
3790 * Parsing should rather stop here.
3792 for (p++; !IsDelimiter(*p, st) && *p != '\0'; p++)
3794 Parse_Error(PARSE_FATAL, "Unknown modifier \"%.*s\"",
3795 (int)(p - mod), mod);
3796 Expr_SetValueRefer(st->expr, var_Error);
3798 if (res == AMR_CLEANUP || res == AMR_BAD) {
3804 LogAfterApply(st, p, mod);
3806 if (*p == '\0' && st->endc != '\0') {
3808 "Unclosed variable expression, expecting '%c' for "
3809 "modifier \"%.*s\" of variable \"%s\" with value \"%s\"",
3811 (int)(p - mod), mod,
3812 st->expr->var->name.str, st->expr->value.str);
3813 } else if (*p == ':') {
3815 } else if (opts.strict && *p != '\0' && *p != st->endc) {
3816 Parse_Error(PARSE_FATAL,
3817 "Missing delimiter ':' after modifier \"%.*s\"",
3818 (int)(p - mod), mod);
3820 * TODO: propagate parse error to the enclosing
3828 /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */
3832 const char **pp, /* the parsing position, updated upon return */
3833 char startc, /* '(' or '{'; or '\0' for indirect modifiers */
3834 char endc /* ')' or '}'; or '\0' for indirect modifiers */
3837 ApplyModifiersState st = {
3842 FALSE /* .oneBigWord */
3847 assert(startc == '(' || startc == '{' || startc == '\0');
3848 assert(endc == ')' || endc == '}' || endc == '\0');
3849 assert(expr->value.str != NULL);
3853 if (*p == '\0' && endc != '\0') {
3855 "Unclosed variable expression (expecting '%c') for \"%s\"",
3856 st.endc, expr->var->name.str);
3860 while (*p != '\0' && *p != endc) {
3861 ApplyModifierResult res;
3864 ApplyModifiersIndirectResult amir =
3865 ApplyModifiersIndirect(&st, &p);
3866 if (amir == AMIR_CONTINUE)
3868 if (amir == AMIR_OUT)
3871 * It's neither '${VAR}:' nor '${VAR}}'. Try to parse
3872 * it as a SysV modifier, as that is the only modifier
3873 * that can start with '$'.
3879 res = ApplySingleModifier(&p, &st);
3880 if (res == AMR_CLEANUP)
3887 assert(expr->value.str != NULL); /* Use var_Error or varUndefined. */
3891 /* XXX: The modifier end is only guessed. */
3892 Error("Bad modifier \":%.*s\" for variable \"%s\"",
3893 (int)strcspn(mod, ":)}"), mod, expr->var->name.str);
3897 * TODO: Use p + strlen(p) instead, to stop parsing immediately.
3899 * In the unit tests, this generates a few unterminated strings in the
3900 * shell commands though. Instead of producing these unfinished
3901 * strings, commands with evaluation errors should not be run at all.
3903 * To make that happen, Var_Subst must report the actual errors
3904 * instead of returning VPR_OK unconditionally.
3907 Expr_SetValueRefer(expr, var_Error);
3911 * Only four of the local variables are treated specially as they are the
3912 * only four that will be set when dynamic sources are expanded.
3915 VarnameIsDynamic(const char *name, size_t len)
3917 if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) {
3928 if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) {
3929 return strcmp(name, ".TARGET") == 0 ||
3930 strcmp(name, ".ARCHIVE") == 0 ||
3931 strcmp(name, ".PREFIX") == 0 ||
3932 strcmp(name, ".MEMBER") == 0;
3939 UndefinedShortVarValue(char varname, const GNode *scope)
3941 if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) {
3943 * If substituting a local variable in a non-local scope,
3944 * assume it's for dynamic source stuff. We have to handle
3945 * this specially and return the longhand for the variable
3946 * with the dollar sign escaped so it makes it back to the
3947 * caller. Only four of the local variables are treated
3948 * specially as they are the only four that will be set
3949 * when dynamic sources are expanded.
3953 return "$(.TARGET)";
3955 return "$(.MEMBER)";
3957 return "$(.PREFIX)";
3959 return "$(.ARCHIVE)";
3966 * Parse a variable name, until the end character or a colon, whichever
3970 ParseVarname(const char **pp, char startc, char endc,
3971 GNode *scope, VarEvalFlags eflags,
3972 size_t *out_varname_len)
3975 const char *p = *pp;
3976 int depth = 0; /* Track depth so we can spot parse errors. */
3980 while (*p != '\0') {
3981 if ((*p == endc || *p == ':') && depth == 0)
3988 /* A variable inside a variable, expand. */
3991 (void)Var_Parse(&p, scope, eflags, &nested_val);
3992 /* TODO: handle errors */
3993 Buf_AddStr(&buf, nested_val.str);
3994 FStr_Done(&nested_val);
3996 Buf_AddByte(&buf, *p);
4001 *out_varname_len = buf.len;
4002 return Buf_DoneData(&buf);
4005 static VarParseResult
4006 ValidShortVarname(char varname, const char *start)
4008 if (varname != '$' && varname != ':' && varname != '}' &&
4009 varname != ')' && varname != '\0')
4013 return VPR_ERR; /* XXX: Missing error message */
4016 Parse_Error(PARSE_FATAL,
4017 "To escape a dollar, use \\$, not $$, at \"%s\"", start);
4018 else if (varname == '\0')
4019 Parse_Error(PARSE_FATAL, "Dollar followed by nothing");
4021 Parse_Error(PARSE_FATAL,
4022 "Invalid variable name '%c', at \"%s\"", varname, start);
4028 * Parse a single-character variable name such as in $V or $@.
4029 * Return whether to continue parsing.
4032 ParseVarnameShort(char varname, const char **pp, GNode *scope,
4033 VarEvalFlags eflags,
4034 VarParseResult *out_FALSE_res, const char **out_FALSE_val,
4041 vpr = ValidShortVarname(varname, *pp);
4042 if (vpr != VPR_OK) {
4044 *out_FALSE_res = vpr;
4045 *out_FALSE_val = var_Error;
4051 v = VarFind(name, scope, TRUE);
4056 val = UndefinedShortVarValue(varname, scope);
4058 val = eflags & VARE_UNDEFERR ? var_Error : varUndefined;
4060 if (opts.strict && val == var_Error) {
4061 Parse_Error(PARSE_FATAL,
4062 "Variable \"%s\" is undefined", name);
4063 *out_FALSE_res = VPR_ERR;
4064 *out_FALSE_val = val;
4069 * XXX: This looks completely wrong.
4071 * If undefined expressions are not allowed, this should
4072 * rather be VPR_ERR instead of VPR_UNDEF, together with an
4075 * If undefined expressions are allowed, this should rather
4076 * be VPR_UNDEF instead of VPR_OK.
4078 *out_FALSE_res = eflags & VARE_UNDEFERR ? VPR_UNDEF : VPR_OK;
4079 *out_FALSE_val = val;
4087 /* Find variables like @F or <D. */
4089 FindLocalLegacyVar(const char *varname, size_t namelen, GNode *scope,
4090 const char **out_extraModifiers)
4092 /* Only resolve these variables if scope is a "real" target. */
4093 if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL)
4098 if (varname[1] != 'F' && varname[1] != 'D')
4100 if (strchr("@%?*!<>", varname[0]) == NULL)
4104 char name[] = { varname[0], '\0' };
4105 Var *v = VarFind(name, scope, FALSE);
4108 if (varname[1] == 'D') {
4109 *out_extraModifiers = "H:";
4111 *out_extraModifiers = "T:";
4118 static VarParseResult
4119 EvalUndefined(Boolean dynamic, const char *start, const char *p, char *varname,
4120 VarEvalFlags eflags,
4124 *out_val = FStr_InitOwn(bmake_strsedup(start, p));
4129 if ((eflags & VARE_UNDEFERR) && opts.strict) {
4130 Parse_Error(PARSE_FATAL,
4131 "Variable \"%s\" is undefined", varname);
4133 *out_val = FStr_InitRefer(var_Error);
4137 if (eflags & VARE_UNDEFERR) {
4139 *out_val = FStr_InitRefer(var_Error);
4140 return VPR_UNDEF; /* XXX: Should be VPR_ERR instead. */
4144 *out_val = FStr_InitRefer(varUndefined);
4149 * Parse a long variable name enclosed in braces or parentheses such as $(VAR)
4150 * or ${VAR}, up to the closing brace or parenthesis, or in the case of
4151 * ${VAR:Modifiers}, up to the ':' that starts the modifiers.
4152 * Return whether to continue parsing.
4159 VarEvalFlags eflags,
4161 const char **out_FALSE_pp,
4162 VarParseResult *out_FALSE_res,
4163 FStr *out_FALSE_val,
4165 char *out_TRUE_endc,
4166 const char **out_TRUE_p,
4168 Boolean *out_TRUE_haveModifier,
4169 const char **out_TRUE_extraModifiers,
4170 Boolean *out_TRUE_dynamic,
4171 ExprDefined *out_TRUE_exprDefined
4177 Boolean haveModifier;
4178 Boolean dynamic = FALSE;
4180 const char *const start = p;
4181 char endc = startc == '(' ? ')' : '}';
4183 p += 2; /* skip "${" or "$(" or "y(" */
4184 varname = ParseVarname(&p, startc, endc, scope, eflags, &namelen);
4187 haveModifier = TRUE;
4188 } else if (*p == endc) {
4189 haveModifier = FALSE;
4191 Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"", varname);
4194 *out_FALSE_val = FStr_InitRefer(var_Error);
4195 *out_FALSE_res = VPR_ERR;
4199 v = VarFind(varname, scope, TRUE);
4201 /* At this point, p points just after the variable name,
4202 * either at ':' or at endc. */
4205 v = FindLocalLegacyVar(varname, namelen, scope,
4206 out_TRUE_extraModifiers);
4211 * Defer expansion of dynamic variables if they appear in
4212 * non-local scope since they are not defined there.
4214 dynamic = VarnameIsDynamic(varname, namelen) &&
4215 (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL);
4217 if (!haveModifier) {
4218 p++; /* skip endc */
4220 *out_FALSE_res = EvalUndefined(dynamic, start, p,
4221 varname, eflags, out_FALSE_val);
4226 * The variable expression is based on an undefined variable.
4227 * Nevertheless it needs a Var, for modifiers that access the
4228 * variable name, such as :L or :?.
4230 * Most modifiers leave this expression in the "undefined"
4231 * state (VES_UNDEF), only a few modifiers like :D, :U, :L,
4232 * :P turn this undefined expression into a defined
4233 * expression (VES_DEF).
4235 * In the end, after applying all modifiers, if the expression
4236 * is still undefined, Var_Parse will return an empty string
4237 * instead of the actually computed value.
4239 v = VarNew(FStr_InitOwn(varname), "", VFL_NONE);
4240 *out_TRUE_exprDefined = DEF_UNDEF;
4244 *out_TRUE_endc = endc;
4247 *out_TRUE_haveModifier = haveModifier;
4248 *out_TRUE_dynamic = dynamic;
4252 /* Free the environment variable now since we own it. */
4254 FreeEnvVar(Var *v, FStr *inout_val)
4256 char *varValue = Buf_DoneData(&v->val);
4257 if (inout_val->str == varValue)
4258 inout_val->freeIt = varValue;
4262 FStr_Done(&v->name);
4267 * Given the start of a variable expression (such as $v, $(VAR),
4268 * ${VAR:Mpattern}), extract the variable name and value, and the modifiers,
4269 * if any. While doing that, apply the modifiers to the value of the
4270 * expression, forming its final value. A few of the modifiers such as :!cmd!
4271 * or ::= have side effects.
4274 * *pp The string to parse.
4275 * When parsing a condition in ParseEmptyArg, it may also
4276 * point to the "y" of "empty(VARNAME:Modifiers)", which
4277 * is syntactically the same.
4278 * scope The scope for finding variables
4279 * eflags Control the exact details of parsing
4282 * *pp The position where to continue parsing.
4283 * TODO: After a parse error, the value of *pp is
4284 * unspecified. It may not have been updated at all,
4285 * point to some random character in the string, to the
4286 * location of the parse error, or at the end of the
4288 * *out_val The value of the variable expression, never NULL.
4289 * *out_val var_Error if there was a parse error.
4290 * *out_val var_Error if the base variable of the expression was
4291 * undefined, eflags contains VARE_UNDEFERR, and none of
4292 * the modifiers turned the undefined expression into a
4293 * defined expression.
4294 * XXX: It is not guaranteed that an error message has
4296 * *out_val varUndefined if the base variable of the expression
4297 * was undefined, eflags did not contain VARE_UNDEFERR,
4298 * and none of the modifiers turned the undefined
4299 * expression into a defined expression.
4300 * XXX: It is not guaranteed that an error message has
4304 Var_Parse(const char **pp, GNode *scope, VarEvalFlags eflags, FStr *out_val)
4306 const char *p = *pp;
4307 const char *const start = p;
4308 /* TRUE if have modifiers for the variable. */
4309 Boolean haveModifier;
4310 /* Starting character if variable in parens or braces. */
4312 /* Ending character if variable in parens or braces. */
4315 * TRUE if the variable is local and we're expanding it in a
4316 * non-local scope. This is done to support dynamic sources.
4317 * The result is just the expression, unaltered.
4320 const char *extramodifiers;
4321 char eflags_str[VarEvalFlags_ToStringSize];
4327 /* NetBSD lint cannot fully parse C99 struct initializers. */
4330 FStr_InitRefer(NULL),
4337 DEBUG2(VAR, "Var_Parse: %s with %s\n", start,
4338 VarEvalFlags_ToString(eflags_str, eflags));
4340 *out_val = FStr_InitRefer(NULL);
4341 extramodifiers = NULL; /* extra modifiers to apply first */
4345 * Appease GCC, which thinks that the variable might not be
4351 if (startc != '(' && startc != '{') {
4353 if (!ParseVarnameShort(startc, pp, scope, eflags, &res,
4354 &out_val->str, &expr.var))
4356 haveModifier = FALSE;
4360 if (!ParseVarnameLong(p, startc, scope, eflags,
4362 &endc, &p, &expr.var, &haveModifier, &extramodifiers,
4363 &dynamic, &expr.defined))
4368 if (v->flags & VFL_IN_USE)
4369 Fatal("Variable %s is recursive.", v->name.str);
4372 * XXX: This assignment creates an alias to the current value of the
4373 * variable. This means that as long as the value of the expression
4374 * stays the same, the value of the variable must not change.
4375 * Using the '::=' modifier, it could be possible to do exactly this.
4376 * At the bottom of this function, the resulting value is compared to
4377 * the then-current value of the variable. This might also invoke
4378 * undefined behavior.
4380 expr.value = FStr_InitRefer(v->val.data);
4383 * Before applying any modifiers, expand any nested expressions from
4384 * the variable value.
4386 if (strchr(expr.value.str, '$') != NULL && (eflags & VARE_WANTRES)) {
4388 VarEvalFlags nested_eflags = eflags;
4390 nested_eflags &= ~(unsigned)VARE_UNDEFERR;
4391 v->flags |= VFL_IN_USE;
4392 (void)Var_Subst(expr.value.str, scope, nested_eflags,
4394 v->flags &= ~(unsigned)VFL_IN_USE;
4395 /* TODO: handle errors */
4396 Expr_SetValueOwn(&expr, expanded);
4399 if (extramodifiers != NULL) {
4400 const char *em = extramodifiers;
4401 ApplyModifiers(&expr, &em, '\0', '\0');
4405 p++; /* Skip initial colon. */
4406 ApplyModifiers(&expr, &p, startc, endc);
4409 if (*p != '\0') /* Skip past endc if possible. */
4414 if (v->flags & VFL_FROM_ENV) {
4415 FreeEnvVar(v, &expr.value);
4417 } else if (expr.defined != DEF_REGULAR) {
4418 if (expr.defined == DEF_UNDEF) {
4420 Expr_SetValueOwn(&expr,
4421 bmake_strsedup(start, p));
4424 * The expression is still undefined,
4425 * therefore discard the actual value and
4426 * return an error marker instead.
4428 Expr_SetValueRefer(&expr,
4429 eflags & VARE_UNDEFERR
4430 ? var_Error : varUndefined);
4433 /* XXX: This is not standard memory management. */
4434 if (expr.value.str != v->val.data)
4436 FStr_Done(&v->name);
4439 *out_val = expr.value;
4440 return VPR_OK; /* XXX: Is not correct in all cases */
4444 VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalFlags eflags)
4446 /* A dollar sign may be escaped with another dollar sign. */
4447 if (save_dollars && (eflags & VARE_KEEP_DOLLAR))
4448 Buf_AddByte(res, '$');
4449 Buf_AddByte(res, '$');
4454 VarSubstExpr(const char **pp, Buffer *buf, GNode *scope,
4455 VarEvalFlags eflags, Boolean *inout_errorReported)
4457 const char *p = *pp;
4458 const char *nested_p = p;
4461 (void)Var_Parse(&nested_p, scope, eflags, &val);
4462 /* TODO: handle errors */
4464 if (val.str == var_Error || val.str == varUndefined) {
4465 if (!(eflags & VARE_KEEP_UNDEF)) {
4467 } else if ((eflags & VARE_UNDEFERR) || val.str == var_Error) {
4470 * XXX: This condition is wrong. If val == var_Error,
4471 * this doesn't necessarily mean there was an undefined
4472 * variable. It could equally well be a parse error;
4473 * see unit-tests/varmod-order.exp.
4477 * If variable is undefined, complain and skip the
4478 * variable. The complaint will stop us from doing
4479 * anything when the file is parsed.
4481 if (!*inout_errorReported) {
4482 Parse_Error(PARSE_FATAL,
4483 "Undefined variable \"%.*s\"",
4484 (int)(size_t)(nested_p - p), p);
4487 *inout_errorReported = TRUE;
4489 /* Copy the initial '$' of the undefined expression,
4490 * thereby deferring expansion of the expression, but
4491 * expand nested expressions if already possible.
4492 * See unit-tests/varparse-undef-partial.mk. */
4493 Buf_AddByte(buf, *p);
4498 Buf_AddStr(buf, val.str);
4507 * Skip as many characters as possible -- either to the end of the string
4508 * or to the next dollar sign (variable expression).
4511 VarSubstPlain(const char **pp, Buffer *res)
4513 const char *p = *pp;
4514 const char *start = p;
4516 for (p++; *p != '$' && *p != '\0'; p++)
4518 Buf_AddBytesBetween(res, start, p);
4523 * Expand all variable expressions like $V, ${VAR}, $(VAR:Modifiers) in the
4527 * str The string in which the variable expressions are
4529 * scope The scope in which to start searching for
4530 * variables. The other scopes are searched as well.
4531 * eflags Special effects during expansion.
4534 Var_Subst(const char *str, GNode *scope, VarEvalFlags eflags, char **out_res)
4536 const char *p = str;
4539 /* Set true if an error has already been reported,
4540 * to prevent a plethora of messages when recursing */
4541 /* XXX: Why is the 'static' necessary here? */
4542 static Boolean errorReported;
4545 errorReported = FALSE;
4547 while (*p != '\0') {
4548 if (p[0] == '$' && p[1] == '$')
4549 VarSubstDollarDollar(&p, &res, eflags);
4550 else if (p[0] == '$')
4551 VarSubstExpr(&p, &res, scope, eflags, &errorReported);
4553 VarSubstPlain(&p, &res);
4556 *out_res = Buf_DoneDataCompact(&res);
4560 /* Initialize the variables module. */
4564 SCOPE_INTERNAL = GNode_New("Internal");
4565 SCOPE_GLOBAL = GNode_New("Global");
4566 SCOPE_CMDLINE = GNode_New("Command");
4569 /* Clean up the variables module. */
4579 HashTable_DebugStats(&SCOPE_GLOBAL->vars, "Global variables");
4582 /* Print all variables in a scope, sorted by name. */
4584 Var_Dump(GNode *scope)
4586 Vector /* of const char * */ vec;
4589 const char **varnames;
4591 Vector_Init(&vec, sizeof(const char *));
4593 HashIter_Init(&hi, &scope->vars);
4594 while (HashIter_Next(&hi) != NULL)
4595 *(const char **)Vector_Push(&vec) = hi.entry->key;
4596 varnames = vec.items;
4598 qsort(varnames, vec.len, sizeof varnames[0], str_cmp_asc);
4600 for (i = 0; i < vec.len; i++) {
4601 const char *varname = varnames[i];
4602 Var *var = HashTable_FindValue(&scope->vars, varname);
4603 debug_printf("%-16s = %s\n", varname, var->val.data);