Skip to content

Commit 437e404

Browse files
authored
ed: initial support for global (g) command (#765)
* Basic usage of g command is to show lines matching a pattern * More advanced use of g command would be to modify the buffer; this needs more work * test1: g/text not found/p ---> no error is printed (consistent with BSD and GNU version) * test2: g/include/p ---> print matching lines * test3: g/include/n ---> prefix matching lines with line number * test4: g/t/l ---> matching lines are listed in with escapes
1 parent 78d5429 commit 437e404

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

bin/ed

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ License: gpl
1313

1414
# What: A perl version of Unix ed editor.
1515
#
16-
# Based on the v7 documentation using GNU ed version 0.2 as a reference.
17-
#
1816
# Currently implemented:
1917
# - most addressing modes (".","$","#","/pat/","?pat?","[+-]#, etc.)
2018
# - command parsing
@@ -49,18 +47,15 @@ License: gpl
4947
#
5048
# Todo:
5149
# - Implement the following commands from the v7 docs:
52-
# g - global command
5350
# k - mark
5451
# u - undo
5552
# v - global command "inVerted"
5653
#
57-
# - Create regression test suite...test against "real" ed.
5854
# - add a "-e" flag to allow it to be used in sed(1) like fashion.
5955
# - add buffer size limitations for strict compatability
6056
# - discard NULS, chars after \n
6157
# - refuse to read non-ascii files
6258
# - Add BSD/GNU extentions
63-
#
6459

6560
use strict;
6661

@@ -96,6 +91,7 @@ my $command; # single letter command entered by user
9691
my $commandsuf; # single letter modifier of command
9792
my @adrs; # 1 or 2 line numbers for commands to operate on
9893
my @args; # command arguments (filenames, search patterns...)
94+
my @inbuf;
9995

10096
my $EXTENDED_MESSAGES = 0;
10197

@@ -110,7 +106,7 @@ my $NO_QUESTIONS_MODE = 0;
110106
my $PRINT_NUM = 1;
111107
my $PRINT_BIN = 2;
112108

113-
our $VERSION = '0.6';
109+
our $VERSION = '0.7';
114110

115111
my @ESC = (
116112
'\\000', '\\001', '\\002', '\\003', '\\004', '\\005', '\\006', '\\a',
@@ -215,7 +211,8 @@ sub input {
215211
@args = ();
216212

217213
print $Prompt if defined $Prompt;
218-
unless ($_ = <>) {
214+
$_ = @inbuf ? shift @inbuf : <>;
215+
unless (defined $_) {
219216
edQuitAsk() or return;
220217
}
221218
chomp;
@@ -909,6 +906,22 @@ sub edParse {
909906
$_ = $found . 'p';
910907
return edParse();
911908
}
909+
if (s/\Ag\///) {
910+
my $end = rindex $_, '/';
911+
return 0 if $end == -1; # g/re/p needs trailing /
912+
my $pat = substr $_, 0, $end;
913+
my $repcmd = substr $_, $end + 1;
914+
my @found = edSearchGlobal($pat);
915+
unless (@found) {
916+
$command = 'nop';
917+
return 1; # match failure is not an error
918+
}
919+
foreach my $i (@found) {
920+
push @inbuf, $i . $repcmd;
921+
}
922+
$command = 'nop';
923+
return 1;
924+
}
912925

913926
my @fields =
914927
(/^(
@@ -1094,12 +1107,17 @@ sub edSearchBackward {
10941107
return 0;
10951108
}
10961109

1110+
sub edSearchGlobal {
1111+
my($pattern) = @_;
10971112

1098-
#
1099-
# Print usage and exit
1100-
#
1101-
# Usage()
1102-
#
1113+
my @found;
1114+
for my $line (($CurrentLineNum + 1) .. maxline(), 1 .. $CurrentLineNum) {
1115+
if ($lines[$line] =~ /$pattern/) {
1116+
push @found, $line;
1117+
}
1118+
}
1119+
return @found;
1120+
}
11031121

11041122
sub Usage {
11051123
die "Usage: ed [-p prompt] [-ds] [file]\n";

0 commit comments

Comments
 (0)