From 3f84fd3c69f0d26fa0609b1f9317af967a1fd6fc Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Tue, 19 Nov 2024 17:31:32 +0800 Subject: [PATCH] ed: shell command io (#824) * 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" --- bin/ed | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/bin/ed b/bin/ed index 7bf92ca8..02bc8fa3 100755 --- a/bin/ed +++ b/bin/ed @@ -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', @@ -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)) { @@ -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); @@ -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, $_;