Blame


1 b7ba71f0 2020-10-07 stsp /* Produce ed(1) script output from a diff_result. */
2 b7ba71f0 2020-10-07 stsp /*
3 b7ba71f0 2020-10-07 stsp * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 b7ba71f0 2020-10-07 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
5 b7ba71f0 2020-10-07 stsp *
6 b7ba71f0 2020-10-07 stsp * Permission to use, copy, modify, and distribute this software for any
7 b7ba71f0 2020-10-07 stsp * purpose with or without fee is hereby granted, provided that the above
8 b7ba71f0 2020-10-07 stsp * copyright notice and this permission notice appear in all copies.
9 b7ba71f0 2020-10-07 stsp *
10 b7ba71f0 2020-10-07 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 b7ba71f0 2020-10-07 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 b7ba71f0 2020-10-07 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 b7ba71f0 2020-10-07 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 b7ba71f0 2020-10-07 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 b7ba71f0 2020-10-07 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 b7ba71f0 2020-10-07 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 b7ba71f0 2020-10-07 stsp */
18 b7ba71f0 2020-10-07 stsp
19 b7ba71f0 2020-10-07 stsp #include <errno.h>
20 fe6d58fb 2020-11-14 naddy #include <stdint.h>
21 b7ba71f0 2020-10-07 stsp #include <stdio.h>
22 b7ba71f0 2020-10-07 stsp #include <stdlib.h>
23 b7ba71f0 2020-10-07 stsp #include <stdbool.h>
24 b7ba71f0 2020-10-07 stsp
25 b7ba71f0 2020-10-07 stsp #include <arraylist.h>
26 b7ba71f0 2020-10-07 stsp #include <diff_main.h>
27 b7ba71f0 2020-10-07 stsp #include <diff_output.h>
28 b7ba71f0 2020-10-07 stsp
29 b7ba71f0 2020-10-07 stsp #include "diff_internal.h"
30 b7ba71f0 2020-10-07 stsp
31 b7ba71f0 2020-10-07 stsp static int
32 b7ba71f0 2020-10-07 stsp output_edscript_chunk(struct diff_output_info *outinfo,
33 b7ba71f0 2020-10-07 stsp FILE *dest, const struct diff_input_info *info,
34 b7ba71f0 2020-10-07 stsp const struct diff_result *result,
35 26595c7d 2020-10-15 stsp struct diff_chunk_context *cc)
36 b7ba71f0 2020-10-07 stsp {
37 b7ba71f0 2020-10-07 stsp off_t outoff = 0, *offp;
38 b7ba71f0 2020-10-07 stsp int left_start, left_len, right_start, right_len;
39 b7ba71f0 2020-10-07 stsp int rc;
40 b7ba71f0 2020-10-07 stsp
41 b7ba71f0 2020-10-07 stsp left_len = cc->left.end - cc->left.start;
42 b7ba71f0 2020-10-07 stsp if (left_len < 0)
43 b7ba71f0 2020-10-07 stsp return EINVAL;
44 c16dde50 2020-10-22 stsp else if (result->left->atoms.len == 0)
45 ede9a86a 2020-10-07 stsp left_start = 0;
46 b7ba71f0 2020-10-07 stsp else if (left_len == 0 && cc->left.start > 0)
47 b7ba71f0 2020-10-07 stsp left_start = cc->left.start;
48 b7ba71f0 2020-10-07 stsp else
49 b7ba71f0 2020-10-07 stsp left_start = cc->left.start + 1;
50 b7ba71f0 2020-10-07 stsp
51 b7ba71f0 2020-10-07 stsp right_len = cc->right.end - cc->right.start;
52 b7ba71f0 2020-10-07 stsp if (right_len < 0)
53 b7ba71f0 2020-10-07 stsp return EINVAL;
54 c16dde50 2020-10-22 stsp else if (result->right->atoms.len == 0)
55 ede9a86a 2020-10-07 stsp right_start = 0;
56 b7ba71f0 2020-10-07 stsp else if (right_len == 0 && cc->right.start > 0)
57 b7ba71f0 2020-10-07 stsp right_start = cc->right.start;
58 b7ba71f0 2020-10-07 stsp else
59 b7ba71f0 2020-10-07 stsp right_start = cc->right.start + 1;
60 b7ba71f0 2020-10-07 stsp
61 26595c7d 2020-10-15 stsp if (left_len == 0) {
62 26595c7d 2020-10-15 stsp /* addition */
63 26595c7d 2020-10-15 stsp if (right_len == 1) {
64 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%da%d\n", left_start, right_start);
65 26595c7d 2020-10-15 stsp } else {
66 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%da%d,%d\n", left_start,
67 26595c7d 2020-10-15 stsp right_start, cc->right.end);
68 26595c7d 2020-10-15 stsp }
69 26595c7d 2020-10-15 stsp } else if (right_len == 0) {
70 26595c7d 2020-10-15 stsp /* deletion */
71 b7ba71f0 2020-10-07 stsp if (left_len == 1) {
72 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%dd%d\n", left_start,
73 26595c7d 2020-10-15 stsp right_start);
74 b7ba71f0 2020-10-07 stsp } else {
75 b7ba71f0 2020-10-07 stsp rc = fprintf(dest, "%d,%dd%d\n", left_start,
76 b7ba71f0 2020-10-07 stsp cc->left.end, right_start);
77 b7ba71f0 2020-10-07 stsp }
78 26595c7d 2020-10-15 stsp } else {
79 26595c7d 2020-10-15 stsp /* change */
80 26595c7d 2020-10-15 stsp if (left_len == 1 && right_len == 1) {
81 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%dc%d\n", left_start, right_start);
82 26595c7d 2020-10-15 stsp } else if (left_len == 1) {
83 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%dc%d,%d\n", left_start,
84 b7ba71f0 2020-10-07 stsp right_start, cc->right.end);
85 26595c7d 2020-10-15 stsp } else if (right_len == 1) {
86 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%d,%dc%d\n", left_start,
87 26595c7d 2020-10-15 stsp cc->left.end, right_start);
88 26595c7d 2020-10-15 stsp } else {
89 26595c7d 2020-10-15 stsp rc = fprintf(dest, "%d,%dc%d,%d\n", left_start,
90 26595c7d 2020-10-15 stsp cc->left.end, right_start, cc->right.end);
91 b7ba71f0 2020-10-07 stsp }
92 26595c7d 2020-10-15 stsp }
93 b7ba71f0 2020-10-07 stsp if (rc < 0)
94 b7ba71f0 2020-10-07 stsp return errno;
95 b7ba71f0 2020-10-07 stsp if (outinfo) {
96 b7ba71f0 2020-10-07 stsp ARRAYLIST_ADD(offp, outinfo->line_offsets);
97 b7ba71f0 2020-10-07 stsp if (offp == NULL)
98 b7ba71f0 2020-10-07 stsp return ENOMEM;
99 b7ba71f0 2020-10-07 stsp outoff += rc;
100 b7ba71f0 2020-10-07 stsp *offp = outoff;
101 b7ba71f0 2020-10-07 stsp }
102 b7ba71f0 2020-10-07 stsp
103 b7ba71f0 2020-10-07 stsp return DIFF_RC_OK;
104 b7ba71f0 2020-10-07 stsp }
105 b7ba71f0 2020-10-07 stsp
106 b7ba71f0 2020-10-07 stsp int
107 b7ba71f0 2020-10-07 stsp diff_output_edscript(struct diff_output_info **output_info,
108 b7ba71f0 2020-10-07 stsp FILE *dest, const struct diff_input_info *info,
109 b7ba71f0 2020-10-07 stsp const struct diff_result *result)
110 b7ba71f0 2020-10-07 stsp {
111 b7ba71f0 2020-10-07 stsp struct diff_output_info *outinfo = NULL;
112 26595c7d 2020-10-15 stsp struct diff_chunk_context cc = {};
113 e51ebd83 2020-11-21 stsp int atomizer_flags = (result->left->atomizer_flags|
114 e51ebd83 2020-11-21 stsp result->right->atomizer_flags);
115 e51ebd83 2020-11-21 stsp int flags = (result->left->root->diff_flags |
116 e51ebd83 2020-11-21 stsp result->right->root->diff_flags);
117 e51ebd83 2020-11-21 stsp bool force_text = (flags & DIFF_FLAG_FORCE_TEXT_DATA);
118 e51ebd83 2020-11-21 stsp bool have_binary = (atomizer_flags & DIFF_ATOMIZER_FOUND_BINARY_DATA);
119 b7ba71f0 2020-10-07 stsp int i, rc;
120 b7ba71f0 2020-10-07 stsp
121 b7ba71f0 2020-10-07 stsp if (!result)
122 b7ba71f0 2020-10-07 stsp return EINVAL;
123 b7ba71f0 2020-10-07 stsp if (result->rc != DIFF_RC_OK)
124 b7ba71f0 2020-10-07 stsp return result->rc;
125 b7ba71f0 2020-10-07 stsp
126 b7ba71f0 2020-10-07 stsp if (output_info) {
127 b7ba71f0 2020-10-07 stsp *output_info = diff_output_info_alloc();
128 b7ba71f0 2020-10-07 stsp if (*output_info == NULL)
129 b7ba71f0 2020-10-07 stsp return ENOMEM;
130 b7ba71f0 2020-10-07 stsp outinfo = *output_info;
131 b7ba71f0 2020-10-07 stsp }
132 b7ba71f0 2020-10-07 stsp
133 e51ebd83 2020-11-21 stsp if (have_binary && !force_text) {
134 e51ebd83 2020-11-21 stsp for (i = 0; i < result->chunks.len; i++) {
135 e51ebd83 2020-11-21 stsp struct diff_chunk *c = &result->chunks.head[i];
136 e51ebd83 2020-11-21 stsp enum diff_chunk_type t = diff_chunk_type(c);
137 e51ebd83 2020-11-21 stsp
138 e51ebd83 2020-11-21 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
139 e51ebd83 2020-11-21 stsp continue;
140 e51ebd83 2020-11-21 stsp
141 e51ebd83 2020-11-21 stsp fprintf(dest, "Binary files %s and %s differ\n",
142 e4c510c1 2020-11-21 stsp diff_output_get_label_left(info),
143 e4c510c1 2020-11-21 stsp diff_output_get_label_right(info));
144 e51ebd83 2020-11-21 stsp break;
145 e51ebd83 2020-11-21 stsp }
146 e51ebd83 2020-11-21 stsp
147 e51ebd83 2020-11-21 stsp return DIFF_RC_OK;
148 e51ebd83 2020-11-21 stsp }
149 e51ebd83 2020-11-21 stsp
150 b7ba71f0 2020-10-07 stsp for (i = 0; i < result->chunks.len; i++) {
151 b7ba71f0 2020-10-07 stsp struct diff_chunk *chunk = &result->chunks.head[i];
152 b7ba71f0 2020-10-07 stsp enum diff_chunk_type t = diff_chunk_type(chunk);
153 26595c7d 2020-10-15 stsp struct diff_chunk_context next;
154 b7ba71f0 2020-10-07 stsp
155 b7ba71f0 2020-10-07 stsp if (t != CHUNK_MINUS && t != CHUNK_PLUS)
156 b7ba71f0 2020-10-07 stsp continue;
157 b7ba71f0 2020-10-07 stsp
158 cbf93b70 2020-10-16 stsp if (diff_chunk_context_empty(&cc)) {
159 26595c7d 2020-10-15 stsp /* Note down the start point, any number of subsequent
160 26595c7d 2020-10-15 stsp * chunks may be joined up to this chunk by being
161 26595c7d 2020-10-15 stsp * directly adjacent. */
162 26595c7d 2020-10-15 stsp diff_chunk_context_get(&cc, result, i, 0);
163 26595c7d 2020-10-15 stsp continue;
164 26595c7d 2020-10-15 stsp }
165 b7ba71f0 2020-10-07 stsp
166 26595c7d 2020-10-15 stsp /* There already is a previous chunk noted down for being
167 26595c7d 2020-10-15 stsp * printed. Does it join up with this one? */
168 26595c7d 2020-10-15 stsp diff_chunk_context_get(&next, result, i, 0);
169 26595c7d 2020-10-15 stsp
170 26595c7d 2020-10-15 stsp if (diff_chunk_contexts_touch(&cc, &next)) {
171 26595c7d 2020-10-15 stsp /* This next context touches or overlaps the previous
172 26595c7d 2020-10-15 stsp * one, join. */
173 26595c7d 2020-10-15 stsp diff_chunk_contexts_merge(&cc, &next);
174 b7ba71f0 2020-10-07 stsp continue;
175 26595c7d 2020-10-15 stsp }
176 b7ba71f0 2020-10-07 stsp
177 26595c7d 2020-10-15 stsp rc = output_edscript_chunk(outinfo, dest, info, result, &cc);
178 b7ba71f0 2020-10-07 stsp if (rc != DIFF_RC_OK)
179 b7ba71f0 2020-10-07 stsp return rc;
180 26595c7d 2020-10-15 stsp cc = next;
181 b7ba71f0 2020-10-07 stsp }
182 b7ba71f0 2020-10-07 stsp
183 cbf93b70 2020-10-16 stsp if (!diff_chunk_context_empty(&cc))
184 26595c7d 2020-10-15 stsp return output_edscript_chunk(outinfo, dest, info, result, &cc);
185 b7ba71f0 2020-10-07 stsp return DIFF_RC_OK;
186 b7ba71f0 2020-10-07 stsp }