source: trunk/src/kmk/expand.c@ 1827

Last change on this file since 1827 was 1827, checked in by bird, 17 years ago

kmk: more length optimizations.

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