blib/script/zoid

filter: Statementstmt 59.3% 73 / 123 Branchbran 31.5% 29 / 92 Conditioncond 25.6% 10 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 83.3% 5 / 6 Podpod n/a totaltotal 41.6% 117 / 281 CC 6 SCAR 31.5
CC
6
Cov
44%
complain
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 23.4
« blib/lib/Zoidberg/Utils/Error.pm ↑ Summary blib/lib/Zoidberg/PluginHash.pm »
1 #!/usr/bin/env perl
2  
3 # ############################################################## #
4 # Copyright (c) 2011 Jaap G Karssenberg and Joel Berger. #
5 # All rights reserved. This program is free software; you can #
6 # redistribute it and/or modify it under the same terms as Perl #
7 # itself. #
8 # #
9 # This script is a frontend to the Zoidberg module, it starts #
10 # the Zoidberg perl shell. #
11 # #
12 # mailto:joel.a.berger@gmail.com #
13 # http://github.com/jberger/Zoidberg #
14 # ############################################################## #
15  
16 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 45 ▶use Cwd qw/cwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 45 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 45 our $VERSION = '0.981';
21  
22 45 my @inc = (); # You can list custom includes here
23 45 my $cwd = cwd;
24  
25 0 ▶unshift @INC, map { m!^/! ? $_ : "$cwd/$_" } @inc;
Branch: 1 branch
BranchTrueFalseTotal
m[^/] ? : 0 0 0
26  
27 45 $0 =~ s!(.*/)!!;
28 45 ▶if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 45 0 45
29 45 my $dir = $1;
30 45 ▶ $dir = "$cwd/$_" unless $dir =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ m[^/] 45 0 45
31 45 $Zoidberg::_base_dir = $dir;
32 45 ▶ unshift @INC, "$dir/lib" if -d "$dir/lib";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$dir/lib" 45 0 45
33 }
34 0 else { $Zoidberg::_base_dir = '' }
35  
36 # ########### #
37 # Get Options #
38 # ########### #
39  
40 45 ▶eval q#use Zoidberg::Utils::GetOpt 'getopt'; 1# or die $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use Zoidberg::Utils::GetOpt 'getopt'; 1" or die $@ 45 0 45
41  
42 45 my ($opts, $args) = eval { getopt( '
43 help,h,usage,u version,V config,C
44 exec,e@ command,c@ stdin,s
45 interactive,i login,l plug,p@
46 debug,-D,D verbose,v include,-I@
47 -I* -D* +o@ -o@ -m* -M*
48 @', @ARGV ) } ;
49 45 ▶if ($@) { # renice error message
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 45 45
50 0 ▶ print STDERR ref($@) ? $@->stringify(format => 'gnu') : $@;
Branch: 1 branch
BranchTrueFalseTotal
ref $@ ? : 0 0 0
51 0 exit 1;
52 }
53  
54 # TODO -q
55 # Quiet (usually without argument). Suppress normal result or
56 # diagnostic output. This is very common. Examples: ci(1), co(1), make(1).
57 # TODO find switch to set mode / include plugins
58  
59 45 ▶if ($$opts{help}) { # pre-emptive #1
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"help"}) 0 45 45
60 0 pod2usage(
61 -verbose => 1,
62 -exitval => 0,
63 );
64 }
65  
66 45 ▶if ($$opts{_opts}) { # special switches
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"_opts"}) 45 0 45
67 45 for (grep /^-[IDMm]./, @{$$opts{_opts}}) {
68 0 ▶ if ( s/^-I// ) { push @inc, $_ }
Branch: 2 branches
BranchTrueFalseTotal
if (s/^-I//) { } 0 0 0
elsif (s/^-D//) { } 0 0 0
69 elsif ( s/^-D// ) {
70 45 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 0 ${$_.'::DEBUG'}++;
72 }
73 else {
74 0 ▶ my $import = /^-M/ ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
/^-M/ ? : 0 0 0
75 0 ▶ my $use =
Branch: 2 branches
BranchTrueFalseTotal
/-[Mm](\S+)=(.*)/ ? : 0 0 0
s/^-M// ? : 0 0 0
76 /-[Mm](\S+)=(.*)/ ? "use $1 split(/,/,q{$2}); " :
77 s/^-M// ? "use $_; " : "use $_ (); " ;
78 0 $use =~ s/^use -/no /;
79 0 $$opts{use} .= $use;
80 }
81 }
82 }
83  
84 # ############### #
85 # set environment #
86 # ############### #
87  
88 45 my @user_info = getpwuid($>);
89 45 ▶$ENV{USER} ||= $user_info[0];
Condition33%1/3
$ENV{"USER"} ||= $user_info[0]
l!l&&r!l&&!r
0450
MC/DC0%0/2
$ENV{"USER"} ||= $user_info[0]
$ENV{"USER"} $user_info[0]
Truth table33%1/3
A ||= B
A= $ENV{"USER"}B= $user_info[0]
ABresult
1X1
011
000
90 45 ▶$ENV{HOME} ||= $user_info[7];
Condition33%1/3
$ENV{"HOME"} ||= $user_info[7]
l!l&&r!l&&!r
4500
MC/DC0%0/2
$ENV{"HOME"} ||= $user_info[7]
$ENV{"HOME"} $user_info[7]
Truth table33%1/3
A ||= B
A= $ENV{"HOME"}B= $user_info[7]
ABresult
1X1
011
000
91 45 $ENV{ZOID} = $0; # _Don't_ change this to ENV{SHELL} !
92  
93 # fix environment
94 45 ▶$$opts{login} = 1 unless $ENV{PWD}; # FIXME a better check ?
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"PWD"} 0 45 45
95 45 ▶if ($$opts{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'login'}) { } 0 45 45
96 0 $ENV{LOGNAME} = $ENV{USER} = $user_info[0];
97 0 $ENV{HOME} = $user_info[7];
98 0 ▶ $ENV{PWD} = $ENV{HOME} || '/';
Condition0%0/2
$ENV{'HOME'} || '/'
l!l
00
MC/DC0%0/1
$ENV{'HOME'} || '/'
$ENV{'HOME'}
Truth table0%0/2
A
A= $ENV{'HOME'}
Aresult
11
00
99 0 chdir $ENV{PWD} ;
100 }
101 45 else { $ENV{PWD} = $cwd }
102  
103 # ############# #
104 # Load includes #
105 # ############# #
106  
107 # parse includes
108 unshift @INC,
109 0 ▶ map { m!^/! ? $_ : "$cwd/$_" }
Branch: 1 branch
BranchTrueFalseTotal
m[^/] ? : 0 0 0
110 45 ▶ grep s/^-I//, @{ $$opts{_opts} } if $$opts{_opts};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"_opts"} 45 0 45
111  
112 # load Zoidberg.pm
113 45 ▶eval q#require Zoidberg# or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require Zoidberg" or die $@ 45 0 45
114  
115 45 ▶if ($$opts{version}) { # pre-emptive #2
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"version"}) 0 45 45
116 0 print "zoid $VERSION\n$Zoidberg::LONG_VERSION\n";
117 0 exit 0;
118 }
119  
120 # ############## #
121 # Parse settings #
122 # ############## #
123  
124 45 my %settings;
125  
126 45 ▶if ($$opts{'-o'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"-o"}) 45 0 45
127 45 for ( @{$$opts{'-o'}} ) {
128 90 my ($opt, $arg) = split '=', $_, 2;
129 90 ▶ $settings{$opt} = defined($arg) ? $arg : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 90 0 90
130 }
131 }
132 45 ▶if ($$opts{'+o'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"+o"}) 0 45 45
133 0 for ( @{$$opts{'+o'}} ) {
134 0 my ($opt, $arg) = split '=', $_, 2;
135 0 ▶ $settings{$opt} = defined($arg) ? $arg : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 0 0
136 }
137 }
138  
139 45 for (qw/data_dirs rcfiles/) { # arrays
140 $settings{$_} = [ split /:/, $settings{$_} ]
141 90 ▶ if defined $settings{$_} and ! ref $settings{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings{$_} and not ref $settings{$_} 90 0 90
Condition33%1/3
defined $settings{$_} and not ref $settings{$_}
!ll&&!rl&&r
0090
MC/DC0%0/2
defined $settings{$_} and not ref $settings{$_}
defined $settings{$_} not ref $settings{$_}
Truth table33%1/3
A and not(B)
A= defined $settings{$_}B= not ref $settings{$_}
ABresult
0X0
100
111
142 }
143  
144 45 for (qw/verbose debug login/) { # options
145 135 ▶ $settings{$_} = $$opts{$_} if defined $$opts{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$_} 0 135 135
146 }
147  
148 45 ▶if ($$opts{config}) { # pre-emptive #3
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"config"}) 0 45 45
149 0 %settings = (%Zoidberg::_settings, %settings);
150 0 for (sort keys %settings) {
151 0 ▶ next unless defined $settings{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $settings{$_} 0 0 0
152 0 my $val = $settings{$_};
153 0 ▶ if (ref($val) eq 'ARRAY') { $val = join ', ', @$val }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val eq 'HASH') { } 0 0 0
154 elsif (ref($val) eq 'HASH') {
155 0 $val = join ', ', map "$_ => $$val{$_}", sort keys %$val;
156 }
157 0 print "$_ = $val\n"
158 }
159 0 exit 0;
160 } # FIXME shouldn't this be a machine parsable format ? -- Yes it should !
161  
162 # ################## #
163 # prepare for launch #
164 # ################## #
165  
166 my $exec_string =
167 0 ▶ $$opts{exec} ? join(' ', @{$$opts{exec}}) :
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'command'} ? : 0 45 45
168 0 ▶ $$opts{command} ? join(' ', @{$$opts{command}}) : '' ;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'exec'} ? : 0 45 45
169  
170 # rest ARGV should be files
171 0 ▶for (@$args) { complain($_, 3) unless -f $_ }
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
172  
173 my $interact = $$opts{interactive} ||
174 45 ▶ (@$args || $exec_string || $$opts{stdin}) ? 0 : (-t STDIN and -t STDOUT) ;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'interactive'} || (@$args || $exec_string || $opts->{'stdin'}) ? : 0 45 45
Condition33%4/12
$opts->{"interactive"} or (@$args or $exec_string or $opts->{"stdin"})
l!l&&r!l&&!r
0045
@$args or $exec_string or $opts->{"stdin"}
l!l&&r!l&&!r
0045
@$args or $exec_string
l!l&&r!l&&!r
0045
-t STDIN and -t STDOUT
!ll&&!rl&&r
4500
MC/DC0%0/4
$opts->{"interactive"} or (@$args or $exec_string or $opts->{"stdin"})
$opts->{"interactive"} @$args $exec_string $opts->{"stdin"}
MC/DC0%0/2
-t STDIN and -t STDOUT
-t STDIN -t STDOUT
Truth table20%1/5
A or B or C or D
A= $opts->{"interactive"}B= @$argsC= $exec_stringD= $opts->{"stdin"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table33%1/3
A and B
A= -t STDINB= -t STDOUT
ABresult
0X0
100
111
175  
176 45 $settings{interactive} = $interact;
177  
178 # ############## #
179 # AND Lift-off ! #
180 # ############## #
181  
182 45 my $cube = Zoidberg->new( settings => \%settings );
183  
184 eval qq{
185 package # hide from pause indexer
186 Zoidberg::Eval;
187 $$opts{use}
188 45 ▶} if $$opts{use};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"use"} 0 45 45
189  
190 0 ▶if ($$opts{plug}) { $cube->plug($_) for @{$$opts{plug}} }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"plug"}) 0 45 45
191  
192 45 ▶if ($exec_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec_string) 0 45 45
193 # if ($args{command}) { $cube->{ipc}->do($exec_string) }
194 # else {
195 0 $cube->shell_string($exec_string)
196 # }
197 }
198  
199 45 $cube->source($_) for @$args;
200  
201 45 ▶if ( $$opts{stdin} || -p STDIN || (!$interact && !$exec_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"stdin"} or -p STDIN or not $interact || $exec_string) 45 0 45
Condition22%2/9
$opts->{"stdin"} or -p STDIN or not $interact || $exec_string
l!l&&r!l&&!r
4500
$opts->{"stdin"} or -p STDIN
l!l&&r!l&&!r
0450
$interact || $exec_string
l!l&&r!l&&!r
000
MC/DC0%0/4
$opts->{"stdin"} or -p STDIN or not $interact || $exec_string
$opts->{"stdin"} -p STDIN $interact $exec_string
Truth table20%1/5
A or B or not(C || D)
A= $opts->{"stdin"}B= -p STDINC= $interactD= $exec_string
ABCDresult
1XXX1
01XX1
00001
001X0
00010
202 45 ▶ while (<STDIN>) { $cube->shell_string($_) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STDIN)) 763 1 764
203 # FIXME do something like set nobuffer and let zoid read STDIN
204 # then it can also pull from it
205 }
206  
207 1 ▶$cube->main_loop if $interact;
Branch: 1 branch
BranchTrueFalseTotal
if $interact 0 1 1
208  
209 1 my $exit = 0;
210 $exit = ref($$cube{error}) ? ($$cube{error}{exit_status} || 1) : 1
211 1 ▶ unless $interact or ! $$cube{error};
Branch: 2 branches
BranchTrueFalseTotal
unless $interact or not $cube->{"error"} 0 1 1
ref $cube->{'error'} ? : 0 0 0
Condition20%1/5
$interact or not $cube->{"error"}
l!l&&r!l&&!r
010
$cube->{"error"}{"exit_status"} or 1
l!l
00
MC/DC0%0/2
$interact or not $cube->{"error"}
$interact not $cube->{"error"}
MC/DC0%0/1
$cube->{"error"}{"exit_status"} or 1
$cube->{"error"}{"exit_status"}
Truth table33%1/3
A or not(B)
A= $interactB= not $cube->{"error"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $cube->{"error"}{"exit_status"}
Aresult
11
00
212  
213 1 $cube->round_up;
214  
215 1 exit $exit;
216  
217 # ############ #
218 # sub routines #
219 # ############ #
220  
221 0 ▶sub complain {
Subroutine complain: not called CC=6 SCAR=37.4
222 0 my $opt = shift;
223 0 ▶ my $m = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
224
225 0 my $bn = $0;
226 0 $bn =~ s|^(.*/)*||;
227 0 ▶ if ($m == 1) { print STDERR "$bn: unrecognized option '$opt'"; }
Branch: 3 branches
BranchTrueFalseTotal
if ($m == 1) { } 0 0 0
elsif ($m == 2) { } 0 0 0
elsif ($m == 3) { } 0 0 0
228 0 elsif ($m == 2) { print STDERR "$bn: option '$opt' requires an argument"; }
229 0 elsif ($m == 3) { print STDERR "$bn: $opt: No such file or directory\n"; }
230
231 0 ▶ if ($m < 3) {print "\nTry '$bn --help' for more information.\n"}
Branch: 1 branch
BranchTrueFalseTotal
if ($m < 3) 0 0 0
232 0 exit $m;
233 }
234  
235 __END__
« blib/lib/Zoidberg/Utils/Error.pm ↑ Summary blib/lib/Zoidberg/PluginHash.pm »