1 3b0f3d61 2020-01-22 neels /* Produce a unidiff output from a diff_result. */
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
5 3b0f3d61 2020-01-22 neels * Permission to use, copy, modify, and distribute this software for any
6 3b0f3d61 2020-01-22 neels * purpose with or without fee is hereby granted, provided that the above
7 3b0f3d61 2020-01-22 neels * copyright notice and this permission notice appear in all copies.
9 3b0f3d61 2020-01-22 neels * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 3b0f3d61 2020-01-22 neels * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 3b0f3d61 2020-01-22 neels * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 3b0f3d61 2020-01-22 neels * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 3b0f3d61 2020-01-22 neels * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 3b0f3d61 2020-01-22 neels * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 3b0f3d61 2020-01-22 neels * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 e10a628a 2020-09-16 stsp #include <errno.h>
19 e10a628a 2020-09-16 stsp #include <stdbool.h>
20 fe6d58fb 2020-11-14 naddy #include <stdint.h>
21 e10a628a 2020-09-16 stsp #include <stdio.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
23 b3fd1fa2 2020-12-10 stsp #include <string.h>
24 4861c9da 2020-10-30 neels #include <assert.h>
26 1dfba055 2020-10-07 stsp #include <arraylist.h>
27 1dfba055 2020-10-07 stsp #include <diff_main.h>
28 1dfba055 2020-10-07 stsp #include <diff_output.h>
30 85ab4559 2020-09-22 stsp #include "diff_internal.h"
31 2a1b94d0 2020-09-26 stsp #include "diff_debug.h"
34 cbf93b70 2020-10-16 stsp diff_chunk_context_empty(const struct diff_chunk_context *cc)
36 d362ea2e 2020-07-25 stsp return diff_range_empty(&cc->chunk);
40 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_start(const struct diff_chunk *c,
41 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
43 c16dde50 2020-10-22 stsp int left_start = diff_atom_root_idx(r->left, c->left_start);
44 fe8af0d6 2020-10-06 stsp return MAX(0, left_start - context_lines);
48 fe8af0d6 2020-10-06 stsp diff_chunk_get_left_end(const struct diff_chunk *c,
49 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
51 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, 0);
52 c16dde50 2020-10-22 stsp return MIN(r->left->atoms.len,
53 fe8af0d6 2020-10-06 stsp left_start + c->left_count + context_lines);
57 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_start(const struct diff_chunk *c,
58 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
60 c16dde50 2020-10-22 stsp int right_start = diff_atom_root_idx(r->right, c->right_start);
61 fe8af0d6 2020-10-06 stsp return MAX(0, right_start - context_lines);
65 fe8af0d6 2020-10-06 stsp diff_chunk_get_right_end(const struct diff_chunk *c,
66 fe8af0d6 2020-10-06 stsp const struct diff_result *r, int context_lines)
68 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, 0);
69 c16dde50 2020-10-22 stsp return MIN(r->right->atoms.len,
70 fe8af0d6 2020-10-06 stsp right_start + c->right_count + context_lines);
73 e8eedebc 2020-11-06 stsp struct diff_chunk *
74 e8eedebc 2020-11-06 stsp diff_chunk_get(const struct diff_result *r, int chunk_idx)
76 e8eedebc 2020-11-06 stsp return &r->chunks.head[chunk_idx];
80 e8eedebc 2020-11-06 stsp diff_chunk_get_left_count(struct diff_chunk *c)
82 e8eedebc 2020-11-06 stsp return c->left_count;
86 e8eedebc 2020-11-06 stsp diff_chunk_get_right_count(struct diff_chunk *c)
88 e8eedebc 2020-11-06 stsp return c->right_count;
92 f374e913 2020-09-22 stsp diff_chunk_context_get(struct diff_chunk_context *cc, const struct diff_result *r,
93 0d27172a 2020-05-06 neels int chunk_idx, int context_lines)
95 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &r->chunks.head[chunk_idx];
96 fe8af0d6 2020-10-06 stsp int left_start = diff_chunk_get_left_start(c, r, context_lines);
97 fe8af0d6 2020-10-06 stsp int left_end = diff_chunk_get_left_end(c, r, context_lines);
98 fe8af0d6 2020-10-06 stsp int right_start = diff_chunk_get_right_start(c, r, context_lines);
99 fe8af0d6 2020-10-06 stsp int right_end = diff_chunk_get_right_end(c, r, context_lines);
101 f374e913 2020-09-22 stsp *cc = (struct diff_chunk_context){
102 3b0f3d61 2020-01-22 neels .chunk = {
103 3b0f3d61 2020-01-22 neels .start = chunk_idx,
104 3b0f3d61 2020-01-22 neels .end = chunk_idx + 1,
107 0d27172a 2020-05-06 neels .start = left_start,
108 0d27172a 2020-05-06 neels .end = left_end,
110 3b0f3d61 2020-01-22 neels .right = {
111 0d27172a 2020-05-06 neels .start = right_start,
112 0d27172a 2020-05-06 neels .end = right_end,
118 26595c7d 2020-10-15 stsp diff_chunk_contexts_touch(const struct diff_chunk_context *cc,
119 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
121 d362ea2e 2020-07-25 stsp return diff_ranges_touch(&cc->chunk, &other->chunk)
122 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->left, &other->left)
123 d362ea2e 2020-07-25 stsp || diff_ranges_touch(&cc->right, &other->right);
127 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(struct diff_chunk_context *cc,
128 26595c7d 2020-10-15 stsp const struct diff_chunk_context *other)
130 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->chunk, &other->chunk);
131 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->left, &other->left);
132 d362ea2e 2020-07-25 stsp diff_ranges_merge(&cc->right, &other->right);
136 7187fe97 2020-10-17 stsp diff_chunk_context_load_change(struct diff_chunk_context *cc,
137 7187fe97 2020-10-17 stsp int *nchunks_used,
138 7187fe97 2020-10-17 stsp struct diff_result *result,
139 7187fe97 2020-10-17 stsp int start_chunk_idx,
140 7187fe97 2020-10-17 stsp int context_lines)
143 7187fe97 2020-10-17 stsp int seen_minus = 0, seen_plus = 0;
145 7187fe97 2020-10-17 stsp if (nchunks_used)
146 7187fe97 2020-10-17 stsp *nchunks_used = 0;
148 7187fe97 2020-10-17 stsp for (i = start_chunk_idx; i < result->chunks.len; i++) {
149 7187fe97 2020-10-17 stsp struct diff_chunk *chunk = &result->chunks.head[i];
150 7187fe97 2020-10-17 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
151 7187fe97 2020-10-17 stsp struct diff_chunk_context next;
153 7187fe97 2020-10-17 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS) {
154 7187fe97 2020-10-17 stsp if (nchunks_used)
155 7187fe97 2020-10-17 stsp (*nchunks_used)++;
156 f4867452 2020-10-17 stsp if (seen_minus || seen_plus)
160 7187fe97 2020-10-17 stsp } else if (t == CHUNK_MINUS)
161 7187fe97 2020-10-17 stsp seen_minus = 1;
162 7187fe97 2020-10-17 stsp else if (t == CHUNK_PLUS)
163 7187fe97 2020-10-17 stsp seen_plus = 1;
165 7187fe97 2020-10-17 stsp if (diff_chunk_context_empty(cc)) {
166 7187fe97 2020-10-17 stsp /* Note down the start point, any number of subsequent
167 7187fe97 2020-10-17 stsp * chunks may be joined up to this chunk by being
168 7187fe97 2020-10-17 stsp * directly adjacent. */
169 7187fe97 2020-10-17 stsp diff_chunk_context_get(cc, result, i, context_lines);
170 7187fe97 2020-10-17 stsp if (nchunks_used)
171 7187fe97 2020-10-17 stsp (*nchunks_used)++;
175 7187fe97 2020-10-17 stsp /* There already is a previous chunk noted down for being
176 7187fe97 2020-10-17 stsp * printed. Does it join up with this one? */
177 7187fe97 2020-10-17 stsp diff_chunk_context_get(&next, result, i, context_lines);
179 7187fe97 2020-10-17 stsp if (diff_chunk_contexts_touch(cc, &next)) {
180 7187fe97 2020-10-17 stsp /* This next context touches or overlaps the previous
181 7187fe97 2020-10-17 stsp * one, join. */
182 7187fe97 2020-10-17 stsp diff_chunk_contexts_merge(cc, &next);
183 7187fe97 2020-10-17 stsp if (nchunks_used)
184 7187fe97 2020-10-17 stsp (*nchunks_used)++;
191 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state {
192 11caa5cc 2020-09-22 stsp bool header_printed;
193 b3fd1fa2 2020-12-10 stsp char prototype[DIFF_FUNCTION_CONTEXT_SIZE];
194 b3fd1fa2 2020-12-10 stsp int last_prototype_idx;
197 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *
198 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_alloc(void)
200 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
202 11caa5cc 2020-09-22 stsp state = calloc(1, sizeof(struct diff_output_unidiff_state));
203 11caa5cc 2020-09-22 stsp if (state != NULL)
204 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(state);
205 11caa5cc 2020-09-22 stsp return state;
209 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_reset(struct diff_output_unidiff_state *state)
211 11caa5cc 2020-09-22 stsp state->header_printed = false;
212 b3fd1fa2 2020-12-10 stsp memset(state->prototype, 0, sizeof(state->prototype));
213 b3fd1fa2 2020-12-10 stsp state->last_prototype_idx = 0;
217 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(struct diff_output_unidiff_state *state)
219 11caa5cc 2020-09-22 stsp free(state);
223 2c20a3ed 2020-09-22 stsp output_unidiff_chunk(struct diff_output_info *outinfo, FILE *dest,
224 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
225 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
226 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
227 13e2caa3 2020-10-17 stsp bool print_header, bool show_function_prototypes,
228 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
230 b6adedb6 2020-10-07 stsp int rc, left_start, left_len, right_start, right_len;
231 ab528e22 2020-09-22 stsp off_t outoff = 0, *offp;
233 d362ea2e 2020-07-25 stsp if (diff_range_empty(&cc->left) && diff_range_empty(&cc->right))
234 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
236 2c20a3ed 2020-09-22 stsp if (outinfo && outinfo->line_offsets.len > 0) {
237 2c20a3ed 2020-09-22 stsp unsigned int idx = outinfo->line_offsets.len - 1;
238 2c20a3ed 2020-09-22 stsp outoff = outinfo->line_offsets.head[idx];
241 66ea8e5a 2020-10-17 stsp if (print_header && !(state->header_printed)) {
242 e4c510c1 2020-11-21 stsp rc = fprintf(dest, "--- %s\n",
243 e4c510c1 2020-11-21 stsp diff_output_get_label_left(info));
244 2c20a3ed 2020-09-22 stsp if (rc < 0)
245 2c20a3ed 2020-09-22 stsp return errno;
246 2c20a3ed 2020-09-22 stsp if (outinfo) {
247 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
248 dabc1008 2020-09-22 stsp if (offp == NULL)
249 dabc1008 2020-09-22 stsp return ENOMEM;
250 2c20a3ed 2020-09-22 stsp outoff += rc;
251 2c20a3ed 2020-09-22 stsp *offp = outoff;
254 e4c510c1 2020-11-21 stsp rc = fprintf(dest, "+++ %s\n",
255 e4c510c1 2020-11-21 stsp diff_output_get_label_right(info));
256 2c20a3ed 2020-09-22 stsp if (rc < 0)
257 2c20a3ed 2020-09-22 stsp return errno;
258 2c20a3ed 2020-09-22 stsp if (outinfo) {
259 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
260 dabc1008 2020-09-22 stsp if (offp == NULL)
261 dabc1008 2020-09-22 stsp return ENOMEM;
262 2c20a3ed 2020-09-22 stsp outoff += rc;
263 2c20a3ed 2020-09-22 stsp *offp = outoff;
266 11caa5cc 2020-09-22 stsp state->header_printed = true;
269 b6adedb6 2020-10-07 stsp left_len = cc->left.end - cc->left.start;
270 c16dde50 2020-10-22 stsp if (result->left->atoms.len == 0)
271 11d9f2f7 2020-10-07 stsp left_start = 0;
272 11d9f2f7 2020-10-07 stsp else if (left_len == 0 && cc->left.start > 0)
273 b6adedb6 2020-10-07 stsp left_start = cc->left.start;
275 b6adedb6 2020-10-07 stsp left_start = cc->left.start + 1;
277 b6adedb6 2020-10-07 stsp right_len = cc->right.end - cc->right.start;
278 c16dde50 2020-10-22 stsp if (result->right->atoms.len == 0)
279 11d9f2f7 2020-10-07 stsp right_start = 0;
280 11d9f2f7 2020-10-07 stsp else if (right_len == 0 && cc->right.start > 0)
281 b6adedb6 2020-10-07 stsp right_start = cc->right.start;
283 b6adedb6 2020-10-07 stsp right_start = cc->right.start + 1;
285 13e2caa3 2020-10-17 stsp if (show_function_prototypes) {
286 b3fd1fa2 2020-12-10 stsp rc = diff_output_match_function_prototype(state->prototype,
287 b3fd1fa2 2020-12-10 stsp sizeof(state->prototype), &state->last_prototype_idx,
288 13e2caa3 2020-10-17 stsp result, cc);
293 d2dfa2ec 2020-10-07 stsp if (left_len == 1 && right_len == 1) {
294 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d +%d @@%s%s\n",
295 13e2caa3 2020-10-17 stsp left_start, right_start,
296 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
297 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
298 d2dfa2ec 2020-10-07 stsp } else if (left_len == 1 && right_len != 1) {
299 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d +%d,%d @@%s%s\n",
300 13e2caa3 2020-10-17 stsp left_start, right_start, right_len,
301 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
302 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
303 b6adedb6 2020-10-07 stsp } else if (left_len != 1 && right_len == 1) {
304 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d,%d +%d @@%s%s\n",
305 13e2caa3 2020-10-17 stsp left_start, left_len, right_start,
306 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
307 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
309 13e2caa3 2020-10-17 stsp rc = fprintf(dest, "@@ -%d,%d +%d,%d @@%s%s\n",
310 13e2caa3 2020-10-17 stsp left_start, left_len, right_start, right_len,
311 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? " " : "",
312 b3fd1fa2 2020-12-10 stsp state->prototype[0] ? state->prototype : "");
314 2c20a3ed 2020-09-22 stsp if (rc < 0)
315 2c20a3ed 2020-09-22 stsp return errno;
316 2c20a3ed 2020-09-22 stsp if (outinfo) {
317 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
318 dabc1008 2020-09-22 stsp if (offp == NULL)
319 dabc1008 2020-09-22 stsp return ENOMEM;
320 2c20a3ed 2020-09-22 stsp outoff += rc;
321 2c20a3ed 2020-09-22 stsp *offp = outoff;
325 0d27172a 2020-05-06 neels /* Got the absolute line numbers where to start printing, and the index
326 0d27172a 2020-05-06 neels * of the interesting (non-context) chunk.
327 0d27172a 2020-05-06 neels * To print context lines above the interesting chunk, nipping on the
328 0d27172a 2020-05-06 neels * previous chunk index may be necessary.
329 0d27172a 2020-05-06 neels * It is guaranteed to be only context lines where left == right, so it
330 0d27172a 2020-05-06 neels * suffices to look on the left. */
331 0d27172a 2020-05-06 neels const struct diff_chunk *first_chunk;
332 0d27172a 2020-05-06 neels int chunk_start_line;
333 0d27172a 2020-05-06 neels first_chunk = &result->chunks.head[cc->chunk.start];
334 c16dde50 2020-10-22 stsp chunk_start_line = diff_atom_root_idx(result->left,
335 0d27172a 2020-05-06 neels first_chunk->left_start);
336 2c20a3ed 2020-09-22 stsp if (cc->left.start < chunk_start_line) {
337 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
338 c16dde50 2020-10-22 stsp &result->left->atoms.head[cc->left.start],
339 3b0f3d61 2020-01-22 neels chunk_start_line - cc->left.start);
344 3b0f3d61 2020-01-22 neels /* Now write out all the joined chunks and contexts between them */
345 3b0f3d61 2020-01-22 neels int c_idx;
346 3b0f3d61 2020-01-22 neels for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
347 3b0f3d61 2020-01-22 neels const struct diff_chunk *c = &result->chunks.head[c_idx];
349 3b0f3d61 2020-01-22 neels if (c->left_count && c->right_count)
350 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
351 0d27172a 2020-05-06 neels c->solved ? " " : "?",
352 0d27172a 2020-05-06 neels c->left_start, c->left_count);
353 3b0f3d61 2020-01-22 neels else if (c->left_count && !c->right_count)
354 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
355 0d27172a 2020-05-06 neels c->solved ? "-" : "?",
356 0d27172a 2020-05-06 neels c->left_start, c->left_count);
357 3b0f3d61 2020-01-22 neels else if (c->right_count && !c->left_count)
358 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest,
359 0d27172a 2020-05-06 neels c->solved ? "+" : "?",
360 0d27172a 2020-05-06 neels c->right_start, c->right_count);
364 7021523c 2020-10-16 stsp if (cc->chunk.end == result->chunks.len) {
365 7021523c 2020-10-16 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
366 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
371 3b0f3d61 2020-01-22 neels /* Trailing context? */
372 0d27172a 2020-05-06 neels const struct diff_chunk *last_chunk;
373 0d27172a 2020-05-06 neels int chunk_end_line;
374 0d27172a 2020-05-06 neels last_chunk = &result->chunks.head[cc->chunk.end - 1];
375 c16dde50 2020-10-22 stsp chunk_end_line = diff_atom_root_idx(result->left,
376 0d27172a 2020-05-06 neels last_chunk->left_start
377 0d27172a 2020-05-06 neels + last_chunk->left_count);
378 2c20a3ed 2020-09-22 stsp if (cc->left.end > chunk_end_line) {
379 2c20a3ed 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, " ",
380 c16dde50 2020-10-22 stsp &result->left->atoms.head[chunk_end_line],
381 3b0f3d61 2020-01-22 neels cc->left.end - chunk_end_line);
386 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
390 2c20a3ed 2020-09-22 stsp diff_output_unidiff_chunk(struct diff_output_info **output_info, FILE *dest,
391 2c20a3ed 2020-09-22 stsp struct diff_output_unidiff_state *state,
392 2c20a3ed 2020-09-22 stsp const struct diff_input_info *info,
393 2c20a3ed 2020-09-22 stsp const struct diff_result *result,
394 2c20a3ed 2020-09-22 stsp const struct diff_chunk_context *cc)
396 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
397 c16dde50 2020-10-22 stsp int flags = (result->left->root->diff_flags |
398 c16dde50 2020-10-22 stsp result->right->root->diff_flags);
399 13e2caa3 2020-10-17 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
401 2c20a3ed 2020-09-22 stsp if (output_info) {
402 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
403 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
404 2c20a3ed 2020-09-22 stsp return ENOMEM;
405 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
408 2c20a3ed 2020-09-22 stsp return output_unidiff_chunk(outinfo, dest, state, info,
409 13e2caa3 2020-10-17 stsp result, false, show_function_prototypes, cc);
413 2c20a3ed 2020-09-22 stsp diff_output_unidiff(struct diff_output_info **output_info,
414 2c20a3ed 2020-09-22 stsp FILE *dest, const struct diff_input_info *info,
415 0d27172a 2020-05-06 neels const struct diff_result *result,
416 0d27172a 2020-05-06 neels unsigned int context_lines)
418 11caa5cc 2020-09-22 stsp struct diff_output_unidiff_state *state;
419 11caa5cc 2020-09-22 stsp struct diff_chunk_context cc = {};
420 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
421 e51ebd83 2020-11-21 stsp int atomizer_flags = (result->left->atomizer_flags|
422 e51ebd83 2020-11-21 stsp result->right->atomizer_flags);
423 c16dde50 2020-10-22 stsp int flags = (result->left->root->diff_flags |
424 c16dde50 2020-10-22 stsp result->right->root->diff_flags);
425 13e2caa3 2020-10-17 stsp bool show_function_prototypes = (flags & DIFF_FLAG_SHOW_PROTOTYPES);
426 e51ebd83 2020-11-21 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
427 e51ebd83 2020-11-21 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
428 b72f51ff 2022-01-06 stsp off_t outoff = 0, *offp;
431 3b0f3d61 2020-01-22 neels if (!result)
432 3e6cba3a 2020-08-13 stsp return EINVAL;
433 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
434 3b0f3d61 2020-01-22 neels return result->rc;
436 2c20a3ed 2020-09-22 stsp if (output_info) {
437 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
438 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
439 2c20a3ed 2020-09-22 stsp return ENOMEM;
440 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
443 e51ebd83 2020-11-21 stsp if (have_binary && !force_text) {
444 e51ebd83 2020-11-21 stsp for (i = 0; i < result->chunks.len; i++) {
445 e51ebd83 2020-11-21 stsp struct diff_chunk *c = &result->chunks.head[i];
446 e51ebd83 2020-11-21 stsp enum diff_chunk_type t = diff_chunk_type(c);
448 e51ebd83 2020-11-21 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
451 b72f51ff 2022-01-06 stsp if (outinfo && outinfo->line_offsets.len > 0) {
452 b72f51ff 2022-01-06 stsp unsigned int idx =
453 b72f51ff 2022-01-06 stsp outinfo->line_offsets.len - 1;
454 b72f51ff 2022-01-06 stsp outoff = outinfo->line_offsets.head[idx];
457 b72f51ff 2022-01-06 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
458 e4c510c1 2020-11-21 stsp diff_output_get_label_left(info),
459 e4c510c1 2020-11-21 stsp diff_output_get_label_right(info));
460 b72f51ff 2022-01-06 stsp if (outinfo) {
461 b72f51ff 2022-01-06 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
462 b72f51ff 2022-01-06 stsp if (offp == NULL)
463 b72f51ff 2022-01-06 stsp return ENOMEM;
464 b72f51ff 2022-01-06 stsp outoff += rc;
465 b72f51ff 2022-01-06 stsp *offp = outoff;
471 e51ebd83 2020-11-21 stsp return DIFF_RC_OK;
474 11caa5cc 2020-09-22 stsp state = diff_output_unidiff_state_alloc();
475 2c20a3ed 2020-09-22 stsp if (state == NULL) {
476 2c20a3ed 2020-09-22 stsp if (output_info) {
477 2c20a3ed 2020-09-22 stsp diff_output_info_free(*output_info);
478 2c20a3ed 2020-09-22 stsp *output_info = NULL;
480 11caa5cc 2020-09-22 stsp return ENOMEM;
484 e638575c 2020-11-06 neels unsigned int check_left_pos, check_right_pos;
485 4861c9da 2020-10-30 neels check_left_pos = 0;
486 4861c9da 2020-10-30 neels check_right_pos = 0;
487 5ff75996 2020-10-11 neels for (i = 0; i < result->chunks.len; i++) {
488 5ff75996 2020-10-11 neels struct diff_chunk *c = &result->chunks.head[i];
489 5ff75996 2020-10-11 neels enum diff_chunk_type t = diff_chunk_type(c);
491 5ff75996 2020-10-11 neels debug("[%d] %s lines L%d R%d @L %d @R %d\n",
492 5ff75996 2020-10-11 neels i, (t == CHUNK_MINUS ? "minus" :
493 5ff75996 2020-10-11 neels (t == CHUNK_PLUS ? "plus" :
494 5ff75996 2020-10-11 neels (t == CHUNK_SAME ? "same" : "?"))),
495 5ff75996 2020-10-11 neels c->left_count,
496 5ff75996 2020-10-11 neels c->right_count,
497 9403a358 2020-10-24 neels c->left_start ? diff_atom_root_idx(result->left, c->left_start) : -1,
498 9403a358 2020-10-24 neels c->right_start ? diff_atom_root_idx(result->right, c->right_start) : -1);
499 4861c9da 2020-10-30 neels assert(check_left_pos == diff_atom_root_idx(result->left, c->left_start));
500 4861c9da 2020-10-30 neels assert(check_right_pos == diff_atom_root_idx(result->right, c->right_start));
501 4861c9da 2020-10-30 neels check_left_pos += c->left_count;
502 4861c9da 2020-10-30 neels check_right_pos += c->right_count;
505 4861c9da 2020-10-30 neels assert(check_left_pos == result->left->atoms.len);
506 4861c9da 2020-10-30 neels assert(check_right_pos == result->right->atoms.len);
509 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
510 3b0f3d61 2020-01-22 neels struct diff_chunk *c = &result->chunks.head[i];
511 8546b045 2020-09-20 neels enum diff_chunk_type t = diff_chunk_type(c);
512 f374e913 2020-09-22 stsp struct diff_chunk_context next;
514 9cc49695 2020-05-05 neels if (t != CHUNK_MINUS && t != CHUNK_PLUS)
517 cbf93b70 2020-10-16 stsp if (diff_chunk_context_empty(&cc)) {
518 9cc49695 2020-05-05 neels /* These are the first lines being printed.
519 0d27172a 2020-05-06 neels * Note down the start point, any number of subsequent
520 0d27172a 2020-05-06 neels * chunks may be joined up to this unidiff chunk by
521 0d27172a 2020-05-06 neels * context lines or by being directly adjacent. */
522 f374e913 2020-09-22 stsp diff_chunk_context_get(&cc, result, i, context_lines);
523 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
524 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
525 9cc49695 2020-05-05 neels cc.chunk.start, cc.chunk.end,
526 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
527 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
531 0d27172a 2020-05-06 neels /* There already is a previous chunk noted down for being
532 0d27172a 2020-05-06 neels * printed. Does it join up with this one? */
533 f374e913 2020-09-22 stsp diff_chunk_context_get(&next, result, i, context_lines);
534 0d27172a 2020-05-06 neels debug("new chunk to be printed:"
535 0d27172a 2020-05-06 neels " chunk %d-%d left %d-%d right %d-%d\n",
536 9cc49695 2020-05-05 neels next.chunk.start, next.chunk.end,
537 9cc49695 2020-05-05 neels next.left.start, next.left.end,
538 9cc49695 2020-05-05 neels next.right.start, next.right.end);
540 26595c7d 2020-10-15 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
541 0d27172a 2020-05-06 neels /* This next context touches or overlaps the previous
542 0d27172a 2020-05-06 neels * one, join. */
543 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(&cc, &next);
544 0d27172a 2020-05-06 neels debug("new chunk to be printed touches previous chunk,"
545 0d27172a 2020-05-06 neels " now: left %d-%d right %d-%d\n",
546 9cc49695 2020-05-05 neels cc.left.start, cc.left.end,
547 9cc49695 2020-05-05 neels cc.right.start, cc.right.end);
551 0d27172a 2020-05-06 neels /* No touching, so the previous context is complete with a gap
552 0d27172a 2020-05-06 neels * between it and this next one. Print the previous one and
553 0d27172a 2020-05-06 neels * start fresh here. */
554 0d27172a 2020-05-06 neels debug("new chunk to be printed does not touch previous chunk;"
555 0d27172a 2020-05-06 neels " print left %d-%d right %d-%d\n",
556 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
557 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
558 13e2caa3 2020-10-17 stsp true, show_function_prototypes, &cc);
559 9cc49695 2020-05-05 neels cc = next;
560 9cc49695 2020-05-05 neels debug("new unprinted chunk is left %d-%d right %d-%d\n",
561 9cc49695 2020-05-05 neels cc.left.start, cc.left.end, cc.right.start, cc.right.end);
564 cbf93b70 2020-10-16 stsp if (!diff_chunk_context_empty(&cc))
565 66ea8e5a 2020-10-17 stsp output_unidiff_chunk(outinfo, dest, state, info, result,
566 13e2caa3 2020-10-17 stsp true, show_function_prototypes, &cc);
567 11caa5cc 2020-09-22 stsp diff_output_unidiff_state_free(state);
568 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;