1 | /* Variable expansion functions for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
---|
4 | 2010 Free Software Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
10 | version.
|
---|
11 |
|
---|
12 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License along with
|
---|
17 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
18 |
|
---|
19 | #include "make.h"
|
---|
20 |
|
---|
21 | #include <assert.h>
|
---|
22 |
|
---|
23 | #include "filedef.h"
|
---|
24 | #include "job.h"
|
---|
25 | #include "commands.h"
|
---|
26 | #include "variable.h"
|
---|
27 | #include "rule.h"
|
---|
28 | #ifdef CONFIG_WITH_COMPILER
|
---|
29 | # include "kmk_cc_exec.h"
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /* Initially, any errors reported when expanding strings will be reported
|
---|
33 | against the file where the error appears. */
|
---|
34 | const struct floc **expanding_var = &reading_file;
|
---|
35 |
|
---|
36 | /* The next two describe the variable output buffer.
|
---|
37 | This buffer is used to hold the variable-expansion of a line of the
|
---|
38 | makefile. It is made bigger with realloc whenever it is too small.
|
---|
39 | variable_buffer_length is the size currently allocated.
|
---|
40 | variable_buffer is the address of the buffer.
|
---|
41 |
|
---|
42 | For efficiency, it's guaranteed that the buffer will always have
|
---|
43 | VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
|
---|
44 | extra chars without having to call a function. Note you should never use
|
---|
45 | these bytes unless you're _sure_ you have room (you know when the buffer
|
---|
46 | length was last checked. */
|
---|
47 |
|
---|
48 | #define VARIABLE_BUFFER_ZONE 5
|
---|
49 |
|
---|
50 | #ifndef KMK
|
---|
51 | static unsigned int variable_buffer_length;
|
---|
52 | #else
|
---|
53 | unsigned int variable_buffer_length;
|
---|
54 | #endif
|
---|
55 | char *variable_buffer;
|
---|
56 |
|
---|
57 |
|
---|
58 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
59 | struct recycled_buffer
|
---|
60 | {
|
---|
61 | struct recycled_buffer *next;
|
---|
62 | unsigned int length;
|
---|
63 | };
|
---|
64 | struct recycled_buffer *recycled_head;
|
---|
65 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | #ifndef KMK
|
---|
70 | /* Subroutine of variable_expand and friends:
|
---|
71 | The text to add is LENGTH chars starting at STRING to the variable_buffer.
|
---|
72 | The text is added to the buffer at PTR, and the updated pointer into
|
---|
73 | the buffer is returned as the value. Thus, the value returned by
|
---|
74 | each call to variable_buffer_output should be the first argument to
|
---|
75 | the following call. */
|
---|
76 |
|
---|
77 | char *
|
---|
78 | variable_buffer_output (char *ptr, const char *string, unsigned int length)
|
---|
79 | {
|
---|
80 | register unsigned int newlen = length + (ptr - variable_buffer);
|
---|
81 |
|
---|
82 | if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
|
---|
83 | {
|
---|
84 | unsigned int offset = ptr - variable_buffer;
|
---|
85 | variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
|
---|
86 | ? newlen + 100
|
---|
87 | : 2 * variable_buffer_length);
|
---|
88 | variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
|
---|
89 | ptr = variable_buffer + offset;
|
---|
90 | }
|
---|
91 |
|
---|
92 | memcpy (ptr, string, length);
|
---|
93 | return ptr + length;
|
---|
94 | }
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | /* Return a pointer to the beginning of the variable buffer. */
|
---|
98 |
|
---|
99 | static char *
|
---|
100 | initialize_variable_output (void)
|
---|
101 | {
|
---|
102 | /* If we don't have a variable output buffer yet, get one. */
|
---|
103 |
|
---|
104 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
105 | if (variable_buffer == 0)
|
---|
106 | {
|
---|
107 | struct recycled_buffer *recycled = recycled_head;
|
---|
108 | if (recycled)
|
---|
109 | {
|
---|
110 | recycled_head = recycled->next;
|
---|
111 | variable_buffer_length = recycled->length;
|
---|
112 | variable_buffer = (char *)recycled;
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | variable_buffer_length = 384;
|
---|
117 | variable_buffer = xmalloc (variable_buffer_length);
|
---|
118 | }
|
---|
119 | variable_buffer[0] = '\0';
|
---|
120 | }
|
---|
121 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
122 | if (variable_buffer == 0)
|
---|
123 | {
|
---|
124 | variable_buffer_length = 200;
|
---|
125 | variable_buffer = xmalloc (variable_buffer_length);
|
---|
126 | variable_buffer[0] = '\0';
|
---|
127 | }
|
---|
128 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
129 |
|
---|
130 | return variable_buffer;
|
---|
131 | }
|
---|
132 | |
---|
133 |
|
---|
134 | /* Recursively expand V. The returned string is malloc'd. */
|
---|
135 |
|
---|
136 | static char *allocated_variable_append (const struct variable *v);
|
---|
137 |
|
---|
138 | char *
|
---|
139 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
140 | recursively_expand_for_file (struct variable *v, struct file *file)
|
---|
141 | #else
|
---|
142 | recursively_expand_for_file (struct variable *v, struct file *file,
|
---|
143 | unsigned int *value_lenp)
|
---|
144 | #endif
|
---|
145 | {
|
---|
146 | char *value;
|
---|
147 | const struct floc *this_var;
|
---|
148 | const struct floc **saved_varp;
|
---|
149 | struct variable_set_list *save = 0;
|
---|
150 | int set_reading = 0;
|
---|
151 |
|
---|
152 | /* Don't install a new location if this location is empty.
|
---|
153 | This can happen for command-line variables, builtin variables, etc. */
|
---|
154 | saved_varp = expanding_var;
|
---|
155 | if (v->fileinfo.filenm)
|
---|
156 | {
|
---|
157 | this_var = &v->fileinfo;
|
---|
158 | expanding_var = &this_var;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* If we have no other file-reading context, use the variable's context. */
|
---|
162 | if (!reading_file)
|
---|
163 | {
|
---|
164 | set_reading = 1;
|
---|
165 | reading_file = &v->fileinfo;
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (v->expanding)
|
---|
169 | {
|
---|
170 | if (!v->exp_count)
|
---|
171 | /* Expanding V causes infinite recursion. Lose. */
|
---|
172 | fatal (*expanding_var,
|
---|
173 | _("Recursive variable `%s' references itself (eventually)"),
|
---|
174 | v->name);
|
---|
175 | --v->exp_count;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (file)
|
---|
179 | {
|
---|
180 | save = current_variable_set_list;
|
---|
181 | current_variable_set_list = file->variables;
|
---|
182 | }
|
---|
183 |
|
---|
184 | v->expanding = 1;
|
---|
185 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
186 | if (v->append)
|
---|
187 | value = allocated_variable_append (v);
|
---|
188 | else
|
---|
189 | value = allocated_variable_expand (v->value);
|
---|
190 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
191 | if (!v->append)
|
---|
192 | value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
|
---|
193 | else
|
---|
194 | {
|
---|
195 | value = allocated_variable_append (v);
|
---|
196 | if (value_lenp)
|
---|
197 | *value_lenp = strlen (value);
|
---|
198 | }
|
---|
199 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
200 | v->expanding = 0;
|
---|
201 |
|
---|
202 | if (set_reading)
|
---|
203 | reading_file = 0;
|
---|
204 |
|
---|
205 | if (file)
|
---|
206 | current_variable_set_list = save;
|
---|
207 |
|
---|
208 | expanding_var = saved_varp;
|
---|
209 |
|
---|
210 | return value;
|
---|
211 | }
|
---|
212 |
|
---|
213 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
214 | /* Worker for reference_variable() and kmk_exec_* that expands the recursive
|
---|
215 | variable V. The main difference between this and
|
---|
216 | recursively_expand[_for_file] is that this worker avoids the temporary
|
---|
217 | buffer and outputs directly into the current variable buffer (O). */
|
---|
218 | char *
|
---|
219 | reference_recursive_variable (char *o, struct variable *v)
|
---|
220 | {
|
---|
221 | const struct floc *this_var;
|
---|
222 | const struct floc **saved_varp;
|
---|
223 | int set_reading = 0;
|
---|
224 |
|
---|
225 | /* Don't install a new location if this location is empty.
|
---|
226 | This can happen for command-line variables, builtin variables, etc. */
|
---|
227 | saved_varp = expanding_var;
|
---|
228 | if (v->fileinfo.filenm)
|
---|
229 | {
|
---|
230 | this_var = &v->fileinfo;
|
---|
231 | expanding_var = &this_var;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /* If we have no other file-reading context, use the variable's context. */
|
---|
235 | if (!reading_file)
|
---|
236 | {
|
---|
237 | set_reading = 1;
|
---|
238 | reading_file = &v->fileinfo;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (v->expanding)
|
---|
242 | {
|
---|
243 | if (!v->exp_count)
|
---|
244 | /* Expanding V causes infinite recursion. Lose. */
|
---|
245 | fatal (*expanding_var,
|
---|
246 | _("Recursive variable `%s' references itself (eventually)"),
|
---|
247 | v->name);
|
---|
248 | --v->exp_count;
|
---|
249 | }
|
---|
250 |
|
---|
251 | v->expanding = 1;
|
---|
252 | if (!v->append)
|
---|
253 | {
|
---|
254 | /* Expand directly into the variable buffer. */
|
---|
255 | # ifdef CONFIG_WITH_COMPILER
|
---|
256 | v->expand_count++;
|
---|
257 | if ( v->expandprog
|
---|
258 | || (v->expand_count == 10 && kmk_cc_compile_variable_for_expand (v)) )
|
---|
259 | o = kmk_exec_expand_to_var_buf (v, o);
|
---|
260 | else
|
---|
261 | variable_expand_string_2 (o, v->value, v->value_length, &o);
|
---|
262 | # else
|
---|
263 | MAKE_STATS_2 (v->expand_count++);
|
---|
264 | variable_expand_string_2 (o, v->value, v->value_length, &o);
|
---|
265 | # endif
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | /* XXX: Feel free to optimize appending target variables as well. */
|
---|
270 | char *value = allocated_variable_append (v);
|
---|
271 | unsigned int value_len = strlen (value);
|
---|
272 | o = variable_buffer_output (o, value, value_len);
|
---|
273 | free (value);
|
---|
274 | }
|
---|
275 | v->expanding = 0;
|
---|
276 |
|
---|
277 | if (set_reading)
|
---|
278 | reading_file = 0;
|
---|
279 |
|
---|
280 | expanding_var = saved_varp;
|
---|
281 |
|
---|
282 | return o;
|
---|
283 | }
|
---|
284 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
285 |
|
---|
286 | /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
|
---|
287 |
|
---|
288 | #ifdef MY_INLINE /* bird */
|
---|
289 | MY_INLINE char *
|
---|
290 | #else
|
---|
291 | #if defined(__GNUC__)
|
---|
292 | __inline
|
---|
293 | #endif
|
---|
294 | static char *
|
---|
295 | #endif
|
---|
296 | reference_variable (char *o, const char *name, unsigned int length)
|
---|
297 | {
|
---|
298 | struct variable *v;
|
---|
299 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
300 | char *value;
|
---|
301 | #endif
|
---|
302 |
|
---|
303 | v = lookup_variable (name, length);
|
---|
304 |
|
---|
305 | if (v == 0)
|
---|
306 | warn_undefined (name, length);
|
---|
307 |
|
---|
308 | /* If there's no variable by that name or it has no value, stop now. */
|
---|
309 | if (v == 0 || (*v->value == '\0' && !v->append))
|
---|
310 | return o;
|
---|
311 |
|
---|
312 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
313 | assert (v->value_length == strlen (v->value));
|
---|
314 | if (!v->recursive)
|
---|
315 | o = variable_buffer_output (o, v->value, v->value_length);
|
---|
316 | else
|
---|
317 | o = reference_recursive_variable (o, v);
|
---|
318 | #else /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
319 | value = (v->recursive ? recursively_expand (v) : v->value);
|
---|
320 |
|
---|
321 | o = variable_buffer_output (o, value, strlen (value));
|
---|
322 |
|
---|
323 | if (v->recursive)
|
---|
324 | free (value);
|
---|
325 | #endif /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
326 |
|
---|
327 | return o;
|
---|
328 | }
|
---|
329 | |
---|
330 |
|
---|
331 | #ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
|
---|
332 | /* Scan STRING for variable references and expansion-function calls. Only
|
---|
333 | LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
|
---|
334 | a null byte is found.
|
---|
335 |
|
---|
336 | Write the results to LINE, which must point into `variable_buffer'. If
|
---|
337 | LINE is NULL, start at the beginning of the buffer.
|
---|
338 | Return a pointer to LINE, or to the beginning of the buffer if LINE is
|
---|
339 | NULL.
|
---|
340 | */
|
---|
341 | char *
|
---|
342 | variable_expand_string (char *line, const char *string, long length)
|
---|
343 | {
|
---|
344 | struct variable *v;
|
---|
345 | const char *p, *p1;
|
---|
346 | char *abuf = NULL;
|
---|
347 | char *o;
|
---|
348 | unsigned int line_offset;
|
---|
349 |
|
---|
350 | if (!line)
|
---|
351 | line = initialize_variable_output();
|
---|
352 | o = line;
|
---|
353 | line_offset = line - variable_buffer;
|
---|
354 |
|
---|
355 | if (length == 0)
|
---|
356 | {
|
---|
357 | variable_buffer_output (o, "", 1);
|
---|
358 | return (variable_buffer);
|
---|
359 | }
|
---|
360 |
|
---|
361 | /* If we want a subset of the string, allocate a temporary buffer for it.
|
---|
362 | Most of the functions we use here don't work with length limits. */
|
---|
363 | if (length > 0 && string[length] != '\0')
|
---|
364 | {
|
---|
365 | abuf = xmalloc(length+1);
|
---|
366 | memcpy(abuf, string, length);
|
---|
367 | abuf[length] = '\0';
|
---|
368 | string = abuf;
|
---|
369 | }
|
---|
370 | p = string;
|
---|
371 |
|
---|
372 | while (1)
|
---|
373 | {
|
---|
374 | /* Copy all following uninteresting chars all at once to the
|
---|
375 | variable output buffer, and skip them. Uninteresting chars end
|
---|
376 | at the next $ or the end of the input. */
|
---|
377 |
|
---|
378 | p1 = strchr (p, '$');
|
---|
379 |
|
---|
380 | o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
|
---|
381 |
|
---|
382 | if (p1 == 0)
|
---|
383 | break;
|
---|
384 | p = p1 + 1;
|
---|
385 |
|
---|
386 | /* Dispatch on the char that follows the $. */
|
---|
387 |
|
---|
388 | switch (*p)
|
---|
389 | {
|
---|
390 | case '$':
|
---|
391 | /* $$ seen means output one $ to the variable output buffer. */
|
---|
392 | o = variable_buffer_output (o, p, 1);
|
---|
393 | break;
|
---|
394 |
|
---|
395 | case '(':
|
---|
396 | case '{':
|
---|
397 | /* $(...) or ${...} is the general case of substitution. */
|
---|
398 | {
|
---|
399 | char openparen = *p;
|
---|
400 | char closeparen = (openparen == '(') ? ')' : '}';
|
---|
401 | const char *begp;
|
---|
402 | const char *beg = p + 1;
|
---|
403 | char *op;
|
---|
404 | char *abeg = NULL;
|
---|
405 | const char *end, *colon;
|
---|
406 |
|
---|
407 | op = o;
|
---|
408 | begp = p;
|
---|
409 | if (handle_function (&op, &begp))
|
---|
410 | {
|
---|
411 | o = op;
|
---|
412 | p = begp;
|
---|
413 | break;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* Is there a variable reference inside the parens or braces?
|
---|
417 | If so, expand it before expanding the entire reference. */
|
---|
418 |
|
---|
419 | end = strchr (beg, closeparen);
|
---|
420 | if (end == 0)
|
---|
421 | /* Unterminated variable reference. */
|
---|
422 | fatal (*expanding_var, _("unterminated variable reference"));
|
---|
423 | p1 = lindex (beg, end, '$');
|
---|
424 | if (p1 != 0)
|
---|
425 | {
|
---|
426 | /* BEG now points past the opening paren or brace.
|
---|
427 | Count parens or braces until it is matched. */
|
---|
428 | int count = 0;
|
---|
429 | for (p = beg; *p != '\0'; ++p)
|
---|
430 | {
|
---|
431 | if (*p == openparen)
|
---|
432 | ++count;
|
---|
433 | else if (*p == closeparen && --count < 0)
|
---|
434 | break;
|
---|
435 | }
|
---|
436 | /* If COUNT is >= 0, there were unmatched opening parens
|
---|
437 | or braces, so we go to the simple case of a variable name
|
---|
438 | such as `$($(a)'. */
|
---|
439 | if (count < 0)
|
---|
440 | {
|
---|
441 | abeg = expand_argument (beg, p); /* Expand the name. */
|
---|
442 | beg = abeg;
|
---|
443 | end = strchr (beg, '\0');
|
---|
444 | }
|
---|
445 | }
|
---|
446 | else
|
---|
447 | /* Advance P to the end of this reference. After we are
|
---|
448 | finished expanding this one, P will be incremented to
|
---|
449 | continue the scan. */
|
---|
450 | p = end;
|
---|
451 |
|
---|
452 | /* This is not a reference to a built-in function and
|
---|
453 | any variable references inside are now expanded.
|
---|
454 | Is the resultant text a substitution reference? */
|
---|
455 |
|
---|
456 | colon = lindex (beg, end, ':');
|
---|
457 | if (colon)
|
---|
458 | {
|
---|
459 | /* This looks like a substitution reference: $(FOO:A=B). */
|
---|
460 | const char *subst_beg, *subst_end, *replace_beg, *replace_end;
|
---|
461 |
|
---|
462 | subst_beg = colon + 1;
|
---|
463 | subst_end = lindex (subst_beg, end, '=');
|
---|
464 | if (subst_end == 0)
|
---|
465 | /* There is no = in sight. Punt on the substitution
|
---|
466 | reference and treat this as a variable name containing
|
---|
467 | a colon, in the code below. */
|
---|
468 | colon = 0;
|
---|
469 | else
|
---|
470 | {
|
---|
471 | replace_beg = subst_end + 1;
|
---|
472 | replace_end = end;
|
---|
473 |
|
---|
474 | /* Extract the variable name before the colon
|
---|
475 | and look up that variable. */
|
---|
476 | v = lookup_variable (beg, colon - beg);
|
---|
477 | if (v == 0)
|
---|
478 | warn_undefined (beg, colon - beg);
|
---|
479 |
|
---|
480 | /* If the variable is not empty, perform the
|
---|
481 | substitution. */
|
---|
482 | if (v != 0 && *v->value != '\0')
|
---|
483 | {
|
---|
484 | char *pattern, *replace, *ppercent, *rpercent;
|
---|
485 | char *value = (v->recursive
|
---|
486 | ? recursively_expand (v)
|
---|
487 | : v->value);
|
---|
488 |
|
---|
489 | /* Copy the pattern and the replacement. Add in an
|
---|
490 | extra % at the beginning to use in case there
|
---|
491 | isn't one in the pattern. */
|
---|
492 | pattern = alloca (subst_end - subst_beg + 2);
|
---|
493 | *(pattern++) = '%';
|
---|
494 | memcpy (pattern, subst_beg, subst_end - subst_beg);
|
---|
495 | pattern[subst_end - subst_beg] = '\0';
|
---|
496 |
|
---|
497 | replace = alloca (replace_end - replace_beg + 2);
|
---|
498 | *(replace++) = '%';
|
---|
499 | memcpy (replace, replace_beg,
|
---|
500 | replace_end - replace_beg);
|
---|
501 | replace[replace_end - replace_beg] = '\0';
|
---|
502 |
|
---|
503 | /* Look for %. Set the percent pointers properly
|
---|
504 | based on whether we find one or not. */
|
---|
505 | ppercent = find_percent (pattern);
|
---|
506 | if (ppercent)
|
---|
507 | {
|
---|
508 | ++ppercent;
|
---|
509 | rpercent = find_percent (replace);
|
---|
510 | if (rpercent)
|
---|
511 | ++rpercent;
|
---|
512 | }
|
---|
513 | else
|
---|
514 | {
|
---|
515 | ppercent = pattern;
|
---|
516 | rpercent = replace;
|
---|
517 | --pattern;
|
---|
518 | --replace;
|
---|
519 | }
|
---|
520 |
|
---|
521 | o = patsubst_expand_pat (o, value, pattern, replace,
|
---|
522 | ppercent, rpercent);
|
---|
523 |
|
---|
524 | if (v->recursive)
|
---|
525 | free (value);
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | if (colon == 0)
|
---|
531 | /* This is an ordinary variable reference.
|
---|
532 | Look up the value of the variable. */
|
---|
533 | o = reference_variable (o, beg, end - beg);
|
---|
534 |
|
---|
535 | if (abeg)
|
---|
536 | free (abeg);
|
---|
537 | }
|
---|
538 | break;
|
---|
539 |
|
---|
540 | case '\0':
|
---|
541 | break;
|
---|
542 |
|
---|
543 | default:
|
---|
544 | if (isblank ((unsigned char)p[-1]))
|
---|
545 | break;
|
---|
546 |
|
---|
547 | /* A $ followed by a random char is a variable reference:
|
---|
548 | $a is equivalent to $(a). */
|
---|
549 | o = reference_variable (o, p, 1);
|
---|
550 |
|
---|
551 | break;
|
---|
552 | }
|
---|
553 |
|
---|
554 | if (*p == '\0')
|
---|
555 | break;
|
---|
556 |
|
---|
557 | ++p;
|
---|
558 | }
|
---|
559 |
|
---|
560 | if (abuf)
|
---|
561 | free (abuf);
|
---|
562 |
|
---|
563 | variable_buffer_output (o, "", 1);
|
---|
564 | return (variable_buffer + line_offset);
|
---|
565 | }
|
---|
566 |
|
---|
567 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
568 | /* Scan STRING for variable references and expansion-function calls. Only
|
---|
569 | LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
|
---|
570 | a null byte is found.
|
---|
571 |
|
---|
572 | Write the results to LINE, which must point into `variable_buffer'. If
|
---|
573 | LINE is NULL, start at the beginning of the buffer.
|
---|
574 | Return a pointer to LINE, or to the beginning of the buffer if LINE is
|
---|
575 | NULL. Set EOLP to point to the string terminator.
|
---|
576 | */
|
---|
577 | char *
|
---|
578 | variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
|
---|
579 | {
|
---|
580 | struct variable *v;
|
---|
581 | const char *p, *p1, *eos;
|
---|
582 | char *o;
|
---|
583 | unsigned int line_offset;
|
---|
584 |
|
---|
585 | if (!line)
|
---|
586 | line = initialize_variable_output();
|
---|
587 | o = line;
|
---|
588 | line_offset = line - variable_buffer;
|
---|
589 |
|
---|
590 | if (length < 0)
|
---|
591 | length = strlen (string);
|
---|
592 | else
|
---|
593 | MY_ASSERT_MSG (string + length == (p1 = memchr (string, '\0', length)) || !p1, ("len=%ld p1=%p %s\n", length, p1, line));
|
---|
594 |
|
---|
595 | /* Simple 1: Emptry string. */
|
---|
596 |
|
---|
597 | if (length == 0)
|
---|
598 | {
|
---|
599 | o = variable_buffer_output (o, "\0", 2);
|
---|
600 | *eolp = o - 2;
|
---|
601 | return (variable_buffer + line_offset);
|
---|
602 | }
|
---|
603 |
|
---|
604 | /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
|
---|
605 |
|
---|
606 | p1 = (const char *)memchr (string, '$', length);
|
---|
607 | if (p1 == 0)
|
---|
608 | {
|
---|
609 | o = variable_buffer_output (o, string, length);
|
---|
610 | o = variable_buffer_output (o, "\0", 2);
|
---|
611 | *eolp = o - 2;
|
---|
612 | assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
|
---|
613 | return (variable_buffer + line_offset);
|
---|
614 | }
|
---|
615 |
|
---|
616 | p = string;
|
---|
617 | eos = p + length;
|
---|
618 |
|
---|
619 | while (1)
|
---|
620 | {
|
---|
621 | /* Copy all following uninteresting chars all at once to the
|
---|
622 | variable output buffer, and skip them. Uninteresting chars end
|
---|
623 | at the next $ or the end of the input. */
|
---|
624 |
|
---|
625 | o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
|
---|
626 |
|
---|
627 | if (p1 == 0)
|
---|
628 | break;
|
---|
629 | p = p1 + 1;
|
---|
630 |
|
---|
631 | /* Dispatch on the char that follows the $. */
|
---|
632 |
|
---|
633 | switch (*p)
|
---|
634 | {
|
---|
635 | case '$':
|
---|
636 | /* $$ seen means output one $ to the variable output buffer. */
|
---|
637 | o = variable_buffer_output (o, p, 1);
|
---|
638 | break;
|
---|
639 |
|
---|
640 | case '(':
|
---|
641 | case '{':
|
---|
642 | /* $(...) or ${...} is the general case of substitution. */
|
---|
643 | {
|
---|
644 | char openparen = *p;
|
---|
645 | char closeparen = (openparen == '(') ? ')' : '}';
|
---|
646 | const char *begp;
|
---|
647 | const char *beg = p + 1;
|
---|
648 | char *op;
|
---|
649 | char *abeg = NULL;
|
---|
650 | unsigned int alen = 0;
|
---|
651 | const char *end, *colon;
|
---|
652 |
|
---|
653 | op = o;
|
---|
654 | begp = p;
|
---|
655 | end = may_be_function_name (p + 1, eos);
|
---|
656 | if ( end
|
---|
657 | && handle_function (&op, &begp, end, eos))
|
---|
658 | {
|
---|
659 | o = op;
|
---|
660 | p = begp;
|
---|
661 | MY_ASSERT_MSG (!(p1 = memchr (variable_buffer + line_offset, '\0', o - (variable_buffer + line_offset))),
|
---|
662 | ("line=%p o/exp_end=%p act_end=%p\n", variable_buffer + line_offset, o, p1));
|
---|
663 | break;
|
---|
664 | }
|
---|
665 |
|
---|
666 | /* Is there a variable reference inside the parens or braces?
|
---|
667 | If so, expand it before expanding the entire reference. */
|
---|
668 |
|
---|
669 | end = memchr (beg, closeparen, eos - beg);
|
---|
670 | if (end == 0)
|
---|
671 | /* Unterminated variable reference. */
|
---|
672 | fatal (*expanding_var, _("unterminated variable reference"));
|
---|
673 | p1 = lindex (beg, end, '$');
|
---|
674 | if (p1 != 0)
|
---|
675 | {
|
---|
676 | /* BEG now points past the opening paren or brace.
|
---|
677 | Count parens or braces until it is matched. */
|
---|
678 | int count = 0;
|
---|
679 | for (p = beg; p < eos; ++p)
|
---|
680 | {
|
---|
681 | if (*p == openparen)
|
---|
682 | ++count;
|
---|
683 | else if (*p == closeparen && --count < 0)
|
---|
684 | break;
|
---|
685 | }
|
---|
686 | /* If COUNT is >= 0, there were unmatched opening parens
|
---|
687 | or braces, so we go to the simple case of a variable name
|
---|
688 | such as `$($(a)'. */
|
---|
689 | if (count < 0)
|
---|
690 | {
|
---|
691 | unsigned int len;
|
---|
692 | char saved;
|
---|
693 |
|
---|
694 | /* Expand the name. */
|
---|
695 | saved = *p;
|
---|
696 | *(char *)p = '\0'; /* XXX: proove that this is safe! XXX2: shouldn't be necessary any longer! */
|
---|
697 | abeg = allocated_variable_expand_3 (beg, p - beg, &len, &alen);
|
---|
698 | beg = abeg;
|
---|
699 | end = beg + len;
|
---|
700 | *(char *)p = saved;
|
---|
701 | }
|
---|
702 | }
|
---|
703 | else
|
---|
704 | /* Advance P to the end of this reference. After we are
|
---|
705 | finished expanding this one, P will be incremented to
|
---|
706 | continue the scan. */
|
---|
707 | p = end;
|
---|
708 |
|
---|
709 | /* This is not a reference to a built-in function and
|
---|
710 | any variable references inside are now expanded.
|
---|
711 | Is the resultant text a substitution reference? */
|
---|
712 |
|
---|
713 | colon = lindex (beg, end, ':');
|
---|
714 | if (colon)
|
---|
715 | {
|
---|
716 | /* This looks like a substitution reference: $(FOO:A=B). */
|
---|
717 | const char *subst_beg, *subst_end, *replace_beg, *replace_end;
|
---|
718 |
|
---|
719 | subst_beg = colon + 1;
|
---|
720 | subst_end = lindex (subst_beg, end, '=');
|
---|
721 | if (subst_end == 0)
|
---|
722 | /* There is no = in sight. Punt on the substitution
|
---|
723 | reference and treat this as a variable name containing
|
---|
724 | a colon, in the code below. */
|
---|
725 | colon = 0;
|
---|
726 | else
|
---|
727 | {
|
---|
728 | replace_beg = subst_end + 1;
|
---|
729 | replace_end = end;
|
---|
730 |
|
---|
731 | /* Extract the variable name before the colon
|
---|
732 | and look up that variable. */
|
---|
733 | v = lookup_variable (beg, colon - beg);
|
---|
734 | if (v == 0)
|
---|
735 | warn_undefined (beg, colon - beg);
|
---|
736 |
|
---|
737 | /* If the variable is not empty, perform the
|
---|
738 | substitution. */
|
---|
739 | if (v != 0 && *v->value != '\0')
|
---|
740 | {
|
---|
741 | char *pattern, *replace, *ppercent, *rpercent;
|
---|
742 | char *value = (v->recursive
|
---|
743 | ? recursively_expand (v)
|
---|
744 | : v->value);
|
---|
745 |
|
---|
746 | /* Copy the pattern and the replacement. Add in an
|
---|
747 | extra % at the beginning to use in case there
|
---|
748 | isn't one in the pattern. */
|
---|
749 | pattern = alloca (subst_end - subst_beg + 2);
|
---|
750 | *(pattern++) = '%';
|
---|
751 | memcpy (pattern, subst_beg, subst_end - subst_beg);
|
---|
752 | pattern[subst_end - subst_beg] = '\0';
|
---|
753 |
|
---|
754 | replace = alloca (replace_end - replace_beg + 2);
|
---|
755 | *(replace++) = '%';
|
---|
756 | memcpy (replace, replace_beg,
|
---|
757 | replace_end - replace_beg);
|
---|
758 | replace[replace_end - replace_beg] = '\0';
|
---|
759 |
|
---|
760 | /* Look for %. Set the percent pointers properly
|
---|
761 | based on whether we find one or not. */
|
---|
762 | ppercent = find_percent (pattern);
|
---|
763 | if (ppercent)
|
---|
764 | {
|
---|
765 | ++ppercent;
|
---|
766 | rpercent = find_percent (replace);
|
---|
767 | if (rpercent)
|
---|
768 | ++rpercent;
|
---|
769 | }
|
---|
770 | else
|
---|
771 | {
|
---|
772 | ppercent = pattern;
|
---|
773 | rpercent = replace;
|
---|
774 | --pattern;
|
---|
775 | --replace;
|
---|
776 | }
|
---|
777 |
|
---|
778 | o = patsubst_expand_pat (o, value, pattern, replace,
|
---|
779 | ppercent, rpercent);
|
---|
780 |
|
---|
781 | if (v->recursive)
|
---|
782 | free (value);
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (colon == 0)
|
---|
788 | /* This is an ordinary variable reference.
|
---|
789 | Look up the value of the variable. */
|
---|
790 | o = reference_variable (o, beg, end - beg);
|
---|
791 |
|
---|
792 | if (abeg)
|
---|
793 | recycle_variable_buffer (abeg, alen);
|
---|
794 | }
|
---|
795 | break;
|
---|
796 |
|
---|
797 | case '\0':
|
---|
798 | assert (p == eos);
|
---|
799 | break;
|
---|
800 |
|
---|
801 | default:
|
---|
802 | if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
|
---|
803 | break;
|
---|
804 |
|
---|
805 | /* A $ followed by a random char is a variable reference:
|
---|
806 | $a is equivalent to $(a). */
|
---|
807 | o = reference_variable (o, p, 1);
|
---|
808 |
|
---|
809 | break;
|
---|
810 | }
|
---|
811 |
|
---|
812 | if (++p >= eos)
|
---|
813 | break;
|
---|
814 | p1 = memchr (p, '$', eos - p);
|
---|
815 | }
|
---|
816 |
|
---|
817 | o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
|
---|
818 | *eolp = o - 2;
|
---|
819 | MY_ASSERT_MSG (strchr (variable_buffer + line_offset, '\0') == *eolp,
|
---|
820 | ("expected=%d actual=%d\nlength=%ld string=%.*s\n",
|
---|
821 | (int)(*eolp - variable_buffer + line_offset), (int)strlen(variable_buffer + line_offset),
|
---|
822 | length, (int)length, string));
|
---|
823 | return (variable_buffer + line_offset);
|
---|
824 | }
|
---|
825 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
826 | |
---|
827 |
|
---|
828 | /* Scan LINE for variable references and expansion-function calls.
|
---|
829 | Build in `variable_buffer' the result of expanding the references and calls.
|
---|
830 | Return the address of the resulting string, which is null-terminated
|
---|
831 | and is valid only until the next time this function is called. */
|
---|
832 |
|
---|
833 | char *
|
---|
834 | variable_expand (const char *line)
|
---|
835 | {
|
---|
836 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
837 | return variable_expand_string(NULL, line, (long)-1);
|
---|
838 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
839 | char *s;
|
---|
840 |
|
---|
841 | /* this function is abused a lot like this: variable_expand(""). */
|
---|
842 | if (!*line)
|
---|
843 | {
|
---|
844 | s = variable_buffer_output (initialize_variable_output (), "\0", 2);
|
---|
845 | return s - 2;
|
---|
846 | }
|
---|
847 | return variable_expand_string_2 (NULL, line, (long)-1, &s);
|
---|
848 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
849 | }
|
---|
850 | |
---|
851 |
|
---|
852 | /* Expand an argument for an expansion function.
|
---|
853 | The text starting at STR and ending at END is variable-expanded
|
---|
854 | into a null-terminated string that is returned as the value.
|
---|
855 | This is done without clobbering `variable_buffer' or the current
|
---|
856 | variable-expansion that is in progress. */
|
---|
857 |
|
---|
858 | char *
|
---|
859 | expand_argument (const char *str, const char *end)
|
---|
860 | {
|
---|
861 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
862 | char *tmp, *alloc = NULL;
|
---|
863 | char *r;
|
---|
864 | #endif
|
---|
865 |
|
---|
866 | if (str == end)
|
---|
867 | return xstrdup("");
|
---|
868 |
|
---|
869 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
870 | if (!end || *end == '\0')
|
---|
871 | return allocated_variable_expand (str);
|
---|
872 |
|
---|
873 | if (end - str + 1 > 1000)
|
---|
874 | tmp = alloc = xmalloc (end - str + 1);
|
---|
875 | else
|
---|
876 | tmp = alloca (end - str + 1);
|
---|
877 |
|
---|
878 | memcpy (tmp, str, end - str);
|
---|
879 | tmp[end - str] = '\0';
|
---|
880 |
|
---|
881 | r = allocated_variable_expand (tmp);
|
---|
882 |
|
---|
883 | if (alloc)
|
---|
884 | free (alloc);
|
---|
885 |
|
---|
886 | return r;
|
---|
887 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
888 | if (!end)
|
---|
889 | return allocated_variable_expand_2 (str, ~0U, NULL);
|
---|
890 | return allocated_variable_expand_2 (str, end - str, NULL);
|
---|
891 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
892 | }
|
---|
893 | |
---|
894 |
|
---|
895 | /* Expand LINE for FILE. Error messages refer to the file and line where
|
---|
896 | FILE's commands were found. Expansion uses FILE's variable set list. */
|
---|
897 |
|
---|
898 | char *
|
---|
899 | variable_expand_for_file (const char *line, struct file *file)
|
---|
900 | {
|
---|
901 | char *result;
|
---|
902 | struct variable_set_list *savev;
|
---|
903 | const struct floc *savef;
|
---|
904 |
|
---|
905 | if (file == 0)
|
---|
906 | return variable_expand (line);
|
---|
907 |
|
---|
908 | savev = current_variable_set_list;
|
---|
909 | current_variable_set_list = file->variables;
|
---|
910 |
|
---|
911 | savef = reading_file;
|
---|
912 | if (file->cmds && file->cmds->fileinfo.filenm)
|
---|
913 | reading_file = &file->cmds->fileinfo;
|
---|
914 | else
|
---|
915 | reading_file = 0;
|
---|
916 |
|
---|
917 | result = variable_expand (line);
|
---|
918 |
|
---|
919 | current_variable_set_list = savev;
|
---|
920 | reading_file = savef;
|
---|
921 |
|
---|
922 | return result;
|
---|
923 | }
|
---|
924 | |
---|
925 |
|
---|
926 | #if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
|
---|
927 | /* Expand LINE for FILE. Error messages refer to the file and line where
|
---|
928 | FILE's commands were found. Expansion uses FILE's variable set list.
|
---|
929 |
|
---|
930 | Differs from variable_expand_for_file in that it takes a pointer to
|
---|
931 | where in the variable buffer to start outputting the expanded string,
|
---|
932 | and that it can returned the length of the string if you wish. */
|
---|
933 |
|
---|
934 | char *
|
---|
935 | variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
|
---|
936 | struct file *file, unsigned int *value_lenp)
|
---|
937 | {
|
---|
938 | char *result;
|
---|
939 | struct variable_set_list *savev;
|
---|
940 | const struct floc *savef;
|
---|
941 | long len = length == ~0U ? (long)-1 : (long)length;
|
---|
942 | char *eol;
|
---|
943 |
|
---|
944 | if (!o)
|
---|
945 | o = initialize_variable_output();
|
---|
946 |
|
---|
947 | if (file == 0)
|
---|
948 | result = variable_expand_string_2 (o, line, len, &eol);
|
---|
949 | else
|
---|
950 | {
|
---|
951 | savev = current_variable_set_list;
|
---|
952 | current_variable_set_list = file->variables;
|
---|
953 |
|
---|
954 | savef = reading_file;
|
---|
955 | if (file->cmds && file->cmds->fileinfo.filenm)
|
---|
956 | reading_file = &file->cmds->fileinfo;
|
---|
957 | else
|
---|
958 | reading_file = 0;
|
---|
959 |
|
---|
960 | result = variable_expand_string_2 (o, line, len, &eol);
|
---|
961 |
|
---|
962 | current_variable_set_list = savev;
|
---|
963 | reading_file = savef;
|
---|
964 | }
|
---|
965 |
|
---|
966 | if (value_lenp)
|
---|
967 | *value_lenp = eol - result;
|
---|
968 |
|
---|
969 | return result;
|
---|
970 | }
|
---|
971 | |
---|
972 |
|
---|
973 | #endif /* CONFIG_WITH_VALUE_LENGTH || CONFIG_WITH_COMMANDS_FUNC */
|
---|
974 | /* Like allocated_variable_expand, but for += target-specific variables.
|
---|
975 | First recursively construct the variable value from its appended parts in
|
---|
976 | any upper variable sets. Then expand the resulting value. */
|
---|
977 |
|
---|
978 | static char *
|
---|
979 | variable_append (const char *name, unsigned int length,
|
---|
980 | const struct variable_set_list *set)
|
---|
981 | {
|
---|
982 | const struct variable *v;
|
---|
983 | char *buf = 0;
|
---|
984 |
|
---|
985 | /* If there's nothing left to check, return the empty buffer. */
|
---|
986 | if (!set)
|
---|
987 | return initialize_variable_output ();
|
---|
988 |
|
---|
989 | /* Try to find the variable in this variable set. */
|
---|
990 | v = lookup_variable_in_set (name, length, set->set);
|
---|
991 |
|
---|
992 | /* If there isn't one, look to see if there's one in a set above us. */
|
---|
993 | if (!v)
|
---|
994 | return variable_append (name, length, set->next);
|
---|
995 |
|
---|
996 | /* If this variable type is append, first get any upper values.
|
---|
997 | If not, initialize the buffer. */
|
---|
998 | if (v->append)
|
---|
999 | buf = variable_append (name, length, set->next);
|
---|
1000 | else
|
---|
1001 | buf = initialize_variable_output ();
|
---|
1002 |
|
---|
1003 | /* Append this value to the buffer, and return it.
|
---|
1004 | If we already have a value, first add a space. */
|
---|
1005 | if (buf > variable_buffer)
|
---|
1006 | buf = variable_buffer_output (buf, " ", 1);
|
---|
1007 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1008 | assert (v->value_length == strlen (v->value));
|
---|
1009 | #endif
|
---|
1010 |
|
---|
1011 | /* Either expand it or copy it, depending. */
|
---|
1012 | if (! v->recursive)
|
---|
1013 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1014 | return variable_buffer_output (buf, v->value, v->value_length);
|
---|
1015 | #else
|
---|
1016 | return variable_buffer_output (buf, v->value, strlen (v->value));
|
---|
1017 | #endif
|
---|
1018 |
|
---|
1019 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1020 | variable_expand_string_2 (buf, v->value, v->value_length, &buf);
|
---|
1021 | return buf;
|
---|
1022 | #else
|
---|
1023 | buf = variable_expand_string (buf, v->value, strlen (v->value));
|
---|
1024 | return (buf + strlen (buf));
|
---|
1025 | #endif
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1029 | /* Expands the specified string, appending it to the specified
|
---|
1030 | variable value. */
|
---|
1031 | void
|
---|
1032 | append_expanded_string_to_variable (struct variable *v, const char *value,
|
---|
1033 | unsigned int value_len, int append)
|
---|
1034 | {
|
---|
1035 | char *p = (char *) memchr (value, '$', value_len);
|
---|
1036 | if (!p)
|
---|
1037 | /* fast path */
|
---|
1038 | append_string_to_variable (v,value, value_len, append);
|
---|
1039 | else if (value_len)
|
---|
1040 | {
|
---|
1041 | unsigned int off_dollar = p - (char *)value;
|
---|
1042 |
|
---|
1043 | /* Install a fresh variable buffer. */
|
---|
1044 | char *saved_buffer;
|
---|
1045 | unsigned int saved_buffer_length;
|
---|
1046 | install_variable_buffer (&saved_buffer, &saved_buffer_length);
|
---|
1047 |
|
---|
1048 | p = variable_buffer;
|
---|
1049 | if (append || !v->value_length)
|
---|
1050 | {
|
---|
1051 | /* Copy the current value into it and append a space. */
|
---|
1052 | if (v->value_length)
|
---|
1053 | {
|
---|
1054 | p = variable_buffer_output (p, v->value, v->value_length);
|
---|
1055 | p = variable_buffer_output (p, " ", 1);
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | /* Append the assignment value. */
|
---|
1059 | p = variable_buffer_output (p, value, off_dollar);
|
---|
1060 | variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
|
---|
1061 | }
|
---|
1062 | else
|
---|
1063 | {
|
---|
1064 | /* Expand the assignemnt value. */
|
---|
1065 | p = variable_buffer_output (p, value, off_dollar);
|
---|
1066 | variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
|
---|
1067 |
|
---|
1068 | /* Append a space followed by the old value. */
|
---|
1069 | p = variable_buffer_output (p, " ", 1);
|
---|
1070 | p = variable_buffer_output (p, v->value, v->value_length + 1) - 1;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | /* Replace the variable with the variable buffer. */
|
---|
1074 | #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
|
---|
1075 | if (v->rdonly_val)
|
---|
1076 | v->rdonly_val = 0;
|
---|
1077 | else
|
---|
1078 | #endif
|
---|
1079 | free (v->value);
|
---|
1080 | v->value = variable_buffer;
|
---|
1081 | v->value_length = p - v->value;
|
---|
1082 | v->value_alloc_len = variable_buffer_length;
|
---|
1083 |
|
---|
1084 | /* Restore the variable buffer, but without freeing the current. */
|
---|
1085 | variable_buffer = NULL;
|
---|
1086 | restore_variable_buffer (saved_buffer, saved_buffer_length);
|
---|
1087 | }
|
---|
1088 | /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
|
---|
1089 | }
|
---|
1090 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
1091 |
|
---|
1092 | static char *
|
---|
1093 | allocated_variable_append (const struct variable *v)
|
---|
1094 | {
|
---|
1095 | char *val;
|
---|
1096 |
|
---|
1097 | /* Construct the appended variable value. */
|
---|
1098 |
|
---|
1099 | char *obuf = variable_buffer;
|
---|
1100 | unsigned int olen = variable_buffer_length;
|
---|
1101 |
|
---|
1102 | variable_buffer = 0;
|
---|
1103 |
|
---|
1104 | assert ((unsigned int)v->length == strlen (v->name)); /* bird */
|
---|
1105 | val = variable_append (v->name, strlen (v->name), current_variable_set_list);
|
---|
1106 | variable_buffer_output (val, "", 1);
|
---|
1107 | val = variable_buffer;
|
---|
1108 |
|
---|
1109 | variable_buffer = obuf;
|
---|
1110 | variable_buffer_length = olen;
|
---|
1111 |
|
---|
1112 | return val;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | /* Like variable_expand_for_file, but the returned string is malloc'd.
|
---|
1116 | This function is called a lot. It wants to be efficient. */
|
---|
1117 |
|
---|
1118 | char *
|
---|
1119 | allocated_variable_expand_for_file (const char *line, struct file *file)
|
---|
1120 | {
|
---|
1121 | char *value;
|
---|
1122 |
|
---|
1123 | char *obuf = variable_buffer;
|
---|
1124 | unsigned int olen = variable_buffer_length;
|
---|
1125 |
|
---|
1126 | variable_buffer = 0;
|
---|
1127 |
|
---|
1128 | value = variable_expand_for_file (line, file);
|
---|
1129 |
|
---|
1130 | variable_buffer = obuf;
|
---|
1131 | variable_buffer_length = olen;
|
---|
1132 |
|
---|
1133 | return value;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1137 | /* Handle the most common case in allocated_variable_expand_for_file
|
---|
1138 | specially and provide some additional string length features. */
|
---|
1139 |
|
---|
1140 | char *
|
---|
1141 | allocated_variable_expand_2 (const char *line, unsigned int length,
|
---|
1142 | unsigned int *value_lenp)
|
---|
1143 | {
|
---|
1144 | char *value;
|
---|
1145 | char *obuf = variable_buffer;
|
---|
1146 | unsigned int olen = variable_buffer_length;
|
---|
1147 | long len = length == ~0U ? -1L : (long)length;
|
---|
1148 | char *eol;
|
---|
1149 |
|
---|
1150 | variable_buffer = 0;
|
---|
1151 |
|
---|
1152 | value = variable_expand_string_2 (NULL, line, len, &eol);
|
---|
1153 | if (value_lenp)
|
---|
1154 | *value_lenp = eol - value;
|
---|
1155 |
|
---|
1156 | variable_buffer = obuf;
|
---|
1157 | variable_buffer_length = olen;
|
---|
1158 |
|
---|
1159 | return value;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /* Initially created for handling a special case for variable_expand_string2
|
---|
1163 | where the variable name is expanded and freed right afterwards. This
|
---|
1164 | variant allows the variable_buffer to be recycled and thus avoid bothering
|
---|
1165 | with a slow free implementation. (Darwin is horrible slow.) */
|
---|
1166 |
|
---|
1167 | char *
|
---|
1168 | allocated_variable_expand_3 (const char *line, unsigned int length,
|
---|
1169 | unsigned int *value_lenp,
|
---|
1170 | unsigned int *buffer_lengthp)
|
---|
1171 | {
|
---|
1172 | char *obuf = variable_buffer;
|
---|
1173 | unsigned int olen = variable_buffer_length;
|
---|
1174 | long len = (long)length;
|
---|
1175 | char *value;
|
---|
1176 | char *eol;
|
---|
1177 |
|
---|
1178 | variable_buffer = 0;
|
---|
1179 |
|
---|
1180 | value = variable_expand_string_2 (NULL, line, len, &eol);
|
---|
1181 | if (value_lenp)
|
---|
1182 | *value_lenp = eol - value;
|
---|
1183 | *buffer_lengthp = variable_buffer_length;
|
---|
1184 |
|
---|
1185 | variable_buffer = obuf;
|
---|
1186 | variable_buffer_length = olen;
|
---|
1187 |
|
---|
1188 | return value;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | /* recycle a buffer. */
|
---|
1192 |
|
---|
1193 | void
|
---|
1194 | recycle_variable_buffer (char *buffer, unsigned int length)
|
---|
1195 | {
|
---|
1196 | struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
|
---|
1197 |
|
---|
1198 | assert (!(length & 31));
|
---|
1199 | assert (length >= 384);
|
---|
1200 | recycled->length = length;
|
---|
1201 | recycled->next = recycled_head;
|
---|
1202 | recycled_head = recycled;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
1206 |
|
---|
1207 | /* Install a new variable_buffer context, returning the current one for
|
---|
1208 | safe-keeping. */
|
---|
1209 |
|
---|
1210 | void
|
---|
1211 | install_variable_buffer (char **bufp, unsigned int *lenp)
|
---|
1212 | {
|
---|
1213 | *bufp = variable_buffer;
|
---|
1214 | *lenp = variable_buffer_length;
|
---|
1215 |
|
---|
1216 | variable_buffer = 0;
|
---|
1217 | initialize_variable_output ();
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | #ifdef CONFIG_WITH_COMPILER
|
---|
1221 | /* Same as install_variable_buffer, except we supply a size hint. */
|
---|
1222 |
|
---|
1223 | char *
|
---|
1224 | install_variable_buffer_with_hint (char **bufp, unsigned int *lenp, unsigned int size_hint)
|
---|
1225 | {
|
---|
1226 | struct recycled_buffer *recycled;
|
---|
1227 | char *buf;
|
---|
1228 |
|
---|
1229 | *bufp = variable_buffer;
|
---|
1230 | *lenp = variable_buffer_length;
|
---|
1231 |
|
---|
1232 | recycled = recycled_head;
|
---|
1233 | if (recycled)
|
---|
1234 | {
|
---|
1235 | recycled_head = recycled->next;
|
---|
1236 | variable_buffer_length = recycled->length;
|
---|
1237 | variable_buffer = buf = (char *)recycled;
|
---|
1238 | }
|
---|
1239 | else
|
---|
1240 | {
|
---|
1241 | if (size_hint < 512)
|
---|
1242 | variable_buffer_length = (size_hint + 1 + 63) & ~(unsigned int)63;
|
---|
1243 | else if (size_hint < 4096)
|
---|
1244 | variable_buffer_length = (size_hint + 1 + 1023) & ~(unsigned int)1023;
|
---|
1245 | else
|
---|
1246 | variable_buffer_length = (size_hint + 1 + 4095) & ~(unsigned int)4095;
|
---|
1247 | variable_buffer = buf = xmalloc (variable_buffer_length);
|
---|
1248 | }
|
---|
1249 | buf[0] = '\0';
|
---|
1250 | return buf;
|
---|
1251 | }
|
---|
1252 | #endif /* CONFIG_WITH_COMPILER */
|
---|
1253 |
|
---|
1254 | /* Restore a previously-saved variable_buffer setting (free the
|
---|
1255 | current one). */
|
---|
1256 |
|
---|
1257 | void
|
---|
1258 | restore_variable_buffer (char *buf, unsigned int len)
|
---|
1259 | {
|
---|
1260 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
1261 | free (variable_buffer);
|
---|
1262 | #else
|
---|
1263 | if (variable_buffer)
|
---|
1264 | recycle_variable_buffer (variable_buffer, variable_buffer_length);
|
---|
1265 | #endif
|
---|
1266 |
|
---|
1267 | variable_buffer = buf;
|
---|
1268 | variable_buffer_length = len;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 |
|
---|
1272 | /* Used to make sure there is at least SIZE bytes of buffer space
|
---|
1273 | available starting at PTR. */
|
---|
1274 | char *
|
---|
1275 | ensure_variable_buffer_space(char *ptr, unsigned int size)
|
---|
1276 | {
|
---|
1277 | unsigned int offset = (unsigned int)(ptr - variable_buffer);
|
---|
1278 | assert(offset <= variable_buffer_length);
|
---|
1279 | if (variable_buffer_length - offset < size)
|
---|
1280 | {
|
---|
1281 | unsigned minlen = size + offset;
|
---|
1282 | variable_buffer_length *= 2;
|
---|
1283 | if (variable_buffer_length < minlen + 100)
|
---|
1284 | variable_buffer_length = (minlen + 100 + 63) & ~(unsigned int)63;
|
---|
1285 | variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
|
---|
1286 | ptr = variable_buffer + offset;
|
---|
1287 | }
|
---|
1288 | return ptr;
|
---|
1289 | }
|
---|
1290 |
|
---|