blib/lib/Shell/Guess.pm

filter: Statementstmt 75.0% 96 / 128 Branchbran 55.5% 70 / 126 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 97.0% 33 / 34 Podpod 100% 29 / 29 totaltotal 68.0% 234 / 344 CC 69 SCAR 31.2
CC
69
Cov
63%
running_shell
82.3
login_shell
35.8
_unixy_shells
20.3
CRAP 22.7
↑ Summary
1 package Shell::Guess;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # TODO: see where we can use P9Y::ProcessTable
8  
9 # ABSTRACT: Make an educated guess about the shell in use
10 16 our $VERSION = '0.10'; # VERSION
11  
12  
13 sub _win32_getppid
14 0 ▶{
Subroutine _win32_getppid: not called CC=1 SCAR=6.9
15 0 require Win32::Getppid;
16 0 Win32::Getppid::getppid();
17 }
18  
19 sub running_shell
20 1 ▶{
Subroutine running_shell: called CC=18 SCAR=44.1
Pod documented
21 1 ▶ if($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
22 {
23 0 my $shell_name = eval {
24 0 require Win32::Process::List;
25 0 my $parent_pid = _win32_getppid();
26 0 Win32::Process::List->new->{processes}->[0]->{$parent_pid}
27 };
28 0 ▶ if(defined $shell_name)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $shell_name) 0 0 0
29 {
30 0 ▶ if($shell_name =~ /cmd\.exe$/)
Branch: 3 branches
BranchTrueFalseTotal
if ($shell_name =~ /cmd\.exe$/) { } 0 0 0
elsif ($shell_name =~ /powershell\.exe$/) { } 0 0 0
elsif ($shell_name =~ /command\.com$/) { } 0 0 0
31 0 { return __PACKAGE__->cmd_shell }
32 elsif($shell_name =~ /powershell\.exe$/)
33 0 { return __PACKAGE__->power_shell }
34 elsif($shell_name =~ /command\.com$/)
35 0 { return __PACKAGE__->command_shell }
36 }
37 }
38  
39 1 ▶ if($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
40 {
41 0 ▶ if($ENV{ComSpec} =~ /cmd\.exe$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'ComSpec'} =~ /cmd\.exe$/) { } 0 0 0
42 0 { return __PACKAGE__->cmd_shell }
43 else
44 0 { return __PACKAGE__->command_shell }
45 }
46  
47 1 ▶ return __PACKAGE__->dcl_shell if $^O eq 'VMS';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "VMS" 0 1 1
48 1 ▶ return __PACKAGE__->command_shell if $^O eq 'dos';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "dos" 0 1 1
49  
50 my $shell = eval {
51 1 ▶ open(my $fh, '<', File::Spec->catfile('', 'proc', getppid, 'comm')) || die;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "comm") or die 1 0 1
52 1 my $command_line = <$fh>;
53 1 ▶ die unless defined $command_line; # don't spew warnings if read failed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command_line 0 1 1
54 1 close $fh;
55 1 $command_line =~ s/\0.*$//;
56 1 _unixy_shells($command_line);
57 }
58  
59 || eval {
60 1 ▶ open(my $fh, '<', File::Spec->catfile('', 'proc', getppid, 'cmdline')) || die;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "cmdline") or die 1 0 1
61 1 my $command_line = <$fh>;
62 1 ▶ die unless defined $command_line; # don't spew warnings if read failed
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command_line 0 1 1
63 1 close $fh;
64 1 $command_line =~ s/\0.*$//;
65 1 _unixy_shells($command_line);
66 }
67  
68 1 ▶ || eval {
Condition33%2/6
eval { do { open my $fh, '<', 'File::Spec'->catfile('', 'proc', getppid, 'comm') or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } || eval { do { open my $fh, '<', 'File::Spec'->catfile('', 'proc', getppid, 'cmdline') or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } || eval { do { require Unix::Process; my $method = $^O eq 'solaris' ? 'comm' : 'command'; my($command) = map({s/\s+.*$//; $_;} 'Unix::Process'->$method(getppid)); _unixy_shells($command) } }
l!l&&r!l&&!r
001
eval { do { open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "comm") or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } or eval { do { open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "cmdline") or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } }
l!l&&r!l&&!r
001
MC/DC0%0/3
eval { do { open my $fh, '<', 'File::Spec'->catfile('', 'proc', getppid, 'comm') or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } || eval { do { open my $fh, '<', 'File::Spec'->catfile('', 'proc', getppid, 'cmdline') or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } || eval { do { require Unix::Process; my $method = $^O eq 'solaris' ? 'comm' : 'command'; my($command) = map({s/\s+.*$//; $_;} 'Unix::Process'->$method(getppid)); _unixy_shells($command) } }
eval { do { open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "comm") or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } eval { do { open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "cmdline") or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } } eval { do { require Unix::Process; my $method = $^O eq 'solaris' ? 'comm' : 'command'; my($command) = map({s/\s+.*$//; $_;} 'Unix::Process'->$method(getppid)); _unixy_shells($command) } }
Truth table25%1/4
A or B || C
A= eval { do { open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "comm") or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } }B= eval { do { open my $fh, "<", "File::Spec"->catfile("", "proc", getppid, "cmdline") or die; my $command_line = readline $fh; die unless defined $command_line; close $fh; $command_line =~ s/\0.*$//; _unixy_shells($command_line) } }C= eval { do { require Unix::Process; my $method = $^O eq 'solaris' ? 'comm' : 'command'; my($command) = map({s/\s+.*$//; $_;} 'Unix::Process'->$method(getppid)); _unixy_shells($command) } }
ABCresult
1XX1
01X1
0011
0000
69 1 require Unix::Process;
70 0 ▶ my $method = $^O eq 'solaris' ? 'comm' : 'command';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'solaris' ? : 0 0 0
71 0 my($command) = map { s/\s+.*$//; $_ } Unix::Process->$method(getppid);
72 0 _unixy_shells($command);
73 };
74  
75 1 ▶ $shell || __PACKAGE__->login_shell;
Branch: 1 branch
BranchTrueFalseTotal
$shell or __PACKAGE__->login_shell 0 1 1
76 }
77  
78  
79 sub login_shell
80 2 ▶{
Subroutine login_shell: called CC=14 SCAR=35.8
Pod documented
81 2 shift; # class ignored
82 2 my $shell;
83  
84 2 ▶ if($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 2 2
85 {
86 0 ▶ if(Win32::IsWin95())
Branch: 1 branch
BranchTrueFalseTotal
if (Win32::IsWin95()) { } 0 0 0
87 0 { return __PACKAGE__->command_shell }
88 else
89 0 { return __PACKAGE__->cmd_shell }
90 }
91  
92 2 ▶ return __PACKAGE__->dcl_shell if $^O eq 'VMS';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "VMS" 0 2 2
93 2 ▶ return __PACKAGE__->command_shell if $^O eq 'dos';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "dos" 0 2 2
94  
95 2 ▶ my $username = shift || $ENV{USER} || $ENV{USERNAME} || $ENV{LOGNAME};
Condition33%3/9
shift() || $ENV{'USER'} || $ENV{'USERNAME'} || $ENV{'LOGNAME'}
l!l&&r!l&&!r
002
shift() or $ENV{"USER"} or $ENV{"USERNAME"}
l!l&&r!l&&!r
002
shift() or $ENV{"USER"}
l!l&&r!l&&!r
002
MC/DC0%0/4
shift() || $ENV{'USER'} || $ENV{'USERNAME'} || $ENV{'LOGNAME'}
shift() $ENV{"USER"} $ENV{"USERNAME"} $ENV{'LOGNAME'}
Truth table20%1/5
A or B or C || D
A= shift()B= $ENV{"USER"}C= $ENV{"USERNAME"}D= $ENV{'LOGNAME'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
96  
97 2 ▶ unless(defined $username)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username) 2 0 2
98 {
99 2 $username = eval { getpwuid $< };
100 }
101  
102 2 ▶ if($^O eq 'darwin')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "darwin") 0 2 2
103 {
104 0 my $command = `dscl . -read /Users/$username UserShell`;
105 0 $shell = _unixy_shells($command);
106 0 ▶ return $shell if defined $shell;
Branch: 1 branch
BranchTrueFalseTotal
if defined $shell 0 0 0
107 }
108  
109 2 eval {
110 2 my $pw_shell = (getpwnam($username))[-1];
111 2 $shell = _unixy_shells($pw_shell);
112 2 ▶ $shell = _unixy_shells(readlink $pw_shell) if !defined($shell) && -l $pw_shell;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $shell and -l $pw_shell 0 2 2
Condition33%1/3
not defined $shell and -l $pw_shell
!ll&&!rl&&r
200
MC/DC0%0/2
not defined $shell and -l $pw_shell
not defined $shell -l $pw_shell
Truth table33%1/3
not(A) and B
A= not defined $shellB= -l $pw_shell
ABresult
0X0
100
111
113 };
114  
115 2 ▶ $shell = __PACKAGE__->bourne_shell unless defined $shell;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shell 0 2 2
116  
117 2 return $shell;
118 }
119  
120  
121 1 ▶sub bash_shell { bless { bash => 1, bourne => 1, unix => 1, name => 'bash', default_location => '/bin/bash' }, __PACKAGE__ }
Subroutine bash_shell: called CC=1 SCAR=0.0
Pod documented
122  
123  
124 3 ▶sub bourne_shell { bless { bourne => 1, unix => 1, name => 'bourne', default_location => '/bin/sh' }, __PACKAGE__ }
Subroutine bourne_shell: called CC=1 SCAR=0.0
Pod documented
125  
126  
127 1 ▶sub c_shell { bless { c => 1, unix => 1, name => 'c', default_location => '/bin/csh' }, __PACKAGE__ }
Subroutine c_shell: called CC=1 SCAR=0.0
Pod documented
128  
129  
130 1 ▶sub cmd_shell { bless { cmd => 1, win32 => 1, name => 'cmd', default_location => 'C:\\Windows\\system32\\cmd.exe' }, __PACKAGE__ }
Subroutine cmd_shell: called CC=1 SCAR=0.0
Pod documented
131  
132  
133 1 ▶sub command_shell { bless { command => 1, win32 => 1, name => 'command', default_location => 'C:\\Windows\\system32\\command.com' }, __PACKAGE__ }
Subroutine command_shell: called CC=1 SCAR=0.0
Pod documented
134  
135  
136 1 ▶sub dcl_shell { bless { dcl => 1, vms => 1, name => 'dcl' }, __PACKAGE__ }
Subroutine dcl_shell: called CC=1 SCAR=0.0
Pod documented
137  
138  
139 1 ▶sub fish_shell { bless { fish => 1, unix => 1, name => 'fish' }, __PACKAGE__ }
Subroutine fish_shell: called CC=1 SCAR=0.0
Pod documented
140  
141  
142 1 ▶sub korn_shell { bless { korn => 1, bourne => 1, unix => 1, name => 'korn', default_location => '/bin/ksh' }, __PACKAGE__ }
Subroutine korn_shell: called CC=1 SCAR=0.0
Pod documented
143  
144  
145 1 ▶sub power_shell { bless { power => 1, win32 => 1, name => 'power' }, __PACKAGE__ }
Subroutine power_shell: called CC=1 SCAR=0.0
Pod documented
146  
147  
148 1 ▶sub tc_shell { bless { c => 1, tc => 1, unix => 1, name => 'tc', default_location => '/bin/tcsh' }, __PACKAGE__ }
Subroutine tc_shell: called CC=1 SCAR=0.0
Pod documented
149  
150  
151 1 ▶sub z_shell { bless { z => 1, bourne => 1, unix => 1, name => 'z', default_location => '/bin/zsh' }, __PACKAGE__ }
Subroutine z_shell: called CC=1 SCAR=0.0
Pod documented
152  
153  
154 16 foreach my $type (qw( cmd command dcl bash fish korn c win32 unix vms bourne tc power z ))
155 {
156 11 ▶ eval qq{
Subroutine is_bash: called CC=3 SCAR=11.1
Subroutine is_bourne: called CC=3 SCAR=11.1
Subroutine is_c: called CC=3 SCAR=11.1
Subroutine is_cmd: called CC=3 SCAR=11.1
Subroutine is_command: called CC=3 SCAR=11.1
Subroutine is_dcl: called CC=3 SCAR=11.1
Subroutine is_fish: called CC=3 SCAR=11.1
Subroutine is_korn: called CC=3 SCAR=11.1
Subroutine is_power: called CC=3 SCAR=11.1
Subroutine is_tc: called CC=3 SCAR=11.1
Subroutine is_unix: called CC=3 SCAR=11.1
Subroutine is_vms: called CC=3 SCAR=11.1
Subroutine is_win32: called CC=3 SCAR=11.1
Subroutine is_z: called CC=3 SCAR=11.1
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 28 branches
BranchTrueFalseTotal
ref $_[0] ? : 11 0 11
$self->{"bash"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"bourne"} or 0 4 7 11
ref $_[0] ? : 11 0 11
$self->{"c"} or 0 2 9 11
ref $_[0] ? : 11 0 11
$self->{"cmd"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"command"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"dcl"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"fish"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"korn"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"power"} or 0 1 10 11
ref $_[0] ? : 11 0 11
$self->{"tc"} or 0 1 10 11
ref $_[0] ? : 13 0 13
$self->{"unix"} or 0 9 4 13
ref $_[0] ? : 11 0 11
$self->{"vms"} or 0 1 10 11
ref $_[0] ? : 13 0 13
$self->{"win32"} or 0 3 10 13
ref $_[0] ? : 11 0 11
$self->{"z"} or 0 1 10 11
157 sub is_$type
158 {
159 my \$self = ref \$_[0] ? shift : __PACKAGE__->running_shell;
160 \$self->{$type} || 0;
161 }
162 };
163 224 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 224 224
164 }
165  
166  
167 sub name
168 4 ▶{
Subroutine name: called CC=2 SCAR=7.2
Pod documented
169 4 ▶ my $self = ref $_[0] ? shift : __PACKAGE__->running_shell;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 0 4
170 4 $self->{name};
171 }
172  
173  
174 sub default_location
175 9 ▶{
Subroutine default_location: called CC=2 SCAR=7.2
Pod documented
176 9 ▶ my $self = ref $_[0] ? shift : __PACKAGE__->running_shell;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 9 0 9
177 9 $self->{default_location};
178 }
179  
180 sub _unixy_shells
181 4 ▶{
Subroutine _unixy_shells: called CC=9 SCAR=30.1
182 4 my $shell = shift;
183 4 ▶ if($shell =~ /tcsh$/)
Branch: 8 branches
BranchTrueFalseTotal
if ($shell =~ /tcsh$/) { } 0 4 4
elsif ($shell =~ /csh$/) { } 0 4 4
elsif ($shell =~ /ksh$/) { } 0 4 4
elsif ($shell =~ /bash$/) { } 0 4 4
elsif ($shell =~ /zsh$/) { } 0 4 4
elsif ($shell =~ /fish$/) { } 0 4 4
elsif ($shell =~ /pwsh$/) { } 0 4 4
elsif ($shell =~ /sh$/) { } 2 2 4
184 0 { return __PACKAGE__->tc_shell }
185 elsif($shell =~ /csh$/)
186 0 { return __PACKAGE__->c_shell }
187 elsif($shell =~ /ksh$/)
188 0 { return __PACKAGE__->korn_shell }
189 elsif($shell =~ /bash$/)
190 0 { return __PACKAGE__->bash_shell }
191 elsif($shell =~ /zsh$/)
192 0 { return __PACKAGE__->z_shell }
193 elsif($shell =~ /fish$/)
194 0 { return __PACKAGE__->fish_shell }
195 elsif($shell =~ /pwsh$/)
196 0 { return __PACKAGE__->power_shell }
197 elsif($shell =~ /sh$/)
198 2 { return __PACKAGE__->bourne_shell }
199 else
200 2 { return; }
201 }
202  
203 16 1;
204  
205 __END__
↑ Summary