Psh/OS.pm

filter: Statementstmt 11.1% 17 / 152 Branchbran 2.8% 2 / 70 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 15.3% 4 / 26 Podpod 0.0% 0 / 19 totaltotal 7.4% 23 / 311 CC 47 SCAR 40.8
CC
47
Cov
8%
fb_glob
186.2
fb_check_terminal_size
110.0
_recursive_glob
30.0
CRAP 59.3
« Psh/Builtins/History.pm ↑ Summary Psh/Strategy/Perlfunc.pm »
1 package Psh::OS;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 my $ospackage;
6  
7 1 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=18.7
8 1 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 1 1
9 0 $ospackage='Psh::OS::Win';
10 0 require Psh::OS::Win;
11 0 ▶ die "Could not find OS specific package $ospackage: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
12 } else {
13 1 $ospackage='Psh::OS::Unix';
14 1 require Psh::OS::Unix;
15 1 ▶ die "Could not find OS specific package $ospackage: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
16 }
17 }
18  
19 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=21.1
20 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 0 $AUTOLOAD=~ s/.*:://;
22 0 my $name="${ospackage}::$AUTOLOAD";
23 0 ▶ $name="Psh::OS::fb_$AUTOLOAD" unless ref *{$name}{CODE} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref *{$name;}{"CODE"} eq "CODE" 0 0 0
24 0 ▶ unless (ref *{$name}{CODE} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref *{$name;}{"CODE"} eq "CODE") 0 0 0
25 0 require Carp;
26 0 eval {
27 0 Carp::croak("Function `$AUTOLOAD' in Psh::OS does not exist.");
28 };
29 }
30 0 *$AUTOLOAD= *$name;
31 0 goto &$AUTOLOAD;
32 }
33  
34 #
35 # The following code is here because it is most probably
36 # portable across at least a large number of platforms
37 # If you need to override them, then modify the symbol
38 # table :-)
39  
40 # recursive glob function used for **/anything glob
41 0 ▶sub _recursive_glob {
Subroutine _recursive_glob: not called CC=5 SCAR=34.0
42 0 my( $pattern, $dir)= @_;
43 0 ▶ opendir( DIR, $dir) || return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or return () 0 0 0
44 0 my @files= readdir(DIR);
45 0 closedir( DIR);
46 0 my @result= map { catdir($dir,$_) }
47 0 grep { /^$pattern$/ } @files;
48 0 foreach my $tmp (@files) {
49 0 my $tmpdir= catdir($dir,$tmp);
50 0 ▶ next if ! -d $tmpdir || !no_upwards($tmp);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $tmpdir and no_upwards($tmp) 0 0 0
Condition0%0/3
-d $tmpdir and no_upwards($tmp)
!ll&&!rl&&r
000
MC/DC0%0/2
-d $tmpdir and no_upwards($tmp)
-d $tmpdir no_upwards($tmp)
Truth table0%0/3
A and B
A= -d $tmpdirB= no_upwards($tmp)
ABresult
0X0
100
111
51 0 push @result, _recursive_glob($pattern, $tmpdir);
52 }
53 0 return @result;
54 }
55  
56 0 ▶sub _escape {
Subroutine _escape: not called CC=2 SCAR=17.9
57 0 my $text= shift;
58 0 ▶ if ($] >= 5.005) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.005) { } 0 0 0
59 0 $text=~s/(?<!\\)([^a-zA-Z0-9\*\?])/\\$1/g;
60 } else {
61 # TODO: no escaping yet
62 }
63 0 return $text;
64 }
65  
66 #
67 # The Perl builtin glob STILL uses csh, furthermore it is
68 # not possible to supply a base directory... so I guess this
69 # is faster
70 #
71 0 ▶sub fb_glob {
Subroutine fb_glob: not called CC=14 SCAR=52.3
Pod undocumented
72 0 my( $pattern, $dir, $already_absed) = @_;
73  
74 0 ▶ return () unless $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern 0 0 0
75  
76 0 my @result;
77 0 ▶ if( !$dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dir) { } 0 0 0
78 0 $dir=$ENV{PWD};
79 } else {
80 0 ▶ $dir=Psh::Util::abs_path($dir) unless $already_absed;
Branch: 1 branch
BranchTrueFalseTotal
unless $already_absed 0 0 0
81 }
82 0 ▶ return unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
83  
84 # Expand ~
85 0 ▶ my $home= $ENV{HOME}||get_home_dir();
Condition0%0/3
$ENV{'HOME'} || get_home_dir()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || get_home_dir()
$ENV{'HOME'} get_home_dir()
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= get_home_dir()
ABresult
1X1
011
000
86 0 ▶ if ($pattern eq '~') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern eq '~') { } 0 0 0
87 0 $pattern=$home;
88 } else {
89 0 $pattern=~ s|^\~/|$home/|;
90 0 $pattern=~ s|^\~([^/]+)|&get_home_dir($1)|e;
91 }
92  
93 0 ▶ return $pattern if $pattern !~ /[*?]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern =~ /[*?]/ 0 0 0
94
95 # Special recursion handling for **/anything globs
96 0 ▶ if( $pattern=~ m:^([^\*]+/)?\*\*/(.*)$: ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pattern =~ m[^([^\*]+/)?\*\*/(.*)$]) { } 0 0 0
elsif ($pattern =~ m[/]) { } 0 0 0
97 0 my $tlen= length($dir)+1;
98 0 ▶ my $prefix= $1||'';
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
99 0 $pattern= $2;
100 0 $prefix=~ s:/$::;
101 0 $dir= catdir($dir,$prefix);
102 0 $pattern=_escape($pattern);
103 0 $pattern=~s/\*/[^\/]*/g;
104 0 $pattern=~s/\?/./g;
105 0 ▶ $pattern='[^\.]'.$pattern if( substr($pattern,0,2) eq '.*');
Branch: 1 branch
BranchTrueFalseTotal
if substr($pattern, 0, 2) eq ".*" 0 0 0
106 0 @result= map { substr($_,$tlen) } _recursive_glob($pattern,$dir);
107 } elsif( $pattern=~ m:/:) {
108 # Too difficult to simulate, so use slow variant
109 0 my $old=$ENV{PWD};
110 0 CORE::chdir $dir;
111 0 $pattern=_escape($pattern);
112 0 ▶ @result= eval { CORE::glob($pattern); };
Subroutine BEGIN: called CC=1 SCAR=0.6
113 0 CORE::chdir $old;
114 } else {
115 # The fast variant for simple matches
116 0 $pattern=_escape($pattern);
117 0 $pattern=~s/\*/.*/g;
118 0 $pattern=~s/\?/./g;
119 0 ▶ $pattern='[^\.]'.$pattern if( substr($pattern,0,2) eq '.*');
Branch: 1 branch
BranchTrueFalseTotal
if substr($pattern, 0, 2) eq ".*" 0 0 0
120
121 0 ▶ opendir( DIR, $dir) || return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or return () 0 0 0
122 0 @result= grep { /^$pattern$/ } readdir(DIR);
123 0 closedir( DIR);
124 }
125 0 return @result;
126 }
127  
128 #
129 # string signal_name( int )
130 # Looks up the name of a signal
131 #
132  
133 0 ▶sub fb_signal_name {
Subroutine fb_signal_name: not called CC=4 SCAR=30.0
Pod undocumented
134 0 my $signalnum = shift;
135 0 require Config;
136 0 my @numbers= split ',',$Config::Config{sig_num};
137 0 ▶ @numbers= split ' ',$Config::Config{sig_num} if( @numbers==1);
Branch: 1 branch
BranchTrueFalseTotal
if @numbers == 1 0 0 0
138 # Strange incompatibility between perl versions
139  
140 0 my @names= split ' ',$Config::Config{sig_name};
141 0 ▶ for( my $i=0; $i<$#numbers; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#numbers) 0 0 0
142 {
143 0 ▶ return $names[$i] if( $numbers[$i]==$signalnum);
Branch: 1 branch
BranchTrueFalseTotal
if $numbers[$i] == $signalnum 0 0 0
144 }
145 0 return $signalnum;
146 }
147  
148 #
149 # string signal_description( int signal_number | string signal_name )
150 # returns a descriptive name for the POSIX signals
151 #
152  
153 0 ▶sub fb_signal_description {
Subroutine fb_signal_description: not called CC=3 SCAR=24.8
Pod undocumented
154 0 my $signal_name= signal_name(shift);
155 0 my $desc= Psh::Locale::get_text('sig_description')->{$signal_name};
156 0 ▶ if( defined($desc) and $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc and $desc) 0 0 0
Condition0%0/3
defined $desc and $desc
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desc and $desc
defined $desc $desc
Truth table0%0/3
A and B
A= defined $descB= $desc
ABresult
0X0
100
111
157 0 return "SIG$signal_name - $desc";
158 }
159 0 return "signal $signal_name";
160 }
161  
162 # Return a name for a temp file
163  
164 0 ▶sub fb_tmpnam {
Subroutine fb_tmpnam: not called CC=1 SCAR=6.9
Pod undocumented
165 0 return POSIX::tmpnam();
166 }
167  
168 0 ▶sub fb_get_window_size {}
Subroutine fb_get_window_size: not called CC=1 SCAR=0.0
Pod undocumented
169 0 ▶sub fb_remove_signal_handlers {1}
Subroutine fb_remove_signal_handlers: not called CC=1 SCAR=6.9
Pod undocumented
170 0 ▶sub fb_setup_signal_handlers {1}
Subroutine fb_setup_signal_handlers: not called CC=1 SCAR=6.9
Pod undocumented
171 0 ▶sub fb_setup_sigsegv_handler {1}
Subroutine fb_setup_sigsegv_handler: not called CC=1 SCAR=6.9
Pod undocumented
172 0 ▶sub fb_setup_readline_handler {1}
Subroutine fb_setup_readline_handler: not called CC=1 SCAR=6.9
Pod undocumented
173 0 ▶sub fb_reap_children {1}
Subroutine fb_reap_children: not called CC=1 SCAR=6.9
Pod undocumented
174 0 ▶sub fb_abs_path { undef }
Subroutine fb_abs_path: not called CC=1 SCAR=6.9
Pod undocumented
175  
176 #
177 # Exit psh - you won't believe it, but exit needs special treatment on
178 # MacOS
179 #
180 0 ▶sub fb_exit_psh {
Subroutine fb_exit_psh: not called CC=3 SCAR=24.8
Pod undocumented
181 0 Psh::Util::print_debug_class('i',"[Psh::OS::exit_psh() called]\n");
182 0 Psh::save_history();
183 0 ▶ $ENV{SHELL} = $Psh::old_shell if $Psh::old_shell;
Branch: 1 branch
BranchTrueFalseTotal
if $Psh::old_shell 0 0 0
184 0 ▶ CORE::exit($_[0]) if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
185 0 CORE::exit(0);
186 }
187  
188 0 ▶sub fb_getcwd_psh {
Subroutine fb_getcwd_psh: not called CC=2 SCAR=17.9
Pod undocumented
189 0 eval { require Cwd; };
190 0 ▶ return eval { Cwd::getcwd(); } || '';
Condition0%0/2
eval { do { Cwd::getcwd() } } || ''
l!l
00
MC/DC0%0/1
eval { do { Cwd::getcwd() } } || ''
eval { do { Cwd::getcwd() } }
Truth table0%0/2
A
A= eval { do { Cwd::getcwd() } }
Aresult
11
00
191 }
192  
193 sub fb_LOCK_SH() { 1; }
194 sub fb_LOCK_EX() { 2; }
195 sub fb_LOCK_NB() { 4; }
196 sub fb_LOCK_UN() { 8; }
197  
198 0 ▶sub fb_lock {
Subroutine fb_lock: not called CC=4 SCAR=30.0
Pod undocumented
199 0 my $file= shift;
200 0 ▶ my $type= shift || Psh::OS::LOCK_SH();
Condition0%0/3
shift() || LOCK_SH()
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || LOCK_SH()
shift() LOCK_SH()
Truth table0%0/3
A || B
A= shift()B= LOCK_SH()
ABresult
1X1
011
000
201 0 my $count=3;
202 0 my $status=0;
203 0 ▶ while ($count-- and !$status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count-- and not $status) 0 0 0
Condition0%0/3
$count-- and not $status
!ll&&!rl&&r
000
MC/DC0%0/2
$count-- and not $status
$count-- not $status
Truth table0%0/3
A and not(B)
A= $count--B= not $status
ABresult
0X0
100
111
204 0 $status= flock($file, $type| Psh::OS::LOCK_NB());
205 }
206 0 return $status;
207 }
208  
209 0 ▶sub fb_unlock {
Subroutine fb_unlock: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my $file= shift;
211 0 flock($file, Psh::OS::LOCK_UN()| Psh::OS::LOCK_NB());
212 }
213  
214 0 ▶sub fb_reinstall_resize_handler { 1; }
Subroutine fb_reinstall_resize_handler: not called CC=1 SCAR=6.9
Pod undocumented
215  
216 {
217 1 my $handler_type=0;
218  
219 0 ▶ sub fb_install_resize_handler {
Subroutine fb_install_resize_handler: not called CC=4 SCAR=30.0
Pod undocumented
220 0 eval '$Psh::term->get_screen_size()';
221 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
222 0 $handler_type=3;
223 0 return;
224 }
225 0 eval 'use Term::Size;';
226 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
227 0 eval 'use Term::ReadKey;';
228 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
229 0 $handler_type=2;
230 }
231 } else {
232 0 $handler_type=1;
233 }
234 }
235  
236  
237 0 ▶ sub fb_check_terminal_size {
Subroutine fb_check_terminal_size: not called CC=10 SCAR=47.0
Pod undocumented
238 0 my ($cols,$rows);
239  
240 0 ▶ if ($handler_type==0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($handler_type == 0) { } 0 0 0
elsif ($handler_type == 3) { } 0 0 0
elsif ($handler_type == 1) { } 0 0 0
elsif ($handler_type == 2) { } 0 0 0
241 0 return;
242 } elsif ($handler_type==3) {
243 0 eval {
244 0 ($rows,$cols)= $Psh::term->get_screen_size();
245 };
246 } elsif ($handler_type==1) {
247 0 eval {
248 0 ($cols,$rows)= Term::Size::chars();
249 };
250 } elsif ($handler_type==2) {
251 0 eval {
252 0 ($cols,$rows)= Term::ReadKey::GetTerminalSize(*STDOUT);
253 };
254 }
255  
256 0 ▶ if($cols && $rows && ($cols > 0) && ($rows > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols and $rows and $cols > 0 and $rows > 0) 0 0 0
Condition0%0/9
$cols and $rows and $cols > 0 and $rows > 0
!ll&&!rl&&r
000
$cols and $rows and $cols > 0
!ll&&!rl&&r
000
$cols and $rows
!ll&&!rl&&r
000
MC/DC0%0/4
$cols and $rows and $cols > 0 and $rows > 0
$cols $rows $cols > 0 $rows > 0
Truth table0%0/5
A and B and C and D
A= $colsB= $rowsC= $cols > 0D= $rows > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
257 0 $ENV{COLUMNS} = $cols;
258 0 $ENV{LINES} = $rows;
259 0 ▶ if( $Psh::term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Psh::term) 0 0 0
260 0 $Psh::term->Attribs->{screen_width}=$cols-1;
261 }
262 # for ReadLine::Perl
263 }
264 }
265 }
266  
267  
268 # File::Spec
269 #
270 # We add the necessary functions directly because:
271 # 1) Changes to File::Spec might be fatal to psh's file location mechanisms
272 # 2) File::Spec loads unwanted modules
273 # 3) We don't need it anyway as we need platform-specific OS modules
274 # anyway
275 #
276 # Normally I wouldn't do it - but this is a shell and memory
277 # consumption and startup time is worth something for everyday work...
278  
279 1 ▶sub fb_no_upwards {
Subroutine fb_no_upwards: not called CC=1 SCAR=1.2
Pod undocumented
280 0 return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
281 }
282  
283  
284 1 1;
285  
286 __END__
« Psh/Builtins/History.pm ↑ Summary Psh/Strategy/Perlfunc.pm »