GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/rcs/diff3.c Lines: 114 416 27.4 %
Date: 2017-11-13 Branches: 26 242 10.7 %

Line Branch Exec Source
1
/*	$OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $	*/
2
3
/*
4
 * Copyright (C) Caldera International Inc.  2001-2002.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code and documentation must retain the above
11
 *    copyright notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. All advertising materials mentioning features or use of this software
16
 *    must display the following acknowledgement:
17
 *	This product includes software developed or owned by Caldera
18
 *	International, Inc.
19
 * 4. Neither the name of Caldera International, Inc. nor the names of other
20
 *    contributors may be used to endorse or promote products derived from
21
 *    this software without specific prior written permission.
22
 *
23
 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24
 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27
 * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28
 * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
/*-
37
 * Copyright (c) 1991, 1993
38
 *	The Regents of the University of California.  All rights reserved.
39
 *
40
 * Redistribution and use in source and binary forms, with or without
41
 * modification, are permitted provided that the following conditions
42
 * are met:
43
 * 1. Redistributions of source code must retain the above copyright
44
 *    notice, this list of conditions and the following disclaimer.
45
 * 2. Redistributions in binary form must reproduce the above copyright
46
 *    notice, this list of conditions and the following disclaimer in the
47
 *    documentation and/or other materials provided with the distribution.
48
 * 3. Neither the name of the University nor the names of its contributors
49
 *    may be used to endorse or promote products derived from this software
50
 *    without specific prior written permission.
51
 *
52
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62
 * SUCH DAMAGE.
63
 *
64
 *	@(#)diff3.c	8.1 (Berkeley) 6/6/93
65
 */
66
67
#include <ctype.h>
68
#include <err.h>
69
#include <stdio.h>
70
#include <stdlib.h>
71
#include <string.h>
72
#include <time.h>
73
#include <unistd.h>
74
75
#include "diff.h"
76
#include "rcsprog.h"
77
78
/* diff3 - 3-way differential file comparison */
79
80
/* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
81
 *
82
 * d13 = diff report on f1 vs f3
83
 * d23 = diff report on f2 vs f3
84
 * f1, f2, f3 the 3 files
85
 * if changes in f1 overlap with changes in f3, m1 and m3 are used
86
 * to mark the overlaps; otherwise, the file names f1 and f3 are used
87
 * (only for options E and X).
88
 */
89
90
/*
91
 * "from" is first in range of changed lines; "to" is last+1
92
 * from=to=line after point of insertion for added lines.
93
 */
94
struct range {
95
	int from;
96
	int to;
97
};
98
99
struct diff {
100
	struct range old;
101
	struct range new;
102
};
103
104
static size_t szchanges;
105
106
static struct diff *d13;
107
static struct diff *d23;
108
109
/*
110
 * "de" is used to gather editing scripts.  These are later spewed out in
111
 * reverse order.  Its first element must be all zero, the "new" component
112
 * of "de" contains line positions or byte positions depending on when you
113
 * look (!?).  Array overlap indicates which sections in "de" correspond to
114
 * lines that are different in all three files.
115
 */
116
static struct diff *de;
117
static char *overlap;
118
static int overlapcnt = 0;
119
static FILE *fp[3];
120
static int cline[3];		/* # of the last-read line in each file (0-2) */
121
122
/*
123
 * the latest known correspondence between line numbers of the 3 files
124
 * is stored in last[1-3];
125
 */
126
static int last[4];
127
static int eflag = 3;	/* default -E for compatibility with former RCS */
128
static int oflag = 1;	/* default -E for compatibility with former RCS */
129
static int debug  = 0;
130
static char f1mark[PATH_MAX], f3mark[PATH_MAX];	/* markers for -E and -X */
131
132
static int duplicate(struct range *, struct range *);
133
static int edit(struct diff *, int, int);
134
static char *getchange(FILE *);
135
static char *get_line(FILE *, size_t *);
136
static int number(char **);
137
static ssize_t readin(char *, struct diff **);
138
static int skip(int, int, char *);
139
static int edscript(int);
140
static int merge(size_t, size_t);
141
static void change(int, struct range *, int);
142
static void keep(int, struct range *);
143
static void prange(struct range *);
144
static void repos(int);
145
static void separate(const char *);
146
static void increase(void);
147
static int diff3_internal(int, char **, const char *, const char *);
148
149
int diff3_conflicts = 0;
150
151
/*
152
 * For merge(1).
153
 */
