source: trunk/src/kmk/expand.c@ 1985

Last change on this file since 1985 was 1985, checked in by bird, 17 years ago

expand.c: avoid some unnecessary double buffering done by reference_variable / recursively_expand[_for_file], reduing the alloc sum from 783MB to 567MB (and malloc/realloc call count even more) on a typical kBuild run.

  • Property svn:eol-style set to native
File size: 34.7 KB
Line 
1/* Variable expansion functions for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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
29/* Initially, any errors reported when expanding strings will be reported
30 against the file where the error appears. */
31const struct floc **expanding_var = &reading_file;
32
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.
38
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. */
44
45#define VARIABLE_BUFFER_ZONE 5
46
47#ifndef KMK
48static unsigned int variable_buffer_length;
49#else
50unsigned int variable_buffer_length;
51#endif
52char *variable_buffer;
53
54
55#ifdef CONFIG_WITH_VALUE_LENGTH
56struct recycled_buffer
57{
58 struct recycled_buffer *next;
59 unsigned int length;
60};
61struct recycled_buffer *recycled_head;
62#endif /* CONFIG_WITH_VALUE_LENGTH */
63
64
65
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. */
73
74char *
75variable_buffer_output (char *ptr, const char *string, unsigned int length)
76{
77 register unsigned int newlen = length + (ptr - variable_buffer);
78
79 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
80 {
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;
87 }
88
89 memcpy (ptr, string, length);
90 return ptr + length;
91}
92#endif
93
94/* Return a pointer to the beginning of the variable buffer. */
95
96static char *
97initialize_variable_output (void)
98{
99 /* If we don't have a variable output buffer yet, get one. */
100
101#ifdef CONFIG_WITH_VALUE_LENGTH
102 if (variable_buffer == 0)
103 {
104 struct recycled_buffer *recycled = recycled_head;
105 if (recycled)
106 {
107 recycled_head = recycled->next;
108 variable_buffer_length = recycled->length;
109 variable_buffer = (char *)recycled;
110 }
111 else
112 {
113 variable_buffer_length = 384;
114 variable_buffer = xmalloc (variable_buffer_length);
115 }
116 variable_buffer[0] = '\0';
117 }
118#else /* CONFIG_WITH_VALUE_LENGTH */
119 if (variable_buffer == 0)
120 {
121 variable_buffer_length = 200;
122 variable_buffer = xmalloc (variable_buffer_length);
123 variable_buffer[0] = '\0';
124 }
125#endif /* CONFIG_WITH_VALUE_LENGTH */
126
127 return variable_buffer;
128}
129
130
131/* Recursively expand V. The returned string is malloc'd. */
132
133static char *allocated_variable_append (const struct variable *v);
134
135char *
136#ifndef CONFIG_WITH_VALUE_LENGTH
137recursively_expand_for_file (struct variable *v, struct file *file)
138#else
139recursively_expand_for_file (struct variable *v, struct file *file,
140 unsigned int *value_lenp)
141#endif
142{
143 char *value;
144 const struct floc *this_var;
145 const struct floc **saved_varp;
146 struct variable_set_list *save = 0;
147 int set_reading = 0;
148
149 /* Don't install a new location if this location is empty.
150 This can happen for command-line variables, builtin variables, etc. */
151 saved_varp = expanding_var;
152 if (v->fileinfo.filenm)
153 {
154 this_var = &v->fileinfo;
155 expanding_var = &this_var;
156 }
157
158 /* If we have no other file-reading context, use the variable's context. */
159 if (!reading_file)
160 {
161 set_reading = 1;
162 reading_file = &v->fileinfo;
163 }
164
165 if (v->expanding)
166 {
167 if (!v->exp_count)
168 /* Expanding V causes infinite recursion. Lose. */
169 fatal (*expanding_var,
170 _("Recursive variable `%s' references itself (eventually)"),
171 v->name);
172 --v->exp_count;
173 }
174
175 if (file)
176 {
177 save = current_variable_set_list;
178 current_variable_set_list = file->variables;
179 }
180
181 v->expanding = 1;
182#ifndef CONFIG_WITH_VALUE_LENGTH
183 if (v->append)
184 value = allocated_variable_append (v);
185 else
186 value = allocated_variable_expand (v->value);
187#else /* CONFIG_WITH_VALUE_LENGTH */
188 if (!v->append)
189 value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
190 else
191 {
192 value = allocated_variable_append (v);
193 if (value_lenp)
194 *value_lenp = strlen (value);
195 }
196#endif /* CONFIG_WITH_VALUE_LENGTH */
197 v->expanding = 0;
198
199 if (set_reading)
200 reading_file = 0;
201
202 if (file)
203 current_variable_set_list = save;
204
205 expanding_var = saved_varp;
206
207 return value;
208}
209
210#ifdef CONFIG_WITH_VALUE_LENGTH
211/* Static worker for reference_variable() that expands the recursive
212 variable V. The main difference between this and
213 recursively_expand[_for_file] is that this worker avoids the temporary
214 buffer and outputs directly into the current variable buffer (O). */
215static char *
216reference_recursive_variable (char *o, struct variable *v)
217{
218 const struct floc *this_var;
219 const struct floc **saved_varp;
220 int set_reading = 0;
221
222 /* Don't install a new location if this location is empty.
223 This can happen for command-line variables, builtin variables, etc. */
224 saved_varp = expanding_var;
225 if (v->fileinfo.filenm)
226 {
227 this_var = &v->fileinfo;
228 expanding_var = &this_var;
229 }
230
231 /* If we have no other file-reading context, use the variable's context. */
232 if (!reading_file)
233 {
234 set_reading = 1;
235 reading_file = &v->fileinfo;
236 }
237
238 if (v->expanding)
239 {
240 if (!v->exp_count)
241 /* Expanding V causes infinite recursion. Lose. */
242 fatal (*expanding_var,
243 _("Recursive variable `%s' references itself (eventually)"),
244 v->name);
245 --v->exp_count;
246 }
247
248 v->expanding = 1;
249 if (!v->append)
250 /* Expand directly into the variable buffer. */
251 variable_expand_string_2 (o, v->value, v->value_length, &o);
252 else
253 {
254 /* XXX: Feel free to optimize appending target variables as well. */
255 char *value = allocated_variable_append (v);
256 unsigned int value_len = strlen (value);
257 o = variable_buffer_output (o, value, value_len);
258 free (value);
259 }
260 v->expanding = 0;
261
262 if (set_reading)
263 reading_file = 0;
264
265 expanding_var = saved_varp;
266
267 return o;
268}
269#endif /* CONFIG_WITH_VALUE_LENGTH */
270
271/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
272
273#if defined(__GNUC__)
274__inline
275#elif defined (MY_INLINE) /* bird */
276MY_INLINE
277#endif
278static char *
279reference_variable (char *o, const char *name, unsigned int length)
280{
281 struct variable *v;
282#ifndef CONFIG_WITH_VALUE_LENGTH
283 char *value;
284#endif
285
286 v = lookup_variable (name, length);
287
288 if (v == 0)
289 warn_undefined (name, length);
290
291 /* If there's no variable by that name or it has no value, stop now. */
292 if (v == 0 || (*v->value == '\0' && !v->append))
293 return o;
294
295#ifdef CONFIG_WITH_VALUE_LENGTH
296 assert ((unsigned int)v->value_length == strlen (v->value));
297 if (!v->recursive)
298 o = variable_buffer_output (o, v->value, v->value_length);
299 else
300 o = reference_recursive_variable (o, v);
301#else /* !CONFIG_WITH_VALUE_LENGTH */
302 value = (v->recursive ? recursively_expand (v) : v->value);
303
304 o = variable_buffer_output (o, value, strlen (value));
305
306 if (v->recursive)
307 free (value);
308#endif /* !CONFIG_WITH_VALUE_LENGTH */
309
310 return o;
311}
312
313
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.
318
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.
323 */
324char *
325variable_expand_string (char *line, const char *string, long length)
326{
327 struct variable *v;
328 const char *p, *p1;
329 char *abuf = NULL;
330 char *o;
331 unsigned int line_offset;
332
333 if (!line)
334 line = initialize_variable_output();
335 o = line;
336 line_offset = line - variable_buffer;
337
338 if (length == 0)
339 {
340 variable_buffer_output (o, "", 1);
341 return (variable_buffer);
342 }
343
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')
347 {
348 abuf = xmalloc(length+1);
349 memcpy(abuf, string, length);
350 abuf[length] = '\0';
351 string = abuf;
352 }
353 p = string;
354
355 while (1)
356 {
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. */
360
361 p1 = strchr (p, '$');
362
363 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
364
365 if (p1 == 0)
366 break;
367 p = p1 + 1;
368
369 /* Dispatch on the char that follows the $. */
370
371 switch (*p)
372 {
373 case '$':
374 /* $$ seen means output one $ to the variable output buffer. */
375 o = variable_buffer_output (o, p, 1);
376 break;
377
378 case '(':
379 case '{':
380 /* $(...) or ${...} is the general case of substitution. */
381 {
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;
389
390 op = o;
391 begp = p;
392 if (handle_function (&op, &begp))
393 {
394 o = op;
395 p = begp;
396 break;
397 }
398
399 /* Is there a variable reference inside the parens or braces?
400 If so, expand it before expanding the entire reference. */
401
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)
408 {
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)
413 {
414 if (*p == openparen)
415 ++count;
416 else if (*p == closeparen && --count < 0)
417 break;
418 }
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)
423 {
424 abeg = expand_argument (beg, p); /* Expand the name. */
425 beg = abeg;
426 end = strchr (beg, '\0');
427 }
428 }
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;
434
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? */
438
439 colon = lindex (beg, end, ':');
440 if (colon)
441 {
442 /* This looks like a substitution reference: $(FOO:A=B). */
443 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
444
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
453 {
454 replace_beg = subst_end + 1;
455 replace_end = end;
456
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);
462
463 /* If the variable is not empty, perform the
464 substitution. */
465 if (v != 0 && *v->value != '\0')
466 {
467 char *pattern, *replace, *ppercent, *rpercent;
468 char *value = (v->recursive
469 ? recursively_expand (v)
470 : v->value);
471
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';
479
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';
485
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)
490 {
491 ++ppercent;
492 rpercent = find_percent (replace);
493 if (rpercent)
494 ++rpercent;
495 }
496 else
497 {
498 ppercent = pattern;
499 rpercent = replace;
500 --pattern;
501 --replace;
502 }
503
504 o = patsubst_expand_pat (o, value, pattern, replace,
505 ppercent, rpercent);
506
507 if (v->recursive)
508 free (value);
509 }
510 }
511 }
512
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);
517
518 if (abeg)
519 free (abeg);
520 }
521 break;
522
523 case '\0':
524 break;
525
526 default:
527 if (isblank ((unsigned char)p[-1]))
528 break;
529
530 /* A $ followed by a random char is a variable reference:
531 $a is equivalent to $(a). */
532 o = reference_variable (o, p, 1);
533
534 break;
535 }
536
537 if (*p == '\0')
538 break;
539 else
540 ++p;
541 }
542
543 if (abuf)
544 free (abuf);
545
546 variable_buffer_output (o, "", 1);
547 return (variable_buffer + line_offset);
548}
549
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.
554
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.
559 */
560char *
561variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
562{
563 struct variable *v;
564 const char *p, *p1, *eos;
565 char *o;
566 unsigned int line_offset;
567
568 if (!line)
569 line = initialize_variable_output();
570 o = line;
571 line_offset = line - variable_buffer;
572
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));
577
578 /* Simple 1: Emptry string. */
579
580 if (length == 0)
581 {
582 o = variable_buffer_output (o, "\0", 2);
583 *eolp = o - 2;
584 return (variable_buffer + line_offset);
585 }
586
587 /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
588
589 p1 = (const char *)memchr (string, '$', length);
590 if (p1 == 0)
591 {
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);
597 }
598
599 p = string;
600 eos = p + length;
601
602 while (1)
603 {
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. */
607
608 o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
609
610 if (p1 == 0)
611 break;
612 p = p1 + 1;
613
614 /* Dispatch on the char that follows the $. */
615
616 switch (*p)
617 {
618 case '$':
619 /* $$ seen means output one $ to the variable output buffer. */
620 o = variable_buffer_output (o, p, 1);
621 break;
622
623 case '(':
624 case '{':
625 /* $(...) or ${...} is the general case of substitution. */
626 {
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;
635
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))
641 {
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;
647 }
648
649 /* Is there a variable reference inside the parens or braces?
650 If so, expand it before expanding the entire reference. */
651
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)
658 {
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)
663 {
664 if (*p == openparen)
665 ++count;
666 else if (*p == closeparen && --count < 0)
667 break;
668 }
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)
673 {
674 unsigned int len;
675 char saved;
676
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;
684 }
685 }
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;
691
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? */
695
696 colon = lindex (beg, end, ':');
697 if (colon)
698 {
699 /* This looks like a substitution reference: $(FOO:A=B). */
700 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
701
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
710 {
711 replace_beg = subst_end + 1;
712 replace_end = end;
713
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);
719
720 /* If the variable is not empty, perform the
721 substitution. */
722 if (v != 0 && *v->value != '\0')
723 {
724 char *pattern, *replace, *ppercent, *rpercent;
725 char *value = (v->recursive
726 ? recursively_expand (v)
727 : v->value);
728
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';
736
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';
742
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)
747 {
748 ++ppercent;
749 rpercent = find_percent (replace);
750 if (rpercent)
751 ++rpercent;
752 }
753 else
754 {
755 ppercent = pattern;
756 rpercent = replace;
757 --pattern;
758 --replace;
759 }
760
761 o = patsubst_expand_pat (o, value, pattern, replace,
762 ppercent, rpercent);
763
764 if (v->recursive)
765 free (value);
766 }
767 }
768 }
769
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);
774
775 if (abeg)
776 recycle_variable_buffer (abeg, alen);
777 }
778 break;
779
780 case '\0':
781 assert (p == eos);
782 break;
783
784 default:
785 if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
786 break;
787
788 /* A $ followed by a random char is a variable reference:
789 $a is equivalent to $(a). */
790 o = reference_variable (o, p, 1);
791
792 break;
793 }
794
795 if (++p >= eos)
796 break;
797 p1 = memchr (p, '$', eos - p);
798 }
799
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);
807}
808#endif /* CONFIG_WITH_VALUE_LENGTH */
809
810
811/* Scan LINE for variable references and expansion-function calls.
812 Build in `variable_buffer' the result of expanding the references and calls.
813 Return the address of the resulting string, which is null-terminated
814 and is valid only until the next time this function is called. */
815
816char *
817variable_expand (const char *line)
818{
819#ifndef CONFIG_WITH_VALUE_LENGTH
820 return variable_expand_string(NULL, line, (long)-1);
821#else /* CONFIG_WITH_VALUE_LENGTH */
822 char *s;
823
824 /* this function is abused a lot like this: variable_expand(""). */
825 if (!*line)
826 {
827 s = variable_buffer_output (initialize_variable_output (), "\0", 2);
828 return s - 2;
829 }
830 return variable_expand_string_2 (NULL, line, (long)-1, &s);
831#endif /* CONFIG_WITH_VALUE_LENGTH */
832}
833
834
835/* Expand an argument for an expansion function.
836 The text starting at STR and ending at END is variable-expanded
837 into a null-terminated string that is returned as the value.
838 This is done without clobbering `variable_buffer' or the current
839 variable-expansion that is in progress. */
840
841char *
842expand_argument (const char *str, const char *end)
843{
844#ifndef CONFIG_WITH_VALUE_LENGTH
845 char *tmp;
846#endif
847
848 if (str == end)
849 return xstrdup("");
850
851#ifndef CONFIG_WITH_VALUE_LENGTH
852 if (!end || *end == '\0')
853 return allocated_variable_expand (str);
854
855 tmp = alloca (end - str + 1);
856 memcpy (tmp, str, end - str);
857 tmp[end - str] = '\0';
858
859 return allocated_variable_expand (tmp);
860#else /* CONFIG_WITH_VALUE_LENGTH */
861 if (!end)
862 return allocated_variable_expand_2 (str, ~0U, NULL);
863 return allocated_variable_expand_2 (str, end - str, NULL);
864#endif /* CONFIG_WITH_VALUE_LENGTH */
865}
866
867
868/* Expand LINE for FILE. Error messages refer to the file and line where
869 FILE's commands were found. Expansion uses FILE's variable set list. */
870
871char *
872variable_expand_for_file (const char *line, struct file *file)
873{
874 char *result;
875 struct variable_set_list *save;
876
877 if (file == 0)
878 return variable_expand (line);
879
880 save = current_variable_set_list;
881 current_variable_set_list = file->variables;
882 if (file->cmds && file->cmds->fileinfo.filenm)
883 reading_file = &file->cmds->fileinfo;
884 else
885 reading_file = 0;
886 result = variable_expand (line);
887 current_variable_set_list = save;
888 reading_file = 0;
889
890 return result;
891}
892
893
894#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
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 Differs from variable_expand_for_file in that it takes a pointer to
899 where in the variable buffer to start outputting the expanded string,
900 and that it can returned the length of the string if you wish. */
901
902char *
903variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
904 struct file *file, unsigned int *value_lenp)
905{
906 char *result;
907 struct variable_set_list *savev;
908 const struct floc *savef;
909 long len = length == ~0U ? (long)-1 : (long)length;
910 char *eol;
911
912 if (!o)
913 o = initialize_variable_output();
914
915 if (file == 0)
916 result = variable_expand_string_2 (o, line, len, &eol);
917 else
918 {
919 savev = current_variable_set_list;
920 current_variable_set_list = file->variables;
921
922 savef = reading_file;
923 if (file->cmds && file->cmds->fileinfo.filenm)
924 reading_file = &file->cmds->fileinfo;
925 else
926 reading_file = 0;
927
928 result = variable_expand_string_2 (o, line, len, &eol);
929
930 current_variable_set_list = savev;
931 reading_file = savef;
932 }
933
934 if (value_lenp)
935 *value_lenp = eol - result;
936
937 return result;
938}
939
940
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. */
945
946static char *
947variable_append (const char *name, unsigned int length,
948 const struct variable_set_list *set)
949{
950 const struct variable *v;
951 char *buf = 0;
952
953 /* If there's nothing left to check, return the empty buffer. */
954 if (!set)
955 return initialize_variable_output ();
956
957 /* Try to find the variable in this variable set. */
958 v = lookup_variable_in_set (name, length, set->set);
959
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);
963
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 ();
970
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 ((unsigned int)v->value_length == strlen (v->value)); /* FIXME */
977#endif
978
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
986
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
994}
995
996#ifdef CONFIG_WITH_VALUE_LENGTH
997/* Expands the specified string, appending it to the specified
998 variable value. */
999void
1000append_expanded_string_to_variable (struct variable *v, const char *value,
1001 unsigned int value_len, int append)
1002{
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)
1008 {
1009 unsigned int off_dollar = p - (char *)value;
1010
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);
1015
1016 p = variable_buffer;
1017 if (append || !v->value_length)
1018 {
1019 /* Copy the current value into it and append a space. */
1020 if (v->value_length)
1021 {
1022 p = variable_buffer_output (p, v->value, v->value_length);
1023 p = variable_buffer_output (p, " ", 1);
1024 }
1025
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);
1029 }
1030 else
1031 {
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);
1035
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;
1039 }
1040
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;
1051
1052 /* Restore the variable buffer, but without freeing the current. */
1053 variable_buffer = NULL;
1054 restore_variable_buffer (saved_buffer, saved_buffer_length);
1055 }
1056 /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1057}
1058#endif /* CONFIG_WITH_VALUE_LENGTH */
1059
1060static char *
1061allocated_variable_append (const struct variable *v)
1062{
1063 char *val;
1064
1065 /* Construct the appended variable value. */
1066
1067 char *obuf = variable_buffer;
1068 unsigned int olen = variable_buffer_length;
1069
1070 variable_buffer = 0;
1071
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;
1076
1077 variable_buffer = obuf;
1078 variable_buffer_length = olen;
1079
1080 return val;
1081}
1082
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. */
1085
1086char *
1087allocated_variable_expand_for_file (const char *line, struct file *file)
1088{
1089 char *value;
1090
1091 char *obuf = variable_buffer;
1092 unsigned int olen = variable_buffer_length;
1093
1094 variable_buffer = 0;
1095
1096 value = variable_expand_for_file (line, file);
1097
1098#if 0
1099 /* Waste a little memory and save time. */
1100 value = xrealloc (value, strlen (value))
1101#endif
1102
1103 variable_buffer = obuf;
1104 variable_buffer_length = olen;
1105
1106 return value;
1107}
1108
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. */
1112
1113char *
1114allocated_variable_expand_2 (const char *line, unsigned int length,
1115 unsigned int *value_lenp)
1116{
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;
1122
1123 variable_buffer = 0;
1124
1125 value = variable_expand_string_2 (NULL, line, len, &eol);
1126 if (value_lenp)
1127 *value_lenp = eol - value;
1128
1129 variable_buffer = obuf;
1130 variable_buffer_length = olen;
1131
1132 return value;
1133}
1134
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.) */
1139
1140char *
1141allocated_variable_expand_3 (const char *line, unsigned int length,
1142 unsigned int *value_lenp,
1143 unsigned int *buffer_lengthp)
1144{
1145 char *obuf = variable_buffer;
1146 unsigned int olen = variable_buffer_length;
1147 long len = (long)length;
1148 char *value;
1149 char *eol;
1150
1151 variable_buffer = 0;
1152
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;
1157
1158 variable_buffer = obuf;
1159 variable_buffer_length = olen;
1160
1161 return value;
1162}
1163
1164/* recycle a buffer. */
1165
1166void
1167recycle_variable_buffer (char *buffer, unsigned int length)
1168{
1169 struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
1170
1171 assert (!(length & 31));
1172 assert (length >= 384);
1173 recycled->length = length;
1174 recycled->next = recycled_head;
1175 recycled_head = recycled;
1176}
1177
1178#endif /* CONFIG_WITH_VALUE_LENGTH */
1179
1180/* Install a new variable_buffer context, returning the current one for
1181 safe-keeping. */
1182
1183void
1184install_variable_buffer (char **bufp, unsigned int *lenp)
1185{
1186 *bufp = variable_buffer;
1187 *lenp = variable_buffer_length;
1188
1189 variable_buffer = 0;
1190 initialize_variable_output ();
1191}
1192
1193/* Restore a previously-saved variable_buffer setting (free the current one).
1194 */
1195
1196void
1197restore_variable_buffer (char *buf, unsigned int len)
1198{
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
1205
1206 variable_buffer = buf;
1207 variable_buffer_length = len;
1208}
Note: See TracBrowser for help on using the repository browser.