xref: /openbsd-src/usr.bin/cvs/diff3.c (revision 08d4acf6df9a2cd5282efd339e89b74b913004ff)
1*08d4acf6Sstsp /*	$OpenBSD: diff3.c,v 1.65 2021/04/13 14:20:23 stsp Exp $	*/
201af718aSjoris 
301af718aSjoris /*
401af718aSjoris  * Copyright (C) Caldera International Inc.  2001-2002.
501af718aSjoris  * All rights reserved.
601af718aSjoris  *
701af718aSjoris  * Redistribution and use in source and binary forms, with or without
801af718aSjoris  * modification, are permitted provided that the following conditions
901af718aSjoris  * are met:
1001af718aSjoris  * 1. Redistributions of source code and documentation must retain the above
1101af718aSjoris  *    copyright notice, this list of conditions and the following disclaimer.
1201af718aSjoris  * 2. Redistributions in binary form must reproduce the above copyright
1301af718aSjoris  *    notice, this list of conditions and the following disclaimer in the
1401af718aSjoris  *    documentation and/or other materials provided with the distribution.
1501af718aSjoris  * 3. All advertising materials mentioning features or use of this software
1601af718aSjoris  *    must display the following acknowledgement:
1701af718aSjoris  *	This product includes software developed or owned by Caldera
1801af718aSjoris  *	International, Inc.
1901af718aSjoris  * 4. Neither the name of Caldera International, Inc. nor the names of other
2001af718aSjoris  *    contributors may be used to endorse or promote products derived from
2101af718aSjoris  *    this software without specific prior written permission.
2201af718aSjoris  *
2301af718aSjoris  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
2401af718aSjoris  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
2501af718aSjoris  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2601af718aSjoris  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2701af718aSjoris  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
2801af718aSjoris  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2901af718aSjoris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
3001af718aSjoris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3101af718aSjoris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3201af718aSjoris  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3301af718aSjoris  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3401af718aSjoris  * POSSIBILITY OF SUCH DAMAGE.
3501af718aSjoris  */
3601af718aSjoris /*-
3701af718aSjoris  * Copyright (c) 1991, 1993
3801af718aSjoris  *	The Regents of the University of California.  All rights reserved.
3901af718aSjoris  *
4001af718aSjoris  * Redistribution and use in source and binary forms, with or without
4101af718aSjoris  * modification, are permitted provided that the following conditions
4201af718aSjoris  * are met:
4301af718aSjoris  * 1. Redistributions of source code must retain the above copyright
4401af718aSjoris  *    notice, this list of conditions and the following disclaimer.
4501af718aSjoris  * 2. Redistributions in binary form must reproduce the above copyright
4601af718aSjoris  *    notice, this list of conditions and the following disclaimer in the
4701af718aSjoris  *    documentation and/or other materials provided with the distribution.
4801af718aSjoris  * 3. Neither the name of the University nor the names of its contributors
4901af718aSjoris  *    may be used to endorse or promote products derived from this software
5001af718aSjoris  *    without specific prior written permission.
5101af718aSjoris  *
5201af718aSjoris  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5301af718aSjoris  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5401af718aSjoris  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5501af718aSjoris  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5601af718aSjoris  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5701af718aSjoris  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5801af718aSjoris  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5901af718aSjoris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6001af718aSjoris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6101af718aSjoris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6201af718aSjoris  * SUCH DAMAGE.
6301af718aSjoris  *
6401af718aSjoris  *	@(#)diff3.c	8.1 (Berkeley) 6/6/93
6501af718aSjoris  */
6601af718aSjoris 
671f8531bdSotto #include <ctype.h>
681f8531bdSotto #include <errno.h>
691f8531bdSotto #include <fcntl.h>
70a52c79cdStobias #include <stdio.h>
711f8531bdSotto #include <stdlib.h>
721f8531bdSotto #include <string.h>
731f8531bdSotto #include <unistd.h>
7401af718aSjoris 
75bb59aeccStobias #include "atomicio.h"
7601af718aSjoris #include "cvs.h"
7701af718aSjoris #include "diff.h"
7801af718aSjoris 
7901af718aSjoris /* diff3 - 3-way differential file comparison */
8001af718aSjoris 
8101af718aSjoris /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
8201af718aSjoris  *
8301af718aSjoris  * d13 = diff report on f1 vs f3
8401af718aSjoris  * d23 = diff report on f2 vs f3
8501af718aSjoris  * f1, f2, f3 the 3 files
8601af718aSjoris  * if changes in f1 overlap with changes in f3, m1 and m3 are used
8701af718aSjoris  * to mark the overlaps; otherwise, the file names f1 and f3 are used
8801af718aSjoris  * (only for options E and X).
8901af718aSjoris  */
9001af718aSjoris 
9101af718aSjoris /*
9201af718aSjoris  * "from" is first in range of changed lines; "to" is last+1
9301af718aSjoris  * from=to=line after point of insertion for added lines.
9401af718aSjoris  */
9501af718aSjoris struct range {
9601af718aSjoris 	int from;
9701af718aSjoris 	int to;
9801af718aSjoris };
9901af718aSjoris 
10001af718aSjoris struct diff {
10101af718aSjoris 	struct range old;
10201af718aSjoris 	struct range new;
10301af718aSjoris };
10401af718aSjoris 
10501af718aSjoris static size_t szchanges;
10601af718aSjoris 
1077bb3ddb0Sray static struct diff *d13;
1087bb3ddb0Sray static struct diff *d23;
10901af718aSjoris 
11001af718aSjoris /*
11101af718aSjoris  * "de" is used to gather editing scripts.  These are later spewed out in
11201af718aSjoris  * reverse order.  Its first element must be all zero, the "new" component
11301af718aSjoris  * of "de" contains line positions or byte positions depending on when you
11401af718aSjoris  * look (!?).  Array overlap indicates which sections in "de" correspond to
11501af718aSjoris  * lines that are different in all three files.
11601af718aSjoris  */
1177bb3ddb0Sray static struct diff *de;
1187bb3ddb0Sray static char *overlap;
119a777650bSjoris static int overlapcnt = 0;
12001af718aSjoris static FILE *fp[3];
12101af718aSjoris static int cline[3];		/* # of the last-read line in each file (0-2) */
12201af718aSjoris 
12301af718aSjoris /*
12401af718aSjoris  * the latest known correspondence between line numbers of the 3 files
12501af718aSjoris  * is stored in last[1-3];
12601af718aSjoris  */
12701af718aSjoris static int last[4];
12801af718aSjoris static int eflag;
12901af718aSjoris static int oflag;		/* indicates whether to mark overlaps (-E or -X)*/
13001af718aSjoris static int debug  = 0;
131b9fc9a72Sderaadt static char f1mark[PATH_MAX], f3mark[PATH_MAX];	/* markers for -E and -X */
13201af718aSjoris 
13301af718aSjoris static int duplicate(struct range *, struct range *);
13401af718aSjoris static int edit(struct diff *, int, int);
13501af718aSjoris static char *getchange(FILE *);
136f9bbbf45Sfgsch static char *get_line(FILE *, size_t *);
13701af718aSjoris static int number(char **);
1382e0d696aSjoris static size_t readin(int, struct diff **);
13901af718aSjoris static int skip(int, int, char *);
14001af718aSjoris static int edscript(int);
141b2dc910bSray static int merge(size_t, size_t);
14201af718aSjoris static void change(int, struct range *, int);
14301af718aSjoris static void keep(int, struct range *);
14401af718aSjoris static void prange(struct range *);
14501af718aSjoris static void repos(int);
14601af718aSjoris static void separate(const char *);
14701af718aSjoris static void increase(void);
14801af718aSjoris static int diff3_internal(int, char **, const char *, const char *);
14901af718aSjoris 
150a4509d3fSjoris int diff3_conflicts = 0;
15125d391d7Sjoris RCSNUM *d3rev1 = NULL;
15225d391d7Sjoris RCSNUM *d3rev2 = NULL;
153a4509d3fSjoris 
1542e0d696aSjoris static int fds[5];
1552e0d696aSjoris 
156828954c0Sjoris void
cvs_merge_file(struct cvs_file * cf,int verbose)157828954c0Sjoris cvs_merge_file(struct cvs_file *cf, int verbose)
15801af718aSjoris {
1592e0d696aSjoris 	int i, argc;
16001af718aSjoris 	char *data, *patch;
1610a7da307Sxsa 	char *argv[5], r1[CVS_REV_BUFSZ], r2[CVS_REV_BUFSZ];
1626a5d6816Sxsa 	char *dp13, *dp23, *path1, *path2, *path3;
16329d9b3a9Snicm 	BUF *b1, *d1, *d2, *diffb;
16431cfd684Sniallo 	size_t dlen, plen;
1657bb3ddb0Sray 	struct rcs_line *lp;
1667bb3ddb0Sray 	struct rcs_lines *dlines, *plines;
16701af718aSjoris 
168cbd9d31aSjoris 	overlapcnt = 0;
16929d9b3a9Snicm 	b1 = d1 = d2 = diffb = NULL;
17025d391d7Sjoris 
17125d391d7Sjoris 	rcsnum_tostr(d3rev1, r1, sizeof(r1));
17225d391d7Sjoris 	rcsnum_tostr(d3rev2, r2, sizeof(r2));
17301af718aSjoris 
1747bb3ddb0Sray 	b1 = buf_load_fd(cf->fd);
1757bb3ddb0Sray 	d1 = buf_alloc(128);
1767bb3ddb0Sray 	d2 = buf_alloc(128);
1777bb3ddb0Sray 	diffb = buf_alloc(128);
17801af718aSjoris 
1796a5d6816Sxsa 	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1806a5d6816Sxsa 	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1816a5d6816Sxsa 	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", cvs_tmpdir);
1826a5d6816Sxsa 
1837bb3ddb0Sray 	fds[2] = buf_write_stmp(b1, path1, NULL);
1840dbb72f1Sniallo 	if (verbose == 1)
1850dbb72f1Sniallo 		cvs_printf("Retrieving revision %s\n", r1);
18625d391d7Sjoris 	fds[3] = rcs_rev_write_stmp(cf->file_rcs, d3rev1, path2, 0);
1870dbb72f1Sniallo 	if (verbose == 1)
1880dbb72f1Sniallo 		cvs_printf("Retrieving revision %s\n", r2);
18925d391d7Sjoris 	fds[4] = rcs_rev_write_stmp(cf->file_rcs, d3rev2, path3, 0);
19001af718aSjoris 
19157003866Sray 	diffreg(path1, path3, fds[2], fds[4], d1, D_FORCEASCII);
19257003866Sray 	diffreg(path2, path3, fds[3], fds[4], d2, D_FORCEASCII);
19301af718aSjoris 
1946a5d6816Sxsa 	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", cvs_tmpdir);
1957bb3ddb0Sray 	fds[0] = buf_write_stmp(d1, dp13, NULL);
1967bb3ddb0Sray 	buf_free(d1);
19701af718aSjoris 
1986a5d6816Sxsa 	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", cvs_tmpdir);
1997bb3ddb0Sray 	fds[1] = buf_write_stmp(d2, dp23, NULL);
2007bb3ddb0Sray 	buf_free(d2);
20101af718aSjoris 
20201af718aSjoris 	argc = 0;
20301af718aSjoris 	diffbuf = diffb;
20401af718aSjoris 	argv[argc++] = dp13;
20501af718aSjoris 	argv[argc++] = dp23;
20601af718aSjoris 	argv[argc++] = path1;
20701af718aSjoris 	argv[argc++] = path2;
20801af718aSjoris 	argv[argc++] = path3;
2095191afffSjoris 
2103aaa63ebSderaadt 	if (lseek(fds[2], 0, SEEK_SET) == -1)
2112e0d696aSjoris 		fatal("cvs_merge_file: lseek fds[2]: %s", strerror(errno));
2123aaa63ebSderaadt 	if (lseek(fds[3], 0, SEEK_SET) == -1)
2132e0d696aSjoris 		fatal("cvs_merge_file: lseek fds[3]: %s", strerror(errno));
2143aaa63ebSderaadt 	if (lseek(fds[4], 0, SEEK_SET) == -1)
2152e0d696aSjoris 		fatal("cvs_merge_file: lseek fds[4]: %s", strerror(errno));
2162e0d696aSjoris 
217828954c0Sjoris 	diff3_conflicts = diff3_internal(argc, argv, cf->file_path, r2);
218828954c0Sjoris 	if (diff3_conflicts < 0)
219828954c0Sjoris 		fatal("cvs_merge_file: merging failed for an unknown reason");
22001af718aSjoris 
2217bb3ddb0Sray 	plen = buf_len(diffb);
2227bb3ddb0Sray 	patch = buf_release(diffb);
2237bb3ddb0Sray 	dlen = buf_len(b1);
2247bb3ddb0Sray 	data = buf_release(b1);
22501af718aSjoris 
22625d391d7Sjoris 	if (verbose == 1)
227828954c0Sjoris 		cvs_printf("Merging differences between %s and %s into `%s'\n",
228828954c0Sjoris 		    r1, r2, cf->file_path);
229828954c0Sjoris 
230828954c0Sjoris 	dlines = cvs_splitlines(data, dlen);
231828954c0Sjoris 	plines = cvs_splitlines(patch, plen);
23225d391d7Sjoris 
233828954c0Sjoris 	ed_patch_lines(dlines, plines);
234828954c0Sjoris 	cvs_freelines(plines);
23501af718aSjoris 
236bbf57f10Sderaadt 	if (verbose == 1 && diff3_conflicts != 0) {
2373ad3fb45Sjoris 		cvs_log(LP_ERR, "%d conflict%s found during merge, "
238db6f0a0cSxsa 		    "please correct.", diff3_conflicts,
239f480112dSreyk 		    (diff3_conflicts > 1) ? "s" : "");
2405191afffSjoris 	}
2415191afffSjoris 
2422e0d696aSjoris 	(void)close(cf->fd);
243961a9361Sjoris 	cf->fd = open(cf->file_path, O_CREAT | O_RDWR | O_TRUNC, 0644);
2442e0d696aSjoris 	if (cf->fd == -1) {
2452e0d696aSjoris 		fatal("cvs_merge_file: failed to reopen fd for writing: %s",
2462e0d696aSjoris 		    strerror(errno));
2472e0d696aSjoris 	}
2482e0d696aSjoris 
249828954c0Sjoris 	TAILQ_FOREACH(lp, &(dlines->l_lines), l_list) {
250828954c0Sjoris 		if (lp->l_line == NULL)
251828954c0Sjoris 			continue;
252828954c0Sjoris 
253bb59aeccStobias 		if (atomicio(vwrite, cf->fd, lp->l_line, lp->l_len) !=
254bb59aeccStobias 		    lp->l_len)
255828954c0Sjoris 			fatal("cvs_merge_file: %s", strerror(errno));
256828954c0Sjoris 	}
257828954c0Sjoris 
258828954c0Sjoris 	cvs_freelines(dlines);
259828954c0Sjoris 
260397ddb8aSnicm 	free(data);
261397ddb8aSnicm 	free(patch);
26201af718aSjoris 
2632e0d696aSjoris 	for (i = 0; i < 3; i++)
2642e0d696aSjoris 		fclose(fp[i]);
2652e0d696aSjoris 
2667a9e6d11Sray 	worklist_run(&temp_files, worklist_unlink);
26701af718aSjoris 
268397ddb8aSnicm 	free(path1);
269397ddb8aSnicm 	free(path2);
270397ddb8aSnicm 	free(path3);
271397ddb8aSnicm 	free(dp13);
272397ddb8aSnicm 	free(dp23);
27301af718aSjoris }
27401af718aSjoris 
27501af718aSjoris static int
diff3_internal(int argc,char ** argv,const char * fmark,const char * rmark)27601af718aSjoris diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
27701af718aSjoris {
278b2dc910bSray 	size_t m, n;
279b2dc910bSray 	int i;
28001af718aSjoris 
28101af718aSjoris 	eflag = 3;
28201af718aSjoris 	oflag = 1;
28301af718aSjoris 
28401af718aSjoris 	if (argc < 5)
28501af718aSjoris 		return (-1);
28601af718aSjoris 
287c486465dSxsa 	(void)xsnprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
288c486465dSxsa 	(void)xsnprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
28901af718aSjoris 
29025d391d7Sjoris 	szchanges = 0;
29125d391d7Sjoris 	memset(last, 0, sizeof(last));
29225d391d7Sjoris 	memset(cline, 0, sizeof(cline));
293397ddb8aSnicm 	free(d13);
294397ddb8aSnicm 	free(d23);
295397ddb8aSnicm 	free(overlap);
296397ddb8aSnicm 	free(de);
29725d391d7Sjoris 
298ae886706Smillert 	de = d13 = d23 = NULL;
299ae886706Smillert 	overlap = NULL;
30025d391d7Sjoris 
30101af718aSjoris 	increase();
3022e0d696aSjoris 
3032e0d696aSjoris 	/* fds[0] and fds[1] are closed in readin() */
3042e0d696aSjoris 	m = readin(fds[0], &d13);
3052e0d696aSjoris 	n = readin(fds[1], &d23);
30601af718aSjoris 
30701af718aSjoris 	for (i = 0; i <= 2; i++) {
3082e0d696aSjoris 		if ((fp[i] = fdopen(fds[i + 2], "r")) == NULL) {
3093ad3fb45Sjoris 			cvs_log(LP_ERR, "%s", argv[i + 2]);
31001af718aSjoris 			return (-1);
31101af718aSjoris 		}
31201af718aSjoris 	}
31301af718aSjoris 
31401af718aSjoris 	return (merge(m, n));
31501af718aSjoris }
31601af718aSjoris 
31701af718aSjoris int
ed_patch_lines(struct rcs_lines * dlines,struct rcs_lines * plines)3187bb3ddb0Sray ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
31901af718aSjoris {
320f904309aSjoris 	char op, *ep;
3217bb3ddb0Sray 	struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
322138122c7Sxsa 	int start, end, i, lineno;
32331cfd684Sniallo 	u_char tmp;
32401af718aSjoris 
32501af718aSjoris 	dlp = TAILQ_FIRST(&(dlines->l_lines));
32601af718aSjoris 	lp = TAILQ_FIRST(&(plines->l_lines));
32701af718aSjoris 
328138122c7Sxsa 	end = 0;
32901af718aSjoris 	for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
33001af718aSjoris 	    lp = TAILQ_NEXT(lp, l_list)) {
33131cfd684Sniallo 		/* Skip blank lines */
33231cfd684Sniallo 		if (lp->l_len < 2)
33331cfd684Sniallo 			continue;
33425d391d7Sjoris 
33531cfd684Sniallo 		/* NUL-terminate line buffer for strtol() safety. */
33631cfd684Sniallo 		tmp = lp->l_line[lp->l_len - 1];
33731cfd684Sniallo 		lp->l_line[lp->l_len - 1] = '\0';
33825d391d7Sjoris 
33901af718aSjoris 		op = lp->l_line[strlen(lp->l_line) - 1];
34001af718aSjoris 		start = (int)strtol(lp->l_line, &ep, 10);
34125d391d7Sjoris 
34231cfd684Sniallo 		/* Restore the last byte of the buffer */
34331cfd684Sniallo 		lp->l_line[lp->l_len - 1] = tmp;
34425d391d7Sjoris 
34501af718aSjoris 		if (op == 'a') {
346bbf57f10Sderaadt 			if (start > dlines->l_nblines ||
347bbf57f10Sderaadt 			    start < 0 || *ep != 'a')
34825d391d7Sjoris 				fatal("ed_patch_lines %d", start);
34901af718aSjoris 		} else if (op == 'c') {
350bbf57f10Sderaadt 			if (start > dlines->l_nblines ||
351bbf57f10Sderaadt 			    start < 0 || (*ep != ',' && *ep != 'c'))
3527c5c0dfeSniallo 				fatal("ed_patch_lines");
35301af718aSjoris 
354a777650bSjoris 			if (*ep == ',') {
35501af718aSjoris 				ep++;
35601af718aSjoris 				end = (int)strtol(ep, &ep, 10);
357bbf57f10Sderaadt 				if (end < 0 || *ep != 'c')
3587c5c0dfeSniallo 					fatal("ed_patch_lines");
359a777650bSjoris 			} else {
360a777650bSjoris 				end = start;
36101af718aSjoris 			}
36225d391d7Sjoris 		} else {
36325d391d7Sjoris 			fatal("invalid op %c found while merging", op);
364a777650bSjoris 		}
365a777650bSjoris 
36601af718aSjoris 
36701af718aSjoris 		for (;;) {
36801af718aSjoris 			if (dlp == NULL)
36901af718aSjoris 				break;
37001af718aSjoris 			if (dlp->l_lineno == start)
37101af718aSjoris 				break;
37201af718aSjoris 			if (dlp->l_lineno > start) {
3737a9e6d11Sray 				dlp = TAILQ_PREV(dlp, tqh, l_list);
37401af718aSjoris 			} else if (dlp->l_lineno < start) {
37501af718aSjoris 				ndlp = TAILQ_NEXT(dlp, l_list);
37601af718aSjoris 				if (ndlp->l_lineno > start)
37701af718aSjoris 					break;
37801af718aSjoris 				dlp = ndlp;
37901af718aSjoris 			}
38001af718aSjoris 		}
38101af718aSjoris 
38201af718aSjoris 		if (dlp == NULL)
3837c5c0dfeSniallo 			fatal("ed_patch_lines");
38401af718aSjoris 
385a777650bSjoris 
38601af718aSjoris 		if (op == 'c') {
3877a9e6d11Sray 			insert_after = TAILQ_PREV(dlp, tqh, l_list);
388a777650bSjoris 			for (i = 0; i <= (end - start); i++) {
38901af718aSjoris 				ndlp = TAILQ_NEXT(dlp, l_list);
39001af718aSjoris 				TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
39101af718aSjoris 				dlp = ndlp;
39201af718aSjoris 			}
3931d57a156Sxsa 			dlp = insert_after;
39401af718aSjoris 		}
39501af718aSjoris 
39601af718aSjoris 		if (op == 'a' || op == 'c') {
39701af718aSjoris 			for (;;) {
39801af718aSjoris 				ndlp = lp;
39901af718aSjoris 				lp = TAILQ_NEXT(lp, l_list);
40001af718aSjoris 				if (lp == NULL)
4017c5c0dfeSniallo 					fatal("ed_patch_lines");
40201af718aSjoris 
40325d391d7Sjoris 				if (lp->l_len == 2 &&
40425d391d7Sjoris 				    lp->l_line[0] == '.' &&
40525d391d7Sjoris 				    lp->l_line[1] == '\n')
40601af718aSjoris 					break;
40701af718aSjoris 
40801af718aSjoris 				TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
40901af718aSjoris 				TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
41001af718aSjoris 				    lp, l_list);
41101af718aSjoris 				dlp = lp;
41201af718aSjoris 
41301af718aSjoris 				lp->l_lineno = start;
41401af718aSjoris 				lp = ndlp;
41501af718aSjoris 			}
41601af718aSjoris 		}
41701af718aSjoris 
41801af718aSjoris 		/*
41901af718aSjoris 		 * always resort lines as the markers might be put at the
42001af718aSjoris 		 * same line as we first started editing.
42101af718aSjoris 		 */
42201af718aSjoris 		lineno = 0;
42301af718aSjoris 		TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
42401af718aSjoris 			sort->l_lineno = lineno++;
42501af718aSjoris 		dlines->l_nblines = lineno - 1;
42601af718aSjoris 	}
42701af718aSjoris 
42801af718aSjoris 	return (0);
42901af718aSjoris }
43001af718aSjoris 
43101af718aSjoris /*
43201af718aSjoris  * Pick up the line numbers of all changes from one change file.
43301af718aSjoris  * (This puts the numbers in a vector, which is not strictly necessary,
43401af718aSjoris  * since the vector is processed in one sequential pass.
43501af718aSjoris  * The vector could be optimized out of existence)
43601af718aSjoris  */
437b2dc910bSray static size_t
readin(int fd,struct diff ** dd)4382e0d696aSjoris readin(int fd, struct diff **dd)
43901af718aSjoris {
44001af718aSjoris 	int a, b, c, d;
44101af718aSjoris 	char kind, *p;
44201af718aSjoris 	size_t i;
44301af718aSjoris 
4442e0d696aSjoris 	fp[0] = fdopen(fd, "r");
4452e0d696aSjoris 	if (fp[0] == NULL)
4462e0d696aSjoris 		fatal("readin: fdopen: %s", strerror(errno));
4472e0d696aSjoris 
44801af718aSjoris 	for (i = 0; (p = getchange(fp[0])); i++) {
44901af718aSjoris 		if (i >= szchanges - 1)
45001af718aSjoris 			increase();
45101af718aSjoris 		a = b = number(&p);
45201af718aSjoris 		if (*p == ',') {
45301af718aSjoris 			p++;
45401af718aSjoris 			b = number(&p);
45501af718aSjoris 		}
45601af718aSjoris 		kind = *p++;
45701af718aSjoris 		c = d = number(&p);
45801af718aSjoris 		if (*p==',') {
45901af718aSjoris 			p++;
46001af718aSjoris 			d = number(&p);
46101af718aSjoris 		}
46201af718aSjoris 		if (kind == 'a')
46301af718aSjoris 			a++;
46401af718aSjoris 		if (kind == 'd')
46501af718aSjoris 			c++;
46601af718aSjoris 		b++;
46701af718aSjoris 		d++;
46801af718aSjoris 		(*dd)[i].old.from = a;
46901af718aSjoris 		(*dd)[i].old.to = b;
47001af718aSjoris 		(*dd)[i].new.from = c;
47101af718aSjoris 		(*dd)[i].new.to = d;
47201af718aSjoris 	}
47301af718aSjoris 
47451bd75bfSxsa 	if (i) {
47501af718aSjoris 		(*dd)[i].old.from = (*dd)[i-1].old.to;
47601af718aSjoris 		(*dd)[i].new.from = (*dd)[i-1].new.to;
47751bd75bfSxsa 	}
4782e0d696aSjoris 
47901af718aSjoris 	(void)fclose(fp[0]);
48001af718aSjoris 
48101af718aSjoris 	return (i);
48201af718aSjoris }
48301af718aSjoris 
48401af718aSjoris static int
number(char ** lc)48501af718aSjoris number(char **lc)
48601af718aSjoris {
48701af718aSjoris 	int nn;
48801af718aSjoris 
48901af718aSjoris 	nn = 0;
49001af718aSjoris 	while (isdigit((unsigned char)(**lc)))
49101af718aSjoris 		nn = nn*10 + *(*lc)++ - '0';
49201af718aSjoris 
49301af718aSjoris 	return (nn);
49401af718aSjoris }
49501af718aSjoris 
49601af718aSjoris static char *
getchange(FILE * b)49701af718aSjoris getchange(FILE *b)
49801af718aSjoris {
49901af718aSjoris 	char *line;
50001af718aSjoris 
501f9bbbf45Sfgsch 	while ((line = get_line(b, NULL))) {
50201af718aSjoris 		if (isdigit((unsigned char)line[0]))
50301af718aSjoris 			return (line);
50401af718aSjoris 	}
50501af718aSjoris 
50601af718aSjoris 	return (NULL);
50701af718aSjoris }
50801af718aSjoris 
50901af718aSjoris static char *
get_line(FILE * b,size_t * n)510f9bbbf45Sfgsch get_line(FILE *b, size_t *n)
51101af718aSjoris {
51201af718aSjoris 	char *cp;
51301af718aSjoris 	size_t len;
51401af718aSjoris 	static char *buf;
51501af718aSjoris 	static size_t bufsize;
51601af718aSjoris 
51701af718aSjoris 	if ((cp = fgetln(b, &len)) == NULL)
51801af718aSjoris 		return (NULL);
51901af718aSjoris 
52001af718aSjoris 	if (cp[len - 1] != '\n')
52101af718aSjoris 		len++;
52201af718aSjoris 	if (len + 1 > bufsize) {
52301af718aSjoris 		do {
52401af718aSjoris 			bufsize += 1024;
52501af718aSjoris 		} while (len + 1 > bufsize);
526caa2ffb0Sderaadt 		buf = xreallocarray(buf, 1, bufsize);
52701af718aSjoris 	}
52801af718aSjoris 	memcpy(buf, cp, len - 1);
52901af718aSjoris 	buf[len - 1] = '\n';
53001af718aSjoris 	buf[len] = '\0';
53101af718aSjoris 	if (n != NULL)
53201af718aSjoris 		*n = len;
53301af718aSjoris 
53401af718aSjoris 	return (buf);
53501af718aSjoris }
53601af718aSjoris 
53701af718aSjoris static int
merge(size_t m1,size_t m2)538b2dc910bSray merge(size_t m1, size_t m2)
53901af718aSjoris {
54001af718aSjoris 	struct diff *d1, *d2, *d3;
54101af718aSjoris 	int dpl, j, t1, t2;
54201af718aSjoris 
54301af718aSjoris 	d1 = d13;
54401af718aSjoris 	d2 = d23;
54501af718aSjoris 	j = 0;
5461c690becSmillert 	for (;;) {
5471c690becSmillert 		t1 = (d1 < d13 + m1);
5481c690becSmillert 		t2 = (d2 < d23 + m2);
5491c690becSmillert 		if (!t1 && !t2)
5501c690becSmillert 			break;
5511c690becSmillert 
55201af718aSjoris 		if (debug) {
55301af718aSjoris 			printf("%d,%d=%d,%d %d,%d=%d,%d\n",
55401af718aSjoris 			d1->old.from, d1->old.to,
55501af718aSjoris 			d1->new.from, d1->new.to,
55601af718aSjoris 			d2->old.from, d2->old.to,
55701af718aSjoris 			d2->new.from, d2->new.to);
55801af718aSjoris 		}
55901af718aSjoris 
56001af718aSjoris 		/* first file is different from others */
56101af718aSjoris 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
56201af718aSjoris 			/* stuff peculiar to 1st file */
56301af718aSjoris 			if (eflag==0) {
56401af718aSjoris 				separate("1");
56501af718aSjoris 				change(1, &d1->old, 0);
56601af718aSjoris 				keep(2, &d1->new);
56701af718aSjoris 				change(3, &d1->new, 0);
56801af718aSjoris 			}
56901af718aSjoris 			d1++;
57001af718aSjoris 			continue;
57101af718aSjoris 		}
57201af718aSjoris 
57301af718aSjoris 		/* second file is different from others */
57401af718aSjoris 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
57501af718aSjoris 			if (eflag==0) {
57601af718aSjoris 				separate("2");
57701af718aSjoris 				keep(1, &d2->new);
57801af718aSjoris 				change(2, &d2->old, 0);
57901af718aSjoris 				change(3, &d2->new, 0);
58001af718aSjoris 			}
58101af718aSjoris 			d2++;
58201af718aSjoris 			continue;
58301af718aSjoris 		}
58401af718aSjoris 
58501af718aSjoris 		/*
58601af718aSjoris 		 * Merge overlapping changes in first file
58701af718aSjoris 		 * this happens after extension (see below).
58801af718aSjoris 		 */
58901af718aSjoris 		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
59001af718aSjoris 			d1[1].old.from = d1->old.from;
59101af718aSjoris 			d1[1].new.from = d1->new.from;
59201af718aSjoris 			d1++;
59301af718aSjoris 			continue;
59401af718aSjoris 		}
59501af718aSjoris 
59601af718aSjoris 		/* merge overlapping changes in second */
59701af718aSjoris 		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
59801af718aSjoris 			d2[1].old.from = d2->old.from;
59901af718aSjoris 			d2[1].new.from = d2->new.from;
60001af718aSjoris 			d2++;
60101af718aSjoris 			continue;
60201af718aSjoris 		}
60301af718aSjoris 		/* stuff peculiar to third file or different in all */
60401af718aSjoris 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
60501af718aSjoris 			dpl = duplicate(&d1->old,&d2->old);
60601af718aSjoris 			if (dpl == -1)
60701af718aSjoris 				return (-1);
60801af718aSjoris 
60901af718aSjoris 			/*
61001af718aSjoris 			 * dpl = 0 means all files differ
61101af718aSjoris 			 * dpl = 1 means files 1 and 2 identical
61201af718aSjoris 			 */
61301af718aSjoris 			if (eflag==0) {
61401af718aSjoris 				separate(dpl ? "3" : "");
61501af718aSjoris 				change(1, &d1->old, dpl);
61601af718aSjoris 				change(2, &d2->old, 0);
61701af718aSjoris 				d3 = d1->old.to > d1->old.from ? d1 : d2;
61801af718aSjoris 				change(3, &d3->new, 0);
61901af718aSjoris 			} else
62001af718aSjoris 				j = edit(d1, dpl, j);
62101af718aSjoris 			d1++;
62201af718aSjoris 			d2++;
62301af718aSjoris 			continue;
62401af718aSjoris 		}
62501af718aSjoris 
62601af718aSjoris 		/*
62701af718aSjoris 		 * Overlapping changes from file 1 and 2; extend changes
62801af718aSjoris 		 * appropriately to make them coincide.
62901af718aSjoris 		 */
63001af718aSjoris 		if (d1->new.from < d2->new.from) {
63101af718aSjoris 			d2->old.from -= d2->new.from-d1->new.from;
63201af718aSjoris 			d2->new.from = d1->new.from;
63301af718aSjoris 		} else if (d2->new.from < d1->new.from) {
63401af718aSjoris 			d1->old.from -= d1->new.from-d2->new.from;
63501af718aSjoris 			d1->new.from = d2->new.from;
63601af718aSjoris 		}
63701af718aSjoris 		if (d1->new.to > d2->new.to) {
63801af718aSjoris 			d2->old.to += d1->new.to - d2->new.to;
63901af718aSjoris 			d2->new.to = d1->new.to;
64001af718aSjoris 		} else if (d2->new.to > d1->new.to) {
64101af718aSjoris 			d1->old.to += d2->new.to - d1->new.to;
64201af718aSjoris 			d1->new.to = d2->new.to;
64301af718aSjoris 		}
64401af718aSjoris 	}
64501af718aSjoris 
64601af718aSjoris 	return (edscript(j));
64701af718aSjoris }
64801af718aSjoris 
64901af718aSjoris static void
separate(const char * s)65001af718aSjoris separate(const char *s)
65101af718aSjoris {
65201af718aSjoris 	diff_output("====%s\n", s);
65301af718aSjoris }
65401af718aSjoris 
65501af718aSjoris /*
65601af718aSjoris  * The range of lines rold.from thru rold.to in file i is to be changed.
65701af718aSjoris  * It is to be printed only if it does not duplicate something to be
65801af718aSjoris  * printed later.
65901af718aSjoris  */
66001af718aSjoris static void
change(int i,struct range * rold,int fdup)66101af718aSjoris change(int i, struct range *rold, int fdup)
66201af718aSjoris {
66301af718aSjoris 	diff_output("%d:", i);
66401af718aSjoris 	last[i] = rold->to;
66501af718aSjoris 	prange(rold);
66601af718aSjoris 	if (fdup || debug)
66701af718aSjoris 		return;
66801af718aSjoris 	i--;
66901af718aSjoris 	(void)skip(i, rold->from, NULL);
67001af718aSjoris 	(void)skip(i, rold->to, "  ");
67101af718aSjoris }
67201af718aSjoris 
67301af718aSjoris /*
67401af718aSjoris  * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
67501af718aSjoris  */
67601af718aSjoris static void
prange(struct range * rold)67701af718aSjoris prange(struct range *rold)
67801af718aSjoris {
67901af718aSjoris 	if (rold->to <= rold->from)
68001af718aSjoris 		diff_output("%da\n", rold->from - 1);
68101af718aSjoris 	else {
68201af718aSjoris 		diff_output("%d", rold->from);
68301af718aSjoris 		if (rold->to > rold->from+1)
68401af718aSjoris 			diff_output(",%d", rold->to - 1);
68501af718aSjoris 		diff_output("c\n");
68601af718aSjoris 	}
68701af718aSjoris }
68801af718aSjoris 
68901af718aSjoris /*
69001af718aSjoris  * No difference was reported by diff between file 1 (or 2) and file 3,
69101af718aSjoris  * and an artificial dummy difference (trange) must be ginned up to
69201af718aSjoris  * correspond to the change reported in the other file.
69301af718aSjoris  */
69401af718aSjoris static void
keep(int i,struct range * rnew)69501af718aSjoris keep(int i, struct range *rnew)
69601af718aSjoris {
69701af718aSjoris 	int delta;
69801af718aSjoris 	struct range trange;
69901af718aSjoris 
70001af718aSjoris 	delta = last[3] - last[i];
70101af718aSjoris 	trange.from = rnew->from - delta;
70201af718aSjoris 	trange.to = rnew->to - delta;
70301af718aSjoris 	change(i, &trange, 1);
70401af718aSjoris }
70501af718aSjoris 
70601af718aSjoris /*
70701af718aSjoris  * skip to just before line number from in file "i".  If "pr" is non-NULL,
70801af718aSjoris  * print all skipped stuff with string pr as a prefix.
70901af718aSjoris  */
71001af718aSjoris static int
skip(int i,int from,char * pr)71101af718aSjoris skip(int i, int from, char *pr)
71201af718aSjoris {
71301af718aSjoris 	size_t j, n;
71401af718aSjoris 	char *line;
71501af718aSjoris 
71601af718aSjoris 	for (n = 0; cline[i] < from - 1; n += j) {
717f9bbbf45Sfgsch 		if ((line = get_line(fp[i], &j)) == NULL)
71801af718aSjoris 			return (-1);
71901af718aSjoris 		if (pr != NULL)
72001af718aSjoris 			diff_output("%s%s", pr, line);
72101af718aSjoris 		cline[i]++;
72201af718aSjoris 	}
72301af718aSjoris 	return ((int) n);
72401af718aSjoris }
72501af718aSjoris 
72601af718aSjoris /*
72701af718aSjoris  * Return 1 or 0 according as the old range (in file 1) contains exactly
72801af718aSjoris  * the same data as the new range (in file 2).
72901af718aSjoris  */
73001af718aSjoris static int
duplicate(struct range * r1,struct range * r2)73101af718aSjoris duplicate(struct range *r1, struct range *r2)
73201af718aSjoris {
73301af718aSjoris 	int c,d;
73401af718aSjoris 	int nchar;
73501af718aSjoris 	int nline;
73601af718aSjoris 
73701af718aSjoris 	if (r1->to-r1->from != r2->to-r2->from)
73801af718aSjoris 		return (0);
73901af718aSjoris 	(void)skip(0, r1->from, NULL);
74001af718aSjoris 	(void)skip(1, r2->from, NULL);
74101af718aSjoris 	nchar = 0;
74201af718aSjoris 	for (nline=0; nline < r1->to - r1->from; nline++) {
74301af718aSjoris 		do {
74401af718aSjoris 			c = getc(fp[0]);
74501af718aSjoris 			d = getc(fp[1]);
746*08d4acf6Sstsp 			if (c == -1 && d == -1)
747*08d4acf6Sstsp 				break;
74801af718aSjoris 			if (c == -1 || d== -1)
74901af718aSjoris 				return (-1);
75001af718aSjoris 			nchar++;
75101af718aSjoris 			if (c != d) {
75201af718aSjoris 				repos(nchar);
75301af718aSjoris 				return (0);
75401af718aSjoris 			}
75501af718aSjoris 		} while (c != '\n');
75601af718aSjoris 	}
75701af718aSjoris 	repos(nchar);
75801af718aSjoris 	return (1);
75901af718aSjoris }
76001af718aSjoris 
76101af718aSjoris static void
repos(int nchar)76201af718aSjoris repos(int nchar)
76301af718aSjoris {
76401af718aSjoris 	int i;
76501af718aSjoris 
76601af718aSjoris 	for (i = 0; i < 2; i++)
767a52c79cdStobias 		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
76801af718aSjoris }
76901af718aSjoris 
77001af718aSjoris /*
77101af718aSjoris  * collect an editing script for later regurgitation
77201af718aSjoris  */
77301af718aSjoris static int
edit(struct diff * diff,int fdup,int j)77401af718aSjoris edit(struct diff *diff, int fdup, int j)
77501af718aSjoris {
77601af718aSjoris 	if (((fdup + 1) & eflag) == 0)
77701af718aSjoris 		return (j);
77801af718aSjoris 	j++;
77901af718aSjoris 	overlap[j] = !fdup;
78001af718aSjoris 	if (!fdup)
78101af718aSjoris 		overlapcnt++;
78201af718aSjoris 	de[j].old.from = diff->old.from;
78301af718aSjoris 	de[j].old.to = diff->old.to;
78401af718aSjoris 	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
78501af718aSjoris 	de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
78601af718aSjoris 	return (j);
78701af718aSjoris }
78801af718aSjoris 
78901af718aSjoris /* regurgitate */
79001af718aSjoris static int
edscript(int n)79101af718aSjoris edscript(int n)
79201af718aSjoris {
79301af718aSjoris 	int j, k;
794a777650bSjoris 	char block[BUFSIZ+1];
79501af718aSjoris 
796bd1f1994Smillert 	for (; n > 0; n--) {
79701af718aSjoris 		if (!oflag || !overlap[n])
79801af718aSjoris 			prange(&de[n].old);
79901af718aSjoris 		else
80001af718aSjoris 			diff_output("%da\n=======\n", de[n].old.to -1);
801a52c79cdStobias 		(void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
80201af718aSjoris 		for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
803c0c93af8Sstsp 			size_t r;
80401af718aSjoris 			j = k > BUFSIZ ? BUFSIZ : k;
805c0c93af8Sstsp 			r = fread(block, 1, j, fp[2]);
806c0c93af8Sstsp 			if (r == 0) {
807c0c93af8Sstsp 				if (feof(fp[2]))
808c0c93af8Sstsp 					break;
80901af718aSjoris 				return (-1);
810c0c93af8Sstsp 			}
811c0c93af8Sstsp 			if (r != (size_t)j)
812c0c93af8Sstsp 				j = r;
81301af718aSjoris 			block[j] = '\0';
81401af718aSjoris 			diff_output("%s", block);
81501af718aSjoris 		}
816a777650bSjoris 
81701af718aSjoris 		if (!oflag || !overlap[n])
81801af718aSjoris 			diff_output(".\n");
81901af718aSjoris 		else {
82001af718aSjoris 			diff_output("%s\n.\n", f3mark);
82101af718aSjoris 			diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
82201af718aSjoris 		}
82301af718aSjoris 	}
82401af718aSjoris 
82501af718aSjoris 	return (overlapcnt);
82601af718aSjoris }
82701af718aSjoris 
82801af718aSjoris static void
increase(void)82901af718aSjoris increase(void)
83001af718aSjoris {
83101af718aSjoris 	size_t newsz, incr;
83201af718aSjoris 
83301af718aSjoris 	/* are the memset(3) calls needed? */
83401af718aSjoris 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
83501af718aSjoris 	incr = newsz - szchanges;
83601af718aSjoris 
837caa2ffb0Sderaadt 	d13 = xreallocarray(d13, newsz, sizeof(*d13));
83880566be2Sray 	memset(d13 + szchanges, 0, incr * sizeof(*d13));
839caa2ffb0Sderaadt 	d23 = xreallocarray(d23, newsz, sizeof(*d23));
84080566be2Sray 	memset(d23 + szchanges, 0, incr * sizeof(*d23));
841caa2ffb0Sderaadt 	de = xreallocarray(de, newsz, sizeof(*de));
84280566be2Sray 	memset(de + szchanges, 0, incr * sizeof(*de));
843caa2ffb0Sderaadt 	overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
84480566be2Sray 	memset(overlap + szchanges, 0, incr * sizeof(*overlap));
84501af718aSjoris 	szchanges = newsz;
84601af718aSjoris }
847