154
BUF *
155
merge_diff3(char **av, int flags)
156
{
157
	int argc;
158
10
	char *argv[5], *dp13, *dp23, *path1, *path2, *path3;
159
	BUF *b1, *b2, *b3, *d1, *d2, *diffb;
160
	u_char *data, *patch;
161
	size_t dlen, plen;
162
163
	b1 = b2 = b3 = d1 = d2 = diffb = NULL;
164
5
	dp13 = dp23 = path1 = path2 = path3 = NULL;
165
	data = patch = NULL;
166
167

5
	if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
168
		oflag = 0;
169
170
5
	if ((b1 = buf_load(av[0])) == NULL)
171
		goto out;
172
5
	if ((b2 = buf_load(av[1])) == NULL)
173
		goto out;
174
5
	if ((b3 = buf_load(av[2])) == NULL)
175
		goto out;
176
177
5
	d1 = buf_alloc(128);
178
5
	d2 = buf_alloc(128);
179
5
	diffb = buf_alloc(128);
180
181
5
	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
182
5
	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
183
5
	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
184
185
5
	buf_write_stmp(b1, path1);
186
5
	buf_write_stmp(b2, path2);
187
5
	buf_write_stmp(b3, path3);
188
189
5
	buf_free(b2);
190
	b2 = NULL;
191
192

10
	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
193
5
	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
194
		buf_free(diffb);
195
		diffb = NULL;
196
		goto out;
197
	}
198
199
5
	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
200
5
	buf_write_stmp(d1, dp13);
201
202
5
	buf_free(d1);
203
	d1 = NULL;
204
205
5
	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
206
5
	buf_write_stmp(d2, dp23);
207
208
5
	buf_free(d2);
209
	d2 = NULL;
210
211
	argc = 0;
212
5
	diffbuf = diffb;
213
5
	argv[argc++] = dp13;
214
5
	argv[argc++] = dp23;
215
5
	argv[argc++] = path1;
216
5
	argv[argc++] = path2;
217
5
	argv[argc++] = path3;
218
219
5
	diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
220
5
	if (diff3_conflicts < 0) {
221
		buf_free(diffb);
222
		diffb = NULL;
223
		goto out;
224
	}
225
226
5
	plen = buf_len(diffb);
227
5
	patch = buf_release(diffb);
228
5
	dlen = buf_len(b1);
229
5
	data = buf_release(b1);
230
231
5
	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
232
		goto out;
233
234
5
	if (!(flags & QUIET) && diff3_conflicts != 0)
235
		warnx("warning: overlaps or other problems during merge");
236
237
out:
238
5
	buf_free(b2);
239
5
	buf_free(b3);
240
5
	buf_free(d1);
241
5
	buf_free(d2);
242
243
5
	(void)unlink(path1);
244
5
	(void)unlink(path2);
245
5
	(void)unlink(path3);
246
5
	(void)unlink(dp13);
247
5
	(void)unlink(dp23);
248
249
5
	free(path1);
250
5
	free(path2);
251
5
	free(path3);
252
5
	free(dp13);
253
5
	free(dp23);
254
5
	free(data);
255
5
	free(patch);
256
257
5
	return (diffb);
