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
47 static unsigned int variable_buffer_length
;
48 char *variable_buffer
;
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. */
58 variable_buffer_output (char *ptr
, const char *string
, unsigned int length
)
60 register unsigned int newlen
= length
+ (ptr
- variable_buffer
);
62 if ((newlen
+ VARIABLE_BUFFER_ZONE
) > variable_buffer_length
)
64 unsigned int offset
= ptr
- variable_buffer
;
65 variable_buffer_length
= (newlen
+ 100 > 2 * variable_buffer_length
67 : 2 * variable_buffer_length
);
68 variable_buffer
= xrealloc (variable_buffer
, variable_buffer_length
);
69 ptr
= variable_buffer
+ offset
;
72 memcpy (ptr
, string
, length
);
76 /* Return a pointer to the beginning of the variable buffer. */
79 initialize_variable_output (void)
81 /* If we don't have a variable output buffer yet, get one. */
83 if (variable_buffer
== 0)
85 variable_buffer_length
= 200;
86 variable_buffer
= xmalloc (variable_buffer_length
);
87 variable_buffer
[0] = '\0';
90 return variable_buffer
;
93 /* Recursively expand V. The returned string is malloc'd. */
95 static char *allocated_variable_append (const struct variable
*v
);
98 recursively_expand_for_file (struct variable
*v
, struct file
*file
)
101 const struct floc
*this_var
;
102 const struct floc
**saved_varp
;
103 struct variable_set_list
*save
= 0;
106 /* Don't install a new location if this location is empty.
107 This can happen for command-line variables, builtin variables, etc. */
108 saved_varp
= expanding_var
;
109 if (v
->fileinfo
.filenm
)
111 this_var
= &v
->fileinfo
;
112 expanding_var
= &this_var
;
115 /* If we have no other file-reading context, use the variable's context. */
119 reading_file
= &v
->fileinfo
;
125 /* Expanding V causes infinite recursion. Lose. */
126 fatal (*expanding_var
,
127 _("Recursive variable `%s' references itself (eventually)"),
134 save
= current_variable_set_list
;
135 current_variable_set_list
= file
->variables
;
140 value
= allocated_variable_append (v
);
142 value
= allocated_variable_expand (v
->value
);
149 current_variable_set_list
= save
;
151 expanding_var
= saved_varp
;
156 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
162 reference_variable (char *o
, const char *name
, unsigned int length
)
167 v
= lookup_variable (name
, length
);
170 warn_undefined (name
, length
);
172 /* If there's no variable by that name or it has no value, stop now. */
173 if (v
== 0 || (*v
->value
== '\0' && !v
->append
))
176 value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
178 o
= variable_buffer_output (o
, value
, strlen (value
));
186 /* Scan STRING for variable references and expansion-function calls. Only
187 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
188 a null byte is found.
190 Write the results to LINE, which must point into `variable_buffer'. If
191 LINE is NULL, start at the beginning of the buffer.
192 Return a pointer to LINE, or to the beginning of the buffer if LINE is
196 variable_expand_string (char *line
, const char *string
, long length
)
202 unsigned int line_offset
;
205 line
= initialize_variable_output();
207 line_offset
= line
- variable_buffer
;
211 variable_buffer_output (o
, "", 1);
212 return (variable_buffer
);
215 /* If we want a subset of the string, allocate a temporary buffer for it.
216 Most of the functions we use here don't work with length limits. */
217 if (length
> 0 && string
[length
] != '\0')
219 abuf
= xmalloc(length
+1);
220 memcpy(abuf
, string
, length
);
228 /* Copy all following uninteresting chars all at once to the
229 variable output buffer, and skip them. Uninteresting chars end
230 at the next $ or the end of the input. */
232 p1
= strchr (p
, '$');
234 o
= variable_buffer_output (o
, p
, p1
!= 0 ? (unsigned int)(p1
- p
) : strlen (p
) + 1);
240 /* Dispatch on the char that follows the $. */
245 /* $$ seen means output one $ to the variable output buffer. */
246 o
= variable_buffer_output (o
, p
, 1);
251 /* $(...) or ${...} is the general case of substitution. */
254 char closeparen
= (openparen
== '(') ? ')' : '}';
256 const char *beg
= p
+ 1;
259 const char *end
, *colon
;
263 if (handle_function (&op
, &begp
))
270 /* Is there a variable reference inside the parens or braces?
271 If so, expand it before expanding the entire reference. */
273 end
= strchr (beg
, closeparen
);
275 /* Unterminated variable reference. */
276 fatal (*expanding_var
, _("unterminated variable reference"));
277 p1
= lindex (beg
, end
, '$');
280 /* BEG now points past the opening paren or brace.
281 Count parens or braces until it is matched. */
283 for (p
= beg
; *p
!= '\0'; ++p
)
287 else if (*p
== closeparen
&& --count
< 0)
290 /* If COUNT is >= 0, there were unmatched opening parens
291 or braces, so we go to the simple case of a variable name
295 abeg
= expand_argument (beg
, p
); /* Expand the name. */
297 end
= strchr (beg
, '\0');
301 /* Advance P to the end of this reference. After we are
302 finished expanding this one, P will be incremented to
303 continue the scan. */
306 /* This is not a reference to a built-in function and
307 any variable references inside are now expanded.
308 Is the resultant text a substitution reference? */
310 colon
= lindex (beg
, end
, ':');
313 /* This looks like a substitution reference: $(FOO:A=B). */
314 const char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
316 subst_beg
= colon
+ 1;
317 subst_end
= lindex (subst_beg
, end
, '=');
319 /* There is no = in sight. Punt on the substitution
320 reference and treat this as a variable name containing
321 a colon, in the code below. */
325 replace_beg
= subst_end
+ 1;
328 /* Extract the variable name before the colon
329 and look up that variable. */
330 v
= lookup_variable (beg
, colon
- beg
);
332 warn_undefined (beg
, colon
- beg
);
334 /* If the variable is not empty, perform the
336 if (v
!= 0 && *v
->value
!= '\0')
338 char *pattern
, *replace
, *ppercent
, *rpercent
;
339 char *value
= (v
->recursive
340 ? recursively_expand (v
)
343 /* Copy the pattern and the replacement. Add in an
344 extra % at the beginning to use in case there
345 isn't one in the pattern. */
346 pattern
= alloca (subst_end
- subst_beg
+ 2);
348 memcpy (pattern
, subst_beg
, subst_end
- subst_beg
);
349 pattern
[subst_end
- subst_beg
] = '\0';
351 replace
= alloca (replace_end
- replace_beg
+ 2);
353 memcpy (replace
, replace_beg
,
354 replace_end
- replace_beg
);
355 replace
[replace_end
- replace_beg
] = '\0';
357 /* Look for %. Set the percent pointers properly
358 based on whether we find one or not. */
359 ppercent
= find_percent (pattern
);
363 rpercent
= find_percent (replace
);
375 o
= patsubst_expand_pat (o
, value
, pattern
, replace
,
385 /* This is an ordinary variable reference.
386 Look up the value of the variable. */
387 o
= reference_variable (o
, beg
, end
- beg
);
398 if (isblank ((unsigned char)p
[-1]))
401 /* A $ followed by a random char is a variable reference:
402 $a is equivalent to $(a). */
403 o
= reference_variable (o
, p
, 1);
417 variable_buffer_output (o
, "", 1);
418 return (variable_buffer
+ line_offset
);
421 /* Scan LINE for variable references and expansion-function calls.
422 Build in `variable_buffer' the result of expanding the references and calls.
423 Return the address of the resulting string, which is null-terminated
424 and is valid only until the next time this function is called. */
427 variable_expand (const char *line
)
429 return variable_expand_string(NULL
, line
, (long)-1);
432 /* Expand an argument for an expansion function.
433 The text starting at STR and ending at END is variable-expanded
434 into a null-terminated string that is returned as the value.
435 This is done without clobbering `variable_buffer' or the current
436 variable-expansion that is in progress. */
439 expand_argument (const char *str
, const char *end
)
446 if (!end
|| *end
== '\0')
447 return allocated_variable_expand (str
);
449 tmp
= alloca (end
- str
+ 1);
450 memcpy (tmp
, str
, end
- str
);
451 tmp
[end
- str
] = '\0';
453 return allocated_variable_expand (tmp
);
456 /* Expand LINE for FILE. Error messages refer to the file and line where
457 FILE's commands were found. Expansion uses FILE's variable set list. */
460 variable_expand_for_file (const char *line
, struct file
*file
)
463 struct variable_set_list
*savev
;
464 const struct floc
*savef
;
467 return variable_expand (line
);
469 savev
= current_variable_set_list
;
470 current_variable_set_list
= file
->variables
;
472 savef
= reading_file
;
473 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
474 reading_file
= &file
->cmds
->fileinfo
;
478 result
= variable_expand (line
);
480 current_variable_set_list
= savev
;
481 reading_file
= savef
;
486 /* Like allocated_variable_expand, but for += target-specific variables.
487 First recursively construct the variable value from its appended parts in
488 any upper variable sets. Then expand the resulting value. */
491 variable_append (const char *name
, unsigned int length
,
492 const struct variable_set_list
*set
)
494 const struct variable
*v
;
497 /* If there's nothing left to check, return the empty buffer. */
499 return initialize_variable_output ();
501 /* Try to find the variable in this variable set. */
502 v
= lookup_variable_in_set (name
, length
, set
->set
);
504 /* If there isn't one, look to see if there's one in a set above us. */
506 return variable_append (name
, length
, set
->next
);
508 /* If this variable type is append, first get any upper values.
509 If not, initialize the buffer. */
511 buf
= variable_append (name
, length
, set
->next
);
513 buf
= initialize_variable_output ();
515 /* Append this value to the buffer, and return it.
516 If we already have a value, first add a space. */
517 if (buf
> variable_buffer
)
518 buf
= variable_buffer_output (buf
, " ", 1);
520 /* Either expand it or copy it, depending. */
522 return variable_buffer_output (buf
, v
->value
, strlen (v
->value
));
524 buf
= variable_expand_string (buf
, v
->value
, strlen (v
->value
));
525 return (buf
+ strlen (buf
));
530 allocated_variable_append (const struct variable
*v
)
534 /* Construct the appended variable value. */
536 char *obuf
= variable_buffer
;
537 unsigned int olen
= variable_buffer_length
;
541 val
= variable_append (v
->name
, strlen (v
->name
), current_variable_set_list
);
542 variable_buffer_output (val
, "", 1);
543 val
= variable_buffer
;
545 variable_buffer
= obuf
;
546 variable_buffer_length
= olen
;
551 /* Like variable_expand_for_file, but the returned string is malloc'd.
552 This function is called a lot. It wants to be efficient. */
555 allocated_variable_expand_for_file (const char *line
, struct file
*file
)
559 char *obuf
= variable_buffer
;
560 unsigned int olen
= variable_buffer_length
;
564 value
= variable_expand_for_file (line
, file
);
567 /* Waste a little memory and save time. */
568 value
= xrealloc (value
, strlen (value
))
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
;