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