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