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