1 fe621944 2020-11-10 stsp /* Common parts for printing diff output */
3 fe621944 2020-11-10 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
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>
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>
31 fe621944 2020-11-10 stsp #include "diff_internal.h"
34 fe621944 2020-11-10 stsp get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
38 fe621944 2020-11-10 stsp if (atom->at != NULL) {
39 fe621944 2020-11-10 stsp *ch = atom->at[off];
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;
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;
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;
58 4f655d1b 2021-07-08 stsp #define DIFF_OUTPUT_BUF_SIZE 512
61 fe621944 2020-11-10 stsp diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
62 fe621944 2020-11-10 stsp const char *prefix, struct diff_atom *start_atom,
63 fe621944 2020-11-10 stsp unsigned int count)
65 fe621944 2020-11-10 stsp struct diff_atom *atom;
66 fe621944 2020-11-10 stsp off_t outoff = 0, *offp;
67 b77ebd68 2022-08-04 mark uint8_t *typep;
70 fe621944 2020-11-10 stsp if (outinfo && outinfo->line_offsets.len > 0) {
71 fe621944 2020-11-10 stsp unsigned int idx = outinfo->line_offsets.len - 1;
72 fe621944 2020-11-10 stsp outoff = outinfo->line_offsets.head[idx];
75 fe621944 2020-11-10 stsp foreach_diff_atom(atom, start_atom, count) {
76 fe621944 2020-11-10 stsp off_t outlen = 0;
77 4f655d1b 2021-07-08 stsp int i, ch, nbuf = 0;
78 fe621944 2020-11-10 stsp unsigned int len = atom->len;
79 4f655d1b 2021-07-08 stsp unsigned char buf[DIFF_OUTPUT_BUF_SIZE + 1 /* '\n' */];
82 4f655d1b 2021-07-08 stsp n = strlcpy(buf, prefix, sizeof(buf));
83 4f655d1b 2021-07-08 stsp if (n >= DIFF_OUTPUT_BUF_SIZE) /* leave room for '\n' */
84 4f655d1b 2021-07-08 stsp return ENOBUFS;
88 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, len - 1);
91 fe621944 2020-11-10 stsp if (ch == '\n')
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);
99 4f655d1b 2021-07-08 stsp if (nbuf >= DIFF_OUTPUT_BUF_SIZE) {
100 4f655d1b 2021-07-08 stsp rc = fwrite(buf, 1, nbuf, dest);
101 4f655d1b 2021-07-08 stsp if (rc != nbuf)
102 4f655d1b 2021-07-08 stsp return errno;
103 4f655d1b 2021-07-08 stsp outlen += rc;
106 4f655d1b 2021-07-08 stsp buf[nbuf++] = ch;
108 4f655d1b 2021-07-08 stsp buf[nbuf++] = '\n';
109 4f655d1b 2021-07-08 stsp rc = fwrite(buf, 1, nbuf, dest);
110 4f655d1b 2021-07-08 stsp if (rc != nbuf)
111 fe621944 2020-11-10 stsp return errno;
112 fe621944 2020-11-10 stsp outlen += rc;
113 fe621944 2020-11-10 stsp if (outinfo) {
114 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
115 fe621944 2020-11-10 stsp if (offp == NULL)
116 fe621944 2020-11-10 stsp return ENOMEM;
117 fe621944 2020-11-10 stsp outoff += outlen;
118 fe621944 2020-11-10 stsp *offp = outoff;
119 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
120 b77ebd68 2022-08-04 mark if (typep == NULL)
121 b77ebd68 2022-08-04 mark return ENOMEM;
122 b77ebd68 2022-08-04 mark *typep = *prefix == ' ' ? DIFF_LINE_CONTEXT :
123 b77ebd68 2022-08-04 mark *prefix == '-' ? DIFF_LINE_MINUS :
124 b77ebd68 2022-08-04 mark *prefix == '+' ? DIFF_LINE_PLUS : DIFF_LINE_NONE;
128 fe621944 2020-11-10 stsp return DIFF_RC_OK;
132 fe621944 2020-11-10 stsp diff_output_chunk_left_version(struct diff_output_info **output_info,
133 fe621944 2020-11-10 stsp FILE *dest,
134 fe621944 2020-11-10 stsp const struct diff_input_info *info,
135 fe621944 2020-11-10 stsp const struct diff_result *result,
136 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
138 fe621944 2020-11-10 stsp int rc, c_idx;
139 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
141 fe621944 2020-11-10 stsp if (diff_range_empty(&cc->left))
142 fe621944 2020-11-10 stsp return DIFF_RC_OK;
144 fe621944 2020-11-10 stsp if (output_info) {
145 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
146 fe621944 2020-11-10 stsp if (*output_info == NULL)
147 fe621944 2020-11-10 stsp return ENOMEM;
148 fe621944 2020-11-10 stsp outinfo = *output_info;
151 fe621944 2020-11-10 stsp /* Write out all chunks on the left side. */
152 fe621944 2020-11-10 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
153 fe621944 2020-11-10 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
155 fe621944 2020-11-10 stsp if (c->left_count) {
156 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, "",
157 fe621944 2020-11-10 stsp c->left_start, c->left_count);
163 fe621944 2020-11-10 stsp return DIFF_RC_OK;
167 fe621944 2020-11-10 stsp diff_output_chunk_right_version(struct diff_output_info **output_info,
168 fe621944 2020-11-10 stsp FILE *dest,
169 fe621944 2020-11-10 stsp const struct diff_input_info *info,
170 fe621944 2020-11-10 stsp const struct diff_result *result,
171 fe621944 2020-11-10 stsp const struct diff_chunk_context *cc)
173 fe621944 2020-11-10 stsp int rc, c_idx;
174 fe621944 2020-11-10 stsp struct diff_output_info *outinfo = NULL;
176 fe621944 2020-11-10 stsp if (diff_range_empty(&cc->right))
177 fe621944 2020-11-10 stsp return DIFF_RC_OK;
179 fe621944 2020-11-10 stsp if (output_info) {
180 fe621944 2020-11-10 stsp *output_info = diff_output_info_alloc();
181 fe621944 2020-11-10 stsp if (*output_info == NULL)
182 fe621944 2020-11-10 stsp return ENOMEM;
183 fe621944 2020-11-10 stsp outinfo = *output_info;
186 fe621944 2020-11-10 stsp /* Write out all chunks on the right side. */
187 fe621944 2020-11-10 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
188 fe621944 2020-11-10 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
190 fe621944 2020-11-10 stsp if (c->right_count) {
191 fe621944 2020-11-10 stsp rc = diff_output_lines(outinfo, dest, "", c->right_start,
192 fe621944 2020-11-10 stsp c->right_count);
198 fe621944 2020-11-10 stsp return DIFF_RC_OK;
202 fe621944 2020-11-10 stsp diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
203 fe621944 2020-11-10 stsp const struct diff_chunk *c)
205 fe621944 2020-11-10 stsp enum diff_chunk_type chunk_type = diff_chunk_type(c);
206 fe621944 2020-11-10 stsp struct diff_atom *atom, *start_atom;
207 fe621944 2020-11-10 stsp unsigned int atom_count;
208 fe621944 2020-11-10 stsp int rc, ch;
209 fe621944 2020-11-10 stsp off_t outoff = 0, *offp;
210 b77ebd68 2022-08-04 mark uint8_t *typep;
213 fe621944 2020-11-10 stsp if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
214 fe621944 2020-11-10 stsp start_atom = c->left_start;
215 fe621944 2020-11-10 stsp atom_count = c->left_count;
216 fe621944 2020-11-10 stsp } else if (chunk_type == CHUNK_PLUS) {
217 fe621944 2020-11-10 stsp start_atom = c->right_start;
218 fe621944 2020-11-10 stsp atom_count = c->right_count;
220 fe621944 2020-11-10 stsp return EINVAL;
222 fe621944 2020-11-10 stsp /* Locate the last atom. */
223 fe621944 2020-11-10 stsp if (atom_count == 0)
224 fe621944 2020-11-10 stsp return EINVAL;
225 fe621944 2020-11-10 stsp atom = &start_atom[atom_count - 1];
227 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, atom->len - 1);
228 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK)
231 fe621944 2020-11-10 stsp if (ch != '\n') {
232 fe621944 2020-11-10 stsp if (outinfo && outinfo->line_offsets.len > 0) {
233 fe621944 2020-11-10 stsp unsigned int idx = outinfo->line_offsets.len - 1;
234 fe621944 2020-11-10 stsp outoff = outinfo->line_offsets.head[idx];
236 fe621944 2020-11-10 stsp rc = fprintf(dest, "\\ No newline at end of file\n");
237 fe621944 2020-11-10 stsp if (rc < 0)
238 fe621944 2020-11-10 stsp return errno;
239 fe621944 2020-11-10 stsp if (outinfo) {
240 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
241 fe621944 2020-11-10 stsp if (offp == NULL)
242 fe621944 2020-11-10 stsp return ENOMEM;
243 fe621944 2020-11-10 stsp outoff += rc;
244 fe621944 2020-11-10 stsp *offp = outoff;
245 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
246 b77ebd68 2022-08-04 mark if (typep == NULL)
247 b77ebd68 2022-08-04 mark return ENOMEM;
248 b77ebd68 2022-08-04 mark *typep = DIFF_LINE_NONE;
252 fe621944 2020-11-10 stsp return DIFF_RC_OK;
255 fe621944 2020-11-10 stsp static bool
256 f3b2b552 2020-12-10 stsp is_function_prototype(unsigned char ch)
258 cee4532d 2022-11-17 op return (isalpha((unsigned char)ch) || ch == '_' || ch == '$');
261 fe621944 2020-11-10 stsp #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
264 f3b2b552 2020-12-10 stsp diff_output_match_function_prototype(char *prototype, size_t prototype_size,
265 f3b2b552 2020-12-10 stsp int *last_prototype_idx, const struct diff_result *result,
266 db3ca9d5 2023-08-29 stsp const struct diff_chunk_context *cc)
268 fe621944 2020-11-10 stsp struct diff_atom *start_atom, *atom;
269 fe621944 2020-11-10 stsp const struct diff_data *data;
270 f3b2b552 2020-12-10 stsp unsigned char buf[DIFF_FUNCTION_CONTEXT_SIZE];
271 58e31a80 2022-06-27 op const char *state = NULL;
272 db3ca9d5 2023-08-29 stsp int rc, i, ch;
274 fe621944 2020-11-10 stsp if (result->left->atoms.len > 0 && cc->left.start > 0) {
275 fe621944 2020-11-10 stsp data = result->left;
276 db3ca9d5 2023-08-29 stsp start_atom = &data->atoms.head[cc->left.start - 1];
278 fe621944 2020-11-10 stsp return DIFF_RC_OK;
280 fe621944 2020-11-10 stsp diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
281 f3b2b552 2020-12-10 stsp int atom_idx = diff_atom_root_idx(data, atom);
282 f3b2b552 2020-12-10 stsp if (atom_idx < *last_prototype_idx)
284 f3b2b552 2020-12-10 stsp rc = get_atom_byte(&ch, atom, 0);
287 f3b2b552 2020-12-10 stsp buf[0] = (unsigned char)ch;
288 f3b2b552 2020-12-10 stsp if (!is_function_prototype(buf[0]))
290 f3b2b552 2020-12-10 stsp for (i = 1; i < atom->len && i < sizeof(buf) - 1; i++) {
291 fe621944 2020-11-10 stsp rc = get_atom_byte(&ch, atom, i);
294 fe621944 2020-11-10 stsp if (ch == '\n')
296 f3b2b552 2020-12-10 stsp buf[i] = (unsigned char)ch;
298 fe621944 2020-11-10 stsp buf[i] = '\0';
299 f3b2b552 2020-12-10 stsp if (begins_with(buf, "private:")) {
300 f3b2b552 2020-12-10 stsp if (!state)
301 f3b2b552 2020-12-10 stsp state = " (private)";
302 f3b2b552 2020-12-10 stsp } else if (begins_with(buf, "protected:")) {
303 f3b2b552 2020-12-10 stsp if (!state)
304 f3b2b552 2020-12-10 stsp state = " (protected)";
305 f3b2b552 2020-12-10 stsp } else if (begins_with(buf, "public:")) {
306 f3b2b552 2020-12-10 stsp if (!state)
307 f3b2b552 2020-12-10 stsp state = " (public)";
309 f3b2b552 2020-12-10 stsp if (state) /* don't care about truncation */
310 f3b2b552 2020-12-10 stsp strlcat(buf, state, sizeof(buf));
311 f3b2b552 2020-12-10 stsp strlcpy(prototype, buf, prototype_size);
316 f3b2b552 2020-12-10 stsp *last_prototype_idx = diff_atom_root_idx(data, start_atom);
317 fe621944 2020-11-10 stsp return DIFF_RC_OK;
320 fe621944 2020-11-10 stsp struct diff_output_info *
321 fe621944 2020-11-10 stsp diff_output_info_alloc(void)
323 fe621944 2020-11-10 stsp struct diff_output_info *output_info;
324 fe621944 2020-11-10 stsp off_t *offp;
325 b77ebd68 2022-08-04 mark uint8_t *typep;
327 fe621944 2020-11-10 stsp output_info = malloc(sizeof(*output_info));
328 fe621944 2020-11-10 stsp if (output_info != NULL) {
329 fe621944 2020-11-10 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
330 fe621944 2020-11-10 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
331 fe621944 2020-11-10 stsp if (offp == NULL) {
332 fe621944 2020-11-10 stsp diff_output_info_free(output_info);
333 fe621944 2020-11-10 stsp return NULL;
336 b77ebd68 2022-08-04 mark ARRAYLIST_INIT(output_info->line_types, 128);
337 b77ebd68 2022-08-04 mark ARRAYLIST_ADD(typep, output_info->line_types);
338 b77ebd68 2022-08-04 mark if (typep == NULL) {
339 b77ebd68 2022-08-04 mark diff_output_info_free(output_info);
340 b77ebd68 2022-08-04 mark return NULL;
342 b77ebd68 2022-08-04 mark *typep = DIFF_LINE_NONE;
344 fe621944 2020-11-10 stsp return output_info;
348 fe621944 2020-11-10 stsp diff_output_info_free(struct diff_output_info *output_info)
350 fe621944 2020-11-10 stsp ARRAYLIST_FREE(output_info->line_offsets);
351 b77ebd68 2022-08-04 mark ARRAYLIST_FREE(output_info->line_types);
352 fe621944 2020-11-10 stsp free(output_info);
355 c4cd9c5b 2020-11-21 stsp const char *
356 c4cd9c5b 2020-11-21 stsp diff_output_get_label_left(const struct diff_input_info *info)
358 c4cd9c5b 2020-11-21 stsp if (info->flags & DIFF_INPUT_LEFT_NONEXISTENT)
359 c4cd9c5b 2020-11-21 stsp return "/dev/null";
361 116d19d9 2022-09-02 mark return info->left_path ? info->left_path : "a";
364 c4cd9c5b 2020-11-21 stsp const char *
365 c4cd9c5b 2020-11-21 stsp diff_output_get_label_right(const struct diff_input_info *info)
367 c4cd9c5b 2020-11-21 stsp if (info->flags & DIFF_INPUT_RIGHT_NONEXISTENT)
368 c4cd9c5b 2020-11-21 stsp return "/dev/null";
370 116d19d9 2022-09-02 mark return info->right_path ? info->right_path : "b";