GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/rcs/diff3.c Lines: 299 423 70.7 %
Date: 2017-11-07 Branches: 116 242 47.9 %

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
20
	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
10
	dp13 = dp23 = path1 = path2 = path3 = NULL;
165
	data = patch = NULL;
166
167

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

20
	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
193
10
	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
194
		buf_free(diffb);
195
		diffb = NULL;
196
		goto out;
197
	}
198
199
10
	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
200
10
	buf_write_stmp(d1, dp13);
201
202
10
	buf_free(d1);
203
	d1 = NULL;
204
205
10
	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
206
10
	buf_write_stmp(d2, dp23);
207
208
10
	buf_free(d2);
209
	d2 = NULL;
210
211
	argc = 0;
212
10
	diffbuf = diffb;
213
10
	argv[argc++] = dp13;
214
10
	argv[argc++] = dp23;
215
10
	argv[argc++] = path1;
216
10
	argv[argc++] = path2;
217
10
	argv[argc++] = path3;
218
219
10
	diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
220
10
	if (diff3_conflicts < 0) {
221
		buf_free(diffb);
222
		diffb = NULL;
223
		goto out;
224
	}
225
226
10
	plen = buf_len(diffb);
227
10
	patch = buf_release(diffb);
228
10
	dlen = buf_len(b1);
229
10
	data = buf_release(b1);
230
231
10
	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
232
		goto out;
233
234
10
	if (!(flags & QUIET) && diff3_conflicts != 0)
235
		warnx("warning: overlaps or other problems during merge");
236
237
out:
238
10
	buf_free(b2);
239
10
	buf_free(b3);
240
10
	buf_free(d1);
241
10
	buf_free(d2);
242
243
10
	(void)unlink(path1);
244
10
	(void)unlink(path2);
245
10
	(void)unlink(path3);
246
10
	(void)unlink(dp13);
247
10
	(void)unlink(dp23);
248
249
10
	free(path1);
250
10
	free(path2);
251
10
	free(path3);
252
10
	free(dp13);
253
10
	free(dp23);
254
10
	free(data);
255
10
	free(patch);
256
257
10
	return (diffb);
