Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply: capture system() failure #376

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions bin/apply
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ License: perl

use strict;

use File::Basename qw(basename);

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my ($VERSION) = '1.2';

sub usage () {
require File::Basename;
$0 = File::Basename::basename ($0);
print <<EOF;
$0 (Perl bin utils) $VERSION
$0 [-ac] [-#] command argument [argument ...]
EOF
exit EX_FAILURE;
};
my $Program = basename($0);
my ($VERSION) = '1.3';

my $argc = 1;
my $magic = '%';
Expand All @@ -48,7 +41,7 @@ while (@ARGV) {
next;
}
if ($ARGV [0] =~ /^-/) {
usage; # usage will exit;
usage(); # usage will exit
}
last;
}
Expand All @@ -65,23 +58,43 @@ if (@thingies) { # Ignore $argc, found our own.
}

# Now, apply the command till we run out.
my $err = EX_SUCCESS;
while (@ARGV && @ARGV >= $argc) {
if (@thingies) {
(my $new_command = $command) =~ s/${magic}(\d+)/$ARGV [$1 - 1]/ge;
system $new_command; # Reinterpreted by the shell!
my $rc = system $new_command; # Reinterpreted by the shell!
checkerr($rc);
splice @ARGV, 0, $argc;
}
else {
if ($argc) {
system $command, splice @ARGV, 0, $argc;
my $rc = system $command, splice @ARGV, 0, $argc;
checkerr($rc);
}
else {
system $command;
my $rc = system $command;
checkerr($rc);
shift;
}
}
}
exit EX_SUCCESS;
exit $err;

sub checkerr {
my $status = shift;
if ($status != 0) {
briandfoy marked this conversation as resolved.
Show resolved Hide resolved
if ($status == -1) {
warn "$Program: command failed: $!\n";
}
$err = EX_FAILURE;
}
}

sub usage {
warn "$Program (Perl bin utils) $VERSION\n";
warn "usage: $Program [-ac] [-#] command argument [argument ...]\n";
exit EX_FAILURE;
}

__END__

Expand Down