| 1 |
/* Output routines for ed-script format. |
| 2 |
|
| 3 |
Copyright (C) 1988-1989, 1991-1993, 1995, 1998, 2001, 2004, 2006, 2009-2013, |
| 4 |
2015-2025 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GNU DIFF. |
| 7 |
|
| 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. |
| 12 |
|
| 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. |
| 17 |
|
| 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/>. */ |
| 20 |
|
| 21 |
#include "diff.h" |
| 22 |
|
| 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 *); |
| 26 |
|
| 27 |
/* Print our script as ed commands. */ |
| 28 |
|
| 29 |
void |
| 30 |
print_ed_script (struct change *script) |
| 31 |
{ |
| 32 |
/* The script is reversed, so plain find_change suffices. */ |
| 33 |
print_script (script, find_change, print_ed_hunk); |
| 34 |
} |
| 35 |
|
| 36 |
/* Print a hunk of an ed diff */ |
| 37 |
|
| 38 |
static void |
| 39 |
print_ed_hunk (struct change *hunk) |
| 40 |
{ |
| 41 |
#ifdef DEBUG |
| 42 |
debug_script (hunk); |
| 43 |
#endif |
| 44 |
|
| 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; |
| 50 |
|
| 51 |
begin_output (); |
| 52 |
|
| 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); |
| 57 |
|
| 58 |
/* Print new/changed lines from second file, if needed */ |
| 59 |
if (changes != OLD) |
| 60 |
{ |
| 61 |
bool insert_mode = true; |
| 62 |
|
| 63 |
for (lin i = f1; i <= l1; i++) |
| 64 |
{ |
| 65 |
if (!insert_mode) |
| 66 |
{ |
| 67 |
fputs ("a\n", outfile); |
| 68 |
insert_mode = true; |
| 69 |
} |
| 70 |
if (curr.file[1].linbuf[i][0] == '.' |
| 71 |
&& curr.file[1].linbuf[i][1] == '\n') |
| 72 |
{ |
| 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; |
| 78 |
} |
| 79 |
else |
| 80 |
print_1_line ("", &curr.file[1].linbuf[i]); |
| 81 |
} |
| 82 |
|
| 83 |
if (insert_mode) |
| 84 |
fputs (".\n", outfile); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 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. */ |
| 93 |
|
| 94 |
void |
| 95 |
pr_forward_ed_script (struct change *script) |
| 96 |
{ |
| 97 |
print_script (script, find_change, pr_forward_ed_hunk); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
pr_forward_ed_hunk (struct change *hunk) |
| 102 |
{ |
| 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; |
| 108 |
|
| 109 |
begin_output (); |
| 110 |
|
| 111 |
fputc (change_letter[changes], outfile); |
| 112 |
print_number_range (' ', &curr.file[0], f0, l0); |
| 113 |
fputc ('\n', outfile); |
| 114 |
|
| 115 |
/* If deletion only, print just the number range. */ |
| 116 |
|
| 117 |
if (changes == OLD) |
| 118 |
return; |
| 119 |
|
| 120 |
/* For insertion (with or without deletion), print the number range |
| 121 |
and the lines from file 2. */ |
| 122 |
|
| 123 |
for (lin i = f1; i <= l1; i++) |
| 124 |
print_1_line ("", &curr.file[1].linbuf[i]); |
| 125 |
|
| 126 |
fputs (".\n", outfile); |
| 127 |
} |
| 128 |
|
| 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. */ |
| 132 |
|
| 133 |
void |
| 134 |
print_rcs_script (struct change *script) |
| 135 |
{ |
| 136 |
print_script (script, find_change, print_rcs_hunk); |
| 137 |
} |
| 138 |
|
| 139 |
/* Print a hunk of an RCS diff */ |
| 140 |
|
| 141 |
static void |
| 142 |
print_rcs_hunk (struct change *hunk) |
| 143 |
{ |
| 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; |
| 149 |
|
| 150 |
begin_output (); |
| 151 |
|
| 152 |
lin tf0, tl0, tf1, tl1; |
| 153 |
translate_range (&curr.file[0], f0, l0, &tf0, &tl0); |
| 154 |
|
| 155 |
if (changes & OLD) |
| 156 |
{ |
| 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); |
| 161 |
} |
| 162 |
|
| 163 |
if (changes & NEW) |
| 164 |
{ |
| 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); |
| 169 |
|
| 170 |
/* Print the inserted lines. */ |
| 171 |
for (lin i = f1; i <= l1; i++) |
| 172 |
print_1_line ("", &curr.file[1].linbuf[i]); |
| 173 |
} |
| 174 |
} |