VirtualBox

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

Last change on this file since 2119 was 2119, checked in by bird, 16 years ago

expand.c: warning.

  • 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, 2007 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 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 else
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;
848#endif
849
850 if (str == end)
851 return xstrdup("");
852
853#ifndef CONFIG_WITH_VALUE_LENGTH
854 if (!end || *end == '\0')
855 return allocated_variable_expand (str);
856
857 tmp = alloca (end - str + 1);
858 memcpy (tmp, str, end - str);
859 tmp[end - str] = '\0';
860
861 return allocated_variable_expand (tmp);
862#else /* CONFIG_WITH_VALUE_LENGTH */
863 if (!end)
864 return allocated_variable_expand_2 (str, ~0U, NULL);
865 return allocated_variable_expand_2 (str, end - str, NULL);
866#endif /* CONFIG_WITH_VALUE_LENGTH */
867}
868
869
870/* Expand LINE for FILE. Error messages refer to the file and line where
871 FILE's commands were found. Expansion uses FILE's variable set list. */
872
873char *
874variable_expand_for_file (const char *line, struct file *file)
875{
876 char *result;
877 struct variable_set_list *savev;
878 const struct floc *savef;
879
880 if (file == 0)
881 return variable_expand (line);
882
883 savev = current_variable_set_list;
884 current_variable_set_list = file->variables;
885
886 savef = reading_file;
887 if (file->cmds && file->cmds->fileinfo.filenm)
888 reading_file = &file->cmds->fileinfo;
889 else
890 reading_file = 0;
891
892 result = variable_expand (line);
893
894 current_variable_set_list = savev;
895 reading_file = savef;
896
897 return result;
898}
899
900
901#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
902/* Expand LINE for FILE. Error messages refer to the file and line where
903 FILE's commands were found. Expansion uses FILE's variable set list.
904
905 Differs from variable_expand_for_file in that it takes a pointer to
906 where in the variable buffer to start outputting the expanded string,
907 and that it can returned the length of the string if you wish. */
908
909char *
910variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
911 struct file *file, unsigned int *value_lenp)
912{
913 char *result;
914 struct variable_set_list *savev;
915 const struct floc *savef;
916 long len = length == ~0U ? (long)-1 : (long)length;
917 char *eol;
918
919 if (!o)
920 o = initialize_variable_output();
921
922 if (file == 0)
923 result = variable_expand_string_2 (o, line, len, &eol);
924 else
925 {
926 savev = current_variable_set_list;
927 current_variable_set_list = file->variables;
928
929 savef = reading_file;
930 if (file->cmds && file->cmds->fileinfo.filenm)
931 reading_file = &file->cmds->fileinfo;
932 else
933 reading_file = 0;
934
935 result = variable_expand_string_2 (o, line, len, &eol);
936
937 current_variable_set_list = savev;
938 reading_file = savef;
939 }
940
941 if (value_lenp)
942 *value_lenp = eol - result;
943
944 return result;
945}
946
947
948#endif /* CONFIG_WITH_VALUE_LENGTH || CONFIG_WITH_COMMANDS_FUNC */
949/* Like allocated_variable_expand, but for += target-specific variables.
950 First recursively construct the variable value from its appended parts in
951 any upper variable sets. Then expand the resulting value. */
952
953static char *
954variable_append (const char *name, unsigned int length,
955 const struct variable_set_list *set)
956{
957 const struct variable *v;
958 char *buf = 0;
959
960 /* If there's nothing left to check, return the empty buffer. */
961 if (!set)
962 return initialize_variable_output ();
963
964 /* Try to find the variable in this variable set. */
965 v = lookup_variable_in_set (name, length, set->set);
966
967 /* If there isn't one, look to see if there's one in a set above us. */
968 if (!v)
969 return variable_append (name, length, set->next);
970
971 /* If this variable type is append, first get any upper values.
972 If not, initialize the buffer. */
973 if (v->append)
974 buf = variable_append (name, length, set->next);
975 else
976 buf = initialize_variable_output ();
977
978 /* Append this value to the buffer, and return it.
979 If we already have a value, first add a space. */
980 if (buf > variable_buffer)
981 buf = variable_buffer_output (buf, " ", 1);
982#ifdef CONFIG_WITH_VALUE_LENGTH
983 assert (v->value_length == strlen (v->value));
984#endif
985
986 /* Either expand it or copy it, depending. */
987 if (! v->recursive)
988#ifdef CONFIG_WITH_VALUE_LENGTH
989 return variable_buffer_output (buf, v->value, v->value_length);
990#else
991 return variable_buffer_output (buf, v->value, strlen (v->value));
992#endif
993
994#ifdef CONFIG_WITH_VALUE_LENGTH
995 variable_expand_string_2 (buf, v->value, v->value_length, &buf);
996 return buf;
997#else
998 buf = variable_expand_string (buf, v->value, strlen (v->value));
999 return (buf + strlen (buf));
1000#endif
1001}
1002
1003#ifdef CONFIG_WITH_VALUE_LENGTH
1004/* Expands the specified string, appending it to the specified
1005 variable value. */
1006void
1007append_expanded_string_to_variable (struct variable *v, const char *value,
1008 unsigned int value_len, int append)
1009{
1010 char *p = (char *) memchr (value, '$', value_len);
1011 if (!p)
1012 /* fast path */
1013 append_string_to_variable (v,value, value_len, append);
1014 else if (value_len)
1015 {
1016 unsigned int off_dollar = p - (char *)value;
1017
1018 /* Install a fresh variable buffer. */
1019 char *saved_buffer;
1020 unsigned int saved_buffer_length;
1021 install_variable_buffer (&saved_buffer, &saved_buffer_length);
1022
1023 p = variable_buffer;
1024 if (append || !v->value_length)
1025 {
1026 /* Copy the current value into it and append a space. */
1027 if (v->value_length)
1028 {
1029 p = variable_buffer_output (p, v->value, v->value_length);
1030 p = variable_buffer_output (p, " ", 1);
1031 }
1032
1033 /* Append the assignment value. */
1034 p = variable_buffer_output (p, value, off_dollar);
1035 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
1036 }
1037 else
1038 {
1039 /* Expand the assignemnt value. */
1040 p = variable_buffer_output (p, value, off_dollar);
1041 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
1042
1043 /* Append a space followed by the old value. */
1044 p = variable_buffer_output (p, " ", 1);
1045 p = variable_buffer_output (p, v->value, v->value_length + 1) - 1;
1046 }
1047
1048 /* Replace the variable with the variable buffer. */
1049#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1050 if (v->rdonly_val)
1051 v->rdonly_val = 0;
1052 else
1053#endif
1054 free (v->value);
1055 v->value = variable_buffer;
1056 v->value_length = p - v->value;
1057 v->value_alloc_len = variable_buffer_length;
1058
1059 /* Restore the variable buffer, but without freeing the current. */
1060 variable_buffer = NULL;
1061 restore_variable_buffer (saved_buffer, saved_buffer_length);
1062 }
1063 /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1064}
1065#endif /* CONFIG_WITH_VALUE_LENGTH */
1066
1067static char *
1068allocated_variable_append (const struct variable *v)
1069{
1070 char *val;
1071
1072 /* Construct the appended variable value. */
1073
1074 char *obuf = variable_buffer;
1075 unsigned int olen = variable_buffer_length;
1076
1077 variable_buffer = 0;
1078
1079 assert ((unsigned int)v->length == strlen (v->name)); /* bird */
1080 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
1081 variable_buffer_output (val, "", 1);
1082 val = variable_buffer;
1083
1084 variable_buffer = obuf;
1085 variable_buffer_length = olen;
1086
1087 return val;
1088}
1089
1090/* Like variable_expand_for_file, but the returned string is malloc'd.
1091 This function is called a lot. It wants to be efficient. */
1092
1093char *
1094allocated_variable_expand_for_file (const char *line, struct file *file)
1095{
1096 char *value;
1097
1098 char *obuf = variable_buffer;
1099 unsigned int olen = variable_buffer_length;
1100
1101 variable_buffer = 0;
1102
1103 value = variable_expand_for_file (line, file);
1104
1105#if 0
1106 /* Waste a little memory and save time. */
1107 value = xrealloc (value, strlen (value))
1108#endif
1109
1110 variable_buffer = obuf;
1111 variable_buffer_length = olen;
1112
1113 return value;
1114}
1115
1116#ifdef CONFIG_WITH_VALUE_LENGTH
1117/* Handle the most common case in allocated_variable_expand_for_file
1118 specially and provide some additional string lenght features. */
1119
1120char *
1121allocated_variable_expand_2 (const char *line, unsigned int length,
1122 unsigned int *value_lenp)
1123{
1124 char *value;
1125 char *obuf = variable_buffer;
1126 unsigned int olen = variable_buffer_length;
1127 long len = length == ~0U ? -1L : (long)length;
1128 char *eol;
1129
1130 variable_buffer = 0;
1131
1132 value = variable_expand_string_2 (NULL, line, len, &eol);
1133 if (value_lenp)
1134 *value_lenp = eol - value;
1135
1136 variable_buffer = obuf;
1137 variable_buffer_length = olen;
1138
1139 return value;
1140}
1141
1142/* Initially created for handling a special case for variable_expand_string2
1143 where the variable name is expanded and freed right afterwards. This
1144 variant allows the variable_buffer to be recycled and thus avoid bothering
1145 with a slow free implementation. (Darwin is horrible slow.) */
1146
1147char *
1148allocated_variable_expand_3 (const char *line, unsigned int length,
1149 unsigned int *value_lenp,
1150 unsigned int *buffer_lengthp)
1151{
1152 char *obuf = variable_buffer;
1153 unsigned int olen = variable_buffer_length;
1154 long len = (long)length;
1155 char *value;
1156 char *eol;
1157
1158 variable_buffer = 0;
1159
1160 value = variable_expand_string_2 (NULL, line, len, &eol);
1161 if (value_lenp)
1162 *value_lenp = eol - value;
1163 *buffer_lengthp = variable_buffer_length;
1164
1165 variable_buffer = obuf;
1166 variable_buffer_length = olen;
1167
1168 return value;
1169}
1170
1171/* recycle a buffer. */
1172
1173void
1174recycle_variable_buffer (char *buffer, unsigned int length)
1175{
1176 struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
1177
1178 assert (!(length & 31));
1179 assert (length >= 384);
1180 recycled->length = length;
1181 recycled->next = recycled_head;
1182 recycled_head = recycled;
1183}
1184
1185#endif /* CONFIG_WITH_VALUE_LENGTH */
1186
1187/* Install a new variable_buffer context, returning the current one for
1188 safe-keeping. */
1189
1190void
1191install_variable_buffer (char **bufp, unsigned int *lenp)
1192{
1193 *bufp = variable_buffer;
1194 *lenp = variable_buffer_length;
1195
1196 variable_buffer = 0;
1197 initialize_variable_output ();
1198}
1199
1200/* Restore a previously-saved variable_buffer setting (free the current one).
1201 */
1202
1203void
1204restore_variable_buffer (char *buf, unsigned int len)
1205{
1206#ifndef CONFIG_WITH_VALUE_LENGTH
1207 free (variable_buffer);
1208#else
1209 if (variable_buffer)
1210 recycle_variable_buffer (variable_buffer, variable_buffer_length);
1211#endif
1212
1213 variable_buffer = buf;
1214 variable_buffer_length = len;
1215}
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