Blame


1 fe621944 2020-11-10 stsp /* Common parts for printing diff output */
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 <ctype.h>
19 fe621944 2020-11-10 stsp #include <errno.h>
20 fe621944 2020-11-10 stsp #include <stdbool.h>
21 f3c44083 2020-11-14 naddy #include <stdint.h>
22 fe621944 2020-11-10 stsp #include <stdio.h>
23 fe621944 2020-11-10 stsp #include <stdlib.h>
24 fe621944 2020-11-10 stsp #include <string.h>
25 fe621944 2020-11-10 stsp #include <unistd.h>
26 fe621944 2020-11-10 stsp
27 fe621944 2020-11-10 stsp #include <arraylist.h>
28 fe621944 2020-11-10 stsp #include <diff_main.h>
29 fe621944 2020-11-10 stsp #include <diff_output.h>
30 fe621944 2020-11-10 stsp
31 fe621944 2020-11-10 stsp #include "diff_internal.h"
32 fe621944 2020-11-10 stsp
33 fe621944 2020-11-10 stsp static int
34 fe621944 2020-11-10 stsp get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
35 fe621944 2020-11-10 stsp {
36 fe621944 2020-11-10 stsp off_t cur;
37 fe621944 2020-11-10 stsp
38 fe621944 2020-11-10 stsp if (atom->at != NULL) {
39 fe621944 2020-11-10 stsp *ch = atom->at[off];
40 fe621944 2020-11-10 stsp return 0;
41 fe621944 2020-11-10 stsp }
42 fe621944 2020-11-10 stsp
43 fe621944 2020-11-10 stsp cur = ftello(atom->root->f);
44 fe621944 2020-11-10 stsp if (cur == -1)
45 fe621944 2020-11-10 stsp return errno;
46 fe621944 2020-11-10 stsp
47 fe621944 2020-11-10 stsp if (cur != atom->pos + off &&
48 fe621944 2020-11-10 stsp fseeko(atom->root->f, atom->pos + off, SEEK_SET) == -1)
49 fe621944 2020-11-10 stsp return errno;
50 fe621944 2020-11-10 stsp
51 fe621944 2020-11-10 stsp *ch = fgetc(atom->root->f);
52 fe621944 2020-11-10 stsp if (*ch == EOF && ferror(atom->root->f))
53 fe621944 2020-11-10 stsp return errno;
54 fe621944 2020-11-10 stsp
55 fe621944 2020-11-10 stsp return 0;
56 fe621944 2020-11-10 stsp }
57 fe621944 2020-11-10 stsp
58 fe621944 2020-11-10 stsp int
59 fe621944 2020-11-10 stsp diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
60 fe621944 2020-11-10 stsp const char *prefix, struct diff_atom *start_atom,
61 fe621944 2020-11-10 stsp unsigned int count)
62 fe621944 2020-11-10 stsp {
63 fe621944 2020-11-10 stsp struct diff_atom *atom;
64 fe621944 2020-11-10 stsp off_t outoff = 0, *offp;
65 fe621944 2020-11-10 stsp int rc;
66 fe621944 2020-11-10 stsp
67 fe621944 2020-11-10 stsp if (outinfo && outinfo->line_offsets.len > 0) {
68 fe621944 2020-11-10 stsp unsigned int idx = outinfo->line_offsets.len - 1;
69 fe621944 2020-11-10 stsp outoff = outinfo->line_offsets.head[idx];
70 fe621944 2020-11-10 stsp }
71 fe621944 2020-11-10 stsp
72 fe621944 2020-11-10 stsp foreach_diff_atom(atom, start_atom, count) {
73 fe621944 2020-11-10 stsp off_t outlen = 0;
74 fe621944 2020-11-10 stsp int i, ch;
75 fe621944 2020-11-10 stsp unsigned int len = atom->len;
76 fe621944 2020-11-10 stsp rc = fprintf(dest, "%s", prefix);
77 fe621944 2020-11-10 stsp if (rc < 0)
78 fe621944 2020-11-10 stsp return errno;
79 fe621944 2020-11-10 stsp outlen += rc;
80 fe621944 2020-11-10 stsp if (len) {
81 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, len - 1);
82 fe621944 2020-11-10 stsp if (rc)
83 fe621944 2020-11-10 stsp return rc;
84 fe621944 2020-11-10 stsp if (ch == '\n')
85 fe621944 2020-11-10 stsp len--;
86 fe621944 2020-11-10 stsp if (len) {
87 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, len - 1);
88 fe621944 2020-11-10 stsp if (rc)
89 fe621944 2020-11-10 stsp return rc;
90 fe621944 2020-11-10 stsp if (ch == '\r')
91 fe621944 2020-11-10 stsp len--;
92 fe621944 2020-11-10 stsp }
93 fe621944 2020-11-10 stsp }
94 fe621944 2020-11-10 stsp
95 fe621944 2020-11-10 stsp for (i = 0; i < len; i++) {
96 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, i);
97 fe621944 2020-11-10 stsp if (rc)
98 fe621944 2020-11-10 stsp return rc;
99 fe621944 2020-11-10 stsp rc = fprintf(dest, "%c", (unsigned char)ch);
100 fe621944 2020-11-10 stsp if (rc < 0)
101 fe621944 2020-11-10 stsp return errno;
102 fe621944 2020-11-10 stsp outlen += rc;
103 fe621944 2020-11-10 stsp }
104 fe621944 2020-11-10 stsp rc = fprintf(dest, "\n");
105 fe621944 2020-11-10 stsp if (rc < 0)
106 fe621944 2020-11-10 stsp return errno;
107 fe621944 2020-11-10 stsp outlen += rc;
108 fe621944 2020-11-10 stsp if (outinfo) {
109 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
110 fe621944 2020-11-10 stsp if (offp == NULL)
111 fe621944 2020-11-10 stsp return ENOMEM;
112 fe621944 2020-11-10 stsp outoff += outlen;
113 fe621944 2020-11-10 stsp *offp = outoff;
114 fe621944 2020-11-10 stsp }
115 fe621944 2020-11-10 stsp }
116 fe621944 2020-11-10 stsp
117 fe621944 2020-11-10 stsp return DIFF_RC_OK;
118 fe621944 2020-11-10 stsp }
119 fe621944 2020-11-10 stsp
120 fe621944 2020-11-10 stsp int
121 fe621944 2020-11-10 stsp diff_output_chunk_left_version(struct diff_output_info **output_info,
122 fe621944 2020-11-10 stsp FILE *dest,
123 fe621944 2020-11-10 stsp const struct diff_input_info *info,
124 fe621944 2020-11-10 stsp const struct diff_result *result,
125 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
126 fe621944 2020-11-10 stsp {
127 fe621944 2020-11-10 stsp int rc, c_idx;
128 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
129 fe621944 2020-11-10 stsp
130 fe621944 2020-11-10 stsp if (diff_range_empty(&cc->left))
131 fe621944 2020-11-10 stsp return DIFF_RC_OK;
132 fe621944 2020-11-10 stsp
133 fe621944 2020-11-10 stsp if (output_info) {
134 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
135 fe621944 2020-11-10 stsp if (*output_info == NULL)
136 fe621944 2020-11-10 stsp return ENOMEM;
137 fe621944 2020-11-10 stsp outinfo = *output_info;
138 fe621944 2020-11-10 stsp }
139 fe621944 2020-11-10 stsp
140 fe621944 2020-11-10 stsp /* Write out all chunks on the left side. */
141 fe621944 2020-11-10 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
142 fe621944 2020-11-10 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
143 fe621944 2020-11-10 stsp
144 fe621944 2020-11-10 stsp if (c->left_count) {
145 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, "",
146 fe621944 2020-11-10 stsp c->left_start, c->left_count);
147 fe621944 2020-11-10 stsp if (rc)
148 fe621944 2020-11-10 stsp return rc;
149 fe621944 2020-11-10 stsp }
150 fe621944 2020-11-10 stsp }
151 fe621944 2020-11-10 stsp
152 fe621944 2020-11-10 stsp return DIFF_RC_OK;
153 fe621944 2020-11-10 stsp }
154 fe621944 2020-11-10 stsp
155 fe621944 2020-11-10 stsp int
156 fe621944 2020-11-10 stsp diff_output_chunk_right_version(struct diff_output_info **output_info,
157 fe621944 2020-11-10 stsp FILE *dest,
158 fe621944 2020-11-10 stsp const struct diff_input_info *info,
159 fe621944 2020-11-10 stsp const struct diff_result *result,
160 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
161 fe621944 2020-11-10 stsp {
162 fe621944 2020-11-10 stsp int rc, c_idx;
163 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
164 fe621944 2020-11-10 stsp
165 fe621944 2020-11-10 stsp if (diff_range_empty(&cc->right))
166 fe621944 2020-11-10 stsp return DIFF_RC_OK;
167 fe621944 2020-11-10 stsp
168 fe621944 2020-11-10 stsp if (output_info) {
169 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
170 fe621944 2020-11-10 stsp if (*output_info == NULL)
171 fe621944 2020-11-10 stsp return ENOMEM;
172 fe621944 2020-11-10 stsp outinfo = *output_info;
173 fe621944 2020-11-10 stsp }
174 fe621944 2020-11-10 stsp
175 fe621944 2020-11-10 stsp /* Write out all chunks on the right side. */
176 fe621944 2020-11-10 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
177 fe621944 2020-11-10 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
178 fe621944 2020-11-10 stsp
179 fe621944 2020-11-10 stsp if (c->right_count) {
180 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, "", c->right_start,
181 fe621944 2020-11-10 stsp c->right_count);
182 fe621944 2020-11-10 stsp if (rc)
183 fe621944 2020-11-10 stsp return rc;
184 fe621944 2020-11-10 stsp }
185 fe621944 2020-11-10 stsp }
186 fe621944 2020-11-10 stsp
187 fe621944 2020-11-10 stsp return DIFF_RC_OK;
188 fe621944 2020-11-10 stsp }
189 fe621944 2020-11-10 stsp
190 fe621944 2020-11-10 stsp int
191 fe621944 2020-11-10 stsp diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
192 fe621944 2020-11-10 stsp const struct diff_chunk *c)
193 fe621944 2020-11-10 stsp {
194 fe621944 2020-11-10 stsp enum diff_chunk_type chunk_type = diff_chunk_type(c);
195 fe621944 2020-11-10 stsp struct diff_atom *atom, *start_atom;
196 fe621944 2020-11-10 stsp unsigned int atom_count;
197 fe621944 2020-11-10 stsp int rc, ch;
198 fe621944 2020-11-10 stsp off_t outoff = 0, *offp;
199 fe621944 2020-11-10 stsp
200 fe621944 2020-11-10 stsp if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
201 fe621944 2020-11-10 stsp start_atom = c->left_start;
202 fe621944 2020-11-10 stsp atom_count = c->left_count;
203 fe621944 2020-11-10 stsp } else if (chunk_type == CHUNK_PLUS) {
204 fe621944 2020-11-10 stsp start_atom = c->right_start;
205 fe621944 2020-11-10 stsp atom_count = c->right_count;
206 fe621944 2020-11-10 stsp } else
207 fe621944 2020-11-10 stsp return EINVAL;
208 fe621944 2020-11-10 stsp
209 fe621944 2020-11-10 stsp /* Locate the last atom. */
210 fe621944 2020-11-10 stsp if (atom_count == 0)
211 fe621944 2020-11-10 stsp return EINVAL;
212 fe621944 2020-11-10 stsp atom = &start_atom[atom_count - 1];
213 fe621944 2020-11-10 stsp
214 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, atom->len - 1);
215 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
216 fe621944 2020-11-10 stsp return rc;
217 fe621944 2020-11-10 stsp
218 fe621944 2020-11-10 stsp if (ch != '\n') {
219 fe621944 2020-11-10 stsp if (outinfo && outinfo->line_offsets.len > 0) {
220 fe621944 2020-11-10 stsp unsigned int idx = outinfo->line_offsets.len - 1;
221 fe621944 2020-11-10 stsp outoff = outinfo->line_offsets.head[idx];
222 fe621944 2020-11-10 stsp }
223 fe621944 2020-11-10 stsp rc = fprintf(dest, "\\ No newline at end of file\n");
224 fe621944 2020-11-10 stsp if (rc < 0)
225 fe621944 2020-11-10 stsp return errno;
226 fe621944 2020-11-10 stsp if (outinfo) {
227 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
228 fe621944 2020-11-10 stsp if (offp == NULL)
229 fe621944 2020-11-10 stsp return ENOMEM;
230 fe621944 2020-11-10 stsp outoff += rc;
231 fe621944 2020-11-10 stsp *offp = outoff;
232 fe621944 2020-11-10 stsp }
233 fe621944 2020-11-10 stsp }
234 fe621944 2020-11-10 stsp
235 fe621944 2020-11-10 stsp return DIFF_RC_OK;
236 fe621944 2020-11-10 stsp }
237 fe621944 2020-11-10 stsp
238 fe621944 2020-11-10 stsp static bool
239 f3b2b552 2020-12-10 stsp is_function_prototype(unsigned char ch)
240 fe621944 2020-11-10 stsp {
241 f3b2b552 2020-12-10 stsp return (isalpha(ch) || ch == '_' || ch == '$');
242 fe621944 2020-11-10 stsp }
243 fe621944 2020-11-10 stsp
244 fe621944 2020-11-10 stsp #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
245 fe621944 2020-11-10 stsp
246 fe621944 2020-11-10 stsp int
247 f3b2b552 2020-12-10 stsp diff_output_match_function_prototype(char *prototype, size_t prototype_size,
248 f3b2b552 2020-12-10 stsp int *last_prototype_idx, const struct diff_result *result,
249 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
250 fe621944 2020-11-10 stsp {
251 fe621944 2020-11-10 stsp struct diff_atom *start_atom, *atom;
252 fe621944 2020-11-10 stsp const struct diff_data *data;
253 f3b2b552 2020-12-10 stsp unsigned char buf[DIFF_FUNCTION_CONTEXT_SIZE];
254 fe621944 2020-11-10 stsp char *state = NULL;
255 f3b2b552 2020-12-10 stsp int rc, i, ch;
256 fe621944 2020-11-10 stsp
257 fe621944 2020-11-10 stsp if (result->left->atoms.len > 0 && cc->left.start > 0) {
258 fe621944 2020-11-10 stsp data = result->left;
259 fe621944 2020-11-10 stsp start_atom = &data->atoms.head[cc->left.start - 1];
260 fe621944 2020-11-10 stsp } else
261 fe621944 2020-11-10 stsp return DIFF_RC_OK;
262 fe621944 2020-11-10 stsp
263 fe621944 2020-11-10 stsp diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
264 f3b2b552 2020-12-10 stsp int atom_idx = diff_atom_root_idx(data, atom);
265 f3b2b552 2020-12-10 stsp if (atom_idx < *last_prototype_idx)
266 f3b2b552 2020-12-10 stsp break;
267 f3b2b552 2020-12-10 stsp rc = get_atom_byte(&ch, atom, 0);
268 f3b2b552 2020-12-10 stsp if (rc)
269 f3b2b552 2020-12-10 stsp return rc;
270 f3b2b552 2020-12-10 stsp buf[0] = (unsigned char)ch;
271 f3b2b552 2020-12-10 stsp if (!is_function_prototype(buf[0]))
272 f3b2b552 2020-12-10 stsp continue;
273 f3b2b552 2020-12-10 stsp for (i = 1; i < atom->len && i < sizeof(buf) - 1; i++) {
274 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, i);
275 fe621944 2020-11-10 stsp if (rc)
276 fe621944 2020-11-10 stsp return rc;
277 fe621944 2020-11-10 stsp if (ch == '\n')
278 fe621944 2020-11-10 stsp break;
279 f3b2b552 2020-12-10 stsp buf[i] = (unsigned char)ch;
280 fe621944 2020-11-10 stsp }
281 fe621944 2020-11-10 stsp buf[i] = '\0';
282 f3b2b552 2020-12-10 stsp if (begins_with(buf, "private:")) {
283 f3b2b552 2020-12-10 stsp if (!state)
284 f3b2b552 2020-12-10 stsp state = " (private)";
285 f3b2b552 2020-12-10 stsp } else if (begins_with(buf, "protected:")) {
286 f3b2b552 2020-12-10 stsp if (!state)
287 f3b2b552 2020-12-10 stsp state = " (protected)";
288 f3b2b552 2020-12-10 stsp } else if (begins_with(buf, "public:")) {
289 f3b2b552 2020-12-10 stsp if (!state)
290 f3b2b552 2020-12-10 stsp state = " (public)";
291 f3b2b552 2020-12-10 stsp } else {
292 f3b2b552 2020-12-10 stsp if (state) /* don't care about truncation */
293 f3b2b552 2020-12-10 stsp strlcat(buf, state, sizeof(buf));
294 f3b2b552 2020-12-10 stsp strlcpy(prototype, buf, prototype_size);
295 f3b2b552 2020-12-10 stsp break;
296 fe621944 2020-11-10 stsp }
297 fe621944 2020-11-10 stsp }
298 fe621944 2020-11-10 stsp
299 f3b2b552 2020-12-10 stsp *last_prototype_idx = diff_atom_root_idx(data, start_atom);
300 fe621944 2020-11-10 stsp return DIFF_RC_OK;
301 fe621944 2020-11-10 stsp }
302 fe621944 2020-11-10 stsp
303 fe621944 2020-11-10 stsp struct diff_output_info *
304 fe621944 2020-11-10 stsp diff_output_info_alloc(void)
305 fe621944 2020-11-10 stsp {
306 fe621944 2020-11-10 stsp struct diff_output_info *output_info;
307 fe621944 2020-11-10 stsp off_t *offp;
308 fe621944 2020-11-10 stsp
309 fe621944 2020-11-10 stsp output_info = malloc(sizeof(*output_info));
310 fe621944 2020-11-10 stsp if (output_info != NULL) {
311 fe621944 2020-11-10 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
312 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
313 fe621944 2020-11-10 stsp if (offp == NULL) {
314 fe621944 2020-11-10 stsp diff_output_info_free(output_info);
315 fe621944 2020-11-10 stsp return NULL;
316 fe621944 2020-11-10 stsp }
317 fe621944 2020-11-10 stsp *offp = 0;
318 fe621944 2020-11-10 stsp }
319 fe621944 2020-11-10 stsp return output_info;
320 fe621944 2020-11-10 stsp }
321 fe621944 2020-11-10 stsp
322 fe621944 2020-11-10 stsp void
323 fe621944 2020-11-10 stsp diff_output_info_free(struct diff_output_info *output_info)
324 fe621944 2020-11-10 stsp {
325 fe621944 2020-11-10 stsp ARRAYLIST_FREE(output_info->line_offsets);
326 fe621944 2020-11-10 stsp free(output_info);
327 c4cd9c5b 2020-11-21 stsp }
328 c4cd9c5b 2020-11-21 stsp
329 c4cd9c5b 2020-11-21 stsp const char *
330 c4cd9c5b 2020-11-21 stsp diff_output_get_label_left(const struct diff_input_info *info)
331 c4cd9c5b 2020-11-21 stsp {
332 c4cd9c5b 2020-11-21 stsp if (info->flags & DIFF_INPUT_LEFT_NONEXISTENT)
333 c4cd9c5b 2020-11-21 stsp return "/dev/null";
334 c4cd9c5b 2020-11-21 stsp
335 c4cd9c5b 2020-11-21 stsp return info->left_path ? : "a";
336 c4cd9c5b 2020-11-21 stsp }
337 c4cd9c5b 2020-11-21 stsp
338 c4cd9c5b 2020-11-21 stsp const char *
339 c4cd9c5b 2020-11-21 stsp diff_output_get_label_right(const struct diff_input_info *info)
340 c4cd9c5b 2020-11-21 stsp {
341 c4cd9c5b 2020-11-21 stsp if (info->flags & DIFF_INPUT_RIGHT_NONEXISTENT)
342 c4cd9c5b 2020-11-21 stsp return "/dev/null";
343 c4cd9c5b 2020-11-21 stsp
344 c4cd9c5b 2020-11-21 stsp return info->right_path ? : "b";
345 fe621944 2020-11-10 stsp }