VirtualBox

source: kBuild/vendor/gnumake/3.81-beta1/expand.c

Last change on this file was 54, checked in by (none), 21 years ago

This commit was manufactured by cvs2svn to create branch 'GNU'.

  • Property svn:eol-style set to native
File size: 14.9 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 (v == 0 || *v->value == '\0')
158 return o;
159
160 value = (v->recursive ? recursively_expand (v) : v->value);
161
162 o = variable_buffer_output (o, value, strlen (value));
163
164 if (v->recursive)
165 free (value);
166
167 return o;
168}
169
170
171/* Scan STRING for variable references and expansion-function calls. Only
172 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
173 a null byte is found.
174
175 Write the results to LINE, which must point into `variable_buffer'. If
176 LINE is NULL, start at the beginning of the buffer.
177 Return a pointer to LINE, or to the beginning of the buffer if LINE is
178 NULL. */
179
180char *
181variable_expand_string (char *line, char *string, long length)
182{
183 register struct variable *v;
184 register char *p, *o, *p1;
185 char save_char = '\0';
186 unsigned int line_offset;
187
188 if (!line)
189 line = initialize_variable_output();
190
191 p = string;
192 o = line;
193 line_offset = line - variable_buffer;
194
195 if (length >= 0)
196 {
197 save_char = string[length];
198 string[length] = '\0';
199 }
200
201 while (1)
202 {
203 /* Copy all following uninteresting chars all at once to the
204 variable output buffer, and skip them. Uninteresting chars end
205 at the next $ or the end of the input. */
206
207 p1 = strchr (p, '$');
208
209 o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
210
211 if (p1 == 0)
212 break;
213 p = p1 + 1;
214
215 /* Dispatch on the char that follows the $. */
216
217 switch (*p)
218 {
219 case '$':
220 /* $$ seen means output one $ to the variable output buffer. */
221 o = variable_buffer_output (o, p, 1);
222 break;
223
224 case '(':
225 case '{':
226 /* $(...) or ${...} is the general case of substitution. */
227 {
228 char openparen = *p;
229 char closeparen = (openparen == '(') ? ')' : '}';
230 register char *beg = p + 1;
231 int free_beg = 0;
232 char *op, *begp;
233 char *end, *colon;
234
235 op = o;
236 begp = p;
237 if (handle_function (&op, &begp))
238 {
239 o = op;
240 p = begp;
241 break;
242 }
243
244 /* Is there a variable reference inside the parens or braces?
245 If so, expand it before expanding the entire reference. */
246
247 end = strchr (beg, closeparen);
248 if (end == 0)
249 /* Unterminated variable reference. */
250 fatal (reading_file, _("unterminated variable reference"));
251 p1 = lindex (beg, end, '$');
252 if (p1 != 0)
253 {
254 /* BEG now points past the opening paren or brace.
255 Count parens or braces until it is matched. */
256 int count = 0;
257 for (p = beg; *p != '\0'; ++p)
258 {
259 if (*p == openparen)
260 ++count;
261 else if (*p == closeparen && --count < 0)
262 break;
263 }
264 /* If COUNT is >= 0, there were unmatched opening parens
265 or braces, so we go to the simple case of a variable name
266 such as `$($(a)'. */
267 if (count < 0)
268 {
269 beg = expand_argument (beg, p); /* Expand the name. */
270 free_beg = 1; /* Remember to free BEG when finished. */
271 end = strchr (beg, '\0');
272 }
273 }
274 else
275 /* Advance P to the end of this reference. After we are
276 finished expanding this one, P will be incremented to
277 continue the scan. */
278 p = end;
279
280 /* This is not a reference to a built-in function and
281 any variable references inside are now expanded.
282 Is the resultant text a substitution reference? */
283
284 colon = lindex (beg, end, ':');
285 if (colon)
286 {
287 /* This looks like a substitution reference: $(FOO:A=B). */
288 char *subst_beg, *subst_end, *replace_beg, *replace_end;
289
290 subst_beg = colon + 1;
291 subst_end = lindex (subst_beg, end, '=');
292 if (subst_end == 0)
293 /* There is no = in sight. Punt on the substitution
294 reference and treat this as a variable name containing
295 a colon, in the code below. */
296 colon = 0;
297 else
298 {
299 replace_beg = subst_end + 1;
300 replace_end = end;
301
302 /* Extract the variable name before the colon
303 and look up that variable. */
304 v = lookup_variable (beg, colon - beg);
305 if (v == 0)
306 warn_undefined (beg, colon - beg);
307
308 if (v != 0 && *v->value != '\0')
309 {
310 char *value = (v->recursive ? recursively_expand (v)
311 : v->value);
312 char *pattern, *percent;
313 if (free_beg)
314 {
315 *subst_end = '\0';
316 pattern = subst_beg;
317 }
318 else
319 {
320 pattern = (char *) alloca (subst_end - subst_beg
321 + 1);
322 bcopy (subst_beg, pattern, subst_end - subst_beg);
323 pattern[subst_end - subst_beg] = '\0';
324 }
325 percent = find_percent (pattern);
326 if (percent != 0)
327 {
328 char *replace;
329 if (free_beg)
330 {
331 *replace_end = '\0';
332 replace = replace_beg;
333 }
334 else
335 {
336 replace = (char *) alloca (replace_end
337 - replace_beg
338 + 1);
339 bcopy (replace_beg, replace,
340 replace_end - replace_beg);
341 replace[replace_end - replace_beg] = '\0';
342 }
343
344 o = patsubst_expand (o, value, pattern, replace,
345 percent, (char *) 0);
346 }
347 else
348 o = subst_expand (o, value,
349 pattern, replace_beg,
350 strlen (pattern),
351 end - replace_beg,
352 0, 1);
353 if (v->recursive)
354 free (value);
355 }
356 }
357 }
358
359 if (colon == 0)
360 /* This is an ordinary variable reference.
361 Look up the value of the variable. */
362 o = reference_variable (o, beg, end - beg);
363
364 if (free_beg)
365 free (beg);
366 }
367 break;
368
369 case '\0':
370 break;
371
372 default:
373 if (isblank ((unsigned char)p[-1]))
374 break;
375
376 /* A $ followed by a random char is a variable reference:
377 $a is equivalent to $(a). */
378 {
379 /* We could do the expanding here, but this way
380 avoids code repetition at a small performance cost. */
381 char name[5];
382 name[0] = '$';
383 name[1] = '(';
384 name[2] = *p;
385 name[3] = ')';
386 name[4] = '\0';
387 p1 = allocated_variable_expand (name);
388 o = variable_buffer_output (o, p1, strlen (p1));
389 free (p1);
390 }
391
392 break;
393 }
394
395 if (*p == '\0')
396 break;
397 else
398 ++p;
399 }
400
401 if (save_char)
402 string[length] = save_char;
403
404 (void)variable_buffer_output (o, "", 1);
405 return (variable_buffer + line_offset);
406}
407
408
409/* Scan LINE for variable references and expansion-function calls.
410 Build in `variable_buffer' the result of expanding the references and calls.
411 Return the address of the resulting string, which is null-terminated
412 and is valid only until the next time this function is called. */
413
414char *
415variable_expand (char *line)
416{
417 return variable_expand_string(NULL, line, (long)-1);
418}
419
420
421/* Expand an argument for an expansion function.
422 The text starting at STR and ending at END is variable-expanded
423 into a null-terminated string that is returned as the value.
424 This is done without clobbering `variable_buffer' or the current
425 variable-expansion that is in progress. */
426
427char *
428expand_argument (const char *str, const char *end)
429{
430 char *tmp;
431
432 if (str == end)
433 return xstrdup("");
434
435 if (!end || *end == '\0')
436 return allocated_variable_expand ((char *)str);
437
438 tmp = (char *) alloca (end - str + 1);
439 bcopy (str, tmp, end - str);
440 tmp[end - str] = '\0';
441
442 return allocated_variable_expand (tmp);
443}
444
445
446/* Expand LINE for FILE. Error messages refer to the file and line where
447 FILE's commands were found. Expansion uses FILE's variable set list. */
448
449static char *
450variable_expand_for_file (char *line, struct file *file)
451{
452 char *result;
453 struct variable_set_list *save;
454
455 if (file == 0)
456 return variable_expand (line);
457
458 save = current_variable_set_list;
459 current_variable_set_list = file->variables;
460 if (file->cmds && file->cmds->fileinfo.filenm)
461 reading_file = &file->cmds->fileinfo;
462 else
463 reading_file = 0;
464 result = variable_expand (line);
465 current_variable_set_list = save;
466 reading_file = 0;
467
468 return result;
469}
470
471
472/* Like allocated_variable_expand, but for += target-specific variables.
473 First recursively construct the variable value from its appended parts in
474 any upper variable sets. Then expand the resulting value. */
475
476static char *
477variable_append (const char *name, unsigned int length,
478 const struct variable_set_list *set)
479{
480 const struct variable *v;
481 char *buf = 0;
482
483 /* If there's nothing left to check, return the empty buffer. */
484 if (!set)
485 return initialize_variable_output ();
486
487 /* Try to find the variable in this variable set. */
488 v = lookup_variable_in_set (name, length, set->set);
489
490 /* If there isn't one, look to see if there's one in a set above us. */
491 if (!v)
492 return variable_append (name, length, set->next);
493
494 /* If this variable type is append, first get any upper values.
495 If not, initialize the buffer. */
496 if (v->append)
497 buf = variable_append (name, length, set->next);
498 else
499 buf = initialize_variable_output ();
500
501 /* Append this value to the buffer, and return it.
502 If we already have a value, first add a space. */
503 if (buf > variable_buffer)
504 buf = variable_buffer_output (buf, " ", 1);
505
506 return variable_buffer_output (buf, v->value, strlen (v->value));
507}
508
509
510static char *
511allocated_variable_append (const struct variable *v)
512{
513 char *val, *retval;
514
515 /* Construct the appended variable value. */
516
517 char *obuf = variable_buffer;
518 unsigned int olen = variable_buffer_length;
519
520 variable_buffer = 0;
521
522 val = variable_append (v->name, strlen (v->name), current_variable_set_list);
523 variable_buffer_output (val, "", 1);
524 val = variable_buffer;
525
526 variable_buffer = obuf;
527 variable_buffer_length = olen;
528
529 /* Now expand it and return that. */
530
531 retval = allocated_variable_expand (val);
532
533 free (val);
534 return retval;
535}
536
537/* Like variable_expand_for_file, but the returned string is malloc'd.
538 This function is called a lot. It wants to be efficient. */
539
540char *
541allocated_variable_expand_for_file (char *line, struct file *file)
542{
543 char *value;
544
545 char *obuf = variable_buffer;
546 unsigned int olen = variable_buffer_length;
547
548 variable_buffer = 0;
549
550 value = variable_expand_for_file (line, file);
551
552#if 0
553 /* Waste a little memory and save time. */
554 value = xrealloc (value, strlen (value))
555#endif
556
557 variable_buffer = obuf;
558 variable_buffer_length = olen;
559
560 return value;
561}
562
563/* Install a new variable_buffer context, returning the current one for
564 safe-keeping. */
565
566void
567install_variable_buffer (char **bufp, unsigned int *lenp)
568{
569 *bufp = variable_buffer;
570 *lenp = variable_buffer_length;
571
572 variable_buffer = 0;
573 initialize_variable_output ();
574}
575
576/* Restore a previously-saved variable_buffer setting (free the current one).
577 */
578
579void
580restore_variable_buffer (char *buf, unsigned int len)
581{
582 free (variable_buffer);
583
584 variable_buffer = buf;
585 variable_buffer_length = len;
586}
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