doc: rewrite the "Unusual File Names" section
[diffutils.git] / src / ed.c
blobbb9cb2a670cb50e5a71b11ff88874ca76ced5d4f
1 /* Output routines for ed-script format.
3 Copyright (C) 1988-1989, 1991-1993, 1995, 1998, 2001, 2004, 2006, 2009-2013,
4 2015-2025 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 /* The script is reversed, so plain find_change suffices. */
33 print_script (script, find_change, print_ed_hunk);
36 /* Print a hunk of an ed diff */
38 static void
39 print_ed_hunk (struct change *hunk)
41 #ifdef DEBUG
42 debug_script (hunk);
43 #endif
45 /* Determine range of line numbers involved in each file. */
46 lin f0, l0, f1, l1;
47 enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
48 if (!changes)
49 return;
51 begin_output ();
53 /* Print out the line number header for this hunk */
54 print_number_range (',', &curr.file[0], f0, l0);
55 fputc (change_letter[changes], outfile);
56 fputc ('\n', outfile);
58 /* Print new/changed lines from second file, if needed */
59 if (changes != OLD)
61 bool insert_mode = true;
63 for (lin i = f1; i <= l1; i++)
65 if (!insert_mode)
67 fputs ("a\n", outfile);
68 insert_mode = true;
70 if (curr.file[1].linbuf[i][0] == '.'
71 && curr.file[1].linbuf[i][1] == '\n')
73 /* The file's line is just a dot, and it would exit
74 insert mode. Precede the dot with another dot, exit
75 insert mode and remove the extra dot. */
76 fputs ("..\n.\ns/.//\n", outfile);
77 insert_mode = false;
79 else
80 print_1_line ("", &curr.file[1].linbuf[i]);
83 if (insert_mode)
84 fputs (".\n", outfile);
88 /* Print change script in the style of ed commands,
89 but print the changes in the order they appear in the input files,
90 which means that the commands are not truly useful with ed.
91 Because of the issue with lines containing just a dot, the output
92 is not even parseable. */
94 void
95 pr_forward_ed_script (struct change *script)
97 print_script (script, find_change, pr_forward_ed_hunk);
100 static void
101 pr_forward_ed_hunk (struct change *hunk)
103 /* Determine range of line numbers involved in each file. */
104 lin f0, l0, f1, l1;
105 enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
106 if (!changes)
107 return;
109 begin_output ();
111 fputc (change_letter[changes], outfile);
112 print_number_range (' ', &curr.file[0], f0, l0);
113 fputc ('\n', outfile);
115 /* If deletion only, print just the number range. */
117 if (changes == OLD)
118 return;
120 /* For insertion (with or without deletion), print the number range
121 and the lines from file 2. */
123 for (lin i = f1; i <= l1; i++)
124 print_1_line ("", &curr.file[1].linbuf[i]);
126 fputs (".\n", outfile);
129 /* Print in a format somewhat like ed commands
130 except that each insert command states the number of lines it inserts.
131 This format is used for RCS. */
133 void
134 print_rcs_script (struct change *script)
136 print_script (script, find_change, print_rcs_hunk);
139 /* Print a hunk of an RCS diff */
141 static void
142 print_rcs_hunk (struct change *hunk)
144 /* Determine range of line numbers involved in each file. */
145 lin f0, l0, f1, l1;
146 enum changes changes = analyze_hunk (hunk, &f0, &l0, &f1, &l1);
147 if (!changes)
148 return;
150 begin_output ();
152 lin tf0, tl0, tf1, tl1;
153 translate_range (&curr.file[0], f0, l0, &tf0, &tl0);
155 if (changes & OLD)
157 /* For deletion, print just the starting line number from file 0
158 and the number of lines deleted. */
159 fprintf (outfile, "d%"pI"d %"pI"d\n", tf0,
160 tf0 <= tl0 ? tl0 - tf0 + 1 : 1);
163 if (changes & NEW)
165 /* Take last-line-number from file 0 and # lines from file 1. */
166 translate_range (&curr.file[1], f1, l1, &tf1, &tl1);
167 fprintf (outfile, "a%"pI"d %"pI"d\n", tl0,
168 tf1 <= tl1 ? tl1 - tf1 + 1 : 1);
170 /* Print the inserted lines. */
171 for (lin i = f1; i <= l1; i++)
172 print_1_line ("", &curr.file[1].linbuf[i]);