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->d->root->f);
42 if (cur == -1)
43 return errno;
45 if (cur != atom->pos + off &&
46 fseeko(atom->d->root->f, atom->pos + off, SEEK_SET) == -1)
47 return errno;
49 *ch = fgetc(atom->d->root->f);
50 if (*ch == EOF && ferror(atom->d->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 struct diff_output_info *
192 diff_output_info_alloc(void)
194 struct diff_output_info *output_info;
195 off_t *offp;
197 output_info = malloc(sizeof(*output_info));
198 if (output_info != NULL) {
199 ARRAYLIST_INIT(output_info->line_offsets, 128);
200 ARRAYLIST_ADD(offp, output_info->line_offsets);
201 if (offp == NULL) {
202 diff_output_info_free(output_info);
203 return NULL;
205 *offp = 0;
207 return output_info;
210 void
211 diff_output_info_free(struct diff_output_info *output_info)
213 ARRAYLIST_FREE(output_info->line_offsets);
214 free(output_info);