VirtualBox

source: kBuild/trunk/src/kmk/expand.c@ 1458

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

Use the variable file location in evalval and evalcall.

  • Property svn:eol-style set to native
File size: 19.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#ifdef KMK
66 variable_buffer_length = variable_buffer_length <= 1024
67 ? 2048 : variable_buffer_length * 4;
68 if (variable_buffer_length < newlen + 100)
69 variable_buffer_length = (newlen + 100 + 1023) & ~1023U;
70#else
71 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
72 ? newlen + 100
73 : 2 * variable_buffer_length);
74#endif
75 variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
76 ptr = variable_buffer + offset;
77 }
78
79 memcpy (ptr, string, length);
80 return ptr + length;
81}
82
83/* Return a pointer to the beginning of the variable buffer. */
84
85static char *
86initialize_variable_output (void)
87{
88 /* If we don't have a variable output buffer yet, get one. */
89
90 if (variable_buffer == 0)
91 {
92#ifdef KMK
93 variable_buffer_length = 384;
94#else
95 variable_buffer_length = 200;
96#endif
97 variable_buffer = xmalloc (variable_buffer_length);
98 variable_buffer[0] = '\0';
99 }
100
101 return variable_buffer;
102}
103
104
105/* Recursively expand V. The returned string is malloc'd. */
106
107static char *allocated_variable_append (const struct variable *v);
108
109char *
110recursively_expand_for_file (struct variable *v, struct file *file)
111{
112 char *value;
113 const struct floc *this_var;
114 const struct floc **saved_varp;
115 struct variable_set_list *save = 0;
116 int set_reading = 0;
117
118 /* Don't install a new location if this location is empty.
119 This can happen for command-line variables, builtin variables, etc. */
120 saved_varp = expanding_var;
121 if (v->fileinfo.filenm)
122 {
123 this_var = &v->fileinfo;
124 expanding_var = &this_var;
125 }
126
127 /* If we have no other file-reading context, use the variable's context. */
128 if (!reading_file)
129 {
130 set_reading = 1;
131 reading_file = &v->fileinfo;
132 }
133
134 if (v->expanding)
135 {
136 if (!v->exp_count)
137 /* Expanding V causes infinite recursion. Lose. */
138 fatal (*expanding_var,
139 _("Recursive variable `%s' references itself (eventually)"),
140 v->name);
141 --v->exp_count;
142 }
143
144 if (file)
145 {
146 save = current_variable_set_list;
147 current_variable_set_list = file->variables;
148 }
149
150 v->expanding = 1;
151 if (v->append)
152 value = allocated_variable_append (v);
153 else
154 value = allocated_variable_expand (v->value);
155 v->expanding = 0;
156
157 if (set_reading)
158 reading_file = 0;
159
160 if (file)
161 current_variable_set_list = save;
162
163 expanding_var = saved_varp;
164
165 return value;
166}
167
168/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
169
170#if defined(__GNUC__) || defined(_MSC_VER) /* bird added MSC */
171__inline
172#endif
173static char *
174reference_variable (char *o, const char *name, unsigned int length)
175{
176 struct variable *v;
177 char *value;
178
179 v = lookup_variable (name, length);
180
181 if (v == 0)
182 warn_undefined (name, length);
183
184 /* If there's no variable by that name or it has no value, stop now. */
185 if (v == 0 || (*v->value == '\0' && !v->append))
186 return o;
187
188 value = (v->recursive ? recursively_expand (v) : v->value);
189
190 o = variable_buffer_output (o, value, strlen (value));
191
192 if (v->recursive)
193 free (value);
194
195 return o;
196}
197
198
199/* Scan STRING for variable references and expansion-function calls. Only
200 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
201 a null byte is found.
202
203 Write the results to LINE, which must point into `variable_buffer'. If
204 LINE is NULL, start at the beginning of the buffer.
205 Return a pointer to LINE, or to the beginning of the buffer if LINE is
206 NULL.
207 */
208char *
209variable_expand_string (char *line, const char *string, long length)
210{
211 struct variable *v;
212 const char *p, *p1;
213 char *abuf = NULL;
214 char *o;
215 unsigned int line_offset;
216
217 if (!line)
218 line = initialize_variable_output();
219 o = line;
220 line_offset = line - variable_buffer;
221
222 if (length == 0)
223 {
224 variable_buffer_output (o, "", 1);
225 return (variable_buffer);
226 }
227
228 /* If we want a subset of the string, allocate a temporary buffer for it.
229 Most of the functions we use here don't work with length limits. */
230 if (length > 0 && string[length] != '\0')
231 {
232 abuf = xmalloc(length+1);
233 memcpy(abuf, string, length);
234 abuf[length] = '\0';
235 string = abuf;
236 }
237 p = string;
238
239 while (1)
240 {
241 /* Copy all following uninteresting chars all at once to the
242 variable output buffer, and skip them. Uninteresting chars end
243 at the next $ or the end of the input. */
244
245 p1 = strchr (p, '$');
246
247 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
248
249 if (p1 == 0)
250 break;
251 p = p1 + 1;
252
253 /* Dispatch on the char that follows the $. */
254
255 switch (*p)
256 {
257 case '$':
258 /* $$ seen means output one $ to the variable output buffer. */
259 o = variable_buffer_output (o, p, 1);
260 break;
261
262 case '(':
263 case '{':
264 /* $(...) or ${...} is the general case of substitution. */
265 {
266 char openparen = *p;
267 char closeparen = (openparen == '(') ? ')' : '}';
268 const char *begp;
269 const char *beg = p + 1;
270 char *op;
271 char *abeg = NULL;
272 const char *end, *colon;
273
274 op = o;
275 begp = p;
276 if (handle_function (&op, &begp))
277 {
278 o = op;
279 p = begp;
280 break;
281 }
282
283 /* Is there a variable reference inside the parens or braces?
284 If so, expand it before expanding the entire reference. */
285
286 end = strchr (beg, closeparen);
287 if (end == 0)
288 /* Unterminated variable reference. */
289 fatal (*expanding_var, _("unterminated variable reference"));
290 p1 = lindex (beg, end, '$');
291 if (p1 != 0)
292 {
293 /* BEG now points past the opening paren or brace.
294 Count parens or braces until it is matched. */
295 int count = 0;
296 for (p = beg; *p != '\0'; ++p)
297 {
298 if (*p == openparen)
299 ++count;
300 else if (*p == closeparen && --count < 0)
301 break;
302 }
303 /* If COUNT is >= 0, there were unmatched opening parens
304 or braces, so we go to the simple case of a variable name
305 such as `$($(a)'. */
306 if (count < 0)
307 {
308 abeg = expand_argument (beg, p); /* Expand the name. */
309 beg = abeg;
310 end = strchr (beg, '\0');
311 }
312 }
313 else
314 /* Advance P to the end of this reference. After we are
315 finished expanding this one, P will be incremented to
316 continue the scan. */
317 p = end;
318
319 /* This is not a reference to a built-in function and
320 any variable references inside are now expanded.
321 Is the resultant text a substitution reference? */
322
323 colon = lindex (beg, end, ':');
324 if (colon)
325 {
326 /* This looks like a substitution reference: $(FOO:A=B). */
327 const char *subst_beg, *subst_end, *replace_beg, *replace_end;
328
329 subst_beg = colon + 1;
330 subst_end = lindex (subst_beg, end, '=');
331 if (subst_end == 0)
332 /* There is no = in sight. Punt on the substitution
333 reference and treat this as a variable name containing
334 a colon, in the code below. */
335 colon = 0;
336 else
337 {
338 replace_beg = subst_end + 1;
339 replace_end = end;
340
341 /* Extract the variable name before the colon
342 and look up that variable. */
343 v = lookup_variable (beg, colon - beg);
344 if (v == 0)
345 warn_undefined (beg, colon - beg);
346
347 /* If the variable is not empty, perform the
348 substitution. */
349 if (v != 0 && *v->value != '\0')
350 {
351 char *pattern, *replace, *ppercent, *rpercent;
352 char *value = (v->recursive
353 ? recursively_expand (v)
354 : v->value);
355
356 /* Copy the pattern and the replacement. Add in an
357 extra % at the beginning to use in case there
358 isn't one in the pattern. */
359 pattern = alloca (subst_end - subst_beg + 2);
360 *(pattern++) = '%';
361 memcpy (pattern, subst_beg, subst_end - subst_beg);
362 pattern[subst_end - subst_beg] = '\0';
363
364 replace = alloca (replace_end - replace_beg + 2);
365 *(replace++) = '%';
366 memcpy (replace, replace_beg,
367 replace_end - replace_beg);
368 replace[replace_end - replace_beg] = '\0';
369
370 /* Look for %. Set the percent pointers properly
371 based on whether we find one or not. */
372 ppercent = find_percent (pattern);
373 if (ppercent)
374 {
375 ++ppercent;
376 rpercent = find_percent (replace);
377 if (rpercent)
378 ++rpercent;
379 }
380 else
381 {
382 ppercent = pattern;
383 rpercent = replace;
384 --pattern;
385 --replace;
386 }
387
388 o = patsubst_expand_pat (o, value, pattern, replace,
389 ppercent, rpercent);
390
391 if (v->recursive)
392 free (value);
393 }
394 }
395 }
396
397 if (colon == 0)
398 /* This is an ordinary variable reference.
399 Look up the value of the variable. */
400 o = reference_variable (o, beg, end - beg);
401
402 if (abeg)
403 free (abeg);
404 }
405 break;
406
407 case '\0':
408 break;
409
410 default:
411 if (isblank ((unsigned char)p[-1]))
412 break;
413
414 /* A $ followed by a random char is a variable reference:
415 $a is equivalent to $(a). */
416 o = reference_variable (o, p, 1);
417
418 break;
419 }
420
421 if (*p == '\0')
422 break;
423 else
424 ++p;
425 }
426
427 if (abuf)
428 free (abuf);
429
430 variable_buffer_output (o, "", 1);
431 return (variable_buffer + line_offset);
432}
433
434
435/* Scan LINE for variable references and expansion-function calls.
436 Build in `variable_buffer' the result of expanding the references and calls.
437 Return the address of the resulting string, which is null-terminated
438 and is valid only until the next time this function is called. */
439
440char *
441variable_expand (const char *line)
442{
443 return variable_expand_string(NULL, line, (long)-1);
444}
445
446
447/* Expand an argument for an expansion function.
448 The text starting at STR and ending at END is variable-expanded
449 into a null-terminated string that is returned as the value.
450 This is done without clobbering `variable_buffer' or the current
451 variable-expansion that is in progress. */
452
453char *
454expand_argument (const char *str, const char *end)
455{
456 char *tmp;
457
458 if (str == end)
459 return xstrdup("");
460
461 if (!end || *end == '\0')
462 return allocated_variable_expand (str);
463
464#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
465 {
466 const char saved_char = *end;
467 *(char *)end = '\0';
468 tmp = allocated_variable_expand ((char *)str);
469 *(char *)end = saved_char;
470 return tmp;
471 }
472#else
473 tmp = alloca (end - str + 1);
474 memcpy (tmp, str, end - str);
475 tmp[end - str] = '\0';
476
477 return allocated_variable_expand (tmp);
478#endif
479}
480
481
482/* Expand LINE for FILE. Error messages refer to the file and line where
483 FILE's commands were found. Expansion uses FILE's variable set list. */
484
485char *
486variable_expand_for_file (const char *line, struct file *file)
487{
488 char *result;
489 struct variable_set_list *save;
490
491 if (file == 0)
492 return variable_expand (line);
493
494 save = current_variable_set_list;
495 current_variable_set_list = file->variables;
496 if (file->cmds && file->cmds->fileinfo.filenm)
497 reading_file = &file->cmds->fileinfo;
498 else
499 reading_file = 0;
500 result = variable_expand (line);
501 current_variable_set_list = save;
502 reading_file = 0;
503
504 return result;
505}
506
507
508#ifdef CONFIG_WITH_COMMANDS_FUNC
509/* Expand LINE for FILE. Error messages refer to the file and line where
510 FILE's commands were found. Expansion uses FILE's variable set list.
511
512 Differs from variable_expand_for_file in that it takes a pointer to
513 where in the variable buffer to start outputting the expanded string. */
514
515char *
516variable_expand_for_file_2 (char *o, const char *line, struct file *file)
517{
518 char *result;
519 struct variable_set_list *save;
520 const struct floc *reading_file_saved;
521
522 if (file == 0)
523 return variable_expand_string (o, line, (long)-1);
524
525 save = current_variable_set_list;
526 current_variable_set_list = file->variables;
527 reading_file_saved = reading_file;
528 if (file->cmds && file->cmds->fileinfo.filenm)
529 reading_file = &file->cmds->fileinfo;
530 else
531 reading_file = 0;
532 result = variable_expand_string (o, line, (long)-1);
533 current_variable_set_list = save;
534 reading_file = reading_file_saved;
535
536 return result;
537}
538
539
540#endif /* CONFIG_WITH_COMMANDS_FUNC */
541/* Like allocated_variable_expand, but for += target-specific variables.
542 First recursively construct the variable value from its appended parts in
543 any upper variable sets. Then expand the resulting value. */
544
545static char *
546variable_append (const char *name, unsigned int length,
547 const struct variable_set_list *set)
548{
549 const struct variable *v;
550 char *buf = 0;
551
552 /* If there's nothing left to check, return the empty buffer. */
553 if (!set)
554 return initialize_variable_output ();
555
556 /* Try to find the variable in this variable set. */
557 v = lookup_variable_in_set (name, length, set->set);
558
559 /* If there isn't one, look to see if there's one in a set above us. */
560 if (!v)
561 return variable_append (name, length, set->next);
562
563 /* If this variable type is append, first get any upper values.
564 If not, initialize the buffer. */
565 if (v->append)
566 buf = variable_append (name, length, set->next);
567 else
568 buf = initialize_variable_output ();
569
570 /* Append this value to the buffer, and return it.
571 If we already have a value, first add a space. */
572 if (buf > variable_buffer)
573 buf = variable_buffer_output (buf, " ", 1);
574
575 /* Either expand it or copy it, depending. */
576 if (! v->recursive)
577#ifdef CONFIG_WITH_VALUE_LENGTH
578 return variable_buffer_output (buf, v->value,
579 v->value_length >= 0 ? v->value_length : strlen (v->value));
580#else
581 return variable_buffer_output (buf, v->value, strlen (v->value));
582#endif
583
584#ifdef CONFIG_WITH_VALUE_LENGTH
585 buf = variable_expand_string (buf, v->value, v->value_length >= 0 ? v->value_length : strlen (v->value));
586#else
587 buf = variable_expand_string (buf, v->value, strlen (v->value));
588#endif
589 return (buf + strlen (buf));
590}
591
592#ifdef CONFIG_WITH_VALUE_LENGTH
593/* Expands the specified string, appending it to the specified variable value. */
594void
595append_expanded_string_to_variable (struct variable *v, const char *value)
596{
597 char *p;
598
599 /* switch the variable buffer to the variable value buffer. */
600 char *saved_buffer = variable_buffer;
601 unsigned int saved_buffer_length = variable_buffer_length;
602 variable_buffer = v->value;
603 variable_buffer_length = v->value_alloc_len;
604
605 /* skip the current value and start appending a space and the expanded string. */
606 p = v->value + v->value_length;
607 if (v->value_length != 0)
608 p = variable_buffer_output (p, " ", 1);
609 p = variable_expand_string (p, value, (long)-1);
610
611 /* update the variable. (The variable_expand_string return is annoying!) */
612 p = strchr (p, '\0');
613 v->value = variable_buffer;
614 v->value_length = p - v->value;
615 v->value_alloc_len = variable_buffer_length;
616
617 /* restore the variable buffer. */
618 variable_buffer = saved_buffer;
619 variable_buffer_length = saved_buffer_length;
620}
621#endif /* CONFIG_WITH_VALUE_LENGTH */
622
623static char *
624allocated_variable_append (const struct variable *v)
625{
626 char *val;
627
628 /* Construct the appended variable value. */
629
630 char *obuf = variable_buffer;
631 unsigned int olen = variable_buffer_length;
632
633 variable_buffer = 0;
634
635 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
636 variable_buffer_output (val, "", 1);
637 val = variable_buffer;
638
639 variable_buffer = obuf;
640 variable_buffer_length = olen;
641
642 return val;
643}
644
645/* Like variable_expand_for_file, but the returned string is malloc'd.
646 This function is called a lot. It wants to be efficient. */
647
648char *
649allocated_variable_expand_for_file (const char *line, struct file *file)
650{
651 char *value;
652
653 char *obuf = variable_buffer;
654 unsigned int olen = variable_buffer_length;
655
656 variable_buffer = 0;
657
658 value = variable_expand_for_file (line, file);
659
660#if 0
661 /* Waste a little memory and save time. */
662 value = xrealloc (value, strlen (value))
663#endif
664
665 variable_buffer = obuf;
666 variable_buffer_length = olen;
667
668 return value;
669}
670
671/* Install a new variable_buffer context, returning the current one for
672 safe-keeping. */
673
674void
675install_variable_buffer (char **bufp, unsigned int *lenp)
676{
677 *bufp = variable_buffer;
678 *lenp = variable_buffer_length;
679
680 variable_buffer = 0;
681 initialize_variable_output ();
682}
683
684/* Restore a previously-saved variable_buffer setting (free the current one).
685 */
686
687void
688restore_variable_buffer (char *buf, unsigned int len)
689{
690 free (variable_buffer);
691
692 variable_buffer = buf;
693 variable_buffer_length = len;
694}
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