Blame


1 74b37681 2019-02-07 stsp /* $OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert Exp $ */
2 74b37681 2019-02-07 stsp
3 74b37681 2019-02-07 stsp /*
4 74b37681 2019-02-07 stsp * Copyright (C) Caldera International Inc. 2001-2002.
5 74b37681 2019-02-07 stsp * All rights reserved.
6 74b37681 2019-02-07 stsp *
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
9 74b37681 2019-02-07 stsp * are met:
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.
22 74b37681 2019-02-07 stsp *
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.
35 74b37681 2019-02-07 stsp */
36 74b37681 2019-02-07 stsp /*-
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.
39 74b37681 2019-02-07 stsp *
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
42 74b37681 2019-02-07 stsp * are met:
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.
51 74b37681 2019-02-07 stsp *
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.
63 74b37681 2019-02-07 stsp *
64 74b37681 2019-02-07 stsp * @(#)diff3.c 8.1 (Berkeley) 6/6/93
65 74b37681 2019-02-07 stsp */
66 74b37681 2019-02-07 stsp
67 c03c6cb3 2019-02-08 stsp #include <sys/stat.h>
68 c03c6cb3 2019-02-08 stsp #include <sys/queue.h>
69 c03c6cb3 2019-02-08 stsp
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>
78 74b37681 2019-02-07 stsp
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"
82 74b37681 2019-02-07 stsp
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"
87 c03c6cb3 2019-02-08 stsp
88 0d26ce36 2019-02-08 stsp #ifndef nitems
89 0d26ce36 2019-02-08 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
90 0d26ce36 2019-02-08 stsp #endif
91 0d26ce36 2019-02-08 stsp
92 c03c6cb3 2019-02-08 stsp /* flags shared between merge(1) and rcsmerge(1) */
93 c03c6cb3 2019-02-08 stsp #define MERGE_EFLAG (1<<16)
94 c03c6cb3 2019-02-08 stsp #define MERGE_OFLAG (1<<17)
95 c03c6cb3 2019-02-08 stsp
96 74b37681 2019-02-07 stsp /* diff3 - 3-way differential file comparison */
97 74b37681 2019-02-07 stsp
98 74b37681 2019-02-07 stsp /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
99 74b37681 2019-02-07 stsp *
100 74b37681 2019-02-07 stsp * d13 = diff report on f1 vs f3
101 74b37681 2019-02-07 stsp * d23 = diff report on f2 vs f3
102 74b37681 2019-02-07 stsp * f1, f2, f3 the 3 files
103 74b37681 2019-02-07 stsp * if changes in f1 overlap with changes in f3, m1 and m3 are used
104 74b37681 2019-02-07 stsp * to mark the overlaps; otherwise, the file names f1 and f3 are used
105 74b37681 2019-02-07 stsp * (only for options E and X).
106 74b37681 2019-02-07 stsp */
107 74b37681 2019-02-07 stsp
108 74b37681 2019-02-07 stsp /*
109 74b37681 2019-02-07 stsp * "from" is first in range of changed lines; "to" is last+1
110 74b37681 2019-02-07 stsp * from=to=line after point of insertion for added lines.
111 74b37681 2019-02-07 stsp */
112 74b37681 2019-02-07 stsp struct range {
113 74b37681 2019-02-07 stsp int from;
114 74b37681 2019-02-07 stsp int to;
115 74b37681 2019-02-07 stsp };
116 74b37681 2019-02-07 stsp
117 74b37681 2019-02-07 stsp struct diff {
118 74b37681 2019-02-07 stsp struct range old;
119 74b37681 2019-02-07 stsp struct range new;
120 74b37681 2019-02-07 stsp };
121 74b37681 2019-02-07 stsp
122 078e971d 2019-02-08 stsp struct diff3_state {
123 078e971d 2019-02-08 stsp size_t szchanges;
124 74b37681 2019-02-07 stsp
125 078e971d 2019-02-08 stsp struct diff *d13;
126 078e971d 2019-02-08 stsp struct diff *d23;
127 74b37681 2019-02-07 stsp
128 078e971d 2019-02-08 stsp /*
129 078e971d 2019-02-08 stsp * "de" is used to gather editing scripts. These are later spewed out
130 078e971d 2019-02-08 stsp * in reverse order. Its first element must be all zero, the "new"
131 078e971d 2019-02-08 stsp * component of "de" contains line positions or byte positions
132 078e971d 2019-02-08 stsp * depending on when you look (!?). Array overlap indicates which
133 078e971d 2019-02-08 stsp * sections in "de" correspond to lines that are different in all
134 078e971d 2019-02-08 stsp * three files.
135 078e971d 2019-02-08 stsp */
136 078e971d 2019-02-08 stsp struct diff *de;
137 078e971d 2019-02-08 stsp char *overlap;
138 078e971d 2019-02-08 stsp int overlapcnt;
139 078e971d 2019-02-08 stsp FILE *fp[3];
140 078e971d 2019-02-08 stsp int cline[3]; /* # of the last-read line in each file (0-2) */
141 74b37681 2019-02-07 stsp
142 078e971d 2019-02-08 stsp /*
143 078e971d 2019-02-08 stsp * the latest known correspondence between line numbers of the 3 files
144 078e971d 2019-02-08 stsp * is stored in last[1-3];
145 078e971d 2019-02-08 stsp */
146 078e971d 2019-02-08 stsp int last[4];
147 078e971d 2019-02-08 stsp int eflag;
148 078e971d 2019-02-08 stsp int oflag;
149 078e971d 2019-02-08 stsp int debug;
150 078e971d 2019-02-08 stsp char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
151 dfc4f8fe 2019-02-08 stsp
152 dfc4f8fe 2019-02-08 stsp char *buf;
153 dfc4f8fe 2019-02-08 stsp size_t bufsize;
154 078e971d 2019-02-08 stsp };
155 078e971d 2019-02-08 stsp
156 c03c6cb3 2019-02-08 stsp static BUF *diffbuf;
157 c03c6cb3 2019-02-08 stsp
158 078e971d 2019-02-08 stsp static int duplicate(struct range *, struct range *, struct diff3_state *);
159 078e971d 2019-02-08 stsp static int edit(struct diff *, int, int, struct diff3_state *);
160 dfc4f8fe 2019-02-08 stsp static char *getchange(FILE *, struct diff3_state *);
161 dfc4f8fe 2019-02-08 stsp static char *get_line(FILE *, size_t *, struct diff3_state *);
162 74b37681 2019-02-07 stsp static int number(char **);
163 b90524b1 2019-02-08 stsp static const struct got_error *readin(size_t *, char *, struct diff **,
164 b90524b1 2019-02-08 stsp struct diff3_state *);
165 c03c6cb3 2019-02-08 stsp static int ed_patch_lines(struct rcs_lines *, struct rcs_lines *);
166 078e971d 2019-02-08 stsp static int skip(int, int, char *, struct diff3_state *);
167 078e971d 2019-02-08 stsp static int edscript(int, struct diff3_state *);
168 078e971d 2019-02-08 stsp static int merge(size_t, size_t, struct diff3_state *);
169 078e971d 2019-02-08 stsp static void change(int, struct range *, int, struct diff3_state *);
170 078e971d 2019-02-08 stsp static void keep(int, struct range *, struct diff3_state *);
171 74b37681 2019-02-07 stsp static void prange(struct range *);
172 078e971d 2019-02-08 stsp static void repos(int, struct diff3_state *);
173 74b37681 2019-02-07 stsp static void separate(const char *);
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 078e971d 2019-02-08 stsp char *, char *, const char *, const char *, struct diff3_state *);
177 74b37681 2019-02-07 stsp
178 74b37681 2019-02-07 stsp int diff3_conflicts = 0;
179 74b37681 2019-02-07 stsp
180 c03c6cb3 2019-02-08 stsp static const struct got_error *
181 c03c6cb3 2019-02-08 stsp diff_output(const char *fmt, ...)
182 c03c6cb3 2019-02-08 stsp {
183 c03c6cb3 2019-02-08 stsp va_list vap;
184 c03c6cb3 2019-02-08 stsp int i;
185 c03c6cb3 2019-02-08 stsp char *str;
186 c03c6cb3 2019-02-08 stsp size_t newsize;
187 c03c6cb3 2019-02-08 stsp
188 c03c6cb3 2019-02-08 stsp va_start(vap, fmt);
189 c03c6cb3 2019-02-08 stsp i = vasprintf(&str, fmt, vap);
190 c03c6cb3 2019-02-08 stsp va_end(vap);
191 c03c6cb3 2019-02-08 stsp if (i == -1)
192 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
193 c03c6cb3 2019-02-08 stsp if (diffbuf != NULL)
194 c03c6cb3 2019-02-08 stsp buf_append(&newsize, diffbuf, str, strlen(str));
195 c03c6cb3 2019-02-08 stsp else
196 c03c6cb3 2019-02-08 stsp printf("%s", str);
197 c03c6cb3 2019-02-08 stsp free(str);
198 c03c6cb3 2019-02-08 stsp return NULL;
199 c03c6cb3 2019-02-08 stsp }
200 c03c6cb3 2019-02-08 stsp
201 c03c6cb3 2019-02-08 stsp static const struct got_error*
202 c03c6cb3 2019-02-08 stsp diffreg(BUF **d, const char *path1, const char *path2)
203 c03c6cb3 2019-02-08 stsp {
204 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
205 c03c6cb3 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
206 c03c6cb3 2019-02-08 stsp char *outpath = NULL;
207 c03c6cb3 2019-02-08 stsp struct got_diff_state ds;
208 c03c6cb3 2019-02-08 stsp struct got_diff_args args;
209 c03c6cb3 2019-02-08 stsp int res;
210 c03c6cb3 2019-02-08 stsp
211 c03c6cb3 2019-02-08 stsp *d = NULL;
212 c03c6cb3 2019-02-08 stsp
213 c03c6cb3 2019-02-08 stsp f1 = fopen(path1, "r");
214 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
215 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
216 c03c6cb3 2019-02-08 stsp goto done;
217 c03c6cb3 2019-02-08 stsp }
218 c03c6cb3 2019-02-08 stsp f2 = fopen(path2, "r");
219 c03c6cb3 2019-02-08 stsp if (f1 == NULL) {
220 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
221 c03c6cb3 2019-02-08 stsp goto done;
222 c03c6cb3 2019-02-08 stsp }
223 c03c6cb3 2019-02-08 stsp
224 c03c6cb3 2019-02-08 stsp err = got_opentemp_named(&outpath, &outfile, "/tmp/got-diff");
225 c03c6cb3 2019-02-08 stsp if (err)
226 c03c6cb3 2019-02-08 stsp goto done;
227 c03c6cb3 2019-02-08 stsp
228 c03c6cb3 2019-02-08 stsp memset(&ds, 0, sizeof(ds));
229 c03c6cb3 2019-02-08 stsp /* XXX should stat buffers be passed in args instead of ds? */
230 c03c6cb3 2019-02-08 stsp if (stat(path1, &ds.stb1) == -1) {
231 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
232 c03c6cb3 2019-02-08 stsp goto done;
233 c03c6cb3 2019-02-08 stsp }
234 c03c6cb3 2019-02-08 stsp if (stat(path2, &ds.stb2) == -1) {
235 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
236 c03c6cb3 2019-02-08 stsp goto done;
237 c03c6cb3 2019-02-08 stsp }
238 c03c6cb3 2019-02-08 stsp
239 c03c6cb3 2019-02-08 stsp memset(&args, 0, sizeof(args));
240 c03c6cb3 2019-02-08 stsp args.diff_format = D_NORMAL;
241 c03c6cb3 2019-02-08 stsp args.label[0] = "";
242 c03c6cb3 2019-02-08 stsp args.label[1] = "";
243 c03c6cb3 2019-02-08 stsp args.diff_context = 0;
244 c03c6cb3 2019-02-08 stsp
245 c03c6cb3 2019-02-08 stsp err = got_diffreg(&res, f1, f2, D_FORCEASCII, &args, &ds,
246 c03c6cb3 2019-02-08 stsp outfile, NULL);
247 c03c6cb3 2019-02-08 stsp if (err)
248 c03c6cb3 2019-02-08 stsp goto done;
249 c03c6cb3 2019-02-08 stsp
250 c03c6cb3 2019-02-08 stsp *d = buf_load(outpath);
251 c03c6cb3 2019-02-08 stsp if (*d == NULL)
252 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
253 c03c6cb3 2019-02-08 stsp done:
254 c03c6cb3 2019-02-08 stsp free(outpath);
255 c03c6cb3 2019-02-08 stsp if (outfile)
256 c03c6cb3 2019-02-08 stsp fclose(outfile);
257 c03c6cb3 2019-02-08 stsp if (f1)
258 c03c6cb3 2019-02-08 stsp fclose(f1);
259 c03c6cb3 2019-02-08 stsp if (f2)
260 c03c6cb3 2019-02-08 stsp fclose(f2);
261 c03c6cb3 2019-02-08 stsp return err;
262 c03c6cb3 2019-02-08 stsp }
263 c03c6cb3 2019-02-08 stsp
264 74b37681 2019-02-07 stsp /*
265 74b37681 2019-02-07 stsp * For merge(1).
266 74b37681 2019-02-07 stsp */
267 c03c6cb3 2019-02-08 stsp const struct got_error *
268 62f59172 2019-02-08 stsp merge_diff3(int outfd, char *p1, char *p2, char *p3, int flags)
269 74b37681 2019-02-07 stsp {
270 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
271 c03c6cb3 2019-02-08 stsp char *dp13, *dp23, *path1, *path2, *path3;
272 74b37681 2019-02-07 stsp BUF *b1, *b2, *b3, *d1, *d2, *diffb;
273 74b37681 2019-02-07 stsp u_char *data, *patch;
274 74b37681 2019-02-07 stsp size_t dlen, plen;
275 c03c6cb3 2019-02-08 stsp struct wklhead temp_files;
276 078e971d 2019-02-08 stsp struct diff3_state *d3s;
277 0d26ce36 2019-02-08 stsp int i;
278 74b37681 2019-02-07 stsp
279 078e971d 2019-02-08 stsp d3s = calloc(1, sizeof(*d3s));
280 078e971d 2019-02-08 stsp if (d3s == NULL)
281 078e971d 2019-02-08 stsp return got_error_from_errno();
282 078e971d 2019-02-08 stsp d3s->eflag = 3; /* default -E for compatibility with former RCS */
283 078e971d 2019-02-08 stsp d3s->oflag = 1; /* default -E for compatibility with former RCS */
284 078e971d 2019-02-08 stsp
285 74b37681 2019-02-07 stsp b1 = b2 = b3 = d1 = d2 = diffb = NULL;
286 74b37681 2019-02-07 stsp dp13 = dp23 = path1 = path2 = path3 = NULL;
287 74b37681 2019-02-07 stsp data = patch = NULL;
288 74b37681 2019-02-07 stsp
289 74b37681 2019-02-07 stsp if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
290 078e971d 2019-02-08 stsp d3s->oflag = 0;
291 74b37681 2019-02-07 stsp
292 0ec6f014 2019-02-08 stsp if ((b1 = buf_load(p1)) == NULL)
293 74b37681 2019-02-07 stsp goto out;
294 0ec6f014 2019-02-08 stsp if ((b2 = buf_load(p2)) == NULL)
295 74b37681 2019-02-07 stsp goto out;
296 0ec6f014 2019-02-08 stsp if ((b3 = buf_load(p3)) == NULL)
297 74b37681 2019-02-07 stsp goto out;
298 74b37681 2019-02-07 stsp
299 74b37681 2019-02-07 stsp diffb = buf_alloc(128);
300 74b37681 2019-02-07 stsp
301 c03c6cb3 2019-02-08 stsp if (asprintf(&path1, "/tmp/got-diff1.XXXXXXXX") == -1) {
302 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
303 74b37681 2019-02-07 stsp goto out;
304 74b37681 2019-02-07 stsp }
305 c03c6cb3 2019-02-08 stsp if (asprintf(&path2, "/tmp/got-diff2.XXXXXXXX") == -1) {
306 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
307 74b37681 2019-02-07 stsp goto out;
308 74b37681 2019-02-07 stsp }
309 c03c6cb3 2019-02-08 stsp if (asprintf(&path3, "/tmp/got-diff3.XXXXXXXX") == -1) {
310 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
311 74b37681 2019-02-07 stsp goto out;
312 c03c6cb3 2019-02-08 stsp }
313 74b37681 2019-02-07 stsp
314 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b1, path1, &temp_files);
315 c03c6cb3 2019-02-08 stsp if (err)
316 74b37681 2019-02-07 stsp goto out;
317 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b2, path2, &temp_files);
318 c03c6cb3 2019-02-08 stsp if (err)
319 74b37681 2019-02-07 stsp goto out;
320 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(b3, path3, &temp_files);
321 c03c6cb3 2019-02-08 stsp if (err)
322 74b37681 2019-02-07 stsp goto out;
323 74b37681 2019-02-07 stsp
324 74b37681 2019-02-07 stsp buf_free(b2);
325 74b37681 2019-02-07 stsp b2 = NULL;
326 74b37681 2019-02-07 stsp
327 c03c6cb3 2019-02-08 stsp err = diffreg(&d1, path1, path3);
328 c03c6cb3 2019-02-08 stsp if (err) {
329 74b37681 2019-02-07 stsp buf_free(diffb);
330 74b37681 2019-02-07 stsp diffb = NULL;
331 74b37681 2019-02-07 stsp goto out;
332 c03c6cb3 2019-02-08 stsp
333 74b37681 2019-02-07 stsp }
334 c03c6cb3 2019-02-08 stsp err = diffreg(&d2, path2, path3);
335 c03c6cb3 2019-02-08 stsp if (err) {
336 c03c6cb3 2019-02-08 stsp buf_free(diffb);
337 c03c6cb3 2019-02-08 stsp diffb = NULL;
338 c03c6cb3 2019-02-08 stsp goto out;
339 c03c6cb3 2019-02-08 stsp }
340 74b37681 2019-02-07 stsp
341 c03c6cb3 2019-02-08 stsp if (asprintf(&dp13, "/tmp/got-d13.XXXXXXXXXX") == -1) {
342 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
343 c03c6cb3 2019-02-08 stsp goto out;
344 c03c6cb3 2019-02-08 stsp }
345 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(d1, dp13, &temp_files);
346 c03c6cb3 2019-02-08 stsp if (err)
347 c03c6cb3 2019-02-08 stsp goto out;
348 74b37681 2019-02-07 stsp
349 74b37681 2019-02-07 stsp buf_free(d1);
350 74b37681 2019-02-07 stsp d1 = NULL;
351 74b37681 2019-02-07 stsp
352 c03c6cb3 2019-02-08 stsp if (asprintf(&dp23, "/tmp/got-d23.XXXXXXXXXX") == -1) {
353 c03c6cb3 2019-02-08 stsp err = got_error_from_errno();
354 c03c6cb3 2019-02-08 stsp goto out;
355 c03c6cb3 2019-02-08 stsp }
356 c03c6cb3 2019-02-08 stsp err = buf_write_stmp(d2, dp23, &temp_files);
357 c03c6cb3 2019-02-08 stsp if (err)
358 c03c6cb3 2019-02-08 stsp goto out;
359 74b37681 2019-02-07 stsp
360 74b37681 2019-02-07 stsp buf_free(d2);
361 74b37681 2019-02-07 stsp d2 = NULL;
362 74b37681 2019-02-07 stsp
363 74b37681 2019-02-07 stsp diffbuf = diffb;
364 0ec6f014 2019-02-08 stsp err = diff3_internal(dp13, dp23, path1, path2, path3, p1, p3,
365 078e971d 2019-02-08 stsp d3s);
366 c03c6cb3 2019-02-08 stsp if (err) {
367 74b37681 2019-02-07 stsp buf_free(diffb);
368 74b37681 2019-02-07 stsp diffb = NULL;
369 74b37681 2019-02-07 stsp goto out;
370 74b37681 2019-02-07 stsp }
371 74b37681 2019-02-07 stsp
372 74b37681 2019-02-07 stsp plen = buf_len(diffb);
373 74b37681 2019-02-07 stsp patch = buf_release(diffb);
374 74b37681 2019-02-07 stsp dlen = buf_len(b1);
375 74b37681 2019-02-07 stsp data = buf_release(b1);
376 74b37681 2019-02-07 stsp
377 74b37681 2019-02-07 stsp if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
378 74b37681 2019-02-07 stsp goto out;
379 74b37681 2019-02-07 stsp out:
380 74b37681 2019-02-07 stsp buf_free(b2);
381 74b37681 2019-02-07 stsp buf_free(b3);
382 74b37681 2019-02-07 stsp buf_free(d1);
383 74b37681 2019-02-07 stsp buf_free(d2);
384 74b37681 2019-02-07 stsp
385 74b37681 2019-02-07 stsp (void)unlink(path1);
386 74b37681 2019-02-07 stsp (void)unlink(path2);
387 74b37681 2019-02-07 stsp (void)unlink(path3);
388 74b37681 2019-02-07 stsp (void)unlink(dp13);
389 74b37681 2019-02-07 stsp (void)unlink(dp23);
390 74b37681 2019-02-07 stsp
391 74b37681 2019-02-07 stsp free(path1);
392 74b37681 2019-02-07 stsp free(path2);
393 74b37681 2019-02-07 stsp free(path3);
394 74b37681 2019-02-07 stsp free(dp13);
395 74b37681 2019-02-07 stsp free(dp23);
396 74b37681 2019-02-07 stsp free(data);
397 74b37681 2019-02-07 stsp free(patch);
398 74b37681 2019-02-07 stsp
399 c03c6cb3 2019-02-08 stsp worklist_clean(&temp_files, worklist_unlink);
400 c03c6cb3 2019-02-08 stsp
401 0d26ce36 2019-02-08 stsp for (i = 0; i < nitems(d3s->fp); i++) {
402 0d26ce36 2019-02-08 stsp if (d3s->fp[i])
403 0d26ce36 2019-02-08 stsp fclose(d3s->fp[i]);
404 0d26ce36 2019-02-08 stsp }
405 0d26ce36 2019-02-08 stsp free(d3s);
406 62f59172 2019-02-08 stsp if (err == NULL) {
407 62f59172 2019-02-08 stsp if (buf_write_fd(diffb, outfd) < 0)
408 62f59172 2019-02-08 stsp err = got_error_from_errno();
409 62f59172 2019-02-08 stsp }
410 62f59172 2019-02-08 stsp buf_free(diffb);
411 c03c6cb3 2019-02-08 stsp return err;
412 74b37681 2019-02-07 stsp }
413 74b37681 2019-02-07 stsp
414 c03c6cb3 2019-02-08 stsp static const struct got_error *
415 c03c6cb3 2019-02-08 stsp diff3_internal(char *dp13, char *dp23, char *path1, char *path2, char *path3,
416 078e971d 2019-02-08 stsp const char *fmark, const char *rmark, struct diff3_state *d3s)
417 74b37681 2019-02-07 stsp {
418 c03c6cb3 2019-02-08 stsp const struct got_error *err = NULL;
419 74b37681 2019-02-07 stsp ssize_t m, n;
420 74b37681 2019-02-07 stsp int i;
421 74b37681 2019-02-07 stsp
422 078e971d 2019-02-08 stsp i = snprintf(d3s->f1mark, sizeof(d3s->f1mark), "<<<<<<< %s", fmark);
423 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f1mark))
424 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
425 74b37681 2019-02-07 stsp
426 078e971d 2019-02-08 stsp i = snprintf(d3s->f3mark, sizeof(d3s->f3mark), ">>>>>>> %s", rmark);
427 078e971d 2019-02-08 stsp if (i < 0 || i >= (int)sizeof(d3s->f3mark))
428 c03c6cb3 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
429 74b37681 2019-02-07 stsp
430 078e971d 2019-02-08 stsp err = increase(d3s);
431 c03c6cb3 2019-02-08 stsp if (err)
432 c03c6cb3 2019-02-08 stsp return err;
433 b90524b1 2019-02-08 stsp
434 b90524b1 2019-02-08 stsp err = readin(&m, dp13, &d3s->d13, d3s);
435 b90524b1 2019-02-08 stsp if (err)
436 b90524b1 2019-02-08 stsp return err;
437 b90524b1 2019-02-08 stsp err = readin(&n, dp23, &d3s->d23, d3s);
438 b90524b1 2019-02-08 stsp if (err)
439 b90524b1 2019-02-08 stsp return err;
440 74b37681 2019-02-07 stsp
441 078e971d 2019-02-08 stsp if ((d3s->fp[0] = fopen(path1, "r")) == NULL)
442 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
443 078e971d 2019-02-08 stsp if ((d3s->fp[1] = fopen(path2, "r")) == NULL)
444 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
445 078e971d 2019-02-08 stsp if ((d3s->fp[2] = fopen(path3, "r")) == NULL)
446 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
447 74b37681 2019-02-07 stsp
448 078e971d 2019-02-08 stsp if (merge(m, n, d3s) < 0)
449 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
450 c03c6cb3 2019-02-08 stsp return NULL;
451 74b37681 2019-02-07 stsp }
452 74b37681 2019-02-07 stsp
453 c03c6cb3 2019-02-08 stsp static int
454 74b37681 2019-02-07 stsp ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
455 74b37681 2019-02-07 stsp {
456 74b37681 2019-02-07 stsp char op, *ep;
457 74b37681 2019-02-07 stsp struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
458 74b37681 2019-02-07 stsp int start, end, i, lineno;
459 74b37681 2019-02-07 stsp u_char tmp;
460 74b37681 2019-02-07 stsp
461 74b37681 2019-02-07 stsp dlp = TAILQ_FIRST(&(dlines->l_lines));
462 74b37681 2019-02-07 stsp lp = TAILQ_FIRST(&(plines->l_lines));
463 74b37681 2019-02-07 stsp
464 74b37681 2019-02-07 stsp end = 0;
465 74b37681 2019-02-07 stsp for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
466 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list)) {
467 74b37681 2019-02-07 stsp /* Skip blank lines */
468 74b37681 2019-02-07 stsp if (lp->l_len < 2)
469 74b37681 2019-02-07 stsp continue;
470 74b37681 2019-02-07 stsp
471 74b37681 2019-02-07 stsp /* NUL-terminate line buffer for strtol() safety. */
472 74b37681 2019-02-07 stsp tmp = lp->l_line[lp->l_len - 1];
473 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = '\0';
474 74b37681 2019-02-07 stsp
475 74b37681 2019-02-07 stsp /* len - 1 is NUL terminator so we use len - 2 for 'op' */
476 74b37681 2019-02-07 stsp op = lp->l_line[lp->l_len - 2];
477 74b37681 2019-02-07 stsp start = (int)strtol(lp->l_line, &ep, 10);
478 74b37681 2019-02-07 stsp
479 74b37681 2019-02-07 stsp /* Restore the last byte of the buffer */
480 74b37681 2019-02-07 stsp lp->l_line[lp->l_len - 1] = tmp;
481 74b37681 2019-02-07 stsp
482 74b37681 2019-02-07 stsp if (op == 'a') {
483 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
484 74b37681 2019-02-07 stsp start < 0 || *ep != 'a')
485 c03c6cb3 2019-02-08 stsp return -1;
486 74b37681 2019-02-07 stsp } else if (op == 'c') {
487 74b37681 2019-02-07 stsp if (start > dlines->l_nblines ||
488 74b37681 2019-02-07 stsp start < 0 || (*ep != ',' && *ep != 'c'))
489 c03c6cb3 2019-02-08 stsp return -1;
490 74b37681 2019-02-07 stsp
491 74b37681 2019-02-07 stsp if (*ep == ',') {
492 74b37681 2019-02-07 stsp ep++;
493 74b37681 2019-02-07 stsp end = (int)strtol(ep, &ep, 10);
494 74b37681 2019-02-07 stsp if (end < 0 || *ep != 'c')
495 c03c6cb3 2019-02-08 stsp return -1;
496 74b37681 2019-02-07 stsp } else {
497 74b37681 2019-02-07 stsp end = start;
498 74b37681 2019-02-07 stsp }
499 74b37681 2019-02-07 stsp }
500 74b37681 2019-02-07 stsp
501 74b37681 2019-02-07 stsp
502 74b37681 2019-02-07 stsp for (;;) {
503 74b37681 2019-02-07 stsp if (dlp == NULL)
504 74b37681 2019-02-07 stsp break;
505 74b37681 2019-02-07 stsp if (dlp->l_lineno == start)
506 74b37681 2019-02-07 stsp break;
507 74b37681 2019-02-07 stsp if (dlp->l_lineno > start) {
508 74b37681 2019-02-07 stsp dlp = TAILQ_PREV(dlp, tqh, l_list);
509 74b37681 2019-02-07 stsp } else if (dlp->l_lineno < start) {
510 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
511 74b37681 2019-02-07 stsp if (ndlp->l_lineno > start)
512 74b37681 2019-02-07 stsp break;
513 74b37681 2019-02-07 stsp dlp = ndlp;
514 74b37681 2019-02-07 stsp }
515 74b37681 2019-02-07 stsp }
516 74b37681 2019-02-07 stsp
517 74b37681 2019-02-07 stsp if (dlp == NULL)
518 c03c6cb3 2019-02-08 stsp return -1;
519 74b37681 2019-02-07 stsp
520 74b37681 2019-02-07 stsp
521 74b37681 2019-02-07 stsp if (op == 'c') {
522 74b37681 2019-02-07 stsp insert_after = TAILQ_PREV(dlp, tqh, l_list);
523 74b37681 2019-02-07 stsp for (i = 0; i <= (end - start); i++) {
524 74b37681 2019-02-07 stsp ndlp = TAILQ_NEXT(dlp, l_list);
525 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
526 74b37681 2019-02-07 stsp dlp = ndlp;
527 74b37681 2019-02-07 stsp }
528 74b37681 2019-02-07 stsp dlp = insert_after;
529 74b37681 2019-02-07 stsp }
530 74b37681 2019-02-07 stsp
531 74b37681 2019-02-07 stsp if (op == 'a' || op == 'c') {
532 74b37681 2019-02-07 stsp for (;;) {
533 74b37681 2019-02-07 stsp ndlp = lp;
534 74b37681 2019-02-07 stsp lp = TAILQ_NEXT(lp, l_list);
535 74b37681 2019-02-07 stsp if (lp == NULL)
536 c03c6cb3 2019-02-08 stsp return -1;
537 74b37681 2019-02-07 stsp
538 74b37681 2019-02-07 stsp if (!memcmp(lp->l_line, ".", 1))
539 74b37681 2019-02-07 stsp break;
540 74b37681 2019-02-07 stsp
541 74b37681 2019-02-07 stsp TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
542 74b37681 2019-02-07 stsp TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
543 74b37681 2019-02-07 stsp lp, l_list);
544 74b37681 2019-02-07 stsp dlp = lp;
545 74b37681 2019-02-07 stsp
546 74b37681 2019-02-07 stsp lp->l_lineno = start;
547 74b37681 2019-02-07 stsp lp = ndlp;
548 74b37681 2019-02-07 stsp }
549 74b37681 2019-02-07 stsp }
550 74b37681 2019-02-07 stsp
551 74b37681 2019-02-07 stsp /*
552 74b37681 2019-02-07 stsp * always resort lines as the markers might be put at the
553 74b37681 2019-02-07 stsp * same line as we first started editing.
554 74b37681 2019-02-07 stsp */
555 74b37681 2019-02-07 stsp lineno = 0;
556 74b37681 2019-02-07 stsp TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
557 74b37681 2019-02-07 stsp sort->l_lineno = lineno++;
558 74b37681 2019-02-07 stsp dlines->l_nblines = lineno - 1;
559 74b37681 2019-02-07 stsp }
560 74b37681 2019-02-07 stsp
561 74b37681 2019-02-07 stsp return (0);
562 74b37681 2019-02-07 stsp }
563 74b37681 2019-02-07 stsp
564 74b37681 2019-02-07 stsp /*
565 74b37681 2019-02-07 stsp * Pick up the line numbers of all changes from one change file.
566 74b37681 2019-02-07 stsp * (This puts the numbers in a vector, which is not strictly necessary,
567 74b37681 2019-02-07 stsp * since the vector is processed in one sequential pass.
568 74b37681 2019-02-07 stsp * The vector could be optimized out of existence)
569 74b37681 2019-02-07 stsp */
570 b90524b1 2019-02-08 stsp static const struct got_error *
571 b90524b1 2019-02-08 stsp readin(size_t *n, char *name, struct diff **dd, struct diff3_state *d3s)
572 74b37681 2019-02-07 stsp {
573 b90524b1 2019-02-08 stsp const struct got_error *err = NULL;
574 74b37681 2019-02-07 stsp int a, b, c, d;
575 74b37681 2019-02-07 stsp char kind, *p;
576 74b37681 2019-02-07 stsp size_t i;
577 74b37681 2019-02-07 stsp
578 078e971d 2019-02-08 stsp d3s->fp[0] = fopen(name, "r");
579 078e971d 2019-02-08 stsp if (d3s->fp[0] == NULL)
580 b90524b1 2019-02-08 stsp return got_error_from_errno();
581 dfc4f8fe 2019-02-08 stsp for (i = 0; (p = getchange(d3s->fp[0], d3s)); i++) {
582 b90524b1 2019-02-08 stsp if (i >= d3s->szchanges - 1) {
583 b90524b1 2019-02-08 stsp err = increase(d3s);
584 b90524b1 2019-02-08 stsp if (err)
585 b90524b1 2019-02-08 stsp return err;
586 b90524b1 2019-02-08 stsp }
587 74b37681 2019-02-07 stsp a = b = number(&p);
588 74b37681 2019-02-07 stsp if (*p == ',') {
589 74b37681 2019-02-07 stsp p++;
590 74b37681 2019-02-07 stsp b = number(&p);
591 74b37681 2019-02-07 stsp }
592 74b37681 2019-02-07 stsp kind = *p++;
593 74b37681 2019-02-07 stsp c = d = number(&p);
594 74b37681 2019-02-07 stsp if (*p==',') {
595 74b37681 2019-02-07 stsp p++;
596 74b37681 2019-02-07 stsp d = number(&p);
597 74b37681 2019-02-07 stsp }
598 74b37681 2019-02-07 stsp if (kind == 'a')
599 74b37681 2019-02-07 stsp a++;
600 74b37681 2019-02-07 stsp if (kind == 'd')
601 74b37681 2019-02-07 stsp c++;
602 74b37681 2019-02-07 stsp b++;
603 74b37681 2019-02-07 stsp d++;
604 74b37681 2019-02-07 stsp (*dd)[i].old.from = a;
605 74b37681 2019-02-07 stsp (*dd)[i].old.to = b;
606 74b37681 2019-02-07 stsp (*dd)[i].new.from = c;
607 74b37681 2019-02-07 stsp (*dd)[i].new.to = d;
608 74b37681 2019-02-07 stsp }
609 74b37681 2019-02-07 stsp
610 74b37681 2019-02-07 stsp if (i) {
611 74b37681 2019-02-07 stsp (*dd)[i].old.from = (*dd)[i-1].old.to;
612 74b37681 2019-02-07 stsp (*dd)[i].new.from = (*dd)[i-1].new.to;
613 74b37681 2019-02-07 stsp }
614 74b37681 2019-02-07 stsp
615 078e971d 2019-02-08 stsp (void)fclose(d3s->fp[0]);
616 74b37681 2019-02-07 stsp
617 b90524b1 2019-02-08 stsp return NULL;
618 74b37681 2019-02-07 stsp }
619 74b37681 2019-02-07 stsp
620 74b37681 2019-02-07 stsp static int
621 74b37681 2019-02-07 stsp number(char **lc)
622 74b37681 2019-02-07 stsp {
623 74b37681 2019-02-07 stsp int nn;
624 74b37681 2019-02-07 stsp
625 74b37681 2019-02-07 stsp nn = 0;
626 74b37681 2019-02-07 stsp while (isdigit((unsigned char)(**lc)))
627 74b37681 2019-02-07 stsp nn = nn*10 + *(*lc)++ - '0';
628 74b37681 2019-02-07 stsp
629 74b37681 2019-02-07 stsp return (nn);
630 74b37681 2019-02-07 stsp }
631 74b37681 2019-02-07 stsp
632 74b37681 2019-02-07 stsp static char *
633 dfc4f8fe 2019-02-08 stsp getchange(FILE *b, struct diff3_state *d3s)
634 74b37681 2019-02-07 stsp {
635 74b37681 2019-02-07 stsp char *line;
636 74b37681 2019-02-07 stsp
637 dfc4f8fe 2019-02-08 stsp while ((line = get_line(b, NULL, d3s))) {
638 74b37681 2019-02-07 stsp if (isdigit((unsigned char)line[0]))
639 74b37681 2019-02-07 stsp return (line);
640 74b37681 2019-02-07 stsp }
641 74b37681 2019-02-07 stsp
642 74b37681 2019-02-07 stsp return (NULL);
643 74b37681 2019-02-07 stsp }
644 74b37681 2019-02-07 stsp
645 74b37681 2019-02-07 stsp static char *
646 dfc4f8fe 2019-02-08 stsp get_line(FILE *b, size_t *n, struct diff3_state *d3s)
647 74b37681 2019-02-07 stsp {
648 74b37681 2019-02-07 stsp char *cp;
649 74b37681 2019-02-07 stsp size_t len;
650 c03c6cb3 2019-02-08 stsp char *new;
651 74b37681 2019-02-07 stsp
652 74b37681 2019-02-07 stsp if ((cp = fgetln(b, &len)) == NULL)
653 74b37681 2019-02-07 stsp return (NULL);
654 74b37681 2019-02-07 stsp
655 74b37681 2019-02-07 stsp if (cp[len - 1] != '\n')
656 74b37681 2019-02-07 stsp len++;
657 dfc4f8fe 2019-02-08 stsp if (len + 1 > d3s->bufsize) {
658 74b37681 2019-02-07 stsp do {
659 dfc4f8fe 2019-02-08 stsp d3s->bufsize += 1024;
660 dfc4f8fe 2019-02-08 stsp } while (len + 1 > d3s->bufsize);
661 dfc4f8fe 2019-02-08 stsp new = reallocarray(d3s->buf, 1, d3s->bufsize);
662 c03c6cb3 2019-02-08 stsp if (new == NULL)
663 c03c6cb3 2019-02-08 stsp return NULL;
664 dfc4f8fe 2019-02-08 stsp d3s->buf = new;
665 74b37681 2019-02-07 stsp }
666 dfc4f8fe 2019-02-08 stsp memcpy(d3s->buf, cp, len - 1);
667 dfc4f8fe 2019-02-08 stsp d3s->buf[len - 1] = '\n';
668 dfc4f8fe 2019-02-08 stsp d3s->buf[len] = '\0';
669 74b37681 2019-02-07 stsp if (n != NULL)
670 74b37681 2019-02-07 stsp *n = len;
671 74b37681 2019-02-07 stsp
672 dfc4f8fe 2019-02-08 stsp return (d3s->buf);
673 74b37681 2019-02-07 stsp }
674 74b37681 2019-02-07 stsp
675 74b37681 2019-02-07 stsp static int
676 078e971d 2019-02-08 stsp merge(size_t m1, size_t m2, struct diff3_state *d3s)
677 74b37681 2019-02-07 stsp {
678 74b37681 2019-02-07 stsp struct diff *d1, *d2, *d3;
679 74b37681 2019-02-07 stsp int dpl, j, t1, t2;
680 74b37681 2019-02-07 stsp
681 078e971d 2019-02-08 stsp d1 = d3s->d13;
682 078e971d 2019-02-08 stsp d2 = d3s->d23;
683 74b37681 2019-02-07 stsp j = 0;
684 74b37681 2019-02-07 stsp for (;;) {
685 078e971d 2019-02-08 stsp t1 = (d1 < d3s->d13 + m1);
686 078e971d 2019-02-08 stsp t2 = (d2 < d3s->d23 + m2);
687 74b37681 2019-02-07 stsp if (!t1 && !t2)
688 74b37681 2019-02-07 stsp break;
689 74b37681 2019-02-07 stsp
690 078e971d 2019-02-08 stsp if (d3s->debug) {
691 74b37681 2019-02-07 stsp printf("%d,%d=%d,%d %d,%d=%d,%d\n",
692 74b37681 2019-02-07 stsp d1->old.from, d1->old.to,
693 74b37681 2019-02-07 stsp d1->new.from, d1->new.to,
694 74b37681 2019-02-07 stsp d2->old.from, d2->old.to,
695 74b37681 2019-02-07 stsp d2->new.from, d2->new.to);
696 74b37681 2019-02-07 stsp }
697 74b37681 2019-02-07 stsp
698 74b37681 2019-02-07 stsp /* first file is different from others */
699 74b37681 2019-02-07 stsp if (!t2 || (t1 && d1->new.to < d2->new.from)) {
700 74b37681 2019-02-07 stsp /* stuff peculiar to 1st file */
701 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
702 74b37681 2019-02-07 stsp separate("1");
703 078e971d 2019-02-08 stsp change(1, &d1->old, 0, d3s);
704 078e971d 2019-02-08 stsp keep(2, &d1->new, d3s);
705 078e971d 2019-02-08 stsp change(3, &d1->new, 0, d3s);
706 74b37681 2019-02-07 stsp }
707 74b37681 2019-02-07 stsp d1++;
708 74b37681 2019-02-07 stsp continue;
709 74b37681 2019-02-07 stsp }
710 74b37681 2019-02-07 stsp
711 74b37681 2019-02-07 stsp /* second file is different from others */
712 74b37681 2019-02-07 stsp if (!t1 || (t2 && d2->new.to < d1->new.from)) {
713 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
714 74b37681 2019-02-07 stsp separate("2");
715 078e971d 2019-02-08 stsp keep(1, &d2->new, d3s);
716 078e971d 2019-02-08 stsp change(2, &d2->old, 0, d3s);
717 078e971d 2019-02-08 stsp change(3, &d2->new, 0, d3s);
718 74b37681 2019-02-07 stsp }
719 74b37681 2019-02-07 stsp d2++;
720 74b37681 2019-02-07 stsp continue;
721 74b37681 2019-02-07 stsp }
722 74b37681 2019-02-07 stsp
723 74b37681 2019-02-07 stsp /*
724 74b37681 2019-02-07 stsp * Merge overlapping changes in first file
725 74b37681 2019-02-07 stsp * this happens after extension (see below).
726 74b37681 2019-02-07 stsp */
727 078e971d 2019-02-08 stsp if (d1 + 1 < d3s->d13 + m1 && d1->new.to >= d1[1].new.from) {
728 74b37681 2019-02-07 stsp d1[1].old.from = d1->old.from;
729 74b37681 2019-02-07 stsp d1[1].new.from = d1->new.from;
730 74b37681 2019-02-07 stsp d1++;
731 74b37681 2019-02-07 stsp continue;
732 74b37681 2019-02-07 stsp }
733 74b37681 2019-02-07 stsp
734 74b37681 2019-02-07 stsp /* merge overlapping changes in second */
735 078e971d 2019-02-08 stsp if (d2 + 1 < d3s->d23 + m2 && d2->new.to >= d2[1].new.from) {
736 74b37681 2019-02-07 stsp d2[1].old.from = d2->old.from;
737 74b37681 2019-02-07 stsp d2[1].new.from = d2->new.from;
738 74b37681 2019-02-07 stsp d2++;
739 74b37681 2019-02-07 stsp continue;
740 74b37681 2019-02-07 stsp }
741 74b37681 2019-02-07 stsp /* stuff peculiar to third file or different in all */
742 74b37681 2019-02-07 stsp if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
743 078e971d 2019-02-08 stsp dpl = duplicate(&d1->old, &d2->old, d3s);
744 74b37681 2019-02-07 stsp if (dpl == -1)
745 74b37681 2019-02-07 stsp return (-1);
746 74b37681 2019-02-07 stsp
747 74b37681 2019-02-07 stsp /*
748 74b37681 2019-02-07 stsp * dpl = 0 means all files differ
749 74b37681 2019-02-07 stsp * dpl = 1 means files 1 and 2 identical
750 74b37681 2019-02-07 stsp */
751 078e971d 2019-02-08 stsp if (d3s->eflag == 0) {
752 74b37681 2019-02-07 stsp separate(dpl ? "3" : "");
753 078e971d 2019-02-08 stsp change(1, &d1->old, dpl, d3s);
754 078e971d 2019-02-08 stsp change(2, &d2->old, 0, d3s);
755 74b37681 2019-02-07 stsp d3 = d1->old.to > d1->old.from ? d1 : d2;
756 078e971d 2019-02-08 stsp change(3, &d3->new, 0, d3s);
757 74b37681 2019-02-07 stsp } else
758 078e971d 2019-02-08 stsp j = edit(d1, dpl, j, d3s);
759 74b37681 2019-02-07 stsp d1++;
760 74b37681 2019-02-07 stsp d2++;
761 74b37681 2019-02-07 stsp continue;
762 74b37681 2019-02-07 stsp }
763 74b37681 2019-02-07 stsp
764 74b37681 2019-02-07 stsp /*
765 74b37681 2019-02-07 stsp * Overlapping changes from file 1 and 2; extend changes
766 74b37681 2019-02-07 stsp * appropriately to make them coincide.
767 74b37681 2019-02-07 stsp */
768 74b37681 2019-02-07 stsp if (d1->new.from < d2->new.from) {
769 74b37681 2019-02-07 stsp d2->old.from -= d2->new.from-d1->new.from;
770 74b37681 2019-02-07 stsp d2->new.from = d1->new.from;
771 74b37681 2019-02-07 stsp } else if (d2->new.from < d1->new.from) {
772 74b37681 2019-02-07 stsp d1->old.from -= d1->new.from-d2->new.from;
773 74b37681 2019-02-07 stsp d1->new.from = d2->new.from;
774 74b37681 2019-02-07 stsp }
775 74b37681 2019-02-07 stsp if (d1->new.to > d2->new.to) {
776 74b37681 2019-02-07 stsp d2->old.to += d1->new.to - d2->new.to;
777 74b37681 2019-02-07 stsp d2->new.to = d1->new.to;
778 74b37681 2019-02-07 stsp } else if (d2->new.to > d1->new.to) {
779 74b37681 2019-02-07 stsp d1->old.to += d2->new.to - d1->new.to;
780 74b37681 2019-02-07 stsp d1->new.to = d2->new.to;
781 74b37681 2019-02-07 stsp }
782 74b37681 2019-02-07 stsp }
783 74b37681 2019-02-07 stsp
784 078e971d 2019-02-08 stsp return (edscript(j, d3s));
785 74b37681 2019-02-07 stsp }
786 74b37681 2019-02-07 stsp
787 74b37681 2019-02-07 stsp static void
788 74b37681 2019-02-07 stsp separate(const char *s)
789 74b37681 2019-02-07 stsp {
790 74b37681 2019-02-07 stsp diff_output("====%s\n", s);
791 74b37681 2019-02-07 stsp }
792 74b37681 2019-02-07 stsp
793 74b37681 2019-02-07 stsp /*
794 74b37681 2019-02-07 stsp * The range of lines rold.from thru rold.to in file i is to be changed.
795 74b37681 2019-02-07 stsp * It is to be printed only if it does not duplicate something to be
796 74b37681 2019-02-07 stsp * printed later.
797 74b37681 2019-02-07 stsp */
798 74b37681 2019-02-07 stsp static void
799 078e971d 2019-02-08 stsp change(int i, struct range *rold, int fdup, struct diff3_state *d3s)
800 74b37681 2019-02-07 stsp {
801 74b37681 2019-02-07 stsp diff_output("%d:", i);
802 078e971d 2019-02-08 stsp d3s->last[i] = rold->to;
803 74b37681 2019-02-07 stsp prange(rold);
804 078e971d 2019-02-08 stsp if (fdup || d3s->debug)
805 74b37681 2019-02-07 stsp return;
806 74b37681 2019-02-07 stsp i--;
807 078e971d 2019-02-08 stsp (void)skip(i, rold->from, NULL, d3s);
808 078e971d 2019-02-08 stsp (void)skip(i, rold->to, " ", d3s);
809 74b37681 2019-02-07 stsp }
810 74b37681 2019-02-07 stsp
811 74b37681 2019-02-07 stsp /*
812 74b37681 2019-02-07 stsp * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
813 74b37681 2019-02-07 stsp */
814 74b37681 2019-02-07 stsp static void
815 74b37681 2019-02-07 stsp prange(struct range *rold)
816 74b37681 2019-02-07 stsp {
817 74b37681 2019-02-07 stsp if (rold->to <= rold->from)
818 74b37681 2019-02-07 stsp diff_output("%da\n", rold->from - 1);
819 74b37681 2019-02-07 stsp else {
820 74b37681 2019-02-07 stsp diff_output("%d", rold->from);
821 74b37681 2019-02-07 stsp if (rold->to > rold->from+1)
822 74b37681 2019-02-07 stsp diff_output(",%d", rold->to - 1);
823 74b37681 2019-02-07 stsp diff_output("c\n");
824 74b37681 2019-02-07 stsp }
825 74b37681 2019-02-07 stsp }
826 74b37681 2019-02-07 stsp
827 74b37681 2019-02-07 stsp /*
828 74b37681 2019-02-07 stsp * No difference was reported by diff between file 1 (or 2) and file 3,
829 74b37681 2019-02-07 stsp * and an artificial dummy difference (trange) must be ginned up to
830 74b37681 2019-02-07 stsp * correspond to the change reported in the other file.
831 74b37681 2019-02-07 stsp */
832 74b37681 2019-02-07 stsp static void
833 078e971d 2019-02-08 stsp keep(int i, struct range *rnew, struct diff3_state *d3s)
834 74b37681 2019-02-07 stsp {
835 74b37681 2019-02-07 stsp int delta;
836 74b37681 2019-02-07 stsp struct range trange;
837 74b37681 2019-02-07 stsp
838 078e971d 2019-02-08 stsp delta = d3s->last[3] - d3s->last[i];
839 74b37681 2019-02-07 stsp trange.from = rnew->from - delta;
840 74b37681 2019-02-07 stsp trange.to = rnew->to - delta;
841 078e971d 2019-02-08 stsp change(i, &trange, 1, d3s);
842 74b37681 2019-02-07 stsp }
843 74b37681 2019-02-07 stsp
844 74b37681 2019-02-07 stsp /*
845 74b37681 2019-02-07 stsp * skip to just before line number from in file "i". If "pr" is non-NULL,
846 74b37681 2019-02-07 stsp * print all skipped stuff with string pr as a prefix.
847 74b37681 2019-02-07 stsp */
848 74b37681 2019-02-07 stsp static int
849 078e971d 2019-02-08 stsp skip(int i, int from, char *pr, struct diff3_state *d3s)
850 74b37681 2019-02-07 stsp {
851 74b37681 2019-02-07 stsp size_t j, n;
852 74b37681 2019-02-07 stsp char *line;
853 74b37681 2019-02-07 stsp
854 078e971d 2019-02-08 stsp for (n = 0; d3s->cline[i] < from - 1; n += j) {
855 dfc4f8fe 2019-02-08 stsp if ((line = get_line(d3s->fp[i], &j, d3s)) == NULL)
856 74b37681 2019-02-07 stsp return (-1);
857 74b37681 2019-02-07 stsp if (pr != NULL)
858 74b37681 2019-02-07 stsp diff_output("%s%s", pr, line);
859 078e971d 2019-02-08 stsp d3s->cline[i]++;
860 74b37681 2019-02-07 stsp }
861 74b37681 2019-02-07 stsp return ((int) n);
862 74b37681 2019-02-07 stsp }
863 74b37681 2019-02-07 stsp
864 74b37681 2019-02-07 stsp /*
865 74b37681 2019-02-07 stsp * Return 1 or 0 according as the old range (in file 1) contains exactly
866 74b37681 2019-02-07 stsp * the same data as the new range (in file 2).
867 74b37681 2019-02-07 stsp */
868 74b37681 2019-02-07 stsp static int
869 078e971d 2019-02-08 stsp duplicate(struct range *r1, struct range *r2, struct diff3_state *d3s)
870 74b37681 2019-02-07 stsp {
871 74b37681 2019-02-07 stsp int c,d;
872 74b37681 2019-02-07 stsp int nchar;
873 74b37681 2019-02-07 stsp int nline;
874 74b37681 2019-02-07 stsp
875 74b37681 2019-02-07 stsp if (r1->to-r1->from != r2->to-r2->from)
876 74b37681 2019-02-07 stsp return (0);
877 078e971d 2019-02-08 stsp (void)skip(0, r1->from, NULL, d3s);
878 078e971d 2019-02-08 stsp (void)skip(1, r2->from, NULL, d3s);
879 74b37681 2019-02-07 stsp nchar = 0;
880 74b37681 2019-02-07 stsp for (nline=0; nline < r1->to - r1->from; nline++) {
881 74b37681 2019-02-07 stsp do {
882 078e971d 2019-02-08 stsp c = getc(d3s->fp[0]);
883 078e971d 2019-02-08 stsp d = getc(d3s->fp[1]);
884 74b37681 2019-02-07 stsp if (c == -1 || d== -1)
885 74b37681 2019-02-07 stsp return (-1);
886 74b37681 2019-02-07 stsp nchar++;
887 74b37681 2019-02-07 stsp if (c != d) {
888 078e971d 2019-02-08 stsp repos(nchar, d3s);
889 74b37681 2019-02-07 stsp return (0);
890 74b37681 2019-02-07 stsp }
891 74b37681 2019-02-07 stsp } while (c != '\n');
892 74b37681 2019-02-07 stsp }
893 078e971d 2019-02-08 stsp repos(nchar, d3s);
894 74b37681 2019-02-07 stsp return (1);
895 74b37681 2019-02-07 stsp }
896 74b37681 2019-02-07 stsp
897 74b37681 2019-02-07 stsp static void
898 078e971d 2019-02-08 stsp repos(int nchar, struct diff3_state *d3s)
899 74b37681 2019-02-07 stsp {
900 74b37681 2019-02-07 stsp int i;
901 74b37681 2019-02-07 stsp
902 74b37681 2019-02-07 stsp for (i = 0; i < 2; i++)
903 078e971d 2019-02-08 stsp (void)fseek(d3s->fp[i], (long)-nchar, SEEK_CUR);
904 74b37681 2019-02-07 stsp }
905 74b37681 2019-02-07 stsp
906 74b37681 2019-02-07 stsp /*
907 74b37681 2019-02-07 stsp * collect an editing script for later regurgitation
908 74b37681 2019-02-07 stsp */
909 74b37681 2019-02-07 stsp static int
910 078e971d 2019-02-08 stsp edit(struct diff *diff, int fdup, int j, struct diff3_state *d3s)
911 74b37681 2019-02-07 stsp {
912 078e971d 2019-02-08 stsp if (((fdup + 1) & d3s->eflag) == 0)
913 74b37681 2019-02-07 stsp return (j);
914 74b37681 2019-02-07 stsp j++;
915 078e971d 2019-02-08 stsp d3s->overlap[j] = !fdup;
916 74b37681 2019-02-07 stsp if (!fdup)
917 078e971d 2019-02-08 stsp d3s->overlapcnt++;
918 078e971d 2019-02-08 stsp d3s->de[j].old.from = diff->old.from;
919 078e971d 2019-02-08 stsp d3s->de[j].old.to = diff->old.to;
920 078e971d 2019-02-08 stsp d3s->de[j].new.from =
921 078e971d 2019-02-08 stsp d3s->de[j-1].new.to + skip(2, diff->new.from, NULL, d3s);
922 078e971d 2019-02-08 stsp d3s->de[j].new.to =
923 078e971d 2019-02-08 stsp d3s->de[j].new.from + skip(2, diff->new.to, NULL, d3s);
924 74b37681 2019-02-07 stsp return (j);
925 74b37681 2019-02-07 stsp }
926 74b37681 2019-02-07 stsp
927 74b37681 2019-02-07 stsp /* regurgitate */
928 74b37681 2019-02-07 stsp static int
929 078e971d 2019-02-08 stsp edscript(int n, struct diff3_state *d3s)
930 74b37681 2019-02-07 stsp {
931 74b37681 2019-02-07 stsp int j, k;
932 74b37681 2019-02-07 stsp char block[BUFSIZ+1];
933 74b37681 2019-02-07 stsp
934 74b37681 2019-02-07 stsp for (; n > 0; n--) {
935 078e971d 2019-02-08 stsp if (!d3s->oflag || !d3s->overlap[n])
936 078e971d 2019-02-08 stsp prange(&d3s->de[n].old);
937 74b37681 2019-02-07 stsp else
938 078e971d 2019-02-08 stsp diff_output("%da\n=======\n", d3s->de[n].old.to -1);
939 078e971d 2019-02-08 stsp (void)fseek(d3s->fp[2], (long)d3s->de[n].new.from, SEEK_SET);
940 078e971d 2019-02-08 stsp k = d3s->de[n].new.to - d3s->de[n].new.from;
941 078e971d 2019-02-08 stsp for (; k > 0; k-= j) {
942 74b37681 2019-02-07 stsp j = k > BUFSIZ ? BUFSIZ : k;
943 078e971d 2019-02-08 stsp if (fread(block, 1, j, d3s->fp[2]) != (size_t)j)
944 74b37681 2019-02-07 stsp return (-1);
945 74b37681 2019-02-07 stsp block[j] = '\0';
946 74b37681 2019-02-07 stsp diff_output("%s", block);
947 74b37681 2019-02-07 stsp }
948 74b37681 2019-02-07 stsp
949 078e971d 2019-02-08 stsp if (!d3s->oflag || !d3s->overlap[n])
950 74b37681 2019-02-07 stsp diff_output(".\n");
951 74b37681 2019-02-07 stsp else {
952 078e971d 2019-02-08 stsp diff_output("%s\n.\n", d3s->f3mark);
953 078e971d 2019-02-08 stsp diff_output("%da\n%s\n.\n", d3s->de[n].old.from - 1,
954 078e971d 2019-02-08 stsp d3s->f1mark);
955 74b37681 2019-02-07 stsp }
956 74b37681 2019-02-07 stsp }
957 74b37681 2019-02-07 stsp
958 078e971d 2019-02-08 stsp return (d3s->overlapcnt);
959 74b37681 2019-02-07 stsp }
960 74b37681 2019-02-07 stsp
961 c03c6cb3 2019-02-08 stsp static const struct got_error *
962 078e971d 2019-02-08 stsp increase(struct diff3_state *d3s)
963 74b37681 2019-02-07 stsp {
964 74b37681 2019-02-07 stsp size_t newsz, incr;
965 c03c6cb3 2019-02-08 stsp struct diff *d;
966 c03c6cb3 2019-02-08 stsp char *s;
967 74b37681 2019-02-07 stsp
968 74b37681 2019-02-07 stsp /* are the memset(3) calls needed? */
969 078e971d 2019-02-08 stsp newsz = d3s->szchanges == 0 ? 64 : 2 * d3s->szchanges;
970 078e971d 2019-02-08 stsp incr = newsz - d3s->szchanges;
971 74b37681 2019-02-07 stsp
972 078e971d 2019-02-08 stsp d = reallocarray(d3s->d13, newsz, sizeof(*d3s->d13));
973 c03c6cb3 2019-02-08 stsp if (d == NULL)
974 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
975 078e971d 2019-02-08 stsp d3s->d13 = d;
976 078e971d 2019-02-08 stsp memset(d3s->d13 + d3s->szchanges, 0, incr * sizeof(*d3s->d13));
977 c03c6cb3 2019-02-08 stsp
978 078e971d 2019-02-08 stsp d = reallocarray(d3s->d23, newsz, sizeof(*d3s->d23));
979 c03c6cb3 2019-02-08 stsp if (d == NULL)
980 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
981 078e971d 2019-02-08 stsp d3s->d23 = d;
982 078e971d 2019-02-08 stsp memset(d3s->d23 + d3s->szchanges, 0, incr * sizeof(*d3s->d23));
983 c03c6cb3 2019-02-08 stsp
984 078e971d 2019-02-08 stsp d = reallocarray(d3s->de, newsz, sizeof(*d3s->de));
985 c03c6cb3 2019-02-08 stsp if (d == NULL)
986 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
987 078e971d 2019-02-08 stsp d3s->de = d;
988 078e971d 2019-02-08 stsp memset(d3s->de + d3s->szchanges, 0, incr * sizeof(*d3s->de));
989 c03c6cb3 2019-02-08 stsp
990 078e971d 2019-02-08 stsp s = reallocarray(d3s->overlap, newsz, sizeof(*d3s->overlap));
991 c03c6cb3 2019-02-08 stsp if (s == NULL)
992 c03c6cb3 2019-02-08 stsp return got_error_from_errno();
993 078e971d 2019-02-08 stsp d3s->overlap = s;
994 078e971d 2019-02-08 stsp memset(d3s->overlap + d3s->szchanges, 0, incr * sizeof(*d3s->overlap));
995 078e971d 2019-02-08 stsp d3s->szchanges = newsz;
996 c03c6cb3 2019-02-08 stsp
997 c03c6cb3 2019-02-08 stsp return NULL;
998 74b37681 2019-02-07 stsp }