Another round of cleanups:
[make.git] / expand.c
blob7ca0b921708acfcda4b42f3b0bbe71bec75fce86
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 = 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, char *name, unsigned int length)
164 register 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, char *string, long length)
198 struct variable *v;
199 char *p, *o, *p1;
200 char save_char = '\0';
201 unsigned int line_offset;
203 if (!line)
204 line = initialize_variable_output();
206 p = string;
207 o = line;
208 line_offset = line - variable_buffer;
210 if (length >= 0)
212 save_char = string[length];
213 string[length] = '\0';
216 while (1)
218 /* Copy all following uninteresting chars all at once to the
219 variable output buffer, and skip them. Uninteresting chars end
220 at the next $ or the end of the input. */
222 p1 = strchr (p, '$');
224 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
226 if (p1 == 0)
227 break;
228 p = p1 + 1;
230 /* Dispatch on the char that follows the $. */
232 switch (*p)
234 case '$':
235 /* $$ seen means output one $ to the variable output buffer. */
236 o = variable_buffer_output (o, p, 1);
237 break;
239 case '(':
240 case '{':
241 /* $(...) or ${...} is the general case of substitution. */
243 char openparen = *p;
244 char closeparen = (openparen == '(') ? ')' : '}';
245 register char *beg = p + 1;
246 int free_beg = 0;
247 char *op, *begp;
248 char *end, *colon;
250 op = o;
251 begp = p;
252 if (handle_function (&op, &begp))
254 o = op;
255 p = begp;
256 break;
259 /* Is there a variable reference inside the parens or braces?
260 If so, expand it before expanding the entire reference. */
262 end = strchr (beg, closeparen);
263 if (end == 0)
264 /* Unterminated variable reference. */
265 fatal (*expanding_var, _("unterminated variable reference"));
266 p1 = lindex (beg, end, '$');
267 if (p1 != 0)
269 /* BEG now points past the opening paren or brace.
270 Count parens or braces until it is matched. */
271 int count = 0;
272 for (p = beg; *p != '\0'; ++p)
274 if (*p == openparen)
275 ++count;
276 else if (*p == closeparen && --count < 0)
277 break;
279 /* If COUNT is >= 0, there were unmatched opening parens
280 or braces, so we go to the simple case of a variable name
281 such as `$($(a)'. */
282 if (count < 0)
284 beg = expand_argument (beg, p); /* Expand the name. */
285 free_beg = 1; /* Remember to free BEG when finished. */
286 end = strchr (beg, '\0');
289 else
290 /* Advance P to the end of this reference. After we are
291 finished expanding this one, P will be incremented to
292 continue the scan. */
293 p = end;
295 /* This is not a reference to a built-in function and
296 any variable references inside are now expanded.
297 Is the resultant text a substitution reference? */
299 colon = lindex (beg, end, ':');
300 if (colon)
302 /* This looks like a substitution reference: $(FOO:A=B). */
303 char *subst_beg, *subst_end, *replace_beg, *replace_end;
305 subst_beg = colon + 1;
306 subst_end = lindex (subst_beg, end, '=');
307 if (subst_end == 0)
308 /* There is no = in sight. Punt on the substitution
309 reference and treat this as a variable name containing
310 a colon, in the code below. */
311 colon = 0;
312 else
314 replace_beg = subst_end + 1;
315 replace_end = end;
317 /* Extract the variable name before the colon
318 and look up that variable. */
319 v = lookup_variable (beg, colon - beg);
320 if (v == 0)
321 warn_undefined (beg, colon - beg);
323 /* If the variable is not empty, perform the
324 substitution. */
325 if (v != 0 && *v->value != '\0')
327 char *pattern, *replace, *ppercent, *rpercent;
328 char *value = (v->recursive
329 ? recursively_expand (v)
330 : v->value);
332 /* Copy the pattern and the replacement. Add in an
333 extra % at the beginning to use in case there
334 isn't one in the pattern. */
335 pattern = alloca (subst_end - subst_beg + 2);
336 *(pattern++) = '%';
337 memcpy (pattern, subst_beg, subst_end - subst_beg);
338 pattern[subst_end - subst_beg] = '\0';
340 replace = alloca (replace_end - replace_beg + 2);
341 *(replace++) = '%';
342 memcpy (replace, replace_beg,
343 replace_end - replace_beg);
344 replace[replace_end - replace_beg] = '\0';
346 /* Look for %. Set the percent pointers properly
347 based on whether we find one or not. */
348 ppercent = find_percent (pattern);
349 if (ppercent)
351 ++ppercent;
352 rpercent = 0;
354 else
356 ppercent = pattern;
357 rpercent = replace;
358 --pattern;
359 --replace;
362 o = patsubst_expand (o, value, pattern, replace,
363 ppercent, rpercent);
365 if (v->recursive)
366 free (value);
371 if (colon == 0)
372 /* This is an ordinary variable reference.
373 Look up the value of the variable. */
374 o = reference_variable (o, beg, end - beg);
376 if (free_beg)
377 free (beg);
379 break;
381 case '\0':
382 break;
384 default:
385 if (isblank ((unsigned char)p[-1]))
386 break;
388 /* A $ followed by a random char is a variable reference:
389 $a is equivalent to $(a). */
390 o = reference_variable (o, p, 1);
392 break;
395 if (*p == '\0')
396 break;
397 else
398 ++p;
401 if (save_char)
402 string[length] = save_char;
404 (void)variable_buffer_output (o, "", 1);
405 return (variable_buffer + line_offset);
408 /* Scan LINE for variable references and expansion-function calls.
409 Build in `variable_buffer' the result of expanding the references and calls.
410 Return the address of the resulting string, which is null-terminated
411 and is valid only until the next time this function is called. */
413 char *
414 variable_expand (char *line)
416 return variable_expand_string(NULL, line, (long)-1);
419 /* Expand an argument for an expansion function.
420 The text starting at STR and ending at END is variable-expanded
421 into a null-terminated string that is returned as the value.
422 This is done without clobbering `variable_buffer' or the current
423 variable-expansion that is in progress. */
425 char *
426 expand_argument (const char *str, const char *end)
428 char *tmp;
430 if (str == end)
431 return xstrdup("");
433 if (!end || *end == '\0')
434 return allocated_variable_expand (str);
436 tmp = alloca (end - str + 1);
437 memcpy (tmp, str, end - str);
438 tmp[end - str] = '\0';
440 return allocated_variable_expand (tmp);
443 /* Expand LINE for FILE. Error messages refer to the file and line where
444 FILE's commands were found. Expansion uses FILE's variable set list. */
446 char *
447 variable_expand_for_file (char *line, struct file *file)
449 char *result;
450 struct variable_set_list *save;
452 if (file == 0)
453 return variable_expand (line);
455 save = current_variable_set_list;
456 current_variable_set_list = file->variables;
457 if (file->cmds && file->cmds->fileinfo.filenm)
458 reading_file = &file->cmds->fileinfo;
459 else
460 reading_file = 0;
461 result = variable_expand (line);
462 current_variable_set_list = save;
463 reading_file = 0;
465 return result;
468 /* Like allocated_variable_expand, but for += target-specific variables.
469 First recursively construct the variable value from its appended parts in
470 any upper variable sets. Then expand the resulting value. */
472 static char *
473 variable_append (const char *name, unsigned int length,
474 const struct variable_set_list *set)
476 const struct variable *v;
477 char *buf = 0;
479 /* If there's nothing left to check, return the empty buffer. */
480 if (!set)
481 return initialize_variable_output ();
483 /* Try to find the variable in this variable set. */
484 v = lookup_variable_in_set (name, length, set->set);
486 /* If there isn't one, look to see if there's one in a set above us. */
487 if (!v)
488 return variable_append (name, length, set->next);
490 /* If this variable type is append, first get any upper values.
491 If not, initialize the buffer. */
492 if (v->append)
493 buf = variable_append (name, length, set->next);
494 else
495 buf = initialize_variable_output ();
497 /* Append this value to the buffer, and return it.
498 If we already have a value, first add a space. */
499 if (buf > variable_buffer)
500 buf = variable_buffer_output (buf, " ", 1);
502 /* Either expand it or copy it, depending. */
503 if (! v->recursive)
504 return variable_buffer_output (buf, v->value, strlen (v->value));
506 buf = variable_expand_string (buf, v->value, strlen (v->value));
507 return (buf + strlen (buf));
511 static char *
512 allocated_variable_append (const struct variable *v)
514 char *val;
516 /* Construct the appended variable value. */
518 char *obuf = variable_buffer;
519 unsigned int olen = variable_buffer_length;
521 variable_buffer = 0;
523 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
524 variable_buffer_output (val, "", 1);
525 val = variable_buffer;
527 variable_buffer = obuf;
528 variable_buffer_length = olen;
530 return val;
533 /* Like variable_expand_for_file, but the returned string is malloc'd.
534 This function is called a lot. It wants to be efficient. */
536 char *
537 allocated_variable_expand_for_file (char *line, struct file *file)
539 char *value;
541 char *obuf = variable_buffer;
542 unsigned int olen = variable_buffer_length;
544 variable_buffer = 0;
546 value = variable_expand_for_file (line, file);
548 #if 0
549 /* Waste a little memory and save time. */
550 value = xrealloc (value, strlen (value))
551 #endif
553 variable_buffer = obuf;
554 variable_buffer_length = olen;
556 return value;
559 /* Install a new variable_buffer context, returning the current one for
560 safe-keeping. */
562 void
563 install_variable_buffer (char **bufp, unsigned int *lenp)
565 *bufp = variable_buffer;
566 *lenp = variable_buffer_length;
568 variable_buffer = 0;
569 initialize_variable_output ();
572 /* Restore a previously-saved variable_buffer setting (free the current one).
575 void
576 restore_variable_buffer (char *buf, unsigned int len)
578 free (variable_buffer);
580 variable_buffer = buf;
581 variable_buffer_length = len;