xref: /netbsd-src/external/gpl2/diffutils/dist/src/ed.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: ed.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2 
3 /* Output routines for ed-script format.
4 
5    Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1998, 2001 Free
6    Software Foundation, Inc.
7 
8    This file is part of GNU DIFF.
9 
10    GNU DIFF is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2, or (at your option)
13    any later version.
14 
15    GNU DIFF is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; see the file COPYING.
22    If not, write to the Free Software Foundation,
23    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
24 
25 #include "diff.h"
26 
27 static void print_ed_hunk (struct change *);
28 static void print_rcs_hunk (struct change *);
29 static void pr_forward_ed_hunk (struct change *);
30 
31 /* Print our script as ed commands.  */
32 
33 void
34 print_ed_script (struct change *script)
35 {
36   print_script (script, find_reverse_change, print_ed_hunk);
37 }
38 
39 /* Print a hunk of an ed diff */
40 
41 static void
42 print_ed_hunk (struct change *hunk)
43 {
44   lin f0, l0, f1, l1;
45   enum changes changes;
46 
47 #ifdef DEBUG
48   debug_script (hunk);
49 #endif
50 
51   /* Determine range of line numbers involved in each file.  */
52   changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
53   if (!changes)
54     return;
55 
56   begin_output ();
57 
58   /* Print out the line number header for this hunk */
59   print_number_range (',', &files[0], f0, l0);
60   fprintf (outfile, "%c\n", change_letter[changes]);
61 
62   /* Print new/changed lines from second file, if needed */
63   if (changes != OLD)
64     {
65       lin i;
66       for (i = f1; i <= l1; i++)
67 	{
68 	  if (files[1].linbuf[i][0] == '.' && files[1].linbuf[i][1] == '\n')
69 	    {
70 	      /* The file's line is just a dot, and it would exit
71 		 insert mode.  Precede the dot with another dot, exit
72 		 insert mode, remove the extra dot, and then resume
73 		 insert mode.  */
74 	      fprintf (outfile, "..\n.\ns/.//\na\n");
75 	    }
76 	  else
77 	    print_1_line ("", &files[1].linbuf[i]);
78 	}
79 
80       fprintf (outfile, ".\n");
81     }
82 }
83 
84 /* Print change script in the style of ed commands,
85    but print the changes in the order they appear in the input files,
86    which means that the commands are not truly useful with ed.  */
87 
88 void
89 pr_forward_ed_script (struct change *script)
90 {
91   print_script (script, find_change, pr_forward_ed_hunk);
92 }
93 
94 static void
95 pr_forward_ed_hunk (struct change *hunk)
96 {
97   lin i, f0, l0, f1, l1;
98 
99   /* Determine range of line numbers involved in each file.  */
100   enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
101   if (!changes)
102     return;
103 
104   begin_output ();
105 
106   fprintf (outfile, "%c", change_letter[changes]);
107   print_number_range (' ', files, f0, l0);
108   fprintf (outfile, "\n");
109 
110   /* If deletion only, print just the number range.  */
111 
112   if (changes == OLD)
113     return;
114 
115   /* For insertion (with or without deletion), print the number range
116      and the lines from file 2.  */
117 
118   for (i = f1; i <= l1; i++)
119     print_1_line ("", &files[1].linbuf[i]);
120 
121   fprintf (outfile, ".\n");
122 }
123 
124 /* Print in a format somewhat like ed commands
125    except that each insert command states the number of lines it inserts.
126    This format is used for RCS.  */
127 
128 void
129 print_rcs_script (struct change *script)
130 {
131   print_script (script, find_change, print_rcs_hunk);
132 }
133 
134 /* Print a hunk of an RCS diff */
135 
136 static void
137 print_rcs_hunk (struct change *hunk)
138 {
139   lin i, f0, l0, f1, l1;
140   long tf0, tl0, tf1, tl1;
141 
142   /* Determine range of line numbers involved in each file.  */
143   enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
144   if (!changes)
145     return;
146 
147   begin_output ();
148 
149   translate_range (&files[0], f0, l0, &tf0, &tl0);
150 
151   if (changes & OLD)
152     {
153       fprintf (outfile, "d");
154       /* For deletion, print just the starting line number from file 0
155 	 and the number of lines deleted.  */
156       fprintf (outfile, "%ld %ld\n", tf0, tf0 <= tl0 ? tl0 - tf0 + 1 : 1);
157     }
158 
159   if (changes & NEW)
160     {
161       fprintf (outfile, "a");
162 
163       /* Take last-line-number from file 0 and # lines from file 1.  */
164       translate_range (&files[1], f1, l1, &tf1, &tl1);
165       fprintf (outfile, "%ld %ld\n", tl0, tf1 <= tl1 ? tl1 - tf1 + 1 : 1);
166 
167       /* Print the inserted lines.  */
168       for (i = f1; i <= l1; i++)
169 	print_1_line ("", &files[1].linbuf[i]);
170     }
171 }
172