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 6f26cb2e 2020-09-20 stsp #include <errno.h>
19 e10a628a 2020-09-16 stsp #include <inttypes.h>
20 e10a628a 2020-09-16 stsp #include <stdbool.h>
21 e10a628a 2020-09-16 stsp #include <stdio.h>
22 e10a628a 2020-09-16 stsp #include <stdlib.h>
23 c6eecea3 2020-07-26 stsp #include <unistd.h>
24 c6eecea3 2020-07-26 stsp
25 1dfba055 2020-10-07 stsp #include <arraylist.h>
26 1dfba055 2020-10-07 stsp #include <diff_main.h>
27 1dfba055 2020-10-07 stsp #include <diff_output.h>
28 3b0f3d61 2020-01-22 neels
29 85ab4559 2020-09-22 stsp #include "diff_internal.h"
30 85ab4559 2020-09-22 stsp
31 6f26cb2e 2020-09-20 stsp static int
32 6f26cb2e 2020-09-20 stsp get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
33 c6eecea3 2020-07-26 stsp {
34 c6eecea3 2020-07-26 stsp off_t cur;
35 c6eecea3 2020-07-26 stsp
36 6f26cb2e 2020-09-20 stsp if (atom->at != NULL) {
37 6f26cb2e 2020-09-20 stsp *ch = atom->at[off];
38 6f26cb2e 2020-09-20 stsp return 0;
39 6f26cb2e 2020-09-20 stsp }
40 c6eecea3 2020-07-26 stsp
41 ad5b3f85 2020-10-12 neels cur = ftello(atom->root->f);
42 c6eecea3 2020-07-26 stsp if (cur == -1)
43 6f26cb2e 2020-09-20 stsp return errno;
44 c6eecea3 2020-07-26 stsp
45 c6eecea3 2020-07-26 stsp if (cur != atom->pos + off &&
46 ad5b3f85 2020-10-12 neels fseeko(atom->root->f, atom->pos + off, SEEK_SET) == -1)
47 6f26cb2e 2020-09-20 stsp return errno;
48 c6eecea3 2020-07-26 stsp
49 ad5b3f85 2020-10-12 neels *ch = fgetc(atom->root->f);
50 ad5b3f85 2020-10-12 neels if (*ch == EOF && ferror(atom->root->f))
51 6f26cb2e 2020-09-20 stsp return errno;
52 c6eecea3 2020-07-26 stsp
53 6f26cb2e 2020-09-20 stsp return 0;
54 c6eecea3 2020-07-26 stsp }
55 c6eecea3 2020-07-26 stsp
56 6f26cb2e 2020-09-20 stsp int
57 2c20a3ed 2020-09-22 stsp diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
58 2c20a3ed 2020-09-22 stsp const char *prefix, struct diff_atom *start_atom,
59 0d27172a 2020-05-06 neels unsigned int count)
60 3b0f3d61 2020-01-22 neels {
61 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
62 2c20a3ed 2020-09-22 stsp off_t outoff = 0, *offp;
63 6f26cb2e 2020-09-20 stsp int rc;
64 2c20a3ed 2020-09-22 stsp
65 2c20a3ed 2020-09-22 stsp if (outinfo && outinfo->line_offsets.len > 0) {
66 2c20a3ed 2020-09-22 stsp unsigned int idx = outinfo->line_offsets.len - 1;
67 2c20a3ed 2020-09-22 stsp outoff = outinfo->line_offsets.head[idx];
68 2c20a3ed 2020-09-22 stsp }
69 2c20a3ed 2020-09-22 stsp
70 3b0f3d61 2020-01-22 neels foreach_diff_atom(atom, start_atom, count) {
71 2c20a3ed 2020-09-22 stsp off_t outlen = 0;
72 6f26cb2e 2020-09-20 stsp int i, ch;
73 3b0f3d61 2020-01-22 neels unsigned int len = atom->len;
74 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "%s", prefix);
75 2c20a3ed 2020-09-22 stsp if (rc < 0)
76 2c20a3ed 2020-09-22 stsp return errno;
77 2c20a3ed 2020-09-22 stsp outlen += rc;
78 c6eecea3 2020-07-26 stsp if (len) {
79 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
80 6f26cb2e 2020-09-20 stsp if (rc)
81 6f26cb2e 2020-09-20 stsp return rc;
82 c6eecea3 2020-07-26 stsp if (ch == '\n')
83 3b0f3d61 2020-01-22 neels len--;
84 c6eecea3 2020-07-26 stsp if (len) {
85 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
86 6f26cb2e 2020-09-20 stsp if (rc)
87 6f26cb2e 2020-09-20 stsp return rc;
88 c6eecea3 2020-07-26 stsp if (ch == '\r')
89 c6eecea3 2020-07-26 stsp len--;
90 c6eecea3 2020-07-26 stsp }
91 3b0f3d61 2020-01-22 neels }
92 3b0f3d61 2020-01-22 neels
93 3b0f3d61 2020-01-22 neels for (i = 0; i < len; i++) {
94 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, i);
95 6f26cb2e 2020-09-20 stsp if (rc)
96 6f26cb2e 2020-09-20 stsp return rc;
97 5ed6e4d2 2020-10-17 stsp rc = fprintf(dest, "%c", (unsigned char)ch);
98 2c20a3ed 2020-09-22 stsp if (rc < 0)
99 2c20a3ed 2020-09-22 stsp return errno;
100 2c20a3ed 2020-09-22 stsp outlen += rc;
101 3b0f3d61 2020-01-22 neels }
102 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "\n");
103 2c20a3ed 2020-09-22 stsp if (rc < 0)
104 2c20a3ed 2020-09-22 stsp return errno;
105 2c20a3ed 2020-09-22 stsp outlen += rc;
106 2c20a3ed 2020-09-22 stsp if (outinfo) {
107 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
108 dabc1008 2020-09-22 stsp if (offp == NULL)
109 dabc1008 2020-09-22 stsp return ENOMEM;
110 2c20a3ed 2020-09-22 stsp outoff += outlen;
111 2c20a3ed 2020-09-22 stsp *offp = outoff;
112 2c20a3ed 2020-09-22 stsp }
113 3b0f3d61 2020-01-22 neels }
114 6f26cb2e 2020-09-20 stsp
115 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
116 3b0f3d61 2020-01-22 neels }
117 24b5052a 2020-09-22 stsp
118 2c20a3ed 2020-09-22 stsp int
119 2c20a3ed 2020-09-22 stsp diff_output_chunk_left_version(struct diff_output_info **output_info,
120 2c20a3ed 2020-09-22 stsp FILE *dest,
121 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
122 24b5052a 2020-09-22 stsp const struct diff_result *result,
123 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
124 24b5052a 2020-09-22 stsp {
125 8cba9b5e 2020-09-22 stsp int rc, c_idx;
126 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
127 24b5052a 2020-09-22 stsp
128 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->left))
129 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
130 24b5052a 2020-09-22 stsp
131 2c20a3ed 2020-09-22 stsp if (output_info) {
132 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
133 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
134 2c20a3ed 2020-09-22 stsp return ENOMEM;
135 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
136 2c20a3ed 2020-09-22 stsp }
137 2c20a3ed 2020-09-22 stsp
138 24b5052a 2020-09-22 stsp /* Write out all chunks on the left side. */
139 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
140 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
141 24b5052a 2020-09-22 stsp
142 fde86f3d 2020-10-07 stsp if (c->left_count) {
143 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "",
144 8cba9b5e 2020-09-22 stsp c->left_start, c->left_count);
145 8cba9b5e 2020-09-22 stsp if (rc)
146 8cba9b5e 2020-09-22 stsp return rc;
147 fde86f3d 2020-10-07 stsp }
148 24b5052a 2020-09-22 stsp }
149 2c20a3ed 2020-09-22 stsp
150 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
151 24b5052a 2020-09-22 stsp }
152 24b5052a 2020-09-22 stsp
153 2c20a3ed 2020-09-22 stsp int
154 2c20a3ed 2020-09-22 stsp diff_output_chunk_right_version(struct diff_output_info **output_info,
155 2c20a3ed 2020-09-22 stsp FILE *dest,
156 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
157 24b5052a 2020-09-22 stsp const struct diff_result *result,
158 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
159 24b5052a 2020-09-22 stsp {
160 8cba9b5e 2020-09-22 stsp int rc, c_idx;
161 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
162 24b5052a 2020-09-22 stsp
163 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->right))
164 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
165 24b5052a 2020-09-22 stsp
166 2c20a3ed 2020-09-22 stsp if (output_info) {
167 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
168 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
169 2c20a3ed 2020-09-22 stsp return ENOMEM;
170 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
171 2c20a3ed 2020-09-22 stsp }
172 2c20a3ed 2020-09-22 stsp
173 24b5052a 2020-09-22 stsp /* Write out all chunks on the right side. */
174 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
175 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
176 24b5052a 2020-09-22 stsp
177 8cba9b5e 2020-09-22 stsp if (c->right_count) {
178 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "", c->right_start,
179 24b5052a 2020-09-22 stsp c->right_count);
180 8cba9b5e 2020-09-22 stsp if (rc)
181 8cba9b5e 2020-09-22 stsp return rc;
182 8cba9b5e 2020-09-22 stsp }
183 24b5052a 2020-09-22 stsp }
184 2c20a3ed 2020-09-22 stsp
185 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
186 24b5052a 2020-09-22 stsp }
187 24b5052a 2020-09-22 stsp
188 7021523c 2020-10-16 stsp int
189 7021523c 2020-10-16 stsp diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
190 7021523c 2020-10-16 stsp const struct diff_chunk *c)
191 7021523c 2020-10-16 stsp {
192 7021523c 2020-10-16 stsp enum diff_chunk_type chunk_type = diff_chunk_type(c);
193 7021523c 2020-10-16 stsp struct diff_atom *atom, *start_atom;
194 7021523c 2020-10-16 stsp unsigned int atom_count;
195 7021523c 2020-10-16 stsp int rc, ch;
196 7021523c 2020-10-16 stsp off_t outoff = 0, *offp;
197 7021523c 2020-10-16 stsp
198 7021523c 2020-10-16 stsp if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
199 7021523c 2020-10-16 stsp start_atom = c->left_start;
200 7021523c 2020-10-16 stsp atom_count = c->left_count;
201 7021523c 2020-10-16 stsp } else if (chunk_type == CHUNK_PLUS) {
202 7021523c 2020-10-16 stsp start_atom = c->right_start;
203 7021523c 2020-10-16 stsp atom_count = c->right_count;
204 7021523c 2020-10-16 stsp } else
205 7021523c 2020-10-16 stsp return EINVAL;
206 7021523c 2020-10-16 stsp
207 7021523c 2020-10-16 stsp /* Locate the last atom. */
208 1b3c539b 2020-10-16 stsp if (atom_count == 0)
209 1b3c539b 2020-10-16 stsp return EINVAL;
210 1b3c539b 2020-10-16 stsp atom = &start_atom[atom_count - 1];
211 7021523c 2020-10-16 stsp
212 7021523c 2020-10-16 stsp rc = get_atom_byte(&ch, atom, atom->len - 1);
213 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
214 7021523c 2020-10-16 stsp return rc;
215 7021523c 2020-10-16 stsp
216 7021523c 2020-10-16 stsp if (ch != '\n') {
217 7021523c 2020-10-16 stsp if (outinfo && outinfo->line_offsets.len > 0) {
218 7021523c 2020-10-16 stsp unsigned int idx = outinfo->line_offsets.len - 1;
219 7021523c 2020-10-16 stsp outoff = outinfo->line_offsets.head[idx];
220 7021523c 2020-10-16 stsp }
221 7021523c 2020-10-16 stsp rc = fprintf(dest, "\\ No newline at end of file\n");
222 7021523c 2020-10-16 stsp if (rc < 0)
223 7021523c 2020-10-16 stsp return errno;
224 7021523c 2020-10-16 stsp if (outinfo) {
225 7021523c 2020-10-16 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
226 7021523c 2020-10-16 stsp if (offp == NULL)
227 7021523c 2020-10-16 stsp return ENOMEM;
228 7021523c 2020-10-16 stsp outoff += rc;
229 7021523c 2020-10-16 stsp *offp = outoff;
230 7021523c 2020-10-16 stsp }
231 7021523c 2020-10-16 stsp }
232 7021523c 2020-10-16 stsp
233 7021523c 2020-10-16 stsp return DIFF_RC_OK;
234 7021523c 2020-10-16 stsp }
235 7021523c 2020-10-16 stsp
236 2c20a3ed 2020-09-22 stsp struct diff_output_info *
237 2c20a3ed 2020-09-22 stsp diff_output_info_alloc(void)
238 2c20a3ed 2020-09-22 stsp {
239 2c20a3ed 2020-09-22 stsp struct diff_output_info *output_info;
240 2c20a3ed 2020-09-22 stsp off_t *offp;
241 2c20a3ed 2020-09-22 stsp
242 2c20a3ed 2020-09-22 stsp output_info = malloc(sizeof(*output_info));
243 2c20a3ed 2020-09-22 stsp if (output_info != NULL) {
244 2c20a3ed 2020-09-22 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
245 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
246 dabc1008 2020-09-22 stsp if (offp == NULL) {
247 dabc1008 2020-09-22 stsp diff_output_info_free(output_info);
248 dabc1008 2020-09-22 stsp return NULL;
249 dabc1008 2020-09-22 stsp }
250 2c20a3ed 2020-09-22 stsp *offp = 0;
251 2c20a3ed 2020-09-22 stsp }
252 2c20a3ed 2020-09-22 stsp return output_info;
253 2c20a3ed 2020-09-22 stsp }
254 2c20a3ed 2020-09-22 stsp
255 2c20a3ed 2020-09-22 stsp void
256 2c20a3ed 2020-09-22 stsp diff_output_info_free(struct diff_output_info *output_info)
257 2c20a3ed 2020-09-22 stsp {
258 2c20a3ed 2020-09-22 stsp ARRAYLIST_FREE(output_info->line_offsets);
259 2c20a3ed 2020-09-22 stsp free(output_info);
260 2c20a3ed 2020-09-22 stsp }