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 
xdl_get_rec(xdfile_t * xdf,long ri,char const ** rec)25 static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) {
26 
27 	*rec = xdf->recs[ri]->ptr;
28 
29 	return xdf->recs[ri]->size;
30 }
31 
32 
xdl_emit_record(xdfile_t * xdf,long ri,char const * pre,xdemitcb_t * ecb)33 static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) {
34 	long size, psize = (long)strlen(pre);
35 	char const *rec;
36 
37 	size = xdl_get_rec(xdf, ri, &rec);
38 	if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) {
39 
40 		return -1;
41 	}
42 
43 	return 0;
44 }
45 
46 
47 /*
48  * Starting at the passed change atom, find the latest change atom to be included
49  * inside the differential hunk according to the specified configuration.
50  * Also advance xscr if the first changes must be discarded.
51  */
xdl_get_hunk(xdchange_t ** xscr,xdemitconf_t const * xecfg)52 xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg)
53 {
54 	xdchange_t *xch, *xchp, *lxch;
55 	long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen;
56 	long max_ignorable = xecfg->ctxlen;
57 	unsigned long ignored = 0; /* number of ignored blank lines */
58 
59 	/* remove ignorable changes that are too far before other changes */
60 	for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) {
61 		xch = xchp->next;
62 
63 		if (xch == NULL ||
64 		    xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable)
65 			*xscr = xch;
66 	}
67 
68 	if (*xscr == NULL)
69 		return NULL;
70 
71 	lxch = *xscr;
72 
73 	for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) {
74 		long distance = xch->i1 - (xchp->i1 + xchp->chg1);
75 		if (distance > max_common)
76 			break;
77 
78 		if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) {
79 			lxch = xch;
80 			ignored = 0;
81 		} else if (distance < max_ignorable && xch->ignore) {
82 			ignored += xch->chg2;
83 		} else if (lxch != xchp &&
84 			   xch->i1 + ignored - (lxch->i1 + lxch->chg1) > (unsigned long)max_common) {
85 			break;
86 		} else if (!xch->ignore) {
87 			lxch = xch;
88 			ignored = 0;
89 		} else {
90 			ignored += xch->chg2;
91 		}
92 	}
93 
94 	return lxch;
95 }
96 
97 
def_ff(const char * rec,long len,char * buf,long sz,void * priv)98 static long def_ff(const char *rec, long len, char *buf, long sz, void *priv)
99 {
100 	(void)priv;
101 
102 	if (len > 0 &&
103 			(isalpha((unsigned char)*rec) || /* identifier? */
104 			 *rec == '_' || /* also identifier? */
105 			 *rec == '$')) { /* identifiers from VMS and other esoterico */
106 		if (len > sz)
107 			len = sz;
108 		while (0 < len && isspace((unsigned char)rec[len - 1]))
109 			len--;
110 		memcpy(buf, rec, len);
111 		return len;
112 	}
113 	return -1;
114 }
115 
match_func_rec(xdfile_t * xdf,xdemitconf_t const * xecfg,long ri,char * buf,long sz)116 static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri,
117 			   char *buf, long sz)
118 {
119 	const char *rec;
120 	long len = xdl_get_rec(xdf, ri, &rec);
121 	if (!xecfg->find_func)
122 		return def_ff(rec, len, buf, sz, xecfg->find_func_priv);
123 	return xecfg->find_func(rec, len, buf, sz, xecfg->find_func_priv);
124 }
125 
is_func_rec(xdfile_t * xdf,xdemitconf_t const * xecfg,long ri)126 static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri)
127 {
128 	char dummy[1];
129 	return match_func_rec(xdf, xecfg, ri, dummy, sizeof(dummy)) >= 0;
130 }
131 
132 struct func_line {
133 	long len;
134 	char buf[80];
135 };
136 
get_func_line(xdfenv_t * xe,xdemitconf_t const * xecfg,struct func_line * func_line,long start,long limit)137 static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
138 			  struct func_line *func_line, long start, long limit)
139 {
140 	long l, size, step = (start > limit) ? -1 : 1;
141 	char *buf, dummy[1];
142 
143 	buf = func_line ? func_line->buf : dummy;
144 	size = func_line ? sizeof(func_line->buf) : sizeof(dummy);
145 
146 	for (l = start; l != limit && 0 <= l && l < xe->xdf1.nrec; l += step) {
147 		long len = match_func_rec(&xe->xdf1, xecfg, l, buf, size);
148 		if (len >= 0) {
149 			if (func_line)
150 				func_line->len = len;
151 			return l;
152 		}
153 	}
154 	return -1;
155 }
156 
is_empty_rec(xdfile_t * xdf,long ri)157 static int is_empty_rec(xdfile_t *xdf, long ri)
158 {
159 	const char *rec;
160 	long len = xdl_get_rec(xdf, ri, &rec);
161 
162 	while (len > 0 && XDL_ISSPACE(*rec)) {
163 		rec++;
164 		len--;
165 	}
166 	return !len;
167 }
168 
xdl_emit_diff(xdfenv_t * xe,xdchange_t * xscr,xdemitcb_t * ecb,xdemitconf_t const * xecfg)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 		xche = xdl_get_hunk(&xch, xecfg);
178 		if (!xch)
179 			break;
180 
181 		s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
182 		s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
183 
184 		if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) {
185 			long fs1, i1 = xch->i1;
186 
187 			/* Appended chunk? */
188 			if (i1 >= xe->xdf1.nrec) {
189 				long i2 = xch->i2;
190 
191 				/*
192 				 * We don't need additional context if
193 				 * a whole function was added.
194 				 */
195 				while (i2 < xe->xdf2.nrec) {
196 					if (is_func_rec(&xe->xdf2, xecfg, i2))
197 						goto post_context_calculation;
198 					i2++;
199 				}
200 
201 				/*
202 				 * Otherwise get more context from the
203 				 * pre-image.
204 				 */
205 				i1 = xe->xdf1.nrec - 1;
206 			}
207 
208 			fs1 = get_func_line(xe, xecfg, NULL, i1, -1);
209 			while (fs1 > 0 && !is_empty_rec(&xe->xdf1, fs1 - 1) &&
210 			       !is_func_rec(&xe->xdf1, xecfg, fs1 - 1))
211 				fs1--;
212 			if (fs1 < 0)
213 				fs1 = 0;
214 			if (fs1 < s1) {
215 				s2 -= s1 - fs1;
216 				s1 = fs1;
217 			}
218 		}
219 
220  post_context_calculation:
221 		lctx = xecfg->ctxlen;
222 		lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1));
223 		lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2));
224 
225 		e1 = xche->i1 + xche->chg1 + lctx;
226 		e2 = xche->i2 + xche->chg2 + lctx;
227 
228 		if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) {
229 			long fe1 = get_func_line(xe, xecfg, NULL,
230 						 xche->i1 + xche->chg1,
231 						 xe->xdf1.nrec);
232 			while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1))
233 				fe1--;
234 			if (fe1 < 0)
235 				fe1 = xe->xdf1.nrec;
236 			if (fe1 > e1) {
237 				e2 += fe1 - e1;
238 				e1 = fe1;
239 			}
240 
241 			/*
242 			 * Overlap with next change?  Then include it
243 			 * in the current hunk and start over to find
244 			 * its new end.
245 			 */
246 			if (xche->next) {
247 				long l = XDL_MIN(xche->next->i1,
248 						 xe->xdf1.nrec - 1);
249 				if (l - xecfg->ctxlen <= e1 ||
250 				    get_func_line(xe, xecfg, NULL, l, e1) < 0) {
251 					xche = xche->next;
252 					goto post_context_calculation;
253 				}
254 			}
255 		}
256 
257 		/*
258 		 * Emit current hunk header.
259 		 */
260 
261 		if (xecfg->flags & XDL_EMIT_FUNCNAMES) {
262 			get_func_line(xe, xecfg, &func_line,
263 				      s1 - 1, funclineprev);
264 			funclineprev = s1 - 1;
265 		}
266 		if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
267 				      func_line.buf, func_line.len, ecb) < 0)
268 			return -1;
269 
270 		/*
271 		 * Emit pre-context.
272 		 */
273 		for (; s2 < xch->i2; s2++)
274 			if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0)
275 				return -1;
276 
277 		for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) {
278 			/*
279 			 * Merge previous with current change atom.
280 			 */
281 			for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++)
282 				if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0)
283 					return -1;
284 
285 			/*
286 			 * Removes lines from the first file.
287 			 */
288 			for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++)
289 				if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0)
290 					return -1;
291 
292 			/*
293 			 * Adds lines from the second file.
294 			 */
295 			for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++)
296 				if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0)
297 					return -1;
298 
299 			if (xch == xche)
300 				break;
301 			s1 = xch->i1 + xch->chg1;
302 			s2 = xch->i2 + xch->chg2;
303 		}
304 
305 		/*
306 		 * Emit post-context.
307 		 */
308 		for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++)
309 			if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0)
310 				return -1;
311 	}
312 
313 	return 0;
314 }
315