* Added the test suite to the main distribution.
[make.git] / expand.c
blobaebbe3f6bfd41ae3f20ebd0b7bf1089baa70ca8f
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 "make.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
25 #include "rule.h"
27 /* The next two describe the variable output buffer.
28 This buffer is used to hold the variable-expansion of a line of the
29 makefile. It is made bigger with realloc whenever it is too small.
30 variable_buffer_length is the size currently allocated.
31 variable_buffer is the address of the buffer.
33 For efficiency, it's guaranteed that the buffer will always have
34 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
35 extra chars without having to call a function. Note you should never use
36 these bytes unless you're _sure_ you have room (you know when the buffer
37 length was last checked. */
39 #define VARIABLE_BUFFER_ZONE 5
41 static unsigned int variable_buffer_length;
42 char *variable_buffer;
44 /* Subroutine of variable_expand and friends:
45 The text to add is LENGTH chars starting at STRING to the variable_buffer.
46 The text is added to the buffer at PTR, and the updated pointer into
47 the buffer is returned as the value. Thus, the value returned by
48 each call to variable_buffer_output should be the first argument to
49 the following call. */
51 char *
52 variable_buffer_output (ptr, string, length)
53 char *ptr, *string;
54 unsigned int length;
56 register unsigned int newlen = length + (ptr - variable_buffer);
58 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
60 unsigned int offset = ptr - variable_buffer;
61 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
62 ? newlen + 100
63 : 2 * variable_buffer_length);
64 variable_buffer = (char *) xrealloc (variable_buffer,
65 variable_buffer_length);
66 ptr = variable_buffer + offset;
69 bcopy (string, ptr, length);
70 return ptr + length;
73 /* Return a pointer to the beginning of the variable buffer. */
75 static char *
76 initialize_variable_output ()
78 /* If we don't have a variable output buffer yet, get one. */
80 if (variable_buffer == 0)
82 variable_buffer_length = 200;
83 variable_buffer = (char *) xmalloc (variable_buffer_length);
84 variable_buffer[0] = '\0';
87 return variable_buffer;
90 /* Recursively expand V. The returned string is malloc'd. */
92 char *
93 recursively_expand (v)
94 register struct variable *v;
96 char *value;
98 if (v->expanding)
99 /* Expanding V causes infinite recursion. Lose. */
100 fatal (reading_file,
101 _("Recursive variable `%s' references itself (eventually)"),
102 v->name);
104 v->expanding = 1;
105 value = allocated_variable_expand (v->value);
106 v->expanding = 0;
108 return value;
111 /* Warn that NAME is an undefined variable. */
113 #ifdef __GNUC__
114 __inline
115 #endif
116 static void
117 warn_undefined (name, length)
118 char *name;
119 unsigned int length;
121 if (warn_undefined_variables_flag)
122 error (reading_file,
123 _("warning: undefined variable `%.*s'"), (int)length, name);
126 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
128 #ifdef __GNUC__
129 __inline
130 #endif
131 static char *
132 reference_variable (o, name, length)
133 char *o;
134 char *name;
135 unsigned int length;
137 register struct variable *v = lookup_variable (name, length);
139 if (v == 0)
140 warn_undefined (name, length);
142 if (v != 0 && *v->value != '\0')
144 char *value = (v->recursive ? recursively_expand (v) : v->value);
145 o = variable_buffer_output (o, value, strlen (value));
146 if (v->recursive)
147 free (value);
150 return o;
153 /* Scan STRING for variable references and expansion-function calls. Only
154 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
155 a null byte is found.
157 Write the results to LINE, which must point into `variable_buffer'. If
158 LINE is NULL, start at the beginning of the buffer.
159 Return a pointer to LINE, or to the beginning of the buffer if LINE is
160 NULL. */
162 char *
163 variable_expand_string (line, string, length)
164 register char *line;
165 char *string;
166 long length;
168 register struct variable *v;
169 register char *p, *o, *p1;
170 char save_char = '\0';
171 unsigned int line_offset;
173 if (!line)
174 line = initialize_variable_output();
176 p = string;
177 o = line;
178 line_offset = line - variable_buffer;
180 if (length >= 0)
182 save_char = string[length];
183 string[length] = '\0';
186 while (1)
188 /* Copy all following uninteresting chars all at once to the
189 variable output buffer, and skip them. Uninteresting chars end
190 at the next $ or the end of the input. */
192 p1 = index (p, '$');
194 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
196 if (p1 == 0)
197 break;
198 p = p1 + 1;
200 /* Dispatch on the char that follows the $. */
202 switch (*p)
204 case '$':
205 /* $$ seen means output one $ to the variable output buffer. */
206 o = variable_buffer_output (o, p, 1);
207 break;
209 case '(':
210 case '{':
211 /* $(...) or ${...} is the general case of substitution. */
213 char openparen = *p;
214 char closeparen = (openparen == '(') ? ')' : '}';
215 register char *beg = p + 1;
216 int free_beg = 0;
217 char *op, *begp;
218 char *end, *colon;
220 op = o;
221 begp = p;
222 if (handle_function (&op, &begp))
224 o = op;
225 p = begp;
226 break;
229 /* Is there a variable reference inside the parens or braces?
230 If so, expand it before expanding the entire reference. */
232 end = index (beg, closeparen);
233 if (end == 0)
234 /* Unterminated variable reference. */
235 fatal (reading_file, _("unterminated variable reference"));
236 p1 = lindex (beg, end, '$');
237 if (p1 != 0)
239 /* BEG now points past the opening paren or brace.
240 Count parens or braces until it is matched. */
241 int count = 0;
242 for (p = beg; *p != '\0'; ++p)
244 if (*p == openparen)
245 ++count;
246 else if (*p == closeparen && --count < 0)
247 break;
249 /* If COUNT is >= 0, there were unmatched opening parens
250 or braces, so we go to the simple case of a variable name
251 such as `$($(a)'. */
252 if (count < 0)
254 beg = expand_argument (beg, p); /* Expand the name. */
255 free_beg = 1; /* Remember to free BEG when finished. */
256 end = index (beg, '\0');
259 else
260 /* Advance P to the end of this reference. After we are
261 finished expanding this one, P will be incremented to
262 continue the scan. */
263 p = end;
265 /* This is not a reference to a built-in function and
266 any variable references inside are now expanded.
267 Is the resultant text a substitution reference? */
269 colon = lindex (beg, end, ':');
270 if (colon != 0)
272 /* This looks like a substitution reference: $(FOO:A=B). */
273 char *subst_beg, *subst_end, *replace_beg, *replace_end;
275 subst_beg = colon + 1;
276 subst_end = index (subst_beg, '=');
277 if (subst_end == 0)
278 /* There is no = in sight. Punt on the substitution
279 reference and treat this as a variable name containing
280 a colon, in the code below. */
281 colon = 0;
282 else
284 replace_beg = subst_end + 1;
285 replace_end = end;
287 /* Extract the variable name before the colon
288 and look up that variable. */
289 v = lookup_variable (beg, colon - beg);
290 if (v == 0)
291 warn_undefined (beg, colon - beg);
293 if (v != 0 && *v->value != '\0')
295 char *value = (v->recursive ? recursively_expand (v)
296 : v->value);
297 char *pattern, *percent;
298 if (free_beg)
300 *subst_end = '\0';
301 pattern = subst_beg;
303 else
305 pattern = (char *) alloca (subst_end - subst_beg
306 + 1);
307 bcopy (subst_beg, pattern, subst_end - subst_beg);
308 pattern[subst_end - subst_beg] = '\0';
310 percent = find_percent (pattern);
311 if (percent != 0)
313 char *replace;
314 if (free_beg)
316 *replace_end = '\0';
317 replace = replace_beg;
319 else
321 replace = (char *) alloca (replace_end
322 - replace_beg
323 + 1);
324 bcopy (replace_beg, replace,
325 replace_end - replace_beg);
326 replace[replace_end - replace_beg] = '\0';
329 o = patsubst_expand (o, value, pattern, replace,
330 percent, (char *) 0);
332 else
333 o = subst_expand (o, value,
334 pattern, replace_beg,
335 strlen (pattern),
336 end - replace_beg,
337 0, 1);
338 if (v->recursive)
339 free (value);
344 if (colon == 0)
345 /* This is an ordinary variable reference.
346 Look up the value of the variable. */
347 o = reference_variable (o, beg, end - beg);
349 if (free_beg)
350 free (beg);
352 break;
354 case '\0':
355 break;
357 default:
358 if (isblank (p[-1]))
359 break;
361 /* A $ followed by a random char is a variable reference:
362 $a is equivalent to $(a). */
364 /* We could do the expanding here, but this way
365 avoids code repetition at a small performance cost. */
366 char name[5];
367 name[0] = '$';
368 name[1] = '(';
369 name[2] = *p;
370 name[3] = ')';
371 name[4] = '\0';
372 p1 = allocated_variable_expand (name);
373 o = variable_buffer_output (o, p1, strlen (p1));
374 free (p1);
377 break;
380 if (*p == '\0')
381 break;
382 else
383 ++p;
386 if (save_char)
387 string[length] = save_char;
389 (void)variable_buffer_output (o, "", 1);
390 return (variable_buffer + line_offset);
393 /* Scan LINE for variable references and expansion-function calls.
394 Build in `variable_buffer' the result of expanding the references and calls.
395 Return the address of the resulting string, which is null-terminated
396 and is valid only until the next time this function is called. */
398 char *
399 variable_expand (line)
400 char *line;
402 return variable_expand_string(NULL, line, (long)-1);
405 /* Expand an argument for an expansion function.
406 The text starting at STR and ending at END is variable-expanded
407 into a null-terminated string that is returned as the value.
408 This is done without clobbering `variable_buffer' or the current
409 variable-expansion that is in progress. */
411 char *
412 expand_argument (str, end)
413 char *str, *end;
415 char *tmp;
417 if (*end == '\0')
418 tmp = str;
419 else
421 tmp = (char *) alloca (end - str + 1);
422 bcopy (str, tmp, end - str);
423 tmp[end - str] = '\0';
426 return allocated_variable_expand (tmp);
429 /* Expand LINE for FILE. Error messages refer to the file and line where
430 FILE's commands were found. Expansion uses FILE's variable set list. */
432 static char *
433 variable_expand_for_file (line, file)
434 char *line;
435 register struct file *file;
437 char *result;
438 struct variable_set_list *save, *fnext;
440 if (file == 0)
441 return variable_expand (line);
443 save = current_variable_set_list;
444 current_variable_set_list = file->variables;
445 if (file->cmds && file->cmds->fileinfo.filenm)
446 reading_file = &file->cmds->fileinfo;
447 else
448 reading_file = 0;
449 fnext = file->variables->next;
450 /* See if there's a pattern-specific variable struct for this target. */
451 if (!file->pat_searched)
453 file->patvar = lookup_pattern_var(file->name);
454 file->pat_searched = 1;
456 if (file->patvar != 0)
458 file->patvar->vars->next = fnext;
459 file->variables->next = file->patvar->vars;
461 result = variable_expand (line);
462 current_variable_set_list = save;
463 reading_file = 0;
464 file->variables->next = fnext;
466 return result;
469 /* Like variable_expand_for_file, but the returned string is malloc'd.
470 This function is called a lot. It wants to be efficient. */
472 char *
473 allocated_variable_expand_for_file (line, file)
474 char *line;
475 struct file *file;
477 char *value;
479 char *obuf = variable_buffer;
480 unsigned int olen = variable_buffer_length;
482 variable_buffer = 0;
484 value = variable_expand_for_file (line, file);
486 #if 0
487 /* Waste a little memory and save time. */
488 value = xrealloc (value, strlen (value))
489 #endif
491 variable_buffer = obuf;
492 variable_buffer_length = olen;
494 return value;