analyze.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /* Analyze file differences for GNU DIFF.
  2. Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU DIFF.
  4. GNU DIFF is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU DIFF is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU DIFF; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* The basic algorithm is described in:
  16. "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
  17. Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
  18. see especially section 4.2, which describes the variation used below.
  19. Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE
  20. heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
  21. at the price of producing suboptimal output for large inputs with
  22. many differences.
  23. The basic algorithm was independently discovered as described in:
  24. "Algorithms for Approximate String Matching", E. Ukkonen,
  25. Information and Control Vol. 64, 1985, pp. 100-118. */
  26. #include "diff.h"
  27. #include "cmpbuf.h"
  28. extern int no_discards;
  29. static int *xvec, *yvec; /* Vectors being compared. */
  30. static int *fdiag; /* Vector, indexed by diagonal, containing
  31. 1 + the X coordinate of the point furthest
  32. along the given diagonal in the forward
  33. search of the edit matrix. */
  34. static int *bdiag; /* Vector, indexed by diagonal, containing
  35. the X coordinate of the point furthest
  36. along the given diagonal in the backward
  37. search of the edit matrix. */
  38. static int too_expensive; /* Edit scripts longer than this are too
  39. expensive to compute. */
  40. #define SNAKE_LIMIT 20 /* Snakes bigger than this are considered `big'. */
  41. struct partition
  42. {
  43. int xmid, ymid; /* Midpoints of this partition. */
  44. int lo_minimal; /* Nonzero if low half will be analyzed minimally. */
  45. int hi_minimal; /* Likewise for high half. */
  46. };
  47. static int diag PARAMS((int, int, int, int, int, struct partition *));
  48. static struct change *add_change PARAMS((int, int, int, int, struct change *));
  49. static struct change *build_reverse_script PARAMS((struct file_data const[]));
  50. static struct change *build_script PARAMS((struct file_data const[]));
  51. static void briefly_report PARAMS((int, struct file_data const[]));
  52. static void compareseq PARAMS((int, int, int, int, int));
  53. static void discard_confusing_lines PARAMS((struct file_data[]));
  54. static void shift_boundaries PARAMS((struct file_data[]));
  55. /* Find the midpoint of the shortest edit script for a specified
  56. portion of the two files.
  57. Scan from the beginnings of the files, and simultaneously from the ends,
  58. doing a breadth-first search through the space of edit-sequence.
  59. When the two searches meet, we have found the midpoint of the shortest
  60. edit sequence.
  61. If MINIMAL is nonzero, find the minimal edit script regardless
  62. of expense. Otherwise, if the search is too expensive, use
  63. heuristics to stop the search and report a suboptimal answer.
  64. Set PART->(XMID,YMID) to the midpoint (XMID,YMID). The diagonal number
  65. XMID - YMID equals the number of inserted lines minus the number
  66. of deleted lines (counting only lines before the midpoint).
  67. Return the approximate edit cost; this is the total number of
  68. lines inserted or deleted (counting only lines before the midpoint),
  69. unless a heuristic is used to terminate the search prematurely.
  70. Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script for the
  71. left half of the partition is known; similarly for PART->RIGHT_MINIMAL.
  72. This function assumes that the first lines of the specified portions
  73. of the two files do not match, and likewise that the last lines do not
  74. match. The caller must trim matching lines from the beginning and end
  75. of the portions it is going to specify.
  76. If we return the "wrong" partitions,
  77. the worst this can do is cause suboptimal diff output.
  78. It cannot cause incorrect diff output. */
  79. static int
  80. diag (xoff, xlim, yoff, ylim, minimal, part)
  81. int xoff, xlim, yoff, ylim, minimal;
  82. struct partition *part;
  83. {
  84. int *const fd = fdiag; /* Give the compiler a chance. */
  85. int *const bd = bdiag; /* Additional help for the compiler. */
  86. int const *const xv = xvec; /* Still more help for the compiler. */
  87. int const *const yv = yvec; /* And more and more . . . */
  88. int const dmin = xoff - ylim; /* Minimum valid diagonal. */
  89. int const dmax = xlim - yoff; /* Maximum valid diagonal. */
  90. int const fmid = xoff - yoff; /* Center diagonal of top-down search. */
  91. int const bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
  92. int fmin = fmid, fmax = fmid; /* Limits of top-down search. */
  93. int bmin = bmid, bmax = bmid; /* Limits of bottom-up search. */
  94. int c; /* Cost. */
  95. int odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd
  96. diagonal with respect to the northwest. */
  97. fd[fmid] = xoff;
  98. bd[bmid] = xlim;
  99. for (c = 1;; ++c)
  100. {
  101. int d; /* Active diagonal. */
  102. int big_snake = 0;
  103. /* Extend the top-down search by an edit step in each diagonal. */
  104. fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
  105. fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
  106. for (d = fmax; d >= fmin; d -= 2)
  107. {
  108. int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
  109. if (tlo >= thi)
  110. x = tlo + 1;
  111. else
  112. x = thi;
  113. oldx = x;
  114. y = x - d;
  115. while (x < xlim && y < ylim && xv[x] == yv[y])
  116. ++x, ++y;
  117. if (x - oldx > SNAKE_LIMIT)
  118. big_snake = 1;
  119. fd[d] = x;
  120. if (odd && bmin <= d && d <= bmax && bd[d] <= x)
  121. {
  122. part->xmid = x;
  123. part->ymid = y;
  124. part->lo_minimal = part->hi_minimal = 1;
  125. return 2 * c - 1;
  126. }
  127. }
  128. /* Similarly extend the bottom-up search. */
  129. bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
  130. bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
  131. for (d = bmax; d >= bmin; d -= 2)
  132. {
  133. int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
  134. if (tlo < thi)
  135. x = tlo;
  136. else
  137. x = thi - 1;
  138. oldx = x;
  139. y = x - d;
  140. while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
  141. --x, --y;
  142. if (oldx - x > SNAKE_LIMIT)
  143. big_snake = 1;
  144. bd[d] = x;
  145. if (!odd && fmin <= d && d <= fmax && x <= fd[d])
  146. {
  147. part->xmid = x;
  148. part->ymid = y;
  149. part->lo_minimal = part->hi_minimal = 1;
  150. return 2 * c;
  151. }
  152. }
  153. if (minimal)
  154. continue;
  155. /* Heuristic: check occasionally for a diagonal that has made
  156. lots of progress compared with the edit distance.
  157. If we have any such, find the one that has made the most
  158. progress and return it as if it had succeeded.
  159. With this heuristic, for files with a constant small density
  160. of changes, the algorithm is linear in the file size. */
  161. if (c > 200 && big_snake && heuristic)
  162. {
  163. int best;
  164. best = 0;
  165. for (d = fmax; d >= fmin; d -= 2)
  166. {
  167. int dd = d - fmid;
  168. int x = fd[d];
  169. int y = x - d;
  170. int v = (x - xoff) * 2 - dd;
  171. if (v > 12 * (c + (dd < 0 ? -dd : dd)))
  172. {
  173. if (v > best
  174. && xoff + SNAKE_LIMIT <= x && x < xlim
  175. && yoff + SNAKE_LIMIT <= y && y < ylim)
  176. {
  177. /* We have a good enough best diagonal;
  178. now insist that it end with a significant snake. */
  179. int k;
  180. for (k = 1; xv[x - k] == yv[y - k]; k++)
  181. if (k == SNAKE_LIMIT)
  182. {
  183. best = v;
  184. part->xmid = x;
  185. part->ymid = y;
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. if (best > 0)
  192. {
  193. part->lo_minimal = 1;
  194. part->hi_minimal = 0;
  195. return 2 * c - 1;
  196. }
  197. best = 0;
  198. for (d = bmax; d >= bmin; d -= 2)
  199. {
  200. int dd = d - bmid;
  201. int x = bd[d];
  202. int y = x - d;
  203. int v = (xlim - x) * 2 + dd;
  204. if (v > 12 * (c + (dd < 0 ? -dd : dd)))
  205. {
  206. if (v > best
  207. && xoff < x && x <= xlim - SNAKE_LIMIT
  208. && yoff < y && y <= ylim - SNAKE_LIMIT)
  209. {
  210. /* We have a good enough best diagonal;
  211. now insist that it end with a significant snake. */
  212. int k;
  213. for (k = 0; xv[x + k] == yv[y + k]; k++)
  214. if (k == SNAKE_LIMIT - 1)
  215. {
  216. best = v;
  217. part->xmid = x;
  218. part->ymid = y;
  219. break;
  220. }
  221. }
  222. }
  223. }
  224. if (best > 0)
  225. {
  226. part->lo_minimal = 0;
  227. part->hi_minimal = 1;
  228. return 2 * c - 1;
  229. }
  230. }
  231. /* Heuristic: if we've gone well beyond the call of duty,
  232. give up and report halfway between our best results so far. */
  233. if (c >= too_expensive)
  234. {
  235. int fxybest, fxbest;
  236. int bxybest, bxbest;
  237. fxbest = bxbest = 0; /* Pacify `gcc -Wall'. */
  238. /* Find forward diagonal that maximizes X + Y. */
  239. fxybest = -1;
  240. for (d = fmax; d >= fmin; d -= 2)
  241. {
  242. int x = min (fd[d], xlim);
  243. int y = x - d;
  244. if (ylim < y)
  245. x = ylim + d, y = ylim;
  246. if (fxybest < x + y)
  247. {
  248. fxybest = x + y;
  249. fxbest = x;
  250. }
  251. }
  252. /* Find backward diagonal that minimizes X + Y. */
  253. bxybest = INT_MAX;
  254. for (d = bmax; d >= bmin; d -= 2)
  255. {
  256. int x = max (xoff, bd[d]);
  257. int y = x - d;
  258. if (y < yoff)
  259. x = yoff + d, y = yoff;
  260. if (x + y < bxybest)
  261. {
  262. bxybest = x + y;
  263. bxbest = x;
  264. }
  265. }
  266. /* Use the better of the two diagonals. */
  267. if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
  268. {
  269. part->xmid = fxbest;
  270. part->ymid = fxybest - fxbest;
  271. part->lo_minimal = 1;
  272. part->hi_minimal = 0;
  273. }
  274. else
  275. {
  276. part->xmid = bxbest;
  277. part->ymid = bxybest - bxbest;
  278. part->lo_minimal = 0;
  279. part->hi_minimal = 1;
  280. }
  281. return 2 * c - 1;
  282. }
  283. }
  284. }
  285. /* Compare in detail contiguous subsequences of the two files
  286. which are known, as a whole, to match each other.
  287. The results are recorded in the vectors files[N].changed_flag, by
  288. storing a 1 in the element for each line that is an insertion or deletion.
  289. The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
  290. Note that XLIM, YLIM are exclusive bounds.
  291. All line numbers are origin-0 and discarded lines are not counted.
  292. If MINIMAL is nonzero, find a minimal difference no matter how
  293. expensive it is. */
  294. static void
  295. compareseq (xoff, xlim, yoff, ylim, minimal)
  296. int xoff, xlim, yoff, ylim, minimal;
  297. {
  298. int * const xv = xvec; /* Help the compiler. */
  299. int * const yv = yvec;
  300. /* Slide down the bottom initial diagonal. */
  301. while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff])
  302. ++xoff, ++yoff;
  303. /* Slide up the top initial diagonal. */
  304. while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1])
  305. --xlim, --ylim;
  306. /* Handle simple cases. */
  307. if (xoff == xlim)
  308. while (yoff < ylim)
  309. files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
  310. else if (yoff == ylim)
  311. while (xoff < xlim)
  312. files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
  313. else
  314. {
  315. int c;
  316. struct partition part;
  317. /* Find a point of correspondence in the middle of the files. */
  318. c = diag (xoff, xlim, yoff, ylim, minimal, &part);
  319. if (c == 1)
  320. {
  321. /* This should be impossible, because it implies that
  322. one of the two subsequences is empty,
  323. and that case was handled above without calling `diag'.
  324. Let's verify that this is true. */
  325. abort ();
  326. #if 0
  327. /* The two subsequences differ by a single insert or delete;
  328. record it and we are done. */
  329. if (part.xmid - part.ymid < xoff - yoff)
  330. files[1].changed_flag[files[1].realindexes[part.ymid - 1]] = 1;
  331. else
  332. files[0].changed_flag[files[0].realindexes[part.xmid]] = 1;
  333. #endif
  334. }
  335. else
  336. {
  337. /* Use the partitions to split this problem into subproblems. */
  338. compareseq (xoff, part.xmid, yoff, part.ymid, part.lo_minimal);
  339. compareseq (part.xmid, xlim, part.ymid, ylim, part.hi_minimal);
  340. }
  341. }
  342. }
  343. /* Discard lines from one file that have no matches in the other file.
  344. A line which is discarded will not be considered by the actual
  345. comparison algorithm; it will be as if that line were not in the file.
  346. The file's `realindexes' table maps virtual line numbers
  347. (which don't count the discarded lines) into real line numbers;
  348. this is how the actual comparison algorithm produces results
  349. that are comprehensible when the discarded lines are counted.
  350. When we discard a line, we also mark it as a deletion or insertion
  351. so that it will be printed in the output. */
  352. static void
  353. discard_confusing_lines (filevec)
  354. struct file_data filevec[];
  355. {
  356. unsigned int f, i;
  357. char *discarded[2];
  358. int *equiv_count[2];
  359. int *p;
  360. /* Allocate our results. */
  361. p = (int *) xmalloc ((filevec[0].buffered_lines + filevec[1].buffered_lines)
  362. * (2 * sizeof (int)));
  363. for (f = 0; f < 2; f++)
  364. {
  365. filevec[f].undiscarded = p; p += filevec[f].buffered_lines;
  366. filevec[f].realindexes = p; p += filevec[f].buffered_lines;
  367. }
  368. /* Set up equiv_count[F][I] as the number of lines in file F
  369. that fall in equivalence class I. */
  370. p = (int *) xmalloc (filevec[0].equiv_max * (2 * sizeof (int)));
  371. equiv_count[0] = p;
  372. equiv_count[1] = p + filevec[0].equiv_max;
  373. bzero (p, filevec[0].equiv_max * (2 * sizeof (int)));
  374. for (i = 0; i < filevec[0].buffered_lines; ++i)
  375. ++equiv_count[0][filevec[0].equivs[i]];
  376. for (i = 0; i < filevec[1].buffered_lines; ++i)
  377. ++equiv_count[1][filevec[1].equivs[i]];
  378. /* Set up tables of which lines are going to be discarded. */
  379. discarded[0] = xmalloc (sizeof (char)
  380. * (filevec[0].buffered_lines
  381. + filevec[1].buffered_lines));
  382. discarded[1] = discarded[0] + filevec[0].buffered_lines;
  383. bzero (discarded[0], sizeof (char) * (filevec[0].buffered_lines
  384. + filevec[1].buffered_lines));
  385. /* Mark to be discarded each line that matches no line of the other file.
  386. If a line matches many lines, mark it as provisionally discardable. */
  387. for (f = 0; f < 2; f++)
  388. {
  389. unsigned int end = filevec[f].buffered_lines;
  390. char *discards = discarded[f];
  391. int *counts = equiv_count[1 - f];
  392. int *equivs = filevec[f].equivs;
  393. unsigned int many = 5;
  394. unsigned int tem = end / 64;
  395. /* Multiply MANY by approximate square root of number of lines.
  396. That is the threshold for provisionally discardable lines. */
  397. while ((tem = tem >> 2) > 0)
  398. many *= 2;
  399. for (i = 0; i < end; i++)
  400. {
  401. int nmatch;
  402. if (equivs[i] == 0)
  403. continue;
  404. nmatch = counts[equivs[i]];
  405. if (nmatch == 0)
  406. discards[i] = 1;
  407. else if (nmatch > many)
  408. discards[i] = 2;
  409. }
  410. }
  411. /* Don't really discard the provisional lines except when they occur
  412. in a run of discardables, with nonprovisionals at the beginning
  413. and end. */
  414. for (f = 0; f < 2; f++)
  415. {
  416. unsigned int end = filevec[f].buffered_lines;
  417. register char *discards = discarded[f];
  418. for (i = 0; i < end; i++)
  419. {
  420. /* Cancel provisional discards not in middle of run of discards. */
  421. if (discards[i] == 2)
  422. discards[i] = 0;
  423. else if (discards[i] != 0)
  424. {
  425. /* We have found a nonprovisional discard. */
  426. register int j;
  427. unsigned int length;
  428. unsigned int provisional = 0;
  429. /* Find end of this run of discardable lines.
  430. Count how many are provisionally discardable. */
  431. for (j = i; j < end; j++)
  432. {
  433. if (discards[j] == 0)
  434. break;
  435. if (discards[j] == 2)
  436. ++provisional;
  437. }
  438. /* Cancel provisional discards at end, and shrink the run. */
  439. while (j > i && discards[j - 1] == 2)
  440. discards[--j] = 0, --provisional;
  441. /* Now we have the length of a run of discardable lines
  442. whose first and last are not provisional. */
  443. length = j - i;
  444. /* If 1/4 of the lines in the run are provisional,
  445. cancel discarding of all provisional lines in the run. */
  446. if (provisional * 4 > length)
  447. {
  448. while (j > i)
  449. if (discards[--j] == 2)
  450. discards[j] = 0;
  451. }
  452. else
  453. {
  454. register unsigned int consec;
  455. unsigned int minimum = 1;
  456. unsigned int tem = length / 4;
  457. /* MINIMUM is approximate square root of LENGTH/4.
  458. A subrun of two or more provisionals can stand
  459. when LENGTH is at least 16.
  460. A subrun of 4 or more can stand when LENGTH >= 64. */
  461. while ((tem = tem >> 2) > 0)
  462. minimum *= 2;
  463. minimum++;
  464. /* Cancel any subrun of MINIMUM or more provisionals
  465. within the larger run. */
  466. for (j = 0, consec = 0; j < length; j++)
  467. if (discards[i + j] != 2)
  468. consec = 0;
  469. else if (minimum == ++consec)
  470. /* Back up to start of subrun, to cancel it all. */
  471. j -= consec;
  472. else if (minimum < consec)
  473. discards[i + j] = 0;
  474. /* Scan from beginning of run
  475. until we find 3 or more nonprovisionals in a row
  476. or until the first nonprovisional at least 8 lines in.
  477. Until that point, cancel any provisionals. */
  478. for (j = 0, consec = 0; j < length; j++)
  479. {
  480. if (j >= 8 && discards[i + j] == 1)
  481. break;
  482. if (discards[i + j] == 2)
  483. consec = 0, discards[i + j] = 0;
  484. else if (discards[i + j] == 0)
  485. consec = 0;
  486. else
  487. consec++;
  488. if (consec == 3)
  489. break;
  490. }
  491. /* I advances to the last line of the run. */
  492. i += length - 1;
  493. /* Same thing, from end. */
  494. for (j = 0, consec = 0; j < length; j++)
  495. {
  496. if (j >= 8 && discards[i - j] == 1)
  497. break;
  498. if (discards[i - j] == 2)
  499. consec = 0, discards[i - j] = 0;
  500. else if (discards[i - j] == 0)
  501. consec = 0;
  502. else
  503. consec++;
  504. if (consec == 3)
  505. break;
  506. }
  507. }
  508. }
  509. }
  510. }
  511. /* Actually discard the lines. */
  512. for (f = 0; f < 2; f++)
  513. {
  514. char *discards = discarded[f];
  515. unsigned int end = filevec[f].buffered_lines;
  516. unsigned int j = 0;
  517. for (i = 0; i < end; ++i)
  518. if (no_discards || discards[i] == 0)
  519. {
  520. filevec[f].undiscarded[j] = filevec[f].equivs[i];
  521. filevec[f].realindexes[j++] = i;
  522. }
  523. else
  524. filevec[f].changed_flag[i] = 1;
  525. filevec[f].nondiscarded_lines = j;
  526. }
  527. free (discarded[0]);
  528. free (equiv_count[0]);
  529. }
  530. /* Adjust inserts/deletes of identical lines to join changes
  531. as much as possible.
  532. We do something when a run of changed lines include a
  533. line at one end and have an excluded, identical line at the other.
  534. We are free to choose which identical line is included.
  535. `compareseq' usually chooses the one at the beginning,
  536. but usually it is cleaner to consider the following identical line
  537. to be the "change". */
  538. int inhibit;
  539. static void
  540. shift_boundaries (filevec)
  541. struct file_data filevec[];
  542. {
  543. int f;
  544. if (inhibit)
  545. return;
  546. for (f = 0; f < 2; f++)
  547. {
  548. char *changed = filevec[f].changed_flag;
  549. char const *other_changed = filevec[1-f].changed_flag;
  550. int const *equivs = filevec[f].equivs;
  551. int i = 0;
  552. int j = 0;
  553. int i_end = filevec[f].buffered_lines;
  554. while (1)
  555. {
  556. int runlength, start, corresponding;
  557. /* Scan forwards to find beginning of another run of changes.
  558. Also keep track of the corresponding point in the other file. */
  559. while (i < i_end && changed[i] == 0)
  560. {
  561. while (other_changed[j++])
  562. continue;
  563. i++;
  564. }
  565. if (i == i_end)
  566. break;
  567. start = i;
  568. /* Find the end of this run of changes. */
  569. while (changed[++i])
  570. continue;
  571. while (other_changed[j])
  572. j++;
  573. do
  574. {
  575. /* Record the length of this run of changes, so that
  576. we can later determine whether the run has grown. */
  577. runlength = i - start;
  578. /* Move the changed region back, so long as the
  579. previous unchanged line matches the last changed one.
  580. This merges with previous changed regions. */
  581. while (start && equivs[start - 1] == equivs[i - 1])
  582. {
  583. changed[--start] = 1;
  584. changed[--i] = 0;
  585. while (changed[start - 1])
  586. start--;
  587. while (other_changed[--j])
  588. continue;
  589. }
  590. /* Set CORRESPONDING to the end of the changed run, at the last
  591. point where it corresponds to a changed run in the other file.
  592. CORRESPONDING == I_END means no such point has been found. */
  593. corresponding = other_changed[j - 1] ? i : i_end;
  594. /* Move the changed region forward, so long as the
  595. first changed line matches the following unchanged one.
  596. This merges with following changed regions.
  597. Do this second, so that if there are no merges,
  598. the changed region is moved forward as far as possible. */
  599. while (i != i_end && equivs[start] == equivs[i])
  600. {
  601. changed[start++] = 0;
  602. changed[i++] = 1;
  603. while (changed[i])
  604. i++;
  605. while (other_changed[++j])
  606. corresponding = i;
  607. }
  608. }
  609. while (runlength != i - start);
  610. /* If possible, move the fully-merged run of changes
  611. back to a corresponding run in the other file. */
  612. while (corresponding < i)
  613. {
  614. changed[--start] = 1;
  615. changed[--i] = 0;
  616. while (other_changed[--j])
  617. continue;
  618. }
  619. }
  620. }
  621. }
  622. /* Cons an additional entry onto the front of an edit script OLD.
  623. LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  624. DELETED is the number of lines deleted here from file 0.
  625. INSERTED is the number of lines inserted here in file 1.
  626. If DELETED is 0 then LINE0 is the number of the line before
  627. which the insertion was done; vice versa for INSERTED and LINE1. */
  628. static struct change *
  629. add_change (line0, line1, deleted, inserted, old)
  630. int line0, line1, deleted, inserted;
  631. struct change *old;
  632. {
  633. struct change *new = (struct change *) xmalloc (sizeof (struct change));
  634. new->line0 = line0;
  635. new->line1 = line1;
  636. new->inserted = inserted;
  637. new->deleted = deleted;
  638. new->link = old;
  639. return new;
  640. }
  641. /* Scan the tables of which lines are inserted and deleted,
  642. producing an edit script in reverse order. */
  643. static struct change *
  644. build_reverse_script (filevec)
  645. struct file_data const filevec[];
  646. {
  647. struct change *script = 0;
  648. char *changed0 = filevec[0].changed_flag;
  649. char *changed1 = filevec[1].changed_flag;
  650. int len0 = filevec[0].buffered_lines;
  651. int len1 = filevec[1].buffered_lines;
  652. /* Note that changedN[len0] does exist, and contains 0. */
  653. int i0 = 0, i1 = 0;
  654. while (i0 < len0 || i1 < len1)
  655. {
  656. if (changed0[i0] || changed1[i1])
  657. {
  658. int line0 = i0, line1 = i1;
  659. /* Find # lines changed here in each file. */
  660. while (changed0[i0]) ++i0;
  661. while (changed1[i1]) ++i1;
  662. /* Record this change. */
  663. script = add_change (line0, line1, i0 - line0, i1 - line1, script);
  664. }
  665. /* We have reached lines in the two files that match each other. */
  666. i0++, i1++;
  667. }
  668. return script;
  669. }
  670. /* Scan the tables of which lines are inserted and deleted,
  671. producing an edit script in forward order. */
  672. static struct change *
  673. build_script (filevec)
  674. struct file_data const filevec[];
  675. {
  676. struct change *script = 0;
  677. char *changed0 = filevec[0].changed_flag;
  678. char *changed1 = filevec[1].changed_flag;
  679. int i0 = filevec[0].buffered_lines, i1 = filevec[1].buffered_lines;
  680. /* Note that changedN[-1] does exist, and contains 0. */
  681. while (i0 >= 0 || i1 >= 0)
  682. {
  683. if (changed0[i0 - 1] || changed1[i1 - 1])
  684. {
  685. int line0 = i0, line1 = i1;
  686. /* Find # lines changed here in each file. */
  687. while (changed0[i0 - 1]) --i0;
  688. while (changed1[i1 - 1]) --i1;
  689. /* Record this change. */
  690. script = add_change (i0, i1, line0 - i0, line1 - i1, script);
  691. }
  692. /* We have reached lines in the two files that match each other. */
  693. i0--, i1--;
  694. }
  695. return script;
  696. }
  697. /* If CHANGES, briefly report that two files differed. */
  698. static void
  699. briefly_report (changes, filevec)
  700. int changes;
  701. struct file_data const filevec[];
  702. {
  703. if (changes)
  704. message (no_details_flag ? "Files %s and %s differ\n"
  705. : "Binary files %s and %s differ\n",
  706. filevec[0].name, filevec[1].name);
  707. }
  708. /* Report the differences of two files. DEPTH is the current directory
  709. depth. */
  710. int
  711. diff_2_files (filevec, depth)
  712. struct file_data filevec[];
  713. int depth;
  714. {
  715. int diags;
  716. int i;
  717. struct change *e, *p;
  718. struct change *script;
  719. int changes;
  720. /* If we have detected that either file is binary,
  721. compare the two files as binary. This can happen
  722. only when the first chunk is read.
  723. Also, --brief without any --ignore-* options means
  724. we can speed things up by treating the files as binary. */
  725. if (read_files (filevec, no_details_flag & ~ignore_some_changes))
  726. {
  727. /* Files with different lengths must be different. */
  728. if (filevec[0].stat.st_size != filevec[1].stat.st_size
  729. && (filevec[0].desc < 0 || S_ISREG (filevec[0].stat.st_mode))
  730. && (filevec[1].desc < 0 || S_ISREG (filevec[1].stat.st_mode)))
  731. changes = 1;
  732. /* Standard input equals itself. */
  733. else if (filevec[0].desc == filevec[1].desc)
  734. changes = 0;
  735. else
  736. /* Scan both files, a buffer at a time, looking for a difference. */
  737. {
  738. /* Allocate same-sized buffers for both files. */
  739. size_t buffer_size = buffer_lcm (STAT_BLOCKSIZE (filevec[0].stat),
  740. STAT_BLOCKSIZE (filevec[1].stat));
  741. for (i = 0; i < 2; i++)
  742. filevec[i].buffer = xrealloc (filevec[i].buffer, buffer_size);
  743. for (;; filevec[0].buffered_chars = filevec[1].buffered_chars = 0)
  744. {
  745. /* Read a buffer's worth from both files. */
  746. for (i = 0; i < 2; i++)
  747. if (0 <= filevec[i].desc)
  748. while (filevec[i].buffered_chars != buffer_size)
  749. {
  750. int r = read (filevec[i].desc,
  751. filevec[i].buffer
  752. + filevec[i].buffered_chars,
  753. buffer_size - filevec[i].buffered_chars);
  754. if (r == 0)
  755. break;
  756. if (r < 0)
  757. pfatal_with_name (filevec[i].name);
  758. filevec[i].buffered_chars += r;
  759. }
  760. /* If the buffers differ, the files differ. */
  761. if (filevec[0].buffered_chars != filevec[1].buffered_chars
  762. || (filevec[0].buffered_chars != 0
  763. && memcmp (filevec[0].buffer,
  764. filevec[1].buffer,
  765. filevec[0].buffered_chars) != 0))
  766. {
  767. changes = 1;
  768. break;
  769. }
  770. /* If we reach end of file, the files are the same. */
  771. if (filevec[0].buffered_chars != buffer_size)
  772. {
  773. changes = 0;
  774. break;
  775. }
  776. }
  777. }
  778. briefly_report (changes, filevec);
  779. }
  780. else
  781. {
  782. /* Allocate vectors for the results of comparison:
  783. a flag for each line of each file, saying whether that line
  784. is an insertion or deletion.
  785. Allocate an extra element, always zero, at each end of each vector. */
  786. size_t s = filevec[0].buffered_lines + filevec[1].buffered_lines + 4;
  787. filevec[0].changed_flag = xmalloc (s);
  788. bzero (filevec[0].changed_flag, s);
  789. filevec[0].changed_flag++;
  790. filevec[1].changed_flag = filevec[0].changed_flag
  791. + filevec[0].buffered_lines + 2;
  792. /* Some lines are obviously insertions or deletions
  793. because they don't match anything. Detect them now, and
  794. avoid even thinking about them in the main comparison algorithm. */
  795. discard_confusing_lines (filevec);
  796. /* Now do the main comparison algorithm, considering just the
  797. undiscarded lines. */
  798. xvec = filevec[0].undiscarded;
  799. yvec = filevec[1].undiscarded;
  800. diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
  801. fdiag = (int *) xmalloc (diags * (2 * sizeof (int)));
  802. bdiag = fdiag + diags;
  803. fdiag += filevec[1].nondiscarded_lines + 1;
  804. bdiag += filevec[1].nondiscarded_lines + 1;
  805. /* Set TOO_EXPENSIVE to be approximate square root of input size,
  806. bounded below by 256. */
  807. too_expensive = 1;
  808. for (i = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines;
  809. i != 0; i >>= 2)
  810. too_expensive <<= 1;
  811. too_expensive = max (256, too_expensive);
  812. files[0] = filevec[0];
  813. files[1] = filevec[1];
  814. compareseq (0, filevec[0].nondiscarded_lines,
  815. 0, filevec[1].nondiscarded_lines, no_discards);
  816. free (fdiag - (filevec[1].nondiscarded_lines + 1));
  817. /* Modify the results slightly to make them prettier
  818. in cases where that can validly be done. */
  819. shift_boundaries (filevec);
  820. /* Get the results of comparison in the form of a chain
  821. of `struct change's -- an edit script. */
  822. if (output_style == OUTPUT_ED)
  823. script = build_reverse_script (filevec);
  824. else
  825. script = build_script (filevec);
  826. /* Set CHANGES if we had any diffs.
  827. If some changes are ignored, we must scan the script to decide. */
  828. if (ignore_blank_lines_flag || ignore_regexp_list)
  829. {
  830. struct change *next = script;
  831. changes = 0;
  832. while (next && changes == 0)
  833. {
  834. struct change *this, *end;
  835. int first0, last0, first1, last1, deletes, inserts;
  836. /* Find a set of changes that belong together. */
  837. this = next;
  838. end = find_change (next);
  839. /* Disconnect them from the rest of the changes, making them
  840. a hunk, and remember the rest for next iteration. */
  841. next = end->link;
  842. end->link = 0;
  843. /* Determine whether this hunk is really a difference. */
  844. analyze_hunk (this, &first0, &last0, &first1, &last1,
  845. &deletes, &inserts);
  846. /* Reconnect the script so it will all be freed properly. */
  847. end->link = next;
  848. if (deletes || inserts)
  849. changes = 1;
  850. }
  851. }
  852. else
  853. changes = (script != 0);
  854. if (no_details_flag)
  855. briefly_report (changes, filevec);
  856. else
  857. {
  858. if (changes || ! no_diff_means_no_output)
  859. {
  860. /* Record info for starting up output,
  861. to be used if and when we have some output to print. */
  862. setup_output (files[0].name, files[1].name, depth);
  863. switch (output_style)
  864. {
  865. case OUTPUT_CONTEXT:
  866. print_context_script (script, 0);
  867. break;
  868. case OUTPUT_UNIFIED:
  869. print_context_script (script, 1);
  870. break;
  871. case OUTPUT_ED:
  872. print_ed_script (script);
  873. break;
  874. case OUTPUT_FORWARD_ED:
  875. pr_forward_ed_script (script);
  876. break;
  877. case OUTPUT_RCS:
  878. print_rcs_script (script);
  879. break;
  880. case OUTPUT_NORMAL:
  881. print_normal_script (script);
  882. break;
  883. case OUTPUT_IFDEF:
  884. print_ifdef_script (script);
  885. break;
  886. case OUTPUT_SDIFF:
  887. print_sdiff_script (script);
  888. }
  889. finish_output ();
  890. }
  891. }
  892. free (filevec[0].undiscarded);
  893. free (filevec[0].changed_flag - 1);
  894. for (i = 1; i >= 0; --i)
  895. free (filevec[i].equivs);
  896. for (i = 0; i < 2; ++i)
  897. free (filevec[i].linbuf + filevec[i].linbuf_base);
  898. for (e = script; e; e = p)
  899. {
  900. p = e->link;
  901. free (e);
  902. }
  903. if (! ROBUST_OUTPUT_STYLE (output_style))
  904. for (i = 0; i < 2; ++i)
  905. if (filevec[i].missing_newline)
  906. {
  907. error ("No newline at end of file %s", filevec[i].name, "");
  908. changes = 2;
  909. }
  910. }
  911. if (filevec[0].buffer != filevec[1].buffer)
  912. free (filevec[0].buffer);
  913. free (filevec[1].buffer);
  914. return changes;
  915. }