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)
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. */
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. */
42 variable_buffer_output (ptr
, string
, 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
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
);
63 /* Return a pointer to the beginning of the variable buffer. */
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. */
83 recursively_expand (v
)
84 register struct variable
*v
;
90 /* Expanding V causes infinite recursion. Lose. */
91 if (reading_filename
== 0)
92 fatal ("Recursive variable `%s' references itself (eventually)",
96 (reading_filename
, *reading_lineno_ptr
,
97 "Recursive variable `%s' references itself (eventually)",
102 value
= allocated_variable_expand (v
->value
);
108 /* Warn that NAME is an undefined variable. */
114 warn_undefined (name
, 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
);
125 error (warnmsg
, length
, name
);
129 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
135 reference_variable (o
, name
, length
)
140 register struct variable
*v
= lookup_variable (name
, length
);
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
));
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. */
162 variable_expand (line
)
165 register struct variable
*v
;
166 register char *p
, *o
, *p1
;
169 o
= initialize_variable_output ();
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. */
179 o
= variable_buffer_output (o
, p
, p1
!= 0 ? p1
- p
: strlen (p
) + 1);
185 /* Dispatch on the char that follows the $. */
190 /* $$ seen means output one $ to the variable output buffer. */
191 o
= variable_buffer_output (o
, p
, 1);
196 /* $(...) or ${...} is the general case of substitution. */
199 char closeparen
= (openparen
== '(') ? ')' : '}';
200 register char *beg
= p
+ 1;
207 if (handle_function (&op
, &begp
))
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
);
220 /* Unterminated variable reference. */
221 if (reading_filename
!= 0)
222 makefile_fatal (reading_filename
, *reading_lineno_ptr
,
223 "unterminated variable reference");
225 fatal ("unterminated variable reference");
227 p1
= lindex (beg
, end
, '$');
230 /* BEG now points past the opening paren or brace.
231 Count parens or braces until it is matched. */
233 for (p
= beg
; *p
!= '\0'; ++p
)
237 else if (*p
== closeparen
&& --count
< 0)
240 /* If COUNT is >= 0, there were unmatched opening parens
241 or braces, so we go to the simple case of a variable name
245 beg
= expand_argument (beg
, p
); /* Expand the name. */
246 free_beg
= 1; /* Remember to free BEG when finished. */
247 end
= index (beg
, '\0');
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. */
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
, ':');
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
, '=');
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. */
275 replace_beg
= subst_end
+ 1;
278 /* Extract the variable name before the colon
279 and look up that variable. */
280 v
= lookup_variable (beg
, colon
- beg
);
282 warn_undefined (beg
, colon
- beg
);
284 if (v
!= 0 && *v
->value
!= '\0')
286 char *value
= (v
->recursive
? recursively_expand (v
)
288 char *pattern
, *percent
;
296 pattern
= (char *) alloca (subst_end
- subst_beg
298 bcopy (subst_beg
, pattern
, subst_end
- subst_beg
);
299 pattern
[subst_end
- subst_beg
] = '\0';
301 percent
= find_percent (pattern
);
308 replace
= replace_beg
;
312 replace
= (char *) alloca (replace_end
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);
324 o
= subst_expand (o
, value
,
325 pattern
, replace_beg
,
336 /* This is an ordinary variable reference.
337 Look up the value of the variable. */
338 o
= reference_variable (o
, beg
, end
- beg
);
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. */
363 p1
= allocated_variable_expand (name
);
364 o
= variable_buffer_output (o
, p1
, strlen (p1
));
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. */
388 expand_argument (str
, end
)
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. */
409 variable_expand_for_file (line
, file
)
411 register struct file
*file
;
414 struct variable_set_list
*save
;
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;
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. */
435 allocated_variable_expand_for_file (line
, file
)
441 char *obuf
= variable_buffer
;
442 unsigned int olen
= variable_buffer_length
;
446 value
= variable_expand_for_file (line
, file
);
449 /* Waste a little memory and save time. */
450 value
= xrealloc (value
, strlen (value
))
453 variable_buffer
= obuf
;
454 variable_buffer_length
= olen
;