sicontrol(8): Add few fallthrough.
[dragonfly.git] / contrib / diffutils / src / ed.c
blob6a884dfb121d2f0df98d3b720ff55b39733d8e8f
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/>. */
21 #include "diff.h"
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. */
29 void
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 */
37 static void
38 print_ed_hunk (struct change *hunk)
40 lin f0, l0, f1, l1;
41 enum changes changes;
43 #ifdef DEBUG
44 debug_script (hunk);
45 #endif
47 /* Determine range of line numbers involved in each file. */
48 changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
49 if (!changes)
50 return;
52 begin_output ();
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 */
60 if (changes != OLD)
62 lin i;
63 bool insert_mode = true;
65 for (i = f1; i <= l1; i++)
67 if (!insert_mode)
69 fputs ("a\n", outfile);
70 insert_mode = true;
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);
78 insert_mode = false;
80 else
81 print_1_line ("", &files[1].linbuf[i]);
84 if (insert_mode)
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. */
95 void
96 pr_forward_ed_script (struct change *script)
98 print_script (script, find_change, pr_forward_ed_hunk);
101 static void
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);
108 if (!changes)
109 return;
111 begin_output ();
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. */
119 if (changes == OLD)
120 return;
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. */
135 void
136 print_rcs_script (struct change *script)
138 print_script (script, find_change, print_rcs_hunk);
141 /* Print a hunk of an RCS diff */
143 static void
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);
151 if (!changes)
152 return;
154 begin_output ();
156 translate_range (&files[0], f0, l0, &tf0, &tl0);
158 if (changes & OLD)
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);
165 if (changes & NEW)
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]);