#define XTERN extern
#include <common.h>
#include <backupfile.h>
#include <dirname.h>
#include <inp.h>
#include <quotearg.h>
#include <util.h>
#undef XTERN
#define XTERN
#include <pch.h>
#define INITHUNKMAX 125
static FILE *pfp;
static int p_says_nonexistent[2];
static int p_rfc934_nesting;
static time_t p_timestamp[2];
static off_t p_filesize;
static LINENUM p_first;
static LINENUM p_newfirst;
static LINENUM p_ptrn_lines;
static LINENUM p_repl_lines;
static LINENUM p_end = -1;
static LINENUM p_max;
static LINENUM p_prefix_context;
static LINENUM p_suffix_context;
static LINENUM p_input_line;
static char **p_line;
static size_t *p_len;
static char *p_Char;
static LINENUM hunkmax = INITHUNKMAX;
static int p_indent;
static int p_strip_trailing_cr;
static file_offset p_base;
static LINENUM p_bline;
static file_offset p_start;
static LINENUM p_sline;
static LINENUM p_hunk_beg;
static LINENUM p_efake = -1;
static LINENUM p_bfake = -1;
enum nametype { OLD, NEW, INDEX, NONE };
static char *scan_linenum (char *, LINENUM *);
static enum diff intuit_diff_type (void);
static enum nametype best_name (char * const *, int const *);
static int prefix_components (char *, int);
static size_t pget_line (int, int, int);
static size_t get_line (void);
static bool incomplete_line (void);
static bool grow_hunkmax (void);
static void malformed (void) __attribute__ ((noreturn));
static void next_intuit_at (file_offset, LINENUM);
static void skip_to (file_offset, LINENUM);
void
re_patch (void)
{
p_first = 0;
p_newfirst = 0;
p_ptrn_lines = 0;
p_repl_lines = 0;
p_end = -1;
p_max = 0;
p_indent = 0;
p_strip_trailing_cr = 0;
}
void
open_patch_file (char const *filename)
{
file_offset file_pos = 0;
struct stat st;
if (!filename || !*filename || strEQ (filename, "-"))
{
file_offset stdin_pos;
#if 0 && HAVE_SETMODE
if (binary_transput)
{
if (isatty (STDIN_FILENO))
fatal ("cannot read binary data from tty on this platform");
setmode (STDIN_FILENO, O_BINARY);
}
#endif
if (fstat (STDIN_FILENO, &st) != 0)
pfatal ("fstat");
if (S_ISREG (st.st_mode) && (stdin_pos = file_tell (stdin)) != -1)
{
pfp = stdin;
file_pos = stdin_pos;
}
else
{
size_t charsread;
int exclusive = TMPPATNAME_needs_removal ? 0 : O_EXCL;
TMPPATNAME_needs_removal = 1;
pfp = fdopen (create_file (TMPPATNAME,
O_RDWR | O_BINARY | exclusive,
(mode_t) 0),
"w+b");
if (!pfp)
pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME));
for (st.st_size = 0;
(charsread = fread (buf, 1, bufsize, stdin)) != 0;
st.st_size += charsread)
if (fwrite (buf, 1, charsread, pfp) != charsread)
write_fatal ();
if (ferror (stdin) || fclose (stdin) != 0)
read_fatal ();
if (fflush (pfp) != 0
|| file_seek (pfp, (file_offset) 0, SEEK_SET) != 0)
write_fatal ();
}
}
else
{
pfp = fopen (filename, binary_transput ? "rb" : "r");
if (!pfp)
pfatal ("Can't open patch file %s", quotearg (filename));
if (fstat (fileno (pfp), &st) != 0)
pfatal ("fstat");
}
p_filesize = st.st_size;
if (p_filesize != (file_offset) p_filesize)
fatal ("patch file is too long");
next_intuit_at (file_pos, (LINENUM) 1);
set_hunkmax();
}
void
set_hunkmax (void)
{
if (!p_line)
p_line = (char **) malloc (hunkmax * sizeof *p_line);
if (!p_len)
p_len = (size_t *) malloc (hunkmax * sizeof *p_len);
if (!p_Char)
p_Char = malloc (hunkmax * sizeof *p_Char);
}
static bool
grow_hunkmax (void)
{
hunkmax *= 2;
assert (p_line && p_len && p_Char);
if ((p_line = (char **) realloc (p_line, hunkmax * sizeof (*p_line)))
&& (p_len = (size_t *) realloc (p_len, hunkmax * sizeof (*p_len)))
&& (p_Char = realloc (p_Char, hunkmax * sizeof (*p_Char))))
return TRUE;
if (!using_plan_a)
memory_fatal ();
return FALSE;
}
bool
there_is_another_patch (void)
{
if (p_base != 0 && p_base >= p_filesize) {
if (verbosity == VERBOSE)
say ("done\n");
return FALSE;
}
if (verbosity == VERBOSE)
say ("Hmm...");
diff_type = intuit_diff_type();
if (diff_type == NO_DIFF) {
if (verbosity == VERBOSE)
say (p_base
? " Ignoring the trailing garbage.\ndone\n"
: " I can't seem to find a patch in there anywhere.\n");
if (! p_base && p_filesize)
fatal ("Only garbage was found in the patch input.");
return FALSE;
}
if (skip_rest_of_patch)
{
Fseek (pfp, p_start, SEEK_SET);
p_input_line = p_sline - 1;
return TRUE;
}
if (verbosity == VERBOSE)
say (" %sooks like %s to me...\n",
(p_base == 0 ? "L" : "The next patch l"),
diff_type == UNI_DIFF ? "a unified diff" :
diff_type == CONTEXT_DIFF ? "a context diff" :
diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
diff_type == NORMAL_DIFF ? "a normal diff" :
"an ed script" );
if (verbosity != SILENT)
{
if (p_indent)
say ("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
if (p_strip_trailing_cr)
say ("(Stripping trailing CRs from patch.)\n");
if (! inname)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("can't find file to patch at input line %s\n",
format_linenum (numbuf, p_sline));
say (strippath == -1
? "Perhaps you should have used the -p or --strip option?\n"
: "Perhaps you used the wrong -p or --strip option?\n");
}
}
skip_to(p_start,p_sline);
while (!inname) {
if (force || batch) {
say ("No file to patch. Skipping patch.\n");
skip_rest_of_patch = TRUE;
return TRUE;
}
ask ("File to patch: ");
inname = fetchname (buf, 0, (time_t *) 0);
if (inname)
{
if (stat (inname, &instat) == 0)
{
inerrno = 0;
invc = -1;
}
else
{
perror (inname);
fflush (stderr);
free (inname);
inname = 0;
}
}
if (!inname) {
ask ("Skip this patch? [y] ");
if (*buf != 'n') {
if (verbosity != SILENT)
say ("Skipping patch.\n");
skip_rest_of_patch = TRUE;
return TRUE;
}
}
}
return TRUE;
}
static enum diff
intuit_diff_type (void)
{
register file_offset this_line = 0;
register file_offset first_command_line = -1;
LINENUM fcl_line = 0;
register bool this_is_a_command = FALSE;
register bool stars_this_line = FALSE;
enum nametype i;
char *name[3];
struct stat st[3];
int stat_errno[3];
int version_controlled[3];
register enum diff retval;
name[OLD] = name[NEW] = name[INDEX] = 0;
version_controlled[OLD] = -1;
version_controlled[NEW] = -1;
version_controlled[INDEX] = -1;
p_rfc934_nesting = 0;
p_timestamp[OLD] = p_timestamp[NEW] = (time_t) -1;
p_says_nonexistent[OLD] = p_says_nonexistent[NEW] = 0;
Fseek (pfp, p_base, SEEK_SET);
p_input_line = p_bline - 1;
for (;;) {
register char *s;
register char *t;
register file_offset previous_line = this_line;
register bool last_line_was_command = this_is_a_command;
register bool stars_last_line = stars_this_line;
register int indent = 0;
int strip_trailing_cr;
size_t chars_read;
this_line = file_tell (pfp);
chars_read = pget_line (0, 0, 0);
if (chars_read == (size_t) -1)
memory_fatal ();
if (! chars_read) {
if (first_command_line >= 0) {
p_start = first_command_line;
p_sline = fcl_line;
retval = ED_DIFF;
goto scan_exit;
}
else {
p_start = this_line;
p_sline = p_input_line;
return NO_DIFF;
}
}
strip_trailing_cr = 2 <= chars_read && buf[chars_read - 2] == '\r';
for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
if (*s == '\t')
indent = (indent + 8) & ~7;
else
indent++;
}
for (t = s; ISDIGIT (*t) || *t == ','; t++)
continue;
this_is_a_command = (ISDIGIT (*s) &&
(*t == 'd' || *t == 'c' || *t == 'a') );
if (first_command_line < 0 && this_is_a_command) {
first_command_line = this_line;
fcl_line = p_input_line;
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
}
if (!stars_last_line && strnEQ(s, "*** ", 4))
name[OLD] = fetchname (s+4, strippath, &p_timestamp[OLD]);
else if (strnEQ(s, "+++ ", 4))
name[OLD] = fetchname (s+4, strippath, &p_timestamp[OLD]);
else if (strnEQ(s, "Index:", 6))
name[INDEX] = fetchname (s+6, strippath, (time_t *) 0);
else if (strnEQ(s, "Prereq:", 7)) {
for (t = s + 7; ISSPACE ((unsigned char) *t); t++)
continue;
revision = t;
for (t = revision; *t; t++)
if (ISSPACE ((unsigned char) *t))
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("Prereq: with multiple words at line %s of patch\n",
format_linenum (numbuf, this_line));
break;
}
if (t == revision)
revision = 0;
else {
char oldc = *t;
*t = '\0';
revision = savestr (revision);
*t = oldc;
}
} else
{
for (t = s; t[0] == '-' && t[1] == ' '; t += 2)
continue;
if (strnEQ(t, "--- ", 4))
{
time_t timestamp = (time_t) -1;
name[NEW] = fetchname (t+4, strippath, ×tamp);
if (timestamp != (time_t) -1)
{
p_timestamp[NEW] = timestamp;
p_rfc934_nesting = (t - s) >> 1;
}
}
}
if ((diff_type == NO_DIFF || diff_type == ED_DIFF) &&
first_command_line >= 0 &&
strEQ(s, ".\n") ) {
p_start = first_command_line;
p_sline = fcl_line;
retval = ED_DIFF;
goto scan_exit;
}
if ((diff_type == NO_DIFF || diff_type == UNI_DIFF)
&& strnEQ(s, "@@ -", 4)) {
time_t ti = p_timestamp[OLD];
p_timestamp[OLD] = p_timestamp[NEW];
p_timestamp[NEW] = ti;
t = name[OLD];
name[OLD] = name[NEW];
name[NEW] = t;
s += 4;
if (s[0] == '0' && !ISDIGIT (s[1]))
p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD];
while (*s != ' ' && *s != '\n')
s++;
while (*s == ' ')
s++;
if (s[0] == '+' && s[1] == '0' && !ISDIGIT (s[2]))
p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW];
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
p_start = this_line;
p_sline = p_input_line;
retval = UNI_DIFF;
if (! ((name[OLD] || ! p_timestamp[OLD])
&& (name[NEW] || ! p_timestamp[NEW]))
&& ! name[INDEX])
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("missing header for unified diff at line %s of patch\n",
format_linenum (numbuf, p_sline));
}
goto scan_exit;
}
stars_this_line = strnEQ(s, "********", 8);
if ((diff_type == NO_DIFF
|| diff_type == CONTEXT_DIFF
|| diff_type == NEW_CONTEXT_DIFF)
&& stars_last_line && strnEQ (s, "*** ", 4)) {
s += 4;
if (s[0] == '0' && !ISDIGIT (s[1]))
p_says_nonexistent[OLD] = 1 + ! p_timestamp[OLD];
while (*s != '\n')
s++;
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
p_start = previous_line;
p_sline = p_input_line - 1;
retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
{
file_offset saved_p_base = p_base;
LINENUM saved_p_bline = p_bline;
Fseek (pfp, previous_line, SEEK_SET);
p_input_line -= 2;
if (another_hunk (retval, 0)
&& ! p_repl_lines && p_newfirst == 1)
p_says_nonexistent[NEW] = 1 + ! p_timestamp[NEW];
next_intuit_at (saved_p_base, saved_p_bline);
}
if (! ((name[OLD] || ! p_timestamp[OLD])
&& (name[NEW] || ! p_timestamp[NEW]))
&& ! name[INDEX])
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
say ("missing header for context diff at line %s of patch\n",
format_linenum (numbuf, p_sline));
}
goto scan_exit;
}
if ((diff_type == NO_DIFF || diff_type == NORMAL_DIFF) &&
last_line_was_command &&
(strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
p_start = previous_line;
p_sline = p_input_line - 1;
p_indent = indent;
p_strip_trailing_cr = strip_trailing_cr;
retval = NORMAL_DIFF;
goto scan_exit;
}
}
scan_exit:
i = NONE;
if (!inname)
{
enum nametype i0 = NONE;
if (! posixly_correct && (name[OLD] || name[NEW]) && name[INDEX])
{
free (name[INDEX]);
name[INDEX] = 0;
}
for (i = OLD; i <= INDEX; i++)
if (name[i])
{
if (i0 != NONE && strcmp (name[i0], name[i]) == 0)
{
stat_errno[i] = stat_errno[i0];
if (! stat_errno[i])
st[i] = st[i0];
}
else if (stat (name[i], &st[i]) != 0)
stat_errno[i] = errno;
else
{
stat_errno[i] = 0;
if (posixly_correct)
break;
}
i0 = i;
}
if (! posixly_correct)
{
int is_empty;
i = best_name (name, stat_errno);
if (i == NONE && patch_get)
{
enum nametype nope = NONE;
for (i = OLD; i <= INDEX; i++)
if (name[i])
{
char const *cs;
char *getbuf;
char *diffbuf;
int readonly = outfile && strcmp (outfile, name[i]) != 0;
if (nope == NONE || strcmp (name[nope], name[i]) != 0)
{
cs = (version_controller
(name[i], readonly, (struct stat *) 0,
&getbuf, &diffbuf));
version_controlled[i] = !! cs;
if (cs)
{
if (version_get (name[i], cs, 0, readonly,
getbuf, &st[i]))
stat_errno[i] = 0;
else
version_controlled[i] = 0;
free (getbuf);
if (diffbuf)
free (diffbuf);
if (! stat_errno[i])
break;
}
}
nope = i;
}
}
is_empty = i == NONE || st[i].st_size == 0;
if ((! is_empty) < p_says_nonexistent[reverse ^ is_empty])
{
assert (i0 != NONE);
if (ok_to_reverse
("The next patch%s would %s the file %s,\nwhich %s!",
reverse ? ", when reversed," : "",
(i == NONE ? "delete"
: st[i].st_size == 0 ? "empty out"
: "create"),
quotearg (name[i == NONE || st[i].st_size == 0 ? i0 : i]),
(i == NONE ? "does not exist"
: st[i].st_size == 0 ? "is already empty"
: "already exists")))
reverse ^= 1;
}
if (i == NONE && p_says_nonexistent[reverse])
{
int newdirs[3];
int newdirs_min = INT_MAX;
int distance_from_minimum[3];
for (i = OLD; i <= INDEX; i++)
if (name[i])
{
newdirs[i] = (prefix_components (name[i], 0)
- prefix_components (name[i], 1));
if (newdirs[i] < newdirs_min)
newdirs_min = newdirs[i];
}
for (i = OLD; i <= INDEX; i++)
if (name[i])
distance_from_minimum[i] = newdirs[i] - newdirs_min;
i = best_name (name, distance_from_minimum);
}
}
}
if (i == NONE)
inerrno = -1;
else
{
inname = name[i];
name[i] = 0;
inerrno = stat_errno[i];
invc = version_controlled[i];
instat = st[i];
}
for (i = OLD; i <= INDEX; i++)
if (name[i])
free (name[i]);
if (inname)
{
size_t len = strlen (inname);
if (len >= 3
&& (strnEQ (inname, "../", 3)
|| strnEQ (inname + len - 3, "/..", 3)
|| strstr (inname, "/../")))
fatal ("rejecting file name with \"..\" component: %s",
quotearg (inname));
}
return retval;
}
static int
prefix_components (char *filename, int checkdirs)
{
int count = 0;
struct stat stat_buf;
int stat_result;
char *f = filename + FILESYSTEM_PREFIX_LEN (filename);
if (*f)
while (*++f)
if (ISSLASH (f[0]) && ! ISSLASH (f[-1]))
{
if (checkdirs)
{
*f = '\0';
stat_result = stat (filename, &stat_buf);
*f = '/';
if (! (stat_result == 0 && S_ISDIR (stat_buf.st_mode)))
break;
}
count++;
}
return count;
}
static enum nametype
best_name (char *const *name, int const *ignore)
{
enum nametype i;
int components[3];
int components_min = INT_MAX;
size_t basename_len[3];
size_t basename_len_min = SIZE_MAX;
size_t len[3];
size_t len_min = SIZE_MAX;
for (i = OLD; i <= INDEX; i++)
if (name[i] && !ignore[i])
{
components[i] = prefix_components (name[i], 0);
if (components_min < components[i])
continue;
components_min = components[i];
basename_len[i] = strlen (base_name (name[i]));
if (basename_len_min < basename_len[i])
continue;
basename_len_min = basename_len[i];
len[i] = strlen (name[i]);
if (len_min < len[i])
continue;
len_min = len[i];
}
for (i = OLD; i <= INDEX; i++)
if (name[i] && !ignore[i]
&& components[i] == components_min
&& basename_len[i] == basename_len_min
&& len[i] == len_min)
break;
return i;
}
static void
next_intuit_at (file_offset file_pos, LINENUM file_line)
{
p_base = file_pos;
p_bline = file_line;
}
static void
skip_to (file_offset file_pos, LINENUM file_line)
{
register FILE *i = pfp;
register FILE *o = stdout;
register int c;
assert(p_base <= file_pos);
if ((verbosity == VERBOSE || !inname) && p_base < file_pos) {
Fseek (i, p_base, SEEK_SET);
say ("The text leading up to this was:\n--------------------------\n");
while (file_tell (i) < file_pos)
{
putc ('|', o);
do
{
if ((c = getc (i)) == EOF)
read_fatal ();
putc (c, o);
}
while (c != '\n');
}
say ("--------------------------\n");
}
else
Fseek (i, file_pos, SEEK_SET);
p_input_line = file_line - 1;
}
static void
malformed (void)
{
char numbuf[LINENUM_LENGTH_BOUND + 1];
fatal ("malformed patch at line %s: %s",
format_linenum (numbuf, p_input_line), buf);
}
static char *
scan_linenum (char *s0, LINENUM *linenum)
{
char *s;
LINENUM n = 0;
int overflow = 0;
char numbuf[LINENUM_LENGTH_BOUND + 1];
for (s = s0; ISDIGIT (*s); s++)
{
LINENUM new_n = 10 * n + (*s - '0');
overflow |= new_n / 10 != n;
n = new_n;
}
if (s == s0)
fatal ("missing line number at line %s: %s",
format_linenum (numbuf, p_input_line), buf);
if (overflow)
fatal ("line number %.*s is too large at line %s: %s",
(int) (s - s0), s0, format_linenum (numbuf, p_input_line), buf);
*linenum = n;
return s;
}
int
another_hunk (enum diff difftype, int rev)
{
register char *s;
register LINENUM context = 0;
register size_t chars_read;
char numbuf0[LINENUM_LENGTH_BOUND + 1];
char numbuf1[LINENUM_LENGTH_BOUND + 1];
char numbuf2[LINENUM_LENGTH_BOUND + 1];
char numbuf3[LINENUM_LENGTH_BOUND + 1];
while (p_end >= 0) {
if (p_end == p_efake)
p_end = p_bfake;
else
free(p_line[p_end]);
p_end--;
}
assert(p_end == -1);
p_efake = -1;
p_max = hunkmax;
if (difftype == CONTEXT_DIFF || difftype == NEW_CONTEXT_DIFF) {
file_offset line_beginning = file_tell (pfp);
LINENUM repl_beginning = 0;
register LINENUM fillcnt = 0;
register LINENUM fillsrc;
register LINENUM filldst;
bool ptrn_spaces_eaten = FALSE;
bool some_context = FALSE;
register bool repl_could_be_missing = TRUE;
bool ptrn_missing = FALSE;
bool repl_missing = FALSE;
file_offset repl_backtrack_position = 0;
LINENUM repl_patch_line;
LINENUM repl_context;
LINENUM ptrn_prefix_context = -1;
LINENUM ptrn_suffix_context = -1;
LINENUM repl_prefix_context = -1;
LINENUM ptrn_copiable = 0;
LINENUM repl_copiable = 0;
fillsrc = filldst = repl_patch_line = repl_context = 0;
chars_read = get_line ();
if (chars_read == (size_t) -1
|| chars_read <= 8
|| strncmp (buf, "********", 8) != 0) {
next_intuit_at(line_beginning,p_input_line);
return chars_read == (size_t) -1 ? -1 : 0;
}
p_hunk_beg = p_input_line + 1;
while (p_end < p_max) {
chars_read = get_line ();
if (chars_read == (size_t) -1)
return -1;
if (!chars_read) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = TRUE;
goto hunk_done;
}
if (p_max - p_end < 4) {
strcpy (buf, " \n");
chars_read = 3;
} else {
fatal ("unexpected end of file in patch");
}
}
p_end++;
if (p_end == hunkmax)
fatal ("unterminated hunk starting at line %s; giving up at line %s: %s",
format_linenum (numbuf0, pch_hunk_beg ()),
format_linenum (numbuf1, p_input_line), buf);
assert(p_end < hunkmax);
p_Char[p_end] = *buf;
p_len[p_end] = 0;
p_line[p_end] = 0;
switch (*buf) {
case '*':
if (strnEQ(buf, "********", 8)) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = TRUE;
goto hunk_done;
}
else
fatal ("unexpected end of hunk at line %s",
format_linenum (numbuf0, p_input_line));
}
if (p_end != 0) {
if (repl_beginning && repl_could_be_missing) {
repl_missing = TRUE;
goto hunk_done;
}
fatal ("unexpected `***' at line %s: %s",
format_linenum (numbuf0, p_input_line), buf);
}
context = 0;
p_len[p_end] = strlen (buf);
if (! (p_line[p_end] = savestr (buf))) {
p_end--;
return -1;
}
for (s = buf; *s && !ISDIGIT (*s); s++)
continue;
if (strnEQ(s,"0,0",3))
remove_prefix (s, 2);
s = scan_linenum (s, &p_first);
if (*s == ',') {
while (*s && !ISDIGIT (*s))
s++;
scan_linenum (s, &p_ptrn_lines);
p_ptrn_lines += 1 - p_first;
}
else if (p_first)
p_ptrn_lines = 1;
else {
p_ptrn_lines = 0;
p_first = 1;
}
p_max = p_ptrn_lines + 6;
while (p_max >= hunkmax)
if (! grow_hunkmax ())
return -1;
p_max = hunkmax;
break;
case '-':
if (buf[1] != '-')
goto change_line;
if (ptrn_prefix_context == -1)
ptrn_prefix_context = context;
ptrn_suffix_context = context;
if (repl_beginning
|| (p_end
!= p_ptrn_lines + 1 + (p_Char[p_end - 1] == '\n')))
{
if (p_end == 1)
{
ptrn_missing = TRUE;
p_end = p_ptrn_lines + 1;
ptrn_prefix_context = ptrn_suffix_context = -1;
fillsrc = p_end + 1;
filldst = 1;
fillcnt = p_ptrn_lines;
}
else if (! repl_beginning)
fatal ("%s `---' at line %s; check line numbers at line %s",
(p_end <= p_ptrn_lines
? "Premature"
: "Overdue"),
format_linenum (numbuf0, p_input_line),
format_linenum (numbuf1, p_hunk_beg));
else if (! repl_could_be_missing)
fatal ("duplicate `---' at line %s; check line numbers at line %s",
format_linenum (numbuf0, p_input_line),
format_linenum (numbuf1,
p_hunk_beg + repl_beginning));
else
{
repl_missing = TRUE;
goto hunk_done;
}
}
repl_beginning = p_end;
repl_backtrack_position = file_tell (pfp);
repl_patch_line = p_input_line;
repl_context = context;
p_len[p_end] = strlen (buf);
if (! (p_line[p_end] = savestr (buf)))
{
p_end--;
return -1;
}
p_Char[p_end] = '=';
for (s = buf; *s && ! ISDIGIT (*s); s++)
continue;
s = scan_linenum (s, &p_newfirst);
if (*s == ',')
{
do
{
if (!*++s)
malformed ();
}
while (! ISDIGIT (*s));
scan_linenum (s, &p_repl_lines);
p_repl_lines += 1 - p_newfirst;
}
else if (p_newfirst)
p_repl_lines = 1;
else
{
p_repl_lines = 0;
p_newfirst = 1;
}
p_max = p_repl_lines + p_end;
while (p_max >= hunkmax)
if (! grow_hunkmax ())
return -1;
if (p_repl_lines != ptrn_copiable
&& (p_prefix_context != 0
|| context != 0
|| p_repl_lines != 1))
repl_could_be_missing = FALSE;
context = 0;
break;
case '+': case '!':
repl_could_be_missing = FALSE;
change_line:
s = buf + 1;
chars_read--;
if (*s == '\n' && canonicalize) {
strcpy (s, " \n");
chars_read = 2;
}
if (*s == ' ' || *s == '\t') {
s++;
chars_read--;
} else if (repl_beginning && repl_could_be_missing) {
repl_missing = TRUE;
goto hunk_done;
}
if (! repl_beginning)
{
if (ptrn_prefix_context == -1)
ptrn_prefix_context = context;
}
else
{
if (repl_prefix_context == -1)
repl_prefix_context = context;
}
chars_read -=
(1 < chars_read
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
if (! (p_line[p_end] = savebuf (s, chars_read))) {
p_end--;
return -1;
}
context = 0;
break;
case '\t': case '\n':
s = buf;
if (*buf == '\t') {
s++;
chars_read--;
}
if (repl_beginning && repl_could_be_missing &&
(!ptrn_spaces_eaten || difftype == NEW_CONTEXT_DIFF) ) {
repl_missing = TRUE;
goto hunk_done;
}
chars_read -=
(1 < chars_read
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
if (! (p_line[p_end] = savebuf (buf, chars_read))) {
p_end--;
return -1;
}
if (p_end != p_ptrn_lines + 1) {
ptrn_spaces_eaten |= (repl_beginning != 0);
some_context = TRUE;
context++;
if (repl_beginning)
repl_copiable++;
else
ptrn_copiable++;
p_Char[p_end] = ' ';
}
break;
case ' ':
s = buf + 1;
chars_read--;
if (*s == '\n' && canonicalize) {
strcpy (s, "\n");
chars_read = 2;
}
if (*s == ' ' || *s == '\t') {
s++;
chars_read--;
} else if (repl_beginning && repl_could_be_missing) {
repl_missing = TRUE;
goto hunk_done;
}
some_context = TRUE;
context++;
if (repl_beginning)
repl_copiable++;
else
ptrn_copiable++;
chars_read -=
(1 < chars_read
&& p_end == (repl_beginning ? p_max : p_ptrn_lines)
&& incomplete_line ());
p_len[p_end] = chars_read;
if (! (p_line[p_end] = savebuf (buf + 2, chars_read))) {
p_end--;
return -1;
}
break;
default:
if (repl_beginning && repl_could_be_missing) {
repl_missing = TRUE;
goto hunk_done;
}
malformed ();
}
}
hunk_done:
if (p_end >=0 && !repl_beginning)
fatal ("no `---' found in patch at line %s",
format_linenum (numbuf0, pch_hunk_beg ()));
if (repl_missing) {
p_input_line = repl_patch_line;
context = repl_context;
for (p_end--; p_end > repl_beginning; p_end--)
free(p_line[p_end]);
Fseek (pfp, repl_backtrack_position, SEEK_SET);
fillsrc = 1;
filldst = repl_beginning+1;
fillcnt = p_repl_lines;
p_end = p_max;
}
else if (! ptrn_missing && ptrn_copiable != repl_copiable)
fatal ("context mangled in hunk at line %s",
format_linenum (numbuf0, p_hunk_beg));
else if (!some_context && fillcnt == 1) {
while (filldst < p_end) {
p_line[filldst] = p_line[filldst+1];
p_Char[filldst] = p_Char[filldst+1];
p_len[filldst] = p_len[filldst+1];
filldst++;
}
#if 0
repl_beginning--;
#endif
p_end--;
p_first++;
fillcnt = 0;
p_ptrn_lines = 0;
}
p_prefix_context = ((repl_prefix_context == -1
|| (ptrn_prefix_context != -1
&& ptrn_prefix_context < repl_prefix_context))
? ptrn_prefix_context : repl_prefix_context);
p_suffix_context = ((ptrn_suffix_context != -1
&& ptrn_suffix_context < context)
? ptrn_suffix_context : context);
assert (p_prefix_context != -1 && p_suffix_context != -1);
if (difftype == CONTEXT_DIFF
&& (fillcnt
|| (p_first > 1
&& p_prefix_context + p_suffix_context < ptrn_copiable))) {
if (verbosity == VERBOSE)
say ("%s\n%s\n%s\n",
"(Fascinating -- this is really a new-style context diff but without",
"the telltale extra asterisks on the *** line that usually indicate",
"the new style...)");
diff_type = difftype = NEW_CONTEXT_DIFF;
}
if (fillcnt) {
p_bfake = filldst;
p_efake = filldst + fillcnt - 1;
while (fillcnt-- > 0) {
while (fillsrc <= p_end && fillsrc != repl_beginning
&& p_Char[fillsrc] != ' ')
fillsrc++;
if (p_end < fillsrc || fillsrc == repl_beginning)
{
fatal ("replacement text or line numbers mangled in hunk at line %s",
format_linenum (numbuf0, p_hunk_beg));
}
p_line[filldst] = p_line[fillsrc];
p_Char[filldst] = p_Char[fillsrc];
p_len[filldst] = p_len[fillsrc];
fillsrc++; filldst++;
}
while (fillsrc <= p_end && fillsrc != repl_beginning)
{
if (p_Char[fillsrc] == ' ')
fatal ("replacement text or line numbers mangled in hunk at line %s",
format_linenum (numbuf0, p_hunk_beg));
fillsrc++;
}
if (debug & 64)
printf ("fillsrc %s, filldst %s, rb %s, e+1 %s\n",
format_linenum (numbuf0, fillsrc),
format_linenum (numbuf1, filldst),
format_linenum (numbuf2, repl_beginning),
format_linenum (numbuf3, p_end + 1));
assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
assert(filldst==p_end+1 || filldst==repl_beginning);
}
}
else if (difftype == UNI_DIFF) {
file_offset line_beginning = file_tell (pfp);
register LINENUM fillsrc;
register LINENUM filldst;
char ch = '\0';
chars_read = get_line ();
if (chars_read == (size_t) -1
|| chars_read <= 4
|| strncmp (buf, "@@ -", 4) != 0) {
next_intuit_at(line_beginning,p_input_line);
return chars_read == (size_t) -1 ? -1 : 0;
}
s = scan_linenum (buf + 4, &p_first);
if (*s == ',')
s = scan_linenum (s + 1, &p_ptrn_lines);
else
p_ptrn_lines = 1;
if (*s == ' ') s++;
if (*s != '+')
malformed ();
s = scan_linenum (s + 1, &p_newfirst);
if (*s == ',')
s = scan_linenum (s + 1, &p_repl_lines);
else
p_repl_lines = 1;
if (*s == ' ') s++;
if (*s != '@')
malformed ();
if (!p_ptrn_lines)
p_first++;
if (!p_repl_lines)
p_newfirst++;
p_max = p_ptrn_lines + p_repl_lines + 1;
while (p_max >= hunkmax)
if (! grow_hunkmax ())
return -1;
fillsrc = 1;
filldst = fillsrc + p_ptrn_lines;
p_end = filldst + p_repl_lines;
sprintf (buf, "*** %s,%s ****\n",
format_linenum (numbuf0, p_first),
format_linenum (numbuf1, p_first + p_ptrn_lines - 1));
p_len[0] = strlen (buf);
if (! (p_line[0] = savestr (buf))) {
p_end = -1;
return -1;
}
p_Char[0] = '*';
sprintf (buf, "--- %s,%s ----\n",
format_linenum (numbuf0, p_newfirst),
format_linenum (numbuf1, p_newfirst + p_repl_lines - 1));
p_len[filldst] = strlen (buf);
if (! (p_line[filldst] = savestr (buf))) {
p_end = 0;
return -1;
}
p_Char[filldst++] = '=';
p_prefix_context = -1;
p_hunk_beg = p_input_line + 1;
while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
chars_read = get_line ();
if (!chars_read) {
if (p_max - filldst < 3) {
strcpy (buf, " \n");
chars_read = 2;
} else {
fatal ("unexpected end of file in patch");
}
}
if (chars_read == (size_t) -1)
s = 0;
else if (*buf == '\t' || *buf == '\n') {
ch = ' ';
s = savebuf (buf, chars_read);
}
else {
ch = *buf;
s = savebuf (buf+1, --chars_read);
}
if (!s) {
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
return -1;
}
switch (ch) {
case '-':
if (fillsrc > p_ptrn_lines) {
free(s);
p_end = filldst-1;
malformed ();
}
chars_read -= fillsrc == p_ptrn_lines && incomplete_line ();
p_Char[fillsrc] = ch;
p_line[fillsrc] = s;
p_len[fillsrc++] = chars_read;
break;
case '=':
ch = ' ';
case ' ':
if (fillsrc > p_ptrn_lines) {
free(s);
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
malformed ();
}
context++;
chars_read -= fillsrc == p_ptrn_lines && incomplete_line ();
p_Char[fillsrc] = ch;
p_line[fillsrc] = s;
p_len[fillsrc++] = chars_read;
s = savebuf (s, chars_read);
if (!s) {
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
return -1;
}
case '+':
if (filldst > p_end) {
free(s);
while (--filldst > p_ptrn_lines)
free(p_line[filldst]);
p_end = fillsrc-1;
malformed ();
}
chars_read -= filldst == p_end && incomplete_line ();
p_Char[filldst] = ch;
p_line[filldst] = s;
p_len[filldst++] = chars_read;
break;
default:
p_end = filldst;
malformed ();
}
if (ch != ' ') {
if (p_prefix_context == -1)
p_prefix_context = context;
context = 0;
}
}
if (p_prefix_context == -1)
malformed ();
p_suffix_context = context;
}
else {
char hunk_type;
register int i;
LINENUM min, max;
file_offset line_beginning = file_tell (pfp);
p_prefix_context = p_suffix_context = 0;
chars_read = get_line ();
if (chars_read == (size_t) -1 || !chars_read || !ISDIGIT (*buf)) {
next_intuit_at(line_beginning,p_input_line);
return chars_read == (size_t) -1 ? -1 : 0;
}
s = scan_linenum (buf, &p_first);
if (*s == ',') {
s = scan_linenum (s + 1, &p_ptrn_lines);
p_ptrn_lines += 1 - p_first;
}
else
p_ptrn_lines = (*s != 'a');
hunk_type = *s;
if (hunk_type == 'a')
p_first++;
s = scan_linenum (s + 1, &min);
if (*s == ',')
scan_linenum (s + 1, &max);
else
max = min;
if (hunk_type == 'd')
min++;
p_end = p_ptrn_lines + 1 + max - min + 1;
while (p_end >= hunkmax)
if (! grow_hunkmax ())
{
p_end = -1;
return -1;
}
p_newfirst = min;
p_repl_lines = max - min + 1;
sprintf (buf, "*** %s,%s\n",
format_linenum (numbuf0, p_first),
format_linenum (numbuf1, p_first + p_ptrn_lines - 1));
p_len[0] = strlen (buf);
if (! (p_line[0] = savestr (buf))) {
p_end = -1;
return -1;
}
p_Char[0] = '*';
for (i=1; i<=p_ptrn_lines; i++) {
chars_read = get_line ();
if (chars_read == (size_t) -1)
{
p_end = i - 1;
return -1;
}
if (!chars_read)
fatal ("unexpected end of file in patch at line %s",
format_linenum (numbuf0, p_input_line));
if (buf[0] != '<' || (buf[1] != ' ' && buf[1] != '\t'))
fatal ("`<' expected at line %s of patch",
format_linenum (numbuf0, p_input_line));
chars_read -= 2 + (i == p_ptrn_lines && incomplete_line ());
p_len[i] = chars_read;
if (! (p_line[i] = savebuf (buf + 2, chars_read))) {
p_end = i-1;
return -1;
}
p_Char[i] = '-';
}
if (hunk_type == 'c') {
chars_read = get_line ();
if (chars_read == (size_t) -1)
{
p_end = i - 1;
return -1;
}
if (! chars_read)
fatal ("unexpected end of file in patch at line %s",
format_linenum (numbuf0, p_input_line));
if (*buf != '-')
fatal ("`---' expected at line %s of patch",
format_linenum (numbuf0, p_input_line));
}
sprintf (buf, "--- %s,%s\n",
format_linenum (numbuf0, min),
format_linenum (numbuf1, max));
p_len[i] = strlen (buf);
if (! (p_line[i] = savestr (buf))) {
p_end = i-1;
return -1;
}
p_Char[i] = '=';
for (i++; i<=p_end; i++) {
chars_read = get_line ();
if (chars_read == (size_t) -1)
{
p_end = i - 1;
return -1;
}
if (!chars_read)
fatal ("unexpected end of file in patch at line %s",
format_linenum (numbuf0, p_input_line));
if (buf[0] != '>' || (buf[1] != ' ' && buf[1] != '\t'))
fatal ("`>' expected at line %s of patch",
format_linenum (numbuf0, p_input_line));
chars_read -= 2 + (i == p_end && incomplete_line ());
p_len[i] = chars_read;
if (! (p_line[i] = savebuf (buf + 2, chars_read))) {
p_end = i-1;
return -1;
}
p_Char[i] = '+';
}
}
if (rev)
if (!pch_swap())
say ("Not enough memory to swap next hunk!\n");
if (debug & 2) {
LINENUM i;
char special;
for (i=0; i <= p_end; i++) {
if (i == p_ptrn_lines)
special = '^';
else
special = ' ';
fprintf (stderr, "%s %c %c ", format_linenum (numbuf0, i),
p_Char[i], special);
pch_write_line (i, stderr);
fflush (stderr);
}
}
if (p_end+1 < hunkmax)
p_Char[p_end+1] = '^';
return 1;
}
static size_t
get_line (void)
{
return pget_line (p_indent, p_rfc934_nesting, p_strip_trailing_cr);
}
static size_t
pget_line (int indent, int rfc934_nesting, int strip_trailing_cr)
{
register FILE *fp = pfp;
register int c;
register int i;
register char *b;
register size_t s;
do
{
i = 0;
for (;;)
{
c = getc (fp);
if (c == EOF)
{
if (ferror (fp))
read_fatal ();
return 0;
}
if (indent <= i)
break;
if (c == ' ' || c == 'X')
i++;
else if (c == '\t')
i = (i + 8) & ~7;
else
break;
}
i = 0;
b = buf;
while (c == '-' && 0 <= --rfc934_nesting)
{
c = getc (fp);
if (c == EOF)
goto patch_ends_in_middle_of_line;
if (c != ' ')
{
i = 1;
b[0] = '-';
break;
}
c = getc (fp);
if (c == EOF)
goto patch_ends_in_middle_of_line;
}
s = bufsize;
for (;;)
{
if (i == s - 1)
{
s *= 2;
b = realloc (b, s);
if (!b)
{
if (!using_plan_a)
memory_fatal ();
return (size_t) -1;
}
buf = b;
bufsize = s;
}
b[i++] = c;
if (c == '\n')
break;
c = getc (fp);
if (c == EOF)
goto patch_ends_in_middle_of_line;
}
p_input_line++;
}
while (*b == '#');
if (strip_trailing_cr && 2 <= i && b[i - 2] == '\r')
b[i-- - 2] = '\n';
b[i] = '\0';
return i;
patch_ends_in_middle_of_line:
if (ferror (fp))
read_fatal ();
say ("patch unexpectedly ends in middle of line\n");
return 0;
}
static bool
incomplete_line (void)
{
register FILE *fp = pfp;
register int c;
register file_offset line_beginning = file_tell (fp);
if (getc (fp) == '\\')
{
while ((c = getc (fp)) != '\n' && c != EOF)
continue;
return TRUE;
}
else
{
Fseek (pfp, line_beginning, SEEK_SET);
return FALSE;
}
}
bool
pch_swap (void)
{
char **tp_line;
size_t *tp_len;
char *tp_char;
register LINENUM i;
register LINENUM n;
bool blankline = FALSE;
register char *s;
i = p_first;
p_first = p_newfirst;
p_newfirst = i;
tp_line = p_line;
tp_len = p_len;
tp_char = p_Char;
p_line = 0;
p_len = 0;
p_Char = 0;
set_hunkmax();
if (!p_line || !p_len || !p_Char) {
if (p_line)
free (p_line);
p_line = tp_line;
if (p_len)
free (p_len);
p_len = tp_len;
if (p_Char)
free (p_Char);
p_Char = tp_char;
return FALSE;
}
i = p_ptrn_lines + 1;
if (tp_char[i] == '\n') {
blankline = TRUE;
i++;
}
if (p_efake >= 0) {
if (p_efake <= i)
n = p_end - i + 1;
else
n = -i;
p_efake += n;
p_bfake += n;
}
for (n=0; i <= p_end; i++,n++) {
p_line[n] = tp_line[i];
p_Char[n] = tp_char[i];
if (p_Char[n] == '+')
p_Char[n] = '-';
p_len[n] = tp_len[i];
}
if (blankline) {
i = p_ptrn_lines + 1;
p_line[n] = tp_line[i];
p_Char[n] = tp_char[i];
p_len[n] = tp_len[i];
n++;
}
assert(p_Char[0] == '=');
p_Char[0] = '*';
for (s=p_line[0]; *s; s++)
if (*s == '-')
*s = '*';
assert(tp_char[0] == '*');
tp_char[0] = '=';
for (s=tp_line[0]; *s; s++)
if (*s == '*')
*s = '-';
for (i=0; n <= p_end; i++,n++) {
p_line[n] = tp_line[i];
p_Char[n] = tp_char[i];
if (p_Char[n] == '-')
p_Char[n] = '+';
p_len[n] = tp_len[i];
}
assert(i == p_ptrn_lines + 1);
i = p_ptrn_lines;
p_ptrn_lines = p_repl_lines;
p_repl_lines = i;
if (tp_line)
free (tp_line);
if (tp_len)
free (tp_len);
if (tp_char)
free (tp_char);
return TRUE;
}
bool
pch_says_nonexistent (int which)
{
return p_says_nonexistent[which];
}
time_t
pch_timestamp (int which)
{
return p_timestamp[which];
}
LINENUM
pch_first (void)
{
return p_first;
}
LINENUM
pch_ptrn_lines (void)
{
return p_ptrn_lines;
}
LINENUM
pch_newfirst (void)
{
return p_newfirst;
}
LINENUM
pch_repl_lines (void)
{
return p_repl_lines;
}
LINENUM
pch_end (void)
{
return p_end;
}
LINENUM
pch_prefix_context (void)
{
return p_prefix_context;
}
LINENUM
pch_suffix_context (void)
{
return p_suffix_context;
}
size_t
pch_line_len (LINENUM line)
{
return p_len[line];
}
char
pch_char (LINENUM line)
{
return p_Char[line];
}
char *
pfetch (LINENUM line)
{
return p_line[line];
}
bool
pch_write_line (LINENUM line, FILE *file)
{
bool after_newline = p_line[line][p_len[line] - 1] == '\n';
if (! fwrite (p_line[line], sizeof (*p_line[line]), p_len[line], file))
write_fatal ();
return after_newline;
}
LINENUM
pch_hunk_beg (void)
{
return p_hunk_beg;
}
void
do_ed_script (FILE *ofp)
{
static char const ed_program[] = ed_PROGRAM;
register char *t;
register file_offset beginning_of_this_line;
register bool this_line_is_command = FALSE;
register FILE *pipefp = 0;
register size_t chars_read;
if (! dry_run && ! skip_rest_of_patch) {
int exclusive = TMPOUTNAME_needs_removal ? 0 : O_EXCL;
assert (! inerrno);
TMPOUTNAME_needs_removal = 1;
copy_file (inname, TMPOUTNAME, exclusive, instat.st_mode);
sprintf (buf, "%s %s%s", ed_program, verbosity == VERBOSE ? "" : "- ",
TMPOUTNAME);
fflush (stdout);
pipefp = popen(buf, binary_transput ? "wb" : "w");
if (!pipefp)
pfatal ("Can't open pipe to %s", quotearg (buf));
}
for (;;) {
beginning_of_this_line = file_tell (pfp);
chars_read = get_line ();
if (! chars_read) {
next_intuit_at(beginning_of_this_line,p_input_line);
break;
}
for (t = buf; ISDIGIT (*t) || *t == ','; t++)
continue;
this_line_is_command = (ISDIGIT (*buf) &&
(*t == 'd' || *t == 'c' || *t == 'a' || *t == 'i' || *t == 's') );
if (this_line_is_command) {
if (pipefp)
if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
write_fatal ();
if (*t != 'd' && *t != 's') {
while ((chars_read = get_line ()) != 0) {
if (pipefp)
if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
write_fatal ();
if (chars_read == 2 && strEQ (buf, ".\n"))
break;
}
}
}
else {
next_intuit_at(beginning_of_this_line,p_input_line);
break;
}
}
if (!pipefp)
return;
if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
|| fflush (pipefp) != 0)
write_fatal ();
if (pclose (pipefp) != 0)
fatal ("%s FAILED", ed_program);
if (ofp)
{
FILE *ifp = fopen (TMPOUTNAME, binary_transput ? "rb" : "r");
int c;
if (!ifp)
pfatal ("can't open `%s'", TMPOUTNAME);
while ((c = getc (ifp)) != EOF)
if (putc (c, ofp) == EOF)
write_fatal ();
if (ferror (ifp) || fclose (ifp) != 0)
read_fatal ();
}
}