Save strings we're expanding in case an embedded eval causes them
[make.git] / expand.c
blobf5b6b996b3965fa23988bd64bd77085762282610
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, 2008, 2009,
4 2010 Free Software 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 3 of the License, or (at your option) any later
10 version.
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/>. */
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 *save;
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 /* We need a copy of STRING: due to eval, it's possible that it will get
216 freed as we process it (it might be the value of a variable that's reset
217 for example). Also having a nil-terminated string is handy. */
218 save = length < 0 ? xstrdup (string) : xstrndup (string, length);
219 p = save;
221 while (1)
223 /* Copy all following uninteresting chars all at once to the
224 variable output buffer, and skip them. Uninteresting chars end
225 at the next $ or the end of the input. */
227 p1 = strchr (p, '$');
229 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
231 if (p1 == 0)
232 break;
233 p = p1 + 1;
235 /* Dispatch on the char that follows the $. */
237 switch (*p)
239 case '$':
240 /* $$ seen means output one $ to the variable output buffer. */
241 o = variable_buffer_output (o, p, 1);
242 break;
244 case '(':
245 case '{':
246 /* $(...) or ${...} is the general case of substitution. */
248 char openparen = *p;
249 char closeparen = (openparen == '(') ? ')' : '}';
250 const char *begp;
251 const char *beg = p + 1;
252 char *op;
253 char *abeg = NULL;
254 const char *end, *colon;
256 op = o;
257 begp = p;
258 if (handle_function (&op, &begp))
260 o = op;
261 p = begp;
262 break;
265 /* Is there a variable reference inside the parens or braces?
266 If so, expand it before expanding the entire reference. */
268 end = strchr (beg, closeparen);
269 if (end == 0)
270 /* Unterminated variable reference. */
271 fatal (*expanding_var, _("unterminated variable reference"));
272 p1 = lindex (beg, end, '$');
273 if (p1 != 0)
275 /* BEG now points past the opening paren or brace.
276 Count parens or braces until it is matched. */
277 int count = 0;
278 for (p = beg; *p != '\0'; ++p)
280 if (*p == openparen)
281 ++count;
282 else if (*p == closeparen && --count < 0)
283 break;
285 /* If COUNT is >= 0, there were unmatched opening parens
286 or braces, so we go to the simple case of a variable name
287 such as `$($(a)'. */
288 if (count < 0)
290 abeg = expand_argument (beg, p); /* Expand the name. */
291 beg = abeg;
292 end = strchr (beg, '\0');
295 else
296 /* Advance P to the end of this reference. After we are
297 finished expanding this one, P will be incremented to
298 continue the scan. */
299 p = end;
301 /* This is not a reference to a built-in function and
302 any variable references inside are now expanded.
303 Is the resultant text a substitution reference? */
305 colon = lindex (beg, end, ':');
306 if (colon)
308 /* This looks like a substitution reference: $(FOO:A=B). */
309 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
311 subst_beg = colon + 1;
312 subst_end = lindex (subst_beg, end, '=');
313 if (subst_end == 0)
314 /* There is no = in sight. Punt on the substitution
315 reference and treat this as a variable name containing
316 a colon, in the code below. */
317 colon = 0;
318 else
320 replace_beg = subst_end + 1;
321 replace_end = end;
323 /* Extract the variable name before the colon
324 and look up that variable. */
325 v = lookup_variable (beg, colon - beg);
326 if (v == 0)
327 warn_undefined (beg, colon - beg);
329 /* If the variable is not empty, perform the
330 substitution. */
331 if (v != 0 && *v->value != '\0')
333 char *pattern, *replace, *ppercent, *rpercent;
334 char *value = (v->recursive
335 ? recursively_expand (v)
336 : v->value);
338 /* Copy the pattern and the replacement. Add in an
339 extra % at the beginning to use in case there
340 isn't one in the pattern. */
341 pattern = alloca (subst_end - subst_beg + 2);
342 *(pattern++) = '%';
343 memcpy (pattern, subst_beg, subst_end - subst_beg);
344 pattern[subst_end - subst_beg] = '\0';
346 replace = alloca (replace_end - replace_beg + 2);
347 *(replace++) = '%';
348 memcpy (replace, replace_beg,
349 replace_end - replace_beg);
350 replace[replace_end - replace_beg] = '\0';
352 /* Look for %. Set the percent pointers properly
353 based on whether we find one or not. */
354 ppercent = find_percent (pattern);
355 if (ppercent)
357 ++ppercent;
358 rpercent = find_percent (replace);
359 if (rpercent)
360 ++rpercent;
362 else
364 ppercent = pattern;
365 rpercent = replace;
366 --pattern;
367 --replace;
370 o = patsubst_expand_pat (o, value, pattern, replace,
371 ppercent, rpercent);
373 if (v->recursive)
374 free (value);
379 if (colon == 0)
380 /* This is an ordinary variable reference.
381 Look up the value of the variable. */
382 o = reference_variable (o, beg, end - beg);
384 if (abeg)
385 free (abeg);
387 break;
389 case '\0':
390 break;
392 default:
393 if (isblank ((unsigned char)p[-1]))
394 break;
396 /* A $ followed by a random char is a variable reference:
397 $a is equivalent to $(a). */
398 o = reference_variable (o, p, 1);
400 break;
403 if (*p == '\0')
404 break;
406 ++p;
409 free (save);
411 variable_buffer_output (o, "", 1);
412 return (variable_buffer + line_offset);
415 /* Scan LINE for variable references and expansion-function calls.
416 Build in `variable_buffer' the result of expanding the references and calls.
417 Return the address of the resulting string, which is null-terminated
418 and is valid only until the next time this function is called. */
420 char *
421 variable_expand (const char *line)
423 return variable_expand_string(NULL, line, (long)-1);
426 /* Expand an argument for an expansion function.
427 The text starting at STR and ending at END is variable-expanded
428 into a null-terminated string that is returned as the value.
429 This is done without clobbering `variable_buffer' or the current
430 variable-expansion that is in progress. */
432 char *
433 expand_argument (const char *str, const char *end)
435 char *tmp, *alloc = NULL;
436 char *r;
438 if (str == end)
439 return xstrdup("");
441 if (!end || *end == '\0')
442 return allocated_variable_expand (str);
444 if (end - str + 1 > 1000)
445 tmp = alloc = xmalloc (end - str + 1);
446 else
447 tmp = alloca (end - str + 1);
449 memcpy (tmp, str, end - str);
450 tmp[end - str] = '\0';
452 r = allocated_variable_expand (tmp);
454 if (alloc)
455 free (alloc);
457 return r;
460 /* Expand LINE for FILE. Error messages refer to the file and line where
461 FILE's commands were found. Expansion uses FILE's variable set list. */
463 char *
464 variable_expand_for_file (const char *line, struct file *file)
466 char *result;
467 struct variable_set_list *savev;
468 const struct floc *savef;
470 if (file == 0)
471 return variable_expand (line);
473 savev = current_variable_set_list;
474 current_variable_set_list = file->variables;
476 savef = reading_file;
477 if (file->cmds && file->cmds->fileinfo.filenm)
478 reading_file = &file->cmds->fileinfo;
479 else
480 reading_file = 0;
482 result = variable_expand (line);
484 current_variable_set_list = savev;
485 reading_file = savef;
487 return result;
490 /* Like allocated_variable_expand, but for += target-specific variables.
491 First recursively construct the variable value from its appended parts in
492 any upper variable sets. Then expand the resulting value. */
494 static char *
495 variable_append (const char *name, unsigned int length,
496 const struct variable_set_list *set, int local)
498 const struct variable *v;
499 char *buf = 0;
501 /* If there's nothing left to check, return the empty buffer. */
502 if (!set)
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, 0);
512 /* If this variable type is append, first get any upper values.
513 If not, initialize the buffer. */
514 if (v->append)
515 buf = variable_append (name, length, set->next, 0);
516 else
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. */
525 if (! v->recursive)
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));
533 static char *
534 allocated_variable_append (const struct variable *v)
536 char *val;
538 /* Construct the appended variable value. */
540 char *obuf = variable_buffer;
541 unsigned int olen = variable_buffer_length;
543 variable_buffer = 0;
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;
553 return val;
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. */
559 char *
560 allocated_variable_expand_for_file (const char *line, struct file *file)
562 char *value;
564 char *obuf = variable_buffer;
565 unsigned int olen = variable_buffer_length;
567 variable_buffer = 0;
569 value = variable_expand_for_file (line, file);
571 variable_buffer = obuf;
572 variable_buffer_length = olen;
574 return value;
577 /* Install a new variable_buffer context, returning the current one for
578 safe-keeping. */
580 void
581 install_variable_buffer (char **bufp, unsigned int *lenp)
583 *bufp = variable_buffer;
584 *lenp = variable_buffer_length;
586 variable_buffer = 0;
587 initialize_variable_output ();
590 /* Restore a previously-saved variable_buffer setting (free the current one).
593 void
594 restore_variable_buffer (char *buf, unsigned int len)
596 free (variable_buffer);
598 variable_buffer = buf;
599 variable_buffer_length = len;