* Change $(call...) to not expand arguments by default.
[make.git] / expand.c
bloba7cfcdbf7fed9836574f8eee8601103422c2bd57
1 /* Variable expansion functions for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 95 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <assert.h>
22 #include "make.h"
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
27 #include "rule.h"
29 /* The next two describe the variable output buffer.
30 This buffer is used to hold the variable-expansion of a line of the
31 makefile. It is made bigger with realloc whenever it is too small.
32 variable_buffer_length is the size currently allocated.
33 variable_buffer is the address of the buffer.
35 For efficiency, it's guaranteed that the buffer will always have
36 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
37 extra chars without having to call a function. Note you should never use
38 these bytes unless you're _sure_ you have room (you know when the buffer
39 length was last checked. */
41 #define VARIABLE_BUFFER_ZONE 5
43 static unsigned int variable_buffer_length;
44 char *variable_buffer;
46 /* Subroutine of variable_expand and friends:
47 The text to add is LENGTH chars starting at STRING to the variable_buffer.
48 The text is added to the buffer at PTR, and the updated pointer into
49 the buffer is returned as the value. Thus, the value returned by
50 each call to variable_buffer_output should be the first argument to
51 the following call. */
53 char *
54 variable_buffer_output (ptr, string, length)
55 char *ptr, *string;
56 unsigned int length;
58 register unsigned int newlen = length + (ptr - variable_buffer);
60 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
62 unsigned int offset = ptr - variable_buffer;
63 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
64 ? newlen + 100
65 : 2 * variable_buffer_length);
66 variable_buffer = (char *) xrealloc (variable_buffer,
67 variable_buffer_length);
68 ptr = variable_buffer + offset;
71 bcopy (string, ptr, length);
72 return ptr + length;
75 /* Return a pointer to the beginning of the variable buffer. */
77 static char *
78 initialize_variable_output ()
80 /* If we don't have a variable output buffer yet, get one. */
82 if (variable_buffer == 0)
84 variable_buffer_length = 200;
85 variable_buffer = (char *) xmalloc (variable_buffer_length);
86 variable_buffer[0] = '\0';
89 return variable_buffer;
92 /* Recursively expand V. The returned string is malloc'd. */
94 static char *allocated_variable_append PARAMS ((struct variable *v));
96 char *
97 recursively_expand (v)
98 register struct variable *v;
100 char *value;
102 if (v->expanding)
103 /* Expanding V causes infinite recursion. Lose. */
104 fatal (reading_file,
105 _("Recursive variable `%s' references itself (eventually)"),
106 v->name);
108 v->expanding = 1;
109 if (v->append)
110 value = allocated_variable_append (v);
111 else
112 value = allocated_variable_expand (v->value);
113 v->expanding = 0;
115 return value;
118 /* Warn that NAME is an undefined variable. */
120 #ifdef __GNUC__
121 __inline
122 #endif
123 static void
124 warn_undefined (name, length)
125 char *name;
126 unsigned int length;
128 if (warn_undefined_variables_flag)
129 error (reading_file,
130 _("warning: undefined variable `%.*s'"), (int)length, name);
133 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
135 #ifdef __GNUC__
136 __inline
137 #endif
138 static char *
139 reference_variable (o, name, length)
140 char *o;
141 char *name;
142 unsigned int length;
144 register struct variable *v = lookup_variable (name, length);
145 char *value;
147 if (v == 0)
148 warn_undefined (name, length);
150 if (v == 0 || *v->value == '\0')
151 return o;
153 value = (v->recursive ? recursively_expand (v) : v->value);
155 o = variable_buffer_output (o, value, strlen (value));
157 if (v->recursive)
158 free (value);
160 return o;
163 /* Scan STRING for variable references and expansion-function calls. Only
164 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
165 a null byte is found.
167 Write the results to LINE, which must point into `variable_buffer'. If
168 LINE is NULL, start at the beginning of the buffer.
169 Return a pointer to LINE, or to the beginning of the buffer if LINE is
170 NULL. */
172 char *
173 variable_expand_string (line, string, length)
174 register char *line;
175 char *string;
176 long length;
178 register struct variable *v;
179 register char *p, *o, *p1;
180 char save_char = '\0';
181 unsigned int line_offset;
183 if (!line)
184 line = initialize_variable_output();
186 p = string;
187 o = line;
188 line_offset = line - variable_buffer;
190 if (length >= 0)
192 save_char = string[length];
193 string[length] = '\0';
196 while (1)
198 /* Copy all following uninteresting chars all at once to the
199 variable output buffer, and skip them. Uninteresting chars end
200 at the next $ or the end of the input. */
202 p1 = strchr (p, '$');
204 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
206 if (p1 == 0)
207 break;
208 p = p1 + 1;
210 /* Dispatch on the char that follows the $. */
212 switch (*p)
214 case '$':
215 /* $$ seen means output one $ to the variable output buffer. */
216 o = variable_buffer_output (o, p, 1);
217 break;
219 case '(':
220 case '{':
221 /* $(...) or ${...} is the general case of substitution. */
223 char openparen = *p;
224 char closeparen = (openparen == '(') ? ')' : '}';
225 register char *beg = p + 1;
226 int free_beg = 0;
227 char *op, *begp;
228 char *end, *colon;
230 op = o;
231 begp = p;
232 if (handle_function (&op, &begp))
234 o = op;
235 p = begp;
236 break;
239 /* Is there a variable reference inside the parens or braces?
240 If so, expand it before expanding the entire reference. */
242 end = strchr (beg, closeparen);
243 if (end == 0)
244 /* Unterminated variable reference. */
245 fatal (reading_file, _("unterminated variable reference"));
246 p1 = lindex (beg, end, '$');
247 if (p1 != 0)
249 /* BEG now points past the opening paren or brace.
250 Count parens or braces until it is matched. */
251 int count = 0;
252 for (p = beg; *p != '\0'; ++p)
254 if (*p == openparen)
255 ++count;
256 else if (*p == closeparen && --count < 0)
257 break;
259 /* If COUNT is >= 0, there were unmatched opening parens
260 or braces, so we go to the simple case of a variable name
261 such as `$($(a)'. */
262 if (count < 0)
264 beg = expand_argument (beg, p); /* Expand the name. */
265 free_beg = 1; /* Remember to free BEG when finished. */
266 end = strchr (beg, '\0');
269 else
270 /* Advance P to the end of this reference. After we are
271 finished expanding this one, P will be incremented to
272 continue the scan. */
273 p = end;
275 /* This is not a reference to a built-in function and
276 any variable references inside are now expanded.
277 Is the resultant text a substitution reference? */
279 colon = lindex (beg, end, ':');
280 if (colon != 0)
282 /* This looks like a substitution reference: $(FOO:A=B). */
283 char *subst_beg, *subst_end, *replace_beg, *replace_end;
285 subst_beg = colon + 1;
286 subst_end = strchr (subst_beg, '=');
287 if (subst_end == 0)
288 /* There is no = in sight. Punt on the substitution
289 reference and treat this as a variable name containing
290 a colon, in the code below. */
291 colon = 0;
292 else
294 replace_beg = subst_end + 1;
295 replace_end = end;
297 /* Extract the variable name before the colon
298 and look up that variable. */
299 v = lookup_variable (beg, colon - beg);
300 if (v == 0)
301 warn_undefined (beg, colon - beg);
303 if (v != 0 && *v->value != '\0')
305 char *value = (v->recursive ? recursively_expand (v)
306 : v->value);
307 char *pattern, *percent;
308 if (free_beg)
310 *subst_end = '\0';
311 pattern = subst_beg;
313 else
315 pattern = (char *) alloca (subst_end - subst_beg
316 + 1);
317 bcopy (subst_beg, pattern, subst_end - subst_beg);
318 pattern[subst_end - subst_beg] = '\0';
320 percent = find_percent (pattern);
321 if (percent != 0)
323 char *replace;
324 if (free_beg)
326 *replace_end = '\0';
327 replace = replace_beg;
329 else
331 replace = (char *) alloca (replace_end
332 - replace_beg
333 + 1);
334 bcopy (replace_beg, replace,
335 replace_end - replace_beg);
336 replace[replace_end - replace_beg] = '\0';
339 o = patsubst_expand (o, value, pattern, replace,
340 percent, (char *) 0);
342 else
343 o = subst_expand (o, value,
344 pattern, replace_beg,
345 strlen (pattern),
346 end - replace_beg,
347 0, 1);
348 if (v->recursive)
349 free (value);
354 if (colon == 0)
355 /* This is an ordinary variable reference.
356 Look up the value of the variable. */
357 o = reference_variable (o, beg, end - beg);
359 if (free_beg)
360 free (beg);
362 break;
364 case '\0':
365 break;
367 default:
368 if (isblank (p[-1]))
369 break;
371 /* A $ followed by a random char is a variable reference:
372 $a is equivalent to $(a). */
374 /* We could do the expanding here, but this way
375 avoids code repetition at a small performance cost. */
376 char name[5];
377 name[0] = '$';
378 name[1] = '(';
379 name[2] = *p;
380 name[3] = ')';
381 name[4] = '\0';
382 p1 = allocated_variable_expand (name);
383 o = variable_buffer_output (o, p1, strlen (p1));
384 free (p1);
387 break;
390 if (*p == '\0')
391 break;
392 else
393 ++p;
396 if (save_char)
397 string[length] = save_char;
399 (void)variable_buffer_output (o, "", 1);
400 return (variable_buffer + line_offset);
403 /* Scan LINE for variable references and expansion-function calls.
404 Build in `variable_buffer' the result of expanding the references and calls.
405 Return the address of the resulting string, which is null-terminated
406 and is valid only until the next time this function is called. */
408 char *
409 variable_expand (line)
410 char *line;
412 return variable_expand_string(NULL, line, (long)-1);
415 /* Expand an argument for an expansion function.
416 The text starting at STR and ending at END is variable-expanded
417 into a null-terminated string that is returned as the value.
418 This is done without clobbering `variable_buffer' or the current
419 variable-expansion that is in progress. */
421 char *
422 expand_argument (str, end)
423 char *str, *end;
425 char *tmp;
427 if (!end || *end == '\0')
428 tmp = str;
429 else
431 tmp = (char *) alloca (end - str + 1);
432 bcopy (str, tmp, end - str);
433 tmp[end - str] = '\0';
436 return allocated_variable_expand (tmp);
439 /* Expand LINE for FILE. Error messages refer to the file and line where
440 FILE's commands were found. Expansion uses FILE's variable set list. */
442 static char *
443 variable_expand_for_file (line, file)
444 char *line;
445 register struct file *file;
447 char *result;
448 struct variable_set_list *save, *fnext;
450 if (file == 0)
451 return variable_expand (line);
453 save = current_variable_set_list;
454 current_variable_set_list = file->variables;
455 if (file->cmds && file->cmds->fileinfo.filenm)
456 reading_file = &file->cmds->fileinfo;
457 else
458 reading_file = 0;
459 fnext = file->variables->next;
460 /* See if there's a pattern-specific variable struct for this target. */
461 if (!file->pat_searched)
463 file->patvar = lookup_pattern_var(file->name);
464 file->pat_searched = 1;
466 if (file->patvar != 0)
468 file->patvar->vars->next = fnext;
469 file->variables->next = file->patvar->vars;
471 result = variable_expand (line);
472 current_variable_set_list = save;
473 reading_file = 0;
474 file->variables->next = fnext;
476 return result;
479 /* Like allocated_variable_expand, but we first expand this variable in the
480 context of the next variable set, then we append the expanded value. */
482 static char *
483 allocated_variable_append (v)
484 struct variable *v;
486 struct variable_set_list *save;
487 int len = strlen (v->name);
488 char *var = alloca (len + 4);
489 char *value;
491 char *obuf = variable_buffer;
492 unsigned int olen = variable_buffer_length;
494 variable_buffer = 0;
496 assert(current_variable_set_list->next != 0);
497 save = current_variable_set_list;
498 current_variable_set_list = current_variable_set_list->next;
500 var[0] = '$';
501 var[1] = '(';
502 strcpy (&var[2], v->name);
503 var[len+2] = ')';
504 var[len+3] = '\0';
506 value = variable_expand_for_file (var, 0);
508 current_variable_set_list = save;
510 value += strlen (value);
511 value = variable_buffer_output (value, " ", 1);
512 value = variable_expand_string (value, v->value, (long)-1);
514 value = variable_buffer;
516 #if 0
517 /* Waste a little memory and save time. */
518 value = xrealloc (value, strlen (value))
519 #endif
521 variable_buffer = obuf;
522 variable_buffer_length = olen;
524 return value;
527 /* Like variable_expand_for_file, but the returned string is malloc'd.
528 This function is called a lot. It wants to be efficient. */
530 char *
531 allocated_variable_expand_for_file (line, file)
532 char *line;
533 struct file *file;
535 char *value;
537 char *obuf = variable_buffer;
538 unsigned int olen = variable_buffer_length;
540 variable_buffer = 0;
542 value = variable_expand_for_file (line, file);
544 #if 0
545 /* Waste a little memory and save time. */
546 value = xrealloc (value, strlen (value))
547 #endif
549 variable_buffer = obuf;
550 variable_buffer_length = olen;
552 return value;