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. */
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. */
55 variable_buffer_output (ptr
, string
, length
)
59 register unsigned int newlen
= length
+ (ptr
- variable_buffer
);
61 if ((newlen
+ VARIABLE_BUFFER_ZONE
) > variable_buffer_length
)
63 unsigned int offset
= ptr
- variable_buffer
;
64 variable_buffer_length
= (newlen
+ 100 > 2 * variable_buffer_length
66 : 2 * variable_buffer_length
);
67 variable_buffer
= (char *) xrealloc (variable_buffer
,
68 variable_buffer_length
);
69 ptr
= variable_buffer
+ offset
;
72 bcopy (string
, ptr
, length
);
76 /* Return a pointer to the beginning of the variable buffer. */
79 initialize_variable_output ()
81 /* If we don't have a variable output buffer yet, get one. */
83 if (variable_buffer
== 0)
85 variable_buffer_length
= 200;
86 variable_buffer
= (char *) xmalloc (variable_buffer_length
);
87 variable_buffer
[0] = '\0';
90 return variable_buffer
;
93 /* Recursively expand V. The returned string is malloc'd. */
95 static char *allocated_variable_append
PARAMS ((struct variable
*v
));
98 recursively_expand (v
)
99 register struct variable
*v
;
104 /* Expanding V causes infinite recursion. Lose. */
106 _("Recursive variable `%s' references itself (eventually)"),
111 value
= allocated_variable_append (v
);
113 value
= allocated_variable_expand (v
->value
);
119 /* Warn that NAME is an undefined variable. */
125 warn_undefined (name
, length
)
129 if (warn_undefined_variables_flag
)
131 _("warning: undefined variable `%.*s'"), (int)length
, name
);
134 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
140 reference_variable (o
, name
, length
)
145 register struct variable
*v
;
148 v
= lookup_variable (name
, length
);
151 warn_undefined (name
, length
);
153 if (v
== 0 || *v
->value
== '\0')
156 value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
158 o
= variable_buffer_output (o
, value
, strlen (value
));
166 /* Scan STRING for variable references and expansion-function calls. Only
167 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
168 a null byte is found.
170 Write the results to LINE, which must point into `variable_buffer'. If
171 LINE is NULL, start at the beginning of the buffer.
172 Return a pointer to LINE, or to the beginning of the buffer if LINE is
176 variable_expand_string (line
, string
, length
)
181 register struct variable
*v
;
182 register char *p
, *o
, *p1
;
183 char save_char
= '\0';
184 unsigned int line_offset
;
187 line
= initialize_variable_output();
191 line_offset
= line
- variable_buffer
;
195 save_char
= string
[length
];
196 string
[length
] = '\0';
201 /* Copy all following uninteresting chars all at once to the
202 variable output buffer, and skip them. Uninteresting chars end
203 at the next $ or the end of the input. */
205 p1
= strchr (p
, '$');
207 o
= variable_buffer_output (o
, p
, p1
!= 0 ? p1
- p
: strlen (p
) + 1);
213 /* Dispatch on the char that follows the $. */
218 /* $$ seen means output one $ to the variable output buffer. */
219 o
= variable_buffer_output (o
, p
, 1);
224 /* $(...) or ${...} is the general case of substitution. */
227 char closeparen
= (openparen
== '(') ? ')' : '}';
228 register char *beg
= p
+ 1;
235 if (handle_function (&op
, &begp
))
242 /* Is there a variable reference inside the parens or braces?
243 If so, expand it before expanding the entire reference. */
245 end
= strchr (beg
, closeparen
);
247 /* Unterminated variable reference. */
248 fatal (reading_file
, _("unterminated variable reference"));
249 p1
= lindex (beg
, end
, '$');
252 /* BEG now points past the opening paren or brace.
253 Count parens or braces until it is matched. */
255 for (p
= beg
; *p
!= '\0'; ++p
)
259 else if (*p
== closeparen
&& --count
< 0)
262 /* If COUNT is >= 0, there were unmatched opening parens
263 or braces, so we go to the simple case of a variable name
267 beg
= expand_argument (beg
, p
); /* Expand the name. */
268 free_beg
= 1; /* Remember to free BEG when finished. */
269 end
= strchr (beg
, '\0');
273 /* Advance P to the end of this reference. After we are
274 finished expanding this one, P will be incremented to
275 continue the scan. */
278 /* This is not a reference to a built-in function and
279 any variable references inside are now expanded.
280 Is the resultant text a substitution reference? */
282 colon
= lindex (beg
, end
, ':');
285 /* This looks like a substitution reference: $(FOO:A=B). */
286 char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
288 subst_beg
= colon
+ 1;
289 subst_end
= strchr (subst_beg
, '=');
291 /* There is no = in sight. Punt on the substitution
292 reference and treat this as a variable name containing
293 a colon, in the code below. */
297 replace_beg
= subst_end
+ 1;
300 /* Extract the variable name before the colon
301 and look up that variable. */
302 v
= lookup_variable (beg
, colon
- beg
);
304 warn_undefined (beg
, colon
- beg
);
306 if (v
!= 0 && *v
->value
!= '\0')
308 char *value
= (v
->recursive
? recursively_expand (v
)
310 char *pattern
, *percent
;
318 pattern
= (char *) alloca (subst_end
- subst_beg
320 bcopy (subst_beg
, pattern
, subst_end
- subst_beg
);
321 pattern
[subst_end
- subst_beg
] = '\0';
323 percent
= find_percent (pattern
);
330 replace
= replace_beg
;
334 replace
= (char *) alloca (replace_end
337 bcopy (replace_beg
, replace
,
338 replace_end
- replace_beg
);
339 replace
[replace_end
- replace_beg
] = '\0';
342 o
= patsubst_expand (o
, value
, pattern
, replace
,
343 percent
, (char *) 0);
346 o
= subst_expand (o
, value
,
347 pattern
, replace_beg
,
358 /* This is an ordinary variable reference.
359 Look up the value of the variable. */
360 o
= reference_variable (o
, beg
, end
- beg
);
371 if (isblank ((unsigned char)p
[-1]))
374 /* A $ followed by a random char is a variable reference:
375 $a is equivalent to $(a). */
377 /* We could do the expanding here, but this way
378 avoids code repetition at a small performance cost. */
385 p1
= allocated_variable_expand (name
);
386 o
= variable_buffer_output (o
, p1
, strlen (p1
));
400 string
[length
] = save_char
;
402 (void)variable_buffer_output (o
, "", 1);
403 return (variable_buffer
+ line_offset
);
406 /* Scan LINE for variable references and expansion-function calls.
407 Build in `variable_buffer' the result of expanding the references and calls.
408 Return the address of the resulting string, which is null-terminated
409 and is valid only until the next time this function is called. */
412 variable_expand (line
)
415 return variable_expand_string(NULL
, line
, (long)-1);
418 /* Expand an argument for an expansion function.
419 The text starting at STR and ending at END is variable-expanded
420 into a null-terminated string that is returned as the value.
421 This is done without clobbering `variable_buffer' or the current
422 variable-expansion that is in progress. */
425 expand_argument (str
, end
)
433 if (!end
|| *end
== '\0')
437 tmp
= (char *) alloca (end
- str
+ 1);
438 bcopy (str
, tmp
, end
- str
);
439 tmp
[end
- str
] = '\0';
442 return allocated_variable_expand (tmp
);
445 /* Expand LINE for FILE. Error messages refer to the file and line where
446 FILE's commands were found. Expansion uses FILE's variable set list. */
449 variable_expand_for_file (line
, file
)
451 register struct file
*file
;
454 struct variable_set_list
*save
;
457 return variable_expand (line
);
459 save
= current_variable_set_list
;
460 current_variable_set_list
= file
->variables
;
461 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
462 reading_file
= &file
->cmds
->fileinfo
;
465 result
= variable_expand (line
);
466 current_variable_set_list
= save
;
472 /* Like allocated_variable_expand, but we first expand this variable in the
473 context of the next variable set, then we append the expanded value. */
476 allocated_variable_append (v
)
479 struct variable_set_list
*save
;
480 int len
= strlen (v
->name
);
481 char *var
= alloca (len
+ 4);
484 char *obuf
= variable_buffer
;
485 unsigned int olen
= variable_buffer_length
;
489 assert(current_variable_set_list
->next
!= 0);
490 save
= current_variable_set_list
;
491 current_variable_set_list
= current_variable_set_list
->next
;
495 strcpy (&var
[2], v
->name
);
499 value
= variable_expand_for_file (var
, 0);
501 current_variable_set_list
= save
;
503 value
+= strlen (value
);
504 value
= variable_buffer_output (value
, " ", 1);
505 value
= variable_expand_string (value
, v
->value
, (long)-1);
507 value
= variable_buffer
;
510 /* Waste a little memory and save time. */
511 value
= xrealloc (value
, strlen (value
))
514 variable_buffer
= obuf
;
515 variable_buffer_length
= olen
;
520 /* Like variable_expand_for_file, but the returned string is malloc'd.
521 This function is called a lot. It wants to be efficient. */
524 allocated_variable_expand_for_file (line
, file
)
530 char *obuf
= variable_buffer
;
531 unsigned int olen
= variable_buffer_length
;
535 value
= variable_expand_for_file (line
, file
);
538 /* Waste a little memory and save time. */
539 value
= xrealloc (value
, strlen (value
))
542 variable_buffer
= obuf
;
543 variable_buffer_length
= olen
;