Release GNU make 3.81.
[make.git] / expand.c
blob993e6d4f08205b3e4de6a010b6b140b2d49bf4cd
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, 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 = (char *) xrealloc (variable_buffer,
69 variable_buffer_length);
70 ptr = variable_buffer + offset;
73 bcopy (string, ptr, length);
74 return ptr + length;
77 /* Return a pointer to the beginning of the variable buffer. */
79 static char *
80 initialize_variable_output (void)
82 /* If we don't have a variable output buffer yet, get one. */
84 if (variable_buffer == 0)
86 variable_buffer_length = 200;
87 variable_buffer = (char *) xmalloc (variable_buffer_length);
88 variable_buffer[0] = '\0';
91 return variable_buffer;
94 /* Recursively expand V. The returned string is malloc'd. */
96 static char *allocated_variable_append PARAMS ((const struct variable *v));
98 char *
99 recursively_expand_for_file (struct variable *v, struct file *file)
101 char *value;
102 const struct floc *this_var;
103 const struct floc **saved_varp;
104 struct variable_set_list *save = 0;
105 int set_reading = 0;
107 /* Don't install a new location if this location is empty.
108 This can happen for command-line variables, builtin variables, etc. */
109 saved_varp = expanding_var;
110 if (v->fileinfo.filenm)
112 this_var = &v->fileinfo;
113 expanding_var = &this_var;
116 /* If we have no other file-reading context, use the variable's context. */
117 if (!reading_file)
119 set_reading = 1;
120 reading_file = &v->fileinfo;
123 if (v->expanding)
125 if (!v->exp_count)
126 /* Expanding V causes infinite recursion. Lose. */
127 fatal (*expanding_var,
128 _("Recursive variable `%s' references itself (eventually)"),
129 v->name);
130 --v->exp_count;
133 if (file)
135 save = current_variable_set_list;
136 current_variable_set_list = file->variables;
139 v->expanding = 1;
140 if (v->append)
141 value = allocated_variable_append (v);
142 else
143 value = allocated_variable_expand (v->value);
144 v->expanding = 0;
146 if (set_reading)
147 reading_file = 0;
149 if (file)
150 current_variable_set_list = save;
152 expanding_var = saved_varp;
154 return value;
157 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
159 #ifdef __GNUC__
160 __inline
161 #endif
162 static char *
163 reference_variable (char *o, char *name, unsigned int length)
165 register struct variable *v;
166 char *value;
168 v = lookup_variable (name, length);
170 if (v == 0)
171 warn_undefined (name, length);
173 /* If there's no variable by that name or it has no value, stop now. */
174 if (v == 0 || (*v->value == '\0' && !v->append))
175 return o;
177 value = (v->recursive ? recursively_expand (v) : v->value);
179 o = variable_buffer_output (o, value, strlen (value));
181 if (v->recursive)
182 free (value);
184 return o;
187 /* Scan STRING for variable references and expansion-function calls. Only
188 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
189 a null byte is found.
191 Write the results to LINE, which must point into `variable_buffer'. If
192 LINE is NULL, start at the beginning of the buffer.
193 Return a pointer to LINE, or to the beginning of the buffer if LINE is
194 NULL. */
196 char *
197 variable_expand_string (char *line, char *string, long length)
199 register struct variable *v;
200 register char *p, *o, *p1;
201 char save_char = '\0';
202 unsigned int line_offset;
204 if (!line)
205 line = initialize_variable_output();
207 p = string;
208 o = line;
209 line_offset = line - variable_buffer;
211 if (length >= 0)
213 save_char = string[length];
214 string[length] = '\0';
217 while (1)
219 /* Copy all following uninteresting chars all at once to the
220 variable output buffer, and skip them. Uninteresting chars end
221 at the next $ or the end of the input. */
223 p1 = strchr (p, '$');
225 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
227 if (p1 == 0)
228 break;
229 p = p1 + 1;
231 /* Dispatch on the char that follows the $. */
233 switch (*p)
235 case '$':
236 /* $$ seen means output one $ to the variable output buffer. */
237 o = variable_buffer_output (o, p, 1);
238 break;
240 case '(':
241 case '{':
242 /* $(...) or ${...} is the general case of substitution. */
244 char openparen = *p;
245 char closeparen = (openparen == '(') ? ')' : '}';
246 register char *beg = p + 1;
247 int free_beg = 0;
248 char *op, *begp;
249 char *end, *colon;
251 op = o;
252 begp = p;
253 if (handle_function (&op, &begp))
255 o = op;
256 p = begp;
257 break;
260 /* Is there a variable reference inside the parens or braces?
261 If so, expand it before expanding the entire reference. */
263 end = strchr (beg, closeparen);
264 if (end == 0)
265 /* Unterminated variable reference. */
266 fatal (*expanding_var, _("unterminated variable reference"));
267 p1 = lindex (beg, end, '$');
268 if (p1 != 0)
270 /* BEG now points past the opening paren or brace.
271 Count parens or braces until it is matched. */
272 int count = 0;
273 for (p = beg; *p != '\0'; ++p)
275 if (*p == openparen)
276 ++count;
277 else if (*p == closeparen && --count < 0)
278 break;
280 /* If COUNT is >= 0, there were unmatched opening parens
281 or braces, so we go to the simple case of a variable name
282 such as `$($(a)'. */
283 if (count < 0)
285 beg = expand_argument (beg, p); /* Expand the name. */
286 free_beg = 1; /* Remember to free BEG when finished. */
287 end = strchr (beg, '\0');
290 else
291 /* Advance P to the end of this reference. After we are
292 finished expanding this one, P will be incremented to
293 continue the scan. */
294 p = end;
296 /* This is not a reference to a built-in function and
297 any variable references inside are now expanded.
298 Is the resultant text a substitution reference? */
300 colon = lindex (beg, end, ':');
301 if (colon)
303 /* This looks like a substitution reference: $(FOO:A=B). */
304 char *subst_beg, *subst_end, *replace_beg, *replace_end;
306 subst_beg = colon + 1;
307 subst_end = lindex (subst_beg, end, '=');
308 if (subst_end == 0)
309 /* There is no = in sight. Punt on the substitution
310 reference and treat this as a variable name containing
311 a colon, in the code below. */
312 colon = 0;
313 else
315 replace_beg = subst_end + 1;
316 replace_end = end;
318 /* Extract the variable name before the colon
319 and look up that variable. */
320 v = lookup_variable (beg, colon - beg);
321 if (v == 0)
322 warn_undefined (beg, colon - beg);
324 /* If the variable is not empty, perform the
325 substitution. */
326 if (v != 0 && *v->value != '\0')
328 char *pattern, *replace, *ppercent, *rpercent;
329 char *value = (v->recursive
330 ? recursively_expand (v)
331 : v->value);
333 /* Copy the pattern and the replacement. Add in an
334 extra % at the beginning to use in case there
335 isn't one in the pattern. */
336 pattern = (char *) alloca (subst_end - subst_beg + 2);
337 *(pattern++) = '%';
338 bcopy (subst_beg, pattern, subst_end - subst_beg);
339 pattern[subst_end - subst_beg] = '\0';
341 replace = (char *) alloca (replace_end
342 - replace_beg + 2);
343 *(replace++) = '%';
344 bcopy (replace_beg, replace,
345 replace_end - replace_beg);
346 replace[replace_end - replace_beg] = '\0';
348 /* Look for %. Set the percent pointers properly
349 based on whether we find one or not. */
350 ppercent = find_percent (pattern);
351 if (ppercent)
353 ++ppercent;
354 rpercent = 0;
356 else
358 ppercent = pattern;
359 rpercent = replace;
360 --pattern;
361 --replace;
364 o = patsubst_expand (o, value, pattern, replace,
365 ppercent, rpercent);
367 if (v->recursive)
368 free (value);
373 if (colon == 0)
374 /* This is an ordinary variable reference.
375 Look up the value of the variable. */
376 o = reference_variable (o, beg, end - beg);
378 if (free_beg)
379 free (beg);
381 break;
383 case '\0':
384 break;
386 default:
387 if (isblank ((unsigned char)p[-1]))
388 break;
390 /* A $ followed by a random char is a variable reference:
391 $a is equivalent to $(a). */
392 o = reference_variable (o, p, 1);
394 break;
397 if (*p == '\0')
398 break;
399 else
400 ++p;
403 if (save_char)
404 string[length] = save_char;
406 (void)variable_buffer_output (o, "", 1);
407 return (variable_buffer + line_offset);
410 /* Scan LINE for variable references and expansion-function calls.
411 Build in `variable_buffer' the result of expanding the references and calls.
412 Return the address of the resulting string, which is null-terminated
413 and is valid only until the next time this function is called. */
415 char *
416 variable_expand (char *line)
418 return variable_expand_string(NULL, line, (long)-1);
421 /* Expand an argument for an expansion function.
422 The text starting at STR and ending at END is variable-expanded
423 into a null-terminated string that is returned as the value.
424 This is done without clobbering `variable_buffer' or the current
425 variable-expansion that is in progress. */
427 char *
428 expand_argument (const char *str, const char *end)
430 char *tmp;
432 if (str == end)
433 return xstrdup("");
435 if (!end || *end == '\0')
436 return allocated_variable_expand ((char *)str);
438 tmp = (char *) alloca (end - str + 1);
439 bcopy (str, tmp, end - str);
440 tmp[end - str] = '\0';
442 return allocated_variable_expand (tmp);
445 /* Expand LINE for FILE. Error messages refer to the file and line where
446 FILE's commands were found. Expansion uses FILE's variable set list. */
448 char *
449 variable_expand_for_file (char *line, struct file *file)
451 char *result;
452 struct variable_set_list *save;
454 if (file == 0)
455 return variable_expand (line);
457 save = current_variable_set_list;
458 current_variable_set_list = file->variables;
459 if (file->cmds && file->cmds->fileinfo.filenm)
460 reading_file = &file->cmds->fileinfo;
461 else
462 reading_file = 0;
463 result = variable_expand (line);
464 current_variable_set_list = save;
465 reading_file = 0;
467 return result;
470 /* Like allocated_variable_expand, but for += target-specific variables.
471 First recursively construct the variable value from its appended parts in
472 any upper variable sets. Then expand the resulting value. */
474 static char *
475 variable_append (const char *name, unsigned int length,
476 const struct variable_set_list *set)
478 const struct variable *v;
479 char *buf = 0;
481 /* If there's nothing left to check, return the empty buffer. */
482 if (!set)
483 return initialize_variable_output ();
485 /* Try to find the variable in this variable set. */
486 v = lookup_variable_in_set (name, length, set->set);
488 /* If there isn't one, look to see if there's one in a set above us. */
489 if (!v)
490 return variable_append (name, length, set->next);
492 /* If this variable type is append, first get any upper values.
493 If not, initialize the buffer. */
494 if (v->append)
495 buf = variable_append (name, length, set->next);
496 else
497 buf = initialize_variable_output ();
499 /* Append this value to the buffer, and return it.
500 If we already have a value, first add a space. */
501 if (buf > variable_buffer)
502 buf = variable_buffer_output (buf, " ", 1);
504 /* Either expand it or copy it, depending. */
505 if (! v->recursive)
506 return variable_buffer_output (buf, v->value, strlen (v->value));
508 buf = variable_expand_string (buf, v->value, strlen (v->value));
509 return (buf + strlen (buf));
513 static char *
514 allocated_variable_append (const struct variable *v)
516 char *val;
518 /* Construct the appended variable value. */
520 char *obuf = variable_buffer;
521 unsigned int olen = variable_buffer_length;
523 variable_buffer = 0;
525 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
526 variable_buffer_output (val, "", 1);
527 val = variable_buffer;
529 variable_buffer = obuf;
530 variable_buffer_length = olen;
532 return val;
535 /* Like variable_expand_for_file, but the returned string is malloc'd.
536 This function is called a lot. It wants to be efficient. */
538 char *
539 allocated_variable_expand_for_file (char *line, struct file *file)
541 char *value;
543 char *obuf = variable_buffer;
544 unsigned int olen = variable_buffer_length;
546 variable_buffer = 0;
548 value = variable_expand_for_file (line, file);
550 #if 0
551 /* Waste a little memory and save time. */
552 value = xrealloc (value, strlen (value))
553 #endif
555 variable_buffer = obuf;
556 variable_buffer_length = olen;
558 return value;
561 /* Install a new variable_buffer context, returning the current one for
562 safe-keeping. */
564 void
565 install_variable_buffer (char **bufp, unsigned int *lenp)
567 *bufp = variable_buffer;
568 *lenp = variable_buffer_length;
570 variable_buffer = 0;
571 initialize_variable_output ();
574 /* Restore a previously-saved variable_buffer setting (free the current one).
577 void
578 restore_variable_buffer (char *buf, unsigned int len)
580 free (variable_buffer);
582 variable_buffer = buf;
583 variable_buffer_length = len;