| @@ -1,1078 +1,1078 @@ | | | @@ -1,1078 +1,1078 @@ |
1 | /* $NetBSD: var.c,v 1.282 2020/07/20 15:48:50 rillig Exp $ */ | | 1 | /* $NetBSD: var.c,v 1.283 2020/07/20 16:12:52 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1988, 1989, 1990, 1993 | | 4 | * Copyright (c) 1988, 1989, 1990, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * Adam de Boor. | | 8 | * Adam de Boor. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * 3. Neither the name of the University nor the names of its contributors | | 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 | | 19 | * may be used to endorse or promote products derived from this software |
20 | * without specific prior written permission. | | 20 | * without specific prior written permission. |
21 | * | | 21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 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 | | 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. | | 32 | * SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | /* | | 35 | /* |
36 | * Copyright (c) 1989 by Berkeley Softworks | | 36 | * Copyright (c) 1989 by Berkeley Softworks |
37 | * All rights reserved. | | 37 | * All rights reserved. |
38 | * | | 38 | * |
39 | * This code is derived from software contributed to Berkeley by | | 39 | * This code is derived from software contributed to Berkeley by |
40 | * Adam de Boor. | | 40 | * Adam de Boor. |
41 | * | | 41 | * |
42 | * Redistribution and use in source and binary forms, with or without | | 42 | * Redistribution and use in source and binary forms, with or without |
43 | * modification, are permitted provided that the following conditions | | 43 | * modification, are permitted provided that the following conditions |
44 | * are met: | | 44 | * are met: |
45 | * 1. Redistributions of source code must retain the above copyright | | 45 | * 1. Redistributions of source code must retain the above copyright |
46 | * notice, this list of conditions and the following disclaimer. | | 46 | * notice, this list of conditions and the following disclaimer. |
47 | * 2. Redistributions in binary form must reproduce the above copyright | | 47 | * 2. Redistributions in binary form must reproduce the above copyright |
48 | * notice, this list of conditions and the following disclaimer in the | | 48 | * notice, this list of conditions and the following disclaimer in the |
49 | * documentation and/or other materials provided with the distribution. | | 49 | * documentation and/or other materials provided with the distribution. |
50 | * 3. All advertising materials mentioning features or use of this software | | 50 | * 3. All advertising materials mentioning features or use of this software |
51 | * must display the following acknowledgement: | | 51 | * must display the following acknowledgement: |
52 | * This product includes software developed by the University of | | 52 | * This product includes software developed by the University of |
53 | * California, Berkeley and its contributors. | | 53 | * California, Berkeley and its contributors. |
54 | * 4. Neither the name of the University nor the names of 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 | | 55 | * may be used to endorse or promote products derived from this software |
56 | * without specific prior written permission. | | 56 | * without specific prior written permission. |
57 | * | | 57 | * |
58 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | | 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 | | 59 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
60 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 60 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
61 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 61 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
62 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 62 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
63 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 63 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
64 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 67 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
68 | * SUCH DAMAGE. | | 68 | * SUCH DAMAGE. |
69 | */ | | 69 | */ |
70 | | | 70 | |
71 | #ifndef MAKE_NATIVE | | 71 | #ifndef MAKE_NATIVE |
72 | static char rcsid[] = "$NetBSD: var.c,v 1.282 2020/07/20 15:48:50 rillig Exp $"; | | 72 | static char rcsid[] = "$NetBSD: var.c,v 1.283 2020/07/20 16:12:52 rillig Exp $"; |
73 | #else | | 73 | #else |
74 | #include <sys/cdefs.h> | | 74 | #include <sys/cdefs.h> |
75 | #ifndef lint | | 75 | #ifndef lint |
76 | #if 0 | | 76 | #if 0 |
77 | static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; | | 77 | static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; |
78 | #else | | 78 | #else |
79 | __RCSID("$NetBSD: var.c,v 1.282 2020/07/20 15:48:50 rillig Exp $"); | | 79 | __RCSID("$NetBSD: var.c,v 1.283 2020/07/20 16:12:52 rillig Exp $"); |
80 | #endif | | 80 | #endif |
81 | #endif /* not lint */ | | 81 | #endif /* not lint */ |
82 | #endif | | 82 | #endif |
83 | | | 83 | |
84 | /*- | | 84 | /*- |
85 | * var.c -- | | 85 | * var.c -- |
86 | * Variable-handling functions | | 86 | * Variable-handling functions |
87 | * | | 87 | * |
88 | * Interface: | | 88 | * Interface: |
89 | * Var_Set Set the value of a variable in the given | | 89 | * Var_Set Set the value of a variable in the given |
90 | * context. The variable is created if it doesn't | | 90 | * context. The variable is created if it doesn't |
91 | * yet exist. The value and variable name need not | | 91 | * yet exist. The value and variable name need not |
92 | * be preserved. | | 92 | * be preserved. |
93 | * | | 93 | * |
94 | * Var_Append Append more characters to an existing variable | | 94 | * Var_Append Append more characters to an existing variable |
95 | * in the given context. The variable needn't | | 95 | * in the given context. The variable needn't |
96 | * exist already -- it will be created if it doesn't. | | 96 | * exist already -- it will be created if it doesn't. |
97 | * A space is placed between the old value and the | | 97 | * A space is placed between the old value and the |
98 | * new one. | | 98 | * new one. |
99 | * | | 99 | * |
100 | * Var_Exists See if a variable exists. | | 100 | * Var_Exists See if a variable exists. |
101 | * | | 101 | * |
102 | * Var_Value Return the value of a variable in a context or | | 102 | * Var_Value Return the value of a variable in a context or |
103 | * NULL if the variable is undefined. | | 103 | * NULL if the variable is undefined. |
104 | * | | 104 | * |
105 | * Var_Subst Substitute either a single variable or all | | 105 | * Var_Subst Substitute either a single variable or all |
106 | * variables in a string, using the given context as | | 106 | * variables in a string, using the given context as |
107 | * the top-most one. | | 107 | * the top-most one. |
108 | * | | 108 | * |
109 | * Var_Parse Parse a variable expansion from a string and | | 109 | * Var_Parse Parse a variable expansion from a string and |
110 | * return the result and the number of characters | | 110 | * return the result and the number of characters |
111 | * consumed. | | 111 | * consumed. |
112 | * | | 112 | * |
113 | * Var_Delete Delete a variable in a context. | | 113 | * Var_Delete Delete a variable in a context. |
114 | * | | 114 | * |
115 | * Var_Init Initialize this module. | | 115 | * Var_Init Initialize this module. |
116 | * | | 116 | * |
117 | * Debugging: | | 117 | * Debugging: |
118 | * Var_Dump Print out all variables defined in the given | | 118 | * Var_Dump Print out all variables defined in the given |
119 | * context. | | 119 | * context. |
120 | * | | 120 | * |
121 | * XXX: There's a lot of duplication in these functions. | | 121 | * XXX: There's a lot of duplication in these functions. |
122 | */ | | 122 | */ |
123 | | | 123 | |
124 | #include <sys/stat.h> | | 124 | #include <sys/stat.h> |
125 | #ifndef NO_REGEX | | 125 | #ifndef NO_REGEX |
126 | #include <sys/types.h> | | 126 | #include <sys/types.h> |
127 | #include <regex.h> | | 127 | #include <regex.h> |
128 | #endif | | 128 | #endif |
129 | #include <ctype.h> | | 129 | #include <ctype.h> |
130 | #include <inttypes.h> | | 130 | #include <inttypes.h> |
131 | #include <stdlib.h> | | 131 | #include <stdlib.h> |
132 | #include <limits.h> | | 132 | #include <limits.h> |
133 | #include <time.h> | | 133 | #include <time.h> |
134 | | | 134 | |
135 | #include "make.h" | | 135 | #include "make.h" |
136 | #include "buf.h" | | 136 | #include "buf.h" |
137 | #include "dir.h" | | 137 | #include "dir.h" |
138 | #include "job.h" | | 138 | #include "job.h" |
139 | #include "metachar.h" | | 139 | #include "metachar.h" |
140 | | | 140 | |
141 | extern int makelevel; | | 141 | extern int makelevel; |
142 | /* | | 142 | /* |
143 | * This lets us tell if we have replaced the original environ | | 143 | * This lets us tell if we have replaced the original environ |
144 | * (which we cannot free). | | 144 | * (which we cannot free). |
145 | */ | | 145 | */ |
146 | char **savedEnv = NULL; | | 146 | char **savedEnv = NULL; |
147 | | | 147 | |
148 | /* | | 148 | /* |
149 | * This is a harmless return value for Var_Parse that can be used by Var_Subst | | 149 | * This is a harmless return value for Var_Parse that can be used by Var_Subst |
150 | * to determine if there was an error in parsing -- easier than returning | | 150 | * to determine if there was an error in parsing -- easier than returning |
151 | * a flag, as things outside this module don't give a hoot. | | 151 | * a flag, as things outside this module don't give a hoot. |
152 | */ | | 152 | */ |
153 | char var_Error[] = ""; | | 153 | char var_Error[] = ""; |
154 | | | 154 | |
155 | /* | | 155 | /* |
156 | * Similar to var_Error, but returned when the 'VARE_UNDEFERR' flag for | | 156 | * Similar to var_Error, but returned when the 'VARE_UNDEFERR' flag for |
157 | * Var_Parse is not set. Why not just use a constant? Well, gcc likes | | 157 | * Var_Parse is not set. Why not just use a constant? Well, gcc likes |
158 | * to condense identical string instances... | | 158 | * to condense identical string instances... |
159 | */ | | 159 | */ |
160 | static char varNoError[] = ""; | | 160 | static char varNoError[] = ""; |
161 | | | 161 | |
162 | /* | | 162 | /* |
163 | * Traditionally we consume $$ during := like any other expansion. | | 163 | * Traditionally we consume $$ during := like any other expansion. |
164 | * Other make's do not. | | 164 | * Other make's do not. |
165 | * This knob allows controlling the behavior. | | 165 | * This knob allows controlling the behavior. |
166 | * FALSE for old behavior. | | 166 | * FALSE for old behavior. |
167 | * TRUE for new compatible. | | 167 | * TRUE for new compatible. |
168 | */ | | 168 | */ |
169 | #define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS" | | 169 | #define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS" |
170 | static Boolean save_dollars = TRUE; | | 170 | static Boolean save_dollars = TRUE; |
171 | | | 171 | |
172 | /* | | 172 | /* |
173 | * Internally, variables are contained in four different contexts. | | 173 | * Internally, variables are contained in four different contexts. |
174 | * 1) the environment. They may not be changed. If an environment | | 174 | * 1) the environment. They may not be changed. If an environment |
175 | * variable is appended-to, the result is placed in the global | | 175 | * variable is appended-to, the result is placed in the global |
176 | * context. | | 176 | * context. |
177 | * 2) the global context. Variables set in the Makefile are located in | | 177 | * 2) the global context. Variables set in the Makefile are located in |
178 | * the global context. It is the penultimate context searched when | | 178 | * the global context. It is the penultimate context searched when |
179 | * substituting. | | 179 | * substituting. |
180 | * 3) the command-line context. All variables set on the command line | | 180 | * 3) the command-line context. All variables set on the command line |
181 | * are placed in this context. They are UNALTERABLE once placed here. | | 181 | * are placed in this context. They are UNALTERABLE once placed here. |
182 | * 4) the local context. Each target has associated with it a context | | 182 | * 4) the local context. Each target has associated with it a context |
183 | * list. On this list are located the structures describing such | | 183 | * list. On this list are located the structures describing such |
184 | * local variables as $(@) and $(*) | | 184 | * local variables as $(@) and $(*) |
185 | * The four contexts are searched in the reverse order from which they are | | 185 | * The four contexts are searched in the reverse order from which they are |
186 | * listed. | | 186 | * listed. |
187 | */ | | 187 | */ |
188 | GNode *VAR_INTERNAL; /* variables from make itself */ | | 188 | GNode *VAR_INTERNAL; /* variables from make itself */ |
189 | GNode *VAR_GLOBAL; /* variables from the makefile */ | | 189 | GNode *VAR_GLOBAL; /* variables from the makefile */ |
190 | GNode *VAR_CMD; /* variables defined on the command-line */ | | 190 | GNode *VAR_CMD; /* variables defined on the command-line */ |
191 | | | 191 | |
192 | typedef enum { | | 192 | typedef enum { |
193 | FIND_CMD = 0x01, /* look in VAR_CMD when searching */ | | 193 | FIND_CMD = 0x01, /* look in VAR_CMD when searching */ |
194 | FIND_GLOBAL = 0x02, /* look in VAR_GLOBAL as well */ | | 194 | FIND_GLOBAL = 0x02, /* look in VAR_GLOBAL as well */ |
195 | FIND_ENV = 0x04 /* look in the environment also */ | | 195 | FIND_ENV = 0x04 /* look in the environment also */ |
196 | } VarFindFlags; | | 196 | } VarFindFlags; |
197 | | | 197 | |
198 | typedef enum { | | 198 | typedef enum { |
199 | VAR_IN_USE = 0x01, /* Variable's value is currently being used. | | 199 | VAR_IN_USE = 0x01, /* Variable's value is currently being used. |
200 | * Used to avoid endless recursion */ | | 200 | * Used to avoid endless recursion */ |
201 | VAR_FROM_ENV = 0x02, /* Variable comes from the environment */ | | 201 | VAR_FROM_ENV = 0x02, /* Variable comes from the environment */ |
202 | VAR_JUNK = 0x04, /* Variable is a junk variable that | | 202 | VAR_JUNK = 0x04, /* Variable is a junk variable that |
203 | * should be destroyed when done with | | 203 | * should be destroyed when done with |
204 | * it. Used by Var_Parse for undefined, | | 204 | * it. Used by Var_Parse for undefined, |
205 | * modified variables */ | | 205 | * modified variables */ |
206 | VAR_KEEP = 0x08, /* Variable is VAR_JUNK, but we found | | 206 | VAR_KEEP = 0x08, /* Variable is VAR_JUNK, but we found |
207 | * a use for it in some modifier and | | 207 | * a use for it in some modifier and |
208 | * the value is therefore valid */ | | 208 | * the value is therefore valid */ |
209 | VAR_EXPORTED = 0x10, /* Variable is exported */ | | 209 | VAR_EXPORTED = 0x10, /* Variable is exported */ |
210 | VAR_REEXPORT = 0x20, /* Indicate if var needs re-export. | | 210 | VAR_REEXPORT = 0x20, /* Indicate if var needs re-export. |
211 | * This would be true if it contains $'s */ | | 211 | * This would be true if it contains $'s */ |
212 | VAR_FROM_CMD = 0x40 /* Variable came from command line */ | | 212 | VAR_FROM_CMD = 0x40 /* Variable came from command line */ |
213 | } Var_Flags; | | 213 | } Var_Flags; |
214 | | | 214 | |
215 | typedef struct Var { | | 215 | typedef struct Var { |
216 | char *name; /* the variable's name */ | | 216 | char *name; /* the variable's name */ |
217 | Buffer val; /* its value */ | | 217 | Buffer val; /* its value */ |
218 | Var_Flags flags; /* miscellaneous status flags */ | | 218 | Var_Flags flags; /* miscellaneous status flags */ |
219 | } Var; | | 219 | } Var; |
220 | | | 220 | |
221 | /* | | 221 | /* |
222 | * Exporting vars is expensive so skip it if we can | | 222 | * Exporting vars is expensive so skip it if we can |
223 | */ | | 223 | */ |
224 | #define VAR_EXPORTED_NONE 0 | | 224 | #define VAR_EXPORTED_NONE 0 |
225 | #define VAR_EXPORTED_YES 1 | | 225 | #define VAR_EXPORTED_YES 1 |
226 | #define VAR_EXPORTED_ALL 2 | | 226 | #define VAR_EXPORTED_ALL 2 |
227 | static int var_exportedVars = VAR_EXPORTED_NONE; | | 227 | static int var_exportedVars = VAR_EXPORTED_NONE; |
228 | /* | | 228 | /* |
229 | * We pass this to Var_Export when doing the initial export | | 229 | * We pass this to Var_Export when doing the initial export |
230 | * or after updating an exported var. | | 230 | * or after updating an exported var. |
231 | */ | | 231 | */ |
232 | #define VAR_EXPORT_PARENT 1 | | 232 | #define VAR_EXPORT_PARENT 1 |
233 | /* | | 233 | /* |
234 | * We pass this to Var_Export1 to tell it to leave the value alone. | | 234 | * We pass this to Var_Export1 to tell it to leave the value alone. |
235 | */ | | 235 | */ |
236 | #define VAR_EXPORT_LITERAL 2 | | 236 | #define VAR_EXPORT_LITERAL 2 |
237 | | | 237 | |
238 | /* Flags for pattern matching in the :S and :C modifiers */ | | 238 | /* Flags for pattern matching in the :S and :C modifiers */ |
239 | typedef enum { | | 239 | typedef enum { |
240 | VARP_SUB_GLOBAL = 0x01, /* Apply substitution globally */ | | 240 | VARP_SUB_GLOBAL = 0x01, /* Apply substitution globally */ |
241 | VARP_SUB_ONE = 0x02, /* Apply substitution to one word */ | | 241 | VARP_SUB_ONE = 0x02, /* Apply substitution to one word */ |
242 | VARP_SUB_MATCHED = 0x04, /* There was a match */ | | 242 | VARP_SUB_MATCHED = 0x04, /* There was a match */ |
243 | VARP_MATCH_START = 0x08, /* Match at start of word */ | | 243 | VARP_MATCH_START = 0x08, /* Match at start of word */ |
244 | VARP_MATCH_END = 0x10, /* Match at end of word */ | | 244 | VARP_MATCH_END = 0x10, /* Match at end of word */ |
245 | | | 245 | |
246 | /* FIXME: This constant doesn't belong here. | | 246 | /* FIXME: This constant doesn't belong here. |
247 | * It is not related to pattern matching. */ | | 247 | * It is not related to pattern matching. */ |
248 | VAR_NOSUBST = 0x20 /* don't expand vars in ParseModifierPart */ | | 248 | VAR_NOSUBST = 0x20 /* don't expand vars in ParseModifierPart */ |
249 | } VarPatternFlags; | | 249 | } VarPatternFlags; |
250 | | | 250 | |
251 | typedef enum { | | 251 | typedef enum { |
252 | VAR_NO_EXPORT = 0x01 /* do not export */ | | 252 | VAR_NO_EXPORT = 0x01 /* do not export */ |
253 | } VarSet_Flags; | | 253 | } VarSet_Flags; |
254 | | | 254 | |
255 | typedef struct { | | 255 | typedef struct { |
256 | /* | | 256 | /* |
257 | * The following fields are set by Var_Parse() when it | | 257 | * The following fields are set by Var_Parse() when it |
258 | * encounters modifiers that need to keep state for use by | | 258 | * encounters modifiers that need to keep state for use by |
259 | * subsequent modifiers within the same variable expansion. | | 259 | * subsequent modifiers within the same variable expansion. |
260 | */ | | 260 | */ |
261 | Byte varSpace; /* Word separator in expansions */ | | 261 | Byte varSpace; /* Word separator in expansions */ |
262 | Boolean oneBigWord; /* TRUE if we will treat the variable as a | | 262 | Boolean oneBigWord; /* TRUE if we will treat the variable as a |
263 | * single big word, even if it contains | | 263 | * single big word, even if it contains |
264 | * embedded spaces (as opposed to the | | 264 | * embedded spaces (as opposed to the |
265 | * usual behaviour of treating it as | | 265 | * usual behaviour of treating it as |
266 | * several space-separated words). */ | | 266 | * several space-separated words). */ |
267 | } Var_Parse_State; | | 267 | } Var_Parse_State; |
268 | | | 268 | |
269 | /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/" */ | | 269 | /* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/" */ |
270 | typedef struct { | | 270 | typedef struct { |
271 | const char *lhs; | | 271 | const char *lhs; |
272 | size_t lhsLen; | | 272 | size_t lhsLen; |
273 | const char *rhs; | | 273 | const char *rhs; |
274 | size_t rhsLen; | | 274 | size_t rhsLen; |
275 | VarPatternFlags pflags; | | 275 | VarPatternFlags pflags; |
276 | } VarSubstituteArgs; | | 276 | } VarSubstituteArgs; |
277 | | | 277 | |
278 | /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */ | | 278 | /* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */ |
279 | typedef struct { | | 279 | typedef struct { |
280 | char *tvar; /* name of temporary variable */ | | 280 | char *tvar; /* name of temporary variable */ |
281 | char *str; /* string to expand */ | | 281 | char *str; /* string to expand */ |
282 | VarEvalFlags eflags; | | 282 | VarEvalFlags eflags; |
283 | } VarLoop; | | 283 | } VarLoop; |
284 | | | 284 | |
285 | #ifndef NO_REGEX | | 285 | #ifndef NO_REGEX |
286 | /* struct passed as 'void *' to VarRESubstitute() for ":C///" */ | | 286 | /* struct passed as 'void *' to VarRESubstitute() for ":C///" */ |
287 | typedef struct { | | 287 | typedef struct { |
288 | regex_t re; | | 288 | regex_t re; |
289 | int nsub; | | 289 | int nsub; |
290 | regmatch_t *matches; | | 290 | regmatch_t *matches; |
291 | char *replace; | | 291 | char *replace; |
292 | VarPatternFlags pflags; | | 292 | VarPatternFlags pflags; |
293 | } VarREPattern; | | 293 | } VarREPattern; |
294 | #endif | | 294 | #endif |
295 | | | 295 | |
296 | /* struct passed to VarSelectWords() for ":[start..end]" */ | | 296 | /* struct passed to VarSelectWords() for ":[start..end]" */ |
297 | typedef struct { | | 297 | typedef struct { |
298 | int start; /* first word to select */ | | 298 | int start; /* first word to select */ |
299 | int end; /* last word to select */ | | 299 | int end; /* last word to select */ |
300 | } VarSelectWords_t; | | 300 | } VarSelectWords_t; |
301 | | | 301 | |
302 | #define BROPEN '{' | | 302 | #define BROPEN '{' |
303 | #define BRCLOSE '}' | | 303 | #define BRCLOSE '}' |
304 | #define PROPEN '(' | | 304 | #define PROPEN '(' |
305 | #define PRCLOSE ')' | | 305 | #define PRCLOSE ')' |
306 | | | 306 | |
307 | /*- | | 307 | /*- |
308 | *----------------------------------------------------------------------- | | 308 | *----------------------------------------------------------------------- |
309 | * VarFind -- | | 309 | * VarFind -- |
310 | * Find the given variable in the given context and any other contexts | | 310 | * Find the given variable in the given context and any other contexts |
311 | * indicated. | | 311 | * indicated. |
312 | * | | 312 | * |
313 | * Input: | | 313 | * Input: |
314 | * name name to find | | 314 | * name name to find |
315 | * ctxt context in which to find it | | 315 | * ctxt context in which to find it |
316 | * flags FIND_GLOBAL look in VAR_GLOBAL as well | | 316 | * flags FIND_GLOBAL look in VAR_GLOBAL as well |
317 | * FIND_CMD look in VAR_CMD as well | | 317 | * FIND_CMD look in VAR_CMD as well |
318 | * FIND_ENV look in the environment as well | | 318 | * FIND_ENV look in the environment as well |
319 | * | | 319 | * |
320 | * Results: | | 320 | * Results: |
321 | * A pointer to the structure describing the desired variable or | | 321 | * A pointer to the structure describing the desired variable or |
322 | * NULL if the variable does not exist. | | 322 | * NULL if the variable does not exist. |
323 | * | | 323 | * |
324 | * Side Effects: | | 324 | * Side Effects: |
325 | * None | | 325 | * None |
326 | *----------------------------------------------------------------------- | | 326 | *----------------------------------------------------------------------- |
327 | */ | | 327 | */ |
328 | static Var * | | 328 | static Var * |
329 | VarFind(const char *name, GNode *ctxt, VarFindFlags flags) | | 329 | VarFind(const char *name, GNode *ctxt, VarFindFlags flags) |
330 | { | | 330 | { |
331 | Hash_Entry *var; | | 331 | Hash_Entry *var; |
332 | Var *v; | | 332 | Var *v; |
333 | | | 333 | |
334 | /* | | 334 | /* |
335 | * If the variable name begins with a '.', it could very well be one of | | 335 | * If the variable name begins with a '.', it could very well be one of |
336 | * the local ones. We check the name against all the local variables | | 336 | * the local ones. We check the name against all the local variables |
337 | * and substitute the short version in for 'name' if it matches one of | | 337 | * and substitute the short version in for 'name' if it matches one of |
338 | * them. | | 338 | * them. |
339 | */ | | 339 | */ |
340 | if (*name == '.' && isupper((unsigned char) name[1])) { | | 340 | if (*name == '.' && isupper((unsigned char) name[1])) { |
341 | switch (name[1]) { | | 341 | switch (name[1]) { |
342 | case 'A': | | 342 | case 'A': |
343 | if (strcmp(name, ".ALLSRC") == 0) | | 343 | if (strcmp(name, ".ALLSRC") == 0) |
344 | name = ALLSRC; | | 344 | name = ALLSRC; |
345 | if (strcmp(name, ".ARCHIVE") == 0) | | 345 | if (strcmp(name, ".ARCHIVE") == 0) |
346 | name = ARCHIVE; | | 346 | name = ARCHIVE; |
347 | break; | | 347 | break; |
348 | case 'I': | | 348 | case 'I': |
349 | if (strcmp(name, ".IMPSRC") == 0) | | 349 | if (strcmp(name, ".IMPSRC") == 0) |
350 | name = IMPSRC; | | 350 | name = IMPSRC; |
351 | break; | | 351 | break; |
352 | case 'M': | | 352 | case 'M': |
353 | if (strcmp(name, ".MEMBER") == 0) | | 353 | if (strcmp(name, ".MEMBER") == 0) |
354 | name = MEMBER; | | 354 | name = MEMBER; |
355 | break; | | 355 | break; |
356 | case 'O': | | 356 | case 'O': |
357 | if (strcmp(name, ".OODATE") == 0) | | 357 | if (strcmp(name, ".OODATE") == 0) |
358 | name = OODATE; | | 358 | name = OODATE; |
359 | break; | | 359 | break; |
360 | case 'P': | | 360 | case 'P': |
361 | if (strcmp(name, ".PREFIX") == 0) | | 361 | if (strcmp(name, ".PREFIX") == 0) |
362 | name = PREFIX; | | 362 | name = PREFIX; |
363 | break; | | 363 | break; |
364 | case 'T': | | 364 | case 'T': |
365 | if (strcmp(name, ".TARGET") == 0) | | 365 | if (strcmp(name, ".TARGET") == 0) |
366 | name = TARGET; | | 366 | name = TARGET; |
367 | break; | | 367 | break; |
368 | } | | 368 | } |
369 | } | | 369 | } |
370 | | | 370 | |
371 | #ifdef notyet | | 371 | #ifdef notyet |
372 | /* for compatibility with gmake */ | | 372 | /* for compatibility with gmake */ |
373 | if (name[0] == '^' && name[1] == '\0') | | 373 | if (name[0] == '^' && name[1] == '\0') |
374 | name = ALLSRC; | | 374 | name = ALLSRC; |
375 | #endif | | 375 | #endif |
376 | | | 376 | |
377 | /* | | 377 | /* |
378 | * First look for the variable in the given context. If it's not there, | | 378 | * First look for the variable in the given context. If it's not there, |
379 | * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, | | 379 | * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, |
380 | * depending on the FIND_* flags in 'flags' | | 380 | * depending on the FIND_* flags in 'flags' |
381 | */ | | 381 | */ |
382 | var = Hash_FindEntry(&ctxt->context, name); | | 382 | var = Hash_FindEntry(&ctxt->context, name); |
383 | | | 383 | |
384 | if (var == NULL && (flags & FIND_CMD) && ctxt != VAR_CMD) { | | 384 | if (var == NULL && (flags & FIND_CMD) && ctxt != VAR_CMD) { |
385 | var = Hash_FindEntry(&VAR_CMD->context, name); | | 385 | var = Hash_FindEntry(&VAR_CMD->context, name); |
386 | } | | 386 | } |
387 | if (!checkEnvFirst && var == NULL && (flags & FIND_GLOBAL) && | | 387 | if (!checkEnvFirst && var == NULL && (flags & FIND_GLOBAL) && |
388 | ctxt != VAR_GLOBAL) | | 388 | ctxt != VAR_GLOBAL) |
389 | { | | 389 | { |
390 | var = Hash_FindEntry(&VAR_GLOBAL->context, name); | | 390 | var = Hash_FindEntry(&VAR_GLOBAL->context, name); |
391 | if (var == NULL && ctxt != VAR_INTERNAL) { | | 391 | if (var == NULL && ctxt != VAR_INTERNAL) { |
392 | /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ | | 392 | /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ |
393 | var = Hash_FindEntry(&VAR_INTERNAL->context, name); | | 393 | var = Hash_FindEntry(&VAR_INTERNAL->context, name); |
394 | } | | 394 | } |
395 | } | | 395 | } |
396 | if (var == NULL && (flags & FIND_ENV)) { | | 396 | if (var == NULL && (flags & FIND_ENV)) { |
397 | char *env; | | 397 | char *env; |
398 | | | 398 | |
399 | if ((env = getenv(name)) != NULL) { | | 399 | if ((env = getenv(name)) != NULL) { |
400 | int len; | | 400 | int len; |
401 | | | 401 | |
402 | v = bmake_malloc(sizeof(Var)); | | 402 | v = bmake_malloc(sizeof(Var)); |
403 | v->name = bmake_strdup(name); | | 403 | v->name = bmake_strdup(name); |
404 | | | 404 | |
405 | len = strlen(env); | | 405 | len = strlen(env); |
406 | | | 406 | |
407 | Buf_Init(&v->val, len + 1); | | 407 | Buf_Init(&v->val, len + 1); |
408 | Buf_AddBytes(&v->val, len, env); | | 408 | Buf_AddBytes(&v->val, len, env); |
409 | | | 409 | |
410 | v->flags = VAR_FROM_ENV; | | 410 | v->flags = VAR_FROM_ENV; |
411 | return v; | | 411 | return v; |
412 | } else if (checkEnvFirst && (flags & FIND_GLOBAL) && | | 412 | } else if (checkEnvFirst && (flags & FIND_GLOBAL) && |
413 | ctxt != VAR_GLOBAL) | | 413 | ctxt != VAR_GLOBAL) |
414 | { | | 414 | { |
415 | var = Hash_FindEntry(&VAR_GLOBAL->context, name); | | 415 | var = Hash_FindEntry(&VAR_GLOBAL->context, name); |
416 | if (var == NULL && ctxt != VAR_INTERNAL) { | | 416 | if (var == NULL && ctxt != VAR_INTERNAL) { |
417 | var = Hash_FindEntry(&VAR_INTERNAL->context, name); | | 417 | var = Hash_FindEntry(&VAR_INTERNAL->context, name); |
418 | } | | 418 | } |
419 | if (var == NULL) { | | 419 | if (var == NULL) { |
420 | return NULL; | | 420 | return NULL; |
421 | } else { | | 421 | } else { |
422 | return (Var *)Hash_GetValue(var); | | 422 | return (Var *)Hash_GetValue(var); |
423 | } | | 423 | } |
424 | } else { | | 424 | } else { |
425 | return NULL; | | 425 | return NULL; |
426 | } | | 426 | } |
427 | } else if (var == NULL) { | | 427 | } else if (var == NULL) { |
428 | return NULL; | | 428 | return NULL; |
429 | } else { | | 429 | } else { |
430 | return (Var *)Hash_GetValue(var); | | 430 | return (Var *)Hash_GetValue(var); |
431 | } | | 431 | } |
432 | } | | 432 | } |
433 | | | 433 | |
434 | /*- | | 434 | /*- |
435 | *----------------------------------------------------------------------- | | 435 | *----------------------------------------------------------------------- |
436 | * VarFreeEnv -- | | 436 | * VarFreeEnv -- |
437 | * If the variable is an environment variable, free it | | 437 | * If the variable is an environment variable, free it |
438 | * | | 438 | * |
439 | * Input: | | 439 | * Input: |
440 | * v the variable | | 440 | * v the variable |
441 | * destroy true if the value buffer should be destroyed. | | 441 | * destroy true if the value buffer should be destroyed. |
442 | * | | 442 | * |
443 | * Results: | | 443 | * Results: |
444 | * 1 if it is an environment variable 0 ow. | | 444 | * 1 if it is an environment variable 0 ow. |
445 | * | | 445 | * |
446 | * Side Effects: | | 446 | * Side Effects: |
447 | * The variable is free'ed if it is an environent variable. | | 447 | * The variable is free'ed if it is an environent variable. |
448 | *----------------------------------------------------------------------- | | 448 | *----------------------------------------------------------------------- |
449 | */ | | 449 | */ |
450 | static Boolean | | 450 | static Boolean |
451 | VarFreeEnv(Var *v, Boolean destroy) | | 451 | VarFreeEnv(Var *v, Boolean destroy) |
452 | { | | 452 | { |
453 | if (!(v->flags & VAR_FROM_ENV)) | | 453 | if (!(v->flags & VAR_FROM_ENV)) |
454 | return FALSE; | | 454 | return FALSE; |
455 | free(v->name); | | 455 | free(v->name); |
456 | Buf_Destroy(&v->val, destroy); | | 456 | Buf_Destroy(&v->val, destroy); |
457 | free(v); | | 457 | free(v); |
458 | return TRUE; | | 458 | return TRUE; |
459 | } | | 459 | } |
460 | | | 460 | |
461 | /*- | | 461 | /*- |
462 | *----------------------------------------------------------------------- | | 462 | *----------------------------------------------------------------------- |
463 | * VarAdd -- | | 463 | * VarAdd -- |
464 | * Add a new variable of name name and value val to the given context | | 464 | * Add a new variable of name name and value val to the given context |
465 | * | | 465 | * |
466 | * Input: | | 466 | * Input: |
467 | * name name of variable to add | | 467 | * name name of variable to add |
468 | * val value to set it to | | 468 | * val value to set it to |
469 | * ctxt context in which to set it | | 469 | * ctxt context in which to set it |
470 | * | | 470 | * |
471 | * Side Effects: | | 471 | * Side Effects: |
472 | * The new variable is placed at the front of the given context | | 472 | * The new variable is placed at the front of the given context |
473 | * The name and val arguments are duplicated so they may | | 473 | * The name and val arguments are duplicated so they may |
474 | * safely be freed. | | 474 | * safely be freed. |
475 | *----------------------------------------------------------------------- | | 475 | *----------------------------------------------------------------------- |
476 | */ | | 476 | */ |
477 | static void | | 477 | static void |
478 | VarAdd(const char *name, const char *val, GNode *ctxt) | | 478 | VarAdd(const char *name, const char *val, GNode *ctxt) |
479 | { | | 479 | { |
480 | Var *v; | | 480 | Var *v; |
481 | int len; | | 481 | int len; |
482 | Hash_Entry *h; | | 482 | Hash_Entry *h; |
483 | | | 483 | |
484 | v = bmake_malloc(sizeof(Var)); | | 484 | v = bmake_malloc(sizeof(Var)); |
485 | | | 485 | |
486 | len = val != NULL ? strlen(val) : 0; | | 486 | len = val != NULL ? strlen(val) : 0; |
487 | Buf_Init(&v->val, len + 1); | | 487 | Buf_Init(&v->val, len + 1); |
488 | Buf_AddBytes(&v->val, len, val); | | 488 | Buf_AddBytes(&v->val, len, val); |
489 | | | 489 | |
490 | v->flags = 0; | | 490 | v->flags = 0; |
491 | | | 491 | |
492 | h = Hash_CreateEntry(&ctxt->context, name, NULL); | | 492 | h = Hash_CreateEntry(&ctxt->context, name, NULL); |
493 | Hash_SetValue(h, v); | | 493 | Hash_SetValue(h, v); |
494 | v->name = h->name; | | 494 | v->name = h->name; |
495 | if (DEBUG(VAR) && !(ctxt->flags & INTERNAL)) { | | 495 | if (DEBUG(VAR) && !(ctxt->flags & INTERNAL)) { |
496 | fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); | | 496 | fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); |
497 | } | | 497 | } |
498 | } | | 498 | } |
499 | | | 499 | |
500 | /*- | | 500 | /*- |
501 | *----------------------------------------------------------------------- | | 501 | *----------------------------------------------------------------------- |
502 | * Var_Delete -- | | 502 | * Var_Delete -- |
503 | * Remove a variable from a context. | | 503 | * Remove a variable from a context. |
504 | * | | 504 | * |
505 | * Side Effects: | | 505 | * Side Effects: |
506 | * The Var structure is removed and freed. | | 506 | * The Var structure is removed and freed. |
507 | * | | 507 | * |
508 | *----------------------------------------------------------------------- | | 508 | *----------------------------------------------------------------------- |
509 | */ | | 509 | */ |
510 | void | | 510 | void |
511 | Var_Delete(const char *name, GNode *ctxt) | | 511 | Var_Delete(const char *name, GNode *ctxt) |
512 | { | | 512 | { |
513 | Hash_Entry *ln; | | 513 | Hash_Entry *ln; |
514 | char *cp; | | 514 | char *cp; |
515 | | | 515 | |
516 | if (strchr(name, '$') != NULL) { | | 516 | if (strchr(name, '$') != NULL) { |
517 | cp = Var_Subst(NULL, name, VAR_GLOBAL, VARE_WANTRES); | | 517 | cp = Var_Subst(NULL, name, VAR_GLOBAL, VARE_WANTRES); |
518 | } else { | | 518 | } else { |
519 | cp = UNCONST(name); | | 519 | cp = UNCONST(name); |
520 | } | | 520 | } |
521 | ln = Hash_FindEntry(&ctxt->context, cp); | | 521 | ln = Hash_FindEntry(&ctxt->context, cp); |
522 | if (DEBUG(VAR)) { | | 522 | if (DEBUG(VAR)) { |
523 | fprintf(debug_file, "%s:delete %s%s\n", | | 523 | fprintf(debug_file, "%s:delete %s%s\n", |
524 | ctxt->name, cp, ln ? "" : " (not found)"); | | 524 | ctxt->name, cp, ln ? "" : " (not found)"); |
525 | } | | 525 | } |
526 | if (cp != name) | | 526 | if (cp != name) |
527 | free(cp); | | 527 | free(cp); |
528 | if (ln != NULL) { | | 528 | if (ln != NULL) { |
529 | Var *v = (Var *)Hash_GetValue(ln); | | 529 | Var *v = (Var *)Hash_GetValue(ln); |
530 | if (v->flags & VAR_EXPORTED) { | | 530 | if (v->flags & VAR_EXPORTED) { |
531 | unsetenv(v->name); | | 531 | unsetenv(v->name); |
532 | } | | 532 | } |
533 | if (strcmp(MAKE_EXPORTED, v->name) == 0) { | | 533 | if (strcmp(MAKE_EXPORTED, v->name) == 0) { |
534 | var_exportedVars = VAR_EXPORTED_NONE; | | 534 | var_exportedVars = VAR_EXPORTED_NONE; |
535 | } | | 535 | } |
536 | if (v->name != ln->name) | | 536 | if (v->name != ln->name) |
537 | free(v->name); | | 537 | free(v->name); |
538 | Hash_DeleteEntry(&ctxt->context, ln); | | 538 | Hash_DeleteEntry(&ctxt->context, ln); |
539 | Buf_Destroy(&v->val, TRUE); | | 539 | Buf_Destroy(&v->val, TRUE); |
540 | free(v); | | 540 | free(v); |
541 | } | | 541 | } |
542 | } | | 542 | } |
543 | | | 543 | |
544 | | | 544 | |
545 | /* | | 545 | /* |
546 | * Export a var. | | 546 | * Export a var. |
547 | * We ignore make internal variables (those which start with '.') | | 547 | * We ignore make internal variables (those which start with '.') |
548 | * Also we jump through some hoops to avoid calling setenv | | 548 | * Also we jump through some hoops to avoid calling setenv |
549 | * more than necessary since it can leak. | | 549 | * more than necessary since it can leak. |
550 | * We only manipulate flags of vars if 'parent' is set. | | 550 | * We only manipulate flags of vars if 'parent' is set. |
551 | */ | | 551 | */ |
552 | static int | | 552 | static int |
553 | Var_Export1(const char *name, int flags) | | 553 | Var_Export1(const char *name, int flags) |
554 | { | | 554 | { |
555 | char tmp[BUFSIZ]; | | 555 | char tmp[BUFSIZ]; |
556 | Var *v; | | 556 | Var *v; |
557 | char *val = NULL; | | 557 | char *val = NULL; |
558 | int n; | | 558 | int n; |
559 | int parent = (flags & VAR_EXPORT_PARENT); | | 559 | int parent = (flags & VAR_EXPORT_PARENT); |
560 | | | 560 | |
561 | if (*name == '.') | | 561 | if (*name == '.') |
562 | return 0; /* skip internals */ | | 562 | return 0; /* skip internals */ |
563 | if (!name[1]) { | | 563 | if (!name[1]) { |
564 | /* | | 564 | /* |
565 | * A single char. | | 565 | * A single char. |
566 | * If it is one of the vars that should only appear in | | 566 | * If it is one of the vars that should only appear in |
567 | * local context, skip it, else we can get Var_Subst | | 567 | * local context, skip it, else we can get Var_Subst |
568 | * into a loop. | | 568 | * into a loop. |
569 | */ | | 569 | */ |
570 | switch (name[0]) { | | 570 | switch (name[0]) { |
571 | case '@': | | 571 | case '@': |
572 | case '%': | | 572 | case '%': |
573 | case '*': | | 573 | case '*': |
574 | case '!': | | 574 | case '!': |
575 | return 0; | | 575 | return 0; |
576 | } | | 576 | } |
577 | } | | 577 | } |
578 | v = VarFind(name, VAR_GLOBAL, 0); | | 578 | v = VarFind(name, VAR_GLOBAL, 0); |
579 | if (v == NULL) | | 579 | if (v == NULL) |
580 | return 0; | | 580 | return 0; |
581 | if (!parent && | | 581 | if (!parent && |
582 | (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { | | 582 | (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { |
583 | return 0; /* nothing to do */ | | 583 | return 0; /* nothing to do */ |
584 | } | | 584 | } |
585 | val = Buf_GetAll(&v->val, NULL); | | 585 | val = Buf_GetAll(&v->val, NULL); |
586 | if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) { | | 586 | if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) { |
587 | if (parent) { | | 587 | if (parent) { |
588 | /* | | 588 | /* |
589 | * Flag this as something we need to re-export. | | 589 | * Flag this as something we need to re-export. |
590 | * No point actually exporting it now though, | | 590 | * No point actually exporting it now though, |
591 | * the child can do it at the last minute. | | 591 | * the child can do it at the last minute. |
592 | */ | | 592 | */ |
593 | v->flags |= (VAR_EXPORTED|VAR_REEXPORT); | | 593 | v->flags |= (VAR_EXPORTED|VAR_REEXPORT); |
594 | return 1; | | 594 | return 1; |
595 | } | | 595 | } |
596 | if (v->flags & VAR_IN_USE) { | | 596 | if (v->flags & VAR_IN_USE) { |
597 | /* | | 597 | /* |
598 | * We recursed while exporting in a child. | | 598 | * We recursed while exporting in a child. |
599 | * This isn't going to end well, just skip it. | | 599 | * This isn't going to end well, just skip it. |
600 | */ | | 600 | */ |
601 | return 0; | | 601 | return 0; |
602 | } | | 602 | } |
603 | n = snprintf(tmp, sizeof(tmp), "${%s}", name); | | 603 | n = snprintf(tmp, sizeof(tmp), "${%s}", name); |
604 | if (n < (int)sizeof(tmp)) { | | 604 | if (n < (int)sizeof(tmp)) { |
605 | val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARE_WANTRES); | | 605 | val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARE_WANTRES); |
606 | setenv(name, val, 1); | | 606 | setenv(name, val, 1); |
607 | free(val); | | 607 | free(val); |
608 | } | | 608 | } |
609 | } else { | | 609 | } else { |
610 | if (parent) { | | 610 | if (parent) { |
611 | v->flags &= ~VAR_REEXPORT; /* once will do */ | | 611 | v->flags &= ~VAR_REEXPORT; /* once will do */ |
612 | } | | 612 | } |
613 | if (parent || !(v->flags & VAR_EXPORTED)) { | | 613 | if (parent || !(v->flags & VAR_EXPORTED)) { |
614 | setenv(name, val, 1); | | 614 | setenv(name, val, 1); |
615 | } | | 615 | } |
616 | } | | 616 | } |
617 | /* | | 617 | /* |
618 | * This is so Var_Set knows to call Var_Export again... | | 618 | * This is so Var_Set knows to call Var_Export again... |
619 | */ | | 619 | */ |
620 | if (parent) { | | 620 | if (parent) { |
621 | v->flags |= VAR_EXPORTED; | | 621 | v->flags |= VAR_EXPORTED; |
622 | } | | 622 | } |
623 | return 1; | | 623 | return 1; |
624 | } | | 624 | } |
625 | | | 625 | |
626 | static void | | 626 | static void |
627 | Var_ExportVars_callback(void *entry, void *unused MAKE_ATTR_UNUSED) | | 627 | Var_ExportVars_callback(void *entry, void *unused MAKE_ATTR_UNUSED) |
628 | { | | 628 | { |
629 | Var *var = entry; | | 629 | Var *var = entry; |
630 | Var_Export1(var->name, 0); | | 630 | Var_Export1(var->name, 0); |
631 | } | | 631 | } |
632 | | | 632 | |
633 | /* | | 633 | /* |
634 | * This gets called from our children. | | 634 | * This gets called from our children. |
635 | */ | | 635 | */ |
636 | void | | 636 | void |
637 | Var_ExportVars(void) | | 637 | Var_ExportVars(void) |
638 | { | | 638 | { |
639 | char tmp[BUFSIZ]; | | 639 | char tmp[BUFSIZ]; |
640 | char *val; | | 640 | char *val; |
641 | int n; | | 641 | int n; |
642 | | | 642 | |
643 | /* | | 643 | /* |
644 | * Several make's support this sort of mechanism for tracking | | 644 | * Several make's support this sort of mechanism for tracking |
645 | * recursion - but each uses a different name. | | 645 | * recursion - but each uses a different name. |
646 | * We allow the makefiles to update MAKELEVEL and ensure | | 646 | * We allow the makefiles to update MAKELEVEL and ensure |
647 | * children see a correctly incremented value. | | 647 | * children see a correctly incremented value. |
648 | */ | | 648 | */ |
649 | snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); | | 649 | snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); |
650 | setenv(MAKE_LEVEL_ENV, tmp, 1); | | 650 | setenv(MAKE_LEVEL_ENV, tmp, 1); |
651 | | | 651 | |
652 | if (VAR_EXPORTED_NONE == var_exportedVars) | | 652 | if (VAR_EXPORTED_NONE == var_exportedVars) |
653 | return; | | 653 | return; |
654 | | | 654 | |
655 | if (VAR_EXPORTED_ALL == var_exportedVars) { | | 655 | if (VAR_EXPORTED_ALL == var_exportedVars) { |
656 | /* Ouch! This is crazy... */ | | 656 | /* Ouch! This is crazy... */ |
657 | Hash_ForEach(&VAR_GLOBAL->context, Var_ExportVars_callback, NULL); | | 657 | Hash_ForEach(&VAR_GLOBAL->context, Var_ExportVars_callback, NULL); |
658 | return; | | 658 | return; |
659 | } | | 659 | } |
660 | /* | | 660 | /* |
661 | * We have a number of exported vars, | | 661 | * We have a number of exported vars, |
662 | */ | | 662 | */ |
663 | n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); | | 663 | n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); |
664 | if (n < (int)sizeof(tmp)) { | | 664 | if (n < (int)sizeof(tmp)) { |
665 | char **av; | | 665 | char **av; |
666 | char *as; | | 666 | char *as; |
667 | int ac; | | 667 | int ac; |
668 | int i; | | 668 | int i; |
669 | | | 669 | |
670 | val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARE_WANTRES); | | 670 | val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARE_WANTRES); |
671 | if (*val) { | | 671 | if (*val) { |
672 | av = brk_string(val, &ac, FALSE, &as); | | 672 | av = brk_string(val, &ac, FALSE, &as); |
673 | for (i = 0; i < ac; i++) | | 673 | for (i = 0; i < ac; i++) |
674 | Var_Export1(av[i], 0); | | 674 | Var_Export1(av[i], 0); |
675 | free(as); | | 675 | free(as); |
676 | free(av); | | 676 | free(av); |
677 | } | | 677 | } |
678 | free(val); | | 678 | free(val); |
679 | } | | 679 | } |
680 | } | | 680 | } |
681 | | | 681 | |
682 | /* | | 682 | /* |
683 | * This is called when .export is seen or | | 683 | * This is called when .export is seen or |
684 | * .MAKE.EXPORTED is modified. | | 684 | * .MAKE.EXPORTED is modified. |
685 | * It is also called when any exported var is modified. | | 685 | * It is also called when any exported var is modified. |
686 | */ | | 686 | */ |
687 | void | | 687 | void |
688 | Var_Export(char *str, int isExport) | | 688 | Var_Export(char *str, int isExport) |
689 | { | | 689 | { |
690 | char *name; | | 690 | char *name; |
691 | char *val; | | 691 | char *val; |
692 | char **av; | | 692 | char **av; |
693 | char *as; | | 693 | char *as; |
694 | int flags; | | 694 | int flags; |
695 | int ac; | | 695 | int ac; |
696 | int i; | | 696 | int i; |
697 | | | 697 | |
698 | if (isExport && (!str || !str[0])) { | | 698 | if (isExport && (!str || !str[0])) { |
699 | var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ | | 699 | var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ |
700 | return; | | 700 | return; |
701 | } | | 701 | } |
702 | | | 702 | |
703 | flags = 0; | | 703 | flags = 0; |
704 | if (strncmp(str, "-env", 4) == 0) { | | 704 | if (strncmp(str, "-env", 4) == 0) { |
705 | str += 4; | | 705 | str += 4; |
706 | } else if (strncmp(str, "-literal", 8) == 0) { | | 706 | } else if (strncmp(str, "-literal", 8) == 0) { |
707 | str += 8; | | 707 | str += 8; |
708 | flags |= VAR_EXPORT_LITERAL; | | 708 | flags |= VAR_EXPORT_LITERAL; |
709 | } else { | | 709 | } else { |
710 | flags |= VAR_EXPORT_PARENT; | | 710 | flags |= VAR_EXPORT_PARENT; |
711 | } | | 711 | } |
712 | val = Var_Subst(NULL, str, VAR_GLOBAL, VARE_WANTRES); | | 712 | val = Var_Subst(NULL, str, VAR_GLOBAL, VARE_WANTRES); |
713 | if (*val) { | | 713 | if (*val) { |
714 | av = brk_string(val, &ac, FALSE, &as); | | 714 | av = brk_string(val, &ac, FALSE, &as); |
715 | for (i = 0; i < ac; i++) { | | 715 | for (i = 0; i < ac; i++) { |
716 | name = av[i]; | | 716 | name = av[i]; |
717 | if (!name[1]) { | | 717 | if (!name[1]) { |
718 | /* | | 718 | /* |
719 | * A single char. | | 719 | * A single char. |
720 | * If it is one of the vars that should only appear in | | 720 | * If it is one of the vars that should only appear in |
721 | * local context, skip it, else we can get Var_Subst | | 721 | * local context, skip it, else we can get Var_Subst |
722 | * into a loop. | | 722 | * into a loop. |
723 | */ | | 723 | */ |
724 | switch (name[0]) { | | 724 | switch (name[0]) { |
725 | case '@': | | 725 | case '@': |
726 | case '%': | | 726 | case '%': |
727 | case '*': | | 727 | case '*': |
728 | case '!': | | 728 | case '!': |
729 | continue; | | 729 | continue; |
730 | } | | 730 | } |
731 | } | | 731 | } |
732 | if (Var_Export1(name, flags)) { | | 732 | if (Var_Export1(name, flags)) { |
733 | if (VAR_EXPORTED_ALL != var_exportedVars) | | 733 | if (VAR_EXPORTED_ALL != var_exportedVars) |
734 | var_exportedVars = VAR_EXPORTED_YES; | | 734 | var_exportedVars = VAR_EXPORTED_YES; |
735 | if (isExport && (flags & VAR_EXPORT_PARENT)) { | | 735 | if (isExport && (flags & VAR_EXPORT_PARENT)) { |
736 | Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL); | | 736 | Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL); |
737 | } | | 737 | } |
738 | } | | 738 | } |
739 | } | | 739 | } |
740 | free(as); | | 740 | free(as); |
741 | free(av); | | 741 | free(av); |
742 | } | | 742 | } |
743 | free(val); | | 743 | free(val); |
744 | } | | 744 | } |
745 | | | 745 | |
746 | | | 746 | |
747 | extern char **environ; | | 747 | extern char **environ; |
748 | | | 748 | |
749 | /* | | 749 | /* |
750 | * This is called when .unexport[-env] is seen. | | 750 | * This is called when .unexport[-env] is seen. |
751 | */ | | 751 | */ |
752 | void | | 752 | void |
753 | Var_UnExport(char *str) | | 753 | Var_UnExport(char *str) |
754 | { | | 754 | { |
755 | char tmp[BUFSIZ]; | | 755 | char tmp[BUFSIZ]; |
756 | char *vlist; | | 756 | char *vlist; |
757 | char *cp; | | 757 | char *cp; |
758 | Boolean unexport_env; | | 758 | Boolean unexport_env; |
759 | int n; | | 759 | int n; |
760 | | | 760 | |
761 | if (str == NULL || str[0] == '\0') | | 761 | if (str == NULL || str[0] == '\0') |
762 | return; /* assert? */ | | 762 | return; /* assert? */ |
763 | | | 763 | |
764 | vlist = NULL; | | 764 | vlist = NULL; |
765 | | | 765 | |
766 | str += 8; | | 766 | str += 8; |
767 | unexport_env = (strncmp(str, "-env", 4) == 0); | | 767 | unexport_env = (strncmp(str, "-env", 4) == 0); |
768 | if (unexport_env) { | | 768 | if (unexport_env) { |
769 | char **newenv; | | 769 | char **newenv; |
770 | | | 770 | |
771 | cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ | | 771 | cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ |
772 | if (environ == savedEnv) { | | 772 | if (environ == savedEnv) { |
773 | /* we have been here before! */ | | 773 | /* we have been here before! */ |
774 | newenv = bmake_realloc(environ, 2 * sizeof(char *)); | | 774 | newenv = bmake_realloc(environ, 2 * sizeof(char *)); |
775 | } else { | | 775 | } else { |
776 | if (savedEnv) { | | 776 | if (savedEnv) { |
777 | free(savedEnv); | | 777 | free(savedEnv); |
778 | savedEnv = NULL; | | 778 | savedEnv = NULL; |
779 | } | | 779 | } |
780 | newenv = bmake_malloc(2 * sizeof(char *)); | | 780 | newenv = bmake_malloc(2 * sizeof(char *)); |
781 | } | | 781 | } |
782 | if (!newenv) | | 782 | if (!newenv) |
783 | return; | | 783 | return; |
784 | /* Note: we cannot safely free() the original environ. */ | | 784 | /* Note: we cannot safely free() the original environ. */ |
785 | environ = savedEnv = newenv; | | 785 | environ = savedEnv = newenv; |
786 | newenv[0] = NULL; | | 786 | newenv[0] = NULL; |
787 | newenv[1] = NULL; | | 787 | newenv[1] = NULL; |
788 | if (cp && *cp) | | 788 | if (cp && *cp) |
789 | setenv(MAKE_LEVEL_ENV, cp, 1); | | 789 | setenv(MAKE_LEVEL_ENV, cp, 1); |
790 | } else { | | 790 | } else { |
791 | for (; *str != '\n' && isspace((unsigned char) *str); str++) | | 791 | for (; *str != '\n' && isspace((unsigned char) *str); str++) |
792 | continue; | | 792 | continue; |
793 | if (str[0] && str[0] != '\n') { | | 793 | if (str[0] && str[0] != '\n') { |
794 | vlist = str; | | 794 | vlist = str; |
795 | } | | 795 | } |
796 | } | | 796 | } |
797 | | | 797 | |
798 | if (!vlist) { | | 798 | if (!vlist) { |
799 | /* Using .MAKE.EXPORTED */ | | 799 | /* Using .MAKE.EXPORTED */ |
800 | vlist = Var_Subst(NULL, "${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, | | 800 | vlist = Var_Subst(NULL, "${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, |
801 | VARE_WANTRES); | | 801 | VARE_WANTRES); |
802 | } | | 802 | } |
803 | if (vlist) { | | 803 | if (vlist) { |
804 | Var *v; | | 804 | Var *v; |
805 | char **av; | | 805 | char **av; |
806 | char *as; | | 806 | char *as; |
807 | int ac; | | 807 | int ac; |
808 | int i; | | 808 | int i; |
809 | | | 809 | |
810 | av = brk_string(vlist, &ac, FALSE, &as); | | 810 | av = brk_string(vlist, &ac, FALSE, &as); |
811 | for (i = 0; i < ac; i++) { | | 811 | for (i = 0; i < ac; i++) { |
812 | v = VarFind(av[i], VAR_GLOBAL, 0); | | 812 | v = VarFind(av[i], VAR_GLOBAL, 0); |
813 | if (!v) | | 813 | if (!v) |
814 | continue; | | 814 | continue; |
815 | if (!unexport_env && | | 815 | if (!unexport_env && |
816 | (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) | | 816 | (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) |
817 | { | | 817 | { |
818 | unsetenv(v->name); | | 818 | unsetenv(v->name); |
819 | } | | 819 | } |
820 | v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT); | | 820 | v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT); |
821 | /* | | 821 | /* |
822 | * If we are unexporting a list, | | 822 | * If we are unexporting a list, |
823 | * remove each one from .MAKE.EXPORTED. | | 823 | * remove each one from .MAKE.EXPORTED. |
824 | * If we are removing them all, | | 824 | * If we are removing them all, |
825 | * just delete .MAKE.EXPORTED below. | | 825 | * just delete .MAKE.EXPORTED below. |
826 | */ | | 826 | */ |
827 | if (vlist == str) { | | 827 | if (vlist == str) { |
828 | n = snprintf(tmp, sizeof(tmp), | | 828 | n = snprintf(tmp, sizeof(tmp), |
829 | "${" MAKE_EXPORTED ":N%s}", v->name); | | 829 | "${" MAKE_EXPORTED ":N%s}", v->name); |
830 | if (n < (int)sizeof(tmp)) { | | 830 | if (n < (int)sizeof(tmp)) { |
831 | cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARE_WANTRES); | | 831 | cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARE_WANTRES); |
832 | Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL); | | 832 | Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL); |
833 | free(cp); | | 833 | free(cp); |
834 | } | | 834 | } |
835 | } | | 835 | } |
836 | } | | 836 | } |
837 | free(as); | | 837 | free(as); |
838 | free(av); | | 838 | free(av); |
839 | if (vlist != str) { | | 839 | if (vlist != str) { |
840 | Var_Delete(MAKE_EXPORTED, VAR_GLOBAL); | | 840 | Var_Delete(MAKE_EXPORTED, VAR_GLOBAL); |
841 | free(vlist); | | 841 | free(vlist); |
842 | } | | 842 | } |
843 | } | | 843 | } |
844 | } | | 844 | } |
845 | | | 845 | |
846 | static void | | 846 | static void |
847 | Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, | | 847 | Var_Set_with_flags(const char *name, const char *val, GNode *ctxt, |
848 | VarSet_Flags flags) | | 848 | VarSet_Flags flags) |
849 | { | | 849 | { |
850 | Var *v; | | 850 | Var *v; |
851 | char *expanded_name = NULL; | | 851 | char *expanded_name = NULL; |
852 | | | 852 | |
853 | /* | | 853 | /* |
854 | * We only look for a variable in the given context since anything set | | 854 | * We only look for a variable in the given context since anything set |
855 | * here will override anything in a lower context, so there's not much | | 855 | * here will override anything in a lower context, so there's not much |
856 | * point in searching them all just to save a bit of memory... | | 856 | * point in searching them all just to save a bit of memory... |
857 | */ | | 857 | */ |
858 | if (strchr(name, '$') != NULL) { | | 858 | if (strchr(name, '$') != NULL) { |
859 | expanded_name = Var_Subst(NULL, name, ctxt, VARE_WANTRES); | | 859 | expanded_name = Var_Subst(NULL, name, ctxt, VARE_WANTRES); |
860 | if (expanded_name[0] == '\0') { | | 860 | if (expanded_name[0] == '\0') { |
861 | if (DEBUG(VAR)) { | | 861 | if (DEBUG(VAR)) { |
862 | fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) " | | 862 | fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) " |
863 | "name expands to empty string - ignored\n", | | 863 | "name expands to empty string - ignored\n", |
864 | name, val); | | 864 | name, val); |
865 | } | | 865 | } |
866 | free(expanded_name); | | 866 | free(expanded_name); |
867 | return; | | 867 | return; |
868 | } | | 868 | } |
869 | name = expanded_name; | | 869 | name = expanded_name; |
870 | } | | 870 | } |
871 | if (ctxt == VAR_GLOBAL) { | | 871 | if (ctxt == VAR_GLOBAL) { |
872 | v = VarFind(name, VAR_CMD, 0); | | 872 | v = VarFind(name, VAR_CMD, 0); |
873 | if (v != NULL) { | | 873 | if (v != NULL) { |
874 | if ((v->flags & VAR_FROM_CMD)) { | | 874 | if ((v->flags & VAR_FROM_CMD)) { |
875 | if (DEBUG(VAR)) { | | 875 | if (DEBUG(VAR)) { |
876 | fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val); | | 876 | fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val); |
877 | } | | 877 | } |
878 | goto out; | | 878 | goto out; |
879 | } | | 879 | } |
880 | VarFreeEnv(v, TRUE); | | 880 | VarFreeEnv(v, TRUE); |
881 | } | | 881 | } |
882 | } | | 882 | } |
883 | v = VarFind(name, ctxt, 0); | | 883 | v = VarFind(name, ctxt, 0); |
884 | if (v == NULL) { | | 884 | if (v == NULL) { |
885 | if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { | | 885 | if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { |
886 | /* | | 886 | /* |
887 | * This var would normally prevent the same name being added | | 887 | * This var would normally prevent the same name being added |
888 | * to VAR_GLOBAL, so delete it from there if needed. | | 888 | * to VAR_GLOBAL, so delete it from there if needed. |
889 | * Otherwise -V name may show the wrong value. | | 889 | * Otherwise -V name may show the wrong value. |
890 | */ | | 890 | */ |
891 | Var_Delete(name, VAR_GLOBAL); | | 891 | Var_Delete(name, VAR_GLOBAL); |
892 | } | | 892 | } |
893 | VarAdd(name, val, ctxt); | | 893 | VarAdd(name, val, ctxt); |
894 | } else { | | 894 | } else { |
895 | Buf_Empty(&v->val); | | 895 | Buf_Empty(&v->val); |
896 | if (val) | | 896 | if (val) |
897 | Buf_AddBytes(&v->val, strlen(val), val); | | 897 | Buf_AddBytes(&v->val, strlen(val), val); |
898 | | | 898 | |
899 | if (DEBUG(VAR)) { | | 899 | if (DEBUG(VAR)) { |
900 | fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); | | 900 | fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); |
901 | } | | 901 | } |
902 | if ((v->flags & VAR_EXPORTED)) { | | 902 | if ((v->flags & VAR_EXPORTED)) { |
903 | Var_Export1(name, VAR_EXPORT_PARENT); | | 903 | Var_Export1(name, VAR_EXPORT_PARENT); |
904 | } | | 904 | } |
905 | } | | 905 | } |
906 | /* | | 906 | /* |
907 | * Any variables given on the command line are automatically exported | | 907 | * Any variables given on the command line are automatically exported |
908 | * to the environment (as per POSIX standard) | | 908 | * to the environment (as per POSIX standard) |
909 | */ | | 909 | */ |
910 | if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { | | 910 | if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { |
911 | if (v == NULL) { | | 911 | if (v == NULL) { |
912 | /* we just added it */ | | 912 | /* we just added it */ |
913 | v = VarFind(name, ctxt, 0); | | 913 | v = VarFind(name, ctxt, 0); |
914 | } | | 914 | } |
915 | if (v != NULL) | | 915 | if (v != NULL) |
916 | v->flags |= VAR_FROM_CMD; | | 916 | v->flags |= VAR_FROM_CMD; |
917 | /* | | 917 | /* |
918 | * If requested, don't export these in the environment | | 918 | * If requested, don't export these in the environment |
919 | * individually. We still put them in MAKEOVERRIDES so | | 919 | * individually. We still put them in MAKEOVERRIDES so |
920 | * that the command-line settings continue to override | | 920 | * that the command-line settings continue to override |
921 | * Makefile settings. | | 921 | * Makefile settings. |
922 | */ | | 922 | */ |
923 | if (varNoExportEnv != TRUE) | | 923 | if (varNoExportEnv != TRUE) |
924 | setenv(name, val ? val : "", 1); | | 924 | setenv(name, val ? val : "", 1); |
925 | | | 925 | |
926 | Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL); | | 926 | Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL); |
927 | } | | 927 | } |
928 | if (*name == '.') { | | 928 | if (*name == '.') { |
929 | if (strcmp(name, SAVE_DOLLARS) == 0) | | 929 | if (strcmp(name, SAVE_DOLLARS) == 0) |
930 | save_dollars = s2Boolean(val, save_dollars); | | 930 | save_dollars = s2Boolean(val, save_dollars); |
931 | } | | 931 | } |
932 | | | 932 | |
933 | out: | | 933 | out: |
934 | free(expanded_name); | | 934 | free(expanded_name); |
935 | if (v != NULL) | | 935 | if (v != NULL) |
936 | VarFreeEnv(v, TRUE); | | 936 | VarFreeEnv(v, TRUE); |
937 | } | | 937 | } |
938 | | | 938 | |
939 | /*- | | 939 | /*- |
940 | *----------------------------------------------------------------------- | | 940 | *----------------------------------------------------------------------- |
941 | * Var_Set -- | | 941 | * Var_Set -- |
942 | * Set the variable name to the value val in the given context. | | 942 | * Set the variable name to the value val in the given context. |
943 | * | | 943 | * |
944 | * Input: | | 944 | * Input: |
945 | * name name of variable to set | | 945 | * name name of variable to set |
946 | * val value to give to the variable | | 946 | * val value to give to the variable |
947 | * ctxt context in which to set it | | 947 | * ctxt context in which to set it |
948 | * | | 948 | * |
949 | * Side Effects: | | 949 | * Side Effects: |
950 | * If the variable doesn't yet exist, a new record is created for it. | | 950 | * If the variable doesn't yet exist, a new record is created for it. |
951 | * Else the old value is freed and the new one stuck in its place | | 951 | * Else the old value is freed and the new one stuck in its place |
952 | * | | 952 | * |
953 | * Notes: | | 953 | * Notes: |
954 | * The variable is searched for only in its context before being | | 954 | * The variable is searched for only in its context before being |
955 | * created in that context. I.e. if the context is VAR_GLOBAL, | | 955 | * created in that context. I.e. if the context is VAR_GLOBAL, |
956 | * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only | | 956 | * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only |
957 | * VAR_CMD->context is searched. This is done to avoid the literally | | 957 | * VAR_CMD->context is searched. This is done to avoid the literally |
958 | * thousands of unnecessary strcmp's that used to be done to | | 958 | * thousands of unnecessary strcmp's that used to be done to |
959 | * set, say, $(@) or $(<). | | 959 | * set, say, $(@) or $(<). |
960 | * If the context is VAR_GLOBAL though, we check if the variable | | 960 | * If the context is VAR_GLOBAL though, we check if the variable |
961 | * was set in VAR_CMD from the command line and skip it if so. | | 961 | * was set in VAR_CMD from the command line and skip it if so. |
962 | *----------------------------------------------------------------------- | | 962 | *----------------------------------------------------------------------- |
963 | */ | | 963 | */ |
964 | void | | 964 | void |
965 | Var_Set(const char *name, const char *val, GNode *ctxt) | | 965 | Var_Set(const char *name, const char *val, GNode *ctxt) |
966 | { | | 966 | { |
967 | Var_Set_with_flags(name, val, ctxt, 0); | | 967 | Var_Set_with_flags(name, val, ctxt, 0); |
968 | } | | 968 | } |
969 | | | 969 | |
970 | /*- | | 970 | /*- |
971 | *----------------------------------------------------------------------- | | 971 | *----------------------------------------------------------------------- |
972 | * Var_Append -- | | 972 | * Var_Append -- |
973 | * The variable of the given name has the given value appended to it in | | 973 | * The variable of the given name has the given value appended to it in |
974 | * the given context. | | 974 | * the given context. |
975 | * | | 975 | * |
976 | * Input: | | 976 | * Input: |
977 | * name name of variable to modify | | 977 | * name name of variable to modify |
978 | * val String to append to it | | 978 | * val String to append to it |
979 | * ctxt Context in which this should occur | | 979 | * ctxt Context in which this should occur |
980 | * | | 980 | * |
981 | * Side Effects: | | 981 | * Side Effects: |
982 | * If the variable doesn't exist, it is created. Else the strings | | 982 | * If the variable doesn't exist, it is created. Else the strings |
983 | * are concatenated (with a space in between). | | 983 | * are concatenated (with a space in between). |
984 | * | | 984 | * |
985 | * Notes: | | 985 | * Notes: |
986 | * Only if the variable is being sought in the global context is the | | 986 | * Only if the variable is being sought in the global context is the |
987 | * environment searched. | | 987 | * environment searched. |
988 | * XXX: Knows its calling circumstances in that if called with ctxt | | 988 | * XXX: Knows its calling circumstances in that if called with ctxt |
989 | * an actual target, it will only search that context since only | | 989 | * an actual target, it will only search that context since only |
990 | * a local variable could be being appended to. This is actually | | 990 | * a local variable could be being appended to. This is actually |
991 | * a big win and must be tolerated. | | 991 | * a big win and must be tolerated. |
992 | *----------------------------------------------------------------------- | | 992 | *----------------------------------------------------------------------- |
993 | */ | | 993 | */ |
994 | void | | 994 | void |
995 | Var_Append(const char *name, const char *val, GNode *ctxt) | | 995 | Var_Append(const char *name, const char *val, GNode *ctxt) |
996 | { | | 996 | { |
997 | Var *v; | | 997 | Var *v; |
998 | Hash_Entry *h; | | 998 | Hash_Entry *h; |
999 | char *expanded_name = NULL; | | 999 | char *expanded_name = NULL; |
1000 | | | 1000 | |
1001 | if (strchr(name, '$') != NULL) { | | 1001 | if (strchr(name, '$') != NULL) { |
1002 | expanded_name = Var_Subst(NULL, name, ctxt, VARE_WANTRES); | | 1002 | expanded_name = Var_Subst(NULL, name, ctxt, VARE_WANTRES); |
1003 | if (expanded_name[0] == '\0') { | | 1003 | if (expanded_name[0] == '\0') { |
1004 | if (DEBUG(VAR)) { | | 1004 | if (DEBUG(VAR)) { |
1005 | fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) " | | 1005 | fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) " |
1006 | "name expands to empty string - ignored\n", | | 1006 | "name expands to empty string - ignored\n", |
1007 | name, val); | | 1007 | name, val); |
1008 | } | | 1008 | } |
1009 | free(expanded_name); | | 1009 | free(expanded_name); |
1010 | return; | | 1010 | return; |
1011 | } | | 1011 | } |
1012 | name = expanded_name; | | 1012 | name = expanded_name; |
1013 | } | | 1013 | } |
1014 | | | 1014 | |
1015 | v = VarFind(name, ctxt, ctxt == VAR_GLOBAL ? (FIND_CMD | FIND_ENV) : 0); | | 1015 | v = VarFind(name, ctxt, ctxt == VAR_GLOBAL ? (FIND_CMD | FIND_ENV) : 0); |
1016 | | | 1016 | |
1017 | if (v == NULL) { | | 1017 | if (v == NULL) { |
1018 | Var_Set(name, val, ctxt); | | 1018 | Var_Set(name, val, ctxt); |
1019 | } else if (ctxt == VAR_CMD || !(v->flags & VAR_FROM_CMD)) { | | 1019 | } else if (ctxt == VAR_CMD || !(v->flags & VAR_FROM_CMD)) { |
1020 | Buf_AddByte(&v->val, ' '); | | 1020 | Buf_AddByte(&v->val, ' '); |
1021 | Buf_AddBytes(&v->val, strlen(val), val); | | 1021 | Buf_AddBytes(&v->val, strlen(val), val); |
1022 | | | 1022 | |
1023 | if (DEBUG(VAR)) { | | 1023 | if (DEBUG(VAR)) { |
1024 | fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, | | 1024 | fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, |
1025 | Buf_GetAll(&v->val, NULL)); | | 1025 | Buf_GetAll(&v->val, NULL)); |
1026 | } | | 1026 | } |
1027 | | | 1027 | |
1028 | if (v->flags & VAR_FROM_ENV) { | | 1028 | if (v->flags & VAR_FROM_ENV) { |
1029 | /* | | 1029 | /* |
1030 | * If the original variable came from the environment, we | | 1030 | * If the original variable came from the environment, we |
1031 | * have to install it in the global context (we could place | | 1031 | * have to install it in the global context (we could place |
1032 | * it in the environment, but then we should provide a way to | | 1032 | * it in the environment, but then we should provide a way to |
1033 | * export other variables...) | | 1033 | * export other variables...) |
1034 | */ | | 1034 | */ |
1035 | v->flags &= ~VAR_FROM_ENV; | | 1035 | v->flags &= ~VAR_FROM_ENV; |
1036 | h = Hash_CreateEntry(&ctxt->context, name, NULL); | | 1036 | h = Hash_CreateEntry(&ctxt->context, name, NULL); |
1037 | Hash_SetValue(h, v); | | 1037 | Hash_SetValue(h, v); |
1038 | } | | 1038 | } |
1039 | } | | 1039 | } |
1040 | free(expanded_name); | | 1040 | free(expanded_name); |
1041 | } | | 1041 | } |
1042 | | | 1042 | |
1043 | /*- | | 1043 | /*- |
1044 | *----------------------------------------------------------------------- | | 1044 | *----------------------------------------------------------------------- |
1045 | * Var_Exists -- | | 1045 | * Var_Exists -- |
1046 | * See if the given variable exists. | | 1046 | * See if the given variable exists. |
1047 | * | | 1047 | * |
1048 | * Input: | | 1048 | * Input: |
1049 | * name Variable to find | | 1049 | * name Variable to find |
1050 | * ctxt Context in which to start search | | 1050 | * ctxt Context in which to start search |
1051 | * | | 1051 | * |
1052 | * Results: | | 1052 | * Results: |
1053 | * TRUE if it does, FALSE if it doesn't | | 1053 | * TRUE if it does, FALSE if it doesn't |
1054 | * | | 1054 | * |
1055 | * Side Effects: | | 1055 | * Side Effects: |
1056 | * None. | | 1056 | * None. |
1057 | * | | 1057 | * |
1058 | *----------------------------------------------------------------------- | | 1058 | *----------------------------------------------------------------------- |
1059 | */ | | 1059 | */ |
1060 | Boolean | | 1060 | Boolean |
1061 | Var_Exists(const char *name, GNode *ctxt) | | 1061 | Var_Exists(const char *name, GNode *ctxt) |
1062 | { | | 1062 | { |
1063 | Var *v; | | 1063 | Var *v; |
1064 | char *cp; | | 1064 | char *cp; |
1065 | | | 1065 | |
1066 | if ((cp = strchr(name, '$')) != NULL) | | 1066 | if ((cp = strchr(name, '$')) != NULL) |
1067 | cp = Var_Subst(NULL, name, ctxt, VARE_WANTRES); | | 1067 | cp = Var_Subst(NULL, name, ctxt, VARE_WANTRES); |
1068 | v = VarFind(cp ? cp : name, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV); | | 1068 | v = VarFind(cp ? cp : name, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV); |
1069 | free(cp); | | 1069 | free(cp); |
1070 | if (v == NULL) | | 1070 | if (v == NULL) |
1071 | return FALSE; | | 1071 | return FALSE; |
1072 | | | 1072 | |
1073 | (void)VarFreeEnv(v, TRUE); | | 1073 | (void)VarFreeEnv(v, TRUE); |
1074 | return TRUE; | | 1074 | return TRUE; |
1075 | } | | 1075 | } |
1076 | | | 1076 | |
1077 | /*- | | 1077 | /*- |
1078 | *----------------------------------------------------------------------- | | 1078 | *----------------------------------------------------------------------- |
| @@ -1918,2104 +1918,2106 @@ ParseModifierPart(GNode *ctxt, const cha | | | @@ -1918,2104 +1918,2106 @@ ParseModifierPart(GNode *ctxt, const cha |
1918 | cp[1] == '\\' || cp[1] == '$' || (subst != NULL && cp[1] == '&')); | | 1918 | cp[1] == '\\' || cp[1] == '$' || (subst != NULL && cp[1] == '&')); |
1919 | if (is_escaped) { | | 1919 | if (is_escaped) { |
1920 | Buf_AddByte(&buf, cp[1]); | | 1920 | Buf_AddByte(&buf, cp[1]); |
1921 | cp++; | | 1921 | cp++; |
1922 | } else if (*cp == '$') { | | 1922 | } else if (*cp == '$') { |
1923 | if (cp[1] == delim) { | | 1923 | if (cp[1] == delim) { |
1924 | if (mpflags == NULL) | | 1924 | if (mpflags == NULL) |
1925 | Buf_AddByte(&buf, *cp); | | 1925 | Buf_AddByte(&buf, *cp); |
1926 | else | | 1926 | else |
1927 | /* | | 1927 | /* |
1928 | * Unescaped $ at end of pattern => anchor | | 1928 | * Unescaped $ at end of pattern => anchor |
1929 | * pattern at end. | | 1929 | * pattern at end. |
1930 | */ | | 1930 | */ |
1931 | *mpflags |= VARP_MATCH_END; | | 1931 | *mpflags |= VARP_MATCH_END; |
1932 | } else { | | 1932 | } else { |
1933 | /* FIXME: mismatch between mpflags and VAR_NOSUBST */ | | 1933 | /* FIXME: mismatch between mpflags and VAR_NOSUBST */ |
1934 | if (mpflags == NULL || !(*mpflags & VAR_NOSUBST)) { | | 1934 | if (mpflags == NULL || !(*mpflags & VAR_NOSUBST)) { |
1935 | char *cp2; | | 1935 | char *cp2; |
1936 | int len; | | 1936 | int len; |
1937 | void *freeIt; | | 1937 | void *freeIt; |
1938 | | | 1938 | |
1939 | /* | | 1939 | /* |
1940 | * If unescaped dollar sign not before the | | 1940 | * If unescaped dollar sign not before the |
1941 | * delimiter, assume it's a variable | | 1941 | * delimiter, assume it's a variable |
1942 | * substitution and recurse. | | 1942 | * substitution and recurse. |
1943 | */ | | 1943 | */ |
1944 | cp2 = Var_Parse(cp, ctxt, errnum | (eflags & VARE_WANTRES), | | 1944 | cp2 = Var_Parse(cp, ctxt, errnum | (eflags & VARE_WANTRES), |
1945 | &len, &freeIt); | | 1945 | &len, &freeIt); |
1946 | Buf_AddBytes(&buf, strlen(cp2), cp2); | | 1946 | Buf_AddBytes(&buf, strlen(cp2), cp2); |
1947 | free(freeIt); | | 1947 | free(freeIt); |
1948 | cp += len - 1; | | 1948 | cp += len - 1; |
1949 | } else { | | 1949 | } else { |
1950 | const char *cp2 = &cp[1]; | | 1950 | const char *cp2 = &cp[1]; |
1951 | | | 1951 | |
1952 | if (*cp2 == PROPEN || *cp2 == BROPEN) { | | 1952 | if (*cp2 == PROPEN || *cp2 == BROPEN) { |
1953 | /* | | 1953 | /* |
1954 | * Find the end of this variable reference | | 1954 | * Find the end of this variable reference |
1955 | * and suck it in without further ado. | | 1955 | * and suck it in without further ado. |
1956 | * It will be interpreted later. | | 1956 | * It will be interpreted later. |
1957 | */ | | 1957 | */ |
1958 | int have = *cp2; | | 1958 | int have = *cp2; |
1959 | int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE; | | 1959 | int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE; |
1960 | int depth = 1; | | 1960 | int depth = 1; |
1961 | | | 1961 | |
1962 | for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) { | | 1962 | for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) { |
1963 | if (cp2[-1] != '\\') { | | 1963 | if (cp2[-1] != '\\') { |
1964 | if (*cp2 == have) | | 1964 | if (*cp2 == have) |
1965 | ++depth; | | 1965 | ++depth; |
1966 | if (*cp2 == want) | | 1966 | if (*cp2 == want) |
1967 | --depth; | | 1967 | --depth; |
1968 | } | | 1968 | } |
1969 | } | | 1969 | } |
1970 | Buf_AddBytes(&buf, cp2 - cp, cp); | | 1970 | Buf_AddBytes(&buf, cp2 - cp, cp); |
1971 | cp = --cp2; | | 1971 | cp = --cp2; |
1972 | } else | | 1972 | } else |
1973 | Buf_AddByte(&buf, *cp); | | 1973 | Buf_AddByte(&buf, *cp); |
1974 | } | | 1974 | } |
1975 | } | | 1975 | } |
1976 | } else if (subst != NULL && *cp == '&') | | 1976 | } else if (subst != NULL && *cp == '&') |
1977 | Buf_AddBytes(&buf, subst->lhsLen, subst->lhs); | | 1977 | Buf_AddBytes(&buf, subst->lhsLen, subst->lhs); |
1978 | else | | 1978 | else |
1979 | Buf_AddByte(&buf, *cp); | | 1979 | Buf_AddByte(&buf, *cp); |
1980 | } | | 1980 | } |
1981 | | | 1981 | |
1982 | if (*cp != delim) { | | 1982 | if (*cp != delim) { |
1983 | *tstr = cp; | | 1983 | *tstr = cp; |
1984 | *length = 0; | | 1984 | *length = 0; |
1985 | return NULL; | | 1985 | return NULL; |
1986 | } | | 1986 | } |
1987 | | | 1987 | |
1988 | *tstr = ++cp; | | 1988 | *tstr = ++cp; |
1989 | *length = Buf_Size(&buf); | | 1989 | *length = Buf_Size(&buf); |
1990 | rstr = Buf_Destroy(&buf, FALSE); | | 1990 | rstr = Buf_Destroy(&buf, FALSE); |
1991 | if (DEBUG(VAR)) | | 1991 | if (DEBUG(VAR)) |
1992 | fprintf(debug_file, "Modifier part: \"%s\"\n", rstr); | | 1992 | fprintf(debug_file, "Modifier part: \"%s\"\n", rstr); |
1993 | return rstr; | | 1993 | return rstr; |
1994 | } | | 1994 | } |
1995 | | | 1995 | |
1996 | /*- | | 1996 | /*- |
1997 | *----------------------------------------------------------------------- | | 1997 | *----------------------------------------------------------------------- |
1998 | * VarQuote -- | | 1998 | * VarQuote -- |
1999 | * Quote shell meta-characters and space characters in the string | | 1999 | * Quote shell meta-characters and space characters in the string |
2000 | * if quoteDollar is set, also quote and double any '$' characters. | | 2000 | * if quoteDollar is set, also quote and double any '$' characters. |
2001 | * | | 2001 | * |
2002 | * Results: | | 2002 | * Results: |
2003 | * The quoted string | | 2003 | * The quoted string |
2004 | * | | 2004 | * |
2005 | * Side Effects: | | 2005 | * Side Effects: |
2006 | * None. | | 2006 | * None. |
2007 | * | | 2007 | * |
2008 | *----------------------------------------------------------------------- | | 2008 | *----------------------------------------------------------------------- |
2009 | */ | | 2009 | */ |
2010 | static char * | | 2010 | static char * |
2011 | VarQuote(char *str, Boolean quoteDollar) | | 2011 | VarQuote(char *str, Boolean quoteDollar) |
2012 | { | | 2012 | { |
2013 | Buffer buf; | | 2013 | Buffer buf; |
2014 | const char *newline; | | 2014 | const char *newline; |
2015 | size_t nlen; | | 2015 | size_t nlen; |
2016 | | | 2016 | |
2017 | if ((newline = Shell_GetNewline()) == NULL) | | 2017 | if ((newline = Shell_GetNewline()) == NULL) |
2018 | newline = "\\\n"; | | 2018 | newline = "\\\n"; |
2019 | nlen = strlen(newline); | | 2019 | nlen = strlen(newline); |
2020 | | | 2020 | |
2021 | Buf_Init(&buf, 0); | | 2021 | Buf_Init(&buf, 0); |
2022 | | | 2022 | |
2023 | for (; *str != '\0'; str++) { | | 2023 | for (; *str != '\0'; str++) { |
2024 | if (*str == '\n') { | | 2024 | if (*str == '\n') { |
2025 | Buf_AddBytes(&buf, nlen, newline); | | 2025 | Buf_AddBytes(&buf, nlen, newline); |
2026 | continue; | | 2026 | continue; |
2027 | } | | 2027 | } |
2028 | if (isspace((unsigned char)*str) || ismeta((unsigned char)*str)) | | 2028 | if (isspace((unsigned char)*str) || ismeta((unsigned char)*str)) |
2029 | Buf_AddByte(&buf, '\\'); | | 2029 | Buf_AddByte(&buf, '\\'); |
2030 | Buf_AddByte(&buf, *str); | | 2030 | Buf_AddByte(&buf, *str); |
2031 | if (quoteDollar && *str == '$') | | 2031 | if (quoteDollar && *str == '$') |
2032 | Buf_AddBytes(&buf, 2, "\\$"); | | 2032 | Buf_AddBytes(&buf, 2, "\\$"); |
2033 | } | | 2033 | } |
2034 | | | 2034 | |
2035 | str = Buf_Destroy(&buf, FALSE); | | 2035 | str = Buf_Destroy(&buf, FALSE); |
2036 | if (DEBUG(VAR)) | | 2036 | if (DEBUG(VAR)) |
2037 | fprintf(debug_file, "QuoteMeta: [%s]\n", str); | | 2037 | fprintf(debug_file, "QuoteMeta: [%s]\n", str); |
2038 | return str; | | 2038 | return str; |
2039 | } | | 2039 | } |
2040 | | | 2040 | |
2041 | /*- | | 2041 | /*- |
2042 | *----------------------------------------------------------------------- | | 2042 | *----------------------------------------------------------------------- |
2043 | * VarHash -- | | 2043 | * VarHash -- |
2044 | * Hash the string using the MurmurHash3 algorithm. | | 2044 | * Hash the string using the MurmurHash3 algorithm. |
2045 | * Output is computed using 32bit Little Endian arithmetic. | | 2045 | * Output is computed using 32bit Little Endian arithmetic. |
2046 | * | | 2046 | * |
2047 | * Input: | | 2047 | * Input: |
2048 | * str String to modify | | 2048 | * str String to modify |
2049 | * | | 2049 | * |
2050 | * Results: | | 2050 | * Results: |
2051 | * Hash value of str, encoded as 8 hex digits. | | 2051 | * Hash value of str, encoded as 8 hex digits. |
2052 | * | | 2052 | * |
2053 | * Side Effects: | | 2053 | * Side Effects: |
2054 | * None. | | 2054 | * None. |
2055 | * | | 2055 | * |
2056 | *----------------------------------------------------------------------- | | 2056 | *----------------------------------------------------------------------- |
2057 | */ | | 2057 | */ |
2058 | static char * | | 2058 | static char * |
2059 | VarHash(const char *str) | | 2059 | VarHash(const char *str) |
2060 | { | | 2060 | { |
2061 | static const char hexdigits[16] = "0123456789abcdef"; | | 2061 | static const char hexdigits[16] = "0123456789abcdef"; |
2062 | Buffer buf; | | 2062 | Buffer buf; |
2063 | size_t len, len2; | | 2063 | size_t len, len2; |
2064 | const unsigned char *ustr = (const unsigned char *)str; | | 2064 | const unsigned char *ustr = (const unsigned char *)str; |
2065 | uint32_t h, k, c1, c2; | | 2065 | uint32_t h, k, c1, c2; |
2066 | | | 2066 | |
2067 | h = 0x971e137bU; | | 2067 | h = 0x971e137bU; |
2068 | c1 = 0x95543787U; | | 2068 | c1 = 0x95543787U; |
2069 | c2 = 0x2ad7eb25U; | | 2069 | c2 = 0x2ad7eb25U; |
2070 | len2 = strlen(str); | | 2070 | len2 = strlen(str); |
2071 | | | 2071 | |
2072 | for (len = len2; len; ) { | | 2072 | for (len = len2; len; ) { |
2073 | k = 0; | | 2073 | k = 0; |
2074 | switch (len) { | | 2074 | switch (len) { |
2075 | default: | | 2075 | default: |
2076 | k = ((uint32_t)ustr[3] << 24) | | | 2076 | k = ((uint32_t)ustr[3] << 24) | |
2077 | ((uint32_t)ustr[2] << 16) | | | 2077 | ((uint32_t)ustr[2] << 16) | |
2078 | ((uint32_t)ustr[1] << 8) | | | 2078 | ((uint32_t)ustr[1] << 8) | |
2079 | (uint32_t)ustr[0]; | | 2079 | (uint32_t)ustr[0]; |
2080 | len -= 4; | | 2080 | len -= 4; |
2081 | ustr += 4; | | 2081 | ustr += 4; |
2082 | break; | | 2082 | break; |
2083 | case 3: | | 2083 | case 3: |
2084 | k |= (uint32_t)ustr[2] << 16; | | 2084 | k |= (uint32_t)ustr[2] << 16; |
2085 | /* FALLTHROUGH */ | | 2085 | /* FALLTHROUGH */ |
2086 | case 2: | | 2086 | case 2: |
2087 | k |= (uint32_t)ustr[1] << 8; | | 2087 | k |= (uint32_t)ustr[1] << 8; |
2088 | /* FALLTHROUGH */ | | 2088 | /* FALLTHROUGH */ |
2089 | case 1: | | 2089 | case 1: |
2090 | k |= (uint32_t)ustr[0]; | | 2090 | k |= (uint32_t)ustr[0]; |
2091 | len = 0; | | 2091 | len = 0; |
2092 | } | | 2092 | } |
2093 | c1 = c1 * 5 + 0x7b7d159cU; | | 2093 | c1 = c1 * 5 + 0x7b7d159cU; |
2094 | c2 = c2 * 5 + 0x6bce6396U; | | 2094 | c2 = c2 * 5 + 0x6bce6396U; |
2095 | k *= c1; | | 2095 | k *= c1; |
2096 | k = (k << 11) ^ (k >> 21); | | 2096 | k = (k << 11) ^ (k >> 21); |
2097 | k *= c2; | | 2097 | k *= c2; |
2098 | h = (h << 13) ^ (h >> 19); | | 2098 | h = (h << 13) ^ (h >> 19); |
2099 | h = h * 5 + 0x52dce729U; | | 2099 | h = h * 5 + 0x52dce729U; |
2100 | h ^= k; | | 2100 | h ^= k; |
2101 | } | | 2101 | } |
2102 | h ^= len2; | | 2102 | h ^= len2; |
2103 | h *= 0x85ebca6b; | | 2103 | h *= 0x85ebca6b; |
2104 | h ^= h >> 13; | | 2104 | h ^= h >> 13; |
2105 | h *= 0xc2b2ae35; | | 2105 | h *= 0xc2b2ae35; |
2106 | h ^= h >> 16; | | 2106 | h ^= h >> 16; |
2107 | | | 2107 | |
2108 | Buf_Init(&buf, 0); | | 2108 | Buf_Init(&buf, 0); |
2109 | for (len = 0; len < 8; ++len) { | | 2109 | for (len = 0; len < 8; ++len) { |
2110 | Buf_AddByte(&buf, hexdigits[h & 15]); | | 2110 | Buf_AddByte(&buf, hexdigits[h & 15]); |
2111 | h >>= 4; | | 2111 | h >>= 4; |
2112 | } | | 2112 | } |
2113 | | | 2113 | |
2114 | return Buf_Destroy(&buf, FALSE); | | 2114 | return Buf_Destroy(&buf, FALSE); |
2115 | } | | 2115 | } |
2116 | | | 2116 | |
2117 | static char * | | 2117 | static char * |
2118 | VarStrftime(const char *fmt, int zulu, time_t utc) | | 2118 | VarStrftime(const char *fmt, int zulu, time_t utc) |
2119 | { | | 2119 | { |
2120 | char buf[BUFSIZ]; | | 2120 | char buf[BUFSIZ]; |
2121 | | | 2121 | |
2122 | if (!utc) | | 2122 | if (!utc) |
2123 | time(&utc); | | 2123 | time(&utc); |
2124 | if (!*fmt) | | 2124 | if (!*fmt) |
2125 | fmt = "%c"; | | 2125 | fmt = "%c"; |
2126 | strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc)); | | 2126 | strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc)); |
2127 | | | 2127 | |
2128 | buf[sizeof(buf) - 1] = '\0'; | | 2128 | buf[sizeof(buf) - 1] = '\0'; |
2129 | return bmake_strdup(buf); | | 2129 | return bmake_strdup(buf); |
2130 | } | | 2130 | } |
2131 | | | 2131 | |
2132 | typedef struct { | | 2132 | typedef struct { |
2133 | /* const parameters */ | | 2133 | /* const parameters */ |
2134 | int startc; /* '\0' or '{' or '(' */ | | 2134 | int startc; /* '\0' or '{' or '(' */ |
2135 | int endc; | | 2135 | int endc; |
2136 | Var *v; | | 2136 | Var *v; |
2137 | GNode *ctxt; | | 2137 | GNode *ctxt; |
2138 | VarEvalFlags eflags; | | 2138 | VarEvalFlags eflags; |
2139 | int *lengthPtr; | | 2139 | int *lengthPtr; |
2140 | void **freePtr; | | 2140 | void **freePtr; |
2141 | | | 2141 | |
2142 | /* read-write */ | | 2142 | /* read-write */ |
2143 | char *nstr; | | 2143 | char *nstr; |
2144 | const char *tstr; | | 2144 | const char *tstr; |
2145 | const char *start; | | 2145 | const char *start; |
2146 | const char *cp; /* Secondary pointer into str (place marker | | 2146 | const char *cp; /* Secondary pointer into str (place marker |
2147 | * for tstr) */ | | 2147 | * for tstr) */ |
2148 | char termc; /* Character which terminated scan */ | | 2148 | char termc; /* Character which terminated scan */ |
2149 | char delim; | | 2149 | char delim; |
2150 | int modifier; /* that we are processing */ | | 2150 | int modifier; /* that we are processing */ |
2151 | Var_Parse_State parsestate; /* Flags passed to helper functions */ | | 2151 | Var_Parse_State parsestate; /* Flags passed to helper functions */ |
2152 | | | 2152 | |
2153 | /* result */ | | 2153 | /* result */ |
2154 | char *newStr; /* New value to return */ | | 2154 | char *newStr; /* New value to return */ |
2155 | } ApplyModifiersState; | | 2155 | } ApplyModifiersState; |
2156 | | | 2156 | |
2157 | /* we now have some modifiers with long names */ | | 2157 | /* we now have some modifiers with long names */ |
2158 | #define STRMOD_MATCH(s, want, n) \ | | 2158 | #define STRMOD_MATCH(s, want, n) \ |
2159 | (strncmp(s, want, n) == 0 && (s[n] == st->endc || s[n] == ':')) | | 2159 | (strncmp(s, want, n) == 0 && (s[n] == st->endc || s[n] == ':')) |
2160 | #define STRMOD_MATCHX(s, want, n) \ | | 2160 | #define STRMOD_MATCHX(s, want, n) \ |
2161 | (strncmp(s, want, n) == 0 && \ | | 2161 | (strncmp(s, want, n) == 0 && \ |
2162 | (s[n] == st->endc || s[n] == ':' || s[n] == '=')) | | 2162 | (s[n] == st->endc || s[n] == ':' || s[n] == '=')) |
2163 | #define CHARMOD_MATCH(c) (c == st->endc || c == ':') | | 2163 | #define CHARMOD_MATCH(c) (c == st->endc || c == ':') |
2164 | | | 2164 | |
2165 | /* :@var@...${var}...@ */ | | 2165 | /* :@var@...${var}...@ */ |
2166 | static Boolean | | 2166 | static Boolean |
2167 | ApplyModifier_At(ApplyModifiersState *st) { | | 2167 | ApplyModifier_At(ApplyModifiersState *st) { |
2168 | VarLoop loop; | | 2168 | VarLoop loop; |
2169 | VarPatternFlags pflags = VAR_NOSUBST; /* FIXME: mismatch between pflags and VAR_NOSUBST */ | | 2169 | VarPatternFlags pflags = VAR_NOSUBST; /* FIXME: mismatch between pflags and VAR_NOSUBST */ |
2170 | | | 2170 | |
2171 | st->cp = ++st->tstr; | | 2171 | st->cp = ++st->tstr; |
2172 | st->delim = '@'; | | 2172 | st->delim = '@'; |
2173 | loop.tvar = ParseModifierPart( | | 2173 | loop.tvar = ParseModifierPart( |
2174 | st->ctxt, &st->cp, st->delim, st->eflags, &pflags, NULL, NULL); | | 2174 | st->ctxt, &st->cp, st->delim, st->eflags, &pflags, NULL, NULL); |
2175 | if (loop.tvar == NULL) | | 2175 | if (loop.tvar == NULL) |
2176 | return FALSE; | | 2176 | return FALSE; |
2177 | | | 2177 | |
2178 | loop.str = ParseModifierPart( | | 2178 | loop.str = ParseModifierPart( |
2179 | st->ctxt, &st->cp, st->delim, st->eflags, &pflags, NULL, NULL); | | 2179 | st->ctxt, &st->cp, st->delim, st->eflags, &pflags, NULL, NULL); |
2180 | if (loop.str == NULL) | | 2180 | if (loop.str == NULL) |
2181 | return FALSE; | | 2181 | return FALSE; |
2182 | | | 2182 | |
2183 | st->termc = *st->cp; | | 2183 | st->termc = *st->cp; |
2184 | st->delim = '\0'; | | 2184 | st->delim = '\0'; |
2185 | | | 2185 | |
2186 | loop.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES); | | 2186 | loop.eflags = st->eflags & (VARE_UNDEFERR | VARE_WANTRES); |
2187 | int prev_sep = st->parsestate.varSpace; | | 2187 | int prev_sep = st->parsestate.varSpace; |
2188 | st->parsestate.varSpace = ' '; | | 2188 | st->parsestate.varSpace = ' '; |
2189 | st->newStr = VarModify( | | 2189 | st->newStr = VarModify( |
2190 | st->ctxt, &st->parsestate, st->nstr, VarLoopExpand, &loop); | | 2190 | st->ctxt, &st->parsestate, st->nstr, VarLoopExpand, &loop); |
2191 | st->parsestate.varSpace = prev_sep; | | 2191 | st->parsestate.varSpace = prev_sep; |
2192 | Var_Delete(loop.tvar, st->ctxt); | | 2192 | Var_Delete(loop.tvar, st->ctxt); |
2193 | free(loop.tvar); | | 2193 | free(loop.tvar); |
2194 | free(loop.str); | | 2194 | free(loop.str); |
2195 | return TRUE; | | 2195 | return TRUE; |
2196 | } | | 2196 | } |
2197 | | | 2197 | |
2198 | /* :Ddefined or :Uundefined */ | | 2198 | /* :Ddefined or :Uundefined */ |
2199 | static void | | 2199 | static void |
2200 | ApplyModifier_Defined(ApplyModifiersState *st) | | 2200 | ApplyModifier_Defined(ApplyModifiersState *st) |
2201 | { | | 2201 | { |
2202 | Buffer buf; /* Buffer for patterns */ | | 2202 | Buffer buf; /* Buffer for patterns */ |
2203 | VarEvalFlags neflags; | | 2203 | VarEvalFlags neflags; |
2204 | | | 2204 | |
2205 | if (st->eflags & VARE_WANTRES) { | | 2205 | if (st->eflags & VARE_WANTRES) { |
2206 | Boolean wantres; | | 2206 | Boolean wantres; |
2207 | if (*st->tstr == 'U') | | 2207 | if (*st->tstr == 'U') |
2208 | wantres = ((st->v->flags & VAR_JUNK) != 0); | | 2208 | wantres = ((st->v->flags & VAR_JUNK) != 0); |
2209 | else | | 2209 | else |
2210 | wantres = ((st->v->flags & VAR_JUNK) == 0); | | 2210 | wantres = ((st->v->flags & VAR_JUNK) == 0); |
2211 | neflags = st->eflags & ~VARE_WANTRES; | | 2211 | neflags = st->eflags & ~VARE_WANTRES; |
2212 | if (wantres) | | 2212 | if (wantres) |
2213 | neflags |= VARE_WANTRES; | | 2213 | neflags |= VARE_WANTRES; |
2214 | } else | | 2214 | } else |
2215 | neflags = st->eflags; | | 2215 | neflags = st->eflags; |
2216 | | | 2216 | |
2217 | /* | | 2217 | /* |
2218 | * Pass through tstr looking for 1) escaped delimiters, | | 2218 | * Pass through tstr looking for 1) escaped delimiters, |
2219 | * '$'s and backslashes (place the escaped character in | | 2219 | * '$'s and backslashes (place the escaped character in |
2220 | * uninterpreted) and 2) unescaped $'s that aren't before | | 2220 | * uninterpreted) and 2) unescaped $'s that aren't before |
2221 | * the delimiter (expand the variable substitution). | | 2221 | * the delimiter (expand the variable substitution). |
2222 | * The result is left in the Buffer buf. | | 2222 | * The result is left in the Buffer buf. |
2223 | */ | | 2223 | */ |
2224 | Buf_Init(&buf, 0); | | 2224 | Buf_Init(&buf, 0); |
2225 | for (st->cp = st->tstr + 1; | | 2225 | for (st->cp = st->tstr + 1; |
2226 | *st->cp != st->endc && *st->cp != ':' && *st->cp != '\0'; | | 2226 | *st->cp != st->endc && *st->cp != ':' && *st->cp != '\0'; |
2227 | st->cp++) { | | 2227 | st->cp++) { |
2228 | if (*st->cp == '\\' && | | 2228 | if (*st->cp == '\\' && |
2229 | (st->cp[1] == ':' || st->cp[1] == '$' || st->cp[1] == st->endc || | | 2229 | (st->cp[1] == ':' || st->cp[1] == '$' || st->cp[1] == st->endc || |
2230 | st->cp[1] == '\\')) { | | 2230 | st->cp[1] == '\\')) { |
2231 | Buf_AddByte(&buf, st->cp[1]); | | 2231 | Buf_AddByte(&buf, st->cp[1]); |
2232 | st->cp++; | | 2232 | st->cp++; |
2233 | } else if (*st->cp == '$') { | | 2233 | } else if (*st->cp == '$') { |
2234 | /* | | 2234 | /* |
2235 | * If unescaped dollar sign, assume it's a | | 2235 | * If unescaped dollar sign, assume it's a |
2236 | * variable substitution and recurse. | | 2236 | * variable substitution and recurse. |
2237 | */ | | 2237 | */ |
2238 | char *cp2; | | 2238 | char *cp2; |
2239 | int len; | | 2239 | int len; |
2240 | void *freeIt; | | 2240 | void *freeIt; |
2241 | | | 2241 | |
2242 | cp2 = Var_Parse(st->cp, st->ctxt, neflags, &len, &freeIt); | | 2242 | cp2 = Var_Parse(st->cp, st->ctxt, neflags, &len, &freeIt); |
2243 | Buf_AddBytes(&buf, strlen(cp2), cp2); | | 2243 | Buf_AddBytes(&buf, strlen(cp2), cp2); |
2244 | free(freeIt); | | 2244 | free(freeIt); |
2245 | st->cp += len - 1; | | 2245 | st->cp += len - 1; |
2246 | } else { | | 2246 | } else { |
2247 | Buf_AddByte(&buf, *st->cp); | | 2247 | Buf_AddByte(&buf, *st->cp); |
2248 | } | | 2248 | } |
2249 | } | | 2249 | } |
2250 | | | 2250 | |
2251 | st->termc = *st->cp; | | 2251 | st->termc = *st->cp; |
2252 | | | 2252 | |
2253 | if (st->v->flags & VAR_JUNK) | | 2253 | if (st->v->flags & VAR_JUNK) |
2254 | st->v->flags |= VAR_KEEP; | | 2254 | st->v->flags |= VAR_KEEP; |
2255 | if (neflags & VARE_WANTRES) { | | 2255 | if (neflags & VARE_WANTRES) { |
2256 | st->newStr = Buf_Destroy(&buf, FALSE); | | 2256 | st->newStr = Buf_Destroy(&buf, FALSE); |
2257 | } else { | | 2257 | } else { |
2258 | st->newStr = st->nstr; | | 2258 | st->newStr = st->nstr; |
2259 | Buf_Destroy(&buf, TRUE); | | 2259 | Buf_Destroy(&buf, TRUE); |
2260 | } | | 2260 | } |
2261 | } | | 2261 | } |
2262 | | | 2262 | |
2263 | /* :gmtime */ | | 2263 | /* :gmtime */ |
2264 | static Boolean | | 2264 | static Boolean |
2265 | ApplyModifier_Gmtime(ApplyModifiersState *st) | | 2265 | ApplyModifier_Gmtime(ApplyModifiersState *st) |
2266 | { | | 2266 | { |
2267 | time_t utc; | | 2267 | time_t utc; |
2268 | char *ep; | | 2268 | char *ep; |
2269 | | | 2269 | |
2270 | st->cp = st->tstr + 1; /* make sure it is set */ | | 2270 | st->cp = st->tstr + 1; /* make sure it is set */ |
2271 | if (!STRMOD_MATCHX(st->tstr, "gmtime", 6)) | | 2271 | if (!STRMOD_MATCHX(st->tstr, "gmtime", 6)) |
2272 | return FALSE; | | 2272 | return FALSE; |
2273 | if (st->tstr[6] == '=') { | | 2273 | if (st->tstr[6] == '=') { |
2274 | utc = strtoul(&st->tstr[7], &ep, 10); | | 2274 | utc = strtoul(&st->tstr[7], &ep, 10); |
2275 | st->cp = ep; | | 2275 | st->cp = ep; |
2276 | } else { | | 2276 | } else { |
2277 | utc = 0; | | 2277 | utc = 0; |
2278 | st->cp = st->tstr + 6; | | 2278 | st->cp = st->tstr + 6; |
2279 | } | | 2279 | } |
2280 | st->newStr = VarStrftime(st->nstr, 1, utc); | | 2280 | st->newStr = VarStrftime(st->nstr, 1, utc); |
2281 | st->termc = *st->cp; | | 2281 | st->termc = *st->cp; |
2282 | return TRUE; | | 2282 | return TRUE; |
2283 | } | | 2283 | } |
2284 | | | 2284 | |
2285 | /* :localtime */ | | 2285 | /* :localtime */ |
2286 | static Boolean | | 2286 | static Boolean |
2287 | ApplyModifier_Localtime(ApplyModifiersState *st) | | 2287 | ApplyModifier_Localtime(ApplyModifiersState *st) |
2288 | { | | 2288 | { |
2289 | time_t utc; | | 2289 | time_t utc; |
2290 | char *ep; | | 2290 | char *ep; |
2291 | | | 2291 | |
2292 | st->cp = st->tstr + 1; /* make sure it is set */ | | 2292 | st->cp = st->tstr + 1; /* make sure it is set */ |
2293 | if (!STRMOD_MATCHX(st->tstr, "localtime", 9)) | | 2293 | if (!STRMOD_MATCHX(st->tstr, "localtime", 9)) |
2294 | return FALSE; | | 2294 | return FALSE; |
2295 | | | 2295 | |
2296 | if (st->tstr[9] == '=') { | | 2296 | if (st->tstr[9] == '=') { |
2297 | utc = strtoul(&st->tstr[10], &ep, 10); | | 2297 | utc = strtoul(&st->tstr[10], &ep, 10); |
2298 | st->cp = ep; | | 2298 | st->cp = ep; |
2299 | } else { | | 2299 | } else { |
2300 | utc = 0; | | 2300 | utc = 0; |
2301 | st->cp = st->tstr + 9; | | 2301 | st->cp = st->tstr + 9; |
2302 | } | | 2302 | } |
2303 | st->newStr = VarStrftime(st->nstr, 0, utc); | | 2303 | st->newStr = VarStrftime(st->nstr, 0, utc); |
2304 | st->termc = *st->cp; | | 2304 | st->termc = *st->cp; |
2305 | return TRUE; | | 2305 | return TRUE; |
2306 | } | | 2306 | } |
2307 | | | 2307 | |
2308 | /* :hash */ | | 2308 | /* :hash */ |
2309 | static Boolean | | 2309 | static Boolean |
2310 | ApplyModifier_Hash(ApplyModifiersState *st) | | 2310 | ApplyModifier_Hash(ApplyModifiersState *st) |
2311 | { | | 2311 | { |
2312 | st->cp = st->tstr + 1; /* make sure it is set */ | | 2312 | st->cp = st->tstr + 1; /* make sure it is set */ |
2313 | if (!STRMOD_MATCH(st->tstr, "hash", 4)) | | 2313 | if (!STRMOD_MATCH(st->tstr, "hash", 4)) |
2314 | return FALSE; | | 2314 | return FALSE; |
2315 | st->newStr = VarHash(st->nstr); | | 2315 | st->newStr = VarHash(st->nstr); |
2316 | st->cp = st->tstr + 4; | | 2316 | st->cp = st->tstr + 4; |
2317 | st->termc = *st->cp; | | 2317 | st->termc = *st->cp; |
2318 | return TRUE; | | 2318 | return TRUE; |
2319 | } | | 2319 | } |
2320 | | | 2320 | |
2321 | /* :P */ | | 2321 | /* :P */ |
2322 | static void | | 2322 | static void |
2323 | ApplyModifier_Path(ApplyModifiersState *st) | | 2323 | ApplyModifier_Path(ApplyModifiersState *st) |
2324 | { | | 2324 | { |
2325 | GNode *gn; | | 2325 | GNode *gn; |
2326 | | | 2326 | |
2327 | if ((st->v->flags & VAR_JUNK) != 0) | | 2327 | if ((st->v->flags & VAR_JUNK) != 0) |
2328 | st->v->flags |= VAR_KEEP; | | 2328 | st->v->flags |= VAR_KEEP; |
2329 | gn = Targ_FindNode(st->v->name, TARG_NOCREATE); | | 2329 | gn = Targ_FindNode(st->v->name, TARG_NOCREATE); |
2330 | if (gn == NULL || gn->type & OP_NOPATH) { | | 2330 | if (gn == NULL || gn->type & OP_NOPATH) { |
2331 | st->newStr = NULL; | | 2331 | st->newStr = NULL; |
2332 | } else if (gn->path) { | | 2332 | } else if (gn->path) { |
2333 | st->newStr = bmake_strdup(gn->path); | | 2333 | st->newStr = bmake_strdup(gn->path); |
2334 | } else { | | 2334 | } else { |
2335 | st->newStr = Dir_FindFile(st->v->name, Suff_FindPath(gn)); | | 2335 | st->newStr = Dir_FindFile(st->v->name, Suff_FindPath(gn)); |
2336 | } | | 2336 | } |
2337 | if (!st->newStr) | | 2337 | if (!st->newStr) |
2338 | st->newStr = bmake_strdup(st->v->name); | | 2338 | st->newStr = bmake_strdup(st->v->name); |
2339 | st->cp = ++st->tstr; | | 2339 | st->cp = ++st->tstr; |
2340 | st->termc = *st->tstr; | | 2340 | st->termc = *st->tstr; |
2341 | } | | 2341 | } |
2342 | | | 2342 | |
2343 | /* :!cmd! */ | | 2343 | /* :!cmd! */ |
2344 | static Boolean | | 2344 | static Boolean |
2345 | ApplyModifier_Exclam(ApplyModifiersState *st) | | 2345 | ApplyModifier_Exclam(ApplyModifiersState *st) |
2346 | { | | 2346 | { |
2347 | st->delim = '!'; | | 2347 | st->delim = '!'; |
2348 | st->cp = ++st->tstr; | | 2348 | st->cp = ++st->tstr; |
2349 | char *cmd = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 2349 | char *cmd = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
2350 | NULL, NULL, NULL); | | 2350 | NULL, NULL, NULL); |
2351 | if (cmd == NULL) | | 2351 | if (cmd == NULL) |
2352 | return FALSE; | | 2352 | return FALSE; |
2353 | | | 2353 | |
2354 | const char *emsg = NULL; | | 2354 | const char *emsg = NULL; |
2355 | if (st->eflags & VARE_WANTRES) | | 2355 | if (st->eflags & VARE_WANTRES) |
2356 | st->newStr = Cmd_Exec(cmd, &emsg); | | 2356 | st->newStr = Cmd_Exec(cmd, &emsg); |
2357 | else | | 2357 | else |
2358 | st->newStr = varNoError; | | 2358 | st->newStr = varNoError; |
2359 | free(cmd); | | 2359 | free(cmd); |
2360 | | | 2360 | |
2361 | if (emsg) | | 2361 | if (emsg) |
2362 | Error(emsg, st->nstr); | | 2362 | Error(emsg, st->nstr); |
2363 | | | 2363 | |
2364 | st->termc = *st->cp; | | 2364 | st->termc = *st->cp; |
2365 | st->delim = '\0'; | | 2365 | st->delim = '\0'; |
2366 | if (st->v->flags & VAR_JUNK) | | 2366 | if (st->v->flags & VAR_JUNK) |
2367 | st->v->flags |= VAR_KEEP; | | 2367 | st->v->flags |= VAR_KEEP; |
2368 | return TRUE; | | 2368 | return TRUE; |
2369 | } | | 2369 | } |
2370 | | | 2370 | |
2371 | /* :range */ | | 2371 | /* :range */ |
2372 | static Boolean | | 2372 | static Boolean |
2373 | ApplyModifier_Range(ApplyModifiersState *st) | | 2373 | ApplyModifier_Range(ApplyModifiersState *st) |
2374 | { | | 2374 | { |
2375 | int n; | | 2375 | int n; |
2376 | char *ep; | | 2376 | char *ep; |
2377 | | | 2377 | |
2378 | st->cp = st->tstr + 1; /* make sure it is set */ | | 2378 | st->cp = st->tstr + 1; /* make sure it is set */ |
2379 | if (!STRMOD_MATCHX(st->tstr, "range", 5)) | | 2379 | if (!STRMOD_MATCHX(st->tstr, "range", 5)) |
2380 | return FALSE; | | 2380 | return FALSE; |
2381 | | | 2381 | |
2382 | if (st->tstr[5] == '=') { | | 2382 | if (st->tstr[5] == '=') { |
2383 | n = strtoul(&st->tstr[6], &ep, 10); | | 2383 | n = strtoul(&st->tstr[6], &ep, 10); |
2384 | st->cp = ep; | | 2384 | st->cp = ep; |
2385 | } else { | | 2385 | } else { |
2386 | n = 0; | | 2386 | n = 0; |
2387 | st->cp = st->tstr + 5; | | 2387 | st->cp = st->tstr + 5; |
2388 | } | | 2388 | } |
2389 | st->newStr = VarRange(st->nstr, n); | | 2389 | st->newStr = VarRange(st->nstr, n); |
2390 | st->termc = *st->cp; | | 2390 | st->termc = *st->cp; |
2391 | return TRUE; | | 2391 | return TRUE; |
2392 | } | | 2392 | } |
2393 | | | 2393 | |
2394 | /* :Mpattern or :Npattern */ | | 2394 | /* :Mpattern or :Npattern */ |
2395 | static void | | 2395 | static void |
2396 | ApplyModifier_Match(ApplyModifiersState *st) | | 2396 | ApplyModifier_Match(ApplyModifiersState *st) |
2397 | { | | 2397 | { |
2398 | char *pattern; | | 2398 | char *pattern; |
2399 | const char *endpat; /* points just after end of pattern */ | | 2399 | const char *endpat; /* points just after end of pattern */ |
2400 | char *cp2; | | 2400 | char *cp2; |
2401 | Boolean copy; /* pattern should be, or has been, copied */ | | 2401 | Boolean copy; /* pattern should be, or has been, copied */ |
2402 | Boolean needSubst; | | 2402 | Boolean needSubst; |
2403 | int nest; | | 2403 | int nest; |
2404 | | | 2404 | |
2405 | copy = FALSE; | | 2405 | copy = FALSE; |
2406 | needSubst = FALSE; | | 2406 | needSubst = FALSE; |
2407 | nest = 1; | | 2407 | nest = 1; |
2408 | /* | | 2408 | /* |
2409 | * In the loop below, ignore ':' unless we are at | | 2409 | * In the loop below, ignore ':' unless we are at |
2410 | * (or back to) the original brace level. | | 2410 | * (or back to) the original brace level. |
2411 | * XXX This will likely not work right if $() and ${} | | 2411 | * XXX This will likely not work right if $() and ${} |
2412 | * are intermixed. | | 2412 | * are intermixed. |
2413 | */ | | 2413 | */ |
2414 | for (st->cp = st->tstr + 1; | | 2414 | for (st->cp = st->tstr + 1; |
2415 | *st->cp != '\0' && !(*st->cp == ':' && nest == 1); | | 2415 | *st->cp != '\0' && !(*st->cp == ':' && nest == 1); |
2416 | st->cp++) { | | 2416 | st->cp++) { |
2417 | if (*st->cp == '\\' && | | 2417 | if (*st->cp == '\\' && |
2418 | (st->cp[1] == ':' || st->cp[1] == st->endc || | | 2418 | (st->cp[1] == ':' || st->cp[1] == st->endc || |
2419 | st->cp[1] == st->startc)) { | | 2419 | st->cp[1] == st->startc)) { |
2420 | if (!needSubst) | | 2420 | if (!needSubst) |
2421 | copy = TRUE; | | 2421 | copy = TRUE; |
2422 | st->cp++; | | 2422 | st->cp++; |
2423 | continue; | | 2423 | continue; |
2424 | } | | 2424 | } |
2425 | if (*st->cp == '$') | | 2425 | if (*st->cp == '$') |
2426 | needSubst = TRUE; | | 2426 | needSubst = TRUE; |
2427 | if (*st->cp == '(' || *st->cp == '{') | | 2427 | if (*st->cp == '(' || *st->cp == '{') |
2428 | ++nest; | | 2428 | ++nest; |
2429 | if (*st->cp == ')' || *st->cp == '}') { | | 2429 | if (*st->cp == ')' || *st->cp == '}') { |
2430 | --nest; | | 2430 | --nest; |
2431 | if (nest == 0) | | 2431 | if (nest == 0) |
2432 | break; | | 2432 | break; |
2433 | } | | 2433 | } |
2434 | } | | 2434 | } |
2435 | st->termc = *st->cp; | | 2435 | st->termc = *st->cp; |
2436 | endpat = st->cp; | | 2436 | endpat = st->cp; |
2437 | if (copy) { | | 2437 | if (copy) { |
2438 | /* | | 2438 | /* |
2439 | * Need to compress the \:'s out of the pattern, so | | 2439 | * Need to compress the \:'s out of the pattern, so |
2440 | * allocate enough room to hold the uncompressed | | 2440 | * allocate enough room to hold the uncompressed |
2441 | * pattern (note that st->cp started at st->tstr+1, so | | 2441 | * pattern (note that st->cp started at st->tstr+1, so |
2442 | * st->cp - st->tstr takes the null byte into account) and | | 2442 | * st->cp - st->tstr takes the null byte into account) and |
2443 | * compress the pattern into the space. | | 2443 | * compress the pattern into the space. |
2444 | */ | | 2444 | */ |
2445 | pattern = bmake_malloc(st->cp - st->tstr); | | 2445 | pattern = bmake_malloc(st->cp - st->tstr); |
2446 | for (cp2 = pattern, st->cp = st->tstr + 1; | | 2446 | for (cp2 = pattern, st->cp = st->tstr + 1; |
2447 | st->cp < endpat; | | 2447 | st->cp < endpat; |
2448 | st->cp++, cp2++) { | | 2448 | st->cp++, cp2++) { |
2449 | if ((*st->cp == '\\') && (st->cp+1 < endpat) && | | 2449 | if ((*st->cp == '\\') && (st->cp+1 < endpat) && |
2450 | (st->cp[1] == ':' || st->cp[1] == st->endc)) | | 2450 | (st->cp[1] == ':' || st->cp[1] == st->endc)) |
2451 | st->cp++; | | 2451 | st->cp++; |
2452 | *cp2 = *st->cp; | | 2452 | *cp2 = *st->cp; |
2453 | } | | 2453 | } |
2454 | *cp2 = '\0'; | | 2454 | *cp2 = '\0'; |
2455 | endpat = cp2; | | 2455 | endpat = cp2; |
2456 | } else { | | 2456 | } else { |
2457 | /* | | 2457 | /* |
2458 | * Either Var_Subst or VarModify will need a | | 2458 | * Either Var_Subst or VarModify will need a |
2459 | * nul-terminated string soon, so construct one now. | | 2459 | * nul-terminated string soon, so construct one now. |
2460 | */ | | 2460 | */ |
2461 | pattern = bmake_strndup(st->tstr+1, endpat - (st->tstr + 1)); | | 2461 | pattern = bmake_strndup(st->tstr+1, endpat - (st->tstr + 1)); |
2462 | } | | 2462 | } |
2463 | if (needSubst) { | | 2463 | if (needSubst) { |
2464 | /* pattern contains embedded '$', so use Var_Subst to expand it. */ | | 2464 | /* pattern contains embedded '$', so use Var_Subst to expand it. */ |
2465 | cp2 = pattern; | | 2465 | cp2 = pattern; |
2466 | pattern = Var_Subst(NULL, cp2, st->ctxt, st->eflags); | | 2466 | pattern = Var_Subst(NULL, cp2, st->ctxt, st->eflags); |
2467 | free(cp2); | | 2467 | free(cp2); |
2468 | } | | 2468 | } |
2469 | if (DEBUG(VAR)) | | 2469 | if (DEBUG(VAR)) |
2470 | fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n", | | 2470 | fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n", |
2471 | st->v->name, st->nstr, pattern); | | 2471 | st->v->name, st->nstr, pattern); |
2472 | VarModifyCallback callback = st->tstr[0] == 'M' ? VarMatch : VarNoMatch; | | 2472 | VarModifyCallback callback = st->tstr[0] == 'M' ? VarMatch : VarNoMatch; |
2473 | st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, callback, | | 2473 | st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, callback, |
2474 | pattern); | | 2474 | pattern); |
2475 | free(pattern); | | 2475 | free(pattern); |
2476 | } | | 2476 | } |
2477 | | | 2477 | |
2478 | /* :S,from,to, */ | | 2478 | /* :S,from,to, */ |
2479 | static Boolean | | 2479 | static Boolean |
2480 | ApplyModifier_Subst(ApplyModifiersState *st) | | 2480 | ApplyModifier_Subst(ApplyModifiersState *st) |
2481 | { | | 2481 | { |
2482 | VarSubstituteArgs args; | | 2482 | VarSubstituteArgs args; |
2483 | Var_Parse_State tmpparsestate = st->parsestate; | | 2483 | Var_Parse_State tmpparsestate = st->parsestate; |
2484 | st->delim = st->tstr[1]; | | 2484 | st->delim = st->tstr[1]; |
2485 | st->tstr += 2; | | 2485 | st->tstr += 2; |
2486 | | | 2486 | |
2487 | /* | | 2487 | /* |
2488 | * If pattern begins with '^', it is anchored to the | | 2488 | * If pattern begins with '^', it is anchored to the |
2489 | * start of the word -- skip over it and flag pattern. | | 2489 | * start of the word -- skip over it and flag pattern. |
2490 | */ | | 2490 | */ |
2491 | args.pflags = 0; | | 2491 | args.pflags = 0; |
2492 | if (*st->tstr == '^') { | | 2492 | if (*st->tstr == '^') { |
2493 | args.pflags |= VARP_MATCH_START; | | 2493 | args.pflags |= VARP_MATCH_START; |
2494 | st->tstr++; | | 2494 | st->tstr++; |
2495 | } | | 2495 | } |
2496 | | | 2496 | |
2497 | st->cp = st->tstr; | | 2497 | st->cp = st->tstr; |
2498 | char *lhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 2498 | char *lhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
2499 | &args.pflags, &args.lhsLen, NULL); | | 2499 | &args.pflags, &args.lhsLen, NULL); |
2500 | if (lhs == NULL) | | 2500 | if (lhs == NULL) |
2501 | return FALSE; | | 2501 | return FALSE; |
2502 | args.lhs = lhs; | | 2502 | args.lhs = lhs; |
2503 | | | 2503 | |
2504 | char *rhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 2504 | char *rhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
2505 | NULL, &args.rhsLen, &args); | | 2505 | NULL, &args.rhsLen, &args); |
2506 | if (rhs == NULL) | | 2506 | if (rhs == NULL) |
2507 | return FALSE; | | 2507 | return FALSE; |
2508 | args.rhs = rhs; | | 2508 | args.rhs = rhs; |
2509 | | | 2509 | |
2510 | /* | | 2510 | /* |
2511 | * Check for global substitution. If 'g' after the final | | 2511 | * Check for global substitution. If 'g' after the final |
2512 | * delimiter, substitution is global and is marked that | | 2512 | * delimiter, substitution is global and is marked that |
2513 | * way. | | 2513 | * way. |
2514 | */ | | 2514 | */ |
2515 | for (;; st->cp++) { | | 2515 | for (;; st->cp++) { |
2516 | switch (*st->cp) { | | 2516 | switch (*st->cp) { |
2517 | case 'g': | | 2517 | case 'g': |
2518 | args.pflags |= VARP_SUB_GLOBAL; | | 2518 | args.pflags |= VARP_SUB_GLOBAL; |
2519 | continue; | | 2519 | continue; |
2520 | case '1': | | 2520 | case '1': |
2521 | args.pflags |= VARP_SUB_ONE; | | 2521 | args.pflags |= VARP_SUB_ONE; |
2522 | continue; | | 2522 | continue; |
2523 | case 'W': | | 2523 | case 'W': |
2524 | tmpparsestate.oneBigWord = TRUE; | | 2524 | tmpparsestate.oneBigWord = TRUE; |
2525 | continue; | | 2525 | continue; |
2526 | } | | 2526 | } |
2527 | break; | | 2527 | break; |
2528 | } | | 2528 | } |
2529 | | | 2529 | |
2530 | st->termc = *st->cp; | | 2530 | st->termc = *st->cp; |
2531 | st->newStr = VarModify(st->ctxt, &tmpparsestate, st->nstr, | | 2531 | st->newStr = VarModify(st->ctxt, &tmpparsestate, st->nstr, |
2532 | VarSubstitute, &args); | | 2532 | VarSubstitute, &args); |
2533 | | | 2533 | |
2534 | free(lhs); | | 2534 | free(lhs); |
2535 | free(rhs); | | 2535 | free(rhs); |
2536 | st->delim = '\0'; | | 2536 | st->delim = '\0'; |
2537 | return TRUE; | | 2537 | return TRUE; |
2538 | } | | 2538 | } |
2539 | | | 2539 | |
2540 | #ifndef NO_REGEX | | 2540 | #ifndef NO_REGEX |
2541 | /* :C,from,to, */ | | 2541 | /* :C,from,to, */ |
2542 | static Boolean | | 2542 | static Boolean |
2543 | ApplyModifier_Regex(ApplyModifiersState *st) | | 2543 | ApplyModifier_Regex(ApplyModifiersState *st) |
2544 | { | | 2544 | { |
2545 | VarREPattern pattern; | | 2545 | VarREPattern pattern; |
2546 | char *re; | | 2546 | char *re; |
2547 | int error; | | 2547 | int error; |
2548 | Var_Parse_State tmpparsestate; | | 2548 | Var_Parse_State tmpparsestate; |
2549 | | | 2549 | |
2550 | pattern.pflags = 0; | | 2550 | pattern.pflags = 0; |
2551 | tmpparsestate = st->parsestate; | | 2551 | tmpparsestate = st->parsestate; |
2552 | st->delim = st->tstr[1]; | | 2552 | st->delim = st->tstr[1]; |
2553 | st->tstr += 2; | | 2553 | st->tstr += 2; |
2554 | | | 2554 | |
2555 | st->cp = st->tstr; | | 2555 | st->cp = st->tstr; |
2556 | | | 2556 | |
2557 | re = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 2557 | re = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
2558 | NULL, NULL, NULL); | | 2558 | NULL, NULL, NULL); |
2559 | if (re == NULL) | | 2559 | if (re == NULL) |
2560 | return FALSE; | | 2560 | return FALSE; |
2561 | | | 2561 | |
2562 | pattern.replace = ParseModifierPart(st->ctxt, &st->cp, st->delim, | | 2562 | pattern.replace = ParseModifierPart(st->ctxt, &st->cp, st->delim, |
2563 | st->eflags, NULL, NULL, NULL); | | 2563 | st->eflags, NULL, NULL, NULL); |
2564 | if (pattern.replace == NULL) { | | 2564 | if (pattern.replace == NULL) { |
2565 | free(re); | | 2565 | free(re); |
2566 | return FALSE; | | 2566 | return FALSE; |
2567 | } | | 2567 | } |
2568 | | | 2568 | |
2569 | for (;; st->cp++) { | | 2569 | for (;; st->cp++) { |
2570 | switch (*st->cp) { | | 2570 | switch (*st->cp) { |
2571 | case 'g': | | 2571 | case 'g': |
2572 | pattern.pflags |= VARP_SUB_GLOBAL; | | 2572 | pattern.pflags |= VARP_SUB_GLOBAL; |
2573 | continue; | | 2573 | continue; |
2574 | case '1': | | 2574 | case '1': |
2575 | pattern.pflags |= VARP_SUB_ONE; | | 2575 | pattern.pflags |= VARP_SUB_ONE; |
2576 | continue; | | 2576 | continue; |
2577 | case 'W': | | 2577 | case 'W': |
2578 | tmpparsestate.oneBigWord = TRUE; | | 2578 | tmpparsestate.oneBigWord = TRUE; |
2579 | continue; | | 2579 | continue; |
2580 | } | | 2580 | } |
2581 | break; | | 2581 | break; |
2582 | } | | 2582 | } |
2583 | | | 2583 | |
2584 | st->termc = *st->cp; | | 2584 | st->termc = *st->cp; |
2585 | | | 2585 | |
2586 | error = regcomp(&pattern.re, re, REG_EXTENDED); | | 2586 | error = regcomp(&pattern.re, re, REG_EXTENDED); |
2587 | free(re); | | 2587 | free(re); |
2588 | if (error) { | | 2588 | if (error) { |
2589 | *st->lengthPtr = st->cp - st->start + 1; | | 2589 | *st->lengthPtr = st->cp - st->start + 1; |
2590 | VarREError(error, &pattern.re, "RE substitution error"); | | 2590 | VarREError(error, &pattern.re, "RE substitution error"); |
2591 | free(pattern.replace); | | 2591 | free(pattern.replace); |
2592 | return FALSE; | | 2592 | return FALSE; |
2593 | } | | 2593 | } |
2594 | | | 2594 | |
2595 | pattern.nsub = pattern.re.re_nsub + 1; | | 2595 | pattern.nsub = pattern.re.re_nsub + 1; |
2596 | if (pattern.nsub < 1) | | 2596 | if (pattern.nsub < 1) |
2597 | pattern.nsub = 1; | | 2597 | pattern.nsub = 1; |
2598 | if (pattern.nsub > 10) | | 2598 | if (pattern.nsub > 10) |
2599 | pattern.nsub = 10; | | 2599 | pattern.nsub = 10; |
2600 | pattern.matches = bmake_malloc(pattern.nsub * sizeof(regmatch_t)); | | 2600 | pattern.matches = bmake_malloc(pattern.nsub * sizeof(regmatch_t)); |
2601 | st->newStr = VarModify( | | 2601 | st->newStr = VarModify( |
2602 | st->ctxt, &tmpparsestate, st->nstr, VarRESubstitute, &pattern); | | 2602 | st->ctxt, &tmpparsestate, st->nstr, VarRESubstitute, &pattern); |
2603 | regfree(&pattern.re); | | 2603 | regfree(&pattern.re); |
2604 | free(pattern.replace); | | 2604 | free(pattern.replace); |
2605 | free(pattern.matches); | | 2605 | free(pattern.matches); |
2606 | st->delim = '\0'; | | 2606 | st->delim = '\0'; |
2607 | return TRUE; | | 2607 | return TRUE; |
2608 | } | | 2608 | } |
2609 | #endif | | 2609 | #endif |
2610 | | | 2610 | |
2611 | static void | | 2611 | static void |
2612 | VarModify_Copy(GNode *ctx MAKE_ATTR_UNUSED, const char *word, SepBuf *buf, | | 2612 | VarModify_Copy(GNode *ctx MAKE_ATTR_UNUSED, const char *word, SepBuf *buf, |
2613 | void *data MAKE_ATTR_UNUSED) | | 2613 | void *data MAKE_ATTR_UNUSED) |
2614 | { | | 2614 | { |
2615 | SepBuf_AddBytes(buf, word, strlen(word)); | | 2615 | SepBuf_AddBytes(buf, word, strlen(word)); |
2616 | } | | 2616 | } |
2617 | | | 2617 | |
2618 | /* :tA, :tu, :tl, etc. */ | | 2618 | /* :tA, :tu, :tl, etc. */ |
2619 | static Boolean | | 2619 | static Boolean |
2620 | ApplyModifier_To(ApplyModifiersState *st) | | 2620 | ApplyModifier_To(ApplyModifiersState *st) |
2621 | { | | 2621 | { |
2622 | st->cp = st->tstr + 1; /* make sure it is set */ | | 2622 | st->cp = st->tstr + 1; /* make sure it is set */ |
2623 | if (st->tstr[1] != st->endc && st->tstr[1] != ':') { | | 2623 | if (st->tstr[1] != st->endc && st->tstr[1] != ':') { |
2624 | if (st->tstr[1] == 's') { | | 2624 | if (st->tstr[1] == 's') { |
2625 | /* Use the char (if any) at st->tstr[2] as the word separator. */ | | 2625 | /* Use the char (if any) at st->tstr[2] as the word separator. */ |
2626 | | | 2626 | |
2627 | if (st->tstr[2] != st->endc && | | 2627 | if (st->tstr[2] != st->endc && |
2628 | (st->tstr[3] == st->endc || st->tstr[3] == ':')) { | | 2628 | (st->tstr[3] == st->endc || st->tstr[3] == ':')) { |
2629 | /* ":ts<unrecognised><endc>" or | | 2629 | /* ":ts<unrecognised><endc>" or |
2630 | * ":ts<unrecognised>:" */ | | 2630 | * ":ts<unrecognised>:" */ |
2631 | st->parsestate.varSpace = st->tstr[2]; | | 2631 | st->parsestate.varSpace = st->tstr[2]; |
2632 | st->cp = st->tstr + 3; | | 2632 | st->cp = st->tstr + 3; |
2633 | } else if (st->tstr[2] == st->endc || st->tstr[2] == ':') { | | 2633 | } else if (st->tstr[2] == st->endc || st->tstr[2] == ':') { |
2634 | /* ":ts<endc>" or ":ts:" */ | | 2634 | /* ":ts<endc>" or ":ts:" */ |
2635 | st->parsestate.varSpace = 0; /* no separator */ | | 2635 | st->parsestate.varSpace = 0; /* no separator */ |
2636 | st->cp = st->tstr + 2; | | 2636 | st->cp = st->tstr + 2; |
2637 | } else if (st->tstr[2] == '\\') { | | 2637 | } else if (st->tstr[2] == '\\') { |
2638 | const char *xp = &st->tstr[3]; | | 2638 | const char *xp = &st->tstr[3]; |
2639 | int base = 8; /* assume octal */ | | 2639 | int base = 8; /* assume octal */ |
2640 | | | 2640 | |
2641 | switch (st->tstr[3]) { | | 2641 | switch (st->tstr[3]) { |
2642 | case 'n': | | 2642 | case 'n': |
2643 | st->parsestate.varSpace = '\n'; | | 2643 | st->parsestate.varSpace = '\n'; |
2644 | st->cp = st->tstr + 4; | | 2644 | st->cp = st->tstr + 4; |
2645 | break; | | 2645 | break; |
2646 | case 't': | | 2646 | case 't': |
2647 | st->parsestate.varSpace = '\t'; | | 2647 | st->parsestate.varSpace = '\t'; |
2648 | st->cp = st->tstr + 4; | | 2648 | st->cp = st->tstr + 4; |
2649 | break; | | 2649 | break; |
2650 | case 'x': | | 2650 | case 'x': |
2651 | base = 16; | | 2651 | base = 16; |
2652 | xp++; | | 2652 | xp++; |
2653 | goto get_numeric; | | 2653 | goto get_numeric; |
2654 | case '0': | | 2654 | case '0': |
2655 | base = 0; | | 2655 | base = 0; |
2656 | goto get_numeric; | | 2656 | goto get_numeric; |
2657 | default: | | 2657 | default: |
2658 | if (isdigit((unsigned char)st->tstr[3])) { | | 2658 | if (isdigit((unsigned char)st->tstr[3])) { |
2659 | char *ep; | | 2659 | char *ep; |
2660 | get_numeric: | | 2660 | get_numeric: |
2661 | st->parsestate.varSpace = strtoul(xp, &ep, base); | | 2661 | st->parsestate.varSpace = strtoul(xp, &ep, base); |
2662 | if (*ep != ':' && *ep != st->endc) | | 2662 | if (*ep != ':' && *ep != st->endc) |
2663 | return FALSE; | | 2663 | return FALSE; |
2664 | st->cp = ep; | | 2664 | st->cp = ep; |
2665 | } else { | | 2665 | } else { |
2666 | /* ":ts<backslash><unrecognised>". */ | | 2666 | /* ":ts<backslash><unrecognised>". */ |
2667 | return FALSE; | | 2667 | return FALSE; |
2668 | } | | 2668 | } |
2669 | break; | | 2669 | break; |
2670 | } | | 2670 | } |
2671 | } else { | | 2671 | } else { |
2672 | /* Found ":ts<unrecognised><unrecognised>". */ | | 2672 | /* Found ":ts<unrecognised><unrecognised>". */ |
2673 | return FALSE; | | 2673 | return FALSE; |
2674 | } | | 2674 | } |
2675 | | | 2675 | |
2676 | st->termc = *st->cp; | | 2676 | st->termc = *st->cp; |
2677 | st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, | | 2677 | st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, |
2678 | VarModify_Copy, NULL); | | 2678 | VarModify_Copy, NULL); |
2679 | } else if (st->tstr[2] == st->endc || st->tstr[2] == ':') { | | 2679 | } else if (st->tstr[2] == st->endc || st->tstr[2] == ':') { |
2680 | /* Check for two-character options: ":tu", ":tl" */ | | 2680 | /* Check for two-character options: ":tu", ":tl" */ |
2681 | if (st->tstr[1] == 'A') { /* absolute path */ | | 2681 | if (st->tstr[1] == 'A') { /* absolute path */ |
2682 | st->newStr = VarModify( | | 2682 | st->newStr = VarModify( |
2683 | st->ctxt, &st->parsestate, st->nstr, VarRealpath, NULL); | | 2683 | st->ctxt, &st->parsestate, st->nstr, VarRealpath, NULL); |
2684 | st->cp = st->tstr + 2; | | 2684 | st->cp = st->tstr + 2; |
2685 | st->termc = *st->cp; | | 2685 | st->termc = *st->cp; |
2686 | } else if (st->tstr[1] == 'u') { | | 2686 | } else if (st->tstr[1] == 'u') { |
2687 | char *dp = bmake_strdup(st->nstr); | | 2687 | char *dp = bmake_strdup(st->nstr); |
2688 | for (st->newStr = dp; *dp; dp++) | | 2688 | for (st->newStr = dp; *dp; dp++) |
2689 | *dp = toupper((unsigned char)*dp); | | 2689 | *dp = toupper((unsigned char)*dp); |
2690 | st->cp = st->tstr + 2; | | 2690 | st->cp = st->tstr + 2; |
2691 | st->termc = *st->cp; | | 2691 | st->termc = *st->cp; |
2692 | } else if (st->tstr[1] == 'l') { | | 2692 | } else if (st->tstr[1] == 'l') { |
2693 | char *dp = bmake_strdup(st->nstr); | | 2693 | char *dp = bmake_strdup(st->nstr); |
2694 | for (st->newStr = dp; *dp; dp++) | | 2694 | for (st->newStr = dp; *dp; dp++) |
2695 | *dp = tolower((unsigned char)*dp); | | 2695 | *dp = tolower((unsigned char)*dp); |
2696 | st->cp = st->tstr + 2; | | 2696 | st->cp = st->tstr + 2; |
2697 | st->termc = *st->cp; | | 2697 | st->termc = *st->cp; |
2698 | } else if (st->tstr[1] == 'W' || st->tstr[1] == 'w') { | | 2698 | } else if (st->tstr[1] == 'W' || st->tstr[1] == 'w') { |
2699 | st->parsestate.oneBigWord = (st->tstr[1] == 'W'); | | 2699 | st->parsestate.oneBigWord = (st->tstr[1] == 'W'); |
2700 | st->newStr = st->nstr; | | 2700 | st->newStr = st->nstr; |
2701 | st->cp = st->tstr + 2; | | 2701 | st->cp = st->tstr + 2; |
2702 | st->termc = *st->cp; | | 2702 | st->termc = *st->cp; |
2703 | } else { | | 2703 | } else { |
2704 | /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */ | | 2704 | /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */ |
2705 | return FALSE; | | 2705 | return FALSE; |
2706 | } | | 2706 | } |
2707 | } else { | | 2707 | } else { |
2708 | /* Found ":t<unrecognised><unrecognised>". */ | | 2708 | /* Found ":t<unrecognised><unrecognised>". */ |
2709 | return FALSE; | | 2709 | return FALSE; |
2710 | } | | 2710 | } |
2711 | } else { | | 2711 | } else { |
2712 | /* Found ":t<endc>" or ":t:". */ | | 2712 | /* Found ":t<endc>" or ":t:". */ |
2713 | return FALSE; | | 2713 | return FALSE; |
2714 | } | | 2714 | } |
2715 | return TRUE; | | 2715 | return TRUE; |
2716 | } | | 2716 | } |
2717 | | | 2717 | |
2718 | /* :[#], :[1], etc. */ | | 2718 | /* :[#], :[1], etc. */ |
2719 | static int | | 2719 | static int |
2720 | ApplyModifier_Words(ApplyModifiersState *st) | | 2720 | ApplyModifier_Words(ApplyModifiersState *st) |
2721 | { | | 2721 | { |
2722 | st->cp = st->tstr + 1; /* point to char after '[' */ | | 2722 | st->cp = st->tstr + 1; /* point to char after '[' */ |
2723 | st->delim = ']'; /* look for closing ']' */ | | 2723 | st->delim = ']'; /* look for closing ']' */ |
2724 | char *estr = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 2724 | char *estr = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
2725 | NULL, NULL, NULL); | | 2725 | NULL, NULL, NULL); |
2726 | if (estr == NULL) | | 2726 | if (estr == NULL) |
2727 | return 'c'; /* report missing ']' */ | | 2727 | return 'c'; /* report missing ']' */ |
2728 | /* now st->cp points just after the closing ']' */ | | 2728 | /* now st->cp points just after the closing ']' */ |
2729 | st->delim = '\0'; | | 2729 | st->delim = '\0'; |
2730 | if (st->cp[0] != ':' && st->cp[0] != st->endc) { | | 2730 | if (st->cp[0] != ':' && st->cp[0] != st->endc) { |
2731 | /* Found junk after ']' */ | | 2731 | /* Found junk after ']' */ |
2732 | free(estr); | | 2732 | free(estr); |
2733 | return 'b'; | | 2733 | return 'b'; |
2734 | } | | 2734 | } |
2735 | if (estr[0] == '\0') { | | 2735 | if (estr[0] == '\0') { |
2736 | /* Found empty square brackets in ":[]". */ | | 2736 | /* Found empty square brackets in ":[]". */ |
2737 | free(estr); | | 2737 | free(estr); |
2738 | return 'b'; | | 2738 | return 'b'; |
2739 | } else if (estr[0] == '#' && estr[1] == '\0') { | | 2739 | } else if (estr[0] == '#' && estr[1] == '\0') { |
2740 | /* Found ":[#]" */ | | 2740 | /* Found ":[#]" */ |
2741 | | | 2741 | |
2742 | /* | | 2742 | /* |
2743 | * We will need enough space for the decimal | | 2743 | * We will need enough space for the decimal |
2744 | * representation of an int. We calculate the | | 2744 | * representation of an int. We calculate the |
2745 | * space needed for the octal representation, | | 2745 | * space needed for the octal representation, |
2746 | * and add enough slop to cope with a '-' sign | | 2746 | * and add enough slop to cope with a '-' sign |
2747 | * (which should never be needed) and a '\0' | | 2747 | * (which should never be needed) and a '\0' |
2748 | * string terminator. | | 2748 | * string terminator. |
2749 | */ | | 2749 | */ |
2750 | int newStrSize = (sizeof(int) * CHAR_BIT + 2) / 3 + 2; | | 2750 | int newStrSize = (sizeof(int) * CHAR_BIT + 2) / 3 + 2; |
2751 | | | 2751 | |
2752 | st->newStr = bmake_malloc(newStrSize); | | 2752 | st->newStr = bmake_malloc(newStrSize); |
2753 | if (st->parsestate.oneBigWord) { | | 2753 | if (st->parsestate.oneBigWord) { |
2754 | strncpy(st->newStr, "1", newStrSize); | | 2754 | strncpy(st->newStr, "1", newStrSize); |
2755 | } else { | | 2755 | } else { |
2756 | /* XXX: brk_string() is a rather expensive | | 2756 | /* XXX: brk_string() is a rather expensive |
2757 | * way of counting words. */ | | 2757 | * way of counting words. */ |
2758 | char **av; | | 2758 | char **av; |
2759 | char *as; | | 2759 | char *as; |
2760 | int ac; | | 2760 | int ac; |
2761 | | | 2761 | |
2762 | av = brk_string(st->nstr, &ac, FALSE, &as); | | 2762 | av = brk_string(st->nstr, &ac, FALSE, &as); |
2763 | snprintf(st->newStr, newStrSize, "%d", ac); | | 2763 | snprintf(st->newStr, newStrSize, "%d", ac); |
2764 | free(as); | | 2764 | free(as); |
2765 | free(av); | | 2765 | free(av); |
2766 | } | | 2766 | } |
2767 | st->termc = *st->cp; | | 2767 | st->termc = *st->cp; |
2768 | free(estr); | | 2768 | free(estr); |
2769 | return 0; | | 2769 | return 0; |
2770 | } else if (estr[0] == '*' && estr[1] == '\0') { | | 2770 | } else if (estr[0] == '*' && estr[1] == '\0') { |
2771 | /* Found ":[*]" */ | | 2771 | /* Found ":[*]" */ |
2772 | st->parsestate.oneBigWord = TRUE; | | 2772 | st->parsestate.oneBigWord = TRUE; |
2773 | st->newStr = st->nstr; | | 2773 | st->newStr = st->nstr; |
2774 | st->termc = *st->cp; | | 2774 | st->termc = *st->cp; |
2775 | free(estr); | | 2775 | free(estr); |
2776 | return 0; | | 2776 | return 0; |
2777 | } else if (estr[0] == '@' && estr[1] == '\0') { | | 2777 | } else if (estr[0] == '@' && estr[1] == '\0') { |
2778 | /* Found ":[@]" */ | | 2778 | /* Found ":[@]" */ |
2779 | st->parsestate.oneBigWord = FALSE; | | 2779 | st->parsestate.oneBigWord = FALSE; |
2780 | st->newStr = st->nstr; | | 2780 | st->newStr = st->nstr; |
2781 | st->termc = *st->cp; | | 2781 | st->termc = *st->cp; |
2782 | free(estr); | | 2782 | free(estr); |
2783 | return 0; | | 2783 | return 0; |
2784 | } else { | | 2784 | } else { |
2785 | char *ep; | | 2785 | char *ep; |
2786 | /* | | 2786 | /* |
2787 | * We expect estr to contain a single | | 2787 | * We expect estr to contain a single |
2788 | * integer for :[N], or two integers | | 2788 | * integer for :[N], or two integers |
2789 | * separated by ".." for :[start..end]. | | 2789 | * separated by ".." for :[start..end]. |
2790 | */ | | 2790 | */ |
2791 | VarSelectWords_t seldata = { 0, 0 }; | | 2791 | VarSelectWords_t seldata = { 0, 0 }; |
2792 | | | 2792 | |
2793 | seldata.start = strtol(estr, &ep, 0); | | 2793 | seldata.start = strtol(estr, &ep, 0); |
2794 | if (ep == estr) { | | 2794 | if (ep == estr) { |
2795 | /* Found junk instead of a number */ | | 2795 | /* Found junk instead of a number */ |
2796 | free(estr); | | 2796 | free(estr); |
2797 | return 'b'; | | 2797 | return 'b'; |
2798 | } else if (ep[0] == '\0') { | | 2798 | } else if (ep[0] == '\0') { |
2799 | /* Found only one integer in :[N] */ | | 2799 | /* Found only one integer in :[N] */ |
2800 | seldata.end = seldata.start; | | 2800 | seldata.end = seldata.start; |
2801 | } else if (ep[0] == '.' && ep[1] == '.' && ep[2] != '\0') { | | 2801 | } else if (ep[0] == '.' && ep[1] == '.' && ep[2] != '\0') { |
2802 | /* Expecting another integer after ".." */ | | 2802 | /* Expecting another integer after ".." */ |
2803 | ep += 2; | | 2803 | ep += 2; |
2804 | seldata.end = strtol(ep, &ep, 0); | | 2804 | seldata.end = strtol(ep, &ep, 0); |
2805 | if (ep[0] != '\0') { | | 2805 | if (ep[0] != '\0') { |
2806 | /* Found junk after ".." */ | | 2806 | /* Found junk after ".." */ |
2807 | free(estr); | | 2807 | free(estr); |
2808 | return 'b'; | | 2808 | return 'b'; |
2809 | } | | 2809 | } |
2810 | } else { | | 2810 | } else { |
2811 | /* Found junk instead of ".." */ | | 2811 | /* Found junk instead of ".." */ |
2812 | free(estr); | | 2812 | free(estr); |
2813 | return 'b'; | | 2813 | return 'b'; |
2814 | } | | 2814 | } |
2815 | /* | | 2815 | /* |
2816 | * Now seldata is properly filled in, | | 2816 | * Now seldata is properly filled in, |
2817 | * but we still have to check for 0 as | | 2817 | * but we still have to check for 0 as |
2818 | * a special case. | | 2818 | * a special case. |
2819 | */ | | 2819 | */ |
2820 | if (seldata.start == 0 && seldata.end == 0) { | | 2820 | if (seldata.start == 0 && seldata.end == 0) { |
2821 | /* ":[0]" or perhaps ":[0..0]" */ | | 2821 | /* ":[0]" or perhaps ":[0..0]" */ |
2822 | st->parsestate.oneBigWord = TRUE; | | 2822 | st->parsestate.oneBigWord = TRUE; |
2823 | st->newStr = st->nstr; | | 2823 | st->newStr = st->nstr; |
2824 | st->termc = *st->cp; | | 2824 | st->termc = *st->cp; |
2825 | free(estr); | | 2825 | free(estr); |
2826 | return 0; | | 2826 | return 0; |
2827 | } else if (seldata.start == 0 || seldata.end == 0) { | | 2827 | } else if (seldata.start == 0 || seldata.end == 0) { |
2828 | /* ":[0..N]" or ":[N..0]" */ | | 2828 | /* ":[0..N]" or ":[N..0]" */ |
2829 | free(estr); | | 2829 | free(estr); |
2830 | return 'b'; | | 2830 | return 'b'; |
2831 | } | | 2831 | } |
2832 | /* Normal case: select the words described by seldata. */ | | 2832 | /* Normal case: select the words described by seldata. */ |
2833 | st->newStr = VarSelectWords( | | 2833 | st->newStr = VarSelectWords( |
2834 | st->ctxt, &st->parsestate, st->nstr, &seldata); | | 2834 | st->ctxt, &st->parsestate, st->nstr, &seldata); |
2835 | | | 2835 | |
2836 | st->termc = *st->cp; | | 2836 | st->termc = *st->cp; |
2837 | free(estr); | | 2837 | free(estr); |
2838 | return 0; | | 2838 | return 0; |
2839 | } | | 2839 | } |
2840 | } | | 2840 | } |
2841 | | | 2841 | |
2842 | /* :O or :Ox */ | | 2842 | /* :O or :Ox */ |
2843 | static Boolean | | 2843 | static Boolean |
2844 | ApplyModifier_Order(ApplyModifiersState *st) | | 2844 | ApplyModifier_Order(ApplyModifiersState *st) |
2845 | { | | 2845 | { |
2846 | char otype; | | 2846 | char otype; |
2847 | | | 2847 | |
2848 | st->cp = st->tstr + 1; /* skip to the rest in any case */ | | 2848 | st->cp = st->tstr + 1; /* skip to the rest in any case */ |
2849 | if (st->tstr[1] == st->endc || st->tstr[1] == ':') { | | 2849 | if (st->tstr[1] == st->endc || st->tstr[1] == ':') { |
2850 | otype = 's'; | | 2850 | otype = 's'; |
2851 | st->termc = *st->cp; | | 2851 | st->termc = *st->cp; |
2852 | } else if ((st->tstr[1] == 'r' || st->tstr[1] == 'x') && | | 2852 | } else if ((st->tstr[1] == 'r' || st->tstr[1] == 'x') && |
2853 | (st->tstr[2] == st->endc || st->tstr[2] == ':')) { | | 2853 | (st->tstr[2] == st->endc || st->tstr[2] == ':')) { |
2854 | otype = st->tstr[1]; | | 2854 | otype = st->tstr[1]; |
2855 | st->cp = st->tstr + 2; | | 2855 | st->cp = st->tstr + 2; |
2856 | st->termc = *st->cp; | | 2856 | st->termc = *st->cp; |
2857 | } else { | | 2857 | } else { |
2858 | return FALSE; | | 2858 | return FALSE; |
2859 | } | | 2859 | } |
2860 | st->newStr = VarOrder(st->nstr, otype); | | 2860 | st->newStr = VarOrder(st->nstr, otype); |
2861 | return TRUE; | | 2861 | return TRUE; |
2862 | } | | 2862 | } |
2863 | | | 2863 | |
2864 | /* :? then : else */ | | 2864 | /* :? then : else */ |
2865 | static Boolean | | 2865 | static Boolean |
2866 | ApplyModifier_IfElse(ApplyModifiersState *st) | | 2866 | ApplyModifier_IfElse(ApplyModifiersState *st) |
2867 | { | | 2867 | { |
2868 | Boolean value; | | 2868 | Boolean value; |
2869 | int cond_rc; | | 2869 | int cond_rc; |
2870 | VarPatternFlags then_flags, else_flags; | | 2870 | VarPatternFlags then_flags, else_flags; |
2871 | /* FIXME: IfElse has nothing to do with VarPatternFlags */ | | 2871 | /* FIXME: IfElse has nothing to do with VarPatternFlags */ |
2872 | | | 2872 | |
2873 | /* find ':', and then substitute accordingly */ | | 2873 | /* find ':', and then substitute accordingly */ |
2874 | if (st->eflags & VARE_WANTRES) { | | 2874 | if (st->eflags & VARE_WANTRES) { |
2875 | cond_rc = Cond_EvalExpression(NULL, st->v->name, &value, 0, FALSE); | | 2875 | cond_rc = Cond_EvalExpression(NULL, st->v->name, &value, 0, FALSE); |
2876 | then_flags = cond_rc != COND_INVALID && value ? 0 : VAR_NOSUBST; | | 2876 | then_flags = cond_rc != COND_INVALID && value ? 0 : VAR_NOSUBST; |
2877 | else_flags = cond_rc != COND_INVALID && !value ? 0 : VAR_NOSUBST; | | 2877 | else_flags = cond_rc != COND_INVALID && !value ? 0 : VAR_NOSUBST; |
2878 | } else { | | 2878 | } else { |
2879 | /* we are just consuming and discarding */ | | 2879 | /* we are just consuming and discarding */ |
2880 | cond_rc = value = 0; | | 2880 | cond_rc = value = 0; |
2881 | then_flags = else_flags = VAR_NOSUBST; | | 2881 | then_flags = else_flags = VAR_NOSUBST; |
2882 | } | | 2882 | } |
2883 | | | 2883 | |
2884 | st->cp = ++st->tstr; | | 2884 | st->cp = ++st->tstr; |
2885 | st->delim = ':'; | | 2885 | st->delim = ':'; |
2886 | char *then_expr = ParseModifierPart( | | 2886 | char *then_expr = ParseModifierPart( |
2887 | st->ctxt, &st->cp, st->delim, st->eflags, &then_flags, NULL, NULL); | | 2887 | st->ctxt, &st->cp, st->delim, st->eflags, &then_flags, NULL, NULL); |
2888 | if (then_expr == NULL) | | 2888 | if (then_expr == NULL) |
2889 | return FALSE; | | 2889 | return FALSE; |
2890 | | | 2890 | |
2891 | st->delim = st->endc; /* BRCLOSE or PRCLOSE */ | | 2891 | st->delim = st->endc; /* BRCLOSE or PRCLOSE */ |
2892 | char *else_expr = ParseModifierPart( | | 2892 | char *else_expr = ParseModifierPart( |
2893 | st->ctxt, &st->cp, st->delim, st->eflags, &else_flags, NULL, NULL); | | 2893 | st->ctxt, &st->cp, st->delim, st->eflags, &else_flags, NULL, NULL); |
2894 | if (else_expr == NULL) | | 2894 | if (else_expr == NULL) |
2895 | return FALSE; | | 2895 | return FALSE; |
2896 | | | 2896 | |
2897 | st->termc = *--st->cp; | | 2897 | st->termc = *--st->cp; |
2898 | st->delim = '\0'; | | 2898 | st->delim = '\0'; |
2899 | if (cond_rc == COND_INVALID) { | | 2899 | if (cond_rc == COND_INVALID) { |
2900 | Error("Bad conditional expression `%s' in %s?%s:%s", | | 2900 | Error("Bad conditional expression `%s' in %s?%s:%s", |
2901 | st->v->name, st->v->name, then_expr, else_expr); | | 2901 | st->v->name, st->v->name, then_expr, else_expr); |
2902 | return FALSE; | | 2902 | return FALSE; |
2903 | } | | 2903 | } |
2904 | | | 2904 | |
2905 | if (value) { | | 2905 | if (value) { |
2906 | st->newStr = then_expr; | | 2906 | st->newStr = then_expr; |
2907 | free(else_expr); | | 2907 | free(else_expr); |
2908 | } else { | | 2908 | } else { |
2909 | st->newStr = else_expr; | | 2909 | st->newStr = else_expr; |
2910 | free(then_expr); | | 2910 | free(then_expr); |
2911 | } | | 2911 | } |
2912 | if (st->v->flags & VAR_JUNK) | | 2912 | if (st->v->flags & VAR_JUNK) |
2913 | st->v->flags |= VAR_KEEP; | | 2913 | st->v->flags |= VAR_KEEP; |
2914 | return TRUE; | | 2914 | return TRUE; |
2915 | } | | 2915 | } |
2916 | | | 2916 | |
2917 | /* "::=", "::!=", "::+=", or "::?=" */ | | 2917 | /* |
| | | 2918 | * The ::= modifiers actually assign a value to the variable. |
| | | 2919 | * Their main purpose is in supporting modifiers of .for loop |
| | | 2920 | * iterators and other obscure uses. They always expand to |
| | | 2921 | * nothing. In a target rule that would otherwise expand to an |
| | | 2922 | * empty line they can be preceded with @: to keep make happy. |
| | | 2923 | * Eg. |
| | | 2924 | * |
| | | 2925 | * foo: .USE |
| | | 2926 | * .for i in ${.TARGET} ${.TARGET:R}.gz |
| | | 2927 | * @: ${t::=$i} |
| | | 2928 | * @echo blah ${t:T} |
| | | 2929 | * .endfor |
| | | 2930 | * |
| | | 2931 | * ::=<str> Assigns <str> as the new value of variable. |
| | | 2932 | * ::?=<str> Assigns <str> as value of variable if |
| | | 2933 | * it was not already set. |
| | | 2934 | * ::+=<str> Appends <str> to variable. |
| | | 2935 | * ::!=<cmd> Assigns output of <cmd> as the new value of |
| | | 2936 | * variable. |
| | | 2937 | */ |
2918 | static int | | 2938 | static int |
2919 | ApplyModifier_Assign(ApplyModifiersState *st) | | 2939 | ApplyModifier_Assign(ApplyModifiersState *st) |
2920 | { | | 2940 | { |
2921 | const char *op = st->tstr + 1; | | 2941 | const char *op = st->tstr + 1; |
2922 | if (!(op[0] == '=' || | | 2942 | if (!(op[0] == '=' || |
2923 | (op[1] == '=' && | | 2943 | (op[1] == '=' && |
2924 | (op[0] == '!' || op[0] == '+' || op[0] == '?')))) | | 2944 | (op[0] == '!' || op[0] == '+' || op[0] == '?')))) |
2925 | return 'd'; /* "::<unrecognised>" */ | | 2945 | return 'd'; /* "::<unrecognised>" */ |
2926 | | | 2946 | |
2927 | GNode *v_ctxt; /* context where v belongs */ | | 2947 | GNode *v_ctxt; /* context where v belongs */ |
2928 | VarPatternFlags pflags; | | 2948 | VarPatternFlags pflags; |
2929 | /* FIXME: Assign has nothing to do with VarPatternFlags */ | | 2949 | /* FIXME: Assign has nothing to do with VarPatternFlags */ |
2930 | | | 2950 | |
2931 | if (st->v->name[0] == 0) | | 2951 | if (st->v->name[0] == 0) |
2932 | return 'b'; | | 2952 | return 'b'; |
2933 | | | 2953 | |
2934 | v_ctxt = st->ctxt; | | 2954 | v_ctxt = st->ctxt; |
2935 | char *sv_name = NULL; | | 2955 | char *sv_name = NULL; |
2936 | ++st->tstr; | | 2956 | ++st->tstr; |
2937 | if (st->v->flags & VAR_JUNK) { | | 2957 | if (st->v->flags & VAR_JUNK) { |
2938 | /* | | 2958 | /* |
2939 | * We need to bmake_strdup() it incase ParseModifierPart() recurses. | | 2959 | * We need to bmake_strdup() it incase ParseModifierPart() recurses. |
2940 | */ | | 2960 | */ |
2941 | sv_name = st->v->name; | | 2961 | sv_name = st->v->name; |
2942 | st->v->name = bmake_strdup(st->v->name); | | 2962 | st->v->name = bmake_strdup(st->v->name); |
2943 | } else if (st->ctxt != VAR_GLOBAL) { | | 2963 | } else if (st->ctxt != VAR_GLOBAL) { |
2944 | Var *gv = VarFind(st->v->name, st->ctxt, 0); | | 2964 | Var *gv = VarFind(st->v->name, st->ctxt, 0); |
2945 | if (gv == NULL) | | 2965 | if (gv == NULL) |
2946 | v_ctxt = VAR_GLOBAL; | | 2966 | v_ctxt = VAR_GLOBAL; |
2947 | else | | 2967 | else |
2948 | VarFreeEnv(gv, TRUE); | | 2968 | VarFreeEnv(gv, TRUE); |
2949 | } | | 2969 | } |
2950 | | | 2970 | |
2951 | switch (op[0]) { | | 2971 | switch (op[0]) { |
2952 | case '+': | | 2972 | case '+': |
2953 | case '?': | | 2973 | case '?': |
2954 | case '!': | | 2974 | case '!': |
2955 | st->cp = &st->tstr[2]; | | 2975 | st->cp = &st->tstr[2]; |
2956 | break; | | 2976 | break; |
2957 | default: | | 2977 | default: |
2958 | st->cp = ++st->tstr; | | 2978 | st->cp = ++st->tstr; |
2959 | break; | | 2979 | break; |
2960 | } | | 2980 | } |
2961 | st->delim = st->startc == PROPEN ? PRCLOSE : BRCLOSE; | | 2981 | st->delim = st->startc == PROPEN ? PRCLOSE : BRCLOSE; |
2962 | | | 2982 | |
2963 | pflags = (st->eflags & VARE_WANTRES) ? 0 : VAR_NOSUBST; | | 2983 | pflags = (st->eflags & VARE_WANTRES) ? 0 : VAR_NOSUBST; |
2964 | char *val = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 2984 | char *val = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
2965 | &pflags, NULL, NULL); | | 2985 | &pflags, NULL, NULL); |
2966 | if (st->v->flags & VAR_JUNK) { | | 2986 | if (st->v->flags & VAR_JUNK) { |
2967 | /* restore original name */ | | 2987 | /* restore original name */ |
2968 | free(st->v->name); | | 2988 | free(st->v->name); |
2969 | st->v->name = sv_name; | | 2989 | st->v->name = sv_name; |
2970 | } | | 2990 | } |
2971 | if (val == NULL) | | 2991 | if (val == NULL) |
2972 | return 'c'; | | 2992 | return 'c'; |
2973 | | | 2993 | |
2974 | st->termc = *--st->cp; | | 2994 | st->termc = *--st->cp; |
2975 | st->delim = '\0'; | | 2995 | st->delim = '\0'; |
2976 | | | 2996 | |
2977 | if (st->eflags & VARE_WANTRES) { | | 2997 | if (st->eflags & VARE_WANTRES) { |
2978 | switch (op[0]) { | | 2998 | switch (op[0]) { |
2979 | case '+': | | 2999 | case '+': |
2980 | Var_Append(st->v->name, val, v_ctxt); | | 3000 | Var_Append(st->v->name, val, v_ctxt); |
2981 | break; | | 3001 | break; |
2982 | case '!': { | | 3002 | case '!': { |
2983 | const char *emsg; | | 3003 | const char *emsg; |
2984 | st->newStr = Cmd_Exec(val, &emsg); | | 3004 | st->newStr = Cmd_Exec(val, &emsg); |
2985 | if (emsg) | | 3005 | if (emsg) |
2986 | Error(emsg, st->nstr); | | 3006 | Error(emsg, st->nstr); |
2987 | else | | 3007 | else |
2988 | Var_Set(st->v->name, st->newStr, v_ctxt); | | 3008 | Var_Set(st->v->name, st->newStr, v_ctxt); |
2989 | free(st->newStr); | | 3009 | free(st->newStr); |
2990 | break; | | 3010 | break; |
2991 | } | | 3011 | } |
2992 | case '?': | | 3012 | case '?': |
2993 | if ((st->v->flags & VAR_JUNK) == 0) | | 3013 | if ((st->v->flags & VAR_JUNK) == 0) |
2994 | break; | | 3014 | break; |
2995 | /* FALLTHROUGH */ | | 3015 | /* FALLTHROUGH */ |
2996 | default: | | 3016 | default: |
2997 | Var_Set(st->v->name, val, v_ctxt); | | 3017 | Var_Set(st->v->name, val, v_ctxt); |
2998 | break; | | 3018 | break; |
2999 | } | | 3019 | } |
3000 | } | | 3020 | } |
3001 | free(val); | | 3021 | free(val); |
3002 | st->newStr = varNoError; | | 3022 | st->newStr = varNoError; |
3003 | return 0; | | 3023 | return 0; |
3004 | } | | 3024 | } |
3005 | | | 3025 | |
3006 | /* remember current value */ | | 3026 | /* remember current value */ |
3007 | static Boolean | | 3027 | static Boolean |
3008 | ApplyModifier_Remember(ApplyModifiersState *st) | | 3028 | ApplyModifier_Remember(ApplyModifiersState *st) |
3009 | { | | 3029 | { |
3010 | st->cp = st->tstr + 1; /* make sure it is set */ | | 3030 | st->cp = st->tstr + 1; /* make sure it is set */ |
3011 | if (!STRMOD_MATCHX(st->tstr, "_", 1)) | | 3031 | if (!STRMOD_MATCHX(st->tstr, "_", 1)) |
3012 | return FALSE; | | 3032 | return FALSE; |
3013 | | | 3033 | |
3014 | if (st->tstr[1] == '=') { | | 3034 | if (st->tstr[1] == '=') { |
3015 | char *np; | | 3035 | char *np; |
3016 | int n; | | 3036 | int n; |
3017 | | | 3037 | |
3018 | st->cp++; | | 3038 | st->cp++; |
3019 | n = strcspn(st->cp, ":)}"); | | 3039 | n = strcspn(st->cp, ":)}"); |
3020 | np = bmake_strndup(st->cp, n + 1); | | 3040 | np = bmake_strndup(st->cp, n + 1); |
3021 | np[n] = '\0'; | | 3041 | np[n] = '\0'; |
3022 | st->cp = st->tstr + 2 + n; | | 3042 | st->cp = st->tstr + 2 + n; |
3023 | Var_Set(np, st->nstr, st->ctxt); | | 3043 | Var_Set(np, st->nstr, st->ctxt); |
3024 | free(np); | | 3044 | free(np); |
3025 | } else { | | 3045 | } else { |
3026 | Var_Set("_", st->nstr, st->ctxt); | | 3046 | Var_Set("_", st->nstr, st->ctxt); |
3027 | } | | 3047 | } |
3028 | st->newStr = st->nstr; | | 3048 | st->newStr = st->nstr; |
3029 | st->termc = *st->cp; | | 3049 | st->termc = *st->cp; |
3030 | return TRUE; | | 3050 | return TRUE; |
3031 | } | | 3051 | } |
3032 | | | 3052 | |
3033 | #ifdef SYSVVARSUB | | 3053 | #ifdef SYSVVARSUB |
3034 | /* :from=to */ | | 3054 | /* :from=to */ |
3035 | static int | | 3055 | static int |
3036 | ApplyModifier_SysV(ApplyModifiersState *st) | | 3056 | ApplyModifier_SysV(ApplyModifiersState *st) |
3037 | { | | 3057 | { |
3038 | Boolean eqFound = FALSE; | | 3058 | Boolean eqFound = FALSE; |
3039 | | | 3059 | |
3040 | /* | | 3060 | /* |
3041 | * First we make a pass through the string trying | | 3061 | * First we make a pass through the string trying |
3042 | * to verify it is a SYSV-make-style translation: | | 3062 | * to verify it is a SYSV-make-style translation: |
3043 | * it must be: <string1>=<string2>) | | 3063 | * it must be: <string1>=<string2>) |
3044 | */ | | 3064 | */ |
3045 | st->cp = st->tstr; | | 3065 | st->cp = st->tstr; |
3046 | int nest = 1; | | 3066 | int nest = 1; |
3047 | while (*st->cp != '\0' && nest > 0) { | | 3067 | while (*st->cp != '\0' && nest > 0) { |
3048 | if (*st->cp == '=') { | | 3068 | if (*st->cp == '=') { |
3049 | eqFound = TRUE; | | 3069 | eqFound = TRUE; |
3050 | /* continue looking for st->endc */ | | 3070 | /* continue looking for st->endc */ |
3051 | } else if (*st->cp == st->endc) | | 3071 | } else if (*st->cp == st->endc) |
3052 | nest--; | | 3072 | nest--; |
3053 | else if (*st->cp == st->startc) | | 3073 | else if (*st->cp == st->startc) |
3054 | nest++; | | 3074 | nest++; |
3055 | if (nest > 0) | | 3075 | if (nest > 0) |
3056 | st->cp++; | | 3076 | st->cp++; |
3057 | } | | 3077 | } |
3058 | if (*st->cp != st->endc || !eqFound) | | 3078 | if (*st->cp != st->endc || !eqFound) |
3059 | return 0; | | 3079 | return 0; |
3060 | | | 3080 | |
3061 | st->delim = '='; | | 3081 | st->delim = '='; |
3062 | st->cp = st->tstr; | | 3082 | st->cp = st->tstr; |
3063 | VarPatternFlags pflags = 0; | | 3083 | VarPatternFlags pflags = 0; |
3064 | /* FIXME: There's no point in having a single $ at the end of a | | 3084 | /* FIXME: There's no point in having a single $ at the end of a |
3065 | * SysV substitution since that will not be interpreted as an | | 3085 | * SysV substitution since that will not be interpreted as an |
3066 | * anchor anyway. */ | | 3086 | * anchor anyway. */ |
3067 | char *lhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 3087 | char *lhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
3068 | &pflags, NULL, NULL); | | 3088 | &pflags, NULL, NULL); |
3069 | if (lhs == NULL) | | 3089 | if (lhs == NULL) |
3070 | return 'c'; | | 3090 | return 'c'; |
3071 | | | 3091 | |
3072 | st->delim = st->endc; | | 3092 | st->delim = st->endc; |
3073 | char *rhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, | | 3093 | char *rhs = ParseModifierPart(st->ctxt, &st->cp, st->delim, st->eflags, |
3074 | NULL, NULL, NULL); | | 3094 | NULL, NULL, NULL); |
3075 | if (rhs == NULL) | | 3095 | if (rhs == NULL) |
3076 | return 'c'; | | 3096 | return 'c'; |
3077 | | | 3097 | |
3078 | /* | | 3098 | /* |
3079 | * SYSV modifications happen through the whole | | 3099 | * SYSV modifications happen through the whole |
3080 | * string. Note the pattern is anchored at the end. | | 3100 | * string. Note the pattern is anchored at the end. |
3081 | */ | | 3101 | */ |
3082 | st->termc = *--st->cp; | | 3102 | st->termc = *--st->cp; |
3083 | st->delim = '\0'; | | 3103 | st->delim = '\0'; |
3084 | if (lhs[0] == '\0' && *st->nstr == '\0') { | | 3104 | if (lhs[0] == '\0' && *st->nstr == '\0') { |
3085 | st->newStr = st->nstr; /* special case */ | | 3105 | st->newStr = st->nstr; /* special case */ |
3086 | } else { | | 3106 | } else { |
3087 | VarSYSVSubstArgs args = { lhs, rhs }; | | 3107 | VarSYSVSubstArgs args = { lhs, rhs }; |
3088 | st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, | | 3108 | st->newStr = VarModify(st->ctxt, &st->parsestate, st->nstr, |
3089 | VarSYSVSubst, &args); | | 3109 | VarSYSVSubst, &args); |
3090 | } | | 3110 | } |
3091 | free(lhs); | | 3111 | free(lhs); |
3092 | free(rhs); | | 3112 | free(rhs); |
3093 | return '='; | | 3113 | return '='; |
3094 | } | | 3114 | } |
3095 | #endif | | 3115 | #endif |
3096 | | | 3116 | |
3097 | /* | | 3117 | /* |
3098 | * Now we need to apply any modifiers the user wants applied. | | 3118 | * Now we need to apply any modifiers the user wants applied. |
3099 | * These are: | | 3119 | * These are: |
3100 | * :M<pattern> words which match the given <pattern>. | | 3120 | * :M<pattern> words which match the given <pattern>. |
3101 | * <pattern> is of the standard file | | 3121 | * <pattern> is of the standard file |
3102 | * wildcarding form. | | 3122 | * wildcarding form. |
3103 | * :N<pattern> words which do not match the given <pattern>. | | 3123 | * :N<pattern> words which do not match the given <pattern>. |
3104 | * :S<d><pat1><d><pat2><d>[1gW] | | 3124 | * :S<d><pat1><d><pat2><d>[1gW] |
3105 | * Substitute <pat2> for <pat1> in the value | | 3125 | * Substitute <pat2> for <pat1> in the value |
3106 | * :C<d><pat1><d><pat2><d>[1gW] | | 3126 | * :C<d><pat1><d><pat2><d>[1gW] |
3107 | * Substitute <pat2> for regex <pat1> in the value | | 3127 | * Substitute <pat2> for regex <pat1> in the value |
3108 | * :H Substitute the head of each word | | 3128 | * :H Substitute the head of each word |
3109 | * :T Substitute the tail of each word | | 3129 | * :T Substitute the tail of each word |
3110 | * :E Substitute the extension (minus '.') of | | 3130 | * :E Substitute the extension (minus '.') of |
3111 | * each word | | 3131 | * each word |
3112 | * :R Substitute the root of each word | | 3132 | * :R Substitute the root of each word |
3113 | * (pathname minus the suffix). | | 3133 | * (pathname minus the suffix). |
3114 | * :O ("Order") Alphabeticaly sort words in variable. | | 3134 | * :O ("Order") Alphabeticaly sort words in variable. |
3115 | * :Ox ("intermiX") Randomize words in variable. | | 3135 | * :Ox ("intermiX") Randomize words in variable. |
3116 | * :u ("uniq") Remove adjacent duplicate words. | | 3136 | * :u ("uniq") Remove adjacent duplicate words. |
3117 | * :tu Converts the variable contents to uppercase. | | 3137 | * :tu Converts the variable contents to uppercase. |
3118 | * :tl Converts the variable contents to lowercase. | | 3138 | * :tl Converts the variable contents to lowercase. |
3119 | * :ts[c] Sets varSpace - the char used to | | 3139 | * :ts[c] Sets varSpace - the char used to |
3120 | * separate words to 'c'. If 'c' is | | 3140 | * separate words to 'c'. If 'c' is |
3121 | * omitted then no separation is used. | | 3141 | * omitted then no separation is used. |
3122 | * :tW Treat the variable contents as a single | | 3142 | * :tW Treat the variable contents as a single |
3123 | * word, even if it contains spaces. | | 3143 | * word, even if it contains spaces. |
3124 | * (Mnemonic: one big 'W'ord.) | | 3144 | * (Mnemonic: one big 'W'ord.) |
3125 | * :tw Treat the variable contents as multiple | | 3145 | * :tw Treat the variable contents as multiple |
3126 | * space-separated words. | | 3146 | * space-separated words. |
3127 | * (Mnemonic: many small 'w'ords.) | | 3147 | * (Mnemonic: many small 'w'ords.) |
3128 | * :[index] Select a single word from the value. | | 3148 | * :[index] Select a single word from the value. |
3129 | * :[start..end] Select multiple words from the value. | | 3149 | * :[start..end] Select multiple words from the value. |
3130 | * :[*] or :[0] Select the entire value, as a single | | 3150 | * :[*] or :[0] Select the entire value, as a single |
3131 | * word. Equivalent to :tW. | | 3151 | * word. Equivalent to :tW. |
3132 | * :[@] Select the entire value, as multiple | | 3152 | * :[@] Select the entire value, as multiple |
3133 | * words. Undoes the effect of :[*]. | | 3153 | * words. Undoes the effect of :[*]. |
3134 | * Equivalent to :tw. | | 3154 | * Equivalent to :tw. |
3135 | * :[#] Returns the number of words in the value. | | 3155 | * :[#] Returns the number of words in the value. |
3136 | * | | 3156 | * |
3137 | * :?<true-value>:<false-value> | | 3157 | * :?<true-value>:<false-value> |
3138 | * If the variable evaluates to true, return | | 3158 | * If the variable evaluates to true, return |
3139 | * true value, else return the second value. | | 3159 | * true value, else return the second value. |
3140 | * :lhs=rhs Like :S, but the rhs goes to the end of | | 3160 | * :lhs=rhs Like :S, but the rhs goes to the end of |
3141 | * the invocation. | | 3161 | * the invocation. |
3142 | * :sh Treat the current value as a command | | 3162 | * :sh Treat the current value as a command |
3143 | * to be run, new value is its output. | | 3163 | * to be run, new value is its output. |
3144 | * The following added so we can handle ODE makefiles. | | 3164 | * The following added so we can handle ODE makefiles. |
3145 | * :@<tmpvar>@<newval>@ | | 3165 | * :@<tmpvar>@<newval>@ |
3146 | * Assign a temporary local variable <tmpvar> | | 3166 | * Assign a temporary local variable <tmpvar> |
3147 | * to the current value of each word in turn | | 3167 | * to the current value of each word in turn |
3148 | * and replace each word with the result of | | 3168 | * and replace each word with the result of |
3149 | * evaluating <newval> | | 3169 | * evaluating <newval> |
3150 | * :D<newval> Use <newval> as value if variable defined | | 3170 | * :D<newval> Use <newval> as value if variable defined |
3151 | * :U<newval> Use <newval> as value if variable undefined | | 3171 | * :U<newval> Use <newval> as value if variable undefined |
3152 | * :L Use the name of the variable as the value. | | 3172 | * :L Use the name of the variable as the value. |
3153 | * :P Use the path of the node that has the same | | 3173 | * :P Use the path of the node that has the same |
3154 | * name as the variable as the value. This | | 3174 | * name as the variable as the value. This |
3155 | * basically includes an implied :L so that | | 3175 | * basically includes an implied :L so that |
3156 | * the common method of refering to the path | | 3176 | * the common method of refering to the path |
3157 | * of your dependent 'x' in a rule is to use | | 3177 | * of your dependent 'x' in a rule is to use |
3158 | * the form '${x:P}'. | | 3178 | * the form '${x:P}'. |
3159 | * :!<cmd>! Run cmd much the same as :sh run's the | | 3179 | * :!<cmd>! Run cmd much the same as :sh run's the |
3160 | * current value of the variable. | | 3180 | * current value of the variable. |
3161 | * The ::= modifiers, actually assign a value to the variable. | | 3181 | * Assignment operators (see ApplyModifier_Assign). |
3162 | * Their main purpose is in supporting modifiers of .for loop | | | |
3163 | * iterators and other obscure uses. They always expand to | | | |
3164 | * nothing. In a target rule that would otherwise expand to an | | | |
3165 | * empty line they can be preceded with @: to keep make happy. | | | |
3166 | * Eg. | | | |
3167 | * | | | |
3168 | * foo: .USE | | | |
3169 | * .for i in ${.TARGET} ${.TARGET:R}.gz | | | |
3170 | * @: ${t::=$i} | | | |
3171 | * @echo blah ${t:T} | | | |
3172 | * .endfor | | | |
3173 | * | | | |
3174 | * ::=<str> Assigns <str> as the new value of variable. | | | |
3175 | * ::?=<str> Assigns <str> as value of variable if | | | |
3176 | * it was not already set. | | | |
3177 | * ::+=<str> Appends <str> to variable. | | | |
3178 | * ::!=<cmd> Assigns output of <cmd> as the new value of | | | |
3179 | * variable. | | | |
3180 | */ | | 3182 | */ |
3181 | static char * | | 3183 | static char * |
3182 | ApplyModifiers(char *nstr, const char *tstr, | | 3184 | ApplyModifiers(char *nstr, const char *tstr, |
3183 | int const startc, int const endc, | | 3185 | int const startc, int const endc, |
3184 | Var * const v, GNode * const ctxt, VarEvalFlags const eflags, | | 3186 | Var * const v, GNode * const ctxt, VarEvalFlags const eflags, |
3185 | int * const lengthPtr, void ** const freePtr) | | 3187 | int * const lengthPtr, void ** const freePtr) |
3186 | { | | 3188 | { |
3187 | ApplyModifiersState st = { | | 3189 | ApplyModifiersState st = { |
3188 | startc, endc, v, ctxt, eflags, lengthPtr, freePtr, | | 3190 | startc, endc, v, ctxt, eflags, lengthPtr, freePtr, |
3189 | nstr, tstr, tstr, tstr, | | 3191 | nstr, tstr, tstr, tstr, |
3190 | '\0', '\0', 0, {' ', FALSE}, NULL | | 3192 | '\0', '\0', 0, {' ', FALSE}, NULL |
3191 | }; | | 3193 | }; |
3192 | | | 3194 | |
3193 | while (*st.tstr && *st.tstr != st.endc) { | | 3195 | while (*st.tstr && *st.tstr != st.endc) { |
3194 | | | 3196 | |
3195 | if (*st.tstr == '$') { | | 3197 | if (*st.tstr == '$') { |
3196 | /* | | 3198 | /* |
3197 | * We may have some complex modifiers in a variable. | | 3199 | * We may have some complex modifiers in a variable. |
3198 | */ | | 3200 | */ |
3199 | void *freeIt; | | 3201 | void *freeIt; |
3200 | char *rval; | | 3202 | char *rval; |
3201 | int rlen; | | 3203 | int rlen; |
3202 | int c; | | 3204 | int c; |
3203 | | | 3205 | |
3204 | rval = Var_Parse(st.tstr, st.ctxt, st.eflags, &rlen, &freeIt); | | 3206 | rval = Var_Parse(st.tstr, st.ctxt, st.eflags, &rlen, &freeIt); |
3205 | | | 3207 | |
3206 | /* | | 3208 | /* |
3207 | * If we have not parsed up to st.endc or ':', | | 3209 | * If we have not parsed up to st.endc or ':', |
3208 | * we are not interested. | | 3210 | * we are not interested. |
3209 | */ | | 3211 | */ |
3210 | if (rval != NULL && *rval && | | 3212 | if (rval != NULL && *rval && |
3211 | (c = st.tstr[rlen]) != '\0' && | | 3213 | (c = st.tstr[rlen]) != '\0' && |
3212 | c != ':' && | | 3214 | c != ':' && |
3213 | c != st.endc) { | | 3215 | c != st.endc) { |
3214 | free(freeIt); | | 3216 | free(freeIt); |
3215 | goto apply_mods; | | 3217 | goto apply_mods; |
3216 | } | | 3218 | } |
3217 | | | 3219 | |
3218 | if (DEBUG(VAR)) { | | 3220 | if (DEBUG(VAR)) { |
3219 | fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n", | | 3221 | fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n", |
3220 | rval, rlen, st.tstr, rlen, st.tstr + rlen); | | 3222 | rval, rlen, st.tstr, rlen, st.tstr + rlen); |
3221 | } | | 3223 | } |
3222 | | | 3224 | |
3223 | st.tstr += rlen; | | 3225 | st.tstr += rlen; |
3224 | | | 3226 | |
3225 | if (rval != NULL && *rval) { | | 3227 | if (rval != NULL && *rval) { |
3226 | int used; | | 3228 | int used; |
3227 | | | 3229 | |
3228 | st.nstr = ApplyModifiers(st.nstr, rval, 0, 0, st.v, | | 3230 | st.nstr = ApplyModifiers(st.nstr, rval, 0, 0, st.v, |
3229 | st.ctxt, st.eflags, &used, st.freePtr); | | 3231 | st.ctxt, st.eflags, &used, st.freePtr); |
3230 | if (st.nstr == var_Error | | 3232 | if (st.nstr == var_Error |
3231 | || (st.nstr == varNoError && (st.eflags & VARE_UNDEFERR) == 0) | | 3233 | || (st.nstr == varNoError && (st.eflags & VARE_UNDEFERR) == 0) |
3232 | || strlen(rval) != (size_t) used) { | | 3234 | || strlen(rval) != (size_t) used) { |
3233 | free(freeIt); | | 3235 | free(freeIt); |
3234 | goto out; /* error already reported */ | | 3236 | goto out; /* error already reported */ |
3235 | } | | 3237 | } |
3236 | } | | 3238 | } |
3237 | free(freeIt); | | 3239 | free(freeIt); |
3238 | if (*st.tstr == ':') | | 3240 | if (*st.tstr == ':') |
3239 | st.tstr++; | | 3241 | st.tstr++; |
3240 | else if (!*st.tstr && st.endc) { | | 3242 | else if (!*st.tstr && st.endc) { |
3241 | Error("Unclosed variable specification after complex " | | 3243 | Error("Unclosed variable specification after complex " |
3242 | "modifier (expecting '%c') for %s", st.endc, st.v->name); | | 3244 | "modifier (expecting '%c') for %s", st.endc, st.v->name); |
3243 | goto out; | | 3245 | goto out; |
3244 | } | | 3246 | } |
3245 | continue; | | 3247 | continue; |
3246 | } | | 3248 | } |
3247 | apply_mods: | | 3249 | apply_mods: |
3248 | if (DEBUG(VAR)) { | | 3250 | if (DEBUG(VAR)) { |
3249 | fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", st.v->name, | | 3251 | fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", st.v->name, |
3250 | *st.tstr, st.nstr); | | 3252 | *st.tstr, st.nstr); |
3251 | } | | 3253 | } |
3252 | st.newStr = var_Error; | | 3254 | st.newStr = var_Error; |
3253 | switch ((st.modifier = *st.tstr)) { | | 3255 | switch ((st.modifier = *st.tstr)) { |
3254 | case ':': | | 3256 | case ':': |
3255 | { | | 3257 | { |
3256 | int res = ApplyModifier_Assign(&st); | | 3258 | int res = ApplyModifier_Assign(&st); |
3257 | if (res == 'b') | | 3259 | if (res == 'b') |
3258 | goto bad_modifier; | | 3260 | goto bad_modifier; |
3259 | if (res == 'c') | | 3261 | if (res == 'c') |
3260 | goto cleanup; | | 3262 | goto cleanup; |
3261 | if (res == 'd') | | 3263 | if (res == 'd') |
3262 | goto default_case; | | 3264 | goto default_case; |
3263 | break; | | 3265 | break; |
3264 | } | | 3266 | } |
3265 | case '@': | | 3267 | case '@': |
3266 | ApplyModifier_At(&st); | | 3268 | ApplyModifier_At(&st); |
3267 | break; | | 3269 | break; |
3268 | case '_': | | 3270 | case '_': |
3269 | if (!ApplyModifier_Remember(&st)) | | 3271 | if (!ApplyModifier_Remember(&st)) |
3270 | goto default_case; | | 3272 | goto default_case; |
3271 | break; | | 3273 | break; |
3272 | case 'D': | | 3274 | case 'D': |
3273 | case 'U': | | 3275 | case 'U': |
3274 | ApplyModifier_Defined(&st); | | 3276 | ApplyModifier_Defined(&st); |
3275 | break; | | 3277 | break; |
3276 | case 'L': | | 3278 | case 'L': |
3277 | { | | 3279 | { |
3278 | if ((st.v->flags & VAR_JUNK) != 0) | | 3280 | if ((st.v->flags & VAR_JUNK) != 0) |
3279 | st.v->flags |= VAR_KEEP; | | 3281 | st.v->flags |= VAR_KEEP; |
3280 | st.newStr = bmake_strdup(st.v->name); | | 3282 | st.newStr = bmake_strdup(st.v->name); |
3281 | st.cp = ++st.tstr; | | 3283 | st.cp = ++st.tstr; |
3282 | st.termc = *st.tstr; | | 3284 | st.termc = *st.tstr; |
3283 | break; | | 3285 | break; |
3284 | } | | 3286 | } |
3285 | case 'P': | | 3287 | case 'P': |
3286 | ApplyModifier_Path(&st); | | 3288 | ApplyModifier_Path(&st); |
3287 | break; | | 3289 | break; |
3288 | case '!': | | 3290 | case '!': |
3289 | if (!ApplyModifier_Exclam(&st)) | | 3291 | if (!ApplyModifier_Exclam(&st)) |
3290 | goto cleanup; | | 3292 | goto cleanup; |
3291 | break; | | 3293 | break; |
3292 | case '[': | | 3294 | case '[': |
3293 | { | | 3295 | { |
3294 | int res = ApplyModifier_Words(&st); | | 3296 | int res = ApplyModifier_Words(&st); |
3295 | if (res == 'b') | | 3297 | if (res == 'b') |
3296 | goto bad_modifier; | | 3298 | goto bad_modifier; |
3297 | if (res == 'c') | | 3299 | if (res == 'c') |
3298 | goto cleanup; | | 3300 | goto cleanup; |
3299 | break; | | 3301 | break; |
3300 | } | | 3302 | } |
3301 | case 'g': | | 3303 | case 'g': |
3302 | if (!ApplyModifier_Gmtime(&st)) | | 3304 | if (!ApplyModifier_Gmtime(&st)) |
3303 | goto default_case; | | 3305 | goto default_case; |
3304 | break; | | 3306 | break; |
3305 | case 'h': | | 3307 | case 'h': |
3306 | if (!ApplyModifier_Hash(&st)) | | 3308 | if (!ApplyModifier_Hash(&st)) |
3307 | goto default_case; | | 3309 | goto default_case; |
3308 | break; | | 3310 | break; |
3309 | case 'l': | | 3311 | case 'l': |
3310 | if (!ApplyModifier_Localtime(&st)) | | 3312 | if (!ApplyModifier_Localtime(&st)) |
3311 | goto default_case; | | 3313 | goto default_case; |
3312 | break; | | 3314 | break; |
3313 | case 't': | | 3315 | case 't': |
3314 | if (!ApplyModifier_To(&st)) | | 3316 | if (!ApplyModifier_To(&st)) |
3315 | goto bad_modifier; | | 3317 | goto bad_modifier; |
3316 | break; | | 3318 | break; |
3317 | case 'N': | | 3319 | case 'N': |
3318 | case 'M': | | 3320 | case 'M': |
3319 | ApplyModifier_Match(&st); | | 3321 | ApplyModifier_Match(&st); |
3320 | break; | | 3322 | break; |
3321 | case 'S': | | 3323 | case 'S': |
3322 | if (!ApplyModifier_Subst(&st)) | | 3324 | if (!ApplyModifier_Subst(&st)) |
3323 | goto cleanup; | | 3325 | goto cleanup; |
3324 | break; | | 3326 | break; |
3325 | case '?': | | 3327 | case '?': |
3326 | if (!ApplyModifier_IfElse(&st)) | | 3328 | if (!ApplyModifier_IfElse(&st)) |
3327 | goto cleanup; | | 3329 | goto cleanup; |
3328 | break; | | 3330 | break; |
3329 | #ifndef NO_REGEX | | 3331 | #ifndef NO_REGEX |
3330 | case 'C': | | 3332 | case 'C': |
3331 | if (!ApplyModifier_Regex(&st)) | | 3333 | if (!ApplyModifier_Regex(&st)) |
3332 | goto cleanup; | | 3334 | goto cleanup; |
3333 | break; | | 3335 | break; |
3334 | #endif | | 3336 | #endif |
3335 | case 'q': | | 3337 | case 'q': |
3336 | case 'Q': | | 3338 | case 'Q': |
3337 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { | | 3339 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { |
3338 | st.newStr = VarQuote(st.nstr, st.modifier == 'q'); | | 3340 | st.newStr = VarQuote(st.nstr, st.modifier == 'q'); |
3339 | st.cp = st.tstr + 1; | | 3341 | st.cp = st.tstr + 1; |
3340 | st.termc = *st.cp; | | 3342 | st.termc = *st.cp; |
3341 | break; | | 3343 | break; |
3342 | } | | 3344 | } |
3343 | goto default_case; | | 3345 | goto default_case; |
3344 | case 'T': | | 3346 | case 'T': |
3345 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { | | 3347 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { |
3346 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarTail, | | 3348 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarTail, |
3347 | NULL); | | 3349 | NULL); |
3348 | st.cp = st.tstr + 1; | | 3350 | st.cp = st.tstr + 1; |
3349 | st.termc = *st.cp; | | 3351 | st.termc = *st.cp; |
3350 | break; | | 3352 | break; |
3351 | } | | 3353 | } |
3352 | goto default_case; | | 3354 | goto default_case; |
3353 | case 'H': | | 3355 | case 'H': |
3354 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { | | 3356 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { |
3355 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarHead, | | 3357 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarHead, |
3356 | NULL); | | 3358 | NULL); |
3357 | st.cp = st.tstr + 1; | | 3359 | st.cp = st.tstr + 1; |
3358 | st.termc = *st.cp; | | 3360 | st.termc = *st.cp; |
3359 | break; | | 3361 | break; |
3360 | } | | 3362 | } |
3361 | goto default_case; | | 3363 | goto default_case; |
3362 | case 'E': | | 3364 | case 'E': |
3363 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { | | 3365 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { |
3364 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarSuffix, | | 3366 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarSuffix, |
3365 | NULL); | | 3367 | NULL); |
3366 | st.cp = st.tstr + 1; | | 3368 | st.cp = st.tstr + 1; |
3367 | st.termc = *st.cp; | | 3369 | st.termc = *st.cp; |
3368 | break; | | 3370 | break; |
3369 | } | | 3371 | } |
3370 | goto default_case; | | 3372 | goto default_case; |
3371 | case 'R': | | 3373 | case 'R': |
3372 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { | | 3374 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { |
3373 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarRoot, | | 3375 | st.newStr = VarModify(st.ctxt, &st.parsestate, st.nstr, VarRoot, |
3374 | NULL); | | 3376 | NULL); |
3375 | st.cp = st.tstr + 1; | | 3377 | st.cp = st.tstr + 1; |
3376 | st.termc = *st.cp; | | 3378 | st.termc = *st.cp; |
3377 | break; | | 3379 | break; |
3378 | } | | 3380 | } |
3379 | goto default_case; | | 3381 | goto default_case; |
3380 | case 'r': | | 3382 | case 'r': |
3381 | if (!ApplyModifier_Range(&st)) | | 3383 | if (!ApplyModifier_Range(&st)) |
3382 | goto default_case; | | 3384 | goto default_case; |
3383 | break; | | 3385 | break; |
3384 | case 'O': | | 3386 | case 'O': |
3385 | if (!ApplyModifier_Order(&st)) | | 3387 | if (!ApplyModifier_Order(&st)) |
3386 | goto bad_modifier; | | 3388 | goto bad_modifier; |
3387 | break; | | 3389 | break; |
3388 | case 'u': | | 3390 | case 'u': |
3389 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { | | 3391 | if (st.tstr[1] == st.endc || st.tstr[1] == ':') { |
3390 | st.newStr = VarUniq(st.nstr); | | 3392 | st.newStr = VarUniq(st.nstr); |
3391 | st.cp = st.tstr + 1; | | 3393 | st.cp = st.tstr + 1; |
3392 | st.termc = *st.cp; | | 3394 | st.termc = *st.cp; |
3393 | break; | | 3395 | break; |
3394 | } | | 3396 | } |
3395 | goto default_case; | | 3397 | goto default_case; |
3396 | #ifdef SUNSHCMD | | 3398 | #ifdef SUNSHCMD |
3397 | case 's': | | 3399 | case 's': |
3398 | if (st.tstr[1] == 'h' && (st.tstr[2] == st.endc || st.tstr[2] == ':')) { | | 3400 | if (st.tstr[1] == 'h' && (st.tstr[2] == st.endc || st.tstr[2] == ':')) { |
3399 | const char *emsg; | | 3401 | const char *emsg; |
3400 | if (st.eflags & VARE_WANTRES) { | | 3402 | if (st.eflags & VARE_WANTRES) { |
3401 | st.newStr = Cmd_Exec(st.nstr, &emsg); | | 3403 | st.newStr = Cmd_Exec(st.nstr, &emsg); |
3402 | if (emsg) | | 3404 | if (emsg) |
3403 | Error(emsg, st.nstr); | | 3405 | Error(emsg, st.nstr); |
3404 | } else | | 3406 | } else |
3405 | st.newStr = varNoError; | | 3407 | st.newStr = varNoError; |
3406 | st.cp = st.tstr + 2; | | 3408 | st.cp = st.tstr + 2; |
3407 | st.termc = *st.cp; | | 3409 | st.termc = *st.cp; |
3408 | break; | | 3410 | break; |
3409 | } | | 3411 | } |
3410 | goto default_case; | | 3412 | goto default_case; |
3411 | #endif | | 3413 | #endif |
3412 | default: | | 3414 | default: |
3413 | default_case: | | 3415 | default_case: |
3414 | { | | 3416 | { |
3415 | #ifdef SYSVVARSUB | | 3417 | #ifdef SYSVVARSUB |
3416 | int res = ApplyModifier_SysV(&st); | | 3418 | int res = ApplyModifier_SysV(&st); |
3417 | if (res == 'c') | | 3419 | if (res == 'c') |
3418 | goto cleanup; | | 3420 | goto cleanup; |
3419 | if (res != '=') | | 3421 | if (res != '=') |
3420 | #endif | | 3422 | #endif |
3421 | { | | 3423 | { |
3422 | Error("Unknown modifier '%c'", *st.tstr); | | 3424 | Error("Unknown modifier '%c'", *st.tstr); |
3423 | for (st.cp = st.tstr+1; | | 3425 | for (st.cp = st.tstr+1; |
3424 | *st.cp != ':' && *st.cp != st.endc && *st.cp != '\0'; | | 3426 | *st.cp != ':' && *st.cp != st.endc && *st.cp != '\0'; |
3425 | st.cp++) | | 3427 | st.cp++) |
3426 | continue; | | 3428 | continue; |
3427 | st.termc = *st.cp; | | 3429 | st.termc = *st.cp; |
3428 | st.newStr = var_Error; | | 3430 | st.newStr = var_Error; |
3429 | } | | 3431 | } |
3430 | } | | 3432 | } |
3431 | } | | 3433 | } |
3432 | if (DEBUG(VAR)) { | | 3434 | if (DEBUG(VAR)) { |
3433 | fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n", | | 3435 | fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n", |
3434 | st.v->name, st.modifier, st.newStr); | | 3436 | st.v->name, st.modifier, st.newStr); |
3435 | } | | 3437 | } |
3436 | | | 3438 | |
3437 | if (st.newStr != st.nstr) { | | 3439 | if (st.newStr != st.nstr) { |
3438 | if (*st.freePtr) { | | 3440 | if (*st.freePtr) { |
3439 | free(st.nstr); | | 3441 | free(st.nstr); |
3440 | *st.freePtr = NULL; | | 3442 | *st.freePtr = NULL; |
3441 | } | | 3443 | } |
3442 | st.nstr = st.newStr; | | 3444 | st.nstr = st.newStr; |
3443 | if (st.nstr != var_Error && st.nstr != varNoError) { | | 3445 | if (st.nstr != var_Error && st.nstr != varNoError) { |
3444 | *st.freePtr = st.nstr; | | 3446 | *st.freePtr = st.nstr; |
3445 | } | | 3447 | } |
3446 | } | | 3448 | } |
3447 | if (st.termc == '\0' && st.endc != '\0') { | | 3449 | if (st.termc == '\0' && st.endc != '\0') { |
3448 | Error("Unclosed variable specification (expecting '%c') " | | 3450 | Error("Unclosed variable specification (expecting '%c') " |
3449 | "for \"%s\" (value \"%s\") modifier %c", | | 3451 | "for \"%s\" (value \"%s\") modifier %c", |
3450 | st.endc, st.v->name, st.nstr, st.modifier); | | 3452 | st.endc, st.v->name, st.nstr, st.modifier); |
3451 | } else if (st.termc == ':') { | | 3453 | } else if (st.termc == ':') { |
3452 | st.cp++; | | 3454 | st.cp++; |
3453 | } | | 3455 | } |
3454 | st.tstr = st.cp; | | 3456 | st.tstr = st.cp; |
3455 | } | | 3457 | } |
3456 | out: | | 3458 | out: |
3457 | *st.lengthPtr = st.tstr - st.start; | | 3459 | *st.lengthPtr = st.tstr - st.start; |
3458 | return st.nstr; | | 3460 | return st.nstr; |
3459 | | | 3461 | |
3460 | bad_modifier: | | 3462 | bad_modifier: |
3461 | /* "{(" */ | | 3463 | /* "{(" */ |
3462 | Error("Bad modifier `:%.*s' for %s", (int)strcspn(st.tstr, ":)}"), st.tstr, | | 3464 | Error("Bad modifier `:%.*s' for %s", (int)strcspn(st.tstr, ":)}"), st.tstr, |
3463 | st.v->name); | | 3465 | st.v->name); |
3464 | | | 3466 | |
3465 | cleanup: | | 3467 | cleanup: |
3466 | *st.lengthPtr = st.cp - st.start; | | 3468 | *st.lengthPtr = st.cp - st.start; |
3467 | if (st.delim != '\0') | | 3469 | if (st.delim != '\0') |
3468 | Error("Unclosed substitution for %s (%c missing)", | | 3470 | Error("Unclosed substitution for %s (%c missing)", |
3469 | st.v->name, st.delim); | | 3471 | st.v->name, st.delim); |
3470 | free(*st.freePtr); | | 3472 | free(*st.freePtr); |
3471 | *st.freePtr = NULL; | | 3473 | *st.freePtr = NULL; |
3472 | return var_Error; | | 3474 | return var_Error; |
3473 | } | | 3475 | } |
3474 | | | 3476 | |
3475 | /*- | | 3477 | /*- |
3476 | *----------------------------------------------------------------------- | | 3478 | *----------------------------------------------------------------------- |
3477 | * Var_Parse -- | | 3479 | * Var_Parse -- |
3478 | * Given the start of a variable invocation, extract the variable | | 3480 | * Given the start of a variable invocation, extract the variable |
3479 | * name and find its value, then modify it according to the | | 3481 | * name and find its value, then modify it according to the |
3480 | * specification. | | 3482 | * specification. |
3481 | * | | 3483 | * |
3482 | * Input: | | 3484 | * Input: |
3483 | * str The string to parse | | 3485 | * str The string to parse |
3484 | * ctxt The context for the variable | | 3486 | * ctxt The context for the variable |
3485 | * flags VARE_UNDEFERR if undefineds are an error | | 3487 | * flags VARE_UNDEFERR if undefineds are an error |
3486 | * VARE_WANTRES if we actually want the result | | 3488 | * VARE_WANTRES if we actually want the result |
3487 | * VARE_ASSIGN if we are in a := assignment | | 3489 | * VARE_ASSIGN if we are in a := assignment |
3488 | * lengthPtr OUT: The length of the specification | | 3490 | * lengthPtr OUT: The length of the specification |
3489 | * freePtr OUT: Non-NULL if caller should free *freePtr | | 3491 | * freePtr OUT: Non-NULL if caller should free *freePtr |
3490 | * | | 3492 | * |
3491 | * Results: | | 3493 | * Results: |
3492 | * The (possibly-modified) value of the variable or var_Error if the | | 3494 | * The (possibly-modified) value of the variable or var_Error if the |
3493 | * specification is invalid. The length of the specification is | | 3495 | * specification is invalid. The length of the specification is |
3494 | * placed in *lengthPtr (for invalid specifications, this is just | | 3496 | * placed in *lengthPtr (for invalid specifications, this is just |
3495 | * 2...?). | | 3497 | * 2...?). |
3496 | * If *freePtr is non-NULL then it's a pointer that the caller | | 3498 | * If *freePtr is non-NULL then it's a pointer that the caller |
3497 | * should pass to free() to free memory used by the result. | | 3499 | * should pass to free() to free memory used by the result. |
3498 | * | | 3500 | * |
3499 | * Side Effects: | | 3501 | * Side Effects: |
3500 | * None. | | 3502 | * None. |
3501 | * | | 3503 | * |
3502 | *----------------------------------------------------------------------- | | 3504 | *----------------------------------------------------------------------- |
3503 | */ | | 3505 | */ |
3504 | /* coverity[+alloc : arg-*4] */ | | 3506 | /* coverity[+alloc : arg-*4] */ |
3505 | char * | | 3507 | char * |
3506 | Var_Parse(const char *str, GNode *ctxt, VarEvalFlags flags, | | 3508 | Var_Parse(const char *str, GNode *ctxt, VarEvalFlags flags, |
3507 | int *lengthPtr, void **freePtr) | | 3509 | int *lengthPtr, void **freePtr) |
3508 | { | | 3510 | { |
3509 | const char *tstr; /* Pointer into str */ | | 3511 | const char *tstr; /* Pointer into str */ |
3510 | Var *v; /* Variable in invocation */ | | 3512 | Var *v; /* Variable in invocation */ |
3511 | Boolean haveModifier; /* TRUE if have modifiers for the variable */ | | 3513 | Boolean haveModifier; /* TRUE if have modifiers for the variable */ |
3512 | char endc; /* Ending character when variable in parens | | 3514 | char endc; /* Ending character when variable in parens |
3513 | * or braces */ | | 3515 | * or braces */ |
3514 | char startc; /* Starting character when variable in parens | | 3516 | char startc; /* Starting character when variable in parens |
3515 | * or braces */ | | 3517 | * or braces */ |
3516 | int vlen; /* Length of variable name */ | | 3518 | int vlen; /* Length of variable name */ |
3517 | const char *start; /* Points to original start of str */ | | 3519 | const char *start; /* Points to original start of str */ |
3518 | char *nstr; /* New string, used during expansion */ | | 3520 | char *nstr; /* New string, used during expansion */ |
3519 | Boolean dynamic; /* TRUE if the variable is local and we're | | 3521 | Boolean dynamic; /* TRUE if the variable is local and we're |
3520 | * expanding it in a non-local context. This | | 3522 | * expanding it in a non-local context. This |
3521 | * is done to support dynamic sources. The | | 3523 | * is done to support dynamic sources. The |
3522 | * result is just the invocation, unaltered */ | | 3524 | * result is just the invocation, unaltered */ |
3523 | const char *extramodifiers; /* extra modifiers to apply first */ | | 3525 | const char *extramodifiers; /* extra modifiers to apply first */ |
3524 | char name[2]; | | 3526 | char name[2]; |
3525 | | | 3527 | |
3526 | *freePtr = NULL; | | 3528 | *freePtr = NULL; |
3527 | extramodifiers = NULL; | | 3529 | extramodifiers = NULL; |
3528 | dynamic = FALSE; | | 3530 | dynamic = FALSE; |
3529 | start = str; | | 3531 | start = str; |
3530 | | | 3532 | |
3531 | startc = str[1]; | | 3533 | startc = str[1]; |
3532 | if (startc != PROPEN && startc != BROPEN) { | | 3534 | if (startc != PROPEN && startc != BROPEN) { |
3533 | /* | | 3535 | /* |
3534 | * If it's not bounded by braces of some sort, life is much simpler. | | 3536 | * If it's not bounded by braces of some sort, life is much simpler. |
3535 | * We just need to check for the first character and return the | | 3537 | * We just need to check for the first character and return the |
3536 | * value if it exists. | | 3538 | * value if it exists. |
3537 | */ | | 3539 | */ |
3538 | | | 3540 | |
3539 | /* Error out some really stupid names */ | | 3541 | /* Error out some really stupid names */ |
3540 | if (startc == '\0' || strchr(")}:$", startc)) { | | 3542 | if (startc == '\0' || strchr(")}:$", startc)) { |
3541 | *lengthPtr = 1; | | 3543 | *lengthPtr = 1; |
3542 | return var_Error; | | 3544 | return var_Error; |
3543 | } | | 3545 | } |
3544 | name[0] = startc; | | 3546 | name[0] = startc; |
3545 | name[1] = '\0'; | | 3547 | name[1] = '\0'; |
3546 | | | 3548 | |
3547 | v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); | | 3549 | v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); |
3548 | if (v == NULL) { | | 3550 | if (v == NULL) { |
3549 | *lengthPtr = 2; | | 3551 | *lengthPtr = 2; |
3550 | | | 3552 | |
3551 | if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) { | | 3553 | if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) { |
3552 | /* | | 3554 | /* |
3553 | * If substituting a local variable in a non-local context, | | 3555 | * If substituting a local variable in a non-local context, |
3554 | * assume it's for dynamic source stuff. We have to handle | | 3556 | * assume it's for dynamic source stuff. We have to handle |
3555 | * this specially and return the longhand for the variable | | 3557 | * this specially and return the longhand for the variable |
3556 | * with the dollar sign escaped so it makes it back to the | | 3558 | * with the dollar sign escaped so it makes it back to the |
3557 | * caller. Only four of the local variables are treated | | 3559 | * caller. Only four of the local variables are treated |
3558 | * specially as they are the only four that will be set | | 3560 | * specially as they are the only four that will be set |
3559 | * when dynamic sources are expanded. | | 3561 | * when dynamic sources are expanded. |
3560 | */ | | 3562 | */ |
3561 | switch (str[1]) { | | 3563 | switch (str[1]) { |
3562 | case '@': | | 3564 | case '@': |
3563 | return UNCONST("$(.TARGET)"); | | 3565 | return UNCONST("$(.TARGET)"); |
3564 | case '%': | | 3566 | case '%': |
3565 | return UNCONST("$(.MEMBER)"); | | 3567 | return UNCONST("$(.MEMBER)"); |
3566 | case '*': | | 3568 | case '*': |
3567 | return UNCONST("$(.PREFIX)"); | | 3569 | return UNCONST("$(.PREFIX)"); |
3568 | case '!': | | 3570 | case '!': |
3569 | return UNCONST("$(.ARCHIVE)"); | | 3571 | return UNCONST("$(.ARCHIVE)"); |
3570 | } | | 3572 | } |
3571 | } | | 3573 | } |
3572 | return (flags & VARE_UNDEFERR) ? var_Error : varNoError; | | 3574 | return (flags & VARE_UNDEFERR) ? var_Error : varNoError; |
3573 | } else { | | 3575 | } else { |
3574 | haveModifier = FALSE; | | 3576 | haveModifier = FALSE; |
3575 | tstr = &str[1]; | | 3577 | tstr = &str[1]; |
3576 | endc = str[1]; | | 3578 | endc = str[1]; |
3577 | } | | 3579 | } |
3578 | } else { | | 3580 | } else { |
3579 | Buffer buf; /* Holds the variable name */ | | 3581 | Buffer buf; /* Holds the variable name */ |
3580 | int depth = 1; | | 3582 | int depth = 1; |
3581 | | | 3583 | |
3582 | endc = startc == PROPEN ? PRCLOSE : BRCLOSE; | | 3584 | endc = startc == PROPEN ? PRCLOSE : BRCLOSE; |
3583 | Buf_Init(&buf, 0); | | 3585 | Buf_Init(&buf, 0); |
3584 | | | 3586 | |
3585 | /* | | 3587 | /* |
3586 | * Skip to the end character or a colon, whichever comes first. | | 3588 | * Skip to the end character or a colon, whichever comes first. |
3587 | */ | | 3589 | */ |
3588 | for (tstr = str + 2; *tstr != '\0'; tstr++) { | | 3590 | for (tstr = str + 2; *tstr != '\0'; tstr++) { |
3589 | /* Track depth so we can spot parse errors. */ | | 3591 | /* Track depth so we can spot parse errors. */ |
3590 | if (*tstr == startc) | | 3592 | if (*tstr == startc) |
3591 | depth++; | | 3593 | depth++; |
3592 | if (*tstr == endc) { | | 3594 | if (*tstr == endc) { |
3593 | if (--depth == 0) | | 3595 | if (--depth == 0) |
3594 | break; | | 3596 | break; |
3595 | } | | 3597 | } |
3596 | if (depth == 1 && *tstr == ':') | | 3598 | if (depth == 1 && *tstr == ':') |
3597 | break; | | 3599 | break; |
3598 | /* A variable inside a variable, expand. */ | | 3600 | /* A variable inside a variable, expand. */ |
3599 | if (*tstr == '$') { | | 3601 | if (*tstr == '$') { |
3600 | int rlen; | | 3602 | int rlen; |
3601 | void *freeIt; | | 3603 | void *freeIt; |
3602 | char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt); | | 3604 | char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt); |
3603 | if (rval != NULL) | | 3605 | if (rval != NULL) |
3604 | Buf_AddBytes(&buf, strlen(rval), rval); | | 3606 | Buf_AddBytes(&buf, strlen(rval), rval); |
3605 | free(freeIt); | | 3607 | free(freeIt); |
3606 | tstr += rlen - 1; | | 3608 | tstr += rlen - 1; |
3607 | } else | | 3609 | } else |
3608 | Buf_AddByte(&buf, *tstr); | | 3610 | Buf_AddByte(&buf, *tstr); |
3609 | } | | 3611 | } |
3610 | if (*tstr == ':') { | | 3612 | if (*tstr == ':') { |
3611 | haveModifier = TRUE; | | 3613 | haveModifier = TRUE; |
3612 | } else if (*tstr == endc) { | | 3614 | } else if (*tstr == endc) { |
3613 | haveModifier = FALSE; | | 3615 | haveModifier = FALSE; |
3614 | } else { | | 3616 | } else { |
3615 | /* | | 3617 | /* |
3616 | * If we never did find the end character, return NULL | | 3618 | * If we never did find the end character, return NULL |
3617 | * right now, setting the length to be the distance to | | 3619 | * right now, setting the length to be the distance to |
3618 | * the end of the string, since that's what make does. | | 3620 | * the end of the string, since that's what make does. |
3619 | */ | | 3621 | */ |
3620 | *lengthPtr = tstr - str; | | 3622 | *lengthPtr = tstr - str; |
3621 | Buf_Destroy(&buf, TRUE); | | 3623 | Buf_Destroy(&buf, TRUE); |
3622 | return var_Error; | | 3624 | return var_Error; |
3623 | } | | 3625 | } |
3624 | str = Buf_GetAll(&buf, &vlen); | | 3626 | str = Buf_GetAll(&buf, &vlen); |
3625 | | | 3627 | |
3626 | /* | | 3628 | /* |
3627 | * At this point, str points into newly allocated memory from | | 3629 | * At this point, str points into newly allocated memory from |
3628 | * buf, containing only the name of the variable. | | 3630 | * buf, containing only the name of the variable. |
3629 | * | | 3631 | * |
3630 | * start and tstr point into the const string that was pointed | | 3632 | * start and tstr point into the const string that was pointed |
3631 | * to by the original value of the str parameter. start points | | 3633 | * to by the original value of the str parameter. start points |
3632 | * to the '$' at the beginning of the string, while tstr points | | 3634 | * to the '$' at the beginning of the string, while tstr points |
3633 | * to the char just after the end of the variable name -- this | | 3635 | * to the char just after the end of the variable name -- this |
3634 | * will be '\0', ':', PRCLOSE, or BRCLOSE. | | 3636 | * will be '\0', ':', PRCLOSE, or BRCLOSE. |
3635 | */ | | 3637 | */ |
3636 | | | 3638 | |
3637 | v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); | | 3639 | v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); |
3638 | /* | | 3640 | /* |
3639 | * Check also for bogus D and F forms of local variables since we're | | 3641 | * Check also for bogus D and F forms of local variables since we're |
3640 | * in a local context and the name is the right length. | | 3642 | * in a local context and the name is the right length. |
3641 | */ | | 3643 | */ |
3642 | if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && | | 3644 | if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && |
3643 | (vlen == 2) && (str[1] == 'F' || str[1] == 'D') && | | 3645 | (vlen == 2) && (str[1] == 'F' || str[1] == 'D') && |
3644 | strchr("@%?*!<>", str[0]) != NULL) { | | 3646 | strchr("@%?*!<>", str[0]) != NULL) { |
3645 | /* | | 3647 | /* |
3646 | * Well, it's local -- go look for it. | | 3648 | * Well, it's local -- go look for it. |
3647 | */ | | 3649 | */ |
3648 | name[0] = *str; | | 3650 | name[0] = *str; |
3649 | name[1] = '\0'; | | 3651 | name[1] = '\0'; |
3650 | v = VarFind(name, ctxt, 0); | | 3652 | v = VarFind(name, ctxt, 0); |
3651 | | | 3653 | |
3652 | if (v != NULL) { | | 3654 | if (v != NULL) { |
3653 | if (str[1] == 'D') { | | 3655 | if (str[1] == 'D') { |
3654 | extramodifiers = "H:"; | | 3656 | extramodifiers = "H:"; |
3655 | } else { /* F */ | | 3657 | } else { /* F */ |
3656 | extramodifiers = "T:"; | | 3658 | extramodifiers = "T:"; |
3657 | } | | 3659 | } |
3658 | } | | 3660 | } |
3659 | } | | 3661 | } |
3660 | | | 3662 | |
3661 | if (v == NULL) { | | 3663 | if (v == NULL) { |
3662 | if (((vlen == 1) || | | 3664 | if (((vlen == 1) || |
3663 | (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) && | | 3665 | (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) && |
3664 | ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) | | 3666 | ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) |
3665 | { | | 3667 | { |
3666 | /* | | 3668 | /* |
3667 | * If substituting a local variable in a non-local context, | | 3669 | * If substituting a local variable in a non-local context, |
3668 | * assume it's for dynamic source stuff. We have to handle | | 3670 | * assume it's for dynamic source stuff. We have to handle |
3669 | * this specially and return the longhand for the variable | | 3671 | * this specially and return the longhand for the variable |
3670 | * with the dollar sign escaped so it makes it back to the | | 3672 | * with the dollar sign escaped so it makes it back to the |
3671 | * caller. Only four of the local variables are treated | | 3673 | * caller. Only four of the local variables are treated |
3672 | * specially as they are the only four that will be set | | 3674 | * specially as they are the only four that will be set |
3673 | * when dynamic sources are expanded. | | 3675 | * when dynamic sources are expanded. |
3674 | */ | | 3676 | */ |
3675 | switch (*str) { | | 3677 | switch (*str) { |
3676 | case '@': | | 3678 | case '@': |
3677 | case '%': | | 3679 | case '%': |
3678 | case '*': | | 3680 | case '*': |
3679 | case '!': | | 3681 | case '!': |
3680 | dynamic = TRUE; | | 3682 | dynamic = TRUE; |
3681 | break; | | 3683 | break; |
3682 | } | | 3684 | } |
3683 | } else if (vlen > 2 && *str == '.' && | | 3685 | } else if (vlen > 2 && *str == '.' && |
3684 | isupper((unsigned char) str[1]) && | | 3686 | isupper((unsigned char) str[1]) && |
3685 | (ctxt == VAR_CMD || ctxt == VAR_GLOBAL)) | | 3687 | (ctxt == VAR_CMD || ctxt == VAR_GLOBAL)) |
3686 | { | | 3688 | { |
3687 | int len = vlen - 1; | | 3689 | int len = vlen - 1; |
3688 | if ((strncmp(str, ".TARGET", len) == 0) || | | 3690 | if ((strncmp(str, ".TARGET", len) == 0) || |
3689 | (strncmp(str, ".ARCHIVE", len) == 0) || | | 3691 | (strncmp(str, ".ARCHIVE", len) == 0) || |
3690 | (strncmp(str, ".PREFIX", len) == 0) || | | 3692 | (strncmp(str, ".PREFIX", len) == 0) || |
3691 | (strncmp(str, ".MEMBER", len) == 0)) | | 3693 | (strncmp(str, ".MEMBER", len) == 0)) |
3692 | { | | 3694 | { |
3693 | dynamic = TRUE; | | 3695 | dynamic = TRUE; |
3694 | } | | 3696 | } |
3695 | } | | 3697 | } |
3696 | | | 3698 | |
3697 | if (!haveModifier) { | | 3699 | if (!haveModifier) { |
3698 | /* | | 3700 | /* |
3699 | * No modifiers -- have specification length so we can return | | 3701 | * No modifiers -- have specification length so we can return |
3700 | * now. | | 3702 | * now. |
3701 | */ | | 3703 | */ |
3702 | *lengthPtr = tstr - start + 1; | | 3704 | *lengthPtr = tstr - start + 1; |
3703 | if (dynamic) { | | 3705 | if (dynamic) { |
3704 | char *pstr = bmake_strndup(start, *lengthPtr); | | 3706 | char *pstr = bmake_strndup(start, *lengthPtr); |
3705 | *freePtr = pstr; | | 3707 | *freePtr = pstr; |
3706 | Buf_Destroy(&buf, TRUE); | | 3708 | Buf_Destroy(&buf, TRUE); |
3707 | return pstr; | | 3709 | return pstr; |
3708 | } else { | | 3710 | } else { |
3709 | Buf_Destroy(&buf, TRUE); | | 3711 | Buf_Destroy(&buf, TRUE); |
3710 | return (flags & VARE_UNDEFERR) ? var_Error : varNoError; | | 3712 | return (flags & VARE_UNDEFERR) ? var_Error : varNoError; |
3711 | } | | 3713 | } |
3712 | } else { | | 3714 | } else { |
3713 | /* | | 3715 | /* |
3714 | * Still need to get to the end of the variable specification, | | 3716 | * Still need to get to the end of the variable specification, |
3715 | * so kludge up a Var structure for the modifications | | 3717 | * so kludge up a Var structure for the modifications |
3716 | */ | | 3718 | */ |
3717 | v = bmake_malloc(sizeof(Var)); | | 3719 | v = bmake_malloc(sizeof(Var)); |
3718 | v->name = UNCONST(str); | | 3720 | v->name = UNCONST(str); |
3719 | Buf_Init(&v->val, 1); | | 3721 | Buf_Init(&v->val, 1); |
3720 | v->flags = VAR_JUNK; | | 3722 | v->flags = VAR_JUNK; |
3721 | Buf_Destroy(&buf, FALSE); | | 3723 | Buf_Destroy(&buf, FALSE); |
3722 | } | | 3724 | } |
3723 | } else | | 3725 | } else |
3724 | Buf_Destroy(&buf, TRUE); | | 3726 | Buf_Destroy(&buf, TRUE); |
3725 | } | | 3727 | } |
3726 | | | 3728 | |
3727 | if (v->flags & VAR_IN_USE) { | | 3729 | if (v->flags & VAR_IN_USE) { |
3728 | Fatal("Variable %s is recursive.", v->name); | | 3730 | Fatal("Variable %s is recursive.", v->name); |
3729 | /*NOTREACHED*/ | | 3731 | /*NOTREACHED*/ |
3730 | } else { | | 3732 | } else { |
3731 | v->flags |= VAR_IN_USE; | | 3733 | v->flags |= VAR_IN_USE; |
3732 | } | | 3734 | } |
3733 | /* | | 3735 | /* |
3734 | * Before doing any modification, we have to make sure the value | | 3736 | * Before doing any modification, we have to make sure the value |
3735 | * has been fully expanded. If it looks like recursion might be | | 3737 | * has been fully expanded. If it looks like recursion might be |
3736 | * necessary (there's a dollar sign somewhere in the variable's value) | | 3738 | * necessary (there's a dollar sign somewhere in the variable's value) |
3737 | * we just call Var_Subst to do any other substitutions that are | | 3739 | * we just call Var_Subst to do any other substitutions that are |
3738 | * necessary. Note that the value returned by Var_Subst will have | | 3740 | * necessary. Note that the value returned by Var_Subst will have |
3739 | * been dynamically-allocated, so it will need freeing when we | | 3741 | * been dynamically-allocated, so it will need freeing when we |
3740 | * return. | | 3742 | * return. |
3741 | */ | | 3743 | */ |
3742 | nstr = Buf_GetAll(&v->val, NULL); | | 3744 | nstr = Buf_GetAll(&v->val, NULL); |
3743 | if (strchr(nstr, '$') != NULL && (flags & VARE_WANTRES) != 0) { | | 3745 | if (strchr(nstr, '$') != NULL && (flags & VARE_WANTRES) != 0) { |
3744 | nstr = Var_Subst(NULL, nstr, ctxt, flags); | | 3746 | nstr = Var_Subst(NULL, nstr, ctxt, flags); |
3745 | *freePtr = nstr; | | 3747 | *freePtr = nstr; |
3746 | } | | 3748 | } |
3747 | | | 3749 | |
3748 | v->flags &= ~VAR_IN_USE; | | 3750 | v->flags &= ~VAR_IN_USE; |
3749 | | | 3751 | |
3750 | if (nstr != NULL && (haveModifier || extramodifiers != NULL)) { | | 3752 | if (nstr != NULL && (haveModifier || extramodifiers != NULL)) { |
3751 | void *extraFree; | | 3753 | void *extraFree; |
3752 | int used; | | 3754 | int used; |
3753 | | | 3755 | |
3754 | extraFree = NULL; | | 3756 | extraFree = NULL; |
3755 | if (extramodifiers != NULL) { | | 3757 | if (extramodifiers != NULL) { |
3756 | nstr = ApplyModifiers(nstr, extramodifiers, '(', ')', | | 3758 | nstr = ApplyModifiers(nstr, extramodifiers, '(', ')', |
3757 | v, ctxt, flags, &used, &extraFree); | | 3759 | v, ctxt, flags, &used, &extraFree); |
3758 | } | | 3760 | } |
3759 | | | 3761 | |
3760 | if (haveModifier) { | | 3762 | if (haveModifier) { |
3761 | /* Skip initial colon. */ | | 3763 | /* Skip initial colon. */ |
3762 | tstr++; | | 3764 | tstr++; |
3763 | | | 3765 | |
3764 | nstr = ApplyModifiers(nstr, tstr, startc, endc, | | 3766 | nstr = ApplyModifiers(nstr, tstr, startc, endc, |
3765 | v, ctxt, flags, &used, freePtr); | | 3767 | v, ctxt, flags, &used, freePtr); |
3766 | tstr += used; | | 3768 | tstr += used; |
3767 | free(extraFree); | | 3769 | free(extraFree); |
3768 | } else { | | 3770 | } else { |
3769 | *freePtr = extraFree; | | 3771 | *freePtr = extraFree; |
3770 | } | | 3772 | } |
3771 | } | | 3773 | } |
3772 | *lengthPtr = tstr - start + (*tstr ? 1 : 0); | | 3774 | *lengthPtr = tstr - start + (*tstr ? 1 : 0); |
3773 | | | 3775 | |
3774 | if (v->flags & VAR_FROM_ENV) { | | 3776 | if (v->flags & VAR_FROM_ENV) { |
3775 | Boolean destroy = FALSE; | | 3777 | Boolean destroy = FALSE; |
3776 | | | 3778 | |
3777 | if (nstr != Buf_GetAll(&v->val, NULL)) { | | 3779 | if (nstr != Buf_GetAll(&v->val, NULL)) { |
3778 | destroy = TRUE; | | 3780 | destroy = TRUE; |
3779 | } else { | | 3781 | } else { |
3780 | /* | | 3782 | /* |
3781 | * Returning the value unmodified, so tell the caller to free | | 3783 | * Returning the value unmodified, so tell the caller to free |
3782 | * the thing. | | 3784 | * the thing. |
3783 | */ | | 3785 | */ |
3784 | *freePtr = nstr; | | 3786 | *freePtr = nstr; |
3785 | } | | 3787 | } |
3786 | VarFreeEnv(v, destroy); | | 3788 | VarFreeEnv(v, destroy); |
3787 | } else if (v->flags & VAR_JUNK) { | | 3789 | } else if (v->flags & VAR_JUNK) { |
3788 | /* | | 3790 | /* |
3789 | * Perform any free'ing needed and set *freePtr to NULL so the caller | | 3791 | * Perform any free'ing needed and set *freePtr to NULL so the caller |
3790 | * doesn't try to free a static pointer. | | 3792 | * doesn't try to free a static pointer. |
3791 | * If VAR_KEEP is also set then we want to keep str as is. | | 3793 | * If VAR_KEEP is also set then we want to keep str as is. |
3792 | */ | | 3794 | */ |
3793 | if (!(v->flags & VAR_KEEP)) { | | 3795 | if (!(v->flags & VAR_KEEP)) { |
3794 | if (*freePtr) { | | 3796 | if (*freePtr) { |
3795 | free(nstr); | | 3797 | free(nstr); |
3796 | *freePtr = NULL; | | 3798 | *freePtr = NULL; |
3797 | } | | 3799 | } |
3798 | if (dynamic) { | | 3800 | if (dynamic) { |
3799 | nstr = bmake_strndup(start, *lengthPtr); | | 3801 | nstr = bmake_strndup(start, *lengthPtr); |
3800 | *freePtr = nstr; | | 3802 | *freePtr = nstr; |
3801 | } else { | | 3803 | } else { |
3802 | nstr = (flags & VARE_UNDEFERR) ? var_Error : varNoError; | | 3804 | nstr = (flags & VARE_UNDEFERR) ? var_Error : varNoError; |
3803 | } | | 3805 | } |
3804 | } | | 3806 | } |
3805 | if (nstr != Buf_GetAll(&v->val, NULL)) | | 3807 | if (nstr != Buf_GetAll(&v->val, NULL)) |
3806 | Buf_Destroy(&v->val, TRUE); | | 3808 | Buf_Destroy(&v->val, TRUE); |
3807 | free(v->name); | | 3809 | free(v->name); |
3808 | free(v); | | 3810 | free(v); |
3809 | } | | 3811 | } |
3810 | return nstr; | | 3812 | return nstr; |
3811 | } | | 3813 | } |
3812 | | | 3814 | |
3813 | /*- | | 3815 | /*- |
3814 | *----------------------------------------------------------------------- | | 3816 | *----------------------------------------------------------------------- |
3815 | * Var_Subst -- | | 3817 | * Var_Subst -- |
3816 | * Substitute for all variables in the given string in the given context. | | 3818 | * Substitute for all variables in the given string in the given context. |
3817 | * If flags & VARE_UNDEFERR, Parse_Error will be called when an undefined | | 3819 | * If flags & VARE_UNDEFERR, Parse_Error will be called when an undefined |
3818 | * variable is encountered. | | 3820 | * variable is encountered. |
3819 | * | | 3821 | * |
3820 | * Input: | | 3822 | * Input: |
3821 | * var Named variable || NULL for all | | 3823 | * var Named variable || NULL for all |
3822 | * str the string which to substitute | | 3824 | * str the string which to substitute |
3823 | * ctxt the context wherein to find variables | | 3825 | * ctxt the context wherein to find variables |
3824 | * flags VARE_UNDEFERR if undefineds are an error | | 3826 | * flags VARE_UNDEFERR if undefineds are an error |
3825 | * VARE_WANTRES if we actually want the result | | 3827 | * VARE_WANTRES if we actually want the result |
3826 | * VARE_ASSIGN if we are in a := assignment | | 3828 | * VARE_ASSIGN if we are in a := assignment |
3827 | * | | 3829 | * |
3828 | * Results: | | 3830 | * Results: |
3829 | * The resulting string. | | 3831 | * The resulting string. |
3830 | * | | 3832 | * |
3831 | * Side Effects: | | 3833 | * Side Effects: |
3832 | * None. | | 3834 | * None. |
3833 | *----------------------------------------------------------------------- | | 3835 | *----------------------------------------------------------------------- |
3834 | */ | | 3836 | */ |
3835 | char * | | 3837 | char * |
3836 | Var_Subst(const char *var, const char *str, GNode *ctxt, VarEvalFlags flags) | | 3838 | Var_Subst(const char *var, const char *str, GNode *ctxt, VarEvalFlags flags) |
3837 | { | | 3839 | { |
3838 | Buffer buf; /* Buffer for forming things */ | | 3840 | Buffer buf; /* Buffer for forming things */ |
3839 | char *val; /* Value to substitute for a variable */ | | 3841 | char *val; /* Value to substitute for a variable */ |
3840 | int length; /* Length of the variable invocation */ | | 3842 | int length; /* Length of the variable invocation */ |
3841 | Boolean trailingBslash; /* variable ends in \ */ | | 3843 | Boolean trailingBslash; /* variable ends in \ */ |
3842 | void *freeIt = NULL; /* Set if it should be freed */ | | 3844 | void *freeIt = NULL; /* Set if it should be freed */ |
3843 | static Boolean errorReported; /* Set true if an error has already | | 3845 | static Boolean errorReported; /* Set true if an error has already |
3844 | * been reported to prevent a plethora | | 3846 | * been reported to prevent a plethora |
3845 | * of messages when recursing */ | | 3847 | * of messages when recursing */ |
3846 | | | 3848 | |
3847 | Buf_Init(&buf, 0); | | 3849 | Buf_Init(&buf, 0); |
3848 | errorReported = FALSE; | | 3850 | errorReported = FALSE; |
3849 | trailingBslash = FALSE; | | 3851 | trailingBslash = FALSE; |
3850 | | | 3852 | |
3851 | while (*str) { | | 3853 | while (*str) { |
3852 | if (*str == '\n' && trailingBslash) | | 3854 | if (*str == '\n' && trailingBslash) |
3853 | Buf_AddByte(&buf, ' '); | | 3855 | Buf_AddByte(&buf, ' '); |
3854 | if (var == NULL && (*str == '$') && (str[1] == '$')) { | | 3856 | if (var == NULL && (*str == '$') && (str[1] == '$')) { |
3855 | /* | | 3857 | /* |
3856 | * A dollar sign may be escaped either with another dollar sign. | | 3858 | * A dollar sign may be escaped either with another dollar sign. |
3857 | * In such a case, we skip over the escape character and store the | | 3859 | * In such a case, we skip over the escape character and store the |
3858 | * dollar sign into the buffer directly. | | 3860 | * dollar sign into the buffer directly. |
3859 | */ | | 3861 | */ |
3860 | if (save_dollars && (flags & VARE_ASSIGN)) | | 3862 | if (save_dollars && (flags & VARE_ASSIGN)) |
3861 | Buf_AddByte(&buf, *str); | | 3863 | Buf_AddByte(&buf, *str); |
3862 | str++; | | 3864 | str++; |
3863 | Buf_AddByte(&buf, *str); | | 3865 | Buf_AddByte(&buf, *str); |
3864 | str++; | | 3866 | str++; |
3865 | } else if (*str != '$') { | | 3867 | } else if (*str != '$') { |
3866 | /* | | 3868 | /* |
3867 | * Skip as many characters as possible -- either to the end of | | 3869 | * Skip as many characters as possible -- either to the end of |
3868 | * the string or to the next dollar sign (variable invocation). | | 3870 | * the string or to the next dollar sign (variable invocation). |
3869 | */ | | 3871 | */ |
3870 | const char *cp; | | 3872 | const char *cp; |
3871 | | | 3873 | |
3872 | for (cp = str++; *str != '$' && *str != '\0'; str++) | | 3874 | for (cp = str++; *str != '$' && *str != '\0'; str++) |
3873 | continue; | | 3875 | continue; |
3874 | Buf_AddBytes(&buf, str - cp, cp); | | 3876 | Buf_AddBytes(&buf, str - cp, cp); |
3875 | } else { | | 3877 | } else { |
3876 | if (var != NULL) { | | 3878 | if (var != NULL) { |
3877 | int expand; | | 3879 | int expand; |
3878 | for (;;) { | | 3880 | for (;;) { |
3879 | if (str[1] == '\0') { | | 3881 | if (str[1] == '\0') { |
3880 | /* A trailing $ is kind of a special case */ | | 3882 | /* A trailing $ is kind of a special case */ |
3881 | Buf_AddByte(&buf, str[0]); | | 3883 | Buf_AddByte(&buf, str[0]); |
3882 | str++; | | 3884 | str++; |
3883 | expand = FALSE; | | 3885 | expand = FALSE; |
3884 | } else if (str[1] != PROPEN && str[1] != BROPEN) { | | 3886 | } else if (str[1] != PROPEN && str[1] != BROPEN) { |
3885 | if (str[1] != *var || strlen(var) > 1) { | | 3887 | if (str[1] != *var || strlen(var) > 1) { |
3886 | Buf_AddBytes(&buf, 2, str); | | 3888 | Buf_AddBytes(&buf, 2, str); |
3887 | str += 2; | | 3889 | str += 2; |
3888 | expand = FALSE; | | 3890 | expand = FALSE; |
3889 | } else | | 3891 | } else |
3890 | expand = TRUE; | | 3892 | expand = TRUE; |
3891 | break; | | 3893 | break; |
3892 | } else { | | 3894 | } else { |
3893 | const char *p; | | 3895 | const char *p; |
3894 | | | 3896 | |
3895 | /* Scan up to the end of the variable name. */ | | 3897 | /* Scan up to the end of the variable name. */ |
3896 | for (p = &str[2]; *p && | | 3898 | for (p = &str[2]; *p && |
3897 | *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++) | | 3899 | *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++) |
3898 | if (*p == '$') | | 3900 | if (*p == '$') |
3899 | break; | | 3901 | break; |
3900 | /* | | 3902 | /* |
3901 | * A variable inside the variable. We cannot expand | | 3903 | * A variable inside the variable. We cannot expand |
3902 | * the external variable yet, so we try again with | | 3904 | * the external variable yet, so we try again with |
3903 | * the nested one | | 3905 | * the nested one |
3904 | */ | | 3906 | */ |
3905 | if (*p == '$') { | | 3907 | if (*p == '$') { |
3906 | Buf_AddBytes(&buf, p - str, str); | | 3908 | Buf_AddBytes(&buf, p - str, str); |
3907 | str = p; | | 3909 | str = p; |
3908 | continue; | | 3910 | continue; |
3909 | } | | 3911 | } |
3910 | | | 3912 | |
3911 | if (strncmp(var, str + 2, p - str - 2) != 0 || | | 3913 | if (strncmp(var, str + 2, p - str - 2) != 0 || |
3912 | var[p - str - 2] != '\0') { | | 3914 | var[p - str - 2] != '\0') { |
3913 | /* | | 3915 | /* |
3914 | * Not the variable we want to expand, scan | | 3916 | * Not the variable we want to expand, scan |
3915 | * until the next variable | | 3917 | * until the next variable |
3916 | */ | | 3918 | */ |
3917 | for (; *p != '$' && *p != '\0'; p++) | | 3919 | for (; *p != '$' && *p != '\0'; p++) |
3918 | continue; | | 3920 | continue; |
3919 | Buf_AddBytes(&buf, p - str, str); | | 3921 | Buf_AddBytes(&buf, p - str, str); |
3920 | str = p; | | 3922 | str = p; |
3921 | expand = FALSE; | | 3923 | expand = FALSE; |
3922 | } else | | 3924 | } else |
3923 | expand = TRUE; | | 3925 | expand = TRUE; |
3924 | break; | | 3926 | break; |
3925 | } | | 3927 | } |
3926 | } | | 3928 | } |
3927 | if (!expand) | | 3929 | if (!expand) |
3928 | continue; | | 3930 | continue; |
3929 | } | | 3931 | } |
3930 | | | 3932 | |
3931 | val = Var_Parse(str, ctxt, flags, &length, &freeIt); | | 3933 | val = Var_Parse(str, ctxt, flags, &length, &freeIt); |
3932 | | | 3934 | |
3933 | /* | | 3935 | /* |
3934 | * When we come down here, val should either point to the | | 3936 | * When we come down here, val should either point to the |
3935 | * value of this variable, suitably modified, or be NULL. | | 3937 | * value of this variable, suitably modified, or be NULL. |
3936 | * Length should be the total length of the potential | | 3938 | * Length should be the total length of the potential |
3937 | * variable invocation (from $ to end character...) | | 3939 | * variable invocation (from $ to end character...) |
3938 | */ | | 3940 | */ |
3939 | if (val == var_Error || val == varNoError) { | | 3941 | if (val == var_Error || val == varNoError) { |
3940 | /* | | 3942 | /* |
3941 | * If performing old-time variable substitution, skip over | | 3943 | * If performing old-time variable substitution, skip over |
3942 | * the variable and continue with the substitution. Otherwise, | | 3944 | * the variable and continue with the substitution. Otherwise, |
3943 | * store the dollar sign and advance str so we continue with | | 3945 | * store the dollar sign and advance str so we continue with |
3944 | * the string... | | 3946 | * the string... |
3945 | */ | | 3947 | */ |
3946 | if (oldVars) { | | 3948 | if (oldVars) { |
3947 | str += length; | | 3949 | str += length; |
3948 | } else if ((flags & VARE_UNDEFERR) || val == var_Error) { | | 3950 | } else if ((flags & VARE_UNDEFERR) || val == var_Error) { |
3949 | /* | | 3951 | /* |
3950 | * If variable is undefined, complain and skip the | | 3952 | * If variable is undefined, complain and skip the |
3951 | * variable. The complaint will stop us from doing anything | | 3953 | * variable. The complaint will stop us from doing anything |
3952 | * when the file is parsed. | | 3954 | * when the file is parsed. |
3953 | */ | | 3955 | */ |
3954 | if (!errorReported) { | | 3956 | if (!errorReported) { |
3955 | Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"", | | 3957 | Parse_Error(PARSE_FATAL, "Undefined variable \"%.*s\"", |
3956 | length, str); | | 3958 | length, str); |
3957 | } | | 3959 | } |
3958 | str += length; | | 3960 | str += length; |
3959 | errorReported = TRUE; | | 3961 | errorReported = TRUE; |
3960 | } else { | | 3962 | } else { |
3961 | Buf_AddByte(&buf, *str); | | 3963 | Buf_AddByte(&buf, *str); |
3962 | str += 1; | | 3964 | str += 1; |
3963 | } | | 3965 | } |
3964 | } else { | | 3966 | } else { |
3965 | /* | | 3967 | /* |
3966 | * We've now got a variable structure to store in. But first, | | 3968 | * We've now got a variable structure to store in. But first, |
3967 | * advance the string pointer. | | 3969 | * advance the string pointer. |
3968 | */ | | 3970 | */ |
3969 | str += length; | | 3971 | str += length; |
3970 | | | 3972 | |
3971 | /* | | 3973 | /* |
3972 | * Copy all the characters from the variable value straight | | 3974 | * Copy all the characters from the variable value straight |
3973 | * into the new string. | | 3975 | * into the new string. |
3974 | */ | | 3976 | */ |
3975 | length = strlen(val); | | 3977 | length = strlen(val); |
3976 | Buf_AddBytes(&buf, length, val); | | 3978 | Buf_AddBytes(&buf, length, val); |
3977 | trailingBslash = length > 0 && val[length - 1] == '\\'; | | 3979 | trailingBslash = length > 0 && val[length - 1] == '\\'; |
3978 | } | | 3980 | } |
3979 | free(freeIt); | | 3981 | free(freeIt); |
3980 | freeIt = NULL; | | 3982 | freeIt = NULL; |
3981 | } | | 3983 | } |
3982 | } | | 3984 | } |
3983 | | | 3985 | |
3984 | return Buf_DestroyCompact(&buf); | | 3986 | return Buf_DestroyCompact(&buf); |
3985 | } | | 3987 | } |
3986 | | | 3988 | |
3987 | /* Initialize the module. */ | | 3989 | /* Initialize the module. */ |
3988 | void | | 3990 | void |
3989 | Var_Init(void) | | 3991 | Var_Init(void) |
3990 | { | | 3992 | { |
3991 | VAR_INTERNAL = Targ_NewGN("Internal"); | | 3993 | VAR_INTERNAL = Targ_NewGN("Internal"); |
3992 | VAR_GLOBAL = Targ_NewGN("Global"); | | 3994 | VAR_GLOBAL = Targ_NewGN("Global"); |
3993 | VAR_CMD = Targ_NewGN("Command"); | | 3995 | VAR_CMD = Targ_NewGN("Command"); |
3994 | } | | 3996 | } |
3995 | | | 3997 | |
3996 | | | 3998 | |
3997 | void | | 3999 | void |
3998 | Var_End(void) | | 4000 | Var_End(void) |
3999 | { | | 4001 | { |
4000 | } | | 4002 | } |
4001 | | | 4003 | |
4002 | | | 4004 | |
4003 | /****************** PRINT DEBUGGING INFO *****************/ | | 4005 | /****************** PRINT DEBUGGING INFO *****************/ |
4004 | static void | | 4006 | static void |
4005 | VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED) | | 4007 | VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED) |
4006 | { | | 4008 | { |
4007 | Var *v = (Var *)vp; | | 4009 | Var *v = (Var *)vp; |
4008 | fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL)); | | 4010 | fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL)); |
4009 | } | | 4011 | } |
4010 | | | 4012 | |
4011 | /*- | | 4013 | /*- |
4012 | *----------------------------------------------------------------------- | | 4014 | *----------------------------------------------------------------------- |
4013 | * Var_Dump -- | | 4015 | * Var_Dump -- |
4014 | * print all variables in a context | | 4016 | * print all variables in a context |
4015 | *----------------------------------------------------------------------- | | 4017 | *----------------------------------------------------------------------- |
4016 | */ | | 4018 | */ |
4017 | void | | 4019 | void |
4018 | Var_Dump(GNode *ctxt) | | 4020 | Var_Dump(GNode *ctxt) |
4019 | { | | 4021 | { |
4020 | Hash_ForEach(&ctxt->context, VarPrintVar, NULL); | | 4022 | Hash_ForEach(&ctxt->context, VarPrintVar, NULL); |
4021 | } | | 4023 | } |