1 3b0f3d61 2020-01-22 neels /* Common parts for printing diff output */
3 3b0f3d61 2020-01-22 neels * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
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>
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>
29 85ab4559 2020-09-22 stsp #include "diff_internal.h"
32 6f26cb2e 2020-09-20 stsp get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
36 6f26cb2e 2020-09-20 stsp if (atom->at != NULL) {
37 6f26cb2e 2020-09-20 stsp *ch = atom->at[off];
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;
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;
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;
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)
61 3b0f3d61 2020-01-22 neels struct diff_atom *atom;
62 2c20a3ed 2020-09-22 stsp off_t outoff = 0, *offp;
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];
70 3b0f3d61 2020-01-22 neels foreach_diff_atom(atom, start_atom, count) {
71 2c20a3ed 2020-09-22 stsp off_t outlen = 0;
73 3b0f3d61 2020-01-22 neels unsigned int len = atom->len;
74 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "%s", prefix);
76 2c20a3ed 2020-09-22 stsp return errno;
77 2c20a3ed 2020-09-22 stsp outlen += rc;
79 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
82 c6eecea3 2020-07-26 stsp if (ch == '\n')
85 6f26cb2e 2020-09-20 stsp rc = get_atom_byte(&ch, atom, len - 1);
88 c6eecea3 2020-07-26 stsp if (ch == '\r')
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);
97 6f26cb2e 2020-09-20 stsp if ((ch < 0x20 || ch >= 0x7f) && ch != '\t')
98 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "\\x%02x", (unsigned char)ch);
100 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "%c", ch);
101 2c20a3ed 2020-09-22 stsp if (rc < 0)
102 2c20a3ed 2020-09-22 stsp return errno;
103 2c20a3ed 2020-09-22 stsp outlen += rc;
105 2c20a3ed 2020-09-22 stsp rc = fprintf(dest, "\n");
106 2c20a3ed 2020-09-22 stsp if (rc < 0)
107 2c20a3ed 2020-09-22 stsp return errno;
108 2c20a3ed 2020-09-22 stsp outlen += rc;
109 2c20a3ed 2020-09-22 stsp if (outinfo) {
110 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
111 dabc1008 2020-09-22 stsp if (offp == NULL)
112 dabc1008 2020-09-22 stsp return ENOMEM;
113 2c20a3ed 2020-09-22 stsp outoff += outlen;
114 2c20a3ed 2020-09-22 stsp *offp = outoff;
118 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
122 2c20a3ed 2020-09-22 stsp diff_output_chunk_left_version(struct diff_output_info **output_info,
123 2c20a3ed 2020-09-22 stsp FILE *dest,
124 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
125 24b5052a 2020-09-22 stsp const struct diff_result *result,
126 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
128 8cba9b5e 2020-09-22 stsp int rc, c_idx;
129 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
131 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->left))
132 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
134 2c20a3ed 2020-09-22 stsp if (output_info) {
135 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
136 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
137 2c20a3ed 2020-09-22 stsp return ENOMEM;
138 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
141 24b5052a 2020-09-22 stsp /* Write out all chunks on the left side. */
142 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
143 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
145 fde86f3d 2020-10-07 stsp if (c->left_count) {
146 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "",
147 8cba9b5e 2020-09-22 stsp c->left_start, c->left_count);
153 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
157 2c20a3ed 2020-09-22 stsp diff_output_chunk_right_version(struct diff_output_info **output_info,
158 2c20a3ed 2020-09-22 stsp FILE *dest,
159 24b5052a 2020-09-22 stsp const struct diff_input_info *info,
160 24b5052a 2020-09-22 stsp const struct diff_result *result,
161 24b5052a 2020-09-22 stsp const struct diff_chunk_context *cc)
163 8cba9b5e 2020-09-22 stsp int rc, c_idx;
164 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
166 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->right))
167 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
169 2c20a3ed 2020-09-22 stsp if (output_info) {
170 2c20a3ed 2020-09-22 stsp *output_info = diff_output_info_alloc();
171 2c20a3ed 2020-09-22 stsp if (*output_info == NULL)
172 2c20a3ed 2020-09-22 stsp return ENOMEM;
173 2c20a3ed 2020-09-22 stsp outinfo = *output_info;
176 24b5052a 2020-09-22 stsp /* Write out all chunks on the right side. */
177 24b5052a 2020-09-22 stsp for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
178 24b5052a 2020-09-22 stsp const struct diff_chunk *c = &result->chunks.head[c_idx];
180 8cba9b5e 2020-09-22 stsp if (c->right_count) {
181 8cba9b5e 2020-09-22 stsp rc = diff_output_lines(outinfo, dest, "", c->right_start,
182 24b5052a 2020-09-22 stsp c->right_count);
188 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
192 7021523c 2020-10-16 stsp diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
193 7021523c 2020-10-16 stsp const struct diff_chunk *c)
195 7021523c 2020-10-16 stsp enum diff_chunk_type chunk_type = diff_chunk_type(c);
196 7021523c 2020-10-16 stsp struct diff_atom *atom, *start_atom;
197 7021523c 2020-10-16 stsp unsigned int atom_count;
198 7021523c 2020-10-16 stsp int rc, ch;
199 7021523c 2020-10-16 stsp off_t outoff = 0, *offp;
201 7021523c 2020-10-16 stsp if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
202 7021523c 2020-10-16 stsp start_atom = c->left_start;
203 7021523c 2020-10-16 stsp atom_count = c->left_count;
204 7021523c 2020-10-16 stsp } else if (chunk_type == CHUNK_PLUS) {
205 7021523c 2020-10-16 stsp start_atom = c->right_start;
206 7021523c 2020-10-16 stsp atom_count = c->right_count;
208 7021523c 2020-10-16 stsp return EINVAL;
210 7021523c 2020-10-16 stsp /* Locate the last atom. */
211 1b3c539b 2020-10-16 stsp if (atom_count == 0)
212 1b3c539b 2020-10-16 stsp return EINVAL;
213 1b3c539b 2020-10-16 stsp atom = &start_atom[atom_count - 1];
215 7021523c 2020-10-16 stsp rc = get_atom_byte(&ch, atom, atom->len - 1);
216 7021523c 2020-10-16 stsp if (rc != DIFF_RC_OK)
219 7021523c 2020-10-16 stsp if (ch != '\n') {
220 7021523c 2020-10-16 stsp if (outinfo && outinfo->line_offsets.len > 0) {
221 7021523c 2020-10-16 stsp unsigned int idx = outinfo->line_offsets.len - 1;
222 7021523c 2020-10-16 stsp outoff = outinfo->line_offsets.head[idx];
224 7021523c 2020-10-16 stsp rc = fprintf(dest, "\\ No newline at end of file\n");
225 7021523c 2020-10-16 stsp if (rc < 0)
226 7021523c 2020-10-16 stsp return errno;
227 7021523c 2020-10-16 stsp if (outinfo) {
228 7021523c 2020-10-16 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
229 7021523c 2020-10-16 stsp if (offp == NULL)
230 7021523c 2020-10-16 stsp return ENOMEM;
231 7021523c 2020-10-16 stsp outoff += rc;
232 7021523c 2020-10-16 stsp *offp = outoff;
236 7021523c 2020-10-16 stsp return DIFF_RC_OK;
239 2c20a3ed 2020-09-22 stsp struct diff_output_info *
240 2c20a3ed 2020-09-22 stsp diff_output_info_alloc(void)
242 2c20a3ed 2020-09-22 stsp struct diff_output_info *output_info;
243 2c20a3ed 2020-09-22 stsp off_t *offp;
245 2c20a3ed 2020-09-22 stsp output_info = malloc(sizeof(*output_info));
246 2c20a3ed 2020-09-22 stsp if (output_info != NULL) {
247 2c20a3ed 2020-09-22 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
248 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
249 dabc1008 2020-09-22 stsp if (offp == NULL) {
250 dabc1008 2020-09-22 stsp diff_output_info_free(output_info);
251 dabc1008 2020-09-22 stsp return NULL;
255 2c20a3ed 2020-09-22 stsp return output_info;
259 2c20a3ed 2020-09-22 stsp diff_output_info_free(struct diff_output_info *output_info)
261 2c20a3ed 2020-09-22 stsp ARRAYLIST_FREE(output_info->line_offsets);
262 2c20a3ed 2020-09-22 stsp free(output_info);