/[make]/make/expand.c
ViewVC logotype

Contents of /make/expand.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (show annotations) (download)
Tue Nov 4 07:40:29 2003 UTC (20 years, 6 months ago) by psmith
Branch: MAIN
Changes since 1.35: +10 -0 lines
File MIME type: text/plain
Fix bugs 5798 and 6195.

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

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26