1 74b37681 2019-02-07 stsp /* $OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $ */
4 74b37681 2019-02-07 stsp * Copyright (C) Caldera International Inc. 2001-2002.
5 74b37681 2019-02-07 stsp * All rights reserved.
7 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
8 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code and documentation must retain the above
11 74b37681 2019-02-07 stsp * copyright notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. Redistributions in binary form must reproduce the above copyright
13 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer in the
14 74b37681 2019-02-07 stsp * documentation and/or other materials provided with the distribution.
15 74b37681 2019-02-07 stsp * 3. All advertising materials mentioning features or use of this software
16 74b37681 2019-02-07 stsp * must display the following acknowledgement:
17 74b37681 2019-02-07 stsp * This product includes software developed or owned by Caldera
18 74b37681 2019-02-07 stsp * International, Inc.
19 74b37681 2019-02-07 stsp * 4. Neither the name of Caldera International, Inc. nor the names of other
20 74b37681 2019-02-07 stsp * contributors may be used to endorse or promote products derived from
21 74b37681 2019-02-07 stsp * this software without specific prior written permission.
23 74b37681 2019-02-07 stsp * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24 74b37681 2019-02-07 stsp * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25 74b37681 2019-02-07 stsp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 74b37681 2019-02-07 stsp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 74b37681 2019-02-07 stsp * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28 74b37681 2019-02-07 stsp * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 74b37681 2019-02-07 stsp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 74b37681 2019-02-07 stsp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 74b37681 2019-02-07 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 74b37681 2019-02-07 stsp * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 74b37681 2019-02-07 stsp * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 74b37681 2019-02-07 stsp * POSSIBILITY OF SUCH DAMAGE.
37 74b37681 2019-02-07 stsp * Copyright (c) 1991, 1993
38 74b37681 2019-02-07 stsp * The Regents of the University of California. All rights reserved.
40 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
41 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
43 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
44 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
45 74b37681 2019-02-07 stsp * 2. Redistributions in binary form must reproduce the above copyright
46 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer in the
47 74b37681 2019-02-07 stsp * documentation and/or other materials provided with the distribution.
48 74b37681 2019-02-07 stsp * 3. Neither the name of the University nor the names of its contributors
49 74b37681 2019-02-07 stsp * may be used to endorse or promote products derived from this software
50 74b37681 2019-02-07 stsp * without specific prior written permission.
52 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 74b37681 2019-02-07 stsp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 74b37681 2019-02-07 stsp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 74b37681 2019-02-07 stsp * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 74b37681 2019-02-07 stsp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 74b37681 2019-02-07 stsp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 74b37681 2019-02-07 stsp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 74b37681 2019-02-07 stsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 74b37681 2019-02-07 stsp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 74b37681 2019-02-07 stsp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 74b37681 2019-02-07 stsp * SUCH DAMAGE.
64 74b37681 2019-02-07 stsp * @(#)diff3.c 8.1 (Berkeley) 6/6/93
67 c03c6cb3 2019-02-08 stsp #include <sys/stat.h>
68 c03c6cb3 2019-02-08 stsp #include <sys/queue.h>
70 74b37681 2019-02-07 stsp #include <ctype.h>
71 c03c6cb3 2019-02-08 stsp #include <limits.h>
72 74b37681 2019-02-07 stsp #include <stdio.h>
73 c03c6cb3 2019-02-08 stsp #include <stdarg.h>
74 74b37681 2019-02-07 stsp #include <stdlib.h>
75 74b37681 2019-02-07 stsp #include <string.h>
76 74b37681 2019-02-07 stsp #include <time.h>
77 74b37681 2019-02-07 stsp #include <unistd.h>
79 c03c6cb3 2019-02-08 stsp #include "got_error.h"
80 c03c6cb3 2019-02-08 stsp #include "got_opentemp.h"
81 c03c6cb3 2019-02-08 stsp #include "got_object.h"
83 c03c6cb3 2019-02-08 stsp #include "buf.h"
84 c03c6cb3 2019-02-08 stsp #include "rcsutil.h"
85 c03c6cb3 2019-02-08 stsp #include "got_lib_diff.h"
86 c03c6cb3 2019-02-08 stsp #include "worklist.h"
88 0d26ce36 2019-02-08 stsp #ifndef nitems
89 0d26ce36 2019-02-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
92 74b37681 2019-02-07 stsp /* diff3 - 3-way differential file comparison */
94 74b37681 2019-02-07 stsp /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
96 74b37681 2019-02-07 stsp * d13 = diff report on f1 vs f3
97 74b37681 2019-02-07 stsp * d23 = diff report on f2 vs f3
98 74b37681 2019-02-07 stsp * f1, f2, f3 the 3 files
99 74b37681 2019-02-07 stsp * if changes in f1 overlap with changes in f3, m1 and m3 are used
100 74b37681 2019-02-07 stsp * to mark the overlaps; otherwise, the file names f1 and f3 are used
101 74b37681 2019-02-07 stsp * (only for options E and X).
105 74b37681 2019-02-07 stsp * "from" is first in range of changed lines; "to" is last+1
106 74b37681 2019-02-07 stsp * from=to=line after point of insertion for added lines.
108 74b37681 2019-02-07 stsp struct range {
113 74b37681 2019-02-07 stsp struct diff {
114 74b37681 2019-02-07 stsp struct range old;
115 74b37681 2019-02-07 stsp struct range new;
118 078e971d 2019-02-08 stsp struct diff3_state {
119 078e971d 2019-02-08 stsp size_t szchanges;
121 078e971d 2019-02-08 stsp struct diff *d13;
122 078e971d 2019-02-08 stsp struct diff *d23;
125 078e971d 2019-02-08 stsp * "de" is used to gather editing scripts. These are later spewed out
126 078e971d 2019-02-08 stsp * in reverse order. Its first element must be all zero, the "new"
127 078e971d 2019-02-08 stsp * component of "de" contains line positions or byte positions
128 078e971d 2019-02-08 stsp * depending on when you look (!?). Array overlap indicates which
129 078e971d 2019-02-08 stsp * sections in "de" correspond to lines that are different in all
130 078e971d 2019-02-08 stsp * three files.
132 078e971d 2019-02-08 stsp struct diff *de;
133 078e971d 2019-02-08 stsp char *overlap;
134 078e971d 2019-02-08 stsp int overlapcnt;
135 078e971d 2019-02-08 stsp FILE *fp[3];
136 078e971d 2019-02-08 stsp int cline[3]; /* # of the last-read line in each file (0-2) */
139 078e971d 2019-02-08 stsp * the latest known correspondence between line numbers of the 3 files
140 078e971d 2019-02-08 stsp * is stored in last[1-3];
142 078e971d 2019-02-08 stsp int last[4];
145 078e971d 2019-02-08 stsp char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
149 5176f942 2019-02-08 stsp BUF *diffbuf;
153 180dce26 2019-10-06 stsp static const struct got_error *duplicate(int *, struct range *, struct range *,
154 180dce26 2019-10-06 stsp struct diff3_state *);
155 02b87977 2019-10-06 stsp static const struct got_error *edit(struct diff *, int, int *,
156 02b87977 2019-10-06 stsp struct diff3_state *);
157 02b87977 2019-10-06 stsp static const struct got_error *getchange(char **, FILE *, struct diff3_state *);
158 02b87977 2019-10-06 stsp static const struct got_error *get_line(char **, FILE *, size_t *,
159 02b87977 2019-10-06 stsp struct diff3_state *);
160 74b37681 2019-02-07 stsp static int number(char **);
161 b90524b1 2019-02-08 stsp static const struct got_error *readin(size_t *, char *, struct diff **,
162 b90524b1 2019-02-08 stsp struct diff3_state *);
163 c03c6cb3 2019-02-08 stsp static int ed_patch_lines(struct rcs_lines *, struct rcs_lines *);
164 02b87977 2019-10-06 stsp static const struct got_error *skip(int *, int, int, char *,
165 02b87977 2019-10-06 stsp struct diff3_state *);
166 180dce26 2019-10-06 stsp static const struct got_error *edscript(int, struct diff3_state *);
167 180dce26 2019-10-06 stsp static const struct got_error *merge(size_t, size_t, struct diff3_state *);
168 02b87977 2019-10-06 stsp static const struct got_error *change(int, struct range *, int,
169 02b87977 2019-10-06 stsp struct diff3_state *);
170 02b87977 2019-10-06 stsp static const struct got_error *keep(int, struct range *, struct diff3_state *);
171 ce09ab8d 2019-10-06 stsp static const struct got_error *prange(struct range *, struct diff3_state *);
172 f571bb85 2019-10-06 stsp static const struct got_error *repos(int, struct diff3_state *);
173 ce09ab8d 2019-10-06 stsp static const struct got_error *separate(const char *, struct diff3_state *);
174 078e971d 2019-02-08 stsp static const struct got_error *increase(struct diff3_state *);
175 c03c6cb3 2019-02-08 stsp static const struct got_error *diff3_internal(char *, char *, char *,
176 a551f215 2019-02-08 stsp char *, char *, const char *, const char *, struct diff3_state *,
177 a551f215 2019-02-08 stsp const char *, const char *);
179 c03c6cb3 2019-02-08 stsp static const struct got_error *
180 5176f942 2019-02-08 stsp diff_output(BUF *diffbuf, const char *fmt, ...)
182 c03c6cb3 2019-02-08 stsp va_list vap;
185 c03c6cb3 2019-02-08 stsp size_t newsize;
187 c03c6cb3 2019-02-08 stsp va_start(vap, fmt);
188 c03c6cb3 2019-02-08 stsp i = vasprintf(&str, fmt, vap);
189 c03c6cb3 2019-02-08 stsp va_end(vap);
190 c03c6cb3 2019-02-08 stsp if (i == -1)
191 638f9024 2019-05-13 stsp return got_error_from_errno("vasprintf");
192 5176f942 2019-02-08 stsp buf_append(&newsize, diffbuf, str, strlen(str));
194 c03c6cb3 2019-02-08 stsp return NULL;
197 c03c6cb3 2019-02-08 stsp static const struct got_error*
198 c03c6cb3 2019-02-08 stsp diffreg(BUF **d, const char *path1, const char *path2)
200 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
201 c03c6cb3 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
202 c03c6cb3 2019-02-08 stsp char *outpath = NULL;
203 c03c6cb3 2019-02-08 stsp struct got_diff_state ds;
204 c03c6cb3 2019-02-08 stsp struct got_diff_args args;
209 c03c6cb3 2019-02-08 stsp f1 = fopen(path1, "r");
210 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
211 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", path1);
214 c03c6cb3 2019-02-08 stsp f2 = fopen(path2, "r");
215 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
216 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", path2);
220 a551f215 2019-02-08 stsp err = got_opentemp_named(&outpath, &outfile, "/tmp/got-diffreg");
224 c03c6cb3 2019-02-08 stsp memset(&ds, 0, sizeof(ds));
225 c03c6cb3 2019-02-08 stsp /* XXX should stat buffers be passed in args instead of ds? */
226 c03c6cb3 2019-02-08 stsp if (stat(path1, &ds.stb1) == -1) {
227 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path1);
230 c03c6cb3 2019-02-08 stsp if (stat(path2, &ds.stb2) == -1) {
231 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", path2);
235 c03c6cb3 2019-02-08 stsp memset(&args, 0, sizeof(args));
236 c03c6cb3 2019-02-08 stsp args.diff_format = D_NORMAL;
237 c03c6cb3 2019-02-08 stsp args.label[0] = "";
238 c03c6cb3 2019-02-08 stsp args.label[1] = "";
239 c03c6cb3 2019-02-08 stsp args.diff_context = 0;
241 c03c6cb3 2019-02-08 stsp err = got_diffreg(&res, f1, f2, D_FORCEASCII, &args, &ds,
242 c03c6cb3 2019-02-08 stsp outfile, NULL);
246 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0) {
247 638f9024 2019-05-13 stsp err = got_error_from_errno2("fflush", outpath);
251 c03c6cb3 2019-02-08 stsp *d = buf_load(outpath);
252 c03c6cb3 2019-02-08 stsp if (*d == NULL)
253 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_load");
255 a551f215 2019-02-08 stsp if (outpath) {
256 a551f215 2019-02-08 stsp unlink(outpath);
257 a551f215 2019-02-08 stsp free(outpath);
259 fb43ecf1 2019-02-11 stsp if (outfile && fclose(outfile) != 0 && err == NULL)
260 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
261 fb43ecf1 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
262 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
263 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
264 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
265 c03c6cb3 2019-02-08 stsp return err;
269 74b37681 2019-02-07 stsp * For merge(1).
271 c03c6cb3 2019-02-08 stsp const struct got_error *
272 57ee5d50 2019-02-08 stsp got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2,
273 57ee5d50 2019-02-08 stsp const char *p3, const char *label1, const char *label3)
275 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
276 c03c6cb3 2019-02-08 stsp char *dp13, *dp23, *path1, *path2, *path3;
277 74b37681 2019-02-07 stsp BUF *b1, *b2, *b3, *d1, *d2, *diffb;
278 74b37681 2019-02-07 stsp u_char *data, *patch;
279 74b37681 2019-02-07 stsp size_t dlen, plen;
280 c03c6cb3 2019-02-08 stsp struct wklhead temp_files;
281 078e971d 2019-02-08 stsp struct diff3_state *d3s;
284 57ee5d50 2019-02-08 stsp *overlapcnt = 0;
286 5d731149 2019-02-08 stsp SLIST_INIT(&temp_files);
288 078e971d 2019-02-08 stsp d3s = calloc(1, sizeof(*d3s));
289 078e971d 2019-02-08 stsp if (d3s == NULL)
290 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
291 078e971d 2019-02-08 stsp d3s->eflag = 3; /* default -E for compatibility with former RCS */
293 74b37681 2019-02-07 stsp b1 = b2 = b3 = d1 = d2 = diffb = NULL;
294 74b37681 2019-02-07 stsp dp13 = dp23 = path1 = path2 = path3 = NULL;
295 74b37681 2019-02-07 stsp data = patch = NULL;
297 0ec6f014 2019-02-08 stsp if ((b1 = buf_load(p1)) == NULL)
299 0ec6f014 2019-02-08 stsp if ((b2 = buf_load(p2)) == NULL)
301 0ec6f014 2019-02-08 stsp if ((b3 = buf_load(p3)) == NULL)
304 74b37681 2019-02-07 stsp diffb = buf_alloc(128);
306 c03c6cb3 2019-02-08 stsp if (asprintf(&path1, "/tmp/got-diff1.XXXXXXXX") == -1) {
307 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
310 c03c6cb3 2019-02-08 stsp if (asprintf(&path2, "/tmp/got-diff2.XXXXXXXX") == -1) {
311 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
314 c03c6cb3 2019-02-08 stsp if (asprintf(&path3, "/tmp/got-diff3.XXXXXXXX") == -1) {
315 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
319 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b1, path1, &temp_files);
322 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b2, path2, &temp_files);
325 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b3, path3, &temp_files);
329 74b37681 2019-02-07 stsp buf_free(b2);
332 c03c6cb3 2019-02-08 stsp err = diffreg(&d1, path1, path3);
334 74b37681 2019-02-07 stsp buf_free(diffb);
335 74b37681 2019-02-07 stsp diffb = NULL;
339 c03c6cb3 2019-02-08 stsp err = diffreg(&d2, path2, path3);
341 c03c6cb3 2019-02-08 stsp buf_free(diffb);
342 c03c6cb3 2019-02-08 stsp diffb = NULL;
346 c03c6cb3 2019-02-08 stsp if (asprintf(&dp13, "/tmp/got-d13.XXXXXXXXXX") == -1) {
347 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
350 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(d1, dp13, &temp_files);
354 74b37681 2019-02-07 stsp buf_free(d1);
357 c03c6cb3 2019-02-08 stsp if (asprintf(&dp23, "/tmp/got-d23.XXXXXXXXXX") == -1) {
358 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
361 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(d2, dp23, &temp_files);
365 74b37681 2019-02-07 stsp buf_free(d2);
368 5176f942 2019-02-08 stsp d3s->diffbuf = diffb;
369 a551f215 2019-02-08 stsp err = diff3_internal(dp13, dp23, path1, path2, path3,
370 a551f215 2019-02-08 stsp label1, label3, d3s, label1, label3);
372 74b37681 2019-02-07 stsp buf_free(diffb);
373 74b37681 2019-02-07 stsp diffb = NULL;
377 74b37681 2019-02-07 stsp plen = buf_len(diffb);
378 74b37681 2019-02-07 stsp patch = buf_release(diffb);
379 74b37681 2019-02-07 stsp dlen = buf_len(b1);
380 74b37681 2019-02-07 stsp data = buf_release(b1);
382 5a3ad9e0 2019-02-08 stsp diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines);
384 74b37681 2019-02-07 stsp buf_free(b2);
385 74b37681 2019-02-07 stsp buf_free(b3);
386 74b37681 2019-02-07 stsp buf_free(d1);
387 74b37681 2019-02-07 stsp buf_free(d2);
389 74b37681 2019-02-07 stsp (void)unlink(path1);
390 74b37681 2019-02-07 stsp (void)unlink(path2);
391 74b37681 2019-02-07 stsp (void)unlink(path3);
392 74b37681 2019-02-07 stsp (void)unlink(dp13);
393 74b37681 2019-02-07 stsp (void)unlink(dp23);
395 74b37681 2019-02-07 stsp free(path1);
396 74b37681 2019-02-07 stsp free(path2);
397 74b37681 2019-02-07 stsp free(path3);
398 74b37681 2019-02-07 stsp free(dp13);
399 74b37681 2019-02-07 stsp free(dp23);
400 74b37681 2019-02-07 stsp free(data);
401 74b37681 2019-02-07 stsp free(patch);
403 c03c6cb3 2019-02-08 stsp worklist_clean(&temp_files, worklist_unlink);
405 0d26ce36 2019-02-08 stsp for (i = 0; i < nitems(d3s->fp); i++) {
406 fb43ecf1 2019-02-11 stsp if (d3s->fp[i] && fclose(d3s->fp[i]) != 0 && err == NULL)
407 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
409 5a3ad9e0 2019-02-08 stsp if (err == NULL && diffb) {
410 62f59172 2019-02-08 stsp if (buf_write_fd(diffb, outfd) < 0)
411 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
412 57ee5d50 2019-02-08 stsp *overlapcnt = d3s->overlapcnt;
415 62f59172 2019-02-08 stsp buf_free(diffb);
416 c03c6cb3 2019-02-08 stsp return err;
419 c03c6cb3 2019-02-08 stsp static const struct got_error *
420 c03c6cb3 2019-02-08 stsp diff3_internal(char *dp13, char *dp23, char *path1, char *path2, char *path3,
421 a551f215 2019-02-08 stsp const char *fmark, const char *rmark, struct diff3_state *d3s,
422 a551f215 2019-02-08 stsp const char *label1, const char *label3)
424 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
425 74b37681 2019-02-07 stsp ssize_t m, n;
428 01a44956 2019-03-27 stsp i = snprintf(d3s->f1mark, sizeof(d3s->f1mark),
429 01a44956 2019-03-27 stsp "%s %s", GOT_DIFF_CONFLICT_MARKER_BEGIN, label1);
430 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f1mark))
431 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
433 01a44956 2019-03-27 stsp i = snprintf(d3s->f3mark, sizeof(d3s->f3mark),
434 01a44956 2019-03-27 stsp "%s %s", GOT_DIFF_CONFLICT_MARKER_END, label3);
435 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f3mark))
436 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
438 078e971d 2019-02-08 stsp err = increase(d3s);
440 c03c6cb3 2019-02-08 stsp return err;
442 b90524b1 2019-02-08 stsp err = readin(&m, dp13, &d3s->d13, d3s);
444 b90524b1 2019-02-08 stsp return err;
445 b90524b1 2019-02-08 stsp err = readin(&n, dp23, &d3s->d23, d3s);
447 b90524b1 2019-02-08 stsp return err;
449 078e971d 2019-02-08 stsp if ((d3s->fp[0] = fopen(path1, "r")) == NULL)
450 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path1);
451 078e971d 2019-02-08 stsp if ((d3s->fp[1] = fopen(path2, "r")) == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path2);
453 078e971d 2019-02-08 stsp if ((d3s->fp[2] = fopen(path3, "r")) == NULL)
454 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", path3);
456 180dce26 2019-10-06 stsp return merge(m, n, d3s);
460 74b37681 2019-02-07 stsp ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
462 74b37681 2019-02-07 stsp char op, *ep;
463 74b37681 2019-02-07 stsp struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
464 74b37681 2019-02-07 stsp int start, end, i, lineno;
465 74b37681 2019-02-07 stsp u_char tmp;
467 74b37681 2019-02-07 stsp dlp = TAILQ_FIRST(&(dlines->l_lines));
468 74b37681 2019-02-07 stsp lp = TAILQ_FIRST(&(plines->l_lines));
471 74b37681 2019-02-07 stsp for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
472 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list)) {
473 74b37681 2019-02-07 stsp /* Skip blank lines */
474 74b37681 2019-02-07 stsp if (lp->l_len < 2)
477 74b37681 2019-02-07 stsp /* NUL-terminate line buffer for strtol() safety. */
478 74b37681 2019-02-07 stsp tmp = lp->l_line[lp->l_len - 1];
479 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = '\0';
481 74b37681 2019-02-07 stsp /* len - 1 is NUL terminator so we use len - 2 for 'op' */
482 74b37681 2019-02-07 stsp op = lp->l_line[lp->l_len - 2];
483 74b37681 2019-02-07 stsp start = (int)strtol(lp->l_line, &ep, 10);
485 74b37681 2019-02-07 stsp /* Restore the last byte of the buffer */
486 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = tmp;
488 74b37681 2019-02-07 stsp if (op == 'a') {
489 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
490 74b37681 2019-02-07 stsp start < 0 || *ep != 'a')
492 74b37681 2019-02-07 stsp } else if (op == 'c') {
493 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
494 74b37681 2019-02-07 stsp start < 0 || (*ep != ',' && *ep != 'c'))
497 74b37681 2019-02-07 stsp if (*ep == ',') {
499 74b37681 2019-02-07 stsp end = (int)strtol(ep, &ep, 10);
500 74b37681 2019-02-07 stsp if (end < 0 || *ep != 'c')
503 74b37681 2019-02-07 stsp end = start;
509 74b37681 2019-02-07 stsp if (dlp == NULL)
511 74b37681 2019-02-07 stsp if (dlp->l_lineno == start)
513 74b37681 2019-02-07 stsp if (dlp->l_lineno > start) {
514 74b37681 2019-02-07 stsp dlp = TAILQ_PREV(dlp, tqh, l_list);
515 74b37681 2019-02-07 stsp } else if (dlp->l_lineno < start) {
516 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
517 74b37681 2019-02-07 stsp if (ndlp->l_lineno > start)
519 74b37681 2019-02-07 stsp dlp = ndlp;
523 74b37681 2019-02-07 stsp if (dlp == NULL)
527 74b37681 2019-02-07 stsp if (op == 'c') {
528 74b37681 2019-02-07 stsp insert_after = TAILQ_PREV(dlp, tqh, l_list);
529 74b37681 2019-02-07 stsp for (i = 0; i <= (end - start); i++) {
530 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
531 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
532 74b37681 2019-02-07 stsp dlp = ndlp;
534 74b37681 2019-02-07 stsp dlp = insert_after;
537 74b37681 2019-02-07 stsp if (op == 'a' || op == 'c') {
540 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list);
541 74b37681 2019-02-07 stsp if (lp == NULL)
544 acb4e1d1 2019-08-10 stsp if (lp->l_len == 2 &&
545 acb4e1d1 2019-08-10 stsp lp->l_line[0] == '.' &&
546 acb4e1d1 2019-08-10 stsp lp->l_line[1] == '\n')
549 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
550 74b37681 2019-02-07 stsp TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
551 74b37681 2019-02-07 stsp lp, l_list);
554 74b37681 2019-02-07 stsp lp->l_lineno = start;
560 74b37681 2019-02-07 stsp * always resort lines as the markers might be put at the
561 74b37681 2019-02-07 stsp * same line as we first started editing.
563 74b37681 2019-02-07 stsp lineno = 0;
564 74b37681 2019-02-07 stsp TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
565 74b37681 2019-02-07 stsp sort->l_lineno = lineno++;
566 74b37681 2019-02-07 stsp dlines->l_nblines = lineno - 1;
569 74b37681 2019-02-07 stsp return (0);
573 74b37681 2019-02-07 stsp * Pick up the line numbers of all changes from one change file.
574 74b37681 2019-02-07 stsp * (This puts the numbers in a vector, which is not strictly necessary,
575 74b37681 2019-02-07 stsp * since the vector is processed in one sequential pass.
576 74b37681 2019-02-07 stsp * The vector could be optimized out of existence)
578 b90524b1 2019-02-08 stsp static const struct got_error *
579 b90524b1 2019-02-08 stsp readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
581 b90524b1 2019-02-08 stsp const struct got_error *err = NULL;
582 74b37681 2019-02-07 stsp int a, b, c, d;
583 74b37681 2019-02-07 stsp char kind, *p;
588 078e971d 2019-02-08 stsp d3s->fp[0] = fopen(name, "r");
589 078e971d 2019-02-08 stsp if (d3s->fp[0] == NULL)
590 638f9024 2019-05-13 stsp return got_error_from_errno2("fopen", name);
591 02b87977 2019-10-06 stsp err = getchange(&p, d3s->fp[0], d3s);
593 02b87977 2019-10-06 stsp return err;
594 02b87977 2019-10-06 stsp for (i = 0; p; i++) {
595 b90524b1 2019-02-08 stsp if (i >= d3s->szchanges - 1) {
596 b90524b1 2019-02-08 stsp err = increase(d3s);
598 b90524b1 2019-02-08 stsp return err;
600 74b37681 2019-02-07 stsp a = b = number(&p);
601 74b37681 2019-02-07 stsp if (*p == ',') {
603 74b37681 2019-02-07 stsp b = number(&p);
605 74b37681 2019-02-07 stsp kind = *p++;
606 74b37681 2019-02-07 stsp c = d = number(&p);
607 74b37681 2019-02-07 stsp if (*p==',') {
609 74b37681 2019-02-07 stsp d = number(&p);
611 74b37681 2019-02-07 stsp if (kind == 'a')
613 74b37681 2019-02-07 stsp if (kind == 'd')
617 74b37681 2019-02-07 stsp (*dd)[i].old.from = a;
618 74b37681 2019-02-07 stsp (*dd)[i].old.to = b;
619 74b37681 2019-02-07 stsp (*dd)[i].new.from = c;
620 74b37681 2019-02-07 stsp (*dd)[i].new.to = d;
622 02b87977 2019-10-06 stsp err = getchange(&p, d3s->fp[0], d3s);
624 02b87977 2019-10-06 stsp return err;
628 74b37681 2019-02-07 stsp (*dd)[i].old.from = (*dd)[i-1].old.to;
629 74b37681 2019-02-07 stsp (*dd)[i].new.from = (*dd)[i-1].new.to;
632 fb43ecf1 2019-02-11 stsp if (fclose(d3s->fp[0]) != 0)
633 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
636 fb43ecf1 2019-02-11 stsp return err;
640 74b37681 2019-02-07 stsp number(char **lc)
645 74b37681 2019-02-07 stsp while (isdigit((unsigned char)(**lc)))
646 74b37681 2019-02-07 stsp nn = nn*10 + *(*lc)++ - '0';
648 74b37681 2019-02-07 stsp return (nn);
651 02b87977 2019-10-06 stsp static const struct got_error *
652 02b87977 2019-10-06 stsp getchange(char **line, FILE *b, struct diff3_state *d3s)
654 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
656 02b87977 2019-10-06 stsp *line = NULL;
658 02b87977 2019-10-06 stsp if (*line && isdigit((unsigned char)(*line)[0]))
659 02b87977 2019-10-06 stsp return NULL;
660 02b87977 2019-10-06 stsp err = get_line(line, b, NULL, d3s);
662 02b87977 2019-10-06 stsp return err;
663 02b87977 2019-10-06 stsp } while (*line);
665 02b87977 2019-10-06 stsp return NULL;
668 02b87977 2019-10-06 stsp static const struct got_error *
669 02b87977 2019-10-06 stsp get_line(char **ret, FILE *b, size_t *n, struct diff3_state *d3s)
671 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
672 6dea73df 2019-09-23 stsp char *cp = NULL;
673 500467ff 2019-09-25 hiltjo size_t size;
674 500467ff 2019-09-25 hiltjo ssize_t len;
677 02b87977 2019-10-06 stsp *ret = NULL;
679 6dea73df 2019-09-23 stsp len = getline(&cp, &size, b);
680 02b87977 2019-10-06 stsp if (len == -1) {
681 02b87977 2019-10-06 stsp if (ferror(b))
682 02b87977 2019-10-06 stsp err = got_error_from_errno("getline");
686 6dea73df 2019-09-23 stsp if (cp[len - 1] != '\n') {
688 6dea73df 2019-09-23 stsp if (len + 1 > size) {
689 6dea73df 2019-09-23 stsp new = realloc(cp, len + 1);
690 02b87977 2019-10-06 stsp if (new == NULL) {
691 02b87977 2019-10-06 stsp err = got_error_from_errno("realloc");
696 6dea73df 2019-09-23 stsp cp[len - 1] = '\n';
697 6dea73df 2019-09-23 stsp cp[len] = '\0';
700 6dea73df 2019-09-23 stsp free(d3s->buf);
701 02b87977 2019-10-06 stsp *ret = d3s->buf = cp;
703 74b37681 2019-02-07 stsp if (n != NULL)
707 02b87977 2019-10-06 stsp return err;
710 180dce26 2019-10-06 stsp static const struct got_error *
711 078e971d 2019-02-08 stsp merge(size_t m1, size_t m2, struct diff3_state *d3s)
713 180dce26 2019-10-06 stsp const struct got_error *err = NULL;
714 74b37681 2019-02-07 stsp struct diff *d1, *d2, *d3;
715 74b37681 2019-02-07 stsp int dpl, j, t1, t2;
717 078e971d 2019-02-08 stsp d1 = d3s->d13;
718 078e971d 2019-02-08 stsp d2 = d3s->d23;
721 078e971d 2019-02-08 stsp t1 = (d1 < d3s->d13 + m1);
722 078e971d 2019-02-08 stsp t2 = (d2 < d3s->d23 + m2);
723 74b37681 2019-02-07 stsp if (!t1 && !t2)
726 078e971d 2019-02-08 stsp if (d3s->debug) {
727 74b37681 2019-02-07 stsp printf("%d,%d=%d,%d %d,%d=%d,%d\n",
728 74b37681 2019-02-07 stsp d1->old.from, d1->old.to,
729 74b37681 2019-02-07 stsp d1->new.from, d1->new.to,
730 74b37681 2019-02-07 stsp d2->old.from, d2->old.to,
731 74b37681 2019-02-07 stsp d2->new.from, d2->new.to);
734 74b37681 2019-02-07 stsp /* first file is different from others */
735 74b37681 2019-02-07 stsp if (!t2 || (t1 && d1->new.to < d2->new.from)) {
736 74b37681 2019-02-07 stsp /* stuff peculiar to 1st file */
737 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
738 ce09ab8d 2019-10-06 stsp err = separate("1", d3s);
740 ce09ab8d 2019-10-06 stsp return err;
741 02b87977 2019-10-06 stsp err = change(1, &d1->old, 0, d3s);
743 02b87977 2019-10-06 stsp return err;
744 02b87977 2019-10-06 stsp err = keep(2, &d1->new, d3s);
746 02b87977 2019-10-06 stsp return err;
747 02b87977 2019-10-06 stsp err = change(3, &d1->new, 0, d3s);
749 02b87977 2019-10-06 stsp return err;
755 74b37681 2019-02-07 stsp /* second file is different from others */
756 74b37681 2019-02-07 stsp if (!t1 || (t2 && d2->new.to < d1->new.from)) {
757 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
758 ce09ab8d 2019-10-06 stsp err = separate("2", d3s);
760 ce09ab8d 2019-10-06 stsp return err;
761 02b87977 2019-10-06 stsp err = keep(1, &d2->new, d3s);
763 02b87977 2019-10-06 stsp return err;
764 02b87977 2019-10-06 stsp err = change(2, &d2->old, 0, d3s);
766 02b87977 2019-10-06 stsp return err;
767 02b87977 2019-10-06 stsp err = change(3, &d2->new, 0, d3s);
769 02b87977 2019-10-06 stsp return err;
776 74b37681 2019-02-07 stsp * Merge overlapping changes in first file
777 74b37681 2019-02-07 stsp * this happens after extension (see below).
779 078e971d 2019-02-08 stsp if (d1 + 1 < d3s->d13 + m1 && d1->new.to >= d1[1].new.from) {
780 74b37681 2019-02-07 stsp d1[1].old.from = d1->old.from;
781 74b37681 2019-02-07 stsp d1[1].new.from = d1->new.from;
786 74b37681 2019-02-07 stsp /* merge overlapping changes in second */
787 078e971d 2019-02-08 stsp if (d2 + 1 < d3s->d23 + m2 && d2->new.to >= d2[1].new.from) {
788 74b37681 2019-02-07 stsp d2[1].old.from = d2->old.from;
789 74b37681 2019-02-07 stsp d2[1].new.from = d2->new.from;
793 74b37681 2019-02-07 stsp /* stuff peculiar to third file or different in all */
794 74b37681 2019-02-07 stsp if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
795 180dce26 2019-10-06 stsp err = duplicate(&dpl, &d1->old, &d2->old, d3s);
797 180dce26 2019-10-06 stsp return err;
800 74b37681 2019-02-07 stsp * dpl = 0 means all files differ
801 74b37681 2019-02-07 stsp * dpl = 1 means files 1 and 2 identical
803 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
804 ce09ab8d 2019-10-06 stsp err = separate(dpl ? "3" : "", d3s);
806 ce09ab8d 2019-10-06 stsp return err;
807 02b87977 2019-10-06 stsp err = change(1, &d1->old, dpl, d3s);
809 02b87977 2019-10-06 stsp return err;
810 02b87977 2019-10-06 stsp err = change(2, &d2->old, 0, d3s);
812 02b87977 2019-10-06 stsp return err;
813 74b37681 2019-02-07 stsp d3 = d1->old.to > d1->old.from ? d1 : d2;
814 02b87977 2019-10-06 stsp err = change(3, &d3->new, 0, d3s);
816 02b87977 2019-10-06 stsp return err;
818 02b87977 2019-10-06 stsp err = edit(d1, dpl, &j, d3s);
820 02b87977 2019-10-06 stsp return err;
828 74b37681 2019-02-07 stsp * Overlapping changes from file 1 and 2; extend changes
829 74b37681 2019-02-07 stsp * appropriately to make them coincide.
831 74b37681 2019-02-07 stsp if (d1->new.from < d2->new.from) {
832 74b37681 2019-02-07 stsp d2->old.from -= d2->new.from-d1->new.from;
833 74b37681 2019-02-07 stsp d2->new.from = d1->new.from;
834 74b37681 2019-02-07 stsp } else if (d2->new.from < d1->new.from) {
835 74b37681 2019-02-07 stsp d1->old.from -= d1->new.from-d2->new.from;
836 74b37681 2019-02-07 stsp d1->new.from = d2->new.from;
838 74b37681 2019-02-07 stsp if (d1->new.to > d2->new.to) {
839 74b37681 2019-02-07 stsp d2->old.to += d1->new.to - d2->new.to;
840 74b37681 2019-02-07 stsp d2->new.to = d1->new.to;
841 74b37681 2019-02-07 stsp } else if (d2->new.to > d1->new.to) {
842 74b37681 2019-02-07 stsp d1->old.to += d2->new.to - d1->new.to;
843 74b37681 2019-02-07 stsp d1->new.to = d2->new.to;
847 078e971d 2019-02-08 stsp return (edscript(j, d3s));
850 ce09ab8d 2019-10-06 stsp static const struct got_error *
851 5176f942 2019-02-08 stsp separate(const char *s, struct diff3_state *d3s)
853 ce09ab8d 2019-10-06 stsp return diff_output(d3s->diffbuf, "====%s\n", s);
857 74b37681 2019-02-07 stsp * The range of lines rold.from thru rold.to in file i is to be changed.
858 74b37681 2019-02-07 stsp * It is to be printed only if it does not duplicate something to be
859 74b37681 2019-02-07 stsp * printed later.
861 02b87977 2019-10-06 stsp static const struct got_error *
862 078e971d 2019-02-08 stsp change(int i, struct range *rold, int fdup, struct diff3_state *d3s)
864 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
865 02b87977 2019-10-06 stsp int nskipped;
867 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%d:", i);
869 ce09ab8d 2019-10-06 stsp return err;
870 078e971d 2019-02-08 stsp d3s->last[i] = rold->to;
871 ce09ab8d 2019-10-06 stsp err = prange(rold, d3s);
873 ce09ab8d 2019-10-06 stsp return err;
874 078e971d 2019-02-08 stsp if (fdup || d3s->debug)
875 02b87977 2019-10-06 stsp return NULL;
877 02b87977 2019-10-06 stsp err = skip(&nskipped, i, rold->from, NULL, d3s);
879 02b87977 2019-10-06 stsp return err;
880 02b87977 2019-10-06 stsp return skip(&nskipped, i, rold->to, " ", d3s);
884 74b37681 2019-02-07 stsp * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
886 ce09ab8d 2019-10-06 stsp static const struct got_error *
887 5176f942 2019-02-08 stsp prange(struct range *rold, struct diff3_state *d3s)
889 ce09ab8d 2019-10-06 stsp const struct got_error *err = NULL;
891 ce09ab8d 2019-10-06 stsp if (rold->to <= rold->from) {
892 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%da\n", rold->from - 1);
894 ce09ab8d 2019-10-06 stsp return err;
896 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%d", rold->from);
898 ce09ab8d 2019-10-06 stsp return err;
899 ce09ab8d 2019-10-06 stsp if (rold->to > rold->from + 1) {
900 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, ",%d", rold->to - 1);
902 ce09ab8d 2019-10-06 stsp return err;
904 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "c\n");
906 ce09ab8d 2019-10-06 stsp return err;
909 ce09ab8d 2019-10-06 stsp return NULL;
913 74b37681 2019-02-07 stsp * No difference was reported by diff between file 1 (or 2) and file 3,
914 74b37681 2019-02-07 stsp * and an artificial dummy difference (trange) must be ginned up to
915 74b37681 2019-02-07 stsp * correspond to the change reported in the other file.
917 02b87977 2019-10-06 stsp static const struct got_error *
918 078e971d 2019-02-08 stsp keep(int i, struct range *rnew, struct diff3_state *d3s)
921 74b37681 2019-02-07 stsp struct range trange;
923 078e971d 2019-02-08 stsp delta = d3s->last[3] - d3s->last[i];
924 74b37681 2019-02-07 stsp trange.from = rnew->from - delta;
925 74b37681 2019-02-07 stsp trange.to = rnew->to - delta;
926 02b87977 2019-10-06 stsp return change(i, &trange, 1, d3s);
930 74b37681 2019-02-07 stsp * skip to just before line number from in file "i". If "pr" is non-NULL,
931 74b37681 2019-02-07 stsp * print all skipped stuff with string pr as a prefix.
933 02b87977 2019-10-06 stsp static const struct got_error *
934 02b87977 2019-10-06 stsp skip(int *nskipped, int i, int from, char *pr, struct diff3_state *d3s)
936 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
937 74b37681 2019-02-07 stsp size_t j, n;
938 74b37681 2019-02-07 stsp char *line;
940 02b87977 2019-10-06 stsp *nskipped = 0;
941 078e971d 2019-02-08 stsp for (n = 0; d3s->cline[i] < from - 1; n += j) {
942 02b87977 2019-10-06 stsp err = get_line(&line, d3s->fp[i], &j, d3s);
944 02b87977 2019-10-06 stsp return err;
945 ce09ab8d 2019-10-06 stsp if (pr != NULL) {
946 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%s%s", pr, line);
948 ce09ab8d 2019-10-06 stsp return err;
950 078e971d 2019-02-08 stsp d3s->cline[i]++;
952 02b87977 2019-10-06 stsp *nskipped = n;
953 02b87977 2019-10-06 stsp return NULL;
957 180dce26 2019-10-06 stsp * Set *dpl to 1 or 0 according as the old range (in file 1) contains exactly
958 74b37681 2019-02-07 stsp * the same data as the new range (in file 2).
960 180dce26 2019-10-06 stsp static const struct got_error *
961 180dce26 2019-10-06 stsp duplicate(int *dpl, struct range *r1, struct range *r2, struct diff3_state *d3s)
963 f571bb85 2019-10-06 stsp const struct got_error *err = NULL;
966 02b87977 2019-10-06 stsp int nline, nskipped;
970 74b37681 2019-02-07 stsp if (r1->to-r1->from != r2->to-r2->from)
971 180dce26 2019-10-06 stsp return NULL;
973 02b87977 2019-10-06 stsp err = skip(&nskipped, 0, r1->from, NULL, d3s);
975 02b87977 2019-10-06 stsp return err;
976 02b87977 2019-10-06 stsp err = skip(&nskipped, 1, r2->from, NULL, d3s);
978 02b87977 2019-10-06 stsp return err;
980 74b37681 2019-02-07 stsp for (nline=0; nline < r1->to - r1->from; nline++) {
982 078e971d 2019-02-08 stsp c = getc(d3s->fp[0]);
983 180dce26 2019-10-06 stsp if (c == EOF)
984 180dce26 2019-10-06 stsp return got_ferror(d3s->fp[0], GOT_ERR_EOF);
985 078e971d 2019-02-08 stsp d = getc(d3s->fp[1]);
986 180dce26 2019-10-06 stsp if (d == EOF)
987 180dce26 2019-10-06 stsp return got_ferror(d3s->fp[1], GOT_ERR_EOF);
989 f571bb85 2019-10-06 stsp if (c != d)
990 f571bb85 2019-10-06 stsp return repos(nchar, d3s);
991 74b37681 2019-02-07 stsp } while (c != '\n');
993 f571bb85 2019-10-06 stsp err = repos(nchar, d3s);
995 f571bb85 2019-10-06 stsp return err;
997 180dce26 2019-10-06 stsp return NULL;
1000 f571bb85 2019-10-06 stsp static const struct got_error *
1001 078e971d 2019-02-08 stsp repos(int nchar, struct diff3_state *d3s)
1005 f571bb85 2019-10-06 stsp for (i = 0; i < 2; i++) {
1006 f571bb85 2019-10-06 stsp if (fseek(d3s->fp[i], (long)-nchar, SEEK_CUR) == -1)
1007 f571bb85 2019-10-06 stsp return got_ferror(d3s->fp[i], GOT_ERR_IO);
1010 f571bb85 2019-10-06 stsp return NULL;
1014 74b37681 2019-02-07 stsp * collect an editing script for later regurgitation
1016 02b87977 2019-10-06 stsp static const struct got_error *
1017 02b87977 2019-10-06 stsp edit(struct diff *diff, int fdup, int *j, struct diff3_state *d3s)
1019 02b87977 2019-10-06 stsp const struct got_error *err = NULL;
1020 02b87977 2019-10-06 stsp int nskipped;
1022 078e971d 2019-02-08 stsp if (((fdup + 1) & d3s->eflag) == 0)
1023 02b87977 2019-10-06 stsp return NULL;
1025 02b87977 2019-10-06 stsp d3s->overlap[*j] = !fdup;
1026 74b37681 2019-02-07 stsp if (!fdup)
1027 078e971d 2019-02-08 stsp d3s->overlapcnt++;
1028 02b87977 2019-10-06 stsp d3s->de[*j].old.from = diff->old.from;
1029 02b87977 2019-10-06 stsp d3s->de[*j].old.to = diff->old.to;
1030 02b87977 2019-10-06 stsp err = skip(&nskipped, 2, diff->new.from, NULL, d3s);
1032 02b87977 2019-10-06 stsp return err;
1033 02b87977 2019-10-06 stsp d3s->de[*j].new.from = d3s->de[*j - 1].new.to + nskipped;
1034 02b87977 2019-10-06 stsp err = skip(&nskipped, 2, diff->new.to, NULL, d3s);
1035 02b87977 2019-10-06 stsp d3s->de[*j].new.to = d3s->de[*j].new.from + nskipped;
1036 02b87977 2019-10-06 stsp return NULL;
1039 74b37681 2019-02-07 stsp /* regurgitate */
1040 180dce26 2019-10-06 stsp static const struct got_error *
1041 078e971d 2019-02-08 stsp edscript(int n, struct diff3_state *d3s)
1043 ce09ab8d 2019-10-06 stsp const struct got_error *err = NULL;
1045 74b37681 2019-02-07 stsp char block[BUFSIZ+1];
1047 74b37681 2019-02-07 stsp for (; n > 0; n--) {
1048 ce09ab8d 2019-10-06 stsp if (!d3s->overlap[n]) {
1049 ce09ab8d 2019-10-06 stsp err = prange(&d3s->de[n].old, d3s);
1051 ce09ab8d 2019-10-06 stsp return err;
1053 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%da\n%s\n",
1054 01a44956 2019-03-27 stsp d3s->de[n].old.to -1, GOT_DIFF_CONFLICT_MARKER_SEP);
1056 ce09ab8d 2019-10-06 stsp return err;
1058 e9dabdfb 2019-10-06 stsp if (fseek(d3s->fp[2], (long)d3s->de[n].new.from, SEEK_SET)
1060 e9dabdfb 2019-10-06 stsp return got_error_from_errno("fseek");
1061 078e971d 2019-02-08 stsp k = d3s->de[n].new.to - d3s->de[n].new.from;
1062 078e971d 2019-02-08 stsp for (; k > 0; k-= j) {
1063 74b37681 2019-02-07 stsp j = k > BUFSIZ ? BUFSIZ : k;
1064 078e971d 2019-02-08 stsp if (fread(block, 1, j, d3s->fp[2]) != (size_t)j)
1065 180dce26 2019-10-06 stsp return got_ferror(d3s->fp[2], GOT_ERR_IO);
1066 74b37681 2019-02-07 stsp block[j] = '\0';
1067 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%s", block);
1069 ce09ab8d 2019-10-06 stsp return err;
1072 ce09ab8d 2019-10-06 stsp if (!d3s->overlap[n]) {
1073 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, ".\n");
1075 ce09ab8d 2019-10-06 stsp return err;
1077 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%s\n.\n", d3s->f3mark);
1079 ce09ab8d 2019-10-06 stsp return err;
1080 ce09ab8d 2019-10-06 stsp err = diff_output(d3s->diffbuf, "%da\n%s\n.\n",
1081 5176f942 2019-02-08 stsp d3s->de[n].old.from - 1, d3s->f1mark);
1083 ce09ab8d 2019-10-06 stsp return err;
1087 180dce26 2019-10-06 stsp return NULL;
1090 c03c6cb3 2019-02-08 stsp static const struct got_error *
1091 078e971d 2019-02-08 stsp increase(struct diff3_state *d3s)
1093 74b37681 2019-02-07 stsp size_t newsz, incr;
1094 c03c6cb3 2019-02-08 stsp struct diff *d;
1097 74b37681 2019-02-07 stsp /* are the memset(3) calls needed? */
1098 078e971d 2019-02-08 stsp newsz = d3s->szchanges == 0 ? 64 : 2 * d3s->szchanges;
1099 078e971d 2019-02-08 stsp incr = newsz - d3s->szchanges;
1101 078e971d 2019-02-08 stsp d = reallocarray(d3s->d13, newsz, sizeof(*d3s->d13));
1102 c03c6cb3 2019-02-08 stsp if (d == NULL)
1103 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1104 078e971d 2019-02-08 stsp d3s->d13 = d;
1105 078e971d 2019-02-08 stsp memset(d3s->d13 + d3s->szchanges, 0, incr * sizeof(*d3s->d13));
1107 078e971d 2019-02-08 stsp d = reallocarray(d3s->d23, newsz, sizeof(*d3s->d23));
1108 c03c6cb3 2019-02-08 stsp if (d == NULL)
1109 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1110 078e971d 2019-02-08 stsp d3s->d23 = d;
1111 078e971d 2019-02-08 stsp memset(d3s->d23 + d3s->szchanges, 0, incr * sizeof(*d3s->d23));
1113 078e971d 2019-02-08 stsp d = reallocarray(d3s->de, newsz, sizeof(*d3s->de));
1114 c03c6cb3 2019-02-08 stsp if (d == NULL)
1115 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1116 078e971d 2019-02-08 stsp d3s->de = d;
1117 078e971d 2019-02-08 stsp memset(d3s->de + d3s->szchanges, 0, incr * sizeof(*d3s->de));
1119 078e971d 2019-02-08 stsp s = reallocarray(d3s->overlap, newsz, sizeof(*d3s->overlap));
1120 c03c6cb3 2019-02-08 stsp if (s == NULL)
1121 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
1122 078e971d 2019-02-08 stsp d3s->overlap = s;
1123 078e971d 2019-02-08 stsp memset(d3s->overlap + d3s->szchanges, 0, incr * sizeof(*d3s->overlap));
1124 078e971d 2019-02-08 stsp d3s->szchanges = newsz;
1126 c03c6cb3 2019-02-08 stsp return NULL;