Blob


1 /* Common parts for printing diff output */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include <arraylist.h>
28 #include <diff_main.h>
29 #include <diff_output.h>
31 #include "diff_internal.h"
33 static int
34 get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
35 {
36 off_t cur;
38 if (atom->at != NULL) {
39 *ch = atom->at[off];
40 return 0;
41 }
43 cur = ftello(atom->root->f);
44 if (cur == -1)
45 return errno;
47 if (cur != atom->pos + off &&
48 fseeko(atom->root->f, atom->pos + off, SEEK_SET) == -1)
49 return errno;
51 *ch = fgetc(atom->root->f);
52 if (*ch == EOF && ferror(atom->root->f))
53 return errno;
55 return 0;
56 }
58 int
59 diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
60 const char *prefix, struct diff_atom *start_atom,
61 unsigned int count)
62 {
63 struct diff_atom *atom;
64 off_t outoff = 0, *offp;
65 int rc;
67 if (outinfo && outinfo->line_offsets.len > 0) {
68 unsigned int idx = outinfo->line_offsets.len - 1;
69 outoff = outinfo->line_offsets.head[idx];
70 }
72 foreach_diff_atom(atom, start_atom, count) {
73 off_t outlen = 0;
74 int i, ch;
75 unsigned int len = atom->len;
76 rc = fprintf(dest, "%s", prefix);
77 if (rc < 0)
78 return errno;
79 outlen += rc;
80 if (len) {
81 rc = get_atom_byte(&ch, atom, len - 1);
82 if (rc)
83 return rc;
84 if (ch == '\n')
85 len--;
86 if (len) {
87 rc = get_atom_byte(&ch, atom, len - 1);
88 if (rc)
89 return rc;
90 if (ch == '\r')
91 len--;
92 }
93 }
95 for (i = 0; i < len; i++) {
96 rc = get_atom_byte(&ch, atom, i);
97 if (rc)
98 return rc;
99 rc = fprintf(dest, "%c", (unsigned char)ch);
100 if (rc < 0)
101 return errno;
102 outlen += rc;
104 rc = fprintf(dest, "\n");
105 if (rc < 0)
106 return errno;
107 outlen += rc;
108 if (outinfo) {
109 ARRAYLIST_ADD(offp, outinfo->line_offsets);
110 if (offp == NULL)
111 return ENOMEM;
112 outoff += outlen;
113 *offp = outoff;
117 return DIFF_RC_OK;
120 int
121 diff_output_chunk_left_version(struct diff_output_info **output_info,
122 FILE *dest,
123 const struct diff_input_info *info,
124 const struct diff_result *result,
125 const struct diff_chunk_context *cc)
127 int rc, c_idx;
128 struct diff_output_info *outinfo = NULL;
130 if (diff_range_empty(&cc->left))
131 return DIFF_RC_OK;
133 if (output_info) {
134 *output_info = diff_output_info_alloc();
135 if (*output_info == NULL)
136 return ENOMEM;
137 outinfo = *output_info;
140 /* Write out all chunks on the left side. */
141 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
142 const struct diff_chunk *c = &result->chunks.head[c_idx];
144 if (c->left_count) {
145 rc = diff_output_lines(outinfo, dest, "",
146 c->left_start, c->left_count);
147 if (rc)
148 return rc;
152 return DIFF_RC_OK;
155 int
156 diff_output_chunk_right_version(struct diff_output_info **output_info,
157 FILE *dest,
158 const struct diff_input_info *info,
159 const struct diff_result *result,
160 const struct diff_chunk_context *cc)
162 int rc, c_idx;
163 struct diff_output_info *outinfo = NULL;
165 if (diff_range_empty(&cc->right))
166 return DIFF_RC_OK;
168 if (output_info) {
169 *output_info = diff_output_info_alloc();
170 if (*output_info == NULL)
171 return ENOMEM;
172 outinfo = *output_info;
175 /* Write out all chunks on the right side. */
176 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
177 const struct diff_chunk *c = &result->chunks.head[c_idx];
179 if (c->right_count) {
180 rc = diff_output_lines(outinfo, dest, "", c->right_start,
181 c->right_count);
182 if (rc)
183 return rc;
187 return DIFF_RC_OK;
190 int
191 diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
192 const struct diff_chunk *c)
194 enum diff_chunk_type chunk_type = diff_chunk_type(c);
195 struct diff_atom *atom, *start_atom;
196 unsigned int atom_count;
197 int rc, ch;
198 off_t outoff = 0, *offp;
200 if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
201 start_atom = c->left_start;
202 atom_count = c->left_count;
203 } else if (chunk_type == CHUNK_PLUS) {
204 start_atom = c->right_start;
205 atom_count = c->right_count;
206 } else
207 return EINVAL;
209 /* Locate the last atom. */
210 if (atom_count == 0)
211 return EINVAL;
212 atom = &start_atom[atom_count - 1];
214 rc = get_atom_byte(&ch, atom, atom->len - 1);
215 if (rc != DIFF_RC_OK)
216 return rc;
218 if (ch != '\n') {
219 if (outinfo && outinfo->line_offsets.len > 0) {
220 unsigned int idx = outinfo->line_offsets.len - 1;
221 outoff = outinfo->line_offsets.head[idx];
223 rc = fprintf(dest, "\\ No newline at end of file\n");
224 if (rc < 0)
225 return errno;
226 if (outinfo) {
227 ARRAYLIST_ADD(offp, outinfo->line_offsets);
228 if (offp == NULL)
229 return ENOMEM;
230 outoff += rc;
231 *offp = outoff;
235 return DIFF_RC_OK;
238 static bool
239 is_function_prototype(const char *buf)
241 return isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$';
244 #define FUNCTION_CONTEXT_SIZE 55
245 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
247 int
248 diff_output_match_function_prototype(char **prototype,
249 const struct diff_result *result,
250 const struct diff_chunk_context *cc)
252 struct diff_atom *start_atom, *atom;
253 const struct diff_data *data;
254 unsigned char buf[FUNCTION_CONTEXT_SIZE];
255 char *state = NULL;
256 int rc, i;
258 *prototype = NULL;
260 if (result->left->atoms.len > 0 && cc->left.start > 0) {
261 data = result->left;
262 start_atom = &data->atoms.head[cc->left.start - 1];
263 } else if (result->right->atoms.len > 0 && cc->right.start > 0) {
264 data = result->right;
265 start_atom = &data->atoms.head[cc->right.start - 1];
266 } else
267 return DIFF_RC_OK;
269 diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
270 for (i = 0; i < atom->len && i < sizeof(buf) - 1; i++) {
271 unsigned int ch;
272 rc = get_atom_byte(&ch, atom, i);
273 if (rc)
274 return rc;
275 if (ch == '\n')
276 break;
277 buf[i] = ch;
279 buf[i] = '\0';
280 if (is_function_prototype(buf)) {
281 if (begins_with(buf, "private:")) {
282 if (!state)
283 state = " (private)";
284 } else if (begins_with(buf, "protected:")) {
285 if (!state)
286 state = " (protected)";
287 } else if (begins_with(buf, "public:")) {
288 if (!state)
289 state = " (public)";
290 } else {
291 if (state) /* don't care about truncation */
292 strlcat(buf, state, sizeof(buf));
293 *prototype = strdup(buf);
294 if (*prototype == NULL)
295 return ENOMEM;
296 return DIFF_RC_OK;
301 return DIFF_RC_OK;
304 struct diff_output_info *
305 diff_output_info_alloc(void)
307 struct diff_output_info *output_info;
308 off_t *offp;
310 output_info = malloc(sizeof(*output_info));
311 if (output_info != NULL) {
312 ARRAYLIST_INIT(output_info->line_offsets, 128);
313 ARRAYLIST_ADD(offp, output_info->line_offsets);
314 if (offp == NULL) {
315 diff_output_info_free(output_info);
316 return NULL;
318 *offp = 0;
320 return output_info;
323 void
324 diff_output_info_free(struct diff_output_info *output_info)
326 ARRAYLIST_FREE(output_info->line_offsets);
327 free(output_info);
330 const char *
331 diff_output_get_label_left(const struct diff_input_info *info)
333 if (info->flags & DIFF_INPUT_LEFT_NONEXISTENT)
334 return "/dev/null";
336 return info->left_path ? : "a";
339 const char *
340 diff_output_get_label_right(const struct diff_input_info *info)
342 if (info->flags & DIFF_INPUT_RIGHT_NONEXISTENT)
343 return "/dev/null";
345 return info->right_path ? : "b";