blib/lib/Zoidberg/Shell.pm

filter: Statementstmt 49.6% 71 / 143 Branchbran 35.5% 27 / 76 Conditioncond 30.4% 7 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 37.1% 13 / 35 Podpod 78.5% 11 / 14 totaltotal 42.1% 129 / 306 CC 53 SCAR 23.6
CC
53
Cov
43%
set
30.0
_return
20.5
_shell_cmd
20.0
CRAP 10.6
« blib/lib/Zoidberg/Utils/FileSystem.pm ↑ Summary blib/lib/Zoidberg/Utils.pm »
1 package Zoidberg::Shell;
2  
3 64 our $VERSION = '0.981';
4  
5 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 64 ▶use vars qw/$AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 64 ▶use Zoidberg::Utils qw/:error :output path getopt/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use Exporter::Tidy
9 64 ▶ default => [qw/AUTOLOAD shell command builtin/],
Subroutine BEGIN: called CC=1 SCAR=0.0
10 command => [qw/shell command builtin/],
11 jobs => [qw/job @JOBS/],
12 data => [qw/alias unalias set setting/],
13 64 other => [qw/source readpipe/];
14  
15 64 our @JOBS;
16 64 tie @JOBS, 'Zoidberg::Shell::JobsArray';
17  
18 16 ▶sub new {
Subroutine new: called CC=7 SCAR=21.3
Pod documented
19 16 shift; #class
20 16 ▶ eval 'use Zoidberg';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
22  
23 # reformat some settings
24 16 my %obj = @_;
25 16 ▶ my $set = $obj{settings} || {};
Condition50%1/2
$obj{'settings'} || {}
l!l
160
MC/DC0%0/1
$obj{'settings'} || {}
$obj{'settings'}
Truth table50%1/2
A
A= $obj{'settings'}
Aresult
11
00
26 16 ▶ $$set{rcfiles} = [] if exists $$set{norc} and delete $$set{norc};
Branch: 1 branch
BranchTrueFalseTotal
if exists $set->{"norc"} and delete $set->{"norc"} 0 16 16
Condition33%1/3
exists $set->{"norc"} and delete $set->{"norc"}
!ll&&!rl&&r
1600
MC/DC0%0/2
exists $set->{"norc"} and delete $set->{"norc"}
exists $set->{"norc"} delete $set->{"norc"}
Truth table33%1/3
A and B
A= exists $set->{"norc"}B= delete $set->{"norc"}
ABresult
0X0
100
111
27 $$set{interactive} = (-t STDIN and -t STDOUT)
28 16 ▶ unless defined $$set{interactive};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set->{"interactive"} 16 0 16
Condition33%1/3
-t STDIN && -t STDOUT
!ll&&!rl&&r
1600
MC/DC0%0/2
-t STDIN && -t STDOUT
-t STDIN -t STDOUT
Truth table33%1/3
A && B
A= -t STDINB= -t STDOUT
ABresult
0X0
100
111
29 16 $obj{settings} = $set;
30  
31 16 Zoidberg->new(%obj);
32 }
33  
34 122 ▶sub current { return $Zoidberg::CURRENT }
Subroutine current: called CC=1 SCAR=0.0
Pod documented
35  
36 0 ▶sub any {
Subroutine any: not called CC=2 SCAR=17.9
Pod documented
37 0 ▶ goto &new unless $Zoidberg::CURRENT;
Branch: 1 branch
BranchTrueFalseTotal
unless $Zoidberg::CURRENT 0 0 0
38 0 return $Zoidberg::CURRENT;
39 }
40  
41 341 ▶sub _self { ( eval{ $_[0]->isa( __PACKAGE__ ) } ) ? shift : $Zoidberg::CURRENT }
Subroutine _self: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_[0]->isa(__PACKAGE__) } } ? : 271 70 341
42  
43 # ################ #
44 # Parser interface #
45 # ################ #
46  
47 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
48 ## Code inspired by Shell.pm ##
49 0 my $cmd = (split/::/, $AUTOLOAD)[-1];
50 0 ▶ return undef if $cmd eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "DESTROY" 0 0 0
51 0 ▶ shift if ref($_[0]) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 0 0
52 0 debug "Zoidberg::Shell::AUTOLOAD got $cmd";
53 0 @_ = ([{plain_words => 1}, $cmd, @_]); # force words
54 0 goto \&shell;
55 }
56  
57 217 ▶sub shell { # FIXME FIXME should not return after ^Z
Subroutine shell: called CC=8 SCAR=20.9
Pod documented
58 217 my $self = &_self;
59 217 ▶ my $meta = (ref($_[0]) eq 'HASH') ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 35 182 217
60 217 ▶ my $pipe = ($_[0] =~ /^-\||\|-$/) ? shift : undef ;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^-\||\|-$/ ? : 0 217 217
61 217 ▶ todo 'pipeline syntax' if $pipe;
Branch: 1 branch
BranchTrueFalseTotal
if $pipe 0 217 217
62 217 my $save_error = $$self{error};
63 217 ▶ $$self{fg_job} ||= $self;
Condition66%2/3
$self->{"fg_job"} ||= $self
l!l&&r!l&&!r
201160
MC/DC0%0/2
$self->{"fg_job"} ||= $self
$self->{"fg_job"} $self
Truth table66%2/3
A ||= B
A= $self->{"fg_job"}B= $self
ABresult
1X1
011
000
64 217 $$meta{capture} = defined wantarray;
65 217 $$meta{wantarray} = wantarray;
66 local $$self{_settings}{output}{error} = 'mute'
67 217 ▶ if delete $$meta{die_silently};
Branch: 1 branch
BranchTrueFalseTotal
if delete $meta->{"die_silently"} 0 217 217
68 217 my @re;
69 217 ▶ if (grep {ref $_} @_) { @re = $$self{fg_job}->shell_list($meta, @_) }
Branch: 2 branches
BranchTrueFalseTotal
if (grep {ref $_;} @_) { } 23 194 217
elsif (@_ > 1) { } 49 145 194
70 49 elsif (@_ > 1) { @re = $$self{fg_job}->shell_list($meta, \@_) }
71 145 else { @re = $self->shell_string($meta, @_) }
72 201 $@ = $$self{error};
73 201 $$self{error} = $save_error;
74 201 _return($@, @re);
75 }
76  
77 201 ▶sub _return { # fairly complex output logic :S
Subroutine _return: called CC=12 SCAR=30.2
78 # the heuristic is that list context is the fastest .. can this be optimised further ?
79 201 ▶ my $error = shift() ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 4 197 201
80 201 ▶ unless (@_) { return Zoidberg::Utils::Output::Scalar->new($error, '') }
Branch: 3 branches
BranchTrueFalseTotal
if (@_) { } 126 75 201
if (do { wantarray }) { } 9 117 126
elsif (not grep(ref($_), @_)) { } 110 7 117
81 elsif (wantarray) {
82 return map { # try to do the same list heuristics as utils::output
83 9 ▶ (ref($_) eq 'ARRAY' and ! grep ref($_), @$_) ? (@$_) : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' && !grep(ref($_), @$_) ? : 0 9 9
Condition33%1/3
ref $_ eq "ARRAY" and not grep(ref($_), @$_)
!ll&&!rl&&r
900
MC/DC0%0/2
ref $_ eq "ARRAY" and not grep(ref($_), @$_)
ref $_ eq "ARRAY" not grep(ref($_), @$_)
Truth table33%1/3
A and not(B)
A= ref $_ eq "ARRAY"B= not grep(ref($_), @$_)
ABresult
0X0
100
111
84 } @_;
85 }
86 elsif (! grep ref($_), @_) { # only strings
87 110 return Zoidberg::Utils::Output::Scalar->new($error, join('', @_)) ;
88 }
89 else { # one or more refs in @_
90 7 ▶ if (@_ == 1) { # must be ref
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 7 0 7
91 7 ▶ return( (ref($_[0]) eq 'ARRAY' and ! grep ref($_), @{$_[0]})
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' && !grep(ref($_), @{$_[0];}) ? : 7 0 7
Condition33%1/3
ref $_[0] eq "ARRAY" and not grep(ref($_), @{$_[0];})
!ll&&!rl&&r
007
MC/DC0%0/2
ref $_[0] eq "ARRAY" and not grep(ref($_), @{$_[0];})
ref $_[0] eq "ARRAY" not grep(ref($_), @{$_[0];})
Truth table33%1/3
A and not(B)
A= ref $_[0] eq "ARRAY"B= not grep(ref($_), @{$_[0];})
ABresult
0X0
100
111
92 ? Zoidberg::Utils::Output::Scalar->new($error, undef, shift())
93 : Zoidberg::Utils::Output::Scalar->new($error, shift() ) ) ;
94 }
95 else { # mixed data - hope we get it right
96 return Zoidberg::Utils::Output::Scalar->new($error, undef, [ map {
97 0 ▶ (ref($_) eq 'ARRAY' and ! grep ref($_), @$_) ? (@$_) : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' && !grep(ref($_), @$_) ? : 0 0 0
Condition0%0/3
ref $_ eq "ARRAY" and not grep(ref($_), @$_)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq "ARRAY" and not grep(ref($_), @$_)
ref $_ eq "ARRAY" not grep(ref($_), @$_)
Truth table0%0/3
A and not(B)
A= ref $_ eq "ARRAY"B= not grep(ref($_), @$_)
ABresult
0X0
100
111
98 } @_ ] ) ;
99 }
100 }
101 }
102  
103 0 ▶sub builtin {
Subroutine builtin: not called CC=1 SCAR=6.9
Pod undocumented
104 0 unshift @_, 'builtin';
105 0 goto \&_shell_cmd;
106 }
107  
108 0 ▶sub command {
Subroutine command: not called CC=1 SCAR=6.9
Pod undocumented
109 0 unshift @_, 'system';
110 0 goto \&_shell_cmd;
111 }
112  
113 0 ▶sub _shell_cmd {
Subroutine _shell_cmd: not called CC=4 SCAR=30.0
114 0 my $type = shift;
115 0 my $self = &_self;
116 0 ▶ my $meta = (ref($_[0]) eq 'HASH') ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
117 0 $$meta{capture} = defined wantarray;
118 0 $$meta{wantarray} = wantarray;
119 local $$self{_settings}{output}{error} = 'mute'
120 0 ▶ if delete $$meta{die_silently};
Branch: 1 branch
BranchTrueFalseTotal
if delete $meta->{"die_silently"} 0 0 0
121 0 my $save_error = $$self{error};
122 0 ▶ $$self{fg_job} ||= $self;
Condition0%0/3
$self->{"fg_job"} ||= $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"fg_job"} ||= $self
$self->{"fg_job"} $self
Truth table0%0/3
A ||= B
A= $self->{"fg_job"}B= $self
ABresult
1X1
011
000
123 0 my @re = $$self{fg_job}->shell_job( $meta,
124 [{context => 'CMD', cmdtype => $type}, @_] );
125 0 $@ = $$self{error};
126 0 $$self{error} = $save_error;
127 0 _return($@, @re);
128 }
129  
130 0 ▶sub readpipe { # TODO handle STDOUT differently, see perlop
Subroutine readpipe: not called CC=2 SCAR=17.9
Pod undocumented
131 0 my $self = &_self;
132 0 my @re = $self->shell_string( {capture => 1}, @_ );
133 0 ▶ return wantarray ? @re : join('', @re);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
134 }
135  
136 0 ▶sub system {
Subroutine system: not called CC=3 SCAR=24.8
Pod documented
137 # quick parser independent way of calling system commands
138 # not exported to avoid conflict with perlfunc system
139 0 my $self = &_self;
140 0 ▶ open CMD, '|-', @_ or error "Could not open pipe to: $_[0]";
Branch: 1 branch
BranchTrueFalseTotal
open CMD, "|-", @_ or error("Could not open pipe to: $_[0]") 0 0 0
141 0 my @re = (<CMD>);
142 0 close CMD;
143 0 ▶ return wantarray ? @re : join('', @re);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
144 # FIXME where does the error code of cmd go?
145 }
146  
147 # ############# #
148 # Some builtins #
149 # ############# #
150  
151 63 ▶sub alias {
Subroutine alias: called CC=2 SCAR=6.9
Pod documented
152 63 my $self = &_self;
153 63 my (undef, $hash) = getopt '%', @_;
154 63 $$self{aliases}{$_} = $$hash{$_} for keys %$hash;
155 # TODO merge code from Commands here
156 }
157  
158 0 ▶sub unalias {
Subroutine unalias: not called CC=4 SCAR=30.0
Pod documented
159 0 my $self = &_self;
160 0 my ($opts, $args) = getopt 'all,a @', @_;
161 0 ▶ if ($$opts{all}) { %{$$self{aliases}} = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'all'}) { } 0 0 0
162 else {
163 0 for (@$args) {
164 error "alias: $_: not found"
165 0 ▶ unless delete $$self{aliases}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $self->{"aliases"}{$_} 0 0 0
166 }
167 }
168 # TODO merge code from Commands here
169 # use path2hashref
170 }
171  
172 0 ▶sub set {
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
173 # TODO merge code from Commands here
174 # use path2hashref
175 0 my $self = &_self;
176 0 ▶ if (ref($_[0]) eq 'HASH') { # perl style, merge hashes
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (ref $_[0]) { } 0 0 0
177 0 ▶ error 'set: only first argument is used' if $#_;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ 0 0 0
178 0 %{$self->{settings}} = ( %{$self->{settings}}, %{$_[0]} );
179 }
180 0 elsif (ref $_[0]) { error 'set: no support for data type'.ref($_[0]) }
181 0 else { $self->{settings}{$_}++ for @_ }
182 }
183  
184 0 ▶sub setting {
Subroutine setting: not called CC=2 SCAR=17.9
Pod documented
185 # use path2hashref
186 0 my $self = &_self;
187 0 my $key = shift;
188 0 ▶ return exists($$self{settings}{$key}) ? $$self{settings}{$key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'settings'}{$key} ? : 0 0 0
189 }
190  
191 61 ▶sub source {
Subroutine source: called CC=4 SCAR=14.0
Pod documented
192 61 my $self = &_self;
193 61 local $Zoidberg::CURRENT = $self;
194 61 for my $file (@_) {
195 61 my $file = path($file);
196 61 ▶ error "source: no such file: $file" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 61 61
197 61 debug "going to source: $file";
198 # FIXME more intelligent behaviour -- see bash man page
199 61 eval q{package Main; our $SCRIPT = $file; do $file; die $@ if $@ };
200 # FIXME wipe Main
201 61 ▶ complain if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 61 61
202 }
203 }
204  
205 0 ▶sub job { $Zoidberg::CURRENT->job_by_spec(pop @_) }
Subroutine job: not called CC=1 SCAR=6.9
Pod documented
206  
207 package Zoidberg::Shell::JobsArray;
208  
209 64 our $VERSION = '0.981';
210  
211 64 ▶sub TIEARRAY { bless \$Zoidberg::Shell::VERSION, shift } # what else is there to bless ?
Subroutine TIEARRAY: called CC=1 SCAR=0.0
212  
213 0 ▶sub FETCH { $_[1] ? $Zoidberg::CURRENT->job_by_id($_[1]) : $$Zoidberg::CURRENT{fg_job} }
Subroutine FETCH: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 0 0
214  
215 0 ▶sub STORE { die "Can't overwrite jobs, try 'push'\n" } # STORE has no meaning for this thing
Subroutine STORE: not called CC=1 SCAR=6.9
216  
217 0 ▶sub DELETE {
Subroutine DELETE: not called CC=2 SCAR=17.9
218 0 my $j = $Zoidberg::CURRENT->job_by_id($_[1]);
219 0 ▶ return unless $j;
Branch: 1 branch
BranchTrueFalseTotal
unless $j 0 0 0
220 0 $j->kill(undef, 'WIPE');
221 }
222  
223 0 ▶sub POP {
Subroutine POP: not called CC=3 SCAR=24.8
224 0 my $last_id;
225 $$_{id} > $last_id and $last_id = $$_{id}
226 0 ▶ for @{$Zoidberg::CURRENT->{jobs}};
Branch: 1 branch
BranchTrueFalseTotal
$_->{"id"} > $last_id and $last_id = $_->{"id"} 0 0 0
227 0 $_[0]->DELETE($last_id);
228 }
229  
230 0 ▶sub SHIFT { $_[0]->DELETE(1) }
Subroutine SHIFT: not called CC=1 SCAR=6.9
231  
232 0 ▶sub PUSH {
Subroutine PUSH: not called CC=2 SCAR=17.9
233 0 ▶ ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
234 ? $Zoidberg::CURRENT->shell_list( {prepare => 1}, $_[1] )
235 : $Zoidberg::CURRENT->shell_string( {prepare => 1}, $_[1] ) ;
236 }
237  
238 0 ▶sub UNSHIFT { die "Can't overwrite jobs, try 'push'\n" }
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
239  
240 0 ▶sub FETCHSIZE { 1 + scalar @{ $Zoidberg::CURRENT->{jobs} } }
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
241  
242 0 ▶sub EXISTS { $Zoidberg::CURRENT->job_by_id($_[1]) }
Subroutine EXISTS: not called CC=1 SCAR=6.9
243  
244 0 ▶sub CLEAR { $_->kill(undef, 'WIPE') for @{$Zoidberg::CURRENT->{jobs}} }
Subroutine CLEAR: not called CC=2 SCAR=17.9
245  
246 0 ▶sub SPLICE { die "Can't overwrite jobs, try 'delete' or 'push'\n" }
Subroutine SPLICE: not called CC=1 SCAR=6.9
247  
248 64 1;
249  
250 __END__
« blib/lib/Zoidberg/Utils/FileSystem.pm ↑ Summary blib/lib/Zoidberg/Utils.pm »