ed: Handle correctly lines in substitutions

The s command can apply a replace pattern with embedded newlines
which modifies the line/index assignament. Using a range in  the
address fail because afther the call to subline() the next  line
has to be searched based in the index because the replace  could
insert newlines.
This commit is contained in:
Roberto E. Vargas Caballero 2023-12-29 20:27:23 +01:00
parent e24228e062
commit 2732217a40
2 changed files with 15 additions and 11 deletions

8
TODO
View File

@ -50,14 +50,6 @@ ed
line
.
1g/^$/p
* cat <<EOF | ed
i
foobar1
foobar2
.
1,2s/foo/&\
&/
,n
* Editing huge files doesn't work well.

18
ed.c
View File

@ -1226,11 +1226,23 @@ subline(int num, int nth)
static void
subst(int nth)
{
int i;
int i, line, next;
for (i = line1; i <= line2; ++i) {
line = line1;
for (i = 0; i < line2 - line1 + 1; i++) {
chksignals();
subline(i, nth);
next = getindex(nextln(line));
subline(line, nth);
/*
* The substitution command can add lines, so
* we have to skip lines until we find the
* index that we saved before the substitution
*/
do
line = nextln(line);
while (getindex(line) != next);
}
}