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