(root)/
diffutils-3.10/
src/
normal.c
       1  /* Normal-format output routines for GNU DIFF.
       2  
       3     Copyright (C) 1988-1989, 1993, 1995, 1998, 2001, 2006, 2009-2013, 2015-2023
       4     Free Software Foundation, Inc.
       5  
       6     This file is part of GNU DIFF.
       7  
       8     This program is free software: you can redistribute it and/or modify
       9     it under the terms of the GNU General Public License as published by
      10     the Free Software Foundation, either version 3 of the License, or
      11     (at your option) any later version.
      12  
      13     This program is distributed in the hope that it will be useful,
      14     but WITHOUT ANY WARRANTY; without even the implied warranty of
      15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16     GNU General Public License for more details.
      17  
      18     You should have received a copy of the GNU General Public License
      19     along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      20  
      21  #include "diff.h"
      22  
      23  static void print_normal_hunk (struct change *);
      24  
      25  /* Print the edit-script SCRIPT as a normal diff.
      26     INF points to an array of descriptions of the two files.  */
      27  
      28  void
      29  print_normal_script (struct change *script)
      30  {
      31    print_script (script, find_change, print_normal_hunk);
      32  }
      33  
      34  /* Print a hunk of a normal diff.
      35     This is a contiguous portion of a complete edit script,
      36     describing changes in consecutive lines.  */
      37  
      38  static void
      39  print_normal_hunk (struct change *hunk)
      40  {
      41    lin first0, last0, first1, last1;
      42    register lin i;
      43  
      44    /* Determine range of line numbers involved in each file.  */
      45    enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1);
      46    if (!changes)
      47      return;
      48  
      49    begin_output ();
      50  
      51    /* Print out the line number header for this hunk */
      52    set_color_context (LINE_NUMBER_CONTEXT);
      53    print_number_range (',', &files[0], first0, last0);
      54    fputc (change_letter[changes], outfile);
      55    print_number_range (',', &files[1], first1, last1);
      56    set_color_context (RESET_CONTEXT);
      57    fputc ('\n', outfile);
      58  
      59    /* Print the lines that the first file has.  */
      60    if (changes & OLD)
      61      {
      62        for (i = first0; i <= last0; i++)
      63          {
      64            set_color_context (DELETE_CONTEXT);
      65            print_1_line_nl ("<", &files[0].linbuf[i], true);
      66            set_color_context (RESET_CONTEXT);
      67            if (files[0].linbuf[i + 1][-1] == '\n')
      68              putc ('\n', outfile);
      69          }
      70      }
      71  
      72    if (changes == CHANGED)
      73      fputs ("---\n", outfile);
      74  
      75    /* Print the lines that the second file has.  */
      76    if (changes & NEW)
      77      {
      78        for (i = first1; i <= last1; i++)
      79          {
      80            set_color_context (ADD_CONTEXT);
      81            print_1_line_nl (">", &files[1].linbuf[i], true);
      82            set_color_context (RESET_CONTEXT);
      83            if (files[1].linbuf[i + 1][-1] == '\n')
      84              putc ('\n', outfile);
      85          }
      86      }
      87  }