VirtualBox

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

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

kmk: Some optimizations for expand_deps.

  • Property svn:eol-style set to native
File size: 31.2 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#ifndef KMK
55/* Subroutine of variable_expand and friends:
56 The text to add is LENGTH chars starting at STRING to the variable_buffer.
57 The text is added to the buffer at PTR, and the updated pointer into
58 the buffer is returned as the value. Thus, the value returned by
59 each call to variable_buffer_output should be the first argument to
60 the following call. */
61
62char *
63variable_buffer_output (char *ptr, const char *string, unsigned int length)
64{
65 register unsigned int newlen = length + (ptr - variable_buffer);
66
67 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
68 {
69 unsigned int offset = ptr - variable_buffer;
70 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
71 ? newlen + 100
72 : 2 * variable_buffer_length);
73 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
74 ptr = variable_buffer + offset;
75 }
76
77 memcpy (ptr, string, length);
78 return ptr + length;
79}
80#endif
81
82/* Return a pointer to the beginning of the variable buffer. */
83
84static char *
85initialize_variable_output (void)
86{
87 /* If we don't have a variable output buffer yet, get one. */
88
89 if (variable_buffer == 0)
90 {
91#ifdef KMK
92 variable_buffer_length = 384;
93#else
94 variable_buffer_length = 200;
95#endif
96 variable_buffer = xmalloc (variable_buffer_length);
97 variable_buffer[0] = '\0';
98 }
99
100 return variable_buffer;
101}
102
103
104/* Recursively expand V. The returned string is malloc'd. */
105
106static char *allocated_variable_append (const struct variable *v);
107
108char *
109#ifndef CONFIG_WITH_VALUE_LENGTH
110recursively_expand_for_file (struct variable *v, struct file *file)
111#else
112recursively_expand_for_file (struct variable *v, struct file *file,
113 unsigned int *value_lenp)
114#endif
115{
116 char *value;
117 const struct floc *this_var;
118 const struct floc **saved_varp;
119 struct variable_set_list *save = 0;
120 int set_reading = 0;
121
122 /* Don't install a new location if this location is empty.
123 This can happen for command-line variables, builtin variables, etc. */
124 saved_varp = expanding_var;
125 if (v->fileinfo.filenm)
126 {
127 this_var = &v->fileinfo;
128 expanding_var = &this_var;
129 }
130
131 /* If we have no other file-reading context, use the variable's context. */
132 if (!reading_file)
133 {
134 set_reading = 1;
135 reading_file = &v->fileinfo;
136 }
137
138 if (v->expanding)
139 {
140 if (!v->exp_count)
141 /* Expanding V causes infinite recursion. Lose. */
142 fatal (*expanding_var,
143 _("Recursive variable `%s' references itself (eventually)"),
144 v->name);
145 --v->exp_count;
146 }
147
148 if (file)
149 {
150 save = current_variable_set_list;
151 current_variable_set_list = file->variables;
152 }
153
154 v->expanding = 1;
155#ifndef CONFIG_WITH_VALUE_LENGTH
156 if (v->append)
157 value = allocated_variable_append (v);
158 else
159 value = allocated_variable_expand (v->value);
160#else /* CONFIG_WITH_VALUE_LENGTH */
161 if (!v->append)
162 value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
163 else
164 {
165 value = allocated_variable_append (v);
166 if (value_lenp)
167 *value_lenp = strlen (value);
168 }
169#endif /* CONFIG_WITH_VALUE_LENGTH */
170 v->expanding = 0;
171
172 if (set_reading)
173 reading_file = 0;
174
175 if (file)
176 current_variable_set_list = save;
177
178 expanding_var = saved_varp;
179
180 return value;
181}
182
183/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
184
185#if defined(__GNUC__) || defined(_MSC_VER) /* bird added MSC */
186__inline
187#endif
188static char *
189reference_variable (char *o, const char *name, unsigned int length)
190{
191 struct variable *v;
192 char *value;
193
194 v = lookup_variable (name, length);
195
196 if (v == 0)
197 warn_undefined (name, length);
198
199 /* If there's no variable by that name or it has no value, stop now. */
200 if (v == 0 || (*v->value == '\0' && !v->append))
201 return o;
202
203#ifdef CONFIG_WITH_VALUE_LENGTH
204 assert ((unsigned int)v->value_length == strlen (v->value));
205 if (!v->recursive)
206 o = variable_buffer_output (o, v->value, v->value_length);
207 else
208 {
209 unsigned int value_len;
210
211 value = recursively_expand_for_file (v, NULL, &value_len);
212 o = variable_buffer_output (o, value, value_len);
213 free (value);
214 }
215#else /* !CONFIG_WITH_VALUE_LENGTH */
216 value = (v->recursive ? recursively_expand (v) : v->value);
217
218 o = variable_buffer_output (o, value, strlen (value));
219
220 if (v->recursive)
221 free (value);
222#endif /* !CONFIG_WITH_VALUE_LENGTH */
223
224 return o;
225}
226
227
228#ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
229/* Scan STRING for variable references and expansion-function calls. Only
230 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
231 a null byte is found.
232
233 Write the results to LINE, which must point into `variable_buffer'. If
234 LINE is NULL, start at the beginning of the buffer.
235 Return a pointer to LINE, or to the beginning of the buffer if LINE is
236 NULL.
237 */
238char *
239variable_expand_string (char *line, const char *string, long length)
240{
241 struct variable *v;
242 const char *p, *p1;
243 char *abuf = NULL;
244 char *o;
245 unsigned int line_offset;
246
247 if (!line)
248 line = initialize_variable_output();
249 o = line;
250 line_offset = line - variable_buffer;
251
252 if (length == 0)
253 {
254 variable_buffer_output (o, "", 1);
255 return (variable_buffer);
256 }
257
258 /* If we want a subset of the string, allocate a temporary buffer for it.
259 Most of the functions we use here don't work with length limits. */
260 if (length > 0 && string[length] != '\0')
261 {
262 abuf = xmalloc(length+1);
263 memcpy(abuf, string, length);
264 abuf[length] = '\0';
265 string = abuf;
266 }
267 p = string;
268
269 while (1)
270 {
271 /* Copy all following uninteresting chars all at once to the
272 variable output buffer, and skip them. Uninteresting chars end
273 at the next $ or the end of the input. */
274
275 p1 = strchr (p, '$');
276
277 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
278
279 if (p1 == 0)
280 break;
281 p = p1 + 1;
282
283 /* Dispatch on the char that follows the $. */
284
285 switch (*p)
286 {
287 case '$':
288 /* $$ seen means output one $ to the variable output buffer. */
289 o = variable_buffer_output (o, p, 1);
290 break;
291
292 case '(':
293 case '{':
294 /* $(...) or ${...} is the general case of substitution. */
295 {
296 char openparen = *p;
297 char closeparen = (openparen == '(') ? ')' : '}';
298 const char *begp;
299 const char *beg = p + 1;
300 char *op;
301 char *abeg = NULL;
302 const char *end, *colon;
303
304 op = o;
305 begp = p;
306 if (handle_function (&op, &begp))
307 {
308 o = op;
309 p = begp;
310 break;
311 }
312
313 /* Is there a variable reference inside the parens or braces?
314 If so, expand it before expanding the entire reference. */
315
316 end = strchr (beg, closeparen);
317 if (end == 0)
318 /* Unterminated variable reference. */
319 fatal (*expanding_var, _("unterminated variable reference"));
320 p1 = lindex (beg, end, '$');
321 if (p1 != 0)
322 {
323 /* BEG now points past the opening paren or brace.
324 Count parens or braces until it is matched. */
325 int count = 0;
326 for (p = beg; *p != '\0'; ++p)
327 {
328 if (*p == openparen)
329 ++count;
330 else if (*p == closeparen && --count < 0)
331 break;
332 }
333 /* If COUNT is >= 0, there were unmatched opening parens
334 or braces, so we go to the simple case of a variable name
335 such as `$($(a)'. */
336 if (count < 0)
337 {
338 abeg = expand_argument (beg, p); /* Expand the name. */
339 beg = abeg;
340 end = strchr (beg, '\0');
341 }
342 }
343 else
344 /* Advance P to the end of this reference. After we are
345 finished expanding this one, P will be incremented to
346 continue the scan. */
347 p = end;
348
349 /* This is not a reference to a built-in function and
350 any variable references inside are now expanded.
351 Is the resultant text a substitution reference? */
352
353 colon = lindex (beg, end, ':');
354 if (colon)
355 {
356 /* This looks like a substitution reference: $(FOO:A=B). */
357 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
358
359 subst_beg = colon + 1;
360 subst_end = lindex (subst_beg, end, '=');
361 if (subst_end == 0)
362 /* There is no = in sight. Punt on the substitution
363 reference and treat this as a variable name containing
364 a colon, in the code below. */
365 colon = 0;
366 else
367 {
368 replace_beg = subst_end + 1;
369 replace_end = end;
370
371 /* Extract the variable name before the colon
372 and look up that variable. */
373 v = lookup_variable (beg, colon - beg);
374 if (v == 0)
375 warn_undefined (beg, colon - beg);
376
377 /* If the variable is not empty, perform the
378 substitution. */
379 if (v != 0 && *v->value != '\0')
380 {
381 char *pattern, *replace, *ppercent, *rpercent;
382 char *value = (v->recursive
383 ? recursively_expand (v)
384 : v->value);
385
386 /* Copy the pattern and the replacement. Add in an
387 extra % at the beginning to use in case there
388 isn't one in the pattern. */
389 pattern = alloca (subst_end - subst_beg + 2);
390 *(pattern++) = '%';
391 memcpy (pattern, subst_beg, subst_end - subst_beg);
392 pattern[subst_end - subst_beg] = '\0';
393
394 replace = alloca (replace_end - replace_beg + 2);
395 *(replace++) = '%';
396 memcpy (replace, replace_beg,
397 replace_end - replace_beg);
398 replace[replace_end - replace_beg] = '\0';
399
400 /* Look for %. Set the percent pointers properly
401 based on whether we find one or not. */
402 ppercent = find_percent (pattern);
403 if (ppercent)
404 {
405 ++ppercent;
406 rpercent = find_percent (replace);
407 if (rpercent)
408 ++rpercent;
409 }
410 else
411 {
412 ppercent = pattern;
413 rpercent = replace;
414 --pattern;
415 --replace;
416 }
417
418 o = patsubst_expand_pat (o, value, pattern, replace,
419 ppercent, rpercent);
420
421 if (v->recursive)
422 free (value);
423 }
424 }
425 }
426
427 if (colon == 0)
428 /* This is an ordinary variable reference.
429 Look up the value of the variable. */
430 o = reference_variable (o, beg, end - beg);
431
432 if (abeg)
433 free (abeg);
434 }
435 break;
436
437 case '\0':
438 break;
439
440 default:
441 if (isblank ((unsigned char)p[-1]))
442 break;
443
444 /* A $ followed by a random char is a variable reference:
445 $a is equivalent to $(a). */
446 o = reference_variable (o, p, 1);
447
448 break;
449 }
450
451 if (*p == '\0')
452 break;
453 else
454 ++p;
455 }
456
457 if (abuf)
458 free (abuf);
459
460 variable_buffer_output (o, "", 1);
461 return (variable_buffer + line_offset);
462}
463
464#else /* CONFIG_WITH_VALUE_LENGTH */
465/* Scan STRING for variable references and expansion-function calls. Only
466 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
467 a null byte is found.
468
469 Write the results to LINE, which must point into `variable_buffer'. If
470 LINE is NULL, start at the beginning of the buffer.
471 Return a pointer to LINE, or to the beginning of the buffer if LINE is
472 NULL. Set EOLP to point to the string terminator.
473 */
474char *
475variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
476{
477 struct variable *v;
478 const char *p, *p1, *eos;
479 char *o;
480 unsigned int line_offset;
481
482 if (!line)
483 line = initialize_variable_output();
484 o = line;
485 line_offset = line - variable_buffer;
486
487 if (length < 0)
488 length = strlen (string);
489 else
490 MY_ASSERT_MSG (string + length == (p1 = memchr (string, '\0', length)) || !p1, ("len=%ld p1=%p %s\n", length, p1, line));
491
492 /* Simple 1: Emptry string. */
493
494 if (length == 0)
495 {
496 o = variable_buffer_output (o, "\0", 2);
497 *eolp = o - 2;
498 return (variable_buffer + line_offset);
499 }
500
501 /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
502
503 p1 = (const char *)memchr (string, '$', length);
504 if (p1 == 0)
505 {
506 o = variable_buffer_output (o, string, length);
507 o = variable_buffer_output (o, "\0", 2);
508 *eolp = o - 2;
509 assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
510 return (variable_buffer + line_offset);
511 }
512
513 p = string;
514 eos = p + length;
515
516 while (1)
517 {
518 /* Copy all following uninteresting chars all at once to the
519 variable output buffer, and skip them. Uninteresting chars end
520 at the next $ or the end of the input. */
521
522 o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
523
524 if (p1 == 0)
525 break;
526 p = p1 + 1;
527
528 /* Dispatch on the char that follows the $. */
529
530 switch (*p)
531 {
532 case '$':
533 /* $$ seen means output one $ to the variable output buffer. */
534 o = variable_buffer_output (o, p, 1);
535 break;
536
537 case '(':
538 case '{':
539 /* $(...) or ${...} is the general case of substitution. */
540 {
541 char openparen = *p;
542 char closeparen = (openparen == '(') ? ')' : '}';
543 const char *begp;
544 const char *beg = p + 1;
545 char *op;
546 char *abeg = NULL;
547 const char *end, *colon;
548
549 op = o;
550 begp = p;
551 if (handle_function (&op, &begp, eos))
552 {
553 o = op;
554 p = begp;
555 MY_ASSERT_MSG (!(p1 = memchr (variable_buffer + line_offset, '\0', o - (variable_buffer + line_offset))),
556 ("line=%p o/exp_end=%p act_end=%p\n", variable_buffer + line_offset, o, p1));
557 break;
558 }
559
560 /* Is there a variable reference inside the parens or braces?
561 If so, expand it before expanding the entire reference. */
562
563 end = memchr (beg, closeparen, eos - beg);
564 if (end == 0)
565 /* Unterminated variable reference. */
566 fatal (*expanding_var, _("unterminated variable reference"));
567 p1 = lindex (beg, end, '$');
568 if (p1 != 0)
569 {
570 /* BEG now points past the opening paren or brace.
571 Count parens or braces until it is matched. */
572 int count = 0;
573 for (p = beg; p < eos; ++p)
574 {
575 if (*p == openparen)
576 ++count;
577 else if (*p == closeparen && --count < 0)
578 break;
579 }
580 /* If COUNT is >= 0, there were unmatched opening parens
581 or braces, so we go to the simple case of a variable name
582 such as `$($(a)'. */
583 if (count < 0)
584 {
585 unsigned int len;
586 char saved;
587
588 /* Expand the name. */
589 saved = *p;
590 *(char *)p = '\0'; /* XXX: proove that this is safe! */
591 abeg = allocated_variable_expand_2 (beg, p - beg, &len);
592 beg = abeg;
593 end = beg + len;
594 *(char *)p = saved;
595 }
596 }
597 else
598 /* Advance P to the end of this reference. After we are
599 finished expanding this one, P will be incremented to
600 continue the scan. */
601 p = end;
602
603 /* This is not a reference to a built-in function and
604 any variable references inside are now expanded.
605 Is the resultant text a substitution reference? */
606
607 colon = lindex (beg, end, ':');
608 if (colon)
609 {
610 /* This looks like a substitution reference: $(FOO:A=B). */
611 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
612
613 subst_beg = colon + 1;
614 subst_end = lindex (subst_beg, end, '=');
615 if (subst_end == 0)
616 /* There is no = in sight. Punt on the substitution
617 reference and treat this as a variable name containing
618 a colon, in the code below. */
619 colon = 0;
620 else
621 {
622 replace_beg = subst_end + 1;
623 replace_end = end;
624
625 /* Extract the variable name before the colon
626 and look up that variable. */
627 v = lookup_variable (beg, colon - beg);
628 if (v == 0)
629 warn_undefined (beg, colon - beg);
630
631 /* If the variable is not empty, perform the
632 substitution. */
633 if (v != 0 && *v->value != '\0')
634 {
635 char *pattern, *replace, *ppercent, *rpercent;
636 char *value = (v->recursive
637 ? recursively_expand (v)
638 : v->value);
639
640 /* Copy the pattern and the replacement. Add in an
641 extra % at the beginning to use in case there
642 isn't one in the pattern. */
643 pattern = alloca (subst_end - subst_beg + 2);
644 *(pattern++) = '%';
645 memcpy (pattern, subst_beg, subst_end - subst_beg);
646 pattern[subst_end - subst_beg] = '\0';
647
648 replace = alloca (replace_end - replace_beg + 2);
649 *(replace++) = '%';
650 memcpy (replace, replace_beg,
651 replace_end - replace_beg);
652 replace[replace_end - replace_beg] = '\0';
653
654 /* Look for %. Set the percent pointers properly
655 based on whether we find one or not. */
656 ppercent = find_percent (pattern);
657 if (ppercent)
658 {
659 ++ppercent;
660 rpercent = find_percent (replace);
661 if (rpercent)
662 ++rpercent;
663 }
664 else
665 {
666 ppercent = pattern;
667 rpercent = replace;
668 --pattern;
669 --replace;
670 }
671
672 o = patsubst_expand_pat (o, value, pattern, replace,
673 ppercent, rpercent);
674
675 if (v->recursive)
676 free (value);
677 }
678 }
679 }
680
681 if (colon == 0)
682 /* This is an ordinary variable reference.
683 Look up the value of the variable. */
684 o = reference_variable (o, beg, end - beg);
685
686 if (abeg)
687 free (abeg);
688 }
689 break;
690
691 case '\0':
692 assert (p == eos);
693 break;
694
695 default:
696 if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
697 break;
698
699 /* A $ followed by a random char is a variable reference:
700 $a is equivalent to $(a). */
701 o = reference_variable (o, p, 1);
702
703 break;
704 }
705
706 if (++p >= eos)
707 break;
708 p1 = memchr (p, '$', eos - p);
709 }
710
711 o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
712 *eolp = o - 2;
713 MY_ASSERT_MSG (strchr (variable_buffer + line_offset, '\0') == *eolp,
714 ("expected=%d actual=%d\nlength=%ld string=%.*s\n",
715 (int)(*eolp - variable_buffer + line_offset), (int)strlen(variable_buffer + line_offset),
716 length, (int)length, string));
717 return (variable_buffer + line_offset);
718}
719#endif /* CONFIG_WITH_VALUE_LENGTH */
720
721
722/* Scan LINE for variable references and expansion-function calls.
723 Build in `variable_buffer' the result of expanding the references and calls.
724 Return the address of the resulting string, which is null-terminated
725 and is valid only until the next time this function is called. */
726
727char *
728variable_expand (const char *line)
729{
730#ifndef CONFIG_WITH_VALUE_LENGTH
731 return variable_expand_string(NULL, line, (long)-1);
732#else
733 char *s;
734
735 /* this function is abused a lot like this: variable_expand(""). */
736 if (!*line)
737 {
738 s = variable_buffer_output (initialize_variable_output (), "\0", 2);
739 return s - 2;
740 }
741 return variable_expand_string_2 (NULL, line, (long)-1, &s);
742#endif
743}
744
745
746/* Expand an argument for an expansion function.
747 The text starting at STR and ending at END is variable-expanded
748 into a null-terminated string that is returned as the value.
749 This is done without clobbering `variable_buffer' or the current
750 variable-expansion that is in progress. */
751
752char *
753expand_argument (const char *str, const char *end)
754{
755#ifndef CONFIG_WITH_VALUE_LENGTH /** @todo the hacks are no longer required. Clean up !! */
756 char *tmp;
757#endif
758
759 if (str == end)
760 return xstrdup("");
761
762#ifndef CONFIG_WITH_VALUE_LENGTH
763 if (!end || *end == '\0')
764 return allocated_variable_expand (str);
765#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
766 {
767 const char saved_char = *end;
768 *(char *)end = '\0';
769# ifndef CONFIG_WITH_VALUE_LENGTH
770 tmp = allocated_variable_expand ((char *)str);
771# else
772 tmp = allocated_variable_expand_2 ((char *)str, end - str, NULL);
773# endif
774 *(char *)end = saved_char;
775 return tmp;
776 }
777#else
778 tmp = alloca (end - str + 1);
779 memcpy (tmp, str, end - str);
780 tmp[end - str] = '\0';
781
782# ifndef CONFIG_WITH_VALUE_LENGTH
783 return allocated_variable_expand (tmp);
784# else
785 return allocated_variable_expand_2 (tmp, end - str, NULL);
786# endif
787#endif
788#else /* CONFIG_WITH_VALUE_LENGTH */
789 if (!end)
790 return allocated_variable_expand_2 (str, ~0U, NULL);
791 return allocated_variable_expand_2 (str, end - str, NULL);
792#endif /* CONFIG_WITH_VALUE_LENGTH */
793}
794
795
796/* Expand LINE for FILE. Error messages refer to the file and line where
797 FILE's commands were found. Expansion uses FILE's variable set list. */
798
799char *
800variable_expand_for_file (const char *line, struct file *file)
801{
802 char *result;
803 struct variable_set_list *save;
804
805 if (file == 0)
806 return variable_expand (line);
807
808 save = current_variable_set_list;
809 current_variable_set_list = file->variables;
810 if (file->cmds && file->cmds->fileinfo.filenm)
811 reading_file = &file->cmds->fileinfo;
812 else
813 reading_file = 0;
814 result = variable_expand (line);
815 current_variable_set_list = save;
816 reading_file = 0;
817
818 return result;
819}
820
821
822#ifdef CONFIG_WITH_COMMANDS_FUNC
823/* Expand LINE for FILE. Error messages refer to the file and line where
824 FILE's commands were found. Expansion uses FILE's variable set list.
825
826 Differs from variable_expand_for_file in that it takes a pointer to
827 where in the variable buffer to start outputting the expanded string,
828 and that it can returned the length of the string if you wish. */
829
830char *
831variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
832 struct file *file, unsigned int *value_lenp)
833{
834 char *result;
835 struct variable_set_list *save;
836 const struct floc *reading_file_saved;
837 long len = length == ~0U ? (long)-1 : (long)length;
838 char *eol;
839
840 if (!o)
841 o = initialize_variable_output();
842
843 if (file == 0)
844 result = variable_expand_string_2 (o, line, len, &eol);
845 else
846 {
847 save = current_variable_set_list;
848 current_variable_set_list = file->variables;
849 reading_file_saved = reading_file;
850 if (file->cmds && file->cmds->fileinfo.filenm)
851 reading_file = &file->cmds->fileinfo;
852 else
853 reading_file = 0;
854 result = variable_expand_string_2 (o, line, len, &eol);
855 current_variable_set_list = save;
856 reading_file = reading_file_saved;
857 }
858
859 if (value_lenp)
860 *value_lenp = eol - result;
861
862 return result;
863}
864
865
866#endif /* CONFIG_WITH_COMMANDS_FUNC */
867/* Like allocated_variable_expand, but for += target-specific variables.
868 First recursively construct the variable value from its appended parts in
869 any upper variable sets. Then expand the resulting value. */
870
871static char *
872variable_append (const char *name, unsigned int length,
873 const struct variable_set_list *set)
874{
875 const struct variable *v;
876 char *buf = 0;
877
878 /* If there's nothing left to check, return the empty buffer. */
879 if (!set)
880 return initialize_variable_output ();
881
882 /* Try to find the variable in this variable set. */
883 v = lookup_variable_in_set (name, length, set->set);
884
885 /* If there isn't one, look to see if there's one in a set above us. */
886 if (!v)
887 return variable_append (name, length, set->next);
888
889 /* If this variable type is append, first get any upper values.
890 If not, initialize the buffer. */
891 if (v->append)
892 buf = variable_append (name, length, set->next);
893 else
894 buf = initialize_variable_output ();
895
896 /* Append this value to the buffer, and return it.
897 If we already have a value, first add a space. */
898 if (buf > variable_buffer)
899 buf = variable_buffer_output (buf, " ", 1);
900#ifdef CONFIG_WITH_VALUE_LENGTH
901 assert ((unsigned int)v->value_length == strlen (v->value)); /* FIXME */
902#endif
903
904 /* Either expand it or copy it, depending. */
905 if (! v->recursive)
906#ifdef CONFIG_WITH_VALUE_LENGTH
907 return variable_buffer_output (buf, v->value, v->value_length);
908#else
909 return variable_buffer_output (buf, v->value, strlen (v->value));
910#endif
911
912#ifdef CONFIG_WITH_VALUE_LENGTH
913 variable_expand_string_2 (buf, v->value, v->value_length, &buf);
914 return buf;
915#else
916 buf = variable_expand_string (buf, v->value, strlen (v->value));
917 return (buf + strlen (buf));
918#endif
919}
920
921#ifdef CONFIG_WITH_VALUE_LENGTH
922/* Expands the specified string, appending it to the specified
923 variable value. */
924void
925append_expanded_string_to_variable (struct variable *v, const char *value,
926 unsigned int value_len, int append)
927{
928 char *p = (char *) memchr (value, '$', value_len);
929 if (!p)
930 /* fast path */
931 append_string_to_variable (v,value, value_len, append);
932 else if (value_len)
933 {
934 unsigned int off_dollar = p - (char *)value;
935
936 /* Install a fresh variable buffer. */
937 char *saved_buffer;
938 unsigned int saved_buffer_length;
939 install_variable_buffer (&saved_buffer, &saved_buffer_length);
940
941 p = variable_buffer;
942 if (append || !v->value_length)
943 {
944 /* Copy the current value into it and append a space. */
945 if (v->value_length)
946 {
947 p = variable_buffer_output (p, v->value, v->value_length);
948 p = variable_buffer_output (p, " ", 1);
949 }
950
951 /* Append the assignment value. */
952 p = variable_buffer_output (p, value, off_dollar);
953 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
954 }
955 else
956 {
957 /* Expand the assignemnt value. */
958 p = variable_buffer_output (p, value, off_dollar);
959 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
960
961 /* Append a space followed by the old value. */
962 p = variable_buffer_output (p, " ", 1);
963 p = variable_buffer_output (p, v->value, v->value_length + 1) - 1;
964 }
965
966 /* Replace the variable with the variable buffer. */
967 free (v->value);
968 v->value = variable_buffer;
969 v->value_length = p - v->value;
970 v->value_alloc_len = variable_buffer_length;
971
972 /* Restore the variable buffer, but without freeing the current. */
973 variable_buffer = NULL;
974 restore_variable_buffer (saved_buffer, saved_buffer_length);
975 }
976 /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
977}
978#endif /* CONFIG_WITH_VALUE_LENGTH */
979
980static char *
981allocated_variable_append (const struct variable *v)
982{
983 char *val;
984
985 /* Construct the appended variable value. */
986
987 char *obuf = variable_buffer;
988 unsigned int olen = variable_buffer_length;
989
990 variable_buffer = 0;
991
992 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
993 variable_buffer_output (val, "", 1);
994 val = variable_buffer;
995
996 variable_buffer = obuf;
997 variable_buffer_length = olen;
998
999 return val;
1000}
1001
1002/* Like variable_expand_for_file, but the returned string is malloc'd.
1003 This function is called a lot. It wants to be efficient. */
1004
1005char *
1006allocated_variable_expand_for_file (const char *line, struct file *file)
1007{
1008 char *value;
1009
1010 char *obuf = variable_buffer;
1011 unsigned int olen = variable_buffer_length;
1012
1013 variable_buffer = 0;
1014
1015 value = variable_expand_for_file (line, file);
1016
1017#if 0
1018 /* Waste a little memory and save time. */
1019 value = xrealloc (value, strlen (value))
1020#endif
1021
1022 variable_buffer = obuf;
1023 variable_buffer_length = olen;
1024
1025 return value;
1026}
1027
1028#ifdef CONFIG_WITH_VALUE_LENGTH
1029/* Handle the most common case in allocated_variable_expand_for_file
1030 specially and provide some additional string lenght features. */
1031
1032char *
1033allocated_variable_expand_2 (const char *line, unsigned int length,
1034 unsigned int *value_lenp)
1035{
1036 char *value;
1037 char *obuf = variable_buffer;
1038 unsigned int olen = variable_buffer_length;
1039 long len = length == ~0U ? -1L : (long)length;
1040 char *eol;
1041
1042 variable_buffer = 0;
1043
1044 value = variable_expand_string_2 (NULL, line, len, &eol);
1045 if (value_lenp)
1046 *value_lenp = eol - value;
1047
1048 variable_buffer = obuf;
1049 variable_buffer_length = olen;
1050
1051 return value;
1052}
1053#endif /* CONFIG_WITH_VALUE_LENGTH */
1054
1055/* Install a new variable_buffer context, returning the current one for
1056 safe-keeping. */
1057
1058void
1059install_variable_buffer (char **bufp, unsigned int *lenp)
1060{
1061 *bufp = variable_buffer;
1062 *lenp = variable_buffer_length;
1063
1064 variable_buffer = 0;
1065 initialize_variable_output ();
1066}
1067
1068/* Restore a previously-saved variable_buffer setting (free the current one).
1069 */
1070
1071void
1072restore_variable_buffer (char *buf, unsigned int len)
1073{
1074 free (variable_buffer);
1075
1076 variable_buffer = buf;
1077 variable_buffer_length = len;
1078}
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