VirtualBox

source: kBuild/trunk/src/gmakenew/expand.c@ 944

Last change on this file since 944 was 916, checked in by bird, 18 years ago

fixed a const issue with the value-length stuff.

  • Property svn:eol-style set to native
File size: 18.1 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#if defined(__GNUC__) || defined(_MSC_VER) /* bird added MSC */
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#ifdef CONFIG_WITH_OPTIMIZATION_HACKS
454 {
455 const char saved_char = *end;
456 *(char *)end = '\0';
457 tmp = allocated_variable_expand ((char *)str);
458 *(char *)end = saved_char;
459 return tmp;
460 }
461#else
462 tmp = alloca (end - str + 1);
463 memcpy (tmp, str, end - str);
464 tmp[end - str] = '\0';
465
466 return allocated_variable_expand (tmp);
467#endif
468}
469
470
471/* Expand LINE for FILE. Error messages refer to the file and line where
472 FILE's commands were found. Expansion uses FILE's variable set list. */
473
474char *
475variable_expand_for_file (const char *line, struct file *file)
476{
477 char *result;
478 struct variable_set_list *save;
479
480 if (file == 0)
481 return variable_expand (line);
482
483 save = current_variable_set_list;
484 current_variable_set_list = file->variables;
485 if (file->cmds && file->cmds->fileinfo.filenm)
486 reading_file = &file->cmds->fileinfo;
487 else
488 reading_file = 0;
489 result = variable_expand (line);
490 current_variable_set_list = save;
491 reading_file = 0;
492
493 return result;
494}
495
496
497/* Like allocated_variable_expand, but for += target-specific variables.
498 First recursively construct the variable value from its appended parts in
499 any upper variable sets. Then expand the resulting value. */
500
501static char *
502variable_append (const char *name, unsigned int length,
503 const struct variable_set_list *set)
504{
505 const struct variable *v;
506 char *buf = 0;
507
508 /* If there's nothing left to check, return the empty buffer. */
509 if (!set)
510 return initialize_variable_output ();
511
512 /* Try to find the variable in this variable set. */
513 v = lookup_variable_in_set (name, length, set->set);
514
515 /* If there isn't one, look to see if there's one in a set above us. */
516 if (!v)
517 return variable_append (name, length, set->next);
518
519 /* If this variable type is append, first get any upper values.
520 If not, initialize the buffer. */
521 if (v->append)
522 buf = variable_append (name, length, set->next);
523 else
524 buf = initialize_variable_output ();
525
526 /* Append this value to the buffer, and return it.
527 If we already have a value, first add a space. */
528 if (buf > variable_buffer)
529 buf = variable_buffer_output (buf, " ", 1);
530
531 /* Either expand it or copy it, depending. */
532 if (! v->recursive)
533#ifdef CONFIG_WITH_VALUE_LENGTH
534 return variable_buffer_output (buf, v->value,
535 v->value_length >= 0 ? v->value_length : strlen (v->value));
536#else
537 return variable_buffer_output (buf, v->value, strlen (v->value));
538#endif
539
540#ifdef CONFIG_WITH_VALUE_LENGTH
541 buf = variable_expand_string (buf, v->value, v->value_length >= 0 ? v->value_length : strlen (v->value));
542#else
543 buf = variable_expand_string (buf, v->value, strlen (v->value));
544#endif
545 return (buf + strlen (buf));
546}
547
548#ifdef CONFIG_WITH_VALUE_LENGTH
549/* Expands the specified string, appending it to the specified variable value. */
550void
551append_expanded_string_to_variable (struct variable *v, const char *value)
552{
553 char *p;
554
555 /* switch the variable buffer to the variable value buffer. */
556 char *saved_buffer = variable_buffer;
557 unsigned int saved_buffer_length = variable_buffer_length;
558 variable_buffer = v->value;
559 variable_buffer_length = v->value_alloc_len;
560
561 /* skip the current value and start appending a space and the expanded string. */
562 p = v->value + v->value_length;
563 if (v->value_length != 0)
564 p = variable_buffer_output (p, " ", 1);
565 p = variable_expand_string (p, value, (long)-1);
566
567 /* update the variable. (The variable_expand_string return is annoying!) */
568 p = strchr (p, '\0');
569 v->value = variable_buffer;
570 v->value_length = p - v->value;
571 v->value_alloc_len = variable_buffer_length;
572
573 /* restore the variable buffer. */
574 variable_buffer = saved_buffer;
575 variable_buffer_length = saved_buffer_length;
576}
577#endif /* CONFIG_WITH_VALUE_LENGTH */
578
579static char *
580allocated_variable_append (const struct variable *v)
581{
582 char *val;
583
584 /* Construct the appended variable value. */
585
586 char *obuf = variable_buffer;
587 unsigned int olen = variable_buffer_length;
588
589 variable_buffer = 0;
590
591 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
592 variable_buffer_output (val, "", 1);
593 val = variable_buffer;
594
595 variable_buffer = obuf;
596 variable_buffer_length = olen;
597
598 return val;
599}
600
601/* Like variable_expand_for_file, but the returned string is malloc'd.
602 This function is called a lot. It wants to be efficient. */
603
604char *
605allocated_variable_expand_for_file (const char *line, struct file *file)
606{
607 char *value;
608
609 char *obuf = variable_buffer;
610 unsigned int olen = variable_buffer_length;
611
612 variable_buffer = 0;
613
614 value = variable_expand_for_file (line, file);
615
616#if 0
617 /* Waste a little memory and save time. */
618 value = xrealloc (value, strlen (value))
619#endif
620
621 variable_buffer = obuf;
622 variable_buffer_length = olen;
623
624 return value;
625}
626
627/* Install a new variable_buffer context, returning the current one for
628 safe-keeping. */
629
630void
631install_variable_buffer (char **bufp, unsigned int *lenp)
632{
633 *bufp = variable_buffer;
634 *lenp = variable_buffer_length;
635
636 variable_buffer = 0;
637 initialize_variable_output ();
638}
639
640/* Restore a previously-saved variable_buffer setting (free the current one).
641 */
642
643void
644restore_variable_buffer (char *buf, unsigned int len)
645{
646 free (variable_buffer);
647
648 variable_buffer = buf;
649 variable_buffer_length = len;
650}
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