Fix Savannah bug # 13478. If -L is given, take the latest mtime for a
[make/kirr.git] / expand.c
blob372a0f59f0fff5fd6eed591d04385281875c147c
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"
22 #include <assert.h>
24 #include "filedef.h"
25 #include "job.h"
26 #include "commands.h"
27 #include "variable.h"
28 #include "rule.h"
30 /* The next two describe the variable output buffer.
31 This buffer is used to hold the variable-expansion of a line of the
32 makefile. It is made bigger with realloc whenever it is too small.
33 variable_buffer_length is the size currently allocated.
34 variable_buffer is the address of the buffer.
36 For efficiency, it's guaranteed that the buffer will always have
37 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
38 extra chars without having to call a function. Note you should never use
39 these bytes unless you're _sure_ you have room (you know when the buffer
40 length was last checked. */
42 #define VARIABLE_BUFFER_ZONE 5
44 static unsigned int variable_buffer_length;
45 char *variable_buffer;
47 /* Subroutine of variable_expand and friends:
48 The text to add is LENGTH chars starting at STRING to the variable_buffer.
49 The text is added to the buffer at PTR, and the updated pointer into
50 the buffer is returned as the value. Thus, the value returned by
51 each call to variable_buffer_output should be the first argument to
52 the following call. */
54 char *
55 variable_buffer_output (char *ptr, char *string, unsigned int length)
57 register unsigned int newlen = length + (ptr - variable_buffer);
59 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
61 unsigned int offset = ptr - variable_buffer;
62 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
63 ? newlen + 100
64 : 2 * variable_buffer_length);
65 variable_buffer = (char *) xrealloc (variable_buffer,
66 variable_buffer_length);
67 ptr = variable_buffer + offset;
70 bcopy (string, ptr, length);
71 return ptr + length;
74 /* Return a pointer to the beginning of the variable buffer. */
76 static char *
77 initialize_variable_output (void)
79 /* If we don't have a variable output buffer yet, get one. */
81 if (variable_buffer == 0)
83 variable_buffer_length = 200;
84 variable_buffer = (char *) xmalloc (variable_buffer_length);
85 variable_buffer[0] = '\0';
88 return variable_buffer;
91 /* Recursively expand V. The returned string is malloc'd. */
93 static char *allocated_variable_append PARAMS ((const struct variable *v));
95 char *
96 recursively_expand_for_file (struct variable *v, struct file *file)
98 char *value;
99 struct variable_set_list *save = 0;
100 int set_reading = 0;
102 if (v->expanding)
104 if (!v->exp_count)
105 /* Expanding V causes infinite recursion. Lose. */
106 fatal (reading_file,
107 _("Recursive variable `%s' references itself (eventually)"),
108 v->name);
109 --v->exp_count;
112 if (file)
114 save = current_variable_set_list;
115 current_variable_set_list = file->variables;
118 /* If we have no other file-reading context, use the variable's context. */
119 if (!reading_file)
121 set_reading = 1;
122 reading_file = &v->fileinfo;
125 v->expanding = 1;
126 if (v->append)
127 value = allocated_variable_append (v);
128 else
129 value = allocated_variable_expand (v->value);
130 v->expanding = 0;
132 if (set_reading)
133 reading_file = 0;
134 if (file)
135 current_variable_set_list = save;
137 return value;
140 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
142 #ifdef __GNUC__
143 __inline
144 #endif
145 static char *
146 reference_variable (char *o, char *name, unsigned int length)
148 register struct variable *v;
149 char *value;
151 v = lookup_variable (name, length);
153 if (v == 0)
154 warn_undefined (name, length);
156 /* If there's no variable by that name or it has no value, stop now. */
157 if (v == 0 || (*v->value == '\0' && !v->append))
158 return o;
160 value = (v->recursive ? recursively_expand (v) : v->value);
162 o = variable_buffer_output (o, value, strlen (value));
164 if (v->recursive)
165 free (value);
167 return o;
170 /* Scan STRING for variable references and expansion-function calls. Only
171 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
172 a null byte is found.
174 Write the results to LINE, which must point into `variable_buffer'. If
175 LINE is NULL, start at the beginning of the buffer.
176 Return a pointer to LINE, or to the beginning of the buffer if LINE is
177 NULL. */
179 char *
180 variable_expand_string (char *line, char *string, long length)
182 register struct variable *v;
183 register char *p, *o, *p1;
184 char save_char = '\0';
185 unsigned int line_offset;
187 if (!line)
188 line = initialize_variable_output();
190 p = string;
191 o = line;
192 line_offset = line - variable_buffer;
194 if (length >= 0)
196 save_char = string[length];
197 string[length] = '\0';
200 while (1)
202 /* Copy all following uninteresting chars all at once to the
203 variable output buffer, and skip them. Uninteresting chars end
204 at the next $ or the end of the input. */
206 p1 = strchr (p, '$');
208 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
210 if (p1 == 0)
211 break;
212 p = p1 + 1;
214 /* Dispatch on the char that follows the $. */
216 switch (*p)
218 case '$':
219 /* $$ seen means output one $ to the variable output buffer. */
220 o = variable_buffer_output (o, p, 1);
221 break;
223 case '(':
224 case '{':
225 /* $(...) or ${...} is the general case of substitution. */
227 char openparen = *p;
228 char closeparen = (openparen == '(') ? ')' : '}';
229 register char *beg = p + 1;
230 int free_beg = 0;
231 char *op, *begp;
232 char *end, *colon;
234 op = o;
235 begp = p;
236 if (handle_function (&op, &begp))
238 o = op;
239 p = begp;
240 break;
243 /* Is there a variable reference inside the parens or braces?
244 If so, expand it before expanding the entire reference. */
246 end = strchr (beg, closeparen);
247 if (end == 0)
248 /* Unterminated variable reference. */
249 fatal (reading_file, _("unterminated variable reference"));
250 p1 = lindex (beg, end, '$');
251 if (p1 != 0)
253 /* BEG now points past the opening paren or brace.
254 Count parens or braces until it is matched. */
255 int count = 0;
256 for (p = beg; *p != '\0'; ++p)
258 if (*p == openparen)
259 ++count;
260 else if (*p == closeparen && --count < 0)
261 break;
263 /* If COUNT is >= 0, there were unmatched opening parens
264 or braces, so we go to the simple case of a variable name
265 such as `$($(a)'. */
266 if (count < 0)
268 beg = expand_argument (beg, p); /* Expand the name. */
269 free_beg = 1; /* Remember to free BEG when finished. */
270 end = strchr (beg, '\0');
273 else
274 /* Advance P to the end of this reference. After we are
275 finished expanding this one, P will be incremented to
276 continue the scan. */
277 p = end;
279 /* This is not a reference to a built-in function and
280 any variable references inside are now expanded.
281 Is the resultant text a substitution reference? */
283 colon = lindex (beg, end, ':');
284 if (colon)
286 /* This looks like a substitution reference: $(FOO:A=B). */
287 char *subst_beg, *subst_end, *replace_beg, *replace_end;
289 subst_beg = colon + 1;
290 subst_end = lindex (subst_beg, end, '=');
291 if (subst_end == 0)
292 /* There is no = in sight. Punt on the substitution
293 reference and treat this as a variable name containing
294 a colon, in the code below. */
295 colon = 0;
296 else
298 replace_beg = subst_end + 1;
299 replace_end = end;
301 /* Extract the variable name before the colon
302 and look up that variable. */
303 v = lookup_variable (beg, colon - beg);
304 if (v == 0)
305 warn_undefined (beg, colon - beg);
307 /* If the variable is not empty, perform the
308 substitution. */
309 if (v != 0 && *v->value != '\0')
311 char *pattern, *replace, *ppercent, *rpercent;
312 char *value = (v->recursive
313 ? recursively_expand (v)
314 : v->value);
316 /* Copy the pattern and the replacement. Add in an
317 extra % at the beginning to use in case there
318 isn't one in the pattern. */
319 pattern = (char *) alloca (subst_end - subst_beg + 2);
320 *(pattern++) = '%';
321 bcopy (subst_beg, pattern, subst_end - subst_beg);
322 pattern[subst_end - subst_beg] = '\0';
324 replace = (char *) alloca (replace_end
325 - replace_beg + 2);
326 *(replace++) = '%';
327 bcopy (replace_beg, replace,
328 replace_end - replace_beg);
329 replace[replace_end - replace_beg] = '\0';
331 /* Look for %. Set the percent pointers properly
332 based on whether we find one or not. */
333 ppercent = find_percent (pattern);
334 if (ppercent)
336 ++ppercent;
337 rpercent = 0;
339 else
341 ppercent = pattern;
342 rpercent = replace;
343 --pattern;
344 --replace;
347 o = patsubst_expand (o, value, pattern, replace,
348 ppercent, rpercent);
350 if (v->recursive)
351 free (value);
356 if (colon == 0)
357 /* This is an ordinary variable reference.
358 Look up the value of the variable. */
359 o = reference_variable (o, beg, end - beg);
361 if (free_beg)
362 free (beg);
364 break;
366 case '\0':
367 break;
369 default:
370 if (isblank ((unsigned char)p[-1]))
371 break;
373 /* A $ followed by a random char is a variable reference:
374 $a is equivalent to $(a). */
376 /* We could do the expanding here, but this way
377 avoids code repetition at a small performance cost. */
378 char name[5];
379 name[0] = '$';
380 name[1] = '(';
381 name[2] = *p;
382 name[3] = ')';
383 name[4] = '\0';
384 p1 = allocated_variable_expand (name);
385 o = variable_buffer_output (o, p1, strlen (p1));
386 free (p1);
389 break;
392 if (*p == '\0')
393 break;
394 else
395 ++p;
398 if (save_char)
399 string[length] = save_char;
401 (void)variable_buffer_output (o, "", 1);
402 return (variable_buffer + line_offset);
405 /* Scan LINE for variable references and expansion-function calls.
406 Build in `variable_buffer' the result of expanding the references and calls.
407 Return the address of the resulting string, which is null-terminated
408 and is valid only until the next time this function is called. */
410 char *
411 variable_expand (char *line)
413 return variable_expand_string(NULL, line, (long)-1);
416 /* Expand an argument for an expansion function.
417 The text starting at STR and ending at END is variable-expanded
418 into a null-terminated string that is returned as the value.
419 This is done without clobbering `variable_buffer' or the current
420 variable-expansion that is in progress. */
422 char *
423 expand_argument (const char *str, const char *end)
425 char *tmp;
427 if (str == end)
428 return xstrdup("");
430 if (!end || *end == '\0')
431 return allocated_variable_expand ((char *)str);
433 tmp = (char *) alloca (end - str + 1);
434 bcopy (str, tmp, end - str);
435 tmp[end - str] = '\0';
437 return allocated_variable_expand (tmp);
440 /* Expand LINE for FILE. Error messages refer to the file and line where
441 FILE's commands were found. Expansion uses FILE's variable set list. */
443 char *
444 variable_expand_for_file (char *line, struct file *file)
446 char *result;
447 struct variable_set_list *save;
449 if (file == 0)
450 return variable_expand (line);
452 save = current_variable_set_list;
453 current_variable_set_list = file->variables;
454 if (file->cmds && file->cmds->fileinfo.filenm)
455 reading_file = &file->cmds->fileinfo;
456 else
457 reading_file = 0;
458 result = variable_expand (line);
459 current_variable_set_list = save;
460 reading_file = 0;
462 return result;
465 /* Like allocated_variable_expand, but for += target-specific variables.
466 First recursively construct the variable value from its appended parts in
467 any upper variable sets. Then expand the resulting value. */
469 static char *
470 variable_append (const char *name, unsigned int length,
471 const struct variable_set_list *set)
473 const struct variable *v;
474 char *buf = 0;
476 /* If there's nothing left to check, return the empty buffer. */
477 if (!set)
478 return initialize_variable_output ();
480 /* Try to find the variable in this variable set. */
481 v = lookup_variable_in_set (name, length, set->set);
483 /* If there isn't one, look to see if there's one in a set above us. */
484 if (!v)
485 return variable_append (name, length, set->next);
487 /* If this variable type is append, first get any upper values.
488 If not, initialize the buffer. */
489 if (v->append)
490 buf = variable_append (name, length, set->next);
491 else
492 buf = initialize_variable_output ();
494 /* Append this value to the buffer, and return it.
495 If we already have a value, first add a space. */
496 if (buf > variable_buffer)
497 buf = variable_buffer_output (buf, " ", 1);
499 return variable_buffer_output (buf, v->value, strlen (v->value));
503 static char *
504 allocated_variable_append (const struct variable *v)
506 char *val, *retval;
508 /* Construct the appended variable value. */
510 char *obuf = variable_buffer;
511 unsigned int olen = variable_buffer_length;
513 variable_buffer = 0;
515 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
516 variable_buffer_output (val, "", 1);
517 val = variable_buffer;
519 variable_buffer = obuf;
520 variable_buffer_length = olen;
522 /* Now expand it and return that. */
524 retval = allocated_variable_expand (val);
526 free (val);
527 return retval;
530 /* Like variable_expand_for_file, but the returned string is malloc'd.
531 This function is called a lot. It wants to be efficient. */
533 char *
534 allocated_variable_expand_for_file (char *line, struct file *file)
536 char *value;
538 char *obuf = variable_buffer;
539 unsigned int olen = variable_buffer_length;
541 variable_buffer = 0;
543 value = variable_expand_for_file (line, file);
545 #if 0
546 /* Waste a little memory and save time. */
547 value = xrealloc (value, strlen (value))
548 #endif
550 variable_buffer = obuf;
551 variable_buffer_length = olen;
553 return value;
556 /* Install a new variable_buffer context, returning the current one for
557 safe-keeping. */
559 void
560 install_variable_buffer (char **bufp, unsigned int *lenp)
562 *bufp = variable_buffer;
563 *lenp = variable_buffer_length;
565 variable_buffer = 0;
566 initialize_variable_output ();
569 /* Restore a previously-saved variable_buffer setting (free the current one).
572 void
573 restore_variable_buffer (char *buf, unsigned int len)
575 free (variable_buffer);
577 variable_buffer = buf;
578 variable_buffer_length = len;