1 /* Variable expansion functions for GNU Make.
2 Copyright (C) 1988-2012 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* Initially, any errors reported when expanding strings will be reported
28 against the file where the error appears. */
29 const struct floc
**expanding_var
= &reading_file
;
31 /* The next two describe the variable output buffer.
32 This buffer is used to hold the variable-expansion of a line of the
33 makefile. It is made bigger with realloc whenever it is too small.
34 variable_buffer_length is the size currently allocated.
35 variable_buffer is the address of the buffer.
37 For efficiency, it's guaranteed that the buffer will always have
38 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
39 extra chars without having to call a function. Note you should never use
40 these bytes unless you're _sure_ you have room (you know when the buffer
41 length was last checked. */
43 #define VARIABLE_BUFFER_ZONE 5
45 static unsigned int variable_buffer_length
;
46 char *variable_buffer
;
48 /* Subroutine of variable_expand and friends:
49 The text to add is LENGTH chars starting at STRING to the variable_buffer.
50 The text is added to the buffer at PTR, and the updated pointer into
51 the buffer is returned as the value. Thus, the value returned by
52 each call to variable_buffer_output should be the first argument to
53 the following call. */
56 variable_buffer_output (char *ptr
, const char *string
, unsigned int length
)
58 register unsigned int newlen
= length
+ (ptr
- variable_buffer
);
60 if ((newlen
+ VARIABLE_BUFFER_ZONE
) > variable_buffer_length
)
62 unsigned int offset
= ptr
- variable_buffer
;
63 variable_buffer_length
= (newlen
+ 100 > 2 * variable_buffer_length
65 : 2 * variable_buffer_length
);
66 variable_buffer
= xrealloc (variable_buffer
, variable_buffer_length
);
67 ptr
= variable_buffer
+ offset
;
70 memcpy (ptr
, string
, length
);
74 /* Return a pointer to the beginning of the variable buffer. */
77 initialize_variable_output (void)
79 /* If we don't have a variable output buffer yet, get one. */
81 if (variable_buffer
== 0)
83 variable_buffer_length
= 200;
84 variable_buffer
= xmalloc (variable_buffer_length
);
85 variable_buffer
[0] = '\0';
88 return variable_buffer
;
91 /* Recursively expand V. The returned string is malloc'd. */
93 static char *allocated_variable_append (const struct variable
*v
);
96 recursively_expand_for_file (struct variable
*v
, struct file
*file
)
99 const struct floc
*this_var
;
100 const struct floc
**saved_varp
;
101 struct variable_set_list
*save
= 0;
104 /* Don't install a new location if this location is empty.
105 This can happen for command-line variables, builtin variables, etc. */
106 saved_varp
= expanding_var
;
107 if (v
->fileinfo
.filenm
)
109 this_var
= &v
->fileinfo
;
110 expanding_var
= &this_var
;
113 /* If we have no other file-reading context, use the variable's context. */
117 reading_file
= &v
->fileinfo
;
123 /* Expanding V causes infinite recursion. Lose. */
124 fatal (*expanding_var
,
125 _("Recursive variable '%s' references itself (eventually)"),
132 save
= current_variable_set_list
;
133 current_variable_set_list
= file
->variables
;
138 value
= allocated_variable_append (v
);
140 value
= allocated_variable_expand (v
->value
);
147 current_variable_set_list
= save
;
149 expanding_var
= saved_varp
;
154 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
160 reference_variable (char *o
, const char *name
, unsigned int length
)
165 v
= lookup_variable (name
, length
);
168 warn_undefined (name
, length
);
170 /* If there's no variable by that name or it has no value, stop now. */
171 if (v
== 0 || (*v
->value
== '\0' && !v
->append
))
174 value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
176 o
= variable_buffer_output (o
, value
, strlen (value
));
184 /* Scan STRING for variable references and expansion-function calls. Only
185 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
186 a null byte is found.
188 Write the results to LINE, which must point into 'variable_buffer'. If
189 LINE is NULL, start at the beginning of the buffer.
190 Return a pointer to LINE, or to the beginning of the buffer if LINE is
194 variable_expand_string (char *line
, const char *string
, long length
)
200 unsigned int line_offset
;
203 line
= initialize_variable_output();
205 line_offset
= line
- variable_buffer
;
209 variable_buffer_output (o
, "", 1);
210 return (variable_buffer
);
213 /* We need a copy of STRING: due to eval, it's possible that it will get
214 freed as we process it (it might be the value of a variable that's reset
215 for example). Also having a nil-terminated string is handy. */
216 save
= length
< 0 ? xstrdup (string
) : xstrndup (string
, length
);
221 /* Copy all following uninteresting chars all at once to the
222 variable output buffer, and skip them. Uninteresting chars end
223 at the next $ or the end of the input. */
225 p1
= strchr (p
, '$');
227 o
= variable_buffer_output (o
, p
, p1
!= 0 ? (unsigned int)(p1
- p
) : strlen (p
) + 1);
233 /* Dispatch on the char that follows the $. */
238 /* $$ seen means output one $ to the variable output buffer. */
239 o
= variable_buffer_output (o
, p
, 1);
244 /* $(...) or ${...} is the general case of substitution. */
247 char closeparen
= (openparen
== '(') ? ')' : '}';
249 const char *beg
= p
+ 1;
252 const char *end
, *colon
;
256 if (handle_function (&op
, &begp
))
263 /* Is there a variable reference inside the parens or braces?
264 If so, expand it before expanding the entire reference. */
266 end
= strchr (beg
, closeparen
);
268 /* Unterminated variable reference. */
269 fatal (*expanding_var
, _("unterminated variable reference"));
270 p1
= lindex (beg
, end
, '$');
273 /* BEG now points past the opening paren or brace.
274 Count parens or braces until it is matched. */
276 for (p
= beg
; *p
!= '\0'; ++p
)
280 else if (*p
== closeparen
&& --count
< 0)
283 /* If COUNT is >= 0, there were unmatched opening parens
284 or braces, so we go to the simple case of a variable name
288 abeg
= expand_argument (beg
, p
); /* Expand the name. */
290 end
= strchr (beg
, '\0');
294 /* Advance P to the end of this reference. After we are
295 finished expanding this one, P will be incremented to
296 continue the scan. */
299 /* This is not a reference to a built-in function and
300 any variable references inside are now expanded.
301 Is the resultant text a substitution reference? */
303 colon
= lindex (beg
, end
, ':');
306 /* This looks like a substitution reference: $(FOO:A=B). */
307 const char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
309 subst_beg
= colon
+ 1;
310 subst_end
= lindex (subst_beg
, end
, '=');
312 /* There is no = in sight. Punt on the substitution
313 reference and treat this as a variable name containing
314 a colon, in the code below. */
318 replace_beg
= subst_end
+ 1;
321 /* Extract the variable name before the colon
322 and look up that variable. */
323 v
= lookup_variable (beg
, colon
- beg
);
325 warn_undefined (beg
, colon
- beg
);
327 /* If the variable is not empty, perform the
329 if (v
!= 0 && *v
->value
!= '\0')
331 char *pattern
, *replace
, *ppercent
, *rpercent
;
332 char *value
= (v
->recursive
333 ? recursively_expand (v
)
336 /* Copy the pattern and the replacement. Add in an
337 extra % at the beginning to use in case there
338 isn't one in the pattern. */
339 pattern
= alloca (subst_end
- subst_beg
+ 2);
341 memcpy (pattern
, subst_beg
, subst_end
- subst_beg
);
342 pattern
[subst_end
- subst_beg
] = '\0';
344 replace
= alloca (replace_end
- replace_beg
+ 2);
346 memcpy (replace
, replace_beg
,
347 replace_end
- replace_beg
);
348 replace
[replace_end
- replace_beg
] = '\0';
350 /* Look for %. Set the percent pointers properly
351 based on whether we find one or not. */
352 ppercent
= find_percent (pattern
);
356 rpercent
= find_percent (replace
);
368 o
= patsubst_expand_pat (o
, value
, pattern
, replace
,
378 /* This is an ordinary variable reference.
379 Look up the value of the variable. */
380 o
= reference_variable (o
, beg
, end
- beg
);
391 if (isblank ((unsigned char)p
[-1]))
394 /* A $ followed by a random char is a variable reference:
395 $a is equivalent to $(a). */
396 o
= reference_variable (o
, p
, 1);
409 variable_buffer_output (o
, "", 1);
410 return (variable_buffer
+ line_offset
);
413 /* Scan LINE for variable references and expansion-function calls.
414 Build in 'variable_buffer' the result of expanding the references and calls.
415 Return the address of the resulting string, which is null-terminated
416 and is valid only until the next time this function is called. */
419 variable_expand (const char *line
)
421 return variable_expand_string(NULL
, line
, (long)-1);
424 /* Expand an argument for an expansion function.
425 The text starting at STR and ending at END is variable-expanded
426 into a null-terminated string that is returned as the value.
427 This is done without clobbering 'variable_buffer' or the current
428 variable-expansion that is in progress. */
431 expand_argument (const char *str
, const char *end
)
433 char *tmp
, *alloc
= NULL
;
439 if (!end
|| *end
== '\0')
440 return allocated_variable_expand (str
);
442 if (end
- str
+ 1 > 1000)
443 tmp
= alloc
= xmalloc (end
- str
+ 1);
445 tmp
= alloca (end
- str
+ 1);
447 memcpy (tmp
, str
, end
- str
);
448 tmp
[end
- str
] = '\0';
450 r
= allocated_variable_expand (tmp
);
458 /* Expand LINE for FILE. Error messages refer to the file and line where
459 FILE's commands were found. Expansion uses FILE's variable set list. */
462 variable_expand_for_file (const char *line
, struct file
*file
)
465 struct variable_set_list
*savev
;
466 const struct floc
*savef
;
469 return variable_expand (line
);
471 savev
= current_variable_set_list
;
472 current_variable_set_list
= file
->variables
;
474 savef
= reading_file
;
475 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
476 reading_file
= &file
->cmds
->fileinfo
;
480 result
= variable_expand (line
);
482 current_variable_set_list
= savev
;
483 reading_file
= savef
;
488 /* Like allocated_variable_expand, but for += target-specific variables.
489 First recursively construct the variable value from its appended parts in
490 any upper variable sets. Then expand the resulting value. */
493 variable_append (const char *name
, unsigned int length
,
494 const struct variable_set_list
*set
, int local
)
496 const struct variable
*v
;
498 /* If this set is local and the next is not a parent, then next is local. */
499 int nextlocal
= local
&& set
->next_is_parent
== 0;
501 /* If there's nothing left to check, return the empty buffer. */
503 return initialize_variable_output ();
505 /* Try to find the variable in this variable set. */
506 v
= lookup_variable_in_set (name
, length
, set
->set
);
508 /* If there isn't one, or this one is private, try the set above us. */
509 if (!v
|| (!local
&& v
->private_var
))
510 return variable_append (name
, length
, set
->next
, nextlocal
);
512 /* If this variable type is append, first get any upper values.
513 If not, initialize the buffer. */
515 buf
= variable_append (name
, length
, set
->next
, nextlocal
);
517 buf
= initialize_variable_output ();
519 /* Append this value to the buffer, and return it.
520 If we already have a value, first add a space. */
521 if (buf
> variable_buffer
)
522 buf
= variable_buffer_output (buf
, " ", 1);
524 /* Either expand it or copy it, depending. */
526 return variable_buffer_output (buf
, v
->value
, strlen (v
->value
));
528 buf
= variable_expand_string (buf
, v
->value
, strlen (v
->value
));
529 return (buf
+ strlen (buf
));
534 allocated_variable_append (const struct variable
*v
)
538 /* Construct the appended variable value. */
540 char *obuf
= variable_buffer
;
541 unsigned int olen
= variable_buffer_length
;
545 val
= variable_append (v
->name
, strlen (v
->name
),
546 current_variable_set_list
, 1);
547 variable_buffer_output (val
, "", 1);
548 val
= variable_buffer
;
550 variable_buffer
= obuf
;
551 variable_buffer_length
= olen
;
556 /* Like variable_expand_for_file, but the returned string is malloc'd.
557 This function is called a lot. It wants to be efficient. */
560 allocated_variable_expand_for_file (const char *line
, struct file
*file
)
564 char *obuf
= variable_buffer
;
565 unsigned int olen
= variable_buffer_length
;
569 value
= variable_expand_for_file (line
, file
);
571 variable_buffer
= obuf
;
572 variable_buffer_length
= olen
;
577 /* Install a new variable_buffer context, returning the current one for
581 install_variable_buffer (char **bufp
, unsigned int *lenp
)
583 *bufp
= variable_buffer
;
584 *lenp
= variable_buffer_length
;
587 initialize_variable_output ();
590 /* Restore a previously-saved variable_buffer setting (free the current one).
594 restore_variable_buffer (char *buf
, unsigned int len
)
596 free (variable_buffer
);
598 variable_buffer
= buf
;
599 variable_buffer_length
= len
;