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

Contents of /make/expand.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.35 - (show annotations) (download)
Thu Jan 30 06:21:36 2003 UTC (21 years, 3 months ago) by psmith
Branch: MAIN
Changes since 1.34: +6 -8 lines
File MIME type: text/plain
Enhancement (bug #2407)  Make error messages more clear.

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

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