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 (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
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
);
74 /* Return a pointer to the beginning of the variable buffer. */
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
));
96 recursively_expand_for_file (struct variable
*v
, struct file
*file
)
99 struct variable_set_list
*save
= 0;
105 /* Expanding V causes infinite recursion. Lose. */
107 _("Recursive variable `%s' references itself (eventually)"),
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. */
122 reading_file
= &v
->fileinfo
;
127 value
= allocated_variable_append (v
);
129 value
= allocated_variable_expand (v
->value
);
135 current_variable_set_list
= save
;
140 /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
146 reference_variable (char *o
, char *name
, unsigned int length
)
148 register struct variable
*v
;
151 v
= lookup_variable (name
, length
);
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
))
160 value
= (v
->recursive
? recursively_expand (v
) : v
->value
);
162 o
= variable_buffer_output (o
, value
, strlen (value
));
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
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
;
188 line
= initialize_variable_output();
192 line_offset
= line
- variable_buffer
;
196 save_char
= string
[length
];
197 string
[length
] = '\0';
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);
214 /* Dispatch on the char that follows the $. */
219 /* $$ seen means output one $ to the variable output buffer. */
220 o
= variable_buffer_output (o
, p
, 1);
225 /* $(...) or ${...} is the general case of substitution. */
228 char closeparen
= (openparen
== '(') ? ')' : '}';
229 register char *beg
= p
+ 1;
236 if (handle_function (&op
, &begp
))
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
);
248 /* Unterminated variable reference. */
249 fatal (reading_file
, _("unterminated variable reference"));
250 p1
= lindex (beg
, end
, '$');
253 /* BEG now points past the opening paren or brace.
254 Count parens or braces until it is matched. */
256 for (p
= beg
; *p
!= '\0'; ++p
)
260 else if (*p
== closeparen
&& --count
< 0)
263 /* If COUNT is >= 0, there were unmatched opening parens
264 or braces, so we go to the simple case of a variable name
268 beg
= expand_argument (beg
, p
); /* Expand the name. */
269 free_beg
= 1; /* Remember to free BEG when finished. */
270 end
= strchr (beg
, '\0');
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. */
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
, ':');
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
, '=');
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. */
298 replace_beg
= subst_end
+ 1;
301 /* Extract the variable name before the colon
302 and look up that variable. */
303 v
= lookup_variable (beg
, colon
- beg
);
305 warn_undefined (beg
, colon
- beg
);
307 if (v
!= 0 && *v
->value
!= '\0')
309 char *value
= (v
->recursive
? recursively_expand (v
)
311 char *pattern
, *percent
;
319 pattern
= (char *) alloca (subst_end
- subst_beg
321 bcopy (subst_beg
, pattern
, subst_end
- subst_beg
);
322 pattern
[subst_end
- subst_beg
] = '\0';
324 percent
= find_percent (pattern
);
331 replace
= replace_beg
;
335 replace
= (char *) alloca (replace_end
338 bcopy (replace_beg
, replace
,
339 replace_end
- replace_beg
);
340 replace
[replace_end
- replace_beg
] = '\0';
343 o
= patsubst_expand (o
, value
, pattern
, replace
,
344 percent
, (char *) 0);
347 o
= subst_expand (o
, value
,
348 pattern
, replace_beg
,
359 /* This is an ordinary variable reference.
360 Look up the value of the variable. */
361 o
= reference_variable (o
, beg
, end
- beg
);
372 if (isblank ((unsigned char)p
[-1]))
375 /* A $ followed by a random char is a variable reference:
376 $a is equivalent to $(a). */
378 /* We could do the expanding here, but this way
379 avoids code repetition at a small performance cost. */
386 p1
= allocated_variable_expand (name
);
387 o
= variable_buffer_output (o
, p1
, strlen (p1
));
401 string
[length
] = save_char
;
403 (void)variable_buffer_output (o
, "", 1);
404 return (variable_buffer
+ line_offset
);
407 /* Scan LINE for variable references and expansion-function calls.
408 Build in `variable_buffer' the result of expanding the references and calls.
409 Return the address of the resulting string, which is null-terminated
410 and is valid only until the next time this function is called. */
413 variable_expand (char *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 (const char *str
, const char *end
)
432 if (!end
|| *end
== '\0')
433 return allocated_variable_expand ((char *)str
);
435 tmp
= (char *) alloca (end
- str
+ 1);
436 bcopy (str
, tmp
, end
- str
);
437 tmp
[end
- str
] = '\0';
439 return allocated_variable_expand (tmp
);
442 /* Expand LINE for FILE. Error messages refer to the file and line where
443 FILE's commands were found. Expansion uses FILE's variable set list. */
446 variable_expand_for_file (char *line
, struct file
*file
)
449 struct variable_set_list
*save
;
452 return variable_expand (line
);
454 save
= current_variable_set_list
;
455 current_variable_set_list
= file
->variables
;
456 if (file
->cmds
&& file
->cmds
->fileinfo
.filenm
)
457 reading_file
= &file
->cmds
->fileinfo
;
460 result
= variable_expand (line
);
461 current_variable_set_list
= save
;
467 /* Like allocated_variable_expand, but for += target-specific variables.
468 First recursively construct the variable value from its appended parts in
469 any upper variable sets. Then expand the resulting value. */
472 variable_append (const char *name
, unsigned int length
,
473 const struct variable_set_list
*set
)
475 const struct variable
*v
;
478 /* If there's nothing left to check, return the empty buffer. */
480 return initialize_variable_output ();
482 /* Try to find the variable in this variable set. */
483 v
= lookup_variable_in_set (name
, length
, set
->set
);
485 /* If there isn't one, look to see if there's one in a set above us. */
487 return variable_append (name
, length
, set
->next
);
489 /* If this variable type is append, first get any upper values.
490 If not, initialize the buffer. */
492 buf
= variable_append (name
, length
, set
->next
);
494 buf
= initialize_variable_output ();
496 /* Append this value to the buffer, and return it.
497 If we already have a value, first add a space. */
498 if (buf
> variable_buffer
)
499 buf
= variable_buffer_output (buf
, " ", 1);
501 return variable_buffer_output (buf
, v
->value
, strlen (v
->value
));
506 allocated_variable_append (const struct variable
*v
)
510 /* Construct the appended variable value. */
512 char *obuf
= variable_buffer
;
513 unsigned int olen
= variable_buffer_length
;
517 val
= variable_append (v
->name
, strlen (v
->name
), current_variable_set_list
);
518 variable_buffer_output (val
, "", 1);
519 val
= variable_buffer
;
521 variable_buffer
= obuf
;
522 variable_buffer_length
= olen
;
524 /* Now expand it and return that. */
526 retval
= allocated_variable_expand (val
);
532 /* Like variable_expand_for_file, but the returned string is malloc'd.
533 This function is called a lot. It wants to be efficient. */
536 allocated_variable_expand_for_file (char *line
, struct file
*file
)
540 char *obuf
= variable_buffer
;
541 unsigned int olen
= variable_buffer_length
;
545 value
= variable_expand_for_file (line
, file
);
548 /* Waste a little memory and save time. */
549 value
= xrealloc (value
, strlen (value
))
552 variable_buffer
= obuf
;
553 variable_buffer_length
= olen
;
558 /* Install a new variable_buffer context, returning the current one for
562 install_variable_buffer (char **bufp
, unsigned int *lenp
)
564 *bufp
= variable_buffer
;
565 *lenp
= variable_buffer_length
;
568 initialize_variable_output ();
571 /* Restore a previously-saved variable_buffer setting (free the current one).
575 restore_variable_buffer (char *buf
, unsigned int len
)
577 free (variable_buffer
);
579 variable_buffer
= buf
;
580 variable_buffer_length
= len
;