Sun May 12 19:19:43 1996 Aaron Digulla <digulla@fh-konstanz.de>
[make.git] / expand.c
blob5f46ac277172860baff799a36b82717999370324
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "filedef.h"
21 #include "job.h"
22 #include "commands.h"
23 #include "variable.h"
25 /* The next two describe the variable output buffer.
26 This buffer is used to hold the variable-expansion of a line of the
27 makefile. It is made bigger with realloc whenever it is too small.
28 variable_buffer_length is the size currently allocated.
29 variable_buffer is the address of the buffer. */
31 static unsigned int variable_buffer_length;
32 static char *variable_buffer;
34 /* Subroutine of variable_expand and friends:
35 The text to add is LENGTH chars starting at STRING to the variable_buffer.
36 The text is added to the buffer at PTR, and the updated pointer into
37 the buffer is returned as the value. Thus, the value returned by
38 each call to variable_buffer_output should be the first argument to
39 the following call. */
41 char *
42 variable_buffer_output (ptr, string, length)
43 char *ptr, *string;
44 unsigned int length;
46 register unsigned int newlen = length + (ptr - variable_buffer);
48 if (newlen > variable_buffer_length)
50 unsigned int offset = ptr - variable_buffer;
51 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
52 ? newlen + 100
53 : 2 * variable_buffer_length);
54 variable_buffer = (char *) xrealloc (variable_buffer,
55 variable_buffer_length);
56 ptr = variable_buffer + offset;
59 bcopy (string, ptr, length);
60 return ptr + length;
63 /* Return a pointer to the beginning of the variable buffer. */
65 static char *
66 initialize_variable_output ()
68 /* If we don't have a variable output buffer yet, get one. */
70 if (variable_buffer == 0)
72 variable_buffer_length = 200;
73 variable_buffer = (char *) xmalloc (variable_buffer_length);
74 variable_buffer[0] = '\0';
77 return variable_buffer;
80 /* Recursively expand V. The returned string is malloc'd. */
82 char *
83 recursively_expand (v)
84 register struct variable *v;
86 char *value;
88 if (v->expanding)
90 /* Expanding V causes infinite recursion. Lose. */
91 if (reading_filename == 0)
92 fatal ("Recursive variable `%s' references itself (eventually)",
93 v->name);
94 else
95 makefile_fatal
96 (reading_filename, *reading_lineno_ptr,
97 "Recursive variable `%s' references itself (eventually)",
98 v->name);
101 v->expanding = 1;
102 value = allocated_variable_expand (v->value);
103 v->expanding = 0;
105 return value;
108 /* Warn that NAME is an undefined variable. */
110 #ifdef __GNUC__
111 __inline
112 #endif
113 static void
114 warn_undefined (name, length)
115 char *name;
116 unsigned int length;
118 if (warn_undefined_variables_flag)
120 static const char warnmsg[] = "warning: undefined variable `%.*s'";
121 if (reading_filename != 0)
122 makefile_error (reading_filename, *reading_lineno_ptr,
123 warnmsg, length, name);
124 else
125 error (warnmsg, length, name);
129 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
131 #ifdef __GNUC__
132 __inline
133 #endif
134 static char *
135 reference_variable (o, name, length)
136 char *o;
137 char *name;
138 unsigned int length;
140 register struct variable *v = lookup_variable (name, length);
142 if (v == 0)
143 warn_undefined (name, length);
145 if (v != 0 && *v->value != '\0')
147 char *value = (v->recursive ? recursively_expand (v) : v->value);
148 o = variable_buffer_output (o, value, strlen (value));
149 if (v->recursive)
150 free (value);
153 return o;
156 /* Scan LINE for variable references and expansion-function calls.
157 Build in `variable_buffer' the result of expanding the references and calls.
158 Return the address of the resulting string, which is null-terminated
159 and is valid only until the next time this function is called. */
161 char *
162 variable_expand (line)
163 register char *line;
165 register struct variable *v;
166 register char *p, *o, *p1;
168 p = line;
169 o = initialize_variable_output ();
171 while (1)
173 /* Copy all following uninteresting chars all at once to the
174 variable output buffer, and skip them. Uninteresting chars end
175 at the next $ or the end of the input. */
177 p1 = index (p, '$');
179 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
181 if (p1 == 0)
182 break;
183 p = p1 + 1;
185 /* Dispatch on the char that follows the $. */
187 switch (*p)
189 case '$':
190 /* $$ seen means output one $ to the variable output buffer. */
191 o = variable_buffer_output (o, p, 1);
192 break;
194 case '(':
195 case '{':
196 /* $(...) or ${...} is the general case of substitution. */
198 char openparen = *p;
199 char closeparen = (openparen == '(') ? ')' : '}';
200 register char *beg = p + 1;
201 int free_beg = 0;
202 char *op, *begp;
203 char *end, *colon;
205 op = o;
206 begp = p;
207 if (handle_function (&op, &begp))
209 o = op;
210 p = begp;
211 break;
214 /* Is there a variable reference inside the parens or braces?
215 If so, expand it before expanding the entire reference. */
217 end = index (beg, closeparen);
218 if (end == 0)
220 /* Unterminated variable reference. */
221 if (reading_filename != 0)
222 makefile_fatal (reading_filename, *reading_lineno_ptr,
223 "unterminated variable reference");
224 else
225 fatal ("unterminated variable reference");
227 p1 = lindex (beg, end, '$');
228 if (p1 != 0)
230 /* BEG now points past the opening paren or brace.
231 Count parens or braces until it is matched. */
232 int count = 0;
233 for (p = beg; *p != '\0'; ++p)
235 if (*p == openparen)
236 ++count;
237 else if (*p == closeparen && --count < 0)
238 break;
240 /* If COUNT is >= 0, there were unmatched opening parens
241 or braces, so we go to the simple case of a variable name
242 such as `$($(a)'. */
243 if (count < 0)
245 beg = expand_argument (beg, p); /* Expand the name. */
246 free_beg = 1; /* Remember to free BEG when finished. */
247 end = index (beg, '\0');
250 else
251 /* Advance P to the end of this reference. After we are
252 finished expanding this one, P will be incremented to
253 continue the scan. */
254 p = end;
256 /* This is not a reference to a built-in function and
257 any variable references inside are now expanded.
258 Is the resultant text a substitution reference? */
260 colon = lindex (beg, end, ':');
261 if (colon != 0)
263 /* This looks like a substitution reference: $(FOO:A=B). */
264 char *subst_beg, *subst_end, *replace_beg, *replace_end;
266 subst_beg = colon + 1;
267 subst_end = index (subst_beg, '=');
268 if (subst_end == 0)
269 /* There is no = in sight. Punt on the substitution
270 reference and treat this as a variable name containing
271 a colon, in the code below. */
272 colon = 0;
273 else
275 replace_beg = subst_end + 1;
276 replace_end = end;
278 /* Extract the variable name before the colon
279 and look up that variable. */
280 v = lookup_variable (beg, colon - beg);
281 if (v == 0)
282 warn_undefined (beg, colon - beg);
284 if (v != 0 && *v->value != '\0')
286 char *value = (v->recursive ? recursively_expand (v)
287 : v->value);
288 char *pattern, *percent;
289 if (free_beg)
291 *subst_end = '\0';
292 pattern = subst_beg;
294 else
296 pattern = (char *) alloca (subst_end - subst_beg
297 + 1);
298 bcopy (subst_beg, pattern, subst_end - subst_beg);
299 pattern[subst_end - subst_beg] = '\0';
301 percent = find_percent (pattern);
302 if (percent != 0)
304 char *replace;
305 if (free_beg)
307 *replace_end = '\0';
308 replace = replace_beg;
310 else
312 replace = (char *) alloca (replace_end
313 - replace_beg
314 + 1);
315 bcopy (replace_beg, replace,
316 replace_end - replace_beg);
317 replace[replace_end - replace_beg] = '\0';
320 o = patsubst_expand (o, value, pattern, replace,
321 percent, (char *) 0);
323 else
324 o = subst_expand (o, value,
325 pattern, replace_beg,
326 strlen (pattern),
327 end - replace_beg,
328 0, 1);
329 if (v->recursive)
330 free (value);
335 if (colon == 0)
336 /* This is an ordinary variable reference.
337 Look up the value of the variable. */
338 o = reference_variable (o, beg, end - beg);
340 if (free_beg)
341 free (beg);
343 break;
345 case '\0':
346 break;
348 default:
349 if (isblank (p[-1]))
350 break;
352 /* A $ followed by a random char is a variable reference:
353 $a is equivalent to $(a). */
355 /* We could do the expanding here, but this way
356 avoids code repetition at a small performance cost. */
357 char name[5];
358 name[0] = '$';
359 name[1] = '(';
360 name[2] = *p;
361 name[3] = ')';
362 name[4] = '\0';
363 p1 = allocated_variable_expand (name);
364 o = variable_buffer_output (o, p1, strlen (p1));
365 free (p1);
368 break;
371 if (*p == '\0')
372 break;
373 else
374 ++p;
377 (void) variable_buffer_output (o, "", 1);
378 return initialize_variable_output ();
381 /* Expand an argument for an expansion function.
382 The text starting at STR and ending at END is variable-expanded
383 into a null-terminated string that is returned as the value.
384 This is done without clobbering `variable_buffer' or the current
385 variable-expansion that is in progress. */
387 char *
388 expand_argument (str, end)
389 char *str, *end;
391 char *tmp;
393 if (*end == '\0')
394 tmp = str;
395 else
397 tmp = (char *) alloca (end - str + 1);
398 bcopy (str, tmp, end - str);
399 tmp[end - str] = '\0';
402 return allocated_variable_expand (tmp);
405 /* Expand LINE for FILE. Error messages refer to the file and line where
406 FILE's commands were found. Expansion uses FILE's variable set list. */
408 char *
409 variable_expand_for_file (line, file)
410 char *line;
411 register struct file *file;
413 char *result;
414 struct variable_set_list *save;
416 if (file == 0)
417 return variable_expand (line);
419 save = current_variable_set_list;
420 current_variable_set_list = file->variables;
421 reading_filename = file->cmds->filename;
422 reading_lineno_ptr = &file->cmds->lineno;
423 result = variable_expand (line);
424 current_variable_set_list = save;
425 reading_filename = 0;
426 reading_lineno_ptr = 0;
428 return result;
431 /* Like variable_expand_for_file, but the returned string is malloc'd.
432 This function is called a lot. It wants to be efficient. */
434 char *
435 allocated_variable_expand_for_file (line, file)
436 char *line;
437 struct file *file;
439 char *value;
441 char *obuf = variable_buffer;
442 unsigned int olen = variable_buffer_length;
444 variable_buffer = 0;
446 value = variable_expand_for_file (line, file);
448 #if 0
449 /* Waste a little memory and save time. */
450 value = xrealloc (value, strlen (value))
451 #endif
453 variable_buffer = obuf;
454 variable_buffer_length = olen;
456 return value;