Blame


1 3b0f3d61 2020-01-22 neels /* Output all lines of a diff_result. */
2 3b0f3d61 2020-01-22 neels /*
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 3b0f3d61 2020-01-22 neels *
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.
8 3b0f3d61 2020-01-22 neels *
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.
16 3b0f3d61 2020-01-22 neels */
17 3b0f3d61 2020-01-22 neels
18 e10a628a 2020-09-16 stsp #include <errno.h>
19 fe6d58fb 2020-11-14 naddy #include <stdint.h>
20 e10a628a 2020-09-16 stsp #include <stdio.h>
21 e10a628a 2020-09-16 stsp #include <stdbool.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
23 e10a628a 2020-09-16 stsp
24 1dfba055 2020-10-07 stsp #include <arraylist.h>
25 1dfba055 2020-10-07 stsp #include <diff_main.h>
26 1dfba055 2020-10-07 stsp #include <diff_output.h>
27 3b0f3d61 2020-01-22 neels
28 85ab4559 2020-09-22 stsp #include "diff_internal.h"
29 e10a628a 2020-09-16 stsp
30 b1aa94aa 2022-08-30 stsp static int
31 b1aa94aa 2022-08-30 stsp output_plain_chunk(struct diff_output_info *outinfo,
32 b1aa94aa 2022-08-30 stsp FILE *dest, const struct diff_input_info *info,
33 b1aa94aa 2022-08-30 stsp const struct diff_result *result,
34 9230203d 2022-09-02 stsp struct diff_chunk_context *cc, off_t *outoff)
35 b1aa94aa 2022-08-30 stsp {
36 9230203d 2022-09-02 stsp off_t *offp;
37 b1aa94aa 2022-08-30 stsp int left_start, left_len, right_start, right_len;
38 b1aa94aa 2022-08-30 stsp int rc;
39 b1aa94aa 2022-08-30 stsp bool change = false;
40 b1aa94aa 2022-08-30 stsp
41 b1aa94aa 2022-08-30 stsp left_len = cc->left.end - cc->left.start;
42 b1aa94aa 2022-08-30 stsp if (left_len < 0)
43 b1aa94aa 2022-08-30 stsp return EINVAL;
44 b1aa94aa 2022-08-30 stsp else if (result->left->atoms.len == 0)
45 b1aa94aa 2022-08-30 stsp left_start = 0;
46 b1aa94aa 2022-08-30 stsp else if (left_len == 0 && cc->left.start > 0)
47 b1aa94aa 2022-08-30 stsp left_start = cc->left.start;
48 b1aa94aa 2022-08-30 stsp else if (cc->left.end > 0)
49 b1aa94aa 2022-08-30 stsp left_start = cc->left.start + 1;
50 b1aa94aa 2022-08-30 stsp else
51 b1aa94aa 2022-08-30 stsp left_start = cc->left.start;
52 b1aa94aa 2022-08-30 stsp
53 b1aa94aa 2022-08-30 stsp right_len = cc->right.end - cc->right.start;
54 b1aa94aa 2022-08-30 stsp if (right_len < 0)
55 b1aa94aa 2022-08-30 stsp return EINVAL;
56 b1aa94aa 2022-08-30 stsp else if (result->right->atoms.len == 0)
57 b1aa94aa 2022-08-30 stsp right_start = 0;
58 b1aa94aa 2022-08-30 stsp else if (right_len == 0 && cc->right.start > 0)
59 b1aa94aa 2022-08-30 stsp right_start = cc->right.start;
60 b1aa94aa 2022-08-30 stsp else if (cc->right.end > 0)
61 b1aa94aa 2022-08-30 stsp right_start = cc->right.start + 1;
62 b1aa94aa 2022-08-30 stsp else
63 b1aa94aa 2022-08-30 stsp right_start = cc->right.start;
64 b1aa94aa 2022-08-30 stsp
65 b1aa94aa 2022-08-30 stsp if (left_len == 0) {
66 b1aa94aa 2022-08-30 stsp /* addition */
67 b1aa94aa 2022-08-30 stsp if (right_len == 1) {
68 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%da%d\n", left_start, right_start);
69 b1aa94aa 2022-08-30 stsp } else {
70 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%da%d,%d\n", left_start,
71 b1aa94aa 2022-08-30 stsp right_start, cc->right.end);
72 b1aa94aa 2022-08-30 stsp }
73 b1aa94aa 2022-08-30 stsp } else if (right_len == 0) {
74 b1aa94aa 2022-08-30 stsp /* deletion */
75 b1aa94aa 2022-08-30 stsp if (left_len == 1) {
76 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%dd%d\n", left_start,
77 b1aa94aa 2022-08-30 stsp right_start);
78 b1aa94aa 2022-08-30 stsp } else {
79 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%d,%dd%d\n", left_start,
80 b1aa94aa 2022-08-30 stsp cc->left.end, right_start);
81 b1aa94aa 2022-08-30 stsp }
82 b1aa94aa 2022-08-30 stsp } else {
83 b1aa94aa 2022-08-30 stsp /* change */
84 b1aa94aa 2022-08-30 stsp change = true;
85 b1aa94aa 2022-08-30 stsp if (left_len == 1 && right_len == 1) {
86 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%dc%d\n", left_start, right_start);
87 b1aa94aa 2022-08-30 stsp } else if (left_len == 1) {
88 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%dc%d,%d\n", left_start,
89 b1aa94aa 2022-08-30 stsp right_start, cc->right.end);
90 b1aa94aa 2022-08-30 stsp } else if (right_len == 1) {
91 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%d,%dc%d\n", left_start,
92 b1aa94aa 2022-08-30 stsp cc->left.end, right_start);
93 b1aa94aa 2022-08-30 stsp } else {
94 b1aa94aa 2022-08-30 stsp rc = fprintf(dest, "%d,%dc%d,%d\n", left_start,
95 b1aa94aa 2022-08-30 stsp cc->left.end, right_start, cc->right.end);
96 b1aa94aa 2022-08-30 stsp }
97 b1aa94aa 2022-08-30 stsp }
98 9230203d 2022-09-02 stsp if (rc < 0)
99 9230203d 2022-09-02 stsp return errno;
100 9230203d 2022-09-02 stsp if (outinfo) {
101 9230203d 2022-09-02 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
102 9230203d 2022-09-02 stsp if (offp == NULL)
103 9230203d 2022-09-02 stsp return ENOMEM;
104 9230203d 2022-09-02 stsp *outoff += rc;
105 9230203d 2022-09-02 stsp *offp = *outoff;
106 9230203d 2022-09-02 stsp }
107 b1aa94aa 2022-08-30 stsp
108 b1aa94aa 2022-08-30 stsp /*
109 b1aa94aa 2022-08-30 stsp * Now write out all the joined chunks.
110 b1aa94aa 2022-08-30 stsp *
111 b1aa94aa 2022-08-30 stsp * If the hunk denotes a change, it will come in the form of a deletion
112 b1aa94aa 2022-08-30 stsp * chunk followed by a addition chunk. Print a marker to break up the
113 b1aa94aa 2022-08-30 stsp * additions and deletions when this happens.
114 b1aa94aa 2022-08-30 stsp */
115 b1aa94aa 2022-08-30 stsp int c_idx;
116 b1aa94aa 2022-08-30 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
117 b1aa94aa 2022-08-30 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
118 b1aa94aa 2022-08-30 stsp if (c->left_count && !c->right_count)
119 b1aa94aa 2022-08-30 stsp rc = diff_output_lines(outinfo, dest,
120 b1aa94aa 2022-08-30 stsp c->solved ? "< " : "?",
121 b1aa94aa 2022-08-30 stsp c->left_start, c->left_count);
122 b1aa94aa 2022-08-30 stsp else if (c->right_count && !c->left_count) {
123 9230203d 2022-09-02 stsp if (change) {
124 9230203d 2022-09-02 stsp rc = fprintf(dest, "---\n");
125 9230203d 2022-09-02 stsp if (rc < 0)
126 9230203d 2022-09-02 stsp return errno;
127 9230203d 2022-09-02 stsp if (outinfo) {
128 9230203d 2022-09-02 stsp ARRAYLIST_ADD(offp,
129 9230203d 2022-09-02 stsp outinfo->line_offsets);
130 9230203d 2022-09-02 stsp if (offp == NULL)
131 9230203d 2022-09-02 stsp return ENOMEM;
132 9230203d 2022-09-02 stsp *outoff += rc;
133 9230203d 2022-09-02 stsp *offp = *outoff;
134 9230203d 2022-09-02 stsp }
135 9230203d 2022-09-02 stsp }
136 b1aa94aa 2022-08-30 stsp rc = diff_output_lines(outinfo, dest,
137 b1aa94aa 2022-08-30 stsp c->solved ? "> " : "?",
138 b1aa94aa 2022-08-30 stsp c->right_start, c->right_count);
139 b1aa94aa 2022-08-30 stsp }
140 b1aa94aa 2022-08-30 stsp if (rc)
141 b1aa94aa 2022-08-30 stsp return rc;
142 b1aa94aa 2022-08-30 stsp if (cc->chunk.end == result->chunks.len) {
143 b1aa94aa 2022-08-30 stsp rc = diff_output_trailing_newline_msg(outinfo, dest, c);
144 b1aa94aa 2022-08-30 stsp if (rc != DIFF_RC_OK)
145 b1aa94aa 2022-08-30 stsp return rc;
146 b1aa94aa 2022-08-30 stsp }
147 b1aa94aa 2022-08-30 stsp }
148 b1aa94aa 2022-08-30 stsp
149 b1aa94aa 2022-08-30 stsp return DIFF_RC_OK;
150 b1aa94aa 2022-08-30 stsp }
151 b1aa94aa 2022-08-30 stsp
152 3e6cba3a 2020-08-13 stsp int
153 b1aa94aa 2022-08-30 stsp diff_output_plain(struct diff_output_info **output_info,
154 b1aa94aa 2022-08-30 stsp FILE *dest, const struct diff_input_info *info,
155 b1aa94aa 2022-08-30 stsp const struct diff_result *result)
156 3b0f3d61 2020-01-22 neels {
157 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
158 b1aa94aa 2022-08-30 stsp struct diff_chunk_context cc = {};
159 b1aa94aa 2022-08-30 stsp int atomizer_flags = (result->left->atomizer_flags|
160 b1aa94aa 2022-08-30 stsp result->right->atomizer_flags);
161 b1aa94aa 2022-08-30 stsp int flags = (result->left->root->diff_flags |
162 b1aa94aa 2022-08-30 stsp result->right->root->diff_flags);
163 b1aa94aa 2022-08-30 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
164 b1aa94aa 2022-08-30 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
165 8cba9b5e 2020-09-22 stsp int i, rc;
166 9230203d 2022-09-02 stsp off_t outoff = 0, *offp;
167 2c20a3ed 2020-09-22 stsp
168 3b0f3d61 2020-01-22 neels if (!result)
169 3e6cba3a 2020-08-13 stsp return EINVAL;
170 3b0f3d61 2020-01-22 neels if (result->rc != DIFF_RC_OK)
171 3b0f3d61 2020-01-22 neels return result->rc;
172 b1aa94aa 2022-08-30 stsp
173 2c20a3ed 2020-09-22 stsp if (output_info) {
174 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
175 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
176 b1aa94aa 2022-08-30 stsp return ENOMEM;
177 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
178 2c20a3ed 2020-09-22 stsp }
179 3b0f3d61 2020-01-22 neels
180 b1aa94aa 2022-08-30 stsp if (have_binary && !force_text) {
181 b1aa94aa 2022-08-30 stsp for (i = 0; i < result->chunks.len; i++) {
182 b1aa94aa 2022-08-30 stsp struct diff_chunk *c = &result->chunks.head[i];
183 b1aa94aa 2022-08-30 stsp enum diff_chunk_type t = diff_chunk_type(c);
184 b1aa94aa 2022-08-30 stsp
185 b1aa94aa 2022-08-30 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
186 b1aa94aa 2022-08-30 stsp continue;
187 b1aa94aa 2022-08-30 stsp
188 9230203d 2022-09-02 stsp rc = fprintf(dest, "Binary files %s and %s differ\n",
189 b1aa94aa 2022-08-30 stsp diff_output_get_label_left(info),
190 b1aa94aa 2022-08-30 stsp diff_output_get_label_right(info));
191 9230203d 2022-09-02 stsp if (rc < 0)
192 9230203d 2022-09-02 stsp return errno;
193 9230203d 2022-09-02 stsp if (outinfo) {
194 9230203d 2022-09-02 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
195 9230203d 2022-09-02 stsp if (offp == NULL)
196 9230203d 2022-09-02 stsp return ENOMEM;
197 9230203d 2022-09-02 stsp outoff += rc;
198 9230203d 2022-09-02 stsp *offp = outoff;
199 9230203d 2022-09-02 stsp }
200 b1aa94aa 2022-08-30 stsp break;
201 b1aa94aa 2022-08-30 stsp }
202 b1aa94aa 2022-08-30 stsp
203 b1aa94aa 2022-08-30 stsp return DIFF_RC_OK;
204 b1aa94aa 2022-08-30 stsp }
205 b1aa94aa 2022-08-30 stsp
206 3b0f3d61 2020-01-22 neels for (i = 0; i < result->chunks.len; i++) {
207 b1aa94aa 2022-08-30 stsp struct diff_chunk *chunk = &result->chunks.head[i];
208 b1aa94aa 2022-08-30 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
209 b1aa94aa 2022-08-30 stsp struct diff_chunk_context next;
210 b1aa94aa 2022-08-30 stsp
211 b1aa94aa 2022-08-30 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
212 b1aa94aa 2022-08-30 stsp continue;
213 b1aa94aa 2022-08-30 stsp
214 b1aa94aa 2022-08-30 stsp if (diff_chunk_context_empty(&cc)) {
215 b1aa94aa 2022-08-30 stsp /* Note down the start point, any number of subsequent
216 b1aa94aa 2022-08-30 stsp * chunks may be joined up to this chunk by being
217 b1aa94aa 2022-08-30 stsp * directly adjacent. */
218 b1aa94aa 2022-08-30 stsp diff_chunk_context_get(&cc, result, i, 0);
219 b1aa94aa 2022-08-30 stsp continue;
220 b1aa94aa 2022-08-30 stsp }
221 b1aa94aa 2022-08-30 stsp
222 b1aa94aa 2022-08-30 stsp /* There already is a previous chunk noted down for being
223 b1aa94aa 2022-08-30 stsp * printed. Does it join up with this one? */
224 b1aa94aa 2022-08-30 stsp diff_chunk_context_get(&next, result, i, 0);
225 b1aa94aa 2022-08-30 stsp
226 b1aa94aa 2022-08-30 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
227 b1aa94aa 2022-08-30 stsp /* This next context touches or overlaps the previous
228 b1aa94aa 2022-08-30 stsp * one, join. */
229 b1aa94aa 2022-08-30 stsp diff_chunk_contexts_merge(&cc, &next);
230 b1aa94aa 2022-08-30 stsp /* When we merge the last chunk we can end up with one
231 b1aa94aa 2022-08-30 stsp * hanging chunk and have to come back for it after the
232 b1aa94aa 2022-08-30 stsp * loop */
233 b1aa94aa 2022-08-30 stsp continue;
234 b1aa94aa 2022-08-30 stsp }
235 9230203d 2022-09-02 stsp rc = output_plain_chunk(outinfo, dest, info, result, &cc,
236 9230203d 2022-09-02 stsp &outoff);
237 b1aa94aa 2022-08-30 stsp if (rc != DIFF_RC_OK)
238 8cba9b5e 2020-09-22 stsp return rc;
239 b1aa94aa 2022-08-30 stsp cc = next;
240 3b0f3d61 2020-01-22 neels }
241 b1aa94aa 2022-08-30 stsp if (!diff_chunk_context_empty(&cc))
242 9230203d 2022-09-02 stsp return output_plain_chunk(outinfo, dest, info, result, &cc,
243 9230203d 2022-09-02 stsp &outoff);
244 3b0f3d61 2020-01-22 neels return DIFF_RC_OK;
245 3b0f3d61 2020-01-22 neels }