Changeset 1827 for trunk/src/kmk/expand.c
- Timestamp:
- Oct 11, 2008, 9:12:10 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/expand.c
r1819 r1827 108 108 109 109 char * 110 #ifndef CONFIG_WITH_VALUE_LENGTH 110 111 recursively_expand_for_file (struct variable *v, struct file *file) 112 #else 113 recursively_expand_for_file (struct variable *v, struct file *file, 114 unsigned int *value_lenp) 115 #endif 111 116 { 112 117 char *value; … … 149 154 150 155 v->expanding = 1; 156 #ifndef CONFIG_WITH_VALUE_LENGTH 151 157 if (v->append) 152 158 value = allocated_variable_append (v); 153 159 else 154 160 value = allocated_variable_expand (v->value); 161 #else /* CONFIG_WITH_VALUE_LENGTH */ 162 if (!v->append) 163 value = allocated_variable_expand_2 (v->value, v->value_length, value_lenp); 164 else 165 { 166 value = allocated_variable_append (v); 167 if (value_lenp) 168 *value_lenp = strlen (value); 169 } 170 #endif /* CONFIG_WITH_VALUE_LENGTH */ 155 171 v->expanding = 0; 156 172 … … 187 203 188 204 #ifdef CONFIG_WITH_VALUE_LENGTH 205 assert ((unsigned int)v->value_length == strlen (v->value)); 189 206 if (!v->recursive) 190 { 191 assert (v->value_length == strlen (v->value)); 192 o = variable_buffer_output (o, v->value, v->value_length); 193 } 207 o = variable_buffer_output (o, v->value, v->value_length); 194 208 else 195 209 { 196 value = recursively_expand (v); 197 o = variable_buffer_output (o, value, strlen (value)); 210 unsigned int value_len; 211 212 value = recursively_expand_for_file (v, NULL, &value_len); 213 o = variable_buffer_output (o, value, value_len); 198 214 free (value); 199 215 } … … 211 227 212 228 229 #ifndef CONFIG_WITH_VALUE_LENGTH /* Only using variable_expand_string_2! */ 213 230 /* Scan STRING for variable references and expansion-function calls. Only 214 231 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until … … 228 245 char *o; 229 246 unsigned int line_offset; 230 #ifdef CONFIG_WITH_VALUE_LENGTH231 const char *eos;232 #endif233 247 234 248 if (!line) … … 239 253 if (length == 0) 240 254 { 241 #ifdef KMK /* this is a bug fix. The 2 vs 1 thing is the strlen + 1 in the loop below. */242 variable_buffer_output (o, "\0", 2);243 return (variable_buffer + line_offset);244 #else245 255 variable_buffer_output (o, "", 1); 246 256 return (variable_buffer); 247 #endif 248 } 249 250 #ifdef CONFIG_WITH_VALUE_LENGTH 251 /* Simple first, 50% of the kBuild calls to this function does 252 not need any expansion at all. Should be worth a special case. */ 253 if (length < 0) 254 length = strlen (string); 255 p1 = (const char *)memchr (string, '$', length); 256 if (p1 == 0) 257 { 258 o = variable_buffer_output (o, string, length); 259 variable_buffer_output (o, "\0", 2); 260 return (variable_buffer + line_offset); 261 } 262 eos = string + length; 263 #endif /* CONFIG_WITH_VALUE_LENGTH */ 257 } 264 258 265 259 /* If we want a subset of the string, allocate a temporary buffer for it. … … 270 264 memcpy(abuf, string, length); 271 265 abuf[length] = '\0'; 272 #ifdef CONFIG_WITH_VALUE_LENGTH273 p1 += abuf - string;274 eos += abuf - string;275 #endif /* CONFIG_WITH_VALUE_LENGTH */276 266 string = abuf; 277 267 } … … 284 274 at the next $ or the end of the input. */ 285 275 286 #ifndef CONFIG_WITH_VALUE_LENGTH287 276 p1 = strchr (p, '$'); 288 #endif289 277 290 278 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1); … … 327 315 If so, expand it before expanding the entire reference. */ 328 316 329 #ifndef CONFIG_WITH_VALUE_LENGTH330 317 end = strchr (beg, closeparen); 331 #else332 end = memchr (beg, closeparen, eos - beg);333 #endif334 318 if (end == 0) 335 319 /* Unterminated variable reference. */ … … 470 454 else 471 455 ++p; 472 #ifdef CONFIG_WITH_VALUE_LENGTH473 p1 = memchr (p, '$', eos - p);474 #endif475 456 } 476 457 … … 481 462 return (variable_buffer + line_offset); 482 463 } 483 #ifdef CONFIG_WITH_VALUE_LENGTH 484 485 486 /* Same as variable_expand_string except that the pointer at EOL will 487 point to the end of the returned string. */ 464 465 #else /* CONFIG_WITH_VALUE_LENGTH */ 466 /* Scan STRING for variable references and expansion-function calls. Only 467 LENGTH bytes of STRING are actually scanned. If LENGTH is -1, scan until 468 a null byte is found. 469 470 Write the results to LINE, which must point into `variable_buffer'. If 471 LINE is NULL, start at the beginning of the buffer. 472 Return a pointer to LINE, or to the beginning of the buffer if LINE is 473 NULL. Set EOLP to point to the string terminator. 474 */ 488 475 char * 489 variable_expand_string_2 (char *line, const char *string, long length, char **eol )476 variable_expand_string_2 (char *line, const char *string, long length, char **eolp) 490 477 { 491 478 struct variable *v; 492 const char *p, *p1; 493 char *abuf = NULL; 479 const char *p, *p1, *eos; 494 480 char *o; 495 481 unsigned int line_offset; 496 #ifdef CONFIG_WITH_VALUE_LENGTH497 const char *eos;498 #endif499 482 500 483 if (!line) … … 503 486 line_offset = line - variable_buffer; 504 487 505 if (length == 0)506 {507 o = variable_buffer_output (o, "\0", 2);508 *eol = o - 2;509 #ifdef KMK /* this is a bug fix. */510 return (variable_buffer + line_offset);511 #else512 return (variable_buffer);513 #endif514 }515 516 #ifdef CONFIG_WITH_VALUE_LENGTH517 /* Simple first, 50% of the kBuild calls to this function does518 not need any expansion at all. Should be worth a special case. */519 488 if (length < 0) 520 489 length = strlen (string); 490 491 /* Simple 1: Emptry string. */ 492 493 if (length == 0) 494 { 495 o = variable_buffer_output (o, "\0", 2); 496 *eolp = o - 2; 497 return (variable_buffer + line_offset); 498 } 499 500 /* Simple 2: Nothing to expand. ~50% if the kBuild calls. */ 501 521 502 p1 = (const char *)memchr (string, '$', length); 522 503 if (p1 == 0) … … 524 505 o = variable_buffer_output (o, string, length); 525 506 o = variable_buffer_output (o, "\0", 2); 526 *eol = o - 2;507 *eolp = o - 2; 527 508 return (variable_buffer + line_offset); 528 509 } 529 eos = string + length; 530 #endif /* CONFIG_WITH_VALUE_LENGTH */ 531 532 /* If we want a subset of the string, allocate a temporary buffer for it. 533 Most of the functions we use here don't work with length limits. */ 534 if (length > 0 && string[length] != '\0') 535 { 536 abuf = xmalloc(length+1); 537 memcpy(abuf, string, length); 538 abuf[length] = '\0'; 539 #ifdef CONFIG_WITH_VALUE_LENGTH 540 p1 += abuf - string; 541 eos += abuf - string; 542 #endif 543 string = abuf; 544 } 510 545 511 p = string; 512 eos = p + length; 546 513 547 514 while (1) … … 551 518 at the next $ or the end of the input. */ 552 519 553 #ifndef CONFIG_WITH_VALUE_LENGTH 554 p1 = strchr (p, '$'); 555 #endif 556 557 /*o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p) + 1); - why +1 ? */ 558 o = variable_buffer_output (o, p, p1 != 0 ? (unsigned int)(p1 - p) : strlen (p)); 520 o = variable_buffer_output (o, p, p1 != 0 ? (p1 - p) : (eos - p)); 559 521 560 522 if (p1 == 0) … … 585 547 op = o; 586 548 begp = p; 587 if (handle_function (&op, &begp ))549 if (handle_function (&op, &begp, eos)) 588 550 { 589 551 o = op; … … 595 557 If so, expand it before expanding the entire reference. */ 596 558 597 #ifndef CONFIG_WITH_VALUE_LENGTH598 end = strchr (beg, closeparen);599 #else600 559 end = memchr (beg, closeparen, eos - beg); 601 #endif602 560 if (end == 0) 603 561 /* Unterminated variable reference. */ … … 609 567 Count parens or braces until it is matched. */ 610 568 int count = 0; 611 for (p = beg; *p != '\0'; ++p)569 for (p = beg; p < eos; ++p) 612 570 { 613 571 if (*p == openparen) … … 621 579 if (count < 0) 622 580 { 623 abeg = expand_argument (beg, p); /* Expand the name. */ 624 beg = abeg; 625 end = strchr (beg, '\0'); 581 unsigned int len; 582 char saved; 583 584 /* Expand the name. */ 585 saved = *p; 586 *(char *)p = '\0'; /* XXX: proove that this is safe! */ 587 abeg = allocated_variable_expand_2 (beg, p - beg, &len); 588 beg = abeg; 589 end = beg + len; 590 *(char *)p = saved; 626 591 } 627 592 } … … 721 686 722 687 case '\0': 723 break; 688 assert (p == eos); 689 break; 724 690 725 691 default: 726 if (isblank ((unsigned char)p[-1])) 692 if (isblank ((unsigned char)p[-1])) /* XXX: This looks incorrect, previous is '$' */ 727 693 break; 728 694 … … 734 700 } 735 701 736 if ( *p == '\0')702 if (++p >= eos) 737 703 break; 738 else739 ++p;740 #ifdef CONFIG_WITH_VALUE_LENGTH741 704 p1 = memchr (p, '$', eos - p); 742 #endif 743 } 744 745 if (abuf) 746 free (abuf); 705 } 747 706 748 707 o = variable_buffer_output (o, "\0", 2); /* KMK: compensate for the strlen + 1 that was removed above. */ 749 *eol = o - 2;708 *eolp = o - 2; 750 709 return (variable_buffer + line_offset); 751 710 } … … 761 720 variable_expand (const char *line) 762 721 { 722 #ifndef CONFIG_WITH_VALUE_LENGTH 763 723 return variable_expand_string(NULL, line, (long)-1); 724 #else 725 char *s; 726 727 /* this function is abused a lot like this: variable_expand(""). */ 728 if (!*line) 729 { 730 s = variable_buffer_output (initialize_variable_output (), "\0", 2); 731 return s - 2; 732 } 733 return variable_expand_string_2 (NULL, line, (long)-1, &s); 734 #endif 764 735 } 765 736 … … 779 750 return xstrdup(""); 780 751 752 #ifndef CONFIG_WITH_VALUE_LENGTH 781 753 if (!end || *end == '\0') 782 754 return allocated_variable_expand (str); 755 #else 756 if (!end) 757 return allocated_variable_expand_2 (str, ~0U, NULL); 758 if (*end == '\0') 759 return allocated_variable_expand_2 (str, end - str, NULL); 760 #endif 783 761 784 762 #ifdef CONFIG_WITH_OPTIMIZATION_HACKS … … 786 764 const char saved_char = *end; 787 765 *(char *)end = '\0'; 766 # ifndef CONFIG_WITH_VALUE_LENGTH 788 767 tmp = allocated_variable_expand ((char *)str); 768 # else 769 tmp = allocated_variable_expand_2 ((char *)str, end - str, NULL); 770 # endif 789 771 *(char *)end = saved_char; 790 772 return tmp; … … 795 777 tmp[end - str] = '\0'; 796 778 779 # ifndef CONFIG_WITH_VALUE_LENGTH 797 780 return allocated_variable_expand (tmp); 781 # else 782 return allocated_variable_expand_2 (tmp, end - str, NULL); 783 # endif 798 784 #endif 799 785 } … … 839 825 struct variable_set_list *save; 840 826 const struct floc *reading_file_saved; 827 char *eol; 841 828 842 829 if (file == 0) 843 return variable_expand_string (o, line, (long)-1);830 return variable_expand_string_2 (o, line, (long)-1, &eol); 844 831 845 832 save = current_variable_set_list; … … 850 837 else 851 838 reading_file = 0; 852 result = variable_expand_string (o, line, (long)-1);839 result = variable_expand_string_2 (o, line, (long)-1, &eol); 853 840 current_variable_set_list = save; 854 841 reading_file = reading_file_saved; … … 893 880 buf = variable_buffer_output (buf, " ", 1); 894 881 #ifdef CONFIG_WITH_VALUE_LENGTH 895 assert ( v->value_length == strlen (v->value));882 assert ((unsigned int)v->value_length == strlen (v->value)); /* FIXME */ 896 883 #endif 897 884 … … 1026 1013 char * 1027 1014 allocated_variable_expand_2 (const char *line, unsigned int length, 1028 unsigned int *value_len )1015 unsigned int *value_lenp) 1029 1016 { 1030 1017 char *value; … … 1032 1019 unsigned int olen = variable_buffer_length; 1033 1020 long len = length == ~0U ? -1L : (long)length; 1021 char *eol; 1034 1022 1035 1023 variable_buffer = 0; 1036 1024 1037 if (!value_len) 1038 value = variable_expand_string (NULL, line, len); 1039 else 1040 { 1041 char *eol; 1042 value = variable_expand_string_2 (NULL, line, len, &eol); 1043 *value_len = eol - value; 1044 } 1025 value = variable_expand_string_2 (NULL, line, len, &eol); 1026 if (value_lenp) 1027 *value_lenp = eol - value; 1045 1028 1046 1029 variable_buffer = obuf;
Note:
See TracChangeset
for help on using the changeset viewer.