show only non-bare remotes
[hband-tools.git] / user-tools / reportcmdstatus
blob44e2fc0179b22d4e523bf18381439d21c2231faf
1 #!/usr/bin/env perl
3 use POSIX;
4 use Getopt::Long qw/:config no_ignore_case permute/;
5 use Pod::Usage;
6 use IPC::Run qw/start finish/;
8 $0 = "reportcmdstatus";
10 $report_start = 0;
11 $clone_status = 0;
13 GetOptions(
14 's|report-start!' => \$report_start,
15 'c|clone!' => \$clone_status,
16 'help|?' => sub{ pod2usage(-exitval=>0, -verbose=>99); },
17 '<>' => sub{ unshift @ARGV, @_; die '!FINISH'; },
18 ) or pod2usage(-exitval=>2, -verbose=>99);
20 if($report_start)
22 eval q{ use String::ShellQuote; 1; } or die $@;
23 $COMMAND_STRING_QUOTED = join(' ', map {String::ShellQuote::shell_quote($_)} @ARGV);
26 sub child_process_forked
28 if($report_start)
30 printf STDERR "starting process %d: %s\n", $$, $COMMAND_STRING_QUOTED;
34 $ipc = start [@ARGV], init=>\&child_process_forked;
35 $COMMAND_PID = $ipc->{KIDS}->[0]->{PID};
36 finish $ipc;
37 $status = $?;
39 if(WIFSIGNALED($status))
41 printf STDERR "command (pid $COMMAND_PID) terminated by signal: %d\n", WTERMSIG($status);
42 $mystatus = 128 + WTERMSIG($status);
44 else
46 printf STDERR "command (pid $COMMAND_PID) exited with status: %d\n", WEXITSTATUS($status);
47 $mystatus = WEXITSTATUS($status);
50 exit $mystatus if $clone_status;
51 exit 0;
54 __END__
56 =pod
58 =head1 NAME
60 reportcmdstatus - Textually show how the given command finished (exit status/signal)
62 =head1 SYNOPSIS
64 reportcmdstatus [I<OPTIONS>] [--] I<COMMAND> [I<ARGS>]
66 =head1 OPTIONS
68 =over 4
70 =item -c, --clone
72 Take I<COMMAND>'s status and itself exits with it.
73 Default is to exit 0.
75 If I<COMMAND> did not exit normally, but it is terminated by a signal,
76 exit 128 + I<SIGNAL>, like most shells do.
78 =item -s, --report-start
80 Report what is being started, ie. I<COMMAND> I<ARGS>, to the STDERR.
82 =back
84 =cut