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