258
5
}
259
260
BUF *
261
rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags)
262
{
263
	int argc;
264
	char *argv[5], r1[RCS_REV_BUFSZ], r2[RCS_REV_BUFSZ];
265
	char *dp13, *dp23, *path1, *path2, *path3;
266
	BUF *b1, *b2, *b3, *d1, *d2, *diffb;
267
	size_t dlen, plen;
268
	u_char *data, *patch;
269
270
	b1 = b2 = b3 = d1 = d2 = diffb = NULL;
271
	dp13 = dp23 = path1 = path2 = path3 = NULL;
272
	data = patch = NULL;
273
274
	if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
275
		oflag = 0;
276
277
	rcsnum_tostr(rev1, r1, sizeof(r1));
278
	rcsnum_tostr(rev2, r2, sizeof(r2));
279
280
	if ((b1 = buf_load(workfile)) == NULL)
281
		goto out;
282
283
	if (!(flags & QUIET))
284
		(void)fprintf(stderr, "retrieving revision %s\n", r1);
285
	if ((b2 = rcs_getrev(rf, rev1)) == NULL)
286
		goto out;
287
288
	if (!(flags & QUIET))
289
		(void)fprintf(stderr, "retrieving revision %s\n", r2);
290
	if ((b3 = rcs_getrev(rf, rev2)) == NULL)
291
		goto out;
292
293
	d1 = buf_alloc(128);
294
	d2 = buf_alloc(128);
295
	diffb = buf_alloc(128);
296
297
	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
298
	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
299
	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
300
301
	buf_write_stmp(b1, path1);
302
	buf_write_stmp(b2, path2);
303
	buf_write_stmp(b3, path3);
304
305
	buf_free(b2);
306
	b2 = NULL;
307
308
	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
309
	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
310
		buf_free(diffb);
311
		diffb = NULL;
312
		goto out;
313
	}
314
315
	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
316
	buf_write_stmp(d1, dp13);
317
318
	buf_free(d1);
319
	d1 = NULL;
320
321
	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
322
	buf_write_stmp(d2, dp23);
323
324
	buf_free(d2);
325
	d2 = NULL;
326
327
	argc = 0;
328
	diffbuf = diffb;
329
	argv[argc++] = dp13;
330
	argv[argc++] = dp23;
331
	argv[argc++] = path1;
332
	argv[argc++] = path2;
333
	argv[argc++] = path3;
334
335
	diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
336
	if (diff3_conflicts < 0) {
337
		buf_free(diffb);
338
		diffb = NULL;
339
		goto out;
340
	}
341
342
	plen = buf_len(diffb);
343
	patch = buf_release(diffb);
344
	dlen = buf_len(b1);
345
	data = buf_release(b1);
346
347
	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
348
		goto out;
349
350
	if (!(flags & QUIET) && diff3_conflicts != 0)
351
		warnx("warning: overlaps or other problems during merge");
352
353
out:
354
	buf_free(b2);
355
	buf_free(b3);
356
	buf_free(d1);
357
	buf_free(d2);
358
359
	(void)unlink(path1);
360
	(void)unlink(path2);
361
	(void)unlink(path3);
362
	(void)unlink(dp13);
363
	(void)unlink(dp23);
364
365
	free(path1);
366
	free(path2);
367
	free(path3);
368
	free(dp13);
369
	free(dp23);
370
	free(data);
371
	free(patch);
372
373
	return (diffb);
374
}
375
376
static int
377
diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
378
{
379
	ssize_t m, n;
380
	int i;
381
382
10
	if (argc < 5)
383
		return (-1);
384
385
5
	if (oflag) {
386
5
		i = snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
387
5
		if (i < 0 || i >= (int)sizeof(f1mark))
388
			errx(1, "diff3_internal: string truncated");
389
390
5
		i = snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
391
5
		if (i < 0 || i >= (int)sizeof(f3mark))
392
			errx(1, "diff3_internal: string truncated");
393
	}
394
395
5
	increase();
396
5
	if ((m = readin(argv[0], &d13)) < 0) {
397
		warn("%s", argv[0]);
398
		return (-1);
399
	}
400
5
	if ((n = readin(argv[1], &d23)) < 0) {
401
		warn("%s", argv[1]);
402
		return (-1);
403
	}
404
405
40
	for (i = 0; i <= 2; i++)
406
15
		if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
407
			warn("%s", argv[i + 2]);
408
			return (-1);
409
		}
