Home | History | Annotate | Line # | Download | only in patch
pch.c revision 1.1.1.1
      1  1.1.1.1  tls /* $Header: /tank/opengrok/rsync2/NetBSD/src/usr.bin/patch/pch.c,v 1.1.1.1 1997/01/09 14:47:40 tls Exp $
      2      1.1  cgd  *
      3      1.1  cgd  * $Log: pch.c,v $
      4      1.1  cgd  * Revision 1.1.1.1  1997/01/09 14:47:40  tls
      5      1.1  cgd  * Import from 4.4BSD-Lite2
      6      1.1  cgd  *
      7      1.1  cgd  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
      8      1.1  cgd  * pch_swap didn't swap p_bfake and p_efake.
      9      1.1  cgd  *
     10      1.1  cgd  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
     11      1.1  cgd  * Improved responses to mangled patches.
     12      1.1  cgd  *
     13      1.1  cgd  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
     14      1.1  cgd  * New-style context diffs caused double call to free().
     15      1.1  cgd  *
     16      1.1  cgd  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
     17      1.1  cgd  * Fixed problem where a long pattern wouldn't grow the hunk.
     18      1.1  cgd  * Also restored p_input_line when backtracking so error messages are right.
     19      1.1  cgd  *
     20      1.1  cgd  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
     21      1.1  cgd  * New-style delete triggers spurious assertion error.
     22      1.1  cgd  *
     23      1.1  cgd  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
     24      1.1  cgd  * Could falsely report new-style context diff.
     25      1.1  cgd  *
     26      1.1  cgd  * Revision 2.0  86/09/17  15:39:37  lwall
     27      1.1  cgd  * Baseline for netwide release.
     28      1.1  cgd  *
     29      1.1  cgd  */
     30      1.1  cgd 
     31      1.1  cgd #include "EXTERN.h"
     32      1.1  cgd #include "common.h"
     33      1.1  cgd #include "util.h"
     34      1.1  cgd #include "INTERN.h"
     35      1.1  cgd #include "pch.h"
     36      1.1  cgd 
     37      1.1  cgd /* Patch (diff listing) abstract type. */
     38      1.1  cgd 
     39      1.1  cgd static long p_filesize;			/* size of the patch file */
     40      1.1  cgd static LINENUM p_first;			/* 1st line number */
     41      1.1  cgd static LINENUM p_newfirst;		/* 1st line number of replacement */
     42      1.1  cgd static LINENUM p_ptrn_lines;		/* # lines in pattern */
     43      1.1  cgd static LINENUM p_repl_lines;		/* # lines in replacement text */
     44      1.1  cgd static LINENUM p_end = -1;		/* last line in hunk */
     45      1.1  cgd static LINENUM p_max;			/* max allowed value of p_end */
     46      1.1  cgd static LINENUM p_context = 3;		/* # of context lines */
     47      1.1  cgd static LINENUM p_input_line = 0;	/* current line # from patch file */
     48      1.1  cgd static char **p_line = Null(char**);	/* the text of the hunk */
     49      1.1  cgd static short *p_len = Null(short*);	/* length of each line */
     50      1.1  cgd static char *p_char = Nullch;		/* +, -, and ! */
     51      1.1  cgd static int hunkmax = INITHUNKMAX;	/* size of above arrays to begin with */
     52      1.1  cgd static int p_indent;			/* indent to patch */
     53      1.1  cgd static LINENUM p_base;			/* where to intuit this time */
     54      1.1  cgd static LINENUM p_bline;			/* line # of p_base */
     55      1.1  cgd static LINENUM p_start;			/* where intuit found a patch */
     56      1.1  cgd static LINENUM p_sline;			/* and the line number for it */
     57      1.1  cgd static LINENUM p_hunk_beg;		/* line number of current hunk */
     58      1.1  cgd static LINENUM p_efake = -1;		/* end of faked up lines--don't free */
     59      1.1  cgd static LINENUM p_bfake = -1;		/* beg of faked up lines */
     60      1.1  cgd 
     61      1.1  cgd /* Prepare to look for the next patch in the patch file. */
     62      1.1  cgd 
     63      1.1  cgd void
     64      1.1  cgd re_patch()
     65      1.1  cgd {
     66      1.1  cgd     p_first = Nulline;
     67      1.1  cgd     p_newfirst = Nulline;
     68      1.1  cgd     p_ptrn_lines = Nulline;
     69      1.1  cgd     p_repl_lines = Nulline;
     70      1.1  cgd     p_end = (LINENUM)-1;
     71      1.1  cgd     p_max = Nulline;
     72      1.1  cgd     p_indent = 0;
     73      1.1  cgd }
     74      1.1  cgd 
     75      1.1  cgd /* Open the patch file at the beginning of time. */
     76      1.1  cgd 
     77      1.1  cgd void
     78      1.1  cgd open_patch_file(filename)
     79      1.1  cgd char *filename;
     80      1.1  cgd {
     81  1.1.1.1  tls     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
     82      1.1  cgd 	pfp = fopen(TMPPATNAME, "w");
     83      1.1  cgd 	if (pfp == Nullfp)
     84      1.1  cgd 	    fatal2("patch: can't create %s.\n", TMPPATNAME);
     85      1.1  cgd 	while (fgets(buf, sizeof buf, stdin) != Nullch)
     86      1.1  cgd 	    fputs(buf, pfp);
     87      1.1  cgd 	Fclose(pfp);
     88      1.1  cgd 	filename = TMPPATNAME;
     89  1.1.1.1  tls     }
     90      1.1  cgd     pfp = fopen(filename, "r");
     91      1.1  cgd     if (pfp == Nullfp)
     92      1.1  cgd 	fatal2("patch file %s not found\n", filename);
     93      1.1  cgd     Fstat(fileno(pfp), &filestat);
     94      1.1  cgd     p_filesize = filestat.st_size;
     95      1.1  cgd     next_intuit_at(0L,1L);			/* start at the beginning */
     96      1.1  cgd     set_hunkmax();
     97      1.1  cgd }
     98      1.1  cgd 
     99      1.1  cgd /* Make sure our dynamically realloced tables are malloced to begin with. */
    100      1.1  cgd 
    101      1.1  cgd void
    102      1.1  cgd set_hunkmax()
    103      1.1  cgd {
    104      1.1  cgd #ifndef lint
    105      1.1  cgd     if (p_line == Null(char**))
    106      1.1  cgd 	p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
    107      1.1  cgd     if (p_len == Null(short*))
    108      1.1  cgd 	p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
    109      1.1  cgd #endif
    110      1.1  cgd     if (p_char == Nullch)
    111      1.1  cgd 	p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
    112      1.1  cgd }
    113      1.1  cgd 
    114      1.1  cgd /* Enlarge the arrays containing the current hunk of patch. */
    115      1.1  cgd 
    116      1.1  cgd void
    117      1.1  cgd grow_hunkmax()
    118      1.1  cgd {
    119      1.1  cgd     hunkmax *= 2;
    120      1.1  cgd     /*
    121      1.1  cgd      * Note that on most systems, only the p_line array ever gets fresh memory
    122      1.1  cgd      * since p_len can move into p_line's old space, and p_char can move into
    123      1.1  cgd      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
    124      1.1  cgd      */
    125      1.1  cgd     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
    126      1.1  cgd #ifndef lint
    127      1.1  cgd     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
    128      1.1  cgd     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
    129      1.1  cgd     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
    130      1.1  cgd #endif
    131  1.1.1.1  tls     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
    132      1.1  cgd 	return;
    133      1.1  cgd     if (!using_plan_a)
    134      1.1  cgd 	fatal1("patch: out of memory (grow_hunkmax)\n");
    135      1.1  cgd     out_of_mem = TRUE;		/* whatever is null will be allocated again */
    136      1.1  cgd 				/* from within plan_a(), of all places */
    137      1.1  cgd }
    138      1.1  cgd 
    139      1.1  cgd /* True if the remainder of the patch file contains a diff of some sort. */
    140      1.1  cgd 
    141      1.1  cgd bool
    142      1.1  cgd there_is_another_patch()
    143      1.1  cgd {
    144      1.1  cgd     if (p_base != 0L && p_base >= p_filesize) {
    145      1.1  cgd 	if (verbose)
    146      1.1  cgd 	    say1("done\n");
    147      1.1  cgd 	return FALSE;
    148      1.1  cgd     }
    149      1.1  cgd     if (verbose)
    150      1.1  cgd 	say1("Hmm...");
    151      1.1  cgd     diff_type = intuit_diff_type();
    152      1.1  cgd     if (!diff_type) {
    153      1.1  cgd 	if (p_base != 0L) {
    154      1.1  cgd 	    if (verbose)
    155      1.1  cgd 		say1("  Ignoring the trailing garbage.\ndone\n");
    156      1.1  cgd 	}
    157      1.1  cgd 	else
    158      1.1  cgd 	    say1("  I can't seem to find a patch in there anywhere.\n");
    159      1.1  cgd 	return FALSE;
    160      1.1  cgd     }
    161      1.1  cgd     if (verbose)
    162      1.1  cgd 	say3("  %sooks like %s to me...\n",
    163      1.1  cgd 	    (p_base == 0L ? "L" : "The next patch l"),
    164      1.1  cgd 	    diff_type == CONTEXT_DIFF ? "a context diff" :
    165      1.1  cgd 	    diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
    166      1.1  cgd 	    diff_type == NORMAL_DIFF ? "a normal diff" :
    167      1.1  cgd 	    "an ed script" );
    168      1.1  cgd     if (p_indent && verbose)
    169  1.1.1.1  tls 	say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
    170      1.1  cgd     skip_to(p_start,p_sline);
    171      1.1  cgd     while (filearg[0] == Nullch) {
    172      1.1  cgd 	if (force) {
    173      1.1  cgd 	    say1("No file to patch.  Skipping...\n");
    174      1.1  cgd 	    filearg[0] = savestr(bestguess);
    175      1.1  cgd 	    return TRUE;
    176      1.1  cgd 	}
    177      1.1  cgd 	ask1("File to patch: ");
    178      1.1  cgd 	if (*buf != '\n') {
    179      1.1  cgd 	    if (bestguess)
    180      1.1  cgd 		free(bestguess);
    181      1.1  cgd 	    bestguess = savestr(buf);
    182      1.1  cgd 	    filearg[0] = fetchname(buf, 0, FALSE);
    183      1.1  cgd 	}
    184      1.1  cgd 	if (filearg[0] == Nullch) {
    185      1.1  cgd 	    ask1("No file found--skip this patch? [n] ");
    186      1.1  cgd 	    if (*buf != 'y') {
    187      1.1  cgd 		continue;
    188      1.1  cgd 	    }
    189      1.1  cgd 	    if (verbose)
    190      1.1  cgd 		say1("Skipping patch...\n");
    191      1.1  cgd 	    filearg[0] = fetchname(bestguess, 0, TRUE);
    192      1.1  cgd 	    skip_rest_of_patch = TRUE;
    193      1.1  cgd 	    return TRUE;
    194      1.1  cgd 	}
    195      1.1  cgd     }
    196      1.1  cgd     return TRUE;
    197      1.1  cgd }
    198      1.1  cgd 
    199      1.1  cgd /* Determine what kind of diff is in the remaining part of the patch file. */
    200      1.1  cgd 
    201      1.1  cgd int
    202      1.1  cgd intuit_diff_type()
    203      1.1  cgd {
    204      1.1  cgd     Reg4 long this_line = 0;
    205      1.1  cgd     Reg5 long previous_line;
    206      1.1  cgd     Reg6 long first_command_line = -1;
    207      1.1  cgd     long fcl_line;
    208      1.1  cgd     Reg7 bool last_line_was_command = FALSE;
    209      1.1  cgd     Reg8 bool this_is_a_command = FALSE;
    210      1.1  cgd     Reg9 bool stars_last_line = FALSE;
    211      1.1  cgd     Reg10 bool stars_this_line = FALSE;
    212      1.1  cgd     Reg3 int indent;
    213      1.1  cgd     Reg1 char *s;
    214      1.1  cgd     Reg2 char *t;
    215      1.1  cgd     char *indtmp = Nullch;
    216      1.1  cgd     char *oldtmp = Nullch;
    217      1.1  cgd     char *newtmp = Nullch;
    218      1.1  cgd     char *indname = Nullch;
    219      1.1  cgd     char *oldname = Nullch;
    220      1.1  cgd     char *newname = Nullch;
    221      1.1  cgd     Reg11 int retval;
    222      1.1  cgd     bool no_filearg = (filearg[0] == Nullch);
    223      1.1  cgd 
    224      1.1  cgd     ok_to_create_file = FALSE;
    225      1.1  cgd     Fseek(pfp, p_base, 0);
    226      1.1  cgd     p_input_line = p_bline - 1;
    227      1.1  cgd     for (;;) {
    228      1.1  cgd 	previous_line = this_line;
    229      1.1  cgd 	last_line_was_command = this_is_a_command;
    230      1.1  cgd 	stars_last_line = stars_this_line;
    231      1.1  cgd 	this_line = ftell(pfp);
    232      1.1  cgd 	indent = 0;
    233      1.1  cgd 	p_input_line++;
    234      1.1  cgd 	if (fgets(buf, sizeof buf, pfp) == Nullch) {
    235      1.1  cgd 	    if (first_command_line >= 0L) {
    236      1.1  cgd 					/* nothing but deletes!? */
    237      1.1  cgd 		p_start = first_command_line;
    238      1.1  cgd 		p_sline = fcl_line;
    239      1.1  cgd 		retval = ED_DIFF;
    240      1.1  cgd 		goto scan_exit;
    241      1.1  cgd 	    }
    242      1.1  cgd 	    else {
    243      1.1  cgd 		p_start = this_line;
    244      1.1  cgd 		p_sline = p_input_line;
    245      1.1  cgd 		retval = 0;
    246  1.1.1.1  tls 		goto scan_exit;
    247      1.1  cgd 	    }
    248      1.1  cgd 	}
    249      1.1  cgd 	for (s = buf; *s == ' ' || *s == '\t'; s++) {
    250      1.1  cgd 	    if (*s == '\t')
    251      1.1  cgd 		indent += 8 - (indent % 8);
    252      1.1  cgd 	    else
    253      1.1  cgd 		indent++;
    254      1.1  cgd 	}
    255      1.1  cgd 	for (t=s; isdigit(*t) || *t == ','; t++) ;
    256      1.1  cgd 	this_is_a_command = (isdigit(*s) &&
    257      1.1  cgd 	  (*t == 'd' || *t == 'c' || *t == 'a') );
    258      1.1  cgd 	if (first_command_line < 0L && this_is_a_command) {
    259      1.1  cgd 	    first_command_line = this_line;
    260      1.1  cgd 	    fcl_line = p_input_line;
    261      1.1  cgd 	    p_indent = indent;		/* assume this for now */
    262      1.1  cgd 	}
    263      1.1  cgd 	if (!stars_last_line && strnEQ(s, "*** ", 4))
    264      1.1  cgd 	    oldtmp = savestr(s+4);
    265      1.1  cgd 	else if (strnEQ(s, "--- ", 4))
    266      1.1  cgd 	    newtmp = savestr(s+4);
    267      1.1  cgd 	else if (strnEQ(s, "Index:", 6))
    268      1.1  cgd 	    indtmp = savestr(s+6);
    269      1.1  cgd 	else if (strnEQ(s, "Prereq:", 7)) {
    270      1.1  cgd 	    for (t=s+7; isspace(*t); t++) ;
    271      1.1  cgd 	    revision = savestr(t);
    272      1.1  cgd 	    for (t=revision; *t && !isspace(*t); t++) ;
    273      1.1  cgd 	    *t = '\0';
    274      1.1  cgd 	    if (!*revision) {
    275      1.1  cgd 		free(revision);
    276      1.1  cgd 		revision = Nullch;
    277      1.1  cgd 	    }
    278      1.1  cgd 	}
    279      1.1  cgd 	if ((!diff_type || diff_type == ED_DIFF) &&
    280      1.1  cgd 	  first_command_line >= 0L &&
    281      1.1  cgd 	  strEQ(s, ".\n") ) {
    282      1.1  cgd 	    p_indent = indent;
    283      1.1  cgd 	    p_start = first_command_line;
    284      1.1  cgd 	    p_sline = fcl_line;
    285      1.1  cgd 	    retval = ED_DIFF;
    286      1.1  cgd 	    goto scan_exit;
    287      1.1  cgd 	}
    288      1.1  cgd 	stars_this_line = strnEQ(s, "********", 8);
    289      1.1  cgd 	if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
    290      1.1  cgd 		 strnEQ(s, "*** ", 4)) {
    291      1.1  cgd 	    if (!atol(s+4))
    292      1.1  cgd 		ok_to_create_file = TRUE;
    293      1.1  cgd 	    /* if this is a new context diff the character just before */
    294      1.1  cgd 	    /* the newline is a '*'. */
    295      1.1  cgd 	    while (*s != '\n')
    296      1.1  cgd 		s++;
    297      1.1  cgd 	    p_indent = indent;
    298      1.1  cgd 	    p_start = previous_line;
    299      1.1  cgd 	    p_sline = p_input_line - 1;
    300      1.1  cgd 	    retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
    301      1.1  cgd 	    goto scan_exit;
    302      1.1  cgd 	}
    303      1.1  cgd 	if ((!diff_type || diff_type == NORMAL_DIFF) &&
    304      1.1  cgd 	  last_line_was_command &&
    305      1.1  cgd 	  (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
    306      1.1  cgd 	    p_start = previous_line;
    307      1.1  cgd 	    p_sline = p_input_line - 1;
    308      1.1  cgd 	    p_indent = indent;
    309      1.1  cgd 	    retval = NORMAL_DIFF;
    310      1.1  cgd 	    goto scan_exit;
    311      1.1  cgd 	}
    312      1.1  cgd     }
    313      1.1  cgd   scan_exit:
    314      1.1  cgd     if (no_filearg) {
    315      1.1  cgd 	if (indtmp != Nullch)
    316      1.1  cgd 	    indname = fetchname(indtmp, strippath, ok_to_create_file);
    317      1.1  cgd 	if (oldtmp != Nullch)
    318      1.1  cgd 	    oldname = fetchname(oldtmp, strippath, ok_to_create_file);
    319      1.1  cgd 	if (newtmp != Nullch)
    320      1.1  cgd 	    newname = fetchname(newtmp, strippath, ok_to_create_file);
    321      1.1  cgd 	if (oldname && newname) {
    322      1.1  cgd 	    if (strlen(oldname) < strlen(newname))
    323      1.1  cgd 		filearg[0] = savestr(oldname);
    324      1.1  cgd 	    else
    325      1.1  cgd 		filearg[0] = savestr(newname);
    326      1.1  cgd 	}
    327      1.1  cgd 	else if (oldname)
    328      1.1  cgd 	    filearg[0] = savestr(oldname);
    329      1.1  cgd 	else if (newname)
    330      1.1  cgd 	    filearg[0] = savestr(newname);
    331      1.1  cgd 	else if (indname)
    332      1.1  cgd 	    filearg[0] = savestr(indname);
    333      1.1  cgd     }
    334      1.1  cgd     if (bestguess) {
    335      1.1  cgd 	free(bestguess);
    336      1.1  cgd 	bestguess = Nullch;
    337      1.1  cgd     }
    338      1.1  cgd     if (filearg[0] != Nullch)
    339      1.1  cgd 	bestguess = savestr(filearg[0]);
    340      1.1  cgd     else if (indtmp != Nullch)
    341      1.1  cgd 	bestguess = fetchname(indtmp, strippath, TRUE);
    342      1.1  cgd     else {
    343      1.1  cgd 	if (oldtmp != Nullch)
    344      1.1  cgd 	    oldname = fetchname(oldtmp, strippath, TRUE);
    345      1.1  cgd 	if (newtmp != Nullch)
    346      1.1  cgd 	    newname = fetchname(newtmp, strippath, TRUE);
    347      1.1  cgd 	if (oldname && newname) {
    348      1.1  cgd 	    if (strlen(oldname) < strlen(newname))
    349      1.1  cgd 		bestguess = savestr(oldname);
    350      1.1  cgd 	    else
    351      1.1  cgd 		bestguess = savestr(newname);
    352      1.1  cgd 	}
    353      1.1  cgd 	else if (oldname)
    354      1.1  cgd 	    bestguess = savestr(oldname);
    355      1.1  cgd 	else if (newname)
    356      1.1  cgd 	    bestguess = savestr(newname);
    357      1.1  cgd     }
    358      1.1  cgd     if (indtmp != Nullch)
    359      1.1  cgd 	free(indtmp);
    360      1.1  cgd     if (oldtmp != Nullch)
    361      1.1  cgd 	free(oldtmp);
    362      1.1  cgd     if (newtmp != Nullch)
    363      1.1  cgd 	free(newtmp);
    364      1.1  cgd     if (indname != Nullch)
    365      1.1  cgd 	free(indname);
    366      1.1  cgd     if (oldname != Nullch)
    367      1.1  cgd 	free(oldname);
    368      1.1  cgd     if (newname != Nullch)
    369      1.1  cgd 	free(newname);
    370      1.1  cgd     return retval;
    371      1.1  cgd }
    372      1.1  cgd 
    373      1.1  cgd /* Remember where this patch ends so we know where to start up again. */
    374      1.1  cgd 
    375      1.1  cgd void
    376      1.1  cgd next_intuit_at(file_pos,file_line)
    377      1.1  cgd long file_pos;
    378      1.1  cgd long file_line;
    379      1.1  cgd {
    380      1.1  cgd     p_base = file_pos;
    381      1.1  cgd     p_bline = file_line;
    382      1.1  cgd }
    383      1.1  cgd 
    384      1.1  cgd /* Basically a verbose fseek() to the actual diff listing. */
    385      1.1  cgd 
    386      1.1  cgd void
    387      1.1  cgd skip_to(file_pos,file_line)
    388      1.1  cgd long file_pos;
    389      1.1  cgd long file_line;
    390      1.1  cgd {
    391      1.1  cgd     char *ret;
    392      1.1  cgd 
    393      1.1  cgd     assert(p_base <= file_pos);
    394      1.1  cgd     if (verbose && p_base < file_pos) {
    395      1.1  cgd 	Fseek(pfp, p_base, 0);
    396      1.1  cgd 	say1("The text leading up to this was:\n--------------------------\n");
    397      1.1  cgd 	while (ftell(pfp) < file_pos) {
    398      1.1  cgd 	    ret = fgets(buf, sizeof buf, pfp);
    399      1.1  cgd 	    assert(ret != Nullch);
    400      1.1  cgd 	    say2("|%s", buf);
    401      1.1  cgd 	}
    402      1.1  cgd 	say1("--------------------------\n");
    403      1.1  cgd     }
    404      1.1  cgd     else
    405      1.1  cgd 	Fseek(pfp, file_pos, 0);
    406      1.1  cgd     p_input_line = file_line - 1;
    407      1.1  cgd }
    408      1.1  cgd 
    409      1.1  cgd /* True if there is more of the current diff listing to process. */
    410      1.1  cgd 
    411      1.1  cgd bool
    412      1.1  cgd another_hunk()
    413      1.1  cgd {
    414      1.1  cgd     Reg1 char *s;
    415      1.1  cgd     Reg8 char *ret;
    416      1.1  cgd     Reg2 int context = 0;
    417      1.1  cgd 
    418      1.1  cgd     while (p_end >= 0) {
    419      1.1  cgd 	if (p_end == p_efake)
    420      1.1  cgd 	    p_end = p_bfake;		/* don't free twice */
    421      1.1  cgd 	else
    422      1.1  cgd 	    free(p_line[p_end]);
    423      1.1  cgd 	p_end--;
    424      1.1  cgd     }
    425      1.1  cgd     assert(p_end == -1);
    426      1.1  cgd     p_efake = -1;
    427      1.1  cgd 
    428      1.1  cgd     p_max = hunkmax;			/* gets reduced when --- found */
    429      1.1  cgd     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
    430      1.1  cgd 	long line_beginning = ftell(pfp);
    431      1.1  cgd 					/* file pos of the current line */
    432      1.1  cgd 	LINENUM repl_beginning = 0;	/* index of --- line */
    433      1.1  cgd 	Reg4 LINENUM fillcnt = 0;	/* #lines of missing ptrn or repl */
    434      1.1  cgd 	Reg5 LINENUM fillsrc;		/* index of first line to copy */
    435      1.1  cgd 	Reg6 LINENUM filldst;		/* index of first missing line */
    436      1.1  cgd 	bool ptrn_spaces_eaten = FALSE;	/* ptrn was slightly misformed */
    437      1.1  cgd 	Reg9 bool repl_could_be_missing = TRUE;
    438      1.1  cgd 					/* no + or ! lines in this hunk */
    439      1.1  cgd 	bool repl_missing = FALSE;	/* we are now backtracking */
    440      1.1  cgd 	long repl_backtrack_position = 0;
    441      1.1  cgd 					/* file pos of first repl line */
    442      1.1  cgd 	LINENUM repl_patch_line;	/* input line number for same */
    443      1.1  cgd 	Reg7 LINENUM ptrn_copiable = 0;
    444      1.1  cgd 					/* # of copiable lines in ptrn */
    445      1.1  cgd 
    446      1.1  cgd 	ret = pgets(buf, sizeof buf, pfp);
    447      1.1  cgd 	p_input_line++;
    448      1.1  cgd 	if (ret == Nullch || strnNE(buf, "********", 8)) {
    449      1.1  cgd 	    next_intuit_at(line_beginning,p_input_line);
    450      1.1  cgd 	    return FALSE;
    451      1.1  cgd 	}
    452      1.1  cgd 	p_context = 100;
    453      1.1  cgd 	p_hunk_beg = p_input_line + 1;
    454      1.1  cgd 	while (p_end < p_max) {
    455      1.1  cgd 	    line_beginning = ftell(pfp);
    456      1.1  cgd 	    ret = pgets(buf, sizeof buf, pfp);
    457      1.1  cgd 	    p_input_line++;
    458      1.1  cgd 	    if (ret == Nullch) {
    459      1.1  cgd 		if (p_max - p_end < 4)
    460      1.1  cgd 		    Strcpy(buf, "  \n");  /* assume blank lines got chopped */
    461      1.1  cgd 		else {
    462      1.1  cgd 		    if (repl_beginning && repl_could_be_missing) {
    463  1.1.1.1  tls 			repl_missing = TRUE;
    464      1.1  cgd 			goto hunk_done;
    465      1.1  cgd 		    }
    466      1.1  cgd 		    fatal1("Unexpected end of file in patch.\n");
    467      1.1  cgd 		}
    468      1.1  cgd 	    }
    469      1.1  cgd 	    p_end++;
    470      1.1  cgd 	    assert(p_end < hunkmax);
    471      1.1  cgd 	    p_char[p_end] = *buf;
    472      1.1  cgd 	    p_line[p_end] = Nullch;
    473      1.1  cgd 	    switch (*buf) {
    474      1.1  cgd 	    case '*':
    475      1.1  cgd 		if (strnEQ(buf, "********", 8)) {
    476      1.1  cgd 		    if (repl_beginning && repl_could_be_missing) {
    477      1.1  cgd 			repl_missing = TRUE;
    478  1.1.1.1  tls 			goto hunk_done;
    479      1.1  cgd 		    }
    480      1.1  cgd 		    else
    481      1.1  cgd 			fatal2("Unexpected end of hunk at line %ld.\n",
    482      1.1  cgd 			    p_input_line);
    483      1.1  cgd 		}
    484      1.1  cgd 		if (p_end != 0) {
    485      1.1  cgd 		    if (repl_beginning && repl_could_be_missing) {
    486  1.1.1.1  tls 			repl_missing = TRUE;
    487      1.1  cgd 			goto hunk_done;
    488      1.1  cgd 		    }
    489      1.1  cgd 		    fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
    490      1.1  cgd 		}
    491      1.1  cgd 		context = 0;
    492      1.1  cgd 		p_line[p_end] = savestr(buf);
    493      1.1  cgd 		if (out_of_mem) {
    494      1.1  cgd 		    p_end--;
    495      1.1  cgd 		    return FALSE;
    496  1.1.1.1  tls 		}
    497      1.1  cgd 		for (s=buf; *s && !isdigit(*s); s++) ;
    498      1.1  cgd 		if (!*s)
    499      1.1  cgd 		    goto malformed;
    500      1.1  cgd 		p_first = (LINENUM) atol(s);
    501      1.1  cgd 		while (isdigit(*s)) s++;
    502  1.1.1.1  tls 		if (*s == ',') {
    503      1.1  cgd 		    for (; *s && !isdigit(*s); s++) ;
    504      1.1  cgd 		    if (!*s)
    505      1.1  cgd 			goto malformed;
    506      1.1  cgd 		    p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
    507      1.1  cgd 		}
    508      1.1  cgd 		else if (p_first)
    509      1.1  cgd 		    p_ptrn_lines = 1;
    510      1.1  cgd 		else {
    511      1.1  cgd 		    p_ptrn_lines = 0;
    512      1.1  cgd 		    p_first = 1;
    513      1.1  cgd 		}
    514      1.1  cgd 		p_max = p_ptrn_lines + 6;	/* we need this much at least */
    515      1.1  cgd 		while (p_max >= hunkmax)
    516      1.1  cgd 		    grow_hunkmax();
    517      1.1  cgd 		p_max = hunkmax;
    518      1.1  cgd 		break;
    519      1.1  cgd 	    case '-':
    520      1.1  cgd 		if (buf[1] == '-') {
    521      1.1  cgd 		    if (repl_beginning ||
    522      1.1  cgd 			(p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
    523      1.1  cgd 		    {
    524      1.1  cgd 			if (p_end == 1) {
    525      1.1  cgd 			    /* `old' lines were omitted - set up to fill */
    526      1.1  cgd 			    /* them in from 'new' context lines. */
    527      1.1  cgd 			    p_end = p_ptrn_lines + 1;
    528      1.1  cgd 			    fillsrc = p_end + 1;
    529      1.1  cgd 			    filldst = 1;
    530      1.1  cgd 			    fillcnt = p_ptrn_lines;
    531      1.1  cgd 			}
    532      1.1  cgd 			else {
    533      1.1  cgd 			    if (repl_beginning) {
    534      1.1  cgd 				if (repl_could_be_missing){
    535      1.1  cgd 				    repl_missing = TRUE;
    536  1.1.1.1  tls 				    goto hunk_done;
    537      1.1  cgd 				}
    538      1.1  cgd 				fatal3(
    539      1.1  cgd "Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
    540      1.1  cgd 				    p_input_line, p_hunk_beg + repl_beginning);
    541  1.1.1.1  tls 			    }
    542      1.1  cgd 			    else {
    543      1.1  cgd 				fatal4(
    544      1.1  cgd "%s \"---\" at line %ld--check line numbers at line %ld.\n",
    545      1.1  cgd 				    (p_end <= p_ptrn_lines
    546      1.1  cgd 					? "Premature"
    547      1.1  cgd 					: "Overdue" ),
    548      1.1  cgd 				    p_input_line, p_hunk_beg);
    549      1.1  cgd 			    }
    550      1.1  cgd 			}
    551      1.1  cgd 		    }
    552      1.1  cgd 		    repl_beginning = p_end;
    553      1.1  cgd 		    repl_backtrack_position = ftell(pfp);
    554      1.1  cgd 		    repl_patch_line = p_input_line;
    555      1.1  cgd 		    p_line[p_end] = savestr(buf);
    556      1.1  cgd 		    if (out_of_mem) {
    557      1.1  cgd 			p_end--;
    558      1.1  cgd 			return FALSE;
    559      1.1  cgd 		    }
    560  1.1.1.1  tls 		    p_char[p_end] = '=';
    561      1.1  cgd 		    for (s=buf; *s && !isdigit(*s); s++) ;
    562      1.1  cgd 		    if (!*s)
    563      1.1  cgd 			goto malformed;
    564      1.1  cgd 		    p_newfirst = (LINENUM) atol(s);
    565      1.1  cgd 		    while (isdigit(*s)) s++;
    566  1.1.1.1  tls 		    if (*s == ',') {
    567      1.1  cgd 			for (; *s && !isdigit(*s); s++) ;
    568      1.1  cgd 			if (!*s)
    569      1.1  cgd 			    goto malformed;
    570      1.1  cgd 			p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
    571      1.1  cgd 		    }
    572      1.1  cgd 		    else if (p_newfirst)
    573      1.1  cgd 			p_repl_lines = 1;
    574      1.1  cgd 		    else {
    575      1.1  cgd 			p_repl_lines = 0;
    576      1.1  cgd 			p_newfirst = 1;
    577  1.1.1.1  tls 		    }
    578      1.1  cgd 		    p_max = p_repl_lines + p_end;
    579      1.1  cgd 		    if (p_max > MAXHUNKSIZE)
    580      1.1  cgd 			fatal4("Hunk too large (%ld lines) at line %ld: %s",
    581  1.1.1.1  tls 			      p_max, p_input_line, buf);
    582      1.1  cgd 		    while (p_max >= hunkmax)
    583      1.1  cgd 			grow_hunkmax();
    584      1.1  cgd 		    if (p_repl_lines != ptrn_copiable)
    585      1.1  cgd 			repl_could_be_missing = FALSE;
    586      1.1  cgd 		    break;
    587      1.1  cgd 		}
    588      1.1  cgd 		goto change_line;
    589      1.1  cgd 	    case '+':  case '!':
    590      1.1  cgd 		repl_could_be_missing = FALSE;
    591      1.1  cgd 	      change_line:
    592      1.1  cgd 		if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
    593      1.1  cgd 		  repl_beginning && repl_could_be_missing) {
    594  1.1.1.1  tls 		    repl_missing = TRUE;
    595      1.1  cgd 		    goto hunk_done;
    596      1.1  cgd 		}
    597      1.1  cgd 		if (context > 0) {
    598      1.1  cgd 		    if (context < p_context)
    599      1.1  cgd 			p_context = context;
    600      1.1  cgd 		    context = -1000;
    601      1.1  cgd 		}
    602      1.1  cgd 		p_line[p_end] = savestr(buf+2);
    603      1.1  cgd 		if (out_of_mem) {
    604      1.1  cgd 		    p_end--;
    605      1.1  cgd 		    return FALSE;
    606      1.1  cgd 		}
    607      1.1  cgd 		break;
    608      1.1  cgd 	    case '\t': case '\n':	/* assume the 2 spaces got eaten */
    609      1.1  cgd 		if (repl_beginning && repl_could_be_missing &&
    610      1.1  cgd 		  (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
    611      1.1  cgd 		    repl_missing = TRUE;
    612      1.1  cgd 		    goto hunk_done;
    613      1.1  cgd 		}
    614      1.1  cgd 		p_line[p_end] = savestr(buf);
    615      1.1  cgd 		if (out_of_mem) {
    616      1.1  cgd 		    p_end--;
    617      1.1  cgd 		    return FALSE;
    618      1.1  cgd 		}
    619      1.1  cgd 		if (p_end != p_ptrn_lines + 1) {
    620      1.1  cgd 		    ptrn_spaces_eaten |= (repl_beginning != 0);
    621      1.1  cgd 		    context++;
    622      1.1  cgd 		    if (!repl_beginning)
    623      1.1  cgd 			ptrn_copiable++;
    624      1.1  cgd 		    p_char[p_end] = ' ';
    625      1.1  cgd 		}
    626      1.1  cgd 		break;
    627      1.1  cgd 	    case ' ':
    628      1.1  cgd 		if (!isspace(buf[1]) &&
    629      1.1  cgd 		  repl_beginning && repl_could_be_missing) {
    630      1.1  cgd 		    repl_missing = TRUE;
    631      1.1  cgd 		    goto hunk_done;
    632      1.1  cgd 		}
    633      1.1  cgd 		context++;
    634      1.1  cgd 		if (!repl_beginning)
    635      1.1  cgd 		    ptrn_copiable++;
    636      1.1  cgd 		p_line[p_end] = savestr(buf+2);
    637      1.1  cgd 		if (out_of_mem) {
    638      1.1  cgd 		    p_end--;
    639      1.1  cgd 		    return FALSE;
    640      1.1  cgd 		}
    641      1.1  cgd 		break;
    642      1.1  cgd 	    default:
    643      1.1  cgd 		if (repl_beginning && repl_could_be_missing) {
    644  1.1.1.1  tls 		    repl_missing = TRUE;
    645      1.1  cgd 		    goto hunk_done;
    646      1.1  cgd 		}
    647      1.1  cgd 		goto malformed;
    648      1.1  cgd 	    }
    649      1.1  cgd 	    /* set up p_len for strncmp() so we don't have to */
    650      1.1  cgd 	    /* assume null termination */
    651      1.1  cgd 	    if (p_line[p_end])
    652      1.1  cgd 		p_len[p_end] = strlen(p_line[p_end]);
    653      1.1  cgd 	    else
    654      1.1  cgd 		p_len[p_end] = 0;
    655      1.1  cgd 	}
    656  1.1.1.1  tls 
    657      1.1  cgd     hunk_done:
    658      1.1  cgd 	if (p_end >=0 && !repl_beginning)
    659      1.1  cgd 	    fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
    660      1.1  cgd 
    661      1.1  cgd 	if (repl_missing) {
    662      1.1  cgd 
    663      1.1  cgd 	    /* reset state back to just after --- */
    664      1.1  cgd 	    p_input_line = repl_patch_line;
    665      1.1  cgd 	    for (p_end--; p_end > repl_beginning; p_end--)
    666      1.1  cgd 		free(p_line[p_end]);
    667      1.1  cgd 	    Fseek(pfp, repl_backtrack_position, 0);
    668      1.1  cgd 
    669      1.1  cgd 	    /* redundant 'new' context lines were omitted - set */
    670      1.1  cgd 	    /* up to fill them in from the old file context */
    671      1.1  cgd 	    fillsrc = 1;
    672      1.1  cgd 	    filldst = repl_beginning+1;
    673      1.1  cgd 	    fillcnt = p_repl_lines;
    674      1.1  cgd 	    p_end = p_max;
    675      1.1  cgd 	}
    676      1.1  cgd 
    677  1.1.1.1  tls 	if (diff_type == CONTEXT_DIFF &&
    678  1.1.1.1  tls 	  (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
    679  1.1.1.1  tls 	    if (verbose)
    680      1.1  cgd 		say1("\
    681      1.1  cgd (Fascinating--this is really a new-style context diff but without the telltale\n\
    682      1.1  cgd extra asterisks on the *** line that usually indicate the new style...)\n");
    683      1.1  cgd 	    diff_type = NEW_CONTEXT_DIFF;
    684      1.1  cgd 	}
    685      1.1  cgd 
    686      1.1  cgd 	/* if there were omitted context lines, fill them in now */
    687      1.1  cgd 	if (fillcnt) {
    688      1.1  cgd 	    p_bfake = filldst;		/* remember where not to free() */
    689      1.1  cgd 	    p_efake = filldst + fillcnt - 1;
    690      1.1  cgd 	    while (fillcnt-- > 0) {
    691  1.1.1.1  tls 		while (fillsrc <= p_end && p_char[fillsrc] != ' ')
    692      1.1  cgd 		    fillsrc++;
    693      1.1  cgd 		if (fillsrc > p_end)
    694      1.1  cgd 		    fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
    695      1.1  cgd 			p_hunk_beg);
    696      1.1  cgd 		p_line[filldst] = p_line[fillsrc];
    697      1.1  cgd 		p_char[filldst] = p_char[fillsrc];
    698      1.1  cgd 		p_len[filldst] = p_len[fillsrc];
    699      1.1  cgd 		fillsrc++; filldst++;
    700      1.1  cgd 	    }
    701      1.1  cgd 	    while (fillsrc <= p_end && fillsrc != repl_beginning &&
    702      1.1  cgd 	      p_char[fillsrc] != ' ')
    703      1.1  cgd 		fillsrc++;
    704      1.1  cgd #ifdef DEBUGGING
    705      1.1  cgd 	    if (debug & 64)
    706      1.1  cgd 		printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
    707      1.1  cgd 		    fillsrc,filldst,repl_beginning,p_end+1);
    708      1.1  cgd #endif
    709      1.1  cgd 	    assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
    710      1.1  cgd 	    assert(filldst==p_end+1 || filldst==repl_beginning);
    711      1.1  cgd 	}
    712      1.1  cgd     }
    713      1.1  cgd     else {				/* normal diff--fake it up */
    714      1.1  cgd 	char hunk_type;
    715      1.1  cgd 	Reg3 int i;
    716      1.1  cgd 	LINENUM min, max;
    717      1.1  cgd 	long line_beginning = ftell(pfp);
    718      1.1  cgd 
    719      1.1  cgd 	p_context = 0;
    720      1.1  cgd 	ret = pgets(buf, sizeof buf, pfp);
    721      1.1  cgd 	p_input_line++;
    722      1.1  cgd 	if (ret == Nullch || !isdigit(*buf)) {
    723      1.1  cgd 	    next_intuit_at(line_beginning,p_input_line);
    724      1.1  cgd 	    return FALSE;
    725      1.1  cgd 	}
    726      1.1  cgd 	p_first = (LINENUM)atol(buf);
    727      1.1  cgd 	for (s=buf; isdigit(*s); s++) ;
    728      1.1  cgd 	if (*s == ',') {
    729      1.1  cgd 	    p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
    730      1.1  cgd 	    while (isdigit(*s)) s++;
    731      1.1  cgd 	}
    732      1.1  cgd 	else
    733      1.1  cgd 	    p_ptrn_lines = (*s != 'a');
    734      1.1  cgd 	hunk_type = *s;
    735      1.1  cgd 	if (hunk_type == 'a')
    736      1.1  cgd 	    p_first++;			/* do append rather than insert */
    737      1.1  cgd 	min = (LINENUM)atol(++s);
    738      1.1  cgd 	for (; isdigit(*s); s++) ;
    739      1.1  cgd 	if (*s == ',')
    740      1.1  cgd 	    max = (LINENUM)atol(++s);
    741      1.1  cgd 	else
    742      1.1  cgd 	    max = min;
    743      1.1  cgd 	if (hunk_type == 'd')
    744  1.1.1.1  tls 	    min++;
    745      1.1  cgd 	p_end = p_ptrn_lines + 1 + max - min + 1;
    746      1.1  cgd 	if (p_end > MAXHUNKSIZE)
    747      1.1  cgd 	    fatal4("Hunk too large (%ld lines) at line %ld: %s",
    748      1.1  cgd 		  p_end, p_input_line, buf);
    749      1.1  cgd 	while (p_end >= hunkmax)
    750      1.1  cgd 	    grow_hunkmax();
    751      1.1  cgd 	p_newfirst = min;
    752      1.1  cgd 	p_repl_lines = max - min + 1;
    753      1.1  cgd 	Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
    754      1.1  cgd 	p_line[0] = savestr(buf);
    755      1.1  cgd 	if (out_of_mem) {
    756      1.1  cgd 	    p_end = -1;
    757      1.1  cgd 	    return FALSE;
    758      1.1  cgd 	}
    759      1.1  cgd 	p_char[0] = '*';
    760      1.1  cgd 	for (i=1; i<=p_ptrn_lines; i++) {
    761  1.1.1.1  tls 	    ret = pgets(buf, sizeof buf, pfp);
    762      1.1  cgd 	    p_input_line++;
    763      1.1  cgd 	    if (ret == Nullch)
    764  1.1.1.1  tls 		fatal2("Unexpected end of file in patch at line %ld.\n",
    765      1.1  cgd 		  p_input_line);
    766      1.1  cgd 	    if (*buf != '<')
    767      1.1  cgd 		fatal2("< expected at line %ld of patch.\n", p_input_line);
    768      1.1  cgd 	    p_line[i] = savestr(buf+2);
    769      1.1  cgd 	    if (out_of_mem) {
    770      1.1  cgd 		p_end = i-1;
    771      1.1  cgd 		return FALSE;
    772      1.1  cgd 	    }
    773      1.1  cgd 	    p_len[i] = strlen(p_line[i]);
    774      1.1  cgd 	    p_char[i] = '-';
    775      1.1  cgd 	}
    776      1.1  cgd 	if (hunk_type == 'c') {
    777  1.1.1.1  tls 	    ret = pgets(buf, sizeof buf, pfp);
    778      1.1  cgd 	    p_input_line++;
    779      1.1  cgd 	    if (ret == Nullch)
    780  1.1.1.1  tls 		fatal2("Unexpected end of file in patch at line %ld.\n",
    781      1.1  cgd 		    p_input_line);
    782      1.1  cgd 	    if (*buf != '-')
    783      1.1  cgd 		fatal2("--- expected at line %ld of patch.\n", p_input_line);
    784      1.1  cgd 	}
    785      1.1  cgd 	Sprintf(buf, "--- %ld,%ld\n", min, max);
    786      1.1  cgd 	p_line[i] = savestr(buf);
    787      1.1  cgd 	if (out_of_mem) {
    788      1.1  cgd 	    p_end = i-1;
    789      1.1  cgd 	    return FALSE;
    790      1.1  cgd 	}
    791      1.1  cgd 	p_char[i] = '=';
    792      1.1  cgd 	for (i++; i<=p_end; i++) {
    793  1.1.1.1  tls 	    ret = pgets(buf, sizeof buf, pfp);
    794      1.1  cgd 	    p_input_line++;
    795      1.1  cgd 	    if (ret == Nullch)
    796  1.1.1.1  tls 		fatal2("Unexpected end of file in patch at line %ld.\n",
    797      1.1  cgd 		    p_input_line);
    798      1.1  cgd 	    if (*buf != '>')
    799      1.1  cgd 		fatal2("> expected at line %ld of patch.\n", p_input_line);
    800      1.1  cgd 	    p_line[i] = savestr(buf+2);
    801      1.1  cgd 	    if (out_of_mem) {
    802      1.1  cgd 		p_end = i-1;
    803      1.1  cgd 		return FALSE;
    804      1.1  cgd 	    }
    805      1.1  cgd 	    p_len[i] = strlen(p_line[i]);
    806      1.1  cgd 	    p_char[i] = '+';
    807      1.1  cgd 	}
    808      1.1  cgd     }
    809      1.1  cgd     if (reverse)			/* backwards patch? */
    810      1.1  cgd 	if (!pch_swap())
    811      1.1  cgd 	    say1("Not enough memory to swap next hunk!\n");
    812      1.1  cgd #ifdef DEBUGGING
    813      1.1  cgd     if (debug & 2) {
    814      1.1  cgd 	int i;
    815      1.1  cgd 	char special;
    816      1.1  cgd 
    817      1.1  cgd 	for (i=0; i <= p_end; i++) {
    818      1.1  cgd 	    if (i == p_ptrn_lines)
    819      1.1  cgd 		special = '^';
    820      1.1  cgd 	    else
    821      1.1  cgd 		special = ' ';
    822      1.1  cgd 	    fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
    823      1.1  cgd 	    Fflush(stderr);
    824      1.1  cgd 	}
    825      1.1  cgd     }
    826      1.1  cgd #endif
    827  1.1.1.1  tls     if (p_end+1 < hunkmax)	/* paranoia reigns supreme... */
    828  1.1.1.1  tls 	p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
    829  1.1.1.1  tls     return TRUE;
    830  1.1.1.1  tls 
    831  1.1.1.1  tls malformed:
    832      1.1  cgd     fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
    833      1.1  cgd 		/* about as informative as "Syntax error" in C */
    834      1.1  cgd     return FALSE;	/* for lint */
    835      1.1  cgd }
    836      1.1  cgd 
    837      1.1  cgd /* Input a line from the patch file, worrying about indentation. */
    838      1.1  cgd 
    839      1.1  cgd char *
    840      1.1  cgd pgets(bf,sz,fp)
    841      1.1  cgd char *bf;
    842      1.1  cgd int sz;
    843      1.1  cgd FILE *fp;
    844      1.1  cgd {
    845      1.1  cgd     char *ret = fgets(bf, sz, fp);
    846      1.1  cgd     Reg1 char *s;
    847  1.1.1.1  tls     Reg2 int indent = 0;
    848      1.1  cgd 
    849      1.1  cgd     if (p_indent && ret != Nullch) {
    850      1.1  cgd 	for (s=buf; indent < p_indent && (*s == ' ' || *s == '\t'); s++) {
    851      1.1  cgd 	    if (*s == '\t')
    852      1.1  cgd 		indent += 8 - (indent % 7);
    853      1.1  cgd 	    else
    854      1.1  cgd 		indent++;
    855      1.1  cgd 	}
    856      1.1  cgd 	if (buf != s)
    857      1.1  cgd 	    Strcpy(buf, s);
    858      1.1  cgd     }
    859      1.1  cgd     return ret;
    860      1.1  cgd }
    861      1.1  cgd 
    862      1.1  cgd /* Reverse the old and new portions of the current hunk. */
    863      1.1  cgd 
    864      1.1  cgd bool
    865      1.1  cgd pch_swap()
    866      1.1  cgd {
    867      1.1  cgd     char **tp_line;		/* the text of the hunk */
    868      1.1  cgd     short *tp_len;		/* length of each line */
    869      1.1  cgd     char *tp_char;		/* +, -, and ! */
    870      1.1  cgd     Reg1 LINENUM i;
    871      1.1  cgd     Reg2 LINENUM n;
    872      1.1  cgd     bool blankline = FALSE;
    873      1.1  cgd     Reg3 char *s;
    874      1.1  cgd 
    875      1.1  cgd     i = p_first;
    876      1.1  cgd     p_first = p_newfirst;
    877      1.1  cgd     p_newfirst = i;
    878      1.1  cgd 
    879      1.1  cgd     /* make a scratch copy */
    880      1.1  cgd 
    881      1.1  cgd     tp_line = p_line;
    882      1.1  cgd     tp_len = p_len;
    883      1.1  cgd     tp_char = p_char;
    884      1.1  cgd     p_line = Null(char**);	/* force set_hunkmax to allocate again */
    885      1.1  cgd     p_len = Null(short*);
    886      1.1  cgd     p_char = Nullch;
    887      1.1  cgd     set_hunkmax();
    888      1.1  cgd     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
    889      1.1  cgd #ifndef lint
    890      1.1  cgd 	if (p_line == Null(char**))
    891      1.1  cgd 	    free((char*)p_line);
    892      1.1  cgd 	p_line = tp_line;
    893      1.1  cgd 	if (p_len == Null(short*))
    894      1.1  cgd 	    free((char*)p_len);
    895      1.1  cgd 	p_len = tp_len;
    896      1.1  cgd #endif
    897      1.1  cgd 	if (p_char == Nullch)
    898      1.1  cgd 	    free((char*)p_char);
    899      1.1  cgd 	p_char = tp_char;
    900      1.1  cgd 	return FALSE;		/* not enough memory to swap hunk! */
    901      1.1  cgd     }
    902      1.1  cgd 
    903      1.1  cgd     /* now turn the new into the old */
    904      1.1  cgd 
    905      1.1  cgd     i = p_ptrn_lines + 1;
    906      1.1  cgd     if (tp_char[i] == '\n') {		/* account for possible blank line */
    907      1.1  cgd 	blankline = TRUE;
    908  1.1.1.1  tls 	i++;
    909  1.1.1.1  tls     }
    910  1.1.1.1  tls     if (p_efake >= 0) {			/* fix non-freeable ptr range */
    911      1.1  cgd 	n = p_end - i + 1;
    912      1.1  cgd 	if (p_efake > i)
    913      1.1  cgd 	    n = -n;
    914      1.1  cgd 	p_efake += n;
    915      1.1  cgd 	p_bfake += n;
    916      1.1  cgd     }
    917      1.1  cgd     for (n=0; i <= p_end; i++,n++) {
    918      1.1  cgd 	p_line[n] = tp_line[i];
    919      1.1  cgd 	p_char[n] = tp_char[i];
    920      1.1  cgd 	if (p_char[n] == '+')
    921      1.1  cgd 	    p_char[n] = '-';
    922      1.1  cgd 	p_len[n] = tp_len[i];
    923      1.1  cgd     }
    924      1.1  cgd     if (blankline) {
    925      1.1  cgd 	i = p_ptrn_lines + 1;
    926      1.1  cgd 	p_line[n] = tp_line[i];
    927      1.1  cgd 	p_char[n] = tp_char[i];
    928      1.1  cgd 	p_len[n] = tp_len[i];
    929      1.1  cgd 	n++;
    930      1.1  cgd     }
    931      1.1  cgd     assert(p_char[0] == '=');
    932      1.1  cgd     p_char[0] = '*';
    933      1.1  cgd     for (s=p_line[0]; *s; s++)
    934      1.1  cgd 	if (*s == '-')
    935      1.1  cgd 	    *s = '*';
    936      1.1  cgd 
    937      1.1  cgd     /* now turn the old into the new */
    938      1.1  cgd 
    939      1.1  cgd     assert(tp_char[0] == '*');
    940      1.1  cgd     tp_char[0] = '=';
    941      1.1  cgd     for (s=tp_line[0]; *s; s++)
    942      1.1  cgd 	if (*s == '*')
    943      1.1  cgd 	    *s = '-';
    944      1.1  cgd     for (i=0; n <= p_end; i++,n++) {
    945      1.1  cgd 	p_line[n] = tp_line[i];
    946      1.1  cgd 	p_char[n] = tp_char[i];
    947      1.1  cgd 	if (p_char[n] == '-')
    948      1.1  cgd 	    p_char[n] = '+';
    949      1.1  cgd 	p_len[n] = tp_len[i];
    950      1.1  cgd     }
    951      1.1  cgd     assert(i == p_ptrn_lines + 1);
    952      1.1  cgd     i = p_ptrn_lines;
    953      1.1  cgd     p_ptrn_lines = p_repl_lines;
    954      1.1  cgd     p_repl_lines = i;
    955      1.1  cgd #ifndef lint
    956      1.1  cgd     if (tp_line == Null(char**))
    957      1.1  cgd 	free((char*)tp_line);
    958      1.1  cgd     if (tp_len == Null(short*))
    959      1.1  cgd 	free((char*)tp_len);
    960      1.1  cgd #endif
    961      1.1  cgd     if (tp_char == Nullch)
    962      1.1  cgd 	free((char*)tp_char);
    963      1.1  cgd     return TRUE;
    964      1.1  cgd }
    965      1.1  cgd 
    966      1.1  cgd /* Return the specified line position in the old file of the old context. */
    967      1.1  cgd 
    968      1.1  cgd LINENUM
    969      1.1  cgd pch_first()
    970      1.1  cgd {
    971      1.1  cgd     return p_first;
    972      1.1  cgd }
    973      1.1  cgd 
    974      1.1  cgd /* Return the number of lines of old context. */
    975      1.1  cgd 
    976      1.1  cgd LINENUM
    977      1.1  cgd pch_ptrn_lines()
    978      1.1  cgd {
    979      1.1  cgd     return p_ptrn_lines;
    980      1.1  cgd }
    981      1.1  cgd 
    982      1.1  cgd /* Return the probable line position in the new file of the first line. */
    983      1.1  cgd 
    984      1.1  cgd LINENUM
    985      1.1  cgd pch_newfirst()
    986      1.1  cgd {
    987      1.1  cgd     return p_newfirst;
    988      1.1  cgd }
    989      1.1  cgd 
    990      1.1  cgd /* Return the number of lines in the replacement text including context. */
    991      1.1  cgd 
    992      1.1  cgd LINENUM
    993      1.1  cgd pch_repl_lines()
    994      1.1  cgd {
    995      1.1  cgd     return p_repl_lines;
    996      1.1  cgd }
    997      1.1  cgd 
    998      1.1  cgd /* Return the number of lines in the whole hunk. */
    999      1.1  cgd 
   1000      1.1  cgd LINENUM
   1001      1.1  cgd pch_end()
   1002      1.1  cgd {
   1003      1.1  cgd     return p_end;
   1004      1.1  cgd }
   1005      1.1  cgd 
   1006      1.1  cgd /* Return the number of context lines before the first changed line. */
   1007      1.1  cgd 
   1008      1.1  cgd LINENUM
   1009      1.1  cgd pch_context()
   1010      1.1  cgd {
   1011      1.1  cgd     return p_context;
   1012      1.1  cgd }
   1013      1.1  cgd 
   1014      1.1  cgd /* Return the length of a particular patch line. */
   1015      1.1  cgd 
   1016      1.1  cgd short
   1017      1.1  cgd pch_line_len(line)
   1018      1.1  cgd LINENUM line;
   1019      1.1  cgd {
   1020      1.1  cgd     return p_len[line];
   1021      1.1  cgd }
   1022      1.1  cgd 
   1023      1.1  cgd /* Return the control character (+, -, *, !, etc) for a patch line. */
   1024      1.1  cgd 
   1025      1.1  cgd char
   1026      1.1  cgd pch_char(line)
   1027      1.1  cgd LINENUM line;
   1028      1.1  cgd {
   1029      1.1  cgd     return p_char[line];
   1030      1.1  cgd }
   1031      1.1  cgd 
   1032      1.1  cgd /* Return a pointer to a particular patch line. */
   1033      1.1  cgd 
   1034      1.1  cgd char *
   1035      1.1  cgd pfetch(line)
   1036      1.1  cgd LINENUM line;
   1037      1.1  cgd {
   1038      1.1  cgd     return p_line[line];
   1039      1.1  cgd }
   1040      1.1  cgd 
   1041      1.1  cgd /* Return where in the patch file this hunk began, for error messages. */
   1042      1.1  cgd 
   1043      1.1  cgd LINENUM
   1044      1.1  cgd pch_hunk_beg()
   1045      1.1  cgd {
   1046      1.1  cgd     return p_hunk_beg;
   1047      1.1  cgd }
   1048      1.1  cgd 
   1049      1.1  cgd /* Apply an ed script by feeding ed itself. */
   1050      1.1  cgd 
   1051      1.1  cgd void
   1052      1.1  cgd do_ed_script()
   1053      1.1  cgd {
   1054      1.1  cgd     Reg1 char *t;
   1055  1.1.1.1  tls     Reg2 long beginning_of_this_line;
   1056      1.1  cgd     Reg3 bool this_line_is_command = FALSE;
   1057      1.1  cgd     Reg4 FILE *pipefp;
   1058      1.1  cgd     FILE *popen();
   1059      1.1  cgd 
   1060      1.1  cgd     if (!skip_rest_of_patch) {
   1061      1.1  cgd 	Unlink(TMPOUTNAME);
   1062      1.1  cgd 	copy_file(filearg[0], TMPOUTNAME);
   1063      1.1  cgd 	if (verbose)
   1064      1.1  cgd 	    Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
   1065      1.1  cgd 	else
   1066      1.1  cgd 	    Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
   1067      1.1  cgd 	pipefp = popen(buf, "w");
   1068      1.1  cgd     }
   1069      1.1  cgd     for (;;) {
   1070      1.1  cgd 	beginning_of_this_line = ftell(pfp);
   1071      1.1  cgd 	if (pgets(buf, sizeof buf, pfp) == Nullch) {
   1072      1.1  cgd 	    next_intuit_at(beginning_of_this_line,p_input_line);
   1073      1.1  cgd 	    break;
   1074      1.1  cgd 	}
   1075      1.1  cgd 	p_input_line++;
   1076      1.1  cgd 	for (t=buf; isdigit(*t) || *t == ','; t++) ;
   1077      1.1  cgd 	this_line_is_command = (isdigit(*buf) &&
   1078      1.1  cgd 	  (*t == 'd' || *t == 'c' || *t == 'a') );
   1079      1.1  cgd 	if (this_line_is_command) {
   1080      1.1  cgd 	    if (!skip_rest_of_patch)
   1081      1.1  cgd 		fputs(buf, pipefp);
   1082      1.1  cgd 	    if (*t != 'd') {
   1083      1.1  cgd 		while (pgets(buf, sizeof buf, pfp) != Nullch) {
   1084      1.1  cgd 		    p_input_line++;
   1085      1.1  cgd 		    if (!skip_rest_of_patch)
   1086      1.1  cgd 			fputs(buf, pipefp);
   1087      1.1  cgd 		    if (strEQ(buf, ".\n"))
   1088      1.1  cgd 			break;
   1089      1.1  cgd 		}
   1090      1.1  cgd 	    }
   1091      1.1  cgd 	}
   1092      1.1  cgd 	else {
   1093      1.1  cgd 	    next_intuit_at(beginning_of_this_line,p_input_line);
   1094      1.1  cgd 	    break;
   1095      1.1  cgd 	}
   1096      1.1  cgd     }
   1097      1.1  cgd     if (skip_rest_of_patch)
   1098      1.1  cgd 	return;
   1099      1.1  cgd     fprintf(pipefp, "w\n");
   1100      1.1  cgd     fprintf(pipefp, "q\n");
   1101      1.1  cgd     Fflush(pipefp);
   1102      1.1  cgd     Pclose(pipefp);
   1103      1.1  cgd     ignore_signals();
   1104      1.1  cgd     if (move_file(TMPOUTNAME, outname) < 0) {
   1105      1.1  cgd 	toutkeep = TRUE;
   1106      1.1  cgd 	chmod(TMPOUTNAME, filemode);
   1107  1.1.1.1  tls     }
   1108      1.1  cgd     else
   1109               	chmod(outname, filemode);
   1110                   set_signals();
   1111               }
   1112