1 /* Output routines for ed-script format.
2 Copyright (C) 1988, 89, 91, 92, 93 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
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)
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.
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. */
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 *));
26 /* Print our script as ed commands. */
29 print_ed_script (script)
30 struct change *script;
32 print_script (script, find_reverse_change, print_ed_hunk);
35 /* Print a hunk of an ed diff */
45 hunk = flip_script (hunk);
51 /* Determine range of line numbers involved in each file. */
52 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
53 if (!deletes && !inserts)
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));
62 /* Print new/changed lines from second file, if needed */
67 for (i = f1; i <= l1; i++)
69 /* Resume the insert, if we stopped. */
71 fprintf (outfile, "%da\n",
72 i - f1 + translate_line_number (&files[0], f0) - 1);
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. */
80 if (files[1].linbuf[i][0] == '.'
81 && files[1].linbuf[i][1] == '\n')
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));
91 /* Line is not `.', so output it unmodified. */
92 print_1_line ("", &files[1].linbuf[i]);
95 /* End insert mode, if we are still in it. */
97 fprintf (outfile, ".\n");
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. */
106 pr_forward_ed_script (script)
107 struct change *script;
109 print_script (script, find_change, pr_forward_ed_hunk);
113 pr_forward_ed_hunk (hunk)
118 int deletes, inserts;
120 /* Determine range of line numbers involved in each file. */
121 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
122 if (!deletes && !inserts)
127 fprintf (outfile, "%c", change_letter (inserts, deletes));
128 print_number_range (' ', files, f0, l0);
129 fprintf (outfile, "\n");
131 /* If deletion only, print just the number range. */
136 /* For insertion (with or without deletion), print the number range
137 and the lines from file 2. */
139 for (i = f1; i <= l1; i++)
140 print_1_line ("", &files[1].linbuf[i]);
142 fprintf (outfile, ".\n");
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. */
150 print_rcs_script (script)
151 struct change *script;
153 print_script (script, find_change, print_rcs_hunk);
156 /* Print a hunk of an RCS diff */
159 print_rcs_hunk (hunk)
164 int deletes, inserts;
165 int tf0, tl0, tf1, tl1;
167 /* Determine range of line numbers involved in each file. */
168 analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
169 if (!deletes && !inserts)
174 translate_range (&files[0], f0, l0, &tf0, &tl0);
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",
183 (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));
188 fprintf (outfile, "a");
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",
194 (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));
196 /* Print the inserted lines. */
197 for (i = f1; i <= l1; i++)
198 print_1_line ("", &files[1].linbuf[i]);