VirtualBox

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

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

build fix.

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

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