ed.c revision 1.1.1.1.2.2 1 /* Output routines for ed-script format.
2 Copyright (C) 1988, 89, 91, 92, 93, 1998 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 */
17
18 #include "diff.h"
19
20 static void print_ed_hunk PARAMS((struct change *));
21 static void print_rcs_hunk PARAMS((struct change *));
22 static void pr_forward_ed_hunk PARAMS((struct change *));
23
24 /* Print our script as ed commands. */
26
27 void
28 print_ed_script (script)
29 struct change *script;
30 {
31 print_script (script, find_reverse_change, print_ed_hunk);
32 }
33
34 /* Print a hunk of an ed diff */
35
36 static void
37 print_ed_hunk (hunk)
38 struct change *hunk;
39 {
40 int f0, l0, f1, l1;
41 int deletes, inserts;
42
43 #if 0
44 hunk = flip_script (hunk);
45 #endif
46 #ifdef DEBUG
47 debug_script (hunk);
48 #endif
49
50 /* Determine range of line numbers involved in each file. */
51 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
52 if (!deletes && !inserts)
53 return;
54
55 begin_output ();
56
57 /* Print out the line number header for this hunk */
58 print_number_range (',', &files[0], f0, l0);
59 printf_output ("%c\n", change_letter (inserts, deletes));
60
61 /* Print new/changed lines from second file, if needed */
62 if (inserts)
63 {
64 int i;
65 int inserting = 1;
66 for (i = f1; i <= l1; i++)
67 {
68 /* Resume the insert, if we stopped. */
69 if (! inserting)
70 printf_output ("%da\n",
71 i - f1 + translate_line_number (&files[0], f0) - 1);
72 inserting = 1;
73
74 /* If the file's line is just a dot, it would confuse `ed'.
75 So output it with a double dot, and set the flag LEADING_DOT
76 so that we will output another ed-command later
77 to change the double dot into a single dot. */
78
79 if (files[1].linbuf[i][0] == '.'
80 && files[1].linbuf[i][1] == '\n')
81 {
82 printf_output ("..\n");
83 printf_output (".\n");
84 /* Now change that double dot to the desired single dot. */
85 printf_output ("%ds/^\\.\\././\n",
86 i - f1 + translate_line_number (&files[0], f0));
87 inserting = 0;
88 }
89 else
90 /* Line is not `.', so output it unmodified. */
91 print_1_line ("", &files[1].linbuf[i]);
92 }
93
94 /* End insert mode, if we are still in it. */
95 if (inserting)
96 printf_output (".\n");
97 }
98 }
99
100 /* 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
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
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 printf_output ("%c", change_letter (inserts, deletes));
128 print_number_range (' ', files, f0, l0);
129 printf_output ("\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 printf_output (".\n");
143 }
144
145 /* Print in a format somewhat like ed commands
147 except that each insert command states the number of lines it inserts.
148 This format is used for RCS. */
149
150 void
151 print_rcs_script (script)
152 struct change *script;
153 {
154 print_script (script, find_change, print_rcs_hunk);
155 }
156
157 /* Print a hunk of an RCS diff */
158
159 static void
160 print_rcs_hunk (hunk)
161 struct change *hunk;
162 {
163 int i;
164 int f0, l0, f1, l1;
165 int deletes, inserts;
166 int tf0, tl0, tf1, tl1;
167
168 /* Determine range of line numbers involved in each file. */
169 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
170 if (!deletes && !inserts)
171 return;
172
173 begin_output ();
174
175 translate_range (&files[0], f0, l0, &tf0, &tl0);
176
177 if (deletes)
178 {
179 printf_output ("d");
180 /* For deletion, print just the starting line number from file 0
181 and the number of lines deleted. */
182 printf_output ("%d %d\n",
183 tf0,
184 (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));
185 }
186
187 if (inserts)
188 {
189 printf_output ("a");
190
191 /* Take last-line-number from file 0 and # lines from file 1. */
192 translate_range (&files[1], f1, l1, &tf1, &tl1);
193 printf_output ("%d %d\n",
194 tl0,
195 (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));
196
197 /* Print the inserted lines. */
198 for (i = f1; i <= l1; i++)
199 print_1_line ("", &files[1].linbuf[i]);
200 }
201 }
202