| @@ -1,1142 +1,1142 @@ | | | @@ -1,1142 +1,1142 @@ |
1 | /* $NetBSD: var.c,v 1.868 2021/03/14 17:34:50 rillig Exp $ */ | | 1 | /* $NetBSD: var.c,v 1.869 2021/03/14 17:38:24 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 | /* | | 71 | /* |
72 | * Handling of variables and the expressions formed from them. | | 72 | * Handling of variables and the expressions formed from them. |
73 | * | | 73 | * |
74 | * Variables are set using lines of the form VAR=value. Both the variable | | 74 | * Variables are set using lines of the form VAR=value. Both the variable |
75 | * name and the value can contain references to other variables, by using | | 75 | * name and the value can contain references to other variables, by using |
76 | * expressions like ${VAR}, ${VAR:Modifiers}, ${${VARNAME}} or ${VAR:${MODS}}. | | 76 | * expressions like ${VAR}, ${VAR:Modifiers}, ${${VARNAME}} or ${VAR:${MODS}}. |
77 | * | | 77 | * |
78 | * Interface: | | 78 | * Interface: |
79 | * Var_Init Initialize this module. | | 79 | * Var_Init Initialize this module. |
80 | * | | 80 | * |
81 | * Var_End Clean up the module. | | 81 | * Var_End Clean up the module. |
82 | * | | 82 | * |
83 | * Var_Set | | 83 | * Var_Set |
84 | * Var_SetExpand | | 84 | * Var_SetExpand |
85 | * Set the value of the variable, creating it if | | 85 | * Set the value of the variable, creating it if |
86 | * necessary. | | 86 | * necessary. |
87 | * | | 87 | * |
88 | * Var_Append | | 88 | * Var_Append |
89 | * Var_AppendExpand | | 89 | * Var_AppendExpand |
90 | * Append more characters to the variable, creating it if | | 90 | * Append more characters to the variable, creating it if |
91 | * necessary. A space is placed between the old value and | | 91 | * necessary. A space is placed between the old value and |
92 | * the new one. | | 92 | * the new one. |
93 | * | | 93 | * |
94 | * Var_Exists | | 94 | * Var_Exists |
95 | * Var_ExistsExpand | | 95 | * Var_ExistsExpand |
96 | * See if a variable exists. | | 96 | * See if a variable exists. |
97 | * | | 97 | * |
98 | * Var_Value Return the unexpanded value of a variable, or NULL if | | 98 | * Var_Value Return the unexpanded value of a variable, or NULL if |
99 | * the variable is undefined. | | 99 | * the variable is undefined. |
100 | * | | 100 | * |
101 | * Var_Subst Substitute all variable expressions in a string. | | 101 | * Var_Subst Substitute all variable expressions in a string. |
102 | * | | 102 | * |
103 | * Var_Parse Parse a variable expression such as ${VAR:Mpattern}. | | 103 | * Var_Parse Parse a variable expression such as ${VAR:Mpattern}. |
104 | * | | 104 | * |
105 | * Var_Delete | | 105 | * Var_Delete |
106 | * Var_DeleteExpand | | 106 | * Var_DeleteExpand |
107 | * Delete a variable. | | 107 | * Delete a variable. |
108 | * | | 108 | * |
109 | * Var_ReexportVars | | 109 | * Var_ReexportVars |
110 | * Export some or even all variables to the environment | | 110 | * Export some or even all variables to the environment |
111 | * of this process and its child processes. | | 111 | * of this process and its child processes. |
112 | * | | 112 | * |
113 | * Var_Export Export the variable to the environment of this process | | 113 | * Var_Export Export the variable to the environment of this process |
114 | * and its child processes. | | 114 | * and its child processes. |
115 | * | | 115 | * |
116 | * Var_UnExport Don't export the variable anymore. | | 116 | * Var_UnExport Don't export the variable anymore. |
117 | * | | 117 | * |
118 | * Debugging: | | 118 | * Debugging: |
119 | * Var_Stats Print out hashing statistics if in -dh mode. | | 119 | * Var_Stats Print out hashing statistics if in -dh mode. |
120 | * | | 120 | * |
121 | * Var_Dump Print out all variables defined in the given scope. | | 121 | * Var_Dump Print out all variables defined in the given scope. |
122 | * | | 122 | * |
123 | * XXX: There's a lot of almost duplicate code in these functions that only | | 123 | * XXX: There's a lot of almost duplicate code in these functions that only |
124 | * differs in subtle details that are not mentioned in the manual page. | | 124 | * differs in subtle details that are not mentioned in the manual page. |
125 | */ | | 125 | */ |
126 | | | 126 | |
127 | #include <sys/stat.h> | | 127 | #include <sys/stat.h> |
128 | #ifndef NO_REGEX | | 128 | #ifndef NO_REGEX |
129 | #include <sys/types.h> | | 129 | #include <sys/types.h> |
130 | #include <regex.h> | | 130 | #include <regex.h> |
131 | #endif | | 131 | #endif |
132 | #include <errno.h> | | 132 | #include <errno.h> |
133 | #include <inttypes.h> | | 133 | #include <inttypes.h> |
134 | #include <limits.h> | | 134 | #include <limits.h> |
135 | #include <time.h> | | 135 | #include <time.h> |
136 | | | 136 | |
137 | #include "make.h" | | 137 | #include "make.h" |
138 | #include "dir.h" | | 138 | #include "dir.h" |
139 | #include "job.h" | | 139 | #include "job.h" |
140 | #include "metachar.h" | | 140 | #include "metachar.h" |
141 | | | 141 | |
142 | /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ | | 142 | /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ |
143 | MAKE_RCSID("$NetBSD: var.c,v 1.868 2021/03/14 17:34:50 rillig Exp $"); | | 143 | MAKE_RCSID("$NetBSD: var.c,v 1.869 2021/03/14 17:38:24 rillig Exp $"); |
144 | | | 144 | |
145 | typedef enum VarFlags { | | 145 | typedef enum VarFlags { |
146 | VFL_NONE = 0, | | 146 | VFL_NONE = 0, |
147 | | | 147 | |
148 | /* | | 148 | /* |
149 | * The variable's value is currently being used by Var_Parse or | | 149 | * The variable's value is currently being used by Var_Parse or |
150 | * Var_Subst. This marker is used to avoid endless recursion. | | 150 | * Var_Subst. This marker is used to avoid endless recursion. |
151 | */ | | 151 | */ |
152 | VFL_IN_USE = 1 << 0, | | 152 | VFL_IN_USE = 1 << 0, |
153 | | | 153 | |
154 | /* | | 154 | /* |
155 | * The variable comes from the environment. | | 155 | * The variable comes from the environment. |
156 | * These variables are not registered in any GNode, therefore they | | 156 | * These variables are not registered in any GNode, therefore they |
157 | * must be freed as soon as they are not used anymore. | | 157 | * must be freed as soon as they are not used anymore. |
158 | */ | | 158 | */ |
159 | VFL_FROM_ENV = 1 << 1, | | 159 | VFL_FROM_ENV = 1 << 1, |
160 | | | 160 | |
161 | /* | | 161 | /* |
162 | * The variable is exported to the environment, to be used by child | | 162 | * The variable is exported to the environment, to be used by child |
163 | * processes. | | 163 | * processes. |
164 | */ | | 164 | */ |
165 | VFL_EXPORTED = 1 << 2, | | 165 | VFL_EXPORTED = 1 << 2, |
166 | | | 166 | |
167 | /* | | 167 | /* |
168 | * At the point where this variable was exported, it contained an | | 168 | * At the point where this variable was exported, it contained an |
169 | * unresolved reference to another variable. Before any child | | 169 | * unresolved reference to another variable. Before any child |
170 | * process is started, it needs to be exported again, in the hope | | 170 | * process is started, it needs to be exported again, in the hope |
171 | * that the referenced variable can then be resolved. | | 171 | * that the referenced variable can then be resolved. |
172 | */ | | 172 | */ |
173 | VFL_REEXPORT = 1 << 3, | | 173 | VFL_REEXPORT = 1 << 3, |
174 | | | 174 | |
175 | /* The variable came from the command line. */ | | 175 | /* The variable came from the command line. */ |
176 | VFL_FROM_CMD = 1 << 4, | | 176 | VFL_FROM_CMD = 1 << 4, |
177 | | | 177 | |
178 | /* | | 178 | /* |
179 | * The variable value cannot be changed anymore, and the variable | | 179 | * The variable value cannot be changed anymore, and the variable |
180 | * cannot be deleted. Any attempts to do so are silently ignored, | | 180 | * cannot be deleted. Any attempts to do so are silently ignored, |
181 | * they are logged with -dv though. | | 181 | * they are logged with -dv though. |
182 | * | | 182 | * |
183 | * See VAR_SET_READONLY. | | 183 | * See VAR_SET_READONLY. |
184 | */ | | 184 | */ |
185 | VFL_READONLY = 1 << 5 | | 185 | VFL_READONLY = 1 << 5 |
186 | } VarFlags; | | 186 | } VarFlags; |
187 | | | 187 | |
188 | /* | | 188 | /* |
189 | * Variables are defined using one of the VAR=value assignments. Their | | 189 | * Variables are defined using one of the VAR=value assignments. Their |
190 | * value can be queried by expressions such as $V, ${VAR}, or with modifiers | | 190 | * value can be queried by expressions such as $V, ${VAR}, or with modifiers |
191 | * such as ${VAR:S,from,to,g:Q}. | | 191 | * such as ${VAR:S,from,to,g:Q}. |
192 | * | | 192 | * |
193 | * There are 3 kinds of variables: scope variables, environment variables, | | 193 | * There are 3 kinds of variables: scope variables, environment variables, |
194 | * undefined variables. | | 194 | * undefined variables. |
195 | * | | 195 | * |
196 | * Scope variables are stored in a GNode.scope. The only way to undefine | | 196 | * Scope variables are stored in a GNode.scope. The only way to undefine |
197 | * a scope variable is using the .undef directive. In particular, it must | | 197 | * a scope variable is using the .undef directive. In particular, it must |
198 | * not be possible to undefine a variable during the evaluation of an | | 198 | * not be possible to undefine a variable during the evaluation of an |
199 | * expression, or Var.name might point nowhere. | | 199 | * expression, or Var.name might point nowhere. |
200 | * | | 200 | * |
201 | * Environment variables are temporary. They are returned by VarFind, and | | 201 | * Environment variables are temporary. They are returned by VarFind, and |
202 | * after using them, they must be freed using VarFreeEnv. | | 202 | * after using them, they must be freed using VarFreeEnv. |
203 | * | | 203 | * |
204 | * Undefined variables occur during evaluation of variable expressions such | | 204 | * Undefined variables occur during evaluation of variable expressions such |
205 | * as ${UNDEF:Ufallback} in Var_Parse and ApplyModifiers. | | 205 | * as ${UNDEF:Ufallback} in Var_Parse and ApplyModifiers. |
206 | */ | | 206 | */ |
207 | typedef struct Var { | | 207 | typedef struct Var { |
208 | /* | | 208 | /* |
209 | * The name of the variable, once set, doesn't change anymore. | | 209 | * The name of the variable, once set, doesn't change anymore. |
210 | * For scope variables, it aliases the corresponding HashEntry name. | | 210 | * For scope variables, it aliases the corresponding HashEntry name. |
211 | * For environment and undefined variables, it is allocated. | | 211 | * For environment and undefined variables, it is allocated. |
212 | */ | | 212 | */ |
213 | FStr name; | | 213 | FStr name; |
214 | | | 214 | |
215 | /* The unexpanded value of the variable. */ | | 215 | /* The unexpanded value of the variable. */ |
216 | Buffer val; | | 216 | Buffer val; |
217 | /* Miscellaneous status flags. */ | | 217 | /* Miscellaneous status flags. */ |
218 | VarFlags flags; | | 218 | VarFlags flags; |
219 | } Var; | | 219 | } Var; |
220 | | | 220 | |
221 | /* | | 221 | /* |
222 | * Exporting variables is expensive and may leak memory, so skip it if we | | 222 | * Exporting variables is expensive and may leak memory, so skip it if we |
223 | * can. | | 223 | * can. |
224 | * | | 224 | * |
225 | * To avoid this, it might be worth encapsulating the environment variables | | 225 | * To avoid this, it might be worth encapsulating the environment variables |
226 | * in a separate data structure called EnvVars. | | 226 | * in a separate data structure called EnvVars. |
227 | */ | | 227 | */ |
228 | typedef enum VarExportedMode { | | 228 | typedef enum VarExportedMode { |
229 | VAR_EXPORTED_NONE, | | 229 | VAR_EXPORTED_NONE, |
230 | VAR_EXPORTED_SOME, | | 230 | VAR_EXPORTED_SOME, |
231 | VAR_EXPORTED_ALL | | 231 | VAR_EXPORTED_ALL |
232 | } VarExportedMode; | | 232 | } VarExportedMode; |
233 | | | 233 | |
234 | typedef enum UnexportWhat { | | 234 | typedef enum UnexportWhat { |
235 | /* Unexport the variables given by name. */ | | 235 | /* Unexport the variables given by name. */ |
236 | UNEXPORT_NAMED, | | 236 | UNEXPORT_NAMED, |
237 | /* | | 237 | /* |
238 | * Unexport all globals previously exported, but keep the environment | | 238 | * Unexport all globals previously exported, but keep the environment |
239 | * inherited from the parent. | | 239 | * inherited from the parent. |
240 | */ | | 240 | */ |
241 | UNEXPORT_ALL, | | 241 | UNEXPORT_ALL, |
242 | /* | | 242 | /* |
243 | * Unexport all globals previously exported and clear the environment | | 243 | * Unexport all globals previously exported and clear the environment |
244 | * inherited from the parent. | | 244 | * inherited from the parent. |
245 | */ | | 245 | */ |
246 | UNEXPORT_ENV | | 246 | UNEXPORT_ENV |
247 | } UnexportWhat; | | 247 | } UnexportWhat; |
248 | | | 248 | |
249 | /* Flags for pattern matching in the :S and :C modifiers */ | | 249 | /* Flags for pattern matching in the :S and :C modifiers */ |
250 | typedef struct VarPatternFlags { | | 250 | typedef struct VarPatternFlags { |
251 | | | 251 | |
252 | /* Replace as often as possible ('g') */ | | 252 | /* Replace as often as possible ('g') */ |
253 | Boolean subGlobal: 1; | | 253 | Boolean subGlobal: 1; |
254 | /* Replace only once ('1') */ | | 254 | /* Replace only once ('1') */ |
255 | Boolean subOnce: 1; | | 255 | Boolean subOnce: 1; |
256 | /* Match at start of word ('^') */ | | 256 | /* Match at start of word ('^') */ |
257 | Boolean anchorStart: 1; | | 257 | Boolean anchorStart: 1; |
258 | /* Match at end of word ('$') */ | | 258 | /* Match at end of word ('$') */ |
259 | Boolean anchorEnd: 1; | | 259 | Boolean anchorEnd: 1; |
260 | } VarPatternFlags; | | 260 | } VarPatternFlags; |
261 | | | 261 | |
262 | /* SepBuf builds a string from words interleaved with separators. */ | | 262 | /* SepBuf builds a string from words interleaved with separators. */ |
263 | typedef struct SepBuf { | | 263 | typedef struct SepBuf { |
264 | Buffer buf; | | 264 | Buffer buf; |
265 | Boolean needSep; | | 265 | Boolean needSep; |
266 | /* Usually ' ', but see the ':ts' modifier. */ | | 266 | /* Usually ' ', but see the ':ts' modifier. */ |
267 | char sep; | | 267 | char sep; |
268 | } SepBuf; | | 268 | } SepBuf; |
269 | | | 269 | |
270 | | | 270 | |
271 | ENUM_FLAGS_RTTI_4(VarEvalFlags, | | 271 | ENUM_FLAGS_RTTI_4(VarEvalFlags, |
272 | VARE_UNDEFERR, VARE_WANTRES, VARE_KEEP_DOLLAR, | | 272 | VARE_UNDEFERR, VARE_WANTRES, VARE_KEEP_DOLLAR, |
273 | VARE_KEEP_UNDEF); | | 273 | VARE_KEEP_UNDEF); |
274 | | | 274 | |
275 | /* | | 275 | /* |
276 | * This lets us tell if we have replaced the original environ | | 276 | * This lets us tell if we have replaced the original environ |
277 | * (which we cannot free). | | 277 | * (which we cannot free). |
278 | */ | | 278 | */ |
279 | char **savedEnv = NULL; | | 279 | char **savedEnv = NULL; |
280 | | | 280 | |
281 | /* | | 281 | /* |
282 | * Special return value for Var_Parse, indicating a parse error. It may be | | 282 | * Special return value for Var_Parse, indicating a parse error. It may be |
283 | * caused by an undefined variable, a syntax error in a modifier or | | 283 | * caused by an undefined variable, a syntax error in a modifier or |
284 | * something entirely different. | | 284 | * something entirely different. |
285 | */ | | 285 | */ |
286 | char var_Error[] = ""; | | 286 | char var_Error[] = ""; |
287 | | | 287 | |
288 | /* | | 288 | /* |
289 | * Special return value for Var_Parse, indicating an undefined variable in | | 289 | * Special return value for Var_Parse, indicating an undefined variable in |
290 | * a case where VARE_UNDEFERR is not set. This undefined variable is | | 290 | * a case where VARE_UNDEFERR is not set. This undefined variable is |
291 | * typically a dynamic variable such as ${.TARGET}, whose expansion needs to | | 291 | * typically a dynamic variable such as ${.TARGET}, whose expansion needs to |
292 | * be deferred until it is defined in an actual target. | | 292 | * be deferred until it is defined in an actual target. |
293 | * | | 293 | * |
294 | * See VARE_KEEP_UNDEF. | | 294 | * See VARE_KEEP_UNDEF. |
295 | */ | | 295 | */ |
296 | static char varUndefined[] = ""; | | 296 | static char varUndefined[] = ""; |
297 | | | 297 | |
298 | /* | | 298 | /* |
299 | * Traditionally this make consumed $$ during := like any other expansion. | | 299 | * Traditionally this make consumed $$ during := like any other expansion. |
300 | * Other make's do not, and this make follows straight since 2016-01-09. | | 300 | * Other make's do not, and this make follows straight since 2016-01-09. |
301 | * | | 301 | * |
302 | * This knob allows controlling the behavior. | | 302 | * This knob allows controlling the behavior. |
303 | * FALSE to consume $$ during := assignment. | | 303 | * FALSE to consume $$ during := assignment. |
304 | * TRUE to preserve $$ during := assignment. | | 304 | * TRUE to preserve $$ during := assignment. |
305 | */ | | 305 | */ |
306 | #define MAKE_SAVE_DOLLARS ".MAKE.SAVE_DOLLARS" | | 306 | #define MAKE_SAVE_DOLLARS ".MAKE.SAVE_DOLLARS" |
307 | static Boolean save_dollars = TRUE; | | 307 | static Boolean save_dollars = TRUE; |
308 | | | 308 | |
309 | /* | | 309 | /* |
310 | * A scope collects variable names and their values. | | 310 | * A scope collects variable names and their values. |
311 | * | | 311 | * |
312 | * The main scope is SCOPE_GLOBAL, which contains the variables that are set | | 312 | * The main scope is SCOPE_GLOBAL, which contains the variables that are set |
313 | * in the makefiles. SCOPE_INTERNAL acts as a fallback for SCOPE_GLOBAL and | | 313 | * in the makefiles. SCOPE_INTERNAL acts as a fallback for SCOPE_GLOBAL and |
314 | * contains some internal make variables. These internal variables can thus | | 314 | * contains some internal make variables. These internal variables can thus |
315 | * be overridden, they can also be restored by undefining the overriding | | 315 | * be overridden, they can also be restored by undefining the overriding |
316 | * variable. | | 316 | * variable. |
317 | * | | 317 | * |
318 | * SCOPE_CMDLINE contains variables from the command line arguments. These | | 318 | * SCOPE_CMDLINE contains variables from the command line arguments. These |
319 | * override variables from SCOPE_GLOBAL. | | 319 | * override variables from SCOPE_GLOBAL. |
320 | * | | 320 | * |
321 | * There is no scope for environment variables, these are generated on-the-fly | | 321 | * There is no scope for environment variables, these are generated on-the-fly |
322 | * whenever they are referenced. If there were such a scope, each change to | | 322 | * whenever they are referenced. If there were such a scope, each change to |
323 | * environment variables would have to be reflected in that scope, which may | | 323 | * environment variables would have to be reflected in that scope, which may |
324 | * be simpler or more complex than the current implementation. | | 324 | * be simpler or more complex than the current implementation. |
325 | * | | 325 | * |
326 | * Each target has its own scope, containing the 7 target-local variables | | 326 | * Each target has its own scope, containing the 7 target-local variables |
327 | * .TARGET, .ALLSRC, etc. No other variables are in these scopes. | | 327 | * .TARGET, .ALLSRC, etc. No other variables are in these scopes. |
328 | */ | | 328 | */ |
329 | | | 329 | |
330 | GNode *SCOPE_CMDLINE; | | 330 | GNode *SCOPE_CMDLINE; |
331 | GNode *SCOPE_GLOBAL; | | 331 | GNode *SCOPE_GLOBAL; |
332 | GNode *SCOPE_INTERNAL; | | 332 | GNode *SCOPE_INTERNAL; |
333 | | | 333 | |
334 | ENUM_FLAGS_RTTI_6(VarFlags, | | 334 | ENUM_FLAGS_RTTI_6(VarFlags, |
335 | VFL_IN_USE, VFL_FROM_ENV, | | 335 | VFL_IN_USE, VFL_FROM_ENV, |
336 | VFL_EXPORTED, VFL_REEXPORT, VFL_FROM_CMD, VFL_READONLY); | | 336 | VFL_EXPORTED, VFL_REEXPORT, VFL_FROM_CMD, VFL_READONLY); |
337 | | | 337 | |
338 | static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE; | | 338 | static VarExportedMode var_exportedVars = VAR_EXPORTED_NONE; |
339 | | | 339 | |
340 | | | 340 | |
341 | static Var * | | 341 | static Var * |
342 | VarNew(FStr name, const char *value, VarFlags flags) | | 342 | VarNew(FStr name, const char *value, VarFlags flags) |
343 | { | | 343 | { |
344 | size_t value_len = strlen(value); | | 344 | size_t value_len = strlen(value); |
345 | Var *var = bmake_malloc(sizeof *var); | | 345 | Var *var = bmake_malloc(sizeof *var); |
346 | var->name = name; | | 346 | var->name = name; |
347 | Buf_InitSize(&var->val, value_len + 1); | | 347 | Buf_InitSize(&var->val, value_len + 1); |
348 | Buf_AddBytes(&var->val, value, value_len); | | 348 | Buf_AddBytes(&var->val, value, value_len); |
349 | var->flags = flags; | | 349 | var->flags = flags; |
350 | return var; | | 350 | return var; |
351 | } | | 351 | } |
352 | | | 352 | |
353 | static const char * | | 353 | static const char * |
354 | CanonicalVarname(const char *name) | | 354 | CanonicalVarname(const char *name) |
355 | { | | 355 | { |
356 | if (*name == '.' && ch_isupper(name[1])) { | | 356 | if (*name == '.' && ch_isupper(name[1])) { |
357 | switch (name[1]) { | | 357 | switch (name[1]) { |
358 | case 'A': | | 358 | case 'A': |
359 | if (strcmp(name, ".ALLSRC") == 0) | | 359 | if (strcmp(name, ".ALLSRC") == 0) |
360 | name = ALLSRC; | | 360 | name = ALLSRC; |
361 | if (strcmp(name, ".ARCHIVE") == 0) | | 361 | if (strcmp(name, ".ARCHIVE") == 0) |
362 | name = ARCHIVE; | | 362 | name = ARCHIVE; |
363 | break; | | 363 | break; |
364 | case 'I': | | 364 | case 'I': |
365 | if (strcmp(name, ".IMPSRC") == 0) | | 365 | if (strcmp(name, ".IMPSRC") == 0) |
366 | name = IMPSRC; | | 366 | name = IMPSRC; |
367 | break; | | 367 | break; |
368 | case 'M': | | 368 | case 'M': |
369 | if (strcmp(name, ".MEMBER") == 0) | | 369 | if (strcmp(name, ".MEMBER") == 0) |
370 | name = MEMBER; | | 370 | name = MEMBER; |
371 | break; | | 371 | break; |
372 | case 'O': | | 372 | case 'O': |
373 | if (strcmp(name, ".OODATE") == 0) | | 373 | if (strcmp(name, ".OODATE") == 0) |
374 | name = OODATE; | | 374 | name = OODATE; |
375 | break; | | 375 | break; |
376 | case 'P': | | 376 | case 'P': |
377 | if (strcmp(name, ".PREFIX") == 0) | | 377 | if (strcmp(name, ".PREFIX") == 0) |
378 | name = PREFIX; | | 378 | name = PREFIX; |
379 | break; | | 379 | break; |
380 | case 'S': | | 380 | case 'S': |
381 | if (strcmp(name, ".SHELL") == 0) { | | 381 | if (strcmp(name, ".SHELL") == 0) { |
382 | if (shellPath == NULL) | | 382 | if (shellPath == NULL) |
383 | Shell_Init(); | | 383 | Shell_Init(); |
384 | } | | 384 | } |
385 | break; | | 385 | break; |
386 | case 'T': | | 386 | case 'T': |
387 | if (strcmp(name, ".TARGET") == 0) | | 387 | if (strcmp(name, ".TARGET") == 0) |
388 | name = TARGET; | | 388 | name = TARGET; |
389 | break; | | 389 | break; |
390 | } | | 390 | } |
391 | } | | 391 | } |
392 | | | 392 | |
393 | /* GNU make has an additional alias $^ == ${.ALLSRC}. */ | | 393 | /* GNU make has an additional alias $^ == ${.ALLSRC}. */ |
394 | | | 394 | |
395 | return name; | | 395 | return name; |
396 | } | | 396 | } |
397 | | | 397 | |
398 | static Var * | | 398 | static Var * |
399 | GNode_FindVar(GNode *scope, const char *varname, unsigned int hash) | | 399 | GNode_FindVar(GNode *scope, const char *varname, unsigned int hash) |
400 | { | | 400 | { |
401 | return HashTable_FindValueHash(&scope->vars, varname, hash); | | 401 | return HashTable_FindValueHash(&scope->vars, varname, hash); |
402 | } | | 402 | } |
403 | | | 403 | |
404 | /* | | 404 | /* |
405 | * Find the variable in the scope, and maybe in other scopes as well. | | 405 | * Find the variable in the scope, and maybe in other scopes as well. |
406 | * | | 406 | * |
407 | * Input: | | 407 | * Input: |
408 | * name name to find, is not expanded any further | | 408 | * name name to find, is not expanded any further |
409 | * scope scope in which to look first | | 409 | * scope scope in which to look first |
410 | * elsewhere TRUE to look in other scopes as well | | 410 | * elsewhere TRUE to look in other scopes as well |
411 | * | | 411 | * |
412 | * Results: | | 412 | * Results: |
413 | * The found variable, or NULL if the variable does not exist. | | 413 | * The found variable, or NULL if the variable does not exist. |
414 | * If the variable is an environment variable, it must be freed using | | 414 | * If the variable is an environment variable, it must be freed using |
415 | * VarFreeEnv after use. | | 415 | * VarFreeEnv after use. |
416 | */ | | 416 | */ |
417 | static Var * | | 417 | static Var * |
418 | VarFind(const char *name, GNode *scope, Boolean elsewhere) | | 418 | VarFind(const char *name, GNode *scope, Boolean elsewhere) |
419 | { | | 419 | { |
420 | Var *var; | | 420 | Var *var; |
421 | unsigned int nameHash; | | 421 | unsigned int nameHash; |
422 | | | 422 | |
423 | /* Replace '.TARGET' with '@', likewise for other local variables. */ | | 423 | /* Replace '.TARGET' with '@', likewise for other local variables. */ |
424 | name = CanonicalVarname(name); | | 424 | name = CanonicalVarname(name); |
425 | nameHash = Hash_Hash(name); | | 425 | nameHash = Hash_Hash(name); |
426 | | | 426 | |
427 | var = GNode_FindVar(scope, name, nameHash); | | 427 | var = GNode_FindVar(scope, name, nameHash); |
428 | if (!elsewhere) | | 428 | if (!elsewhere) |
429 | return var; | | 429 | return var; |
430 | | | 430 | |
431 | if (var == NULL && scope != SCOPE_CMDLINE) | | 431 | if (var == NULL && scope != SCOPE_CMDLINE) |
432 | var = GNode_FindVar(SCOPE_CMDLINE, name, nameHash); | | 432 | var = GNode_FindVar(SCOPE_CMDLINE, name, nameHash); |
433 | | | 433 | |
434 | if (!opts.checkEnvFirst && var == NULL && scope != SCOPE_GLOBAL) { | | 434 | if (!opts.checkEnvFirst && var == NULL && scope != SCOPE_GLOBAL) { |
435 | var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash); | | 435 | var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash); |
436 | if (var == NULL && scope != SCOPE_INTERNAL) { | | 436 | if (var == NULL && scope != SCOPE_INTERNAL) { |
437 | /* SCOPE_INTERNAL is subordinate to SCOPE_GLOBAL */ | | 437 | /* SCOPE_INTERNAL is subordinate to SCOPE_GLOBAL */ |
438 | var = GNode_FindVar(SCOPE_INTERNAL, name, nameHash); | | 438 | var = GNode_FindVar(SCOPE_INTERNAL, name, nameHash); |
439 | } | | 439 | } |
440 | } | | 440 | } |
441 | | | 441 | |
442 | if (var == NULL) { | | 442 | if (var == NULL) { |
443 | char *env; | | 443 | char *env; |
444 | | | 444 | |
445 | if ((env = getenv(name)) != NULL) { | | 445 | if ((env = getenv(name)) != NULL) { |
446 | char *varname = bmake_strdup(name); | | 446 | char *varname = bmake_strdup(name); |
447 | return VarNew(FStr_InitOwn(varname), env, VFL_FROM_ENV); | | 447 | return VarNew(FStr_InitOwn(varname), env, VFL_FROM_ENV); |
448 | } | | 448 | } |
449 | | | 449 | |
450 | if (opts.checkEnvFirst && scope != SCOPE_GLOBAL) { | | 450 | if (opts.checkEnvFirst && scope != SCOPE_GLOBAL) { |
451 | var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash); | | 451 | var = GNode_FindVar(SCOPE_GLOBAL, name, nameHash); |
452 | if (var == NULL && scope != SCOPE_INTERNAL) | | 452 | if (var == NULL && scope != SCOPE_INTERNAL) |
453 | var = GNode_FindVar(SCOPE_INTERNAL, name, | | 453 | var = GNode_FindVar(SCOPE_INTERNAL, name, |
454 | nameHash); | | 454 | nameHash); |
455 | return var; | | 455 | return var; |
456 | } | | 456 | } |
457 | | | 457 | |
458 | return NULL; | | 458 | return NULL; |
459 | } | | 459 | } |
460 | | | 460 | |
461 | return var; | | 461 | return var; |
462 | } | | 462 | } |
463 | | | 463 | |
464 | /* | | 464 | /* |
465 | * If the variable is an environment variable, free it, including its value. | | 465 | * If the variable is an environment variable, free it, including its value. |
466 | * | | 466 | * |
467 | * Results: | | 467 | * Results: |
468 | * TRUE if it was an environment variable, | | 468 | * TRUE if it was an environment variable, |
469 | * FALSE if it is still a regular variable. | | 469 | * FALSE if it is still a regular variable. |
470 | */ | | 470 | */ |
471 | static void | | 471 | static void |
472 | VarFreeEnv(Var *v) | | 472 | VarFreeEnv(Var *v) |
473 | { | | 473 | { |
474 | if (!(v->flags & VFL_FROM_ENV)) | | 474 | if (!(v->flags & VFL_FROM_ENV)) |
475 | return; | | 475 | return; |
476 | | | 476 | |
477 | FStr_Done(&v->name); | | 477 | FStr_Done(&v->name); |
478 | Buf_Done(&v->val); | | 478 | Buf_Done(&v->val); |
479 | free(v); | | 479 | free(v); |
480 | } | | 480 | } |
481 | | | 481 | |
482 | /* Add a new variable of the given name and value to the given scope. */ | | 482 | /* Add a new variable of the given name and value to the given scope. */ |
483 | static Var * | | 483 | static Var * |
484 | VarAdd(const char *name, const char *value, GNode *scope, VarSetFlags flags) | | 484 | VarAdd(const char *name, const char *value, GNode *scope, VarSetFlags flags) |
485 | { | | 485 | { |
486 | HashEntry *he = HashTable_CreateEntry(&scope->vars, name, NULL); | | 486 | HashEntry *he = HashTable_CreateEntry(&scope->vars, name, NULL); |
487 | Var *v = VarNew(FStr_InitRefer(/* aliased to */ he->key), value, | | 487 | Var *v = VarNew(FStr_InitRefer(/* aliased to */ he->key), value, |
488 | flags & VAR_SET_READONLY ? VFL_READONLY : VFL_NONE); | | 488 | flags & VAR_SET_READONLY ? VFL_READONLY : VFL_NONE); |
489 | HashEntry_Set(he, v); | | 489 | HashEntry_Set(he, v); |
490 | DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, value); | | 490 | DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, value); |
491 | return v; | | 491 | return v; |
492 | } | | 492 | } |
493 | | | 493 | |
494 | /* | | 494 | /* |
495 | * Remove a variable from a scope, freeing all related memory as well. | | 495 | * Remove a variable from a scope, freeing all related memory as well. |
496 | * The variable name is kept as-is, it is not expanded. | | 496 | * The variable name is kept as-is, it is not expanded. |
497 | */ | | 497 | */ |
498 | void | | 498 | void |
499 | Var_Delete(GNode *scope, const char *varname) | | 499 | Var_Delete(GNode *scope, const char *varname) |
500 | { | | 500 | { |
501 | HashEntry *he = HashTable_FindEntry(&scope->vars, varname); | | 501 | HashEntry *he = HashTable_FindEntry(&scope->vars, varname); |
502 | Var *v; | | 502 | Var *v; |
503 | | | 503 | |
504 | if (he == NULL) { | | 504 | if (he == NULL) { |
505 | DEBUG2(VAR, "%s:delete %s (not found)\n", scope->name, varname); | | 505 | DEBUG2(VAR, "%s:delete %s (not found)\n", scope->name, varname); |
506 | return; | | 506 | return; |
507 | } | | 507 | } |
508 | | | 508 | |
509 | DEBUG2(VAR, "%s:delete %s\n", scope->name, varname); | | 509 | DEBUG2(VAR, "%s:delete %s\n", scope->name, varname); |
510 | v = he->value; | | 510 | v = he->value; |
511 | if (v->flags & VFL_EXPORTED) | | 511 | if (v->flags & VFL_EXPORTED) |
512 | unsetenv(v->name.str); | | 512 | unsetenv(v->name.str); |
513 | if (strcmp(v->name.str, MAKE_EXPORTED) == 0) | | 513 | if (strcmp(v->name.str, MAKE_EXPORTED) == 0) |
514 | var_exportedVars = VAR_EXPORTED_NONE; | | 514 | var_exportedVars = VAR_EXPORTED_NONE; |
515 | assert(v->name.freeIt == NULL); | | 515 | assert(v->name.freeIt == NULL); |
516 | HashTable_DeleteEntry(&scope->vars, he); | | 516 | HashTable_DeleteEntry(&scope->vars, he); |
517 | Buf_Done(&v->val); | | 517 | Buf_Done(&v->val); |
518 | free(v); | | 518 | free(v); |
519 | } | | 519 | } |
520 | | | 520 | |
521 | /* | | 521 | /* |
522 | * Remove a variable from a scope, freeing all related memory as well. | | 522 | * Remove a variable from a scope, freeing all related memory as well. |
523 | * The variable name is expanded once. | | 523 | * The variable name is expanded once. |
524 | */ | | 524 | */ |
525 | void | | 525 | void |
526 | Var_DeleteExpand(GNode *scope, const char *name) | | 526 | Var_DeleteExpand(GNode *scope, const char *name) |
527 | { | | 527 | { |
528 | FStr varname = FStr_InitRefer(name); | | 528 | FStr varname = FStr_InitRefer(name); |
529 | | | 529 | |
530 | if (strchr(varname.str, '$') != NULL) { | | 530 | if (strchr(varname.str, '$') != NULL) { |
531 | char *expanded; | | 531 | char *expanded; |
532 | (void)Var_Subst(varname.str, SCOPE_GLOBAL, VARE_WANTRES, | | 532 | (void)Var_Subst(varname.str, SCOPE_GLOBAL, VARE_WANTRES, |
533 | &expanded); | | 533 | &expanded); |
534 | /* TODO: handle errors */ | | 534 | /* TODO: handle errors */ |
535 | varname = FStr_InitOwn(expanded); | | 535 | varname = FStr_InitOwn(expanded); |
536 | } | | 536 | } |
537 | | | 537 | |
538 | Var_Delete(scope, varname.str); | | 538 | Var_Delete(scope, varname.str); |
539 | FStr_Done(&varname); | | 539 | FStr_Done(&varname); |
540 | } | | 540 | } |
541 | | | 541 | |
542 | /* | | 542 | /* |
543 | * Undefine one or more variables from the global scope. | | 543 | * Undefine one or more variables from the global scope. |
544 | * The argument is expanded exactly once and then split into words. | | 544 | * The argument is expanded exactly once and then split into words. |
545 | */ | | 545 | */ |
546 | void | | 546 | void |
547 | Var_Undef(const char *arg) | | 547 | Var_Undef(const char *arg) |
548 | { | | 548 | { |
549 | VarParseResult vpr; | | 549 | VarParseResult vpr; |
550 | char *expanded; | | 550 | char *expanded; |
551 | Words varnames; | | 551 | Words varnames; |
552 | size_t i; | | 552 | size_t i; |
553 | | | 553 | |
554 | if (arg[0] == '\0') { | | 554 | if (arg[0] == '\0') { |
555 | Parse_Error(PARSE_FATAL, | | 555 | Parse_Error(PARSE_FATAL, |
556 | "The .undef directive requires an argument"); | | 556 | "The .undef directive requires an argument"); |
557 | return; | | 557 | return; |
558 | } | | 558 | } |
559 | | | 559 | |
560 | vpr = Var_Subst(arg, SCOPE_GLOBAL, VARE_WANTRES, &expanded); | | 560 | vpr = Var_Subst(arg, SCOPE_GLOBAL, VARE_WANTRES, &expanded); |
561 | if (vpr != VPR_OK) { | | 561 | if (vpr != VPR_OK) { |
562 | Parse_Error(PARSE_FATAL, | | 562 | Parse_Error(PARSE_FATAL, |
563 | "Error in variable names to be undefined"); | | 563 | "Error in variable names to be undefined"); |
564 | return; | | 564 | return; |
565 | } | | 565 | } |
566 | | | 566 | |
567 | varnames = Str_Words(expanded, FALSE); | | 567 | varnames = Str_Words(expanded, FALSE); |
568 | if (varnames.len == 1 && varnames.words[0][0] == '\0') | | 568 | if (varnames.len == 1 && varnames.words[0][0] == '\0') |
569 | varnames.len = 0; | | 569 | varnames.len = 0; |
570 | | | 570 | |
571 | for (i = 0; i < varnames.len; i++) { | | 571 | for (i = 0; i < varnames.len; i++) { |
572 | const char *varname = varnames.words[i]; | | 572 | const char *varname = varnames.words[i]; |
573 | Global_Delete(varname); | | 573 | Global_Delete(varname); |
574 | } | | 574 | } |
575 | | | 575 | |
576 | Words_Free(varnames); | | 576 | Words_Free(varnames); |
577 | free(expanded); | | 577 | free(expanded); |
578 | } | | 578 | } |
579 | | | 579 | |
580 | static Boolean | | 580 | static Boolean |
581 | MayExport(const char *name) | | 581 | MayExport(const char *name) |
582 | { | | 582 | { |
583 | if (name[0] == '.') | | 583 | if (name[0] == '.') |
584 | return FALSE; /* skip internals */ | | 584 | return FALSE; /* skip internals */ |
585 | if (name[0] == '-') | | 585 | if (name[0] == '-') |
586 | return FALSE; /* skip misnamed variables */ | | 586 | return FALSE; /* skip misnamed variables */ |
587 | if (name[1] == '\0') { | | 587 | if (name[1] == '\0') { |
588 | /* | | 588 | /* |
589 | * A single char. | | 589 | * A single char. |
590 | * If it is one of the variables that should only appear in | | 590 | * If it is one of the variables that should only appear in |
591 | * local scope, skip it, else we can get Var_Subst | | 591 | * local scope, skip it, else we can get Var_Subst |
592 | * into a loop. | | 592 | * into a loop. |
593 | */ | | 593 | */ |
594 | switch (name[0]) { | | 594 | switch (name[0]) { |
595 | case '@': | | 595 | case '@': |
596 | case '%': | | 596 | case '%': |
597 | case '*': | | 597 | case '*': |
598 | case '!': | | 598 | case '!': |
599 | return FALSE; | | 599 | return FALSE; |
600 | } | | 600 | } |
601 | } | | 601 | } |
602 | return TRUE; | | 602 | return TRUE; |
603 | } | | 603 | } |
604 | | | 604 | |
605 | static Boolean | | 605 | static Boolean |
606 | ExportVarEnv(Var *v) | | 606 | ExportVarEnv(Var *v) |
607 | { | | 607 | { |
608 | const char *name = v->name.str; | | 608 | const char *name = v->name.str; |
609 | char *val = v->val.data; | | 609 | char *val = v->val.data; |
610 | char *expr; | | 610 | char *expr; |
611 | | | 611 | |
612 | if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT)) | | 612 | if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT)) |
613 | return FALSE; /* nothing to do */ | | 613 | return FALSE; /* nothing to do */ |
614 | | | 614 | |
615 | if (strchr(val, '$') == NULL) { | | 615 | if (strchr(val, '$') == NULL) { |
616 | if (!(v->flags & VFL_EXPORTED)) | | 616 | if (!(v->flags & VFL_EXPORTED)) |
617 | setenv(name, val, 1); | | 617 | setenv(name, val, 1); |
618 | return TRUE; | | 618 | return TRUE; |
619 | } | | 619 | } |
620 | | | 620 | |
621 | if (v->flags & VFL_IN_USE) { | | 621 | if (v->flags & VFL_IN_USE) { |
622 | /* | | 622 | /* |
623 | * We recursed while exporting in a child. | | 623 | * We recursed while exporting in a child. |
624 | * This isn't going to end well, just skip it. | | 624 | * This isn't going to end well, just skip it. |
625 | */ | | 625 | */ |
626 | return FALSE; | | 626 | return FALSE; |
627 | } | | 627 | } |
628 | | | 628 | |
629 | /* XXX: name is injected without escaping it */ | | 629 | /* XXX: name is injected without escaping it */ |
630 | expr = str_concat3("${", name, "}"); | | 630 | expr = str_concat3("${", name, "}"); |
631 | (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &val); | | 631 | (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &val); |
632 | /* TODO: handle errors */ | | 632 | /* TODO: handle errors */ |
633 | setenv(name, val, 1); | | 633 | setenv(name, val, 1); |
634 | free(val); | | 634 | free(val); |
635 | free(expr); | | 635 | free(expr); |
636 | return TRUE; | | 636 | return TRUE; |
637 | } | | 637 | } |
638 | | | 638 | |
639 | static Boolean | | 639 | static Boolean |
640 | ExportVarPlain(Var *v) | | 640 | ExportVarPlain(Var *v) |
641 | { | | 641 | { |
642 | if (strchr(v->val.data, '$') == NULL) { | | 642 | if (strchr(v->val.data, '$') == NULL) { |
643 | setenv(v->name.str, v->val.data, 1); | | 643 | setenv(v->name.str, v->val.data, 1); |
644 | v->flags |= VFL_EXPORTED; | | 644 | v->flags |= VFL_EXPORTED; |
645 | v->flags &= ~(unsigned)VFL_REEXPORT; | | 645 | v->flags &= ~(unsigned)VFL_REEXPORT; |
646 | return TRUE; | | 646 | return TRUE; |
647 | } | | 647 | } |
648 | | | 648 | |
649 | /* | | 649 | /* |
650 | * Flag the variable as something we need to re-export. | | 650 | * Flag the variable as something we need to re-export. |
651 | * No point actually exporting it now though, | | 651 | * No point actually exporting it now though, |
652 | * the child process can do it at the last minute. | | 652 | * the child process can do it at the last minute. |
653 | * Avoid calling setenv more often than necessary since it can leak. | | 653 | * Avoid calling setenv more often than necessary since it can leak. |
654 | */ | | 654 | */ |
655 | v->flags |= VFL_EXPORTED | VFL_REEXPORT; | | 655 | v->flags |= VFL_EXPORTED | VFL_REEXPORT; |
656 | return TRUE; | | 656 | return TRUE; |
657 | } | | 657 | } |
658 | | | 658 | |
659 | static Boolean | | 659 | static Boolean |
660 | ExportVarLiteral(Var *v) | | 660 | ExportVarLiteral(Var *v) |
661 | { | | 661 | { |
662 | if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT)) | | 662 | if ((v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT)) |
663 | return FALSE; | | 663 | return FALSE; |
664 | | | 664 | |
665 | if (!(v->flags & VFL_EXPORTED)) | | 665 | if (!(v->flags & VFL_EXPORTED)) |
666 | setenv(v->name.str, v->val.data, 1); | | 666 | setenv(v->name.str, v->val.data, 1); |
667 | | | 667 | |
668 | return TRUE; | | 668 | return TRUE; |
669 | } | | 669 | } |
670 | | | 670 | |
671 | /* | | 671 | /* |
672 | * Mark a single variable to be exported later for subprocesses. | | 672 | * Mark a single variable to be exported later for subprocesses. |
673 | * | | 673 | * |
674 | * Internal variables (those starting with '.') are not exported. | | 674 | * Internal variables (those starting with '.') are not exported. |
675 | */ | | 675 | */ |
676 | static Boolean | | 676 | static Boolean |
677 | ExportVar(const char *name, VarExportMode mode) | | 677 | ExportVar(const char *name, VarExportMode mode) |
678 | { | | 678 | { |
679 | Var *v; | | 679 | Var *v; |
680 | | | 680 | |
681 | if (!MayExport(name)) | | 681 | if (!MayExport(name)) |
682 | return FALSE; | | 682 | return FALSE; |
683 | | | 683 | |
684 | v = VarFind(name, SCOPE_GLOBAL, FALSE); | | 684 | v = VarFind(name, SCOPE_GLOBAL, FALSE); |
685 | if (v == NULL) | | 685 | if (v == NULL) |
686 | return FALSE; | | 686 | return FALSE; |
687 | | | 687 | |
688 | if (mode == VEM_ENV) | | 688 | if (mode == VEM_ENV) |
689 | return ExportVarEnv(v); | | 689 | return ExportVarEnv(v); |
690 | else if (mode == VEM_PLAIN) | | 690 | else if (mode == VEM_PLAIN) |
691 | return ExportVarPlain(v); | | 691 | return ExportVarPlain(v); |
692 | else | | 692 | else |
693 | return ExportVarLiteral(v); | | 693 | return ExportVarLiteral(v); |
694 | } | | 694 | } |
695 | | | 695 | |
696 | /* | | 696 | /* |
697 | * Actually export the variables that have been marked as needing to be | | 697 | * Actually export the variables that have been marked as needing to be |
698 | * re-exported. | | 698 | * re-exported. |
699 | */ | | 699 | */ |
700 | void | | 700 | void |
701 | Var_ReexportVars(void) | | 701 | Var_ReexportVars(void) |
702 | { | | 702 | { |
703 | char *xvarnames; | | 703 | char *xvarnames; |
704 | | | 704 | |
705 | /* | | 705 | /* |
706 | * Several make implementations support this sort of mechanism for | | 706 | * Several make implementations support this sort of mechanism for |
707 | * tracking recursion - but each uses a different name. | | 707 | * tracking recursion - but each uses a different name. |
708 | * We allow the makefiles to update MAKELEVEL and ensure | | 708 | * We allow the makefiles to update MAKELEVEL and ensure |
709 | * children see a correctly incremented value. | | 709 | * children see a correctly incremented value. |
710 | */ | | 710 | */ |
711 | char tmp[21]; | | 711 | char tmp[21]; |
712 | snprintf(tmp, sizeof tmp, "%d", makelevel + 1); | | 712 | snprintf(tmp, sizeof tmp, "%d", makelevel + 1); |
713 | setenv(MAKE_LEVEL_ENV, tmp, 1); | | 713 | setenv(MAKE_LEVEL_ENV, tmp, 1); |
714 | | | 714 | |
715 | if (var_exportedVars == VAR_EXPORTED_NONE) | | 715 | if (var_exportedVars == VAR_EXPORTED_NONE) |
716 | return; | | 716 | return; |
717 | | | 717 | |
718 | if (var_exportedVars == VAR_EXPORTED_ALL) { | | 718 | if (var_exportedVars == VAR_EXPORTED_ALL) { |
719 | HashIter hi; | | 719 | HashIter hi; |
720 | | | 720 | |
721 | /* Ouch! Exporting all variables at once is crazy. */ | | 721 | /* Ouch! Exporting all variables at once is crazy. */ |
722 | HashIter_Init(&hi, &SCOPE_GLOBAL->vars); | | 722 | HashIter_Init(&hi, &SCOPE_GLOBAL->vars); |
723 | while (HashIter_Next(&hi) != NULL) { | | 723 | while (HashIter_Next(&hi) != NULL) { |
724 | Var *var = hi.entry->value; | | 724 | Var *var = hi.entry->value; |
725 | ExportVar(var->name.str, VEM_ENV); | | 725 | ExportVar(var->name.str, VEM_ENV); |
726 | } | | 726 | } |
727 | return; | | 727 | return; |
728 | } | | 728 | } |
729 | | | 729 | |
730 | (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL, VARE_WANTRES, | | 730 | (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL, VARE_WANTRES, |
731 | &xvarnames); | | 731 | &xvarnames); |
732 | /* TODO: handle errors */ | | 732 | /* TODO: handle errors */ |
733 | if (xvarnames[0] != '\0') { | | 733 | if (xvarnames[0] != '\0') { |
734 | Words varnames = Str_Words(xvarnames, FALSE); | | 734 | Words varnames = Str_Words(xvarnames, FALSE); |
735 | size_t i; | | 735 | size_t i; |
736 | | | 736 | |
737 | for (i = 0; i < varnames.len; i++) | | 737 | for (i = 0; i < varnames.len; i++) |
738 | ExportVar(varnames.words[i], VEM_ENV); | | 738 | ExportVar(varnames.words[i], VEM_ENV); |
739 | Words_Free(varnames); | | 739 | Words_Free(varnames); |
740 | } | | 740 | } |
741 | free(xvarnames); | | 741 | free(xvarnames); |
742 | } | | 742 | } |
743 | | | 743 | |
744 | static void | | 744 | static void |
745 | ExportVars(const char *varnames, Boolean isExport, VarExportMode mode) | | 745 | ExportVars(const char *varnames, Boolean isExport, VarExportMode mode) |
746 | /* TODO: try to combine the parameters 'isExport' and 'mode'. */ | | 746 | /* TODO: try to combine the parameters 'isExport' and 'mode'. */ |
747 | { | | 747 | { |
748 | Words words = Str_Words(varnames, FALSE); | | 748 | Words words = Str_Words(varnames, FALSE); |
749 | size_t i; | | 749 | size_t i; |
750 | | | 750 | |
751 | if (words.len == 1 && words.words[0][0] == '\0') | | 751 | if (words.len == 1 && words.words[0][0] == '\0') |
752 | words.len = 0; | | 752 | words.len = 0; |
753 | | | 753 | |
754 | for (i = 0; i < words.len; i++) { | | 754 | for (i = 0; i < words.len; i++) { |
755 | const char *varname = words.words[i]; | | 755 | const char *varname = words.words[i]; |
756 | if (!ExportVar(varname, mode)) | | 756 | if (!ExportVar(varname, mode)) |
757 | continue; | | 757 | continue; |
758 | | | 758 | |
759 | if (var_exportedVars == VAR_EXPORTED_NONE) | | 759 | if (var_exportedVars == VAR_EXPORTED_NONE) |
760 | var_exportedVars = VAR_EXPORTED_SOME; | | 760 | var_exportedVars = VAR_EXPORTED_SOME; |
761 | | | 761 | |
762 | if (isExport && mode == VEM_PLAIN) | | 762 | if (isExport && mode == VEM_PLAIN) |
763 | Global_Append(MAKE_EXPORTED, varname); | | 763 | Global_Append(MAKE_EXPORTED, varname); |
764 | } | | 764 | } |
765 | Words_Free(words); | | 765 | Words_Free(words); |
766 | } | | 766 | } |
767 | | | 767 | |
768 | static void | | 768 | static void |
769 | ExportVarsExpand(const char *uvarnames, Boolean isExport, VarExportMode mode) | | 769 | ExportVarsExpand(const char *uvarnames, Boolean isExport, VarExportMode mode) |
770 | { | | 770 | { |
771 | char *xvarnames; | | 771 | char *xvarnames; |
772 | | | 772 | |
773 | (void)Var_Subst(uvarnames, SCOPE_GLOBAL, VARE_WANTRES, &xvarnames); | | 773 | (void)Var_Subst(uvarnames, SCOPE_GLOBAL, VARE_WANTRES, &xvarnames); |
774 | /* TODO: handle errors */ | | 774 | /* TODO: handle errors */ |
775 | ExportVars(xvarnames, isExport, mode); | | 775 | ExportVars(xvarnames, isExport, mode); |
776 | free(xvarnames); | | 776 | free(xvarnames); |
777 | } | | 777 | } |
778 | | | 778 | |
779 | /* Export the named variables, or all variables. */ | | 779 | /* Export the named variables, or all variables. */ |
780 | void | | 780 | void |
781 | Var_Export(VarExportMode mode, const char *varnames) | | 781 | Var_Export(VarExportMode mode, const char *varnames) |
782 | { | | 782 | { |
783 | if (mode == VEM_PLAIN && varnames[0] == '\0') { | | 783 | if (mode == VEM_PLAIN && varnames[0] == '\0') { |
784 | var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ | | 784 | var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ |
785 | return; | | 785 | return; |
786 | } | | 786 | } |
787 | | | 787 | |
788 | ExportVarsExpand(varnames, TRUE, mode); | | 788 | ExportVarsExpand(varnames, TRUE, mode); |
789 | } | | 789 | } |
790 | | | 790 | |
791 | void | | 791 | void |
792 | Var_ExportVars(const char *varnames) | | 792 | Var_ExportVars(const char *varnames) |
793 | { | | 793 | { |
794 | ExportVarsExpand(varnames, FALSE, VEM_PLAIN); | | 794 | ExportVarsExpand(varnames, FALSE, VEM_PLAIN); |
795 | } | | 795 | } |
796 | | | 796 | |
797 | | | 797 | |
798 | extern char **environ; | | 798 | extern char **environ; |
799 | | | 799 | |
800 | static void | | 800 | static void |
801 | ClearEnv(void) | | 801 | ClearEnv(void) |
802 | { | | 802 | { |
803 | const char *cp; | | 803 | const char *cp; |
804 | char **newenv; | | 804 | char **newenv; |
805 | | | 805 | |
806 | cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ | | 806 | cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ |
807 | if (environ == savedEnv) { | | 807 | if (environ == savedEnv) { |
808 | /* we have been here before! */ | | 808 | /* we have been here before! */ |
809 | newenv = bmake_realloc(environ, 2 * sizeof(char *)); | | 809 | newenv = bmake_realloc(environ, 2 * sizeof(char *)); |
810 | } else { | | 810 | } else { |
811 | if (savedEnv != NULL) { | | 811 | if (savedEnv != NULL) { |
812 | free(savedEnv); | | 812 | free(savedEnv); |
813 | savedEnv = NULL; | | 813 | savedEnv = NULL; |
814 | } | | 814 | } |
815 | newenv = bmake_malloc(2 * sizeof(char *)); | | 815 | newenv = bmake_malloc(2 * sizeof(char *)); |
816 | } | | 816 | } |
817 | | | 817 | |
818 | /* Note: we cannot safely free() the original environ. */ | | 818 | /* Note: we cannot safely free() the original environ. */ |
819 | environ = savedEnv = newenv; | | 819 | environ = savedEnv = newenv; |
820 | newenv[0] = NULL; | | 820 | newenv[0] = NULL; |
821 | newenv[1] = NULL; | | 821 | newenv[1] = NULL; |
822 | if (cp != NULL && *cp != '\0') | | 822 | if (cp != NULL && *cp != '\0') |
823 | setenv(MAKE_LEVEL_ENV, cp, 1); | | 823 | setenv(MAKE_LEVEL_ENV, cp, 1); |
824 | } | | 824 | } |
825 | | | 825 | |
826 | static void | | 826 | static void |
827 | GetVarnamesToUnexport(Boolean isEnv, const char *arg, | | 827 | GetVarnamesToUnexport(Boolean isEnv, const char *arg, |
828 | FStr *out_varnames, UnexportWhat *out_what) | | 828 | FStr *out_varnames, UnexportWhat *out_what) |
829 | { | | 829 | { |
830 | UnexportWhat what; | | 830 | UnexportWhat what; |
831 | FStr varnames = FStr_InitRefer(""); | | 831 | FStr varnames = FStr_InitRefer(""); |
832 | | | 832 | |
833 | if (isEnv) { | | 833 | if (isEnv) { |
834 | if (arg[0] != '\0') { | | 834 | if (arg[0] != '\0') { |
835 | Parse_Error(PARSE_FATAL, | | 835 | Parse_Error(PARSE_FATAL, |
836 | "The directive .unexport-env does not take " | | 836 | "The directive .unexport-env does not take " |
837 | "arguments"); | | 837 | "arguments"); |
838 | /* continue anyway */ | | 838 | /* continue anyway */ |
839 | } | | 839 | } |
840 | what = UNEXPORT_ENV; | | 840 | what = UNEXPORT_ENV; |
841 | | | 841 | |
842 | } else { | | 842 | } else { |
843 | what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL; | | 843 | what = arg[0] != '\0' ? UNEXPORT_NAMED : UNEXPORT_ALL; |
844 | if (what == UNEXPORT_NAMED) | | 844 | if (what == UNEXPORT_NAMED) |
845 | varnames = FStr_InitRefer(arg); | | 845 | varnames = FStr_InitRefer(arg); |
846 | } | | 846 | } |
847 | | | 847 | |
848 | if (what != UNEXPORT_NAMED) { | | 848 | if (what != UNEXPORT_NAMED) { |
849 | char *expanded; | | 849 | char *expanded; |
850 | /* Using .MAKE.EXPORTED */ | | 850 | /* Using .MAKE.EXPORTED */ |
851 | (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL, | | 851 | (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", SCOPE_GLOBAL, |
852 | VARE_WANTRES, &expanded); | | 852 | VARE_WANTRES, &expanded); |
853 | /* TODO: handle errors */ | | 853 | /* TODO: handle errors */ |
854 | varnames = FStr_InitOwn(expanded); | | 854 | varnames = FStr_InitOwn(expanded); |
855 | } | | 855 | } |
856 | | | 856 | |
857 | *out_varnames = varnames; | | 857 | *out_varnames = varnames; |
858 | *out_what = what; | | 858 | *out_what = what; |
859 | } | | 859 | } |
860 | | | 860 | |
861 | static void | | 861 | static void |
862 | UnexportVar(const char *varname, UnexportWhat what) | | 862 | UnexportVar(const char *varname, UnexportWhat what) |
863 | { | | 863 | { |
864 | Var *v = VarFind(varname, SCOPE_GLOBAL, FALSE); | | 864 | Var *v = VarFind(varname, SCOPE_GLOBAL, FALSE); |
865 | if (v == NULL) { | | 865 | if (v == NULL) { |
866 | DEBUG1(VAR, "Not unexporting \"%s\" (not found)\n", varname); | | 866 | DEBUG1(VAR, "Not unexporting \"%s\" (not found)\n", varname); |
867 | return; | | 867 | return; |
868 | } | | 868 | } |
869 | | | 869 | |
870 | DEBUG1(VAR, "Unexporting \"%s\"\n", varname); | | 870 | DEBUG1(VAR, "Unexporting \"%s\"\n", varname); |
871 | if (what != UNEXPORT_ENV && | | 871 | if (what != UNEXPORT_ENV && |
872 | (v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT)) | | 872 | (v->flags & VFL_EXPORTED) && !(v->flags & VFL_REEXPORT)) |
873 | unsetenv(v->name.str); | | 873 | unsetenv(v->name.str); |
874 | v->flags &= ~(unsigned)(VFL_EXPORTED | VFL_REEXPORT); | | 874 | v->flags &= ~(unsigned)(VFL_EXPORTED | VFL_REEXPORT); |
875 | | | 875 | |
876 | if (what == UNEXPORT_NAMED) { | | 876 | if (what == UNEXPORT_NAMED) { |
877 | /* Remove the variable names from .MAKE.EXPORTED. */ | | 877 | /* Remove the variable names from .MAKE.EXPORTED. */ |
878 | /* XXX: v->name is injected without escaping it */ | | 878 | /* XXX: v->name is injected without escaping it */ |
879 | char *expr = str_concat3("${" MAKE_EXPORTED ":N", | | 879 | char *expr = str_concat3("${" MAKE_EXPORTED ":N", |
880 | v->name.str, "}"); | | 880 | v->name.str, "}"); |
881 | char *cp; | | 881 | char *cp; |
882 | (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &cp); | | 882 | (void)Var_Subst(expr, SCOPE_GLOBAL, VARE_WANTRES, &cp); |
883 | /* TODO: handle errors */ | | 883 | /* TODO: handle errors */ |
884 | Global_Set(MAKE_EXPORTED, cp); | | 884 | Global_Set(MAKE_EXPORTED, cp); |
885 | free(cp); | | 885 | free(cp); |
886 | free(expr); | | 886 | free(expr); |
887 | } | | 887 | } |
888 | } | | 888 | } |
889 | | | 889 | |
890 | static void | | 890 | static void |
891 | UnexportVars(FStr *varnames, UnexportWhat what) | | 891 | UnexportVars(FStr *varnames, UnexportWhat what) |
892 | { | | 892 | { |
893 | size_t i; | | 893 | size_t i; |
894 | Words words; | | 894 | Words words; |
895 | | | 895 | |
896 | if (what == UNEXPORT_ENV) | | 896 | if (what == UNEXPORT_ENV) |
897 | ClearEnv(); | | 897 | ClearEnv(); |
898 | | | 898 | |
899 | words = Str_Words(varnames->str, FALSE); | | 899 | words = Str_Words(varnames->str, FALSE); |
900 | for (i = 0; i < words.len; i++) { | | 900 | for (i = 0; i < words.len; i++) { |
901 | const char *varname = words.words[i]; | | 901 | const char *varname = words.words[i]; |
902 | UnexportVar(varname, what); | | 902 | UnexportVar(varname, what); |
903 | } | | 903 | } |
904 | Words_Free(words); | | 904 | Words_Free(words); |
905 | | | 905 | |
906 | if (what != UNEXPORT_NAMED) | | 906 | if (what != UNEXPORT_NAMED) |
907 | Global_Delete(MAKE_EXPORTED); | | 907 | Global_Delete(MAKE_EXPORTED); |
908 | } | | 908 | } |
909 | | | 909 | |
910 | /* | | 910 | /* |
911 | * This is called when .unexport[-env] is seen. | | 911 | * This is called when .unexport[-env] is seen. |
912 | * | | 912 | * |
913 | * str must have the form "unexport[-env] varname...". | | 913 | * str must have the form "unexport[-env] varname...". |
914 | */ | | 914 | */ |
915 | void | | 915 | void |
916 | Var_UnExport(Boolean isEnv, const char *arg) | | 916 | Var_UnExport(Boolean isEnv, const char *arg) |
917 | { | | 917 | { |
918 | UnexportWhat what; | | 918 | UnexportWhat what; |
919 | FStr varnames; | | 919 | FStr varnames; |
920 | | | 920 | |
921 | GetVarnamesToUnexport(isEnv, arg, &varnames, &what); | | 921 | GetVarnamesToUnexport(isEnv, arg, &varnames, &what); |
922 | UnexportVars(&varnames, what); | | 922 | UnexportVars(&varnames, what); |
923 | FStr_Done(&varnames); | | 923 | FStr_Done(&varnames); |
924 | } | | 924 | } |
925 | | | 925 | |
926 | /* | | 926 | /* |
927 | * When there is a variable of the same name in the command line scope, the | | 927 | * When there is a variable of the same name in the command line scope, the |
928 | * global variable would not be visible anywhere. Therefore there is no | | 928 | * global variable would not be visible anywhere. Therefore there is no |
929 | * point in setting it at all. | | 929 | * point in setting it at all. |
930 | * | | 930 | * |
931 | * See 'scope == SCOPE_CMDLINE' in Var_SetWithFlags. | | 931 | * See 'scope == SCOPE_CMDLINE' in Var_SetWithFlags. |
932 | */ | | 932 | */ |
933 | static Boolean | | 933 | static Boolean |
934 | ExistsInCmdline(const char *name, const char *val) | | 934 | ExistsInCmdline(const char *name, const char *val) |
935 | { | | 935 | { |
936 | Var *v; | | 936 | Var *v; |
937 | | | 937 | |
938 | v = VarFind(name, SCOPE_CMDLINE, FALSE); | | 938 | v = VarFind(name, SCOPE_CMDLINE, FALSE); |
939 | if (v == NULL) | | 939 | if (v == NULL) |
940 | return FALSE; | | 940 | return FALSE; |
941 | | | 941 | |
942 | if (v->flags & VFL_FROM_CMD) { | | 942 | if (v->flags & VFL_FROM_CMD) { |
943 | DEBUG3(VAR, "%s:%s = %s ignored!\n", | | 943 | DEBUG3(VAR, "%s:%s = %s ignored!\n", |
944 | SCOPE_GLOBAL->name, name, val); | | 944 | SCOPE_GLOBAL->name, name, val); |
945 | return TRUE; | | 945 | return TRUE; |
946 | } | | 946 | } |
947 | | | 947 | |
948 | VarFreeEnv(v); | | 948 | VarFreeEnv(v); |
949 | return FALSE; | | 949 | return FALSE; |
950 | } | | 950 | } |
951 | | | 951 | |
952 | /* Set the variable to the value; the name is not expanded. */ | | 952 | /* Set the variable to the value; the name is not expanded. */ |
953 | void | | 953 | void |
954 | Var_SetWithFlags(GNode *scope, const char *name, const char *val, | | 954 | Var_SetWithFlags(GNode *scope, const char *name, const char *val, |
955 | VarSetFlags flags) | | 955 | VarSetFlags flags) |
956 | { | | 956 | { |
957 | Var *v; | | 957 | Var *v; |
958 | | | 958 | |
959 | assert(val != NULL); | | 959 | assert(val != NULL); |
960 | if (name[0] == '\0') { | | 960 | if (name[0] == '\0') { |
961 | DEBUG0(VAR, "SetVar: variable name is empty - ignored\n"); | | 961 | DEBUG0(VAR, "SetVar: variable name is empty - ignored\n"); |
962 | return; | | 962 | return; |
963 | } | | 963 | } |
964 | | | 964 | |
965 | if (scope == SCOPE_GLOBAL && ExistsInCmdline(name, val)) | | 965 | if (scope == SCOPE_GLOBAL && ExistsInCmdline(name, val)) |
966 | return; | | 966 | return; |
967 | | | 967 | |
968 | /* | | 968 | /* |
969 | * Only look for a variable in the given scope since anything set | | 969 | * Only look for a variable in the given scope since anything set |
970 | * here will override anything in a lower scope, so there's not much | | 970 | * here will override anything in a lower scope, so there's not much |
971 | * point in searching them all. | | 971 | * point in searching them all. |
972 | */ | | 972 | */ |
973 | v = VarFind(name, scope, FALSE); | | 973 | v = VarFind(name, scope, FALSE); |
974 | if (v == NULL) { | | 974 | if (v == NULL) { |
975 | if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) { | | 975 | if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT)) { |
976 | /* | | 976 | /* |
977 | * This var would normally prevent the same name being | | 977 | * This var would normally prevent the same name being |
978 | * added to SCOPE_GLOBAL, so delete it from there if | | 978 | * added to SCOPE_GLOBAL, so delete it from there if |
979 | * needed. Otherwise -V name may show the wrong value. | | 979 | * needed. Otherwise -V name may show the wrong value. |
980 | * | | 980 | * |
981 | * See ExistsInCmdline. | | 981 | * See ExistsInCmdline. |
982 | */ | | 982 | */ |
983 | Var_Delete(SCOPE_GLOBAL, name); | | 983 | Var_Delete(SCOPE_GLOBAL, name); |
984 | } | | 984 | } |
985 | v = VarAdd(name, val, scope, flags); | | 985 | v = VarAdd(name, val, scope, flags); |
986 | } else { | | 986 | } else { |
987 | if ((v->flags & VFL_READONLY) && !(flags & VAR_SET_READONLY)) { | | 987 | if ((v->flags & VFL_READONLY) && !(flags & VAR_SET_READONLY)) { |
988 | DEBUG3(VAR, "%s:%s = %s ignored (read-only)\n", | | 988 | DEBUG3(VAR, "%s:%s = %s ignored (read-only)\n", |
989 | scope->name, name, val); | | 989 | scope->name, name, val); |
990 | return; | | 990 | return; |
991 | } | | 991 | } |
992 | Buf_Empty(&v->val); | | 992 | Buf_Empty(&v->val); |
993 | Buf_AddStr(&v->val, val); | | 993 | Buf_AddStr(&v->val, val); |
994 | | | 994 | |
995 | DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, val); | | 995 | DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, val); |
996 | if (v->flags & VFL_EXPORTED) | | 996 | if (v->flags & VFL_EXPORTED) |
997 | ExportVar(name, VEM_PLAIN); | | 997 | ExportVar(name, VEM_PLAIN); |
998 | } | | 998 | } |
999 | | | 999 | |
1000 | /* | | 1000 | /* |
1001 | * Any variables given on the command line are automatically exported | | 1001 | * Any variables given on the command line are automatically exported |
1002 | * to the environment (as per POSIX standard), except for internals. | | 1002 | * to the environment (as per POSIX standard), except for internals. |
1003 | */ | | 1003 | */ |
1004 | if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT) && | | 1004 | if (scope == SCOPE_CMDLINE && !(flags & VAR_SET_NO_EXPORT) && |
1005 | name[0] != '.') { | | 1005 | name[0] != '.') { |
1006 | v->flags |= VFL_FROM_CMD; | | 1006 | v->flags |= VFL_FROM_CMD; |
1007 | | | 1007 | |
1008 | /* | | 1008 | /* |
1009 | * If requested, don't export these in the environment | | 1009 | * If requested, don't export these in the environment |
1010 | * individually. We still put them in MAKEOVERRIDES so | | 1010 | * individually. We still put them in MAKEOVERRIDES so |
1011 | * that the command-line settings continue to override | | 1011 | * that the command-line settings continue to override |
1012 | * Makefile settings. | | 1012 | * Makefile settings. |
1013 | */ | | 1013 | */ |
1014 | if (!opts.varNoExportEnv) | | 1014 | if (!opts.varNoExportEnv) |
1015 | setenv(name, val, 1); | | 1015 | setenv(name, val, 1); |
1016 | /* XXX: What about .MAKE.EXPORTED? */ | | 1016 | /* XXX: What about .MAKE.EXPORTED? */ |
1017 | /* XXX: Why not just mark the variable for needing export, | | 1017 | /* XXX: Why not just mark the variable for needing export, |
1018 | * as in ExportVarPlain? */ | | 1018 | * as in ExportVarPlain? */ |
1019 | | | 1019 | |
1020 | Global_Append(MAKEOVERRIDES, name); | | 1020 | Global_Append(MAKEOVERRIDES, name); |
1021 | } | | 1021 | } |
1022 | | | 1022 | |
1023 | if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0) | | 1023 | if (name[0] == '.' && strcmp(name, MAKE_SAVE_DOLLARS) == 0) |
1024 | save_dollars = ParseBoolean(val, save_dollars); | | 1024 | save_dollars = ParseBoolean(val, save_dollars); |
1025 | | | 1025 | |
1026 | if (v != NULL) | | 1026 | if (v != NULL) |
1027 | VarFreeEnv(v); | | 1027 | VarFreeEnv(v); |
1028 | } | | 1028 | } |
1029 | | | 1029 | |
1030 | /* See Var_Set for documentation. */ | | 1030 | /* See Var_Set for documentation. */ |
1031 | void | | 1031 | void |
1032 | Var_SetExpandWithFlags(GNode *scope, const char *name, const char *val, | | 1032 | Var_SetExpandWithFlags(GNode *scope, const char *name, const char *val, |
1033 | VarSetFlags flags) | | 1033 | VarSetFlags flags) |
1034 | { | | 1034 | { |
1035 | const char *unexpanded_name = name; | | 1035 | const char *unexpanded_name = name; |
1036 | FStr varname = FStr_InitRefer(name); | | 1036 | FStr varname = FStr_InitRefer(name); |
1037 | | | 1037 | |
1038 | assert(val != NULL); | | 1038 | assert(val != NULL); |
1039 | | | 1039 | |
1040 | if (strchr(varname.str, '$') != NULL) { | | 1040 | if (strchr(varname.str, '$') != NULL) { |
1041 | char *expanded; | | 1041 | char *expanded; |
1042 | (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded); | | 1042 | (void)Var_Subst(varname.str, scope, VARE_WANTRES, &expanded); |
1043 | /* TODO: handle errors */ | | 1043 | /* TODO: handle errors */ |
1044 | varname = FStr_InitOwn(expanded); | | 1044 | varname = FStr_InitOwn(expanded); |
1045 | } | | 1045 | } |
1046 | | | 1046 | |
1047 | if (varname.str[0] == '\0') { | | 1047 | if (varname.str[0] == '\0') { |
1048 | DEBUG2(VAR, "Var_Set(\"%s\", \"%s\", ...) " | | 1048 | DEBUG2(VAR, "Var_Set(\"%s\", \"%s\", ...) " |
1049 | "name expands to empty string - ignored\n", | | 1049 | "name expands to empty string - ignored\n", |
1050 | unexpanded_name, val); | | 1050 | unexpanded_name, val); |
1051 | } else | | 1051 | } else |
1052 | Var_SetWithFlags(scope, varname.str, val, flags); | | 1052 | Var_SetWithFlags(scope, varname.str, val, flags); |
1053 | | | 1053 | |
1054 | FStr_Done(&varname); | | 1054 | FStr_Done(&varname); |
1055 | } | | 1055 | } |
1056 | | | 1056 | |
1057 | void | | 1057 | void |
1058 | Var_Set(GNode *scope, const char *name, const char *val) | | 1058 | Var_Set(GNode *scope, const char *name, const char *val) |
1059 | { | | 1059 | { |
1060 | Var_SetWithFlags(scope, name, val, VAR_SET_NONE); | | 1060 | Var_SetWithFlags(scope, name, val, VAR_SET_NONE); |
1061 | } | | 1061 | } |
1062 | | | 1062 | |
1063 | /* | | 1063 | /* |
1064 | * Set the variable name to the value val in the given scope. | | 1064 | * Set the variable name to the value val in the given scope. |
1065 | * | | 1065 | * |
1066 | * If the variable doesn't yet exist, it is created. | | 1066 | * If the variable doesn't yet exist, it is created. |
1067 | * Otherwise the new value overwrites and replaces the old value. | | 1067 | * Otherwise the new value overwrites and replaces the old value. |
1068 | * | | 1068 | * |
1069 | * Input: | | 1069 | * Input: |
1070 | * name name of the variable to set, is expanded once | | 1070 | * name name of the variable to set, is expanded once |
1071 | * val value to give to the variable | | 1071 | * val value to give to the variable |
1072 | * scope scope in which to set it | | 1072 | * scope scope in which to set it |
1073 | */ | | 1073 | */ |
1074 | void | | 1074 | void |
1075 | Var_SetExpand(GNode *scope, const char *name, const char *val) | | 1075 | Var_SetExpand(GNode *scope, const char *name, const char *val) |
1076 | { | | 1076 | { |
1077 | Var_SetExpandWithFlags(scope, name, val, VAR_SET_NONE); | | 1077 | Var_SetExpandWithFlags(scope, name, val, VAR_SET_NONE); |
1078 | } | | 1078 | } |
1079 | | | 1079 | |
1080 | void | | 1080 | void |
1081 | Global_Set(const char *name, const char *value) | | 1081 | Global_Set(const char *name, const char *value) |
1082 | { | | 1082 | { |
1083 | Var_Set(SCOPE_GLOBAL, name, value); | | 1083 | Var_Set(SCOPE_GLOBAL, name, value); |
1084 | } | | 1084 | } |
1085 | | | 1085 | |
1086 | void | | 1086 | void |
1087 | Global_SetExpand(const char *name, const char *value) | | 1087 | Global_SetExpand(const char *name, const char *value) |
1088 | { | | 1088 | { |
1089 | Var_SetExpand(SCOPE_GLOBAL, name, value); | | 1089 | Var_SetExpand(SCOPE_GLOBAL, name, value); |
1090 | } | | 1090 | } |
1091 | | | 1091 | |
1092 | void | | 1092 | void |
1093 | Global_Delete(const char *name) | | 1093 | Global_Delete(const char *name) |
1094 | { | | 1094 | { |
1095 | Var_Delete(SCOPE_GLOBAL, name); | | 1095 | Var_Delete(SCOPE_GLOBAL, name); |
1096 | } | | 1096 | } |
1097 | | | 1097 | |
1098 | /* | | 1098 | /* |
1099 | * Append the value to the named variable. | | 1099 | * Append the value to the named variable. |
1100 | * | | 1100 | * |
1101 | * If the variable doesn't exist, it is created. Otherwise a single space | | 1101 | * If the variable doesn't exist, it is created. Otherwise a single space |
1102 | * and the given value are appended. | | 1102 | * and the given value are appended. |
1103 | */ | | 1103 | */ |
1104 | void | | 1104 | void |
1105 | Var_Append(GNode *scope, const char *name, const char *val) | | 1105 | Var_Append(GNode *scope, const char *name, const char *val) |
1106 | { | | 1106 | { |
1107 | Var *v; | | 1107 | Var *v; |
1108 | | | 1108 | |
1109 | v = VarFind(name, scope, scope == SCOPE_GLOBAL); | | 1109 | v = VarFind(name, scope, scope == SCOPE_GLOBAL); |
1110 | | | 1110 | |
1111 | if (v == NULL) { | | 1111 | if (v == NULL) { |
1112 | Var_SetWithFlags(scope, name, val, VAR_SET_NONE); | | 1112 | Var_SetWithFlags(scope, name, val, VAR_SET_NONE); |
1113 | } else if (v->flags & VFL_READONLY) { | | 1113 | } else if (v->flags & VFL_READONLY) { |
1114 | DEBUG1(VAR, "Ignoring append to %s since it is read-only\n", | | 1114 | DEBUG1(VAR, "Ignoring append to %s since it is read-only\n", |
1115 | name); | | 1115 | name); |
1116 | } else if (scope == SCOPE_CMDLINE || !(v->flags & VFL_FROM_CMD)) { | | 1116 | } else if (scope == SCOPE_CMDLINE || !(v->flags & VFL_FROM_CMD)) { |
1117 | Buf_AddByte(&v->val, ' '); | | 1117 | Buf_AddByte(&v->val, ' '); |
1118 | Buf_AddStr(&v->val, val); | | 1118 | Buf_AddStr(&v->val, val); |
1119 | | | 1119 | |
1120 | DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, v->val.data); | | 1120 | DEBUG3(VAR, "%s:%s = %s\n", scope->name, name, v->val.data); |
1121 | | | 1121 | |
1122 | if (v->flags & VFL_FROM_ENV) { | | 1122 | if (v->flags & VFL_FROM_ENV) { |
1123 | /* | | 1123 | /* |
1124 | * If the original variable came from the environment, | | 1124 | * If the original variable came from the environment, |
1125 | * we have to install it in the global scope (we | | 1125 | * we have to install it in the global scope (we |
1126 | * could place it in the environment, but then we | | 1126 | * could place it in the environment, but then we |
1127 | * should provide a way to export other variables...) | | 1127 | * should provide a way to export other variables...) |
1128 | */ | | 1128 | */ |
1129 | v->flags &= ~(unsigned)VFL_FROM_ENV; | | 1129 | v->flags &= ~(unsigned)VFL_FROM_ENV; |
1130 | /* | | 1130 | /* |
1131 | * This is the only place where a variable is | | 1131 | * This is the only place where a variable is |
1132 | * created whose v->name is not the same as | | 1132 | * created whose v->name is not the same as |
1133 | * scope->vars->key. | | 1133 | * scope->vars->key. |
1134 | */ | | 1134 | */ |
1135 | HashTable_Set(&scope->vars, name, v); | | 1135 | HashTable_Set(&scope->vars, name, v); |
1136 | } | | 1136 | } |
1137 | } | | 1137 | } |
1138 | } | | 1138 | } |
1139 | | | 1139 | |
1140 | /* | | 1140 | /* |
1141 | * The variable of the given name has the given value appended to it in the | | 1141 | * The variable of the given name has the given value appended to it in the |
1142 | * given scope. | | 1142 | * given scope. |
| @@ -2434,2001 +2434,2002 @@ ApplyModifier_Loop(const char **pp, Appl | | | @@ -2434,2001 +2434,2002 @@ ApplyModifier_Loop(const char **pp, Appl |
2434 | st->sep = ' '; /* XXX: should be st->sep for consistency */ | | 2434 | st->sep = ' '; /* XXX: should be st->sep for consistency */ |
2435 | ModifyWords(st, ModifyWord_Loop, &args, st->oneBigWord); | | 2435 | ModifyWords(st, ModifyWord_Loop, &args, st->oneBigWord); |
2436 | st->sep = prev_sep; | | 2436 | st->sep = prev_sep; |
2437 | /* XXX: Consider restoring the previous variable instead of deleting. */ | | 2437 | /* XXX: Consider restoring the previous variable instead of deleting. */ |
2438 | /* | | 2438 | /* |
2439 | * XXX: The variable name should not be expanded here, see | | 2439 | * XXX: The variable name should not be expanded here, see |
2440 | * ModifyWord_Loop. | | 2440 | * ModifyWord_Loop. |
2441 | */ | | 2441 | */ |
2442 | Var_DeleteExpand(expr->scope, args.tvar); | | 2442 | Var_DeleteExpand(expr->scope, args.tvar); |
2443 | free(args.tvar); | | 2443 | free(args.tvar); |
2444 | free(args.str); | | 2444 | free(args.str); |
2445 | return AMR_OK; | | 2445 | return AMR_OK; |
2446 | } | | 2446 | } |
2447 | | | 2447 | |
2448 | /* :Ddefined or :Uundefined */ | | 2448 | /* :Ddefined or :Uundefined */ |
2449 | static ApplyModifierResult | | 2449 | static ApplyModifierResult |
2450 | ApplyModifier_Defined(const char **pp, ApplyModifiersState *st) | | 2450 | ApplyModifier_Defined(const char **pp, ApplyModifiersState *st) |
2451 | { | | 2451 | { |
2452 | Expr *expr = st->expr; | | 2452 | Expr *expr = st->expr; |
2453 | Buffer buf; | | 2453 | Buffer buf; |
2454 | const char *p; | | 2454 | const char *p; |
2455 | | | 2455 | |
2456 | VarEvalFlags eflags = VARE_NONE; | | 2456 | VarEvalFlags eflags = VARE_NONE; |
2457 | if (expr->eflags & VARE_WANTRES) | | 2457 | if (expr->eflags & VARE_WANTRES) |
2458 | if ((**pp == 'D') == (expr->defined == DEF_REGULAR)) | | 2458 | if ((**pp == 'D') == (expr->defined == DEF_REGULAR)) |
2459 | eflags = expr->eflags; | | 2459 | eflags = expr->eflags; |
2460 | | | 2460 | |
2461 | Buf_Init(&buf); | | 2461 | Buf_Init(&buf); |
2462 | p = *pp + 1; | | 2462 | p = *pp + 1; |
2463 | while (!IsDelimiter(*p, st) && *p != '\0') { | | 2463 | while (!IsDelimiter(*p, st) && *p != '\0') { |
2464 | | | 2464 | |
2465 | /* XXX: This code is similar to the one in Var_Parse. | | 2465 | /* XXX: This code is similar to the one in Var_Parse. |
2466 | * See if the code can be merged. | | 2466 | * See if the code can be merged. |
2467 | * See also ApplyModifier_Match and ParseModifierPart. */ | | 2467 | * See also ApplyModifier_Match and ParseModifierPart. */ |
2468 | | | 2468 | |
2469 | /* Escaped delimiter or other special character */ | | 2469 | /* Escaped delimiter or other special character */ |
2470 | /* See Buf_AddEscaped in for.c. */ | | 2470 | /* See Buf_AddEscaped in for.c. */ |
2471 | if (*p == '\\') { | | 2471 | if (*p == '\\') { |
2472 | char c = p[1]; | | 2472 | char c = p[1]; |
2473 | if (IsDelimiter(c, st) || c == '$' || c == '\\') { | | 2473 | if (IsDelimiter(c, st) || c == '$' || c == '\\') { |
2474 | Buf_AddByte(&buf, c); | | 2474 | Buf_AddByte(&buf, c); |
2475 | p += 2; | | 2475 | p += 2; |
2476 | continue; | | 2476 | continue; |
2477 | } | | 2477 | } |
2478 | } | | 2478 | } |
2479 | | | 2479 | |
2480 | /* Nested variable expression */ | | 2480 | /* Nested variable expression */ |
2481 | if (*p == '$') { | | 2481 | if (*p == '$') { |
2482 | FStr nested_val; | | 2482 | FStr nested_val; |
2483 | | | 2483 | |
2484 | (void)Var_Parse(&p, expr->scope, eflags, &nested_val); | | 2484 | (void)Var_Parse(&p, expr->scope, eflags, &nested_val); |
2485 | /* TODO: handle errors */ | | 2485 | /* TODO: handle errors */ |
2486 | Buf_AddStr(&buf, nested_val.str); | | 2486 | Buf_AddStr(&buf, nested_val.str); |
2487 | FStr_Done(&nested_val); | | 2487 | FStr_Done(&nested_val); |
2488 | continue; | | 2488 | continue; |
2489 | } | | 2489 | } |
2490 | | | 2490 | |
2491 | /* Ordinary text */ | | 2491 | /* Ordinary text */ |
2492 | Buf_AddByte(&buf, *p); | | 2492 | Buf_AddByte(&buf, *p); |
2493 | p++; | | 2493 | p++; |
2494 | } | | 2494 | } |
2495 | *pp = p; | | 2495 | *pp = p; |
2496 | | | 2496 | |
2497 | Expr_Define(expr); | | 2497 | Expr_Define(expr); |
2498 | | | 2498 | |
2499 | if (eflags & VARE_WANTRES) | | 2499 | if (eflags & VARE_WANTRES) |
2500 | Expr_SetValueOwn(expr, Buf_DoneData(&buf)); | | 2500 | Expr_SetValueOwn(expr, Buf_DoneData(&buf)); |
2501 | else | | 2501 | else |
2502 | Buf_Done(&buf); | | 2502 | Buf_Done(&buf); |
2503 | | | 2503 | |
2504 | return AMR_OK; | | 2504 | return AMR_OK; |
2505 | } | | 2505 | } |
2506 | | | 2506 | |
2507 | /* :L */ | | 2507 | /* :L */ |
2508 | static ApplyModifierResult | | 2508 | static ApplyModifierResult |
2509 | ApplyModifier_Literal(const char **pp, ApplyModifiersState *st) | | 2509 | ApplyModifier_Literal(const char **pp, ApplyModifiersState *st) |
2510 | { | | 2510 | { |
2511 | Expr *expr = st->expr; | | 2511 | Expr *expr = st->expr; |
2512 | | | 2512 | |
2513 | (*pp)++; | | 2513 | (*pp)++; |
2514 | | | 2514 | |
2515 | Expr_Define(expr); | | 2515 | Expr_Define(expr); |
2516 | Expr_SetValueOwn(expr, bmake_strdup(expr->var->name.str)); | | 2516 | Expr_SetValueOwn(expr, bmake_strdup(expr->var->name.str)); |
2517 | | | 2517 | |
2518 | return AMR_OK; | | 2518 | return AMR_OK; |
2519 | } | | 2519 | } |
2520 | | | 2520 | |
2521 | static Boolean | | 2521 | static Boolean |
2522 | TryParseTime(const char **pp, time_t *out_time) | | 2522 | TryParseTime(const char **pp, time_t *out_time) |
2523 | { | | 2523 | { |
2524 | char *end; | | 2524 | char *end; |
2525 | unsigned long n; | | 2525 | unsigned long n; |
2526 | | | 2526 | |
2527 | if (!ch_isdigit(**pp)) | | 2527 | if (!ch_isdigit(**pp)) |
2528 | return FALSE; | | 2528 | return FALSE; |
2529 | | | 2529 | |
2530 | errno = 0; | | 2530 | errno = 0; |
2531 | n = strtoul(*pp, &end, 10); | | 2531 | n = strtoul(*pp, &end, 10); |
2532 | if (n == ULONG_MAX && errno == ERANGE) | | 2532 | if (n == ULONG_MAX && errno == ERANGE) |
2533 | return FALSE; | | 2533 | return FALSE; |
2534 | | | 2534 | |
2535 | *pp = end; | | 2535 | *pp = end; |
2536 | *out_time = (time_t)n; /* ignore possible truncation for now */ | | 2536 | *out_time = (time_t)n; /* ignore possible truncation for now */ |
2537 | return TRUE; | | 2537 | return TRUE; |
2538 | } | | 2538 | } |
2539 | | | 2539 | |
2540 | /* :gmtime */ | | 2540 | /* :gmtime */ |
2541 | static ApplyModifierResult | | 2541 | static ApplyModifierResult |
2542 | ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st) | | 2542 | ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st) |
2543 | { | | 2543 | { |
2544 | time_t utc; | | 2544 | time_t utc; |
2545 | | | 2545 | |
2546 | const char *mod = *pp; | | 2546 | const char *mod = *pp; |
2547 | if (!ModMatchEq(mod, "gmtime", st)) | | 2547 | if (!ModMatchEq(mod, "gmtime", st)) |
2548 | return AMR_UNKNOWN; | | 2548 | return AMR_UNKNOWN; |
2549 | | | 2549 | |
2550 | if (mod[6] == '=') { | | 2550 | if (mod[6] == '=') { |
2551 | const char *p = mod + 7; | | 2551 | const char *p = mod + 7; |
2552 | if (!TryParseTime(&p, &utc)) { | | 2552 | if (!TryParseTime(&p, &utc)) { |
2553 | Parse_Error(PARSE_FATAL, | | 2553 | Parse_Error(PARSE_FATAL, |
2554 | "Invalid time value: %s", mod + 7); | | 2554 | "Invalid time value: %s", mod + 7); |
2555 | return AMR_CLEANUP; | | 2555 | return AMR_CLEANUP; |
2556 | } | | 2556 | } |
2557 | *pp = p; | | 2557 | *pp = p; |
2558 | } else { | | 2558 | } else { |
2559 | utc = 0; | | 2559 | utc = 0; |
2560 | *pp = mod + 6; | | 2560 | *pp = mod + 6; |
2561 | } | | 2561 | } |
2562 | | | 2562 | |
2563 | Expr_SetValueOwn(st->expr, | | 2563 | Expr_SetValueOwn(st->expr, |
2564 | VarStrftime(st->expr->value.str, TRUE, utc)); | | 2564 | VarStrftime(st->expr->value.str, TRUE, utc)); |
2565 | | | 2565 | |
2566 | return AMR_OK; | | 2566 | return AMR_OK; |
2567 | } | | 2567 | } |
2568 | | | 2568 | |
2569 | /* :localtime */ | | 2569 | /* :localtime */ |
2570 | static ApplyModifierResult | | 2570 | static ApplyModifierResult |
2571 | ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st) | | 2571 | ApplyModifier_Localtime(const char **pp, ApplyModifiersState *st) |
2572 | { | | 2572 | { |
2573 | time_t utc; | | 2573 | time_t utc; |
2574 | | | 2574 | |
2575 | const char *mod = *pp; | | 2575 | const char *mod = *pp; |
2576 | if (!ModMatchEq(mod, "localtime", st)) | | 2576 | if (!ModMatchEq(mod, "localtime", st)) |
2577 | return AMR_UNKNOWN; | | 2577 | return AMR_UNKNOWN; |
2578 | | | 2578 | |
2579 | if (mod[9] == '=') { | | 2579 | if (mod[9] == '=') { |
2580 | const char *p = mod + 10; | | 2580 | const char *p = mod + 10; |
2581 | if (!TryParseTime(&p, &utc)) { | | 2581 | if (!TryParseTime(&p, &utc)) { |
2582 | Parse_Error(PARSE_FATAL, | | 2582 | Parse_Error(PARSE_FATAL, |
2583 | "Invalid time value: %s", mod + 10); | | 2583 | "Invalid time value: %s", mod + 10); |
2584 | return AMR_CLEANUP; | | 2584 | return AMR_CLEANUP; |
2585 | } | | 2585 | } |
2586 | *pp = p; | | 2586 | *pp = p; |
2587 | } else { | | 2587 | } else { |
2588 | utc = 0; | | 2588 | utc = 0; |
2589 | *pp = mod + 9; | | 2589 | *pp = mod + 9; |
2590 | } | | 2590 | } |
2591 | | | 2591 | |
2592 | Expr_SetValueOwn(st->expr, | | 2592 | Expr_SetValueOwn(st->expr, |
2593 | VarStrftime(st->expr->value.str, FALSE, utc)); | | 2593 | VarStrftime(st->expr->value.str, FALSE, utc)); |
2594 | | | 2594 | |
2595 | return AMR_OK; | | 2595 | return AMR_OK; |
2596 | } | | 2596 | } |
2597 | | | 2597 | |
2598 | /* :hash */ | | 2598 | /* :hash */ |
2599 | static ApplyModifierResult | | 2599 | static ApplyModifierResult |
2600 | ApplyModifier_Hash(const char **pp, ApplyModifiersState *st) | | 2600 | ApplyModifier_Hash(const char **pp, ApplyModifiersState *st) |
2601 | { | | 2601 | { |
2602 | if (!ModMatch(*pp, "hash", st)) | | 2602 | if (!ModMatch(*pp, "hash", st)) |
2603 | return AMR_UNKNOWN; | | 2603 | return AMR_UNKNOWN; |
2604 | *pp += 4; | | 2604 | *pp += 4; |
2605 | | | 2605 | |
2606 | Expr_SetValueOwn(st->expr, VarHash(st->expr->value.str)); | | 2606 | Expr_SetValueOwn(st->expr, VarHash(st->expr->value.str)); |
2607 | | | 2607 | |
2608 | return AMR_OK; | | 2608 | return AMR_OK; |
2609 | } | | 2609 | } |
2610 | | | 2610 | |
2611 | /* :P */ | | 2611 | /* :P */ |
2612 | static ApplyModifierResult | | 2612 | static ApplyModifierResult |
2613 | ApplyModifier_Path(const char **pp, ApplyModifiersState *st) | | 2613 | ApplyModifier_Path(const char **pp, ApplyModifiersState *st) |
2614 | { | | 2614 | { |
2615 | Expr *expr = st->expr; | | 2615 | Expr *expr = st->expr; |
2616 | GNode *gn; | | 2616 | GNode *gn; |
2617 | char *path; | | 2617 | char *path; |
2618 | | | 2618 | |
2619 | (*pp)++; | | 2619 | (*pp)++; |
2620 | | | 2620 | |
2621 | Expr_Define(expr); | | 2621 | Expr_Define(expr); |
2622 | | | 2622 | |
2623 | gn = Targ_FindNode(expr->var->name.str); | | 2623 | gn = Targ_FindNode(expr->var->name.str); |
2624 | if (gn == NULL || gn->type & OP_NOPATH) { | | 2624 | if (gn == NULL || gn->type & OP_NOPATH) { |
2625 | path = NULL; | | 2625 | path = NULL; |
2626 | } else if (gn->path != NULL) { | | 2626 | } else if (gn->path != NULL) { |
2627 | path = bmake_strdup(gn->path); | | 2627 | path = bmake_strdup(gn->path); |
2628 | } else { | | 2628 | } else { |
2629 | SearchPath *searchPath = Suff_FindPath(gn); | | 2629 | SearchPath *searchPath = Suff_FindPath(gn); |
2630 | path = Dir_FindFile(expr->var->name.str, searchPath); | | 2630 | path = Dir_FindFile(expr->var->name.str, searchPath); |
2631 | } | | 2631 | } |
2632 | if (path == NULL) | | 2632 | if (path == NULL) |
2633 | path = bmake_strdup(expr->var->name.str); | | 2633 | path = bmake_strdup(expr->var->name.str); |
2634 | Expr_SetValueOwn(expr, path); | | 2634 | Expr_SetValueOwn(expr, path); |
2635 | | | 2635 | |
2636 | return AMR_OK; | | 2636 | return AMR_OK; |
2637 | } | | 2637 | } |
2638 | | | 2638 | |
2639 | /* :!cmd! */ | | 2639 | /* :!cmd! */ |
2640 | static ApplyModifierResult | | 2640 | static ApplyModifierResult |
2641 | ApplyModifier_ShellCommand(const char **pp, ApplyModifiersState *st) | | 2641 | ApplyModifier_ShellCommand(const char **pp, ApplyModifiersState *st) |
2642 | { | | 2642 | { |
2643 | Expr *expr = st->expr; | | 2643 | Expr *expr = st->expr; |
2644 | char *cmd; | | 2644 | char *cmd; |
2645 | const char *errfmt; | | 2645 | const char *errfmt; |
2646 | VarParseResult res; | | 2646 | VarParseResult res; |
2647 | | | 2647 | |
2648 | (*pp)++; | | 2648 | (*pp)++; |
2649 | res = ParseModifierPart(pp, '!', expr->eflags, st, &cmd); | | 2649 | res = ParseModifierPart(pp, '!', expr->eflags, st, &cmd); |
2650 | if (res != VPR_OK) | | 2650 | if (res != VPR_OK) |
2651 | return AMR_CLEANUP; | | 2651 | return AMR_CLEANUP; |
2652 | | | 2652 | |
2653 | errfmt = NULL; | | 2653 | errfmt = NULL; |
2654 | if (expr->eflags & VARE_WANTRES) | | 2654 | if (expr->eflags & VARE_WANTRES) |
2655 | Expr_SetValueOwn(expr, Cmd_Exec(cmd, &errfmt)); | | 2655 | Expr_SetValueOwn(expr, Cmd_Exec(cmd, &errfmt)); |
2656 | else | | 2656 | else |
2657 | Expr_SetValueRefer(expr, ""); | | 2657 | Expr_SetValueRefer(expr, ""); |
2658 | if (errfmt != NULL) | | 2658 | if (errfmt != NULL) |
2659 | Error(errfmt, cmd); /* XXX: why still return AMR_OK? */ | | 2659 | Error(errfmt, cmd); /* XXX: why still return AMR_OK? */ |
2660 | free(cmd); | | 2660 | free(cmd); |
2661 | Expr_Define(expr); | | 2661 | Expr_Define(expr); |
2662 | | | 2662 | |
2663 | return AMR_OK; | | 2663 | return AMR_OK; |
2664 | } | | 2664 | } |
2665 | | | 2665 | |
2666 | /* | | 2666 | /* |
2667 | * The :range modifier generates an integer sequence as long as the words. | | 2667 | * The :range modifier generates an integer sequence as long as the words. |
2668 | * The :range=7 modifier generates an integer sequence from 1 to 7. | | 2668 | * The :range=7 modifier generates an integer sequence from 1 to 7. |
2669 | */ | | 2669 | */ |
2670 | static ApplyModifierResult | | 2670 | static ApplyModifierResult |
2671 | ApplyModifier_Range(const char **pp, ApplyModifiersState *st) | | 2671 | ApplyModifier_Range(const char **pp, ApplyModifiersState *st) |
2672 | { | | 2672 | { |
2673 | size_t n; | | 2673 | size_t n; |
2674 | Buffer buf; | | 2674 | Buffer buf; |
2675 | size_t i; | | 2675 | size_t i; |
2676 | | | 2676 | |
2677 | const char *mod = *pp; | | 2677 | const char *mod = *pp; |
2678 | if (!ModMatchEq(mod, "range", st)) | | 2678 | if (!ModMatchEq(mod, "range", st)) |
2679 | return AMR_UNKNOWN; | | 2679 | return AMR_UNKNOWN; |
2680 | | | 2680 | |
2681 | if (mod[5] == '=') { | | 2681 | if (mod[5] == '=') { |
2682 | const char *p = mod + 6; | | 2682 | const char *p = mod + 6; |
2683 | if (!TryParseSize(&p, &n)) { | | 2683 | if (!TryParseSize(&p, &n)) { |
2684 | Parse_Error(PARSE_FATAL, | | 2684 | Parse_Error(PARSE_FATAL, |
2685 | "Invalid number \"%s\" for ':range' modifier", | | 2685 | "Invalid number \"%s\" for ':range' modifier", |
2686 | mod + 6); | | 2686 | mod + 6); |
2687 | return AMR_CLEANUP; | | 2687 | return AMR_CLEANUP; |
2688 | } | | 2688 | } |
2689 | *pp = p; | | 2689 | *pp = p; |
2690 | } else { | | 2690 | } else { |
2691 | n = 0; | | 2691 | n = 0; |
2692 | *pp = mod + 5; | | 2692 | *pp = mod + 5; |
2693 | } | | 2693 | } |
2694 | | | 2694 | |
2695 | if (n == 0) { | | 2695 | if (n == 0) { |
2696 | Words words = Str_Words(st->expr->value.str, FALSE); | | 2696 | Words words = Str_Words(st->expr->value.str, FALSE); |
2697 | n = words.len; | | 2697 | n = words.len; |
2698 | Words_Free(words); | | 2698 | Words_Free(words); |
2699 | } | | 2699 | } |
2700 | | | 2700 | |
2701 | Buf_Init(&buf); | | 2701 | Buf_Init(&buf); |
2702 | | | 2702 | |
2703 | for (i = 0; i < n; i++) { | | 2703 | for (i = 0; i < n; i++) { |
2704 | if (i != 0) { | | 2704 | if (i != 0) { |
2705 | /* XXX: Use st->sep instead of ' ', for consistency. */ | | 2705 | /* XXX: Use st->sep instead of ' ', for consistency. */ |
2706 | Buf_AddByte(&buf, ' '); | | 2706 | Buf_AddByte(&buf, ' '); |
2707 | } | | 2707 | } |
2708 | Buf_AddInt(&buf, 1 + (int)i); | | 2708 | Buf_AddInt(&buf, 1 + (int)i); |
2709 | } | | 2709 | } |
2710 | | | 2710 | |
2711 | Expr_SetValueOwn(st->expr, Buf_DoneData(&buf)); | | 2711 | Expr_SetValueOwn(st->expr, Buf_DoneData(&buf)); |
2712 | return AMR_OK; | | 2712 | return AMR_OK; |
2713 | } | | 2713 | } |
2714 | | | 2714 | |
2715 | /* Parse a ':M' or ':N' modifier. */ | | 2715 | /* Parse a ':M' or ':N' modifier. */ |
2716 | static void | | 2716 | static void |
2717 | ParseModifier_Match(const char **pp, const ApplyModifiersState *st, | | 2717 | ParseModifier_Match(const char **pp, const ApplyModifiersState *st, |
2718 | char **out_pattern) | | 2718 | char **out_pattern) |
2719 | { | | 2719 | { |
2720 | const char *mod = *pp; | | 2720 | const char *mod = *pp; |
2721 | Expr *expr = st->expr; | | 2721 | Expr *expr = st->expr; |
2722 | Boolean copy = FALSE; /* pattern should be, or has been, copied */ | | 2722 | Boolean copy = FALSE; /* pattern should be, or has been, copied */ |
2723 | Boolean needSubst = FALSE; | | 2723 | Boolean needSubst = FALSE; |
2724 | const char *endpat; | | 2724 | const char *endpat; |
2725 | char *pattern; | | 2725 | char *pattern; |
2726 | | | 2726 | |
2727 | /* | | 2727 | /* |
2728 | * In the loop below, ignore ':' unless we are at (or back to) the | | 2728 | * In the loop below, ignore ':' unless we are at (or back to) the |
2729 | * original brace level. | | 2729 | * original brace level. |
2730 | * XXX: This will likely not work right if $() and ${} are intermixed. | | 2730 | * XXX: This will likely not work right if $() and ${} are intermixed. |
2731 | */ | | 2731 | */ |
2732 | /* | | 2732 | /* |
2733 | * XXX: This code is similar to the one in Var_Parse. | | 2733 | * XXX: This code is similar to the one in Var_Parse. |
2734 | * See if the code can be merged. | | 2734 | * See if the code can be merged. |
2735 | * See also ApplyModifier_Defined. | | 2735 | * See also ApplyModifier_Defined. |
2736 | */ | | 2736 | */ |
2737 | int nest = 0; | | 2737 | int nest = 0; |
2738 | const char *p; | | 2738 | const char *p; |
2739 | for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) { | | 2739 | for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) { |
2740 | if (*p == '\\' && | | 2740 | if (*p == '\\' && |
2741 | (IsDelimiter(p[1], st) || p[1] == st->startc)) { | | 2741 | (IsDelimiter(p[1], st) || p[1] == st->startc)) { |
2742 | if (!needSubst) | | 2742 | if (!needSubst) |
2743 | copy = TRUE; | | 2743 | copy = TRUE; |
2744 | p++; | | 2744 | p++; |
2745 | continue; | | 2745 | continue; |
2746 | } | | 2746 | } |
2747 | if (*p == '$') | | 2747 | if (*p == '$') |
2748 | needSubst = TRUE; | | 2748 | needSubst = TRUE; |
2749 | if (*p == '(' || *p == '{') | | 2749 | if (*p == '(' || *p == '{') |
2750 | nest++; | | 2750 | nest++; |
2751 | if (*p == ')' || *p == '}') { | | 2751 | if (*p == ')' || *p == '}') { |
2752 | nest--; | | 2752 | nest--; |
2753 | if (nest < 0) | | 2753 | if (nest < 0) |
2754 | break; | | 2754 | break; |
2755 | } | | 2755 | } |
2756 | } | | 2756 | } |
2757 | *pp = p; | | 2757 | *pp = p; |
2758 | endpat = p; | | 2758 | endpat = p; |
2759 | | | 2759 | |
2760 | if (copy) { | | 2760 | if (copy) { |
2761 | char *dst; | | 2761 | char *dst; |
2762 | const char *src; | | 2762 | const char *src; |
2763 | | | 2763 | |
2764 | /* Compress the \:'s out of the pattern. */ | | 2764 | /* Compress the \:'s out of the pattern. */ |
2765 | pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1); | | 2765 | pattern = bmake_malloc((size_t)(endpat - (mod + 1)) + 1); |
2766 | dst = pattern; | | 2766 | dst = pattern; |
2767 | src = mod + 1; | | 2767 | src = mod + 1; |
2768 | for (; src < endpat; src++, dst++) { | | 2768 | for (; src < endpat; src++, dst++) { |
2769 | if (src[0] == '\\' && src + 1 < endpat && | | 2769 | if (src[0] == '\\' && src + 1 < endpat && |
2770 | /* XXX: st->startc is missing here; see above */ | | 2770 | /* XXX: st->startc is missing here; see above */ |
2771 | IsDelimiter(src[1], st)) | | 2771 | IsDelimiter(src[1], st)) |
2772 | src++; | | 2772 | src++; |
2773 | *dst = *src; | | 2773 | *dst = *src; |
2774 | } | | 2774 | } |
2775 | *dst = '\0'; | | 2775 | *dst = '\0'; |
2776 | } else { | | 2776 | } else { |
2777 | pattern = bmake_strsedup(mod + 1, endpat); | | 2777 | pattern = bmake_strsedup(mod + 1, endpat); |
2778 | } | | 2778 | } |
2779 | | | 2779 | |
2780 | if (needSubst) { | | 2780 | if (needSubst) { |
2781 | char *old_pattern = pattern; | | 2781 | char *old_pattern = pattern; |
2782 | (void)Var_Subst(pattern, expr->scope, expr->eflags, &pattern); | | 2782 | (void)Var_Subst(pattern, expr->scope, expr->eflags, &pattern); |
2783 | /* TODO: handle errors */ | | 2783 | /* TODO: handle errors */ |
2784 | free(old_pattern); | | 2784 | free(old_pattern); |
2785 | } | | 2785 | } |
2786 | | | 2786 | |
2787 | DEBUG3(VAR, "Pattern[%s] for [%s] is [%s]\n", | | 2787 | DEBUG3(VAR, "Pattern[%s] for [%s] is [%s]\n", |
2788 | expr->var->name.str, expr->value.str, pattern); | | 2788 | expr->var->name.str, expr->value.str, pattern); |
2789 | | | 2789 | |
2790 | *out_pattern = pattern; | | 2790 | *out_pattern = pattern; |
2791 | } | | 2791 | } |
2792 | | | 2792 | |
2793 | /* :Mpattern or :Npattern */ | | 2793 | /* :Mpattern or :Npattern */ |
2794 | static ApplyModifierResult | | 2794 | static ApplyModifierResult |
2795 | ApplyModifier_Match(const char **pp, ApplyModifiersState *st) | | 2795 | ApplyModifier_Match(const char **pp, ApplyModifiersState *st) |
2796 | { | | 2796 | { |
2797 | const char *mod = *pp; | | 2797 | const char *mod = *pp; |
2798 | char *pattern; | | 2798 | char *pattern; |
2799 | ModifyWordProc modifyWord; | | 2799 | ModifyWordProc modifyWord; |
2800 | | | 2800 | |
2801 | ParseModifier_Match(pp, st, &pattern); | | 2801 | ParseModifier_Match(pp, st, &pattern); |
2802 | | | 2802 | |
2803 | modifyWord = mod[0] == 'M' ? ModifyWord_Match : ModifyWord_NoMatch; | | 2803 | modifyWord = mod[0] == 'M' ? ModifyWord_Match : ModifyWord_NoMatch; |
2804 | ModifyWords(st, modifyWord, pattern, st->oneBigWord); | | 2804 | ModifyWords(st, modifyWord, pattern, st->oneBigWord); |
2805 | free(pattern); | | 2805 | free(pattern); |
2806 | return AMR_OK; | | 2806 | return AMR_OK; |
2807 | } | | 2807 | } |
2808 | | | 2808 | |
2809 | static void | | 2809 | static void |
2810 | ParsePatternFlags(const char **pp, VarPatternFlags *pflags, Boolean *oneBigWord) | | 2810 | ParsePatternFlags(const char **pp, VarPatternFlags *pflags, Boolean *oneBigWord) |
2811 | { | | 2811 | { |
2812 | for (;; (*pp)++) { | | 2812 | for (;; (*pp)++) { |
2813 | if (**pp == 'g') | | 2813 | if (**pp == 'g') |
2814 | pflags->subGlobal = TRUE; | | 2814 | pflags->subGlobal = TRUE; |
2815 | else if (**pp == '1') | | 2815 | else if (**pp == '1') |
2816 | pflags->subOnce = TRUE; | | 2816 | pflags->subOnce = TRUE; |
2817 | else if (**pp == 'W') | | 2817 | else if (**pp == 'W') |
2818 | *oneBigWord = TRUE; | | 2818 | *oneBigWord = TRUE; |
2819 | else | | 2819 | else |
2820 | break; | | 2820 | break; |
2821 | } | | 2821 | } |
2822 | } | | 2822 | } |
2823 | | | 2823 | |
2824 | /* :S,from,to, */ | | 2824 | /* :S,from,to, */ |
2825 | static ApplyModifierResult | | 2825 | static ApplyModifierResult |
2826 | ApplyModifier_Subst(const char **pp, ApplyModifiersState *st) | | 2826 | ApplyModifier_Subst(const char **pp, ApplyModifiersState *st) |
2827 | { | | 2827 | { |
2828 | struct ModifyWord_SubstArgs args; | | 2828 | struct ModifyWord_SubstArgs args; |
2829 | char *lhs, *rhs; | | 2829 | char *lhs, *rhs; |
2830 | Boolean oneBigWord; | | 2830 | Boolean oneBigWord; |
2831 | VarParseResult res; | | 2831 | VarParseResult res; |
2832 | | | 2832 | |
2833 | char delim = (*pp)[1]; | | 2833 | char delim = (*pp)[1]; |
2834 | if (delim == '\0') { | | 2834 | if (delim == '\0') { |
2835 | Error("Missing delimiter for modifier ':S'"); | | 2835 | Error("Missing delimiter for modifier ':S'"); |
2836 | (*pp)++; | | 2836 | (*pp)++; |
2837 | return AMR_CLEANUP; | | 2837 | return AMR_CLEANUP; |
2838 | } | | 2838 | } |
2839 | | | 2839 | |
2840 | *pp += 2; | | 2840 | *pp += 2; |
2841 | | | 2841 | |
2842 | args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE }; | | 2842 | args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE }; |
2843 | args.matched = FALSE; | | 2843 | args.matched = FALSE; |
2844 | | | 2844 | |
2845 | if (**pp == '^') { | | 2845 | if (**pp == '^') { |
2846 | args.pflags.anchorStart = TRUE; | | 2846 | args.pflags.anchorStart = TRUE; |
2847 | (*pp)++; | | 2847 | (*pp)++; |
2848 | } | | 2848 | } |
2849 | | | 2849 | |
2850 | res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &lhs, | | 2850 | res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &lhs, |
2851 | &args.lhsLen, &args.pflags, NULL); | | 2851 | &args.lhsLen, &args.pflags, NULL); |
2852 | if (res != VPR_OK) | | 2852 | if (res != VPR_OK) |
2853 | return AMR_CLEANUP; | | 2853 | return AMR_CLEANUP; |
2854 | args.lhs = lhs; | | 2854 | args.lhs = lhs; |
2855 | | | 2855 | |
2856 | res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &rhs, | | 2856 | res = ParseModifierPartSubst(pp, delim, st->expr->eflags, st, &rhs, |
2857 | &args.rhsLen, NULL, &args); | | 2857 | &args.rhsLen, NULL, &args); |
2858 | if (res != VPR_OK) | | 2858 | if (res != VPR_OK) |
2859 | return AMR_CLEANUP; | | 2859 | return AMR_CLEANUP; |
2860 | args.rhs = rhs; | | 2860 | args.rhs = rhs; |
2861 | | | 2861 | |
2862 | oneBigWord = st->oneBigWord; | | 2862 | oneBigWord = st->oneBigWord; |
2863 | ParsePatternFlags(pp, &args.pflags, &oneBigWord); | | 2863 | ParsePatternFlags(pp, &args.pflags, &oneBigWord); |
2864 | | | 2864 | |
2865 | ModifyWords(st, ModifyWord_Subst, &args, oneBigWord); | | 2865 | ModifyWords(st, ModifyWord_Subst, &args, oneBigWord); |
2866 | | | 2866 | |
2867 | free(lhs); | | 2867 | free(lhs); |
2868 | free(rhs); | | 2868 | free(rhs); |
2869 | return AMR_OK; | | 2869 | return AMR_OK; |
2870 | } | | 2870 | } |
2871 | | | 2871 | |
2872 | #ifndef NO_REGEX | | 2872 | #ifndef NO_REGEX |
2873 | | | 2873 | |
2874 | /* :C,from,to, */ | | 2874 | /* :C,from,to, */ |
2875 | static ApplyModifierResult | | 2875 | static ApplyModifierResult |
2876 | ApplyModifier_Regex(const char **pp, ApplyModifiersState *st) | | 2876 | ApplyModifier_Regex(const char **pp, ApplyModifiersState *st) |
2877 | { | | 2877 | { |
2878 | char *re; | | 2878 | char *re; |
2879 | struct ModifyWord_SubstRegexArgs args; | | 2879 | struct ModifyWord_SubstRegexArgs args; |
2880 | Boolean oneBigWord; | | 2880 | Boolean oneBigWord; |
2881 | int error; | | 2881 | int error; |
2882 | VarParseResult res; | | 2882 | VarParseResult res; |
2883 | | | 2883 | |
2884 | char delim = (*pp)[1]; | | 2884 | char delim = (*pp)[1]; |
2885 | if (delim == '\0') { | | 2885 | if (delim == '\0') { |
2886 | Error("Missing delimiter for :C modifier"); | | 2886 | Error("Missing delimiter for :C modifier"); |
2887 | (*pp)++; | | 2887 | (*pp)++; |
2888 | return AMR_CLEANUP; | | 2888 | return AMR_CLEANUP; |
2889 | } | | 2889 | } |
2890 | | | 2890 | |
2891 | *pp += 2; | | 2891 | *pp += 2; |
2892 | | | 2892 | |
2893 | res = ParseModifierPart(pp, delim, st->expr->eflags, st, &re); | | 2893 | res = ParseModifierPart(pp, delim, st->expr->eflags, st, &re); |
2894 | if (res != VPR_OK) | | 2894 | if (res != VPR_OK) |
2895 | return AMR_CLEANUP; | | 2895 | return AMR_CLEANUP; |
2896 | | | 2896 | |
2897 | res = ParseModifierPart(pp, delim, st->expr->eflags, st, &args.replace); | | 2897 | res = ParseModifierPart(pp, delim, st->expr->eflags, st, &args.replace); |
2898 | if (args.replace == NULL) { | | 2898 | if (args.replace == NULL) { |
2899 | free(re); | | 2899 | free(re); |
2900 | return AMR_CLEANUP; | | 2900 | return AMR_CLEANUP; |
2901 | } | | 2901 | } |
2902 | | | 2902 | |
2903 | args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE }; | | 2903 | args.pflags = (VarPatternFlags){ FALSE, FALSE, FALSE, FALSE }; |
2904 | args.matched = FALSE; | | 2904 | args.matched = FALSE; |
2905 | oneBigWord = st->oneBigWord; | | 2905 | oneBigWord = st->oneBigWord; |
2906 | ParsePatternFlags(pp, &args.pflags, &oneBigWord); | | 2906 | ParsePatternFlags(pp, &args.pflags, &oneBigWord); |
2907 | | | 2907 | |
2908 | if (!(st->expr->eflags & VARE_WANTRES)) { | | 2908 | if (!(st->expr->eflags & VARE_WANTRES)) { |
2909 | free(args.replace); | | 2909 | free(args.replace); |
2910 | free(re); | | 2910 | free(re); |
2911 | return AMR_OK; | | 2911 | return AMR_OK; |
2912 | } | | 2912 | } |
2913 | | | 2913 | |
2914 | error = regcomp(&args.re, re, REG_EXTENDED); | | 2914 | error = regcomp(&args.re, re, REG_EXTENDED); |
2915 | free(re); | | 2915 | free(re); |
2916 | if (error != 0) { | | 2916 | if (error != 0) { |
2917 | VarREError(error, &args.re, "Regex compilation error"); | | 2917 | VarREError(error, &args.re, "Regex compilation error"); |
2918 | free(args.replace); | | 2918 | free(args.replace); |
2919 | return AMR_CLEANUP; | | 2919 | return AMR_CLEANUP; |
2920 | } | | 2920 | } |
2921 | | | 2921 | |
2922 | args.nsub = args.re.re_nsub + 1; | | 2922 | args.nsub = args.re.re_nsub + 1; |
2923 | if (args.nsub > 10) | | 2923 | if (args.nsub > 10) |
2924 | args.nsub = 10; | | 2924 | args.nsub = 10; |
2925 | | | 2925 | |
2926 | ModifyWords(st, ModifyWord_SubstRegex, &args, oneBigWord); | | 2926 | ModifyWords(st, ModifyWord_SubstRegex, &args, oneBigWord); |
2927 | | | 2927 | |
2928 | regfree(&args.re); | | 2928 | regfree(&args.re); |
2929 | free(args.replace); | | 2929 | free(args.replace); |
2930 | return AMR_OK; | | 2930 | return AMR_OK; |
2931 | } | | 2931 | } |
2932 | | | 2932 | |
2933 | #endif | | 2933 | #endif |
2934 | | | 2934 | |
2935 | /* :Q, :q */ | | 2935 | /* :Q, :q */ |
2936 | static ApplyModifierResult | | 2936 | static ApplyModifierResult |
2937 | ApplyModifier_Quote(const char **pp, ApplyModifiersState *st) | | 2937 | ApplyModifier_Quote(const char **pp, ApplyModifiersState *st) |
2938 | { | | 2938 | { |
2939 | Boolean quoteDollar = **pp == 'q'; | | 2939 | Boolean quoteDollar = **pp == 'q'; |
2940 | if (!IsDelimiter((*pp)[1], st)) | | 2940 | if (!IsDelimiter((*pp)[1], st)) |
2941 | return AMR_UNKNOWN; | | 2941 | return AMR_UNKNOWN; |
2942 | (*pp)++; | | 2942 | (*pp)++; |
2943 | | | 2943 | |
2944 | Expr_SetValueOwn(st->expr, VarQuote(st->expr->value.str, quoteDollar)); | | 2944 | Expr_SetValueOwn(st->expr, VarQuote(st->expr->value.str, quoteDollar)); |
2945 | | | 2945 | |
2946 | return AMR_OK; | | 2946 | return AMR_OK; |
2947 | } | | 2947 | } |
2948 | | | 2948 | |
2949 | /*ARGSUSED*/ | | 2949 | /*ARGSUSED*/ |
2950 | static void | | 2950 | static void |
2951 | ModifyWord_Copy(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED) | | 2951 | ModifyWord_Copy(const char *word, SepBuf *buf, void *data MAKE_ATTR_UNUSED) |
2952 | { | | 2952 | { |
2953 | SepBuf_AddStr(buf, word); | | 2953 | SepBuf_AddStr(buf, word); |
2954 | } | | 2954 | } |
2955 | | | 2955 | |
2956 | /* :ts<separator> */ | | 2956 | /* :ts<separator> */ |
2957 | static ApplyModifierResult | | 2957 | static ApplyModifierResult |
2958 | ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st) | | 2958 | ApplyModifier_ToSep(const char **pp, ApplyModifiersState *st) |
2959 | { | | 2959 | { |
2960 | const char *sep = *pp + 2; | | 2960 | const char *sep = *pp + 2; |
2961 | | | 2961 | |
2962 | /* ":ts<any><endc>" or ":ts<any>:" */ | | 2962 | /* ":ts<any><endc>" or ":ts<any>:" */ |
2963 | if (sep[0] != st->endc && IsDelimiter(sep[1], st)) { | | 2963 | if (sep[0] != st->endc && IsDelimiter(sep[1], st)) { |
2964 | *pp = sep + 1; | | 2964 | *pp = sep + 1; |
2965 | st->sep = sep[0]; | | 2965 | st->sep = sep[0]; |
2966 | goto ok; | | 2966 | goto ok; |
2967 | } | | 2967 | } |
2968 | | | 2968 | |
2969 | /* ":ts<endc>" or ":ts:" */ | | 2969 | /* ":ts<endc>" or ":ts:" */ |
2970 | if (IsDelimiter(sep[0], st)) { | | 2970 | if (IsDelimiter(sep[0], st)) { |
2971 | *pp = sep; | | 2971 | *pp = sep; |
2972 | st->sep = '\0'; /* no separator */ | | 2972 | st->sep = '\0'; /* no separator */ |
2973 | goto ok; | | 2973 | goto ok; |
2974 | } | | 2974 | } |
2975 | | | 2975 | |
2976 | /* ":ts<unrecognised><unrecognised>". */ | | 2976 | /* ":ts<unrecognised><unrecognised>". */ |
2977 | if (sep[0] != '\\') { | | 2977 | if (sep[0] != '\\') { |
2978 | (*pp)++; /* just for backwards compatibility */ | | 2978 | (*pp)++; /* just for backwards compatibility */ |
2979 | return AMR_BAD; | | 2979 | return AMR_BAD; |
2980 | } | | 2980 | } |
2981 | | | 2981 | |
2982 | /* ":ts\n" */ | | 2982 | /* ":ts\n" */ |
2983 | if (sep[1] == 'n') { | | 2983 | if (sep[1] == 'n') { |
2984 | *pp = sep + 2; | | 2984 | *pp = sep + 2; |
2985 | st->sep = '\n'; | | 2985 | st->sep = '\n'; |
2986 | goto ok; | | 2986 | goto ok; |
2987 | } | | 2987 | } |
2988 | | | 2988 | |
2989 | /* ":ts\t" */ | | 2989 | /* ":ts\t" */ |
2990 | if (sep[1] == 't') { | | 2990 | if (sep[1] == 't') { |
2991 | *pp = sep + 2; | | 2991 | *pp = sep + 2; |
2992 | st->sep = '\t'; | | 2992 | st->sep = '\t'; |
2993 | goto ok; | | 2993 | goto ok; |
2994 | } | | 2994 | } |
2995 | | | 2995 | |
2996 | /* ":ts\x40" or ":ts\100" */ | | 2996 | /* ":ts\x40" or ":ts\100" */ |
2997 | { | | 2997 | { |
2998 | const char *p = sep + 1; | | 2998 | const char *p = sep + 1; |
2999 | int base = 8; /* assume octal */ | | 2999 | int base = 8; /* assume octal */ |
3000 | | | 3000 | |
3001 | if (sep[1] == 'x') { | | 3001 | if (sep[1] == 'x') { |
3002 | base = 16; | | 3002 | base = 16; |
3003 | p++; | | 3003 | p++; |
3004 | } else if (!ch_isdigit(sep[1])) { | | 3004 | } else if (!ch_isdigit(sep[1])) { |
3005 | (*pp)++; /* just for backwards compatibility */ | | 3005 | (*pp)++; /* just for backwards compatibility */ |
3006 | return AMR_BAD; /* ":ts<backslash><unrecognised>". */ | | 3006 | return AMR_BAD; /* ":ts<backslash><unrecognised>". */ |
3007 | } | | 3007 | } |
3008 | | | 3008 | |
3009 | if (!TryParseChar(&p, base, &st->sep)) { | | 3009 | if (!TryParseChar(&p, base, &st->sep)) { |
3010 | Parse_Error(PARSE_FATAL, | | 3010 | Parse_Error(PARSE_FATAL, |
3011 | "Invalid character number: %s", p); | | 3011 | "Invalid character number: %s", p); |
3012 | return AMR_CLEANUP; | | 3012 | return AMR_CLEANUP; |
3013 | } | | 3013 | } |
3014 | if (!IsDelimiter(*p, st)) { | | 3014 | if (!IsDelimiter(*p, st)) { |
3015 | (*pp)++; /* just for backwards compatibility */ | | 3015 | (*pp)++; /* just for backwards compatibility */ |
3016 | return AMR_BAD; | | 3016 | return AMR_BAD; |
3017 | } | | 3017 | } |
3018 | | | 3018 | |
3019 | *pp = p; | | 3019 | *pp = p; |
3020 | } | | 3020 | } |
3021 | | | 3021 | |
3022 | ok: | | 3022 | ok: |
3023 | ModifyWords(st, ModifyWord_Copy, NULL, st->oneBigWord); | | 3023 | ModifyWords(st, ModifyWord_Copy, NULL, st->oneBigWord); |
3024 | return AMR_OK; | | 3024 | return AMR_OK; |
3025 | } | | 3025 | } |
3026 | | | 3026 | |
3027 | static char * | | 3027 | static char * |
3028 | str_toupper(const char *str) | | 3028 | str_toupper(const char *str) |
3029 | { | | 3029 | { |
3030 | char *res; | | 3030 | char *res; |
3031 | size_t i, len; | | 3031 | size_t i, len; |
3032 | | | 3032 | |
3033 | len = strlen(str); | | 3033 | len = strlen(str); |
3034 | res = bmake_malloc(len + 1); | | 3034 | res = bmake_malloc(len + 1); |
3035 | for (i = 0; i < len + 1; i++) | | 3035 | for (i = 0; i < len + 1; i++) |
3036 | res[i] = ch_toupper(str[i]); | | 3036 | res[i] = ch_toupper(str[i]); |
3037 | | | 3037 | |
3038 | return res; | | 3038 | return res; |
3039 | } | | 3039 | } |
3040 | | | 3040 | |
3041 | static char * | | 3041 | static char * |
3042 | str_tolower(const char *str) | | 3042 | str_tolower(const char *str) |
3043 | { | | 3043 | { |
3044 | char *res; | | 3044 | char *res; |
3045 | size_t i, len; | | 3045 | size_t i, len; |
3046 | | | 3046 | |
3047 | len = strlen(str); | | 3047 | len = strlen(str); |
3048 | res = bmake_malloc(len + 1); | | 3048 | res = bmake_malloc(len + 1); |
3049 | for (i = 0; i < len + 1; i++) | | 3049 | for (i = 0; i < len + 1; i++) |
3050 | res[i] = ch_tolower(str[i]); | | 3050 | res[i] = ch_tolower(str[i]); |
3051 | | | 3051 | |
3052 | return res; | | 3052 | return res; |
3053 | } | | 3053 | } |
3054 | | | 3054 | |
3055 | /* :tA, :tu, :tl, :ts<separator>, etc. */ | | 3055 | /* :tA, :tu, :tl, :ts<separator>, etc. */ |
3056 | static ApplyModifierResult | | 3056 | static ApplyModifierResult |
3057 | ApplyModifier_To(const char **pp, ApplyModifiersState *st) | | 3057 | ApplyModifier_To(const char **pp, ApplyModifiersState *st) |
3058 | { | | 3058 | { |
3059 | Expr *expr = st->expr; | | 3059 | Expr *expr = st->expr; |
3060 | const char *mod = *pp; | | 3060 | const char *mod = *pp; |
3061 | assert(mod[0] == 't'); | | 3061 | assert(mod[0] == 't'); |
3062 | | | 3062 | |
3063 | if (IsDelimiter(mod[1], st) || mod[1] == '\0') { | | 3063 | if (IsDelimiter(mod[1], st) || mod[1] == '\0') { |
3064 | *pp = mod + 1; | | 3064 | *pp = mod + 1; |
3065 | return AMR_BAD; /* Found ":t<endc>" or ":t:". */ | | 3065 | return AMR_BAD; /* Found ":t<endc>" or ":t:". */ |
3066 | } | | 3066 | } |
3067 | | | 3067 | |
3068 | if (mod[1] == 's') | | 3068 | if (mod[1] == 's') |
3069 | return ApplyModifier_ToSep(pp, st); | | 3069 | return ApplyModifier_ToSep(pp, st); |
3070 | | | 3070 | |
3071 | if (!IsDelimiter(mod[2], st)) { /* :t<unrecognized> */ | | 3071 | if (!IsDelimiter(mod[2], st)) { /* :t<unrecognized> */ |
3072 | *pp = mod + 1; | | 3072 | *pp = mod + 1; |
3073 | return AMR_BAD; | | 3073 | return AMR_BAD; |
3074 | } | | 3074 | } |
3075 | | | 3075 | |
3076 | if (mod[1] == 'A') { /* :tA */ | | 3076 | if (mod[1] == 'A') { /* :tA */ |
3077 | *pp = mod + 2; | | 3077 | *pp = mod + 2; |
3078 | ModifyWords(st, ModifyWord_Realpath, NULL, st->oneBigWord); | | 3078 | ModifyWords(st, ModifyWord_Realpath, NULL, st->oneBigWord); |
3079 | return AMR_OK; | | 3079 | return AMR_OK; |
3080 | } | | 3080 | } |
3081 | | | 3081 | |
3082 | if (mod[1] == 'u') { /* :tu */ | | 3082 | if (mod[1] == 'u') { /* :tu */ |
3083 | *pp = mod + 2; | | 3083 | *pp = mod + 2; |
3084 | Expr_SetValueOwn(expr, str_toupper(expr->value.str)); | | 3084 | Expr_SetValueOwn(expr, str_toupper(expr->value.str)); |
3085 | return AMR_OK; | | 3085 | return AMR_OK; |
3086 | } | | 3086 | } |
3087 | | | 3087 | |
3088 | if (mod[1] == 'l') { /* :tl */ | | 3088 | if (mod[1] == 'l') { /* :tl */ |
3089 | *pp = mod + 2; | | 3089 | *pp = mod + 2; |
3090 | Expr_SetValueOwn(expr, str_tolower(expr->value.str)); | | 3090 | Expr_SetValueOwn(expr, str_tolower(expr->value.str)); |
3091 | return AMR_OK; | | 3091 | return AMR_OK; |
3092 | } | | 3092 | } |
3093 | | | 3093 | |
3094 | if (mod[1] == 'W' || mod[1] == 'w') { /* :tW, :tw */ | | 3094 | if (mod[1] == 'W' || mod[1] == 'w') { /* :tW, :tw */ |
3095 | *pp = mod + 2; | | 3095 | *pp = mod + 2; |
3096 | st->oneBigWord = mod[1] == 'W'; | | 3096 | st->oneBigWord = mod[1] == 'W'; |
3097 | return AMR_OK; | | 3097 | return AMR_OK; |
3098 | } | | 3098 | } |
3099 | | | 3099 | |
3100 | /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */ | | 3100 | /* Found ":t<unrecognised>:" or ":t<unrecognised><endc>". */ |
3101 | *pp = mod + 1; /* XXX: unnecessary but observable */ | | 3101 | *pp = mod + 1; /* XXX: unnecessary but observable */ |
3102 | return AMR_BAD; | | 3102 | return AMR_BAD; |
3103 | } | | 3103 | } |
3104 | | | 3104 | |
3105 | /* :[#], :[1], :[-1..1], etc. */ | | 3105 | /* :[#], :[1], :[-1..1], etc. */ |
3106 | static ApplyModifierResult | | 3106 | static ApplyModifierResult |
3107 | ApplyModifier_Words(const char **pp, ApplyModifiersState *st) | | 3107 | ApplyModifier_Words(const char **pp, ApplyModifiersState *st) |
3108 | { | | 3108 | { |
3109 | Expr *expr = st->expr; | | 3109 | Expr *expr = st->expr; |
3110 | char *estr; | | 3110 | char *estr; |
3111 | int first, last; | | 3111 | int first, last; |
3112 | VarParseResult res; | | 3112 | VarParseResult res; |
3113 | const char *p; | | 3113 | const char *p; |
3114 | | | 3114 | |
3115 | (*pp)++; /* skip the '[' */ | | 3115 | (*pp)++; /* skip the '[' */ |
3116 | res = ParseModifierPart(pp, ']', expr->eflags, st, &estr); | | 3116 | res = ParseModifierPart(pp, ']', expr->eflags, st, &estr); |
3117 | if (res != VPR_OK) | | 3117 | if (res != VPR_OK) |
3118 | return AMR_CLEANUP; | | 3118 | return AMR_CLEANUP; |
3119 | | | 3119 | |
3120 | if (!IsDelimiter(**pp, st)) | | 3120 | if (!IsDelimiter(**pp, st)) |
3121 | goto bad_modifier; /* Found junk after ']' */ | | 3121 | goto bad_modifier; /* Found junk after ']' */ |
3122 | | | 3122 | |
3123 | if (estr[0] == '\0') | | 3123 | if (estr[0] == '\0') |
3124 | goto bad_modifier; /* Found ":[]". */ | | 3124 | goto bad_modifier; /* Found ":[]". */ |
3125 | | | 3125 | |
3126 | if (estr[0] == '#' && estr[1] == '\0') { /* Found ":[#]" */ | | 3126 | if (estr[0] == '#' && estr[1] == '\0') { /* Found ":[#]" */ |
3127 | if (st->oneBigWord) { | | 3127 | if (st->oneBigWord) { |
3128 | Expr_SetValueRefer(expr, "1"); | | 3128 | Expr_SetValueRefer(expr, "1"); |
3129 | } else { | | 3129 | } else { |
3130 | Buffer buf; | | 3130 | Buffer buf; |
3131 | | | 3131 | |
3132 | Words words = Str_Words(expr->value.str, FALSE); | | 3132 | Words words = Str_Words(expr->value.str, FALSE); |
3133 | size_t ac = words.len; | | 3133 | size_t ac = words.len; |
3134 | Words_Free(words); | | 3134 | Words_Free(words); |
3135 | | | 3135 | |
3136 | /* 3 digits + '\0' is usually enough */ | | 3136 | /* 3 digits + '\0' is usually enough */ |
3137 | Buf_InitSize(&buf, 4); | | 3137 | Buf_InitSize(&buf, 4); |
3138 | Buf_AddInt(&buf, (int)ac); | | 3138 | Buf_AddInt(&buf, (int)ac); |
3139 | Expr_SetValueOwn(expr, Buf_DoneData(&buf)); | | 3139 | Expr_SetValueOwn(expr, Buf_DoneData(&buf)); |
3140 | } | | 3140 | } |
3141 | goto ok; | | 3141 | goto ok; |
3142 | } | | 3142 | } |
3143 | | | 3143 | |
3144 | if (estr[0] == '*' && estr[1] == '\0') { /* Found ":[*]" */ | | 3144 | if (estr[0] == '*' && estr[1] == '\0') { /* Found ":[*]" */ |
3145 | st->oneBigWord = TRUE; | | 3145 | st->oneBigWord = TRUE; |
3146 | goto ok; | | 3146 | goto ok; |
3147 | } | | 3147 | } |
3148 | | | 3148 | |
3149 | if (estr[0] == '@' && estr[1] == '\0') { /* Found ":[@]" */ | | 3149 | if (estr[0] == '@' && estr[1] == '\0') { /* Found ":[@]" */ |
3150 | st->oneBigWord = FALSE; | | 3150 | st->oneBigWord = FALSE; |
3151 | goto ok; | | 3151 | goto ok; |
3152 | } | | 3152 | } |
3153 | | | 3153 | |
3154 | /* | | 3154 | /* |
3155 | * We expect estr to contain a single integer for :[N], or two | | 3155 | * We expect estr to contain a single integer for :[N], or two |
3156 | * integers separated by ".." for :[start..end]. | | 3156 | * integers separated by ".." for :[start..end]. |
3157 | */ | | 3157 | */ |
3158 | p = estr; | | 3158 | p = estr; |
3159 | if (!TryParseIntBase0(&p, &first)) | | 3159 | if (!TryParseIntBase0(&p, &first)) |
3160 | goto bad_modifier; /* Found junk instead of a number */ | | 3160 | goto bad_modifier; /* Found junk instead of a number */ |
3161 | | | 3161 | |
3162 | if (p[0] == '\0') { /* Found only one integer in :[N] */ | | 3162 | if (p[0] == '\0') { /* Found only one integer in :[N] */ |
3163 | last = first; | | 3163 | last = first; |
3164 | } else if (p[0] == '.' && p[1] == '.' && p[2] != '\0') { | | 3164 | } else if (p[0] == '.' && p[1] == '.' && p[2] != '\0') { |
3165 | /* Expecting another integer after ".." */ | | 3165 | /* Expecting another integer after ".." */ |
3166 | p += 2; | | 3166 | p += 2; |
3167 | if (!TryParseIntBase0(&p, &last) || *p != '\0') | | 3167 | if (!TryParseIntBase0(&p, &last) || *p != '\0') |
3168 | goto bad_modifier; /* Found junk after ".." */ | | 3168 | goto bad_modifier; /* Found junk after ".." */ |
3169 | } else | | 3169 | } else |
3170 | goto bad_modifier; /* Found junk instead of ".." */ | | 3170 | goto bad_modifier; /* Found junk instead of ".." */ |
3171 | | | 3171 | |
3172 | /* | | 3172 | /* |
3173 | * Now first and last are properly filled in, but we still have to | | 3173 | * Now first and last are properly filled in, but we still have to |
3174 | * check for 0 as a special case. | | 3174 | * check for 0 as a special case. |
3175 | */ | | 3175 | */ |
3176 | if (first == 0 && last == 0) { | | 3176 | if (first == 0 && last == 0) { |
3177 | /* ":[0]" or perhaps ":[0..0]" */ | | 3177 | /* ":[0]" or perhaps ":[0..0]" */ |
3178 | st->oneBigWord = TRUE; | | 3178 | st->oneBigWord = TRUE; |
3179 | goto ok; | | 3179 | goto ok; |
3180 | } | | 3180 | } |
3181 | | | 3181 | |
3182 | /* ":[0..N]" or ":[N..0]" */ | | 3182 | /* ":[0..N]" or ":[N..0]" */ |
3183 | if (first == 0 || last == 0) | | 3183 | if (first == 0 || last == 0) |
3184 | goto bad_modifier; | | 3184 | goto bad_modifier; |
3185 | | | 3185 | |
3186 | /* Normal case: select the words described by first and last. */ | | 3186 | /* Normal case: select the words described by first and last. */ |
3187 | Expr_SetValueOwn(expr, | | 3187 | Expr_SetValueOwn(expr, |
3188 | VarSelectWords(expr->value.str, first, last, | | 3188 | VarSelectWords(expr->value.str, first, last, |
3189 | st->sep, st->oneBigWord)); | | 3189 | st->sep, st->oneBigWord)); |
3190 | | | 3190 | |
3191 | ok: | | 3191 | ok: |
3192 | free(estr); | | 3192 | free(estr); |
3193 | return AMR_OK; | | 3193 | return AMR_OK; |
3194 | | | 3194 | |
3195 | bad_modifier: | | 3195 | bad_modifier: |
3196 | free(estr); | | 3196 | free(estr); |
3197 | return AMR_BAD; | | 3197 | return AMR_BAD; |
3198 | } | | 3198 | } |
3199 | | | 3199 | |
3200 | static int | | 3200 | static int |
3201 | str_cmp_asc(const void *a, const void *b) | | 3201 | str_cmp_asc(const void *a, const void *b) |
3202 | { | | 3202 | { |
3203 | return strcmp(*(const char *const *)a, *(const char *const *)b); | | 3203 | return strcmp(*(const char *const *)a, *(const char *const *)b); |
3204 | } | | 3204 | } |
3205 | | | 3205 | |
3206 | static int | | 3206 | static int |
3207 | str_cmp_desc(const void *a, const void *b) | | 3207 | str_cmp_desc(const void *a, const void *b) |
3208 | { | | 3208 | { |
3209 | return strcmp(*(const char *const *)b, *(const char *const *)a); | | 3209 | return strcmp(*(const char *const *)b, *(const char *const *)a); |
3210 | } | | 3210 | } |
3211 | | | 3211 | |
3212 | static void | | 3212 | static void |
3213 | ShuffleStrings(char **strs, size_t n) | | 3213 | ShuffleStrings(char **strs, size_t n) |
3214 | { | | 3214 | { |
3215 | size_t i; | | 3215 | size_t i; |
3216 | | | 3216 | |
3217 | for (i = n - 1; i > 0; i--) { | | 3217 | for (i = n - 1; i > 0; i--) { |
3218 | size_t rndidx = (size_t)random() % (i + 1); | | 3218 | size_t rndidx = (size_t)random() % (i + 1); |
3219 | char *t = strs[i]; | | 3219 | char *t = strs[i]; |
3220 | strs[i] = strs[rndidx]; | | 3220 | strs[i] = strs[rndidx]; |
3221 | strs[rndidx] = t; | | 3221 | strs[rndidx] = t; |
3222 | } | | 3222 | } |
3223 | } | | 3223 | } |
3224 | | | 3224 | |
3225 | /* :O (order ascending) or :Or (order descending) or :Ox (shuffle) */ | | 3225 | /* :O (order ascending) or :Or (order descending) or :Ox (shuffle) */ |
3226 | static ApplyModifierResult | | 3226 | static ApplyModifierResult |
3227 | ApplyModifier_Order(const char **pp, ApplyModifiersState *st) | | 3227 | ApplyModifier_Order(const char **pp, ApplyModifiersState *st) |
3228 | { | | 3228 | { |
3229 | const char *mod = (*pp)++; /* skip past the 'O' in any case */ | | 3229 | const char *mod = (*pp)++; /* skip past the 'O' in any case */ |
3230 | | | 3230 | |
3231 | /* TODO: separate parsing from evaluating */ | | 3231 | /* TODO: separate parsing from evaluating */ |
3232 | | | 3232 | |
3233 | Words words = Str_Words(st->expr->value.str, FALSE); | | 3233 | Words words = Str_Words(st->expr->value.str, FALSE); |
3234 | | | 3234 | |
3235 | if (IsDelimiter(mod[1], st)) { | | 3235 | if (IsDelimiter(mod[1], st)) { |
3236 | /* :O sorts ascending */ | | 3236 | /* :O sorts ascending */ |
3237 | qsort(words.words, words.len, sizeof words.words[0], | | 3237 | qsort(words.words, words.len, sizeof words.words[0], |
3238 | str_cmp_asc); | | 3238 | str_cmp_asc); |
3239 | | | 3239 | |
3240 | } else if ((mod[1] == 'r' || mod[1] == 'x') && | | 3240 | } else if ((mod[1] == 'r' || mod[1] == 'x') && |
3241 | IsDelimiter(mod[2], st)) { | | 3241 | IsDelimiter(mod[2], st)) { |
3242 | (*pp)++; | | 3242 | (*pp)++; |
3243 | | | 3243 | |
3244 | if (mod[1] == 'r') { /* :Or sorts descending */ | | 3244 | if (mod[1] == 'r') { /* :Or sorts descending */ |
3245 | qsort(words.words, words.len, sizeof words.words[0], | | 3245 | qsort(words.words, words.len, sizeof words.words[0], |
3246 | str_cmp_desc); | | 3246 | str_cmp_desc); |
3247 | } else | | 3247 | } else |
3248 | ShuffleStrings(words.words, words.len); | | 3248 | ShuffleStrings(words.words, words.len); |
3249 | } else { | | 3249 | } else { |
3250 | Words_Free(words); | | 3250 | Words_Free(words); |
3251 | return AMR_BAD; | | 3251 | return AMR_BAD; |
3252 | } | | 3252 | } |
3253 | | | 3253 | |
3254 | Expr_SetValueOwn(st->expr, Words_JoinFree(words)); | | 3254 | Expr_SetValueOwn(st->expr, Words_JoinFree(words)); |
3255 | return AMR_OK; | | 3255 | return AMR_OK; |
3256 | } | | 3256 | } |
3257 | | | 3257 | |
3258 | /* :? then : else */ | | 3258 | /* :? then : else */ |
3259 | static ApplyModifierResult | | 3259 | static ApplyModifierResult |
3260 | ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st) | | 3260 | ApplyModifier_IfElse(const char **pp, ApplyModifiersState *st) |
3261 | { | | 3261 | { |
3262 | Expr *expr = st->expr; | | 3262 | Expr *expr = st->expr; |
3263 | char *then_expr, *else_expr; | | 3263 | char *then_expr, *else_expr; |
3264 | VarParseResult res; | | 3264 | VarParseResult res; |
3265 | | | 3265 | |
3266 | Boolean value = FALSE; | | 3266 | Boolean value = FALSE; |
3267 | VarEvalFlags then_eflags = VARE_NONE; | | 3267 | VarEvalFlags then_eflags = VARE_NONE; |
3268 | VarEvalFlags else_eflags = VARE_NONE; | | 3268 | VarEvalFlags else_eflags = VARE_NONE; |
3269 | | | 3269 | |
3270 | int cond_rc = COND_PARSE; /* anything other than COND_INVALID */ | | 3270 | int cond_rc = COND_PARSE; /* anything other than COND_INVALID */ |
3271 | if (expr->eflags & VARE_WANTRES) { | | 3271 | if (expr->eflags & VARE_WANTRES) { |
3272 | cond_rc = Cond_EvalCondition(expr->var->name.str, &value); | | 3272 | cond_rc = Cond_EvalCondition(expr->var->name.str, &value); |
3273 | if (cond_rc != COND_INVALID && value) | | 3273 | if (cond_rc != COND_INVALID && value) |
3274 | then_eflags = expr->eflags; | | 3274 | then_eflags = expr->eflags; |
3275 | if (cond_rc != COND_INVALID && !value) | | 3275 | if (cond_rc != COND_INVALID && !value) |
3276 | else_eflags = expr->eflags; | | 3276 | else_eflags = expr->eflags; |
3277 | } | | 3277 | } |
3278 | | | 3278 | |
3279 | (*pp)++; /* skip past the '?' */ | | 3279 | (*pp)++; /* skip past the '?' */ |
3280 | res = ParseModifierPart(pp, ':', then_eflags, st, &then_expr); | | 3280 | res = ParseModifierPart(pp, ':', then_eflags, st, &then_expr); |
3281 | if (res != VPR_OK) | | 3281 | if (res != VPR_OK) |
3282 | return AMR_CLEANUP; | | 3282 | return AMR_CLEANUP; |
3283 | | | 3283 | |
3284 | res = ParseModifierPart(pp, st->endc, else_eflags, st, &else_expr); | | 3284 | res = ParseModifierPart(pp, st->endc, else_eflags, st, &else_expr); |
3285 | if (res != VPR_OK) | | 3285 | if (res != VPR_OK) |
3286 | return AMR_CLEANUP; | | 3286 | return AMR_CLEANUP; |
3287 | | | 3287 | |
3288 | (*pp)--; /* Go back to the st->endc. */ | | 3288 | (*pp)--; /* Go back to the st->endc. */ |
3289 | | | 3289 | |
3290 | if (cond_rc == COND_INVALID) { | | 3290 | if (cond_rc == COND_INVALID) { |
3291 | Error("Bad conditional expression `%s' in %s?%s:%s", | | 3291 | Error("Bad conditional expression `%s' in %s?%s:%s", |
3292 | expr->var->name.str, expr->var->name.str, | | 3292 | expr->var->name.str, expr->var->name.str, |
3293 | then_expr, else_expr); | | 3293 | then_expr, else_expr); |
3294 | return AMR_CLEANUP; | | 3294 | return AMR_CLEANUP; |
3295 | } | | 3295 | } |
3296 | | | 3296 | |
3297 | if (value) { | | 3297 | if (value) { |
3298 | Expr_SetValueOwn(expr, then_expr); | | 3298 | Expr_SetValueOwn(expr, then_expr); |
3299 | free(else_expr); | | 3299 | free(else_expr); |
3300 | } else { | | 3300 | } else { |
3301 | Expr_SetValueOwn(expr, else_expr); | | 3301 | Expr_SetValueOwn(expr, else_expr); |
3302 | free(then_expr); | | 3302 | free(then_expr); |
3303 | } | | 3303 | } |
3304 | Expr_Define(expr); | | 3304 | Expr_Define(expr); |
3305 | return AMR_OK; | | 3305 | return AMR_OK; |
3306 | } | | 3306 | } |
3307 | | | 3307 | |
3308 | /* | | 3308 | /* |
3309 | * The ::= modifiers are special in that they do not read the variable value | | 3309 | * The ::= modifiers are special in that they do not read the variable value |
3310 | * but instead assign to that variable. They always expand to an empty | | 3310 | * but instead assign to that variable. They always expand to an empty |
3311 | * string. | | 3311 | * string. |
3312 | * | | 3312 | * |
3313 | * Their main purpose is in supporting .for loops that generate shell commands | | 3313 | * Their main purpose is in supporting .for loops that generate shell commands |
3314 | * since an ordinary variable assignment at that point would terminate the | | 3314 | * since an ordinary variable assignment at that point would terminate the |
3315 | * dependency group for these targets. For example: | | 3315 | * dependency group for these targets. For example: |
3316 | * | | 3316 | * |
3317 | * list-targets: .USE | | 3317 | * list-targets: .USE |
3318 | * .for i in ${.TARGET} ${.TARGET:R}.gz | | 3318 | * .for i in ${.TARGET} ${.TARGET:R}.gz |
3319 | * @${t::=$i} | | 3319 | * @${t::=$i} |
3320 | * @echo 'The target is ${t:T}.' | | 3320 | * @echo 'The target is ${t:T}.' |
3321 | * .endfor | | 3321 | * .endfor |
3322 | * | | 3322 | * |
3323 | * ::=<str> Assigns <str> as the new value of variable. | | 3323 | * ::=<str> Assigns <str> as the new value of variable. |
3324 | * ::?=<str> Assigns <str> as value of variable if | | 3324 | * ::?=<str> Assigns <str> as value of variable if |
3325 | * it was not already set. | | 3325 | * it was not already set. |
3326 | * ::+=<str> Appends <str> to variable. | | 3326 | * ::+=<str> Appends <str> to variable. |
3327 | * ::!=<cmd> Assigns output of <cmd> as the new value of | | 3327 | * ::!=<cmd> Assigns output of <cmd> as the new value of |
3328 | * variable. | | 3328 | * variable. |
3329 | */ | | 3329 | */ |
3330 | static ApplyModifierResult | | 3330 | static ApplyModifierResult |
3331 | ApplyModifier_Assign(const char **pp, ApplyModifiersState *st) | | 3331 | ApplyModifier_Assign(const char **pp, ApplyModifiersState *st) |
3332 | { | | 3332 | { |
3333 | Expr *expr = st->expr; | | 3333 | Expr *expr = st->expr; |
3334 | GNode *scope; | | 3334 | GNode *scope; |
3335 | char *val; | | 3335 | char *val; |
3336 | VarParseResult res; | | 3336 | VarParseResult res; |
3337 | | | 3337 | |
3338 | /* TODO: separate parsing from evaluating */ | | 3338 | /* TODO: separate parsing from evaluating */ |
3339 | | | 3339 | |
3340 | const char *mod = *pp; | | 3340 | const char *mod = *pp; |
3341 | const char *op = mod + 1; | | 3341 | const char *op = mod + 1; |
3342 | | | 3342 | |
3343 | if (op[0] == '=') | | 3343 | if (op[0] == '=') |
3344 | goto ok; | | 3344 | goto ok; |
3345 | if ((op[0] == '!' || op[0] == '+' || op[0] == '?') && op[1] == '=') | | 3345 | if ((op[0] == '!' || op[0] == '+' || op[0] == '?') && op[1] == '=') |
3346 | goto ok; | | 3346 | goto ok; |
3347 | return AMR_UNKNOWN; /* "::<unrecognised>" */ | | 3347 | return AMR_UNKNOWN; /* "::<unrecognised>" */ |
3348 | | | 3348 | |
3349 | ok: | | 3349 | ok: |
3350 | if (expr->var->name.str[0] == '\0') { | | 3350 | if (expr->var->name.str[0] == '\0') { |
3351 | *pp = mod + 1; | | 3351 | *pp = mod + 1; |
3352 | return AMR_BAD; | | 3352 | return AMR_BAD; |
3353 | } | | 3353 | } |
3354 | | | 3354 | |
3355 | scope = expr->scope; /* scope where v belongs */ | | 3355 | scope = expr->scope; /* scope where v belongs */ |
3356 | if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL) { | | 3356 | if (expr->defined == DEF_REGULAR && expr->scope != SCOPE_GLOBAL) { |
3357 | Var *gv = VarFind(expr->var->name.str, expr->scope, FALSE); | | 3357 | Var *gv = VarFind(expr->var->name.str, expr->scope, FALSE); |
3358 | if (gv == NULL) | | 3358 | if (gv == NULL) |
3359 | scope = SCOPE_GLOBAL; | | 3359 | scope = SCOPE_GLOBAL; |
3360 | else | | 3360 | else |
3361 | VarFreeEnv(gv); | | 3361 | VarFreeEnv(gv); |
3362 | } | | 3362 | } |
3363 | | | 3363 | |
3364 | switch (op[0]) { | | 3364 | switch (op[0]) { |
3365 | case '+': | | 3365 | case '+': |
3366 | case '?': | | 3366 | case '?': |
3367 | case '!': | | 3367 | case '!': |
3368 | *pp = mod + 3; | | 3368 | *pp = mod + 3; |
3369 | break; | | 3369 | break; |
3370 | default: | | 3370 | default: |
3371 | *pp = mod + 2; | | 3371 | *pp = mod + 2; |
3372 | break; | | 3372 | break; |
3373 | } | | 3373 | } |
3374 | | | 3374 | |
3375 | res = ParseModifierPart(pp, st->endc, expr->eflags, st, &val); | | 3375 | res = ParseModifierPart(pp, st->endc, expr->eflags, st, &val); |
3376 | if (res != VPR_OK) | | 3376 | if (res != VPR_OK) |
3377 | return AMR_CLEANUP; | | 3377 | return AMR_CLEANUP; |
3378 | | | 3378 | |
3379 | (*pp)--; /* Go back to the st->endc. */ | | 3379 | (*pp)--; /* Go back to the st->endc. */ |
3380 | | | 3380 | |
3381 | /* XXX: Expanding the variable name at this point sounds wrong. */ | | 3381 | /* XXX: Expanding the variable name at this point sounds wrong. */ |
3382 | if (expr->eflags & VARE_WANTRES) { | | 3382 | if (expr->eflags & VARE_WANTRES) { |
3383 | switch (op[0]) { | | 3383 | switch (op[0]) { |
3384 | case '+': | | 3384 | case '+': |
3385 | Var_AppendExpand(scope, expr->var->name.str, val); | | 3385 | Var_AppendExpand(scope, expr->var->name.str, val); |
3386 | break; | | 3386 | break; |
3387 | case '!': { | | 3387 | case '!': { |
3388 | const char *errfmt; | | 3388 | const char *errfmt; |
3389 | char *cmd_output = Cmd_Exec(val, &errfmt); | | 3389 | char *cmd_output = Cmd_Exec(val, &errfmt); |
3390 | if (errfmt != NULL) | | 3390 | if (errfmt != NULL) |
3391 | Error(errfmt, val); | | 3391 | Error(errfmt, val); |
3392 | else | | 3392 | else |
3393 | Var_SetExpand(scope, | | 3393 | Var_SetExpand(scope, |
3394 | expr->var->name.str, cmd_output); | | 3394 | expr->var->name.str, cmd_output); |
3395 | free(cmd_output); | | 3395 | free(cmd_output); |
3396 | break; | | 3396 | break; |
3397 | } | | 3397 | } |
3398 | case '?': | | 3398 | case '?': |
3399 | if (expr->defined == DEF_REGULAR) | | 3399 | if (expr->defined == DEF_REGULAR) |
3400 | break; | | 3400 | break; |
3401 | /* FALLTHROUGH */ | | 3401 | /* FALLTHROUGH */ |
3402 | default: | | 3402 | default: |
3403 | Var_SetExpand(scope, expr->var->name.str, val); | | 3403 | Var_SetExpand(scope, expr->var->name.str, val); |
3404 | break; | | 3404 | break; |
3405 | } | | 3405 | } |
3406 | } | | 3406 | } |
3407 | free(val); | | 3407 | free(val); |
3408 | Expr_SetValueRefer(expr, ""); | | 3408 | Expr_SetValueRefer(expr, ""); |
3409 | return AMR_OK; | | 3409 | return AMR_OK; |
3410 | } | | 3410 | } |
3411 | | | 3411 | |
3412 | /* | | 3412 | /* |
3413 | * :_=... | | 3413 | * :_=... |
3414 | * remember current value | | 3414 | * remember current value |
3415 | */ | | 3415 | */ |
3416 | static ApplyModifierResult | | 3416 | static ApplyModifierResult |
3417 | ApplyModifier_Remember(const char **pp, ApplyModifiersState *st) | | 3417 | ApplyModifier_Remember(const char **pp, ApplyModifiersState *st) |
3418 | { | | 3418 | { |
3419 | Expr *expr = st->expr; | | 3419 | Expr *expr = st->expr; |
3420 | const char *mod = *pp; | | 3420 | const char *mod = *pp; |
3421 | FStr name; | | 3421 | FStr name; |
3422 | | | 3422 | |
3423 | if (!ModMatchEq(mod, "_", st)) | | 3423 | if (!ModMatchEq(mod, "_", st)) |
3424 | return AMR_UNKNOWN; | | 3424 | return AMR_UNKNOWN; |
3425 | | | 3425 | |
3426 | name = FStr_InitRefer("_"); | | 3426 | name = FStr_InitRefer("_"); |
3427 | if (mod[1] == '=') { | | 3427 | if (mod[1] == '=') { |
3428 | /* | | 3428 | /* |
3429 | * XXX: This ad-hoc call to strcspn deviates from the usual | | 3429 | * XXX: This ad-hoc call to strcspn deviates from the usual |
3430 | * behavior defined in ParseModifierPart. This creates an | | 3430 | * behavior defined in ParseModifierPart. This creates an |
3431 | * unnecessary, undocumented inconsistency in make. | | 3431 | * unnecessary, undocumented inconsistency in make. |
3432 | */ | | 3432 | */ |
3433 | size_t n = strcspn(mod + 2, ":)}"); | | 3433 | const char *arg = mod + 2; |
3434 | *pp = mod + 2 + n; | | 3434 | size_t argLen = strcspn(arg, ":)}"); |
3435 | name = FStr_InitOwn(bmake_strldup(mod + 2, n)); | | 3435 | *pp = arg + argLen; |
| | | 3436 | name = FStr_InitOwn(bmake_strldup(arg, argLen)); |
3436 | } else | | 3437 | } else |
3437 | *pp = mod + 1; | | 3438 | *pp = mod + 1; |
3438 | | | 3439 | |
3439 | if (expr->eflags & VARE_WANTRES) | | 3440 | if (expr->eflags & VARE_WANTRES) |
3440 | Var_Set(expr->scope, name.str, expr->value.str); | | 3441 | Var_Set(expr->scope, name.str, expr->value.str); |
3441 | FStr_Done(&name); | | 3442 | FStr_Done(&name); |
3442 | | | 3443 | |
3443 | return AMR_OK; | | 3444 | return AMR_OK; |
3444 | } | | 3445 | } |
3445 | | | 3446 | |
3446 | /* | | 3447 | /* |
3447 | * Apply the given function to each word of the variable value, | | 3448 | * Apply the given function to each word of the variable value, |
3448 | * for a single-letter modifier such as :H, :T. | | 3449 | * for a single-letter modifier such as :H, :T. |
3449 | */ | | 3450 | */ |
3450 | static ApplyModifierResult | | 3451 | static ApplyModifierResult |
3451 | ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st, | | 3452 | ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st, |
3452 | ModifyWordProc modifyWord) | | 3453 | ModifyWordProc modifyWord) |
3453 | { | | 3454 | { |
3454 | if (!IsDelimiter((*pp)[1], st)) | | 3455 | if (!IsDelimiter((*pp)[1], st)) |
3455 | return AMR_UNKNOWN; | | 3456 | return AMR_UNKNOWN; |
3456 | (*pp)++; | | 3457 | (*pp)++; |
3457 | | | 3458 | |
3458 | ModifyWords(st, modifyWord, NULL, st->oneBigWord); | | 3459 | ModifyWords(st, modifyWord, NULL, st->oneBigWord); |
3459 | | | 3460 | |
3460 | return AMR_OK; | | 3461 | return AMR_OK; |
3461 | } | | 3462 | } |
3462 | | | 3463 | |
3463 | static ApplyModifierResult | | 3464 | static ApplyModifierResult |
3464 | ApplyModifier_Unique(const char **pp, ApplyModifiersState *st) | | 3465 | ApplyModifier_Unique(const char **pp, ApplyModifiersState *st) |
3465 | { | | 3466 | { |
3466 | if (!IsDelimiter((*pp)[1], st)) | | 3467 | if (!IsDelimiter((*pp)[1], st)) |
3467 | return AMR_UNKNOWN; | | 3468 | return AMR_UNKNOWN; |
3468 | (*pp)++; | | 3469 | (*pp)++; |
3469 | | | 3470 | |
3470 | Expr_SetValueOwn(st->expr, VarUniq(st->expr->value.str)); | | 3471 | Expr_SetValueOwn(st->expr, VarUniq(st->expr->value.str)); |
3471 | | | 3472 | |
3472 | return AMR_OK; | | 3473 | return AMR_OK; |
3473 | } | | 3474 | } |
3474 | | | 3475 | |
3475 | #ifdef SYSVVARSUB | | 3476 | #ifdef SYSVVARSUB |
3476 | /* :from=to */ | | 3477 | /* :from=to */ |
3477 | static ApplyModifierResult | | 3478 | static ApplyModifierResult |
3478 | ApplyModifier_SysV(const char **pp, ApplyModifiersState *st) | | 3479 | ApplyModifier_SysV(const char **pp, ApplyModifiersState *st) |
3479 | { | | 3480 | { |
3480 | Expr *expr = st->expr; | | 3481 | Expr *expr = st->expr; |
3481 | char *lhs, *rhs; | | 3482 | char *lhs, *rhs; |
3482 | VarParseResult res; | | 3483 | VarParseResult res; |
3483 | | | 3484 | |
3484 | const char *mod = *pp; | | 3485 | const char *mod = *pp; |
3485 | Boolean eqFound = FALSE; | | 3486 | Boolean eqFound = FALSE; |
3486 | | | 3487 | |
3487 | /* | | 3488 | /* |
3488 | * First we make a pass through the string trying to verify it is a | | 3489 | * First we make a pass through the string trying to verify it is a |
3489 | * SysV-make-style translation. It must be: <lhs>=<rhs> | | 3490 | * SysV-make-style translation. It must be: <lhs>=<rhs> |
3490 | */ | | 3491 | */ |
3491 | int depth = 1; | | 3492 | int depth = 1; |
3492 | const char *p = mod; | | 3493 | const char *p = mod; |
3493 | while (*p != '\0' && depth > 0) { | | 3494 | while (*p != '\0' && depth > 0) { |
3494 | if (*p == '=') { /* XXX: should also test depth == 1 */ | | 3495 | if (*p == '=') { /* XXX: should also test depth == 1 */ |
3495 | eqFound = TRUE; | | 3496 | eqFound = TRUE; |
3496 | /* continue looking for st->endc */ | | 3497 | /* continue looking for st->endc */ |
3497 | } else if (*p == st->endc) | | 3498 | } else if (*p == st->endc) |
3498 | depth--; | | 3499 | depth--; |
3499 | else if (*p == st->startc) | | 3500 | else if (*p == st->startc) |
3500 | depth++; | | 3501 | depth++; |
3501 | if (depth > 0) | | 3502 | if (depth > 0) |
3502 | p++; | | 3503 | p++; |
3503 | } | | 3504 | } |
3504 | if (*p != st->endc || !eqFound) | | 3505 | if (*p != st->endc || !eqFound) |
3505 | return AMR_UNKNOWN; | | 3506 | return AMR_UNKNOWN; |
3506 | | | 3507 | |
3507 | res = ParseModifierPart(pp, '=', expr->eflags, st, &lhs); | | 3508 | res = ParseModifierPart(pp, '=', expr->eflags, st, &lhs); |
3508 | if (res != VPR_OK) | | 3509 | if (res != VPR_OK) |
3509 | return AMR_CLEANUP; | | 3510 | return AMR_CLEANUP; |
3510 | | | 3511 | |
3511 | /* The SysV modifier lasts until the end of the variable expression. */ | | 3512 | /* The SysV modifier lasts until the end of the variable expression. */ |
3512 | res = ParseModifierPart(pp, st->endc, expr->eflags, st, &rhs); | | 3513 | res = ParseModifierPart(pp, st->endc, expr->eflags, st, &rhs); |
3513 | if (res != VPR_OK) | | 3514 | if (res != VPR_OK) |
3514 | return AMR_CLEANUP; | | 3515 | return AMR_CLEANUP; |
3515 | | | 3516 | |
3516 | (*pp)--; /* Go back to the st->endc. */ | | 3517 | (*pp)--; /* Go back to the st->endc. */ |
3517 | | | 3518 | |
3518 | if (lhs[0] == '\0' && expr->value.str[0] == '\0') { | | 3519 | if (lhs[0] == '\0' && expr->value.str[0] == '\0') { |
3519 | /* Do not turn an empty expression into non-empty. */ | | 3520 | /* Do not turn an empty expression into non-empty. */ |
3520 | } else { | | 3521 | } else { |
3521 | struct ModifyWord_SYSVSubstArgs args = { | | 3522 | struct ModifyWord_SYSVSubstArgs args = { |
3522 | expr->scope, lhs, rhs | | 3523 | expr->scope, lhs, rhs |
3523 | }; | | 3524 | }; |
3524 | ModifyWords(st, ModifyWord_SYSVSubst, &args, st->oneBigWord); | | 3525 | ModifyWords(st, ModifyWord_SYSVSubst, &args, st->oneBigWord); |
3525 | } | | 3526 | } |
3526 | free(lhs); | | 3527 | free(lhs); |
3527 | free(rhs); | | 3528 | free(rhs); |
3528 | return AMR_OK; | | 3529 | return AMR_OK; |
3529 | } | | 3530 | } |
3530 | #endif | | 3531 | #endif |
3531 | | | 3532 | |
3532 | #ifdef SUNSHCMD | | 3533 | #ifdef SUNSHCMD |
3533 | /* :sh */ | | 3534 | /* :sh */ |
3534 | static ApplyModifierResult | | 3535 | static ApplyModifierResult |
3535 | ApplyModifier_SunShell(const char **pp, ApplyModifiersState *st) | | 3536 | ApplyModifier_SunShell(const char **pp, ApplyModifiersState *st) |
3536 | { | | 3537 | { |
3537 | Expr *expr = st->expr; | | 3538 | Expr *expr = st->expr; |
3538 | const char *p = *pp; | | 3539 | const char *p = *pp; |
3539 | if (!(p[1] == 'h' && IsDelimiter(p[2], st))) | | 3540 | if (!(p[1] == 'h' && IsDelimiter(p[2], st))) |
3540 | return AMR_UNKNOWN; | | 3541 | return AMR_UNKNOWN; |
3541 | *pp = p + 2; | | 3542 | *pp = p + 2; |
3542 | | | 3543 | |
3543 | if (expr->eflags & VARE_WANTRES) { | | 3544 | if (expr->eflags & VARE_WANTRES) { |
3544 | const char *errfmt; | | 3545 | const char *errfmt; |
3545 | char *output = Cmd_Exec(expr->value.str, &errfmt); | | 3546 | char *output = Cmd_Exec(expr->value.str, &errfmt); |
3546 | if (errfmt != NULL) | | 3547 | if (errfmt != NULL) |
3547 | Error(errfmt, expr->value.str); | | 3548 | Error(errfmt, expr->value.str); |
3548 | Expr_SetValueOwn(expr, output); | | 3549 | Expr_SetValueOwn(expr, output); |
3549 | } else { | | 3550 | } else { |
3550 | /* | | 3551 | /* |
3551 | * TODO: Check whether returning ":sh" would be | | 3552 | * TODO: Check whether returning ":sh" would be |
3552 | * more consistent with the other modifiers. | | 3553 | * more consistent with the other modifiers. |
3553 | * | | 3554 | * |
3554 | * TODO: Add a unit test demonstrating that the | | 3555 | * TODO: Add a unit test demonstrating that the |
3555 | * actual value of this expression has any effect. | | 3556 | * actual value of this expression has any effect. |
3556 | */ | | 3557 | */ |
3557 | Expr_SetValueRefer(expr, ""); | | 3558 | Expr_SetValueRefer(expr, ""); |
3558 | } | | 3559 | } |
3559 | | | 3560 | |
3560 | return AMR_OK; | | 3561 | return AMR_OK; |
3561 | } | | 3562 | } |
3562 | #endif | | 3563 | #endif |
3563 | | | 3564 | |
3564 | static void | | 3565 | static void |
3565 | LogBeforeApply(const ApplyModifiersState *st, const char *mod) | | 3566 | LogBeforeApply(const ApplyModifiersState *st, const char *mod) |
3566 | { | | 3567 | { |
3567 | const Expr *expr = st->expr; | | 3568 | const Expr *expr = st->expr; |
3568 | char eflags_str[VarEvalFlags_ToStringSize]; | | 3569 | char eflags_str[VarEvalFlags_ToStringSize]; |
3569 | char vflags_str[VarFlags_ToStringSize]; | | 3570 | char vflags_str[VarFlags_ToStringSize]; |
3570 | Boolean is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], st); | | 3571 | Boolean is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], st); |
3571 | | | 3572 | |
3572 | /* At this point, only the first character of the modifier can | | 3573 | /* At this point, only the first character of the modifier can |
3573 | * be used since the end of the modifier is not yet known. */ | | 3574 | * be used since the end of the modifier is not yet known. */ |
3574 | debug_printf("Applying ${%s:%c%s} to \"%s\" (%s, %s, %s)\n", | | 3575 | debug_printf("Applying ${%s:%c%s} to \"%s\" (%s, %s, %s)\n", |
3575 | expr->var->name.str, mod[0], is_single_char ? "" : "...", | | 3576 | expr->var->name.str, mod[0], is_single_char ? "" : "...", |
3576 | expr->value.str, | | 3577 | expr->value.str, |
3577 | VarEvalFlags_ToString(eflags_str, expr->eflags), | | 3578 | VarEvalFlags_ToString(eflags_str, expr->eflags), |
3578 | VarFlags_ToString(vflags_str, expr->var->flags), | | 3579 | VarFlags_ToString(vflags_str, expr->var->flags), |
3579 | ExprDefined_Name[expr->defined]); | | 3580 | ExprDefined_Name[expr->defined]); |
3580 | } | | 3581 | } |
3581 | | | 3582 | |
3582 | static void | | 3583 | static void |
3583 | LogAfterApply(const ApplyModifiersState *st, const char *p, const char *mod) | | 3584 | LogAfterApply(const ApplyModifiersState *st, const char *p, const char *mod) |
3584 | { | | 3585 | { |
3585 | const Expr *expr = st->expr; | | 3586 | const Expr *expr = st->expr; |
3586 | const char *value = expr->value.str; | | 3587 | const char *value = expr->value.str; |
3587 | char eflags_str[VarEvalFlags_ToStringSize]; | | 3588 | char eflags_str[VarEvalFlags_ToStringSize]; |
3588 | char vflags_str[VarFlags_ToStringSize]; | | 3589 | char vflags_str[VarFlags_ToStringSize]; |
3589 | const char *quot = value == var_Error ? "" : "\""; | | 3590 | const char *quot = value == var_Error ? "" : "\""; |
3590 | | | 3591 | |
3591 | debug_printf("Result of ${%s:%.*s} is %s%s%s (%s, %s, %s)\n", | | 3592 | debug_printf("Result of ${%s:%.*s} is %s%s%s (%s, %s, %s)\n", |
3592 | expr->var->name.str, (int)(p - mod), mod, | | 3593 | expr->var->name.str, (int)(p - mod), mod, |
3593 | quot, value == var_Error ? "error" : value, quot, | | 3594 | quot, value == var_Error ? "error" : value, quot, |
3594 | VarEvalFlags_ToString(eflags_str, expr->eflags), | | 3595 | VarEvalFlags_ToString(eflags_str, expr->eflags), |
3595 | VarFlags_ToString(vflags_str, expr->var->flags), | | 3596 | VarFlags_ToString(vflags_str, expr->var->flags), |
3596 | ExprDefined_Name[expr->defined]); | | 3597 | ExprDefined_Name[expr->defined]); |
3597 | } | | 3598 | } |
3598 | | | 3599 | |
3599 | static ApplyModifierResult | | 3600 | static ApplyModifierResult |
3600 | ApplyModifier(const char **pp, ApplyModifiersState *st) | | 3601 | ApplyModifier(const char **pp, ApplyModifiersState *st) |
3601 | { | | 3602 | { |
3602 | switch (**pp) { | | 3603 | switch (**pp) { |
3603 | case '!': | | 3604 | case '!': |
3604 | return ApplyModifier_ShellCommand(pp, st); | | 3605 | return ApplyModifier_ShellCommand(pp, st); |
3605 | case ':': | | 3606 | case ':': |
3606 | return ApplyModifier_Assign(pp, st); | | 3607 | return ApplyModifier_Assign(pp, st); |
3607 | case '?': | | 3608 | case '?': |
3608 | return ApplyModifier_IfElse(pp, st); | | 3609 | return ApplyModifier_IfElse(pp, st); |
3609 | case '@': | | 3610 | case '@': |
3610 | return ApplyModifier_Loop(pp, st); | | 3611 | return ApplyModifier_Loop(pp, st); |
3611 | case '[': | | 3612 | case '[': |
3612 | return ApplyModifier_Words(pp, st); | | 3613 | return ApplyModifier_Words(pp, st); |
3613 | case '_': | | 3614 | case '_': |
3614 | return ApplyModifier_Remember(pp, st); | | 3615 | return ApplyModifier_Remember(pp, st); |
3615 | #ifndef NO_REGEX | | 3616 | #ifndef NO_REGEX |
3616 | case 'C': | | 3617 | case 'C': |
3617 | return ApplyModifier_Regex(pp, st); | | 3618 | return ApplyModifier_Regex(pp, st); |
3618 | #endif | | 3619 | #endif |
3619 | case 'D': | | 3620 | case 'D': |
3620 | return ApplyModifier_Defined(pp, st); | | 3621 | return ApplyModifier_Defined(pp, st); |
3621 | case 'E': | | 3622 | case 'E': |
3622 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Suffix); | | 3623 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Suffix); |
3623 | case 'g': | | 3624 | case 'g': |
3624 | return ApplyModifier_Gmtime(pp, st); | | 3625 | return ApplyModifier_Gmtime(pp, st); |
3625 | case 'H': | | 3626 | case 'H': |
3626 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Head); | | 3627 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Head); |
3627 | case 'h': | | 3628 | case 'h': |
3628 | return ApplyModifier_Hash(pp, st); | | 3629 | return ApplyModifier_Hash(pp, st); |
3629 | case 'L': | | 3630 | case 'L': |
3630 | return ApplyModifier_Literal(pp, st); | | 3631 | return ApplyModifier_Literal(pp, st); |
3631 | case 'l': | | 3632 | case 'l': |
3632 | return ApplyModifier_Localtime(pp, st); | | 3633 | return ApplyModifier_Localtime(pp, st); |
3633 | case 'M': | | 3634 | case 'M': |
3634 | case 'N': | | 3635 | case 'N': |
3635 | return ApplyModifier_Match(pp, st); | | 3636 | return ApplyModifier_Match(pp, st); |
3636 | case 'O': | | 3637 | case 'O': |
3637 | return ApplyModifier_Order(pp, st); | | 3638 | return ApplyModifier_Order(pp, st); |
3638 | case 'P': | | 3639 | case 'P': |
3639 | return ApplyModifier_Path(pp, st); | | 3640 | return ApplyModifier_Path(pp, st); |
3640 | case 'Q': | | 3641 | case 'Q': |
3641 | case 'q': | | 3642 | case 'q': |
3642 | return ApplyModifier_Quote(pp, st); | | 3643 | return ApplyModifier_Quote(pp, st); |
3643 | case 'R': | | 3644 | case 'R': |
3644 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Root); | | 3645 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Root); |
3645 | case 'r': | | 3646 | case 'r': |
3646 | return ApplyModifier_Range(pp, st); | | 3647 | return ApplyModifier_Range(pp, st); |
3647 | case 'S': | | 3648 | case 'S': |
3648 | return ApplyModifier_Subst(pp, st); | | 3649 | return ApplyModifier_Subst(pp, st); |
3649 | #ifdef SUNSHCMD | | 3650 | #ifdef SUNSHCMD |
3650 | case 's': | | 3651 | case 's': |
3651 | return ApplyModifier_SunShell(pp, st); | | 3652 | return ApplyModifier_SunShell(pp, st); |
3652 | #endif | | 3653 | #endif |
3653 | case 'T': | | 3654 | case 'T': |
3654 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Tail); | | 3655 | return ApplyModifier_WordFunc(pp, st, ModifyWord_Tail); |
3655 | case 't': | | 3656 | case 't': |
3656 | return ApplyModifier_To(pp, st); | | 3657 | return ApplyModifier_To(pp, st); |
3657 | case 'U': | | 3658 | case 'U': |
3658 | return ApplyModifier_Defined(pp, st); | | 3659 | return ApplyModifier_Defined(pp, st); |
3659 | case 'u': | | 3660 | case 'u': |
3660 | return ApplyModifier_Unique(pp, st); | | 3661 | return ApplyModifier_Unique(pp, st); |
3661 | default: | | 3662 | default: |
3662 | return AMR_UNKNOWN; | | 3663 | return AMR_UNKNOWN; |
3663 | } | | 3664 | } |
3664 | } | | 3665 | } |
3665 | | | 3666 | |
3666 | static void ApplyModifiers(Expr *, const char **, char, char); | | 3667 | static void ApplyModifiers(Expr *, const char **, char, char); |
3667 | | | 3668 | |
3668 | typedef enum ApplyModifiersIndirectResult { | | 3669 | typedef enum ApplyModifiersIndirectResult { |
3669 | /* The indirect modifiers have been applied successfully. */ | | 3670 | /* The indirect modifiers have been applied successfully. */ |
3670 | AMIR_CONTINUE, | | 3671 | AMIR_CONTINUE, |
3671 | /* Fall back to the SysV modifier. */ | | 3672 | /* Fall back to the SysV modifier. */ |
3672 | AMIR_SYSV, | | 3673 | AMIR_SYSV, |
3673 | /* Error out. */ | | 3674 | /* Error out. */ |
3674 | AMIR_OUT | | 3675 | AMIR_OUT |
3675 | } ApplyModifiersIndirectResult; | | 3676 | } ApplyModifiersIndirectResult; |
3676 | | | 3677 | |
3677 | /* | | 3678 | /* |
3678 | * While expanding a variable expression, expand and apply indirect modifiers, | | 3679 | * While expanding a variable expression, expand and apply indirect modifiers, |
3679 | * such as in ${VAR:${M_indirect}}. | | 3680 | * such as in ${VAR:${M_indirect}}. |
3680 | * | | 3681 | * |
3681 | * All indirect modifiers of a group must come from a single variable | | 3682 | * All indirect modifiers of a group must come from a single variable |
3682 | * expression. ${VAR:${M1}} is valid but ${VAR:${M1}${M2}} is not. | | 3683 | * expression. ${VAR:${M1}} is valid but ${VAR:${M1}${M2}} is not. |
3683 | * | | 3684 | * |
3684 | * Multiple groups of indirect modifiers can be chained by separating them | | 3685 | * Multiple groups of indirect modifiers can be chained by separating them |
3685 | * with colons. ${VAR:${M1}:${M2}} contains 2 indirect modifiers. | | 3686 | * with colons. ${VAR:${M1}:${M2}} contains 2 indirect modifiers. |
3686 | * | | 3687 | * |
3687 | * If the variable expression is not followed by st->endc or ':', fall | | 3688 | * If the variable expression is not followed by st->endc or ':', fall |
3688 | * back to trying the SysV modifier, such as in ${VAR:${FROM}=${TO}}. | | 3689 | * back to trying the SysV modifier, such as in ${VAR:${FROM}=${TO}}. |
3689 | */ | | 3690 | */ |
3690 | static ApplyModifiersIndirectResult | | 3691 | static ApplyModifiersIndirectResult |
3691 | ApplyModifiersIndirect(ApplyModifiersState *st, const char **pp) | | 3692 | ApplyModifiersIndirect(ApplyModifiersState *st, const char **pp) |
3692 | { | | 3693 | { |
3693 | Expr *expr = st->expr; | | 3694 | Expr *expr = st->expr; |
3694 | const char *p = *pp; | | 3695 | const char *p = *pp; |
3695 | FStr mods; | | 3696 | FStr mods; |
3696 | | | 3697 | |
3697 | (void)Var_Parse(&p, expr->scope, expr->eflags, &mods); | | 3698 | (void)Var_Parse(&p, expr->scope, expr->eflags, &mods); |
3698 | /* TODO: handle errors */ | | 3699 | /* TODO: handle errors */ |
3699 | | | 3700 | |
3700 | if (mods.str[0] != '\0' && *p != '\0' && !IsDelimiter(*p, st)) { | | 3701 | if (mods.str[0] != '\0' && *p != '\0' && !IsDelimiter(*p, st)) { |
3701 | FStr_Done(&mods); | | 3702 | FStr_Done(&mods); |
3702 | return AMIR_SYSV; | | 3703 | return AMIR_SYSV; |
3703 | } | | 3704 | } |
3704 | | | 3705 | |
3705 | DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n", | | 3706 | DEBUG3(VAR, "Indirect modifier \"%s\" from \"%.*s\"\n", |
3706 | mods.str, (int)(p - *pp), *pp); | | 3707 | mods.str, (int)(p - *pp), *pp); |
3707 | | | 3708 | |
3708 | if (mods.str[0] != '\0') { | | 3709 | if (mods.str[0] != '\0') { |
3709 | const char *modsp = mods.str; | | 3710 | const char *modsp = mods.str; |
3710 | ApplyModifiers(expr, &modsp, '\0', '\0'); | | 3711 | ApplyModifiers(expr, &modsp, '\0', '\0'); |
3711 | if (expr->value.str == var_Error || *modsp != '\0') { | | 3712 | if (expr->value.str == var_Error || *modsp != '\0') { |
3712 | FStr_Done(&mods); | | 3713 | FStr_Done(&mods); |
3713 | *pp = p; | | 3714 | *pp = p; |
3714 | return AMIR_OUT; /* error already reported */ | | 3715 | return AMIR_OUT; /* error already reported */ |
3715 | } | | 3716 | } |
3716 | } | | 3717 | } |
3717 | FStr_Done(&mods); | | 3718 | FStr_Done(&mods); |
3718 | | | 3719 | |
3719 | if (*p == ':') | | 3720 | if (*p == ':') |
3720 | p++; | | 3721 | p++; |
3721 | else if (*p == '\0' && st->endc != '\0') { | | 3722 | else if (*p == '\0' && st->endc != '\0') { |
3722 | Error("Unclosed variable expression after indirect " | | 3723 | Error("Unclosed variable expression after indirect " |
3723 | "modifier, expecting '%c' for variable \"%s\"", | | 3724 | "modifier, expecting '%c' for variable \"%s\"", |
3724 | st->endc, expr->var->name.str); | | 3725 | st->endc, expr->var->name.str); |
3725 | *pp = p; | | 3726 | *pp = p; |
3726 | return AMIR_OUT; | | 3727 | return AMIR_OUT; |
3727 | } | | 3728 | } |
3728 | | | 3729 | |
3729 | *pp = p; | | 3730 | *pp = p; |
3730 | return AMIR_CONTINUE; | | 3731 | return AMIR_CONTINUE; |
3731 | } | | 3732 | } |
3732 | | | 3733 | |
3733 | static ApplyModifierResult | | 3734 | static ApplyModifierResult |
3734 | ApplySingleModifier(const char **pp, ApplyModifiersState *st) | | 3735 | ApplySingleModifier(const char **pp, ApplyModifiersState *st) |
3735 | { | | 3736 | { |
3736 | ApplyModifierResult res; | | 3737 | ApplyModifierResult res; |
3737 | const char *mod = *pp; | | 3738 | const char *mod = *pp; |
3738 | const char *p = *pp; | | 3739 | const char *p = *pp; |
3739 | | | 3740 | |
3740 | if (DEBUG(VAR)) | | 3741 | if (DEBUG(VAR)) |
3741 | LogBeforeApply(st, mod); | | 3742 | LogBeforeApply(st, mod); |
3742 | | | 3743 | |
3743 | res = ApplyModifier(&p, st); | | 3744 | res = ApplyModifier(&p, st); |
3744 | | | 3745 | |
3745 | #ifdef SYSVVARSUB | | 3746 | #ifdef SYSVVARSUB |
3746 | if (res == AMR_UNKNOWN) { | | 3747 | if (res == AMR_UNKNOWN) { |
3747 | assert(p == mod); | | 3748 | assert(p == mod); |
3748 | res = ApplyModifier_SysV(&p, st); | | 3749 | res = ApplyModifier_SysV(&p, st); |
3749 | } | | 3750 | } |
3750 | #endif | | 3751 | #endif |
3751 | | | 3752 | |
3752 | if (res == AMR_UNKNOWN) { | | 3753 | if (res == AMR_UNKNOWN) { |
3753 | /* | | 3754 | /* |
3754 | * Guess the end of the current modifier. | | 3755 | * Guess the end of the current modifier. |
3755 | * XXX: Skipping the rest of the modifier hides | | 3756 | * XXX: Skipping the rest of the modifier hides |
3756 | * errors and leads to wrong results. | | 3757 | * errors and leads to wrong results. |
3757 | * Parsing should rather stop here. | | 3758 | * Parsing should rather stop here. |
3758 | */ | | 3759 | */ |
3759 | for (p++; !IsDelimiter(*p, st) && *p != '\0'; p++) | | 3760 | for (p++; !IsDelimiter(*p, st) && *p != '\0'; p++) |
3760 | continue; | | 3761 | continue; |
3761 | Parse_Error(PARSE_FATAL, "Unknown modifier \"%.*s\"", | | 3762 | Parse_Error(PARSE_FATAL, "Unknown modifier \"%.*s\"", |
3762 | (int)(p - mod), mod); | | 3763 | (int)(p - mod), mod); |
3763 | Expr_SetValueRefer(st->expr, var_Error); | | 3764 | Expr_SetValueRefer(st->expr, var_Error); |
3764 | } | | 3765 | } |
3765 | if (res == AMR_CLEANUP || res == AMR_BAD) { | | 3766 | if (res == AMR_CLEANUP || res == AMR_BAD) { |
3766 | *pp = p; | | 3767 | *pp = p; |
3767 | return res; | | 3768 | return res; |
3768 | } | | 3769 | } |
3769 | | | 3770 | |
3770 | if (DEBUG(VAR)) | | 3771 | if (DEBUG(VAR)) |
3771 | LogAfterApply(st, p, mod); | | 3772 | LogAfterApply(st, p, mod); |
3772 | | | 3773 | |
3773 | if (*p == '\0' && st->endc != '\0') { | | 3774 | if (*p == '\0' && st->endc != '\0') { |
3774 | Error( | | 3775 | Error( |
3775 | "Unclosed variable expression, expecting '%c' for " | | 3776 | "Unclosed variable expression, expecting '%c' for " |
3776 | "modifier \"%.*s\" of variable \"%s\" with value \"%s\"", | | 3777 | "modifier \"%.*s\" of variable \"%s\" with value \"%s\"", |
3777 | st->endc, | | 3778 | st->endc, |
3778 | (int)(p - mod), mod, | | 3779 | (int)(p - mod), mod, |
3779 | st->expr->var->name.str, st->expr->value.str); | | 3780 | st->expr->var->name.str, st->expr->value.str); |
3780 | } else if (*p == ':') { | | 3781 | } else if (*p == ':') { |
3781 | p++; | | 3782 | p++; |
3782 | } else if (opts.strict && *p != '\0' && *p != st->endc) { | | 3783 | } else if (opts.strict && *p != '\0' && *p != st->endc) { |
3783 | Parse_Error(PARSE_FATAL, | | 3784 | Parse_Error(PARSE_FATAL, |
3784 | "Missing delimiter ':' after modifier \"%.*s\"", | | 3785 | "Missing delimiter ':' after modifier \"%.*s\"", |
3785 | (int)(p - mod), mod); | | 3786 | (int)(p - mod), mod); |
3786 | /* | | 3787 | /* |
3787 | * TODO: propagate parse error to the enclosing | | 3788 | * TODO: propagate parse error to the enclosing |
3788 | * expression | | 3789 | * expression |
3789 | */ | | 3790 | */ |
3790 | } | | 3791 | } |
3791 | *pp = p; | | 3792 | *pp = p; |
3792 | return AMR_OK; | | 3793 | return AMR_OK; |
3793 | } | | 3794 | } |
3794 | | | 3795 | |
3795 | /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */ | | 3796 | /* Apply any modifiers (such as :Mpattern or :@var@loop@ or :Q or ::=value). */ |
3796 | static void | | 3797 | static void |
3797 | ApplyModifiers( | | 3798 | ApplyModifiers( |
3798 | Expr *expr, | | 3799 | Expr *expr, |
3799 | const char **pp, /* the parsing position, updated upon return */ | | 3800 | const char **pp, /* the parsing position, updated upon return */ |
3800 | char startc, /* '(' or '{'; or '\0' for indirect modifiers */ | | 3801 | char startc, /* '(' or '{'; or '\0' for indirect modifiers */ |
3801 | char endc /* ')' or '}'; or '\0' for indirect modifiers */ | | 3802 | char endc /* ')' or '}'; or '\0' for indirect modifiers */ |
3802 | ) | | 3803 | ) |
3803 | { | | 3804 | { |
3804 | ApplyModifiersState st = { | | 3805 | ApplyModifiersState st = { |
3805 | expr, | | 3806 | expr, |
3806 | startc, | | 3807 | startc, |
3807 | endc, | | 3808 | endc, |
3808 | ' ', /* .sep */ | | 3809 | ' ', /* .sep */ |
3809 | FALSE /* .oneBigWord */ | | 3810 | FALSE /* .oneBigWord */ |
3810 | }; | | 3811 | }; |
3811 | const char *p; | | 3812 | const char *p; |
3812 | const char *mod; | | 3813 | const char *mod; |
3813 | | | 3814 | |
3814 | assert(startc == '(' || startc == '{' || startc == '\0'); | | 3815 | assert(startc == '(' || startc == '{' || startc == '\0'); |
3815 | assert(endc == ')' || endc == '}' || endc == '\0'); | | 3816 | assert(endc == ')' || endc == '}' || endc == '\0'); |
3816 | assert(expr->value.str != NULL); | | 3817 | assert(expr->value.str != NULL); |
3817 | | | 3818 | |
3818 | p = *pp; | | 3819 | p = *pp; |
3819 | | | 3820 | |
3820 | if (*p == '\0' && endc != '\0') { | | 3821 | if (*p == '\0' && endc != '\0') { |
3821 | Error( | | 3822 | Error( |
3822 | "Unclosed variable expression (expecting '%c') for \"%s\"", | | 3823 | "Unclosed variable expression (expecting '%c') for \"%s\"", |
3823 | st.endc, expr->var->name.str); | | 3824 | st.endc, expr->var->name.str); |
3824 | goto cleanup; | | 3825 | goto cleanup; |
3825 | } | | 3826 | } |
3826 | | | 3827 | |
3827 | while (*p != '\0' && *p != endc) { | | 3828 | while (*p != '\0' && *p != endc) { |
3828 | ApplyModifierResult res; | | 3829 | ApplyModifierResult res; |
3829 | | | 3830 | |
3830 | if (*p == '$') { | | 3831 | if (*p == '$') { |
3831 | ApplyModifiersIndirectResult amir = | | 3832 | ApplyModifiersIndirectResult amir = |
3832 | ApplyModifiersIndirect(&st, &p); | | 3833 | ApplyModifiersIndirect(&st, &p); |
3833 | if (amir == AMIR_CONTINUE) | | 3834 | if (amir == AMIR_CONTINUE) |
3834 | continue; | | 3835 | continue; |
3835 | if (amir == AMIR_OUT) | | 3836 | if (amir == AMIR_OUT) |
3836 | break; | | 3837 | break; |
3837 | /* | | 3838 | /* |
3838 | * It's neither '${VAR}:' nor '${VAR}}'. Try to parse | | 3839 | * It's neither '${VAR}:' nor '${VAR}}'. Try to parse |
3839 | * it as a SysV modifier, as that is the only modifier | | 3840 | * it as a SysV modifier, as that is the only modifier |
3840 | * that can start with '$'. | | 3841 | * that can start with '$'. |
3841 | */ | | 3842 | */ |
3842 | } | | 3843 | } |
3843 | | | 3844 | |
3844 | mod = p; | | 3845 | mod = p; |
3845 | | | 3846 | |
3846 | res = ApplySingleModifier(&p, &st); | | 3847 | res = ApplySingleModifier(&p, &st); |
3847 | if (res == AMR_CLEANUP) | | 3848 | if (res == AMR_CLEANUP) |
3848 | goto cleanup; | | 3849 | goto cleanup; |
3849 | if (res == AMR_BAD) | | 3850 | if (res == AMR_BAD) |
3850 | goto bad_modifier; | | 3851 | goto bad_modifier; |
3851 | } | | 3852 | } |
3852 | | | 3853 | |
3853 | *pp = p; | | 3854 | *pp = p; |
3854 | assert(expr->value.str != NULL); /* Use var_Error or varUndefined. */ | | 3855 | assert(expr->value.str != NULL); /* Use var_Error or varUndefined. */ |
3855 | return; | | 3856 | return; |
3856 | | | 3857 | |
3857 | bad_modifier: | | 3858 | bad_modifier: |
3858 | /* XXX: The modifier end is only guessed. */ | | 3859 | /* XXX: The modifier end is only guessed. */ |
3859 | Error("Bad modifier \":%.*s\" for variable \"%s\"", | | 3860 | Error("Bad modifier \":%.*s\" for variable \"%s\"", |
3860 | (int)strcspn(mod, ":)}"), mod, expr->var->name.str); | | 3861 | (int)strcspn(mod, ":)}"), mod, expr->var->name.str); |
3861 | | | 3862 | |
3862 | cleanup: | | 3863 | cleanup: |
3863 | /* | | 3864 | /* |
3864 | * TODO: Use p + strlen(p) instead, to stop parsing immediately. | | 3865 | * TODO: Use p + strlen(p) instead, to stop parsing immediately. |
3865 | * | | 3866 | * |
3866 | * In the unit tests, this generates a few unterminated strings in the | | 3867 | * In the unit tests, this generates a few unterminated strings in the |
3867 | * shell commands though. Instead of producing these unfinished | | 3868 | * shell commands though. Instead of producing these unfinished |
3868 | * strings, commands with evaluation errors should not be run at all. | | 3869 | * strings, commands with evaluation errors should not be run at all. |
3869 | * | | 3870 | * |
3870 | * To make that happen, Var_Subst must report the actual errors | | 3871 | * To make that happen, Var_Subst must report the actual errors |
3871 | * instead of returning VPR_OK unconditionally. | | 3872 | * instead of returning VPR_OK unconditionally. |
3872 | */ | | 3873 | */ |
3873 | *pp = p; | | 3874 | *pp = p; |
3874 | Expr_SetValueRefer(expr, var_Error); | | 3875 | Expr_SetValueRefer(expr, var_Error); |
3875 | } | | 3876 | } |
3876 | | | 3877 | |
3877 | /* | | 3878 | /* |
3878 | * Only four of the local variables are treated specially as they are the | | 3879 | * Only four of the local variables are treated specially as they are the |
3879 | * only four that will be set when dynamic sources are expanded. | | 3880 | * only four that will be set when dynamic sources are expanded. |
3880 | */ | | 3881 | */ |
3881 | static Boolean | | 3882 | static Boolean |
3882 | VarnameIsDynamic(const char *name, size_t len) | | 3883 | VarnameIsDynamic(const char *name, size_t len) |
3883 | { | | 3884 | { |
3884 | if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) { | | 3885 | if (len == 1 || (len == 2 && (name[1] == 'F' || name[1] == 'D'))) { |
3885 | switch (name[0]) { | | 3886 | switch (name[0]) { |
3886 | case '@': | | 3887 | case '@': |
3887 | case '%': | | 3888 | case '%': |
3888 | case '*': | | 3889 | case '*': |
3889 | case '!': | | 3890 | case '!': |
3890 | return TRUE; | | 3891 | return TRUE; |
3891 | } | | 3892 | } |
3892 | return FALSE; | | 3893 | return FALSE; |
3893 | } | | 3894 | } |
3894 | | | 3895 | |
3895 | if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) { | | 3896 | if ((len == 7 || len == 8) && name[0] == '.' && ch_isupper(name[1])) { |
3896 | return strcmp(name, ".TARGET") == 0 || | | 3897 | return strcmp(name, ".TARGET") == 0 || |
3897 | strcmp(name, ".ARCHIVE") == 0 || | | 3898 | strcmp(name, ".ARCHIVE") == 0 || |
3898 | strcmp(name, ".PREFIX") == 0 || | | 3899 | strcmp(name, ".PREFIX") == 0 || |
3899 | strcmp(name, ".MEMBER") == 0; | | 3900 | strcmp(name, ".MEMBER") == 0; |
3900 | } | | 3901 | } |
3901 | | | 3902 | |
3902 | return FALSE; | | 3903 | return FALSE; |
3903 | } | | 3904 | } |
3904 | | | 3905 | |
3905 | static const char * | | 3906 | static const char * |
3906 | UndefinedShortVarValue(char varname, const GNode *scope) | | 3907 | UndefinedShortVarValue(char varname, const GNode *scope) |
3907 | { | | 3908 | { |
3908 | if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) { | | 3909 | if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) { |
3909 | /* | | 3910 | /* |
3910 | * If substituting a local variable in a non-local scope, | | 3911 | * If substituting a local variable in a non-local scope, |
3911 | * assume it's for dynamic source stuff. We have to handle | | 3912 | * assume it's for dynamic source stuff. We have to handle |
3912 | * this specially and return the longhand for the variable | | 3913 | * this specially and return the longhand for the variable |
3913 | * with the dollar sign escaped so it makes it back to the | | 3914 | * with the dollar sign escaped so it makes it back to the |
3914 | * caller. Only four of the local variables are treated | | 3915 | * caller. Only four of the local variables are treated |
3915 | * specially as they are the only four that will be set | | 3916 | * specially as they are the only four that will be set |
3916 | * when dynamic sources are expanded. | | 3917 | * when dynamic sources are expanded. |
3917 | */ | | 3918 | */ |
3918 | switch (varname) { | | 3919 | switch (varname) { |
3919 | case '@': | | 3920 | case '@': |
3920 | return "$(.TARGET)"; | | 3921 | return "$(.TARGET)"; |
3921 | case '%': | | 3922 | case '%': |
3922 | return "$(.MEMBER)"; | | 3923 | return "$(.MEMBER)"; |
3923 | case '*': | | 3924 | case '*': |
3924 | return "$(.PREFIX)"; | | 3925 | return "$(.PREFIX)"; |
3925 | case '!': | | 3926 | case '!': |
3926 | return "$(.ARCHIVE)"; | | 3927 | return "$(.ARCHIVE)"; |
3927 | } | | 3928 | } |
3928 | } | | 3929 | } |
3929 | return NULL; | | 3930 | return NULL; |
3930 | } | | 3931 | } |
3931 | | | 3932 | |
3932 | /* | | 3933 | /* |
3933 | * Parse a variable name, until the end character or a colon, whichever | | 3934 | * Parse a variable name, until the end character or a colon, whichever |
3934 | * comes first. | | 3935 | * comes first. |
3935 | */ | | 3936 | */ |
3936 | static char * | | 3937 | static char * |
3937 | ParseVarname(const char **pp, char startc, char endc, | | 3938 | ParseVarname(const char **pp, char startc, char endc, |
3938 | GNode *scope, VarEvalFlags eflags, | | 3939 | GNode *scope, VarEvalFlags eflags, |
3939 | size_t *out_varname_len) | | 3940 | size_t *out_varname_len) |
3940 | { | | 3941 | { |
3941 | Buffer buf; | | 3942 | Buffer buf; |
3942 | const char *p = *pp; | | 3943 | const char *p = *pp; |
3943 | int depth = 0; /* Track depth so we can spot parse errors. */ | | 3944 | int depth = 0; /* Track depth so we can spot parse errors. */ |
3944 | | | 3945 | |
3945 | Buf_Init(&buf); | | 3946 | Buf_Init(&buf); |
3946 | | | 3947 | |
3947 | while (*p != '\0') { | | 3948 | while (*p != '\0') { |
3948 | if ((*p == endc || *p == ':') && depth == 0) | | 3949 | if ((*p == endc || *p == ':') && depth == 0) |
3949 | break; | | 3950 | break; |
3950 | if (*p == startc) | | 3951 | if (*p == startc) |
3951 | depth++; | | 3952 | depth++; |
3952 | if (*p == endc) | | 3953 | if (*p == endc) |
3953 | depth--; | | 3954 | depth--; |
3954 | | | 3955 | |
3955 | /* A variable inside a variable, expand. */ | | 3956 | /* A variable inside a variable, expand. */ |
3956 | if (*p == '$') { | | 3957 | if (*p == '$') { |
3957 | FStr nested_val; | | 3958 | FStr nested_val; |
3958 | (void)Var_Parse(&p, scope, eflags, &nested_val); | | 3959 | (void)Var_Parse(&p, scope, eflags, &nested_val); |
3959 | /* TODO: handle errors */ | | 3960 | /* TODO: handle errors */ |
3960 | Buf_AddStr(&buf, nested_val.str); | | 3961 | Buf_AddStr(&buf, nested_val.str); |
3961 | FStr_Done(&nested_val); | | 3962 | FStr_Done(&nested_val); |
3962 | } else { | | 3963 | } else { |
3963 | Buf_AddByte(&buf, *p); | | 3964 | Buf_AddByte(&buf, *p); |
3964 | p++; | | 3965 | p++; |
3965 | } | | 3966 | } |
3966 | } | | 3967 | } |
3967 | *pp = p; | | 3968 | *pp = p; |
3968 | *out_varname_len = buf.len; | | 3969 | *out_varname_len = buf.len; |
3969 | return Buf_DoneData(&buf); | | 3970 | return Buf_DoneData(&buf); |
3970 | } | | 3971 | } |
3971 | | | 3972 | |
3972 | static VarParseResult | | 3973 | static VarParseResult |
3973 | ValidShortVarname(char varname, const char *start) | | 3974 | ValidShortVarname(char varname, const char *start) |
3974 | { | | 3975 | { |
3975 | if (varname != '$' && varname != ':' && varname != '}' && | | 3976 | if (varname != '$' && varname != ':' && varname != '}' && |
3976 | varname != ')' && varname != '\0') | | 3977 | varname != ')' && varname != '\0') |
3977 | return VPR_OK; | | 3978 | return VPR_OK; |
3978 | | | 3979 | |
3979 | if (!opts.strict) | | 3980 | if (!opts.strict) |
3980 | return VPR_ERR; /* XXX: Missing error message */ | | 3981 | return VPR_ERR; /* XXX: Missing error message */ |
3981 | | | 3982 | |
3982 | if (varname == '$') | | 3983 | if (varname == '$') |
3983 | Parse_Error(PARSE_FATAL, | | 3984 | Parse_Error(PARSE_FATAL, |
3984 | "To escape a dollar, use \\$, not $$, at \"%s\"", start); | | 3985 | "To escape a dollar, use \\$, not $$, at \"%s\"", start); |
3985 | else if (varname == '\0') | | 3986 | else if (varname == '\0') |
3986 | Parse_Error(PARSE_FATAL, "Dollar followed by nothing"); | | 3987 | Parse_Error(PARSE_FATAL, "Dollar followed by nothing"); |
3987 | else | | 3988 | else |
3988 | Parse_Error(PARSE_FATAL, | | 3989 | Parse_Error(PARSE_FATAL, |
3989 | "Invalid variable name '%c', at \"%s\"", varname, start); | | 3990 | "Invalid variable name '%c', at \"%s\"", varname, start); |
3990 | | | 3991 | |
3991 | return VPR_ERR; | | 3992 | return VPR_ERR; |
3992 | } | | 3993 | } |
3993 | | | 3994 | |
3994 | /* | | 3995 | /* |
3995 | * Parse a single-character variable name such as in $V or $@. | | 3996 | * Parse a single-character variable name such as in $V or $@. |
3996 | * Return whether to continue parsing. | | 3997 | * Return whether to continue parsing. |
3997 | */ | | 3998 | */ |
3998 | static Boolean | | 3999 | static Boolean |
3999 | ParseVarnameShort(char varname, const char **pp, GNode *scope, | | 4000 | ParseVarnameShort(char varname, const char **pp, GNode *scope, |
4000 | VarEvalFlags eflags, | | 4001 | VarEvalFlags eflags, |
4001 | VarParseResult *out_FALSE_res, const char **out_FALSE_val, | | 4002 | VarParseResult *out_FALSE_res, const char **out_FALSE_val, |
4002 | Var **out_TRUE_var) | | 4003 | Var **out_TRUE_var) |
4003 | { | | 4004 | { |
4004 | char name[2]; | | 4005 | char name[2]; |
4005 | Var *v; | | 4006 | Var *v; |
4006 | VarParseResult vpr; | | 4007 | VarParseResult vpr; |
4007 | | | 4008 | |
4008 | vpr = ValidShortVarname(varname, *pp); | | 4009 | vpr = ValidShortVarname(varname, *pp); |
4009 | if (vpr != VPR_OK) { | | 4010 | if (vpr != VPR_OK) { |
4010 | (*pp)++; | | 4011 | (*pp)++; |
4011 | *out_FALSE_res = vpr; | | 4012 | *out_FALSE_res = vpr; |
4012 | *out_FALSE_val = var_Error; | | 4013 | *out_FALSE_val = var_Error; |
4013 | return FALSE; | | 4014 | return FALSE; |
4014 | } | | 4015 | } |
4015 | | | 4016 | |
4016 | name[0] = varname; | | 4017 | name[0] = varname; |
4017 | name[1] = '\0'; | | 4018 | name[1] = '\0'; |
4018 | v = VarFind(name, scope, TRUE); | | 4019 | v = VarFind(name, scope, TRUE); |
4019 | if (v == NULL) { | | 4020 | if (v == NULL) { |
4020 | const char *val; | | 4021 | const char *val; |
4021 | *pp += 2; | | 4022 | *pp += 2; |
4022 | | | 4023 | |
4023 | val = UndefinedShortVarValue(varname, scope); | | 4024 | val = UndefinedShortVarValue(varname, scope); |
4024 | if (val == NULL) | | 4025 | if (val == NULL) |
4025 | val = eflags & VARE_UNDEFERR ? var_Error : varUndefined; | | 4026 | val = eflags & VARE_UNDEFERR ? var_Error : varUndefined; |
4026 | | | 4027 | |
4027 | if (opts.strict && val == var_Error) { | | 4028 | if (opts.strict && val == var_Error) { |
4028 | Parse_Error(PARSE_FATAL, | | 4029 | Parse_Error(PARSE_FATAL, |
4029 | "Variable \"%s\" is undefined", name); | | 4030 | "Variable \"%s\" is undefined", name); |
4030 | *out_FALSE_res = VPR_ERR; | | 4031 | *out_FALSE_res = VPR_ERR; |
4031 | *out_FALSE_val = val; | | 4032 | *out_FALSE_val = val; |
4032 | return FALSE; | | 4033 | return FALSE; |
4033 | } | | 4034 | } |
4034 | | | 4035 | |
4035 | /* | | 4036 | /* |
4036 | * XXX: This looks completely wrong. | | 4037 | * XXX: This looks completely wrong. |
4037 | * | | 4038 | * |
4038 | * If undefined expressions are not allowed, this should | | 4039 | * If undefined expressions are not allowed, this should |
4039 | * rather be VPR_ERR instead of VPR_UNDEF, together with an | | 4040 | * rather be VPR_ERR instead of VPR_UNDEF, together with an |
4040 | * error message. | | 4041 | * error message. |
4041 | * | | 4042 | * |
4042 | * If undefined expressions are allowed, this should rather | | 4043 | * If undefined expressions are allowed, this should rather |
4043 | * be VPR_UNDEF instead of VPR_OK. | | 4044 | * be VPR_UNDEF instead of VPR_OK. |
4044 | */ | | 4045 | */ |
4045 | *out_FALSE_res = eflags & VARE_UNDEFERR ? VPR_UNDEF : VPR_OK; | | 4046 | *out_FALSE_res = eflags & VARE_UNDEFERR ? VPR_UNDEF : VPR_OK; |
4046 | *out_FALSE_val = val; | | 4047 | *out_FALSE_val = val; |
4047 | return FALSE; | | 4048 | return FALSE; |
4048 | } | | 4049 | } |
4049 | | | 4050 | |
4050 | *out_TRUE_var = v; | | 4051 | *out_TRUE_var = v; |
4051 | return TRUE; | | 4052 | return TRUE; |
4052 | } | | 4053 | } |
4053 | | | 4054 | |
4054 | /* Find variables like @F or <D. */ | | 4055 | /* Find variables like @F or <D. */ |
4055 | static Var * | | 4056 | static Var * |
4056 | FindLocalLegacyVar(const char *varname, size_t namelen, GNode *scope, | | 4057 | FindLocalLegacyVar(const char *varname, size_t namelen, GNode *scope, |
4057 | const char **out_extraModifiers) | | 4058 | const char **out_extraModifiers) |
4058 | { | | 4059 | { |
4059 | /* Only resolve these variables if scope is a "real" target. */ | | 4060 | /* Only resolve these variables if scope is a "real" target. */ |
4060 | if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) | | 4061 | if (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL) |
4061 | return NULL; | | 4062 | return NULL; |
4062 | | | 4063 | |
4063 | if (namelen != 2) | | 4064 | if (namelen != 2) |
4064 | return NULL; | | 4065 | return NULL; |
4065 | if (varname[1] != 'F' && varname[1] != 'D') | | 4066 | if (varname[1] != 'F' && varname[1] != 'D') |
4066 | return NULL; | | 4067 | return NULL; |
4067 | if (strchr("@%?*!<>", varname[0]) == NULL) | | 4068 | if (strchr("@%?*!<>", varname[0]) == NULL) |
4068 | return NULL; | | 4069 | return NULL; |
4069 | | | 4070 | |
4070 | { | | 4071 | { |
4071 | char name[] = { varname[0], '\0' }; | | 4072 | char name[] = { varname[0], '\0' }; |
4072 | Var *v = VarFind(name, scope, FALSE); | | 4073 | Var *v = VarFind(name, scope, FALSE); |
4073 | | | 4074 | |
4074 | if (v != NULL) { | | 4075 | if (v != NULL) { |
4075 | if (varname[1] == 'D') { | | 4076 | if (varname[1] == 'D') { |
4076 | *out_extraModifiers = "H:"; | | 4077 | *out_extraModifiers = "H:"; |
4077 | } else { /* F */ | | 4078 | } else { /* F */ |
4078 | *out_extraModifiers = "T:"; | | 4079 | *out_extraModifiers = "T:"; |
4079 | } | | 4080 | } |
4080 | } | | 4081 | } |
4081 | return v; | | 4082 | return v; |
4082 | } | | 4083 | } |
4083 | } | | 4084 | } |
4084 | | | 4085 | |
4085 | static VarParseResult | | 4086 | static VarParseResult |
4086 | EvalUndefined(Boolean dynamic, const char *start, const char *p, char *varname, | | 4087 | EvalUndefined(Boolean dynamic, const char *start, const char *p, char *varname, |
4087 | VarEvalFlags eflags, | | 4088 | VarEvalFlags eflags, |
4088 | FStr *out_val) | | 4089 | FStr *out_val) |
4089 | { | | 4090 | { |
4090 | if (dynamic) { | | 4091 | if (dynamic) { |
4091 | *out_val = FStr_InitOwn(bmake_strsedup(start, p)); | | 4092 | *out_val = FStr_InitOwn(bmake_strsedup(start, p)); |
4092 | free(varname); | | 4093 | free(varname); |
4093 | return VPR_OK; | | 4094 | return VPR_OK; |
4094 | } | | 4095 | } |
4095 | | | 4096 | |
4096 | if ((eflags & VARE_UNDEFERR) && opts.strict) { | | 4097 | if ((eflags & VARE_UNDEFERR) && opts.strict) { |
4097 | Parse_Error(PARSE_FATAL, | | 4098 | Parse_Error(PARSE_FATAL, |
4098 | "Variable \"%s\" is undefined", varname); | | 4099 | "Variable \"%s\" is undefined", varname); |
4099 | free(varname); | | 4100 | free(varname); |
4100 | *out_val = FStr_InitRefer(var_Error); | | 4101 | *out_val = FStr_InitRefer(var_Error); |
4101 | return VPR_ERR; | | 4102 | return VPR_ERR; |
4102 | } | | 4103 | } |
4103 | | | 4104 | |
4104 | if (eflags & VARE_UNDEFERR) { | | 4105 | if (eflags & VARE_UNDEFERR) { |
4105 | free(varname); | | 4106 | free(varname); |
4106 | *out_val = FStr_InitRefer(var_Error); | | 4107 | *out_val = FStr_InitRefer(var_Error); |
4107 | return VPR_UNDEF; /* XXX: Should be VPR_ERR instead. */ | | 4108 | return VPR_UNDEF; /* XXX: Should be VPR_ERR instead. */ |
4108 | } | | 4109 | } |
4109 | | | 4110 | |
4110 | free(varname); | | 4111 | free(varname); |
4111 | *out_val = FStr_InitRefer(varUndefined); | | 4112 | *out_val = FStr_InitRefer(varUndefined); |
4112 | return VPR_OK; | | 4113 | return VPR_OK; |
4113 | } | | 4114 | } |
4114 | | | 4115 | |
4115 | /* | | 4116 | /* |
4116 | * Parse a long variable name enclosed in braces or parentheses such as $(VAR) | | 4117 | * Parse a long variable name enclosed in braces or parentheses such as $(VAR) |
4117 | * or ${VAR}, up to the closing brace or parenthesis, or in the case of | | 4118 | * or ${VAR}, up to the closing brace or parenthesis, or in the case of |
4118 | * ${VAR:Modifiers}, up to the ':' that starts the modifiers. | | 4119 | * ${VAR:Modifiers}, up to the ':' that starts the modifiers. |
4119 | * Return whether to continue parsing. | | 4120 | * Return whether to continue parsing. |
4120 | */ | | 4121 | */ |
4121 | static Boolean | | 4122 | static Boolean |
4122 | ParseVarnameLong( | | 4123 | ParseVarnameLong( |
4123 | const char *p, | | 4124 | const char *p, |
4124 | char startc, | | 4125 | char startc, |
4125 | GNode *scope, | | 4126 | GNode *scope, |
4126 | VarEvalFlags eflags, | | 4127 | VarEvalFlags eflags, |
4127 | | | 4128 | |
4128 | const char **out_FALSE_pp, | | 4129 | const char **out_FALSE_pp, |
4129 | VarParseResult *out_FALSE_res, | | 4130 | VarParseResult *out_FALSE_res, |
4130 | FStr *out_FALSE_val, | | 4131 | FStr *out_FALSE_val, |
4131 | | | 4132 | |
4132 | char *out_TRUE_endc, | | 4133 | char *out_TRUE_endc, |
4133 | const char **out_TRUE_p, | | 4134 | const char **out_TRUE_p, |
4134 | Var **out_TRUE_v, | | 4135 | Var **out_TRUE_v, |
4135 | Boolean *out_TRUE_haveModifier, | | 4136 | Boolean *out_TRUE_haveModifier, |
4136 | const char **out_TRUE_extraModifiers, | | 4137 | const char **out_TRUE_extraModifiers, |
4137 | Boolean *out_TRUE_dynamic, | | 4138 | Boolean *out_TRUE_dynamic, |
4138 | ExprDefined *out_TRUE_exprDefined | | 4139 | ExprDefined *out_TRUE_exprDefined |
4139 | ) | | 4140 | ) |
4140 | { | | 4141 | { |
4141 | size_t namelen; | | 4142 | size_t namelen; |
4142 | char *varname; | | 4143 | char *varname; |
4143 | Var *v; | | 4144 | Var *v; |
4144 | Boolean haveModifier; | | 4145 | Boolean haveModifier; |
4145 | Boolean dynamic = FALSE; | | 4146 | Boolean dynamic = FALSE; |
4146 | | | 4147 | |
4147 | const char *const start = p; | | 4148 | const char *const start = p; |
4148 | char endc = startc == '(' ? ')' : '}'; | | 4149 | char endc = startc == '(' ? ')' : '}'; |
4149 | | | 4150 | |
4150 | p += 2; /* skip "${" or "$(" or "y(" */ | | 4151 | p += 2; /* skip "${" or "$(" or "y(" */ |
4151 | varname = ParseVarname(&p, startc, endc, scope, eflags, &namelen); | | 4152 | varname = ParseVarname(&p, startc, endc, scope, eflags, &namelen); |
4152 | | | 4153 | |
4153 | if (*p == ':') { | | 4154 | if (*p == ':') { |
4154 | haveModifier = TRUE; | | 4155 | haveModifier = TRUE; |
4155 | } else if (*p == endc) { | | 4156 | } else if (*p == endc) { |
4156 | haveModifier = FALSE; | | 4157 | haveModifier = FALSE; |
4157 | } else { | | 4158 | } else { |
4158 | Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"", varname); | | 4159 | Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"", varname); |
4159 | free(varname); | | 4160 | free(varname); |
4160 | *out_FALSE_pp = p; | | 4161 | *out_FALSE_pp = p; |
4161 | *out_FALSE_val = FStr_InitRefer(var_Error); | | 4162 | *out_FALSE_val = FStr_InitRefer(var_Error); |
4162 | *out_FALSE_res = VPR_ERR; | | 4163 | *out_FALSE_res = VPR_ERR; |
4163 | return FALSE; | | 4164 | return FALSE; |
4164 | } | | 4165 | } |
4165 | | | 4166 | |
4166 | v = VarFind(varname, scope, TRUE); | | 4167 | v = VarFind(varname, scope, TRUE); |
4167 | | | 4168 | |
4168 | /* At this point, p points just after the variable name, | | 4169 | /* At this point, p points just after the variable name, |
4169 | * either at ':' or at endc. */ | | 4170 | * either at ':' or at endc. */ |
4170 | | | 4171 | |
4171 | if (v == NULL) { | | 4172 | if (v == NULL) { |
4172 | v = FindLocalLegacyVar(varname, namelen, scope, | | 4173 | v = FindLocalLegacyVar(varname, namelen, scope, |
4173 | out_TRUE_extraModifiers); | | 4174 | out_TRUE_extraModifiers); |
4174 | } | | 4175 | } |
4175 | | | 4176 | |
4176 | if (v == NULL) { | | 4177 | if (v == NULL) { |
4177 | /* | | 4178 | /* |
4178 | * Defer expansion of dynamic variables if they appear in | | 4179 | * Defer expansion of dynamic variables if they appear in |
4179 | * non-local scope since they are not defined there. | | 4180 | * non-local scope since they are not defined there. |
4180 | */ | | 4181 | */ |
4181 | dynamic = VarnameIsDynamic(varname, namelen) && | | 4182 | dynamic = VarnameIsDynamic(varname, namelen) && |
4182 | (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL); | | 4183 | (scope == SCOPE_CMDLINE || scope == SCOPE_GLOBAL); |
4183 | | | 4184 | |
4184 | if (!haveModifier) { | | 4185 | if (!haveModifier) { |
4185 | p++; /* skip endc */ | | 4186 | p++; /* skip endc */ |
4186 | *out_FALSE_pp = p; | | 4187 | *out_FALSE_pp = p; |
4187 | *out_FALSE_res = EvalUndefined(dynamic, start, p, | | 4188 | *out_FALSE_res = EvalUndefined(dynamic, start, p, |
4188 | varname, eflags, out_FALSE_val); | | 4189 | varname, eflags, out_FALSE_val); |
4189 | return FALSE; | | 4190 | return FALSE; |
4190 | } | | 4191 | } |
4191 | | | 4192 | |
4192 | /* | | 4193 | /* |
4193 | * The variable expression is based on an undefined variable. | | 4194 | * The variable expression is based on an undefined variable. |
4194 | * Nevertheless it needs a Var, for modifiers that access the | | 4195 | * Nevertheless it needs a Var, for modifiers that access the |
4195 | * variable name, such as :L or :?. | | 4196 | * variable name, such as :L or :?. |
4196 | * | | 4197 | * |
4197 | * Most modifiers leave this expression in the "undefined" | | 4198 | * Most modifiers leave this expression in the "undefined" |
4198 | * state (VES_UNDEF), only a few modifiers like :D, :U, :L, | | 4199 | * state (VES_UNDEF), only a few modifiers like :D, :U, :L, |
4199 | * :P turn this undefined expression into a defined | | 4200 | * :P turn this undefined expression into a defined |
4200 | * expression (VES_DEF). | | 4201 | * expression (VES_DEF). |
4201 | * | | 4202 | * |
4202 | * In the end, after applying all modifiers, if the expression | | 4203 | * In the end, after applying all modifiers, if the expression |
4203 | * is still undefined, Var_Parse will return an empty string | | 4204 | * is still undefined, Var_Parse will return an empty string |
4204 | * instead of the actually computed value. | | 4205 | * instead of the actually computed value. |
4205 | */ | | 4206 | */ |
4206 | v = VarNew(FStr_InitOwn(varname), "", VFL_NONE); | | 4207 | v = VarNew(FStr_InitOwn(varname), "", VFL_NONE); |
4207 | *out_TRUE_exprDefined = DEF_UNDEF; | | 4208 | *out_TRUE_exprDefined = DEF_UNDEF; |
4208 | } else | | 4209 | } else |
4209 | free(varname); | | 4210 | free(varname); |
4210 | | | 4211 | |
4211 | *out_TRUE_endc = endc; | | 4212 | *out_TRUE_endc = endc; |
4212 | *out_TRUE_p = p; | | 4213 | *out_TRUE_p = p; |
4213 | *out_TRUE_v = v; | | 4214 | *out_TRUE_v = v; |
4214 | *out_TRUE_haveModifier = haveModifier; | | 4215 | *out_TRUE_haveModifier = haveModifier; |
4215 | *out_TRUE_dynamic = dynamic; | | 4216 | *out_TRUE_dynamic = dynamic; |
4216 | return TRUE; | | 4217 | return TRUE; |
4217 | } | | 4218 | } |
4218 | | | 4219 | |
4219 | /* Free the environment variable now since we own it. */ | | 4220 | /* Free the environment variable now since we own it. */ |
4220 | static void | | 4221 | static void |
4221 | FreeEnvVar(Var *v, FStr *inout_val) | | 4222 | FreeEnvVar(Var *v, FStr *inout_val) |
4222 | { | | 4223 | { |
4223 | char *varValue = Buf_DoneData(&v->val); | | 4224 | char *varValue = Buf_DoneData(&v->val); |
4224 | if (inout_val->str == varValue) | | 4225 | if (inout_val->str == varValue) |
4225 | inout_val->freeIt = varValue; | | 4226 | inout_val->freeIt = varValue; |
4226 | else | | 4227 | else |
4227 | free(varValue); | | 4228 | free(varValue); |
4228 | | | 4229 | |
4229 | FStr_Done(&v->name); | | 4230 | FStr_Done(&v->name); |
4230 | free(v); | | 4231 | free(v); |
4231 | } | | 4232 | } |
4232 | | | 4233 | |
4233 | /* | | 4234 | /* |
4234 | * Given the start of a variable expression (such as $v, $(VAR), | | 4235 | * Given the start of a variable expression (such as $v, $(VAR), |
4235 | * ${VAR:Mpattern}), extract the variable name and value, and the modifiers, | | 4236 | * ${VAR:Mpattern}), extract the variable name and value, and the modifiers, |
4236 | * if any. While doing that, apply the modifiers to the value of the | | 4237 | * if any. While doing that, apply the modifiers to the value of the |
4237 | * expression, forming its final value. A few of the modifiers such as :!cmd! | | 4238 | * expression, forming its final value. A few of the modifiers such as :!cmd! |
4238 | * or ::= have side effects. | | 4239 | * or ::= have side effects. |
4239 | * | | 4240 | * |
4240 | * Input: | | 4241 | * Input: |
4241 | * *pp The string to parse. | | 4242 | * *pp The string to parse. |
4242 | * When parsing a condition in ParseEmptyArg, it may also | | 4243 | * When parsing a condition in ParseEmptyArg, it may also |
4243 | * point to the "y" of "empty(VARNAME:Modifiers)", which | | 4244 | * point to the "y" of "empty(VARNAME:Modifiers)", which |
4244 | * is syntactically the same. | | 4245 | * is syntactically the same. |
4245 | * scope The scope for finding variables | | 4246 | * scope The scope for finding variables |
4246 | * eflags Control the exact details of parsing | | 4247 | * eflags Control the exact details of parsing |
4247 | * | | 4248 | * |
4248 | * Output: | | 4249 | * Output: |
4249 | * *pp The position where to continue parsing. | | 4250 | * *pp The position where to continue parsing. |
4250 | * TODO: After a parse error, the value of *pp is | | 4251 | * TODO: After a parse error, the value of *pp is |
4251 | * unspecified. It may not have been updated at all, | | 4252 | * unspecified. It may not have been updated at all, |
4252 | * point to some random character in the string, to the | | 4253 | * point to some random character in the string, to the |
4253 | * location of the parse error, or at the end of the | | 4254 | * location of the parse error, or at the end of the |
4254 | * string. | | 4255 | * string. |
4255 | * *out_val The value of the variable expression, never NULL. | | 4256 | * *out_val The value of the variable expression, never NULL. |
4256 | * *out_val var_Error if there was a parse error. | | 4257 | * *out_val var_Error if there was a parse error. |
4257 | * *out_val var_Error if the base variable of the expression was | | 4258 | * *out_val var_Error if the base variable of the expression was |
4258 | * undefined, eflags contains VARE_UNDEFERR, and none of | | 4259 | * undefined, eflags contains VARE_UNDEFERR, and none of |
4259 | * the modifiers turned the undefined expression into a | | 4260 | * the modifiers turned the undefined expression into a |
4260 | * defined expression. | | 4261 | * defined expression. |
4261 | * XXX: It is not guaranteed that an error message has | | 4262 | * XXX: It is not guaranteed that an error message has |
4262 | * been printed. | | 4263 | * been printed. |
4263 | * *out_val varUndefined if the base variable of the expression | | 4264 | * *out_val varUndefined if the base variable of the expression |
4264 | * was undefined, eflags did not contain VARE_UNDEFERR, | | 4265 | * was undefined, eflags did not contain VARE_UNDEFERR, |
4265 | * and none of the modifiers turned the undefined | | 4266 | * and none of the modifiers turned the undefined |
4266 | * expression into a defined expression. | | 4267 | * expression into a defined expression. |
4267 | * XXX: It is not guaranteed that an error message has | | 4268 | * XXX: It is not guaranteed that an error message has |
4268 | * been printed. | | 4269 | * been printed. |
4269 | */ | | 4270 | */ |
4270 | VarParseResult | | 4271 | VarParseResult |
4271 | Var_Parse(const char **pp, GNode *scope, VarEvalFlags eflags, FStr *out_val) | | 4272 | Var_Parse(const char **pp, GNode *scope, VarEvalFlags eflags, FStr *out_val) |
4272 | { | | 4273 | { |
4273 | const char *p = *pp; | | 4274 | const char *p = *pp; |
4274 | const char *const start = p; | | 4275 | const char *const start = p; |
4275 | /* TRUE if have modifiers for the variable. */ | | 4276 | /* TRUE if have modifiers for the variable. */ |
4276 | Boolean haveModifier; | | 4277 | Boolean haveModifier; |
4277 | /* Starting character if variable in parens or braces. */ | | 4278 | /* Starting character if variable in parens or braces. */ |
4278 | char startc; | | 4279 | char startc; |
4279 | /* Ending character if variable in parens or braces. */ | | 4280 | /* Ending character if variable in parens or braces. */ |
4280 | char endc; | | 4281 | char endc; |
4281 | /* | | 4282 | /* |
4282 | * TRUE if the variable is local and we're expanding it in a | | 4283 | * TRUE if the variable is local and we're expanding it in a |
4283 | * non-local scope. This is done to support dynamic sources. | | 4284 | * non-local scope. This is done to support dynamic sources. |
4284 | * The result is just the expression, unaltered. | | 4285 | * The result is just the expression, unaltered. |
4285 | */ | | 4286 | */ |
4286 | Boolean dynamic; | | 4287 | Boolean dynamic; |
4287 | const char *extramodifiers; | | 4288 | const char *extramodifiers; |
4288 | char eflags_str[VarEvalFlags_ToStringSize]; | | 4289 | char eflags_str[VarEvalFlags_ToStringSize]; |
4289 | Var *v; | | 4290 | Var *v; |
4290 | | | 4291 | |
4291 | Expr expr = { | | 4292 | Expr expr = { |
4292 | NULL, | | 4293 | NULL, |
4293 | #if defined(lint) | | 4294 | #if defined(lint) |
4294 | /* NetBSD lint cannot fully parse C99 struct initializers. */ | | 4295 | /* NetBSD lint cannot fully parse C99 struct initializers. */ |
4295 | { NULL, NULL }, | | 4296 | { NULL, NULL }, |
4296 | #else | | 4297 | #else |
4297 | FStr_InitRefer(NULL), | | 4298 | FStr_InitRefer(NULL), |
4298 | #endif | | 4299 | #endif |
4299 | eflags, | | 4300 | eflags, |
4300 | scope, | | 4301 | scope, |
4301 | DEF_REGULAR | | 4302 | DEF_REGULAR |
4302 | }; | | 4303 | }; |
4303 | | | 4304 | |
4304 | DEBUG2(VAR, "Var_Parse: %s with %s\n", start, | | 4305 | DEBUG2(VAR, "Var_Parse: %s with %s\n", start, |
4305 | VarEvalFlags_ToString(eflags_str, eflags)); | | 4306 | VarEvalFlags_ToString(eflags_str, eflags)); |
4306 | | | 4307 | |
4307 | *out_val = FStr_InitRefer(NULL); | | 4308 | *out_val = FStr_InitRefer(NULL); |
4308 | extramodifiers = NULL; /* extra modifiers to apply first */ | | 4309 | extramodifiers = NULL; /* extra modifiers to apply first */ |
4309 | dynamic = FALSE; | | 4310 | dynamic = FALSE; |
4310 | | | 4311 | |
4311 | /* | | 4312 | /* |
4312 | * Appease GCC, which thinks that the variable might not be | | 4313 | * Appease GCC, which thinks that the variable might not be |
4313 | * initialized. | | 4314 | * initialized. |
4314 | */ | | 4315 | */ |
4315 | endc = '\0'; | | 4316 | endc = '\0'; |
4316 | | | 4317 | |
4317 | startc = p[1]; | | 4318 | startc = p[1]; |
4318 | if (startc != '(' && startc != '{') { | | 4319 | if (startc != '(' && startc != '{') { |
4319 | VarParseResult res; | | 4320 | VarParseResult res; |
4320 | if (!ParseVarnameShort(startc, pp, scope, eflags, &res, | | 4321 | if (!ParseVarnameShort(startc, pp, scope, eflags, &res, |
4321 | &out_val->str, &expr.var)) | | 4322 | &out_val->str, &expr.var)) |
4322 | return res; | | 4323 | return res; |
4323 | haveModifier = FALSE; | | 4324 | haveModifier = FALSE; |
4324 | p++; | | 4325 | p++; |
4325 | } else { | | 4326 | } else { |
4326 | VarParseResult res; | | 4327 | VarParseResult res; |
4327 | if (!ParseVarnameLong(p, startc, scope, eflags, | | 4328 | if (!ParseVarnameLong(p, startc, scope, eflags, |
4328 | pp, &res, out_val, | | 4329 | pp, &res, out_val, |
4329 | &endc, &p, &expr.var, &haveModifier, &extramodifiers, | | 4330 | &endc, &p, &expr.var, &haveModifier, &extramodifiers, |
4330 | &dynamic, &expr.defined)) | | 4331 | &dynamic, &expr.defined)) |
4331 | return res; | | 4332 | return res; |
4332 | } | | 4333 | } |
4333 | | | 4334 | |
4334 | v = expr.var; | | 4335 | v = expr.var; |
4335 | if (v->flags & VFL_IN_USE) | | 4336 | if (v->flags & VFL_IN_USE) |
4336 | Fatal("Variable %s is recursive.", v->name.str); | | 4337 | Fatal("Variable %s is recursive.", v->name.str); |
4337 | | | 4338 | |
4338 | /* | | 4339 | /* |
4339 | * XXX: This assignment creates an alias to the current value of the | | 4340 | * XXX: This assignment creates an alias to the current value of the |
4340 | * variable. This means that as long as the value of the expression | | 4341 | * variable. This means that as long as the value of the expression |
4341 | * stays the same, the value of the variable must not change. | | 4342 | * stays the same, the value of the variable must not change. |
4342 | * Using the '::=' modifier, it could be possible to do exactly this. | | 4343 | * Using the '::=' modifier, it could be possible to do exactly this. |
4343 | * At the bottom of this function, the resulting value is compared to | | 4344 | * At the bottom of this function, the resulting value is compared to |
4344 | * the then-current value of the variable. This might also invoke | | 4345 | * the then-current value of the variable. This might also invoke |
4345 | * undefined behavior. | | 4346 | * undefined behavior. |
4346 | */ | | 4347 | */ |
4347 | expr.value = FStr_InitRefer(v->val.data); | | 4348 | expr.value = FStr_InitRefer(v->val.data); |
4348 | | | 4349 | |
4349 | /* | | 4350 | /* |
4350 | * Before applying any modifiers, expand any nested expressions from | | 4351 | * Before applying any modifiers, expand any nested expressions from |
4351 | * the variable value. | | 4352 | * the variable value. |
4352 | */ | | 4353 | */ |
4353 | if (strchr(expr.value.str, '$') != NULL && (eflags & VARE_WANTRES)) { | | 4354 | if (strchr(expr.value.str, '$') != NULL && (eflags & VARE_WANTRES)) { |
4354 | char *expanded; | | 4355 | char *expanded; |
4355 | VarEvalFlags nested_eflags = eflags; | | 4356 | VarEvalFlags nested_eflags = eflags; |
4356 | if (opts.strict) | | 4357 | if (opts.strict) |
4357 | nested_eflags &= ~(unsigned)VARE_UNDEFERR; | | 4358 | nested_eflags &= ~(unsigned)VARE_UNDEFERR; |
4358 | v->flags |= VFL_IN_USE; | | 4359 | v->flags |= VFL_IN_USE; |
4359 | (void)Var_Subst(expr.value.str, scope, nested_eflags, | | 4360 | (void)Var_Subst(expr.value.str, scope, nested_eflags, |
4360 | &expanded); | | 4361 | &expanded); |
4361 | v->flags &= ~(unsigned)VFL_IN_USE; | | 4362 | v->flags &= ~(unsigned)VFL_IN_USE; |
4362 | /* TODO: handle errors */ | | 4363 | /* TODO: handle errors */ |
4363 | Expr_SetValueOwn(&expr, expanded); | | 4364 | Expr_SetValueOwn(&expr, expanded); |
4364 | } | | 4365 | } |
4365 | | | 4366 | |
4366 | if (extramodifiers != NULL) { | | 4367 | if (extramodifiers != NULL) { |
4367 | const char *em = extramodifiers; | | 4368 | const char *em = extramodifiers; |
4368 | ApplyModifiers(&expr, &em, '\0', '\0'); | | 4369 | ApplyModifiers(&expr, &em, '\0', '\0'); |
4369 | } | | 4370 | } |
4370 | | | 4371 | |
4371 | if (haveModifier) { | | 4372 | if (haveModifier) { |
4372 | p++; /* Skip initial colon. */ | | 4373 | p++; /* Skip initial colon. */ |
4373 | ApplyModifiers(&expr, &p, startc, endc); | | 4374 | ApplyModifiers(&expr, &p, startc, endc); |
4374 | } | | 4375 | } |
4375 | | | 4376 | |
4376 | if (*p != '\0') /* Skip past endc if possible. */ | | 4377 | if (*p != '\0') /* Skip past endc if possible. */ |
4377 | p++; | | 4378 | p++; |
4378 | | | 4379 | |
4379 | *pp = p; | | 4380 | *pp = p; |
4380 | | | 4381 | |
4381 | if (v->flags & VFL_FROM_ENV) { | | 4382 | if (v->flags & VFL_FROM_ENV) { |
4382 | FreeEnvVar(v, &expr.value); | | 4383 | FreeEnvVar(v, &expr.value); |
4383 | | | 4384 | |
4384 | } else if (expr.defined != DEF_REGULAR) { | | 4385 | } else if (expr.defined != DEF_REGULAR) { |
4385 | if (expr.defined == DEF_UNDEF) { | | 4386 | if (expr.defined == DEF_UNDEF) { |
4386 | if (dynamic) { | | 4387 | if (dynamic) { |
4387 | Expr_SetValueOwn(&expr, | | 4388 | Expr_SetValueOwn(&expr, |
4388 | bmake_strsedup(start, p)); | | 4389 | bmake_strsedup(start, p)); |
4389 | } else { | | 4390 | } else { |
4390 | /* | | 4391 | /* |
4391 | * The expression is still undefined, | | 4392 | * The expression is still undefined, |
4392 | * therefore discard the actual value and | | 4393 | * therefore discard the actual value and |
4393 | * return an error marker instead. | | 4394 | * return an error marker instead. |
4394 | */ | | 4395 | */ |
4395 | Expr_SetValueRefer(&expr, | | 4396 | Expr_SetValueRefer(&expr, |
4396 | eflags & VARE_UNDEFERR | | 4397 | eflags & VARE_UNDEFERR |
4397 | ? var_Error : varUndefined); | | 4398 | ? var_Error : varUndefined); |
4398 | } | | 4399 | } |
4399 | } | | 4400 | } |
4400 | /* XXX: This is not standard memory management. */ | | 4401 | /* XXX: This is not standard memory management. */ |
4401 | if (expr.value.str != v->val.data) | | 4402 | if (expr.value.str != v->val.data) |
4402 | Buf_Done(&v->val); | | 4403 | Buf_Done(&v->val); |
4403 | FStr_Done(&v->name); | | 4404 | FStr_Done(&v->name); |
4404 | free(v); | | 4405 | free(v); |
4405 | } | | 4406 | } |
4406 | *out_val = expr.value; | | 4407 | *out_val = expr.value; |
4407 | return VPR_OK; /* XXX: Is not correct in all cases */ | | 4408 | return VPR_OK; /* XXX: Is not correct in all cases */ |
4408 | } | | 4409 | } |
4409 | | | 4410 | |
4410 | static void | | 4411 | static void |
4411 | VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalFlags eflags) | | 4412 | VarSubstDollarDollar(const char **pp, Buffer *res, VarEvalFlags eflags) |
4412 | { | | 4413 | { |
4413 | /* A dollar sign may be escaped with another dollar sign. */ | | 4414 | /* A dollar sign may be escaped with another dollar sign. */ |
4414 | if (save_dollars && (eflags & VARE_KEEP_DOLLAR)) | | 4415 | if (save_dollars && (eflags & VARE_KEEP_DOLLAR)) |
4415 | Buf_AddByte(res, '$'); | | 4416 | Buf_AddByte(res, '$'); |
4416 | Buf_AddByte(res, '$'); | | 4417 | Buf_AddByte(res, '$'); |
4417 | *pp += 2; | | 4418 | *pp += 2; |
4418 | } | | 4419 | } |
4419 | | | 4420 | |
4420 | static void | | 4421 | static void |
4421 | VarSubstExpr(const char **pp, Buffer *buf, GNode *scope, | | 4422 | VarSubstExpr(const char **pp, Buffer *buf, GNode *scope, |
4422 | VarEvalFlags eflags, Boolean *inout_errorReported) | | 4423 | VarEvalFlags eflags, Boolean *inout_errorReported) |
4423 | { | | 4424 | { |
4424 | const char *p = *pp; | | 4425 | const char *p = *pp; |
4425 | const char *nested_p = p; | | 4426 | const char *nested_p = p; |
4426 | FStr val; | | 4427 | FStr val; |
4427 | | | 4428 | |
4428 | (void)Var_Parse(&nested_p, scope, eflags, &val); | | 4429 | (void)Var_Parse(&nested_p, scope, eflags, &val); |
4429 | /* TODO: handle errors */ | | 4430 | /* TODO: handle errors */ |
4430 | | | 4431 | |
4431 | if (val.str == var_Error || val.str == varUndefined) { | | 4432 | if (val.str == var_Error || val.str == varUndefined) { |
4432 | if (!(eflags & VARE_KEEP_UNDEF)) { | | 4433 | if (!(eflags & VARE_KEEP_UNDEF)) { |
4433 | p = nested_p; | | 4434 | p = nested_p; |
4434 | } else if ((eflags & VARE_UNDEFERR) || val.str == var_Error) { | | 4435 | } else if ((eflags & VARE_UNDEFERR) || val.str == var_Error) { |