kbuild.c: added todo for #80.
[kbuild-mirror.git] / src / kmk / expand.c
blob554624ae24612f595f5af90beb1b75ae05e64512
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 Free Software
4 Foundation, Inc.
5 This file is part of GNU Make.
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.
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.
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/>. */
19 #include "make.h"
21 #include <assert.h>
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
27 #include "rule.h"
29 /* Initially, any errors reported when expanding strings will be reported
30 against the file where the error appears. */
31 const struct floc **expanding_var = &reading_file;
33 /* The next two describe the variable output buffer.
34 This buffer is used to hold the variable-expansion of a line of the
35 makefile. It is made bigger with realloc whenever it is too small.
36 variable_buffer_length is the size currently allocated.
37 variable_buffer is the address of the buffer.
39 For efficiency, it's guaranteed that the buffer will always have
40 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
41 extra chars without having to call a function. Note you should never use
42 these bytes unless you're _sure_ you have room (you know when the buffer
43 length was last checked. */
45 #define VARIABLE_BUFFER_ZONE 5
47 #ifndef KMK
48 static unsigned int variable_buffer_length;
49 #else
50 unsigned int variable_buffer_length;
51 #endif
52 char *variable_buffer;
55 #ifdef CONFIG_WITH_VALUE_LENGTH
56 struct recycled_buffer
58 struct recycled_buffer *next;
59 unsigned int length;
61 struct recycled_buffer *recycled_head;
62 #endif /* CONFIG_WITH_VALUE_LENGTH */
66 #ifndef KMK
67 /* Subroutine of variable_expand and friends:
68 The text to add is LENGTH chars starting at STRING to the variable_buffer.
69 The text is added to the buffer at PTR, and the updated pointer into
70 the buffer is returned as the value. Thus, the value returned by
71 each call to variable_buffer_output should be the first argument to
72 the following call. */
74 char *
75 variable_buffer_output (char *ptr, const char *string, unsigned int length)
77 register unsigned int newlen = length + (ptr - variable_buffer);
79 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
81 unsigned int offset = ptr - variable_buffer;
82 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
83 ? newlen + 100
84 : 2 * variable_buffer_length);
85 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
86 ptr = variable_buffer + offset;
89 memcpy (ptr, string, length);
90 return ptr + length;
92 #endif
94 /* Return a pointer to the beginning of the variable buffer. */
96 static char *
97 initialize_variable_output (void)
99 /* If we don't have a variable output buffer yet, get one. */
101 #ifdef CONFIG_WITH_VALUE_LENGTH
102 if (variable_buffer == 0)
104 struct recycled_buffer *recycled = recycled_head;
105 if (recycled)
107 recycled_head = recycled->next;
108 variable_buffer_length = recycled->length;
109 variable_buffer = (char *)recycled;
111 else
113 variable_buffer_length = 384;
114 variable_buffer = xmalloc (variable_buffer_length);
116 variable_buffer[0] = '\0';
118 #else /* CONFIG_WITH_VALUE_LENGTH */
119 if (variable_buffer == 0)
121 variable_buffer_length = 200;
122 variable_buffer = xmalloc (variable_buffer_length);
123 variable_buffer[0] = '\0';
125 #endif /* CONFIG_WITH_VALUE_LENGTH */
127 return variable_buffer;
130 /* Recursively expand V. The returned string is malloc'd. */
132 static char *allocated_variable_append (const struct variable *v);
134 char *
135 #ifndef CONFIG_WITH_VALUE_LENGTH
136 recursively_expand_for_file (struct variable *v, struct file *file)
137 #else
138 recursively_expand_for_file (struct variable *v, struct file *file,
139 unsigned int *value_lenp)
140 #endif
142 char *value;
143 const struct floc *this_var;
144 const struct floc **saved_varp;
145 struct variable_set_list *save = 0;
146 int set_reading = 0;
148 /* Don't install a new location if this location is empty.
149 This can happen for command-line variables, builtin variables, etc. */
150 saved_varp = expanding_var;
151 if (v->fileinfo.filenm)
153 this_var = &v->fileinfo;
154 expanding_var = &this_var;
157 /* If we have no other file-reading context, use the variable's context. */
158 if (!reading_file)
160 set_reading = 1;
161 reading_file = &v->fileinfo;
164 if (v->expanding)
166 if (!v->exp_count)
167 /* Expanding V causes infinite recursion. Lose. */
168 fatal (*expanding_var,
169 _("Recursive variable `%s' references itself (eventually)"),
170 v->name);
171 --v->exp_count;
174 if (file)
176 save = current_variable_set_list;
177 current_variable_set_list = file->variables;
180 v->expanding = 1;
181 #ifndef CONFIG_WITH_VALUE_LENGTH
182 if (v->append)
183 value = allocated_variable_append (v);
184 else
185 value = allocated_variable_expand (v->value);
186 #else /* CONFIG_WITH_VALUE_LENGTH */
187 if (!v->append)
188 value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
189 else
191 value = allocated_variable_append (v);
192 if (value_lenp)
193 *value_lenp = strlen (value);
195 #endif /* CONFIG_WITH_VALUE_LENGTH */
196 v->expanding = 0;
198 if (set_reading)
199 reading_file = 0;
201 if (file)
202 current_variable_set_list = save;
204 expanding_var = saved_varp;
206 return value;
209 #ifdef CONFIG_WITH_VALUE_LENGTH
210 /* Static worker for reference_variable() that expands the recursive
211 variable V. The main difference between this and
212 recursively_expand[_for_file] is that this worker avoids the temporary
213 buffer and outputs directly into the current variable buffer (O). */
214 static char *
215 reference_recursive_variable (char *o, struct variable *v)
217 const struct floc *this_var;
218 const struct floc **saved_varp;
219 int set_reading = 0;
221 /* Don't install a new location if this location is empty.
222 This can happen for command-line variables, builtin variables, etc. */
223 saved_varp = expanding_var;
224 if (v->fileinfo.filenm)
226 this_var = &v->fileinfo;
227 expanding_var = &this_var;
230 /* If we have no other file-reading context, use the variable's context. */
231 if (!reading_file)
233 set_reading = 1;
234 reading_file = &v->fileinfo;
237 if (v->expanding)
239 if (!v->exp_count)
240 /* Expanding V causes infinite recursion. Lose. */
241 fatal (*expanding_var,
242 _("Recursive variable `%s' references itself (eventually)"),
243 v->name);
244 --v->exp_count;
247 v->expanding = 1;
248 if (!v->append)
249 /* Expand directly into the variable buffer. */
250 variable_expand_string_2 (o, v->value, v->value_length, &o);
251 else
253 /* XXX: Feel free to optimize appending target variables as well. */
254 char *value = allocated_variable_append (v);
255 unsigned int value_len = strlen (value);
256 o = variable_buffer_output (o, value, value_len);
257 free (value);
259 v->expanding = 0;
261 if (set_reading)
262 reading_file = 0;
264 expanding_var = saved_varp;
266 return o;
268 #endif /* CONFIG_WITH_VALUE_LENGTH */
270 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
272 #ifdef MY_INLINE /* bird */
273 MY_INLINE char *
274 #else
275 #if defined(__GNUC__)
276 __inline
277 #endif
278 static char *
279 #endif
280 reference_variable (char *o, const char *name, unsigned int length)
282 struct variable *v;
283 #ifndef CONFIG_WITH_VALUE_LENGTH
284 char *value;
285 #endif
287 v = lookup_variable (name, length);
289 if (v == 0)
290 warn_undefined (name, length);
292 /* If there's no variable by that name or it has no value, stop now. */
293 if (v == 0 || (*v->value == '\0' && !v->append))
294 return o;
296 #ifdef CONFIG_WITH_VALUE_LENGTH
297 assert (v->value_length == strlen (v->value));
298 if (!v->recursive)
299 o = variable_buffer_output (o, v->value, v->value_length);
300 else
301 o = reference_recursive_variable (o, v);
302 #else /* !CONFIG_WITH_VALUE_LENGTH */
303 value = (v->recursive ? recursively_expand (v) : v->value);
305 o = variable_buffer_output (o, value, strlen (value));
307 if (v->recursive)
308 free (value);
309 #endif /* !CONFIG_WITH_VALUE_LENGTH */
311 return o;
314 #ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
315 /* Scan STRING for variable references and expansion-function calls. Only
316 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
317 a null byte is found.
319 Write the results to LINE, which must point into `variable_buffer'. If
320 LINE is NULL, start at the beginning of the buffer.
321 Return a pointer to LINE, or to the beginning of the buffer if LINE is
322 NULL.
324 char *
325 variable_expand_string (char *line, const char *string, long length)
327 struct variable *v;
328 const char *p, *p1;
329 char *abuf = NULL;
330 char *o;
331 unsigned int line_offset;
333 if (!line)
334 line = initialize_variable_output();
335 o = line;
336 line_offset = line - variable_buffer;
338 if (length == 0)
340 variable_buffer_output (o, "", 1);
341 return (variable_buffer);
344 /* If we want a subset of the string, allocate a temporary buffer for it.
345 Most of the functions we use here don't work with length limits. */
346 if (length > 0 && string[length] != '\0')
348 abuf = xmalloc(length+1);
349 memcpy(abuf, string, length);
350 abuf[length] = '\0';
351 string = abuf;
353 p = string;
355 while (1)
357 /* Copy all following uninteresting chars all at once to the
358 variable output buffer, and skip them. Uninteresting chars end
359 at the next $ or the end of the input. */
361 p1 = strchr (p, '$');
363 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
365 if (p1 == 0)
366 break;
367 p = p1 + 1;
369 /* Dispatch on the char that follows the $. */
371 switch (*p)
373 case '$':
374 /* $$ seen means output one $ to the variable output buffer. */
375 o = variable_buffer_output (o, p, 1);
376 break;
378 case '(':
379 case '{':
380 /* $(...) or ${...} is the general case of substitution. */
382 char openparen = *p;
383 char closeparen = (openparen == '(') ? ')' : '}';
384 const char *begp;
385 const char *beg = p + 1;
386 char *op;
387 char *abeg = NULL;
388 const char *end, *colon;
390 op = o;
391 begp = p;
392 if (handle_function (&op, &begp))
394 o = op;
395 p = begp;
396 break;
399 /* Is there a variable reference inside the parens or braces?
400 If so, expand it before expanding the entire reference. */
402 end = strchr (beg, closeparen);
403 if (end == 0)
404 /* Unterminated variable reference. */
405 fatal (*expanding_var, _("unterminated variable reference"));
406 p1 = lindex (beg, end, '$');
407 if (p1 != 0)
409 /* BEG now points past the opening paren or brace.
410 Count parens or braces until it is matched. */
411 int count = 0;
412 for (p = beg; *p != '\0'; ++p)
414 if (*p == openparen)
415 ++count;
416 else if (*p == closeparen && --count < 0)
417 break;
419 /* If COUNT is >= 0, there were unmatched opening parens
420 or braces, so we go to the simple case of a variable name
421 such as `$($(a)'. */
422 if (count < 0)
424 abeg = expand_argument (beg, p); /* Expand the name. */
425 beg = abeg;
426 end = strchr (beg, '\0');
429 else
430 /* Advance P to the end of this reference. After we are
431 finished expanding this one, P will be incremented to
432 continue the scan. */
433 p = end;
435 /* This is not a reference to a built-in function and
436 any variable references inside are now expanded.
437 Is the resultant text a substitution reference? */
439 colon = lindex (beg, end, ':');
440 if (colon)
442 /* This looks like a substitution reference: $(FOO:A=B). */
443 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
445 subst_beg = colon + 1;
446 subst_end = lindex (subst_beg, end, '=');
447 if (subst_end == 0)
448 /* There is no = in sight. Punt on the substitution
449 reference and treat this as a variable name containing
450 a colon, in the code below. */
451 colon = 0;
452 else
454 replace_beg = subst_end + 1;
455 replace_end = end;
457 /* Extract the variable name before the colon
458 and look up that variable. */
459 v = lookup_variable (beg, colon - beg);
460 if (v == 0)
461 warn_undefined (beg, colon - beg);
463 /* If the variable is not empty, perform the
464 substitution. */
465 if (v != 0 && *v->value != '\0')
467 char *pattern, *replace, *ppercent, *rpercent;
468 char *value = (v->recursive
469 ? recursively_expand (v)
470 : v->value);
472 /* Copy the pattern and the replacement. Add in an
473 extra % at the beginning to use in case there
474 isn't one in the pattern. */
475 pattern = alloca (subst_end - subst_beg + 2);
476 *(pattern++) = '%';
477 memcpy (pattern, subst_beg, subst_end - subst_beg);
478 pattern[subst_end - subst_beg] = '\0';
480 replace = alloca (replace_end - replace_beg + 2);
481 *(replace++) = '%';
482 memcpy (replace, replace_beg,
483 replace_end - replace_beg);
484 replace[replace_end - replace_beg] = '\0';
486 /* Look for %. Set the percent pointers properly
487 based on whether we find one or not. */
488 ppercent = find_percent (pattern);
489 if (ppercent)
491 ++ppercent;
492 rpercent = find_percent (replace);
493 if (rpercent)
494 ++rpercent;
496 else
498 ppercent = pattern;
499 rpercent = replace;
500 --pattern;
501 --replace;
504 o = patsubst_expand_pat (o, value, pattern, replace,
505 ppercent, rpercent);
507 if (v->recursive)
508 free (value);
513 if (colon == 0)
514 /* This is an ordinary variable reference.
515 Look up the value of the variable. */
516 o = reference_variable (o, beg, end - beg);
518 if (abeg)
519 free (abeg);
521 break;
523 case '\0':
524 break;
526 default:
527 if (isblank ((unsigned char)p[-1]))
528 break;
530 /* A $ followed by a random char is a variable reference:
531 $a is equivalent to $(a). */
532 o = reference_variable (o, p, 1);
534 break;
537 if (*p == '\0')
538 break;
539 else
540 ++p;
543 if (abuf)
544 free (abuf);
546 variable_buffer_output (o, "", 1);
547 return (variable_buffer + line_offset);
550 #else /* CONFIG_WITH_VALUE_LENGTH */
551 /* Scan STRING for variable references and expansion-function calls. Only
552 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
553 a null byte is found.
555 Write the results to LINE, which must point into `variable_buffer'. If
556 LINE is NULL, start at the beginning of the buffer.
557 Return a pointer to LINE, or to the beginning of the buffer if LINE is
558 NULL. Set EOLP to point to the string terminator.
560 char *
561 variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
563 struct variable *v;
564 const char *p, *p1, *eos;
565 char *o;
566 unsigned int line_offset;
568 if (!line)
569 line = initialize_variable_output();
570 o = line;
571 line_offset = line - variable_buffer;
573 if (length < 0)
574 length = strlen (string);
575 else
576 MY_ASSERT_MSG (string + length == (p1 = memchr (string, '\0', length)) || !p1, ("len=%ld p1=%p %s\n", length, p1, line));
578 /* Simple 1: Emptry string. */
580 if (length == 0)
582 o = variable_buffer_output (o, "\0", 2);
583 *eolp = o - 2;
584 return (variable_buffer + line_offset);
587 /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
589 p1 = (const char *)memchr (string, '$', length);
590 if (p1 == 0)
592 o = variable_buffer_output (o, string, length);
593 o = variable_buffer_output (o, "\0", 2);
594 *eolp = o - 2;
595 assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
596 return (variable_buffer + line_offset);
599 p = string;
600 eos = p + length;
602 while (1)
604 /* Copy all following uninteresting chars all at once to the
605 variable output buffer, and skip them. Uninteresting chars end
606 at the next $ or the end of the input. */
608 o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
610 if (p1 == 0)
611 break;
612 p = p1 + 1;
614 /* Dispatch on the char that follows the $. */
616 switch (*p)
618 case '$':
619 /* $$ seen means output one $ to the variable output buffer. */
620 o = variable_buffer_output (o, p, 1);
621 break;
623 case '(':
624 case '{':
625 /* $(...) or ${...} is the general case of substitution. */
627 char openparen = *p;
628 char closeparen = (openparen == '(') ? ')' : '}';
629 const char *begp;
630 const char *beg = p + 1;
631 char *op;
632 char *abeg = NULL;
633 unsigned int alen = 0;
634 const char *end, *colon;
636 op = o;
637 begp = p;
638 end = may_be_function_name (p + 1, eos);
639 if ( end
640 && handle_function (&op, &begp, end, eos))
642 o = op;
643 p = begp;
644 MY_ASSERT_MSG (!(p1 = memchr (variable_buffer + line_offset, '\0', o - (variable_buffer + line_offset))),
645 ("line=%p o/exp_end=%p act_end=%p\n", variable_buffer + line_offset, o, p1));
646 break;
649 /* Is there a variable reference inside the parens or braces?
650 If so, expand it before expanding the entire reference. */
652 end = memchr (beg, closeparen, eos - beg);
653 if (end == 0)
654 /* Unterminated variable reference. */
655 fatal (*expanding_var, _("unterminated variable reference"));
656 p1 = lindex (beg, end, '$');
657 if (p1 != 0)
659 /* BEG now points past the opening paren or brace.
660 Count parens or braces until it is matched. */
661 int count = 0;
662 for (p = beg; p < eos; ++p)
664 if (*p == openparen)
665 ++count;
666 else if (*p == closeparen && --count < 0)
667 break;
669 /* If COUNT is >= 0, there were unmatched opening parens
670 or braces, so we go to the simple case of a variable name
671 such as `$($(a)'. */
672 if (count < 0)
674 unsigned int len;
675 char saved;
677 /* Expand the name. */
678 saved = *p;
679 *(char *)p = '\0'; /* XXX: proove that this is safe! XXX2: shouldn't be necessary any longer! */
680 abeg = allocated_variable_expand_3 (beg, p - beg, &len, &alen);
681 beg = abeg;
682 end = beg + len;
683 *(char *)p = saved;
686 else
687 /* Advance P to the end of this reference. After we are
688 finished expanding this one, P will be incremented to
689 continue the scan. */
690 p = end;
692 /* This is not a reference to a built-in function and
693 any variable references inside are now expanded.
694 Is the resultant text a substitution reference? */
696 colon = lindex (beg, end, ':');
697 if (colon)
699 /* This looks like a substitution reference: $(FOO:A=B). */
700 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
702 subst_beg = colon + 1;
703 subst_end = lindex (subst_beg, end, '=');
704 if (subst_end == 0)
705 /* There is no = in sight. Punt on the substitution
706 reference and treat this as a variable name containing
707 a colon, in the code below. */
708 colon = 0;
709 else
711 replace_beg = subst_end + 1;
712 replace_end = end;
714 /* Extract the variable name before the colon
715 and look up that variable. */
716 v = lookup_variable (beg, colon - beg);
717 if (v == 0)
718 warn_undefined (beg, colon - beg);
720 /* If the variable is not empty, perform the
721 substitution. */
722 if (v != 0 && *v->value != '\0')
724 char *pattern, *replace, *ppercent, *rpercent;
725 char *value = (v->recursive
726 ? recursively_expand (v)
727 : v->value);
729 /* Copy the pattern and the replacement. Add in an
730 extra % at the beginning to use in case there
731 isn't one in the pattern. */
732 pattern = alloca (subst_end - subst_beg + 2);
733 *(pattern++) = '%';
734 memcpy (pattern, subst_beg, subst_end - subst_beg);
735 pattern[subst_end - subst_beg] = '\0';
737 replace = alloca (replace_end - replace_beg + 2);
738 *(replace++) = '%';
739 memcpy (replace, replace_beg,
740 replace_end - replace_beg);
741 replace[replace_end - replace_beg] = '\0';
743 /* Look for %. Set the percent pointers properly
744 based on whether we find one or not. */
745 ppercent = find_percent (pattern);
746 if (ppercent)
748 ++ppercent;
749 rpercent = find_percent (replace);
750 if (rpercent)
751 ++rpercent;
753 else
755 ppercent = pattern;
756 rpercent = replace;
757 --pattern;
758 --replace;
761 o = patsubst_expand_pat (o, value, pattern, replace,
762 ppercent, rpercent);
764 if (v->recursive)
765 free (value);
770 if (colon == 0)
771 /* This is an ordinary variable reference.
772 Look up the value of the variable. */
773 o = reference_variable (o, beg, end - beg);
775 if (abeg)
776 recycle_variable_buffer (abeg, alen);
778 break;
780 case '\0':
781 assert (p == eos);
782 break;
784 default:
785 if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
786 break;
788 /* A $ followed by a random char is a variable reference:
789 $a is equivalent to $(a). */
790 o = reference_variable (o, p, 1);
792 break;
795 if (++p >= eos)
796 break;
797 p1 = memchr (p, '$', eos - p);
800 o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
801 *eolp = o - 2;
802 MY_ASSERT_MSG (strchr (variable_buffer + line_offset, '\0') == *eolp,
803 ("expected=%d actual=%d\nlength=%ld string=%.*s\n",
804 (int)(*eolp - variable_buffer + line_offset), (int)strlen(variable_buffer + line_offset),
805 length, (int)length, string));
806 return (variable_buffer + line_offset);
808 #endif /* CONFIG_WITH_VALUE_LENGTH */
810 /* Scan LINE for variable references and expansion-function calls.
811 Build in `variable_buffer' the result of expanding the references and calls.
812 Return the address of the resulting string, which is null-terminated
813 and is valid only until the next time this function is called. */
815 char *
816 variable_expand (const char *line)
818 #ifndef CONFIG_WITH_VALUE_LENGTH
819 return variable_expand_string(NULL, line, (long)-1);
820 #else /* CONFIG_WITH_VALUE_LENGTH */
821 char *s;
823 /* this function is abused a lot like this: variable_expand(""). */
824 if (!*line)
826 s = variable_buffer_output (initialize_variable_output (), "\0", 2);
827 return s - 2;
829 return variable_expand_string_2 (NULL, line, (long)-1, &s);
830 #endif /* CONFIG_WITH_VALUE_LENGTH */
833 /* Expand an argument for an expansion function.
834 The text starting at STR and ending at END is variable-expanded
835 into a null-terminated string that is returned as the value.
836 This is done without clobbering `variable_buffer' or the current
837 variable-expansion that is in progress. */
839 char *
840 expand_argument (const char *str, const char *end)
842 #ifndef CONFIG_WITH_VALUE_LENGTH
843 char *tmp;
844 #endif
846 if (str == end)
847 return xstrdup("");
849 #ifndef CONFIG_WITH_VALUE_LENGTH
850 if (!end || *end == '\0')
851 return allocated_variable_expand (str);
853 tmp = alloca (end - str + 1);
854 memcpy (tmp, str, end - str);
855 tmp[end - str] = '\0';
857 return allocated_variable_expand (tmp);
858 #else /* CONFIG_WITH_VALUE_LENGTH */
859 if (!end)
860 return allocated_variable_expand_2 (str, ~0U, NULL);
861 return allocated_variable_expand_2 (str, end - str, NULL);
862 #endif /* CONFIG_WITH_VALUE_LENGTH */
865 /* Expand LINE for FILE. Error messages refer to the file and line where
866 FILE's commands were found. Expansion uses FILE's variable set list. */
868 char *
869 variable_expand_for_file (const char *line, struct file *file)
871 char *result;
872 struct variable_set_list *savev;
873 const struct floc *savef;
875 if (file == 0)
876 return variable_expand (line);
878 savev = current_variable_set_list;
879 current_variable_set_list = file->variables;
881 savef = reading_file;
882 if (file->cmds && file->cmds->fileinfo.filenm)
883 reading_file = &file->cmds->fileinfo;
884 else
885 reading_file = 0;
887 result = variable_expand (line);
889 current_variable_set_list = savev;
890 reading_file = savef;
892 return result;
895 #if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
896 /* Expand LINE for FILE. Error messages refer to the file and line where
897 FILE's commands were found. Expansion uses FILE's variable set list.
899 Differs from variable_expand_for_file in that it takes a pointer to
900 where in the variable buffer to start outputting the expanded string,
901 and that it can returned the length of the string if you wish. */
903 char *
904 variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
905 struct file *file, unsigned int *value_lenp)
907 char *result;
908 struct variable_set_list *savev;
909 const struct floc *savef;
910 long len = length == ~0U ? (long)-1 : (long)length;
911 char *eol;
913 if (!o)
914 o = initialize_variable_output();
916 if (file == 0)
917 result = variable_expand_string_2 (o, line, len, &eol);
918 else
920 savev = current_variable_set_list;
921 current_variable_set_list = file->variables;
923 savef = reading_file;
924 if (file->cmds && file->cmds->fileinfo.filenm)
925 reading_file = &file->cmds->fileinfo;
926 else
927 reading_file = 0;
929 result = variable_expand_string_2 (o, line, len, &eol);
931 current_variable_set_list = savev;
932 reading_file = savef;
935 if (value_lenp)
936 *value_lenp = eol - result;
938 return result;
941 #endif /* CONFIG_WITH_VALUE_LENGTH || CONFIG_WITH_COMMANDS_FUNC */
942 /* Like allocated_variable_expand, but for += target-specific variables.
943 First recursively construct the variable value from its appended parts in
944 any upper variable sets. Then expand the resulting value. */
946 static char *
947 variable_append (const char *name, unsigned int length,
948 const struct variable_set_list *set)
950 const struct variable *v;
951 char *buf = 0;
953 /* If there's nothing left to check, return the empty buffer. */
954 if (!set)
955 return initialize_variable_output ();
957 /* Try to find the variable in this variable set. */
958 v = lookup_variable_in_set (name, length, set->set);
960 /* If there isn't one, look to see if there's one in a set above us. */
961 if (!v)
962 return variable_append (name, length, set->next);
964 /* If this variable type is append, first get any upper values.
965 If not, initialize the buffer. */
966 if (v->append)
967 buf = variable_append (name, length, set->next);
968 else
969 buf = initialize_variable_output ();
971 /* Append this value to the buffer, and return it.
972 If we already have a value, first add a space. */
973 if (buf > variable_buffer)
974 buf = variable_buffer_output (buf, " ", 1);
975 #ifdef CONFIG_WITH_VALUE_LENGTH
976 assert (v->value_length == strlen (v->value));
977 #endif
979 /* Either expand it or copy it, depending. */
980 if (! v->recursive)
981 #ifdef CONFIG_WITH_VALUE_LENGTH
982 return variable_buffer_output (buf, v->value, v->value_length);
983 #else
984 return variable_buffer_output (buf, v->value, strlen (v->value));
985 #endif
987 #ifdef CONFIG_WITH_VALUE_LENGTH
988 variable_expand_string_2 (buf, v->value, v->value_length, &buf);
989 return buf;
990 #else
991 buf = variable_expand_string (buf, v->value, strlen (v->value));
992 return (buf + strlen (buf));
993 #endif
996 #ifdef CONFIG_WITH_VALUE_LENGTH
997 /* Expands the specified string, appending it to the specified
998 variable value. */
999 void
1000 append_expanded_string_to_variable (struct variable *v, const char *value,
1001 unsigned int value_len, int append)
1003 char *p = (char *) memchr (value, '$', value_len);
1004 if (!p)
1005 /* fast path */
1006 append_string_to_variable (v,value, value_len, append);
1007 else if (value_len)
1009 unsigned int off_dollar = p - (char *)value;
1011 /* Install a fresh variable buffer. */
1012 char *saved_buffer;
1013 unsigned int saved_buffer_length;
1014 install_variable_buffer (&saved_buffer, &saved_buffer_length);
1016 p = variable_buffer;
1017 if (append || !v->value_length)
1019 /* Copy the current value into it and append a space. */
1020 if (v->value_length)
1022 p = variable_buffer_output (p, v->value, v->value_length);
1023 p = variable_buffer_output (p, " ", 1);
1026 /* Append the assignment value. */
1027 p = variable_buffer_output (p, value, off_dollar);
1028 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
1030 else
1032 /* Expand the assignemnt value. */
1033 p = variable_buffer_output (p, value, off_dollar);
1034 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
1036 /* Append a space followed by the old value. */
1037 p = variable_buffer_output (p, " ", 1);
1038 p = variable_buffer_output (p, v->value, v->value_length + 1) - 1;
1041 /* Replace the variable with the variable buffer. */
1042 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1043 if (v->rdonly_val)
1044 v->rdonly_val = 0;
1045 else
1046 #endif
1047 free (v->value);
1048 v->value = variable_buffer;
1049 v->value_length = p - v->value;
1050 v->value_alloc_len = variable_buffer_length;
1052 /* Restore the variable buffer, but without freeing the current. */
1053 variable_buffer = NULL;
1054 restore_variable_buffer (saved_buffer, saved_buffer_length);
1056 /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1058 #endif /* CONFIG_WITH_VALUE_LENGTH */
1060 static char *
1061 allocated_variable_append (const struct variable *v)
1063 char *val;
1065 /* Construct the appended variable value. */
1067 char *obuf = variable_buffer;
1068 unsigned int olen = variable_buffer_length;
1070 variable_buffer = 0;
1072 assert ((unsigned int)v->length == strlen (v->name)); /* bird */
1073 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
1074 variable_buffer_output (val, "", 1);
1075 val = variable_buffer;
1077 variable_buffer = obuf;
1078 variable_buffer_length = olen;
1080 return val;
1083 /* Like variable_expand_for_file, but the returned string is malloc'd.
1084 This function is called a lot. It wants to be efficient. */
1086 char *
1087 allocated_variable_expand_for_file (const char *line, struct file *file)
1089 char *value;
1091 char *obuf = variable_buffer;
1092 unsigned int olen = variable_buffer_length;
1094 variable_buffer = 0;
1096 value = variable_expand_for_file (line, file);
1098 #if 0
1099 /* Waste a little memory and save time. */
1100 value = xrealloc (value, strlen (value))
1101 #endif
1103 variable_buffer = obuf;
1104 variable_buffer_length = olen;
1106 return value;
1109 #ifdef CONFIG_WITH_VALUE_LENGTH
1110 /* Handle the most common case in allocated_variable_expand_for_file
1111 specially and provide some additional string lenght features. */
1113 char *
1114 allocated_variable_expand_2 (const char *line, unsigned int length,
1115 unsigned int *value_lenp)
1117 char *value;
1118 char *obuf = variable_buffer;
1119 unsigned int olen = variable_buffer_length;
1120 long len = length == ~0U ? -1L : (long)length;
1121 char *eol;
1123 variable_buffer = 0;
1125 value = variable_expand_string_2 (NULL, line, len, &eol);
1126 if (value_lenp)
1127 *value_lenp = eol - value;
1129 variable_buffer = obuf;
1130 variable_buffer_length = olen;
1132 return value;
1135 /* Initially created for handling a special case for variable_expand_string2
1136 where the variable name is expanded and freed right afterwards. This
1137 variant allows the variable_buffer to be recycled and thus avoid bothering
1138 with a slow free implementation. (Darwin is horrible slow.) */
1140 char *
1141 allocated_variable_expand_3 (const char *line, unsigned int length,
1142 unsigned int *value_lenp,
1143 unsigned int *buffer_lengthp)
1145 char *obuf = variable_buffer;
1146 unsigned int olen = variable_buffer_length;
1147 long len = (long)length;
1148 char *value;
1149 char *eol;
1151 variable_buffer = 0;
1153 value = variable_expand_string_2 (NULL, line, len, &eol);
1154 if (value_lenp)
1155 *value_lenp = eol - value;
1156 *buffer_lengthp = variable_buffer_length;
1158 variable_buffer = obuf;
1159 variable_buffer_length = olen;
1161 return value;
1164 /* recycle a buffer. */
1166 void
1167 recycle_variable_buffer (char *buffer, unsigned int length)
1169 struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
1171 assert (!(length & 31));
1172 assert (length >= 384);
1173 recycled->length = length;
1174 recycled->next = recycled_head;
1175 recycled_head = recycled;
1178 #endif /* CONFIG_WITH_VALUE_LENGTH */
1180 /* Install a new variable_buffer context, returning the current one for
1181 safe-keeping. */
1183 void
1184 install_variable_buffer (char **bufp, unsigned int *lenp)
1186 *bufp = variable_buffer;
1187 *lenp = variable_buffer_length;
1189 variable_buffer = 0;
1190 initialize_variable_output ();
1193 /* Restore a previously-saved variable_buffer setting (free the current one).
1196 void
1197 restore_variable_buffer (char *buf, unsigned int len)
1199 #ifndef CONFIG_WITH_VALUE_LENGTH
1200 free (variable_buffer);
1201 #else
1202 if (variable_buffer)
1203 recycle_variable_buffer (variable_buffer, variable_buffer_length);
1204 #endif
1206 variable_buffer = buf;
1207 variable_buffer_length = len;