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 #define DIFF_OUTPUT_BUF_SIZE 512
60 int
61 diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
62 const char *prefix, struct diff_atom *start_atom,
63 unsigned int count)
64 {
65 struct diff_atom *atom;
66 off_t outoff = 0, *offp;
67 uint8_t *typep;
68 int rc;
70 if (outinfo && outinfo->line_offsets.len > 0) {
71 unsigned int idx = outinfo->line_offsets.len - 1;
72 outoff = outinfo->line_offsets.head[idx];
73 }
75 foreach_diff_atom(atom, start_atom, count) {
76 off_t outlen = 0;
77 int i, ch, nbuf = 0;
78 unsigned int len = atom->len;
79 unsigned char buf[DIFF_OUTPUT_BUF_SIZE + 1 /* '\n' */];
80 size_t n;
82 n = strlcpy(buf, prefix, sizeof(buf));
83 if (n >= DIFF_OUTPUT_BUF_SIZE) /* leave room for '\n' */
84 return ENOBUFS;
85 nbuf += n;
87 if (len) {
88 rc = get_atom_byte(&ch, atom, len - 1);
89 if (rc)
90 return rc;
91 if (ch == '\n')
92 len--;
93 if (len) {
94 rc = get_atom_byte(&ch, atom, len - 1);
95 if (rc)
96 return rc;
97 if (ch == '\r')
98 len--;
99 }
102 for (i = 0; i < len; i++) {
103 rc = get_atom_byte(&ch, atom, i);
104 if (rc)
105 return rc;
106 if (nbuf >= DIFF_OUTPUT_BUF_SIZE) {
107 rc = fwrite(buf, 1, nbuf, dest);
108 if (rc != nbuf)
109 return errno;
110 outlen += rc;
111 nbuf = 0;
113 buf[nbuf++] = ch;
115 buf[nbuf++] = '\n';
116 rc = fwrite(buf, 1, nbuf, dest);
117 if (rc != nbuf)
118 return errno;
119 outlen += rc;
120 if (outinfo) {
121 ARRAYLIST_ADD(offp, outinfo->line_offsets);
122 if (offp == NULL)
123 return ENOMEM;
124 outoff += outlen;
125 *offp = outoff;
126 ARRAYLIST_ADD(typep, outinfo->line_types);
127 if (typep == NULL)
128 return ENOMEM;
129 *typep = *prefix == ' ' ? DIFF_LINE_CONTEXT :
130 *prefix == '-' ? DIFF_LINE_MINUS :
131 *prefix == '+' ? DIFF_LINE_PLUS : DIFF_LINE_NONE;
135 return DIFF_RC_OK;
138 int
139 diff_output_chunk_left_version(struct diff_output_info **output_info,
140 FILE *dest,
141 const struct diff_input_info *info,
142 const struct diff_result *result,
143 const struct diff_chunk_context *cc)
145 int rc, c_idx;
146 struct diff_output_info *outinfo = NULL;
148 if (diff_range_empty(&cc->left))
149 return DIFF_RC_OK;
151 if (output_info) {
152 *output_info = diff_output_info_alloc();
153 if (*output_info == NULL)
154 return ENOMEM;
155 outinfo = *output_info;
158 /* Write out all chunks on the left side. */
159 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
160 const struct diff_chunk *c = &result->chunks.head[c_idx];
162 if (c->left_count) {
163 rc = diff_output_lines(outinfo, dest, "",
164 c->left_start, c->left_count);
165 if (rc)
166 return rc;
170 return DIFF_RC_OK;
173 int
174 diff_output_chunk_right_version(struct diff_output_info **output_info,
175 FILE *dest,
176 const struct diff_input_info *info,
177 const struct diff_result *result,
178 const struct diff_chunk_context *cc)
180 int rc, c_idx;
181 struct diff_output_info *outinfo = NULL;
183 if (diff_range_empty(&cc->right))
184 return DIFF_RC_OK;
186 if (output_info) {
187 *output_info = diff_output_info_alloc();
188 if (*output_info == NULL)
189 return ENOMEM;
190 outinfo = *output_info;
193 /* Write out all chunks on the right side. */
194 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
195 const struct diff_chunk *c = &result->chunks.head[c_idx];
197 if (c->right_count) {
198 rc = diff_output_lines(outinfo, dest, "", c->right_start,
199 c->right_count);
200 if (rc)
201 return rc;
205 return DIFF_RC_OK;
208 int
209 diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
210 const struct diff_chunk *c)
212 enum diff_chunk_type chunk_type = diff_chunk_type(c);
213 struct diff_atom *atom, *start_atom;
214 unsigned int atom_count;
215 int rc, ch;
216 off_t outoff = 0, *offp;
217 uint8_t *typep;
220 if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
221 start_atom = c->left_start;
222 atom_count = c->left_count;
223 } else if (chunk_type == CHUNK_PLUS) {
224 start_atom = c->right_start;
225 atom_count = c->right_count;
226 } else
227 return EINVAL;
229 /* Locate the last atom. */
230 if (atom_count == 0)
231 return EINVAL;
232 atom = &start_atom[atom_count - 1];
234 rc = get_atom_byte(&ch, atom, atom->len - 1);
235 if (rc != DIFF_RC_OK)
236 return rc;
238 if (ch != '\n') {
239 if (outinfo && outinfo->line_offsets.len > 0) {
240 unsigned int idx = outinfo->line_offsets.len - 1;
241 outoff = outinfo->line_offsets.head[idx];
243 rc = fprintf(dest, "\\ No newline at end of file\n");
244 if (rc < 0)
245 return errno;
246 if (outinfo) {
247 ARRAYLIST_ADD(offp, outinfo->line_offsets);
248 if (offp == NULL)
249 return ENOMEM;
250 outoff += rc;
251 *offp = outoff;
252 ARRAYLIST_ADD(typep, outinfo->line_types);
253 if (typep == NULL)
254 return ENOMEM;
255 *typep = DIFF_LINE_NONE;
259 return DIFF_RC_OK;
262 static bool
263 is_function_prototype(unsigned char ch)
265 return (isalpha((unsigned char)ch) || ch == '_' || ch == '$');
268 #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
270 int
271 diff_output_match_function_prototype(char *prototype, size_t prototype_size,
272 int *last_prototype_idx, const struct diff_result *result,
273 const struct diff_chunk_context *cc, unsigned int ncontext)
275 struct diff_atom *start_atom, *atom;
276 const struct diff_data *data;
277 unsigned char buf[DIFF_FUNCTION_CONTEXT_SIZE];
278 const char *state = NULL;
279 int rc, i, ch, idx;
281 idx = MIN(cc->left.start + (ncontext ? ncontext : 0), cc->left.end - 1);
283 if (result->left->atoms.len > 0 && cc->left.start > 0) {
284 data = result->left;
285 start_atom = &data->atoms.head[idx];
286 } else
287 return DIFF_RC_OK;
289 diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
290 int atom_idx = diff_atom_root_idx(data, atom);
291 if (atom_idx < *last_prototype_idx)
292 break;
293 rc = get_atom_byte(&ch, atom, 0);
294 if (rc)
295 return rc;
296 buf[0] = (unsigned char)ch;
297 if (!is_function_prototype(buf[0]))
298 continue;
299 for (i = 1; i < atom->len && i < sizeof(buf) - 1; i++) {
300 rc = get_atom_byte(&ch, atom, i);
301 if (rc)
302 return rc;
303 if (ch == '\n')
304 break;
305 buf[i] = (unsigned char)ch;
307 buf[i] = '\0';
308 if (begins_with(buf, "private:")) {
309 if (!state)
310 state = " (private)";
311 } else if (begins_with(buf, "protected:")) {
312 if (!state)
313 state = " (protected)";
314 } else if (begins_with(buf, "public:")) {
315 if (!state)
316 state = " (public)";
317 } else {
318 if (state) /* don't care about truncation */
319 strlcat(buf, state, sizeof(buf));
320 strlcpy(prototype, buf, prototype_size);
321 break;
325 *last_prototype_idx = diff_atom_root_idx(data, start_atom);
326 return DIFF_RC_OK;
329 struct diff_output_info *
330 diff_output_info_alloc(void)
332 struct diff_output_info *output_info;
333 off_t *offp;
334 uint8_t *typep;
336 output_info = malloc(sizeof(*output_info));
337 if (output_info != NULL) {
338 ARRAYLIST_INIT(output_info->line_offsets, 128);
339 ARRAYLIST_ADD(offp, output_info->line_offsets);
340 if (offp == NULL) {
341 diff_output_info_free(output_info);
342 return NULL;
344 *offp = 0;
345 ARRAYLIST_INIT(output_info->line_types, 128);
346 ARRAYLIST_ADD(typep, output_info->line_types);
347 if (typep == NULL) {
348 diff_output_info_free(output_info);
349 return NULL;
351 *typep = DIFF_LINE_NONE;
353 return output_info;
356 void
357 diff_output_info_free(struct diff_output_info *output_info)
359 ARRAYLIST_FREE(output_info->line_offsets);
360 ARRAYLIST_FREE(output_info->line_types);
361 free(output_info);
364 const char *
365 diff_output_get_label_left(const struct diff_input_info *info)
367 if (info->flags & DIFF_INPUT_LEFT_NONEXISTENT)
368 return "/dev/null";
370 return info->left_path ? info->left_path : "a";
373 const char *
374 diff_output_get_label_right(const struct diff_input_info *info)
376 if (info->flags & DIFF_INPUT_RIGHT_NONEXISTENT)
377 return "/dev/null";
379 return info->right_path ? info->right_path : "b";