.
[make.git] / expand.c
blob7348fb261763ddc0bd19c6462f5442b886de2ecf
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 "commands.h"
21 #include "file.h"
22 #include "variable.h"
24 /* The next two describe the variable output buffer.
25 This buffer is used to hold the variable-expansion of a line of the
26 makefile. It is made bigger with realloc whenever it is too small.
27 variable_buffer_length is the size currently allocated.
28 variable_buffer is the address of the buffer. */
30 static unsigned int variable_buffer_length;
31 static char *variable_buffer;
33 /* Subroutine of variable_expand and friends:
34 The text to add is LENGTH chars starting at STRING to the variable_buffer.
35 The text is added to the buffer at PTR, and the updated pointer into
36 the buffer is returned as the value. Thus, the value returned by
37 each call to variable_buffer_output should be the first argument to
38 the following call. */
40 char *
41 variable_buffer_output (ptr, string, length)
42 char *ptr, *string;
43 unsigned int length;
45 register unsigned int newlen = length + (ptr - variable_buffer);
47 if (newlen > variable_buffer_length)
49 unsigned int offset = ptr - variable_buffer;
50 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
51 ? newlen + 100
52 : 2 * variable_buffer_length);
53 variable_buffer = (char *) xrealloc (variable_buffer,
54 variable_buffer_length);
55 ptr = variable_buffer + offset;
58 bcopy (string, ptr, length);
59 return ptr + length;
62 /* Return a pointer to the beginning of the variable buffer. */
64 static char *
65 initialize_variable_output ()
67 /* If we don't have a variable output buffer yet, get one. */
69 if (variable_buffer == 0)
71 variable_buffer_length = 200;
72 variable_buffer = (char *) xmalloc (variable_buffer_length);
73 variable_buffer[0] = '\0';
76 return variable_buffer;
79 /* Recursively expand V. The returned string is malloc'd. */
81 char *
82 recursively_expand (v)
83 register struct variable *v;
85 char *value;
87 if (v->expanding)
89 /* Expanding V causes infinite recursion. Lose. */
90 if (reading_filename == 0)
91 fatal ("Recursive variable `%s' references itself (eventually)",
92 v->name);
93 else
94 makefile_fatal
95 (reading_filename, *reading_lineno_ptr,
96 "Recursive variable `%s' references itself (eventually)",
97 v->name);
100 v->expanding = 1;
101 value = allocated_variable_expand (v->value);
102 v->expanding = 0;
104 return value;
107 /* Warn that NAME is an undefined variable. */
109 #ifdef __GNUC__
110 __inline
111 #endif
112 static void
113 warn_undefined (name, length)
114 char *name;
115 unsigned int length;
117 if (warn_undefined_variables_flag)
119 static const char warnmsg[] = "warning: undefined variable `%.*s'";
120 if (reading_filename != 0)
121 makefile_error (reading_filename, *reading_lineno_ptr,
122 warnmsg, length, name);
123 else
124 error (warnmsg, length, name);
128 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
130 #ifdef __GNUC__
131 __inline
132 #endif
133 static char *
134 reference_variable (o, name, length)
135 char *o;
136 char *name;
137 unsigned int length;
139 register struct variable *v = lookup_variable (name, length);
141 if (v == 0)
142 warn_undefined (name, length);
144 if (v != 0 && *v->value != '\0')
146 char *value = (v->recursive ? recursively_expand (v) : v->value);
147 o = variable_buffer_output (o, value, strlen (value));
148 if (v->recursive)
149 free (value);
152 return o;
155 /* Scan LINE for variable references and expansion-function calls.
156 Build in `variable_buffer' the result of expanding the references and calls.
157 Return the address of the resulting string, which is null-terminated
158 and is valid only until the next time this function is called. */
160 char *
161 variable_expand (line)
162 register char *line;
164 register struct variable *v;
165 register char *p, *o, *p1;
167 p = line;
168 o = initialize_variable_output ();
170 while (1)
172 /* Copy all following uninteresting chars all at once to the
173 variable output buffer, and skip them. Uninteresting chars end
174 at the next $ or the end of the input. */
176 p1 = index (p, '$');
178 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
180 if (p1 == 0)
181 break;
182 p = p1 + 1;
184 /* Dispatch on the char that follows the $. */
186 switch (*p)
188 case '$':
189 /* $$ seen means output one $ to the variable output buffer. */
190 o = variable_buffer_output (o, p, 1);
191 break;
193 case '(':
194 case '{':
195 /* $(...) or ${...} is the general case of substitution. */
197 char openparen = *p;
198 char closeparen = (openparen == '(') ? ')' : '}';
199 register char *beg = p + 1;
200 int free_beg = 0;
201 char *op, *begp;
202 char *end, *colon;
204 op = o;
205 begp = p;
206 if (handle_function (&op, &begp))
208 o = op;
209 p = begp;
210 break;
213 /* Is there a variable reference inside the parens or braces?
214 If so, expand it before expanding the entire reference. */
216 end = index (beg, closeparen);
217 if (end == 0)
219 /* Unterminated variable reference. */
220 if (reading_filename != 0)
221 makefile_fatal (reading_filename, *reading_lineno_ptr,
222 "unterminated variable reference");
223 else
224 fatal ("unterminated variable reference");
226 p1 = lindex (beg, end, '$');
227 if (p1 != 0)
229 /* BEG now points past the opening paren or brace.
230 Count parens or braces until it is matched. */
231 int count = 0;
232 for (p = beg; *p != '\0'; ++p)
234 if (*p == openparen)
235 ++count;
236 else if (*p == closeparen && --count < 0)
237 break;
239 /* If COUNT is >= 0, there were unmatched opening parens
240 or braces, so we go to the simple case of a variable name
241 such as `$($(a)'. */
242 if (count < 0)
244 beg = expand_argument (beg, p); /* Expand the name. */
245 free_beg = 1; /* Remember to free BEG when finished. */
246 end = index (beg, '\0');
249 else
250 /* Advance P to the end of this reference. After we are
251 finished expanding this one, P will be incremented to
252 continue the scan. */
253 p = end;
255 /* This is not a reference to a built-in function and
256 any variable references inside are now expanded.
257 Is the resultant text a substitution reference? */
259 colon = lindex (beg, end, ':');
260 if (colon != 0)
262 /* This looks like a substitution reference: $(FOO:A=B). */
263 char *subst_beg, *subst_end, *replace_beg, *replace_end;
265 subst_beg = colon + 1;
266 subst_end = index (subst_beg, '=');
267 if (subst_end == 0)
268 /* There is no = in sight. Punt on the substitution
269 reference and treat this as a variable name containing
270 a colon, in the code below. */
271 colon = 0;
272 else
274 replace_beg = subst_end + 1;
275 replace_end = end;
277 /* Extract the variable name before the colon
278 and look up that variable. */
279 v = lookup_variable (beg, colon - beg);
280 if (v == 0)
281 warn_undefined (beg, colon - beg);
283 if (v != 0 && *v->value != '\0')
285 char *value = (v->recursive ? recursively_expand (v)
286 : v->value);
287 char *pattern, *percent;
288 if (free_beg)
290 *subst_end = '\0';
291 pattern = subst_beg;
293 else
295 pattern = (char *) alloca (subst_end - subst_beg
296 + 1);
297 bcopy (subst_beg, pattern, subst_end - subst_beg);
298 pattern[subst_end - subst_beg] = '\0';
300 percent = find_percent (pattern);
301 if (percent != 0)
303 char *replace;
304 if (free_beg)
306 *replace_end = '\0';
307 replace = replace_beg;
309 else
311 replace = (char *) alloca (replace_end
312 - replace_beg
313 + 1);
314 bcopy (replace_beg, replace,
315 replace_end - replace_beg);
316 replace[replace_end - replace_beg] = '\0';
319 o = patsubst_expand (o, value, pattern, replace,
320 percent, (char *) 0);
322 else
323 o = subst_expand (o, value,
324 pattern, replace_beg,
325 strlen (pattern),
326 end - replace_beg,
327 0, 1);
328 if (v->recursive)
329 free (value);
334 if (colon == 0)
335 /* This is an ordinary variable reference.
336 Look up the value of the variable. */
337 o = reference_variable (o, beg, end - beg);
339 if (free_beg)
340 free (beg);
342 break;
344 case '\0':
345 break;
347 default:
348 if (isblank (p[-1]))
349 break;
351 /* A $ followed by a random char is a variable reference:
352 $a is equivalent to $(a). */
354 /* We could do the expanding here, but this way
355 avoids code repetition at a small performance cost. */
356 char name[5];
357 name[0] = '$';
358 name[1] = '(';
359 name[2] = *p;
360 name[3] = ')';
361 name[4] = '\0';
362 p1 = allocated_variable_expand (name);
363 o = variable_buffer_output (o, p1, strlen (p1));
364 free (p1);
367 break;
370 if (*p == '\0')
371 break;
372 else
373 ++p;
376 (void) variable_buffer_output (o, "", 1);
377 return initialize_variable_output ();
380 /* Expand an argument for an expansion function.
381 The text starting at STR and ending at END is variable-expanded
382 into a null-terminated string that is returned as the value.
383 This is done without clobbering `variable_buffer' or the current
384 variable-expansion that is in progress. */
386 char *
387 expand_argument (str, end)
388 char *str, *end;
390 char *tmp;
392 if (*end == '\0')
393 tmp = str;
394 else
396 tmp = (char *) alloca (end - str + 1);
397 bcopy (str, tmp, end - str);
398 tmp[end - str] = '\0';
401 return allocated_variable_expand (tmp);
404 /* Expand LINE for FILE. Error messages refer to the file and line where
405 FILE's commands were found. Expansion uses FILE's variable set list. */
407 char *
408 variable_expand_for_file (line, file)
409 char *line;
410 register struct file *file;
412 char *result;
413 struct variable_set_list *save;
415 if (file == 0)
416 return variable_expand (line);
418 save = current_variable_set_list;
419 current_variable_set_list = file->variables;
420 reading_filename = file->cmds->filename;
421 reading_lineno_ptr = &file->cmds->lineno;
422 result = variable_expand (line);
423 current_variable_set_list = save;
424 reading_filename = 0;
425 reading_lineno_ptr = 0;
427 return result;
430 /* Like variable_expand_for_file, but the returned string is malloc'd.
431 This function is called a lot. It wants to be efficient. */
433 char *
434 allocated_variable_expand_for_file (line, file)
435 char *line;
436 struct file *file;
438 char *value;
440 char *obuf = variable_buffer;
441 unsigned int olen = variable_buffer_length;
443 variable_buffer = 0;
445 value = variable_expand_for_file (line, file);
447 #if 0
448 /* Waste a little memory and save time. */
449 value = xrealloc (value, strlen (value))
450 #endif
452 variable_buffer = obuf;
453 variable_buffer_length = olen;
455 return value;