Skip to content

Commit 15c6086

Browse files
authored
ed: w! command should not update saved filename
* I found that the "w !CMD" case had the same problem as "r !CMD"; avoid updating $RememberedFilename when writing lines from buffer to an external command thru a pipe * Now init_pipe() takes an extra WriteFlag argument so edWrite() can use it too * For consistency add illegal_file() check in edWrite() before output file is opened * Also add quotewords() in edPipe() to allow system() to be passed an argument list * test1: "1,2w !rev" --> pipe buffer lines 1-2 into rev command * test2: "$w .." --> illegal_file() == TRUE * test3: "!cc secret.c" --> build secret project file
1 parent c43c895 commit 15c6086

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

bin/ed

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,10 @@ sub edPipe {
401401
return E_ADDREXT if defined $adrs[0];
402402

403403
if (defined $args[0]) {
404-
my $rc = system $args[0];
404+
return E_SUFFBAD if $args[0] =~ m/\0/;
405+
$args[0] =~ s/(\A\s+)|(\s+\z)//g;
406+
my @arglist = quotewords('\s+', 0, $args[0]);
407+
my $rc = system @arglist;
405408
print "$args[0]: $!\n" if ($rc == -1);
406409
}
407410
print "!\n";
@@ -628,9 +631,7 @@ sub illegal_file {
628631

629632
sub edWrite {
630633
my($AppendMode) = @_;
631-
my($fh, $filename, $chars, $qflag);
632-
633-
$chars = 0;
634+
my($fh, $filename, $chars, $qflag, $do_pipe);
634635

635636
if (!defined($adrs[0]) && !defined($adrs[1])) {
636637
$adrs[0] = 1;
@@ -649,21 +650,28 @@ sub edWrite {
649650
}
650651

651652
if (defined $args[0]) {
652-
$filename = $RememberedFilename = $args[0];
653+
if ($args[0] =~ s/\A\!//) {
654+
$do_pipe = 1;
655+
$fh = init_pipe($args[0], 1);
656+
return E_OPEN unless $fh;
657+
} else {
658+
$filename = $RememberedFilename = $args[0];
659+
}
653660
} elsif (defined $RememberedFilename) {
654661
$filename = $RememberedFilename;
655662
} else {
656663
return E_NOFILE;
657664
}
658-
if ($filename =~ s/\A\!//) {
659-
return unless (open $fh, "| $filename"); # no error
660-
} else {
665+
666+
unless ($do_pipe) {
667+
return E_FNAME if illegal_file($filename);
661668
my $mode = $AppendMode ? '>>' : '>';
662669
unless (open $fh, $mode, $filename) {
663670
warn "$filename: $!\n";
664671
return E_OPEN;
665672
}
666673
}
674+
$chars = 0;
667675
for my $line (@lines[$adrs[0]..$adrs[1]]) {
668676
print {$fh} $line;
669677
$chars += length($line);
@@ -782,13 +790,15 @@ sub edEdit {
782790
}
783791

784792
sub init_pipe {
785-
my $cmd = shift;
793+
my ($cmd, $writemode) = @_;
794+
786795
return if $cmd =~ m/\0/;
787796
return unless $cmd =~ m/\S/;
788797
$cmd =~ s/(\A\s+)|(\s+\z)//g;
789798
my @arglist = quotewords('\s+', 0, $cmd);
790799
my $fh;
791-
unless (open $fh, '-|', @arglist) {
800+
my $mode = $writemode ? '|-' : '-|';
801+
unless (open $fh, $mode, @arglist) {
792802
warn "open: $!\n";
793803
return;
794804
}

0 commit comments

Comments
 (0)