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