VirtualBox

source: kBuild/trunk/src/kmk/expand.c@ 2665

Last change on this file since 2665 was 2591, checked in by bird, 13 years ago

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette