amd64 port: mainly on the pmap headers, identify_cpu and initcpu
[dragonfly/port-amd64.git] / contrib / diffutils-2.8 / src / ed.c
blobc90ed78f0df7daa5b31c830e497619173fbac820
1 /* Output routines for ed-script format.
3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995, 1998, 2001, 2004
4 Free Software Foundation, Inc.
6 This file is part of GNU DIFF.
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.
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.
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. */
23 #include "diff.h"
25 static void print_ed_hunk (struct change *);
26 static void print_rcs_hunk (struct change *);
27 static void pr_forward_ed_hunk (struct change *);
29 /* Print our script as ed commands. */
31 void
32 print_ed_script (struct change *script)
34 print_script (script, find_reverse_change, print_ed_hunk);
37 /* Print a hunk of an ed diff */
39 static void
40 print_ed_hunk (struct change *hunk)
42 lin f0, l0, f1, l1;
43 enum changes changes;
45 #ifdef DEBUG
46 debug_script (hunk);
47 #endif
49 /* Determine range of line numbers involved in each file. */
50 changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
51 if (!changes)
52 return;
54 begin_output ();
56 /* Print out the line number header for this hunk */
57 print_number_range (',', &files[0], f0, l0);
58 fprintf (outfile, "%c\n", change_letter[changes]);
60 /* Print new/changed lines from second file, if needed */
61 if (changes != OLD)
63 lin i;
64 for (i = f1; i <= l1; i++)
66 if (files[1].linbuf[i][0] == '.' && files[1].linbuf[i][1] == '\n')
68 /* The file's line is just a dot, and it would exit
69 insert mode. Precede the dot with another dot, exit
70 insert mode, remove the extra dot, and then resume
71 insert mode. */
72 fprintf (outfile, "..\n.\ns/.//\na\n");
74 else
75 print_1_line ("", &files[1].linbuf[i]);
78 fprintf (outfile, ".\n");
82 /* Print change script in the style of ed commands,
83 but print the changes in the order they appear in the input files,
84 which means that the commands are not truly useful with ed. */
86 void
87 pr_forward_ed_script (struct change *script)
89 print_script (script, find_change, pr_forward_ed_hunk);
92 static void
93 pr_forward_ed_hunk (struct change *hunk)
95 lin i, f0, l0, f1, l1;
97 /* Determine range of line numbers involved in each file. */
98 enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
99 if (!changes)
100 return;
102 begin_output ();
104 fprintf (outfile, "%c", change_letter[changes]);
105 print_number_range (' ', files, f0, l0);
106 fprintf (outfile, "\n");
108 /* If deletion only, print just the number range. */
110 if (changes == OLD)
111 return;
113 /* For insertion (with or without deletion), print the number range
114 and the lines from file 2. */
116 for (i = f1; i <= l1; i++)
117 print_1_line ("", &files[1].linbuf[i]);
119 fprintf (outfile, ".\n");
122 /* Print in a format somewhat like ed commands
123 except that each insert command states the number of lines it inserts.
124 This format is used for RCS. */
126 void
127 print_rcs_script (struct change *script)
129 print_script (script, find_change, print_rcs_hunk);
132 /* Print a hunk of an RCS diff */
134 static void
135 print_rcs_hunk (struct change *hunk)
137 lin i, f0, l0, f1, l1;
138 long int tf0, tl0, tf1, tl1;
140 /* Determine range of line numbers involved in each file. */
141 enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
142 if (!changes)
143 return;
145 begin_output ();
147 translate_range (&files[0], f0, l0, &tf0, &tl0);
149 if (changes & OLD)
151 fprintf (outfile, "d");
152 /* For deletion, print just the starting line number from file 0
153 and the number of lines deleted. */
154 fprintf (outfile, "%ld %ld\n", tf0, tf0 <= tl0 ? tl0 - tf0 + 1 : 1);
157 if (changes & NEW)
159 fprintf (outfile, "a");
161 /* Take last-line-number from file 0 and # lines from file 1. */
162 translate_range (&files[1], f1, l1, &tf1, &tl1);
163 fprintf (outfile, "%ld %ld\n", tl0, tf1 <= tl1 ? tl1 - tf1 + 1 : 1);
165 /* Print the inserted lines. */
166 for (i = f1; i <= l1; i++)
167 print_1_line ("", &files[1].linbuf[i]);