VirtualBox

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

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

some silly realloc optimizations.

  • Property svn:eol-style set to native
File size: 18.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/* Like allocated_variable_expand, but for += target-specific variables.
509 First recursively construct the variable value from its appended parts in
510 any upper variable sets. Then expand the resulting value. */
511
512static char *
513variable_append (const char *name, unsigned int length,
514 const struct variable_set_list *set)
515{
516 const struct variable *v;
517 char *buf = 0;
518
519 /* If there's nothing left to check, return the empty buffer. */
520 if (!set)
521 return initialize_variable_output ();
522
523 /* Try to find the variable in this variable set. */
524 v = lookup_variable_in_set (name, length, set->set);
525
526 /* If there isn't one, look to see if there's one in a set above us. */
527 if (!v)
528 return variable_append (name, length, set->next);
529
530 /* If this variable type is append, first get any upper values.
531 If not, initialize the buffer. */
532 if (v->append)
533 buf = variable_append (name, length, set->next);
534 else
535 buf = initialize_variable_output ();
536
537 /* Append this value to the buffer, and return it.
538 If we already have a value, first add a space. */
539 if (buf > variable_buffer)
540 buf = variable_buffer_output (buf, " ", 1);
541
542 /* Either expand it or copy it, depending. */
543 if (! v->recursive)
544#ifdef CONFIG_WITH_VALUE_LENGTH
545 return variable_buffer_output (buf, v->value,
546 v->value_length >= 0 ? v->value_length : strlen (v->value));
547#else
548 return variable_buffer_output (buf, v->value, strlen (v->value));
549#endif
550
551#ifdef CONFIG_WITH_VALUE_LENGTH
552 buf = variable_expand_string (buf, v->value, v->value_length >= 0 ? v->value_length : strlen (v->value));
553#else
554 buf = variable_expand_string (buf, v->value, strlen (v->value));
555#endif
556 return (buf + strlen (buf));
557}
558
559#ifdef CONFIG_WITH_VALUE_LENGTH
560/* Expands the specified string, appending it to the specified variable value. */
561void
562append_expanded_string_to_variable (struct variable *v, const char *value)
563{
564 char *p;
565
566 /* switch the variable buffer to the variable value buffer. */
567 char *saved_buffer = variable_buffer;
568 unsigned int saved_buffer_length = variable_buffer_length;
569 variable_buffer = v->value;
570 variable_buffer_length = v->value_alloc_len;
571
572 /* skip the current value and start appending a space and the expanded string. */
573 p = v->value + v->value_length;
574 if (v->value_length != 0)
575 p = variable_buffer_output (p, " ", 1);
576 p = variable_expand_string (p, value, (long)-1);
577
578 /* update the variable. (The variable_expand_string return is annoying!) */
579 p = strchr (p, '\0');
580 v->value = variable_buffer;
581 v->value_length = p - v->value;
582 v->value_alloc_len = variable_buffer_length;
583
584 /* restore the variable buffer. */
585 variable_buffer = saved_buffer;
586 variable_buffer_length = saved_buffer_length;
587}
588#endif /* CONFIG_WITH_VALUE_LENGTH */
589
590static char *
591allocated_variable_append (const struct variable *v)
592{
593 char *val;
594
595 /* Construct the appended variable value. */
596
597 char *obuf = variable_buffer;
598 unsigned int olen = variable_buffer_length;
599
600 variable_buffer = 0;
601
602 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
603 variable_buffer_output (val, "", 1);
604 val = variable_buffer;
605
606 variable_buffer = obuf;
607 variable_buffer_length = olen;
608
609 return val;
610}
611
612/* Like variable_expand_for_file, but the returned string is malloc'd.
613 This function is called a lot. It wants to be efficient. */
614
615char *
616allocated_variable_expand_for_file (const char *line, struct file *file)
617{
618 char *value;
619
620 char *obuf = variable_buffer;
621 unsigned int olen = variable_buffer_length;
622
623 variable_buffer = 0;
624
625 value = variable_expand_for_file (line, file);
626
627#if 0
628 /* Waste a little memory and save time. */
629 value = xrealloc (value, strlen (value))
630#endif
631
632 variable_buffer = obuf;
633 variable_buffer_length = olen;
634
635 return value;
636}
637
638/* Install a new variable_buffer context, returning the current one for
639 safe-keeping. */
640
641void
642install_variable_buffer (char **bufp, unsigned int *lenp)
643{
644 *bufp = variable_buffer;
645 *lenp = variable_buffer_length;
646
647 variable_buffer = 0;
648 initialize_variable_output ();
649}
650
651/* Restore a previously-saved variable_buffer setting (free the current one).
652 */
653
654void
655restore_variable_buffer (char *buf, unsigned int len)
656{
657 free (variable_buffer);
658
659 variable_buffer = buf;
660 variable_buffer_length = len;
661}
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