blib/lib/Zoidberg.pm

filter: Statementstmt 68.6% 502 / 731 Branchbran 49.0% 213 / 434 Conditioncond 44.4% 100 / 225 MC/DCmcdc 20.4% 27 / 132 Subroutinesub 73.1% 49 / 67 Podpod 30.4% 7 / 23 totaltotal 55.7% 898 / 1612 CC 309 SCAR 47.5
CC
309
Cov
59%
parse_block
449.7
expand_param
103.3
parse_env
91.5
CRAP 116.0
« blib/lib/Zoidberg/Fish/Commands.pm ↑ Summary blib/lib/Zoidberg/Utils/Output.pm »
1 package Zoidberg;
2  
3 64 our $VERSION = '0.981';
4 64 our $LONG_VERSION = "Zoidberg $VERSION
5  
6 Copyright (c) 2011 Jaap G Karssenberg and Joel Berger. All rights reserved.
7 This program is free software; you can redistribute it and/or
8 modify it under the same terms as Perl.
9  
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  
14 http://github.com/jberger/Zoidberg";
15  
16 129 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 84 ▶use vars qw/$AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 #use warnings;
19 #no warnings 'uninitialized'; # yes, undefined == '' == 0
20 64 ▶no warnings; # I am leaving this, because I don't totally understand how warnings propagate through -- Joel
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 64 require Cwd;
23 64 require File::Glob;
24 64 ▶use File::ShareDir qw/dist_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 64 ▶use File::Copy qw/copy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 64 ▶use File::Spec::Functions qw/catfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 64 require Zoidberg::Contractor;
29 64 require Zoidberg::Shell;
30 64 require Zoidberg::PluginHash;
31 64 require Zoidberg::StringParser;
32  
33 64 ▶use Zoidberg::DispatchTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 use Zoidberg::Utils
35 64 ▶ qw/:error :output :fs read_data_file merge_hash regex_glob getopt/;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 64 our @ISA = qw/Zoidberg::Contractor Zoidberg::Shell/;
38  
39 =head1 NAME
40  
41 Zoidberg - A modular perl shell
42  
43 =head1 SYNOPSIS
44  
45 You should use the B<zoid> system command to start the Zoidberg shell.
46 To embed the Zoidberg shell in another perl program use the L<Zoidberg::Shell>
47 module.
48  
49 =head1 DESCRIPTION
50  
51 I<This page contains devel documentation, if you're looking for user documentation start with the zoid(1) and zoiduser(1) man pages.>
52  
53 This module contains the core dispatch and event logic of the Zoidberg shell.
54 Also it is used as a 'main object' so other objects can find each other here;
55 all other objects are nested below this object.
56 Also it contains some parser code.
57  
58 This object inherits from both L<Zoidberg::Contractor>
59 and L<Zoidberg::Shell>.
60  
61 =head1 METHODS
62  
63 =over 4
64  
65 =cut
66  
67 64 our %OBJECTS; # used to store refs to ALL Zoidberg objects in a process
68 64 our $CURRENT; # current Zoidberg object
69  
70 64 our $_base_dir; # relative path for some settings
71 64 our @_parser_settings = qw/
72 split_script split_words
73 parse_env parse_fd parse_aliases parse_def_contexts
74 expand_comm expand_param expand_path
75 /;
76  
77 our %_settings = ( # default settings
78 output => { error => 'red', debug => 'yellow' },
79 clothes => {
80 keys => [qw/settings commands aliases events error/],
81 subs => [qw/shell alias unalias setting set source mode plug unplug/],
82 },
83 perl => {
84 keywords => [qw/
85 if unless for foreach while until
86 print
87 push shift unshift pop splice
88 delete
89 do eval
90 tie untie
91 my our use no sub package
92 import bless
93 /],
94 namespace => 'Zoidberg::Eval',
95 opts => 'Z',
96 },
97 hide_private_method => 1,
98 hide_hidden_files => 1,
99 naked_zoid => 0,
100 576 ▶ ( map {($_ => 1)} @_parser_settings ),
Branch: 2 branches
BranchTrueFalseTotal
$ENV{'PAR_TEMP'} ? : 0 64 64
$ENV{'PAR_TEMP'} ? : 0 64 64
101 ##Insert defaults here##
102 rcfiles => [
103 ( $ENV{PAR_TEMP} ? "$ENV{PAR_TEMP}/inc/etc/zoidrc" : '/etc/zoidrc' ),
104 "$ENV{HOME}/.zoidrc",
105 "$ENV{HOME}/.zoid/zoidrc",
106 ],
107 data_dirs => [
108 "$ENV{HOME}/.zoid",
109 64 ( $ENV{PAR_TEMP} ? "$ENV{PAR_TEMP}/inc/share" : ( qw# /usr/local/share/zoid /usr/share/zoid # ) ),
110 dist_dir('Zoidberg'),
111 ],
112 );
113 64 our %_grammars = ( # default grammar
114 _base_gram => {
115 esc => '\\',
116 quotes => {
117 '"' => '"',
118 "'" => "'",
119 '`' => '`',
120 },
121 nests => {
122 '{' => '}',
123 '(' => ')',
124 },
125 },
126 script_gram => {
127 tokens => [
128 [ ';', 'EOS' ],
129 [ "\n", 'EOL' ],
130 [ '&&', 'AND' ],
131 [ '||', 'OR' ],
132 [ '|', '_CUT' ],
133 [ qr/(?<![<>])&/ , 'EOS_BG' ],
134 [ '==>', 'XFW' ],
135 [ '<==', 'XBW' ],
136 ],
137 },
138 word_gram => qr/\s/,
139 redirect_gram => {
140 s_esc => qr/[\\\-\=]/,
141 tokens => [
142 [ qr/<\S+>/, '_SELF' ],
143 [ '>&', 'DUP_OUT' ],
144 [ '>|', 'CLOB_OUT' ],
145 [ '>!', 'CLOB_OUT' ],
146 [ '>>', 'APP_OUT' ],
147 [ '<&', 'DUP_IN' ],
148 [ '<<', 'ERROR' ],
149 [ '<>', 'RW' ],
150 [ '>', 'OUT' ],
151 [ '<', 'IN' ],
152 ],
153 },
154 dezoid_gram => {
155 tokens => [
156 [ qr/->/, 'ARR' ], # ARRow
157 [ qr/[\$\@][A-Za-z_][\w\-]*(?<!\-)/, '_SELF' ], # env var
158 ],
159 quotes => { "'" => "'" }, # interpolate also between '"'
160 nests => {},
161 },
162 expand_comm_gram => {
163 tokens => {
164 '$(' => {
165 token => 'COMM',
166 tokens => {')' => '_CUT'},
167 },
168 '`' => {
169 token => 'COMM',
170 tokens => {'`' => '_CUT'},
171 }
172 },
173 },
174 # expand_braces_gram => {
175 # tokens => {
176 # '{' => {
177 # token => 'BRACE',
178 # tokens => { '}' => '_CUT' },
179 # },
180 # },
181 # },
182 );
183  
184 =item C<new(\%attr)>
185  
186 Initialize secondary objects and sets config.
187 C<%attr> contains non-default attributes and is used to set runtime settings.
188  
189 You probably don't want to use this to construct a new Zoidberg shell object,
190 better use L<Zoidberg::Shell>.
191  
192 =cut
193  
194 61 ▶sub new { # FIXME maybe rename this to init ?
Subroutine new: called CC=9 SCAR=27.4
Pod documented
195 61 my $class = shift;
196 61 ▶ my $self = @_ ? { @_ } : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 61 0 61
197 61 ▶ $$self{$_} ||= {} for qw/settings commands aliases events objects/;
Condition100%2/2
$self->{$_} ||= {}
l!l
61244
MC/DC100%1/1
$self->{$_} ||= {}
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
198 61 ▶ $$self{no_words} ||= [];
Condition50%1/2
$self->{"no_words"} ||= []
l!l
061
MC/DC0%0/1
$self->{"no_words"} ||= []
$self->{"no_words"}
Truth table50%1/2
A
A= $self->{"no_words"}
Aresult
11
00
199 61 push @{$$self{no_words}}, qw/PERL SUBZ/; # parser stuff
200 61 $$self{round_up}++;
201 61 ▶ $$self{topic} ||= '';
Condition50%1/2
$self->{"topic"} ||= ""
l!l
061
MC/DC0%0/1
$self->{"topic"} ||= ""
$self->{"topic"}
Truth table50%1/2
A
A= $self->{"topic"}
Aresult
11
00
202  
203 61 bless($self, $class);
204  
205 61 $OBJECTS{"$self"} = $self;
206 61 ▶ $CURRENT = $self unless ref( $CURRENT ) eq $class; # could be autovivicated
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Zoidberg::CURRENT eq $class 61 0 61
207 61 $self->{shell} = $self; # for Contractor
208  
209 ## settings
210 61 $$self{_settings} = merge_hash(\%_settings, $$self{settings});
211 $$self{_settings}{data_dirs}
212 61 ▶ || error 'You should at least set a config value for \'data_dirs\'';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_settings"}{"data_dirs"} or error("You should at least set a config value for 'data_dirs'") 61 0 61
213  
214 61 my %set;
215 61 tie %set, 'Zoidberg::SettingsHash', $$self{_settings}, $self;
216 61 $$self{settings} = \%set;
217  
218 ## commands
219 $$self{commands} = Zoidberg::DispatchTable->new(
220 $self, {
221 exit => '->exit',
222 plug => '->plug',
223 unplug => '->unplug',
224 mode => '->mode',
225 readline => "->stdin('zoid-$VERSION\$ ')",
226 readmore => "->stdin('> ')",
227 builtin => '->builtin',
228 command => '->command',
229 61 ( %{$$self{commands}} )
230 }
231 );
232  
233 ## events
234 61 $$self{events} = Zoidberg::DispatchTable->new($self, $$self{events});
235 2169 ▶ $$self{events}{envupdate} = sub {
Subroutine __ANON__: called CC=3 SCAR=17.3
236 2169 my $pwd = Cwd::cwd();
237 2169 ▶ return if $pwd eq $ENV{PWD};
Branch: 1 branch
BranchTrueFalseTotal
if $pwd eq $ENV{"PWD"} 2169 0 2169
238 0 @ENV{qw/OLDPWD PWD/} = ($ENV{PWD}, $pwd);
239 0 $self->broadcast('newpwd');
240 0 ▶ $self->builtin('log', $pwd, 'pwd') if $$self{settings}{interactive};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"settings"}{"interactive"} 0 0 0
241 61 };
242  
243 ## parser
244 61 $$self{parser} = Zoidberg::DispatchTable->new($self, $$self{parser});
245  
246 ## stringparser
247 61 ▶ $$self{grammars} ||= \%_grammars;
Condition50%1/2
$self->{"grammars"} ||= \%Zoidberg::_grammars
l!l
061
MC/DC0%0/1
$self->{"grammars"} ||= \%Zoidberg::_grammars
$self->{"grammars"}
Truth table50%1/2
A
A= $self->{"grammars"}
Aresult
11
00
248 $$self{stringparser} = Zoidberg::StringParser->new(
249 $$self{grammars}{_base_gram}, $$self{grammars},
250 61 {allow_broken => 1, no_esc_rm => 1} );
251  
252 ## initialize contractor
253 61 $self->shell_init;
254  
255 ## plugins
256 61 my %objects;
257 61 tie %objects, 'Zoidberg::PluginHash', $self;
258 61 $self->{objects} = \%objects;
259  
260 # autoloading of contexts after plugin loading
261 # because of bootstrapping issues
262 128 ▶ $$self{parser}{_AUTOLOAD} = sub {
Subroutine __ANON__: called CC=4 SCAR=21.1
263 128 my $c = shift;
264 128 debug "trying to autoload $c";
265 128 ▶ if ($c =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /::/) { } 0 128 128
266 0 $c =~ m#(.*?)(::|->)$#;
267 0 my ($class, $type) = ($1, $2);
268 0 debug "loading class $class";
269 0 $$self{parser}{$c} = {};
270 0 ▶ $$self{parser}{$c}{handler} = sub {
Subroutine __ANON__: not called CC=2 SCAR=12.2
271 0 my (undef, $sub, @args) = @{ shift() };
272 0 ▶ unshift @args, $class if $type eq '->';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "->" 0 0 0
273 64 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
274 0 $sub = $class.'::'.$sub;
275 0 $sub->(@args);
276 0 };
277 0 ▶ $$self{parser}{$c}{intel} = sub {
Subroutine __ANON__: not called CC=3 SCAR=20.6
278 0 my $block = shift;
279 0 ▶ return undef if @$block > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$block > 2 0 0 0
280 64 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
281 my @p = grep m/^$$block[1]/,
282 0 grep defined *{$class.'::'.$_}{CODE}, keys %{$class.'::'};
283 0 push @p, grep m/^$$block[1]/, keys %{$$self{aliases}{'mode_'.$c}}
284 0 ▶ if exists $$self{aliases}{'mode_'.$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"aliases"}{'mode_' . $c} 0 0 0
285 0 $$block[0]{poss} = \@p;
286 0 return $block;
287 0 };
288 }
289 128 else { eval { $self->plug($c) } }
290 128 ▶ debug 'did you know 5.6.2 sucks ?' if $] < 5.008; # don't ask ... i suspect another vivication bug
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.008" 0 128 128
291 128 ▶ return exists($$self{parser}{$c}) ? $$self{parser}{$c} : undef ;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'parser'}{$c} ? : 0 128 128
292 61 };
293  
294 ## let's load the rcfiles
295 61 ▶ $$self{events}{loadrc} = sub {
Subroutine __ANON__: called CC=3 SCAR=16.0
296 #check for existant rcfiles in the known locations
297 61 my @rcfiles = grep {-f $_} @{$$self{_settings}{rcfiles}};
298 #if no zoidrc file is found, create one from the template in the dist_dir
299 61 ▶ unless (@rcfiles) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rcfiles) 0 61 61
300 0 my $rc_template = catfile(dist_dir('Zoidberg'), "zoidrc.example");
301 0 my $new_rc = catfile($ENV{HOME}, ".zoidrc");
302 0 warn "### No zoidrc file was found. A new zoidrc file will be created for you at $new_rc. If you really intend to use without a zoidrc file, simply create an empty zoidrc file in that location or at /etc/zoidrc\n\n";
303 0 ▶ if( copy( $rc_template, $new_rc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (copy($rc_template, $new_rc)) { } 0 0 0
304 0 push @rcfiles, $new_rc;
305 } else {
306 0 warn "### Could not copy $rc_template to $new_rc\n\n";
307 }
308
309 }
310 61 $self->source(@rcfiles);
311 61 };
312 61 $self->broadcast('loadrc');
313  
314 61 $self->broadcast('envupdate'); # set/log pwd and maybe init other env stuff
315  
316 61 return $self;
317 }
318  
319 19 ▶sub import { bug "You should use Zoidberg::Shell to import from" if @_ > 1 }
Subroutine import: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 19 19
320  
321 # hooks overloading Contracter # FIXME these are not used !?
322 64 *pre_job = \&parse_block;
323 64 *post_job = \&broadcast;
324  
325 # ############ #
326 # Main routine #
327 # ############ #
328  
329 =item C<main_loop()>
330  
331 Spans interactive shell reading from a secondary ReadLine object or from STDIN.
332  
333 To quit this loop the routine C<exit()> of this package should be called.
334 Most common way to do this is pressing ^D.
335  
336 =cut
337  
338 0 ▶sub main_loop {
Subroutine main_loop: not called CC=7 SCAR=40.3
Pod documented
339 0 my $self = shift;
340  
341 0 $$self{_continue} = 1;
342 0 ▶ while ($$self{_continue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_continue"}) 0 0 0
343 0 $self->reap_jobs();
344 0 $self->broadcast('prompt');
345 0 my ($cmd) = $self->builtin('readline');
346 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
347 0 complain "\nInput routine died. (You can interrupt zoid NOW)";
348 0 local $SIG{INT} = 'DEFAULT';
349 0 sleep 1; # infinite loop protection
350 }
351 else {
352 0 $self->reap_jobs();
353  
354 0 ▶ unless (defined $cmd || $$self{_settings}{ignoreeof}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cmd or $self->{'_settings'}{'ignoreeof'}) { } 0 0 0
Condition0%0/3
defined $cmd or $self->{"_settings"}{"ignoreeof"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $cmd or $self->{"_settings"}{"ignoreeof"}
defined $cmd $self->{"_settings"}{"ignoreeof"}
Truth table0%0/3
A or B
A= defined $cmdB= $self->{"_settings"}{"ignoreeof"}
ABresult
1X1
011
000
355 0 debug 'readline returned undef .. exiting';
356 0 $self->exit();
357 }
358 0 else { $$self{_warned_bout_jobs} = 0 }
359  
360 0 ▶ last unless $$self{_continue};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_continue"} 0 0 0
361  
362 0 ▶ $self->shell_string({interactive => 1}, $cmd) if length $cmd;
Branch: 1 branch
BranchTrueFalseTotal
if length $cmd 0 0 0
363 }
364 }
365 }
366  
367 # ############ #
368 # Parser stuff #
369 # ############ #
370  
371 918 ▶sub shell_string {
Subroutine shell_string: called CC=15 SCAR=39.1
Pod undocumented
372 918 my ($self, $meta, $string) = @_;
373 918 ▶ ($meta, $string) = ({}, $meta) unless ref($meta) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $meta eq "HASH" 763 155 918
374 918 local $CURRENT = $self;
375  
376 PARSE_STRING:
377 my @list = $$self{_settings}{split_script}
378 918 ▶ ? ($$self{stringparser}->split('script_gram', $string)) : ($string) ;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_settings'}{'split_script'} ? : 918 0 918
379 918 ▶ my $b = $$self{stringparser}{broken} ? 1
Branch: 2 branches
BranchTrueFalseTotal
$self->{'stringparser'}{'broken'} ? : 0 918 918
@list && !ref($list[-1]) && !($list[-1] =~ /^EO/) ? : 0 918 918
Condition66%4/6
@list and not ref $list[-1] and not $list[-1] =~ /^EO/
!ll&&!rl&&r
1557630
@list and not ref $list[-1]
!ll&&!rl&&r
0155763
MC/DC0%0/3
@list and not ref $list[-1] and not $list[-1] =~ /^EO/
@list not ref $list[-1] not $list[-1] =~ /^EO/
Truth table50%2/4
A and not(B) and not(C)
A= @listB= not ref $list[-1]C= not $list[-1] =~ /^EO/
ABCresult
0XX0
10X0
1100
1111
380 : (@list and ! ref $list[-1] and $list[-1] !~ /^EO/) ? 2 : 0 ;
381 918 ▶ if ($b and ! $$self{_settings}{interactive}) { # FIXME should be STDIN on non interactive
Branch: 2 branches
BranchTrueFalseTotal
if ($b and not $self->{'_settings'}{'interactive'}) { } 0 918 918
elsif ($b) { } 0 918 918
Condition33%1/3
$b and not $self->{"_settings"}{"interactive"}
!ll&&!rl&&r
91800
MC/DC0%0/2
$b and not $self->{"_settings"}{"interactive"}
$b not $self->{"_settings"}{"interactive"}
Truth table33%1/3
A and not(B)
A= $bB= not $self->{"_settings"}{"interactive"}
ABresult
0X0
100
111
382 0 ▶ error qq#Operator at end of input# if $b == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $b == 2 0 0 0
383 0 my $gram = $$self{stringparser}{broken}[1];
384 0 error qq#Unmatched $$gram{_open}[1] at end of input: $$gram{_open}[0]#;
385 }
386 elsif ($b) {
387 0 ($string) = $self->builtin('readmore');
388 0 debug "\n\ngot $string\n\n\n";
389 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
390 0 complain "\nInput routine died.\n$@";
391 0 return;
392 }
393 0 goto PARSE_STRING;
394 }
395  
396 918 ▶ if ($$meta{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"interactive"}) 0 918 918
397 0 $self->broadcast('cmd', $string);
398 0 $$self{previous_cmd} = $string;
399 0 ▶ print STDERR $string if $$self{_settings}{verbose}; # posix spec
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_settings"}{"verbose"} 0 0 0
400 }
401  
402 918 debug 'block list: ', \@list;
403 918 ▶ $$self{fg_job} ||= $self;
Condition66%2/3
$self->{"fg_job"} ||= $self
l!l&&r!l&&!r
873450
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
404 918 $$self{fg_job}->shell_list($meta, @list); # calling a contractor
405 }
406  
407 1721 ▶sub prepare_block {
Subroutine prepare_block: called CC=6 SCAR=18.8
Pod undocumented
408 1721 my ($self, $block) = @_;
409 1721 my $t = ref $block;
410 1721 ▶ if ($t eq 'SCALAR') { $block = [{env => {pwd => $ENV{PWD}}}, $$block] }
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'SCALAR') { } 1344 377 1721
elsif ($t eq 'ARRAY') { } 377 0 377
411 elsif ($t eq 'ARRAY') {
412 377 ▶ if (ref($$block[0]) eq 'HASH') { $$block[0]{env}{pwd} ||= $ENV{PWD} }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $block->[0] eq 'HASH') { } 319 58 377
Condition66%2/3
$block->[0]{"env"}{"pwd"} ||= $ENV{"PWD"}
l!l&&r!l&&!r
31180
MC/DC0%0/2
$block->[0]{"env"}{"pwd"} ||= $ENV{"PWD"}
$block->[0]{"env"}{"pwd"} $ENV{"PWD"}
Truth table66%2/3
A ||= B
A= $block->[0]{"env"}{"pwd"}B= $ENV{"PWD"}
ABresult
1X1
011
000
413 58 else { unshift @$block, {env => {pwd => $ENV{PWD}}} }
414 }
415 else {
416 0 ▶ bug $t ? "prepare_block can't handle type $t"
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 0 0 0
417 : "block ain't a ref !??" ;
418 }
419 1721 return $block;
420 }
421  
422 1309 ▶sub parse_block { # call as late as possible before execution
Subroutine parse_block: called CC=72 SCAR=61.1
Pod undocumented
423 # FIXME can this be more optimised for builtin() call ?
424 1309 my $self = shift;
425 1309 ▶ my $meta = (ref($_[0]) eq 'HASH') ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 1309 1309
426 1309 my $block = shift;
427  
428 # check settings
429 1309 $$meta{$_} = $$self{_settings}{$_} for grep {! defined $$meta{$_}} @_parser_settings;
430 # FIXME mode settings, uc || lc ?
431
432 # decipher block
433 2552 PARSE_BLOCK:
434 my @words;
435 2552 my $t = ref $block;
436 2552 ▶ if (!$t or $t eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (not $t or $t eq 'SCALAR') { } 1243 1309 2552
elsif ($t eq 'ARRAY') { } 1309 0 1309
elsif ($t eq 'CODE') { } 0 0 0
Condition66%2/3
not $t or $t eq "SCALAR"
l!l&&r!l&&!r
124301309
MC/DC50%1/2
not $t or $t eq "SCALAR"
not $t $t eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not $tB= $t eq "SCALAR"
ABresult
1X1
011
000
437 1243 ▶ ($meta, @words) = @{ $self->parse_env([$meta, $t ? $$block : $block]) };
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 0 1243 1243
438 1243 ▶ ++$$meta{no_mode} and (length $words[0] or shift @words) if @words && $words[0] =~ s/^\!\s*//;
Branch: 1 branch
BranchTrueFalseTotal
if @words and $words[0] =~ s/^\!\s*// 0 1243 1243
Condition25%3/12
@words && $words[0] =~ s/^\!\s*// && (++$meta->{'no_mode'} && (length $words[0] || shift @words))
!ll&&!rl&&r
124300
@words and $words[0] =~ s/^\!\s*//
!ll&&!rl&&r
3612070
++$meta->{"no_mode"} and length $words[0] || shift @words
!ll&&!rl&&r
000
length $words[0] or shift @words
l!l&&r!l&&!r
000
MC/DC0%0/5
@words && $words[0] =~ s/^\!\s*// && (++$meta->{'no_mode'} && (length $words[0] || shift @words))
@words $words[0] =~ s/^\!\s*// ++$meta->{"no_mode"} length $words[0] shift @words
Truth table33%2/6
A and B && C and D or E
A= @wordsB= $words[0] =~ s/^\!\s*//C= ++$meta->{"no_mode"}D= length $words[0]E= shift @words
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
439 }
440 elsif ($t eq 'ARRAY') {
441 1309 ▶ $meta = { %$meta, %{shift @$block} } if ref($$block[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $block->[0] eq "HASH" 1309 0 1309
442 1309 ▶ unless (@$block > 1 or $$meta{plain_words}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$block > 1 or $meta->{"plain_words"}) 1243 66 1309
Condition66%2/3
@$block > 1 or $meta->{"plain_words"}
l!l&&r!l&&!r
6601243
MC/DC50%1/2
@$block > 1 or $meta->{"plain_words"}
@$block > 1 $meta->{"plain_words"}
Truth table66%2/3
A or B
A= @$block > 1B= $meta->{"plain_words"}
ABresult
1X1
011
000
443 1243 debug "block aint a word block";
444 1243 $block = shift @$block;
445 1243 goto PARSE_BLOCK;
446 }
447 66 @words = @$block;
448 66 ▶ ++$$meta{no_mode} and shift @words if @words && $words[0] eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if @words and $words[0] eq "!" 0 66 66
Condition22%2/9
@words && $words[0] eq '!' && (++$meta->{'no_mode'} && shift @words)
!ll&&!rl&&r
6600
@words and $words[0] eq "!"
!ll&&!rl&&r
0660
++$meta->{"no_mode"} and shift @words
!ll&&!rl&&r
000
MC/DC0%0/4
@words && $words[0] eq '!' && (++$meta->{'no_mode'} && shift @words)
@words $words[0] eq "!" ++$meta->{"no_mode"} shift @words
Truth table20%1/5
A and B && C and D
A= @wordsB= $words[0] eq "!"C= ++$meta->{"no_mode"}D= shift @words
ABCDresult
0XXX0
10XX0
110X0
11100
11111
449 }
450 0 elsif ($t eq 'CODE') { return [{context => 'PERL', %$meta}, $block] }
451 0 else { bug "parse tree contains $t reference" }
452  
453 # do aliases
454 1309 debug 'meta: ', $meta; # , 'words: ', [[@words]];
455 1309 ▶ if (@words and ! $$meta{pretend} and $$meta{parse_aliases}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words and not $meta->{"pretend"} and $meta->{"parse_aliases"}) 1273 36 1309
Condition66%4/6
@words and not $meta->{"pretend"} and $meta->{"parse_aliases"}
!ll&&!rl&&r
3601273
@words and not $meta->{"pretend"}
!ll&&!rl&&r
3601273
MC/DC33%1/3
@words and not $meta->{"pretend"} and $meta->{"parse_aliases"}
@words not $meta->{"pretend"} $meta->{"parse_aliases"}
Truth table50%2/4
A and not(B) and C
A= @wordsB= not $meta->{"pretend"}C= $meta->{"parse_aliases"}
ABCresult
0XX0
10X0
1100
1111
456 1273 my @blocks = $self->parse_aliases($meta, @words);
457 0 ▶ if (@blocks > 1) { return @blocks } # probably an alias contained pipe or logic operator
Branch: 2 branches
BranchTrueFalseTotal
if (@blocks > 1) { } 0 1273 1273
elsif (not @blocks) { } 0 1273 1273
458 0 elsif (! @blocks) { return undef }
459 else {
460 1273 ($meta, @words) = @{ shift(@blocks) };
461 }
462 }
463 # post alias stuff
464 1309 $$meta{zoidcmd} = join ' ', @words; # unix haters guide pdf page 60
465 #FIXME how does this hadle escaped whitespacec ?
466 1309 ▶ $$meta{no_mode}++ if $words[0] eq 'mode'; # explicitly after alias expansion .. ! is before alias expansion
Branch: 1 branch
BranchTrueFalseTotal
if $words[0] eq "mode" 0 1309 1309
467  
468 # check custom filters
469 1309 for my $sub ($$self{parser}->stack('filter')) {
470 0 my $r = $sub->([$meta, @words]);
471 0 ▶ ($meta, @words) = @$r if $r; # skip on undef
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
472 }
473 1309 ▶ return undef unless $$meta{context} or @words;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"context"} or @words 36 1273 1309
Condition66%2/3
$meta->{"context"} or @words
l!l&&r!l&&!r
0127336
MC/DC50%1/2
$meta->{"context"} or @words
$meta->{"context"} @words
Truth table66%2/3
A or B
A= $meta->{"context"}B= @words
ABresult
1X1
011
000
474  
475 1273 ▶ $$meta{context} = 'SUBZ' if $$meta{zoidcmd} =~ /^\s*\(.*\)\s*$/s; # check for subshell
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"zoidcmd"} =~ /^\s*\(.*\)\s*$/s 97 1176 1273
476  
477 # check builtin contexts/filters
478 1273 ▶ unless ($$meta{context} or ! $$meta{parse_def_contexts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"context"} or not $meta->{"parse_def_contexts"}) 1176 97 1273
Condition66%2/3
$meta->{"context"} or not $meta->{"parse_def_contexts"}
l!l&&r!l&&!r
9701176
MC/DC50%1/2
$meta->{"context"} or not $meta->{"parse_def_contexts"}
$meta->{"context"} not $meta->{"parse_def_contexts"}
Truth table66%2/3
A or not(B)
A= $meta->{"context"}B= not $meta->{"parse_def_contexts"}
ABresult
1X1
011
000
479 1176 debug 'trying builtin contexts';
480 1176 my $perl_regexp = join '|', @{$self->{_settings}{perl}{keywords}};
481 1176 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if ($meta->{'zoidcmd'} =~ s/^\s*(\w*){(.*)}(\w*)\s*$/$2/s or $meta->{'pretend'} and $meta->{'zoidcmd'} =~ s/^\s*(\w*){(.*)$/$2/s) { } 174 1002 1176
elsif ($meta->{'zoidcmd'} =~ s/^\s*(\w+):\s+//) { } 0 1002 1002
elsif (@words == 1 and $words[0] =~ /^%/) { } 0 1002 1002
elsif ($words[0] =~ /^\s*(->|[\$\@\%\&\*\xA3]\S|\w+::|\w+[\(\{]|($perl_regexp)\b)/s) { } 42 960 1002
Condition50%3/6
$meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)}(\w*)\s*$/$2/s or $meta->{"pretend"} and $meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)$/$2/s
l!l&&r!l&&!r
17401002
$meta->{"pretend"} and $meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)$/$2/s
!ll&&!rl&&r
100200
MC/DC33%1/3
$meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)}(\w*)\s*$/$2/s or $meta->{"pretend"} and $meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)$/$2/s
$meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)}(\w*)\s*$/$2/s $meta->{"pretend"} $meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)$/$2/s
Truth table50%2/4
A or B and C
A= $meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)}(\w*)\s*$/$2/sB= $meta->{"pretend"}C= $meta->{"zoidcmd"} =~ s/^\s*(\w*){(.*)$/$2/s
ABCresult
1XX1
0111
00X0
0100
482 $$meta{zoidcmd} =~ s/^\s*(\w*){(.*)}(\w*)\s*$/$2/s or $$meta{pretend} and
483 $$meta{zoidcmd} =~ s/^\s*(\w*){(.*)$/$2/s
484 ) { # all kinds of blocks with { ... }
485 174 ▶ unless (length $1) { @$meta{qw/context opts/} = ('PERL', $3 || '') }
Branch: 2 branches
BranchTrueFalseTotal
if (length $1) { } 0 174 174
if (do { grep {$_ eq $1;} 's', 'm', 'tr', 'y' }) { } 0 0 0
Condition100%2/2
$3 || ''
l!l
8166
MC/DC100%1/1
$3 || ''
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
486 0 elsif (grep {$_ eq $1} qw/s m tr y/) {
487 0 $$meta{zoidcmd} = $1.'{'.$$meta{zoidcmd}.'}'.$3; # always the exceptions
488 0 ▶ @$meta{qw/context opts/} = ('PERL', ($1 eq 'm') ? 'g' : 'p')
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'm' ? : 0 0 0
489 }
490 else {
491 0 ▶ @$meta{qw/context opts/} = (uc($1), $3 || '');
Condition0%0/2
$3 || ''
l!l
00
MC/DC0%0/1
$3 || ''
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
492 0 @words = $$self{stringparser}->split('word_gram', $$meta{zoidcmd});
493 }
494 }
495 elsif ($$meta{zoidcmd} =~ s/^\s*(\w+):\s+//) { # little bit o psh2 compat
496 0 $$meta{context} = uc $1;
497 0 shift @words;
498 }
499 0 ▶ elsif (@words == 1 and $words[0] =~ /^%/) { unshift @words, 'fg' } # and another exception
Condition66%2/3
@words == 1 and $words[0] =~ /^%/
!ll&&!rl&&r
8761260
MC/DC0%0/2
@words == 1 and $words[0] =~ /^%/
@words == 1 $words[0] =~ /^%/
Truth table66%2/3
A and B
A= @words == 1B= $words[0] =~ /^%/
ABresult
0X0
100
111
500 elsif ($words[0] =~ /^\s*(->|[\$\@\%\&\*\xA3]\S|\w+::|\w+[\(\{]|($perl_regexp)\b)/s) {
501 42 $$meta{context} = 'PERL';
502 }
503 }
504  
505 1273 $$meta{env}{ZOIDCMD} = $$meta{zoidcmd}; # unix haters guide, pdf page 60
506 1273 ▶ if ($$self{_settings}{mode} and ! $$meta{no_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_settings"}{"mode"} and not $meta->{"no_mode"}) 0 1273 1273
Condition33%1/3
$self->{"_settings"}{"mode"} and not $meta->{"no_mode"}
!ll&&!rl&&r
127300
MC/DC0%0/2
$self->{"_settings"}{"mode"} and not $meta->{"no_mode"}
$self->{"_settings"}{"mode"} not $meta->{"no_mode"}
Truth table33%1/3
A and not(B)
A= $self->{"_settings"}{"mode"}B= not $meta->{"no_mode"}
ABresult
0X0
100
111
507 0 my $m = $$self{_settings}{mode};
508 0 ▶ $$meta{context} ||= ($m =~ /::/) ? $m : uc($m);
Branch: 1 branch
BranchTrueFalseTotal
$m =~ /::/ ? : 0 0 0
Condition0%0/3
$meta->{"context"} ||= $m =~ /::/ ? $m : uc $m
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta->{"context"} ||= $m =~ /::/ ? $m : uc $m
$meta->{"context"} $m =~ /::/ ? $m : uc $m
Truth table0%0/3
A ||= B
A= $meta->{"context"}B= $m =~ /::/ ? $m : uc $m
ABresult
1X1
011
000
509 }
510  
511 1273 ▶ return [$meta, @words] if $$meta{pretend} and @words == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"pretend"} and @words == 1 0 1273 1273
Condition33%1/3
$meta->{"pretend"} and @words == 1
!ll&&!rl&&r
127300
MC/DC0%0/2
$meta->{"pretend"} and @words == 1
$meta->{"pretend"} @words == 1
Truth table33%1/3
A and B
A= $meta->{"pretend"}B= @words == 1
ABresult
0X0
100
111
512  
513 # check custom contexts
514 1273 ▶ unless ($$meta{context}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"context"}) 960 313 1273
515 960 debug 'trying custom contexts';
516 960 for my $pair ($$self{parser}->stack('word_list', 'TAGS')) {
517 0 my $r = $$pair[0]->([$meta, @words]);
518 0 ▶ unless ($r) { next }
Branch: 2 branches
BranchTrueFalseTotal
if ($r) { } 0 0 0
if (do { ref $r }) { } 0 0 0
519 0 elsif (ref $r) { ($meta , @words) = @$r }
520 0 ▶ else { $$meta{context} = length($r) > 1 ? $r : $$pair[1] }
Branch: 1 branch
BranchTrueFalseTotal
length $r > 1 ? : 0 0 0
521 0 ▶ last if $$meta{context};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"context"} 0 0 0
522 }
523 }
524  
525 # use default builtin context
526 1273 ▶ unless ($$meta{context} or ! $$meta{parse_def_contexts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"context"} or not $meta->{"parse_def_contexts"}) 960 313 1273
Condition66%2/3
$meta->{"context"} or not $meta->{"parse_def_contexts"}
l!l&&r!l&&!r
3130960
MC/DC50%1/2
$meta->{"context"} or not $meta->{"parse_def_contexts"}
$meta->{"context"} not $meta->{"parse_def_contexts"}
Truth table66%2/3
A or not(B)
A= $meta->{"context"}B= not $meta->{"parse_def_contexts"}
ABresult
1X1
011
000
527 960 debug 'using default context';
528 960 $$meta{context} = 'CMD';
529 }
530  
531 1273 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'parser'}{$meta->{'context'}} and exists $self->{'parser'}{$meta->{'context'}}{'parser'}) { } 0 1273 1273
elsif (grep {$meta->{'context'} eq $_;} @{$$self{'no_words'};}) { } 313 960 1273
elsif (@words and not $meta->{'pretend'}) { } 960 0 960
Condition66%2/3
exists $self->{"parser"}{$meta->{"context"}} and exists $self->{"parser"}{$meta->{"context"}}{"parser"}
!ll&&!rl&&r
10592140
MC/DC0%0/2
exists $self->{"parser"}{$meta->{"context"}} and exists $self->{"parser"}{$meta->{"context"}}{"parser"}
exists $self->{"parser"}{$meta->{"context"}} exists $self->{"parser"}{$meta->{"context"}}{"parser"}
Truth table66%2/3
A and B
A= exists $self->{"parser"}{$meta->{"context"}}B= exists $self->{"parser"}{$meta->{"context"}}{"parser"}
ABresult
0X0
100
111
532 exists $$self{parser}{$$meta{context}} and
533 exists $$self{parser}{$$meta{context}}{parser}
534 ) { # custom parser
535 0 ($meta, @words) = @{ $$self{parser}{$$meta{context}}{parser}->([$meta, @words]) };
536 }
537 2546 elsif (grep {$$meta{context} eq $_} @{$$self{no_words}}) { # no words
538 @words = $$meta{pretend}
539 ? $$self{stringparser}->split('word_gram', $$meta{zoidcmd})
540 313 ▶ : ( $$meta{zoidcmd} ) ;
Branch: 1 branch
BranchTrueFalseTotal
$meta->{'pretend'} ? : 0 313 313
541 313 ▶ $$meta{fork_job} = 1 if $$meta{context} eq 'SUBZ';
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"context"} eq "SUBZ" 97 216 313
542 216 ($meta, @words) = @{ $self->parse_perl([$meta, @words]) }
543 313 ▶ if ! $$meta{pretend} and $$meta{context} eq 'PERL';
Branch: 1 branch
BranchTrueFalseTotal
if not $meta->{"pretend"} and $meta->{"context"} eq "PERL" 216 97 313
Condition66%2/3
not $meta->{"pretend"} and $meta->{"context"} eq "PERL"
!ll&&!rl&&r
097216
MC/DC50%1/2
not $meta->{"pretend"} and $meta->{"context"} eq "PERL"
not $meta->{"pretend"} $meta->{"context"} eq "PERL"
Truth table66%2/3
not(A) and B
A= not $meta->{"pretend"}B= $meta->{"context"} eq "PERL"
ABresult
0X0
100
111
544 }
545 ▶ elsif (@words and ! $$meta{pretend}) { # expand and set topic
Condition33%1/3
@words and not $meta->{"pretend"}
!ll&&!rl&&r
00960
MC/DC0%0/2
@words and not $meta->{"pretend"}
@words not $meta->{"pretend"}
Truth table33%1/3
A and not(B)
A= @wordsB= not $meta->{"pretend"}
ABresult
0X0
100
111
546 960 ▶ ($meta, @words) = @{ $self->parse_words([$meta, @words]) } unless $$meta{plain_words};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"plain_words"} 960 0 960
547 $$self{topic} =
548 # FIXME exists($$meta{fd}{0}) ? $$meta{fd}{0}[0] :
549 958 ▶ (@words > 1 and $words[-1] !~ /^-/) ? $words[-1] : $$self{topic};
Branch: 1 branch
BranchTrueFalseTotal
@words > 1 && !($words[-1] =~ /^-/) ? : 832 126 958
Condition66%2/3
@words > 1 and not $words[-1] =~ /^-/
!ll&&!rl&&r
1260832
MC/DC50%1/2
@words > 1 and not $words[-1] =~ /^-/
@words > 1 not $words[-1] =~ /^-/
Truth table66%2/3
A and not(B)
A= @words > 1B= not $words[-1] =~ /^-/
ABresult
0X0
100
111
550 $$meta{fork_job} = 1 if $$meta{context} eq 'CMD' and
551 958 ▶ $$meta{cmdtype} ne 'builtin' and ! exists $$self{commands}{$words[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"context"} eq "CMD" and $meta->{"cmdtype"} ne "builtin" and not exists $self->{"commands"}{$words[0]} 916 42 958
Condition50%3/6
$meta->{"context"} eq "CMD" and $meta->{"cmdtype"} ne "builtin" and not exists $self->{"commands"}{$words[0]}
!ll&&!rl&&r
042916
$meta->{"context"} eq "CMD" and $meta->{"cmdtype"} ne "builtin"
!ll&&!rl&&r
00958
MC/DC33%1/3
$meta->{"context"} eq "CMD" and $meta->{"cmdtype"} ne "builtin" and not exists $self->{"commands"}{$words[0]}
$meta->{"context"} eq "CMD" $meta->{"cmdtype"} ne "builtin" not exists $self->{"commands"}{$words[0]}
Truth table50%2/4
A and B and not(C)
A= $meta->{"context"} eq "CMD"B= $meta->{"cmdtype"} ne "builtin"C= not exists $self->{"commands"}{$words[0]}
ABCresult
0XX0
10X0
1100
1111
552 }
553 1271 return [$meta, @words];
554 }
555  
556 64 our %_redir_ops = (
557 IN => '<', OUT => '>',
558 CLOB_OUT => '>!', APP_OUT => '>>',
559 RW => '+<', DUP_OUT => '>&', DUP_IN => '<&'
560 );
561  
562 1243 ▶sub parse_env {
Subroutine parse_env: called CC=40 SCAR=45.2
Pod undocumented
563 1243 my ($self, $block) = @_;
564 1243 my ($meta, @words) = @$block;
565  
566 1243 ▶ if (@words > 1 or ! $$meta{split_words}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words > 1 or not $meta->{'split_words'}) { } 0 1243 1243
Condition33%1/3
@words > 1 or not $meta->{"split_words"}
l!l&&r!l&&!r
001243
MC/DC0%0/2
@words > 1 or not $meta->{"split_words"}
@words > 1 not $meta->{"split_words"}
Truth table33%1/3
A or not(B)
A= @words > 1B= not $meta->{"split_words"}
ABresult
1X1
011
000
567 0 $$meta{string} = join ' ', @words;
568 }
569 else {
570 1243 $$meta{string} = $words[0];
571 1243 @words = $$self{stringparser}->split('word_gram', $words[0])
572 }
573 # FIXME parse word_gram and redir_gram at same time
574  
575 # parse environment
576 1243 ▶ if ($$meta{parse_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"parse_env"}) 1243 0 1243
577 1243 my $_env = delete $$meta{env}; # PWD and SHELL
578 1243 ▶ while ($words[0] =~ /^(\w[\w\-]*)=(.*)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0] =~ /^(\w[\w\-]*)=(.*)/s) 42 1243 1285
579 42 $$meta{compl} = shift @words;
580 42 $$meta{env}{$1} = $2
581 }
582 1243 ▶ if (! @words and $$meta{env}) { # special case
Branch: 2 branches
BranchTrueFalseTotal
if (not @words and $meta->{'env'}) { } 0 1243 1243
elsif ($meta->{'env'}) { } 42 1201 1243
Condition66%2/3
not @words and $meta->{"env"}
!ll&&!rl&&r
1207360
MC/DC0%0/2
not @words and $meta->{"env"}
not @words $meta->{"env"}
Truth table66%2/3
not(A) and B
A= not @wordsB= $meta->{"env"}
ABresult
0X0
100
111
583 0 @words = ('export', map $_.'='.$$meta{env}{$_}, keys %{$$meta{env}});
584 0 delete $$meta{env}; # duplicate would make var local
585 }
586 elsif ($$meta{env}) {
587 42 delete $$meta{compl}; # @words > 0
588 42 for (keys %{$$meta{env}}) {
589 42 my (undef, @w) = @{ $self->parse_words([$meta, $$meta{env}{$_}]) };
590 42 $$meta{env}{$_} = join ':', @w;
591 }
592 }
593 1243 for (keys %$_env) {
594 1243 ▶ $$meta{env}{$_} = $$_env{$_} unless defined $$meta{env}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta->{"env"}{$_} 1243 0 1243
595 }
596 }
597  
598 # parse redirections
599 1243 ▶ return [$meta, @words] unless $$meta{parse_fd};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"parse_fd"} 0 1243 1243
600 1243 my @s_words = map [ $$self{stringparser}->split('redirect_gram', $_) ], @words;
601 1243 ▶ return [$meta, @words] if ! grep {! ref $_} map @$_, @s_words;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not ref $_;} map(@$_, @s_words) 1134 109 1243
602 109 ▶ $$meta{fd} ||= [];
Condition50%1/2
$meta->{"fd"} ||= []
l!l
0109
MC/DC0%0/1
$meta->{"fd"} ||= []
$meta->{"fd"}
Truth table50%1/2
A
A= $meta->{"fd"}
Aresult
11
00
603 109 my @re;
604  
605 PARSE_REDIR_S_WORD:
606 770 my @parts = @{shift @s_words};
607 770 my $last = $#parts; # length of @parts changes later on
608 770 for (0 .. $#parts) {
609 807 ▶ next unless defined $parts[$_] and ! ref $parts[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts[$_] and not ref $parts[$_] 660 147 807
Condition100%3/3
defined $parts[$_] and not ref $parts[$_]
!ll&&!rl&&r
38622147
MC/DC100%2/2
defined $parts[$_] and not ref $parts[$_]
defined $parts[$_] not ref $parts[$_]
Truth table100%3/3
A and not(B)
A= defined $parts[$_]B= not ref $parts[$_]
ABresult
0X0
100
111
610 147 my $op = delete $parts[$_];
611 147 ▶ if ($op =~ /[^A-Z_]/) { # _SELF escape for "<fh>"
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ /[^A-Z_]/) { } 16 131 147
elsif ($op eq 'ERROR') { } 0 131 131
612 16 $parts[$_] = \$op;
613 16 next;
614 }
615 elsif ($op eq 'ERROR') {
616 error 'redirection operation not supported'
617 0 ▶ unless $$meta{pretend};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"pretend"} 0 0 0
618 }
619  
620 131 my ($n, $word);
621 131 ▶ if ($_ > 0 and ref $parts[$_-1]) { # find file descriptor number
Branch: 1 branch
BranchTrueFalseTotal
if ($_ > 0 and ref $parts[$_ - 1]) 76 55 131
Condition66%2/3
$_ > 0 and ref $parts[$_ - 1]
!ll&&!rl&&r
55076
MC/DC50%1/2
$_ > 0 and ref $parts[$_ - 1]
$_ > 0 ref $parts[$_ - 1]
Truth table66%2/3
A and B
A= $_ > 0B= ref $parts[$_ - 1]
ABresult
0X0
100
111
622 76 ▶ if (${$parts[$_-1]} =~ /^\d+$/) { $n = ${delete $parts[$_-1]} }
Branch: 1 branch
BranchTrueFalseTotal
if (${$parts[$_ - 1];} =~ /^\d+$/) { } 76 0 76
623 else {
624 0 ▶ ${$parts[$_-1]} =~ s/(\\\\)|(\\\d+)$|(\d+)$/$1 || $2/eg;
Condition0%0/3
$1 || $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 || $2
$1 $2
Truth table0%0/3
A || B
A= $1B= $2
ABresult
1X1
011
000
625 0 $n = $3;
626 }
627 }
628  
629 131 ▶ if ($_ < $#parts and ref $parts[$_+1]) { # find argument
Branch: 2 branches
BranchTrueFalseTotal
if ($_ < $#parts and ref $parts[$_ + 1]) { } 38 93 131
elsif (@s_words and ref $s_words[0][0]) { } 93 0 93
Condition66%2/3
$_ < $#parts and ref $parts[$_ + 1]
!ll&&!rl&&r
93038
MC/DC50%1/2
$_ < $#parts and ref $parts[$_ + 1]
$_ < $#parts ref $parts[$_ + 1]
Truth table66%2/3
A and B
A= $_ < $#partsB= ref $parts[$_ + 1]
ABresult
0X0
100
111
630 38 $word = ${ delete $parts[$_+1] };
631 38 ▶ $$meta{compl} = $word if $_+1 == $last and ! @s_words; # complete last word
Branch: 1 branch
BranchTrueFalseTotal
if $_ + 1 == $last and not @s_words 38 0 38
Condition33%1/3
$_ + 1 == $last and not @s_words
!ll&&!rl&&r
0038
MC/DC0%0/2
$_ + 1 == $last and not @s_words
$_ + 1 == $last not @s_words
Truth table33%1/3
A and not(B)
A= $_ + 1 == $lastB= not @s_words
ABresult
0X0
100
111
632 }
633 ▶ elsif (@s_words and ref $s_words[0][0]) {
Condition33%1/3
@s_words and ref $s_words[0][0]
!ll&&!rl&&r
0093
MC/DC0%0/2
@s_words and ref $s_words[0][0]
@s_words ref $s_words[0][0]
Truth table33%1/3
A and B
A= @s_wordsB= ref $s_words[0][0]
ABresult
0X0
100
111
634 93 $word = ${ delete $s_words[0][0] };
635 93 ▶ $$meta{compl} = $word if @s_words == 1 and ! @{$s_words[0]};
Branch: 1 branch
BranchTrueFalseTotal
if @s_words == 1 and not @{$s_words[0];} 55 38 93
Condition66%2/3
@s_words == 1 and not @{$s_words[0];}
!ll&&!rl&&r
38055
MC/DC50%1/2
@s_words == 1 and not @{$s_words[0];}
@s_words == 1 not @{$s_words[0];}
Truth table66%2/3
A and not(B)
A= @s_words == 1B= not @{$s_words[0];}
ABresult
0X0
100
111
636 }
637 else {
638 error 'redirection needs argument'
639 0 ▶ unless $op =~ /^DUP/ or $$meta{pretend};
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /^DUP/ or $meta->{"pretend"} 0 0 0
Condition0%0/3
$op =~ /^DUP/ or $meta->{"pretend"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$op =~ /^DUP/ or $meta->{"pretend"}
$op =~ /^DUP/ $meta->{"pretend"}
Truth table0%0/3
A or B
A= $op =~ /^DUP/B= $meta->{"pretend"}
ABresult
1X1
011
000
640 0 $$meta{compl} = '';
641 }
642  
643 131 ▶ unless ($$meta{pretend}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"pretend"}) 131 0 131
644 131 ▶ $n ||= ($op =~ /OUT$/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /OUT$/ ? : 55 0 55
Condition66%2/3
$n ||= $op =~ /OUT$/ ? 1 : 0
l!l&&r!l&&!r
76550
MC/DC0%0/2
$n ||= $op =~ /OUT$/ ? 1 : 0
$n $op =~ /OUT$/ ? 1 : 0
Truth table66%2/3
A ||= B
A= $nB= $op =~ /OUT$/ ? 1 : 0
ABresult
1X1
011
000
645 131 my (undef, @w) = @{ $self->parse_words([$meta, $word]) };
646 131 ▶ if (@w == 1) { push @{$$meta{fd}}, $n.$_redir_ops{$op}.$w[0] }
Branch: 2 branches
BranchTrueFalseTotal
if (@w == 1) { } 131 0 131
elsif (@w > 1) { } 0 0 0
647 0 elsif (@w > 1) { error 'redirection argument expands to multiple words' }
648 0 else { error 'redirection needs argument' } # @w < 1
649 }
650 }
651 770 push @re, map $$_, @parts;
652 770 ▶ goto PARSE_REDIR_S_WORD if @s_words;
Branch: 1 branch
BranchTrueFalseTotal
if @s_words 661 109 770
653  
654 109 return [$meta, @re];
655 }
656  
657 1329 ▶sub parse_aliases { # recursive sub (aliases are 3 way recursive, 2 ways are in this sub)
Subroutine parse_aliases: called CC=19 SCAR=42.4
Pod undocumented
658 1329 my ($self, $meta, @words) = @_;
659 my $aliases = ($$self{_settings}{mode} && ! $$meta{no_mode})
660 ? $$self{aliases}{'mode_'.$$self{_settings}{mode}}
661 1329 ▶ : $$self{aliases};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_settings'}{'mode'} && !$meta->{'no_mode'} ? : 0 1329 1329
Condition33%1/3
$self->{"_settings"}{"mode"} and not $meta->{"no_mode"}
!ll&&!rl&&r
132900
MC/DC0%0/2
$self->{"_settings"}{"mode"} and not $meta->{"no_mode"}
$self->{"_settings"}{"mode"} not $meta->{"no_mode"}
Truth table33%1/3
A and not(B)
A= $self->{"_settings"}{"mode"}B= not $meta->{"no_mode"}
ABresult
0X0
100
111
662 1329 ▶ return [$meta, @words] unless ref $aliases and exists $$aliases{$words[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aliases and exists $$aliases{$words[0]} 1273 56 1329
Condition66%2/3
ref $aliases and exists $$aliases{$words[0]}
!ll&&!rl&&r
0127356
MC/DC50%1/2
ref $aliases and exists $$aliases{$words[0]}
ref $aliases exists $$aliases{$words[0]}
Truth table66%2/3
A and B
A= ref $aliasesB= exists $$aliases{$words[0]}
ABresult
0X0
100
111
663 56 ▶ $$meta{alias_stack} ||= [];
Condition50%1/2
$meta->{"alias_stack"} ||= []
l!l
056
MC/DC0%0/1
$meta->{"alias_stack"} ||= []
$meta->{"alias_stack"}
Truth table50%1/2
A
A= $meta->{"alias_stack"}
Aresult
11
00
664 0 ▶ return [$meta, @words] if grep {$_ eq $words[0]} @{$$meta{alias_stack}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $words[0];} @{$$meta{"alias_stack"};} 0 56 56
665 56 push @{$$meta{alias_stack}}, $words[0];
666  
667 56 my $string = $$aliases{$words[0]};
668 56 debug "$words[0] is aliased to: $string";
669 56 shift @words;
670  
671 =cut
672  
673 # saving code for later usage in pipelines
674 # this is not the right place for it
675  
676 { # variable substitution in the macro
677 local @_ = @words;
678 my $n = ( $string =~ s# (?<!\\) (?: \$_\[(\d+)\] | \@_(\[.*?\])? ) #
679 if ($1) { $words[$1] }
680 elsif ($2) { eval "join ' ', \@_[$2]" }
681 else { join ' ', @words }
682 #xge );
683 @words = () if $n;
684 }
685  
686 =cut
687  
688 56 my @as = @{$$meta{alias_stack}}; # force copy
689 my @l = map {
690 ref($_) ? [
691 { alias_stack => [@as] },
692 56 ▶ $$self{stringparser}->split('word_gram', $$_)
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 56 0 56
$meta->{'split_script'} ? : 56 0 56
693 ] : $_
694 56 } $$meta{split_script} ? ($$self{stringparser}->split('script_gram', $string)) : ($string);
695  
696 56 ▶ if ( my ($firstref) = grep ref($_), @l ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($firstref) = grep(ref($_), @l)) 56 0 56
697 56 $$firstref[0] = $meta; # re-insert %meta
698 56 ▶ ++$$meta{no_mode} and (length $$firstref[1] or delete $$firstref[1])
Branch: 1 branch
BranchTrueFalseTotal
if @$firstref > 1 and $firstref->[1] =~ s/^\!\s*// 0 56 56
Condition16%2/12
@$firstref > 1 && $firstref->[1] =~ s/^\!\s*// && (++$meta->{'no_mode'} && (length $firstref->[1] || delete $firstref->[1]))
!ll&&!rl&&r
5600
@$firstref > 1 and $firstref->[1] =~ s/^\!\s*//
!ll&&!rl&&r
0560
++$meta->{"no_mode"} and length $firstref->[1] || delete $firstref->[1]
!ll&&!rl&&r
000
length $firstref->[1] or delete $firstref->[1]
l!l&&r!l&&!r
000
MC/DC0%0/5
@$firstref > 1 && $firstref->[1] =~ s/^\!\s*// && (++$meta->{'no_mode'} && (length $firstref->[1] || delete $firstref->[1]))
@$firstref > 1 $firstref->[1] =~ s/^\!\s*// ++$meta->{"no_mode"} length $firstref->[1] delete $firstref->[1]
Truth table16%1/6
A and B && C and D or E
A= @$firstref > 1B= $firstref->[1] =~ s/^\!\s*//C= ++$meta->{"no_mode"}D= length $firstref->[1]E= delete $firstref->[1]
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
699 if @$firstref > 1 and $$firstref[1] =~ s/^\!\s*//; # check mode
700 }
701  
702 56 ▶ if ($string =~ /\s$/) { # recurs for 2nd word - see posix spec
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ /\s$/) { } 0 56 56
elsif (@l == 1) { } 56 0 56
703 0 my @l1 = $self->parse_aliases({}, @words); # recurs
704 0 ▶ push @{$l[-1]}, splice(@{ shift(@l1) }, 1) if ref $l[-1] and ref $l1[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $l[-1] and ref $l1[0] 0 0 0
Condition0%0/3
ref $l[-1] and ref $l1[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $l[-1] and ref $l1[0]
ref $l[-1] ref $l1[0]
Truth table0%0/3
A and B
A= ref $l[-1]B= ref $l1[0]
ABresult
0X0
100
111
705 0 push @l, @l1;
706 }
707 56 elsif (@l == 1) { return $self->parse_aliases(@{$l[0]}, @words) } # recurs
708 else {
709 0 ▶ if (ref $l[-1]) { push @{$l[-1]}, @words }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $l[-1]) { } 0 0 0
710 0 else { push @l, \@words }
711 }
712  
713 0 return @l;
714 }
715  
716 1142 ▶sub parse_words { # expand words etc.
Subroutine parse_words: called CC=7 SCAR=20.4
Pod undocumented
717 1142 my ($self, $block) = @_;
718  
719 # custom stack
720 1142 for ($$self{parser}->stack('word_expansion')) {
721 0 my $re = $_->($block);
722 0 ▶ $block = $re if $re;
Branch: 1 branch
BranchTrueFalseTotal
if $re 0 0 0
723 }
724  
725 # default expansions
726 # expand_comm resets zoidcmd, all other stuff is left for appliction level re-parsing
727 @$block = $self->$_(@$block)
728 1142 for grep $$block[0]{$_}, qw/expand_param expand_comm expand_path/;
729  
730 # remove quote
731 1140 my ($meta, @words) = @$block;
732 1140 for (@words) {
733 4274 ▶ if (/^([\/\w]+=)?(['"])(.*)\2$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^([/\w]+=)?(['"])(.*)\2$]s) 361 3913 4274
734 # quote removal and escape removal within quotes
735 361 $_ = $1.$3;
736 361 ▶ if ($2 eq '\'') { $_ =~ s/\\([\\'])/$1/ge }
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq q[']) { } 312 49 361
737 0 else { $_ =~ s/\\(.)/$1/ge }
738 }
739 # FIXME also do escape removal here
740 # is now done by File::Glob
741 }
742  
743 1140 return [$meta, @words];
744 }
745  
746 =cut
747  
748 # so far no luck of getting this to work - maybe combine intgrate
749 # this with stringparser some how :S
750  
751 our $_IFS = [undef, qr/\s+/, qr/\s+/];
752 sub _split_on_IFS { # bloody heavy routine for such a simple parsing rule
753 my $self = shift;
754 unless ($ENV{IFS} eq $$_IFS[0]) {
755 debug "generating new IFS regexes";
756 if (! defined $ENV{IFS}) { $_IFS = [undef, qr/\s+/, qr/\s+/] }
757 elsif ($ENV{IFS} eq '') { $_IFS = [''] }
758 else {
759 my $ifs_white = join '', ($ENV{IFS} =~ m/(\s)/g);
760 my $ifs_char = join '', ($ENV{IFS} =~ m/(\S)/g);
761 $_IFS = [ $ENV{IFS}, qr/[$ifs_white]+/,
762 qr/[$ifs_white]*[$ifs_char][$ifs_white]*|[$ifs_white]+/ ];
763 }
764 debug "IFS = ['$ENV{IFS}', $$_IFS[1], $$_IFS[2]]";
765 }
766 debug "IFS = ['$ENV{IFS}', $$_IFS[1], $$_IFS[2]]";
767 return @_ if defined $$_IFS[0] and $$_IFS[0] eq '';
768 return map {
769 $_ =~ s/(\\\\)|^$$_IFS[1]|(?<!\\)$$_IFS[1]$/$1?$1:''/ge;
770 $$self{stringparser}->split($$_IFS[2], $_)
771 } @_;
772 }
773  
774 =cut
775  
776 =cut
777  
778 sub expand_braces {
779 my ($self, $meta, @words) = @_;
780 my @re;
781 for (@words) {
782 my @parts = $$self{stringparser}->split('expand_braces_gram', $_);
783 error $$self{stringparser}{broken} if $$self{stringparser}{broken};
784 # FIXME let stringparser do the error throwing ?
785 unless (@parts > 1) {
786 push @re, $_;
787 next;
788 }
789 for (0 .. $#parts) {
790 if ($parts[$_] eq 'BRACE') {
791 my $braced = delete $parts[$_+1];
792  
793 }
794 elsif (ref $parts[$_]) { $parts[$_] = ${$parts[$_]} }
795 }
796 push @re, join '', map {ref($_) ? (@$_) : $_} @parts;
797 }
798 return ($meta, @re);
799 }
800  
801 =cut
802  
803 1142 ▶sub expand_param {
Subroutine expand_param: called CC=28 SCAR=46.4
Pod undocumented
804 # make sure $() and @() remain untouched ... `` are considered quotes
805 64 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
806 1142 my ($self, $meta, @words) = @_;
807 1142 my ($e);
808
809 1142 my $class = $$self{_settings}{perl}{namespace};
810 @words = map { # substitute vars
811 4132 ▶ if (/^([\/\w]+=)?'.*'$/s) { $_ }# skip quoted words
Branch: 1 branch
BranchTrueFalseTotal
if (m[^([/\w]+=)?'.*'$]s) { } 312 3820 4132
812 else {
813 3820 my $old = $_;
814 0 ▶ s{(?<!\\)\$\?}{ ref($$self{error}) ? $$self{error}{exit_status} : $$self{error} ? 1 : 0 }ge;
Branch: 2 branches
BranchTrueFalseTotal
ref $self->{'error'} ? : 0 0 0
$self->{'error'} ? : 0 0 0
815 3820 s{ (?<!\\) \$ (?: \{ (.*?) \} | ([\w-]+) ) (?: \[(-?\d+)\] )? }{
816 154 ▶ my ($w, $i) = ($1 || $2, $3);
Condition33%1/3
$1 || $2
l!l&&r!l&&!r
01540
MC/DC0%0/2
$1 || $2
$1 $2
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
817 154 ▶ $e ||= "no advanced expansion for \$\{$w\}" if $w =~ /[^\w-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $w =~ /[^\w-]/ 0 154 154
Condition0%0/2
$e ||= "no advanced expansion for \${$w}"
l!l
00
MC/DC0%0/1
$e ||= "no advanced expansion for \${$w}"
$e
Truth table0%0/2
A
A= $e
Aresult
11
00
818 0 ▶ if ($w eq '_') { $w = $$self{topic} }
Branch: 3 branches
BranchTrueFalseTotal
if ($w eq '_') { } 0 154 154
elsif (exists $meta->{'env'}{$w} or exists $ENV{$w}) { } 154 0 154
elsif ($i ? defined *{$class . '::' . $w;}{'ARRAY'} : defined *{$class . '::' . $w;}{'SCALAR'}) { } 0 0 0
819 ▶ elsif (exists $$meta{env}{$w} or exists $ENV{$w}) {
Condition33%1/3
exists $meta->{"env"}{$w} or exists $ENV{$w}
l!l&&r!l&&!r
01540
MC/DC0%0/2
exists $meta->{"env"}{$w} or exists $ENV{$w}
exists $meta->{"env"}{$w} exists $ENV{$w}
Truth table33%1/3
A or B
A= exists $meta->{"env"}{$w}B= exists $ENV{$w}
ABresult
1X1
011
000
820 154 ▶ $w = exists( $$meta{env}{$w} ) ? $$meta{env}{$w} : $ENV{$w} ;
Branch: 1 branch
BranchTrueFalseTotal
exists $meta->{'env'}{$w} ? : 0 154 154
821 154 ▶ $w = $i ? (split /:/, $w)[$i] : $w;
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 52 102 154
822 }
823 0 ▶ elsif ($i ? defined(*{$class.'::'.$w}{ARRAY}) : defined(*{$class.'::'.$w}{SCALAR})) {
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 0 0 0
824 0 ▶ $w = $i ? ${$class.'::'.$w}[$i] : ${$class.'::'.$w};
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 0 0 0
825 }
826 0 else { $w = '' }
827 154 $w =~ s/\\/\\\\/g; # literal backslashes
828 154 $w;
829 }exg;
830 3820 ▶ if ($_ eq $old or $_ =~ /^".*"$/) { $_ }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $old or $_ =~ /^".*"$/) { } 3714 106 3820
Condition100%3/3
$_ eq $old or $_ =~ /^".*"$/
l!l&&r!l&&!r
366648106
MC/DC100%2/2
$_ eq $old or $_ =~ /^".*"$/
$_ eq $old $_ =~ /^".*"$/
Truth table100%3/3
A or B
A= $_ eq $oldB= $_ =~ /^".*"$/
ABresult
1X1
011
000
831 106 else { $$self{stringparser}->split('word_gram', $_) }
832 # TODO honour IFS here -- POSIX tells us so
833 }
834 }
835  
836 @words = map { # substitute arrays
837 1142 ▶ if (m/^ \@ (?: \{ (.*?) \} | ([\w-]+) ) $/x) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^ \@ (?: \{ (.*?) \} | ([\w-]+) ) $/x) { } 1 4129 4130
838 1 ▶ my $w = $1 || $2;
Condition33%1/3
$1 || $2
l!l&&r!l&&!r
010
MC/DC0%0/2
$1 || $2
$1 $2
Truth table33%1/3
A || B
A= $1B= $2
ABresult
1X1
011
000
839 1 ▶ $e ||= "no advanced expansion for \@\{$w\}" if $w =~ /[^\w-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $w =~ /[^\w-]/ 0 1 1
Condition0%0/2
$e ||= "no advanced expansion for \@{$w}"
l!l
00
MC/DC0%0/1
$e ||= "no advanced expansion for \@{$w}"
$e
Truth table0%0/2
A
A= $e
Aresult
11
00
840 1 ▶ $e ||= '@_ is reserved for future syntax usage' if $2 eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $2 eq "_" 0 1 1
Condition0%0/2
$e ||= "\@_ is reserved for future syntax usage"
l!l
00
MC/DC0%0/1
$e ||= "\@_ is reserved for future syntax usage"
$e
Truth table0%0/2
A
A= $e
Aresult
11
00
841 1 ▶ if (exists $$meta{env}{$w} or exists $ENV{$w}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $meta->{'env'}{$w} or exists $ENV{$w}) { } 1 0 1
elsif (defined *{$class . '::' . $w;}{'ARRAY'}) { } 0 0 0
Condition33%1/3
exists $meta->{"env"}{$w} or exists $ENV{$w}
l!l&&r!l&&!r
010
MC/DC0%0/2
exists $meta->{"env"}{$w} or exists $ENV{$w}
exists $meta->{"env"}{$w} exists $ENV{$w}
Truth table33%1/3
A or B
A= exists $meta->{"env"}{$w}B= exists $ENV{$w}
ABresult
1X1
011
000
842 1 ▶ $w = (exists $$meta{env}{$w}) ? $$meta{env}{$w} : $ENV{$w};
Branch: 1 branch
BranchTrueFalseTotal
exists $meta->{'env'}{$w} ? : 0 1 1
843 1 map {s/\\/\\\\/g; $_} split /:/, $w;
844 }
845 0 elsif (defined *{$class.'::'.$w}{ARRAY}) {
846 0 map {s/\\/\\\\/g; $_} @{$class.'::'.$w};
847 }
848 0 else { () }
849 }
850 4129 else { $_ }
851 } @words;
852 1142 ▶ error $e if $e; # "Attempt to free unreferenced scalar" when dying inside the map !?
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 1142 1142
853 1142 return ($meta, @words);
854 }
855  
856 1142 ▶sub expand_comm {
Subroutine expand_comm: called CC=14 SCAR=27.7
Pod undocumented
857 1142 my ($self, $meta, @words) = @_;
858 1142 my @re;
859 1142 my $m = {capture => 1, env => $$meta{env}};
860 1142 for (@words) {
861 4186 ▶ if (/^([\/\w]+=)?'.*'$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if (m[^([/\w]+=)?'.*'$]s) { } 312 3874 4186
elsif (/^\@\((.*?)\)$/s) { } 10 3864 3874
862 312 push @re, $_;
863 }
864 elsif (/^\@\((.*?)\)$/s) {
865 10 debug "\@() subz: $1";
866 10 push @re, $self->shell($m, $1); # list context
867 }
868 else {
869 3864 ▶ my $quote = $1 if s/^(")(.*)\1$/$2/s;
Branch: 1 branch
BranchTrueFalseTotal
if s/^(")(.*)\1$/$2/s 48 3816 3864
870 3864 my @parts = $$self{stringparser}->split('expand_comm_gram', $_);
871 3864 ▶ error $$self{stringparser}{broken} if $$self{stringparser}{broken};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stringparser"}{"broken"} 0 3864 3864
872 # FIXME let stringparser do the error throwing ?
873 3864 ▶ unless (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@parts > 1) 3839 25 3864
874 3839 ▶ push @re, $quote ? $quote.$_.$quote : $_;
Branch: 1 branch
BranchTrueFalseTotal
$quote ? : 48 3791 3839
875 3839 next;
876 }
877 25 for (0 .. $#parts) {
878 49 ▶ if ($parts[$_] eq 'COMM') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parts[$_] eq 'COMM') { } 25 24 49
elsif (ref $parts[$_]) { } 0 24 24
879 25 debug '$() subz: '.$parts[$_+1];
880 25 $parts[$_] = $self->shell($m, ${delete $parts[$_+1]}); # scalar context
881 0 ▶ if ($_ < $#parts-1 and ${$parts[$_+2]} =~ s/^\[(\d*)\]//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < $#parts - 1 and ${$parts[$_ + 2];} =~ s/^\[(\d*)\]//) { } 0 24 24
Condition33%1/3
$_ < $#parts - 1 and ${$parts[$_ + 2];} =~ s/^\[(\d*)\]//
!ll&&!rl&&r
2400
MC/DC0%0/2
$_ < $#parts - 1 and ${$parts[$_ + 2];} =~ s/^\[(\d*)\]//
$_ < $#parts - 1 ${$parts[$_ + 2];} =~ s/^\[(\d*)\]//
Truth table33%1/3
A and B
A= $_ < $#parts - 1B= ${$parts[$_ + 2];} =~ s/^\[(\d*)\]//
ABresult
0X0
100
111
882 0 $parts[$_] = $parts[$_][$1];
883 0 chomp $parts[$_];
884 }
885 24 else { $parts[$_] = "$parts[$_]" } # just to be sure bout overload
886 }
887 0 elsif (ref $parts[$_]) { $parts[$_] = ${$parts[$_]} }
888 }
889 24 my $word = join '', @parts; # map {ref($_) ? (@$_) : $_} @parts;
890 0 ▶ if ($quote) { push @re, $quote.$word.$quote }
Branch: 1 branch
BranchTrueFalseTotal
if ($quote) { } 0 24 24
891 24 else { push @re, $$self{stringparser}->split('word_gram', $word) }
892 # TODO honour IFS here - POSIX says so
893 }
894 }
895 1140 $$meta{env}{ZOIDCMD} = $$meta{zoidcmd} = join ' ', @re;
896 1140 return $meta, @re;
897 }
898  
899 # See File::Glob for explanation of behaviour
900 64 our $_GLOB_OPTS = File::Glob::GLOB_TILDE() | File::Glob::GLOB_QUOTE() | File::Glob::GLOB_BRACE();
901 64 our $_NC_GLOB_OPTS = $_GLOB_OPTS | File::Glob::GLOB_NOCHECK();
902  
903 1140 ▶sub expand_path { # path expansion
Subroutine expand_path: called CC=13 SCAR=30.7
Pod undocumented
904 # FIXME add 'failglob' setting (useful in scripts)
905 1140 my ($self, $meta, @files) = @_;
906 1140 ▶ return $meta, @files if $$self{_settings}{noglob};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_settings"}{"noglob"} 0 1140 1140
907 1140 ▶ my $opts = $$self{_settings}{nullglob} ? $_GLOB_OPTS : $_NC_GLOB_OPTS;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_settings'}{'nullglob'} ? : 0 1140 1140
908 1140 ▶ $opts |= File::Glob::GLOB_NOCASE() if $$self{_settings}{nocaseglob};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_settings"}{"nocaseglob"} 0 1140 1140
909 return $meta, map {
910 1140 ▶ if (/^([\/\w]+=)?(['"])/) { $_ } # quoted
Branch: 3 branches
BranchTrueFalseTotal
if (m[^([/\w]+=)?(['"])]) { } 361 3910 4271
elsif (/^m\{(.*)\}([imsx]*)$/) { } 0 3910 3910
elsif (/^~|[*?\[\]{}]/) { } 11 3899 3910
911 elsif (/^m\{(.*)\}([imsx]*)$/) { # regex globs
912 0 my @r = regex_glob($1, $2);
913 0 ▶ if (@r) { @r }
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 0 0 0
914 0 ▶ else { $_ =~ s/\\\\|\\(.)/$1||'\\'/eg; $_ }
Condition0%0/2
$1 || '\\'
l!l
00
MC/DC0%0/1
$1 || '\\'
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
915 }
916 elsif (/^~|[*?\[\]{}]/) { # normal globs
917 # TODO: {x..y} brace expansion
918 4 ▶ $_ =~ s#(\\\\)|(?<!\\){([^,{}]*)(?<!\\)}#$1?$1:"\\{$2\\}"#ge
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 4 4
919 11 ▶ unless $$self{_settings}{voidbraces}; # brace pre-parsing
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_settings"}{"voidbraces"} 11 0 11
920 11 my @r = File::Glob::bsd_glob($_, $opts);
921 11 debug "glob: $_ ==> ".join(', ', @r);
922 11 ▶ ($_ !~ /^-/) ? (grep {$_ !~ /^-/} @r) : (@r);
Branch: 1 branch
BranchTrueFalseTotal
!($_ =~ /^-/) ? : 11 0 11
923 # protect against implict switches as file names
924 }
925 3899 ▶ else { $_ =~ s/\\\\|\\(.)/$1||'\\'/eg; $_ } # remove escapes # FIXME should be done in parse_words like quote removal
Condition100%2/2
$1 || '\\'
l!l
481
MC/DC100%1/1
$1 || '\\'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
926 } @files ;
927 }
928  
929 216 ▶sub parse_perl { # parse switches
Subroutine parse_perl: called CC=9 SCAR=22.7
Pod undocumented
930 216 my ($self, $block) = @_;
931 216 my ($meta, $string) = @$block;
932 216 my %opts = map {($_ => 1)} split '', $$self{_settings}{perl}{opts};
933 216 ▶ $opts{z} = 0 if delete $opts{Z};
Branch: 1 branch
BranchTrueFalseTotal
if delete $opts{"Z"} 216 0 216
934 216 $opts{$_}++ for split '', $$meta{opts};
935 216 ▶ $opts{z} = 0 if delete $opts{Z};
Branch: 1 branch
BranchTrueFalseTotal
if delete $opts{"Z"} 0 216 216
936 216 debug 'perl block options: ', \%opts;
937  
938 216 ▶ ($meta, $string) = $self->_expand_zoid($meta, $string) unless $opts{z};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"z"} 216 0 216
939  
940 216 ▶ if ($opts{g}) { $string = "\nwhile (<STDIN>) {\n\tif (eval {".$string."}) { print \$_; }\n}" }
Branch: 3 branches
BranchTrueFalseTotal
if ($opts{'g'}) { } 4 212 216
elsif ($opts{'p'}) { } 4 208 212
elsif ($opts{'n'}) { } 0 208 208
941 4 elsif ($opts{p}) { $string = "\nwhile (<STDIN>) {\n\t".$string.";\n\tprint \$_\n}" }
942 0 elsif ($opts{n}) { $string = "\nwhile (<STDIN>) {\n\t".$string.";\n}" }
943  
944 216 ▶ $string = "no strict;\n".$string unless $opts{z};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"z"} 216 0 216
945  
946 216 return [$meta, $string];
947 }
948  
949 223 ▶sub _expand_zoid {
Subroutine _expand_zoid: called CC=15 SCAR=27.3
950 223 my ($self, $meta, $code) = @_;
951  
952 223 my @parts = $$self{stringparser}->split('dezoid_gram', $code);
953 223 my @idx = grep {! ref $parts[$_]} 0 .. $#parts;
954 223 ▶ @parts = map {ref($_) ? $$_ : $_} @parts;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 404 198 602
955  
956 223 my $pre = '';
957 223 for (@idx) { # probably could be done much cleaner
958 198 my $token = delete $parts[$_];
959 198 ▶ my $next = ($_ < $#parts) ? $parts[$_+1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$_ < $#parts ? : 198 0 198
960 198 ▶ my $prev = $_ ? $parts[$_-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 184 14 198
961  
962 198 my $class = $$self{_settings}{perl}{namespace};
963 198 ▶ if ($token =~ /^([\@\$])(\w+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token =~ /^([\@\$])(\w+)/) { } 191 7 198
elsif ($prev =~ /[\w\}\)\]]$/) { } 1 6 7
964 191 my ($sigil, $name) = ($1, $2);
965 191 ▶ if ( # global, reserved or non-env var
Branch: 2 branches
BranchTrueFalseTotal
if ($next =~ /^::/ or grep {$name eq $_;} '_', 'ARGV', 'ENV', 'SIG', 'INC', 'JOBS' or not exists $ENV{$name} || exists $meta->{'env'}{$name}) { } 147 44 191
elsif ($sigil eq '@' or $next =~ /^\[/) { } 1 43 44
Condition83%5/6
$next =~ /^::/ or grep {$name eq $_;} "_", "ARGV", "ENV", "SIG", "INC", "JOBS" or not exists $ENV{$name} || exists $meta->{"env"}{$name}
l!l&&r!l&&!r
1361144
$next =~ /^::/ or grep {$name eq $_;} "_", "ARGV", "ENV", "SIG", "INC", "JOBS"
l!l&&r!l&&!r
013655
MC/DC66%2/3
$next =~ /^::/ or grep {$name eq $_;} "_", "ARGV", "ENV", "SIG", "INC", "JOBS" or not exists $ENV{$name} || exists $meta->{"env"}{$name}
$next =~ /^::/ grep {$name eq $_;} "_", "ARGV", "ENV", "SIG", "INC", "JOBS" not exists $ENV{$name} || exists $meta->{"env"}{$name}
Truth table75%3/4
A or B or not(C)
A= $next =~ /^::/B= grep {$name eq $_;} "_", "ARGV", "ENV", "SIG", "INC", "JOBS"C= not exists $ENV{$name} || exists $meta->{"env"}{$name}
ABCresult
1XX1
01X1
0011
0000
966 $next =~ /^::/
967 1146 ▶ or grep {$name eq $_} qw/_ ARGV ENV SIG INC JOBS/
Condition100%3/3
exists $ENV{$name} || exists $meta->{'env'}{$name}
l!l&&r!l&&!r
24211
MC/DC100%2/2
exists $ENV{$name} || exists $meta->{'env'}{$name}
exists $ENV{$name} exists $meta->{'env'}{$name}
Truth table100%3/3
A || B
A= exists $ENV{$name}B= exists $meta->{'env'}{$name}
ABresult
1X1
011
000
968 or ! exists $ENV{$name} and ! exists $$meta{env}{$name}
969 147 ) { $parts[$_] = $token }
970 ▶ elsif ($sigil eq '@' or $next =~ /^\[/) { # array
Condition66%2/3
$sigil eq "\@" or $next =~ /^\[/
l!l&&r!l&&!r
1043
MC/DC50%1/2
$sigil eq "\@" or $next =~ /^\[/
$sigil eq "\@" $next =~ /^\[/
Truth table66%2/3
A or B
A= $sigil eq "\@"B= $next =~ /^\[/
ABresult
1X1
011
000
971 64 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
972 $pre .= "Env->import('$token');\n"
973 0 ▶ unless defined *{$class.'::'.$name}{ARRAY} and @{$class.'::'.$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$class . '::' . $name;}{"ARRAY"} and @{$class . '::' . $name;} 1 0 1
Condition33%1/3
defined *{$class . '::' . $name;}{"ARRAY"} and @{$class . '::' . $name;}
!ll&&!rl&&r
100
MC/DC0%0/2
defined *{$class . '::' . $name;}{"ARRAY"} and @{$class . '::' . $name;}
defined *{$class . '::' . $name;}{"ARRAY"} @{$class . '::' . $name;}
Truth table33%1/3
A and B
A= defined *{$class . '::' . $name;}{"ARRAY"}B= @{$class . '::' . $name;}
ABresult
0X0
100
111
974 1 $parts[$_] = $token;
975 }
976 43 else { $parts[$_] = '$ENV{'.$name.'}' } # scalar
977 }
978 # else token eq 'ARR'
979 1 elsif ($prev =~ /[\w\}\)\]]$/) { $parts[$_] = '->' }
980 6 else { $parts[$_] = '$shell->' }
981 }
982  
983 223 return $meta, $pre . join '', grep defined($_), @parts;
984 }
985  
986 # ########## #
987 # Exec stuff #
988 # ########## #
989  
990 310 ▶sub eval_block { # real exec code
Subroutine eval_block: called CC=4 SCAR=17.3
Pod undocumented
991 310 my ($self, $ref) = @_;
992 310 my $context = $$ref[0]{context};
993  
994 310 ▶ if ($$self{parser}{$context}{handler}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'parser'}{$context}{'handler'}) { } 0 310 310
elsif ($self->can('_do_' . lc($context))) { } 310 0 310
995 0 debug "going to call handler for context: $context";
996 0 $$self{parser}{$context}{handler}->($ref);
997 }
998 elsif ($self->can('_do_'.lc($context))) {
999 310 my $sub = '_do_'.lc($context);
1000 310 debug "going to call sub: $sub";
1001 310 $self->$sub(@$ref);
1002 }
1003 else {
1004 0 ▶ $context
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 0 0 0
1005 ? error "No handler defined for context $context"
1006 : bug 'No context defined !'
1007 }
1008 }
1009  
1010 # FIXME FIXME remove _do_* subs below and store them in {parser}
1011  
1012 10 ▶sub _do_subz { # sub shell, forked if all is well
Subroutine _do_subz: called CC=3 SCAR=11.0
1013 10 my ($self, $meta) = @_;
1014 10 my $cmd = $$meta{zoidcmd};
1015 10 ▶ $cmd = $1 if $cmd =~ /^\s*\((.*)\)\s*$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^\s*\((.*)\)\s*$/s 10 0 10
1016 10 %$meta = map {($_ => $$meta{$_})} qw/env/; # FIXME also add parser opts n stuff
1017 # FIXME reset mode n stuff ?
1018 10 $self->shell_string($meta, $cmd);
1019 4 ▶ error $$self{error} if $$self{error}; # forward the error
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 2 2 4
1020 }
1021  
1022 93 ▶sub _do_cmd {
Subroutine _do_cmd: called CC=12 SCAR=27.1
1023 93 my ($self, $meta, $cmd, @args) = @_;
1024 # exec = exexvp which checks $PATH for us
1025 # the block syntax to force use of execvp, not shell for one argument list
1026 # If a command is not found, the exit status shall be 127. If the command name is found,
1027 # but it is not an executable utility, the exit status shall be 126.
1028 93 ▶ $$meta{cmdtype} ||= '';
Condition50%1/2
$meta->{"cmdtype"} ||= ""
l!l
093
MC/DC0%0/1
$meta->{"cmdtype"} ||= ""
$meta->{"cmdtype"}
Truth table50%1/2
A
A= $meta->{"cmdtype"}
Aresult
11
00
1029 93 ▶ if ($cmd =~ m|/|) { # executable file
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd =~ m[/]) { } 6 87 93
elsif ($meta->{'cmdtype'} eq 'builtin' or exists $self->{'commands'}{$cmd}) { } 42 45 87
1030 6 ▶ error 'builtin should not contain a "/"' if $$meta{cmdtype} eq 'builtin';
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"cmdtype"} eq "builtin" 0 6 6
1031 6 ▶ error {exit_status => 127}, $cmd.': No such file or directory' unless -e $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cmd 0 6 6
1032 6 ▶ error {exit_status => 126}, $cmd.': is a directory' if -d _;
Branch: 1 branch
BranchTrueFalseTotal
if -d _ 0 6 6
1033 6 ▶ error {exit_status => 126}, $cmd.': Permission denied' unless -x _;
Branch: 1 branch
BranchTrueFalseTotal
unless -x _ 0 6 6
1034 6 debug 'going to exec file: ', join ', ', $cmd, @args;
1035 6 ▶ exec {$cmd} $cmd, @args or error {exit_status => 127}, $cmd.': command not found';
Branch: 1 branch
BranchTrueFalseTotal
exec {$cmd;} $cmd, @args or error({"exit_status", 127}, $cmd . ': command not found') 0 0 0
1036 }
1037 ▶ elsif ($$meta{cmdtype} eq 'builtin' or exists $$self{commands}{$cmd}) { # built-in, not forked I hope
Condition66%2/3
$meta->{"cmdtype"} eq "builtin" or exists $self->{"commands"}{$cmd}
l!l&&r!l&&!r
04245
MC/DC50%1/2
$meta->{"cmdtype"} eq "builtin" or exists $self->{"commands"}{$cmd}
$meta->{"cmdtype"} eq "builtin" exists $self->{"commands"}{$cmd}
Truth table66%2/3
A or B
A= $meta->{"cmdtype"} eq "builtin"B= exists $self->{"commands"}{$cmd}
ABresult
1X1
011
000
1038 42 ▶ error {exit_status => 127}, $cmd.': no such builtin' unless exists $$self{commands}{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"commands"}{$cmd} 0 42 42
1039 42 debug 'going to do built-in: ', join ', ', $cmd, @args;
1040 42 local $Zoidberg::Utils::Error::Scope = $cmd;
1041 42 $$self{commands}{$cmd}->(@args);
1042 }
1043 else { # command in path ?
1044 45 debug 'going to exec: ', join ', ', $cmd, @args;
1045 45 ▶ exec {$cmd} $cmd, @args or error {exit_status => 127}, $cmd.': command not found';
Branch: 1 branch
BranchTrueFalseTotal
exec {$cmd;} $cmd, @args or error({"exit_status", 127}, $cmd . ': command not found') 0 0 0
1046 }
1047 }
1048  
1049 207 ▶sub _do_perl {
Subroutine _do_perl: called CC=9 SCAR=24.7
1050 207 my ($shell, $_Meta, $_Code) = @_;
1051 207 ▶ my $_Class = $$shell{_settings}{perl}{namespace} || 'Zoidberg::Eval';
Condition50%1/2
$shell->{'_settings'}{'perl'}{'namespace'} || 'Zoidberg::Eval'
l!l
2070
MC/DC0%0/1
$shell->{'_settings'}{'perl'}{'namespace'} || 'Zoidberg::Eval'
$shell->{'_settings'}{'perl'}{'namespace'}
Truth table50%1/2
A
A= $shell->{'_settings'}{'perl'}{'namespace'}
Aresult
11
00
1052 207 ▶ $_Code .= ";\n\$_Class = __PACKAGE__;" if $_Code =~ /package/;
Branch: 1 branch
BranchTrueFalseTotal
if $_Code =~ /package/ 0 207 207
1053 207 $_Code = "package $_Class;\n$_Code";
1054 207 undef $_Class;
1055 207 debug "going to eval perl code: << '...'\n$_Code\n...";
1056  
1057 207 local $Zoidberg::Utils::Error::Scope = ['zoid', 0];
1058 207 $_ = $$shell{topic};
1059 207 ▶ $? = $$shell{error}{exit_status} if ref $$shell{error};
Branch: 1 branch
BranchTrueFalseTotal
if ref $shell->{"error"} 0 207 207
1060 0 ▶ ref($_Code) ? eval { $_Code->() } : eval $_Code;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_Code ? : 0 207 207
1061 207 ▶ if ($@) { # post parse errors
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 207 207
1062 0 ▶ die if ref $@; # just propagate the exception
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 0 0
1063 0 $@ =~ s/ at \(eval \d+\) line (\d+)(\.|,.*\.)$/ at line $1/;
1064 0 error { string => $@, scope => [] };
1065 }
1066 else {
1067 207 $$shell{topic} = $_;
1068 207 ▶ $$shell{settings}{perl}{namespace} = $_Class if $_Class;
Branch: 1 branch
BranchTrueFalseTotal
if $_Class 0 207 207
1069 207 ▶ print "\n" if $$shell{_settings}{interactive}; # ugly hack
Branch: 1 branch
BranchTrueFalseTotal
if $shell->{"_settings"}{"interactive"} 0 207 207
1070 }
1071 }
1072  
1073 # ############## #
1074 # some functions #
1075 # ############## #
1076  
1077 =item mode [mode]
1078  
1079 Without arguments prints the current mode.
1080 With arguments sets the mode.
1081  
1082 =cut
1083  
1084 0 ▶sub mode {
Subroutine mode: not called CC=8 SCAR=42.8
Pod documented
1085 0 my $self = shift;
1086 0 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
1087 0 ▶ output $$self{_settings}{mode} if $$self{_settings}{mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_settings"}{"mode"} 0 0 0
1088 0 return;
1089 }
1090 0 my $mode = shift;
1091 0 ▶ if ($mode eq '-' or $mode eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq '-' or $mode eq 'default') { } 0 0 0
Condition0%0/3
$mode eq "-" or $mode eq "default"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "-" or $mode eq "default"
$mode eq "-" $mode eq "default"
Truth table0%0/3
A or B
A= $mode eq "-"B= $mode eq "default"
ABresult
1X1
011
000
1092 0 $$self{settings}{mode} = undef;
1093 }
1094 else {
1095 0 ▶ my $m = ($mode =~ /::/) ? $mode : uc($mode);
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /::/ ? : 0 0 0
1096 error $mode.': No such context defined'
1097 0 unless grep {lc($mode) eq $_} qw/perl cmd sh/
1098 0 ▶ or $$self{parser}{$m}{handler} ; # allow for autoloading
Branch: 1 branch
BranchTrueFalseTotal
unless grep {lc $mode eq $_;} "perl", "cmd", "sh" or $self->{"parser"}{$m}{"handler"} 0 0 0
Condition0%0/3
grep {lc $mode eq $_;} "perl", "cmd", "sh" or $self->{"parser"}{$m}{"handler"}
l!l&&r!l&&!r
000
MC/DC0%0/2
grep {lc $mode eq $_;} "perl", "cmd", "sh" or $self->{"parser"}{$m}{"handler"}
grep {lc $mode eq $_;} "perl", "cmd", "sh" $self->{"parser"}{$m}{"handler"}
Truth table0%0/3
A or B
A= grep {lc $mode eq $_;} "perl", "cmd", "sh"B= $self->{"parser"}{$m}{"handler"}
ABresult
1X1
011
000
1099 0 $$self{settings}{mode} = $mode;
1100 }
1101 }
1102  
1103 =item plug
1104  
1105 TODO
1106  
1107 =cut
1108  
1109 128 ▶sub plug {
Subroutine plug: called CC=7 SCAR=33.0
Pod documented
1110 128 my $self = shift;
1111 128 my ($opts, $args) = getopt 'list,l verbose,v @', @_;
1112 128 ▶ if ($$opts{list}) { # list info
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'list'}) { } 0 128 128
1113 0 my @items = keys %{$$self{objects}};
1114 0 ▶ if (@$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args) 0 0 0
1115 0 my $re = join '|', @$args;
1116 0 @items = grep m/$re/i, @items;
1117 }
1118 0 ▶ if ($$opts{verbose}) { # FIXME nicer PLuginHash interface for this
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"verbose"}) 0 0 0
1119 0 my ($raw, $meta) = @{ tied( %{$$self{objects}} ) };
1120 @items = map {
1121 0 $_ .' '. $$meta{$_}{module}
1122 0 ▶ . (exists($$raw{$_}) ? ' (loaded)' : '')
Branch: 1 branch
BranchTrueFalseTotal
exists $raw->{$_} ? : 0 0 0
1123 } @items;
1124 }
1125 0 output \@items;
1126 }
1127 else { # load plugin
1128 128 ▶ error 'usage: plug name [args]' unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 128 128
1129 error $$args[0].': no such plugin'
1130 128 ▶ unless exists $$self{objects}{ $$args[0] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"objects"}{$args->[0]} 128 0 128
1131 0 tied( %{$$self{objects}} )->load(@$args);
1132 }
1133 }
1134  
1135 =item unplug
1136  
1137 TODO
1138  
1139 =cut
1140  
1141 0 ▶sub unplug {
Subroutine unplug: not called CC=3 SCAR=24.8
Pod documented
1142 0 my $self = shift;
1143 0 my ($opt, $args) = getopt 'all,a @', @_;
1144 0 ▶ if ($$opt{all}) { tied( %{$$self{objects}} )->CLEAR() }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'all'}) { } 0 0 0
1145 else {
1146 0 ▶ error "usage: unplug name" unless @$args == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args == 1 0 0 0
1147 0 delete $$self{objects}{$$args[0]};
1148 }
1149 }
1150  
1151 0 ▶sub dev_null {} # does absolutely nothing
Subroutine dev_null: not called CC=1 SCAR=0.0
Pod undocumented
1152  
1153 0 ▶sub stdin { # stub STDIN input
Subroutine stdin: not called CC=3 SCAR=24.8
Pod undocumented
1154 0 my (undef, $prompt, $preput) = @_;
1155 0 local $/ = "\n";
1156 0 ▶ print $prompt if length $prompt;
Branch: 1 branch
BranchTrueFalseTotal
if length $prompt 0 0 0
1157 0 ▶ my $string = length($preput) ? $preput . <STDIN> : <STDIN> ;
Branch: 1 branch
BranchTrueFalseTotal
length $preput ? : 0 0 0
1158 0 output $string;
1159 };
1160  
1161 0 ▶sub list_clothes {
Subroutine list_clothes: not called CC=1 SCAR=6.9
Pod undocumented
1162 0 my $self = shift;
1163 0 my @return = map {'{'.$_.'}'} sort @{$self->{_settings}{clothes}{keys}};
1164 0 push @return, sort @{$self->{_settings}{clothes}{subs}};
1165 0 return [@return];
1166 }
1167  
1168 # ########### #
1169 # Event logic #
1170 # ########### #
1171  
1172 1526052 ▶sub broadcast { # eval to be sure we return
Subroutine broadcast: called CC=4 SCAR=13.9
Pod undocumented
1173 1526052 my ($self, $event) = (shift(), shift());
1174 1526052 ▶ return unless exists $self->{events}{$event};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"events"}{$event} 1523822 2230 1526052
1175 2230 debug "Broadcasting event: $event";
1176 2230 for my $sub ($$self{events}->stack($event)) {
1177 2246 eval { $sub->($event, @_) };
1178 2246 ▶ complain("$sub died on event $event ($@)") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2246 2246
1179 }
1180 }
1181  
1182 0 ▶sub call { bug 'deprecated routine used' }
Subroutine call: not called CC=1 SCAR=6.9
Pod undocumented
1183  
1184 # ########### #
1185 # auto loader #
1186 # ########### #
1187  
1188 64 our $ERROR_CALLER;
1189  
1190 9 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=12.0
1191 9 my $self = shift;
1192 9 my $call = (split/::/,$AUTOLOAD)[-1];
1193  
1194 9 local $ERROR_CALLER = 1;
1195 9 ▶ error "Undefined subroutine &Zoidberg::$call called" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 9 9
1196 9 debug "Zoidberg::AUTOLOAD got $call";
1197  
1198 9 ▶ if (exists $self->{objects}{$call}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'objects'}{$call}) { } 0 9 9
1199 64 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1200 0 ▶ *{ref($self).'::'.$call} = sub { return $self->{objects}{$call} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1201 0 goto \&{$call};
1202 }
1203 else { # Shell like behaviour
1204 9 debug "No such method or object: '$call', trying to shell() it";
1205 9 @_ = ([$call, @_]); # force words parsing
1206 9 goto \&Zoidberg::Shell::shell;
1207 }
1208 }
1209  
1210 # ############# #
1211 # Exit routines #
1212 # ############# #
1213  
1214 =item C<exit()>
1215  
1216 Called by plugins to exit zoidberg -- this ends a interactive C<main_loop()>
1217 loop. This does not clean up or destroy any objects, C<main_loop()> can be
1218 called again to restart it.
1219  
1220 =cut
1221  
1222 0 ▶sub exit {
Subroutine exit: not called CC=3 SCAR=24.8
Pod documented
1223 0 my $self = shift;
1224 0 ▶ if (@{$$self{jobs}} and ! $$self{_warned_bout_jobs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'jobs'};} and not $self->{'_warned_bout_jobs'}) { } 0 0 0
Condition0%0/3
@{$self->{"jobs"};} and not $self->{"_warned_bout_jobs"}
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->{"jobs"};} and not $self->{"_warned_bout_jobs"}
@{$self->{"jobs"};} not $self->{"_warned_bout_jobs"}
Truth table0%0/3
A and not(B)
A= @{$self->{"jobs"};}B= not $self->{"_warned_bout_jobs"}
ABresult
0X0
100
111
1225 0 complain "There are unfinished jobs";
1226 0 $$self{_warned_bout_jobs}++;
1227 }
1228 else {
1229 0 message join ' ', @_;
1230 0 $self->{_continue} = 0;
1231 }
1232 # FIXME this should force ReadLine to quit
1233 }
1234  
1235 =item C<round_up()>
1236  
1237 This method should be called to clean up the shell objects.
1238 A C<round_up()> method will be called recursively for all secondairy objects.
1239  
1240 =cut
1241  
1242 3 ▶sub round_up {
Subroutine round_up: called CC=2 SCAR=7.0
Pod documented
1243 3 my $self = shift;
1244 3 $self->broadcast('exit');
1245 3 ▶ if ($self->{round_up}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"round_up"}) 3 0 3
1246 3 tied( %{$$self{objects}} )->round_up(); # round up loaded plugins
1247 3 Zoidberg::Contractor::round_up($self);
1248 3 undef $self->{round_up};
1249 }
1250 }
1251  
1252 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=8.4
1253 1 my $self = shift;
1254 1 ▶ if ($$self{round_up}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"round_up"}) 0 1 1
1255 0 warn "Zoidberg was not properly cleaned up.\n";
1256 0 $self->round_up;
1257 }
1258 1 delete $OBJECTS{"$self"};
1259 }
1260  
1261 package Zoidberg::SettingsHash;
1262  
1263 64 our $VERSION = '0.981';
1264  
1265 61 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
1266 61 my ($class, $ref, $shell) = @_;
1267 61 bless [$ref, $shell], $class;
1268 }
1269  
1270 153 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
1271 153 my ($self, $key, $val) = @_;
1272 153 my $old = $$self[0]{$key};
1273 153 $$self[0]{$key} = $val;
1274 153 $$self[1]->broadcast('set_'.$key, $val, $old); # new, old
1275 153 1;
1276 }
1277  
1278 #sub set_default {
1279 # my ($self, $key, @list) = @_;
1280 # $$self[0]{_SettingsHash_def}{$key} = \@list;
1281 #}
1282  
1283 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
1284 0 my ($self, $key) = @_;
1285 0 my $val = delete $$self[0]{$key};
1286 0 $$self[1]->broadcast('set_'.$key, undef, $val); # new, old
1287 0 return $val;
1288 }
1289  
1290 0 ▶sub CLEAR { $_[0]->DELETE($_) for keys %{$_[0][0]} }
Subroutine CLEAR: not called CC=2 SCAR=17.9
1291  
1292 63224 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
1293 63224 return $_[0][0]{$_[1]}
1294 # unless !defined $_[0][0]{$_[1]}
1295 # and exists $_[0][0]{_SettingsHash_def}{$_[1]};
1296 # check for default (environment) values
1297 # for my $def (@{$_[0][0]{_SettingsHash_def}{$_[1]}}) {
1298 # $def = $ENV{$1} if $def =~ /^\$(.*)/;
1299 # return $def if defined $def;
1300 # }
1301 }
1302  
1303 0 ▶sub EXISTS { exists $_[0][0]{$_[1]} }
Subroutine EXISTS: not called CC=1 SCAR=6.9
1304  
1305 0 ▶sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
1306  
1307 0 ▶sub NEXTKEY { each %{$_[0][0]} }
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
1308  
1309 package Zoidberg::Eval;
1310  
1311 64 our $VERSION = '0.981';
1312  
1313 # included to bootstrap a bit of default environment
1314 # for the perl syntax
1315  
1316 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1317 64 ▶use vars qw/$AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1318  
1319 64 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
1320 64 ▶use Zoidberg::Shell qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1321 64 ▶use Zoidberg::Utils qw/:error :output :fs regex_glob/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1322 64 require Env;
1323  
1324 64 $| = 1;
1325 64 $Data::Dumper::Sortkeys = 1;
1326  
1327 0 ▶sub pp { # pretty print
Subroutine pp: not called CC=3 SCAR=24.8
1328 0 ▶ local $Data::Dumper::Maxdepth = shift if $_[0] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^\d+$/ 0 0 0
1329 0 ▶ if (wantarray) { return Dumper @_ }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1330 0 else { print Dumper @_ }
1331 }
1332  
1333 {
1334 64 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1335 0 ▶ sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=4 SCAR=30.0
1336 ## Code inspired by Shell.pm ##
1337 0 my $cmd = (split/::/, $AUTOLOAD)[-1];
1338 0 ▶ return undef if $cmd eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "DESTROY" 0 0 0
1339 0 ▶ shift if ref($_[0]) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 0 0 0
1340 0 debug "Zoidberg::Eval::AUTOLOAD got $cmd";
1341 0 @_ = ([$cmd, @_]); # force words
1342 0 unshift @{$_[0]}, '!'
1343 0 ▶ if lc( $Zoidberg::CURRENT->{settings}{mode} ) eq 'perl';
Branch: 1 branch
BranchTrueFalseTotal
if lc $Zoidberg::CURRENT->{"settings"}{"mode"} eq "perl" 0 0 0
1344 0 goto \&shell;
1345 }
1346 }
1347  
1348 64 1;
1349  
1350 __END__
« blib/lib/Zoidberg/Fish/Commands.pm ↑ Summary blib/lib/Zoidberg/Utils/Output.pm »