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 7a54ad3a 2020-09-20 stsp cur = ftello(atom->d->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 7a54ad3a 2020-09-20 stsp fseeko(atom->d->root->f, atom->pos + off, SEEK_SET) == -1)
47 6f26cb2e 2020-09-20 stsp return errno;
48 c6eecea3 2020-07-26 stsp
49 6f26cb2e 2020-09-20 stsp *ch = fgetc(atom->d->root->f);
50 6f26cb2e 2020-09-20 stsp if (*ch == EOF && ferror(atom->d->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 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);
99 3b0f3d61 2020-01-22 neels else
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;
104 3b0f3d61 2020-01-22 neels }
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;
115 2c20a3ed 2020-09-22 stsp }
116 3b0f3d61 2020-01-22 neels }
117 6f26cb2e 2020-09-20 stsp
118 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
119 3b0f3d61 2020-01-22 neels }
120 24b5052a 2020-09-22 stsp
121 2c20a3ed 2020-09-22 stsp int
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)
127 24b5052a 2020-09-22 stsp {
128 8cba9b5e 2020-09-22 stsp int rc, c_idx;
129 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
130 24b5052a 2020-09-22 stsp
131 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->left))
132 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
133 24b5052a 2020-09-22 stsp
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;
139 2c20a3ed 2020-09-22 stsp }
140 2c20a3ed 2020-09-22 stsp
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];
144 24b5052a 2020-09-22 stsp
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);
148 8cba9b5e 2020-09-22 stsp if (rc)
149 8cba9b5e 2020-09-22 stsp return rc;
150 fde86f3d 2020-10-07 stsp }
151 24b5052a 2020-09-22 stsp }
152 2c20a3ed 2020-09-22 stsp
153 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
154 24b5052a 2020-09-22 stsp }
155 24b5052a 2020-09-22 stsp
156 2c20a3ed 2020-09-22 stsp int
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)
162 24b5052a 2020-09-22 stsp {
163 8cba9b5e 2020-09-22 stsp int rc, c_idx;
164 2c20a3ed 2020-09-22 stsp struct diff_output_info *outinfo = NULL;
165 24b5052a 2020-09-22 stsp
166 24b5052a 2020-09-22 stsp if (diff_range_empty(&cc->right))
167 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
168 24b5052a 2020-09-22 stsp
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;
174 2c20a3ed 2020-09-22 stsp }
175 2c20a3ed 2020-09-22 stsp
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];
179 24b5052a 2020-09-22 stsp
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);
183 8cba9b5e 2020-09-22 stsp if (rc)
184 8cba9b5e 2020-09-22 stsp return rc;
185 8cba9b5e 2020-09-22 stsp }
186 24b5052a 2020-09-22 stsp }
187 2c20a3ed 2020-09-22 stsp
188 2c20a3ed 2020-09-22 stsp return DIFF_RC_OK;
189 24b5052a 2020-09-22 stsp }
190 24b5052a 2020-09-22 stsp
191 2c20a3ed 2020-09-22 stsp struct diff_output_info *
192 2c20a3ed 2020-09-22 stsp diff_output_info_alloc(void)
193 2c20a3ed 2020-09-22 stsp {
194 2c20a3ed 2020-09-22 stsp struct diff_output_info *output_info;
195 2c20a3ed 2020-09-22 stsp off_t *offp;
196 2c20a3ed 2020-09-22 stsp
197 2c20a3ed 2020-09-22 stsp output_info = malloc(sizeof(*output_info));
198 2c20a3ed 2020-09-22 stsp if (output_info != NULL) {
199 2c20a3ed 2020-09-22 stsp ARRAYLIST_INIT(output_info->line_offsets, 128);
200 2c20a3ed 2020-09-22 stsp ARRAYLIST_ADD(offp, output_info->line_offsets);
201 dabc1008 2020-09-22 stsp if (offp == NULL) {
202 dabc1008 2020-09-22 stsp diff_output_info_free(output_info);
203 dabc1008 2020-09-22 stsp return NULL;
204 dabc1008 2020-09-22 stsp }
205 2c20a3ed 2020-09-22 stsp *offp = 0;
206 2c20a3ed 2020-09-22 stsp }
207 2c20a3ed 2020-09-22 stsp return output_info;
208 2c20a3ed 2020-09-22 stsp }
209 2c20a3ed 2020-09-22 stsp
210 2c20a3ed 2020-09-22 stsp void
211 2c20a3ed 2020-09-22 stsp diff_output_info_free(struct diff_output_info *output_info)
212 2c20a3ed 2020-09-22 stsp {
213 2c20a3ed 2020-09-22 stsp ARRAYLIST_FREE(output_info->line_offsets);
214 2c20a3ed 2020-09-22 stsp free(output_info);
215 2c20a3ed 2020-09-22 stsp }