GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/rcs/diff3.c Lines: 115 435 26.4 %
Date: 2016-12-06 Branches: 28 246 11.4 %

Line Branch Exec Source
1
/*	$OpenBSD: diff3.c,v 1.38 2015/11/02 16:45:21 nicm 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 <unistd.h>
73
74
#include "diff.h"
75
#include "rcsprog.h"
76
77
/* diff3 - 3-way differential file comparison */
78
79
/* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
80
 *
81
 * d13 = diff report on f1 vs f3
82
 * d23 = diff report on f2 vs f3
83
 * f1, f2, f3 the 3 files
84
 * if changes in f1 overlap with changes in f3, m1 and m3 are used
85
 * to mark the overlaps; otherwise, the file names f1 and f3 are used
86
 * (only for options E and X).
87
 */
88
89
/*
90
 * "from" is first in range of changed lines; "to" is last+1
91
 * from=to=line after point of insertion for added lines.
92
 */
93
struct range {
94
	int from;
95
	int to;
96
};
97
98
struct diff {
99
	struct range old;
100
	struct range new;
101
};
102
103
static size_t szchanges;
104
105
static struct diff *d13;
106
static struct diff *d23;
107
108
/*
109
 * "de" is used to gather editing scripts.  These are later spewed out in
110
 * reverse order.  Its first element must be all zero, the "new" component
111
 * of "de" contains line positions or byte positions depending on when you
112
 * look (!?).  Array overlap indicates which sections in "de" correspond to
113
 * lines that are different in all three files.
114
 */
115
static struct diff *de;
116
static char *overlap;
117
static int overlapcnt = 0;
118
static FILE *fp[3];
119
static int cline[3];		/* # of the last-read line in each file (0-2) */
120
121
/*
122
 * the latest known correspondence between line numbers of the 3 files
123
 * is stored in last[1-3];
124
 */
125
static int last[4];
126
static int eflag = 3;	/* default -E for compatibility with former RCS */
127
static int oflag = 1;	/* default -E for compatibility with former RCS */
128
static int debug  = 0;
129
static char f1mark[PATH_MAX], f3mark[PATH_MAX];	/* markers for -E and -X */
130
131
static int duplicate(struct range *, struct range *);
132
static int edit(struct diff *, int, int);
133
static char *getchange(FILE *);
134
static char *get_line(FILE *, size_t *);
135
static int number(char **);
136
static ssize_t readin(char *, struct diff **);
137
static int skip(int, int, char *);
138
static int edscript(int);
139
static int merge(size_t, size_t);
140
static void change(int, struct range *, int);
141
static void keep(int, struct range *);
142
static void prange(struct range *);
143
static void repos(int);
144
static void separate(const char *);
145
static void increase(void);
146
static int diff3_internal(int, char **, const char *, const char *);
147
148
int diff3_conflicts = 0;
149
150
/*
151
 * For merge(1).
152
 */
153
BUF *
154
merge_diff3(char **av, int flags)
155
1
{
156
	int argc;
157
	char *argv[5], *dp13, *dp23, *path1, *path2, *path3;
158
	BUF *b1, *b2, *b3, *d1, *d2, *diffb;
159
	u_char *data, *patch;
160
	size_t dlen, plen;
161
162
1
	b1 = b2 = b3 = d1 = d2 = diffb = NULL;
163
1
	dp13 = dp23 = path1 = path2 = path3 = NULL;
164
1
	data = patch = NULL;
165
166
1
	if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
167
		oflag = 0;
168
169
1
	if ((b1 = buf_load(av[0])) == NULL)
170
		goto out;
171
1
	if ((b2 = buf_load(av[1])) == NULL)
172
		goto out;
173
1
	if ((b3 = buf_load(av[2])) == NULL)
174
		goto out;
175
176
1
	d1 = buf_alloc(128);
177
1
	d2 = buf_alloc(128);
178
1
	diffb = buf_alloc(128);
179
180
1
	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
181
1
	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
182
1
	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
183
184
1
	buf_write_stmp(b1, path1);
185
1
	buf_write_stmp(b2, path2);
186
1
	buf_write_stmp(b3, path3);
187
188
1
	buf_free(b2);
189
1
	b2 = NULL;
190
191

1
	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
192
	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
193
		buf_free(diffb);
194
		diffb = NULL;
195
		goto out;
196
	}
197
198
1
	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
199
1
	buf_write_stmp(d1, dp13);
200
201
1
	buf_free(d1);
202
1
	d1 = NULL;
203
204
1
	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
205
1
	buf_write_stmp(d2, dp23);
206
207
1
	buf_free(d2);
208
1
	d2 = NULL;
209
210
1
	argc = 0;
211
1
	diffbuf = diffb;
212
1
	argv[argc++] = dp13;
213
1
	argv[argc++] = dp23;
214
1
	argv[argc++] = path1;
215
1
	argv[argc++] = path2;
216
1
	argv[argc++] = path3;
217
218
1
	diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
219
1
	if (diff3_conflicts < 0) {
220
		buf_free(diffb);
221
		diffb = NULL;
222
		goto out;
223
	}
224
225
1
	plen = buf_len(diffb);
226
1
	patch = buf_release(diffb);
227
1
	dlen = buf_len(b1);
228
1
	data = buf_release(b1);
229
230
1
	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
231
		goto out;
232
233

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