410
411
5
	return (merge(m, n));
412
5
}
413
414
int
415
ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
416
{
417
10
	char op, *ep;
418
	struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
419
	int start, end, i, lineno;
420
	u_char tmp;
421
422
5
	dlp = TAILQ_FIRST(&(dlines->l_lines));
423
5
	lp = TAILQ_FIRST(&(plines->l_lines));
424
425
	end = 0;
426
10
	for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
427
	    lp = TAILQ_NEXT(lp, l_list)) {
428
		/* Skip blank lines */
429
		if (lp->l_len < 2)
430
			continue;
431
432
		/* NUL-terminate line buffer for strtol() safety. */
433
		tmp = lp->l_line[lp->l_len - 1];
434
		lp->l_line[lp->l_len - 1] = '\0';
435
436
		/* len - 1 is NUL terminator so we use len - 2 for 'op' */
437
		op = lp->l_line[lp->l_len - 2];
438
		start = (int)strtol(lp->l_line, &ep, 10);
439
440
		/* Restore the last byte of the buffer */
441
		lp->l_line[lp->l_len - 1] = tmp;
442
443
		if (op == 'a') {
444
			if (start > dlines->l_nblines ||
445
			    start < 0 || *ep != 'a')
446
				errx(1, "ed_patch_lines");
447
		} else if (op == 'c') {
448
			if (start > dlines->l_nblines ||
449
			    start < 0 || (*ep != ',' && *ep != 'c'))
450
				errx(1, "ed_patch_lines");
451
452
			if (*ep == ',') {
453
				ep++;
454
				end = (int)strtol(ep, &ep, 10);
455
				if (end < 0 || *ep != 'c')
456
					errx(1, "ed_patch_lines");
457
			} else {
458
				end = start;
459
			}
460
		}
461
462
463
		for (;;) {
464
			if (dlp == NULL)
465
				break;
466
			if (dlp->l_lineno == start)
467
				break;
468
			if (dlp->l_lineno > start) {
469
				dlp = TAILQ_PREV(dlp, tqh, l_list);
470
			} else if (dlp->l_lineno < start) {
471
				ndlp = TAILQ_NEXT(dlp, l_list);
472
				if (ndlp->l_lineno > start)
473
					break;
474
				dlp = ndlp;
475
			}
476
		}
477
478
		if (dlp == NULL)
479
			errx(1, "ed_patch_lines");
480
481
482
		if (op == 'c') {
483
			insert_after = TAILQ_PREV(dlp, tqh, l_list);
484
			for (i = 0; i <= (end - start); i++) {
485
				ndlp = TAILQ_NEXT(dlp, l_list);
486
				TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
487
				dlp = ndlp;
488
			}
489
			dlp = insert_after;
490
		}
491
492
		if (op == 'a' || op == 'c') {
493
			for (;;) {
494
				ndlp = lp;
495
				lp = TAILQ_NEXT(lp, l_list);
496
				if (lp == NULL)
497
					errx(1, "ed_patch_lines");
498
499
				if (!memcmp(lp->l_line, ".", 1))
500
					break;
501
502
				TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
503
				TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
504
				    lp, l_list);
505
				dlp = lp;
506
507
				lp->l_lineno = start;
508
				lp = ndlp;
509
			}
510
		}
511
512
		/*
513
		 * always resort lines as the markers might be put at the
514
		 * same line as we first started editing.
515
		 */
516
		lineno = 0;
517
		TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
518
			sort->l_lineno = lineno++;
519
		dlines->l_nblines = lineno - 1;
520
	}
521
522
5
	return (0);
523
5
}
524
525
/*
526
 * Pick up the line numbers of all changes from one change file.
527
 * (This puts the numbers in a vector, which is not strictly necessary,
528
 * since the vector is processed in one sequential pass.
529
 * The vector could be optimized out of existence)
530
 */
