Blob


1 /* Common parts for printing diff output */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <errno.h>
19 #include <inttypes.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <arraylist.h>
26 #include <diff_main.h>
27 #include <diff_output.h>
29 #include "diff_internal.h"
31 static int
32 get_atom_byte(int *ch, struct diff_atom *atom, off_t off)
33 {
34 off_t cur;
36 if (atom->at != NULL) {
37 *ch = atom->at[off];
38 return 0;
39 }
41 cur = ftello(atom->root->f);
42 if (cur == -1)
43 return errno;
45 if (cur != atom->pos + off &&
46 fseeko(atom->root->f, atom->pos + off, SEEK_SET) == -1)
47 return errno;
49 *ch = fgetc(atom->root->f);
50 if (*ch == EOF && ferror(atom->root->f))
51 return errno;
53 return 0;
54 }
56 int
57 diff_output_lines(struct diff_output_info *outinfo, FILE *dest,
58 const char *prefix, struct diff_atom *start_atom,
59 unsigned int count)
60 {
61 struct diff_atom *atom;
62 off_t outoff = 0, *offp;
63 int rc;
65 if (outinfo && outinfo->line_offsets.len > 0) {
66 unsigned int idx = outinfo->line_offsets.len - 1;
67 outoff = outinfo->line_offsets.head[idx];
68 }
70 foreach_diff_atom(atom, start_atom, count) {
71 off_t outlen = 0;
72 int i, ch;
73 unsigned int len = atom->len;
74 rc = fprintf(dest, "%s", prefix);
75 if (rc < 0)
76 return errno;
77 outlen += rc;
78 if (len) {
79 rc = get_atom_byte(&ch, atom, len - 1);
80 if (rc)
81 return rc;
82 if (ch == '\n')
83 len--;
84 if (len) {
85 rc = get_atom_byte(&ch, atom, len - 1);
86 if (rc)
87 return rc;
88 if (ch == '\r')
89 len--;
90 }
91 }
93 for (i = 0; i < len; i++) {
94 rc = get_atom_byte(&ch, atom, i);
95 if (rc)
96 return rc;
97 if ((ch < 0x20 || ch >= 0x7f) && ch != '\t')
98 rc = fprintf(dest, "\\x%02x", (unsigned char)ch);
99 else
100 rc = fprintf(dest, "%c", ch);
101 if (rc < 0)
102 return errno;
103 outlen += rc;
105 rc = fprintf(dest, "\n");
106 if (rc < 0)
107 return errno;
108 outlen += rc;
109 if (outinfo) {
110 ARRAYLIST_ADD(offp, outinfo->line_offsets);
111 if (offp == NULL)
112 return ENOMEM;
113 outoff += outlen;
114 *offp = outoff;
118 return DIFF_RC_OK;
121 int
122 diff_output_chunk_left_version(struct diff_output_info **output_info,
123 FILE *dest,
124 const struct diff_input_info *info,
125 const struct diff_result *result,
126 const struct diff_chunk_context *cc)
128 int rc, c_idx;
129 struct diff_output_info *outinfo = NULL;
131 if (diff_range_empty(&cc->left))
132 return DIFF_RC_OK;
134 if (output_info) {
135 *output_info = diff_output_info_alloc();
136 if (*output_info == NULL)
137 return ENOMEM;
138 outinfo = *output_info;
141 /* Write out all chunks on the left side. */
142 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
143 const struct diff_chunk *c = &result->chunks.head[c_idx];
145 if (c->left_count) {
146 rc = diff_output_lines(outinfo, dest, "",
147 c->left_start, c->left_count);
148 if (rc)
149 return rc;
153 return DIFF_RC_OK;
156 int
157 diff_output_chunk_right_version(struct diff_output_info **output_info,
158 FILE *dest,
159 const struct diff_input_info *info,
160 const struct diff_result *result,
161 const struct diff_chunk_context *cc)
163 int rc, c_idx;
164 struct diff_output_info *outinfo = NULL;
166 if (diff_range_empty(&cc->right))
167 return DIFF_RC_OK;
169 if (output_info) {
170 *output_info = diff_output_info_alloc();
171 if (*output_info == NULL)
172 return ENOMEM;
173 outinfo = *output_info;
176 /* Write out all chunks on the right side. */
177 for (c_idx = cc->chunk.start; c_idx < cc->chunk.end; c_idx++) {
178 const struct diff_chunk *c = &result->chunks.head[c_idx];
180 if (c->right_count) {
181 rc = diff_output_lines(outinfo, dest, "", c->right_start,
182 c->right_count);
183 if (rc)
184 return rc;
188 return DIFF_RC_OK;
191 int
192 diff_output_trailing_newline_msg(struct diff_output_info *outinfo, FILE *dest,
193 const struct diff_chunk *c)
195 enum diff_chunk_type chunk_type = diff_chunk_type(c);
196 struct diff_atom *atom, *start_atom;
197 unsigned int atom_count;
198 int rc, ch;
199 off_t outoff = 0, *offp;
201 if (chunk_type == CHUNK_MINUS || chunk_type == CHUNK_SAME) {
202 start_atom = c->left_start;
203 atom_count = c->left_count;
204 } else if (chunk_type == CHUNK_PLUS) {
205 start_atom = c->right_start;
206 atom_count = c->right_count;
207 } else
208 return EINVAL;
210 /* Locate the last atom. */
211 if (atom_count == 0)
212 return EINVAL;
213 atom = &start_atom[atom_count - 1];
215 rc = get_atom_byte(&ch, atom, atom->len - 1);
216 if (rc != DIFF_RC_OK)
217 return rc;
219 if (ch != '\n') {
220 if (outinfo && outinfo->line_offsets.len > 0) {
221 unsigned int idx = outinfo->line_offsets.len - 1;
222 outoff = outinfo->line_offsets.head[idx];
224 rc = fprintf(dest, "\\ No newline at end of file\n");
225 if (rc < 0)
226 return errno;
227 if (outinfo) {
228 ARRAYLIST_ADD(offp, outinfo->line_offsets);
229 if (offp == NULL)
230 return ENOMEM;
231 outoff += rc;
232 *offp = outoff;
236 return DIFF_RC_OK;
239 struct diff_output_info *
240 diff_output_info_alloc(void)
242 struct diff_output_info *output_info;
243 off_t *offp;
245 output_info = malloc(sizeof(*output_info));
246 if (output_info != NULL) {
247 ARRAYLIST_INIT(output_info->line_offsets, 128);
248 ARRAYLIST_ADD(offp, output_info->line_offsets);
249 if (offp == NULL) {
250 diff_output_info_free(output_info);
251 return NULL;
253 *offp = 0;
255 return output_info;
258 void
259 diff_output_info_free(struct diff_output_info *output_info)
261 ARRAYLIST_FREE(output_info->line_offsets);
262 free(output_info);