lib/Devel/Trepan/CmdProcessor/Load.pm

filter: Statementstmt 48.9% 73 / 149 Branchbran 25.8% 15 / 58 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.4% 13 / 17 Podpod 0.0% 0 / 10 totaltotal 41.8% 102 / 244 CC 37 SCAR 22.8
CC
37
Cov
42%
load_debugger_commands
16.7
load_debugger_commands
16.7
load_debugger_command
14.1
CRAP 9.8
« lib/Devel/Trepan/CmdProcessor/Command/Eval.pm ↑ Summary lib/Devel/Trepan/Interface/Client.pm »
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2012, 2014 Rocky Bernstein <rocky@cpan.org>
3  
4 # Part of Devel::Trepan::CmdProcessor that loads up debugger commands from
5 # builtin and user directories.
6 # Sets @commands, @aliases, @macros
7 12 ▶use rlib '../../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Devel::Trepan::CmdProcessor;
10 12 $Load_seen = 1;
11 12 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head2 load_cmds_initialize
19  
20 load_debugger_commands($self) -> undef
21  
22 Loads in our built-in commands.
23  
24 Called from Devel::Trepan::CmdProcessor->new in CmdProcessor.pm
25 =cut
26  
27 sub load_cmds_initialize($)
28 13 ▶{
Subroutine load_cmds_initialize: called CC=3 SCAR=15.0
Pod undocumented
29 0 ▶ my $self = shift;
Subroutine load_cmds_initialize: not called CC=3 SCAR=15.0
Pod undocumented
30 0 $self->{commands} = {};
31 0 $self->{aliases} = {};
32 0 $self->{macros} = {};
33  
34 my @cmd_dirs = (
35 File::Spec->catfile(dirname(__FILE__), 'Command'),
36 0 @{$self->{settings}{cmddir}}
37 );
38 0 for my $cmd_dir (@cmd_dirs) {
39 0 ▶ $self->load_debugger_commands($cmd_dir) if -d $cmd_dir;
Branch: 2 branches
BranchTrueFalseTotal
if -d $cmd_dir 13 0 13
if -d $cmd_dir 0 0 0
40 }
41 }
42  
43 =head2 load_debugger_commands
44  
45 load_debugger_commands($self, $file_or_dir) -> @errors
46  
47 Loads in debugger commands by require'ing each Perl file in the
48 'command' directory. Then a new instance of each class of the
49 form Trepan::xxCommand is added to @commands and that array
50 is returned.
51 =cut
52 sub load_debugger_commands($$)
53 13 ▶{
Subroutine load_debugger_commands: called CC=6 SCAR=28.1
Pod undocumented
54 0 ▶ my ($self, $file_or_dir) = @_;
Subroutine load_debugger_commands: not called CC=6 SCAR=28.1
Pod undocumented
55 0 my @errors = ();
56 0 ▶ if ( -d $file_or_dir ) {
Branch: 4 branches
BranchTrueFalseTotal
if (-d $file_or_dir) { } 13 0 13
elsif (-r $file_or_dir) { } 0 0 0
if (-d $file_or_dir) { } 0 0 0
elsif (-r $file_or_dir) { } 0 0 0
57 0 my $dir = abs_path($file_or_dir);
58 # change $0 so it doesn't get in the way of __FILE__ eq $0
59 # old_dollar0 = $0
60 # $0 = ''
61 0 ▶ for my $pm (glob(File::Spec->catfile($dir, '*.pm'))) {
Subroutine BEGIN: called CC=1 SCAR=0.1
62 0 my $err = $self->load_debugger_command($pm);
63 0 ▶ push @errors, $err if $err;
Branch: 2 branches
BranchTrueFalseTotal
if $err 0 494 494
if $err 0 0 0
64 }
65 # $0 = old_dollar0
66 } elsif (-r $file_or_dir) {
67 0 my $err = $self->load_debugger_command($file_or_dir);
68 0 ▶ push @errors, $err if $err;
Branch: 2 branches
BranchTrueFalseTotal
if $err 0 0 0
if $err 0 0 0
69 }
70 0 return @errors;
71 }
72  
73 =head2 load_debugger_command
74  
75 load_debugger_command($self, $command_file, [$force])
76  
77 Loads a debugger command. Returns a string containing the error or '' if no error.
78 =cut
79  
80 sub load_debugger_command($$;$)
81 494 ▶{
Subroutine load_debugger_command: called CC=5 SCAR=26.5
Pod undocumented
82 0 ▶ my ($self, $command_file, $force) = @_;
Subroutine load_debugger_command: not called CC=5 SCAR=26.5
Pod undocumented
83 0 ▶ return unless -r $command_file;
Branch: 2 branches
BranchTrueFalseTotal
unless -r $command_file 0 494 494
unless -r $command_file 0 0 0
84 0 my $rc = '';
85 0 eval { $rc = do $command_file; };
86 0 ▶ if (!$rc or $rc eq 'Skip me!') {
Branch: 4 branches
BranchTrueFalseTotal
if (not $rc or $rc eq 'Skip me!') { } 0 494 494
elsif ($rc) { } 494 0 494
if (not $rc or $rc eq 'Skip me!') { } 0 0 0
elsif ($rc) { } 0 0 0
Condition16%1/6
not $rc or $rc eq "Skip me!"
l!l&&r!l&&!r
00494
not $rc or $rc eq "Skip me!"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $rc or $rc eq "Skip me!"
not $rc $rc eq "Skip me!"
MC/DC0%0/2
not $rc or $rc eq "Skip me!"
not $rc $rc eq "Skip me!"
Truth table33%1/3
not(A) or B
A= not $rcB= $rc eq "Skip me!"
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not $rcB= $rc eq "Skip me!"
ABresult
1X1
011
000
87 0 return 'skipped';
88 } elsif ($rc) {
89 # Instantiate each Command class found by the above require(s).
90 0 my $name = basename($command_file, '.pm');
91 0 return $self->setup_command($name);
92 } else {
93 0 my $errmsg = "Trouble reading ${command_file}: $@";
94 0 $self->errmsg($errmsg);
95 0 return $errmsg;
96 }
97 }
98  
99 =head2 run_cmd
100  
101 run_cmd($self, $cmd_arry)
102  
103 Looks up cmd_array[0] in @commands and runs that. We do lots of
104 validity testing on cmd_array.
105  
106 =cut
107 sub run_cmd($$)
108 3 ▶{
Subroutine run_cmd: called CC=5 SCAR=22.2
Pod undocumented
109 0 ▶ my ($self, $cmd_array) = @_;
Subroutine run_cmd: not called CC=5 SCAR=22.2
Pod undocumented
110 0 ▶ unless ('ARRAY' eq ref $cmd_array) {
Branch: 2 branches
BranchTrueFalseTotal
unless ("ARRAY" eq ref $cmd_array) 1 2 3
unless ("ARRAY" eq ref $cmd_array) 0 0 0
111 0 ▶ my $ref_msg = ref($cmd_array) ? ", got: " . ref($cmd_array): '';
Branch: 2 branches
BranchTrueFalseTotal
ref $cmd_array ? : 0 1 1
ref $cmd_array ? : 0 0 0
112 0 $self->errmsg("run_cmd argument should be an Array reference$ref_msg");
113 0 return;
114 }
115 # if ($cmd_array.detect{|item| !item.is_a?(String)}) {
116 # $self ->errmsg("run_cmd argument Array should only contain strings. " .
117 # "Got #{cmd_array.inspect}");
118 # return;
119 # }
120 0 ▶ if (0 == scalar @$cmd_array) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == scalar @$cmd_array) 1 1 2
if (0 == scalar @$cmd_array) 0 0 0
121 0 $self->errmsg("run_cmd Array should have at least one item");
122 0 return;
123 }
124 0 my $cmd_name = $cmd_array->[0];
125 0 ▶ if (exists($self->{commands}{$cmd_name})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{"commands"}{$cmd_name}) 1 0 1
if (exists $self->{"commands"}{$cmd_name}) 0 0 0
126 0 $self->{commands}{$cmd_name}->run($cmd_array);
127 }
128 }
129  
130 # sub save_commands(opts)
131 # {
132 # save_filename = opts[:filename] ||
133 # File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname(['trepanning-save', '.txt'], nil))
134 # begin
135 # save_file = File.open(save_filename, 'w')
136 # rescue => exc
137 # errmsg("Can't open #{save_filename} for writing.")
138 # errmsg("System reports: #{exc.inspect}")
139 # return nil
140 # }
141 # save_file.print "#\n# Commands to restore trepanning environment\n#\n"
142 # @commands.each do |cmd_name, cmd_obj|
143 # cmd_obj.save_command if cmd_obj.respond_to?(:save_command)
144 # next unless cmd_obj.is_a?(Trepan::SubcommandMgr)
145 # cmd_obj.subcmds.subcmds.each do |subcmd_name, subcmd_obj|
146 # save_file.print subcmd_obj.save_command if
147 # subcmd_obj.respond_to?(:save_command)
148 # next unless subcmd_obj.is_a?(Trepan::SubSubcommandMgr)
149 # subcmd_obj.subcmds.subcmds.each do |subsubcmd_name, subsubcmd_obj|
150 # save_file.print subsubcmd_obj.save_command if
151 # subsubcmd_obj.respond_to?(:save_command)
152 # }
153 # }
154 # }
155 # save_file.print "!FileUtils.rm #{save_filename.inspect}" if
156 # opts[:erase]
157 # save_file.close
158  
159 # return save_filename
160 # }
161  
162 =head2 setup_command
163  
164 setup_command($self, $name)
165  
166 Instantiate a Devel::Trepan::Command and extract info: the NAME, ALIASES
167 and store the command in @commands.
168 =cut
169 sub setup_command($$)
170 494 ▶{
Subroutine setup_command: called CC=4 SCAR=20.2
Pod undocumented
171 0 ▶ my ($self, $name) = @_;
Pod undocumented
172 0 my $cmd_obj;
173 0 my $cmd_name = lc $name;
174 0 my $new_cmd = "\$cmd_obj=Devel::Trepan::CmdProcessor::Command::${name}" .
175 "->new(\$self, \$cmd_name); 1";
176 0 ▶ if (eval $new_cmd) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval $new_cmd) { } 494 0 494
if (eval $new_cmd) { } 0 0 0
177 # Add to list of commands and aliases.
178 0 ▶ if ($cmd_obj->{aliases}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd_obj->{"aliases"}) 494 0 494
if ($cmd_obj->{"aliases"}) 0 0 0
179 0 for my $a (@{$cmd_obj->{aliases}}) {
180 0 $self->{aliases}{$a} = $cmd_name;
181 }
182 }
183 0 $self->{commands}{$cmd_name} = $cmd_obj;
184 0 return '';
185 } else {
186 0 $self->errmsg("Error instantiating $name");
187 0 $self->errmsg($@);
188 0 return $@;
189 }
190 }
191  
192 12 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 12 12
193 0 require Devel::Trepan::CmdProcessor;
194 0 my $cmdproc = Devel::Trepan::CmdProcessor->new;
195 0 require Array::Columnize;
196 0 my @cmds = sort keys(%{$cmdproc->{commands}});
197 0 print Array::Columnize::columnize(\@cmds);
198 0 my $sep = '=' x 20 . "\n";
199 0 print $sep;
200 0 my @aliases = sort keys(%{$cmdproc->{aliases}});
201 0 print Array::Columnize::columnize(\@aliases);
202 0 print $sep;
203  
204 0 $cmdproc->run_cmd('foo'); # Invalid - not an Array
205 0 $cmdproc->run_cmd([]); # Invalid - empty Array
206 0 $cmdproc->run_cmd(['help', '*']);
207 # $cmdproc->run_cmd(['list', 5]); # Invalid - nonstring arg
208 }
209  
210 12 1;
« lib/Devel/Trepan/CmdProcessor/Command/Eval.pm ↑ Summary lib/Devel/Trepan/Interface/Client.pm »