VirtualBox

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

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

kmk: Made struct variable capable of holding read only variables to speed up set_file_variables.

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