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, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
27 /* The next two describe the variable output buffer.
28 This buffer is used to hold the variable-expansion of a line of the
29 makefile. It is made bigger with realloc whenever it is too small.
30 variable_buffer_length is the size currently allocated.
31 variable_buffer is the address of the buffer.
33 For efficiency, it's guaranteed that the buffer will always have
34 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
35 extra chars without having to call a function. Note you should never use
36 these bytes unless you're _sure_ you have room (you know when the buffer
37 length was last checked. */
39 #define VARIABLE_BUFFER_ZONE 5
41 static unsigned int variable_buffer_length
;
42 char *variable_buffer
;
44 /* Subroutine of variable_expand and friends:
45 The text to add is LENGTH chars starting at STRING to the variable_buffer.
46 The text is added to the buffer at PTR, and the updated pointer into
47 the buffer is returned as the value. Thus, the value returned by
48 each call to variable_buffer_output should be the first argument to
49 the following call. */
52 variable_buffer_output (ptr
, string
, length
)
56 register unsigned int newlen
= length
+ (ptr
- variable_buffer
);
58 if ((newlen
+ VARIABLE_BUFFER_ZONE
) > variable_buffer_length
)
60 unsigned int offset
= ptr
- variable_buffer
;
61 variable_buffer_length
= (newlen
+ 100 > 2 * variable_buffer_length
63 : 2 * variable_buffer_length
);
64 variable_buffer
= (char *) xrealloc (variable_buffer
,
65 variable_buffer_length
);
66 ptr
= variable_buffer
+ offset
;
69 bcopy (string
, ptr
, length
);
73 /* Return a pointer to the beginning of the variable buffer. */
76 initialize_variable_output ()
78 /* If we don't have a variable output buffer yet, get one. */
80 if (variable_buffer
== 0)
82 variable_buffer_length
= 200;
83 variable_buffer
= (char *) xmalloc (variable_buffer_length
);
84 variable_buffer
[0] = '\0';
87 return variable_buffer
;
90 /* Recursively expand V. The returned string is malloc'd. */
93 recursively_expand (v
)
94 register struct variable
*v
;
99 /* Expanding V causes infinite recursion. Lose. */
101 "Recursive variable `%s' references itself (eventually)", v
->name
);
104 value
= allocated_variable_expand (v
->value
);
110 /* Warn that NAME is an undefined variable. */
116 warn_undefined (name
, length
)
120 if (warn_undefined_variables_flag
)
122 "warning: undefined variable `%.*s'", (int)length
, name
);
125 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
131 reference_variable (o
, name
, length
)
136 register struct variable
*v
= lookup_variable (name
, length
);
139 warn_undefined (name
, length
);
141 if (v
!= 0 && *v
->value
!= '\0')
143 char *value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
144 o
= variable_buffer_output (o
, value
, strlen (value
));
152 /* Scan STRING for variable references and expansion-function calls. Only
153 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
154 a null byte is found.
156 Write the results to LINE, which must point into `variable_buffer'. If
157 LINE is NULL, start at the beginning of the buffer.
158 Return a pointer to LINE, or to the beginning of the buffer if LINE is
162 variable_expand_string (line
, string
, length
)
167 register struct variable
*v
;
168 register char *p
, *o
, *p1
;
169 char save_char
= '\0';
170 unsigned int line_offset
;
173 line
= initialize_variable_output();
177 line_offset
= line
- variable_buffer
;
181 save_char
= string
[length
];
182 string
[length
] = '\0';
187 /* Copy all following uninteresting chars all at once to the
188 variable output buffer, and skip them. Uninteresting chars end
189 at the next $ or the end of the input. */
193 o
= variable_buffer_output (o
, p
, p1
!= 0 ? p1
- p
: strlen (p
) + 1);
199 /* Dispatch on the char that follows the $. */
204 /* $$ seen means output one $ to the variable output buffer. */
205 o
= variable_buffer_output (o
, p
, 1);
210 /* $(...) or ${...} is the general case of substitution. */
213 char closeparen
= (openparen
== '(') ? ')' : '}';
214 register char *beg
= p
+ 1;
221 if (handle_function (&op
, &begp
))
228 /* Is there a variable reference inside the parens or braces?
229 If so, expand it before expanding the entire reference. */
231 end
= index (beg
, closeparen
);
233 /* Unterminated variable reference. */
234 fatal (reading_file
, "unterminated variable reference");
235 p1
= lindex (beg
, end
, '$');
238 /* BEG now points past the opening paren or brace.
239 Count parens or braces until it is matched. */
241 for (p
= beg
; *p
!= '\0'; ++p
)
245 else if (*p
== closeparen
&& --count
< 0)
248 /* If COUNT is >= 0, there were unmatched opening parens
249 or braces, so we go to the simple case of a variable name
253 beg
= expand_argument (beg
, p
); /* Expand the name. */
254 free_beg
= 1; /* Remember to free BEG when finished. */
255 end
= index (beg
, '\0');
259 /* Advance P to the end of this reference. After we are
260 finished expanding this one, P will be incremented to
261 continue the scan. */
264 /* This is not a reference to a built-in function and
265 any variable references inside are now expanded.
266 Is the resultant text a substitution reference? */
268 colon
= lindex (beg
, end
, ':');
271 /* This looks like a substitution reference: $(FOO:A=B). */
272 char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
274 subst_beg
= colon
+ 1;
275 subst_end
= index (subst_beg
, '=');
277 /* There is no = in sight. Punt on the substitution
278 reference and treat this as a variable name containing
279 a colon, in the code below. */
283 replace_beg
= subst_end
+ 1;
286 /* Extract the variable name before the colon
287 and look up that variable. */
288 v
= lookup_variable (beg
, colon
- beg
);
290 warn_undefined (beg
, colon
- beg
);
292 if (v
!= 0 && *v
->value
!= '\0')
294 char *value
= (v
->recursive
? recursively_expand (v
)
296 char *pattern
, *percent
;
304 pattern
= (char *) alloca (subst_end
- subst_beg
306 bcopy (subst_beg
, pattern
, subst_end
- subst_beg
);
307 pattern
[subst_end
- subst_beg
] = '\0';
309 percent
= find_percent (pattern
);
316 replace
= replace_beg
;
320 replace
= (char *) alloca (replace_end
323 bcopy (replace_beg
, replace
,
324 replace_end
- replace_beg
);
325 replace
[replace_end
- replace_beg
] = '\0';
328 o
= patsubst_expand (o
, value
, pattern
, replace
,
329 percent
, (char *) 0);
332 o
= subst_expand (o
, value
,
333 pattern
, replace_beg
,
344 /* This is an ordinary variable reference.
345 Look up the value of the variable. */
346 o
= reference_variable (o
, beg
, end
- beg
);
360 /* A $ followed by a random char is a variable reference:
361 $a is equivalent to $(a). */
363 /* We could do the expanding here, but this way
364 avoids code repetition at a small performance cost. */
371 p1
= allocated_variable_expand (name
);
372 o
= variable_buffer_output (o
, p1
, strlen (p1
));
386 string
[length
] = save_char
;
388 (void)variable_buffer_output (o
, "", 1);
389 return (variable_buffer
+ line_offset
);
392 /* Scan LINE for variable references and expansion-function calls.
393 Build in `variable_buffer' the result of expanding the references and calls.
394 Return the address of the resulting string, which is null-terminated
395 and is valid only until the next time this function is called. */
398 variable_expand (line
)
401 return variable_expand_string(NULL
, line
, (long)-1);
404 /* Expand an argument for an expansion function.
405 The text starting at STR and ending at END is variable-expanded
406 into a null-terminated string that is returned as the value.
407 This is done without clobbering `variable_buffer' or the current
408 variable-expansion that is in progress. */
411 expand_argument (str
, end
)
420 tmp
= (char *) alloca (end
- str
+ 1);
421 bcopy (str
, tmp
, end
- str
);
422 tmp
[end
- str
] = '\0';
425 return allocated_variable_expand (tmp
);
428 /* Expand LINE for FILE. Error messages refer to the file and line where
429 FILE's commands were found. Expansion uses FILE's variable set list. */
432 variable_expand_for_file (line
, file
)
434 register struct file
*file
;
437 struct variable_set_list
*save
, *fnext
;
440 return variable_expand (line
);
442 save
= current_variable_set_list
;
443 current_variable_set_list
= file
->variables
;
444 reading_file
= &file
->cmds
->fileinfo
;
445 fnext
= file
->variables
->next
;
446 /* See if there's a pattern-specific variable struct for this target. */
447 if (!file
->pat_searched
)
449 file
->patvar
= lookup_pattern_var(file
->name
);
450 file
->pat_searched
= 1;
452 if (file
->patvar
!= 0)
454 file
->patvar
->vars
->next
= fnext
;
455 file
->variables
->next
= file
->patvar
->vars
;
457 result
= variable_expand (line
);
458 current_variable_set_list
= save
;
460 file
->variables
->next
= fnext
;
465 /* Like variable_expand_for_file, but the returned string is malloc'd.
466 This function is called a lot. It wants to be efficient. */
469 allocated_variable_expand_for_file (line
, file
)
475 char *obuf
= variable_buffer
;
476 unsigned int olen
= variable_buffer_length
;
480 value
= variable_expand_for_file (line
, file
);
483 /* Waste a little memory and save time. */
484 value
= xrealloc (value
, strlen (value
))
487 variable_buffer
= obuf
;
488 variable_buffer_length
= olen
;