VirtualBox

source: kBuild/vendor/gnumake/2008-10-28/expand.c

Last change on this file was 1989, checked in by bird, 16 years ago

Load gnumake-2008-10-28-CVS into vendor/gnumake/current.

  • Property svn:eol-style set to native
File size: 16.4 KB
Line 
1/* Variable expansion functions for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include "filedef.h"
24#include "job.h"
25#include "commands.h"
26#include "variable.h"
27#include "rule.h"
28
29/* Initially, any errors reported when expanding strings will be reported
30 against the file where the error appears. */
31const struct floc **expanding_var = &reading_file;
32
33/* The next two describe the variable output buffer.
34 This buffer is used to hold the variable-expansion of a line of the
35 makefile. It is made bigger with realloc whenever it is too small.
36 variable_buffer_length is the size currently allocated.
37 variable_buffer is the address of the buffer.
38
39 For efficiency, it's guaranteed that the buffer will always have
40 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
41 extra chars without having to call a function. Note you should never use
42 these bytes unless you're _sure_ you have room (you know when the buffer
43 length was last checked. */
44
45#define VARIABLE_BUFFER_ZONE 5
46
47static unsigned int variable_buffer_length;
48char *variable_buffer;
49
50/* Subroutine of variable_expand and friends:
51 The text to add is LENGTH chars starting at STRING to the variable_buffer.
52 The text is added to the buffer at PTR, and the updated pointer into
53 the buffer is returned as the value. Thus, the value returned by
54 each call to variable_buffer_output should be the first argument to
55 the following call. */
56
57char *
58variable_buffer_output (char *ptr, const char *string, unsigned int length)
59{
60 register unsigned int newlen = length + (ptr - variable_buffer);
61
62 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
63 {
64 unsigned int offset = ptr - variable_buffer;
65 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
66 ? newlen + 100
67 : 2 * variable_buffer_length);
68 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
69 ptr = variable_buffer + offset;
70 }
71
72 memcpy (ptr, string, length);
73 return ptr + length;
74}
75
76/* Return a pointer to the beginning of the variable buffer. */
77
78static char *
79initialize_variable_output (void)
80{
81 /* If we don't have a variable output buffer yet, get one. */
82
83 if (variable_buffer == 0)
84 {
85 variable_buffer_length = 200;
86 variable_buffer = xmalloc (variable_buffer_length);
87 variable_buffer[0] = '\0';
88 }
89
90 return variable_buffer;
91}
92
93
94/* Recursively expand V. The returned string is malloc'd. */
95
96static char *allocated_variable_append (const struct variable *v);
97
98char *
99recursively_expand_for_file (struct variable *v, struct file *file)
100{
101 char *value;
102 const struct floc *this_var;
103 const struct floc **saved_varp;
104 struct variable_set_list *save = 0;
105 int set_reading = 0;
106
107 /* Don't install a new location if this location is empty.
108 This can happen for command-line variables, builtin variables, etc. */
109 saved_varp = expanding_var;
110 if (v->fileinfo.filenm)
111 {
112 this_var = &v->fileinfo;
113 expanding_var = &this_var;
114 }
115
116 /* If we have no other file-reading context, use the variable's context. */
117 if (!reading_file)
118 {
119 set_reading = 1;
120 reading_file = &v->fileinfo;
121 }
122
123 if (v->expanding)
124 {
125 if (!v->exp_count)
126 /* Expanding V causes infinite recursion. Lose. */
127 fatal (*expanding_var,
128 _("Recursive variable `%s' references itself (eventually)"),
129 v->name);
130 --v->exp_count;
131 }
132
133 if (file)
134 {
135 save = current_variable_set_list;
136 current_variable_set_list = file->variables;
137 }
138
139 v->expanding = 1;
140 if (v->append)
141 value = allocated_variable_append (v);
142 else
143 value = allocated_variable_expand (v->value);
144 v->expanding = 0;
145
146 if (set_reading)
147 reading_file = 0;
148
149 if (file)
150 current_variable_set_list = save;
151
152 expanding_var = saved_varp;
153
154 return value;
155}
156
157/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
158
159#ifdef __GNUC__
160__inline
161#endif
162static char *
163reference_variable (char *o, const char *name, unsigned int length)
164{
165 struct variable *v;
166 char *value;
167
168 v = lookup_variable (name, length);
169
170 if (v == 0)
171 warn_undefined (name, length);
172
173 /* If there's no variable by that name or it has no value, stop now. */
174 if (v == 0 || (*v->value == '\0' && !v->append))
175 return o;
176
177 value = (v->recursive ? recursively_expand (v) : v->value);
178
179 o = variable_buffer_output (o, value, strlen (value));
180
181 if (v->recursive)
182 free (value);
183
184 return o;
185}
186
187
188/* Scan STRING for variable references and expansion-function calls. Only
189 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
190 a null byte is found.
191
192 Write the results to LINE, which must point into `variable_buffer'. If
193 LINE is NULL, start at the beginning of the buffer.
194 Return a pointer to LINE, or to the beginning of the buffer if LINE is
195 NULL.
196 */
197char *
198variable_expand_string (char *line, const char *string, long length)
199{
200 struct variable *v;
201 const char *p, *p1;
202 char *abuf = NULL;
203 char *o;
204 unsigned int line_offset;
205
206 if (!line)
207 line = initialize_variable_output();
208 o = line;
209 line_offset = line - variable_buffer;
210
211 if (length == 0)
212 {
213 variable_buffer_output (o, "", 1);
214 return (variable_buffer);
215 }
216
217 /* If we want a subset of the string, allocate a temporary buffer for it.
218 Most of the functions we use here don't work with length limits. */
219 if (length > 0 && string[length] != '\0')
220 {
221 abuf = xmalloc(length+1);
222 memcpy(abuf, string, length);
223 abuf[length] = '\0';
224 string = abuf;
225 }
226 p = string;
227
228 while (1)
229 {
230 /* Copy all following uninteresting chars all at once to the
231 variable output buffer, and skip them. Uninteresting chars end
232 at the next $ or the end of the input. */
233
234 p1 = strchr (p, '$');
235
236 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
237
238 if (p1 == 0)
239 break;
240 p = p1 + 1;
241
242 /* Dispatch on the char that follows the $. */
243
244 switch (*p)
245 {
246 case '$':
247 /* $$ seen means output one $ to the variable output buffer. */
248 o = variable_buffer_output (o, p, 1);
249 break;
250
251 case '(':
252 case '{':
253 /* $(...) or ${...} is the general case of substitution. */
254 {
255 char openparen = *p;
256 char closeparen = (openparen == '(') ? ')' : '}';
257 const char *begp;
258 const char *beg = p + 1;
259 char *op;
260 char *abeg = NULL;
261 const char *end, *colon;
262
263 op = o;
264 begp = p;
265 if (handle_function (&op, &begp))
266 {
267 o = op;
268 p = begp;
269 break;
270 }
271
272 /* Is there a variable reference inside the parens or braces?
273 If so, expand it before expanding the entire reference. */
274
275 end = strchr (beg, closeparen);
276 if (end == 0)
277 /* Unterminated variable reference. */
278 fatal (*expanding_var, _("unterminated variable reference"));
279 p1 = lindex (beg, end, '$');
280 if (p1 != 0)
281 {
282 /* BEG now points past the opening paren or brace.
283 Count parens or braces until it is matched. */
284 int count = 0;
285 for (p = beg; *p != '\0'; ++p)
286 {
287 if (*p == openparen)
288 ++count;
289 else if (*p == closeparen && --count < 0)
290 break;
291 }
292 /* If COUNT is >= 0, there were unmatched opening parens
293 or braces, so we go to the simple case of a variable name
294 such as `$($(a)'. */
295 if (count < 0)
296 {
297 abeg = expand_argument (beg, p); /* Expand the name. */
298 beg = abeg;
299 end = strchr (beg, '\0');
300 }
301 }
302 else
303 /* Advance P to the end of this reference. After we are
304 finished expanding this one, P will be incremented to
305 continue the scan. */
306 p = end;
307
308 /* This is not a reference to a built-in function and
309 any variable references inside are now expanded.
310 Is the resultant text a substitution reference? */
311
312 colon = lindex (beg, end, ':');
313 if (colon)
314 {
315 /* This looks like a substitution reference: $(FOO:A=B). */
316 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
317
318 subst_beg = colon + 1;
319 subst_end = lindex (subst_beg, end, '=');
320 if (subst_end == 0)
321 /* There is no = in sight. Punt on the substitution
322 reference and treat this as a variable name containing
323 a colon, in the code below. */
324 colon = 0;
325 else
326 {
327 replace_beg = subst_end + 1;
328 replace_end = end;
329
330 /* Extract the variable name before the colon
331 and look up that variable. */
332 v = lookup_variable (beg, colon - beg);
333 if (v == 0)
334 warn_undefined (beg, colon - beg);
335
336 /* If the variable is not empty, perform the
337 substitution. */
338 if (v != 0 && *v->value != '\0')
339 {
340 char *pattern, *replace, *ppercent, *rpercent;
341 char *value = (v->recursive
342 ? recursively_expand (v)
343 : v->value);
344
345 /* Copy the pattern and the replacement. Add in an
346 extra % at the beginning to use in case there
347 isn't one in the pattern. */
348 pattern = alloca (subst_end - subst_beg + 2);
349 *(pattern++) = '%';
350 memcpy (pattern, subst_beg, subst_end - subst_beg);
351 pattern[subst_end - subst_beg] = '\0';
352
353 replace = alloca (replace_end - replace_beg + 2);
354 *(replace++) = '%';
355 memcpy (replace, replace_beg,
356 replace_end - replace_beg);
357 replace[replace_end - replace_beg] = '\0';
358
359 /* Look for %. Set the percent pointers properly
360 based on whether we find one or not. */
361 ppercent = find_percent (pattern);
362 if (ppercent)
363 {
364 ++ppercent;
365 rpercent = find_percent (replace);
366 if (rpercent)
367 ++rpercent;
368 }
369 else
370 {
371 ppercent = pattern;
372 rpercent = replace;
373 --pattern;
374 --replace;
375 }
376
377 o = patsubst_expand_pat (o, value, pattern, replace,
378 ppercent, rpercent);
379
380 if (v->recursive)
381 free (value);
382 }
383 }
384 }
385
386 if (colon == 0)
387 /* This is an ordinary variable reference.
388 Look up the value of the variable. */
389 o = reference_variable (o, beg, end - beg);
390
391 if (abeg)
392 free (abeg);
393 }
394 break;
395
396 case '\0':
397 break;
398
399 default:
400 if (isblank ((unsigned char)p[-1]))
401 break;
402
403 /* A $ followed by a random char is a variable reference:
404 $a is equivalent to $(a). */
405 o = reference_variable (o, p, 1);
406
407 break;
408 }
409
410 if (*p == '\0')
411 break;
412 else
413 ++p;
414 }
415
416 if (abuf)
417 free (abuf);
418
419 variable_buffer_output (o, "", 1);
420 return (variable_buffer + line_offset);
421}
422
423
424/* Scan LINE for variable references and expansion-function calls.
425 Build in `variable_buffer' the result of expanding the references and calls.
426 Return the address of the resulting string, which is null-terminated
427 and is valid only until the next time this function is called. */
428
429char *
430variable_expand (const char *line)
431{
432 return variable_expand_string(NULL, line, (long)-1);
433}
434
435
436/* Expand an argument for an expansion function.
437 The text starting at STR and ending at END is variable-expanded
438 into a null-terminated string that is returned as the value.
439 This is done without clobbering `variable_buffer' or the current
440 variable-expansion that is in progress. */
441
442char *
443expand_argument (const char *str, const char *end)
444{
445 char *tmp;
446
447 if (str == end)
448 return xstrdup("");
449
450 if (!end || *end == '\0')
451 return allocated_variable_expand (str);
452
453 tmp = alloca (end - str + 1);
454 memcpy (tmp, str, end - str);
455 tmp[end - str] = '\0';
456
457 return allocated_variable_expand (tmp);
458}
459
460
461/* Expand LINE for FILE. Error messages refer to the file and line where
462 FILE's commands were found. Expansion uses FILE's variable set list. */
463
464char *
465variable_expand_for_file (const char *line, struct file *file)
466{
467 char *result;
468 struct variable_set_list *savev;
469 const struct floc *savef;
470
471 if (file == 0)
472 return variable_expand (line);
473
474 savev = current_variable_set_list;
475 current_variable_set_list = file->variables;
476
477 savef = reading_file;
478 if (file->cmds && file->cmds->fileinfo.filenm)
479 reading_file = &file->cmds->fileinfo;
480 else
481 reading_file = 0;
482
483 result = variable_expand (line);
484
485 current_variable_set_list = savev;
486 reading_file = savef;
487
488 return result;
489}
490
491
492/* Like allocated_variable_expand, but for += target-specific variables.
493 First recursively construct the variable value from its appended parts in
494 any upper variable sets. Then expand the resulting value. */
495
496static char *
497variable_append (const char *name, unsigned int length,
498 const struct variable_set_list *set)
499{
500 const struct variable *v;
501 char *buf = 0;
502
503 /* If there's nothing left to check, return the empty buffer. */
504 if (!set)
505 return initialize_variable_output ();
506
507 /* Try to find the variable in this variable set. */
508 v = lookup_variable_in_set (name, length, set->set);
509
510 /* If there isn't one, look to see if there's one in a set above us. */
511 if (!v)
512 return variable_append (name, length, set->next);
513
514 /* If this variable type is append, first get any upper values.
515 If not, initialize the buffer. */
516 if (v->append)
517 buf = variable_append (name, length, set->next);
518 else
519 buf = initialize_variable_output ();
520
521 /* Append this value to the buffer, and return it.
522 If we already have a value, first add a space. */
523 if (buf > variable_buffer)
524 buf = variable_buffer_output (buf, " ", 1);
525
526 /* Either expand it or copy it, depending. */
527 if (! v->recursive)
528 return variable_buffer_output (buf, v->value, strlen (v->value));
529
530 buf = variable_expand_string (buf, v->value, strlen (v->value));
531 return (buf + strlen (buf));
532}
533
534
535static char *
536allocated_variable_append (const struct variable *v)
537{
538 char *val;
539
540 /* Construct the appended variable value. */
541
542 char *obuf = variable_buffer;
543 unsigned int olen = variable_buffer_length;
544
545 variable_buffer = 0;
546
547 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
548 variable_buffer_output (val, "", 1);
549 val = variable_buffer;
550
551 variable_buffer = obuf;
552 variable_buffer_length = olen;
553
554 return val;
555}
556
557/* Like variable_expand_for_file, but the returned string is malloc'd.
558 This function is called a lot. It wants to be efficient. */
559
560char *
561allocated_variable_expand_for_file (const char *line, struct file *file)
562{
563 char *value;
564
565 char *obuf = variable_buffer;
566 unsigned int olen = variable_buffer_length;
567
568 variable_buffer = 0;
569
570 value = variable_expand_for_file (line, file);
571
572#if 0
573 /* Waste a little memory and save time. */
574 value = xrealloc (value, strlen (value))
575#endif
576
577 variable_buffer = obuf;
578 variable_buffer_length = olen;
579
580 return value;
581}
582
583/* Install a new variable_buffer context, returning the current one for
584 safe-keeping. */
585
586void
587install_variable_buffer (char **bufp, unsigned int *lenp)
588{
589 *bufp = variable_buffer;
590 *lenp = variable_buffer_length;
591
592 variable_buffer = 0;
593 initialize_variable_output ();
594}
595
596/* Restore a previously-saved variable_buffer setting (free the current one).
597 */
598
599void
600restore_variable_buffer (char *buf, unsigned int len)
601{
602 free (variable_buffer);
603
604 variable_buffer = buf;
605 variable_buffer_length = len;
606}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette