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

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

kmk: Some minor optimizations.

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