version 1.60, 2018/04/07 14:55:13 |
version 1.61, 2019/12/02 22:17:32 |
|
|
pfp = fopen(TMPPATNAME, "w"); |
pfp = fopen(TMPPATNAME, "w"); |
if (pfp == NULL) |
if (pfp == NULL) |
pfatal("can't create %s", TMPPATNAME); |
pfatal("can't create %s", TMPPATNAME); |
while (fgets(buf, sizeof buf, stdin) != NULL) |
while (getline(&buf, &bufsz, stdin) != -1) |
fputs(buf, pfp); |
fprintf(pfp, "%s", buf); |
fclose(pfp); |
fclose(pfp); |
filename = TMPPATNAME; |
filename = TMPPATNAME; |
} |
} |
|
|
this_line = ftello(pfp); |
this_line = ftello(pfp); |
indent = 0; |
indent = 0; |
p_input_line++; |
p_input_line++; |
if (fgets(buf, sizeof buf, pfp) == NULL) { |
if (getline(&buf, &bufsz, pfp) == -1) { |
if (first_command_line >= 0) { |
if (first_command_line >= 0) { |
/* nothing but deletes!? */ |
/* nothing but deletes!? */ |
p_start = first_command_line; |
p_start = first_command_line; |
|
|
static void |
static void |
skip_to(off_t file_pos, LINENUM file_line) |
skip_to(off_t file_pos, LINENUM file_line) |
{ |
{ |
char *ret; |
int ret; |
|
|
if (p_base > file_pos) |
if (p_base > file_pos) |
fatal("Internal error: seek %lld>%lld\n", |
fatal("Internal error: seek %lld>%lld\n", |
|
|
fseeko(pfp, p_base, SEEK_SET); |
fseeko(pfp, p_base, SEEK_SET); |
say("The text leading up to this was:\n--------------------------\n"); |
say("The text leading up to this was:\n--------------------------\n"); |
while (ftello(pfp) < file_pos) { |
while (ftello(pfp) < file_pos) { |
ret = fgets(buf, sizeof buf, pfp); |
ret = getline(&buf, &bufsz, pfp); |
if (ret == NULL) |
if (ret == -1) |
fatal("Unexpected end of file\n"); |
fatal("Unexpected end of file\n"); |
say("|%s", buf); |
say("|%s", buf); |
} |
} |
|
|
off_t repl_backtrack_position; /* file pos of first repl line */ |
off_t repl_backtrack_position; /* file pos of first repl line */ |
LINENUM repl_patch_line; /* input line number for same */ |
LINENUM repl_patch_line; /* input line number for same */ |
LINENUM ptrn_copiable; /* # of copiable lines in ptrn */ |
LINENUM ptrn_copiable; /* # of copiable lines in ptrn */ |
char *s, *ret; |
char *s; |
int context = 0; |
int context = 0; |
|
int ret; |
|
|
while (p_end >= 0) { |
while (p_end >= 0) { |
if (p_end == p_efake) |
if (p_end == p_efake) |
|
|
repl_patch_line = 0; |
repl_patch_line = 0; |
ptrn_copiable = 0; |
ptrn_copiable = 0; |
|
|
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL || strnNE(buf, "********", 8)) { |
if (ret == -1 || strnNE(buf, "********", 8)) { |
next_intuit_at(line_beginning, p_input_line); |
next_intuit_at(line_beginning, p_input_line); |
return false; |
return false; |
} |
} |
|
|
p_hunk_beg = p_input_line + 1; |
p_hunk_beg = p_input_line + 1; |
while (p_end < p_max) { |
while (p_end < p_max) { |
line_beginning = ftello(pfp); |
line_beginning = ftello(pfp); |
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL) { |
if (ret == -1) { |
if (p_max - p_end < 4) { |
if (p_max - p_end < 4) { |
/* assume blank lines got chopped */ |
/* assume blank lines got chopped */ |
strlcpy(buf, " \n", sizeof buf); |
strlcpy(buf, " \n", bufsz); |
} else { |
} else { |
if (repl_beginning && repl_could_be_missing) { |
if (repl_beginning && repl_could_be_missing) { |
repl_missing = true; |
repl_missing = true; |
|
|
repl_could_be_missing = false; |
repl_could_be_missing = false; |
change_line: |
change_line: |
if (buf[1] == '\n' && canonicalize) |
if (buf[1] == '\n' && canonicalize) |
strlcpy(buf + 1, " \n", sizeof buf - 1); |
strlcpy(buf + 1, " \n", bufsz - 1); |
if (!isspace((unsigned char)buf[1]) && |
if (!isspace((unsigned char)buf[1]) && |
buf[1] != '>' && buf[1] != '<' && |
buf[1] != '>' && buf[1] != '<' && |
repl_beginning && repl_could_be_missing) { |
repl_beginning && repl_could_be_missing) { |
|
|
LINENUM filldst; /* index of new lines */ |
LINENUM filldst; /* index of new lines */ |
char ch; |
char ch; |
|
|
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL || strnNE(buf, "@@ -", 4)) { |
if (ret == -1 || strnNE(buf, "@@ -", 4)) { |
next_intuit_at(line_beginning, p_input_line); |
next_intuit_at(line_beginning, p_input_line); |
return false; |
return false; |
} |
} |
|
|
fillsrc = 1; |
fillsrc = 1; |
filldst = fillsrc + p_ptrn_lines; |
filldst = fillsrc + p_ptrn_lines; |
p_end = filldst + p_repl_lines; |
p_end = filldst + p_repl_lines; |
snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first, |
snprintf(buf, bufsz, "*** %ld,%ld ****\n", p_first, |
p_first + p_ptrn_lines - 1); |
p_first + p_ptrn_lines - 1); |
p_line[0] = savestr(buf); |
p_line[0] = savestr(buf); |
if (out_of_mem) { |
if (out_of_mem) { |
|
|
return false; |
return false; |
} |
} |
p_char[0] = '*'; |
p_char[0] = '*'; |
snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst, |
snprintf(buf, bufsz, "--- %ld,%ld ----\n", p_newfirst, |
p_newfirst + p_repl_lines - 1); |
p_newfirst + p_repl_lines - 1); |
p_line[filldst] = savestr(buf); |
p_line[filldst] = savestr(buf); |
if (out_of_mem) { |
if (out_of_mem) { |
|
|
p_hunk_beg = p_input_line + 1; |
p_hunk_beg = p_input_line + 1; |
while (fillsrc <= p_ptrn_lines || filldst <= p_end) { |
while (fillsrc <= p_ptrn_lines || filldst <= p_end) { |
line_beginning = ftello(pfp); |
line_beginning = ftello(pfp); |
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL) { |
if (ret == -1) { |
if (p_max - filldst < 3) { |
if (p_max - filldst < 3) { |
/* assume blank lines got chopped */ |
/* assume blank lines got chopped */ |
strlcpy(buf, " \n", sizeof buf); |
strlcpy(buf, " \n", bufsz); |
} else { |
} else { |
fatal("unexpected end of file in patch\n"); |
fatal("unexpected end of file in patch\n"); |
} |
} |
|
|
off_t line_beginning = ftello(pfp); |
off_t line_beginning = ftello(pfp); |
|
|
p_context = 0; |
p_context = 0; |
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL || !isdigit((unsigned char)*buf)) { |
if (ret == -1 || !isdigit((unsigned char)*buf)) { |
next_intuit_at(line_beginning, p_input_line); |
next_intuit_at(line_beginning, p_input_line); |
return false; |
return false; |
} |
} |
|
|
p_end, p_input_line, buf); |
p_end, p_input_line, buf); |
while (p_end >= hunkmax) |
while (p_end >= hunkmax) |
grow_hunkmax(); |
grow_hunkmax(); |
snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first, |
snprintf(buf, bufsz, "*** %ld,%ld\n", p_first, |
p_first + p_ptrn_lines - 1); |
p_first + p_ptrn_lines - 1); |
p_line[0] = savestr(buf); |
p_line[0] = savestr(buf); |
if (out_of_mem) { |
if (out_of_mem) { |
|
|
} |
} |
p_char[0] = '*'; |
p_char[0] = '*'; |
for (i = 1; i <= p_ptrn_lines; i++) { |
for (i = 1; i <= p_ptrn_lines; i++) { |
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL) |
if (ret == -1) |
fatal("unexpected end of file in patch at line %ld\n", |
fatal("unexpected end of file in patch at line %ld\n", |
p_input_line); |
p_input_line); |
if (*buf != '<') |
if (*buf != '<') |
|
|
(p_line[i - 1])[p_len[i - 1]] = 0; |
(p_line[i - 1])[p_len[i - 1]] = 0; |
} |
} |
if (hunk_type == 'c') { |
if (hunk_type == 'c') { |
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL) |
if (ret == -1) |
fatal("unexpected end of file in patch at line %ld\n", |
fatal("unexpected end of file in patch at line %ld\n", |
p_input_line); |
p_input_line); |
if (*buf != '-') |
if (*buf != '-') |
fatal("--- expected at line %ld of patch\n", |
fatal("--- expected at line %ld of patch\n", |
p_input_line); |
p_input_line); |
} |
} |
snprintf(buf, sizeof(buf), "--- %ld,%ld\n", min, max); |
snprintf(buf, bufsz, "--- %ld,%ld\n", min, max); |
p_line[i] = savestr(buf); |
p_line[i] = savestr(buf); |
if (out_of_mem) { |
if (out_of_mem) { |
p_end = i - 1; |
p_end = i - 1; |
|
|
} |
} |
p_char[i] = '='; |
p_char[i] = '='; |
for (i++; i <= p_end; i++) { |
for (i++; i <= p_end; i++) { |
ret = pgets(buf, sizeof buf, pfp); |
ret = pgetline(&buf, &bufsz, pfp); |
p_input_line++; |
p_input_line++; |
if (ret == NULL) |
if (ret == -1) |
fatal("unexpected end of file in patch at line %ld\n", |
fatal("unexpected end of file in patch at line %ld\n", |
p_input_line); |
p_input_line); |
if (*buf != '>') |
if (*buf != '>') |
|
|
/* |
/* |
* Input a line from the patch file, worrying about indentation. |
* Input a line from the patch file, worrying about indentation. |
*/ |
*/ |
char * |
int |
pgets(char *bf, int sz, FILE *fp) |
pgetline(char **bf, size_t *sz, FILE *fp) |
{ |
{ |
char *s, *ret = fgets(bf, sz, fp); |
char *s; |
int indent = 0; |
int indent = 0; |
|
int ret; |
|
|
if (p_indent && ret != NULL) { |
ret = getline(bf, sz, fp); |
|
|
|
if (p_indent && ret != -1) { |
for (s = buf; |
for (s = buf; |
indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); |
indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); |
s++) { |
s++) { |
|
|
else |
else |
indent++; |
indent++; |
} |
} |
if (buf != s && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf)) |
if (buf != s && strlcpy(buf, s, bufsz) >= bufsz) |
fatal("buffer too small in pgets()\n"); |
fatal("buffer too small in pgetline()\n"); |
} |
} |
return ret; |
return ret; |
} |
} |