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

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

kmk: More memchr and less strlen for simple assignments (var:=val).

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