VirtualBox

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

最後變更 在這個檔案從2771是 2771,由 bird 提交於 10 年 前

Optimizations, tuning and bug fixes for the 'compiled' string expansion code.

  • 屬性 svn:eol-style 設為 native
檔案大小: 37.3 KB
 
1/* Variable expansion functions for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
42010 Free Software Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "filedef.h"
24#include "job.h"
25#include "commands.h"
26#include "variable.h"
27#include "rule.h"
28#ifdef CONFIG_WITH_COMPILER
29# include "kmk_cc_exec.h"
30#endif
31
32/* Initially, any errors reported when expanding strings will be reported
33 against the file where the error appears. */
34const struct floc **expanding_var = &reading_file;
35
36/* The next two describe the variable output buffer.
37 This buffer is used to hold the variable-expansion of a line of the
38 makefile. It is made bigger with realloc whenever it is too small.
39 variable_buffer_length is the size currently allocated.
40 variable_buffer is the address of the buffer.
41
42 For efficiency, it's guaranteed that the buffer will always have
43 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
44 extra chars without having to call a function. Note you should never use
45 these bytes unless you're _sure_ you have room (you know when the buffer
46 length was last checked. */
47
48#define VARIABLE_BUFFER_ZONE 5
49
50#ifndef KMK
51static unsigned int variable_buffer_length;
52#else
53unsigned int variable_buffer_length;
54#endif
55char *variable_buffer;
56
57
58#ifdef CONFIG_WITH_VALUE_LENGTH
59struct recycled_buffer
60{
61 struct recycled_buffer *next;
62 unsigned int length;
63};
64struct recycled_buffer *recycled_head;
65#endif /* CONFIG_WITH_VALUE_LENGTH */
66
67
68
69#ifndef KMK
70/* Subroutine of variable_expand and friends:
71 The text to add is LENGTH chars starting at STRING to the variable_buffer.
72 The text is added to the buffer at PTR, and the updated pointer into
73 the buffer is returned as the value. Thus, the value returned by
74 each call to variable_buffer_output should be the first argument to
75 the following call. */
76
77char *
78variable_buffer_output (char *ptr, const char *string, unsigned int length)
79{
80 register unsigned int newlen = length + (ptr - variable_buffer);
81
82 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
83 {
84 unsigned int offset = ptr - variable_buffer;
85 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
86 ? newlen + 100
87 : 2 * variable_buffer_length);
88 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
89 ptr = variable_buffer + offset;
90 }
91
92 memcpy (ptr, string, length);
93 return ptr + length;
94}
95#endif
96
97/* Return a pointer to the beginning of the variable buffer. */
98
99static char *
100initialize_variable_output (void)
101{
102 /* If we don't have a variable output buffer yet, get one. */
103
104#ifdef CONFIG_WITH_VALUE_LENGTH
105 if (variable_buffer == 0)
106 {
107 struct recycled_buffer *recycled = recycled_head;
108 if (recycled)
109 {
110 recycled_head = recycled->next;
111 variable_buffer_length = recycled->length;
112 variable_buffer = (char *)recycled;
113 }
114 else
115 {
116 variable_buffer_length = 384;
117 variable_buffer = xmalloc (variable_buffer_length);
118 }
119 variable_buffer[0] = '\0';
120 }
121#else /* CONFIG_WITH_VALUE_LENGTH */
122 if (variable_buffer == 0)
123 {
124 variable_buffer_length = 200;
125 variable_buffer = xmalloc (variable_buffer_length);
126 variable_buffer[0] = '\0';
127 }
128#endif /* CONFIG_WITH_VALUE_LENGTH */
129
130 return variable_buffer;
131}
132
133
134/* Recursively expand V. The returned string is malloc'd. */
135
136static char *allocated_variable_append (const struct variable *v);
137
138char *
139#ifndef CONFIG_WITH_VALUE_LENGTH
140recursively_expand_for_file (struct variable *v, struct file *file)
141#else
142recursively_expand_for_file (struct variable *v, struct file *file,
143 unsigned int *value_lenp)
144#endif
145{
146 char *value;
147 const struct floc *this_var;
148 const struct floc **saved_varp;
149 struct variable_set_list *save = 0;
150 int set_reading = 0;
151
152 /* Don't install a new location if this location is empty.
153 This can happen for command-line variables, builtin variables, etc. */
154 saved_varp = expanding_var;
155 if (v->fileinfo.filenm)
156 {
157 this_var = &v->fileinfo;
158 expanding_var = &this_var;
159 }
160
161 /* If we have no other file-reading context, use the variable's context. */
162 if (!reading_file)
163 {
164 set_reading = 1;
165 reading_file = &v->fileinfo;
166 }
167
168 if (v->expanding)
169 {
170 if (!v->exp_count)
171 /* Expanding V causes infinite recursion. Lose. */
172 fatal (*expanding_var,
173 _("Recursive variable `%s' references itself (eventually)"),
174 v->name);
175 --v->exp_count;
176 }
177
178 if (file)
179 {
180 save = current_variable_set_list;
181 current_variable_set_list = file->variables;
182 }
183
184 v->expanding = 1;
185#ifndef CONFIG_WITH_VALUE_LENGTH
186 if (v->append)
187 value = allocated_variable_append (v);
188 else
189 value = allocated_variable_expand (v->value);
190#else /* CONFIG_WITH_VALUE_LENGTH */
191 if (!v->append)
192 {
193 if (!IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR (v))
194 value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
195 else
196 {
197 unsigned int len = v->value_length;
198 value = xmalloc (len + 2);
199 memcpy (value, v->value, len + 1);
200 value[len + 1] = '\0'; /* Extra terminator like allocated_variable_expand_2 returns. Why? */
201 if (value_lenp)
202 *value_lenp = len;
203 }
204 }
205 else
206 {
207 value = allocated_variable_append (v);
208 if (value_lenp)
209 *value_lenp = strlen (value);
210 }
211#endif /* CONFIG_WITH_VALUE_LENGTH */
212 v->expanding = 0;
213
214 if (set_reading)
215 reading_file = 0;
216
217 if (file)
218 current_variable_set_list = save;
219
220 expanding_var = saved_varp;
221
222 return value;
223}
224
225#ifdef CONFIG_WITH_VALUE_LENGTH
226/* Worker for reference_variable() and kmk_exec_* that expands the recursive
227 variable V. The main difference between this and
228 recursively_expand[_for_file] is that this worker avoids the temporary
229 buffer and outputs directly into the current variable buffer (O). */
230char *
231reference_recursive_variable (char *o, struct variable *v)
232{
233 const struct floc *this_var;
234 const struct floc **saved_varp;
235 int set_reading = 0;
236
237 /* Don't install a new location if this location is empty.
238 This can happen for command-line variables, builtin variables, etc. */
239 saved_varp = expanding_var;
240 if (v->fileinfo.filenm)
241 {
242 this_var = &v->fileinfo;
243 expanding_var = &this_var;
244 }
245
246 /* If we have no other file-reading context, use the variable's context. */
247 if (!reading_file)
248 {
249 set_reading = 1;
250 reading_file = &v->fileinfo;
251 }
252
253 if (v->expanding)
254 {
255 if (!v->exp_count)
256 /* Expanding V causes infinite recursion. Lose. */
257 fatal (*expanding_var,
258 _("Recursive variable `%s' references itself (eventually)"),
259 v->name);
260 --v->exp_count;
261 }
262
263 v->expanding = 1;
264 if (!v->append)
265 {
266 /* Expand directly into the variable buffer. */
267# ifdef CONFIG_WITH_COMPILER
268 v->expand_count++;
269 if ( v->expandprog
270 || (v->expand_count == 3 && kmk_cc_compile_variable_for_expand (v)) )
271 o = kmk_exec_expand_to_var_buf (v, o);
272 else
273 variable_expand_string_2 (o, v->value, v->value_length, &o);
274# else
275 MAKE_STATS_2 (v->expand_count++);
276 variable_expand_string_2 (o, v->value, v->value_length, &o);
277# endif
278 }
279 else
280 {
281 /* XXX: Feel free to optimize appending target variables as well. */
282 char *value = allocated_variable_append (v);
283 unsigned int value_len = strlen (value);
284 o = variable_buffer_output (o, value, value_len);
285 free (value);
286 }
287 v->expanding = 0;
288
289 if (set_reading)
290 reading_file = 0;
291
292 expanding_var = saved_varp;
293
294 return o;
295}
296#endif /* CONFIG_WITH_VALUE_LENGTH */
297
298/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
299
300#ifdef MY_INLINE /* bird */
301MY_INLINE char *
302#else
303#if defined(__GNUC__)
304__inline
305#endif
306static char *
307#endif
308reference_variable (char *o, const char *name, unsigned int length)
309{
310 struct variable *v;
311#ifndef CONFIG_WITH_VALUE_LENGTH
312 char *value;
313#endif
314
315 v = lookup_variable (name, length);
316
317 if (v == 0)
318 warn_undefined (name, length);
319
320 /* If there's no variable by that name or it has no value, stop now. */
321 if (v == 0 || (*v->value == '\0' && !v->append))
322 return o;
323
324#ifdef CONFIG_WITH_VALUE_LENGTH
325 assert (v->value_length == strlen (v->value));
326 if (!v->recursive || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR (v))
327 o = variable_buffer_output (o, v->value, v->value_length);
328 else
329 o = reference_recursive_variable (o, v);
330#else /* !CONFIG_WITH_VALUE_LENGTH */
331 value = (v->recursive ? recursively_expand (v) : v->value);
332
333 o = variable_buffer_output (o, value, strlen (value));
334
335 if (v->recursive)
336 free (value);
337#endif /* !CONFIG_WITH_VALUE_LENGTH */
338
339 return o;
340}
341
342
343#ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
344/* Scan STRING for variable references and expansion-function calls. Only
345 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
346 a null byte is found.
347
348 Write the results to LINE, which must point into `variable_buffer'. If
349 LINE is NULL, start at the beginning of the buffer.
350 Return a pointer to LINE, or to the beginning of the buffer if LINE is
351 NULL.
352 */
353char *
354variable_expand_string (char *line, const char *string, long length)
355{
356 struct variable *v;
357 const char *p, *p1;
358 char *abuf = NULL;
359 char *o;
360 unsigned int line_offset;
361
362 if (!line)
363 line = initialize_variable_output();
364 o = line;
365 line_offset = line - variable_buffer;
366
367 if (length == 0)
368 {
369 variable_buffer_output (o, "", 1);
370 return (variable_buffer);
371 }
372
373 /* If we want a subset of the string, allocate a temporary buffer for it.
374 Most of the functions we use here don't work with length limits. */
375 if (length > 0 && string[length] != '\0')
376 {
377 abuf = xmalloc(length+1);
378 memcpy(abuf, string, length);
379 abuf[length] = '\0';
380 string = abuf;
381 }
382 p = string;
383
384 while (1)
385 {
386 /* Copy all following uninteresting chars all at once to the
387 variable output buffer, and skip them. Uninteresting chars end
388 at the next $ or the end of the input. */
389
390 p1 = strchr (p, '$');
391
392 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
393
394 if (p1 == 0)
395 break;
396 p = p1 + 1;
397
398 /* Dispatch on the char that follows the $. */
399
400 switch (*p)
401 {
402 case '$':
403 /* $$ seen means output one $ to the variable output buffer. */
404 o = variable_buffer_output (o, p, 1);
405 break;
406
407 case '(':
408 case '{':
409 /* $(...) or ${...} is the general case of substitution. */
410 {
411 char openparen = *p;
412 char closeparen = (openparen == '(') ? ')' : '}';
413 const char *begp;
414 const char *beg = p + 1;
415 char *op;
416 char *abeg = NULL;
417 const char *end, *colon;
418
419 op = o;
420 begp = p;
421 if (handle_function (&op, &begp))
422 {
423 o = op;
424 p = begp;
425 break;
426 }
427
428 /* Is there a variable reference inside the parens or braces?
429 If so, expand it before expanding the entire reference. */
430
431 end = strchr (beg, closeparen);
432 if (end == 0)
433 /* Unterminated variable reference. */
434 fatal (*expanding_var, _("unterminated variable reference"));
435 p1 = lindex (beg, end, '$');
436 if (p1 != 0)
437 {
438 /* BEG now points past the opening paren or brace.
439 Count parens or braces until it is matched. */
440 int count = 0;
441 for (p = beg; *p != '\0'; ++p)
442 {
443 if (*p == openparen)
444 ++count;
445 else if (*p == closeparen && --count < 0)
446 break;
447 }
448 /* If COUNT is >= 0, there were unmatched opening parens
449 or braces, so we go to the simple case of a variable name
450 such as `$($(a)'. */
451 if (count < 0)
452 {
453 abeg = expand_argument (beg, p); /* Expand the name. */
454 beg = abeg;
455 end = strchr (beg, '\0');
456 }
457 }
458 else
459 /* Advance P to the end of this reference. After we are
460 finished expanding this one, P will be incremented to
461 continue the scan. */
462 p = end;
463
464 /* This is not a reference to a built-in function and
465 any variable references inside are now expanded.
466 Is the resultant text a substitution reference? */
467
468 colon = lindex (beg, end, ':');
469 if (colon)
470 {
471 /* This looks like a substitution reference: $(FOO:A=B). */
472 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
473
474 subst_beg = colon + 1;
475 subst_end = lindex (subst_beg, end, '=');
476 if (subst_end == 0)
477 /* There is no = in sight. Punt on the substitution
478 reference and treat this as a variable name containing
479 a colon, in the code below. */
480 colon = 0;
481 else
482 {
483 replace_beg = subst_end + 1;
484 replace_end = end;
485
486 /* Extract the variable name before the colon
487 and look up that variable. */
488 v = lookup_variable (beg, colon - beg);
489 if (v == 0)
490 warn_undefined (beg, colon - beg);
491
492 /* If the variable is not empty, perform the
493 substitution. */
494 if (v != 0 && *v->value != '\0')
495 {
496 char *pattern, *replace, *ppercent, *rpercent;
497 char *value = (v->recursive
498 ? recursively_expand (v)
499 : v->value);
500
501 /* Copy the pattern and the replacement. Add in an
502 extra % at the beginning to use in case there
503 isn't one in the pattern. */
504 pattern = alloca (subst_end - subst_beg + 2);
505 *(pattern++) = '%';
506 memcpy (pattern, subst_beg, subst_end - subst_beg);
507 pattern[subst_end - subst_beg] = '\0';
508
509 replace = alloca (replace_end - replace_beg + 2);
510 *(replace++) = '%';
511 memcpy (replace, replace_beg,
512 replace_end - replace_beg);
513 replace[replace_end - replace_beg] = '\0';
514
515 /* Look for %. Set the percent pointers properly
516 based on whether we find one or not. */
517 ppercent = find_percent (pattern);
518 if (ppercent)
519 {
520 ++ppercent;
521 rpercent = find_percent (replace);
522 if (rpercent)
523 ++rpercent;
524 }
525 else
526 {
527 ppercent = pattern;
528 rpercent = replace;
529 --pattern;
530 --replace;
531 }
532
533 o = patsubst_expand_pat (o, value, pattern, replace,
534 ppercent, rpercent);
535
536 if (v->recursive)
537 free (value);
538 }
539 }
540 }
541
542 if (colon == 0)
543 /* This is an ordinary variable reference.
544 Look up the value of the variable. */
545 o = reference_variable (o, beg, end - beg);
546
547 if (abeg)
548 free (abeg);
549 }
550 break;
551
552 case '\0':
553 break;
554
555 default:
556 if (isblank ((unsigned char)p[-1]))
557 break;
558
559 /* A $ followed by a random char is a variable reference:
560 $a is equivalent to $(a). */
561 o = reference_variable (o, p, 1);
562
563 break;
564 }
565
566 if (*p == '\0')
567 break;
568
569 ++p;
570 }
571
572 if (abuf)
573 free (abuf);
574
575 variable_buffer_output (o, "", 1);
576 return (variable_buffer + line_offset);
577}
578
579#else /* CONFIG_WITH_VALUE_LENGTH */
580/* Scan STRING for variable references and expansion-function calls. Only
581 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
582 a null byte is found.
583
584 Write the results to LINE, which must point into `variable_buffer'. If
585 LINE is NULL, start at the beginning of the buffer.
586 Return a pointer to LINE, or to the beginning of the buffer if LINE is
587 NULL. Set EOLP to point to the string terminator.
588 */
589char *
590variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
591{
592 struct variable *v;
593 const char *p, *p1, *eos;
594 char *o;
595 unsigned int line_offset;
596
597 if (!line)
598 line = initialize_variable_output();
599 o = line;
600 line_offset = line - variable_buffer;
601
602 if (length < 0)
603 length = strlen (string);
604 else
605 MY_ASSERT_MSG (string + length == (p1 = memchr (string, '\0', length)) || !p1, ("len=%ld p1=%p %s\n", length, p1, line));
606
607 /* Simple 1: Emptry string. */
608
609 if (length == 0)
610 {
611 o = variable_buffer_output (o, "\0", 2);
612 *eolp = o - 2;
613 return (variable_buffer + line_offset);
614 }
615
616 /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
617
618 p1 = (const char *)memchr (string, '$', length);
619 if (p1 == 0)
620 {
621 o = variable_buffer_output (o, string, length);
622 o = variable_buffer_output (o, "\0", 2);
623 *eolp = o - 2;
624 assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
625 return (variable_buffer + line_offset);
626 }
627
628 p = string;
629 eos = p + length;
630
631 while (1)
632 {
633 /* Copy all following uninteresting chars all at once to the
634 variable output buffer, and skip them. Uninteresting chars end
635 at the next $ or the end of the input. */
636
637 o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
638
639 if (p1 == 0)
640 break;
641 p = p1 + 1;
642
643 /* Dispatch on the char that follows the $. */
644
645 switch (*p)
646 {
647 case '$':
648 /* $$ seen means output one $ to the variable output buffer. */
649 o = variable_buffer_output (o, p, 1);
650 break;
651
652 case '(':
653 case '{':
654 /* $(...) or ${...} is the general case of substitution. */
655 {
656 char openparen = *p;
657 char closeparen = (openparen == '(') ? ')' : '}';
658 const char *begp;
659 const char *beg = p + 1;
660 char *op;
661 char *abeg = NULL;
662 unsigned int alen = 0;
663 const char *end, *colon;
664
665 op = o;
666 begp = p;
667 end = may_be_function_name (p + 1, eos);
668 if ( end
669 && handle_function (&op, &begp, end, eos))
670 {
671 o = op;
672 p = begp;
673 MY_ASSERT_MSG (!(p1 = memchr (variable_buffer + line_offset, '\0', o - (variable_buffer + line_offset))),
674 ("line=%p o/exp_end=%p act_end=%p\n", variable_buffer + line_offset, o, p1));
675 break;
676 }
677
678 /* Is there a variable reference inside the parens or braces?
679 If so, expand it before expanding the entire reference. */
680
681 end = memchr (beg, closeparen, eos - beg);
682 if (end == 0)
683 /* Unterminated variable reference. */
684 fatal (*expanding_var, _("unterminated variable reference"));
685 p1 = lindex (beg, end, '$');
686 if (p1 != 0)
687 {
688 /* BEG now points past the opening paren or brace.
689 Count parens or braces until it is matched. */
690 int count = 0;
691 for (p = beg; p < eos; ++p)
692 {
693 if (*p == openparen)
694 ++count;
695 else if (*p == closeparen && --count < 0)
696 break;
697 }
698 /* If COUNT is >= 0, there were unmatched opening parens
699 or braces, so we go to the simple case of a variable name
700 such as `$($(a)'. */
701 if (count < 0)
702 {
703 unsigned int len;
704 char saved;
705
706 /* Expand the name. */
707 saved = *p;
708 *(char *)p = '\0'; /* XXX: proove that this is safe! XXX2: shouldn't be necessary any longer! */
709 abeg = allocated_variable_expand_3 (beg, p - beg, &len, &alen);
710 beg = abeg;
711 end = beg + len;
712 *(char *)p = saved;
713 }
714 }
715 else
716 /* Advance P to the end of this reference. After we are
717 finished expanding this one, P will be incremented to
718 continue the scan. */
719 p = end;
720
721 /* This is not a reference to a built-in function and
722 any variable references inside are now expanded.
723 Is the resultant text a substitution reference? */
724
725 colon = lindex (beg, end, ':');
726 if (colon)
727 {
728 /* This looks like a substitution reference: $(FOO:A=B). */
729 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
730
731 subst_beg = colon + 1;
732 subst_end = lindex (subst_beg, end, '=');
733 if (subst_end == 0)
734 /* There is no = in sight. Punt on the substitution
735 reference and treat this as a variable name containing
736 a colon, in the code below. */
737 colon = 0;
738 else
739 {
740 replace_beg = subst_end + 1;
741 replace_end = end;
742
743 /* Extract the variable name before the colon
744 and look up that variable. */
745 v = lookup_variable (beg, colon - beg);
746 if (v == 0)
747 warn_undefined (beg, colon - beg);
748
749 /* If the variable is not empty, perform the
750 substitution. */
751 if (v != 0 && *v->value != '\0')
752 {
753 char *pattern, *replace, *ppercent, *rpercent;
754 char *value = (v->recursive
755 ? recursively_expand (v)
756 : v->value);
757
758 /* Copy the pattern and the replacement. Add in an
759 extra % at the beginning to use in case there
760 isn't one in the pattern. */
761 pattern = alloca (subst_end - subst_beg + 2);
762 *(pattern++) = '%';
763 memcpy (pattern, subst_beg, subst_end - subst_beg);
764 pattern[subst_end - subst_beg] = '\0';
765
766 replace = alloca (replace_end - replace_beg + 2);
767 *(replace++) = '%';
768 memcpy (replace, replace_beg,
769 replace_end - replace_beg);
770 replace[replace_end - replace_beg] = '\0';
771
772 /* Look for %. Set the percent pointers properly
773 based on whether we find one or not. */
774 ppercent = find_percent (pattern);
775 if (ppercent)
776 {
777 ++ppercent;
778 rpercent = find_percent (replace);
779 if (rpercent)
780 ++rpercent;
781 }
782 else
783 {
784 ppercent = pattern;
785 rpercent = replace;
786 --pattern;
787 --replace;
788 }
789
790 o = patsubst_expand_pat (o, value, pattern, replace,
791 ppercent, rpercent);
792
793 if (v->recursive)
794 free (value);
795 }
796 }
797 }
798
799 if (colon == 0)
800 /* This is an ordinary variable reference.
801 Look up the value of the variable. */
802 o = reference_variable (o, beg, end - beg);
803
804 if (abeg)
805 recycle_variable_buffer (abeg, alen);
806 }
807 break;
808
809 case '\0':
810 assert (p == eos);
811 break;
812
813 default:
814 if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
815 break;
816
817 /* A $ followed by a random char is a variable reference:
818 $a is equivalent to $(a). */
819 o = reference_variable (o, p, 1);
820
821 break;
822 }
823
824 if (++p >= eos)
825 break;
826 p1 = memchr (p, '$', eos - p);
827 }
828
829 o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
830 *eolp = o - 2;
831 MY_ASSERT_MSG (strchr (variable_buffer + line_offset, '\0') == *eolp,
832 ("expected=%d actual=%d\nlength=%ld string=%.*s\n",
833 (int)(*eolp - variable_buffer + line_offset), (int)strlen(variable_buffer + line_offset),
834 length, (int)length, string));
835 return (variable_buffer + line_offset);
836}
837#endif /* CONFIG_WITH_VALUE_LENGTH */
838
839
840/* Scan LINE for variable references and expansion-function calls.
841 Build in `variable_buffer' the result of expanding the references and calls.
842 Return the address of the resulting string, which is null-terminated
843 and is valid only until the next time this function is called. */
844
845char *
846variable_expand (const char *line)
847{
848#ifndef CONFIG_WITH_VALUE_LENGTH
849 return variable_expand_string(NULL, line, (long)-1);
850#else /* CONFIG_WITH_VALUE_LENGTH */
851 char *s;
852
853 /* this function is abused a lot like this: variable_expand(""). */
854 if (!*line)
855 {
856 s = variable_buffer_output (initialize_variable_output (), "\0", 2);
857 return s - 2;
858 }
859 return variable_expand_string_2 (NULL, line, (long)-1, &s);
860#endif /* CONFIG_WITH_VALUE_LENGTH */
861}
862
863
864/* Expand an argument for an expansion function.
865 The text starting at STR and ending at END is variable-expanded
866 into a null-terminated string that is returned as the value.
867 This is done without clobbering `variable_buffer' or the current
868 variable-expansion that is in progress. */
869
870char *
871expand_argument (const char *str, const char *end)
872{
873#ifndef CONFIG_WITH_VALUE_LENGTH
874 char *tmp, *alloc = NULL;
875 char *r;
876#endif
877
878 if (str == end)
879 return xstrdup("");
880
881#ifndef CONFIG_WITH_VALUE_LENGTH
882 if (!end || *end == '\0')
883 return allocated_variable_expand (str);
884
885 if (end - str + 1 > 1000)
886 tmp = alloc = xmalloc (end - str + 1);
887 else
888 tmp = alloca (end - str + 1);
889
890 memcpy (tmp, str, end - str);
891 tmp[end - str] = '\0';
892
893 r = allocated_variable_expand (tmp);
894
895 if (alloc)
896 free (alloc);
897
898 return r;
899#else /* CONFIG_WITH_VALUE_LENGTH */
900 if (!end)
901 return allocated_variable_expand_2 (str, ~0U, NULL);
902 return allocated_variable_expand_2 (str, end - str, NULL);
903#endif /* CONFIG_WITH_VALUE_LENGTH */
904}
905
906
907/* Expand LINE for FILE. Error messages refer to the file and line where
908 FILE's commands were found. Expansion uses FILE's variable set list. */
909
910char *
911variable_expand_for_file (const char *line, struct file *file)
912{
913 char *result;
914 struct variable_set_list *savev;
915 const struct floc *savef;
916
917 if (file == 0)
918 return variable_expand (line);
919
920 savev = current_variable_set_list;
921 current_variable_set_list = file->variables;
922
923 savef = reading_file;
924 if (file->cmds && file->cmds->fileinfo.filenm)
925 reading_file = &file->cmds->fileinfo;
926 else
927 reading_file = 0;
928
929 result = variable_expand (line);
930
931 current_variable_set_list = savev;
932 reading_file = savef;
933
934 return result;
935}
936
937
938#if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
939/* Expand LINE for FILE. Error messages refer to the file and line where
940 FILE's commands were found. Expansion uses FILE's variable set list.
941
942 Differs from variable_expand_for_file in that it takes a pointer to
943 where in the variable buffer to start outputting the expanded string,
944 and that it can returned the length of the string if you wish. */
945
946char *
947variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
948 struct file *file, unsigned int *value_lenp)
949{
950 char *result;
951 struct variable_set_list *savev;
952 const struct floc *savef;
953 long len = length == ~0U ? (long)-1 : (long)length;
954 char *eol;
955
956 if (!o)
957 o = initialize_variable_output();
958
959 if (file == 0)
960 result = variable_expand_string_2 (o, line, len, &eol);
961 else
962 {
963 savev = current_variable_set_list;
964 current_variable_set_list = file->variables;
965
966 savef = reading_file;
967 if (file->cmds && file->cmds->fileinfo.filenm)
968 reading_file = &file->cmds->fileinfo;
969 else
970 reading_file = 0;
971
972 result = variable_expand_string_2 (o, line, len, &eol);
973
974 current_variable_set_list = savev;
975 reading_file = savef;
976 }
977
978 if (value_lenp)
979 *value_lenp = eol - result;
980
981 return result;
982}
983
984
985#endif /* CONFIG_WITH_VALUE_LENGTH || CONFIG_WITH_COMMANDS_FUNC */
986/* Like allocated_variable_expand, but for += target-specific variables.
987 First recursively construct the variable value from its appended parts in
988 any upper variable sets. Then expand the resulting value. */
989
990static char *
991variable_append (const char *name, unsigned int length,
992 const struct variable_set_list *set)
993{
994 const struct variable *v;
995 char *buf = 0;
996
997 /* If there's nothing left to check, return the empty buffer. */
998 if (!set)
999 return initialize_variable_output ();
1000
1001 /* Try to find the variable in this variable set. */
1002 v = lookup_variable_in_set (name, length, set->set);
1003
1004 /* If there isn't one, look to see if there's one in a set above us. */
1005 if (!v)
1006 return variable_append (name, length, set->next);
1007
1008 /* If this variable type is append, first get any upper values.
1009 If not, initialize the buffer. */
1010 if (v->append)
1011 buf = variable_append (name, length, set->next);
1012 else
1013 buf = initialize_variable_output ();
1014
1015 /* Append this value to the buffer, and return it.
1016 If we already have a value, first add a space. */
1017 if (buf > variable_buffer)
1018 buf = variable_buffer_output (buf, " ", 1);
1019#ifdef CONFIG_WITH_VALUE_LENGTH
1020 assert (v->value_length == strlen (v->value));
1021#endif
1022
1023 /* Either expand it or copy it, depending. */
1024 if (! v->recursive || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR (v))
1025#ifdef CONFIG_WITH_VALUE_LENGTH
1026 return variable_buffer_output (buf, v->value, v->value_length);
1027#else
1028 return variable_buffer_output (buf, v->value, strlen (v->value));
1029#endif
1030
1031#ifdef CONFIG_WITH_VALUE_LENGTH
1032 variable_expand_string_2 (buf, v->value, v->value_length, &buf);
1033 return buf;
1034#else
1035 buf = variable_expand_string (buf, v->value, strlen (v->value));
1036 return (buf + strlen (buf));
1037#endif
1038}
1039
1040#ifdef CONFIG_WITH_VALUE_LENGTH
1041/* Expands the specified string, appending it to the specified
1042 variable value. */
1043void
1044append_expanded_string_to_variable (struct variable *v, const char *value,
1045 unsigned int value_len, int append)
1046{
1047 char *p = (char *) memchr (value, '$', value_len);
1048 if (!p)
1049 /* fast path */
1050 append_string_to_variable (v,value, value_len, append);
1051 else if (value_len)
1052 {
1053 unsigned int off_dollar = p - (char *)value;
1054
1055 /* Install a fresh variable buffer. */
1056 char *saved_buffer;
1057 unsigned int saved_buffer_length;
1058 install_variable_buffer (&saved_buffer, &saved_buffer_length);
1059
1060 p = variable_buffer;
1061 if (append || !v->value_length)
1062 {
1063 /* Copy the current value into it and append a space. */
1064 if (v->value_length)
1065 {
1066 p = variable_buffer_output (p, v->value, v->value_length);
1067 p = variable_buffer_output (p, " ", 1);
1068 }
1069
1070 /* Append the assignment value. */
1071 p = variable_buffer_output (p, value, off_dollar);
1072 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
1073 }
1074 else
1075 {
1076 /* Expand the assignemnt value. */
1077 p = variable_buffer_output (p, value, off_dollar);
1078 variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
1079
1080 /* Append a space followed by the old value. */
1081 p = variable_buffer_output (p, " ", 1);
1082 p = variable_buffer_output (p, v->value, v->value_length + 1) - 1;
1083 }
1084
1085 /* Replace the variable with the variable buffer. */
1086#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1087 if (v->rdonly_val)
1088 v->rdonly_val = 0;
1089 else
1090#endif
1091 free (v->value);
1092 v->value = variable_buffer;
1093 v->value_length = p - v->value;
1094 v->value_alloc_len = variable_buffer_length;
1095 VARIABLE_CHANGED(v);
1096
1097 /* Restore the variable buffer, but without freeing the current. */
1098 variable_buffer = NULL;
1099 restore_variable_buffer (saved_buffer, saved_buffer_length);
1100 }
1101 /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1102}
1103#endif /* CONFIG_WITH_VALUE_LENGTH */
1104
1105static char *
1106allocated_variable_append (const struct variable *v)
1107{
1108 char *val;
1109
1110 /* Construct the appended variable value. */
1111
1112 char *obuf = variable_buffer;
1113 unsigned int olen = variable_buffer_length;
1114
1115 variable_buffer = 0;
1116
1117 assert ((unsigned int)v->length == strlen (v->name)); /* bird */
1118 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
1119 variable_buffer_output (val, "", 1);
1120 val = variable_buffer;
1121
1122 variable_buffer = obuf;
1123 variable_buffer_length = olen;
1124
1125 return val;
1126}
1127
1128/* Like variable_expand_for_file, but the returned string is malloc'd.
1129 This function is called a lot. It wants to be efficient. */
1130
1131char *
1132allocated_variable_expand_for_file (const char *line, struct file *file)
1133{
1134 char *value;
1135
1136 char *obuf = variable_buffer;
1137 unsigned int olen = variable_buffer_length;
1138
1139 variable_buffer = 0;
1140
1141 value = variable_expand_for_file (line, file);
1142
1143 variable_buffer = obuf;
1144 variable_buffer_length = olen;
1145
1146 return value;
1147}
1148
1149#ifdef CONFIG_WITH_VALUE_LENGTH
1150/* Handle the most common case in allocated_variable_expand_for_file
1151 specially and provide some additional string length features. */
1152
1153char *
1154allocated_variable_expand_2 (const char *line, unsigned int length,
1155 unsigned int *value_lenp)
1156{
1157 char *value;
1158 char *obuf = variable_buffer;
1159 unsigned int olen = variable_buffer_length;
1160 long len = length == ~0U ? -1L : (long)length;
1161 char *eol;
1162
1163 variable_buffer = 0;
1164
1165 value = variable_expand_string_2 (NULL, line, len, &eol);
1166 if (value_lenp)
1167 *value_lenp = eol - value;
1168
1169 variable_buffer = obuf;
1170 variable_buffer_length = olen;
1171
1172 return value;
1173}
1174
1175/* Initially created for handling a special case for variable_expand_string2
1176 where the variable name is expanded and freed right afterwards. This
1177 variant allows the variable_buffer to be recycled and thus avoid bothering
1178 with a slow free implementation. (Darwin is horrible slow.) */
1179
1180char *
1181allocated_variable_expand_3 (const char *line, unsigned int length,
1182 unsigned int *value_lenp,
1183 unsigned int *buffer_lengthp)
1184{
1185 char *obuf = variable_buffer;
1186 unsigned int olen = variable_buffer_length;
1187 long len = (long)length;
1188 char *value;
1189 char *eol;
1190
1191 variable_buffer = 0;
1192
1193 value = variable_expand_string_2 (NULL, line, len, &eol);
1194 if (value_lenp)
1195 *value_lenp = eol - value;
1196 *buffer_lengthp = variable_buffer_length;
1197
1198 variable_buffer = obuf;
1199 variable_buffer_length = olen;
1200
1201 return value;
1202}
1203
1204/* recycle a buffer. */
1205
1206void
1207recycle_variable_buffer (char *buffer, unsigned int length)
1208{
1209 struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
1210
1211 assert (!(length & 31));
1212 assert (length >= 384);
1213 recycled->length = length;
1214 recycled->next = recycled_head;
1215 recycled_head = recycled;
1216}
1217
1218#endif /* CONFIG_WITH_VALUE_LENGTH */
1219
1220/* Install a new variable_buffer context, returning the current one for
1221 safe-keeping. */
1222
1223void
1224install_variable_buffer (char **bufp, unsigned int *lenp)
1225{
1226 *bufp = variable_buffer;
1227 *lenp = variable_buffer_length;
1228
1229 variable_buffer = 0;
1230 initialize_variable_output ();
1231}
1232
1233#ifdef CONFIG_WITH_COMPILER
1234/* Same as install_variable_buffer, except we supply a size hint. */
1235
1236char *
1237install_variable_buffer_with_hint (char **bufp, unsigned int *lenp, unsigned int size_hint)
1238{
1239 struct recycled_buffer *recycled;
1240 char *buf;
1241
1242 *bufp = variable_buffer;
1243 *lenp = variable_buffer_length;
1244
1245 recycled = recycled_head;
1246 if (recycled)
1247 {
1248 recycled_head = recycled->next;
1249 variable_buffer_length = recycled->length;
1250 variable_buffer = buf = (char *)recycled;
1251 }
1252 else
1253 {
1254 if (size_hint < 512)
1255 variable_buffer_length = (size_hint + 1 + 63) & ~(unsigned int)63;
1256 else if (size_hint < 4096)
1257 variable_buffer_length = (size_hint + 1 + 1023) & ~(unsigned int)1023;
1258 else
1259 variable_buffer_length = (size_hint + 1 + 4095) & ~(unsigned int)4095;
1260 variable_buffer = buf = xmalloc (variable_buffer_length);
1261 }
1262 buf[0] = '\0';
1263 return buf;
1264}
1265#endif /* CONFIG_WITH_COMPILER */
1266
1267/* Restore a previously-saved variable_buffer setting (free the
1268 current one). */
1269
1270void
1271restore_variable_buffer (char *buf, unsigned int len)
1272{
1273#ifndef CONFIG_WITH_VALUE_LENGTH
1274 free (variable_buffer);
1275#else
1276 if (variable_buffer)
1277 recycle_variable_buffer (variable_buffer, variable_buffer_length);
1278#endif
1279
1280 variable_buffer = buf;
1281 variable_buffer_length = len;
1282}
1283
1284
1285/* Used to make sure there is at least SIZE bytes of buffer space
1286 available starting at PTR. */
1287char *
1288ensure_variable_buffer_space(char *ptr, unsigned int size)
1289{
1290 unsigned int offset = (unsigned int)(ptr - variable_buffer);
1291 assert(offset <= variable_buffer_length);
1292 if (variable_buffer_length - offset < size)
1293 {
1294 unsigned minlen = size + offset;
1295 variable_buffer_length *= 2;
1296 if (variable_buffer_length < minlen + 100)
1297 variable_buffer_length = (minlen + 100 + 63) & ~(unsigned int)63;
1298 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
1299 ptr = variable_buffer + offset;
1300 }
1301 return ptr;
1302}
1303
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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