VirtualBox

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

Last change on this file since 3387 was 3140, checked in by bird, 6 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

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

© 2023 Oracle
ContactPrivacy policyTerms of Use