line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Original author: Paul Johnson |
2
|
|
|
|
|
|
|
# Created: Fri 12 Mar 1999 10:25:51 am |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package System::Explain; |
5
|
2
|
|
|
2
|
|
97905
|
use 5.008001; |
|
2
|
|
|
|
|
18
|
|
6
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
39
|
|
7
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
84
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=encoding utf-8 |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
System::Explain - run a system command and explain the result |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use System::Explain "command, verbose, errors"; |
20
|
|
|
|
|
|
|
sys qw(ls -al); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
The C function runs a system command, checks the result, and comments on |
23
|
|
|
|
|
|
|
it to STDOUT. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
System::Explain is a standalone release of L, part of L |
28
|
|
|
|
|
|
|
v1.20 and earlier. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 FUNCTIONS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
2
|
|
847
|
use parent 'Exporter'; |
|
2
|
|
|
|
|
294
|
|
|
2
|
|
|
|
|
13
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our @EXPORT = qw(sys dsys); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $Command = 0; |
39
|
|
|
|
|
|
|
my $Errors = 0; |
40
|
|
|
|
|
|
|
my $Verbose = 0; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 import |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Say C |
45
|
|
|
|
|
|
|
The options are: C (to print the command before running it), |
46
|
|
|
|
|
|
|
C (to report on the exit status), and C (to do both of those). |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub import |
51
|
|
|
|
|
|
|
{ |
52
|
2
|
|
|
2
|
|
16
|
my $class = shift; |
53
|
2
|
|
|
|
|
7
|
my $args = "@_"; |
54
|
2
|
|
|
|
|
8
|
$Command = $args =~ /\bcommand\b/i; |
55
|
2
|
|
|
|
|
5
|
$Errors = $args =~ /\berror\b/i; |
56
|
2
|
|
|
|
|
4
|
$Verbose = $args =~ /\bverbose\b/i; |
57
|
2
|
|
66
|
|
|
20
|
$Command ||= $Verbose; |
58
|
2
|
|
66
|
|
|
11
|
$Errors ||= $Verbose; |
59
|
2
|
100
|
|
|
|
93
|
$class->export_to_level(1, "sys") if $args =~ /\bsys\b/i; |
60
|
2
|
100
|
|
|
|
1632
|
$class->export_to_level(1, "dsys") if $args =~ /\bdsys\b/i; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 sys |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
C runs C<@command> (by passing C<@command> to C) and |
66
|
|
|
|
|
|
|
optionally prints human-readable information about the result (specifically, |
67
|
|
|
|
|
|
|
about the return value of C). |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Returns the return value of the C call. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub sys |
74
|
|
|
|
|
|
|
{ |
75
|
2
|
|
|
2
|
0
|
1610
|
my (@command) = @_; |
76
|
2
|
|
|
|
|
10
|
local $| = 1; |
77
|
2
|
50
|
|
|
|
63
|
print "@command" if $Command; |
78
|
2
|
|
|
|
|
8424
|
my $rc = 0xffff & system @command; |
79
|
2
|
50
|
66
|
|
|
163
|
print "\n" if $Command && !$rc && !$Verbose; |
|
|
|
66
|
|
|
|
|
80
|
2
|
|
|
|
|
64
|
_print_explanation_of($rc); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 dsys |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
As L, but dies if the C call fails. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub dsys |
90
|
|
|
|
|
|
|
{ |
91
|
1
|
50
|
|
1
|
0
|
3901
|
die "@_ failed" if sys @_; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Print the explanation |
95
|
|
|
|
|
|
|
sub _print_explanation_of |
96
|
|
|
|
|
|
|
{ |
97
|
2
|
|
|
2
|
|
21
|
my ($rc) = @_; |
98
|
2
|
100
|
66
|
|
|
98
|
printf " returned %#04x: ", $rc if $Errors && $rc; |
99
|
2
|
100
|
|
|
|
46
|
if ($rc == 0) |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
100
|
|
|
|
|
|
|
{ |
101
|
1
|
50
|
|
|
|
95
|
print "ran with normal exit\n" if $Verbose; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
elsif ($rc == 0xff00) |
104
|
|
|
|
|
|
|
{ |
105
|
1
|
50
|
|
|
|
50
|
print "command failed: $!\n" if $Errors; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
elsif ($rc > 0x80) |
108
|
|
|
|
|
|
|
{ |
109
|
0
|
|
|
|
|
0
|
$rc >>= 8; |
110
|
0
|
0
|
|
|
|
0
|
print "ran with non-zero exit status $rc\n" if $Errors; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
0
|
|
|
|
0
|
print "ran with " if $Errors; |
115
|
0
|
0
|
|
|
|
0
|
if ($rc & 0x80) |
116
|
|
|
|
|
|
|
{ |
117
|
0
|
|
|
|
|
0
|
$rc &= ~0x80; |
118
|
0
|
0
|
|
|
|
0
|
print "coredump from " if $Errors; |
119
|
|
|
|
|
|
|
} |
120
|
0
|
0
|
|
|
|
0
|
print "signal $rc\n" if $Errors; |
121
|
|
|
|
|
|
|
} |
122
|
2
|
|
|
|
|
138
|
return $rc; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
126
|
|
|
|
|
|
|
__END__ |