1 /* Variable expansion functions for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
5 This file is part of GNU Make.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* Initially, any errors reported when expanding strings will be reported
30 against the file where the error appears. */
31 const struct floc
**expanding_var
= &reading_file
;
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.
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. */
45 #define VARIABLE_BUFFER_ZONE 5
48 static unsigned int variable_buffer_length
;
50 unsigned int variable_buffer_length
;
52 char *variable_buffer
;
55 #ifdef CONFIG_WITH_VALUE_LENGTH
56 struct recycled_buffer
58 struct recycled_buffer
*next
;
61 struct recycled_buffer
*recycled_head
;
62 #endif /* CONFIG_WITH_VALUE_LENGTH */
67 /* Subroutine of variable_expand and friends:
68 The text to add is LENGTH chars starting at STRING to the variable_buffer.
69 The text is added to the buffer at PTR, and the updated pointer into
70 the buffer is returned as the value. Thus, the value returned by
71 each call to variable_buffer_output should be the first argument to
72 the following call. */
75 variable_buffer_output (char *ptr
, const char *string
, unsigned int length
)
77 register unsigned int newlen
= length
+ (ptr
- variable_buffer
);
79 if ((newlen
+ VARIABLE_BUFFER_ZONE
) > variable_buffer_length
)
81 unsigned int offset
= ptr
- variable_buffer
;
82 variable_buffer_length
= (newlen
+ 100 > 2 * variable_buffer_length
84 : 2 * variable_buffer_length
);
85 variable_buffer
= xrealloc (variable_buffer
, variable_buffer_length
);
86 ptr
= variable_buffer
+ offset
;
89 memcpy (ptr
, string
, length
);
94 /* Return a pointer to the beginning of the variable buffer. */
97 initialize_variable_output (void)
99 /* If we don't have a variable output buffer yet, get one. */
101 #ifdef CONFIG_WITH_VALUE_LENGTH
102 if (variable_buffer
== 0)
104 struct recycled_buffer
*recycled
= recycled_head
;
107 recycled_head
= recycled
->next
;
108 variable_buffer_length
= recycled
->length
;
109 variable_buffer
= (char *)recycled
;
113 variable_buffer_length
= 384;
114 variable_buffer
= xmalloc (variable_buffer_length
);
116 variable_buffer
[0] = '\0';
118 #else /* CONFIG_WITH_VALUE_LENGTH */
119 if (variable_buffer
== 0)
121 variable_buffer_length
= 200;
122 variable_buffer
= xmalloc (variable_buffer_length
);
123 variable_buffer
[0] = '\0';
125 #endif /* CONFIG_WITH_VALUE_LENGTH */
127 return variable_buffer
;
130 /* Recursively expand V. The returned string is malloc'd. */
132 static char *allocated_variable_append (const struct variable
*v
);
135 #ifndef CONFIG_WITH_VALUE_LENGTH
136 recursively_expand_for_file (struct variable
*v
, struct file
*file
)
138 recursively_expand_for_file (struct variable
*v
, struct file
*file
,
139 unsigned int *value_lenp
)
143 const struct floc
*this_var
;
144 const struct floc
**saved_varp
;
145 struct variable_set_list
*save
= 0;
148 /* Don't install a new location if this location is empty.
149 This can happen for command-line variables, builtin variables, etc. */
150 saved_varp
= expanding_var
;
151 if (v
->fileinfo
.filenm
)
153 this_var
= &v
->fileinfo
;
154 expanding_var
= &this_var
;
157 /* If we have no other file-reading context, use the variable's context. */
161 reading_file
= &v
->fileinfo
;
167 /* Expanding V causes infinite recursion. Lose. */
168 fatal (*expanding_var
,
169 _("Recursive variable `%s' references itself (eventually)"),
176 save
= current_variable_set_list
;
177 current_variable_set_list
= file
->variables
;
181 #ifndef CONFIG_WITH_VALUE_LENGTH
183 value
= allocated_variable_append (v
);
185 value
= allocated_variable_expand (v
->value
);
186 #else /* CONFIG_WITH_VALUE_LENGTH */
188 value
= allocated_variable_expand_2 (v
->value
, v
->value_length
, value_lenp
);
191 value
= allocated_variable_append (v
);
193 *value_lenp
= strlen (value
);
195 #endif /* CONFIG_WITH_VALUE_LENGTH */
202 current_variable_set_list
= save
;
204 expanding_var
= saved_varp
;
209 #ifdef CONFIG_WITH_VALUE_LENGTH
210 /* Static worker for reference_variable() that expands the recursive
211 variable V. The main difference between this and
212 recursively_expand[_for_file] is that this worker avoids the temporary
213 buffer and outputs directly into the current variable buffer (O). */
215 reference_recursive_variable (char *o
, struct variable
*v
)
217 const struct floc
*this_var
;
218 const struct floc
**saved_varp
;
221 /* Don't install a new location if this location is empty.
222 This can happen for command-line variables, builtin variables, etc. */
223 saved_varp
= expanding_var
;
224 if (v
->fileinfo
.filenm
)
226 this_var
= &v
->fileinfo
;
227 expanding_var
= &this_var
;
230 /* If we have no other file-reading context, use the variable's context. */
234 reading_file
= &v
->fileinfo
;
240 /* Expanding V causes infinite recursion. Lose. */
241 fatal (*expanding_var
,
242 _("Recursive variable `%s' references itself (eventually)"),
249 /* Expand directly into the variable buffer. */
250 variable_expand_string_2 (o
, v
->value
, v
->value_length
, &o
);
253 /* XXX: Feel free to optimize appending target variables as well. */
254 char *value
= allocated_variable_append (v
);
255 unsigned int value_len
= strlen (value
);
256 o
= variable_buffer_output (o
, value
, value_len
);
264 expanding_var
= saved_varp
;
268 #endif /* CONFIG_WITH_VALUE_LENGTH */
270 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
272 #ifdef MY_INLINE /* bird */
275 #if defined(__GNUC__)
280 reference_variable (char *o
, const char *name
, unsigned int length
)
283 #ifndef CONFIG_WITH_VALUE_LENGTH
287 v
= lookup_variable (name
, length
);
290 warn_undefined (name
, length
);
292 /* If there's no variable by that name or it has no value, stop now. */
293 if (v
== 0 || (*v
->value
== '\0' && !v
->append
))
296 #ifdef CONFIG_WITH_VALUE_LENGTH
297 assert (v
->value_length
== strlen (v
->value
));
299 o
= variable_buffer_output (o
, v
->value
, v
->value_length
);
301 o
= reference_recursive_variable (o
, v
);
302 #else /* !CONFIG_WITH_VALUE_LENGTH */
303 value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
305 o
= variable_buffer_output (o
, value
, strlen (value
));
309 #endif /* !CONFIG_WITH_VALUE_LENGTH */
314 #ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
315 /* Scan STRING for variable references and expansion-function calls. Only
316 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
317 a null byte is found.
319 Write the results to LINE, which must point into `variable_buffer'. If
320 LINE is NULL, start at the beginning of the buffer.
321 Return a pointer to LINE, or to the beginning of the buffer if LINE is
325 variable_expand_string (char *line
, const char *string
, long length
)
331 unsigned int line_offset
;
334 line
= initialize_variable_output();
336 line_offset
= line
- variable_buffer
;
340 variable_buffer_output (o
, "", 1);
341 return (variable_buffer
);
344 /* If we want a subset of the string, allocate a temporary buffer for it.
345 Most of the functions we use here don't work with length limits. */
346 if (length
> 0 && string
[length
] != '\0')
348 abuf
= xmalloc(length
+1);
349 memcpy(abuf
, string
, length
);
357 /* Copy all following uninteresting chars all at once to the
358 variable output buffer, and skip them. Uninteresting chars end
359 at the next $ or the end of the input. */
361 p1
= strchr (p
, '$');
363 o
= variable_buffer_output (o
, p
, p1
!= 0 ? (unsigned int)(p1
- p
) : strlen (p
) + 1);
369 /* Dispatch on the char that follows the $. */
374 /* $$ seen means output one $ to the variable output buffer. */
375 o
= variable_buffer_output (o
, p
, 1);
380 /* $(...) or ${...} is the general case of substitution. */
383 char closeparen
= (openparen
== '(') ? ')' : '}';
385 const char *beg
= p
+ 1;
388 const char *end
, *colon
;
392 if (handle_function (&op
, &begp
))
399 /* Is there a variable reference inside the parens or braces?
400 If so, expand it before expanding the entire reference. */
402 end
= strchr (beg
, closeparen
);
404 /* Unterminated variable reference. */
405 fatal (*expanding_var
, _("unterminated variable reference"));
406 p1
= lindex (beg
, end
, '$');
409 /* BEG now points past the opening paren or brace.
410 Count parens or braces until it is matched. */
412 for (p
= beg
; *p
!= '\0'; ++p
)
416 else if (*p
== closeparen
&& --count
< 0)
419 /* If COUNT is >= 0, there were unmatched opening parens
420 or braces, so we go to the simple case of a variable name
424 abeg
= expand_argument (beg
, p
); /* Expand the name. */
426 end
= strchr (beg
, '\0');
430 /* Advance P to the end of this reference. After we are
431 finished expanding this one, P will be incremented to
432 continue the scan. */
435 /* This is not a reference to a built-in function and
436 any variable references inside are now expanded.
437 Is the resultant text a substitution reference? */
439 colon
= lindex (beg
, end
, ':');
442 /* This looks like a substitution reference: $(FOO:A=B). */
443 const char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
445 subst_beg
= colon
+ 1;
446 subst_end
= lindex (subst_beg
, end
, '=');
448 /* There is no = in sight. Punt on the substitution
449 reference and treat this as a variable name containing
450 a colon, in the code below. */
454 replace_beg
= subst_end
+ 1;
457 /* Extract the variable name before the colon
458 and look up that variable. */
459 v
= lookup_variable (beg
, colon
- beg
);
461 warn_undefined (beg
, colon
- beg
);
463 /* If the variable is not empty, perform the
465 if (v
!= 0 && *v
->value
!= '\0')
467 char *pattern
, *replace
, *ppercent
, *rpercent
;
468 char *value
= (v
->recursive
469 ? recursively_expand (v
)
472 /* Copy the pattern and the replacement. Add in an
473 extra % at the beginning to use in case there
474 isn't one in the pattern. */
475 pattern
= alloca (subst_end
- subst_beg
+ 2);
477 memcpy (pattern
, subst_beg
, subst_end
- subst_beg
);
478 pattern
[subst_end
- subst_beg
] = '\0';
480 replace
= alloca (replace_end
- replace_beg
+ 2);
482 memcpy (replace
, replace_beg
,
483 replace_end
- replace_beg
);
484 replace
[replace_end
- replace_beg
] = '\0';
486 /* Look for %. Set the percent pointers properly
487 based on whether we find one or not. */
488 ppercent
= find_percent (pattern
);
492 rpercent
= find_percent (replace
);
504 o
= patsubst_expand_pat (o
, value
, pattern
, replace
,
514 /* This is an ordinary variable reference.
515 Look up the value of the variable. */
516 o
= reference_variable (o
, beg
, end
- beg
);
527 if (isblank ((unsigned char)p
[-1]))
530 /* A $ followed by a random char is a variable reference:
531 $a is equivalent to $(a). */
532 o
= reference_variable (o
, p
, 1);
546 variable_buffer_output (o
, "", 1);
547 return (variable_buffer
+ line_offset
);
550 #else /* CONFIG_WITH_VALUE_LENGTH */
551 /* Scan STRING for variable references and expansion-function calls. Only
552 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
553 a null byte is found.
555 Write the results to LINE, which must point into `variable_buffer'. If
556 LINE is NULL, start at the beginning of the buffer.
557 Return a pointer to LINE, or to the beginning of the buffer if LINE is
558 NULL. Set EOLP to point to the string terminator.
561 variable_expand_string_2 (char *line
, const char *string
, long length
, char **eolp
)
564 const char *p
, *p1
, *eos
;
566 unsigned int line_offset
;
569 line
= initialize_variable_output();
571 line_offset
= line
- variable_buffer
;
574 length
= strlen (string
);
576 MY_ASSERT_MSG (string
+ length
== (p1
= memchr (string
, '\0', length
)) || !p1
, ("len=%ld p1=%p %s\n", length
, p1
, line
));
578 /* Simple 1: Emptry string. */
582 o
= variable_buffer_output (o
, "\0", 2);
584 return (variable_buffer
+ line_offset
);
587 /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
589 p1
= (const char *)memchr (string
, '$', length
);
592 o
= variable_buffer_output (o
, string
, length
);
593 o
= variable_buffer_output (o
, "\0", 2);
595 assert (strchr (variable_buffer
+ line_offset
, '\0') == *eolp
);
596 return (variable_buffer
+ line_offset
);
604 /* Copy all following uninteresting chars all at once to the
605 variable output buffer, and skip them. Uninteresting chars end
606 at the next $ or the end of the input. */
608 o
= variable_buffer_output (o
, p
, p1
!= 0 ? (p1
- p
) : (eos
- p
));
614 /* Dispatch on the char that follows the $. */
619 /* $$ seen means output one $ to the variable output buffer. */
620 o
= variable_buffer_output (o
, p
, 1);
625 /* $(...) or ${...} is the general case of substitution. */
628 char closeparen
= (openparen
== '(') ? ')' : '}';
630 const char *beg
= p
+ 1;
633 unsigned int alen
= 0;
634 const char *end
, *colon
;
638 end
= may_be_function_name (p
+ 1, eos
);
640 && handle_function (&op
, &begp
, end
, eos
))
644 MY_ASSERT_MSG (!(p1
= memchr (variable_buffer
+ line_offset
, '\0', o
- (variable_buffer
+ line_offset
))),
645 ("line=%p o/exp_end=%p act_end=%p\n", variable_buffer
+ line_offset
, o
, p1
));
649 /* Is there a variable reference inside the parens or braces?
650 If so, expand it before expanding the entire reference. */
652 end
= memchr (beg
, closeparen
, eos
- beg
);
654 /* Unterminated variable reference. */
655 fatal (*expanding_var
, _("unterminated variable reference"));
656 p1
= lindex (beg
, end
, '$');
659 /* BEG now points past the opening paren or brace.
660 Count parens or braces until it is matched. */
662 for (p
= beg
; p
< eos
; ++p
)
666 else if (*p
== closeparen
&& --count
< 0)
669 /* If COUNT is >= 0, there were unmatched opening parens
670 or braces, so we go to the simple case of a variable name
677 /* Expand the name. */
679 *(char *)p
= '\0'; /* XXX: proove that this is safe! XXX2: shouldn't be necessary any longer! */
680 abeg
= allocated_variable_expand_3 (beg
, p
- beg
, &len
, &alen
);
687 /* Advance P to the end of this reference. After we are
688 finished expanding this one, P will be incremented to
689 continue the scan. */
692 /* This is not a reference to a built-in function and
693 any variable references inside are now expanded.
694 Is the resultant text a substitution reference? */
696 colon
= lindex (beg
, end
, ':');
699 /* This looks like a substitution reference: $(FOO:A=B). */
700 const char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
702 subst_beg
= colon
+ 1;
703 subst_end
= lindex (subst_beg
, end
, '=');
705 /* There is no = in sight. Punt on the substitution
706 reference and treat this as a variable name containing
707 a colon, in the code below. */
711 replace_beg
= subst_end
+ 1;
714 /* Extract the variable name before the colon
715 and look up that variable. */
716 v
= lookup_variable (beg
, colon
- beg
);
718 warn_undefined (beg
, colon
- beg
);
720 /* If the variable is not empty, perform the
722 if (v
!= 0 && *v
->value
!= '\0')
724 char *pattern
, *replace
, *ppercent
, *rpercent
;
725 char *value
= (v
->recursive
726 ? recursively_expand (v
)
729 /* Copy the pattern and the replacement. Add in an
730 extra % at the beginning to use in case there
731 isn't one in the pattern. */
732 pattern
= alloca (subst_end
- subst_beg
+ 2);
734 memcpy (pattern
, subst_beg
, subst_end
- subst_beg
);
735 pattern
[subst_end
- subst_beg
] = '\0';
737 replace
= alloca (replace_end
- replace_beg
+ 2);
739 memcpy (replace
, replace_beg
,
740 replace_end
- replace_beg
);
741 replace
[replace_end
- replace_beg
] = '\0';
743 /* Look for %. Set the percent pointers properly
744 based on whether we find one or not. */
745 ppercent
= find_percent (pattern
);
749 rpercent
= find_percent (replace
);
761 o
= patsubst_expand_pat (o
, value
, pattern
, replace
,
771 /* This is an ordinary variable reference.
772 Look up the value of the variable. */
773 o
= reference_variable (o
, beg
, end
- beg
);
776 recycle_variable_buffer (abeg
, alen
);
785 if (isblank ((unsigned char)p
[-1])) /* XXX: This looks incorrect, previous is '$' */
788 /* A $ followed by a random char is a variable reference:
789 $a is equivalent to $(a). */
790 o
= reference_variable (o
, p
, 1);
797 p1
= memchr (p
, '$', eos
- p
);
800 o
= variable_buffer_output (o
, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
802 MY_ASSERT_MSG (strchr (variable_buffer
+ line_offset
, '\0') == *eolp
,
803 ("expected=%d actual=%d\nlength=%ld string=%.*s\n",
804 (int)(*eolp
- variable_buffer
+ line_offset
), (int)strlen(variable_buffer
+ line_offset
),
805 length
, (int)length
, string
));
806 return (variable_buffer
+ line_offset
);
808 #endif /* CONFIG_WITH_VALUE_LENGTH */
810 /* Scan LINE for variable references and expansion-function calls.
811 Build in `variable_buffer' the result of expanding the references and calls.
812 Return the address of the resulting string, which is null-terminated
813 and is valid only until the next time this function is called. */
816 variable_expand (const char *line
)
818 #ifndef CONFIG_WITH_VALUE_LENGTH
819 return variable_expand_string(NULL
, line
, (long)-1);
820 #else /* CONFIG_WITH_VALUE_LENGTH */
823 /* this function is abused a lot like this: variable_expand(""). */
826 s
= variable_buffer_output (initialize_variable_output (), "\0", 2);
829 return variable_expand_string_2 (NULL
, line
, (long)-1, &s
);
830 #endif /* CONFIG_WITH_VALUE_LENGTH */
833 /* Expand an argument for an expansion function.
834 The text starting at STR and ending at END is variable-expanded
835 into a null-terminated string that is returned as the value.
836 This is done without clobbering `variable_buffer' or the current
837 variable-expansion that is in progress. */
840 expand_argument (const char *str
, const char *end
)
842 #ifndef CONFIG_WITH_VALUE_LENGTH
849 #ifndef CONFIG_WITH_VALUE_LENGTH
850 if (!end
|| *end
== '\0')
851 return allocated_variable_expand (str
);
853 tmp
= alloca (end
- str
+ 1);
854 memcpy (tmp
, str
, end
- str
);
855 tmp
[end
- str
] = '\0';
857 return allocated_variable_expand (tmp
);
858 #else /* CONFIG_WITH_VALUE_LENGTH */
860 return allocated_variable_expand_2 (str
, ~0U, NULL
);
861 return allocated_variable_expand_2 (str
, end
- str
, NULL
);
862 #endif /* CONFIG_WITH_VALUE_LENGTH */
865 /* Expand LINE for FILE. Error messages refer to the file and line where
866 FILE's commands were found. Expansion uses FILE's variable set list. */
869 variable_expand_for_file (const char *line
, struct file
*file
)
872 struct variable_set_list
*savev
;
873 const struct floc
*savef
;
876 return variable_expand (line
);
878 savev
= current_variable_set_list
;
879 current_variable_set_list
= file
->variables
;
881 savef
= reading_file
;
882 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
883 reading_file
= &file
->cmds
->fileinfo
;
887 result
= variable_expand (line
);
889 current_variable_set_list
= savev
;
890 reading_file
= savef
;
895 #if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
896 /* Expand LINE for FILE. Error messages refer to the file and line where
897 FILE's commands were found. Expansion uses FILE's variable set list.
899 Differs from variable_expand_for_file in that it takes a pointer to
900 where in the variable buffer to start outputting the expanded string,
901 and that it can returned the length of the string if you wish. */
904 variable_expand_for_file_2 (char *o
, const char *line
, unsigned int length
,
905 struct file
*file
, unsigned int *value_lenp
)
908 struct variable_set_list
*savev
;
909 const struct floc
*savef
;
910 long len
= length
== ~0U ? (long)-1 : (long)length
;
914 o
= initialize_variable_output();
917 result
= variable_expand_string_2 (o
, line
, len
, &eol
);
920 savev
= current_variable_set_list
;
921 current_variable_set_list
= file
->variables
;
923 savef
= reading_file
;
924 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
925 reading_file
= &file
->cmds
->fileinfo
;
929 result
= variable_expand_string_2 (o
, line
, len
, &eol
);
931 current_variable_set_list
= savev
;
932 reading_file
= savef
;
936 *value_lenp
= eol
- result
;
941 #endif /* CONFIG_WITH_VALUE_LENGTH || CONFIG_WITH_COMMANDS_FUNC */
942 /* Like allocated_variable_expand, but for += target-specific variables.
943 First recursively construct the variable value from its appended parts in
944 any upper variable sets. Then expand the resulting value. */
947 variable_append (const char *name
, unsigned int length
,
948 const struct variable_set_list
*set
)
950 const struct variable
*v
;
953 /* If there's nothing left to check, return the empty buffer. */
955 return initialize_variable_output ();
957 /* Try to find the variable in this variable set. */
958 v
= lookup_variable_in_set (name
, length
, set
->set
);
960 /* If there isn't one, look to see if there's one in a set above us. */
962 return variable_append (name
, length
, set
->next
);
964 /* If this variable type is append, first get any upper values.
965 If not, initialize the buffer. */
967 buf
= variable_append (name
, length
, set
->next
);
969 buf
= initialize_variable_output ();
971 /* Append this value to the buffer, and return it.
972 If we already have a value, first add a space. */
973 if (buf
> variable_buffer
)
974 buf
= variable_buffer_output (buf
, " ", 1);
975 #ifdef CONFIG_WITH_VALUE_LENGTH
976 assert (v
->value_length
== strlen (v
->value
));
979 /* Either expand it or copy it, depending. */
981 #ifdef CONFIG_WITH_VALUE_LENGTH
982 return variable_buffer_output (buf
, v
->value
, v
->value_length
);
984 return variable_buffer_output (buf
, v
->value
, strlen (v
->value
));
987 #ifdef CONFIG_WITH_VALUE_LENGTH
988 variable_expand_string_2 (buf
, v
->value
, v
->value_length
, &buf
);
991 buf
= variable_expand_string (buf
, v
->value
, strlen (v
->value
));
992 return (buf
+ strlen (buf
));
996 #ifdef CONFIG_WITH_VALUE_LENGTH
997 /* Expands the specified string, appending it to the specified
1000 append_expanded_string_to_variable (struct variable
*v
, const char *value
,
1001 unsigned int value_len
, int append
)
1003 char *p
= (char *) memchr (value
, '$', value_len
);
1006 append_string_to_variable (v
,value
, value_len
, append
);
1009 unsigned int off_dollar
= p
- (char *)value
;
1011 /* Install a fresh variable buffer. */
1013 unsigned int saved_buffer_length
;
1014 install_variable_buffer (&saved_buffer
, &saved_buffer_length
);
1016 p
= variable_buffer
;
1017 if (append
|| !v
->value_length
)
1019 /* Copy the current value into it and append a space. */
1020 if (v
->value_length
)
1022 p
= variable_buffer_output (p
, v
->value
, v
->value_length
);
1023 p
= variable_buffer_output (p
, " ", 1);
1026 /* Append the assignment value. */
1027 p
= variable_buffer_output (p
, value
, off_dollar
);
1028 variable_expand_string_2 (p
, value
+ off_dollar
, value_len
- off_dollar
, &p
);
1032 /* Expand the assignemnt value. */
1033 p
= variable_buffer_output (p
, value
, off_dollar
);
1034 variable_expand_string_2 (p
, value
+ off_dollar
, value_len
- off_dollar
, &p
);
1036 /* Append a space followed by the old value. */
1037 p
= variable_buffer_output (p
, " ", 1);
1038 p
= variable_buffer_output (p
, v
->value
, v
->value_length
+ 1) - 1;
1041 /* Replace the variable with the variable buffer. */
1042 #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
1048 v
->value
= variable_buffer
;
1049 v
->value_length
= p
- v
->value
;
1050 v
->value_alloc_len
= variable_buffer_length
;
1052 /* Restore the variable buffer, but without freeing the current. */
1053 variable_buffer
= NULL
;
1054 restore_variable_buffer (saved_buffer
, saved_buffer_length
);
1056 /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
1058 #endif /* CONFIG_WITH_VALUE_LENGTH */
1061 allocated_variable_append (const struct variable
*v
)
1065 /* Construct the appended variable value. */
1067 char *obuf
= variable_buffer
;
1068 unsigned int olen
= variable_buffer_length
;
1070 variable_buffer
= 0;
1072 assert ((unsigned int)v
->length
== strlen (v
->name
)); /* bird */
1073 val
= variable_append (v
->name
, strlen (v
->name
), current_variable_set_list
);
1074 variable_buffer_output (val
, "", 1);
1075 val
= variable_buffer
;
1077 variable_buffer
= obuf
;
1078 variable_buffer_length
= olen
;
1083 /* Like variable_expand_for_file, but the returned string is malloc'd.
1084 This function is called a lot. It wants to be efficient. */
1087 allocated_variable_expand_for_file (const char *line
, struct file
*file
)
1091 char *obuf
= variable_buffer
;
1092 unsigned int olen
= variable_buffer_length
;
1094 variable_buffer
= 0;
1096 value
= variable_expand_for_file (line
, file
);
1099 /* Waste a little memory and save time. */
1100 value
= xrealloc (value
, strlen (value
))
1103 variable_buffer
= obuf
;
1104 variable_buffer_length
= olen
;
1109 #ifdef CONFIG_WITH_VALUE_LENGTH
1110 /* Handle the most common case in allocated_variable_expand_for_file
1111 specially and provide some additional string lenght features. */
1114 allocated_variable_expand_2 (const char *line
, unsigned int length
,
1115 unsigned int *value_lenp
)
1118 char *obuf
= variable_buffer
;
1119 unsigned int olen
= variable_buffer_length
;
1120 long len
= length
== ~0U ? -1L : (long)length
;
1123 variable_buffer
= 0;
1125 value
= variable_expand_string_2 (NULL
, line
, len
, &eol
);
1127 *value_lenp
= eol
- value
;
1129 variable_buffer
= obuf
;
1130 variable_buffer_length
= olen
;
1135 /* Initially created for handling a special case for variable_expand_string2
1136 where the variable name is expanded and freed right afterwards. This
1137 variant allows the variable_buffer to be recycled and thus avoid bothering
1138 with a slow free implementation. (Darwin is horrible slow.) */
1141 allocated_variable_expand_3 (const char *line
, unsigned int length
,
1142 unsigned int *value_lenp
,
1143 unsigned int *buffer_lengthp
)
1145 char *obuf
= variable_buffer
;
1146 unsigned int olen
= variable_buffer_length
;
1147 long len
= (long)length
;
1151 variable_buffer
= 0;
1153 value
= variable_expand_string_2 (NULL
, line
, len
, &eol
);
1155 *value_lenp
= eol
- value
;
1156 *buffer_lengthp
= variable_buffer_length
;
1158 variable_buffer
= obuf
;
1159 variable_buffer_length
= olen
;
1164 /* recycle a buffer. */
1167 recycle_variable_buffer (char *buffer
, unsigned int length
)
1169 struct recycled_buffer
*recycled
= (struct recycled_buffer
*)buffer
;
1171 assert (!(length
& 31));
1172 assert (length
>= 384);
1173 recycled
->length
= length
;
1174 recycled
->next
= recycled_head
;
1175 recycled_head
= recycled
;
1178 #endif /* CONFIG_WITH_VALUE_LENGTH */
1180 /* Install a new variable_buffer context, returning the current one for
1184 install_variable_buffer (char **bufp
, unsigned int *lenp
)
1186 *bufp
= variable_buffer
;
1187 *lenp
= variable_buffer_length
;
1189 variable_buffer
= 0;
1190 initialize_variable_output ();
1193 /* Restore a previously-saved variable_buffer setting (free the current one).
1197 restore_variable_buffer (char *buf
, unsigned int len
)
1199 #ifndef CONFIG_WITH_VALUE_LENGTH
1200 free (variable_buffer
);
1202 if (variable_buffer
)
1203 recycle_variable_buffer (variable_buffer
, variable_buffer_length
);
1206 variable_buffer
= buf
;
1207 variable_buffer_length
= len
;