1 /* Output routines for ed-script format.
3 Copyright (C) 1988-1989, 1991-1993, 1995, 1998, 2001, 2004, 2006, 2009-2013
4 Free Software Foundation, Inc.
6 This file is part of GNU DIFF.
8 This program 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 3 of the License, or
11 (at your option) any later version.
13 This program 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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 static void print_ed_hunk (struct change
*);
24 static void print_rcs_hunk (struct change
*);
25 static void pr_forward_ed_hunk (struct change
*);
27 /* Print our script as ed commands. */
30 print_ed_script (struct change
*script
)
32 print_script (script
, find_reverse_change
, print_ed_hunk
);
35 /* Print a hunk of an ed diff */
38 print_ed_hunk (struct change
*hunk
)
47 /* Determine range of line numbers involved in each file. */
48 changes
= analyze_hunk (hunk
, &f0
, &l0
, &f1
, &l1
);
54 /* Print out the line number header for this hunk */
55 print_number_range (',', &files
[0], f0
, l0
);
56 fputc (change_letter
[changes
], outfile
);
57 fputc ('\n', outfile
);
59 /* Print new/changed lines from second file, if needed */
63 bool insert_mode
= true;
65 for (i
= f1
; i
<= l1
; i
++)
69 fputs ("a\n", outfile
);
72 if (files
[1].linbuf
[i
][0] == '.' && files
[1].linbuf
[i
][1] == '\n')
74 /* The file's line is just a dot, and it would exit
75 insert mode. Precede the dot with another dot, exit
76 insert mode and remove the extra dot. */
77 fputs ("..\n.\ns/.//\n", outfile
);
81 print_1_line ("", &files
[1].linbuf
[i
]);
85 fputs (".\n", outfile
);
89 /* Print change script in the style of ed commands,
90 but print the changes in the order they appear in the input files,
91 which means that the commands are not truly useful with ed.
92 Because of the issue with lines containing just a dot, the output
93 is not even parseable. */
96 pr_forward_ed_script (struct change
*script
)
98 print_script (script
, find_change
, pr_forward_ed_hunk
);
102 pr_forward_ed_hunk (struct change
*hunk
)
104 lin i
, f0
, l0
, f1
, l1
;
106 /* Determine range of line numbers involved in each file. */
107 enum changes changes
= analyze_hunk (hunk
, &f0
, &l0
, &f1
, &l1
);
113 fputc (change_letter
[changes
], outfile
);
114 print_number_range (' ', files
, f0
, l0
);
115 fputc ('\n', outfile
);
117 /* If deletion only, print just the number range. */
122 /* For insertion (with or without deletion), print the number range
123 and the lines from file 2. */
125 for (i
= f1
; i
<= l1
; i
++)
126 print_1_line ("", &files
[1].linbuf
[i
]);
128 fputs (".\n", outfile
);
131 /* Print in a format somewhat like ed commands
132 except that each insert command states the number of lines it inserts.
133 This format is used for RCS. */
136 print_rcs_script (struct change
*script
)
138 print_script (script
, find_change
, print_rcs_hunk
);
141 /* Print a hunk of an RCS diff */
144 print_rcs_hunk (struct change
*hunk
)
146 lin i
, f0
, l0
, f1
, l1
;
147 long int tf0
, tl0
, tf1
, tl1
;
149 /* Determine range of line numbers involved in each file. */
150 enum changes changes
= analyze_hunk (hunk
, &f0
, &l0
, &f1
, &l1
);
156 translate_range (&files
[0], f0
, l0
, &tf0
, &tl0
);
160 /* For deletion, print just the starting line number from file 0
161 and the number of lines deleted. */
162 fprintf (outfile
, "d%ld %ld\n", tf0
, tf0
<= tl0
? tl0
- tf0
+ 1 : 1);
167 /* Take last-line-number from file 0 and # lines from file 1. */
168 translate_range (&files
[1], f1
, l1
, &tf1
, &tl1
);
169 fprintf (outfile
, "a%ld %ld\n", tl0
, tf1
<= tl1
? tl1
- tf1
+ 1 : 1);
171 /* Print the inserted lines. */
172 for (i
= f1
; i
<= l1
; i
++)
173 print_1_line ("", &files
[1].linbuf
[i
]);