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