Fix Savannah bug #19348: if the user specified
[make.git] / expand.c
blobcc8ede414cdb34a50646ae828d52baed48329484
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 Free Software
4 Foundation, Inc.
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 2, or (at your option) any later version.
11 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 GNU Make; see the file COPYING. If not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
19 #include "make.h"
21 #include <assert.h>
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
27 #include "rule.h"
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. */
57 char *
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
66 ? newlen + 100
67 : 2 * variable_buffer_length);
68 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
69 ptr = variable_buffer + offset;
72 memcpy (ptr, string, length);
73 return ptr + length;
76 /* Return a pointer to the beginning of the variable buffer. */
78 static char *
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);
97 char *
98 recursively_expand_for_file (struct variable *v, struct file *file)
100 char *value;
101 const struct floc *this_var;
102 const struct floc **saved_varp;
103 struct variable_set_list *save = 0;
104 int set_reading = 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. */
116 if (!reading_file)
118 set_reading = 1;
119 reading_file = &v->fileinfo;
122 if (v->expanding)
124 if (!v->exp_count)
125 /* Expanding V causes infinite recursion. Lose. */
126 fatal (*expanding_var,
127 _("Recursive variable `%s' references itself (eventually)"),
128 v->name);
129 --v->exp_count;
132 if (file)
134 save = current_variable_set_list;
135 current_variable_set_list = file->variables;
138 v->expanding = 1;
139 if (v->append)
140 value = allocated_variable_append (v);
141 else
142 value = allocated_variable_expand (v->value);
143 v->expanding = 0;
145 if (set_reading)
146 reading_file = 0;
148 if (file)
149 current_variable_set_list = save;
151 expanding_var = saved_varp;
153 return value;
156 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
158 #ifdef __GNUC__
159 __inline
160 #endif
161 static char *
162 reference_variable (char *o, const char *name, unsigned int length)
164 struct variable *v;
165 char *value;
167 v = lookup_variable (name, length);
169 if (v == 0)
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))
174 return o;
176 value = (v->recursive ? recursively_expand (v) : v->value);
178 o = variable_buffer_output (o, value, strlen (value));
180 if (v->recursive)
181 free (value);
183 return o;
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
193 NULL.
195 char *
196 variable_expand_string (char *line, const char *string, long length)
198 struct variable *v;
199 const char *p, *p1;
200 char *abuf = NULL;
201 char *o;
202 unsigned int line_offset;
204 if (!line)
205 line = initialize_variable_output();
206 o = line;
207 line_offset = line - variable_buffer;
209 if (length == 0)
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);
221 abuf[length] = '\0';
222 string = abuf;
224 p = string;
226 while (1)
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);
236 if (p1 == 0)
237 break;
238 p = p1 + 1;
240 /* Dispatch on the char that follows the $. */
242 switch (*p)
244 case '$':
245 /* $$ seen means output one $ to the variable output buffer. */
246 o = variable_buffer_output (o, p, 1);
247 break;
249 case '(':
250 case '{':
251 /* $(...) or ${...} is the general case of substitution. */
253 char openparen = *p;
254 char closeparen = (openparen == '(') ? ')' : '}';
255 const char *begp;
256 const char *beg = p + 1;
257 char *op;
258 char *abeg = NULL;
259 const char *end, *colon;
261 op = o;
262 begp = p;
263 if (handle_function (&op, &begp))
265 o = op;
266 p = begp;
267 break;
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);
274 if (end == 0)
275 /* Unterminated variable reference. */
276 fatal (*expanding_var, _("unterminated variable reference"));
277 p1 = lindex (beg, end, '$');
278 if (p1 != 0)
280 /* BEG now points past the opening paren or brace.
281 Count parens or braces until it is matched. */
282 int count = 0;
283 for (p = beg; *p != '\0'; ++p)
285 if (*p == openparen)
286 ++count;
287 else if (*p == closeparen && --count < 0)
288 break;
290 /* If COUNT is >= 0, there were unmatched opening parens
291 or braces, so we go to the simple case of a variable name
292 such as `$($(a)'. */
293 if (count < 0)
295 abeg = expand_argument (beg, p); /* Expand the name. */
296 beg = abeg;
297 end = strchr (beg, '\0');
300 else
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. */
304 p = end;
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, ':');
311 if (colon)
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, '=');
318 if (subst_end == 0)
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. */
322 colon = 0;
323 else
325 replace_beg = subst_end + 1;
326 replace_end = end;
328 /* Extract the variable name before the colon
329 and look up that variable. */
330 v = lookup_variable (beg, colon - beg);
331 if (v == 0)
332 warn_undefined (beg, colon - beg);
334 /* If the variable is not empty, perform the
335 substitution. */
336 if (v != 0 && *v->value != '\0')
338 char *pattern, *replace, *ppercent, *rpercent;
339 char *value = (v->recursive
340 ? recursively_expand (v)
341 : v->value);
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);
347 *(pattern++) = '%';
348 memcpy (pattern, subst_beg, subst_end - subst_beg);
349 pattern[subst_end - subst_beg] = '\0';
351 replace = alloca (replace_end - replace_beg + 2);
352 *(replace++) = '%';
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);
360 if (ppercent)
362 ++ppercent;
363 rpercent = find_percent (replace);
364 if (rpercent)
365 ++rpercent;
367 else
369 ppercent = pattern;
370 rpercent = replace;
371 --pattern;
372 --replace;
375 o = patsubst_expand_pat (o, value, pattern, replace,
376 ppercent, rpercent);
378 if (v->recursive)
379 free (value);
384 if (colon == 0)
385 /* This is an ordinary variable reference.
386 Look up the value of the variable. */
387 o = reference_variable (o, beg, end - beg);
389 if (abeg)
390 free (abeg);
392 break;
394 case '\0':
395 break;
397 default:
398 if (isblank ((unsigned char)p[-1]))
399 break;
401 /* A $ followed by a random char is a variable reference:
402 $a is equivalent to $(a). */
403 o = reference_variable (o, p, 1);
405 break;
408 if (*p == '\0')
409 break;
410 else
411 ++p;
414 if (abuf)
415 free (abuf);
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. */
426 char *
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. */
438 char *
439 expand_argument (const char *str, const char *end)
441 char *tmp;
443 if (str == end)
444 return xstrdup("");
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. */
459 char *
460 variable_expand_for_file (const char *line, struct file *file)
462 char *result;
463 struct variable_set_list *save;
465 if (file == 0)
466 return variable_expand (line);
468 save = current_variable_set_list;
469 current_variable_set_list = file->variables;
470 if (file->cmds && file->cmds->fileinfo.filenm)
471 reading_file = &file->cmds->fileinfo;
472 else
473 reading_file = 0;
474 result = variable_expand (line);
475 current_variable_set_list = save;
476 reading_file = 0;
478 return result;
481 /* Like allocated_variable_expand, but for += target-specific variables.
482 First recursively construct the variable value from its appended parts in
483 any upper variable sets. Then expand the resulting value. */
485 static char *
486 variable_append (const char *name, unsigned int length,
487 const struct variable_set_list *set)
489 const struct variable *v;
490 char *buf = 0;
492 /* If there's nothing left to check, return the empty buffer. */
493 if (!set)
494 return initialize_variable_output ();
496 /* Try to find the variable in this variable set. */
497 v = lookup_variable_in_set (name, length, set->set);
499 /* If there isn't one, look to see if there's one in a set above us. */
500 if (!v)
501 return variable_append (name, length, set->next);
503 /* If this variable type is append, first get any upper values.
504 If not, initialize the buffer. */
505 if (v->append)
506 buf = variable_append (name, length, set->next);
507 else
508 buf = initialize_variable_output ();
510 /* Append this value to the buffer, and return it.
511 If we already have a value, first add a space. */
512 if (buf > variable_buffer)
513 buf = variable_buffer_output (buf, " ", 1);
515 /* Either expand it or copy it, depending. */
516 if (! v->recursive)
517 return variable_buffer_output (buf, v->value, strlen (v->value));
519 buf = variable_expand_string (buf, v->value, strlen (v->value));
520 return (buf + strlen (buf));
524 static char *
525 allocated_variable_append (const struct variable *v)
527 char *val;
529 /* Construct the appended variable value. */
531 char *obuf = variable_buffer;
532 unsigned int olen = variable_buffer_length;
534 variable_buffer = 0;
536 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
537 variable_buffer_output (val, "", 1);
538 val = variable_buffer;
540 variable_buffer = obuf;
541 variable_buffer_length = olen;
543 return val;
546 /* Like variable_expand_for_file, but the returned string is malloc'd.
547 This function is called a lot. It wants to be efficient. */
549 char *
550 allocated_variable_expand_for_file (const char *line, struct file *file)
552 char *value;
554 char *obuf = variable_buffer;
555 unsigned int olen = variable_buffer_length;
557 variable_buffer = 0;
559 value = variable_expand_for_file (line, file);
561 #if 0
562 /* Waste a little memory and save time. */
563 value = xrealloc (value, strlen (value))
564 #endif
566 variable_buffer = obuf;
567 variable_buffer_length = olen;
569 return value;
572 /* Install a new variable_buffer context, returning the current one for
573 safe-keeping. */
575 void
576 install_variable_buffer (char **bufp, unsigned int *lenp)
578 *bufp = variable_buffer;
579 *lenp = variable_buffer_length;
581 variable_buffer = 0;
582 initialize_variable_output ();
585 /* Restore a previously-saved variable_buffer setting (free the current one).
588 void
589 restore_variable_buffer (char *buf, unsigned int len)
591 free (variable_buffer);
593 variable_buffer = buf;
594 variable_buffer_length = len;