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 <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>
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 9879b82a 2021-07-08 stsp #define DIFF_OUTPUT_BUF_SIZE 512
59 9879b82a 2021-07-08 stsp
60 6f26cb2e 2020-09-20 stsp int
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)
64 3b0f3d61 2020-01-22 neels {
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;
68 6f26cb2e 2020-09-20 stsp int rc;
69 2c20a3ed 2020-09-22 stsp
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];
73 2c20a3ed 2020-09-22 stsp }
74 2c20a3ed 2020-09-22 stsp
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' */];
80 9879b82a 2021-07-08 stsp size_t n;
81 9879b82a 2021-07-08 stsp
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;
85 9879b82a 2021-07-08 stsp nbuf += n;
86 9879b82a 2021-07-08 stsp
87 c6eecea3 2020-07-26 stsp if (len) {
88 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
89 6f26cb2e 2020-09-20 stsp if (rc)
90 6f26cb2e 2020-09-20 stsp return rc;
91 c6eecea3 2020-07-26 stsp if (ch == '\n')
92 3b0f3d61 2020-01-22 neels len--;
93 c6eecea3 2020-07-26 stsp if (len) {
94 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
95 6f26cb2e 2020-09-20 stsp if (rc)
96 6f26cb2e 2020-09-20 stsp return rc;
97 c6eecea3 2020-07-26 stsp if (ch == '\r')
98 c6eecea3 2020-07-26 stsp len--;
99 c6eecea3 2020-07-26 stsp }
100 3b0f3d61 2020-01-22 neels }
101 3b0f3d61 2020-01-22 neels
102 3b0f3d61 2020-01-22 neels for (i = 0; i < len; i++) {
103 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, i);
104 6f26cb2e 2020-09-20 stsp if (rc)
105 6f26cb2e 2020-09-20 stsp return rc;
106 9879b82a 2021-07-08 stsp if (nbuf >= DIFF_OUTPUT_BUF_SIZE) {
107 9879b82a 2021-07-08 stsp rc = fwrite(buf, 1, nbuf, dest);
108 9879b82a 2021-07-08 stsp if (rc != nbuf)
109 9879b82a 2021-07-08 stsp return errno;
110 9879b82a 2021-07-08 stsp outlen += rc;
111 9879b82a 2021-07-08 stsp nbuf = 0;
112 9879b82a 2021-07-08 stsp }
113 9879b82a 2021-07-08 stsp buf[nbuf++] = ch;
114 3b0f3d61 2020-01-22 neels }
115 9879b82a 2021-07-08 stsp buf[nbuf++] = '\n';
116 9879b82a 2021-07-08 stsp rc = fwrite(buf, 1, nbuf, dest);
117 9879b82a 2021-07-08 stsp if (rc != nbuf)
118 2c20a3ed 2020-09-22 stsp return errno;
119 2c20a3ed 2020-09-22 stsp outlen += rc;
120 2c20a3ed 2020-09-22 stsp if (outinfo) {
121 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
122 dabc1008 2020-09-22 stsp if (offp == NULL)
123 dabc1008 2020-09-22 stsp return ENOMEM;
124 2c20a3ed 2020-09-22 stsp outoff += outlen;
125 2c20a3ed 2020-09-22 stsp *offp = outoff;
126 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
127 9343b925 2022-08-04 mark if (typep == NULL)
128 9343b925 2022-08-04 mark return ENOMEM;
129 9343b925 2022-08-04 mark *typep = *prefix == ' ' ? DIFF_LINE_CONTEXT :
130 9343b925 2022-08-04 mark *prefix == '-' ? DIFF_LINE_MINUS :
131 9343b925 2022-08-04 mark *prefix == '+' ? DIFF_LINE_PLUS : DIFF_LINE_NONE;
132 2c20a3ed 2020-09-22 stsp }
133 3b0f3d61 2020-01-22 neels }
134 6f26cb2e 2020-09-20 stsp
135 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
136 3b0f3d61 2020-01-22 neels }
137 24b5052a 2020-09-22 stsp
138 2c20a3ed 2020-09-22 stsp int
139 2c20a3ed 2020-09-22 stsp diff_output_chunk_left_version(struct diff_output_info **output_info,
140 2c20a3ed 2020-09-22 stsp FILE *dest,
141 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
142 24b5052a 2020-09-22 stsp const struct diff_result *result,
143 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
144 24b5052a 2020-09-22 stsp {
145 8cba9b5e 2020-09-22 stsp int rc, c_idx;
146 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
147 24b5052a 2020-09-22 stsp
148 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->left))
149 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
150 24b5052a 2020-09-22 stsp
151 2c20a3ed 2020-09-22 stsp if (output_info) {
152 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
153 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
154 2c20a3ed 2020-09-22 stsp return ENOMEM;
155 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
156 2c20a3ed 2020-09-22 stsp }
157 2c20a3ed 2020-09-22 stsp
158 24b5052a 2020-09-22 stsp /* Write out all chunks on the left side. */
159 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
160 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
161 24b5052a 2020-09-22 stsp
162 fde86f3d 2020-10-07 stsp if (c->left_count) {
163 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "",
164 8cba9b5e 2020-09-22 stsp c->left_start, c->left_count);
165 8cba9b5e 2020-09-22 stsp if (rc)
166 8cba9b5e 2020-09-22 stsp return rc;
167 fde86f3d 2020-10-07 stsp }
168 24b5052a 2020-09-22 stsp }
169 2c20a3ed 2020-09-22 stsp
170 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
171 24b5052a 2020-09-22 stsp }
172 24b5052a 2020-09-22 stsp
173 2c20a3ed 2020-09-22 stsp int
174 2c20a3ed 2020-09-22 stsp diff_output_chunk_right_version(struct diff_output_info **output_info,
175 2c20a3ed 2020-09-22 stsp FILE *dest,
176 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
177 24b5052a 2020-09-22 stsp const struct diff_result *result,
178 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
179 24b5052a 2020-09-22 stsp {
180 8cba9b5e 2020-09-22 stsp int rc, c_idx;
181 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
182 24b5052a 2020-09-22 stsp
183 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->right))
184 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
185 24b5052a 2020-09-22 stsp
186 2c20a3ed 2020-09-22 stsp if (output_info) {
187 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
188 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
189 2c20a3ed 2020-09-22 stsp return ENOMEM;
190 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
191 2c20a3ed 2020-09-22 stsp }
192 2c20a3ed 2020-09-22 stsp
193 24b5052a 2020-09-22 stsp /* Write out all chunks on the right side. */
194 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
195 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
196 24b5052a 2020-09-22 stsp
197 8cba9b5e 2020-09-22 stsp if (c->right_count) {
198 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "", c->right_start,
199 24b5052a 2020-09-22 stsp c->right_count);
200 8cba9b5e 2020-09-22 stsp if (rc)
201 8cba9b5e 2020-09-22 stsp return rc;
202 8cba9b5e 2020-09-22 stsp }
203 24b5052a 2020-09-22 stsp }
204 2c20a3ed 2020-09-22 stsp
205 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
206 24b5052a 2020-09-22 stsp }
207 24b5052a 2020-09-22 stsp
208 7021523c 2020-10-16 stsp int
209 7021523c 2020-10-16 stsp diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
210 7021523c 2020-10-16 stsp const struct diff_chunk *c)
211 7021523c 2020-10-16 stsp {
212 7021523c 2020-10-16 stsp enum diff_chunk_type chunk_type = diff_chunk_type(c);
213 7021523c 2020-10-16 stsp struct diff_atom *atom, *start_atom;
214 7021523c 2020-10-16 stsp unsigned int atom_count;
215 7021523c 2020-10-16 stsp int rc, ch;
216 7021523c 2020-10-16 stsp off_t outoff = 0, *offp;
217 9343b925 2022-08-04 mark uint8_t *typep;
218 7021523c 2020-10-16 stsp
219 9343b925 2022-08-04 mark
220 7021523c 2020-10-16 stsp if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
221 7021523c 2020-10-16 stsp start_atom = c->left_start;
222 7021523c 2020-10-16 stsp atom_count = c->left_count;
223 7021523c 2020-10-16 stsp } else if (chunk_type == CHUNK_PLUS) {
224 7021523c 2020-10-16 stsp start_atom = c->right_start;
225 7021523c 2020-10-16 stsp atom_count = c->right_count;
226 7021523c 2020-10-16 stsp } else
227 7021523c 2020-10-16 stsp return EINVAL;
228 7021523c 2020-10-16 stsp
229 7021523c 2020-10-16 stsp /* Locate the last atom. */
230 1b3c539b 2020-10-16 stsp if (atom_count == 0)
231 1b3c539b 2020-10-16 stsp return EINVAL;
232 1b3c539b 2020-10-16 stsp atom = &start_atom[atom_count - 1];
233 7021523c 2020-10-16 stsp
234 7021523c 2020-10-16 stsp rc = get_atom_byte(&ch, atom, atom->len - 1);
235 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
236 7021523c 2020-10-16 stsp return rc;
237 7021523c 2020-10-16 stsp
238 7021523c 2020-10-16 stsp if (ch != '\n') {
239 7021523c 2020-10-16 stsp if (outinfo && outinfo->line_offsets.len > 0) {
240 7021523c 2020-10-16 stsp unsigned int idx = outinfo->line_offsets.len - 1;
241 7021523c 2020-10-16 stsp outoff = outinfo->line_offsets.head[idx];
242 7021523c 2020-10-16 stsp }
243 7021523c 2020-10-16 stsp rc = fprintf(dest, "\\ No newline at end of file\n");
244 7021523c 2020-10-16 stsp if (rc < 0)
245 7021523c 2020-10-16 stsp return errno;
246 7021523c 2020-10-16 stsp if (outinfo) {
247 7021523c 2020-10-16 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
248 7021523c 2020-10-16 stsp if (offp == NULL)
249 7021523c 2020-10-16 stsp return ENOMEM;
250 7021523c 2020-10-16 stsp outoff += rc;
251 7021523c 2020-10-16 stsp *offp = outoff;
252 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, outinfo->line_types);
253 9343b925 2022-08-04 mark if (typep == NULL)
254 9343b925 2022-08-04 mark return ENOMEM;
255 9343b925 2022-08-04 mark *typep = DIFF_LINE_NONE;
256 7021523c 2020-10-16 stsp }
257 7021523c 2020-10-16 stsp }
258 7021523c 2020-10-16 stsp
259 7021523c 2020-10-16 stsp return DIFF_RC_OK;
260 7021523c 2020-10-16 stsp }
261 7021523c 2020-10-16 stsp
262 13e2caa3 2020-10-17 stsp static bool
263 b3fd1fa2 2020-12-10 stsp is_function_prototype(unsigned char ch)
264 13e2caa3 2020-10-17 stsp {
265 b3fd1fa2 2020-12-10 stsp return (isalpha(ch) || ch == '_' || ch == '$');
266 13e2caa3 2020-10-17 stsp }
267 13e2caa3 2020-10-17 stsp
268 b756ffd2 2020-10-22 stsp #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
269 13e2caa3 2020-10-17 stsp
270 13e2caa3 2020-10-17 stsp int
271 b3fd1fa2 2020-12-10 stsp diff_output_match_function_prototype(char *prototype, size_t prototype_size,
272 b3fd1fa2 2020-12-10 stsp int *last_prototype_idx, const struct diff_result *result,
273 8993f425 2022-09-23 mark const struct diff_chunk_context *cc, unsigned int ncontext)
274 13e2caa3 2020-10-17 stsp {
275 13e2caa3 2020-10-17 stsp struct diff_atom *start_atom, *atom;
276 13e2caa3 2020-10-17 stsp const struct diff_data *data;
277 b3fd1fa2 2020-12-10 stsp unsigned char buf[DIFF_FUNCTION_CONTEXT_SIZE];
278 e450035c 2022-08-04 mark const char *state = NULL;
279 8993f425 2022-09-23 mark int rc, i, ch, idx;
280 8993f425 2022-09-23 mark
281 8993f425 2022-09-23 mark idx = MIN(cc->left.start + (ncontext ? ncontext : 0), cc->left.end - 1);
282 13e2caa3 2020-10-17 stsp
283 c16dde50 2020-10-22 stsp if (result->left->atoms.len > 0 && cc->left.start > 0) {
284 c16dde50 2020-10-22 stsp data = result->left;
285 8993f425 2022-09-23 mark start_atom = &data->atoms.head[idx];
286 13e2caa3 2020-10-17 stsp } else
287 13e2caa3 2020-10-17 stsp return DIFF_RC_OK;
288 13e2caa3 2020-10-17 stsp
289 13e2caa3 2020-10-17 stsp diff_data_foreach_atom_backwards_from(start_atom, atom, data) {
290 b3fd1fa2 2020-12-10 stsp int atom_idx = diff_atom_root_idx(data, atom);
291 b3fd1fa2 2020-12-10 stsp if (atom_idx < *last_prototype_idx)
292 b3fd1fa2 2020-12-10 stsp break;
293 b3fd1fa2 2020-12-10 stsp rc = get_atom_byte(&ch, atom, 0);
294 b3fd1fa2 2020-12-10 stsp if (rc)
295 b3fd1fa2 2020-12-10 stsp return rc;
296 b3fd1fa2 2020-12-10 stsp buf[0] = (unsigned char)ch;
297 b3fd1fa2 2020-12-10 stsp if (!is_function_prototype(buf[0]))
298 b3fd1fa2 2020-12-10 stsp continue;
299 b3fd1fa2 2020-12-10 stsp for (i = 1; i < atom->len && i < sizeof(buf) - 1; i++) {
300 13e2caa3 2020-10-17 stsp rc = get_atom_byte(&ch, atom, i);
301 13e2caa3 2020-10-17 stsp if (rc)
302 13e2caa3 2020-10-17 stsp return rc;
303 13e2caa3 2020-10-17 stsp if (ch == '\n')
304 13e2caa3 2020-10-17 stsp break;
305 b3fd1fa2 2020-12-10 stsp buf[i] = (unsigned char)ch;
306 13e2caa3 2020-10-17 stsp }
307 13e2caa3 2020-10-17 stsp buf[i] = '\0';
308 b3fd1fa2 2020-12-10 stsp if (begins_with(buf, "private:")) {
309 b3fd1fa2 2020-12-10 stsp if (!state)
310 b3fd1fa2 2020-12-10 stsp state = " (private)";
311 b3fd1fa2 2020-12-10 stsp } else if (begins_with(buf, "protected:")) {
312 b3fd1fa2 2020-12-10 stsp if (!state)
313 b3fd1fa2 2020-12-10 stsp state = " (protected)";
314 b3fd1fa2 2020-12-10 stsp } else if (begins_with(buf, "public:")) {
315 b3fd1fa2 2020-12-10 stsp if (!state)
316 b3fd1fa2 2020-12-10 stsp state = " (public)";
317 b3fd1fa2 2020-12-10 stsp } else {
318 b3fd1fa2 2020-12-10 stsp if (state) /* don't care about truncation */
319 b3fd1fa2 2020-12-10 stsp strlcat(buf, state, sizeof(buf));
320 b3fd1fa2 2020-12-10 stsp strlcpy(prototype, buf, prototype_size);
321 b3fd1fa2 2020-12-10 stsp break;
322 13e2caa3 2020-10-17 stsp }
323 13e2caa3 2020-10-17 stsp }
324 13e2caa3 2020-10-17 stsp
325 b3fd1fa2 2020-12-10 stsp *last_prototype_idx = diff_atom_root_idx(data, start_atom);
326 13e2caa3 2020-10-17 stsp return DIFF_RC_OK;
327 13e2caa3 2020-10-17 stsp }
328 13e2caa3 2020-10-17 stsp
329 2c20a3ed 2020-09-22 stsp struct diff_output_info *
330 2c20a3ed 2020-09-22 stsp diff_output_info_alloc(void)
331 2c20a3ed 2020-09-22 stsp {
332 2c20a3ed 2020-09-22 stsp struct diff_output_info *output_info;
333 2c20a3ed 2020-09-22 stsp off_t *offp;
334 9343b925 2022-08-04 mark uint8_t *typep;
335 2c20a3ed 2020-09-22 stsp
336 2c20a3ed 2020-09-22 stsp output_info = malloc(sizeof(*output_info));
337 2c20a3ed 2020-09-22 stsp if (output_info != NULL) {
338 2c20a3ed 2020-09-22 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
339 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
340 dabc1008 2020-09-22 stsp if (offp == NULL) {
341 dabc1008 2020-09-22 stsp diff_output_info_free(output_info);
342 dabc1008 2020-09-22 stsp return NULL;
343 dabc1008 2020-09-22 stsp }
344 2c20a3ed 2020-09-22 stsp *offp = 0;
345 9343b925 2022-08-04 mark ARRAYLIST_INIT(output_info->line_types, 128);
346 9343b925 2022-08-04 mark ARRAYLIST_ADD(typep, output_info->line_types);
347 9343b925 2022-08-04 mark if (typep == NULL) {
348 9343b925 2022-08-04 mark diff_output_info_free(output_info);
349 9343b925 2022-08-04 mark return NULL;
350 9343b925 2022-08-04 mark }
351 9343b925 2022-08-04 mark *typep = DIFF_LINE_NONE;
352 2c20a3ed 2020-09-22 stsp }
353 2c20a3ed 2020-09-22 stsp return output_info;
354 2c20a3ed 2020-09-22 stsp }
355 2c20a3ed 2020-09-22 stsp
356 2c20a3ed 2020-09-22 stsp void
357 2c20a3ed 2020-09-22 stsp diff_output_info_free(struct diff_output_info *output_info)
358 2c20a3ed 2020-09-22 stsp {
359 2c20a3ed 2020-09-22 stsp ARRAYLIST_FREE(output_info->line_offsets);
360 9343b925 2022-08-04 mark ARRAYLIST_FREE(output_info->line_types);
361 2c20a3ed 2020-09-22 stsp free(output_info);
362 e4c510c1 2020-11-21 stsp }
363 e4c510c1 2020-11-21 stsp
364 e4c510c1 2020-11-21 stsp const char *
365 e4c510c1 2020-11-21 stsp diff_output_get_label_left(const struct diff_input_info *info)
366 e4c510c1 2020-11-21 stsp {
367 e4c510c1 2020-11-21 stsp if (info->flags & DIFF_INPUT_LEFT_NONEXISTENT)
368 e4c510c1 2020-11-21 stsp return "/dev/null";
369 e4c510c1 2020-11-21 stsp
370 35eae7fa 2022-08-31 mark return info->left_path ? info->left_path : "a";
371 e4c510c1 2020-11-21 stsp }
372 e4c510c1 2020-11-21 stsp
373 e4c510c1 2020-11-21 stsp const char *
374 e4c510c1 2020-11-21 stsp diff_output_get_label_right(const struct diff_input_info *info)
375 e4c510c1 2020-11-21 stsp {
376 e4c510c1 2020-11-21 stsp if (info->flags & DIFF_INPUT_RIGHT_NONEXISTENT)
377 e4c510c1 2020-11-21 stsp return "/dev/null";
378 e4c510c1 2020-11-21 stsp
379 35eae7fa 2022-08-31 mark return info->right_path ? info->right_path : "b";
380 2c20a3ed 2020-09-22 stsp }