VirtualBox

source: kBuild/vendor/gnumake/current/expand.c@ 486

Last change on this file since 486 was 280, checked in by bird, 20 years ago

Current make snaphot, 2005-05-16.

  • Property svn:eol-style set to native
File size: 15.7 KB
Line 
1/* Variable expansion functions for GNU Make.
2Copyright (C) 1988, 89, 91, 92, 93, 95 Free Software Foundation, Inc.
3This file is part of GNU Make.
4
5GNU Make is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10GNU Make is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Make; see the file COPYING. If not, write to
17the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, MA 02111-1307, USA. */
19
20#include "make.h"
21
22#include <assert.h>
23
24#include "filedef.h"
25#include "job.h"
26#include "commands.h"
27#include "variable.h"
28#include "rule.h"
29
30/* The next two describe the variable output buffer.
31 This buffer is used to hold the variable-expansion of a line of the
32 makefile. It is made bigger with realloc whenever it is too small.
33 variable_buffer_length is the size currently allocated.
34 variable_buffer is the address of the buffer.
35
36 For efficiency, it's guaranteed that the buffer will always have
37 VARIABLE_BUFFER_ZONE extra bytes allocated. This allows you to add a few
38 extra chars without having to call a function. Note you should never use
39 these bytes unless you're _sure_ you have room (you know when the buffer
40 length was last checked. */
41
42#define VARIABLE_BUFFER_ZONE 5
43
44static unsigned int variable_buffer_length;
45char *variable_buffer;
46
47/* Subroutine of variable_expand and friends:
48 The text to add is LENGTH chars starting at STRING to the variable_buffer.
49 The text is added to the buffer at PTR, and the updated pointer into
50 the buffer is returned as the value. Thus, the value returned by
51 each call to variable_buffer_output should be the first argument to
52 the following call. */
53
54char *
55variable_buffer_output (char *ptr, char *string, unsigned int length)
56{
57 register unsigned int newlen = length + (ptr - variable_buffer);
58
59 if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
60 {
61 unsigned int offset = ptr - variable_buffer;
62 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
63 ? newlen + 100
64 : 2 * variable_buffer_length);
65 variable_buffer = (char *) xrealloc (variable_buffer,
66 variable_buffer_length);
67 ptr = variable_buffer + offset;
68 }
69
70 bcopy (string, ptr, length);
71 return ptr + length;
72}
73
74/* Return a pointer to the beginning of the variable buffer. */
75
76static char *
77initialize_variable_output (void)
78{
79 /* If we don't have a variable output buffer yet, get one. */
80
81 if (variable_buffer == 0)
82 {
83 variable_buffer_length = 200;
84 variable_buffer = (char *) xmalloc (variable_buffer_length);
85 variable_buffer[0] = '\0';
86 }
87
88 return variable_buffer;
89}
90
91
92/* Recursively expand V. The returned string is malloc'd. */
93
94static char *allocated_variable_append PARAMS ((const struct variable *v));
95
96char *
97recursively_expand_for_file (struct variable *v, struct file *file)
98{
99 char *value;
100 struct variable_set_list *save = 0;
101 int set_reading = 0;
102
103 if (v->expanding)
104 {
105 if (!v->exp_count)
106 /* Expanding V causes infinite recursion. Lose. */
107 fatal (reading_file,
108 _("Recursive variable `%s' references itself (eventually)"),
109 v->name);
110 --v->exp_count;
111 }
112
113 if (file)
114 {
115 save = current_variable_set_list;
116 current_variable_set_list = file->variables;
117 }
118
119 /* If we have no other file-reading context, use the variable's context. */
120 if (!reading_file)
121 {
122 set_reading = 1;
123 reading_file = &v->fileinfo;
124 }
125
126 v->expanding = 1;
127 if (v->append)
128 value = allocated_variable_append (v);
129 else
130 value = allocated_variable_expand (v->value);
131 v->expanding = 0;
132
133 if (set_reading)
134 reading_file = 0;
135 if (file)
136 current_variable_set_list = save;
137
138 return value;
139}
140
141/* Expand a simple reference to variable NAME, which is LENGTH chars long. */
142
143#ifdef __GNUC__
144__inline
145#endif
146static char *
147reference_variable (char *o, char *name, unsigned int length)
148{
149 register struct variable *v;
150 char *value;
151
152 v = lookup_variable (name, length);
153
154 if (v == 0)
155 warn_undefined (name, length);
156
157 /* If there's no variable by that name or it has no value, stop now. */
158 if (v == 0 || (*v->value == '\0' && !v->append))
159 return o;
160
161 value = (v->recursive ? recursively_expand (v) : v->value);
162
163 o = variable_buffer_output (o, value, strlen (value));
164
165 if (v->recursive)
166 free (value);
167
168 return o;
169}
170
171
172/* Scan STRING for variable references and expansion-function calls. Only
173 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
174 a null byte is found.
175
176 Write the results to LINE, which must point into `variable_buffer'. If
177 LINE is NULL, start at the beginning of the buffer.
178 Return a pointer to LINE, or to the beginning of the buffer if LINE is
179 NULL. */
180
181char *
182variable_expand_string (char *line, char *string, long length)
183{
184 register struct variable *v;
185 register char *p, *o, *p1;
186 char save_char = '\0';
187 unsigned int line_offset;
188
189 if (!line)
190 line = initialize_variable_output();
191
192 p = string;
193 o = line;
194 line_offset = line - variable_buffer;
195
196 if (length >= 0)
197 {
198 save_char = string[length];
199 string[length] = '\0';
200 }
201
202 while (1)
203 {
204 /* Copy all following uninteresting chars all at once to the
205 variable output buffer, and skip them. Uninteresting chars end
206 at the next $ or the end of the input. */
207
208 p1 = strchr (p, '$');
209
210 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
211
212 if (p1 == 0)
213 break;
214 p = p1 + 1;
215
216 /* Dispatch on the char that follows the $. */
217
218 switch (*p)
219 {
220 case '$':
221 /* $$ seen means output one $ to the variable output buffer. */
222 o = variable_buffer_output (o, p, 1);
223 break;
224
225 case '(':
226 case '{':
227 /* $(...) or ${...} is the general case of substitution. */
228 {
229 char openparen = *p;
230 char closeparen = (openparen == '(') ? ')' : '}';
231 register char *beg = p + 1;
232 int free_beg = 0;
233 char *op, *begp;
234 char *end, *colon;
235
236 op = o;
237 begp = p;
238 if (handle_function (&op, &begp))
239 {
240 o = op;
241 p = begp;
242 break;
243 }
244
245 /* Is there a variable reference inside the parens or braces?
246 If so, expand it before expanding the entire reference. */
247
248 end = strchr (beg, closeparen);
249 if (end == 0)
250 /* Unterminated variable reference. */
251 fatal (reading_file, _("unterminated variable reference"));
252 p1 = lindex (beg, end, '$');
253 if (p1 != 0)
254 {
255 /* BEG now points past the opening paren or brace.
256 Count parens or braces until it is matched. */
257 int count = 0;
258 for (p = beg; *p != '\0'; ++p)
259 {
260 if (*p == openparen)
261 ++count;
262 else if (*p == closeparen && --count < 0)
263 break;
264 }
265 /* If COUNT is >= 0, there were unmatched opening parens
266 or braces, so we go to the simple case of a variable name
267 such as `$($(a)'. */
268 if (count < 0)
269 {
270 beg = expand_argument (beg, p); /* Expand the name. */
271 free_beg = 1; /* Remember to free BEG when finished. */
272 end = strchr (beg, '\0');
273 }
274 }
275 else
276 /* Advance P to the end of this reference. After we are
277 finished expanding this one, P will be incremented to
278 continue the scan. */
279 p = end;
280
281 /* This is not a reference to a built-in function and
282 any variable references inside are now expanded.
283 Is the resultant text a substitution reference? */
284
285 colon = lindex (beg, end, ':');
286 if (colon)
287 {
288 /* This looks like a substitution reference: $(FOO:A=B). */
289 char *subst_beg, *subst_end, *replace_beg, *replace_end;
290
291 subst_beg = colon + 1;
292 subst_end = lindex (subst_beg, end, '=');
293 if (subst_end == 0)
294 /* There is no = in sight. Punt on the substitution
295 reference and treat this as a variable name containing
296 a colon, in the code below. */
297 colon = 0;
298 else
299 {
300 replace_beg = subst_end + 1;
301 replace_end = end;
302
303 /* Extract the variable name before the colon
304 and look up that variable. */
305 v = lookup_variable (beg, colon - beg);
306 if (v == 0)
307 warn_undefined (beg, colon - beg);
308
309 /* If the variable is not empty, perform the
310 substitution. */
311 if (v != 0 && *v->value != '\0')
312 {
313 char *pattern, *replace, *ppercent, *rpercent;
314 char *value = (v->recursive
315 ? recursively_expand (v)
316 : v->value);
317
318 /* Copy the pattern and the replacement. Add in an
319 extra % at the beginning to use in case there
320 isn't one in the pattern. */
321 pattern = (char *) alloca (subst_end - subst_beg + 2);
322 *(pattern++) = '%';
323 bcopy (subst_beg, pattern, subst_end - subst_beg);
324 pattern[subst_end - subst_beg] = '\0';
325
326 replace = (char *) alloca (replace_end
327 - replace_beg + 2);
328 *(replace++) = '%';
329 bcopy (replace_beg, replace,
330 replace_end - replace_beg);
331 replace[replace_end - replace_beg] = '\0';
332
333 /* Look for %. Set the percent pointers properly
334 based on whether we find one or not. */
335 ppercent = find_percent (pattern);
336 if (ppercent)
337 {
338 ++ppercent;
339 rpercent = 0;
340 }
341 else
342 {
343 ppercent = pattern;
344 rpercent = replace;
345 --pattern;
346 --replace;
347 }
348
349 o = patsubst_expand (o, value, pattern, replace,
350 ppercent, rpercent);
351
352 if (v->recursive)
353 free (value);
354 }
355 }
356 }
357
358 if (colon == 0)
359 /* This is an ordinary variable reference.
360 Look up the value of the variable. */
361 o = reference_variable (o, beg, end - beg);
362
363 if (free_beg)
364 free (beg);
365 }
366 break;
367
368 case '\0':
369 break;
370
371 default:
372 if (isblank ((unsigned char)p[-1]))
373 break;
374
375 /* A $ followed by a random char is a variable reference:
376 $a is equivalent to $(a). */
377 {
378 /* We could do the expanding here, but this way
379 avoids code repetition at a small performance cost. */
380 char name[5];
381 name[0] = '$';
382 name[1] = '(';
383 name[2] = *p;
384 name[3] = ')';
385 name[4] = '\0';
386 p1 = allocated_variable_expand (name);
387 o = variable_buffer_output (o, p1, strlen (p1));
388 free (p1);
389 }
390
391 break;
392 }
393
394 if (*p == '\0')
395 break;
396 else
397 ++p;
398 }
399
400 if (save_char)
401 string[length] = save_char;
402
403 (void)variable_buffer_output (o, "", 1);
404 return (variable_buffer + line_offset);
405}
406
407
408/* Scan LINE for variable references and expansion-function calls.
409 Build in `variable_buffer' the result of expanding the references and calls.
410 Return the address of the resulting string, which is null-terminated
411 and is valid only until the next time this function is called. */
412
413char *
414variable_expand (char *line)
415{
416 return variable_expand_string(NULL, line, (long)-1);
417}
418
419
420/* Expand an argument for an expansion function.
421 The text starting at STR and ending at END is variable-expanded
422 into a null-terminated string that is returned as the value.
423 This is done without clobbering `variable_buffer' or the current
424 variable-expansion that is in progress. */
425
426char *
427expand_argument (const char *str, const char *end)
428{
429 char *tmp;
430
431 if (str == end)
432 return xstrdup("");
433
434 if (!end || *end == '\0')
435 return allocated_variable_expand ((char *)str);
436
437 tmp = (char *) alloca (end - str + 1);
438 bcopy (str, tmp, end - str);
439 tmp[end - str] = '\0';
440
441 return allocated_variable_expand (tmp);
442}
443
444
445/* Expand LINE for FILE. Error messages refer to the file and line where
446 FILE's commands were found. Expansion uses FILE's variable set list. */
447
448char *
449variable_expand_for_file (char *line, struct file *file)
450{
451 char *result;
452 struct variable_set_list *save;
453
454 if (file == 0)
455 return variable_expand (line);
456
457 save = current_variable_set_list;
458 current_variable_set_list = file->variables;
459 if (file->cmds && file->cmds->fileinfo.filenm)
460 reading_file = &file->cmds->fileinfo;
461 else
462 reading_file = 0;
463 result = variable_expand (line);
464 current_variable_set_list = save;
465 reading_file = 0;
466
467 return result;
468}
469
470
471/* Like allocated_variable_expand, but for += target-specific variables.
472 First recursively construct the variable value from its appended parts in
473 any upper variable sets. Then expand the resulting value. */
474
475static char *
476variable_append (const char *name, unsigned int length,
477 const struct variable_set_list *set)
478{
479 const struct variable *v;
480 char *buf = 0;
481
482 /* If there's nothing left to check, return the empty buffer. */
483 if (!set)
484 return initialize_variable_output ();
485
486 /* Try to find the variable in this variable set. */
487 v = lookup_variable_in_set (name, length, set->set);
488
489 /* If there isn't one, look to see if there's one in a set above us. */
490 if (!v)
491 return variable_append (name, length, set->next);
492
493 /* If this variable type is append, first get any upper values.
494 If not, initialize the buffer. */
495 if (v->append)
496 buf = variable_append (name, length, set->next);
497 else
498 buf = initialize_variable_output ();
499
500 /* Append this value to the buffer, and return it.
501 If we already have a value, first add a space. */
502 if (buf > variable_buffer)
503 buf = variable_buffer_output (buf, " ", 1);
504
505 return variable_buffer_output (buf, v->value, strlen (v->value));
506}
507
508
509static char *
510allocated_variable_append (const struct variable *v)
511{
512 char *val, *retval;
513
514 /* Construct the appended variable value. */
515
516 char *obuf = variable_buffer;
517 unsigned int olen = variable_buffer_length;
518
519 variable_buffer = 0;
520
521 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
522 variable_buffer_output (val, "", 1);
523 val = variable_buffer;
524
525 variable_buffer = obuf;
526 variable_buffer_length = olen;
527
528 /* Now expand it and return that. */
529
530 retval = allocated_variable_expand (val);
531
532 free (val);
533 return retval;
534}
535
536/* Like variable_expand_for_file, but the returned string is malloc'd.
537 This function is called a lot. It wants to be efficient. */
538
539char *
540allocated_variable_expand_for_file (char *line, struct file *file)
541{
542 char *value;
543
544 char *obuf = variable_buffer;
545 unsigned int olen = variable_buffer_length;
546
547 variable_buffer = 0;
548
549 value = variable_expand_for_file (line, file);
550
551#if 0
552 /* Waste a little memory and save time. */
553 value = xrealloc (value, strlen (value))
554#endif
555
556 variable_buffer = obuf;
557 variable_buffer_length = olen;
558
559 return value;
560}
561
562/* Install a new variable_buffer context, returning the current one for
563 safe-keeping. */
564
565void
566install_variable_buffer (char **bufp, unsigned int *lenp)
567{
568 *bufp = variable_buffer;
569 *lenp = variable_buffer_length;
570
571 variable_buffer = 0;
572 initialize_variable_output ();
573}
574
575/* Restore a previously-saved variable_buffer setting (free the current one).
576 */
577
578void
579restore_variable_buffer (char *buf, unsigned int len)
580{
581 free (variable_buffer);
582
583 variable_buffer = buf;
584 variable_buffer_length = len;
585}
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