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. */
29 /* The next two describe the variable output buffer.
30 This buffer is used to hold the variable-expansion of a line of the
31 makefile. It is made bigger with realloc whenever it is too small.
32 variable_buffer_length is the size currently allocated.
33 variable_buffer is the address of the buffer.
35 For efficiency, it's guaranteed that the buffer will always have
36 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
37 extra chars without having to call a function. Note you should never use
38 these bytes unless you're _sure_ you have room (you know when the buffer
39 length was last checked. */
41 #define VARIABLE_BUFFER_ZONE 5
43 static unsigned int variable_buffer_length
;
44 char *variable_buffer
;
46 /* Subroutine of variable_expand and friends:
47 The text to add is LENGTH chars starting at STRING to the variable_buffer.
48 The text is added to the buffer at PTR, and the updated pointer into
49 the buffer is returned as the value. Thus, the value returned by
50 each call to variable_buffer_output should be the first argument to
51 the following call. */
54 variable_buffer_output (ptr
, string
, length
)
58 register unsigned int newlen
= length
+ (ptr
- variable_buffer
);
60 if ((newlen
+ VARIABLE_BUFFER_ZONE
) > variable_buffer_length
)
62 unsigned int offset
= ptr
- variable_buffer
;
63 variable_buffer_length
= (newlen
+ 100 > 2 * variable_buffer_length
65 : 2 * variable_buffer_length
);
66 variable_buffer
= (char *) xrealloc (variable_buffer
,
67 variable_buffer_length
);
68 ptr
= variable_buffer
+ offset
;
71 bcopy (string
, ptr
, length
);
75 /* Return a pointer to the beginning of the variable buffer. */
78 initialize_variable_output ()
80 /* If we don't have a variable output buffer yet, get one. */
82 if (variable_buffer
== 0)
84 variable_buffer_length
= 200;
85 variable_buffer
= (char *) xmalloc (variable_buffer_length
);
86 variable_buffer
[0] = '\0';
89 return variable_buffer
;
92 /* Recursively expand V. The returned string is malloc'd. */
94 static char *allocated_variable_append
PARAMS ((struct variable
*v
));
97 recursively_expand (v
)
98 register struct variable
*v
;
103 /* Expanding V causes infinite recursion. Lose. */
105 _("Recursive variable `%s' references itself (eventually)"),
110 value
= allocated_variable_append (v
);
112 value
= allocated_variable_expand (v
->value
);
118 /* Warn that NAME is an undefined variable. */
124 warn_undefined (name
, length
)
128 if (warn_undefined_variables_flag
)
130 _("warning: undefined variable `%.*s'"), (int)length
, name
);
133 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
139 reference_variable (o
, name
, length
)
144 register struct variable
*v
;
147 v
= lookup_variable (name
, length
);
150 warn_undefined (name
, length
);
152 if (v
== 0 || *v
->value
== '\0')
155 value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
157 o
= variable_buffer_output (o
, value
, strlen (value
));
165 /* Scan STRING for variable references and expansion-function calls. Only
166 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
167 a null byte is found.
169 Write the results to LINE, which must point into `variable_buffer'. If
170 LINE is NULL, start at the beginning of the buffer.
171 Return a pointer to LINE, or to the beginning of the buffer if LINE is
175 variable_expand_string (line
, string
, length
)
180 register struct variable
*v
;
181 register char *p
, *o
, *p1
;
182 char save_char
= '\0';
183 unsigned int line_offset
;
186 line
= initialize_variable_output();
190 line_offset
= line
- variable_buffer
;
194 save_char
= string
[length
];
195 string
[length
] = '\0';
200 /* Copy all following uninteresting chars all at once to the
201 variable output buffer, and skip them. Uninteresting chars end
202 at the next $ or the end of the input. */
204 p1
= strchr (p
, '$');
206 o
= variable_buffer_output (o
, p
, p1
!= 0 ? p1
- p
: strlen (p
) + 1);
212 /* Dispatch on the char that follows the $. */
217 /* $$ seen means output one $ to the variable output buffer. */
218 o
= variable_buffer_output (o
, p
, 1);
223 /* $(...) or ${...} is the general case of substitution. */
226 char closeparen
= (openparen
== '(') ? ')' : '}';
227 register char *beg
= p
+ 1;
234 if (handle_function (&op
, &begp
))
241 /* Is there a variable reference inside the parens or braces?
242 If so, expand it before expanding the entire reference. */
244 end
= strchr (beg
, closeparen
);
246 /* Unterminated variable reference. */
247 fatal (reading_file
, _("unterminated variable reference"));
248 p1
= lindex (beg
, end
, '$');
251 /* BEG now points past the opening paren or brace.
252 Count parens or braces until it is matched. */
254 for (p
= beg
; *p
!= '\0'; ++p
)
258 else if (*p
== closeparen
&& --count
< 0)
261 /* If COUNT is >= 0, there were unmatched opening parens
262 or braces, so we go to the simple case of a variable name
266 beg
= expand_argument (beg
, p
); /* Expand the name. */
267 free_beg
= 1; /* Remember to free BEG when finished. */
268 end
= strchr (beg
, '\0');
272 /* Advance P to the end of this reference. After we are
273 finished expanding this one, P will be incremented to
274 continue the scan. */
277 /* This is not a reference to a built-in function and
278 any variable references inside are now expanded.
279 Is the resultant text a substitution reference? */
281 colon
= lindex (beg
, end
, ':');
284 /* This looks like a substitution reference: $(FOO:A=B). */
285 char *subst_beg
, *subst_end
, *replace_beg
, *replace_end
;
287 subst_beg
= colon
+ 1;
288 subst_end
= strchr (subst_beg
, '=');
290 /* There is no = in sight. Punt on the substitution
291 reference and treat this as a variable name containing
292 a colon, in the code below. */
296 replace_beg
= subst_end
+ 1;
299 /* Extract the variable name before the colon
300 and look up that variable. */
301 v
= lookup_variable (beg
, colon
- beg
);
303 warn_undefined (beg
, colon
- beg
);
305 if (v
!= 0 && *v
->value
!= '\0')
307 char *value
= (v
->recursive
? recursively_expand (v
)
309 char *pattern
, *percent
;
317 pattern
= (char *) alloca (subst_end
- subst_beg
319 bcopy (subst_beg
, pattern
, subst_end
- subst_beg
);
320 pattern
[subst_end
- subst_beg
] = '\0';
322 percent
= find_percent (pattern
);
329 replace
= replace_beg
;
333 replace
= (char *) alloca (replace_end
336 bcopy (replace_beg
, replace
,
337 replace_end
- replace_beg
);
338 replace
[replace_end
- replace_beg
] = '\0';
341 o
= patsubst_expand (o
, value
, pattern
, replace
,
342 percent
, (char *) 0);
345 o
= subst_expand (o
, value
,
346 pattern
, replace_beg
,
357 /* This is an ordinary variable reference.
358 Look up the value of the variable. */
359 o
= reference_variable (o
, beg
, end
- beg
);
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. */
384 p1
= allocated_variable_expand (name
);
385 o
= variable_buffer_output (o
, p1
, strlen (p1
));
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. */
411 variable_expand (line
)
414 return variable_expand_string(NULL
, line
, (long)-1);
417 /* Expand an argument for an expansion function.
418 The text starting at STR and ending at END is variable-expanded
419 into a null-terminated string that is returned as the value.
420 This is done without clobbering `variable_buffer' or the current
421 variable-expansion that is in progress. */
424 expand_argument (str
, end
)
429 if (!end
|| *end
== '\0')
433 tmp
= (char *) alloca (end
- str
+ 1);
434 bcopy (str
, tmp
, end
- str
);
435 tmp
[end
- str
] = '\0';
438 return allocated_variable_expand (tmp
);
441 /* Expand LINE for FILE. Error messages refer to the file and line where
442 FILE's commands were found. Expansion uses FILE's variable set list. */
445 variable_expand_for_file (line
, file
)
447 register struct file
*file
;
450 struct variable_set_list
*save
;
453 return variable_expand (line
);
455 save
= current_variable_set_list
;
456 current_variable_set_list
= file
->variables
;
457 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
458 reading_file
= &file
->cmds
->fileinfo
;
461 result
= variable_expand (line
);
462 current_variable_set_list
= save
;
468 /* Like allocated_variable_expand, but we first expand this variable in the
469 context of the next variable set, then we append the expanded value. */
472 allocated_variable_append (v
)
475 struct variable_set_list
*save
;
476 int len
= strlen (v
->name
);
477 char *var
= alloca (len
+ 4);
480 char *obuf
= variable_buffer
;
481 unsigned int olen
= variable_buffer_length
;
485 assert(current_variable_set_list
->next
!= 0);
486 save
= current_variable_set_list
;
487 current_variable_set_list
= current_variable_set_list
->next
;
491 strcpy (&var
[2], v
->name
);
495 value
= variable_expand_for_file (var
, 0);
497 current_variable_set_list
= save
;
499 value
+= strlen (value
);
500 value
= variable_buffer_output (value
, " ", 1);
501 value
= variable_expand_string (value
, v
->value
, (long)-1);
503 value
= variable_buffer
;
506 /* Waste a little memory and save time. */
507 value
= xrealloc (value
, strlen (value
))
510 variable_buffer
= obuf
;
511 variable_buffer_length
= olen
;
516 /* Like variable_expand_for_file, but the returned string is malloc'd.
517 This function is called a lot. It wants to be efficient. */
520 allocated_variable_expand_for_file (line
, file
)
526 char *obuf
= variable_buffer
;
527 unsigned int olen
= variable_buffer_length
;
531 value
= variable_expand_for_file (line
, file
);
534 /* Waste a little memory and save time. */
535 value
= xrealloc (value
, strlen (value
))
538 variable_buffer
= obuf
;
539 variable_buffer_length
= olen
;