531
static ssize_t
532
readin(char *name, struct diff **dd)
533
{
534
	int a, b, c, d;
535
20
	char kind, *p;
536
	size_t i;
537
538
10
	fp[0] = fopen(name, "r");
539
10
	if (fp[0] == NULL)
540
		return (-1);
541
20
	for (i = 0; (p = getchange(fp[0])); i++) {
542
		if (i >= szchanges - 1)
543
			increase();
544
		a = b = number(&p);
545
		if (*p == ',') {
546
			p++;
547
			b = number(&p);
548
		}
549
		kind = *p++;
550
		c = d = number(&p);
551
		if (*p==',') {
552
			p++;
553
			d = number(&p);
554
		}
555
		if (kind == 'a')
556
			a++;
557
		if (kind == 'd')
558
			c++;
559
		b++;
560
		d++;
561
		(*dd)[i].old.from = a;
562
		(*dd)[i].old.to = b;
563
		(*dd)[i].new.from = c;
564
		(*dd)[i].new.to = d;
565
	}
566
567
10
	if (i) {
568
		(*dd)[i].old.from = (*dd)[i-1].old.to;
569
		(*dd)[i].new.from = (*dd)[i-1].new.to;
570
	}
571
572
10
	(void)fclose(fp[0]);
573
574
10
	return (i);
575
10
}
576
577
static int
578
number(char **lc)
579
{
580
	int nn;
581
582
	nn = 0;
583
	while (isdigit((unsigned char)(**lc)))
584
		nn = nn*10 + *(*lc)++ - '0';
585
586
	return (nn);
587
}
588
589
static char *
590
getchange(FILE *b)
591
{
592
	char *line;
593
594
30
	while ((line = get_line(b, NULL))) {
595
		if (isdigit((unsigned char)line[0]))
596
			return (line);
597
	}
598
599
10
	return (NULL);
600
10
}
601
602
static char *
603
get_line(FILE *b, size_t *n)
604
{
605
	char *cp;
606
20
	size_t len;
607
	static char *buf;
608
	static size_t bufsize;
609
610
10
	if ((cp = fgetln(b, &len)) == NULL)
611
10
		return (NULL);
612
613
	if (cp[len - 1] != '\n')
614
		len++;
615
	if (len + 1 > bufsize) {
616
		do {
617
			bufsize += 1024;
618
		} while (len + 1 > bufsize);
619
		buf = xreallocarray(buf, 1, bufsize);
620
	}
621
	memcpy(buf, cp, len - 1);
622
	buf[len - 1] = '\n';
623
	buf[len] = '\0';
624
	if (n != NULL)
625
		*n = len;
626
627
	return (buf);
628
10
}
629
630
static int
631
merge(size_t m1, size_t m2)
632
{
633
	struct diff *d1, *d2, *d3;
634
	int dpl, j, t1, t2;
635
636
10
	d1 = d13;
637
5
	d2 = d23;
638
	j = 0;
639
5
	for (;;) {
640
5
		t1 = (d1 < d13 + m1);
641
5
		t2 = (d2 < d23 + m2);
642
5
		if (!t1 && !t2)
643
			break;
644
645
		if (debug) {
646
			printf("%d,%d=%d,%d %d,%d=%d,%d\n",
647
			d1->old.from, d1->old.to,
648
			d1->new.from, d1->new.to,
649
			d2->old.from, d2->old.to,
650
			d2->new.from, d2->new.to);
651
		}
652
653
		/* first file is different from others */
654
		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
655
			/* stuff peculiar to 1st file */
656
			if (eflag==0) {
657
				separate("1");
658
				change(1, &d1->old, 0);
659
				keep(2, &d1->new);
660
				change(3, &d1->new, 0);
661
			}
662
			d1++;
663
			continue;
664
		}
665
666
		/* second file is different from others */
667
		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
668
			if (eflag==0) {
669
				separate("2");
670
				keep(1, &d2->new);
671
				change(2, &d2->old, 0);
672
				change(3, &d2->new, 0);
673
			}
674
			d2++;
675
			continue;
676
		}
