diff options
Diffstat (limited to 'pch.c')
-rw-r--r-- | pch.c | 1318 |
1 files changed, 773 insertions, 545 deletions
@@ -1,78 +1,82 @@ -/* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $ - * - * $Log: pch.c,v $ - * Revision 2.0.2.0 90/05/01 22:17:51 davison - * patch12u: unidiff support added - * - * Revision 2.0.1.7 88/06/03 15:13:28 lwall - * patch10: Can now find patches in shar scripts. - * patch10: Hunks that swapped and then swapped back could core dump. - * - * Revision 2.0.1.6 87/06/04 16:18:13 lwall - * pch_swap didn't swap p_bfake and p_efake. - * - * Revision 2.0.1.5 87/01/30 22:47:42 lwall - * Improved responses to mangled patches. - * - * Revision 2.0.1.4 87/01/05 16:59:53 lwall - * New-style context diffs caused double call to free(). - * - * Revision 2.0.1.3 86/11/14 10:08:33 lwall - * Fixed problem where a long pattern wouldn't grow the hunk. - * Also restored p_input_line when backtracking so error messages are right. - * - * Revision 2.0.1.2 86/11/03 17:49:52 lwall - * New-style delete triggers spurious assertion error. - * - * Revision 2.0.1.1 86/10/29 15:52:08 lwall - * Could falsely report new-style context diff. - * - * Revision 2.0 86/09/17 15:39:37 lwall - * Baseline for netwide release. - * - */ - -#include "EXTERN.h" -#include "common.h" -#include "util.h" -#include "INTERN.h" -#include "pch.h" +/* reading patches */ + +/* $Id: pch.c,v 1.7 1997/04/14 05:32:30 eggert Exp $ */ + +/* +Copyright 1986, 1987, 1988 Larry Wall +Copyright 1990, 1991, 1992, 1993, 1997 Free Software Foundation, Inc. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING. +If not, write to the Free Software Foundation, +59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#define XTERN extern +#include <common.h> +#include <inp.h> +#include <util.h> +#undef XTERN +#define XTERN +#include <pch.h> + +#define INITHUNKMAX 125 /* initial dynamic allocation size */ /* Patch (diff listing) abstract type. */ -static long p_filesize; /* size of the patch file */ +static FILE *pfp; /* patch file pointer */ +static off_t p_filesize; /* size of the patch file */ static LINENUM p_first; /* 1st line number */ static LINENUM p_newfirst; /* 1st line number of replacement */ static LINENUM p_ptrn_lines; /* # lines in pattern */ static LINENUM p_repl_lines; /* # lines in replacement text */ static LINENUM p_end = -1; /* last line in hunk */ static LINENUM p_max; /* max allowed value of p_end */ -static LINENUM p_context = 3; /* # of context lines */ -static LINENUM p_input_line = 0; /* current line # from patch file */ -static char **p_line = Null(char**); /* the text of the hunk */ -static short *p_len = Null(short*); /* length of each line */ -static char *p_Char = Nullch; /* +, -, and ! */ -static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */ +static LINENUM p_prefix_context; /* # of prefix context lines */ +static LINENUM p_suffix_context; /* # of suffix context lines */ +static LINENUM p_input_line; /* current line # from patch file */ +static char **p_line; /* the text of the hunk */ +static size_t *p_len; /* line length including \n if any */ +static char *p_Char; /* +, -, and ! */ +static size_t hunkmax = INITHUNKMAX; /* size of above arrays */ static int p_indent; /* indent to patch */ -static LINENUM p_base; /* where to intuit this time */ +static long p_base; /* where to intuit this time */ static LINENUM p_bline; /* line # of p_base */ -static LINENUM p_start; /* where intuit found a patch */ +static long p_start; /* where intuit found a patch */ static LINENUM p_sline; /* and the line number for it */ static LINENUM p_hunk_beg; /* line number of current hunk */ static LINENUM p_efake = -1; /* end of faked up lines--don't free */ static LINENUM p_bfake = -1; /* beg of faked up lines */ +static enum diff intuit_diff_type PARAMS ((void)); +static size_t pget_line PARAMS ((int)); +static size_t get_line PARAMS ((void)); +static bool incomplete_line PARAMS ((void)); +static bool grow_hunkmax PARAMS ((void)); +static void next_intuit_at PARAMS ((long, LINENUM)); +static void skip_to PARAMS ((long, LINENUM)); + /* Prepare to look for the next patch in the patch file. */ void re_patch() { - p_first = Nulline; - p_newfirst = Nulline; - p_ptrn_lines = Nulline; - p_repl_lines = Nulline; - p_end = (LINENUM)-1; - p_max = Nulline; + p_first = 0; + p_newfirst = 0; + p_ptrn_lines = 0; + p_repl_lines = 0; + p_end = -1; + p_max = 0; p_indent = 0; } @@ -80,23 +84,45 @@ re_patch() void open_patch_file(filename) -char *filename; + char const *filename; { - if (filename == Nullch || !*filename || strEQ(filename, "-")) { - pfp = fopen(TMPPATNAME, "w"); - if (pfp == Nullfp) - pfatal2("can't create %s", TMPPATNAME); - while (fgets(buf, sizeof buf, stdin) != Nullch) - fputs(buf, pfp); - Fclose(pfp); - filename = TMPPATNAME; + long file_pos = 0; + struct stat st; + if (!filename || !*filename || strEQ (filename, "-")) + { + if (fstat (STDIN_FILENO, &st) != 0) + pfatal ("fstat"); + if (S_ISREG (st.st_mode)) + { + pfp = stdin; + file_pos = ftell (stdin); + filename = 0; + } + else + { + size_t charsread; + pfp = fopen (TMPPATNAME, "w"); + if (!pfp) + pfatal ("can't create %s", TMPPATNAME); + while ((charsread = fread (buf, 1, bufsize, stdin)) != 0) + if (fwrite (buf, 1, charsread, pfp) != charsread) + write_fatal (); + if (ferror (stdin) || fclose (stdin) != 0) + read_fatal (); + if (fclose (pfp) != 0) + write_fatal (); + filename = TMPPATNAME; + } + } + if (filename) { + pfp = fopen (filename, "r"); + if (!pfp) + pfatal ("patch file %s not found", filename); + if (fstat (fileno (pfp), &st) != 0) + pfatal ("fstat"); } - pfp = fopen(filename, "r"); - if (pfp == Nullfp) - pfatal2("patch file %s not found", filename); - Fstat(fileno(pfp), &filestat); - p_filesize = filestat.st_size; - next_intuit_at(0L,1L); /* start at the beginning */ + p_filesize = st.st_size; + next_intuit_at (file_pos, (LINENUM) 1); set_hunkmax(); } @@ -105,39 +131,32 @@ char *filename; void set_hunkmax() { -#ifndef lint - if (p_line == Null(char**)) - p_line = (char**) malloc((MEM)hunkmax * sizeof(char *)); - if (p_len == Null(short*)) - p_len = (short*) malloc((MEM)hunkmax * sizeof(short)); -#endif - if (p_Char == Nullch) - p_Char = (char*) malloc((MEM)hunkmax * sizeof(char)); + 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); } /* Enlarge the arrays containing the current hunk of patch. */ -void +static bool grow_hunkmax() { hunkmax *= 2; - /* - * Note that on most systems, only the p_line array ever gets fresh memory - * since p_len can move into p_line's old space, and p_Char can move into - * p_len's old space. Not on PDP-11's however. But it doesn't matter. - */ - assert(p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch); -#ifndef lint - p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *)); - p_len = (short*) realloc((char*)p_len, (MEM)hunkmax * sizeof(short)); - p_Char = (char*) realloc((char*)p_Char, (MEM)hunkmax * sizeof(char)); -#endif - if (p_line != Null(char**) && p_len != Null(short*) && p_Char != Nullch) - return; + 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) - fatal1("out of memory\n"); - out_of_mem = TRUE; /* whatever is null will be allocated again */ - /* from within plan_a(), of all places */ + memory_fatal (); + /* Don't free previous values of p_line etc., + since some broken implementations free them for us. + Whatever is null will be allocated again from within plan_a (), + of all places. */ + return FALSE; } /* True if the remainder of the patch file contains a diff of some sort. */ @@ -145,55 +164,47 @@ grow_hunkmax() bool there_is_another_patch() { - if (p_base != 0L && p_base >= p_filesize) { - if (verbose) - say1("done\n"); + if (p_base != 0 && p_base >= p_filesize) { + if (verbosity == VERBOSE) + say ("done\n"); return FALSE; } - if (verbose) - say1("Hmm..."); + if (verbosity == VERBOSE) + say ("Hmm..."); diff_type = intuit_diff_type(); - if (!diff_type) { - if (p_base != 0L) { - if (verbose) - say1(" Ignoring the trailing garbage.\ndone\n"); - } - else - say1(" I can't seem to find a patch in there anywhere.\n"); + 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"); return FALSE; } - if (verbose) - say3(" %sooks like %s to me...\n", - (p_base == 0L ? "L" : "The next patch l"), + 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 (p_indent && verbose) - say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s"); + if (p_indent && verbosity != SILENT) + say ("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s"); skip_to(p_start,p_sline); - while (filearg[0] == Nullch) { + while (!inname) { if (force || batch) { - say1("No file to patch. Skipping...\n"); - filearg[0] = savestr(bestguess); + say ("No file to patch. Skipping...\n"); + skip_rest_of_patch = TRUE; return TRUE; } - ask1("File to patch: "); - if (*buf != '\n') { - if (bestguess) - free(bestguess); - bestguess = savestr(buf); - filearg[0] = fetchname(buf, 0, FALSE); - } - if (filearg[0] == Nullch) { - ask1("No file found--skip this patch? [n] "); - if (*buf != 'y') { + ask ("File to patch: "); + inname = fetchname (buf, 0); + if (!inname) { + ask ("Skip this patch? [y] "); + if (*buf == 'n') { continue; } - if (verbose) - say1("Skipping patch...\n"); - filearg[0] = fetchname(bestguess, 0, TRUE); + if (verbosity != SILENT) + say ("Skipping patch...\n"); skip_rest_of_patch = TRUE; return TRUE; } @@ -203,31 +214,30 @@ there_is_another_patch() /* Determine what kind of diff is in the remaining part of the patch file. */ -int +static enum diff intuit_diff_type() { - Reg4 long this_line = 0; - Reg5 long previous_line; - Reg6 long first_command_line = -1; - long fcl_line; - Reg7 bool last_line_was_command = FALSE; - Reg8 bool this_is_a_command = FALSE; - Reg9 bool stars_last_line = FALSE; - Reg10 bool stars_this_line = FALSE; - Reg3 int indent; - Reg1 char *s; - Reg2 char *t; - char *indtmp = Nullch; - char *oldtmp = Nullch; - char *newtmp = Nullch; - char *indname = Nullch; - char *oldname = Nullch; - char *newname = Nullch; - Reg11 int retval; - bool no_filearg = (filearg[0] == Nullch); - + register char *s; + register char *t; + register int indent; + register long this_line = 0; + register long previous_line; + register long first_command_line = -1; + LINENUM fcl_line = 0; /* Pacify `gcc -W'. */ + register bool last_line_was_command = FALSE; + register bool this_is_a_command = FALSE; + register bool stars_last_line = FALSE; + register bool stars_this_line = FALSE; + enum nametype { OLD, NEW, INDEX, NONE } i; + char *name[3]; + struct stat st[3]; + int stat_errno[3]; + int dir_count[3]; + register enum diff retval; + + name[OLD] = name[NEW] = name[INDEX] = 0; ok_to_create_file = FALSE; - Fseek(pfp, p_base, 0); + Fseek (pfp, p_base, SEEK_SET); p_input_line = p_bline - 1; for (;;) { previous_line = this_line; @@ -235,9 +245,8 @@ intuit_diff_type() stars_last_line = stars_this_line; this_line = ftell(pfp); indent = 0; - p_input_line++; - if (fgets(buf, sizeof buf, pfp) == Nullch) { - if (first_command_line >= 0L) { + if (! pget_line (0)) { + if (first_command_line >= 0) { /* nothing but deletes!? */ p_start = first_command_line; p_sline = fcl_line; @@ -247,44 +256,50 @@ intuit_diff_type() else { p_start = this_line; p_sline = p_input_line; - retval = 0; + retval = NO_DIFF; goto scan_exit; } } for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) { if (*s == '\t') - indent += 8 - (indent % 8); + indent = (indent + 8) & ~7; else indent++; } - for (t=s; isdigit(*t) || *t == ','; t++) ; - this_is_a_command = (isdigit(*s) && + for (t = s; ISDIGIT (*t) || *t == ','; t++) + continue; + this_is_a_command = (ISDIGIT (*s) && (*t == 'd' || *t == 'c' || *t == 'a') ); - if (first_command_line < 0L && this_is_a_command) { + if (first_command_line < 0 && this_is_a_command) { first_command_line = this_line; fcl_line = p_input_line; p_indent = indent; /* assume this for now */ } if (!stars_last_line && strnEQ(s, "*** ", 4)) - oldtmp = savestr(s+4); + name[OLD] = fetchname (s+4, strippath); else if (strnEQ(s, "--- ", 4)) - newtmp = savestr(s+4); + name[NEW] = fetchname (s+4, strippath); else if (strnEQ(s, "+++ ", 4)) - oldtmp = savestr(s+4); /* pretend it is the old name */ + name[OLD] = fetchname (s+4, strippath); /* Swap with NEW below. */ else if (strnEQ(s, "Index:", 6)) - indtmp = savestr(s+6); + name[INDEX] = fetchname (s+6, strippath); else if (strnEQ(s, "Prereq:", 7)) { - for (t=s+7; isspace(*t); t++) ; - revision = savestr(t); - for (t=revision; *t && !isspace(*t); t++) ; - *t = '\0'; - if (!*revision) { - free(revision); - revision = Nullch; + for (t = s + 7; ISSPACE ((unsigned char) *t); t++) + continue; + revision = t; + for (t = revision; *t && !ISSPACE ((unsigned char) *t); t++) + continue; + if (t == revision) + revision = 0; + else { + char oldc = *t; + *t = '\0'; + revision = savestr (revision); + *t = oldc; } } - if ((!diff_type || diff_type == ED_DIFF) && - first_command_line >= 0L && + if ((diff_type == NO_DIFF || diff_type == ED_DIFF) && + first_command_line >= 0 && strEQ(s, ".\n") ) { p_indent = indent; p_start = first_command_line; @@ -292,19 +307,34 @@ intuit_diff_type() retval = ED_DIFF; goto scan_exit; } - if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) { - if (!atol(s+3)) + if ((diff_type == NO_DIFF || diff_type == UNI_DIFF) + && strnEQ(s, "@@ -", 4)) { + s += 3; + if (reverse) + { + while (*s != ' ' && *s != '\n') + s++; + while (*s == ' ') + s++; + } + if (!atol(s)) ok_to_create_file = TRUE; p_indent = indent; p_start = this_line; p_sline = p_input_line; retval = UNI_DIFF; + t = name[OLD]; + name[OLD] = name[NEW]; + name[NEW] = t; goto scan_exit; } stars_this_line = strnEQ(s, "********", 8); - if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && - strnEQ(s, "*** ", 4)) { - if (!atol(s+4)) + if ((diff_type == NO_DIFF + || diff_type == CONTEXT_DIFF + || diff_type == NEW_CONTEXT_DIFF) + && stars_last_line && strnEQ (s, "*** ", 4)) { + if (! reverse) + if (! atol (s + 4)) ok_to_create_file = TRUE; /* if this is a new context diff the character just before */ /* the newline is a '*'. */ @@ -314,9 +344,22 @@ intuit_diff_type() p_start = previous_line; p_sline = p_input_line - 1; retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF); + if (reverse) + { + /* If the patch is a reversed context diff, + scan the entire first hunk to see whether + it's OK to create the file. */ + long saved_p_base = p_base; + LINENUM saved_p_bline = p_bline; + p_input_line = p_sline; + Fseek (pfp, previous_line, SEEK_SET); + if (another_hunk (retval) && ! p_ptrn_lines && p_first == 1) + ok_to_create_file = TRUE; + next_intuit_at (saved_p_base, saved_p_bline); + } goto scan_exit; } - if ((!diff_type || diff_type == NORMAL_DIFF) && + if ((diff_type == NO_DIFF || diff_type == NORMAL_DIFF) && last_line_was_command && (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) { p_start = previous_line; @@ -326,72 +369,68 @@ intuit_diff_type() goto scan_exit; } } + scan_exit: - if (no_filearg) { - if (indtmp != Nullch) - indname = fetchname(indtmp, strippath, ok_to_create_file); - if (oldtmp != Nullch) - oldname = fetchname(oldtmp, strippath, ok_to_create_file); - if (newtmp != Nullch) - newname = fetchname(newtmp, strippath, ok_to_create_file); - if (oldname && newname) { - if (strlen(oldname) < strlen(newname)) - filearg[0] = savestr(oldname); - else - filearg[0] = savestr(newname); - } - else if (oldname) - filearg[0] = savestr(oldname); - else if (newname) - filearg[0] = savestr(newname); - else if (indname) - filearg[0] = savestr(indname); - } - if (bestguess) { - free(bestguess); - bestguess = Nullch; - } - if (filearg[0] != Nullch) - bestguess = savestr(filearg[0]); - else if (indtmp != Nullch) - bestguess = fetchname(indtmp, strippath, TRUE); - else { - if (oldtmp != Nullch) - oldname = fetchname(oldtmp, strippath, TRUE); - if (newtmp != Nullch) - newname = fetchname(newtmp, strippath, TRUE); - if (oldname && newname) { - if (strlen(oldname) < strlen(newname)) - bestguess = savestr(oldname); - else - bestguess = savestr(newname); + + /* Use algorithm specified by POSIX 1003.2b/D11 to deduce `inname', + the name of the file to patch; except that if the patch syntactically + can create a file, before asking the user for a file name + redo the earlier steps in sequence, this time ignoring the + nonexistence of a file if the path length of the existing directory + in the file name is a maximum among all the candidate file names. */ + + for (i = OLD; i <= INDEX; i++) + if (!inname && name[i]) + { + if (stat (name[i], &st[i]) != 0) + stat_errno[i] = errno; + else + { + stat_errno[i] = 0; + break; + } } - else if (oldname) - bestguess = savestr(oldname); - else if (newname) - bestguess = savestr(newname); - } - if (indtmp != Nullch) - free(indtmp); - if (oldtmp != Nullch) - free(oldtmp); - if (newtmp != Nullch) - free(newtmp); - if (indname != Nullch) - free(indname); - if (oldname != Nullch) - free(oldname); - if (newname != Nullch) - free(newname); + + if (i == NONE && !inname && ok_to_create_file) + { + int dir_count_max = 0; + + for (i = OLD; i <= INDEX; i++) + if (name[i]) + { + dir_count[i] = countdirs (name[i]); + if (dir_count_max < dir_count[i]) + dir_count_max = dir_count[i]; + } + + for (i = OLD; i <= INDEX; i++) + if (name[i] && dir_count[i] == dir_count_max) + break; + } + + if (i == NONE) + inerrno = -1; + else + { + inname = name[i]; + name[i] = 0; + inerrno = stat_errno[i]; + instat = st[i]; + } + + for (i = OLD; i <= INDEX; i++) + if (name[i]) + free (name[i]); + return retval; } /* Remember where this patch ends so we know where to start up again. */ -void +static void next_intuit_at(file_pos,file_line) long file_pos; -long file_line; +LINENUM file_line; { p_base = file_pos; p_bline = file_line; @@ -399,26 +438,36 @@ long file_line; /* Basically a verbose fseek() to the actual diff listing. */ -void +static void skip_to(file_pos,file_line) long file_pos; -long file_line; +LINENUM file_line; { - char *ret; + register FILE *i = pfp; + register FILE *o = stdout; + register int c; assert(p_base <= file_pos); - if (verbose && p_base < file_pos) { - Fseek(pfp, p_base, 0); - say1("The text leading up to this was:\n--------------------------\n"); - while (ftell(pfp) < file_pos) { - ret = fgets(buf, sizeof buf, pfp); - assert(ret != Nullch); - say2("|%s", buf); - } - say1("--------------------------\n"); + 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 (ftell (i) < file_pos) + { + putc ('|', o); + do + { + if ((c = getc (i)) == EOF) + read_fatal (); + putc (c, o); + } + while (c != '\n'); + } + + say ("--------------------------\n"); } else - Fseek(pfp, file_pos, 0); + Fseek (i, file_pos, SEEK_SET); p_input_line = file_line - 1; } @@ -426,18 +475,20 @@ long file_line; static void malformed () { - fatal3("malformed patch at line %ld: %s", p_input_line, buf); + fatal ("malformed patch at line %ld: %s", p_input_line, buf); /* about as informative as "Syntax error" in C */ } -/* True if there is more of the current diff listing to process. */ +/* 1 if there is more of the current diff listing to process; + 0 if not; -1 if ran out of memory. */ -bool -another_hunk() +int +another_hunk (difftype) + enum diff difftype; { - Reg1 char *s; - Reg8 char *ret; - Reg2 int context = 0; + register char *s; + register LINENUM context = 0; + register size_t chars_read; while (p_end >= 0) { if (p_end == p_efake) @@ -450,54 +501,60 @@ another_hunk() p_efake = -1; p_max = hunkmax; /* gets reduced when --- found */ - if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) { + if (difftype == CONTEXT_DIFF || difftype == NEW_CONTEXT_DIFF) { long line_beginning = ftell(pfp); /* file pos of the current line */ LINENUM repl_beginning = 0; /* index of --- line */ - Reg4 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */ - Reg5 LINENUM fillsrc; /* index of first line to copy */ - Reg6 LINENUM filldst; /* index of first missing line */ + register LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */ + register LINENUM fillsrc; /* index of first line to copy */ + register LINENUM filldst; /* index of first missing line */ bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */ - Reg9 bool repl_could_be_missing = TRUE; - /* no + or ! lines in this hunk */ + bool some_context = FALSE; /* (perhaps internal) context seen */ + register bool repl_could_be_missing = TRUE; bool repl_missing = FALSE; /* we are now backtracking */ long repl_backtrack_position = 0; /* file pos of first repl line */ LINENUM repl_patch_line; /* input line number for same */ - Reg7 LINENUM ptrn_copiable = 0; + LINENUM repl_context; /* context for same */ + register LINENUM ptrn_copiable = 0; /* # of copiable lines in ptrn */ - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch || strnNE(buf, "********", 8)) { + /* Pacify `gcc -Wall'. */ + 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 FALSE; + return chars_read == (size_t) -1 ? -1 : 0; } - p_context = 100; + p_prefix_context = -1; p_hunk_beg = p_input_line + 1; while (p_end < p_max) { - line_beginning = ftell(pfp); - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch) { - if (p_max - p_end < 4) - Strcpy(buf, " \n"); /* assume blank lines got chopped */ - else { - if (repl_beginning && repl_could_be_missing) { - repl_missing = TRUE; - goto hunk_done; - } - fatal1("unexpected end of file in patch\n"); + 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"); /* assume blank lines got chopped */ + chars_read = 3; + } else { + fatal ("unexpected end of file in patch"); } } p_end++; + if (p_end == hunkmax) + fatal ("unterminated hunk starting at line %ld; giving up at line %ld: %s", + pch_hunk_beg (), p_input_line, buf); assert(p_end < hunkmax); p_Char[p_end] = *buf; -#ifdef zilog - p_line[(short)p_end] = Nullch; -#else - p_line[p_end] = Nullch; -#endif + p_len[p_end] = 0; + p_line[p_end] = 0; switch (*buf) { case '*': if (strnEQ(buf, "********", 8)) { @@ -506,7 +563,7 @@ another_hunk() goto hunk_done; } else - fatal2("unexpected end of hunk at line %ld\n", + fatal ("unexpected end of hunk at line %ld", p_input_line); } if (p_end != 0) { @@ -514,23 +571,27 @@ another_hunk() repl_missing = TRUE; goto hunk_done; } - fatal3("unexpected *** at line %ld: %s", p_input_line, buf); + fatal ("unexpected `***' at line %ld: %s", + p_input_line, buf); } context = 0; - p_line[p_end] = savestr(buf); - if (out_of_mem) { + p_len[p_end] = strlen (buf); + if (! (p_line[p_end] = savestr (buf))) { p_end--; - return FALSE; + return -1; } - for (s=buf; *s && !isdigit(*s); s++) ; + for (s = buf; *s && !ISDIGIT (*s); s++) + continue; if (!*s) malformed (); if (strnEQ(s,"0,0",3)) - strcpy(s,s+2); + remove_prefix (s, 2); p_first = (LINENUM) atol(s); - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; if (*s == ',') { - for (; *s && !isdigit(*s); s++) ; + while (*s && !ISDIGIT (*s)) + s++; if (!*s) malformed (); p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1; @@ -543,11 +604,14 @@ another_hunk() } p_max = p_ptrn_lines + 6; /* we need this much at least */ while (p_max >= hunkmax) - grow_hunkmax(); + if (! grow_hunkmax ()) + return -1; p_max = hunkmax; break; case '-': if (buf[1] == '-') { + if (p_prefix_context == -1) + p_prefix_context = context; if (repl_beginning || (p_end != p_ptrn_lines + 1 + (p_Char[p_end-1] == '\n'))) { @@ -565,13 +629,13 @@ another_hunk() repl_missing = TRUE; goto hunk_done; } - fatal3( -"duplicate \"---\" at line %ld--check line numbers at line %ld\n", + fatal ( +"duplicate \"---\" at line %ld--check line numbers at line %ld", p_input_line, p_hunk_beg + repl_beginning); } else { - fatal4( -"%s \"---\" at line %ld--check line numbers at line %ld\n", + fatal ( +"%s \"---\" at line %ld--check line numbers at line %ld", (p_end <= p_ptrn_lines ? "Premature" : "Overdue" ), @@ -582,21 +646,27 @@ another_hunk() repl_beginning = p_end; repl_backtrack_position = ftell(pfp); repl_patch_line = p_input_line; - p_line[p_end] = savestr(buf); - if (out_of_mem) { + repl_context = context; + p_len[p_end] = strlen (buf); + if (! (p_line[p_end] = savestr (buf))) { p_end--; - return FALSE; + return -1; } p_Char[p_end] = '='; - for (s=buf; *s && !isdigit(*s); s++) ; + for (s = buf; *s && !ISDIGIT (*s); s++) + continue; if (!*s) malformed (); p_newfirst = (LINENUM) atol(s); - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; if (*s == ',') { - for (; *s && !isdigit(*s); s++) ; - if (!*s) - malformed (); + do + { + if (!*++s) + malformed (); + } + while (!ISDIGIT (*s)); p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1; } else if (p_newfirst) @@ -606,51 +676,70 @@ another_hunk() p_newfirst = 1; } p_max = p_repl_lines + p_end; - if (p_max > MAXHUNKSIZE) - fatal4("hunk too large (%ld lines) at line %ld: %s", - p_max, p_input_line, buf); while (p_max >= hunkmax) - grow_hunkmax(); + if (! grow_hunkmax ()) + return -1; if (p_repl_lines != ptrn_copiable - && (p_context != 0 || p_repl_lines != 1)) + && (p_prefix_context != 0 + || context != 0 + || p_repl_lines != 1)) repl_could_be_missing = FALSE; + context = 0; break; } goto change_line; case '+': case '!': repl_could_be_missing = FALSE; change_line: - if (buf[1] == '\n' && canonicalize) - strcpy(buf+1," \n"); - if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' && - repl_beginning && repl_could_be_missing) { + 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 (context >= 0) { - if (context < p_context) - p_context = context; - context = -1000; - } - p_line[p_end] = savestr(buf+2); - if (out_of_mem) { + if (p_prefix_context == -1) + p_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 FALSE; + return -1; } + context = 0; break; - case '\t': case '\n': /* assume the 2 spaces got eaten */ + case '\t': case '\n': /* assume spaces got eaten */ + s = buf; + if (*buf == '\t') { + s++; + chars_read--; + } if (repl_beginning && repl_could_be_missing && - (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) { + (!ptrn_spaces_eaten || difftype == NEW_CONTEXT_DIFF) ) { repl_missing = TRUE; goto hunk_done; } - p_line[p_end] = savestr(buf); - if (out_of_mem) { + 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 FALSE; + return -1; } if (p_end != p_ptrn_lines + 1) { ptrn_spaces_eaten |= (repl_beginning != 0); + some_context = TRUE; context++; if (!repl_beginning) ptrn_copiable++; @@ -658,18 +747,31 @@ another_hunk() } break; case ' ': - if (!isspace(buf[1]) && - repl_beginning && repl_could_be_missing) { + 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) ptrn_copiable++; - p_line[p_end] = savestr(buf+2); - if (out_of_mem) { + 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 FALSE; + return -1; } break; default: @@ -679,38 +781,29 @@ another_hunk() } malformed (); } - /* set up p_len for strncmp() so we don't have to */ - /* assume null termination */ - if (p_line[p_end]) - p_len[p_end] = strlen(p_line[p_end]); - else - p_len[p_end] = 0; } - + hunk_done: if (p_end >=0 && !repl_beginning) - fatal2("no --- found in patch at line %ld\n", pch_hunk_beg()); + fatal ("no --- found in patch at line %ld", pch_hunk_beg ()); if (repl_missing) { - + /* reset state back to just after --- */ 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, 0); - + Fseek (pfp, repl_backtrack_position, SEEK_SET); + /* redundant 'new' context lines were omitted - set */ /* up to fill them in from the old file context */ - if (!p_context && p_repl_lines == 1) { - p_repl_lines = 0; - p_max--; - } fillsrc = 1; filldst = repl_beginning+1; fillcnt = p_repl_lines; p_end = p_max; } - else if (!p_context && fillcnt == 1) { + else if (!some_context && fillcnt == 1) { /* the first hunk was a null hunk with no context */ /* and we were expecting one line -- fix it up. */ while (filldst < p_end) { @@ -728,74 +821,86 @@ another_hunk() p_ptrn_lines = 0; } - if (diff_type == CONTEXT_DIFF && - (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) { - if (verbose) - say4("%s\n%s\n%s\n", + p_suffix_context = context; + + 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 = NEW_CONTEXT_DIFF; + diff_type = difftype = NEW_CONTEXT_DIFF; } - + /* if there were omitted context lines, fill them in now */ if (fillcnt) { p_bfake = filldst; /* remember where not to free() */ p_efake = filldst + fillcnt - 1; while (fillcnt-- > 0) { - while (fillsrc <= p_end && p_Char[fillsrc] != ' ') + while (fillsrc <= p_end && fillsrc != repl_beginning + && p_Char[fillsrc] != ' ') fillsrc++; - if (fillsrc > p_end) - fatal2("replacement text or line numbers mangled in hunk at line %ld\n", + if (p_end < fillsrc || fillsrc == repl_beginning) + fatal ("replacement text or line numbers mangled in hunk at line %ld", 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 && - p_Char[fillsrc] != ' ') + while (fillsrc <= p_end && fillsrc != repl_beginning) + { + if (p_Char[fillsrc] == ' ') + fatal ("replacement text or line numbers mangled in hunk at line %ld", + p_hunk_beg); fillsrc++; -#ifdef DEBUGGING + } if (debug & 64) printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n", fillsrc,filldst,repl_beginning,p_end+1); -#endif assert(fillsrc==p_end+1 || fillsrc==repl_beginning); assert(filldst==p_end+1 || filldst==repl_beginning); } } - else if (diff_type == UNI_DIFF) { + else if (difftype == UNI_DIFF) { long line_beginning = ftell(pfp); /* file pos of the current line */ - Reg4 LINENUM fillsrc; /* index of old lines */ - Reg5 LINENUM filldst; /* index of new lines */ - char ch; - - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch || strnNE(buf, "@@ -", 4)) { + register LINENUM fillsrc; /* index of old lines */ + register LINENUM filldst; /* index of new lines */ + 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 FALSE; + return chars_read == (size_t) -1 ? -1 : 0; } s = buf+4; if (!*s) malformed (); p_first = (LINENUM) atol(s); - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; if (*s == ',') { p_ptrn_lines = (LINENUM) atol(++s); - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; } else p_ptrn_lines = 1; if (*s == ' ') s++; if (*s != '+' || !*++s) malformed (); p_newfirst = (LINENUM) atol(s); - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; if (*s == ',') { p_repl_lines = (LINENUM) atol(++s); - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; } else p_repl_lines = 1; if (*s == ' ') s++; @@ -803,53 +908,56 @@ another_hunk() malformed (); if (!p_ptrn_lines) p_first++; /* do append rather than insert */ + if (!p_repl_lines) + p_newfirst++; p_max = p_ptrn_lines + p_repl_lines + 1; while (p_max >= hunkmax) - grow_hunkmax(); + if (! grow_hunkmax ()) + return -1; fillsrc = 1; filldst = fillsrc + p_ptrn_lines; p_end = filldst + p_repl_lines; - Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1); - p_line[0] = savestr(buf); - if (out_of_mem) { + sprintf (buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1); + p_len[0] = strlen (buf); + if (! (p_line[0] = savestr (buf))) { p_end = -1; - return FALSE; + return -1; } p_Char[0] = '*'; - Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1); - p_line[filldst] = savestr(buf); - if (out_of_mem) { + sprintf (buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1); + p_len[filldst] = strlen (buf); + if (! (p_line[filldst] = savestr (buf))) { p_end = 0; - return FALSE; + return -1; } p_Char[filldst++] = '='; - p_context = 100; - context = 0; + p_prefix_context = -1; p_hunk_beg = p_input_line + 1; while (fillsrc <= p_ptrn_lines || filldst <= p_end) { - line_beginning = ftell(pfp); - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch) { - if (p_max - filldst < 3) - Strcpy(buf, " \n"); /* assume blank lines got chopped */ - else { - fatal1("unexpected end of file in patch\n"); + chars_read = get_line (); + if (!chars_read) { + if (p_max - filldst < 3) { + strcpy (buf, " \n"); /* assume blank lines got chopped */ + chars_read = 2; + } else { + fatal ("unexpected end of file in patch"); } } - if (*buf == '\t' || *buf == '\n') { + if (chars_read == (size_t) -1) + s = 0; + else if (*buf == '\t' || *buf == '\n') { ch = ' '; /* assume the space got eaten */ - s = savestr(buf); + s = savebuf (buf, chars_read); } else { ch = *buf; - s = savestr(buf+1); + s = savebuf (buf+1, --chars_read); } - if (out_of_mem) { + if (!s) { while (--filldst > p_ptrn_lines) free(p_line[filldst]); p_end = fillsrc-1; - return FALSE; + return -1; } switch (ch) { case '-': @@ -858,9 +966,10 @@ another_hunk() p_end = filldst-1; malformed (); } + chars_read -= fillsrc == p_ptrn_lines && incomplete_line (); p_Char[fillsrc] = ch; p_line[fillsrc] = s; - p_len[fillsrc++] = strlen(s); + p_len[fillsrc++] = chars_read; break; case '=': ch = ' '; @@ -874,15 +983,16 @@ another_hunk() malformed (); } context++; + chars_read -= fillsrc == p_ptrn_lines && incomplete_line (); p_Char[fillsrc] = ch; p_line[fillsrc] = s; - p_len[fillsrc++] = strlen(s); - s = savestr(s); - if (out_of_mem) { + 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 FALSE; + return -1; } /* FALL THROUGH */ case '+': @@ -893,39 +1003,42 @@ another_hunk() p_end = fillsrc-1; malformed (); } + chars_read -= filldst == p_end && incomplete_line (); p_Char[filldst] = ch; p_line[filldst] = s; - p_len[filldst++] = strlen(s); + p_len[filldst++] = chars_read; break; default: p_end = filldst; malformed (); } - if (ch != ' ' && context > 0) { - if (context < p_context) - p_context = context; - context = -1000; + if (ch != ' ') { + if (p_prefix_context == -1) + p_prefix_context = context; + context = 0; } }/* while */ + p_suffix_context = context; } else { /* normal diff--fake it up */ char hunk_type; - Reg3 int i; + register int i; LINENUM min, max; long line_beginning = ftell(pfp); - p_context = 0; - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch || !isdigit(*buf)) { + 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 FALSE; + return chars_read == (size_t) -1 ? -1 : 0; } p_first = (LINENUM)atol(buf); - for (s=buf; isdigit(*s); s++) ; + for (s = buf; ISDIGIT (*s); s++) + continue; if (*s == ',') { p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1; - while (isdigit(*s)) s++; + while (ISDIGIT (*s)) + s++; } else p_ptrn_lines = (*s != 'a'); @@ -933,7 +1046,8 @@ another_hunk() if (hunk_type == 'a') p_first++; /* do append rather than insert */ min = (LINENUM)atol(++s); - for (; isdigit(*s); s++) ; + while (ISDIGIT (*s)) + s++; if (*s == ',') max = (LINENUM)atol(++s); else @@ -941,75 +1055,87 @@ another_hunk() if (hunk_type == 'd') min++; p_end = p_ptrn_lines + 1 + max - min + 1; - if (p_end > MAXHUNKSIZE) - fatal4("hunk too large (%ld lines) at line %ld: %s", - p_end, p_input_line, buf); while (p_end >= hunkmax) - grow_hunkmax(); + if (! grow_hunkmax ()) + { + p_end = -1; + return -1; + } p_newfirst = min; p_repl_lines = max - min + 1; - Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1); - p_line[0] = savestr(buf); - if (out_of_mem) { + sprintf (buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1); + p_len[0] = strlen (buf); + if (! (p_line[0] = savestr (buf))) { p_end = -1; - return FALSE; + return -1; } p_Char[0] = '*'; for (i=1; i<=p_ptrn_lines; i++) { - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch) - fatal2("unexpected end of file in patch at line %ld\n", + 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 %ld", p_input_line); - if (*buf != '<') - fatal2("< expected at line %ld of patch\n", p_input_line); - p_line[i] = savestr(buf+2); - if (out_of_mem) { + if (buf[0] != '<' || (buf[1] != ' ' && buf[1] != '\t')) + fatal ("`<' expected at line %ld of patch", 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 FALSE; + return -1; } - p_len[i] = strlen(p_line[i]); p_Char[i] = '-'; } if (hunk_type == 'c') { - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch) - fatal2("unexpected end of file in patch at line %ld\n", + 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 %ld", p_input_line); if (*buf != '-') - fatal2("--- expected at line %ld of patch\n", p_input_line); + fatal ("`---' expected at line %ld of patch", p_input_line); } - Sprintf(buf, "--- %ld,%ld\n", min, max); - p_line[i] = savestr(buf); - if (out_of_mem) { + sprintf (buf, "--- %ld,%ld\n", min, max); + p_len[i] = strlen (buf); + if (! (p_line[i] = savestr (buf))) { p_end = i-1; - return FALSE; + return -1; } p_Char[i] = '='; for (i++; i<=p_end; i++) { - ret = pgets(buf, sizeof buf, pfp); - p_input_line++; - if (ret == Nullch) - fatal2("unexpected end of file in patch at line %ld\n", + 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 %ld", p_input_line); - if (*buf != '>') - fatal2("> expected at line %ld of patch\n", p_input_line); - p_line[i] = savestr(buf+2); - if (out_of_mem) { + if (buf[0] != '>' || (buf[1] != ' ' && buf[1] != '\t')) + fatal ("`>' expected at line %ld of patch", 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 FALSE; + return -1; } - p_len[i] = strlen(p_line[i]); p_Char[i] = '+'; } } if (reverse) /* backwards patch? */ if (!pch_swap()) - say1("Not enough memory to swap next hunk!\n"); -#ifdef DEBUGGING + say ("Not enough memory to swap next hunk!\n"); if (debug & 2) { - int i; + LINENUM i; char special; for (i=0; i <= p_end; i++) { @@ -1017,40 +1143,114 @@ another_hunk() special = '^'; else special = ' '; - fprintf(stderr, "%3d %c %c %s", i, p_Char[i], special, p_line[i]); - Fflush(stderr); + fprintf (stderr, "%3ld %c %c ", i, p_Char[i], special); + pch_write_line (i, stderr); + fflush (stderr); } } -#endif if (p_end+1 < hunkmax) /* paranoia reigns supreme... */ p_Char[p_end+1] = '^'; /* add a stopper for apply_hunk */ - return TRUE; + return 1; +} + +static size_t +get_line () +{ + return pget_line (p_indent); } -/* Input a line from the patch file, worrying about indentation. */ +/* Input a line from the patch file, worrying about indentation. + Strip up to INDENT characters' worth of leading indentation. + Ignore any partial lines at end of input, but warn about them. + Succeed if a line was read; it is terminated by "\n\0" for convenience. + Return the number of characters read, including '\n' but not '\0'. + Return -1 if we ran out of memory. */ -char * -pgets(bf,sz,fp) -char *bf; -int sz; -FILE *fp; +static size_t +pget_line (indent) + int indent; { - char *ret = fgets(bf, sz, fp); - Reg1 char *s; - Reg2 int indent = 0; + register FILE *fp = pfp; + register int c; + register int i = 0; + register char *b; + register size_t s; + + 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; + } - if (p_indent && ret != Nullch) { - for (s=buf; - indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) { - if (*s == '\t') - indent += 8 - (indent % 7); - else - indent++; + i = 0; + b = buf; + 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; } - if (buf != s) - Strcpy(buf, s); + b[i++] = c; + if (c == '\n') + break; + c = getc (fp); + if (c == EOF) + { + if (ferror (fp)) + read_fatal (); + say ("patch unexpectedly ends in middle of line\n"); + return 0; + } + } + + b[i] = '\0'; + p_input_line++; + return i; +} + +static bool +incomplete_line () +{ + register FILE *fp = pfp; + register int c; + register long line_beginning = ftell (fp); + + if (getc (fp) == '\\') + { + while ((c = getc (fp)) != '\n' && c != EOF) + continue; + return TRUE; + } + else + { + /* We don't trust ungetc. */ + Fseek (pfp, line_beginning, SEEK_SET); + return FALSE; } - return ret; } /* Reverse the old and new portions of the current hunk. */ @@ -1059,37 +1259,35 @@ bool pch_swap() { char **tp_line; /* the text of the hunk */ - short *tp_len; /* length of each line */ + size_t *tp_len; /* length of each line */ char *tp_char; /* +, -, and ! */ - Reg1 LINENUM i; - Reg2 LINENUM n; + register LINENUM i; + register LINENUM n; bool blankline = FALSE; - Reg3 char *s; + register char *s; i = p_first; p_first = p_newfirst; p_newfirst = i; - + /* make a scratch copy */ tp_line = p_line; tp_len = p_len; tp_char = p_Char; - p_line = Null(char**); /* force set_hunkmax to allocate again */ - p_len = Null(short*); - p_Char = Nullch; + p_line = 0; /* force set_hunkmax to allocate again */ + p_len = 0; + p_Char = 0; set_hunkmax(); - if (p_line == Null(char**) || p_len == Null(short*) || p_Char == Nullch) { -#ifndef lint - if (p_line == Null(char**)) - free((char*)p_line); + if (!p_line || !p_len || !p_Char) { + if (p_line) + free (p_line); p_line = tp_line; - if (p_len == Null(short*)) - free((char*)p_len); + if (p_len) + free (p_len); p_len = tp_len; -#endif - if (p_Char == Nullch) - free((char*)p_Char); + if (p_Char) + free (p_Char); p_Char = tp_char; return FALSE; /* not enough memory to swap hunk! */ } @@ -1147,14 +1345,12 @@ pch_swap() i = p_ptrn_lines; p_ptrn_lines = p_repl_lines; p_repl_lines = i; -#ifndef lint - if (tp_line == Null(char**)) - free((char*)tp_line); - if (tp_len == Null(short*)) - free((char*)tp_len); -#endif - if (tp_char == Nullch) - free((char*)tp_char); + if (tp_line) + free (tp_line); + if (tp_len) + free (tp_len); + if (tp_char) + free (tp_char); return TRUE; } @@ -1201,14 +1397,22 @@ pch_end() /* Return the number of context lines before the first changed line. */ LINENUM -pch_context() +pch_prefix_context () { - return p_context; + return p_prefix_context; +} + +/* Return the number of context lines after the last changed line. */ + +LINENUM +pch_suffix_context () +{ + return p_suffix_context; } /* Return the length of a particular patch line. */ -short +size_t pch_line_len(line) LINENUM line; { @@ -1233,6 +1437,19 @@ LINENUM line; return p_line[line]; } +/* Output a patch line. */ + +bool +pch_write_line (line, file) + 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; +} + /* Return where in the patch file this hunk began, for error messages. */ LINENUM @@ -1244,41 +1461,46 @@ pch_hunk_beg() /* Apply an ed script by feeding ed itself. */ void -do_ed_script() +do_ed_script (ofp) + FILE *ofp; { - Reg1 char *t; - Reg2 long beginning_of_this_line; - Reg3 bool this_line_is_command = FALSE; - Reg4 FILE *pipefp; + static char const ed_program[] = ED_PROGRAM; + + register char *t; + register long beginning_of_this_line; + register bool this_line_is_command = FALSE; + register FILE *pipefp = 0; + register size_t chars_read; if (!skip_rest_of_patch) { - Unlink(TMPOUTNAME); - copy_file(filearg[0], TMPOUTNAME); - if (verbose) - Sprintf(buf, "/bin/ed %s", TMPOUTNAME); - else - Sprintf(buf, "/bin/ed - %s", TMPOUTNAME); + copy_file (inname, TMPOUTNAME); + sprintf (buf, "%s %s%s", ed_program, verbosity == VERBOSE ? "" : "- ", + TMPOUTNAME); pipefp = popen(buf, "w"); + if (!pipefp) + pfatal (buf); } for (;;) { beginning_of_this_line = ftell(pfp); - if (pgets(buf, sizeof buf, pfp) == Nullch) { + chars_read = get_line (); + if (! chars_read) { next_intuit_at(beginning_of_this_line,p_input_line); break; } - p_input_line++; - for (t=buf; isdigit(*t) || *t == ','; t++) ; - this_line_is_command = (isdigit(*buf) && - (*t == 'd' || *t == 'c' || *t == 'a') ); + 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 (!skip_rest_of_patch) - fputs(buf, pipefp); - if (*t != 'd') { - while (pgets(buf, sizeof buf, pfp) != Nullch) { - p_input_line++; - if (!skip_rest_of_patch) - fputs(buf, pipefp); - if (strEQ(buf, ".\n")) + 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; } } @@ -1288,18 +1510,24 @@ do_ed_script() break; } } - if (skip_rest_of_patch) - return; - fprintf(pipefp, "w\n"); - fprintf(pipefp, "q\n"); - Fflush(pipefp); - Pclose(pipefp); - ignore_signals(); - if (move_file(TMPOUTNAME, outname) < 0) { - toutkeep = TRUE; - chmod(TMPOUTNAME, filemode); - } - else - chmod(outname, filemode); - set_signals(1); + 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, "r"); + int c; + if (!ifp) + pfatal (TMPOUTNAME); + while ((c = getc (ifp)) != EOF) + if (putc (c, ofp) == EOF) + write_fatal (); + if (ferror (ifp) || fclose (ifp) != 0) + read_fatal (); + } } |