* Fix for dir.c from Andreas Schwab.
[make.git] / expand.c
blob070b8933269a18c24676a7d1569be7813497806f
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,
95 struct variable_set_list *l));
97 char *
98 recursively_expand_setlist (v, list)
99 register struct variable *v;
100 struct variable_set_list *list;
102 char *value;
104 if (v->expanding)
105 /* Expanding V causes infinite recursion. Lose. */
106 fatal (reading_file,
107 _("Recursive variable `%s' references itself (eventually)"),
108 v->name);
110 v->expanding = 1;
111 if (v->append)
112 value = allocated_variable_append (v, list);
113 else
114 value = allocated_variable_expand (v->value);
115 v->expanding = 0;
117 return value;
120 /* Warn that NAME is an undefined variable. */
122 #ifdef __GNUC__
123 __inline
124 #endif
125 static void
126 warn_undefined (name, length)
127 char *name;
128 unsigned int length;
130 if (warn_undefined_variables_flag)
131 error (reading_file,
132 _("warning: undefined variable `%.*s'"), (int)length, name);
135 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
137 #ifdef __GNUC__
138 __inline
139 #endif
140 static char *
141 reference_variable (o, name, length)
142 char *o;
143 char *name;
144 unsigned int length;
146 register struct variable *v;
147 struct variable_set_list *setlist;
148 char *value;
150 v = lookup_variable_setlist (name, length, &setlist);
152 if (v == 0)
153 warn_undefined (name, length);
155 if (v == 0 || *v->value == '\0')
156 return o;
158 value = (v->recursive ? recursively_expand_setlist (v, setlist) : v->value);
160 o = variable_buffer_output (o, value, strlen (value));
162 if (v->recursive)
163 free (value);
165 return o;
168 /* Scan STRING for variable references and expansion-function calls. Only
169 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
170 a null byte is found.
172 Write the results to LINE, which must point into `variable_buffer'. If
173 LINE is NULL, start at the beginning of the buffer.
174 Return a pointer to LINE, or to the beginning of the buffer if LINE is
175 NULL. */
177 char *
178 variable_expand_string (line, string, length)
179 register char *line;
180 char *string;
181 long length;
183 register struct variable *v;
184 register char *p, *o, *p1;
185 char save_char = '\0';
186 unsigned int line_offset;
188 if (!line)
189 line = initialize_variable_output();
191 p = string;
192 o = line;
193 line_offset = line - variable_buffer;
195 if (length >= 0)
197 save_char = string[length];
198 string[length] = '\0';
201 while (1)
203 /* Copy all following uninteresting chars all at once to the
204 variable output buffer, and skip them. Uninteresting chars end
205 at the next $ or the end of the input. */
207 p1 = strchr (p, '$');
209 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
211 if (p1 == 0)
212 break;
213 p = p1 + 1;
215 /* Dispatch on the char that follows the $. */
217 switch (*p)
219 case '$':
220 /* $$ seen means output one $ to the variable output buffer. */
221 o = variable_buffer_output (o, p, 1);
222 break;
224 case '(':
225 case '{':
226 /* $(...) or ${...} is the general case of substitution. */
228 char openparen = *p;
229 char closeparen = (openparen == '(') ? ')' : '}';
230 register char *beg = p + 1;
231 int free_beg = 0;
232 char *op, *begp;
233 char *end, *colon;
235 op = o;
236 begp = p;
237 if (handle_function (&op, &begp))
239 o = op;
240 p = begp;
241 break;
244 /* Is there a variable reference inside the parens or braces?
245 If so, expand it before expanding the entire reference. */
247 end = strchr (beg, closeparen);
248 if (end == 0)
249 /* Unterminated variable reference. */
250 fatal (reading_file, _("unterminated variable reference"));
251 p1 = lindex (beg, end, '$');
252 if (p1 != 0)
254 /* BEG now points past the opening paren or brace.
255 Count parens or braces until it is matched. */
256 int count = 0;
257 for (p = beg; *p != '\0'; ++p)
259 if (*p == openparen)
260 ++count;
261 else if (*p == closeparen && --count < 0)
262 break;
264 /* If COUNT is >= 0, there were unmatched opening parens
265 or braces, so we go to the simple case of a variable name
266 such as `$($(a)'. */
267 if (count < 0)
269 beg = expand_argument (beg, p); /* Expand the name. */
270 free_beg = 1; /* Remember to free BEG when finished. */
271 end = strchr (beg, '\0');
274 else
275 /* Advance P to the end of this reference. After we are
276 finished expanding this one, P will be incremented to
277 continue the scan. */
278 p = end;
280 /* This is not a reference to a built-in function and
281 any variable references inside are now expanded.
282 Is the resultant text a substitution reference? */
284 colon = lindex (beg, end, ':');
285 if (colon != 0)
287 /* This looks like a substitution reference: $(FOO:A=B). */
288 char *subst_beg, *subst_end, *replace_beg, *replace_end;
290 subst_beg = colon + 1;
291 subst_end = strchr (subst_beg, '=');
292 if (subst_end == 0)
293 /* There is no = in sight. Punt on the substitution
294 reference and treat this as a variable name containing
295 a colon, in the code below. */
296 colon = 0;
297 else
299 replace_beg = subst_end + 1;
300 replace_end = end;
302 /* Extract the variable name before the colon
303 and look up that variable. */
304 v = lookup_variable (beg, colon - beg);
305 if (v == 0)
306 warn_undefined (beg, colon - beg);
308 if (v != 0 && *v->value != '\0')
310 char *value = (v->recursive ? recursively_expand (v)
311 : v->value);
312 char *pattern, *percent;
313 if (free_beg)
315 *subst_end = '\0';
316 pattern = subst_beg;
318 else
320 pattern = (char *) alloca (subst_end - subst_beg
321 + 1);
322 bcopy (subst_beg, pattern, subst_end - subst_beg);
323 pattern[subst_end - subst_beg] = '\0';
325 percent = find_percent (pattern);
326 if (percent != 0)
328 char *replace;
329 if (free_beg)
331 *replace_end = '\0';
332 replace = replace_beg;
334 else
336 replace = (char *) alloca (replace_end
337 - replace_beg
338 + 1);
339 bcopy (replace_beg, replace,
340 replace_end - replace_beg);
341 replace[replace_end - replace_beg] = '\0';
344 o = patsubst_expand (o, value, pattern, replace,
345 percent, (char *) 0);
347 else
348 o = subst_expand (o, value,
349 pattern, replace_beg,
350 strlen (pattern),
351 end - replace_beg,
352 0, 1);
353 if (v->recursive)
354 free (value);
359 if (colon == 0)
360 /* This is an ordinary variable reference.
361 Look up the value of the variable. */
362 o = reference_variable (o, beg, end - beg);
364 if (free_beg)
365 free (beg);
367 break;
369 case '\0':
370 break;
372 default:
373 if (isblank (p[-1]))
374 break;
376 /* A $ followed by a random char is a variable reference:
377 $a is equivalent to $(a). */
379 /* We could do the expanding here, but this way
380 avoids code repetition at a small performance cost. */
381 char name[5];
382 name[0] = '$';
383 name[1] = '(';
384 name[2] = *p;
385 name[3] = ')';
386 name[4] = '\0';
387 p1 = allocated_variable_expand (name);
388 o = variable_buffer_output (o, p1, strlen (p1));
389 free (p1);
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 (line)
415 char *line;
417 return variable_expand_string(NULL, line, (long)-1);
420 /* Expand an argument for an expansion function.
421 The text starting at STR and ending at END is variable-expanded
422 into a null-terminated string that is returned as the value.
423 This is done without clobbering `variable_buffer' or the current
424 variable-expansion that is in progress. */
426 char *
427 expand_argument (str, end)
428 char *str, *end;
430 char *tmp;
432 if (!end || *end == '\0')
433 tmp = str;
434 else
436 tmp = (char *) alloca (end - str + 1);
437 bcopy (str, tmp, end - str);
438 tmp[end - str] = '\0';
441 return allocated_variable_expand (tmp);
444 /* Expand LINE for FILE. Error messages refer to the file and line where
445 FILE's commands were found. Expansion uses FILE's variable set list. */
447 static char *
448 variable_expand_for_file (line, file)
449 char *line;
450 register struct file *file;
452 char *result;
453 struct variable_set_list *save;
455 if (file == 0)
456 return variable_expand (line);
458 save = current_variable_set_list;
459 current_variable_set_list = file->variables;
460 if (file->cmds && file->cmds->fileinfo.filenm)
461 reading_file = &file->cmds->fileinfo;
462 else
463 reading_file = 0;
464 result = variable_expand (line);
465 current_variable_set_list = save;
466 reading_file = 0;
468 return result;
471 /* Like allocated_variable_expand, but we first expand this variable in the
472 context of the next variable set, then we append the expanded value. */
474 static char *
475 allocated_variable_append (v, list)
476 struct variable *v;
477 struct variable_set_list *list;
479 struct variable_set_list *save;
480 int len = strlen (v->name);
481 char *var = alloca (len + 4);
482 char *value;
484 char *obuf = variable_buffer;
485 unsigned int olen = variable_buffer_length;
487 variable_buffer = 0;
489 if (!list)
490 list = current_variable_set_list;
492 assert(list->next != 0);
493 save = current_variable_set_list;
494 current_variable_set_list = list->next;
496 var[0] = '$';
497 var[1] = '(';
498 strcpy (&var[2], v->name);
499 var[len+2] = ')';
500 var[len+3] = '\0';
502 value = variable_expand_for_file (var, 0);
504 current_variable_set_list = save;
506 value += strlen (value);
507 value = variable_buffer_output (value, " ", 1);
508 value = variable_expand_string (value, v->value, (long)-1);
510 value = variable_buffer;
512 #if 0
513 /* Waste a little memory and save time. */
514 value = xrealloc (value, strlen (value))
515 #endif
517 variable_buffer = obuf;
518 variable_buffer_length = olen;
520 return value;
523 /* Like variable_expand_for_file, but the returned string is malloc'd.
524 This function is called a lot. It wants to be efficient. */
526 char *
527 allocated_variable_expand_for_file (line, file)
528 char *line;
529 struct file *file;
531 char *value;
533 char *obuf = variable_buffer;
534 unsigned int olen = variable_buffer_length;
536 variable_buffer = 0;
538 value = variable_expand_for_file (line, file);
540 #if 0
541 /* Waste a little memory and save time. */
542 value = xrealloc (value, strlen (value))
543 #endif
545 variable_buffer = obuf;
546 variable_buffer_length = olen;
548 return value;