677
678
		/*
679
		 * Merge overlapping changes in first file
680
		 * this happens after extension (see below).
681
		 */
682
		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
683
			d1[1].old.from = d1->old.from;
684
			d1[1].new.from = d1->new.from;
685
			d1++;
686
			continue;
687
		}
688
689
		/* merge overlapping changes in second */
690
		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
691
			d2[1].old.from = d2->old.from;
692
			d2[1].new.from = d2->new.from;
693
			d2++;
694
			continue;
695
		}
696
		/* stuff peculiar to third file or different in all */
697
		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
698
			dpl = duplicate(&d1->old,&d2->old);
699
			if (dpl == -1)
700
				return (-1);
701
702
			/*
703
			 * dpl = 0 means all files differ
704
			 * dpl = 1 means files 1 and 2 identical
705
			 */
706
			if (eflag==0) {
707
				separate(dpl ? "3" : "");
708
				change(1, &d1->old, dpl);
709
				change(2, &d2->old, 0);
710
				d3 = d1->old.to > d1->old.from ? d1 : d2;
711
				change(3, &d3->new, 0);
712
			} else
713
				j = edit(d1, dpl, j);
714
			d1++;
715
			d2++;
716
			continue;
717
		}
718
719
		/*
720
		 * Overlapping changes from file 1 and 2; extend changes
721
		 * appropriately to make them coincide.
722
		 */
723
		if (d1->new.from < d2->new.from) {
724
			d2->old.from -= d2->new.from-d1->new.from;
725
			d2->new.from = d1->new.from;
726
		} else if (d2->new.from < d1->new.from) {
727
			d1->old.from -= d1->new.from-d2->new.from;
728
			d1->new.from = d2->new.from;
729
		}
730
		if (d1->new.to > d2->new.to) {
731
			d2->old.to += d1->new.to - d2->new.to;
732
			d2->new.to = d1->new.to;
733
		} else if (d2->new.to > d1->new.to) {
734
			d1->old.to += d2->new.to - d1->new.to;
735
			d1->new.to = d2->new.to;
736
		}
737
	}
738
739
5
	return (edscript(j));
740
5
}
741
742
static void
743
separate(const char *s)
744
{
745
	diff_output("====%s\n", s);
746
}
747
748
/*
749
 * The range of lines rold.from thru rold.to in file i is to be changed.
750
 * It is to be printed only if it does not duplicate something to be
751
 * printed later.
752
 */
753
static void
754
change(int i, struct range *rold, int fdup)
755
{
756
	diff_output("%d:", i);
757
	last[i] = rold->to;
758
	prange(rold);
759
	if (fdup || debug)
760
		return;
761
	i--;
762
	(void)skip(i, rold->from, NULL);
763
	(void)skip(i, rold->to, "  ");
764
}
765
766
/*
767
 * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
768
 */
769
static void
770
prange(struct range *rold)
771
{
772
	if (rold->to <= rold->from)
773
		diff_output("%da\n", rold->from - 1);
774
	else {
775
		diff_output("%d", rold->from);
776
		if (rold->to > rold->from+1)
777
			diff_output(",%d", rold->to - 1);
778
		diff_output("c\n");
779
	}
780
}
781
782
/*
783
 * No difference was reported by diff between file 1 (or 2) and file 3,
784
 * and an artificial dummy difference (trange) must be ginned up to
785
 * correspond to the change reported in the other file.
786
 */
787
static void
788
keep(int i, struct range *rnew)
789
{
790
	int delta;
791
	struct range trange;
792
793
	delta = last[3] - last[i];
794
	trange.from = rnew->from - delta;
795
	trange.to = rnew->to - delta;
796
	change(i, &trange, 1);
797
}
798
799
/*
800
 * skip to just before line number from in file "i".  If "pr" is non-NULL,
801
 * print all skipped stuff with string pr as a prefix.
802
 */