258
10
}
259
260
BUF *
261
rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags)
262
{
263
	int argc;
264
4
	char *argv[5], r1[RCS_REV_BUFSZ], r2[RCS_REV_BUFSZ];
265
2
	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
2
	dp13 = dp23 = path1 = path2 = path3 = NULL;
272
	data = patch = NULL;
273
274

2
	if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
275
		oflag = 0;
276
277
2
	rcsnum_tostr(rev1, r1, sizeof(r1));
278
2
	rcsnum_tostr(rev2, r2, sizeof(r2));
279
280
2
	if ((b1 = buf_load(workfile)) == NULL)
281
		goto out;
282
283
2
	if (!(flags & QUIET))
284
		(void)fprintf(stderr, "retrieving revision %s\n", r1);
285
2
	if ((b2 = rcs_getrev(rf, rev1)) == NULL)
286
		goto out;
287
288
2
	if (!(flags & QUIET))
289
		(void)fprintf(stderr, "retrieving revision %s\n", r2);
290
2
	if ((b3 = rcs_getrev(rf, rev2)) == NULL)
291
		goto out;
292
293
2
	d1 = buf_alloc(128);
294
2
	d2 = buf_alloc(128);
295
2
	diffb = buf_alloc(128);
296
297
2
	(void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
298
2
	(void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
299
2
	(void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
300
301
2
	buf_write_stmp(b1, path1);
302
2
	buf_write_stmp(b2, path2);
303
2
	buf_write_stmp(b3, path3);
304
305
2
	buf_free(b2);
306
	b2 = NULL;
307
308

4
	if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
309
2
	    (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
310
		buf_free(diffb);
311
		diffb = NULL;
312
		goto out;
313
	}
314
315
2
	(void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
316
2
	buf_write_stmp(d1, dp13);
317
318
2
	buf_free(d1);
319
	d1 = NULL;
320
321
2
	(void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
322
2
	buf_write_stmp(d2, dp23);
323
324
2
	buf_free(d2);
325
	d2 = NULL;
326
327
	argc = 0;
328
2
	diffbuf = diffb;
329
2
	argv[argc++] = dp13;
330
2
	argv[argc++] = dp23;
331
2
	argv[argc++] = path1;
332
2
	argv[argc++] = path2;
333
2
	argv[argc++] = path3;
334
335
2
	diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
336
2
	if (diff3_conflicts < 0) {
337
		buf_free(diffb);
338
		diffb = NULL;
339
		goto out;
340
	}
341
342
2
	plen = buf_len(diffb);
343
2
	patch = buf_release(diffb);
344
2
	dlen = buf_len(b1);
345
2
	data = buf_release(b1);
346
347
2
	if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
348
		goto out;
349
350
2
	if (!(flags & QUIET) && diff3_conflicts != 0)
351
		warnx("warning: overlaps or other problems during merge");
352
353
out:
354
2
	buf_free(b2);
355
2
	buf_free(b3);
356
2
	buf_free(d1);
357
2
	buf_free(d2);
358
359
2
	(void)unlink(path1);
360
2
	(void)unlink(path2);
361
2
	(void)unlink(path3);
362
2
	(void)unlink(dp13);
363
2
	(void)unlink(dp23);
364
365
2
	free(path1);
366
2
	free(path2);
367
2
	free(path3);
368
2
	free(dp13);
369
2
	free(dp23);
370
2
	free(data);
371
2
	free(patch);
372
373
2
	return (diffb);
374
2
}
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
24
	if (argc < 5)
383
		return (-1);
384
385
12
	if (oflag) {
386
11
		i = snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
387
11
		if (i < 0 || i >= (int)sizeof(f1mark))
388
			errx(1, "diff3_internal: string truncated");
389
390
11
		i = snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
391
11
		if (i < 0 || i >= (int)sizeof(f3mark))
392
			errx(1, "diff3_internal: string truncated");
393
	}
394
395
12
	increase();
396
12
	if ((m = readin(argv[0], &d13)) < 0) {
397
		warn("%s", argv[0]);
398
		return (-1);
399
	}
400
12
	if ((n = readin(argv[1], &d23)) < 0) {
401
		warn("%s", argv[1]);
402
		return (-1);
403
	}
404
405
96
	for (i = 0; i <= 2; i++)
406
36
		if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
407
			warn("%s", argv[i + 2]);
408
			return (-1);
409
		}
410
411
12
	return (merge(m, n));
412
12
}
413
414
int
415
ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
416
{
417
24
	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
12
	dlp = TAILQ_FIRST(&(dlines->l_lines));
423
12
	lp = TAILQ_FIRST(&(plines->l_lines));
424
425
	end = 0;
426
26
	for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
427
1
	    lp = TAILQ_NEXT(lp, l_list)) {
428
		/* Skip blank lines */
429
1
		if (lp->l_len < 2)
430
			continue;
431
432
		/* NUL-terminate line buffer for strtol() safety. */
433
1
		tmp = lp->l_line[lp->l_len - 1];
434
1
		lp->l_line[lp->l_len - 1] = '\0';
435
436
		/* len - 1 is NUL terminator so we use len - 2 for 'op' */
437
1
		op = lp->l_line[lp->l_len - 2];
438
1
		start = (int)strtol(lp->l_line, &ep, 10);
439
440
		/* Restore the last byte of the buffer */
441
1
		lp->l_line[lp->l_len - 1] = tmp;
442
443
1
		if (op == 'a') {
444

3
			if (start > dlines->l_nblines ||
445
2
			    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
2
			if (dlp == NULL)
465
				break;
466
2
			if (dlp->l_lineno == start)
467
				break;
468
1
			if (dlp->l_lineno > start) {
469
				dlp = TAILQ_PREV(dlp, tqh, l_list);
470
1
			} else if (dlp->l_lineno < start) {
471
1
				ndlp = TAILQ_NEXT(dlp, l_list);
472
1
				if (ndlp->l_lineno > start)
473
					break;
474
				dlp = ndlp;
475
1
			}
476
		}
477
478
1
		if (dlp == NULL)
479
			errx(1, "ed_patch_lines");
480
481
482
1
		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

1
		if (op == 'a' || op == 'c') {
493
1
			for (;;) {
494
				ndlp = lp;
495
2
				lp = TAILQ_NEXT(lp, l_list);
496
2
				if (lp == NULL)
497
					errx(1, "ed_patch_lines");
498
499
2
				if (!memcmp(lp->l_line, ".", 1))
500
					break;
501
502
3
				TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
503
3
				TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
504
				    lp, l_list);
505
				dlp = lp;
506
507
1
				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
12
		TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
518
5
			sort->l_lineno = lineno++;
519
1
		dlines->l_nblines = lineno - 1;
520
1
	}
521
522
12
	return (0);
523
12
}
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
48
	char kind, *p;
536
	size_t i;
537
538
24
	fp[0] = fopen(name, "r");
539
24
	if (fp[0] == NULL)
540
		return (-1);
541
54
	for (i = 0; (p = getchange(fp[0])); i++) {
542
3
		if (i >= szchanges - 1)
543
			increase();
544
3
		a = b = number(&p);
545
3
		if (*p == ',') {
546
			p++;
547
			b = number(&p);
548
		}
549
3
		kind = *p++;
550
3
		c = d = number(&p);
551
3
		if (*p==',') {
552
			p++;
553
			d = number(&p);
554
		}
555
3
		if (kind == 'a')
556
2
			a++;
557
3
		if (kind == 'd')
558
1
			c++;
559
3
		b++;
560
3
		d++;
561
3
		(*dd)[i].old.from = a;
562
3
		(*dd)[i].old.to = b;
563
3
		(*dd)[i].new.from = c;
564
3
		(*dd)[i].new.to = d;
565
	}
566
567
24
	if (i) {
568
2
		(*dd)[i].old.from = (*dd)[i-1].old.to;
569
2
		(*dd)[i].new.from = (*dd)[i-1].new.to;
570
2
	}
571
572
24
	(void)fclose(fp[0]);
573
574
24
	return (i);
575
24
}
576
577
static int
578
number(char **lc)
579
{
580
	int nn;
581
582
	nn = 0;
583
30
	while (isdigit((unsigned char)(**lc)))
584
6
		nn = nn*10 + *(*lc)++ - '0';
585
586
6
	return (nn);
587
}
588
589
static char *
590
getchange(FILE *b)
591
{
592
	char *line;
593
594
84
	while ((line = get_line(b, NULL))) {
595
6
		if (isdigit((unsigned char)line[0]))
596
3
			return (line);
597
	}
598
599
24
	return (NULL);
600
27
}
601
602
static char *
603
get_line(FILE *b, size_t *n)
604
{
605
	char *cp;
606
68
	size_t len;
607
	static char *buf;
608
	static size_t bufsize;
609
610
34
	if ((cp = fgetln(b, &len)) == NULL)
611
24
		return (NULL);
612
613
10
	if (cp[len - 1] != '\n')
614
		len++;
615
10
	if (len + 1 > bufsize) {
616
		do {
617
1
			bufsize += 1024;
618
1
		} while (len + 1 > bufsize);
619
1
		buf = xreallocarray(buf, 1, bufsize);
620
1
	}
621
10
	memcpy(buf, cp, len - 1);
622
10
	buf[len - 1] = '\n';
623
10
	buf[len] = '\0';
624
10
	if (n != NULL)
625
4
		*n = len;
626
627
10
	return (buf);
628
34
}
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
24
	d1 = d13;
637
12
	d2 = d23;
638
	j = 0;
639
12
	for (;;) {
640
14
		t1 = (d1 < d13 + m1);
641
14
		t2 = (d2 < d23 + m2);
642
14
		if (!t1 && !t2)
643
			break;
644
645
2
		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

4
		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
655
			/* stuff peculiar to 1st file */
656
1
			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
1
			d1++;
663
1
			continue;
664
		}
665
666
		/* second file is different from others */
667

3
		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

2
		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

1
		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

2
		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
698
1
			dpl = duplicate(&d1->old,&d2->old);
699
1
			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
1
			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
1
				j = edit(d1, dpl, j);
714
1
			d1++;
715
1
			d2++;
716
1
			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
12
	return (edscript(j));
740
12
}
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
2
	if (rold->to <= rold->from)
773
1
		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
1
}
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
8
	size_t j, n;
807
	char *line;
808
809
16
	for (n = 0; cline[i] < from - 1; n += j) {
810
4
		if ((line = get_line(fp[i], &j)) == NULL)
811
			return (-1);
812
4
		if (pr != NULL)
813
			diff_output("%s%s", pr, line);
814
4
		cline[i]++;
815
	}
816
4
	return ((int) n);
817
4
}
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
2
	if (r1->to-r1->from != r2->to-r2->from)
