Skip to content

Commit

Permalink
ed: shell command io (#824)
Browse files Browse the repository at this point in the history
* Reject filenames starting with '!' for f command (other versions reject this)
* Implement "r !cmd" in edEdit() to read output of a shell command into the editor buffer
* Implement "w !cmd" in edWrite() to pass data from editor buffer to a shell command
* test1: 2r !ifconfig ---> output from ifconfig is added after line 2
* test2: 1,20w !cat -n ---> lines 1-20 from buffer are piped to "cat -n"
  • Loading branch information
mknos authored Nov 19, 2024
1 parent 822219a commit 3f84fd3
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ my $NO_QUESTIONS_MODE = 0;
my $PRINT_NUM = 1;
my $PRINT_BIN = 2;

our $VERSION = '0.17';
our $VERSION = '0.18';

my @ESC = (
'\\000', '\\001', '\\002', '\\003', '\\004', '\\005', '\\006', '\\a',
Expand Down Expand Up @@ -603,6 +603,10 @@ sub edFilename {
return;
}
if (defined($args[0])) {
if ($args[0] =~ m/\A\!/) {
edWarn(E_FNAME);
return;
}
$RememberedFilename = $args[0];
}
if (defined($RememberedFilename)) {
Expand Down Expand Up @@ -650,13 +654,16 @@ sub edWrite {
edWarn(E_NOFILE);
return;
}
my $mode = $AppendMode ? '>>' : '>';
unless (open $fh, $mode, $filename) {
warn "$filename: $!\n";
edWarn(E_OPEN);
return;
if ($filename =~ s/\A\!//) {
return unless (open $fh, "| $filename"); # no error
} else {
my $mode = $AppendMode ? '>>' : '>';
unless (open $fh, $mode, $filename) {
warn "$filename: $!\n";
edWarn(E_OPEN);
return;
}
}

for my $line (@lines[$adrs[0]..$adrs[1]]) {
print {$fh} $line;
$chars += length($line);
Expand Down Expand Up @@ -726,12 +733,15 @@ sub edEdit {
edWarn(E_READ);
return 0;
}
unless (open $fh, '<', $filename) {
warn "$filename: $!\n";
edWarn(E_OPEN);
return 0;
if ($filename =~ s/\A\!//) {
return unless (open $fh, "$filename |"); # no error
} else {
unless (open $fh, '<', $filename) {
warn "$filename: $!\n";
edWarn(E_OPEN);
return 0;
}
}

$chars = 0;
while (<$fh>) {
push @tmp_lines, $_;
Expand Down

0 comments on commit 3f84fd3

Please sign in to comment.