803
static int
804
skip(int i, int from, char *pr)
805
{
806
	size_t j, n;
807
	char *line;
808
809
	for (n = 0; cline[i] < from - 1; n += j) {
810
		if ((line = get_line(fp[i], &j)) == NULL)
811
			return (-1);
812
		if (pr != NULL)
813
			diff_output("%s%s", pr, line);
814
		cline[i]++;
815
	}
816
	return ((int) n);
817
}
818
819
/*
820
 * Return 1 or 0 according as the old range (in file 1) contains exactly
821
 * the same data as the new range (in file 2).
822
 */
823
static int
824
duplicate(struct range *r1, struct range *r2)
825
{
826
	int c,d;
827
	int nchar;
828
	int nline;
829
830
	if (r1->to-r1->from != r2->to-r2->from)
831
		return (0);
832
	(void)skip(0, r1->from, NULL);
833
	(void)skip(1, r2->from, NULL);
834
	nchar = 0;
835
	for (nline=0; nline < r1->to - r1->from; nline++) {
836
		do {
837
			c = getc(fp[0]);
838
			d = getc(fp[1]);
839
			if (c == -1 || d== -1)
840
				return (-1);
841
			nchar++;
842
			if (c != d) {
843
				repos(nchar);
844
				return (0);
845
			}
846
		} while (c != '\n');
847
	}
848
	repos(nchar);
849
	return (1);
850
}
851
852
static void
853
repos(int nchar)
854
{
855
	int i;
856
857
	for (i = 0; i < 2; i++)
858
		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
859
}
860
861
/*
862
 * collect an editing script for later regurgitation
863
 */
864
static int
865
edit(struct diff *diff, int fdup, int j)
866
{
867
	if (((fdup + 1) & eflag) == 0)
868
		return (j);
869
	j++;
870
	overlap[j] = !fdup;
871
	if (!fdup)
872
		overlapcnt++;
873
	de[j].old.from = diff->old.from;
874
	de[j].old.to = diff->old.to;
875
	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
876
	de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
877
	return (j);
878
}
879
880
/* regurgitate */
881
static int
882
edscript(int n)
883
{
884
	int j, k;
885
10
	char block[BUFSIZ+1];
886
887
10
	for (; n > 0; n--) {
888
		if (!oflag || !overlap[n])
889
			prange(&de[n].old);
890
		else
891
			diff_output("%da\n=======\n", de[n].old.to -1);
892
		(void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
893
		for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
894
			j = k > BUFSIZ ? BUFSIZ : k;
895
			if (fread(block, 1, j, fp[2]) != (size_t)j)
896
				return (-1);
897
			block[j] = '\0';
898
			diff_output("%s", block);
899
		}
900
901
		if (!oflag || !overlap[n])
902
			diff_output(".\n");
903
		else {
904
			diff_output("%s\n.\n", f3mark);
905
			diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
906
		}
907
	}
908
909
5
	return (overlapcnt);
910
5
}
911
912
static void
913
increase(void)
914
{
915
	size_t newsz, incr;
916
917
	/* are the memset(3) calls needed? */
918
10
	newsz = szchanges == 0 ? 64 : 2 * szchanges;
919
5
	incr = newsz - szchanges;
920
921
5
	d13 = xreallocarray(d13, newsz, sizeof(*d13));
922
5
	memset(d13 + szchanges, 0, incr * sizeof(*d13));
923
5
	d23 = xreallocarray(d23, newsz, sizeof(*d23));
924
5
	memset(d23 + szchanges, 0, incr * sizeof(*d23));
925
5
	de = xreallocarray(de, newsz, sizeof(*de));
926
5
	memset(de + szchanges, 0, incr * sizeof(*de));
927
5
	overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
928
5
	memset(overlap + szchanges, 0, incr * sizeof(*overlap));
929
5
	szchanges = newsz;
930
5
}