1 | /* Variable expansion functions for GNU Make.
|
---|
2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
|
---|
4 | Foundation, Inc.
|
---|
5 | This file is part of GNU Make.
|
---|
6 |
|
---|
7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
8 | terms of the GNU General Public License as published by the Free Software
|
---|
9 | Foundation; either version 2, or (at your option) any later version.
|
---|
10 |
|
---|
11 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
13 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License along with
|
---|
16 | GNU Make; see the file COPYING. If not, write to the Free Software
|
---|
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
|
---|
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. */
|
---|
31 | const 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 |
|
---|
47 | #ifndef KMK
|
---|
48 | static unsigned int variable_buffer_length;
|
---|
49 | #else
|
---|
50 | unsigned int variable_buffer_length;
|
---|
51 | #endif
|
---|
52 | char *variable_buffer;
|
---|
53 |
|
---|
54 |
|
---|
55 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
56 | struct recycled_buffer
|
---|
57 | {
|
---|
58 | struct recycled_buffer *next;
|
---|
59 | unsigned int length;
|
---|
60 | };
|
---|
61 | struct recycled_buffer *recycled_head;
|
---|
62 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 | #ifndef KMK
|
---|
67 | /* Subroutine of variable_expand and friends:
|
---|
68 | The text to add is LENGTH chars starting at STRING to the variable_buffer.
|
---|
69 | The text is added to the buffer at PTR, and the updated pointer into
|
---|
70 | the buffer is returned as the value. Thus, the value returned by
|
---|
71 | each call to variable_buffer_output should be the first argument to
|
---|
72 | the following call. */
|
---|
73 |
|
---|
74 | char *
|
---|
75 | variable_buffer_output (char *ptr, const char *string, unsigned int length)
|
---|
76 | {
|
---|
77 | register unsigned int newlen = length + (ptr - variable_buffer);
|
---|
78 |
|
---|
79 | if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
|
---|
80 | {
|
---|
81 | unsigned int offset = ptr - variable_buffer;
|
---|
82 | variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
|
---|
83 | ? newlen + 100
|
---|
84 | : 2 * variable_buffer_length);
|
---|
85 | variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
|
---|
86 | ptr = variable_buffer + offset;
|
---|
87 | }
|
---|
88 |
|
---|
89 | memcpy (ptr, string, length);
|
---|
90 | return ptr + length;
|
---|
91 | }
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | /* Return a pointer to the beginning of the variable buffer. */
|
---|
95 |
|
---|
96 | static char *
|
---|
97 | initialize_variable_output (void)
|
---|
98 | {
|
---|
99 | /* If we don't have a variable output buffer yet, get one. */
|
---|
100 |
|
---|
101 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
102 | if (variable_buffer == 0)
|
---|
103 | {
|
---|
104 | struct recycled_buffer *recycled = recycled_head;
|
---|
105 | if (recycled)
|
---|
106 | {
|
---|
107 | recycled_head = recycled->next;
|
---|
108 | variable_buffer_length = recycled->length;
|
---|
109 | variable_buffer = (char *)recycled;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | variable_buffer_length = 384;
|
---|
114 | variable_buffer = xmalloc (variable_buffer_length);
|
---|
115 | }
|
---|
116 | variable_buffer[0] = '\0';
|
---|
117 | }
|
---|
118 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
119 | if (variable_buffer == 0)
|
---|
120 | {
|
---|
121 | variable_buffer_length = 200;
|
---|
122 | variable_buffer = xmalloc (variable_buffer_length);
|
---|
123 | variable_buffer[0] = '\0';
|
---|
124 | }
|
---|
125 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
126 |
|
---|
127 | return variable_buffer;
|
---|
128 | }
|
---|
129 | |
---|
130 |
|
---|
131 | /* Recursively expand V. The returned string is malloc'd. */
|
---|
132 |
|
---|
133 | static char *allocated_variable_append (const struct variable *v);
|
---|
134 |
|
---|
135 | char *
|
---|
136 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
137 | recursively_expand_for_file (struct variable *v, struct file *file)
|
---|
138 | #else
|
---|
139 | recursively_expand_for_file (struct variable *v, struct file *file,
|
---|
140 | unsigned int *value_lenp)
|
---|
141 | #endif
|
---|
142 | {
|
---|
143 | char *value;
|
---|
144 | const struct floc *this_var;
|
---|
145 | const struct floc **saved_varp;
|
---|
146 | struct variable_set_list *save = 0;
|
---|
147 | int set_reading = 0;
|
---|
148 |
|
---|
149 | /* Don't install a new location if this location is empty.
|
---|
150 | This can happen for command-line variables, builtin variables, etc. */
|
---|
151 | saved_varp = expanding_var;
|
---|
152 | if (v->fileinfo.filenm)
|
---|
153 | {
|
---|
154 | this_var = &v->fileinfo;
|
---|
155 | expanding_var = &this_var;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /* If we have no other file-reading context, use the variable's context. */
|
---|
159 | if (!reading_file)
|
---|
160 | {
|
---|
161 | set_reading = 1;
|
---|
162 | reading_file = &v->fileinfo;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (v->expanding)
|
---|
166 | {
|
---|
167 | if (!v->exp_count)
|
---|
168 | /* Expanding V causes infinite recursion. Lose. */
|
---|
169 | fatal (*expanding_var,
|
---|
170 | _("Recursive variable `%s' references itself (eventually)"),
|
---|
171 | v->name);
|
---|
172 | --v->exp_count;
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (file)
|
---|
176 | {
|
---|
177 | save = current_variable_set_list;
|
---|
178 | current_variable_set_list = file->variables;
|
---|
179 | }
|
---|
180 |
|
---|
181 | v->expanding = 1;
|
---|
182 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
183 | if (v->append)
|
---|
184 | value = allocated_variable_append (v);
|
---|
185 | else
|
---|
186 | value = allocated_variable_expand (v->value);
|
---|
187 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
188 | if (!v->append)
|
---|
189 | value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp);
|
---|
190 | else
|
---|
191 | {
|
---|
192 | value = allocated_variable_append (v);
|
---|
193 | if (value_lenp)
|
---|
194 | *value_lenp = strlen (value);
|
---|
195 | }
|
---|
196 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
197 | v->expanding = 0;
|
---|
198 |
|
---|
199 | if (set_reading)
|
---|
200 | reading_file = 0;
|
---|
201 |
|
---|
202 | if (file)
|
---|
203 | current_variable_set_list = save;
|
---|
204 |
|
---|
205 | expanding_var = saved_varp;
|
---|
206 |
|
---|
207 | return value;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* Expand a simple reference to variable NAME, which is LENGTH chars long. */
|
---|
211 |
|
---|
212 | #if defined(__GNUC__)
|
---|
213 | __inline
|
---|
214 | #elif defined (MY_INLINE) /* bird */
|
---|
215 | MY_INLINE
|
---|
216 | #endif
|
---|
217 | static char *
|
---|
218 | reference_variable (char *o, const char *name, unsigned int length)
|
---|
219 | {
|
---|
220 | struct variable *v;
|
---|
221 | char *value;
|
---|
222 |
|
---|
223 | v = lookup_variable (name, length);
|
---|
224 |
|
---|
225 | if (v == 0)
|
---|
226 | warn_undefined (name, length);
|
---|
227 |
|
---|
228 | /* If there's no variable by that name or it has no value, stop now. */
|
---|
229 | if (v == 0 || (*v->value == '\0' && !v->append))
|
---|
230 | return o;
|
---|
231 |
|
---|
232 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
233 | assert ((unsigned int)v->value_length == strlen (v->value));
|
---|
234 | if (!v->recursive)
|
---|
235 | o = variable_buffer_output (o, v->value, v->value_length);
|
---|
236 | else
|
---|
237 | {
|
---|
238 | unsigned int value_len;
|
---|
239 |
|
---|
240 | /* XXX: Inline recursively_expand_for_file() here and what it calls, try
|
---|
241 | make use of O directly instead wasting time on an intermediate buffer. */
|
---|
242 | value = recursively_expand_for_file (v, NULL, &value_len);
|
---|
243 | o = variable_buffer_output (o, value, value_len);
|
---|
244 | free (value);
|
---|
245 | }
|
---|
246 | #else /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
247 | value = (v->recursive ? recursively_expand (v) : v->value);
|
---|
248 |
|
---|
249 | o = variable_buffer_output (o, value, strlen (value));
|
---|
250 |
|
---|
251 | if (v->recursive)
|
---|
252 | free (value);
|
---|
253 | #endif /* !CONFIG_WITH_VALUE_LENGTH */
|
---|
254 |
|
---|
255 | return o;
|
---|
256 | }
|
---|
257 | |
---|
258 |
|
---|
259 | #ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */
|
---|
260 | /* Scan STRING for variable references and expansion-function calls. Only
|
---|
261 | LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
|
---|
262 | a null byte is found.
|
---|
263 |
|
---|
264 | Write the results to LINE, which must point into `variable_buffer'. If
|
---|
265 | LINE is NULL, start at the beginning of the buffer.
|
---|
266 | Return a pointer to LINE, or to the beginning of the buffer if LINE is
|
---|
267 | NULL.
|
---|
268 | */
|
---|
269 | char *
|
---|
270 | variable_expand_string (char *line, const char *string, long length)
|
---|
271 | {
|
---|
272 | struct variable *v;
|
---|
273 | const char *p, *p1;
|
---|
274 | char *abuf = NULL;
|
---|
275 | char *o;
|
---|
276 | unsigned int line_offset;
|
---|
277 |
|
---|
278 | if (!line)
|
---|
279 | line = initialize_variable_output();
|
---|
280 | o = line;
|
---|
281 | line_offset = line - variable_buffer;
|
---|
282 |
|
---|
283 | if (length == 0)
|
---|
284 | {
|
---|
285 | variable_buffer_output (o, "", 1);
|
---|
286 | return (variable_buffer);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* If we want a subset of the string, allocate a temporary buffer for it.
|
---|
290 | Most of the functions we use here don't work with length limits. */
|
---|
291 | if (length > 0 && string[length] != '\0')
|
---|
292 | {
|
---|
293 | abuf = xmalloc(length+1);
|
---|
294 | memcpy(abuf, string, length);
|
---|
295 | abuf[length] = '\0';
|
---|
296 | string = abuf;
|
---|
297 | }
|
---|
298 | p = string;
|
---|
299 |
|
---|
300 | while (1)
|
---|
301 | {
|
---|
302 | /* Copy all following uninteresting chars all at once to the
|
---|
303 | variable output buffer, and skip them. Uninteresting chars end
|
---|
304 | at the next $ or the end of the input. */
|
---|
305 |
|
---|
306 | p1 = strchr (p, '$');
|
---|
307 |
|
---|
308 | o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1);
|
---|
309 |
|
---|
310 | if (p1 == 0)
|
---|
311 | break;
|
---|
312 | p = p1 + 1;
|
---|
313 |
|
---|
314 | /* Dispatch on the char that follows the $. */
|
---|
315 |
|
---|
316 | switch (*p)
|
---|
317 | {
|
---|
318 | case '$':
|
---|
319 | /* $$ seen means output one $ to the variable output buffer. */
|
---|
320 | o = variable_buffer_output (o, p, 1);
|
---|
321 | break;
|
---|
322 |
|
---|
323 | case '(':
|
---|
324 | case '{':
|
---|
325 | /* $(...) or ${...} is the general case of substitution. */
|
---|
326 | {
|
---|
327 | char openparen = *p;
|
---|
328 | char closeparen = (openparen == '(') ? ')' : '}';
|
---|
329 | const char *begp;
|
---|
330 | const char *beg = p + 1;
|
---|
331 | char *op;
|
---|
332 | char *abeg = NULL;
|
---|
333 | const char *end, *colon;
|
---|
334 |
|
---|
335 | op = o;
|
---|
336 | begp = p;
|
---|
337 | if (handle_function (&op, &begp))
|
---|
338 | {
|
---|
339 | o = op;
|
---|
340 | p = begp;
|
---|
341 | break;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /* Is there a variable reference inside the parens or braces?
|
---|
345 | If so, expand it before expanding the entire reference. */
|
---|
346 |
|
---|
347 | end = strchr (beg, closeparen);
|
---|
348 | if (end == 0)
|
---|
349 | /* Unterminated variable reference. */
|
---|
350 | fatal (*expanding_var, _("unterminated variable reference"));
|
---|
351 | p1 = lindex (beg, end, '$');
|
---|
352 | if (p1 != 0)
|
---|
353 | {
|
---|
354 | /* BEG now points past the opening paren or brace.
|
---|
355 | Count parens or braces until it is matched. */
|
---|
356 | int count = 0;
|
---|
357 | for (p = beg; *p != '\0'; ++p)
|
---|
358 | {
|
---|
359 | if (*p == openparen)
|
---|
360 | ++count;
|
---|
361 | else if (*p == closeparen && --count < 0)
|
---|
362 | break;
|
---|
363 | }
|
---|
364 | /* If COUNT is >= 0, there were unmatched opening parens
|
---|
365 | or braces, so we go to the simple case of a variable name
|
---|
366 | such as `$($(a)'. */
|
---|
367 | if (count < 0)
|
---|
368 | {
|
---|
369 | abeg = expand_argument (beg, p); /* Expand the name. */
|
---|
370 | beg = abeg;
|
---|
371 | end = strchr (beg, '\0');
|
---|
372 | }
|
---|
373 | }
|
---|
374 | else
|
---|
375 | /* Advance P to the end of this reference. After we are
|
---|
376 | finished expanding this one, P will be incremented to
|
---|
377 | continue the scan. */
|
---|
378 | p = end;
|
---|
379 |
|
---|
380 | /* This is not a reference to a built-in function and
|
---|
381 | any variable references inside are now expanded.
|
---|
382 | Is the resultant text a substitution reference? */
|
---|
383 |
|
---|
384 | colon = lindex (beg, end, ':');
|
---|
385 | if (colon)
|
---|
386 | {
|
---|
387 | /* This looks like a substitution reference: $(FOO:A=B). */
|
---|
388 | const char *subst_beg, *subst_end, *replace_beg, *replace_end;
|
---|
389 |
|
---|
390 | subst_beg = colon + 1;
|
---|
391 | subst_end = lindex (subst_beg, end, '=');
|
---|
392 | if (subst_end == 0)
|
---|
393 | /* There is no = in sight. Punt on the substitution
|
---|
394 | reference and treat this as a variable name containing
|
---|
395 | a colon, in the code below. */
|
---|
396 | colon = 0;
|
---|
397 | else
|
---|
398 | {
|
---|
399 | replace_beg = subst_end + 1;
|
---|
400 | replace_end = end;
|
---|
401 |
|
---|
402 | /* Extract the variable name before the colon
|
---|
403 | and look up that variable. */
|
---|
404 | v = lookup_variable (beg, colon - beg);
|
---|
405 | if (v == 0)
|
---|
406 | warn_undefined (beg, colon - beg);
|
---|
407 |
|
---|
408 | /* If the variable is not empty, perform the
|
---|
409 | substitution. */
|
---|
410 | if (v != 0 && *v->value != '\0')
|
---|
411 | {
|
---|
412 | char *pattern, *replace, *ppercent, *rpercent;
|
---|
413 | char *value = (v->recursive
|
---|
414 | ? recursively_expand (v)
|
---|
415 | : v->value);
|
---|
416 |
|
---|
417 | /* Copy the pattern and the replacement. Add in an
|
---|
418 | extra % at the beginning to use in case there
|
---|
419 | isn't one in the pattern. */
|
---|
420 | pattern = alloca (subst_end - subst_beg + 2);
|
---|
421 | *(pattern++) = '%';
|
---|
422 | memcpy (pattern, subst_beg, subst_end - subst_beg);
|
---|
423 | pattern[subst_end - subst_beg] = '\0';
|
---|
424 |
|
---|
425 | replace = alloca (replace_end - replace_beg + 2);
|
---|
426 | *(replace++) = '%';
|
---|
427 | memcpy (replace, replace_beg,
|
---|
428 | replace_end - replace_beg);
|
---|
429 | replace[replace_end - replace_beg] = '\0';
|
---|
430 |
|
---|
431 | /* Look for %. Set the percent pointers properly
|
---|
432 | based on whether we find one or not. */
|
---|
433 | ppercent = find_percent (pattern);
|
---|
434 | if (ppercent)
|
---|
435 | {
|
---|
436 | ++ppercent;
|
---|
437 | rpercent = find_percent (replace);
|
---|
438 | if (rpercent)
|
---|
439 | ++rpercent;
|
---|
440 | }
|
---|
441 | else
|
---|
442 | {
|
---|
443 | ppercent = pattern;
|
---|
444 | rpercent = replace;
|
---|
445 | --pattern;
|
---|
446 | --replace;
|
---|
447 | }
|
---|
448 |
|
---|
449 | o = patsubst_expand_pat (o, value, pattern, replace,
|
---|
450 | ppercent, rpercent);
|
---|
451 |
|
---|
452 | if (v->recursive)
|
---|
453 | free (value);
|
---|
454 | }
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | if (colon == 0)
|
---|
459 | /* This is an ordinary variable reference.
|
---|
460 | Look up the value of the variable. */
|
---|
461 | o = reference_variable (o, beg, end - beg);
|
---|
462 |
|
---|
463 | if (abeg)
|
---|
464 | free (abeg);
|
---|
465 | }
|
---|
466 | break;
|
---|
467 |
|
---|
468 | case '\0':
|
---|
469 | break;
|
---|
470 |
|
---|
471 | default:
|
---|
472 | if (isblank ((unsigned char)p[-1]))
|
---|
473 | break;
|
---|
474 |
|
---|
475 | /* A $ followed by a random char is a variable reference:
|
---|
476 | $a is equivalent to $(a). */
|
---|
477 | o = reference_variable (o, p, 1);
|
---|
478 |
|
---|
479 | break;
|
---|
480 | }
|
---|
481 |
|
---|
482 | if (*p == '\0')
|
---|
483 | break;
|
---|
484 | else
|
---|
485 | ++p;
|
---|
486 | }
|
---|
487 |
|
---|
488 | if (abuf)
|
---|
489 | free (abuf);
|
---|
490 |
|
---|
491 | variable_buffer_output (o, "", 1);
|
---|
492 | return (variable_buffer + line_offset);
|
---|
493 | }
|
---|
494 |
|
---|
495 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
496 | /* Scan STRING for variable references and expansion-function calls. Only
|
---|
497 | LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until
|
---|
498 | a null byte is found.
|
---|
499 |
|
---|
500 | Write the results to LINE, which must point into `variable_buffer'. If
|
---|
501 | LINE is NULL, start at the beginning of the buffer.
|
---|
502 | Return a pointer to LINE, or to the beginning of the buffer if LINE is
|
---|
503 | NULL. Set EOLP to point to the string terminator.
|
---|
504 | */
|
---|
505 | char *
|
---|
506 | variable_expand_string_2 (char *line, const char *string, long length, char **eolp)
|
---|
507 | {
|
---|
508 | struct variable *v;
|
---|
509 | const char *p, *p1, *eos;
|
---|
510 | char *o;
|
---|
511 | unsigned int line_offset;
|
---|
512 |
|
---|
513 | if (!line)
|
---|
514 | line = initialize_variable_output();
|
---|
515 | o = line;
|
---|
516 | line_offset = line - variable_buffer;
|
---|
517 |
|
---|
518 | if (length < 0)
|
---|
519 | length = strlen (string);
|
---|
520 | else
|
---|
521 | MY_ASSERT_MSG (string + length == (p1 = memchr (string, '\0', length)) || !p1, ("len=%ld p1=%p %s\n", length, p1, line));
|
---|
522 |
|
---|
523 | /* Simple 1: Emptry string. */
|
---|
524 |
|
---|
525 | if (length == 0)
|
---|
526 | {
|
---|
527 | o = variable_buffer_output (o, "\0", 2);
|
---|
528 | *eolp = o - 2;
|
---|
529 | return (variable_buffer + line_offset);
|
---|
530 | }
|
---|
531 |
|
---|
532 | /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */
|
---|
533 |
|
---|
534 | p1 = (const char *)memchr (string, '$', length);
|
---|
535 | if (p1 == 0)
|
---|
536 | {
|
---|
537 | o = variable_buffer_output (o, string, length);
|
---|
538 | o = variable_buffer_output (o, "\0", 2);
|
---|
539 | *eolp = o - 2;
|
---|
540 | assert (strchr (variable_buffer + line_offset, '\0') == *eolp);
|
---|
541 | return (variable_buffer + line_offset);
|
---|
542 | }
|
---|
543 |
|
---|
544 | p = string;
|
---|
545 | eos = p + length;
|
---|
546 |
|
---|
547 | while (1)
|
---|
548 | {
|
---|
549 | /* Copy all following uninteresting chars all at once to the
|
---|
550 | variable output buffer, and skip them. Uninteresting chars end
|
---|
551 | at the next $ or the end of the input. */
|
---|
552 |
|
---|
553 | o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p));
|
---|
554 |
|
---|
555 | if (p1 == 0)
|
---|
556 | break;
|
---|
557 | p = p1 + 1;
|
---|
558 |
|
---|
559 | /* Dispatch on the char that follows the $. */
|
---|
560 |
|
---|
561 | switch (*p)
|
---|
562 | {
|
---|
563 | case '$':
|
---|
564 | /* $$ seen means output one $ to the variable output buffer. */
|
---|
565 | o = variable_buffer_output (o, p, 1);
|
---|
566 | break;
|
---|
567 |
|
---|
568 | case '(':
|
---|
569 | case '{':
|
---|
570 | /* $(...) or ${...} is the general case of substitution. */
|
---|
571 | {
|
---|
572 | char openparen = *p;
|
---|
573 | char closeparen = (openparen == '(') ? ')' : '}';
|
---|
574 | const char *begp;
|
---|
575 | const char *beg = p + 1;
|
---|
576 | char *op;
|
---|
577 | char *abeg = NULL;
|
---|
578 | unsigned int alen = 0;
|
---|
579 | const char *end, *colon;
|
---|
580 |
|
---|
581 | op = o;
|
---|
582 | begp = p;
|
---|
583 | end = may_be_function_name (p + 1, eos);
|
---|
584 | if ( end
|
---|
585 | && handle_function (&op, &begp, end, eos))
|
---|
586 | {
|
---|
587 | o = op;
|
---|
588 | p = begp;
|
---|
589 | MY_ASSERT_MSG (!(p1 = memchr (variable_buffer + line_offset, '\0', o - (variable_buffer + line_offset))),
|
---|
590 | ("line=%p o/exp_end=%p act_end=%p\n", variable_buffer + line_offset, o, p1));
|
---|
591 | break;
|
---|
592 | }
|
---|
593 |
|
---|
594 | /* Is there a variable reference inside the parens or braces?
|
---|
595 | If so, expand it before expanding the entire reference. */
|
---|
596 |
|
---|
597 | end = memchr (beg, closeparen, eos - beg);
|
---|
598 | if (end == 0)
|
---|
599 | /* Unterminated variable reference. */
|
---|
600 | fatal (*expanding_var, _("unterminated variable reference"));
|
---|
601 | p1 = lindex (beg, end, '$');
|
---|
602 | if (p1 != 0)
|
---|
603 | {
|
---|
604 | /* BEG now points past the opening paren or brace.
|
---|
605 | Count parens or braces until it is matched. */
|
---|
606 | int count = 0;
|
---|
607 | for (p = beg; p < eos; ++p)
|
---|
608 | {
|
---|
609 | if (*p == openparen)
|
---|
610 | ++count;
|
---|
611 | else if (*p == closeparen && --count < 0)
|
---|
612 | break;
|
---|
613 | }
|
---|
614 | /* If COUNT is >= 0, there were unmatched opening parens
|
---|
615 | or braces, so we go to the simple case of a variable name
|
---|
616 | such as `$($(a)'. */
|
---|
617 | if (count < 0)
|
---|
618 | {
|
---|
619 | unsigned int len;
|
---|
620 | char saved;
|
---|
621 |
|
---|
622 | /* Expand the name. */
|
---|
623 | saved = *p;
|
---|
624 | *(char *)p = '\0'; /* XXX: proove that this is safe! XXX2: shouldn't be necessary any longer! */
|
---|
625 | abeg = allocated_variable_expand_3 (beg, p - beg, &len, &alen);
|
---|
626 | beg = abeg;
|
---|
627 | end = beg + len;
|
---|
628 | *(char *)p = saved;
|
---|
629 | }
|
---|
630 | }
|
---|
631 | else
|
---|
632 | /* Advance P to the end of this reference. After we are
|
---|
633 | finished expanding this one, P will be incremented to
|
---|
634 | continue the scan. */
|
---|
635 | p = end;
|
---|
636 |
|
---|
637 | /* This is not a reference to a built-in function and
|
---|
638 | any variable references inside are now expanded.
|
---|
639 | Is the resultant text a substitution reference? */
|
---|
640 |
|
---|
641 | colon = lindex (beg, end, ':');
|
---|
642 | if (colon)
|
---|
643 | {
|
---|
644 | /* This looks like a substitution reference: $(FOO:A=B). */
|
---|
645 | const char *subst_beg, *subst_end, *replace_beg, *replace_end;
|
---|
646 |
|
---|
647 | subst_beg = colon + 1;
|
---|
648 | subst_end = lindex (subst_beg, end, '=');
|
---|
649 | if (subst_end == 0)
|
---|
650 | /* There is no = in sight. Punt on the substitution
|
---|
651 | reference and treat this as a variable name containing
|
---|
652 | a colon, in the code below. */
|
---|
653 | colon = 0;
|
---|
654 | else
|
---|
655 | {
|
---|
656 | replace_beg = subst_end + 1;
|
---|
657 | replace_end = end;
|
---|
658 |
|
---|
659 | /* Extract the variable name before the colon
|
---|
660 | and look up that variable. */
|
---|
661 | v = lookup_variable (beg, colon - beg);
|
---|
662 | if (v == 0)
|
---|
663 | warn_undefined (beg, colon - beg);
|
---|
664 |
|
---|
665 | /* If the variable is not empty, perform the
|
---|
666 | substitution. */
|
---|
667 | if (v != 0 && *v->value != '\0')
|
---|
668 | {
|
---|
669 | char *pattern, *replace, *ppercent, *rpercent;
|
---|
670 | char *value = (v->recursive
|
---|
671 | ? recursively_expand (v)
|
---|
672 | : v->value);
|
---|
673 |
|
---|
674 | /* Copy the pattern and the replacement. Add in an
|
---|
675 | extra % at the beginning to use in case there
|
---|
676 | isn't one in the pattern. */
|
---|
677 | pattern = alloca (subst_end - subst_beg + 2);
|
---|
678 | *(pattern++) = '%';
|
---|
679 | memcpy (pattern, subst_beg, subst_end - subst_beg);
|
---|
680 | pattern[subst_end - subst_beg] = '\0';
|
---|
681 |
|
---|
682 | replace = alloca (replace_end - replace_beg + 2);
|
---|
683 | *(replace++) = '%';
|
---|
684 | memcpy (replace, replace_beg,
|
---|
685 | replace_end - replace_beg);
|
---|
686 | replace[replace_end - replace_beg] = '\0';
|
---|
687 |
|
---|
688 | /* Look for %. Set the percent pointers properly
|
---|
689 | based on whether we find one or not. */
|
---|
690 | ppercent = find_percent (pattern);
|
---|
691 | if (ppercent)
|
---|
692 | {
|
---|
693 | ++ppercent;
|
---|
694 | rpercent = find_percent (replace);
|
---|
695 | if (rpercent)
|
---|
696 | ++rpercent;
|
---|
697 | }
|
---|
698 | else
|
---|
699 | {
|
---|
700 | ppercent = pattern;
|
---|
701 | rpercent = replace;
|
---|
702 | --pattern;
|
---|
703 | --replace;
|
---|
704 | }
|
---|
705 |
|
---|
706 | o = patsubst_expand_pat (o, value, pattern, replace,
|
---|
707 | ppercent, rpercent);
|
---|
708 |
|
---|
709 | if (v->recursive)
|
---|
710 | free (value);
|
---|
711 | }
|
---|
712 | }
|
---|
713 | }
|
---|
714 |
|
---|
715 | if (colon == 0)
|
---|
716 | /* This is an ordinary variable reference.
|
---|
717 | Look up the value of the variable. */
|
---|
718 | o = reference_variable (o, beg, end - beg);
|
---|
719 |
|
---|
720 | if (abeg)
|
---|
721 | recycle_variable_buffer (abeg, alen);
|
---|
722 | }
|
---|
723 | break;
|
---|
724 |
|
---|
725 | case '\0':
|
---|
726 | assert (p == eos);
|
---|
727 | break;
|
---|
728 |
|
---|
729 | default:
|
---|
730 | if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */
|
---|
731 | break;
|
---|
732 |
|
---|
733 | /* A $ followed by a random char is a variable reference:
|
---|
734 | $a is equivalent to $(a). */
|
---|
735 | o = reference_variable (o, p, 1);
|
---|
736 |
|
---|
737 | break;
|
---|
738 | }
|
---|
739 |
|
---|
740 | if (++p >= eos)
|
---|
741 | break;
|
---|
742 | p1 = memchr (p, '$', eos - p);
|
---|
743 | }
|
---|
744 |
|
---|
745 | o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */
|
---|
746 | *eolp = o - 2;
|
---|
747 | MY_ASSERT_MSG (strchr (variable_buffer + line_offset, '\0') == *eolp,
|
---|
748 | ("expected=%d actual=%d\nlength=%ld string=%.*s\n",
|
---|
749 | (int)(*eolp - variable_buffer + line_offset), (int)strlen(variable_buffer + line_offset),
|
---|
750 | length, (int)length, string));
|
---|
751 | return (variable_buffer + line_offset);
|
---|
752 | }
|
---|
753 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
754 | |
---|
755 |
|
---|
756 | /* Scan LINE for variable references and expansion-function calls.
|
---|
757 | Build in `variable_buffer' the result of expanding the references and calls.
|
---|
758 | Return the address of the resulting string, which is null-terminated
|
---|
759 | and is valid only until the next time this function is called. */
|
---|
760 |
|
---|
761 | char *
|
---|
762 | variable_expand (const char *line)
|
---|
763 | {
|
---|
764 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
765 | return variable_expand_string(NULL, line, (long)-1);
|
---|
766 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
767 | char *s;
|
---|
768 |
|
---|
769 | /* this function is abused a lot like this: variable_expand(""). */
|
---|
770 | if (!*line)
|
---|
771 | {
|
---|
772 | s = variable_buffer_output (initialize_variable_output (), "\0", 2);
|
---|
773 | return s - 2;
|
---|
774 | }
|
---|
775 | return variable_expand_string_2 (NULL, line, (long)-1, &s);
|
---|
776 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
777 | }
|
---|
778 | |
---|
779 |
|
---|
780 | /* Expand an argument for an expansion function.
|
---|
781 | The text starting at STR and ending at END is variable-expanded
|
---|
782 | into a null-terminated string that is returned as the value.
|
---|
783 | This is done without clobbering `variable_buffer' or the current
|
---|
784 | variable-expansion that is in progress. */
|
---|
785 |
|
---|
786 | char *
|
---|
787 | expand_argument (const char *str, const char *end)
|
---|
788 | {
|
---|
789 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
790 | char *tmp;
|
---|
791 | #endif
|
---|
792 |
|
---|
793 | if (str == end)
|
---|
794 | return xstrdup("");
|
---|
795 |
|
---|
796 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
797 | if (!end || *end == '\0')
|
---|
798 | return allocated_variable_expand (str);
|
---|
799 |
|
---|
800 | tmp = alloca (end - str + 1);
|
---|
801 | memcpy (tmp, str, end - str);
|
---|
802 | tmp[end - str] = '\0';
|
---|
803 |
|
---|
804 | return allocated_variable_expand (tmp);
|
---|
805 | #else /* CONFIG_WITH_VALUE_LENGTH */
|
---|
806 | if (!end)
|
---|
807 | return allocated_variable_expand_2 (str, ~0U, NULL);
|
---|
808 | return allocated_variable_expand_2 (str, end - str, NULL);
|
---|
809 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
810 | }
|
---|
811 | |
---|
812 |
|
---|
813 | /* Expand LINE for FILE. Error messages refer to the file and line where
|
---|
814 | FILE's commands were found. Expansion uses FILE's variable set list. */
|
---|
815 |
|
---|
816 | char *
|
---|
817 | variable_expand_for_file (const char *line, struct file *file)
|
---|
818 | {
|
---|
819 | char *result;
|
---|
820 | struct variable_set_list *save;
|
---|
821 |
|
---|
822 | if (file == 0)
|
---|
823 | return variable_expand (line);
|
---|
824 |
|
---|
825 | save = current_variable_set_list;
|
---|
826 | current_variable_set_list = file->variables;
|
---|
827 | if (file->cmds && file->cmds->fileinfo.filenm)
|
---|
828 | reading_file = &file->cmds->fileinfo;
|
---|
829 | else
|
---|
830 | reading_file = 0;
|
---|
831 | result = variable_expand (line);
|
---|
832 | current_variable_set_list = save;
|
---|
833 | reading_file = 0;
|
---|
834 |
|
---|
835 | return result;
|
---|
836 | }
|
---|
837 | |
---|
838 |
|
---|
839 | #if defined (CONFIG_WITH_VALUE_LENGTH) || defined (CONFIG_WITH_COMMANDS_FUNC)
|
---|
840 | /* Expand LINE for FILE. Error messages refer to the file and line where
|
---|
841 | FILE's commands were found. Expansion uses FILE's variable set list.
|
---|
842 |
|
---|
843 | Differs from variable_expand_for_file in that it takes a pointer to
|
---|
844 | where in the variable buffer to start outputting the expanded string,
|
---|
845 | and that it can returned the length of the string if you wish. */
|
---|
846 |
|
---|
847 | char *
|
---|
848 | variable_expand_for_file_2 (char *o, const char *line, unsigned int length,
|
---|
849 | struct file *file, unsigned int *value_lenp)
|
---|
850 | {
|
---|
851 | char *result;
|
---|
852 | struct variable_set_list *savev;
|
---|
853 | const struct floc *savef;
|
---|
854 | long len = length == ~0U ? (long)-1 : (long)length;
|
---|
855 | char *eol;
|
---|
856 |
|
---|
857 | if (!o)
|
---|
858 | o = initialize_variable_output();
|
---|
859 |
|
---|
860 | if (file == 0)
|
---|
861 | result = variable_expand_string_2 (o, line, len, &eol);
|
---|
862 | else
|
---|
863 | {
|
---|
864 | savev = current_variable_set_list;
|
---|
865 | current_variable_set_list = file->variables;
|
---|
866 |
|
---|
867 | savef = reading_file;
|
---|
868 | if (file->cmds && file->cmds->fileinfo.filenm)
|
---|
869 | reading_file = &file->cmds->fileinfo;
|
---|
870 | else
|
---|
871 | reading_file = 0;
|
---|
872 |
|
---|
873 | result = variable_expand_string_2 (o, line, len, &eol);
|
---|
874 |
|
---|
875 | current_variable_set_list = savev;
|
---|
876 | reading_file = savef;
|
---|
877 | }
|
---|
878 |
|
---|
879 | if (value_lenp)
|
---|
880 | *value_lenp = eol - result;
|
---|
881 |
|
---|
882 | return result;
|
---|
883 | }
|
---|
884 | |
---|
885 |
|
---|
886 | #endif /* CONFIG_WITH_VALUE_LENGTH || CONFIG_WITH_COMMANDS_FUNC */
|
---|
887 | /* Like allocated_variable_expand, but for += target-specific variables.
|
---|
888 | First recursively construct the variable value from its appended parts in
|
---|
889 | any upper variable sets. Then expand the resulting value. */
|
---|
890 |
|
---|
891 | static char *
|
---|
892 | variable_append (const char *name, unsigned int length,
|
---|
893 | const struct variable_set_list *set)
|
---|
894 | {
|
---|
895 | const struct variable *v;
|
---|
896 | char *buf = 0;
|
---|
897 |
|
---|
898 | /* If there's nothing left to check, return the empty buffer. */
|
---|
899 | if (!set)
|
---|
900 | return initialize_variable_output ();
|
---|
901 |
|
---|
902 | /* Try to find the variable in this variable set. */
|
---|
903 | v = lookup_variable_in_set (name, length, set->set);
|
---|
904 |
|
---|
905 | /* If there isn't one, look to see if there's one in a set above us. */
|
---|
906 | if (!v)
|
---|
907 | return variable_append (name, length, set->next);
|
---|
908 |
|
---|
909 | /* If this variable type is append, first get any upper values.
|
---|
910 | If not, initialize the buffer. */
|
---|
911 | if (v->append)
|
---|
912 | buf = variable_append (name, length, set->next);
|
---|
913 | else
|
---|
914 | buf = initialize_variable_output ();
|
---|
915 |
|
---|
916 | /* Append this value to the buffer, and return it.
|
---|
917 | If we already have a value, first add a space. */
|
---|
918 | if (buf > variable_buffer)
|
---|
919 | buf = variable_buffer_output (buf, " ", 1);
|
---|
920 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
921 | assert ((unsigned int)v->value_length == strlen (v->value)); /* FIXME */
|
---|
922 | #endif
|
---|
923 |
|
---|
924 | /* Either expand it or copy it, depending. */
|
---|
925 | if (! v->recursive)
|
---|
926 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
927 | return variable_buffer_output (buf, v->value, v->value_length);
|
---|
928 | #else
|
---|
929 | return variable_buffer_output (buf, v->value, strlen (v->value));
|
---|
930 | #endif
|
---|
931 |
|
---|
932 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
933 | variable_expand_string_2 (buf, v->value, v->value_length, &buf);
|
---|
934 | return buf;
|
---|
935 | #else
|
---|
936 | buf = variable_expand_string (buf, v->value, strlen (v->value));
|
---|
937 | return (buf + strlen (buf));
|
---|
938 | #endif
|
---|
939 | }
|
---|
940 |
|
---|
941 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
942 | /* Expands the specified string, appending it to the specified
|
---|
943 | variable value. */
|
---|
944 | void
|
---|
945 | append_expanded_string_to_variable (struct variable *v, const char *value,
|
---|
946 | unsigned int value_len, int append)
|
---|
947 | {
|
---|
948 | char *p = (char *) memchr (value, '$', value_len);
|
---|
949 | if (!p)
|
---|
950 | /* fast path */
|
---|
951 | append_string_to_variable (v,value, value_len, append);
|
---|
952 | else if (value_len)
|
---|
953 | {
|
---|
954 | unsigned int off_dollar = p - (char *)value;
|
---|
955 |
|
---|
956 | /* Install a fresh variable buffer. */
|
---|
957 | char *saved_buffer;
|
---|
958 | unsigned int saved_buffer_length;
|
---|
959 | install_variable_buffer (&saved_buffer, &saved_buffer_length);
|
---|
960 |
|
---|
961 | p = variable_buffer;
|
---|
962 | if (append || !v->value_length)
|
---|
963 | {
|
---|
964 | /* Copy the current value into it and append a space. */
|
---|
965 | if (v->value_length)
|
---|
966 | {
|
---|
967 | p = variable_buffer_output (p, v->value, v->value_length);
|
---|
968 | p = variable_buffer_output (p, " ", 1);
|
---|
969 | }
|
---|
970 |
|
---|
971 | /* Append the assignment value. */
|
---|
972 | p = variable_buffer_output (p, value, off_dollar);
|
---|
973 | variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
|
---|
974 | }
|
---|
975 | else
|
---|
976 | {
|
---|
977 | /* Expand the assignemnt value. */
|
---|
978 | p = variable_buffer_output (p, value, off_dollar);
|
---|
979 | variable_expand_string_2 (p, value + off_dollar, value_len - off_dollar, &p);
|
---|
980 |
|
---|
981 | /* Append a space followed by the old value. */
|
---|
982 | p = variable_buffer_output (p, " ", 1);
|
---|
983 | p = variable_buffer_output (p, v->value, v->value_length + 1) - 1;
|
---|
984 | }
|
---|
985 |
|
---|
986 | /* Replace the variable with the variable buffer. */
|
---|
987 | #ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
|
---|
988 | if (v->rdonly_val)
|
---|
989 | v->rdonly_val = 0;
|
---|
990 | else
|
---|
991 | #endif
|
---|
992 | free (v->value);
|
---|
993 | v->value = variable_buffer;
|
---|
994 | v->value_length = p - v->value;
|
---|
995 | v->value_alloc_len = variable_buffer_length;
|
---|
996 |
|
---|
997 | /* Restore the variable buffer, but without freeing the current. */
|
---|
998 | variable_buffer = NULL;
|
---|
999 | restore_variable_buffer (saved_buffer, saved_buffer_length);
|
---|
1000 | }
|
---|
1001 | /* else: Drop empty strings. Use $(NO_SUCH_VARIABLE) if a space is wanted. */
|
---|
1002 | }
|
---|
1003 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
1004 |
|
---|
1005 | static char *
|
---|
1006 | allocated_variable_append (const struct variable *v)
|
---|
1007 | {
|
---|
1008 | char *val;
|
---|
1009 |
|
---|
1010 | /* Construct the appended variable value. */
|
---|
1011 |
|
---|
1012 | char *obuf = variable_buffer;
|
---|
1013 | unsigned int olen = variable_buffer_length;
|
---|
1014 |
|
---|
1015 | variable_buffer = 0;
|
---|
1016 |
|
---|
1017 | assert ((unsigned int)v->length == strlen (v->name)); /* bird */
|
---|
1018 | val = variable_append (v->name, strlen (v->name), current_variable_set_list);
|
---|
1019 | variable_buffer_output (val, "", 1);
|
---|
1020 | val = variable_buffer;
|
---|
1021 |
|
---|
1022 | variable_buffer = obuf;
|
---|
1023 | variable_buffer_length = olen;
|
---|
1024 |
|
---|
1025 | return val;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | /* Like variable_expand_for_file, but the returned string is malloc'd.
|
---|
1029 | This function is called a lot. It wants to be efficient. */
|
---|
1030 |
|
---|
1031 | char *
|
---|
1032 | allocated_variable_expand_for_file (const char *line, struct file *file)
|
---|
1033 | {
|
---|
1034 | char *value;
|
---|
1035 |
|
---|
1036 | char *obuf = variable_buffer;
|
---|
1037 | unsigned int olen = variable_buffer_length;
|
---|
1038 |
|
---|
1039 | variable_buffer = 0;
|
---|
1040 |
|
---|
1041 | value = variable_expand_for_file (line, file);
|
---|
1042 |
|
---|
1043 | #if 0
|
---|
1044 | /* Waste a little memory and save time. */
|
---|
1045 | value = xrealloc (value, strlen (value))
|
---|
1046 | #endif
|
---|
1047 |
|
---|
1048 | variable_buffer = obuf;
|
---|
1049 | variable_buffer_length = olen;
|
---|
1050 |
|
---|
1051 | return value;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | #ifdef CONFIG_WITH_VALUE_LENGTH
|
---|
1055 | /* Handle the most common case in allocated_variable_expand_for_file
|
---|
1056 | specially and provide some additional string lenght features. */
|
---|
1057 |
|
---|
1058 | char *
|
---|
1059 | allocated_variable_expand_2 (const char *line, unsigned int length,
|
---|
1060 | unsigned int *value_lenp)
|
---|
1061 | {
|
---|
1062 | char *value;
|
---|
1063 | char *obuf = variable_buffer;
|
---|
1064 | unsigned int olen = variable_buffer_length;
|
---|
1065 | long len = length == ~0U ? -1L : (long)length;
|
---|
1066 | char *eol;
|
---|
1067 |
|
---|
1068 | variable_buffer = 0;
|
---|
1069 |
|
---|
1070 | value = variable_expand_string_2 (NULL, line, len, &eol);
|
---|
1071 | if (value_lenp)
|
---|
1072 | *value_lenp = eol - value;
|
---|
1073 |
|
---|
1074 | variable_buffer = obuf;
|
---|
1075 | variable_buffer_length = olen;
|
---|
1076 |
|
---|
1077 | return value;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /* Initially created for handling a special case for variable_expand_string2
|
---|
1081 | where the variable name is expanded and freed right afterwards. This
|
---|
1082 | variant allows the variable_buffer to be recycled and thus avoid bothering
|
---|
1083 | with a slow free implementation. (Darwin is horrible slow.) */
|
---|
1084 |
|
---|
1085 | char *
|
---|
1086 | allocated_variable_expand_3 (const char *line, unsigned int length,
|
---|
1087 | unsigned int *value_lenp,
|
---|
1088 | unsigned int *buffer_lengthp)
|
---|
1089 | {
|
---|
1090 | char *obuf = variable_buffer;
|
---|
1091 | unsigned int olen = variable_buffer_length;
|
---|
1092 | long len = (long)length;
|
---|
1093 | char *value;
|
---|
1094 | char *eol;
|
---|
1095 |
|
---|
1096 | variable_buffer = 0;
|
---|
1097 |
|
---|
1098 | value = variable_expand_string_2 (NULL, line, len, &eol);
|
---|
1099 | if (value_lenp)
|
---|
1100 | *value_lenp = eol - value;
|
---|
1101 | *buffer_lengthp = variable_buffer_length;
|
---|
1102 |
|
---|
1103 | variable_buffer = obuf;
|
---|
1104 | variable_buffer_length = olen;
|
---|
1105 |
|
---|
1106 | return value;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | /* recycle a buffer. */
|
---|
1110 |
|
---|
1111 | void
|
---|
1112 | recycle_variable_buffer (char *buffer, unsigned int length)
|
---|
1113 | {
|
---|
1114 | struct recycled_buffer *recycled = (struct recycled_buffer *)buffer;
|
---|
1115 |
|
---|
1116 | assert (!(length & 31));
|
---|
1117 | assert (length >= 384);
|
---|
1118 | recycled->length = length;
|
---|
1119 | recycled->next = recycled_head;
|
---|
1120 | recycled_head = recycled;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | #endif /* CONFIG_WITH_VALUE_LENGTH */
|
---|
1124 |
|
---|
1125 | /* Install a new variable_buffer context, returning the current one for
|
---|
1126 | safe-keeping. */
|
---|
1127 |
|
---|
1128 | void
|
---|
1129 | install_variable_buffer (char **bufp, unsigned int *lenp)
|
---|
1130 | {
|
---|
1131 | *bufp = variable_buffer;
|
---|
1132 | *lenp = variable_buffer_length;
|
---|
1133 |
|
---|
1134 | variable_buffer = 0;
|
---|
1135 | initialize_variable_output ();
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | /* Restore a previously-saved variable_buffer setting (free the current one).
|
---|
1139 | */
|
---|
1140 |
|
---|
1141 | void
|
---|
1142 | restore_variable_buffer (char *buf, unsigned int len)
|
---|
1143 | {
|
---|
1144 | #ifndef CONFIG_WITH_VALUE_LENGTH
|
---|
1145 | free (variable_buffer);
|
---|
1146 | #else
|
---|
1147 | if (variable_buffer)
|
---|
1148 | recycle_variable_buffer (variable_buffer, variable_buffer_length);
|
---|
1149 | #endif
|
---|
1150 |
|
---|
1151 | variable_buffer = buf;
|
---|
1152 | variable_buffer_length = len;
|
---|
1153 | }
|
---|