Lines 78-83
Link Here
|
78 |
static char *best_name(const struct file_name *, bool); |
78 |
static char *best_name(const struct file_name *, bool); |
79 |
static char *posix_name(const struct file_name *, bool); |
79 |
static char *posix_name(const struct file_name *, bool); |
80 |
static size_t num_components(const char *); |
80 |
static size_t num_components(const char *); |
|
|
81 |
static LINENUM strtolinenum(char *, char **); |
81 |
|
82 |
|
82 |
/* |
83 |
/* |
83 |
* Prepare to look for the next patch in the patch file. |
84 |
* Prepare to look for the next patch in the patch file. |
Lines 354-360
Link Here
|
354 |
stars_this_line = strnEQ(s, "********", 8); |
355 |
stars_this_line = strnEQ(s, "********", 8); |
355 |
if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && |
356 |
if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && |
356 |
strnEQ(s, "*** ", 4)) { |
357 |
strnEQ(s, "*** ", 4)) { |
357 |
if (atol(s + 4) == 0) |
358 |
if (strtolinenum(s + 4, &s) == 0) |
358 |
ok_to_create_file = true; |
359 |
ok_to_create_file = true; |
359 |
/* |
360 |
/* |
360 |
* If this is a new context diff the character just |
361 |
* If this is a new context diff the character just |
Lines 593-601
Link Here
|
593 |
malformed(); |
594 |
malformed(); |
594 |
if (strnEQ(s, "0,0", 3)) |
595 |
if (strnEQ(s, "0,0", 3)) |
595 |
memmove(s, s + 2, strlen(s + 2) + 1); |
596 |
memmove(s, s + 2, strlen(s + 2) + 1); |
596 |
p_first = (LINENUM) atol(s); |
597 |
p_first = strtolinenum(s, &s); |
597 |
while (isdigit((unsigned char)*s)) |
|
|
598 |
s++; |
599 |
if (*s == ',') { |
598 |
if (*s == ',') { |
600 |
for (; |
599 |
for (; |
601 |
*s && !isdigit((unsigned char)*s); s++) |
600 |
*s && !isdigit((unsigned char)*s); s++) |
Lines 602-608
Link Here
|
602 |
; |
601 |
; |
603 |
if (!*s) |
602 |
if (!*s) |
604 |
malformed(); |
603 |
malformed(); |
605 |
p_ptrn_lines = ((LINENUM) atol(s)) - p_first + 1; |
604 |
p_ptrn_lines = strtolinenum(s, &s) - p_first + 1; |
|
|
605 |
if (p_ptrn_lines < 0) |
606 |
malformed(); |
606 |
} else if (p_first) |
607 |
} else if (p_first) |
607 |
p_ptrn_lines = 1; |
608 |
p_ptrn_lines = 1; |
608 |
else { |
609 |
else { |
Lines 609-614
Link Here
|
609 |
p_ptrn_lines = 0; |
610 |
p_ptrn_lines = 0; |
610 |
p_first = 1; |
611 |
p_first = 1; |
611 |
} |
612 |
} |
|
|
613 |
if (p_first >= LINENUM_MAX - p_ptrn_lines || |
614 |
p_ptrn_lines >= LINENUM_MAX - 6) |
615 |
malformed(); |
612 |
|
616 |
|
613 |
/* we need this much at least */ |
617 |
/* we need this much at least */ |
614 |
p_max = p_ptrn_lines + 6; |
618 |
p_max = p_ptrn_lines + 6; |
Lines 661-676
Link Here
|
661 |
; |
665 |
; |
662 |
if (!*s) |
666 |
if (!*s) |
663 |
malformed(); |
667 |
malformed(); |
664 |
p_newfirst = (LINENUM) atol(s); |
668 |
p_newfirst = strtolinenum(s, &s); |
665 |
while (isdigit((unsigned char)*s)) |
|
|
666 |
s++; |
667 |
if (*s == ',') { |
669 |
if (*s == ',') { |
668 |
for (; *s && !isdigit((unsigned char)*s); s++) |
670 |
for (; *s && !isdigit((unsigned char)*s); s++) |
669 |
; |
671 |
; |
670 |
if (!*s) |
672 |
if (!*s) |
671 |
malformed(); |
673 |
malformed(); |
672 |
p_repl_lines = ((LINENUM) atol(s)) - |
674 |
p_repl_lines = strtolinenum(s, &s) - |
673 |
p_newfirst + 1; |
675 |
p_newfirst + 1; |
|
|
676 |
if (p_repl_lines < 0) |
677 |
malformed(); |
674 |
} else if (p_newfirst) |
678 |
} else if (p_newfirst) |
675 |
p_repl_lines = 1; |
679 |
p_repl_lines = 1; |
676 |
else { |
680 |
else { |
Lines 677-682
Link Here
|
677 |
p_repl_lines = 0; |
681 |
p_repl_lines = 0; |
678 |
p_newfirst = 1; |
682 |
p_newfirst = 1; |
679 |
} |
683 |
} |
|
|
684 |
if (p_newfirst >= LINENUM_MAX - p_repl_lines || |
685 |
p_repl_lines >= LINENUM_MAX - p_end) |
686 |
malformed(); |
680 |
p_max = p_repl_lines + p_end; |
687 |
p_max = p_repl_lines + p_end; |
681 |
if (p_max > MAXHUNKSIZE) |
688 |
if (p_max > MAXHUNKSIZE) |
682 |
fatal("hunk too large (%ld lines) at line %ld: %s", |
689 |
fatal("hunk too large (%ld lines) at line %ld: %s", |
Lines 869-881
Link Here
|
869 |
s = buf + 4; |
876 |
s = buf + 4; |
870 |
if (!*s) |
877 |
if (!*s) |
871 |
malformed(); |
878 |
malformed(); |
872 |
p_first = (LINENUM) atol(s); |
879 |
p_first = strtolinenum(s, &s); |
873 |
while (isdigit((unsigned char)*s)) |
|
|
874 |
s++; |
875 |
if (*s == ',') { |
880 |
if (*s == ',') { |
876 |
p_ptrn_lines = (LINENUM) atol(++s); |
881 |
p_ptrn_lines = strtolinenum(s + 1, &s); |
877 |
while (isdigit((unsigned char)*s)) |
|
|
878 |
s++; |
879 |
} else |
882 |
} else |
880 |
p_ptrn_lines = 1; |
883 |
p_ptrn_lines = 1; |
881 |
if (*s == ' ') |
884 |
if (*s == ' ') |
Lines 882-894
Link Here
|
882 |
s++; |
885 |
s++; |
883 |
if (*s != '+' || !*++s) |
886 |
if (*s != '+' || !*++s) |
884 |
malformed(); |
887 |
malformed(); |
885 |
p_newfirst = (LINENUM) atol(s); |
888 |
p_newfirst = strtolinenum(s, &s); |
886 |
while (isdigit((unsigned char)*s)) |
|
|
887 |
s++; |
888 |
if (*s == ',') { |
889 |
if (*s == ',') { |
889 |
p_repl_lines = (LINENUM) atol(++s); |
890 |
p_repl_lines = strtolinenum(s + 1, &s); |
890 |
while (isdigit((unsigned char)*s)) |
|
|
891 |
s++; |
892 |
} else |
891 |
} else |
893 |
p_repl_lines = 1; |
892 |
p_repl_lines = 1; |
894 |
if (*s == ' ') |
893 |
if (*s == ' ') |
Lines 895-900
Link Here
|
895 |
s++; |
894 |
s++; |
896 |
if (*s != '@') |
895 |
if (*s != '@') |
897 |
malformed(); |
896 |
malformed(); |
|
|
897 |
if (p_first >= LINENUM_MAX - p_ptrn_lines || |
898 |
p_newfirst > LINENUM_MAX - p_repl_lines || |
899 |
p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1) |
900 |
malformed(); |
898 |
if (!p_ptrn_lines) |
901 |
if (!p_ptrn_lines) |
899 |
p_first++; /* do append rather than insert */ |
902 |
p_first++; /* do append rather than insert */ |
900 |
p_max = p_ptrn_lines + p_repl_lines + 1; |
903 |
p_max = p_ptrn_lines + p_repl_lines + 1; |
Lines 1034-1068
Link Here
|
1034 |
next_intuit_at(line_beginning, p_input_line); |
1037 |
next_intuit_at(line_beginning, p_input_line); |
1035 |
return false; |
1038 |
return false; |
1036 |
} |
1039 |
} |
1037 |
p_first = (LINENUM) atol(buf); |
1040 |
p_first = strtolinenum(buf, &s); |
1038 |
for (s = buf; isdigit((unsigned char)*s); s++) |
|
|
1039 |
; |
1040 |
if (*s == ',') { |
1041 |
if (*s == ',') { |
1041 |
p_ptrn_lines = (LINENUM) atol(++s) - p_first + 1; |
1042 |
p_ptrn_lines = strtolinenum(s + 1, &s) - p_first + 1; |
1042 |
while (isdigit((unsigned char)*s)) |
1043 |
if (p_ptrn_lines < 0) |
1043 |
s++; |
1044 |
malformed(); |
1044 |
} else |
1045 |
} else |
1045 |
p_ptrn_lines = (*s != 'a'); |
1046 |
p_ptrn_lines = (*s != 'a'); |
1046 |
hunk_type = *s; |
1047 |
hunk_type = *s; |
1047 |
if (hunk_type == 'a') |
1048 |
if (hunk_type == 'a') |
1048 |
p_first++; /* do append rather than insert */ |
1049 |
p_first++; /* do append rather than insert */ |
1049 |
min = (LINENUM) atol(++s); |
1050 |
min = strtolinenum(s + 1, &s); |
1050 |
for (; isdigit((unsigned char)*s); s++) |
|
|
1051 |
; |
1052 |
if (*s == ',') |
1051 |
if (*s == ',') |
1053 |
max = (LINENUM) atol(++s); |
1052 |
max = strtolinenum(s + 1, &s); |
1054 |
else |
1053 |
else |
1055 |
max = min; |
1054 |
max = min; |
|
|
1055 |
if (min < 0 || min > max || max - min == LINENUM_MAX) |
1056 |
malformed(); |
1056 |
if (hunk_type == 'd') |
1057 |
if (hunk_type == 'd') |
1057 |
min++; |
1058 |
min++; |
1058 |
p_end = p_ptrn_lines + 1 + max - min + 1; |
1059 |
p_newfirst = min; |
|
|
1060 |
p_repl_lines = max - min + 1; |
1061 |
if (p_newfirst > LINENUM_MAX - p_repl_lines || |
1062 |
p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1) |
1063 |
malformed(); |
1064 |
p_end = p_ptrn_lines + p_repl_lines + 1; |
1059 |
if (p_end > MAXHUNKSIZE) |
1065 |
if (p_end > MAXHUNKSIZE) |
1060 |
fatal("hunk too large (%ld lines) at line %ld: %s", |
1066 |
fatal("hunk too large (%ld lines) at line %ld: %s", |
1061 |
p_end, p_input_line, buf); |
1067 |
p_end, p_input_line, buf); |
1062 |
while (p_end >= hunkmax) |
1068 |
while (p_end >= hunkmax) |
1063 |
grow_hunkmax(); |
1069 |
grow_hunkmax(); |
1064 |
p_newfirst = min; |
|
|
1065 |
p_repl_lines = max - min + 1; |
1066 |
snprintf(buf, buf_size, "*** %ld,%ld\n", p_first, |
1070 |
snprintf(buf, buf_size, "*** %ld,%ld\n", p_first, |
1067 |
p_first + p_ptrn_lines - 1); |
1071 |
p_first + p_ptrn_lines - 1); |
1068 |
p_line[0] = savestr(buf); |
1072 |
p_line[0] = savestr(buf); |
Lines 1582-1584
Link Here
|
1582 |
} |
1586 |
} |
1583 |
return n; |
1587 |
return n; |
1584 |
} |
1588 |
} |
|
|
1589 |
|
1590 |
/* |
1591 |
* Convert number at NPTR into LINENUM and save address of first |
1592 |
* character that is not a digit in ENDPTR. If conversion is not |
1593 |
* possible, call fatal. |
1594 |
*/ |
1595 |
static LINENUM |
1596 |
strtolinenum(char *nptr, char **endptr) |
1597 |
{ |
1598 |
LINENUM rv; |
1599 |
char c; |
1600 |
char *p; |
1601 |
const char *errstr; |
1602 |
|
1603 |
for (p = nptr; isdigit((unsigned char)*p); p++) |
1604 |
; |
1605 |
|
1606 |
if (p == nptr) |
1607 |
malformed(); |
1608 |
|
1609 |
c = *p; |
1610 |
*p = '\0'; |
1611 |
|
1612 |
rv = strtonum(nptr, 0, LINENUM_MAX, &errstr); |
1613 |
if (errstr != NULL) |
1614 |
fatal("invalid line number at line %ld: `%s' is %s\n", |
1615 |
p_input_line, nptr, errstr); |
1616 |
|
1617 |
*p = c; |
1618 |
*endptr = p; |
1619 |
|
1620 |
return rv; |
1621 |
} |