Line | Count | Source |
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 | 0 | { |
28 | 0 | xrecord_t *rec = &xdf->recs[ri]; |
29 | |
|
30 | 0 | if (xdl_emit_diffrec((char const *)rec->ptr, (long)rec->size, pre, strlen(pre), ecb) < 0) |
31 | 0 | return -1; |
32 | | |
33 | 0 | return 0; |
34 | 0 | } |
35 | | |
36 | | static long saturating_add(long a, long b) |
37 | 0 | { |
38 | 0 | return signed_add_overflows(a, b) ? LONG_MAX : a + b; |
39 | 0 | } |
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 | 0 | { |
48 | 0 | xdchange_t *xch, *xchp, *lxch; |
49 | 0 | long max_common; |
50 | 0 | long max_ignorable; |
51 | 0 | long ignored = 0; /* number of ignored blank lines */ |
52 | |
|
53 | 0 | if (xecfg->ctxlen < 0) |
54 | 0 | BUG("negative context length: %ld", xecfg->ctxlen); |
55 | 0 | if (xecfg->interhunkctxlen < 0) |
56 | 0 | BUG("negative inter-hunk context length: %ld", xecfg->interhunkctxlen); |
57 | | |
58 | 0 | max_common = saturating_add(saturating_add(xecfg->ctxlen, |
59 | 0 | xecfg->ctxlen), |
60 | 0 | xecfg->interhunkctxlen); |
61 | 0 | max_ignorable = xecfg->ctxlen; |
62 | | |
63 | | /* remove ignorable changes that are too far before other changes */ |
64 | 0 | for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { |
65 | 0 | xch = xchp->next; |
66 | |
|
67 | 0 | if (xch == NULL || |
68 | 0 | xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) |
69 | 0 | *xscr = xch; |
70 | 0 | } |
71 | |
|
72 | 0 | if (!*xscr) |
73 | 0 | return NULL; |
74 | | |
75 | 0 | lxch = *xscr; |
76 | |
|
77 | 0 | for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { |
78 | 0 | long distance = xch->i1 - (xchp->i1 + xchp->chg1); |
79 | 0 | if (distance > max_common) |
80 | 0 | break; |
81 | | |
82 | 0 | if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) { |
83 | 0 | lxch = xch; |
84 | 0 | ignored = 0; |
85 | 0 | } else if (distance < max_ignorable && xch->ignore) { |
86 | 0 | ignored += xch->chg2; |
87 | 0 | } else if (lxch != xchp && |
88 | 0 | xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) { |
89 | 0 | break; |
90 | 0 | } else if (!xch->ignore) { |
91 | 0 | lxch = xch; |
92 | 0 | ignored = 0; |
93 | 0 | } else { |
94 | 0 | ignored += xch->chg2; |
95 | 0 | } |
96 | 0 | } |
97 | |
|
98 | 0 | return lxch; |
99 | 0 | } |
100 | | |
101 | | |
102 | | static long def_ff(const char *rec, long len, char *buf, long sz) |
103 | 0 | { |
104 | 0 | if (len > 0 && |
105 | 0 | (isalpha((unsigned char)*rec) || /* identifier? */ |
106 | 0 | *rec == '_' || /* also identifier? */ |
107 | 0 | *rec == '$')) { /* identifiers from VMS and other esoterico */ |
108 | 0 | if (len > sz) |
109 | 0 | len = sz; |
110 | 0 | while (0 < len && isspace((unsigned char)rec[len - 1])) |
111 | 0 | len--; |
112 | 0 | memcpy(buf, rec, len); |
113 | 0 | return len; |
114 | 0 | } |
115 | 0 | return -1; |
116 | 0 | } |
117 | | |
118 | | static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri, |
119 | | char *buf, long sz) |
120 | 0 | { |
121 | 0 | xrecord_t *rec = &xdf->recs[ri]; |
122 | |
|
123 | 0 | if (!xecfg->find_func) |
124 | 0 | return def_ff((const char *)rec->ptr, (long)rec->size, buf, sz); |
125 | 0 | return xecfg->find_func((const char *)rec->ptr, (long)rec->size, buf, sz, xecfg->find_func_priv); |
126 | 0 | } |
127 | | |
128 | | static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri) |
129 | 0 | { |
130 | 0 | char dummy[1]; |
131 | 0 | return match_func_rec(xdf, xecfg, ri, dummy, sizeof(dummy)) >= 0; |
132 | 0 | } |
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 | 0 | { |
142 | 0 | long l, size, step = (start > limit) ? -1 : 1; |
143 | 0 | char *buf, dummy[1]; |
144 | |
|
145 | 0 | buf = func_line ? func_line->buf : dummy; |
146 | 0 | size = func_line ? sizeof(func_line->buf) : sizeof(dummy); |
147 | |
|
148 | 0 | for (l = start; l != limit && 0 <= l && l < (long)xe->xdf1.nrec; l += step) { |
149 | 0 | long len = match_func_rec(&xe->xdf1, xecfg, l, buf, size); |
150 | 0 | if (len >= 0) { |
151 | 0 | if (func_line) |
152 | 0 | func_line->len = len; |
153 | 0 | return l; |
154 | 0 | } |
155 | 0 | } |
156 | 0 | return -1; |
157 | 0 | } |
158 | | |
159 | | static int is_empty_rec(xdfile_t *xdf, long ri) |
160 | 0 | { |
161 | 0 | xrecord_t *rec = &xdf->recs[ri]; |
162 | 0 | size_t i = 0; |
163 | |
|
164 | 0 | for (; i < rec->size && XDL_ISSPACE(rec->ptr[i]); i++); |
165 | |
|
166 | 0 | return i == rec->size; |
167 | 0 | } |
168 | | |
169 | | int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, |
170 | 0 | xdemitconf_t const *xecfg) { |
171 | 0 | long s1, s2, e1, e2, lctx; |
172 | 0 | xdchange_t *xch, *xche; |
173 | 0 | long funclineprev = -1; |
174 | 0 | struct func_line func_line = { 0 }; |
175 | |
|
176 | 0 | for (xch = xscr; xch; xch = xche->next) { |
177 | 0 | xdchange_t *xchp = xch; |
178 | 0 | xche = xdl_get_hunk(&xch, xecfg); |
179 | 0 | if (!xch) |
180 | 0 | break; |
181 | | |
182 | 0 | pre_context_calculation: |
183 | 0 | s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0); |
184 | 0 | s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0); |
185 | |
|
186 | 0 | if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { |
187 | 0 | long fs1, i1 = xch->i1; |
188 | | |
189 | | /* Appended chunk? */ |
190 | 0 | if (i1 >= (long)xe->xdf1.nrec) { |
191 | 0 | long i2 = xch->i2; |
192 | | |
193 | | /* |
194 | | * We don't need additional context if |
195 | | * a whole function was added. |
196 | | */ |
197 | 0 | while (i2 < (long)xe->xdf2.nrec) { |
198 | 0 | if (is_func_rec(&xe->xdf2, xecfg, i2)) |
199 | 0 | goto post_context_calculation; |
200 | 0 | i2++; |
201 | 0 | } |
202 | | |
203 | | /* |
204 | | * Otherwise get more context from the |
205 | | * pre-image. |
206 | | */ |
207 | 0 | i1 = (long)xe->xdf1.nrec - 1; |
208 | 0 | } |
209 | | |
210 | 0 | fs1 = get_func_line(xe, xecfg, NULL, i1, -1); |
211 | 0 | while (fs1 > 0 && !is_empty_rec(&xe->xdf1, fs1 - 1) && |
212 | 0 | !is_func_rec(&xe->xdf1, xecfg, fs1 - 1)) |
213 | 0 | fs1--; |
214 | 0 | if (fs1 < 0) |
215 | 0 | fs1 = 0; |
216 | 0 | if (fs1 < s1) { |
217 | 0 | s2 = XDL_MAX(s2 - (s1 - fs1), 0); |
218 | 0 | s1 = fs1; |
219 | | |
220 | | /* |
221 | | * Did we extend context upwards into an |
222 | | * ignored change? |
223 | | */ |
224 | 0 | while (xchp != xch && |
225 | 0 | xchp->i1 + xchp->chg1 <= s1 && |
226 | 0 | xchp->i2 + xchp->chg2 <= s2) |
227 | 0 | xchp = xchp->next; |
228 | | |
229 | | /* If so, show it after all. */ |
230 | 0 | if (xchp != xch) { |
231 | 0 | xch = xchp; |
232 | 0 | goto pre_context_calculation; |
233 | 0 | } |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | 0 | post_context_calculation: |
238 | 0 | lctx = xecfg->ctxlen; |
239 | 0 | lctx = XDL_MIN(lctx, (long)xe->xdf1.nrec - (xche->i1 + xche->chg1)); |
240 | 0 | lctx = XDL_MIN(lctx, (long)xe->xdf2.nrec - (xche->i2 + xche->chg2)); |
241 | |
|
242 | 0 | e1 = xche->i1 + xche->chg1 + lctx; |
243 | 0 | e2 = xche->i2 + xche->chg2 + lctx; |
244 | |
|
245 | 0 | if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { |
246 | 0 | long fe1 = get_func_line(xe, xecfg, NULL, |
247 | 0 | xche->i1 + xche->chg1, |
248 | 0 | (long)xe->xdf1.nrec); |
249 | 0 | while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1)) |
250 | 0 | fe1--; |
251 | 0 | if (fe1 < 0) |
252 | 0 | fe1 = (long)xe->xdf1.nrec; |
253 | 0 | if (fe1 > e1) { |
254 | 0 | e2 = XDL_MIN(e2 + (fe1 - e1), (long)xe->xdf2.nrec); |
255 | 0 | e1 = fe1; |
256 | 0 | } |
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 | 0 | if (xche->next) { |
264 | 0 | long l = XDL_MIN(xche->next->i1, |
265 | 0 | (long)xe->xdf1.nrec - 1); |
266 | 0 | if (l - xecfg->ctxlen <= e1 || |
267 | 0 | get_func_line(xe, xecfg, NULL, l, e1) < 0) { |
268 | 0 | xche = xche->next; |
269 | 0 | goto post_context_calculation; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | /* |
275 | | * Emit current hunk header. |
276 | | */ |
277 | | |
278 | 0 | if (xecfg->flags & XDL_EMIT_FUNCNAMES) { |
279 | 0 | get_func_line(xe, xecfg, &func_line, |
280 | 0 | s1 - 1, funclineprev); |
281 | 0 | funclineprev = s1 - 1; |
282 | 0 | } |
283 | 0 | if (!(xecfg->flags & XDL_EMIT_NO_HUNK_HDR) && |
284 | 0 | xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, |
285 | 0 | func_line.buf, func_line.len, ecb) < 0) |
286 | 0 | return -1; |
287 | | |
288 | | /* |
289 | | * Emit pre-context. |
290 | | */ |
291 | 0 | for (; s2 < xch->i2; s2++) |
292 | 0 | if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) |
293 | 0 | return -1; |
294 | | |
295 | 0 | for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) { |
296 | | /* |
297 | | * Merge previous with current change atom. |
298 | | */ |
299 | 0 | for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) |
300 | 0 | if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) |
301 | 0 | return -1; |
302 | | |
303 | | /* |
304 | | * Removes lines from the first file. |
305 | | */ |
306 | 0 | for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++) |
307 | 0 | if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0) |
308 | 0 | return -1; |
309 | | |
310 | | /* |
311 | | * Adds lines from the second file. |
312 | | */ |
313 | 0 | for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++) |
314 | 0 | if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0) |
315 | 0 | return -1; |
316 | | |
317 | 0 | if (xch == xche) |
318 | 0 | break; |
319 | 0 | s1 = xch->i1 + xch->chg1; |
320 | 0 | s2 = xch->i2 + xch->chg2; |
321 | 0 | } |
322 | | |
323 | | /* |
324 | | * Emit post-context. |
325 | | */ |
326 | 0 | for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++) |
327 | 0 | if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) |
328 | 0 | return -1; |
329 | 0 | } |
330 | | |
331 | 0 | return 0; |
332 | 0 | } |