Blame


1 3b0f3d61 2020-01-22 neels /* Common parts for printing diff output */
2 3b0f3d61 2020-01-22 neels /*
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 3b0f3d61 2020-01-22 neels *
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.
8 3b0f3d61 2020-01-22 neels *
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.
16 3b0f3d61 2020-01-22 neels */
17 3b0f3d61 2020-01-22 neels
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 <inttypes.h>
21 e10a628a 2020-09-16 stsp #include <stdbool.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>
26 c6eecea3 2020-07-26 stsp
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>
30 3b0f3d61 2020-01-22 neels
31 85ab4559 2020-09-22 stsp #include "diff_internal.h"
32 85ab4559 2020-09-22 stsp
33 6f26cb2e 2020-09-20 stsp static int
34 6f26cb2e 2020-09-20 stsp get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
35 c6eecea3 2020-07-26 stsp {
36 c6eecea3 2020-07-26 stsp off_t cur;
37 c6eecea3 2020-07-26 stsp
38 6f26cb2e 2020-09-20 stsp if (atom->at != NULL) {
39 6f26cb2e 2020-09-20 stsp *ch = atom->at[off];
40 6f26cb2e 2020-09-20 stsp return 0;
41 6f26cb2e 2020-09-20 stsp }
42 c6eecea3 2020-07-26 stsp
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;
46 c6eecea3 2020-07-26 stsp
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;
50 c6eecea3 2020-07-26 stsp
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;
54 c6eecea3 2020-07-26 stsp
55 6f26cb2e 2020-09-20 stsp return 0;
56 c6eecea3 2020-07-26 stsp }
57 c6eecea3 2020-07-26 stsp
58 6f26cb2e 2020-09-20 stsp int
59 2c20a3ed 2020-09-22 stsp diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
60 2c20a3ed 2020-09-22 stsp const char *prefix, struct diff_atom *start_atom,
61 0d27172a 2020-05-06 neels unsigned int count)
62 3b0f3d61 2020-01-22 neels {
63 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
64 2c20a3ed 2020-09-22 stsp off_t outoff = 0, *offp;
65 6f26cb2e 2020-09-20 stsp int rc;
66 2c20a3ed 2020-09-22 stsp
67 2c20a3ed 2020-09-22 stsp if (outinfo && outinfo->line_offsets.len > 0) {
68 2c20a3ed 2020-09-22 stsp unsigned int idx = outinfo->line_offsets.len - 1;
69 2c20a3ed 2020-09-22 stsp outoff = outinfo->line_offsets.head[idx];
70 2c20a3ed 2020-09-22 stsp }
71 2c20a3ed 2020-09-22 stsp
72 3b0f3d61 2020-01-22 neels foreach_diff_atom(atom, start_atom, count) {
73 2c20a3ed 2020-09-22 stsp off_t outlen = 0;
74 6f26cb2e 2020-09-20 stsp int i, ch;
75 3b0f3d61 2020-01-22 neels unsigned int len = atom->len;
76 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "%s", prefix);
77 2c20a3ed 2020-09-22 stsp if (rc < 0)
78 2c20a3ed 2020-09-22 stsp return errno;
79 2c20a3ed 2020-09-22 stsp outlen += rc;
80 c6eecea3 2020-07-26 stsp if (len) {
81 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
82 6f26cb2e 2020-09-20 stsp if (rc)
83 6f26cb2e 2020-09-20 stsp return rc;
84 c6eecea3 2020-07-26 stsp if (ch == '\n')
85 3b0f3d61 2020-01-22 neels len--;
86 c6eecea3 2020-07-26 stsp if (len) {
87 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
88 6f26cb2e 2020-09-20 stsp if (rc)
89 6f26cb2e 2020-09-20 stsp return rc;
90 c6eecea3 2020-07-26 stsp if (ch == '\r')
91 c6eecea3 2020-07-26 stsp len--;
92 c6eecea3 2020-07-26 stsp }
93 3b0f3d61 2020-01-22 neels }
94 3b0f3d61 2020-01-22 neels
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);
97 6f26cb2e 2020-09-20 stsp if (rc)
98 6f26cb2e 2020-09-20 stsp return rc;
99 5ed6e4d2 2020-10-17 stsp rc = fprintf(dest, "%c", (unsigned char)ch);
100 2c20a3ed 2020-09-22 stsp if (rc < 0)
101 2c20a3ed 2020-09-22 stsp return errno;
102 2c20a3ed 2020-09-22 stsp outlen += rc;
103 3b0f3d61 2020-01-22 neels }
104 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "\n");
105 2c20a3ed 2020-09-22 stsp if (rc < 0)
106 2c20a3ed 2020-09-22 stsp return errno;
107 2c20a3ed 2020-09-22 stsp outlen += rc;
108 2c20a3ed 2020-09-22 stsp if (outinfo) {
109 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
110 dabc1008 2020-09-22 stsp if (offp == NULL)
111 dabc1008 2020-09-22 stsp return ENOMEM;
112 2c20a3ed 2020-09-22 stsp outoff += outlen;
113 2c20a3ed 2020-09-22 stsp *offp = outoff;
114 2c20a3ed 2020-09-22 stsp }
115 3b0f3d61 2020-01-22 neels }
116 6f26cb2e 2020-09-20 stsp
117 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
118 3b0f3d61 2020-01-22 neels }
119 24b5052a 2020-09-22 stsp
120 2c20a3ed 2020-09-22 stsp int
121 2c20a3ed 2020-09-22 stsp diff_output_chunk_left_version(struct diff_output_info **output_info,
122 2c20a3ed 2020-09-22 stsp FILE *dest,
123 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
124 24b5052a 2020-09-22 stsp const struct diff_result *result,
125 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
126 24b5052a 2020-09-22 stsp {
127 8cba9b5e 2020-09-22 stsp int rc, c_idx;
128 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
129 24b5052a 2020-09-22 stsp
130 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->left))
131 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
132 24b5052a 2020-09-22 stsp
133 2c20a3ed 2020-09-22 stsp if (output_info) {
134 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
135 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
136 2c20a3ed 2020-09-22 stsp return ENOMEM;
137 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
138 2c20a3ed 2020-09-22 stsp }
139 2c20a3ed 2020-09-22 stsp
140 24b5052a 2020-09-22 stsp /* Write out all chunks on the left side. */
141 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
142 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
143 24b5052a 2020-09-22 stsp
144 fde86f3d 2020-10-07 stsp if (c->left_count) {
145 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "",
146 8cba9b5e 2020-09-22 stsp c->left_start, c->left_count);
147 8cba9b5e 2020-09-22 stsp if (rc)
148 8cba9b5e 2020-09-22 stsp return rc;
149 fde86f3d 2020-10-07 stsp }
150 24b5052a 2020-09-22 stsp }
151 2c20a3ed 2020-09-22 stsp
152 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
153 24b5052a 2020-09-22 stsp }
154 24b5052a 2020-09-22 stsp
155 2c20a3ed 2020-09-22 stsp int
156 2c20a3ed 2020-09-22 stsp diff_output_chunk_right_version(struct diff_output_info **output_info,
157 2c20a3ed 2020-09-22 stsp FILE *dest,
158 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
159 24b5052a 2020-09-22 stsp const struct diff_result *result,
160 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
161 24b5052a 2020-09-22 stsp {
162 8cba9b5e 2020-09-22 stsp int rc, c_idx;
163 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
164 24b5052a 2020-09-22 stsp
165 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->right))
166 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
167 24b5052a 2020-09-22 stsp
168 2c20a3ed 2020-09-22 stsp if (output_info) {
169 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
170 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
171 2c20a3ed 2020-09-22 stsp return ENOMEM;
172 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
173 2c20a3ed 2020-09-22 stsp }
174 2c20a3ed 2020-09-22 stsp
175 24b5052a 2020-09-22 stsp /* Write out all chunks on the right side. */
176 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
177 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
178 24b5052a 2020-09-22 stsp
179 8cba9b5e 2020-09-22 stsp if (c->right_count) {
180 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "", c->right_start,
181 24b5052a 2020-09-22 stsp c->right_count);
182 8cba9b5e 2020-09-22 stsp if (rc)
183 8cba9b5e 2020-09-22 stsp return rc;
184 8cba9b5e 2020-09-22 stsp }
185 24b5052a 2020-09-22 stsp }
186 2c20a3ed 2020-09-22 stsp
187 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
188 24b5052a 2020-09-22 stsp }
189 24b5052a 2020-09-22 stsp
190 7021523c 2020-10-16 stsp int
191 7021523c 2020-10-16 stsp diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
192 7021523c 2020-10-16 stsp const struct diff_chunk *c)
193 7021523c 2020-10-16 stsp {
194 7021523c 2020-10-16 stsp enum diff_chunk_type chunk_type = diff_chunk_type(c);
195 7021523c 2020-10-16 stsp struct diff_atom *atom, *start_atom;
196 7021523c 2020-10-16 stsp unsigned int atom_count;
197 7021523c 2020-10-16 stsp int rc, ch;
198 7021523c 2020-10-16 stsp off_t outoff = 0, *offp;
199 7021523c 2020-10-16 stsp
200 7021523c 2020-10-16 stsp if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
201 7021523c 2020-10-16 stsp start_atom = c->left_start;
202 7021523c 2020-10-16 stsp atom_count = c->left_count;
203 7021523c 2020-10-16 stsp } else if (chunk_type == CHUNK_PLUS) {
204 7021523c 2020-10-16 stsp start_atom = c->right_start;
205 7021523c 2020-10-16 stsp atom_count = c->right_count;
206 7021523c 2020-10-16 stsp } else
207 7021523c 2020-10-16 stsp return EINVAL;
208 7021523c 2020-10-16 stsp
209 7021523c 2020-10-16 stsp /* Locate the last atom. */
210 1b3c539b 2020-10-16 stsp if (atom_count == 0)
211 1b3c539b 2020-10-16 stsp return EINVAL;
212 1b3c539b 2020-10-16 stsp atom = &start_atom[atom_count - 1];
213 7021523c 2020-10-16 stsp
214 7021523c 2020-10-16 stsp rc = get_atom_byte(&ch, atom, atom->len - 1);
215 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
216 7021523c 2020-10-16 stsp return rc;
217 7021523c 2020-10-16 stsp
218 7021523c 2020-10-16 stsp if (ch != '\n') {
219 7021523c 2020-10-16 stsp if (outinfo && outinfo->line_offsets.len > 0) {
220 7021523c 2020-10-16 stsp unsigned int idx = outinfo->line_offsets.len - 1;
221 7021523c 2020-10-16 stsp outoff = outinfo->line_offsets.head[idx];
222 7021523c 2020-10-16 stsp }
223 7021523c 2020-10-16 stsp rc = fprintf(dest, "\\ No newline at end of file\n");
224 7021523c 2020-10-16 stsp if (rc < 0)
225 7021523c 2020-10-16 stsp return errno;
226 7021523c 2020-10-16 stsp if (outinfo) {
227 7021523c 2020-10-16 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
228 7021523c 2020-10-16 stsp if (offp == NULL)
229 7021523c 2020-10-16 stsp return ENOMEM;
230 7021523c 2020-10-16 stsp outoff += rc;
231 7021523c 2020-10-16 stsp *offp = outoff;
232 7021523c 2020-10-16 stsp }
233 7021523c 2020-10-16 stsp }
234 7021523c 2020-10-16 stsp
235 7021523c 2020-10-16 stsp return DIFF_RC_OK;
236 7021523c 2020-10-16 stsp }
237 7021523c 2020-10-16 stsp
238 13e2caa3 2020-10-17 stsp static bool
239 13e2caa3 2020-10-17 stsp is_function_prototype(const char *buf)
240 13e2caa3 2020-10-17 stsp {
241 13e2caa3 2020-10-17 stsp return isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$';
242 13e2caa3 2020-10-17 stsp }
243 13e2caa3 2020-10-17 stsp
244 13e2caa3 2020-10-17 stsp #define FUNCTION_CONTEXT_SIZE 55
245 b756ffd2 2020-10-22 stsp #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
246 13e2caa3 2020-10-17 stsp
247 13e2caa3 2020-10-17 stsp int
248 13e2caa3 2020-10-17 stsp diff_output_match_function_prototype(char **prototype,
249 13e2caa3 2020-10-17 stsp const struct diff_result *result,
250 13e2caa3 2020-10-17 stsp const struct diff_chunk_context *cc)
251 13e2caa3 2020-10-17 stsp {
252 13e2caa3 2020-10-17 stsp struct diff_atom *start_atom, *atom;
253 13e2caa3 2020-10-17 stsp const struct diff_data *data;
254 13e2caa3 2020-10-17 stsp unsigned char buf[FUNCTION_CONTEXT_SIZE];
255 b756ffd2 2020-10-22 stsp char *state = NULL;
256 13e2caa3 2020-10-17 stsp int rc, i;
257 13e2caa3 2020-10-17 stsp
258 13e2caa3 2020-10-17 stsp *prototype = NULL;
259 13e2caa3 2020-10-17 stsp
260 c16dde50 2020-10-22 stsp if (result->left->atoms.len > 0 && cc->left.start > 0) {
261 c16dde50 2020-10-22 stsp data = result->left;
262 e6435d8f 2020-10-17 stsp start_atom = &data->atoms.head[cc->left.start - 1];
263 c16dde50 2020-10-22 stsp } else if (result->right->atoms.len > 0 && cc->right.start > 0) {
264 c16dde50 2020-10-22 stsp data = result->right;
265 e6435d8f 2020-10-17 stsp start_atom = &data->atoms.head[cc->right.start - 1];
266 13e2caa3 2020-10-17 stsp } else
267 13e2caa3 2020-10-17 stsp return DIFF_RC_OK;
268 13e2caa3 2020-10-17 stsp
269 13e2caa3 2020-10-17 stsp diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
270 13e2caa3 2020-10-17 stsp for (i = 0; i < atom->len && i < sizeof(buf) - 1; i++) {
271 13e2caa3 2020-10-17 stsp unsigned int ch;
272 13e2caa3 2020-10-17 stsp rc = get_atom_byte(&ch, atom, i);
273 13e2caa3 2020-10-17 stsp if (rc)
274 13e2caa3 2020-10-17 stsp return rc;
275 13e2caa3 2020-10-17 stsp if (ch == '\n')
276 13e2caa3 2020-10-17 stsp break;
277 13e2caa3 2020-10-17 stsp buf[i] = ch;
278 13e2caa3 2020-10-17 stsp }
279 13e2caa3 2020-10-17 stsp buf[i] = '\0';
280 13e2caa3 2020-10-17 stsp if (is_function_prototype(buf)) {
281 b756ffd2 2020-10-22 stsp if (begins_with(buf, "private:")) {
282 b756ffd2 2020-10-22 stsp if (!state)
283 b756ffd2 2020-10-22 stsp state = " (private)";
284 b756ffd2 2020-10-22 stsp } else if (begins_with(buf, "protected:")) {
285 b756ffd2 2020-10-22 stsp if (!state)
286 b756ffd2 2020-10-22 stsp state = " (protected)";
287 b756ffd2 2020-10-22 stsp } else if (begins_with(buf, "public:")) {
288 b756ffd2 2020-10-22 stsp if (!state)
289 b756ffd2 2020-10-22 stsp state = " (public)";
290 b756ffd2 2020-10-22 stsp } else {
291 b756ffd2 2020-10-22 stsp if (state) /* don't care about truncation */
292 b756ffd2 2020-10-22 stsp strlcat(buf, state, sizeof(buf));
293 b756ffd2 2020-10-22 stsp *prototype = strdup(buf);
294 b756ffd2 2020-10-22 stsp if (*prototype == NULL)
295 b756ffd2 2020-10-22 stsp return ENOMEM;
296 b756ffd2 2020-10-22 stsp return DIFF_RC_OK;
297 b756ffd2 2020-10-22 stsp }
298 13e2caa3 2020-10-17 stsp }
299 13e2caa3 2020-10-17 stsp }
300 13e2caa3 2020-10-17 stsp
301 13e2caa3 2020-10-17 stsp return DIFF_RC_OK;
302 13e2caa3 2020-10-17 stsp }
303 13e2caa3 2020-10-17 stsp
304 2c20a3ed 2020-09-22 stsp struct diff_output_info *
305 2c20a3ed 2020-09-22 stsp diff_output_info_alloc(void)
306 2c20a3ed 2020-09-22 stsp {
307 2c20a3ed 2020-09-22 stsp struct diff_output_info *output_info;
308 2c20a3ed 2020-09-22 stsp off_t *offp;
309 2c20a3ed 2020-09-22 stsp
310 2c20a3ed 2020-09-22 stsp output_info = malloc(sizeof(*output_info));
311 2c20a3ed 2020-09-22 stsp if (output_info != NULL) {
312 2c20a3ed 2020-09-22 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
313 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
314 dabc1008 2020-09-22 stsp if (offp == NULL) {
315 dabc1008 2020-09-22 stsp diff_output_info_free(output_info);
316 dabc1008 2020-09-22 stsp return NULL;
317 dabc1008 2020-09-22 stsp }
318 2c20a3ed 2020-09-22 stsp *offp = 0;
319 2c20a3ed 2020-09-22 stsp }
320 2c20a3ed 2020-09-22 stsp return output_info;
321 2c20a3ed 2020-09-22 stsp }
322 2c20a3ed 2020-09-22 stsp
323 2c20a3ed 2020-09-22 stsp void
324 2c20a3ed 2020-09-22 stsp diff_output_info_free(struct diff_output_info *output_info)
325 2c20a3ed 2020-09-22 stsp {
326 2c20a3ed 2020-09-22 stsp ARRAYLIST_FREE(output_info->line_offsets);
327 2c20a3ed 2020-09-22 stsp free(output_info);
328 2c20a3ed 2020-09-22 stsp }