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