1 /* Output routines for ed-script format.
2 Copyright (C) 1988, 89, 91, 92, 93 Free Software Foundation, Inc.
3
4 This file is part of GNU DIFF.
5
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU DIFF; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "diff.h"
21
22 static void print_ed_hunk PARAMS((struct change *));
23 static void print_rcs_hunk PARAMS((struct change *));
24 static void pr_forward_ed_hunk PARAMS((struct change *));
25
26 /* Print our script as ed commands. */
27
28 void
print_ed_script(script)29 print_ed_script (script)
30 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
print_ed_hunk(hunk)38 print_ed_hunk (hunk)
39 struct change *hunk;
40 {
41 int f0, l0, f1, l1;
42 int deletes, inserts;
43
44 #if 0
45 hunk = flip_script (hunk);
46 #endif
47 #ifdef DEBUG
48 debug_script (hunk);
49 #endif
50
51 /* Determine range of line numbers involved in each file. */
52 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
53 if (!deletes && !inserts)
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 (inserts, deletes));
61
62 /* Print new/changed lines from second file, if needed */
63 if (inserts)
64 {
65 int i;
66 int inserting = 1;
67 for (i = f1; i <= l1; i++)
68 {
69 /* Resume the insert, if we stopped. */
70 if (! inserting)
71 fprintf (outfile, "%da\n",
72 i - f1 + translate_line_number (&files[0], f0) - 1);
73 inserting = 1;
74
75 /* If the file's line is just a dot, it would confuse `ed'.
76 So output it with a double dot, and set the flag LEADING_DOT
77 so that we will output another ed-command later
78 to change the double dot into a single dot. */
79
80 if (files[1].linbuf[i][0] == '.'
81 && files[1].linbuf[i][1] == '\n')
82 {
83 fprintf (outfile, "..\n");
84 fprintf (outfile, ".\n");
85 /* Now change that double dot to the desired single dot. */
86 fprintf (outfile, "%ds/^\\.\\././\n",
87 i - f1 + translate_line_number (&files[0], f0));
88 inserting = 0;
89 }
90 else
91 /* Line is not `.', so output it unmodified. */
92 print_1_line ("", &files[1].linbuf[i]);
93 }
94
95 /* End insert mode, if we are still in it. */
96 if (inserting)
97 fprintf (outfile, ".\n");
98 }
99 }
100
101 /* Print change script in the style of ed commands,
102 but print the changes in the order they appear in the input files,
103 which means that the commands are not truly useful with ed. */
104
105 void
pr_forward_ed_script(script)106 pr_forward_ed_script (script)
107 struct change *script;
108 {
109 print_script (script, find_change, pr_forward_ed_hunk);
110 }
111
112 static void
pr_forward_ed_hunk(hunk)113 pr_forward_ed_hunk (hunk)
114 struct change *hunk;
115 {
116 int i;
117 int f0, l0, f1, l1;
118 int deletes, inserts;
119
120 /* Determine range of line numbers involved in each file. */
121 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
122 if (!deletes && !inserts)
123 return;
124
125 begin_output ();
126
127 fprintf (outfile, "%c", change_letter (inserts, deletes));
128 print_number_range (' ', files, f0, l0);
129 fprintf (outfile, "\n");
130
131 /* If deletion only, print just the number range. */
132
133 if (!inserts)
134 return;
135
136 /* For insertion (with or without deletion), print the number range
137 and the lines from file 2. */
138
139 for (i = f1; i <= l1; i++)
140 print_1_line ("", &files[1].linbuf[i]);
141
142 fprintf (outfile, ".\n");
143 }
144
145 /* Print in a format somewhat like ed commands
146 except that each insert command states the number of lines it inserts.
147 This format is used for RCS. */
148
149 void
print_rcs_script(script)150 print_rcs_script (script)
151 struct change *script;
152 {
153 print_script (script, find_change, print_rcs_hunk);
154 }
155
156 /* Print a hunk of an RCS diff */
157
158 static void
print_rcs_hunk(hunk)159 print_rcs_hunk (hunk)
160 struct change *hunk;
161 {
162 int i;
163 int f0, l0, f1, l1;
164 int deletes, inserts;
165 int tf0, tl0, tf1, tl1;
166
167 /* Determine range of line numbers involved in each file. */
168 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
169 if (!deletes && !inserts)
170 return;
171
172 begin_output ();
173
174 translate_range (&files[0], f0, l0, &tf0, &tl0);
175
176 if (deletes)
177 {
178 fprintf (outfile, "d");
179 /* For deletion, print just the starting line number from file 0
180 and the number of lines deleted. */
181 fprintf (outfile, "%d %d\n",
182 tf0,
183 (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));
184 }
185
186 if (inserts)
187 {
188 fprintf (outfile, "a");
189
190 /* Take last-line-number from file 0 and # lines from file 1. */
191 translate_range (&files[1], f1, l1, &tf1, &tl1);
192 fprintf (outfile, "%d %d\n",
193 tl0,
194 (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));
195
196 /* Print the inserted lines. */
197 for (i = f1; i <= l1; i++)
198 print_1_line ("", &files[1].linbuf[i]);
199 }
200 }
201