Raw
1 /*
2 * LibXDiff by Davide Libenzi ( File Differential Library )
3 * Copyright (C) 2003 Davide Libenzi
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * Davide Libenzi <davidel@xmailserver.org>
20 *
21 */
22
23 #include "xinclude.h"
24
25
26 static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb)
27 {
28 xrecord_t *rec = &xdf->recs[ri];
29
30 if (xdl_emit_diffrec((char const *)rec->ptr, (long)rec->size, pre, strlen(pre), ecb) < 0)
31 return -1;
32
33 return 0;
34 }
35
36 static long saturating_add(long a, long b)
37 {
38 return signed_add_overflows(a, b) ? LONG_MAX : a + b;
39 }
40
41 /*
42 * Starting at the passed change atom, find the latest change atom to be included
43 * inside the differential hunk according to the specified configuration.
44 * Also advance xscr if the first changes must be discarded.
45 */
46 xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg)
47 {
48 xdchange_t *xch, *xchp, *lxch;
49 long max_common;
50 long max_ignorable;
51 long ignored = 0; /* number of ignored blank lines */
52
53 if (xecfg->ctxlen < 0)
54 BUG("negative context length: %ld", xecfg->ctxlen);
55 if (xecfg->interhunkctxlen < 0)
56 BUG("negative inter-hunk context length: %ld", xecfg->interhunkctxlen);
57
58 max_common = saturating_add(saturating_add(xecfg->ctxlen,
59 xecfg->ctxlen),
60 xecfg->interhunkctxlen);
61 max_ignorable = xecfg->ctxlen;
62
63 /* remove ignorable changes that are too far before other changes */
64 for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) {
65 xch = xchp->next;
66
67 if (xch == NULL ||
68 xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable)
69 *xscr = xch;
70 }
71
72 if (!*xscr)
73 return NULL;
74
75 lxch = *xscr;
76
77 for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) {
78 long distance = xch->i1 - (xchp->i1 + xchp->chg1);
79 if (distance > max_common)
80 break;
81
82 if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) {
83 lxch = xch;
84 ignored = 0;
85 } else if (distance < max_ignorable && xch->ignore) {
86 ignored += xch->chg2;
87 } else if (lxch != xchp &&
88 xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) {
89 break;
90 } else if (!xch->ignore) {
91 lxch = xch;
92 ignored = 0;
93 } else {
94 ignored += xch->chg2;
95 }
96 }
97
98 return lxch;
99 }
100
101
102 static long def_ff(const char *rec, long len, char *buf, long sz)
103 {
104 if (len > 0 &&
105 (isalpha((unsigned char)*rec) || /* identifier? */
106 *rec == '_' || /* also identifier? */
107 *rec == '$')) { /* identifiers from VMS and other esoterico */
108 if (len > sz)
109 len = sz;
110 while (0 < len && isspace((unsigned char)rec[len - 1]))
111 len--;
112 memcpy(buf, rec, len);
113 return len;
114 }
115 return -1;
116 }
117
118 static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri,
119 char *buf, long sz)
120 {
121 xrecord_t *rec = &xdf->recs[ri];
122
123 if (!xecfg->find_func)
124 return def_ff((const char *)rec->ptr, (long)rec->size, buf, sz);
125 return xecfg->find_func((const char *)rec->ptr, (long)rec->size, buf, sz, xecfg->find_func_priv);
126 }
127
128 static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri)
129 {
130 char dummy[1];
131 return match_func_rec(xdf, xecfg, ri, dummy, sizeof(dummy)) >= 0;
132 }
133
134 struct func_line {
135 long len;
136 char buf[80];
137 };
138
139 static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
140 struct func_line *func_line, long start, long limit)
141 {
142 long l, size, step = (start > limit) ? -1 : 1;
143 char *buf, dummy[1];
144
145 buf = func_line ? func_line->buf : dummy;
146 size = func_line ? sizeof(func_line->buf) : sizeof(dummy);
147
148 for (l = start; l != limit && 0 <= l && l < (long)xe->xdf1.nrec; l += step) {
149 long len = match_func_rec(&xe->xdf1, xecfg, l, buf, size);
150 if (len >= 0) {
151 if (func_line)
152 func_line->len = len;
153 return l;
154 }
155 }
156 return -1;
157 }
158
159 static int is_empty_rec(xdfile_t *xdf, long ri)
160 {
161 xrecord_t *rec = &xdf->recs[ri];
162 size_t i = 0;
163
164 for (; i < rec->size && XDL_ISSPACE(rec->ptr[i]); i++);
165
166 return i == rec->size;
167 }
168
169 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
170 xdemitconf_t const *xecfg) {
171 long s1, s2, e1, e2, lctx;
172 xdchange_t *xch, *xche;
173 long funclineprev = -1;
174 struct func_line func_line = { 0 };
175
176 for (xch = xscr; xch; xch = xche->next) {
177 xdchange_t *xchp = xch;
178 xche = xdl_get_hunk(&xch, xecfg);
179 if (!xch)
180 break;
181
182 pre_context_calculation:
183 s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
184 s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
185
186 if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) {
187 long fs1, i1 = xch->i1;
188
189 /* Appended chunk? */
190 if (i1 >= (long)xe->xdf1.nrec) {
191 long i2 = xch->i2;
192
193 /*
194 * We don't need additional context if
195 * a whole function was added.
196 */
197 while (i2 < (long)xe->xdf2.nrec) {
198 if (is_func_rec(&xe->xdf2, xecfg, i2))
199 goto post_context_calculation;
200 i2++;
201 }
202
203 /*
204 * Otherwise get more context from the
205 * pre-image.
206 */
207 i1 = (long)xe->xdf1.nrec - 1;
208 }
209
210 fs1 = get_func_line(xe, xecfg, NULL, i1, -1);
211 while (fs1 > 0 && !is_empty_rec(&xe->xdf1, fs1 - 1) &&
212 !is_func_rec(&xe->xdf1, xecfg, fs1 - 1))
213 fs1--;
214 if (fs1 < 0)
215 fs1 = 0;
216 if (fs1 < s1) {
217 s2 = XDL_MAX(s2 - (s1 - fs1), 0);
218 s1 = fs1;
219
220 /*
221 * Did we extend context upwards into an
222 * ignored change?
223 */
224 while (xchp != xch &&
225 xchp->i1 + xchp->chg1 <= s1 &&
226 xchp->i2 + xchp->chg2 <= s2)
227 xchp = xchp->next;
228
229 /* If so, show it after all. */
230 if (xchp != xch) {
231 xch = xchp;
232 goto pre_context_calculation;
233 }
234 }
235 }
236
237 post_context_calculation:
238 lctx = xecfg->ctxlen;
239 lctx = XDL_MIN(lctx, (long)xe->xdf1.nrec - (xche->i1 + xche->chg1));
240 lctx = XDL_MIN(lctx, (long)xe->xdf2.nrec - (xche->i2 + xche->chg2));
241
242 e1 = xche->i1 + xche->chg1 + lctx;
243 e2 = xche->i2 + xche->chg2 + lctx;
244
245 if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) {
246 long fe1 = get_func_line(xe, xecfg, NULL,
247 xche->i1 + xche->chg1,
248 (long)xe->xdf1.nrec);
249 while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1))
250 fe1--;
251 if (fe1 < 0)
252 fe1 = (long)xe->xdf1.nrec;
253 if (fe1 > e1) {
254 e2 = XDL_MIN(e2 + (fe1 - e1), (long)xe->xdf2.nrec);
255 e1 = fe1;
256 }
257
258 /*
259 * Overlap with next change? Then include it
260 * in the current hunk and start over to find
261 * its new end.
262 */
263 if (xche->next) {
264 long l = XDL_MIN(xche->next->i1,
265 (long)xe->xdf1.nrec - 1);
266 if (l - xecfg->ctxlen <= e1 ||
267 get_func_line(xe, xecfg, NULL, l, e1) < 0) {
268 xche = xche->next;
269 goto post_context_calculation;
270 }
271 }
272 }
273
274 /*
275 * Emit current hunk header.
276 */
277
278 if (xecfg->flags & XDL_EMIT_FUNCNAMES) {
279 get_func_line(xe, xecfg, &func_line,
280 s1 - 1, funclineprev);
281 funclineprev = s1 - 1;
282 }
283 if (!(xecfg->flags & XDL_EMIT_NO_HUNK_HDR) &&
284 xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
285 func_line.buf, func_line.len, ecb) < 0)
286 return -1;
287
288 /*
289 * Emit pre-context.
290 */
291 for (; s2 < xch->i2; s2++)
292 if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0)
293 return -1;
294
295 for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) {
296 /*
297 * Merge previous with current change atom.
298 */
299 for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++)
300 if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0)
301 return -1;
302
303 /*
304 * Removes lines from the first file.
305 */
306 for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++)
307 if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0)
308 return -1;
309
310 /*
311 * Adds lines from the second file.
312 */
313 for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++)
314 if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0)
315 return -1;
316
317 if (xch == xche)
318 break;
319 s1 = xch->i1 + xch->chg1;
320 s2 = xch->i2 + xch->chg2;
321 }
322
323 /*
324 * Emit post-context.
325 */
326 for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++)
327 if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0)
328 return -1;
329 }
330
331 return 0;
332 }