Skip to content

Commit

Permalink
ed: implement ! command
Browse files Browse the repository at this point in the history
* Take a hint from the TODO comments
* The big regex in edParse() doesn't help because spaces have a special meaning; the new branch bypasses it
* "!" line is printed at end of command output (or after error message if command can't be started)
* Ignore if the command exits with non-zero status; print error only if command doesn't start
* Tested against GNU and OpenBSD versions
* test1: shell pattern: "!for i in 1 2 3; do echo $i; done"
* test2: zero exit status command: "!ls"
* test3: non-zero exit status command: "!false"
  • Loading branch information
mknos authored Jan 10, 2024
1 parent 0205f6d commit 6f111c8
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ License: gpl
# u - undo
# v - global command "inVerted"
# x - get encryption key
# ! - shell command
#
# - Create regression test suite...test against "real" ed.
# - add a "-e" flag to allow it to be used in sed(1) like fashion.
Expand Down Expand Up @@ -230,6 +229,8 @@ while (1) {

if (!length($command)) {
&edSetCurrentLine;
} elsif ($command eq '!') {
edPipe();
} elsif ($command eq '=') {
&edPrintLineNum;

Expand Down Expand Up @@ -395,6 +396,15 @@ sub escape_line {
return $s;
}

# does not modify buffer
sub edPipe {
return unless defined $args[0];
my $rc = system $args[0];
print "$args[0]: $!\n" if ($rc == -1);
print "!\n";
return;
}

# merge lines back into $lines[$adrs[0]]
sub edJoin {
if (defined($args[0])) {
Expand Down Expand Up @@ -933,13 +943,18 @@ sub edSetCurrentLine {

sub edParse {

#
# Parse commands....and yes, this could be done a lot more elegantly
# with fancy regexps
#

@adrs = ();

my $cmdline = $_;
$cmdline =~ s/\A\s+//;

if (substr($cmdline, 0, 1) eq '!') {
$command = '!';
$cmdline =~ s/\A.\s*//;
$args[0] = $cmdline;
return 1;
}

my @fields =
(/^(
(
Expand Down

0 comments on commit 6f111c8

Please sign in to comment.