VirtualBox

source: kBuild/vendor/gnumake/3.82-cvs/expand.c@ 2580

Last change on this file since 2580 was 2580, checked in by bird, 12 years ago

Importing the make-3-82 CVS tag with --auto-props but no keywords.

  • Property svn:eol-style set to native
File size: 16.5 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, 2008, 2009,
42010 Free Software Foundation, 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
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, *alloc = NULL;
446 char *r;
447
448 if (str == end)
449 return xstrdup("");
450
451 if (!end || *end == '\0')
452 return allocated_variable_expand (str);
453
454 if (end - str + 1 > 1000)
455 tmp = alloc = xmalloc (end - str + 1);
456 else
457 tmp = alloca (end - str + 1);
458
459 memcpy (tmp, str, end - str);
460 tmp[end - str] = '\0';
461
462 r = allocated_variable_expand (tmp);
463
464 if (alloc)
465 free (alloc);
466
467 return r;
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 *savev;
479 const struct floc *savef;
480
481 if (file == 0)
482 return variable_expand (line);
483
484 savev = current_variable_set_list;
485 current_variable_set_list = file->variables;
486
487 savef = reading_file;
488 if (file->cmds && file->cmds->fileinfo.filenm)
489 reading_file = &file->cmds->fileinfo;
490 else
491 reading_file = 0;
492
493 result = variable_expand (line);
494
495 current_variable_set_list = savev;
496 reading_file = savef;
497
498 return result;
499}
500
501
502/* Like allocated_variable_expand, but for += target-specific variables.
503 First recursively construct the variable value from its appended parts in
504 any upper variable sets. Then expand the resulting value. */
505
506static char *
507variable_append (const char *name, unsigned int length,
508 const struct variable_set_list *set)
509{
510 const struct variable *v;
511 char *buf = 0;
512
513 /* If there's nothing left to check, return the empty buffer. */
514 if (!set)
515 return initialize_variable_output ();
516
517 /* Try to find the variable in this variable set. */
518 v = lookup_variable_in_set (name, length, set->set);
519
520 /* If there isn't one, look to see if there's one in a set above us. */
521 if (!v)
522 return variable_append (name, length, set->next);
523
524 /* If this variable type is append, first get any upper values.
525 If not, initialize the buffer. */
526 if (v->append)
527 buf = variable_append (name, length, set->next);
528 else
529 buf = initialize_variable_output ();
530
531 /* Append this value to the buffer, and return it.
532 If we already have a value, first add a space. */
533 if (buf > variable_buffer)
534 buf = variable_buffer_output (buf, " ", 1);
535
536 /* Either expand it or copy it, depending. */
537 if (! v->recursive)
538 return variable_buffer_output (buf, v->value, strlen (v->value));
539
540 buf = variable_expand_string (buf, v->value, strlen (v->value));
541 return (buf + strlen (buf));
542}
543
544
545static char *
546allocated_variable_append (const struct variable *v)
547{
548 char *val;
549
550 /* Construct the appended variable value. */
551
552 char *obuf = variable_buffer;
553 unsigned int olen = variable_buffer_length;
554
555 variable_buffer = 0;
556
557 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
558 variable_buffer_output (val, "", 1);
559 val = variable_buffer;
560
561 variable_buffer = obuf;
562 variable_buffer_length = olen;
563
564 return val;
565}
566
567/* Like variable_expand_for_file, but the returned string is malloc'd.
568 This function is called a lot. It wants to be efficient. */
569
570char *
571allocated_variable_expand_for_file (const char *line, struct file *file)
572{
573 char *value;
574
575 char *obuf = variable_buffer;
576 unsigned int olen = variable_buffer_length;
577
578 variable_buffer = 0;
579
580 value = variable_expand_for_file (line, file);
581
582 variable_buffer = obuf;
583 variable_buffer_length = olen;
584
585 return value;
586}
587
588/* Install a new variable_buffer context, returning the current one for
589 safe-keeping. */
590
591void
592install_variable_buffer (char **bufp, unsigned int *lenp)
593{
594 *bufp = variable_buffer;
595 *lenp = variable_buffer_length;
596
597 variable_buffer = 0;
598 initialize_variable_output ();
599}
600
601/* Restore a previously-saved variable_buffer setting (free the current one).
602 */
603
604void
605restore_variable_buffer (char *buf, unsigned int len)
606{
607 free (variable_buffer);
608
609 variable_buffer = buf;
610 variable_buffer_length = len;
611}
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