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 5ed6e4d2 2020-10-17 stsp rc = fprintf(dest, "%c", (unsigned char)ch);
99 2c20a3ed 2020-09-22 stsp return errno;
100 2c20a3ed 2020-09-22 stsp outlen += rc;
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;
115 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
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)
125 8cba9b5e 2020-09-22 stsp int rc, c_idx;
126 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
128 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->left))
129 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
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;
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];
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);
150 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
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)
160 8cba9b5e 2020-09-22 stsp int rc, c_idx;
161 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
163 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->right))
164 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
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;
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];
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);
185 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
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)
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;
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;
205 7021523c 2020-10-16 stsp return EINVAL;
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];
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)
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];
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;
233 7021523c 2020-10-16 stsp return DIFF_RC_OK;
236 2c20a3ed 2020-09-22 stsp struct diff_output_info *
237 2c20a3ed 2020-09-22 stsp diff_output_info_alloc(void)
239 2c20a3ed 2020-09-22 stsp struct diff_output_info *output_info;
240 2c20a3ed 2020-09-22 stsp off_t *offp;
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;
252 2c20a3ed 2020-09-22 stsp return output_info;
256 2c20a3ed 2020-09-22 stsp diff_output_info_free(struct diff_output_info *output_info)
258 2c20a3ed 2020-09-22 stsp ARRAYLIST_FREE(output_info->line_offsets);
259 2c20a3ed 2020-09-22 stsp free(output_info);