Changeset 3138 in kBuild for vendor/gnumake/current/expand.c
- Timestamp:
- Mar 12, 2018 7:32:29 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/gnumake/current/expand.c
r2596 r3138 1 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, 2007, 2008, 2009, 4 2010 Free Software Foundation, Inc. 2 Copyright (C) 1988-2016 Free Software Foundation, Inc. 5 3 This file is part of GNU Make. 6 4 … … 17 15 this program. If not, see <http://www.gnu.org/licenses/>. */ 18 16 19 #include "make .h"17 #include "makeint.h" 20 18 21 19 #include <assert.h> … … 29 27 /* Initially, any errors reported when expanding strings will be reported 30 28 against the file where the error appears. */ 31 const structfloc **expanding_var = &reading_file;29 const floc **expanding_var = &reading_file; 32 30 33 31 /* The next two describe the variable output buffer. … … 64 62 unsigned int offset = ptr - variable_buffer; 65 63 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length 66 67 64 ? newlen + 100 65 : 2 * variable_buffer_length); 68 66 variable_buffer = xrealloc (variable_buffer, variable_buffer_length); 69 67 ptr = variable_buffer + offset; … … 100 98 { 101 99 char *value; 102 const structfloc *this_var;103 const structfloc **saved_varp;100 const floc *this_var; 101 const floc **saved_varp; 104 102 struct variable_set_list *save = 0; 105 103 int set_reading = 0; … … 125 123 if (!v->exp_count) 126 124 /* Expanding V causes infinite recursion. Lose. */ 127 fatal (*expanding_var,128 _("Recursive variable `%s' references itself (eventually)"),129 125 OS (fatal, *expanding_var, 126 _("Recursive variable '%s' references itself (eventually)"), 127 v->name); 130 128 --v->exp_count; 131 129 } … … 190 188 a null byte is found. 191 189 192 Write the results to LINE, which must point into `variable_buffer'. If190 Write the results to LINE, which must point into 'variable_buffer'. If 193 191 LINE is NULL, start at the beginning of the buffer. 194 192 Return a pointer to LINE, or to the beginning of the buffer if LINE is … … 200 198 struct variable *v; 201 199 const char *p, *p1; 202 char * abuf = NULL;200 char *save; 203 201 char *o; 204 202 unsigned int line_offset; 205 203 206 204 if (!line) 207 line = initialize_variable_output ();205 line = initialize_variable_output (); 208 206 o = line; 209 207 line_offset = line - variable_buffer; … … 215 213 } 216 214 217 /* If we want a subset of the string, allocate a temporary buffer for it. 218 Most of the functions we use here don't work with length limits. */ 219 if (length > 0 && string[length] != '\0') 220 { 221 abuf = xmalloc(length+1); 222 memcpy(abuf, string, length); 223 abuf[length] = '\0'; 224 string = abuf; 225 } 226 p = string; 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. */ 218 save = length < 0 ? xstrdup (string) : xstrndup (string, length); 219 p = save; 227 220 228 221 while (1) … … 230 223 /* Copy all following uninteresting chars all at once to the 231 224 variable output buffer, and skip them. Uninteresting chars end 232 225 at the next $ or the end of the input. */ 233 226 234 227 p1 = strchr (p, '$'); … … 237 230 238 231 if (p1 == 0) 239 232 break; 240 233 p = p1 + 1; 241 234 … … 243 236 244 237 switch (*p) 245 { 246 case '$': 247 /* $$ seen means output one $ to the variable output buffer. */ 248 o = variable_buffer_output (o, p, 1); 249 break; 250 251 case '(': 252 case '{': 253 /* $(...) or ${...} is the general case of substitution. */ 254 { 255 char openparen = *p; 256 char closeparen = (openparen == '(') ? ')' : '}'; 238 { 239 case '$': 240 case '\0': 241 /* $$ or $ at the end of the string means output one $ to the 242 variable output buffer. */ 243 o = variable_buffer_output (o, p1, 1); 244 break; 245 246 case '(': 247 case '{': 248 /* $(...) or ${...} is the general case of substitution. */ 249 { 250 char openparen = *p; 251 char closeparen = (openparen == '(') ? ')' : '}'; 257 252 const char *begp; 258 259 253 const char *beg = p + 1; 254 char *op; 260 255 char *abeg = NULL; 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 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; 264 break; 265 } 266 267 /* Is there a variable reference inside the parens or braces? 268 If so, expand it before expanding the entire reference. */ 269 270 end = strchr (beg, closeparen); 271 if (end == 0) 277 272 /* Unterminated variable reference. */ 278 fatal (*expanding_var, _("unterminated variable reference"));279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 such as `$($(a)'. */295 296 297 298 299 300 301 302 303 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 } 297 else 298 /* Advance P to the end of this reference. After we are 304 299 finished expanding this one, P will be incremented to 305 300 continue the scan. */ 306 p = end; 307 308 /* This is not a reference to a built-in function and 309 any variable references inside are now expanded. 310 Is the resultant text a substitution reference? */ 311 312 colon = lindex (beg, end, ':'); 313 if (colon) 314 { 315 /* This looks like a substitution reference: $(FOO:A=B). */ 316 const char *subst_beg, *subst_end, *replace_beg, *replace_end; 317 318 subst_beg = colon + 1; 319 subst_end = lindex (subst_beg, end, '='); 320 if (subst_end == 0) 321 /* There is no = in sight. Punt on the substitution 322 reference and treat this as a variable name containing 323 a colon, in the code below. */ 324 colon = 0; 325 else 326 { 327 replace_beg = subst_end + 1; 328 replace_end = end; 329 330 /* Extract the variable name before the colon 331 and look up that variable. */ 332 v = lookup_variable (beg, colon - beg); 333 if (v == 0) 334 warn_undefined (beg, colon - beg); 301 p = end; 302 303 /* This is not a reference to a built-in function and 304 any variable references inside are now expanded. 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 { 320 const char *replace_beg = subst_end + 1; 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); 326 if (v == 0) 327 warn_undefined (beg, colon - beg); 335 328 336 329 /* If the variable is not empty, perform the 337 330 substitution. */ 338 339 340 341 331 if (v != 0 && *v->value != '\0') 332 { 333 char *pattern, *replace, *ppercent, *rpercent; 334 char *value = (v->recursive 342 335 ? recursively_expand (v) 343 336 : v->value); 344 337 345 338 /* Copy the pattern and the replacement. Add in an … … 359 352 /* Look for %. Set the percent pointers properly 360 353 based on whether we find one or not. */ 361 362 354 ppercent = find_percent (pattern); 355 if (ppercent) 363 356 { 364 357 ++ppercent; … … 367 360 ++rpercent; 368 361 } 369 362 else 370 363 { 371 364 ppercent = pattern; … … 378 371 ppercent, rpercent); 379 372 380 if (v->recursive) 381 free (value); 382 } 383 } 384 } 385 386 if (colon == 0) 387 /* This is an ordinary variable reference. 388 Look up the value of the variable. */ 389 o = reference_variable (o, beg, end - beg); 390 391 if (abeg) 392 free (abeg); 393 } 394 break; 395 396 case '\0': 397 break; 398 399 default: 400 if (isblank ((unsigned char)p[-1])) 401 break; 402 403 /* A $ followed by a random char is a variable reference: 404 $a is equivalent to $(a). */ 373 if (v->recursive) 374 free (value); 375 } 376 } 377 } 378 379 if (colon == 0) 380 /* This is an ordinary variable reference. 381 Look up the value of the variable. */ 382 o = reference_variable (o, beg, end - beg); 383 384 free (abeg); 385 } 386 break; 387 388 default: 389 if (ISSPACE (p[-1])) 390 break; 391 392 /* A $ followed by a random char is a variable reference: 393 $a is equivalent to $(a). */ 405 394 o = reference_variable (o, p, 1); 406 395 407 408 396 break; 397 } 409 398 410 399 if (*p == '\0') 411 400 break; 412 401 413 402 ++p; 414 403 } 415 404 416 if (abuf) 417 free (abuf); 405 free (save); 418 406 419 407 variable_buffer_output (o, "", 1); … … 423 411 424 412 /* Scan LINE for variable references and expansion-function calls. 425 Build in `variable_buffer' the result of expanding the references and calls.413 Build in 'variable_buffer' the result of expanding the references and calls. 426 414 Return the address of the resulting string, which is null-terminated 427 415 and is valid only until the next time this function is called. */ … … 430 418 variable_expand (const char *line) 431 419 { 432 return variable_expand_string (NULL, line, (long)-1);420 return variable_expand_string (NULL, line, (long)-1); 433 421 } 434 422 … … 437 425 The text starting at STR and ending at END is variable-expanded 438 426 into a null-terminated string that is returned as the value. 439 This is done without clobbering `variable_buffer' or the current427 This is done without clobbering 'variable_buffer' or the current 440 428 variable-expansion that is in progress. */ 441 429 … … 447 435 448 436 if (str == end) 449 return xstrdup ("");437 return xstrdup (""); 450 438 451 439 if (!end || *end == '\0') … … 462 450 r = allocated_variable_expand (tmp); 463 451 464 if (alloc) 465 free (alloc); 452 free (alloc); 466 453 467 454 return r; … … 477 464 char *result; 478 465 struct variable_set_list *savev; 479 const structfloc *savef;466 const floc *savef; 480 467 481 468 if (file == 0) … … 506 493 static char * 507 494 variable_append (const char *name, unsigned int length, 508 const struct variable_set_list *set )495 const struct variable_set_list *set, int local) 509 496 { 510 497 const struct variable *v; 511 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; 512 501 513 502 /* If there's nothing left to check, return the empty buffer. */ … … 518 507 v = lookup_variable_in_set (name, length, set->set); 519 508 520 /* If there isn't one, look to see if there's one in aset above us. */521 if (!v )522 return variable_append (name, length, set->next );509 /* If there isn't one, or this one is private, try the set above us. */ 510 if (!v || (!local && v->private_var)) 511 return variable_append (name, length, set->next, nextlocal); 523 512 524 513 /* If this variable type is append, first get any upper values. 525 514 If not, initialize the buffer. */ 526 515 if (v->append) 527 buf = variable_append (name, length, set->next );516 buf = variable_append (name, length, set->next, nextlocal); 528 517 else 529 518 buf = initialize_variable_output (); … … 555 544 variable_buffer = 0; 556 545 557 val = variable_append (v->name, strlen (v->name), current_variable_set_list); 546 val = variable_append (v->name, strlen (v->name), 547 current_variable_set_list, 1); 558 548 variable_buffer_output (val, "", 1); 559 549 val = variable_buffer;
Note:
See TracChangeset
for help on using the changeset viewer.