VirtualBox

source: kBuild/vendor/gnumake/2007-05-23/expand.c

Last change on this file was 900, checked in by bird, 17 years ago

Load /home/bird/src/Gnu/make/2007-05-23 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 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 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
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 *save;
469
470 if (file == 0)
471 return variable_expand (line);
472
473 save = current_variable_set_list;
474 current_variable_set_list = file->variables;
475 if (file->cmds && file->cmds->fileinfo.filenm)
476 reading_file = &file->cmds->fileinfo;
477 else
478 reading_file = 0;
479 result = variable_expand (line);
480 current_variable_set_list = save;
481 reading_file = 0;
482
483 return result;
484}
485
486
487/* Like allocated_variable_expand, but for += target-specific variables.
488 First recursively construct the variable value from its appended parts in
489 any upper variable sets. Then expand the resulting value. */
490
491static char *
492variable_append (const char *name, unsigned int length,
493 const struct variable_set_list *set)
494{
495 const struct variable *v;
496 char *buf = 0;
497
498 /* If there's nothing left to check, return the empty buffer. */
499 if (!set)
500 return initialize_variable_output ();
501
502 /* Try to find the variable in this variable set. */
503 v = lookup_variable_in_set (name, length, set->set);
504
505 /* If there isn't one, look to see if there's one in a set above us. */
506 if (!v)
507 return variable_append (name, length, set->next);
508
509 /* If this variable type is append, first get any upper values.
510 If not, initialize the buffer. */
511 if (v->append)
512 buf = variable_append (name, length, set->next);
513 else
514 buf = initialize_variable_output ();
515
516 /* Append this value to the buffer, and return it.
517 If we already have a value, first add a space. */
518 if (buf > variable_buffer)
519 buf = variable_buffer_output (buf, " ", 1);
520
521 /* Either expand it or copy it, depending. */
522 if (! v->recursive)
523 return variable_buffer_output (buf, v->value, strlen (v->value));
524
525 buf = variable_expand_string (buf, v->value, strlen (v->value));
526 return (buf + strlen (buf));
527}
528
529
530static char *
531allocated_variable_append (const struct variable *v)
532{
533 char *val;
534
535 /* Construct the appended variable value. */
536
537 char *obuf = variable_buffer;
538 unsigned int olen = variable_buffer_length;
539
540 variable_buffer = 0;
541
542 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
543 variable_buffer_output (val, "", 1);
544 val = variable_buffer;
545
546 variable_buffer = obuf;
547 variable_buffer_length = olen;
548
549 return val;
550}
551
552/* Like variable_expand_for_file, but the returned string is malloc'd.
553 This function is called a lot. It wants to be efficient. */
554
555char *
556allocated_variable_expand_for_file (const char *line, struct file *file)
557{
558 char *value;
559
560 char *obuf = variable_buffer;
561 unsigned int olen = variable_buffer_length;
562
563 variable_buffer = 0;
564
565 value = variable_expand_for_file (line, file);
566
567#if 0
568 /* Waste a little memory and save time. */
569 value = xrealloc (value, strlen (value))
570#endif
571
572 variable_buffer = obuf;
573 variable_buffer_length = olen;
574
575 return value;
576}
577
578/* Install a new variable_buffer context, returning the current one for
579 safe-keeping. */
580
581void
582install_variable_buffer (char **bufp, unsigned int *lenp)
583{
584 *bufp = variable_buffer;
585 *lenp = variable_buffer_length;
586
587 variable_buffer = 0;
588 initialize_variable_output ();
589}
590
591/* Restore a previously-saved variable_buffer setting (free the current one).
592 */
593
594void
595restore_variable_buffer (char *buf, unsigned int len)
596{
597 free (variable_buffer);
598
599 variable_buffer = buf;
600 variable_buffer_length = len;
601}
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