Changeset 3140 for trunk/src/kmk/expand.c
- Timestamp:
- Mar 14, 2018, 10:28:10 PM (7 years ago)
- Location:
- trunk/src/kmk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk
-
Property svn:mergeinfo
set to
/vendor/gnumake/current merged eligible
-
Property svn:mergeinfo
set to
-
trunk/src/kmk/expand.c
r2771 r3140 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> … … 32 30 /* Initially, any errors reported when expanding strings will be reported 33 31 against the file where the error appears. */ 34 const structfloc **expanding_var = &reading_file;32 const floc **expanding_var = &reading_file; 35 33 36 34 /* The next two describe the variable output buffer. … … 84 82 unsigned int offset = ptr - variable_buffer; 85 83 variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length 86 87 84 ? newlen + 100 85 : 2 * variable_buffer_length); 88 86 variable_buffer = xrealloc (variable_buffer, variable_buffer_length); 89 87 ptr = variable_buffer + offset; … … 145 143 { 146 144 char *value; 147 const structfloc *this_var;148 const structfloc **saved_varp;145 const floc *this_var; 146 const floc **saved_varp; 149 147 struct variable_set_list *save = 0; 150 148 int set_reading = 0; … … 170 168 if (!v->exp_count) 171 169 /* Expanding V causes infinite recursion. Lose. */ 172 fatal (*expanding_var,173 _("Recursive variable `%s' references itself (eventually)"),174 170 OS (fatal, *expanding_var, 171 _("Recursive variable '%s' references itself (eventually)"), 172 v->name); 175 173 --v->exp_count; 176 174 } … … 231 229 reference_recursive_variable (char *o, struct variable *v) 232 230 { 233 const structfloc *this_var;234 const structfloc **saved_varp;231 const floc *this_var; 232 const floc **saved_varp; 235 233 int set_reading = 0; 236 234 … … 255 253 if (!v->exp_count) 256 254 /* Expanding V causes infinite recursion. Lose. */ 257 fatal (*expanding_var,258 259 255 OS (fatal, *expanding_var, 256 _("Recursive variable `%s' references itself (eventually)"), 257 v->name); 260 258 --v->exp_count; 261 259 } … … 346 344 a null byte is found. 347 345 348 Write the results to LINE, which must point into `variable_buffer'. If346 Write the results to LINE, which must point into 'variable_buffer'. If 349 347 LINE is NULL, start at the beginning of the buffer. 350 348 Return a pointer to LINE, or to the beginning of the buffer if LINE is … … 356 354 struct variable *v; 357 355 const char *p, *p1; 358 char * abuf = NULL;356 char *save; 359 357 char *o; 360 358 unsigned int line_offset; 361 359 362 360 if (!line) 363 line = initialize_variable_output ();361 line = initialize_variable_output (); 364 362 o = line; 365 363 line_offset = line - variable_buffer; … … 371 369 } 372 370 373 /* If we want a subset of the string, allocate a temporary buffer for it. 374 Most of the functions we use here don't work with length limits. */ 375 if (length > 0 && string[length] != '\0') 376 { 377 abuf = xmalloc(length+1); 378 memcpy(abuf, string, length); 379 abuf[length] = '\0'; 380 string = abuf; 381 } 382 p = string; 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. */ 374 save = length < 0 ? xstrdup (string) : xstrndup (string, length); 375 p = save; 383 376 384 377 while (1) … … 386 379 /* Copy all following uninteresting chars all at once to the 387 380 variable output buffer, and skip them. Uninteresting chars end 388 381 at the next $ or the end of the input. */ 389 382 390 383 p1 = strchr (p, '$'); … … 393 386 394 387 if (p1 == 0) 395 388 break; 396 389 p = p1 + 1; 397 390 … … 399 392 400 393 switch (*p) 401 { 402 case '$': 403 /* $$ seen means output one $ to the variable output buffer. */ 404 o = variable_buffer_output (o, p, 1); 405 break; 406 407 case '(': 408 case '{': 409 /* $(...) or ${...} is the general case of substitution. */ 410 { 411 char openparen = *p; 412 char closeparen = (openparen == '(') ? ')' : '}'; 394 { 395 case '$': 396 case '\0': 397 /* $$ or $ at the end of the string means output one $ to the 398 variable output buffer. */ 399 o = variable_buffer_output (o, p1, 1); 400 break; 401 402 case '(': 403 case '{': 404 /* $(...) or ${...} is the general case of substitution. */ 405 { 406 char openparen = *p; 407 char closeparen = (openparen == '(') ? ')' : '}'; 413 408 const char *begp; 414 415 409 const char *beg = p + 1; 410 char *op; 416 411 char *abeg = NULL; 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 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; 420 break; 421 } 422 423 /* Is there a variable reference inside the parens or braces? 424 If so, expand it before expanding the entire reference. */ 425 426 end = strchr (beg, closeparen); 427 if (end == 0) 433 428 /* Unterminated variable reference. */ 434 fatal (*expanding_var, _("unterminated variable reference"));435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 such as `$($(a)'. */451 452 453 454 455 456 457 458 459 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 } 453 else 454 /* Advance P to the end of this reference. After we are 460 455 finished expanding this one, P will be incremented to 461 456 continue the scan. */ 462 p = end; 463 464 /* This is not a reference to a built-in function and 465 any variable references inside are now expanded. 466 Is the resultant text a substitution reference? */ 467 468 colon = lindex (beg, end, ':'); 469 if (colon) 470 { 471 /* This looks like a substitution reference: $(FOO:A=B). */ 472 const char *subst_beg, *subst_end, *replace_beg, *replace_end; 473 474 subst_beg = colon + 1; 475 subst_end = lindex (subst_beg, end, '='); 476 if (subst_end == 0) 477 /* There is no = in sight. Punt on the substitution 478 reference and treat this as a variable name containing 479 a colon, in the code below. */ 480 colon = 0; 481 else 482 { 483 replace_beg = subst_end + 1; 484 replace_end = end; 485 486 /* Extract the variable name before the colon 487 and look up that variable. */ 488 v = lookup_variable (beg, colon - beg); 489 if (v == 0) 490 warn_undefined (beg, colon - beg); 457 p = end; 458 459 /* This is not a reference to a built-in function and 460 any variable references inside are now expanded. 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 { 476 const char *replace_beg = subst_end + 1; 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); 482 if (v == 0) 483 warn_undefined (beg, colon - beg); 491 484 492 485 /* If the variable is not empty, perform the 493 486 substitution. */ 494 495 496 497 487 if (v != 0 && *v->value != '\0') 488 { 489 char *pattern, *replace, *ppercent, *rpercent; 490 char *value = (v->recursive 498 491 ? recursively_expand (v) 499 492 : v->value); 500 493 501 494 /* Copy the pattern and the replacement. Add in an … … 515 508 /* Look for %. Set the percent pointers properly 516 509 based on whether we find one or not. */ 517 518 510 ppercent = find_percent (pattern); 511 if (ppercent) 519 512 { 520 513 ++ppercent; … … 523 516 ++rpercent; 524 517 } 525 518 else 526 519 { 527 520 ppercent = pattern; … … 534 527 ppercent, rpercent); 535 528 536 if (v->recursive) 537 free (value); 538 } 539 } 540 } 541 542 if (colon == 0) 543 /* This is an ordinary variable reference. 544 Look up the value of the variable. */ 545 o = reference_variable (o, beg, end - beg); 546 547 if (abeg) 548 free (abeg); 549 } 550 break; 551 552 case '\0': 553 break; 554 555 default: 556 if (isblank ((unsigned char)p[-1])) 557 break; 558 559 /* A $ followed by a random char is a variable reference: 560 $a is equivalent to $(a). */ 529 if (v->recursive) 530 free (value); 531 } 532 } 533 } 534 535 if (colon == 0) 536 /* This is an ordinary variable reference. 537 Look up the value of the variable. */ 538 o = reference_variable (o, beg, end - beg); 539 540 free (abeg); 541 } 542 break; 543 544 default: 545 if (ISSPACE (p[-1])) 546 break; 547 548 /* A $ followed by a random char is a variable reference: 549 $a is equivalent to $(a). */ 561 550 o = reference_variable (o, p, 1); 562 551 563 564 552 break; 553 } 565 554 566 555 if (*p == '\0') 567 556 break; 568 557 569 558 ++p; 570 559 } 571 560 572 if (abuf) 573 free (abuf); 561 free (save); 574 562 575 563 variable_buffer_output (o, "", 1); … … 682 670 if (end == 0) 683 671 /* Unterminated variable reference. */ 684 fatal (*expanding_var, _("unterminated variable reference"));672 O (fatal, *expanding_var, _("unterminated variable reference")); 685 673 p1 = lindex (beg, end, '$'); 686 674 if (p1 != 0) … … 812 800 813 801 default: 814 if ( isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */802 if (ISBLANK (p[-1])) /* XXX: This looks incorrect, previous is '$' */ 815 803 break; 816 804 … … 839 827 840 828 /* Scan LINE for variable references and expansion-function calls. 841 Build in `variable_buffer' the result of expanding the references and calls.829 Build in 'variable_buffer' the result of expanding the references and calls. 842 830 Return the address of the resulting string, which is null-terminated 843 831 and is valid only until the next time this function is called. */ … … 847 835 { 848 836 #ifndef CONFIG_WITH_VALUE_LENGTH 849 return variable_expand_string (NULL, line, (long)-1);837 return variable_expand_string (NULL, line, (long)-1); 850 838 #else /* CONFIG_WITH_VALUE_LENGTH */ 851 839 char *s; … … 865 853 The text starting at STR and ending at END is variable-expanded 866 854 into a null-terminated string that is returned as the value. 867 This is done without clobbering `variable_buffer' or the current855 This is done without clobbering 'variable_buffer' or the current 868 856 variable-expansion that is in progress. */ 869 857 … … 877 865 878 866 if (str == end) 879 return xstrdup ("");867 return xstrdup (""); 880 868 881 869 #ifndef CONFIG_WITH_VALUE_LENGTH … … 893 881 r = allocated_variable_expand (tmp); 894 882 895 if (alloc) 896 free (alloc); 883 free (alloc); 897 884 898 885 return r; … … 913 900 char *result; 914 901 struct variable_set_list *savev; 915 const structfloc *savef;902 const floc *savef; 916 903 917 904 if (file == 0) … … 950 937 char *result; 951 938 struct variable_set_list *savev; 952 const structfloc *savef;939 const floc *savef; 953 940 long len = length == ~0U ? (long)-1 : (long)length; 954 941 char *eol; … … 990 977 static char * 991 978 variable_append (const char *name, unsigned int length, 992 const struct variable_set_list *set )979 const struct variable_set_list *set, int local) 993 980 { 994 981 const struct variable *v; 995 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; 996 985 997 986 /* If there's nothing left to check, return the empty buffer. */ … … 1002 991 v = lookup_variable_in_set (name, length, set->set); 1003 992 1004 /* If there isn't one, look to see if there's one in aset above us. */1005 if (!v )1006 return variable_append (name, length, set->next );993 /* If there isn't one, or this one is private, try the set above us. */ 994 if (!v || (!local && v->private_var)) 995 return variable_append (name, length, set->next, nextlocal); 1007 996 1008 997 /* If this variable type is append, first get any upper values. 1009 998 If not, initialize the buffer. */ 1010 999 if (v->append) 1011 buf = variable_append (name, length, set->next );1000 buf = variable_append (name, length, set->next, nextlocal); 1012 1001 else 1013 1002 buf = initialize_variable_output (); … … 1115 1104 variable_buffer = 0; 1116 1105 1117 assert ((unsigned int)v->length == strlen (v->name)); /* bird */ 1118 val = variable_append (v->name, strlen (v->name), current_variable_set_list); 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! */ 1108 current_variable_set_list, 1); 1119 1109 variable_buffer_output (val, "", 1); 1120 1110 val = variable_buffer;
Note:
See TracChangeset
for help on using the changeset viewer.