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