/contrib/cvs/diff/context.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 462 lines · 288 code · 71 blank · 103 comment · 51 complexity · fa4cf20ae03e2c865dbd3db3907f9c8c MD5 · raw file

  1. /* Context-format output routines for GNU DIFF.
  2. Copyright (C) 1988,1989,1991,1992,1993,1994,1998 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. */
  13. #include "diff.h"
  14. static struct change *find_hunk PARAMS((struct change *));
  15. static void find_function PARAMS((struct file_data const *, int, char const **, size_t *));
  16. static void mark_ignorable PARAMS((struct change *));
  17. static void pr_context_hunk PARAMS((struct change *));
  18. static void pr_unidiff_hunk PARAMS((struct change *));
  19. static void print_context_label PARAMS ((char const *, struct file_data *, char const *));
  20. static void print_context_number_range PARAMS((struct file_data const *, int, int));
  21. static void print_unidiff_number_range PARAMS((struct file_data const *, int, int));
  22. /* Last place find_function started searching from. */
  23. static int find_function_last_search;
  24. /* The value find_function returned when it started searching there. */
  25. static int find_function_last_match;
  26. /* Print a label for a context diff, with a file name and date or a label. */
  27. static void
  28. print_context_label (mark, inf, label)
  29. char const *mark;
  30. struct file_data *inf;
  31. char const *label;
  32. {
  33. if (label)
  34. printf_output ("%s %s\n", mark, label);
  35. else
  36. {
  37. char const *ct = ctime (&inf->stat.st_mtime);
  38. if (!ct)
  39. ct = "?\n";
  40. /* See Posix.2 section 4.17.6.1.4 for this format. */
  41. printf_output ("%s %s\t%s", mark, inf->name, ct);
  42. }
  43. }
  44. /* Print a header for a context diff, with the file names and dates. */
  45. void
  46. print_context_header (inf, unidiff_flag)
  47. struct file_data inf[];
  48. int unidiff_flag;
  49. {
  50. if (unidiff_flag)
  51. {
  52. print_context_label ("---", &inf[0], file_label[0]);
  53. print_context_label ("+++", &inf[1], file_label[1]);
  54. }
  55. else
  56. {
  57. print_context_label ("***", &inf[0], file_label[0]);
  58. print_context_label ("---", &inf[1], file_label[1]);
  59. }
  60. }
  61. /* Print an edit script in context format. */
  62. void
  63. print_context_script (script, unidiff_flag)
  64. struct change *script;
  65. int unidiff_flag;
  66. {
  67. if (ignore_blank_lines_flag || ignore_regexp_list)
  68. mark_ignorable (script);
  69. else
  70. {
  71. struct change *e;
  72. for (e = script; e; e = e->link)
  73. e->ignore = 0;
  74. }
  75. find_function_last_search = - files[0].prefix_lines;
  76. find_function_last_match = find_function_last_search - 1;
  77. if (unidiff_flag)
  78. print_script (script, find_hunk, pr_unidiff_hunk);
  79. else
  80. print_script (script, find_hunk, pr_context_hunk);
  81. }
  82. /* Print a pair of line numbers with a comma, translated for file FILE.
  83. If the second number is not greater, use the first in place of it.
  84. Args A and B are internal line numbers.
  85. We print the translated (real) line numbers. */
  86. static void
  87. print_context_number_range (file, a, b)
  88. struct file_data const *file;
  89. int a, b;
  90. {
  91. int trans_a, trans_b;
  92. translate_range (file, a, b, &trans_a, &trans_b);
  93. /* Note: we can have B < A in the case of a range of no lines.
  94. In this case, we should print the line number before the range,
  95. which is B. */
  96. if (trans_b > trans_a)
  97. printf_output ("%d,%d", trans_a, trans_b);
  98. else
  99. printf_output ("%d", trans_b);
  100. }
  101. /* Print a portion of an edit script in context format.
  102. HUNK is the beginning of the portion to be printed.
  103. The end is marked by a `link' that has been nulled out.
  104. Prints out lines from both files, and precedes each
  105. line with the appropriate flag-character. */
  106. static void
  107. pr_context_hunk (hunk)
  108. struct change *hunk;
  109. {
  110. int first0, last0, first1, last1, show_from, show_to, i;
  111. struct change *next;
  112. char const *prefix;
  113. char const *function;
  114. size_t function_length;
  115. /* Determine range of line numbers involved in each file. */
  116. analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  117. if (!show_from && !show_to)
  118. return;
  119. /* Include a context's width before and after. */
  120. i = - files[0].prefix_lines;
  121. first0 = max (first0 - context, i);
  122. first1 = max (first1 - context, i);
  123. last0 = min (last0 + context, files[0].valid_lines - 1);
  124. last1 = min (last1 + context, files[1].valid_lines - 1);
  125. /* If desired, find the preceding function definition line in file 0. */
  126. function = 0;
  127. if (function_regexp_list)
  128. find_function (&files[0], first0, &function, &function_length);
  129. begin_output ();
  130. /* If we looked for and found a function this is part of,
  131. include its name in the header of the diff section. */
  132. printf_output ("***************");
  133. if (function)
  134. {
  135. printf_output (" ");
  136. write_output (function, min (function_length - 1, 40));
  137. }
  138. printf_output ("\n*** ");
  139. print_context_number_range (&files[0], first0, last0);
  140. printf_output (" ****\n");
  141. if (show_from)
  142. {
  143. next = hunk;
  144. for (i = first0; i <= last0; i++)
  145. {
  146. /* Skip past changes that apply (in file 0)
  147. only to lines before line I. */
  148. while (next && next->line0 + next->deleted <= i)
  149. next = next->link;
  150. /* Compute the marking for line I. */
  151. prefix = " ";
  152. if (next && next->line0 <= i)
  153. /* The change NEXT covers this line.
  154. If lines were inserted here in file 1, this is "changed".
  155. Otherwise it is "deleted". */
  156. prefix = (next->inserted > 0 ? "!" : "-");
  157. print_1_line (prefix, &files[0].linbuf[i]);
  158. }
  159. }
  160. printf_output ("--- ");
  161. print_context_number_range (&files[1], first1, last1);
  162. printf_output (" ----\n");
  163. if (show_to)
  164. {
  165. next = hunk;
  166. for (i = first1; i <= last1; i++)
  167. {
  168. /* Skip past changes that apply (in file 1)
  169. only to lines before line I. */
  170. while (next && next->line1 + next->inserted <= i)
  171. next = next->link;
  172. /* Compute the marking for line I. */
  173. prefix = " ";
  174. if (next && next->line1 <= i)
  175. /* The change NEXT covers this line.
  176. If lines were deleted here in file 0, this is "changed".
  177. Otherwise it is "inserted". */
  178. prefix = (next->deleted > 0 ? "!" : "+");
  179. print_1_line (prefix, &files[1].linbuf[i]);
  180. }
  181. }
  182. }
  183. /* Print a pair of line numbers with a comma, translated for file FILE.
  184. If the second number is smaller, use the first in place of it.
  185. If the numbers are equal, print just one number.
  186. Args A and B are internal line numbers.
  187. We print the translated (real) line numbers. */
  188. static void
  189. print_unidiff_number_range (file, a, b)
  190. struct file_data const *file;
  191. int a, b;
  192. {
  193. int trans_a, trans_b;
  194. translate_range (file, a, b, &trans_a, &trans_b);
  195. /* Note: we can have B < A in the case of a range of no lines.
  196. In this case, we should print the line number before the range,
  197. which is B. */
  198. if (trans_b <= trans_a)
  199. printf_output (trans_b == trans_a ? "%d" : "%d,0", trans_b);
  200. else
  201. printf_output ("%d,%d", trans_a, trans_b - trans_a + 1);
  202. }
  203. /* Print a portion of an edit script in unidiff format.
  204. HUNK is the beginning of the portion to be printed.
  205. The end is marked by a `link' that has been nulled out.
  206. Prints out lines from both files, and precedes each
  207. line with the appropriate flag-character. */
  208. static void
  209. pr_unidiff_hunk (hunk)
  210. struct change *hunk;
  211. {
  212. int first0, last0, first1, last1, show_from, show_to, i, j, k;
  213. struct change *next;
  214. char const *function;
  215. size_t function_length;
  216. /* Determine range of line numbers involved in each file. */
  217. analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  218. if (!show_from && !show_to)
  219. return;
  220. /* Include a context's width before and after. */
  221. i = - files[0].prefix_lines;
  222. first0 = max (first0 - context, i);
  223. first1 = max (first1 - context, i);
  224. last0 = min (last0 + context, files[0].valid_lines - 1);
  225. last1 = min (last1 + context, files[1].valid_lines - 1);
  226. /* If desired, find the preceding function definition line in file 0. */
  227. function = 0;
  228. if (function_regexp_list)
  229. find_function (&files[0], first0, &function, &function_length);
  230. begin_output ();
  231. printf_output ("@@ -");
  232. print_unidiff_number_range (&files[0], first0, last0);
  233. printf_output (" +");
  234. print_unidiff_number_range (&files[1], first1, last1);
  235. printf_output (" @@");
  236. /* If we looked for and found a function this is part of,
  237. include its name in the header of the diff section. */
  238. if (function)
  239. {
  240. write_output (" ", 1);
  241. write_output (function, min (function_length - 1, 40));
  242. }
  243. write_output ("\n", 1);
  244. next = hunk;
  245. i = first0;
  246. j = first1;
  247. while (i <= last0 || j <= last1)
  248. {
  249. /* If the line isn't a difference, output the context from file 0. */
  250. if (!next || i < next->line0)
  251. {
  252. write_output (tab_align_flag ? "\t" : " ", 1);
  253. print_1_line (0, &files[0].linbuf[i++]);
  254. j++;
  255. }
  256. else
  257. {
  258. /* For each difference, first output the deleted part. */
  259. k = next->deleted;
  260. while (k--)
  261. {
  262. write_output ("-", 1);
  263. if (tab_align_flag)
  264. write_output ("\t", 1);
  265. print_1_line (0, &files[0].linbuf[i++]);
  266. }
  267. /* Then output the inserted part. */
  268. k = next->inserted;
  269. while (k--)
  270. {
  271. write_output ("+", 1);
  272. if (tab_align_flag)
  273. write_output ("\t", 1);
  274. print_1_line (0, &files[1].linbuf[j++]);
  275. }
  276. /* We're done with this hunk, so on to the next! */
  277. next = next->link;
  278. }
  279. }
  280. }
  281. /* Scan a (forward-ordered) edit script for the first place that more than
  282. 2*CONTEXT unchanged lines appear, and return a pointer
  283. to the `struct change' for the last change before those lines. */
  284. static struct change *
  285. find_hunk (start)
  286. struct change *start;
  287. {
  288. struct change *prev;
  289. int top0, top1;
  290. int thresh;
  291. do
  292. {
  293. /* Compute number of first line in each file beyond this changed. */
  294. top0 = start->line0 + start->deleted;
  295. top1 = start->line1 + start->inserted;
  296. prev = start;
  297. start = start->link;
  298. /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  299. but only CONTEXT if one is ignorable. */
  300. thresh = ((prev->ignore || (start && start->ignore))
  301. ? context
  302. : 2 * context + 1);
  303. /* It is not supposed to matter which file we check in the end-test.
  304. If it would matter, crash. */
  305. if (start && start->line0 - top0 != start->line1 - top1)
  306. abort ();
  307. } while (start
  308. /* Keep going if less than THRESH lines
  309. elapse before the affected line. */
  310. && start->line0 < top0 + thresh);
  311. return prev;
  312. }
  313. /* Set the `ignore' flag properly in each change in SCRIPT.
  314. It should be 1 if all the lines inserted or deleted in that change
  315. are ignorable lines. */
  316. static void
  317. mark_ignorable (script)
  318. struct change *script;
  319. {
  320. while (script)
  321. {
  322. struct change *next = script->link;
  323. int first0, last0, first1, last1, deletes, inserts;
  324. /* Turn this change into a hunk: detach it from the others. */
  325. script->link = 0;
  326. /* Determine whether this change is ignorable. */
  327. analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
  328. /* Reconnect the chain as before. */
  329. script->link = next;
  330. /* If the change is ignorable, mark it. */
  331. script->ignore = (!deletes && !inserts);
  332. /* Advance to the following change. */
  333. script = next;
  334. }
  335. }
  336. /* Find the last function-header line in FILE prior to line number LINENUM.
  337. This is a line containing a match for the regexp in `function_regexp'.
  338. Store the address of the line text into LINEP and the length of the
  339. line into LENP.
  340. Do not store anything if no function-header is found. */
  341. static void
  342. find_function (file, linenum, linep, lenp)
  343. struct file_data const *file;
  344. int linenum;
  345. char const **linep;
  346. size_t *lenp;
  347. {
  348. int i = linenum;
  349. int last = find_function_last_search;
  350. find_function_last_search = i;
  351. while (--i >= last)
  352. {
  353. /* See if this line is what we want. */
  354. struct regexp_list *r;
  355. char const *line = file->linbuf[i];
  356. size_t len = file->linbuf[i + 1] - line;
  357. for (r = function_regexp_list; r; r = r->next)
  358. if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  359. {
  360. *linep = line;
  361. *lenp = len;
  362. find_function_last_match = i;
  363. return;
  364. }
  365. }
  366. /* If we search back to where we started searching the previous time,
  367. find the line we found last time. */
  368. if (find_function_last_match >= - file->prefix_lines)
  369. {
  370. i = find_function_last_match;
  371. *linep = file->linbuf[i];
  372. *lenp = file->linbuf[i + 1] - *linep;
  373. return;
  374. }
  375. return;
  376. }