Abbreviate copyright years.
[make.git] / expand.c
blobbbbd6a704dd2c129af8a19d0f9dbb552ce61faca
1 /* Variable expansion functions for GNU Make.
2 Copyright (C) 1988, 1989, 1991, 1992, 1993 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 char *op, *begp;
201 char *end;
202 char *colon = 0;
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 p1 = index (beg, closeparen);
217 if (p1 != 0)
218 p1 = lindex (beg, p1, '$');
219 if (p1 != 0 && lindex (beg, p1, ':') == 0)
221 /* BEG now points past the opening paren or brace.
222 Count parens or braces until it is matched. */
223 int count = 0;
224 for (p = beg; *p != '\0'; ++p)
226 if (*p == openparen)
227 ++count;
228 else if (*p == closeparen && --count < 0)
229 break;
230 else if (colon == 0 && count == 0 && *p == ':')
231 /* Record where we found a colon, which
232 indicates a substitution reference.
233 We want to expand the text before the
234 reference only. */
235 colon = p;
237 /* If COUNT is >= 0, there were unmatched opening parens
238 or braces, so we go to the simple case of a variable name
239 such as `$($(a)'. */
240 if (count < 0)
242 char *name = expand_argument (beg, colon == 0 ? p : colon);
243 unsigned int namelen = strlen (name);
244 if (colon == 0)
246 /* This is a simple reference to the expanded name. */
247 o = reference_variable (o, name, namelen);
248 free (name);
249 break;
251 else
253 /* This is a substitution reference to the expanded
254 name. We replace the pending text with a copy
255 containing the expanded name in place of the
256 original name, and then fall through to
257 the normal substitution reference code below. */
258 unsigned int restlen = strlen (colon) + 1;
259 beg = (char *) alloca (namelen + restlen);
260 bcopy (name, beg, namelen);
261 bcopy (colon, &beg[namelen], restlen);
262 /* Point COLON into the new copy. */
263 colon = &beg[namelen];
268 /* This is not a reference to a built-in function and
269 it does not contain any variable references inside.
270 There are several things it could be. */
272 if (colon == 0)
273 colon = index (beg, ':');
274 if (colon != 0 && lindex (beg, colon, closeparen) == 0)
276 /* This is a substitution reference: $(FOO:A=B). */
277 int count;
278 char *subst_beg, *subst_end, *replace_beg, *replace_end;
280 v = lookup_variable (beg, colon - beg);
281 if (v == 0)
282 warn_undefined (beg, colon - beg);
284 subst_beg = colon + 1;
285 count = 0;
286 for (p = subst_beg; *p != '\0'; ++p)
288 if (*p == openparen)
289 ++count;
290 else if (*p == closeparen)
291 --count;
292 else if (*p == '=' && count <= 0)
293 break;
295 if (count > 0)
296 /* There were unmatched opening parens. */
297 return initialize_variable_output ();
298 subst_end = p;
300 replace_beg = p + 1;
301 count = 0;
302 for (p = replace_beg; *p != '\0'; ++p)
304 if (*p == openparen)
305 ++count;
306 else if (*p == closeparen && --count < 0)
307 break;
309 if (count > 0)
310 /* There were unmatched opening parens. */
311 return initialize_variable_output ();
312 end = p;
313 replace_end = p;
315 p = expand_argument (subst_beg, subst_end);
316 p1 = expand_argument (replace_beg, replace_end);
318 if (v != 0 && *v->value != '\0')
320 char *value = (v->recursive ? recursively_expand (v)
321 : v->value);
322 char *percent = find_percent (p);
323 if (percent != 0)
324 o = patsubst_expand (o, value, p, p1,
325 percent, (char *) 0);
326 else
327 o = subst_expand (o, value,
328 p, p1, strlen (p), strlen (p1),
329 0, 1);
330 if (v->recursive)
331 free (value);
334 free (p);
335 free (p1);
338 /* No, this must be an ordinary variable reference. */
339 else
341 /* Look up the value of the variable. */
342 end = index (beg, closeparen);
343 if (end == 0)
345 /* Unterminated variable reference. */
346 if (reading_filename != 0)
347 makefile_fatal (reading_filename, *reading_lineno_ptr,
348 "unterminated variable reference");
349 else
350 fatal ("unterminated variable reference");
352 o = reference_variable (o, beg, end - beg);
355 /* Advance p past the variable reference to resume scan. */
356 p = end;
358 break;
360 case '\0':
361 break;
363 default:
364 if (isblank (p[-1]))
365 break;
367 /* A $ followed by a random char is a variable reference:
368 $a is equivalent to $(a). */
370 /* We could do the expanding here, but this way
371 avoids code repetition at a small performance cost. */
372 char name[5];
373 name[0] = '$';
374 name[1] = '(';
375 name[2] = *p;
376 name[3] = ')';
377 name[4] = '\0';
378 p1 = allocated_variable_expand (name);
379 o = variable_buffer_output (o, p1, strlen (p1));
380 free (p1);
383 break;
386 if (*p == '\0')
387 break;
388 else
389 ++p;
392 (void) variable_buffer_output (o, "", 1);
393 return initialize_variable_output ();
396 /* Expand an argument for an expansion function.
397 The text starting at STR and ending at END is variable-expanded
398 into a null-terminated string that is returned as the value.
399 This is done without clobbering `variable_buffer' or the current
400 variable-expansion that is in progress. */
402 char *
403 expand_argument (str, end)
404 char *str, *end;
406 char *tmp;
408 if (*end == '\0')
409 tmp = str;
410 else
412 tmp = (char *) alloca (end - str + 1);
413 bcopy (str, tmp, end - str);
414 tmp[end - str] = '\0';
417 return allocated_variable_expand (tmp);
420 /* Expand LINE for FILE. Error messages refer to the file and line where
421 FILE's commands were found. Expansion uses FILE's variable set list. */
423 char *
424 variable_expand_for_file (line, file)
425 char *line;
426 register struct file *file;
428 char *result;
429 struct variable_set_list *save;
431 if (file == 0)
432 return variable_expand (line);
434 save = current_variable_set_list;
435 current_variable_set_list = file->variables;
436 reading_filename = file->cmds->filename;
437 reading_lineno_ptr = &file->cmds->lineno;
438 result = variable_expand (line);
439 current_variable_set_list = save;
440 reading_filename = 0;
441 reading_lineno_ptr = 0;
443 return result;
446 /* Like variable_expand_for_file, but the returned string is malloc'd.
447 This function is called a lot. It wants to be efficient. */
449 char *
450 allocated_variable_expand_for_file (line, file)
451 char *line;
452 struct file *file;
454 char *value;
456 char *obuf = variable_buffer;
457 unsigned int olen = variable_buffer_length;
459 variable_buffer = 0;
461 value = variable_expand_for_file (line, file);
463 #if 0
464 /* Waste a little memory and save time. */
465 value = xrealloc (value, strlen (value))
466 #endif
468 variable_buffer = obuf;
469 variable_buffer_length = olen;
471 return value;