@@ -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
6560use strict;
6661
@@ -96,6 +91,7 @@ my $command; # single letter command entered by user
9691my $commandsuf ; # single letter modifier of command
9792my @adrs ; # 1 or 2 line numbers for commands to operate on
9893my @args ; # command arguments (filenames, search patterns...)
94+ my @inbuf ;
9995
10096my $EXTENDED_MESSAGES = 0;
10197
@@ -110,7 +106,7 @@ my $NO_QUESTIONS_MODE = 0;
110106my $PRINT_NUM = 1;
111107my $PRINT_BIN = 2;
112108
113- our $VERSION = ' 0.6 ' ;
109+ our $VERSION = ' 0.7 ' ;
114110
115111my @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 /\A g\/ // ) {
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
11041122sub Usage {
11051123 die " Usage: ed [-p prompt] [-ds] [file]\n " ;
0 commit comments