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