source: trunk/diffutils/src/ed.c@ 2559

Last change on this file since 2559 was 2556, checked in by bird, 19 years ago

diffutils 2.8.1

File size: 4.4 KB
Line 
1/* Output routines for ed-script format.
2
3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1998, 2001 Free
4 Software Foundation, Inc.
5
6 This file is part of GNU DIFF.
7
8 GNU DIFF 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 2, or (at your option)
11 any later version.
12
13 GNU DIFF 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; see the file COPYING.
20 If not, write to the Free Software Foundation,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23#include "diff.h"
24
25static void print_ed_hunk (struct change *);
26static void print_rcs_hunk (struct change *);
27static void pr_forward_ed_hunk (struct change *);
28
29
30/* Print our script as ed commands. */
31
32void
33print_ed_script (struct change *script)
34{
35 print_script (script, find_reverse_change, print_ed_hunk);
36}
37
38/* Print a hunk of an ed diff */
39
40static void
41print_ed_hunk (struct change *hunk)
42{
43 lin f0, l0, f1, l1;
44 enum changes changes;
45
46#ifdef DEBUG
47 debug_script (hunk);
48#endif
49
50 /* Determine range of line numbers involved in each file. */
51 changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
52 if (!changes)
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 fprintf (outfile, "%c\n", change_letter[changes]);
60
61 /* Print new/changed lines from second file, if needed */
62 if (changes != OLD)
63 {
64 lin i;
65 for (i = f1; i <= l1; i++)
66 {
67 if (files[1].linbuf[i][0] == '.' && files[1].linbuf[i][1] == '\n')
68 {
69 /* The file's line is just a dot, and it would exit
70 insert mode. Precede the dot with another dot, exit
71 insert mode, remove the extra dot, and then resume
72 insert mode. */
73 fprintf (outfile, "..\n.\ns/.//\na\n");
74 }
75 else
76 print_1_line ("", &files[1].linbuf[i]);
77 }
78
79 fprintf (outfile, ".\n");
80 }
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
88void
89pr_forward_ed_script (struct change *script)
90{
91 print_script (script, find_change, pr_forward_ed_hunk);
92}
93
94static void
95pr_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
125/* Print in a format somewhat like ed commands
126 except that each insert command states the number of lines it inserts.
127 This format is used for RCS. */
128
129void
130print_rcs_script (struct change *script)
131{
132 print_script (script, find_change, print_rcs_hunk);
133}
134
135/* Print a hunk of an RCS diff */
136
137static void
138print_rcs_hunk (struct change *hunk)
139{
140 lin i, f0, l0, f1, l1;
141 long tf0, tl0, tf1, tl1;
142
143 /* Determine range of line numbers involved in each file. */
144 enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
145 if (!changes)
146 return;
147
148 begin_output ();
149
150 translate_range (&files[0], f0, l0, &tf0, &tl0);
151
152 if (changes & OLD)
153 {
154 fprintf (outfile, "d");
155 /* For deletion, print just the starting line number from file 0
156 and the number of lines deleted. */
157 fprintf (outfile, "%ld %ld\n", tf0, tf0 <= tl0 ? tl0 - tf0 + 1 : 1);
158 }
159
160 if (changes & NEW)
161 {
162 fprintf (outfile, "a");
163
164 /* Take last-line-number from file 0 and # lines from file 1. */
165 translate_range (&files[1], f1, l1, &tf1, &tl1);
166 fprintf (outfile, "%ld %ld\n", tl0, tf1 <= tl1 ? tl1 - tf1 + 1 : 1);
167
168 /* Print the inserted lines. */
169 for (i = f1; i <= l1; i++)
170 print_1_line ("", &files[1].linbuf[i]);
171 }
172}
Note: See TracBrowser for help on using the repository browser.