(root)/
diffutils-3.10/
src/
ed.c
       1  /* Output routines for ed-script format.
       2  
       3     Copyright (C) 1988-1989, 1991-1993, 1995, 1998, 2001, 2004, 2006, 2009-2013,
       4     2015-2023 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_ed_hunk (struct change *);
      24  static void print_rcs_hunk (struct change *);
      25  static void pr_forward_ed_hunk (struct change *);
      26  
      27  /* Print our script as ed commands.  */
      28  
      29  void
      30  print_ed_script (struct change *script)
      31  {
      32    print_script (script, find_reverse_change, print_ed_hunk);
      33  }
      34  
      35  /* Print a hunk of an ed diff */
      36  
      37  static void
      38  print_ed_hunk (struct change *hunk)
      39  {
      40    lin f0, l0, f1, l1;
      41    enum changes changes;
      42  
      43  #ifdef DEBUG
      44    debug_script (hunk);
      45  #endif
      46  
      47    /* Determine range of line numbers involved in each file.  */
      48    changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
      49    if (!changes)
      50      return;
      51  
      52    begin_output ();
      53  
      54    /* Print out the line number header for this hunk */
      55    print_number_range (',', &files[0], f0, l0);
      56    fputc (change_letter[changes], outfile);
      57    fputc ('\n', outfile);
      58  
      59    /* Print new/changed lines from second file, if needed */
      60    if (changes != OLD)
      61      {
      62        lin i;
      63        bool insert_mode = true;
      64  
      65        for (i = f1; i <= l1; i++)
      66          {
      67            if (!insert_mode)
      68              {
      69                fputs ("a\n", outfile);
      70                insert_mode = true;
      71              }
      72            if (files[1].linbuf[i][0] == '.' && files[1].linbuf[i][1] == '\n')
      73              {
      74                /* The file's line is just a dot, and it would exit
      75                   insert mode.  Precede the dot with another dot, exit
      76                   insert mode and remove the extra dot.  */
      77                fputs ("..\n.\ns/.//\n", outfile);
      78                insert_mode = false;
      79              }
      80            else
      81              print_1_line ("", &files[1].linbuf[i]);
      82          }
      83  
      84        if (insert_mode)
      85          fputs (".\n", outfile);
      86      }
      87  }
      88  
      89  /* Print change script in the style of ed commands,
      90     but print the changes in the order they appear in the input files,
      91     which means that the commands are not truly useful with ed.
      92     Because of the issue with lines containing just a dot, the output
      93     is not even parseable.  */
      94  
      95  void
      96  pr_forward_ed_script (struct change *script)
      97  {
      98    print_script (script, find_change, pr_forward_ed_hunk);
      99  }
     100  
     101  static void
     102  pr_forward_ed_hunk (struct change *hunk)
     103  {
     104    lin i, f0, l0, f1, l1;
     105  
     106    /* Determine range of line numbers involved in each file.  */
     107    enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
     108    if (!changes)
     109      return;
     110  
     111    begin_output ();
     112  
     113    fputc (change_letter[changes], outfile);
     114    print_number_range (' ', files, f0, l0);
     115    fputc ('\n', outfile);
     116  
     117    /* If deletion only, print just the number range.  */
     118  
     119    if (changes == OLD)
     120      return;
     121  
     122    /* For insertion (with or without deletion), print the number range
     123       and the lines from file 2.  */
     124  
     125    for (i = f1; i <= l1; i++)
     126      print_1_line ("", &files[1].linbuf[i]);
     127  
     128    fputs (".\n", outfile);
     129  }
     130  
     131  /* Print in a format somewhat like ed commands
     132     except that each insert command states the number of lines it inserts.
     133     This format is used for RCS.  */
     134  
     135  void
     136  print_rcs_script (struct change *script)
     137  {
     138    print_script (script, find_change, print_rcs_hunk);
     139  }
     140  
     141  /* Print a hunk of an RCS diff */
     142  
     143  static void
     144  print_rcs_hunk (struct change *hunk)
     145  {
     146    lin i, f0, l0, f1, l1;
     147    lin tf0, tl0, tf1, tl1;
     148  
     149    /* Determine range of line numbers involved in each file.  */
     150    enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
     151    if (!changes)
     152      return;
     153  
     154    begin_output ();
     155  
     156    translate_range (&files[0], f0, l0, &tf0, &tl0);
     157  
     158    if (changes & OLD)
     159      {
     160        /* For deletion, print just the starting line number from file 0
     161           and the number of lines deleted.  */
     162        fprintf (outfile, "d%"pI"d %"pI"d\n", tf0,
     163                 tf0 <= tl0 ? tl0 - tf0 + 1 : 1);
     164      }
     165  
     166    if (changes & NEW)
     167      {
     168        /* Take last-line-number from file 0 and # lines from file 1.  */
     169        translate_range (&files[1], f1, l1, &tf1, &tl1);
     170        fprintf (outfile, "a%"pI"d %"pI"d\n", tl0,
     171                 tf1 <= tl1 ? tl1 - tf1 + 1 : 1);
     172  
     173        /* Print the inserted lines.  */
     174        for (i = f1; i <= l1; i++)
     175          print_1_line ("", &files[1].linbuf[i]);
     176      }
     177  }