831
		return (0);
832
1
	(void)skip(0, r1->from, NULL);
833
1
	(void)skip(1, r2->from, NULL);
834
	nchar = 0;
835
2
	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
1
	repos(nchar);
849
1
	return (1);
850
1
}
851
852
static void
853
repos(int nchar)
854
{
855
	int i;
856
857
7
	for (i = 0; i < 2; i++)
858
2
		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
859
1
}
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
2
	if (((fdup + 1) & eflag) == 0)
868
		return (j);
869
1
	j++;
870
1
	overlap[j] = !fdup;
871
1
	if (!fdup)
872
		overlapcnt++;
873
1
	de[j].old.from = diff->old.from;
874
1
	de[j].old.to = diff->old.to;
875
1
	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
876
1
	de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
877
1
	return (j);
878
1
}
879
880
/* regurgitate */
881
static int
882
edscript(int n)
883
{
884
	int j, k;
885
24
	char block[BUFSIZ+1];
886
887
26
	for (; n > 0; n--) {
888

1
		if (!oflag || !overlap[n])
889
1
			prange(&de[n].old);
890
		else
891
			diff_output("%da\n=======\n", de[n].old.to -1);
892
1
		(void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
893
4
		for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
894
1
			j = k > BUFSIZ ? BUFSIZ : k;
895
1
			if (fread(block, 1, j, fp[2]) != (size_t)j)
896
				return (-1);
897
1
			block[j] = '\0';
898
1
			diff_output("%s", block);
899
		}
900
901

1
		if (!oflag || !overlap[n])
902
1
			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
12
	return (overlapcnt);
910
12
}
911
912
static void
913
increase(void)
914
{
915
	size_t newsz, incr;
916
917
	/* are the memset(3) calls needed? */
918
24
	newsz = szchanges == 0 ? 64 : 2 * szchanges;
919
12
	incr = newsz - szchanges;
920
921
12
	d13 = xreallocarray(d13, newsz, sizeof(*d13));
922
12
	memset(d13 + szchanges, 0, incr * sizeof(*d13));
923
12
	d23 = xreallocarray(d23, newsz, sizeof(*d23));
924
12
	memset(d23 + szchanges, 0, incr * sizeof(*d23));
925
12
	de = xreallocarray(de, newsz, sizeof(*de));
926
12
	memset(de + szchanges, 0, incr * sizeof(*de));
927
12
	overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
928
12
	memset(overlap + szchanges, 0, incr * sizeof(*overlap));
929
12
	szchanges = newsz;
930
12
}