Blame


1 fe621944 2020-11-10 stsp /* Produce a unidiff output from a diff_result. */
2 fe621944 2020-11-10 stsp /*
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 fe621944 2020-11-10 stsp *
5 fe621944 2020-11-10 stsp * Permission to use, copy, modify, and distribute this software for any
6 fe621944 2020-11-10 stsp * purpose with or without fee is hereby granted, provided that the above
7 fe621944 2020-11-10 stsp * copyright notice and this permission notice appear in all copies.
8 fe621944 2020-11-10 stsp *
9 fe621944 2020-11-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 fe621944 2020-11-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 fe621944 2020-11-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 fe621944 2020-11-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 fe621944 2020-11-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 fe621944 2020-11-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 fe621944 2020-11-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 fe621944 2020-11-10 stsp */
17 fe621944 2020-11-10 stsp
18 fe621944 2020-11-10 stsp #include <errno.h>
19 fe621944 2020-11-10 stsp #include <stdbool.h>
20 f3c44083 2020-11-14 naddy #include <stdint.h>
21 fe621944 2020-11-10 stsp #include <stdio.h>
22 fe621944 2020-11-10 stsp #include <stdlib.h>
23 f3b2b552 2020-12-10 stsp #include <string.h>
24 fe621944 2020-11-10 stsp #include <assert.h>
25 fe621944 2020-11-10 stsp
26 fe621944 2020-11-10 stsp #include <arraylist.h>
27 fe621944 2020-11-10 stsp #include <diff_main.h>
28 fe621944 2020-11-10 stsp #include <diff_output.h>
29 fe621944 2020-11-10 stsp
30 fe621944 2020-11-10 stsp #include "diff_internal.h"
31 fe621944 2020-11-10 stsp #include "diff_debug.h"
32 fe621944 2020-11-10 stsp
33 fe621944 2020-11-10 stsp bool
34 fe621944 2020-11-10 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
35 fe621944 2020-11-10 stsp {
36 fe621944 2020-11-10 stsp return diff_range_empty(&cc->chunk);
37 fe621944 2020-11-10 stsp }
38 fe621944 2020-11-10 stsp
39 fe621944 2020-11-10 stsp int
40 fe621944 2020-11-10 stsp diff_chunk_get_left_start(const struct diff_chunk *c,
41 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
42 fe621944 2020-11-10 stsp {
43 fe621944 2020-11-10 stsp int left_start = diff_atom_root_idx(r->left, c->left_start);
44 fe621944 2020-11-10 stsp return MAX(0, left_start - context_lines);
45 fe621944 2020-11-10 stsp }
46 fe621944 2020-11-10 stsp
47 fe621944 2020-11-10 stsp int
48 fe621944 2020-11-10 stsp diff_chunk_get_left_end(const struct diff_chunk *c,
49 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
50 fe621944 2020-11-10 stsp {
51 fe621944 2020-11-10 stsp int left_start = diff_chunk_get_left_start(c, r, 0);
52 fe621944 2020-11-10 stsp return MIN(r->left->atoms.len,
53 fe621944 2020-11-10 stsp left_start + c->left_count + context_lines);
54 fe621944 2020-11-10 stsp }
55 fe621944 2020-11-10 stsp
56 fe621944 2020-11-10 stsp int
57 fe621944 2020-11-10 stsp diff_chunk_get_right_start(const struct diff_chunk *c,
58 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
59 fe621944 2020-11-10 stsp {
60 fe621944 2020-11-10 stsp int right_start = diff_atom_root_idx(r->right, c->right_start);
61 fe621944 2020-11-10 stsp return MAX(0, right_start - context_lines);
62 fe621944 2020-11-10 stsp }
63 fe621944 2020-11-10 stsp
64 fe621944 2020-11-10 stsp int
65 fe621944 2020-11-10 stsp diff_chunk_get_right_end(const struct diff_chunk *c,
66 fe621944 2020-11-10 stsp const struct diff_result *r, int context_lines)
67 fe621944 2020-11-10 stsp {
68 fe621944 2020-11-10 stsp int right_start = diff_chunk_get_right_start(c, r, 0);
69 fe621944 2020-11-10 stsp return MIN(r->right->atoms.len,
70 fe621944 2020-11-10 stsp right_start + c->right_count + context_lines);
71 fe621944 2020-11-10 stsp }
72 fe621944 2020-11-10 stsp
73 fe621944 2020-11-10 stsp struct diff_chunk *
74 fe621944 2020-11-10 stsp diff_chunk_get(const struct diff_result *r, int chunk_idx)
75 fe621944 2020-11-10 stsp {
76 fe621944 2020-11-10 stsp return &r->chunks.head[chunk_idx];
77 fe621944 2020-11-10 stsp }
78 fe621944 2020-11-10 stsp
79 fe621944 2020-11-10 stsp int
80 fe621944 2020-11-10 stsp diff_chunk_get_left_count(struct diff_chunk *c)
81 fe621944 2020-11-10 stsp {
82 fe621944 2020-11-10 stsp return c->left_count;
83 fe621944 2020-11-10 stsp }
84 fe621944 2020-11-10 stsp
85 fe621944 2020-11-10 stsp int
86 fe621944 2020-11-10 stsp diff_chunk_get_right_count(struct diff_chunk *c)
87 fe621944 2020-11-10 stsp {
88 fe621944 2020-11-10 stsp return c->right_count;
89 fe621944 2020-11-10 stsp }
90 fe621944 2020-11-10 stsp
91 fe621944 2020-11-10 stsp void
92 fe621944 2020-11-10 stsp diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
93 fe621944 2020-11-10 stsp int chunk_idx, int context_lines)
94 fe621944 2020-11-10 stsp {
95 fe621944 2020-11-10 stsp const struct diff_chunk *c = &r->chunks.head[chunk_idx];
96 fe621944 2020-11-10 stsp int left_start = diff_chunk_get_left_start(c, r, context_lines);
97 fe621944 2020-11-10 stsp int left_end = diff_chunk_get_left_end(c, r, context_lines);
98 fe621944 2020-11-10 stsp int right_start = diff_chunk_get_right_start(c, r, context_lines);
99 fe621944 2020-11-10 stsp int right_end = diff_chunk_get_right_end(c, r, context_lines);
100 fe621944 2020-11-10 stsp
101 fe621944 2020-11-10 stsp *cc = (struct diff_chunk_context){
102 fe621944 2020-11-10 stsp .chunk = {
103 fe621944 2020-11-10 stsp .start = chunk_idx,
104 fe621944 2020-11-10 stsp .end = chunk_idx + 1,
105 fe621944 2020-11-10 stsp },
106 fe621944 2020-11-10 stsp .left = {
107 fe621944 2020-11-10 stsp .start = left_start,
108 fe621944 2020-11-10 stsp .end = left_end,
109 fe621944 2020-11-10 stsp },
110 fe621944 2020-11-10 stsp .right = {
111 fe621944 2020-11-10 stsp .start = right_start,
112 fe621944 2020-11-10 stsp .end = right_end,
113 fe621944 2020-11-10 stsp },
114 fe621944 2020-11-10 stsp };
115 fe621944 2020-11-10 stsp }
116 fe621944 2020-11-10 stsp
117 fe621944 2020-11-10 stsp bool
118 fe621944 2020-11-10 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
119 fe621944 2020-11-10 stsp const struct diff_chunk_context *other)
120 fe621944 2020-11-10 stsp {
121 fe621944 2020-11-10 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
122 fe621944 2020-11-10 stsp || diff_ranges_touch(&cc->left, &other->left)
123 fe621944 2020-11-10 stsp || diff_ranges_touch(&cc->right, &other->right);
124 fe621944 2020-11-10 stsp }
125 fe621944 2020-11-10 stsp
126 fe621944 2020-11-10 stsp void
127 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
128 fe621944 2020-11-10 stsp const struct diff_chunk_context *other)
129 fe621944 2020-11-10 stsp {
130 fe621944 2020-11-10 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
131 fe621944 2020-11-10 stsp diff_ranges_merge(&cc->left, &other->left);
132 fe621944 2020-11-10 stsp diff_ranges_merge(&cc->right, &other->right);
133 fe621944 2020-11-10 stsp }
134 fe621944 2020-11-10 stsp
135 fe621944 2020-11-10 stsp void
136 fe621944 2020-11-10 stsp diff_chunk_context_load_change(struct diff_chunk_context *cc,
137 fe621944 2020-11-10 stsp int *nchunks_used,
138 fe621944 2020-11-10 stsp struct diff_result *result,
139 fe621944 2020-11-10 stsp int start_chunk_idx,
140 fe621944 2020-11-10 stsp int context_lines)
141 fe621944 2020-11-10 stsp {
142 fe621944 2020-11-10 stsp int i;
143 fe621944 2020-11-10 stsp int seen_minus = 0, seen_plus = 0;
144 fe621944 2020-11-10 stsp
145 fe621944 2020-11-10 stsp if (nchunks_used)
146 fe621944 2020-11-10 stsp *nchunks_used = 0;
147 fe621944 2020-11-10 stsp
148 fe621944 2020-11-10 stsp for (i = start_chunk_idx; i < result->chunks.len; i++) {
149 fe621944 2020-11-10 stsp struct diff_chunk *chunk = &result->chunks.head[i];
150 fe621944 2020-11-10 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
151 fe621944 2020-11-10 stsp struct diff_chunk_context next;
152 fe621944 2020-11-10 stsp
153 fe621944 2020-11-10 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS) {
154 fe621944 2020-11-10 stsp if (nchunks_used)
155 fe621944 2020-11-10 stsp (*nchunks_used)++;
156 fe621944 2020-11-10 stsp if (seen_minus || seen_plus)
157 fe621944 2020-11-10 stsp break;
158 fe621944 2020-11-10 stsp else
159 fe621944 2020-11-10 stsp continue;
160 fe621944 2020-11-10 stsp } else if (t == CHUNK_MINUS)
161 fe621944 2020-11-10 stsp seen_minus = 1;
162 fe621944 2020-11-10 stsp else if (t == CHUNK_PLUS)
163 fe621944 2020-11-10 stsp seen_plus = 1;
164 fe621944 2020-11-10 stsp
165 fe621944 2020-11-10 stsp if (diff_chunk_context_empty(cc)) {
166 fe621944 2020-11-10 stsp /* Note down the start point, any number of subsequent
167 fe621944 2020-11-10 stsp * chunks may be joined up to this chunk by being
168 fe621944 2020-11-10 stsp * directly adjacent. */
169 fe621944 2020-11-10 stsp diff_chunk_context_get(cc, result, i, context_lines);
170 fe621944 2020-11-10 stsp if (nchunks_used)
171 fe621944 2020-11-10 stsp (*nchunks_used)++;
172 fe621944 2020-11-10 stsp continue;
173 fe621944 2020-11-10 stsp }
174 fe621944 2020-11-10 stsp
175 fe621944 2020-11-10 stsp /* There already is a previous chunk noted down for being
176 fe621944 2020-11-10 stsp * printed. Does it join up with this one? */
177 fe621944 2020-11-10 stsp diff_chunk_context_get(&next, result, i, context_lines);
178 fe621944 2020-11-10 stsp
179 fe621944 2020-11-10 stsp if (diff_chunk_contexts_touch(cc, &next)) {
180 fe621944 2020-11-10 stsp /* This next context touches or overlaps the previous
181 fe621944 2020-11-10 stsp * one, join. */
182 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(cc, &next);
183 fe621944 2020-11-10 stsp if (nchunks_used)
184 fe621944 2020-11-10 stsp (*nchunks_used)++;
185 fe621944 2020-11-10 stsp continue;
186 fe621944 2020-11-10 stsp } else
187 fe621944 2020-11-10 stsp break;
188 fe621944 2020-11-10 stsp }
189 fe621944 2020-11-10 stsp }
190 fe621944 2020-11-10 stsp
191 fe621944 2020-11-10 stsp struct diff_output_unidiff_state {
192 fe621944 2020-11-10 stsp bool header_printed;
193 f3b2b552 2020-12-10 stsp char prototype[DIFF_FUNCTION_CONTEXT_SIZE];
194 f3b2b552 2020-12-10 stsp int last_prototype_idx;
195 fe621944 2020-11-10 stsp };
196 fe621944 2020-11-10 stsp
197 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *
198 fe621944 2020-11-10 stsp diff_output_unidiff_state_alloc(void)
199 fe621944 2020-11-10 stsp {
200 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state;
201 fe621944 2020-11-10 stsp
202 fe621944 2020-11-10 stsp state = calloc(1, sizeof(struct diff_output_unidiff_state));
203 fe621944 2020-11-10 stsp if (state != NULL)
204 fe621944 2020-11-10 stsp diff_output_unidiff_state_reset(state);
205 fe621944 2020-11-10 stsp return state;
206 fe621944 2020-11-10 stsp }
207 fe621944 2020-11-10 stsp
208 fe621944 2020-11-10 stsp void
209 fe621944 2020-11-10 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
210 fe621944 2020-11-10 stsp {
211 fe621944 2020-11-10 stsp state->header_printed = false;
212 f3b2b552 2020-12-10 stsp memset(state->prototype, 0, sizeof(state->prototype));
213 f3b2b552 2020-12-10 stsp state->last_prototype_idx = 0;
214 fe621944 2020-11-10 stsp }
215 fe621944 2020-11-10 stsp
216 fe621944 2020-11-10 stsp void
217 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
218 fe621944 2020-11-10 stsp {
219 fe621944 2020-11-10 stsp free(state);
220 fe621944 2020-11-10 stsp }
221 fe621944 2020-11-10 stsp
222 fe621944 2020-11-10 stsp static int
223 fe621944 2020-11-10 stsp output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
224 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state,
225 fe621944 2020-11-10 stsp const struct diff_input_info *info,
226 fe621944 2020-11-10 stsp const struct diff_result *result,
227 fe621944 2020-11-10 stsp bool print_header, bool show_function_prototypes,
228 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
229 fe621944 2020-11-10 stsp {
230 fe621944 2020-11-10 stsp int rc, left_start, left_len, right_start, right_len;
231 fe621944 2020-11-10 stsp off_t outoff = 0, *offp;
232 fe621944 2020-11-10 stsp
233 fe621944 2020-11-10 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
234 fe621944 2020-11-10 stsp return DIFF_RC_OK;
235 fe621944 2020-11-10 stsp
236 fe621944 2020-11-10 stsp if (outinfo && outinfo->line_offsets.len > 0) {
237 fe621944 2020-11-10 stsp unsigned int idx = outinfo->line_offsets.len - 1;
238 fe621944 2020-11-10 stsp outoff = outinfo->line_offsets.head[idx];
239 fe621944 2020-11-10 stsp }
240 fe621944 2020-11-10 stsp
241 fe621944 2020-11-10 stsp if (print_header && !(state->header_printed)) {
242 c4cd9c5b 2020-11-21 stsp rc = fprintf(dest, "--- %s\n",
243 c4cd9c5b 2020-11-21 stsp diff_output_get_label_left(info));
244 fe621944 2020-11-10 stsp if (rc < 0)
245 fe621944 2020-11-10 stsp return errno;
246 fe621944 2020-11-10 stsp if (outinfo) {
247 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
248 fe621944 2020-11-10 stsp if (offp == NULL)
249 fe621944 2020-11-10 stsp return ENOMEM;
250 fe621944 2020-11-10 stsp outoff += rc;
251 fe621944 2020-11-10 stsp *offp = outoff;
252 fe621944 2020-11-10 stsp
253 fe621944 2020-11-10 stsp }
254 c4cd9c5b 2020-11-21 stsp rc = fprintf(dest, "+++ %s\n",
255 c4cd9c5b 2020-11-21 stsp diff_output_get_label_right(info));
256 fe621944 2020-11-10 stsp if (rc < 0)
257 fe621944 2020-11-10 stsp return errno;
258 fe621944 2020-11-10 stsp if (outinfo) {
259 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
260 fe621944 2020-11-10 stsp if (offp == NULL)
261 fe621944 2020-11-10 stsp return ENOMEM;
262 fe621944 2020-11-10 stsp outoff += rc;
263 fe621944 2020-11-10 stsp *offp = outoff;
264 fe621944 2020-11-10 stsp
265 fe621944 2020-11-10 stsp }
266 fe621944 2020-11-10 stsp state->header_printed = true;
267 fe621944 2020-11-10 stsp }
268 fe621944 2020-11-10 stsp
269 fe621944 2020-11-10 stsp left_len = cc->left.end - cc->left.start;
270 fe621944 2020-11-10 stsp if (result->left->atoms.len == 0)
271 fe621944 2020-11-10 stsp left_start = 0;
272 fe621944 2020-11-10 stsp else if (left_len == 0 && cc->left.start > 0)
273 fe621944 2020-11-10 stsp left_start = cc->left.start;
274 fe621944 2020-11-10 stsp else
275 fe621944 2020-11-10 stsp left_start = cc->left.start + 1;
276 fe621944 2020-11-10 stsp
277 fe621944 2020-11-10 stsp right_len = cc->right.end - cc->right.start;
278 fe621944 2020-11-10 stsp if (result->right->atoms.len == 0)
279 fe621944 2020-11-10 stsp right_start = 0;
280 fe621944 2020-11-10 stsp else if (right_len == 0 && cc->right.start > 0)
281 fe621944 2020-11-10 stsp right_start = cc->right.start;
282 fe621944 2020-11-10 stsp else
283 fe621944 2020-11-10 stsp right_start = cc->right.start + 1;
284 fe621944 2020-11-10 stsp
285 fe621944 2020-11-10 stsp if (show_function_prototypes) {
286 f3b2b552 2020-12-10 stsp rc = diff_output_match_function_prototype(state->prototype,
287 f3b2b552 2020-12-10 stsp sizeof(state->prototype), &state->last_prototype_idx,
288 fe621944 2020-11-10 stsp result, cc);
289 fe621944 2020-11-10 stsp if (rc)
290 fe621944 2020-11-10 stsp return rc;
291 fe621944 2020-11-10 stsp }
292 fe621944 2020-11-10 stsp
293 fe621944 2020-11-10 stsp if (left_len == 1 && right_len == 1) {
294 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d +%d @@%s%s\n",
295 fe621944 2020-11-10 stsp left_start, right_start,
296 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
297 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
298 fe621944 2020-11-10 stsp } else if (left_len == 1 && right_len != 1) {
299 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d +%d,%d @@%s%s\n",
300 fe621944 2020-11-10 stsp left_start, right_start, right_len,
301 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
302 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
303 fe621944 2020-11-10 stsp } else if (left_len != 1 && right_len == 1) {
304 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d,%d +%d @@%s%s\n",
305 fe621944 2020-11-10 stsp left_start, left_len, right_start,
306 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
307 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
308 fe621944 2020-11-10 stsp } else {
309 fe621944 2020-11-10 stsp rc = fprintf(dest, "@@ -%d,%d +%d,%d @@%s%s\n",
310 fe621944 2020-11-10 stsp left_start, left_len, right_start, right_len,
311 f3b2b552 2020-12-10 stsp state->prototype[0] ? " " : "",
312 f3b2b552 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
313 fe621944 2020-11-10 stsp }
314 fe621944 2020-11-10 stsp if (rc < 0)
315 fe621944 2020-11-10 stsp return errno;
316 fe621944 2020-11-10 stsp if (outinfo) {
317 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
318 fe621944 2020-11-10 stsp if (offp == NULL)
319 fe621944 2020-11-10 stsp return ENOMEM;
320 fe621944 2020-11-10 stsp outoff += rc;
321 fe621944 2020-11-10 stsp *offp = outoff;
322 fe621944 2020-11-10 stsp
323 fe621944 2020-11-10 stsp }
324 fe621944 2020-11-10 stsp
325 fe621944 2020-11-10 stsp /* Got the absolute line numbers where to start printing, and the index
326 fe621944 2020-11-10 stsp * of the interesting (non-context) chunk.
327 fe621944 2020-11-10 stsp * To print context lines above the interesting chunk, nipping on the
328 fe621944 2020-11-10 stsp * previous chunk index may be necessary.
329 fe621944 2020-11-10 stsp * It is guaranteed to be only context lines where left == right, so it
330 fe621944 2020-11-10 stsp * suffices to look on the left. */
331 fe621944 2020-11-10 stsp const struct diff_chunk *first_chunk;
332 fe621944 2020-11-10 stsp int chunk_start_line;
333 fe621944 2020-11-10 stsp first_chunk = &result->chunks.head[cc->chunk.start];
334 fe621944 2020-11-10 stsp chunk_start_line = diff_atom_root_idx(result->left,
335 fe621944 2020-11-10 stsp first_chunk->left_start);
336 fe621944 2020-11-10 stsp if (cc->left.start < chunk_start_line) {
337 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, " ",
338 fe621944 2020-11-10 stsp &result->left->atoms.head[cc->left.start],
339 fe621944 2020-11-10 stsp chunk_start_line - cc->left.start);
340 fe621944 2020-11-10 stsp if (rc)
341 fe621944 2020-11-10 stsp return rc;
342 fe621944 2020-11-10 stsp }
343 fe621944 2020-11-10 stsp
344 fe621944 2020-11-10 stsp /* Now write out all the joined chunks and contexts between them */
345 fe621944 2020-11-10 stsp int c_idx;
346 fe621944 2020-11-10 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
347 fe621944 2020-11-10 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
348 fe621944 2020-11-10 stsp
349 fe621944 2020-11-10 stsp if (c->left_count && c->right_count)
350 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest,
351 fe621944 2020-11-10 stsp c->solved ? " " : "?",
352 fe621944 2020-11-10 stsp c->left_start, c->left_count);
353 fe621944 2020-11-10 stsp else if (c->left_count && !c->right_count)
354 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest,
355 fe621944 2020-11-10 stsp c->solved ? "-" : "?",
356 fe621944 2020-11-10 stsp c->left_start, c->left_count);
357 fe621944 2020-11-10 stsp else if (c->right_count && !c->left_count)
358 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest,
359 fe621944 2020-11-10 stsp c->solved ? "+" : "?",
360 fe621944 2020-11-10 stsp c->right_start, c->right_count);
361 fe621944 2020-11-10 stsp if (rc)
362 fe621944 2020-11-10 stsp return rc;
363 fe621944 2020-11-10 stsp
364 fe621944 2020-11-10 stsp if (cc->chunk.end == result->chunks.len) {
365 fe621944 2020-11-10 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
366 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
367 fe621944 2020-11-10 stsp return rc;
368 fe621944 2020-11-10 stsp }
369 fe621944 2020-11-10 stsp }
370 fe621944 2020-11-10 stsp
371 fe621944 2020-11-10 stsp /* Trailing context? */
372 fe621944 2020-11-10 stsp const struct diff_chunk *last_chunk;
373 fe621944 2020-11-10 stsp int chunk_end_line;
374 fe621944 2020-11-10 stsp last_chunk = &result->chunks.head[cc->chunk.end - 1];
375 fe621944 2020-11-10 stsp chunk_end_line = diff_atom_root_idx(result->left,
376 fe621944 2020-11-10 stsp last_chunk->left_start
377 fe621944 2020-11-10 stsp + last_chunk->left_count);
378 fe621944 2020-11-10 stsp if (cc->left.end > chunk_end_line) {
379 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, " ",
380 fe621944 2020-11-10 stsp &result->left->atoms.head[chunk_end_line],
381 fe621944 2020-11-10 stsp cc->left.end - chunk_end_line);
382 fe621944 2020-11-10 stsp if (rc)
383 fe621944 2020-11-10 stsp return rc;
384 fe621944 2020-11-10 stsp }
385 fe621944 2020-11-10 stsp
386 fe621944 2020-11-10 stsp return DIFF_RC_OK;
387 fe621944 2020-11-10 stsp }
388 fe621944 2020-11-10 stsp
389 fe621944 2020-11-10 stsp int
390 fe621944 2020-11-10 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
391 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state,
392 fe621944 2020-11-10 stsp const struct diff_input_info *info,
393 fe621944 2020-11-10 stsp const struct diff_result *result,
394 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
395 fe621944 2020-11-10 stsp {
396 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
397 fe621944 2020-11-10 stsp int flags = (result->left->root->diff_flags |
398 fe621944 2020-11-10 stsp result->right->root->diff_flags);
399 fe621944 2020-11-10 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
400 fe621944 2020-11-10 stsp
401 fe621944 2020-11-10 stsp if (output_info) {
402 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
403 fe621944 2020-11-10 stsp if (*output_info == NULL)
404 fe621944 2020-11-10 stsp return ENOMEM;
405 fe621944 2020-11-10 stsp outinfo = *output_info;
406 fe621944 2020-11-10 stsp }
407 fe621944 2020-11-10 stsp
408 fe621944 2020-11-10 stsp return output_unidiff_chunk(outinfo, dest, state, info,
409 fe621944 2020-11-10 stsp result, false, show_function_prototypes, cc);
410 fe621944 2020-11-10 stsp }
411 fe621944 2020-11-10 stsp
412 fe621944 2020-11-10 stsp int
413 fe621944 2020-11-10 stsp diff_output_unidiff(struct diff_output_info **output_info,
414 fe621944 2020-11-10 stsp FILE *dest, const struct diff_input_info *info,
415 fe621944 2020-11-10 stsp const struct diff_result *result,
416 fe621944 2020-11-10 stsp unsigned int context_lines)
417 fe621944 2020-11-10 stsp {
418 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *state;
419 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
420 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
421 b67f3bcb 2020-11-21 stsp int atomizer_flags = (result->left->atomizer_flags|
422 b67f3bcb 2020-11-21 stsp result->right->atomizer_flags);
423 fe621944 2020-11-10 stsp int flags = (result->left->root->diff_flags |
424 fe621944 2020-11-10 stsp result->right->root->diff_flags);
425 fe621944 2020-11-10 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
426 b67f3bcb 2020-11-21 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
427 b67f3bcb 2020-11-21 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
428 2471f5c4 2022-01-06 stsp off_t outoff = 0, *offp;
429 2471f5c4 2022-01-06 stsp int rc, i;
430 fe621944 2020-11-10 stsp
431 fe621944 2020-11-10 stsp if (!result)
432 fe621944 2020-11-10 stsp return EINVAL;
433 fe621944 2020-11-10 stsp if (result->rc != DIFF_RC_OK)
434 fe621944 2020-11-10 stsp return result->rc;
435 fe621944 2020-11-10 stsp
436 fe621944 2020-11-10 stsp if (output_info) {
437 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
438 fe621944 2020-11-10 stsp if (*output_info == NULL)
439 fe621944 2020-11-10 stsp return ENOMEM;
440 fe621944 2020-11-10 stsp outinfo = *output_info;
441 fe621944 2020-11-10 stsp }
442 b67f3bcb 2020-11-21 stsp
443 b67f3bcb 2020-11-21 stsp if (have_binary && !force_text) {
444 b67f3bcb 2020-11-21 stsp for (i = 0; i < result->chunks.len; i++) {
445 b67f3bcb 2020-11-21 stsp struct diff_chunk *c = &result->chunks.head[i];
446 b67f3bcb 2020-11-21 stsp enum diff_chunk_type t = diff_chunk_type(c);
447 fe621944 2020-11-10 stsp
448 b67f3bcb 2020-11-21 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
449 b67f3bcb 2020-11-21 stsp continue;
450 b67f3bcb 2020-11-21 stsp
451 2471f5c4 2022-01-06 stsp if (outinfo && outinfo->line_offsets.len > 0) {
452 2471f5c4 2022-01-06 stsp unsigned int idx =
453 2471f5c4 2022-01-06 stsp outinfo->line_offsets.len - 1;
454 2471f5c4 2022-01-06 stsp outoff = outinfo->line_offsets.head[idx];
455 2471f5c4 2022-01-06 stsp }
456 2471f5c4 2022-01-06 stsp
457 2471f5c4 2022-01-06 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
458 c4cd9c5b 2020-11-21 stsp diff_output_get_label_left(info),
459 c4cd9c5b 2020-11-21 stsp diff_output_get_label_right(info));
460 2471f5c4 2022-01-06 stsp if (outinfo) {
461 2471f5c4 2022-01-06 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
462 2471f5c4 2022-01-06 stsp if (offp == NULL)
463 2471f5c4 2022-01-06 stsp return ENOMEM;
464 2471f5c4 2022-01-06 stsp outoff += rc;
465 2471f5c4 2022-01-06 stsp *offp = outoff;
466 2471f5c4 2022-01-06 stsp
467 2471f5c4 2022-01-06 stsp }
468 b67f3bcb 2020-11-21 stsp break;
469 b67f3bcb 2020-11-21 stsp }
470 b67f3bcb 2020-11-21 stsp
471 b67f3bcb 2020-11-21 stsp return DIFF_RC_OK;
472 b67f3bcb 2020-11-21 stsp }
473 b67f3bcb 2020-11-21 stsp
474 fe621944 2020-11-10 stsp state = diff_output_unidiff_state_alloc();
475 fe621944 2020-11-10 stsp if (state == NULL) {
476 fe621944 2020-11-10 stsp if (output_info) {
477 fe621944 2020-11-10 stsp diff_output_info_free(*output_info);
478 fe621944 2020-11-10 stsp *output_info = NULL;
479 fe621944 2020-11-10 stsp }
480 fe621944 2020-11-10 stsp return ENOMEM;
481 fe621944 2020-11-10 stsp }
482 fe621944 2020-11-10 stsp
483 fe621944 2020-11-10 stsp #if DEBUG
484 fe621944 2020-11-10 stsp unsigned int check_left_pos, check_right_pos;
485 fe621944 2020-11-10 stsp check_left_pos = 0;
486 fe621944 2020-11-10 stsp check_right_pos = 0;
487 fe621944 2020-11-10 stsp for (i = 0; i < result->chunks.len; i++) {
488 fe621944 2020-11-10 stsp struct diff_chunk *c = &result->chunks.head[i];
489 fe621944 2020-11-10 stsp enum diff_chunk_type t = diff_chunk_type(c);
490 fe621944 2020-11-10 stsp
491 fe621944 2020-11-10 stsp debug("[%d] %s lines L%d R%d @L %d @R %d\n",
492 fe621944 2020-11-10 stsp i, (t == CHUNK_MINUS ? "minus" :
493 fe621944 2020-11-10 stsp (t == CHUNK_PLUS ? "plus" :
494 fe621944 2020-11-10 stsp (t == CHUNK_SAME ? "same" : "?"))),
495 fe621944 2020-11-10 stsp c->left_count,
496 fe621944 2020-11-10 stsp c->right_count,
497 fe621944 2020-11-10 stsp c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1,
498 fe621944 2020-11-10 stsp c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1);
499 fe621944 2020-11-10 stsp assert(check_left_pos == diff_atom_root_idx(result->left, c->left_start));
500 fe621944 2020-11-10 stsp assert(check_right_pos == diff_atom_root_idx(result->right, c->right_start));
501 fe621944 2020-11-10 stsp check_left_pos += c->left_count;
502 fe621944 2020-11-10 stsp check_right_pos += c->right_count;
503 fe621944 2020-11-10 stsp
504 fe621944 2020-11-10 stsp }
505 fe621944 2020-11-10 stsp assert(check_left_pos == result->left->atoms.len);
506 fe621944 2020-11-10 stsp assert(check_right_pos == result->right->atoms.len);
507 fe621944 2020-11-10 stsp #endif
508 fe621944 2020-11-10 stsp
509 fe621944 2020-11-10 stsp for (i = 0; i < result->chunks.len; i++) {
510 fe621944 2020-11-10 stsp struct diff_chunk *c = &result->chunks.head[i];
511 fe621944 2020-11-10 stsp enum diff_chunk_type t = diff_chunk_type(c);
512 fe621944 2020-11-10 stsp struct diff_chunk_context next;
513 fe621944 2020-11-10 stsp
514 fe621944 2020-11-10 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
515 fe621944 2020-11-10 stsp continue;
516 fe621944 2020-11-10 stsp
517 fe621944 2020-11-10 stsp if (diff_chunk_context_empty(&cc)) {
518 fe621944 2020-11-10 stsp /* These are the first lines being printed.
519 fe621944 2020-11-10 stsp * Note down the start point, any number of subsequent
520 fe621944 2020-11-10 stsp * chunks may be joined up to this unidiff chunk by
521 fe621944 2020-11-10 stsp * context lines or by being directly adjacent. */
522 fe621944 2020-11-10 stsp diff_chunk_context_get(&cc, result, i, context_lines);
523 fe621944 2020-11-10 stsp debug("new chunk to be printed:"
524 fe621944 2020-11-10 stsp " chunk %d-%d left %d-%d right %d-%d\n",
525 fe621944 2020-11-10 stsp cc.chunk.start, cc.chunk.end,
526 fe621944 2020-11-10 stsp cc.left.start, cc.left.end,
527 fe621944 2020-11-10 stsp cc.right.start, cc.right.end);
528 fe621944 2020-11-10 stsp continue;
529 fe621944 2020-11-10 stsp }
530 fe621944 2020-11-10 stsp
531 fe621944 2020-11-10 stsp /* There already is a previous chunk noted down for being
532 fe621944 2020-11-10 stsp * printed. Does it join up with this one? */
533 fe621944 2020-11-10 stsp diff_chunk_context_get(&next, result, i, context_lines);
534 fe621944 2020-11-10 stsp debug("new chunk to be printed:"
535 fe621944 2020-11-10 stsp " chunk %d-%d left %d-%d right %d-%d\n",
536 fe621944 2020-11-10 stsp next.chunk.start, next.chunk.end,
537 fe621944 2020-11-10 stsp next.left.start, next.left.end,
538 fe621944 2020-11-10 stsp next.right.start, next.right.end);
539 fe621944 2020-11-10 stsp
540 fe621944 2020-11-10 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
541 fe621944 2020-11-10 stsp /* This next context touches or overlaps the previous
542 fe621944 2020-11-10 stsp * one, join. */
543 fe621944 2020-11-10 stsp diff_chunk_contexts_merge(&cc, &next);
544 fe621944 2020-11-10 stsp debug("new chunk to be printed touches previous chunk,"
545 fe621944 2020-11-10 stsp " now: left %d-%d right %d-%d\n",
546 fe621944 2020-11-10 stsp cc.left.start, cc.left.end,
547 fe621944 2020-11-10 stsp cc.right.start, cc.right.end);
548 fe621944 2020-11-10 stsp continue;
549 fe621944 2020-11-10 stsp }
550 fe621944 2020-11-10 stsp
551 fe621944 2020-11-10 stsp /* No touching, so the previous context is complete with a gap
552 fe621944 2020-11-10 stsp * between it and this next one. Print the previous one and
553 fe621944 2020-11-10 stsp * start fresh here. */
554 fe621944 2020-11-10 stsp debug("new chunk to be printed does not touch previous chunk;"
555 fe621944 2020-11-10 stsp " print left %d-%d right %d-%d\n",
556 fe621944 2020-11-10 stsp cc.left.start, cc.left.end, cc.right.start, cc.right.end);
557 fe621944 2020-11-10 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
558 fe621944 2020-11-10 stsp true, show_function_prototypes, &cc);
559 fe621944 2020-11-10 stsp cc = next;
560 fe621944 2020-11-10 stsp debug("new unprinted chunk is left %d-%d right %d-%d\n",
561 fe621944 2020-11-10 stsp cc.left.start, cc.left.end, cc.right.start, cc.right.end);
562 fe621944 2020-11-10 stsp }
563 fe621944 2020-11-10 stsp
564 fe621944 2020-11-10 stsp if (!diff_chunk_context_empty(&cc))
565 fe621944 2020-11-10 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
566 fe621944 2020-11-10 stsp true, show_function_prototypes, &cc);
567 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(state);
568 fe621944 2020-11-10 stsp return DIFF_RC_OK;
569 fe621944 2020-11-10 stsp }