From e03b37d70700e9ff5c5dc7f0c2f0f15f098405cd Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Tue, 29 Oct 2024 13:36:50 +0800 Subject: [PATCH] ed: re-add support for /re/ address-mode * Standard ed allows a regex to be placed in front of a command * Previously CalculateLine() supported this but the function was replaced by getAddr() * Add a new condition in getAddr() to allow resolving /re/ command prefix to a line number * Be careful to support repeated search for empty pattern * test1: /include/n ---> search for /include/, resolve line number and run n command * test2: //l ---> repeat search using saved /include/ pattern, then run l command on next-match * test3: /return/ ---> no command given with pattern still works * test4: /a\/b/p --> search pattern includes escaped slash; run p command on result --- bin/ed | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bin/ed b/bin/ed index 099d70ff..34ebaa62 100755 --- a/bin/ed +++ b/bin/ed @@ -958,6 +958,25 @@ sub getAddr { $n = $CurrentLineNum; } elsif (s/\A\$//) { # '$' == max line $n = maxline(); + } elsif (s/\A\///) { # '/re/' == search + my $i; + my @chars = split //; + for ($i = 0; $i < scalar(@chars); $i++) { + my $j = $i - 1; + $j = 0 if $j < 0; + last if $chars[$i] eq '/' && $chars[$j] ne '\\'; + } + my $re = substr $_, 0, $i; + if (length($re) == 0) { + if (defined $SearchPat) { + $re = $SearchPat; + } else { + edWarn(E_NOPAT); + return 0; + } + } + $_ = substr $_, $i + 1; + $n = edSearchForward($re); } return $n; }