Cmd.pm

filter: Statementstmt 61.0% 254 / 416 Branchbran 43.7% 84 / 192 Conditioncond 29.6% 19 / 64 MC/DCmcdc 7.6% 3 / 39 Subroutinesub 64.1% 34 / 53 Podpod 87.5% 21 / 24 totaltotal 52.6% 415 / 788 CC 123 SCAR 27.5
CC
123
Cov
53%
rawdatasend
56.0
READ
56.0
_print_isa
33.3
CRAP 15.7
« FTP/I.pm ↑ Summary FTP/dataconn.pm »
1 # Net::Cmd.pm
2 #
3 # Copyright (C) 1995-2006 Graham Barr. All rights reserved.
4 # Copyright (C) 2013-2016, 2020, 2022, 2026 Steve Hay. All rights reserved.
5 # This module is free software; you can redistribute it and/or modify it under
6 # the same terms as Perl itself, i.e. under the terms of either the GNU General
7 # Public License or the Artistic License, as specified in the F<LICENCE> file.
8  
9 package Net::Cmd;
10  
11 18 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use Errno 'EINTR';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 if (ord "A" == 193) {
23 require Convert::EBCDIC;
24  
25 # Convert::EBCDIC->import;
26 }
27 }
28  
29 18 our $VERSION = "3.16";
30 18 our @ISA = qw(Exporter);
31 18 our @EXPORT = qw(CMD_INFO CMD_OK CMD_MORE CMD_REJECT CMD_ERROR CMD_PENDING);
32  
33 18 ▶use constant CMD_INFO => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 18 ▶use constant CMD_OK => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 18 ▶use constant CMD_MORE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 18 ▶use constant CMD_REJECT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 18 ▶use constant CMD_ERROR => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 18 ▶use constant CMD_PENDING => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 18 ▶use constant DEF_REPLY_CODE => 421;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 18 my %debug = ();
43  
44 18 my $tr = ord "A" == 193 ? Convert::EBCDIC->new() : undef;
45  
46 0 ▶sub toebcdic {
Subroutine toebcdic: not called CC=4 SCAR=30.0
Pod undocumented
47 0 my $cmd = shift;
48  
49 0 ▶ unless (exists ${*$cmd}{'net_cmd_asciipeer'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists do { *$cmd }->{"net_cmd_asciipeer"}) 0 0 0
50 0 my $string = $_[0];
51 0 my $ebcdicstr = $tr->toebcdic($string);
52 0 ▶ ${*$cmd}{'net_cmd_asciipeer'} = $string !~ /^\d+/ && $ebcdicstr =~ /^\d+/;
Condition0%0/3
!($string =~ /^\d+/) && $ebcdicstr =~ /^\d+/
!ll&&!rl&&r
000
MC/DC0%0/2
!($string =~ /^\d+/) && $ebcdicstr =~ /^\d+/
!($string =~ /^\d+/) $ebcdicstr =~ /^\d+/
Truth table0%0/3
not(A) && B
A= !($string =~ /^\d+/)B= $ebcdicstr =~ /^\d+/
ABresult
0X0
100
111
53 }
54  
55 0 ▶ ${*$cmd}{'net_cmd_asciipeer'}
Branch: 1 branch
BranchTrueFalseTotal
do { *$cmd }->{'net_cmd_asciipeer'} ? : 0 0 0
56 ? $tr->toebcdic($_[0])
57 : $_[0];
58 }
59  
60  
61 0 ▶sub toascii {
Subroutine toascii: not called CC=2 SCAR=17.9
Pod undocumented
62 0 my $cmd = shift;
63 0 ▶ ${*$cmd}{'net_cmd_asciipeer'}
Branch: 1 branch
BranchTrueFalseTotal
do { *$cmd }->{'net_cmd_asciipeer'} ? : 0 0 0
64 ? $tr->toascii($_[0])
65 : $_[0];
66 }
67  
68  
69 0 ▶sub _print_isa {
Subroutine _print_isa: not called CC=6 SCAR=35.1
70 18 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 0 my $pkg = shift;
73 0 my $cmd = $pkg;
74  
75 0 ▶ $debug{$pkg} ||= 0;
Condition0%0/2
$debug{$pkg} ||= 0
l!l
00
MC/DC0%0/1
$debug{$pkg} ||= 0
$debug{$pkg}
Truth table0%0/2
A
A= $debug{$pkg}
Aresult
11
00
76  
77 0 my %done = ();
78 0 my @do = ($pkg);
79 0 my %spc = ($pkg, "");
80  
81 0 ▶ while ($pkg = shift @do) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = shift @do) 0 0 0
82 0 ▶ next if defined $done{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $done{$pkg} 0 0 0
83  
84 0 $done{$pkg} = 1;
85  
86 my $v =
87 0 defined ${"${pkg}::VERSION"}
88 0 ▶ ? "(" . ${"${pkg}::VERSION"} . ")"
Branch: 1 branch
BranchTrueFalseTotal
defined ${"${pkg}::VERSION";} ? : 0 0 0
89 : "";
90  
91 0 my $spc = $spc{$pkg};
92 0 $cmd->debug_print(1, "${spc}${pkg}${v}\n");
93  
94 0 ▶ if (@{"${pkg}::ISA"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{"${pkg}::ISA";}) 0 0 0
95 0 @spc{@{"${pkg}::ISA"}} = (" " . $spc{$pkg}) x @{"${pkg}::ISA"};
96 0 unshift(@do, @{"${pkg}::ISA"});
97 }
98 }
99 }
100  
101  
102 163 ▶sub debug {
Subroutine debug: called CC=13 SCAR=31.5
Pod documented
103 163 ▶ @_ == 1 or @_ == 2 or croak 'usage: $obj->debug([$level])';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or croak("usage: \$obj->debug([\$level])") 163 0 163
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
15490
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
104  
105 163 my ($cmd, $level) = @_;
106 163 ▶ my $pkg = ref($cmd) || $cmd;
Condition33%1/3
ref $cmd || $cmd
l!l&&r!l&&!r
16300
MC/DC0%0/2
ref $cmd || $cmd
ref $cmd $cmd
Truth table33%1/3
A || B
A= ref $cmdB= $cmd
ABresult
1X1
011
000
107 163 my $oldval = 0;
108  
109 163 ▶ if (ref($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd) { } 163 0 163
110 163 ▶ $oldval = ${*$cmd}{'net_cmd_debug'} || 0;
Condition50%1/2
do { *$cmd }->{'net_cmd_debug'} || 0
l!l
0163
MC/DC0%0/1
do { *$cmd }->{'net_cmd_debug'} || 0
do { *$cmd }->{'net_cmd_debug'}
Truth table50%1/2
A
A= do { *$cmd }->{'net_cmd_debug'}
Aresult
11
00
111 }
112 else {
113 0 ▶ $oldval = $debug{$pkg} || 0;
Condition0%0/2
$debug{$pkg} || 0
l!l
00
MC/DC0%0/1
$debug{$pkg} || 0
$debug{$pkg}
Truth table0%0/2
A
A= $debug{$pkg}
Aresult
11
00
114 }
115  
116 163 ▶ return $oldval
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 154 9 163
117 unless @_ == 2;
118  
119 9 ▶ $level = $debug{$pkg} || 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 9 9
Condition0%0/2
$debug{$pkg} || 0
l!l
00
MC/DC0%0/1
$debug{$pkg} || 0
$debug{$pkg}
Truth table0%0/2
A
A= $debug{$pkg}
Aresult
11
00
120 unless defined $level;
121  
122 _print_isa($pkg)
123 9 ▶ if ($level && !exists $debug{$pkg});
Branch: 1 branch
BranchTrueFalseTotal
if $level and not exists $debug{$pkg} 0 9 9
Condition33%1/3
$level and not exists $debug{$pkg}
!ll&&!rl&&r
900
MC/DC0%0/2
$level and not exists $debug{$pkg}
$level not exists $debug{$pkg}
Truth table33%1/3
A and not(B)
A= $levelB= not exists $debug{$pkg}
ABresult
0X0
100
111
124  
125 9 ▶ if (ref($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd) { } 9 0 9
126 9 ${*$cmd}{'net_cmd_debug'} = $level;
127 }
128 else {
129 0 $debug{$pkg} = $level;
130 }
131  
132 9 $oldval;
133 }
134  
135  
136 22 ▶sub message {
Subroutine message: called CC=3 SCAR=11.0
Pod documented
137 22 ▶ @_ == 1 or croak 'usage: $obj->message()';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$obj->message()") 22 0 22
138  
139 22 my $cmd = shift;
140  
141 wantarray
142 16 ? @{${*$cmd}{'net_cmd_resp'}}
143 22 ▶ : join("", @{${*$cmd}{'net_cmd_resp'}});
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 6 22
144 }
145  
146  
147 0 ▶sub debug_text { $_[2] }
Subroutine debug_text: not called CC=1 SCAR=6.9
Pod documented
148  
149  
150 0 ▶sub debug_print {
Subroutine debug_print: not called CC=2 SCAR=17.9
Pod documented
151 0 my ($cmd, $out, $text) = @_;
152 0 ▶ print STDERR $cmd, ($out ? '>>> ' : '<<< '), $cmd->debug_text($out, $text);
Branch: 1 branch
BranchTrueFalseTotal
$out ? : 0 0 0
153 }
154  
155  
156 11 ▶sub code {
Subroutine code: called CC=3 SCAR=11.0
Pod documented
157 11 ▶ @_ == 1 or croak 'usage: $obj->code()';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$obj->code()") 11 0 11
158  
159 11 my $cmd = shift;
160  
161 1 ${*$cmd}{'net_cmd_code'} = $cmd->DEF_REPLY_CODE
162 11 ▶ unless exists ${*$cmd}{'net_cmd_code'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { *$cmd }->{"net_cmd_code"} 1 10 11
163  
164 11 ${*$cmd}{'net_cmd_code'};
165 }
166  
167  
168 0 ▶sub status {
Subroutine status: not called CC=2 SCAR=17.9
Pod documented
169 0 ▶ @_ == 1 or croak 'usage: $obj->status()';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$obj->status()") 0 0 0
170  
171 0 my $cmd = shift;
172  
173 0 substr(${*$cmd}{'net_cmd_code'}, 0, 1);
174 }
175  
176  
177 31 ▶sub set_status {
Subroutine set_status: called CC=4 SCAR=14.0
Pod undocumented
178 31 ▶ @_ == 3 or croak 'usage: $obj->set_status($code, $resp)';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("usage: \$obj->set_status(\$code, \$resp)") 31 0 31
179  
180 31 my $cmd = shift;
181 31 my ($code, $resp) = @_;
182  
183 31 ▶ $resp = defined $resp ? [$resp] : []
Branch: 2 branches
BranchTrueFalseTotal
unless ref $resp 31 0 31
defined $resp ? : 6 25 31
184 unless ref($resp);
185  
186 31 (${*$cmd}{'net_cmd_code'}, ${*$cmd}{'net_cmd_resp'}) = ($code, $resp);
187  
188 31 1;
189 }
190  
191 56 ▶sub _syswrite_with_timeout {
Subroutine _syswrite_with_timeout: called CC=12 SCAR=34.9
192 56 my $cmd = shift;
193 56 my $line = shift;
194  
195 56 my $len = length($line);
196 56 my $offset = 0;
197 56 my $win = "";
198 56 vec($win, fileno($cmd), 1) = 1;
199 56 ▶ my $timeout = $cmd->timeout || undef;
Condition100%2/2
$cmd->timeout || undef
l!l
1937
MC/DC100%1/1
$cmd->timeout || undef
$cmd->timeout
Truth table100%2/2
A
A= $cmd->timeout
Aresult
11
00
200 56 my $initial = time;
201 56 my $pending = $timeout;
202  
203 56 ▶ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MacOS" 56 0 56
204  
205 56 ▶ while ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 56 56 112
206 56 my $wout;
207 56 my $nfound = select(undef, $wout = $win, undef, $pending);
208 56 ▶ if ((defined $nfound and $nfound > 0) or -f $cmd) # -f for testing on win32
Branch: 2 branches
BranchTrueFalseTotal
if (defined $nfound and $nfound > 0 or -f $cmd) { } 56 0 56
elsif ($nfound == -1) { } 0 0 0
Condition33%2/6
defined $nfound and $nfound > 0 or -f $cmd
l!l&&r!l&&!r
5600
defined $nfound and $nfound > 0
!ll&&!rl&&r
0056
MC/DC0%0/3
defined $nfound and $nfound > 0 or -f $cmd
defined $nfound $nfound > 0 -f $cmd
Truth table20%1/5
A and B or C
A= defined $nfoundB= $nfound > 0C= -f $cmd
ABCresult
11X1
0X11
1011
0X00
1000
209 {
210 56 my $w = syswrite($cmd, $line, $len, $offset);
211 56 ▶ if (! defined($w) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 0 56 56
212 0 my $err = $!;
213 0 $cmd->close;
214 0 $cmd->_set_status_closed($err);
215 0 return;
216 }
217 56 $len -= $w;
218 56 $offset += $w;
219 }
220 elsif ($nfound == -1) {
221 0 ▶ if ( $! == EINTR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4) 0 0 0
222 0 ▶ if ( defined($timeout) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 0 0 0
223 0 ▶ redo if ($pending = $timeout - ( time - $initial ) ) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($pending = $timeout - (time - $initial)) > 0 0 0 0
224 0 $cmd->_set_status_timeout;
225 0 return;
226 }
227 0 redo;
228 }
229 0 my $err = $!;
230 0 $cmd->close;
231 0 $cmd->_set_status_closed($err);
232 0 return;
233 }
234 else {
235 0 $cmd->_set_status_timeout;
236 0 return;
237 }
238 }
239  
240 56 return 1;
241 }
242  
243 0 ▶sub _set_status_timeout {
Subroutine _set_status_timeout: not called CC=3 SCAR=24.8
244 0 my $cmd = shift;
245 0 ▶ my $pkg = ref($cmd) || $cmd;
Condition0%0/3
ref $cmd || $cmd
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $cmd || $cmd
ref $cmd $cmd
Truth table0%0/3
A || B
A= ref $cmdB= $cmd
ABresult
1X1
011
000
246  
247 0 $cmd->set_status($cmd->DEF_REPLY_CODE, "[$pkg] Timeout");
248 0 ▶ carp(ref($cmd) . ": " . (caller(1))[3] . "(): timeout") if $cmd->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 0 0
249 }
250  
251 0 ▶sub _set_status_closed {
Subroutine _set_status_closed: not called CC=3 SCAR=24.8
252 0 my $cmd = shift;
253 0 my $err = shift;
254 0 ▶ my $pkg = ref($cmd) || $cmd;
Condition0%0/3
ref $cmd || $cmd
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $cmd || $cmd
ref $cmd $cmd
Truth table0%0/3
A || B
A= ref $cmdB= $cmd
ABresult
1X1
011
000
255  
256 0 $cmd->set_status($cmd->DEF_REPLY_CODE, "[$pkg] Connection closed");
257 0 ▶ carp(ref($cmd) . ": " . (caller(1))[3]
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 0 0
258 . "(): unexpected EOF on command channel: $err") if $cmd->debug;
259 }
260  
261 # Max length of an incomplete response line
262 8 ▶sub max_partial_bytes { 1024 * 1024 } # 1 MiB
Subroutine max_partial_bytes: called CC=1 SCAR=0.0
Pod documented
263  
264 # Max total size of a multiline response
265 26 ▶sub max_resp_bytes { 8 * 1024 * 1024 } # 8 MiB
Subroutine max_resp_bytes: called CC=1 SCAR=0.0
Pod documented
266  
267 # Max total size of a dot-terminated body
268 0 ▶sub max_dotread_bytes { 32 * 1024 * 1024 } # 32 MiB
Subroutine max_dotread_bytes: not called CC=1 SCAR=6.9
Pod documented
269  
270 3 ▶sub _set_status_line_too_long {
Subroutine _set_status_line_too_long: called CC=3 SCAR=12.0
271 3 my $cmd = shift;
272 3 ▶ my $pkg = ref($cmd) || $cmd;
Condition33%1/3
ref $cmd || $cmd
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $cmd || $cmd
ref $cmd $cmd
Truth table33%1/3
A || B
A= ref $cmdB= $cmd
ABresult
1X1
011
000
273  
274 3 $cmd->set_status($cmd->DEF_REPLY_CODE, "[$pkg] Response line too long");
275 3 ▶ carp(ref($cmd) . ": " . (caller(1))[3]
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 3 3
276 . "(): response line too long") if $cmd->debug;
277 }
278  
279 1 ▶sub _set_status_resp_too_large {
Subroutine _set_status_resp_too_large: called CC=3 SCAR=12.0
280 1 my $cmd = shift;
281 1 ▶ my $pkg = ref($cmd) || $cmd;
Condition33%1/3
ref $cmd || $cmd
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $cmd || $cmd
ref $cmd $cmd
Truth table33%1/3
A || B
A= ref $cmdB= $cmd
ABresult
1X1
011
000
282  
283 1 $cmd->set_status($cmd->DEF_REPLY_CODE, "[$pkg] Response too large");
284 1 ▶ carp(ref($cmd) . ": " . (caller(1))[3]
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 1 1
285 . "(): response too large") if $cmd->debug;
286 }
287  
288 2 ▶sub _set_status_dotread_too_large {
Subroutine _set_status_dotread_too_large: called CC=3 SCAR=12.0
289 2 my $cmd = shift;
290 2 ▶ my $pkg = ref($cmd) || $cmd;
Condition33%1/3
ref $cmd || $cmd
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $cmd || $cmd
ref $cmd $cmd
Truth table33%1/3
A || B
A= ref $cmdB= $cmd
ABresult
1X1
011
000
291  
292 2 $cmd->set_status($cmd->DEF_REPLY_CODE, "[$pkg] Dot-read too large");
293 2 ▶ carp(ref($cmd) . ": " . (caller(1))[3]
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 2 2
294 . "(): dot-read too large") if $cmd->debug;
295 }
296  
297 78 ▶sub _is_closed {
Subroutine _is_closed: called CC=2 SCAR=8.4
298 78 my $cmd = shift;
299 78 ▶ if (!defined fileno($cmd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno $cmd) 0 78 78
300 0 $cmd->_set_status_closed($!);
301 0 return 1;
302 }
303 78 return 0;
304 }
305  
306 19 ▶sub command {
Subroutine command: called CC=8 SCAR=23.6
Pod documented
307 19 my $cmd = shift;
308  
309 19 ▶ return $cmd
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->_is_closed 0 19 19
310 if $cmd->_is_closed;
311  
312 $cmd->dataend()
313 19 ▶ if (exists ${*$cmd}{'net_cmd_last_ch'});
Branch: 1 branch
BranchTrueFalseTotal
if exists do { *$cmd }->{"net_cmd_last_ch"} 0 19 19
314  
315 19 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 19 0 19
316 my $str = join(
317 " ",
318 map {
319 19 /\n/
320 0 ▶ ? do { my $n = $_; $n =~ tr/\n/ /; $n }
Branch: 1 branch
BranchTrueFalseTotal
/\n/ ? : 0 26 26
321 : $_;
322 } @_
323 );
324 19 ▶ $str = $cmd->toascii($str) if $tr;
Branch: 1 branch
BranchTrueFalseTotal
if $tr 0 19 19
325 19 $str .= "\015\012";
326  
327 19 ▶ $cmd->debug_print(1, $str)
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 19 19
328 if ($cmd->debug);
329  
330 # though documented to return undef on failure, the legacy behavior
331 # was to return $cmd even on failure, so this odd construct does that
332 19 ▶ $cmd->_syswrite_with_timeout($str)
Branch: 1 branch
BranchTrueFalseTotal
$cmd->_syswrite_with_timeout($str) or return $cmd 19 0 19
333 or return $cmd;
334 }
335  
336 19 $cmd;
337 }
338  
339  
340 0 ▶sub ok {
Subroutine ok: not called CC=3 SCAR=24.8
Pod documented
341 0 ▶ @_ == 1 or croak 'usage: $obj->ok()';
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("usage: \$obj->ok()") 0 0 0
342  
343 0 my $code = $_[0]->code;
344 0 ▶ 0 < $code && $code < 400;
Branch: 1 branch
BranchTrueFalseTotal
0 < $code and $code < 400 0 0 0
345 }
346  
347  
348 0 ▶sub unsupported {
Subroutine unsupported: not called CC=1 SCAR=6.9
Pod documented
349 0 my $cmd = shift;
350  
351 0 $cmd->set_status(580, 'Unsupported command');
352  
353 0 0;
354 }
355  
356  
357 74 ▶sub getline {
Subroutine getline: called CC=12 SCAR=25.7
Pod documented
358 74 my $cmd = shift;
359  
360 74 ▶ ${*$cmd}{'net_cmd_lines'} ||= [];
Condition100%2/2
do { *$cmd }->{"net_cmd_lines"} ||= []
l!l
5816
MC/DC100%1/1
do { *$cmd }->{"net_cmd_lines"} ||= []
do { *$cmd }->{"net_cmd_lines"}
Truth table100%2/2
A
A= do { *$cmd }->{"net_cmd_lines"}
Aresult
11
00
361  
362 53 return shift @{${*$cmd}{'net_cmd_lines'}}
363 74 ▶ if scalar(@{${*$cmd}{'net_cmd_lines'}});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{do { *$cmd }->{"net_cmd_lines"};} 53 21 74
364  
365 21 ▶ my $partial = defined(${*$cmd}{'net_cmd_partial'}) ? ${*$cmd}{'net_cmd_partial'} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined do { *$cmd }->{'net_cmd_partial'} ? : 5 16 21
366  
367 return
368 21 ▶ if $cmd->_is_closed;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->_is_closed 0 21 21
369  
370 21 my $fd = fileno($cmd);
371 21 my $rin = "";
372 21 vec($rin, $fd, 1) = 1;
373  
374 21 my $buf;
375  
376 21 ▶ until (scalar(@{${*$cmd}{'net_cmd_lines'}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{do { *$cmd }->{"net_cmd_lines"};}) 21 18 39
377 21 ▶ my $timeout = $cmd->timeout || undef;
Condition100%2/2
$cmd->timeout || undef
l!l
813
MC/DC100%1/1
$cmd->timeout || undef
$cmd->timeout
Truth table100%2/2
A
A= $cmd->timeout
Aresult
11
00
378 21 my $rout;
379  
380 21 my $select_ret = select($rout = $rin, undef, undef, $timeout);
381 21 ▶ if ($select_ret > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_ret > 0) { } 21 0 21
382 21 ▶ unless (sysread($cmd, $buf = "", 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysread $cmd, $buf = "", 1024) 0 21 21
383 0 my $err = $!;
384 0 $cmd->close;
385 0 $cmd->_set_status_closed($err);
386 0 return;
387 }
388  
389 21 ▶ if (length($partial) + length($buf) > $cmd->max_partial_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($partial) + length($buf) > $cmd->max_partial_bytes) 3 18 21
390 3 $cmd->close;
391 3 $cmd->_set_status_line_too_long;
392 3 return;
393 }
394  
395 18 substr($buf, 0, 0) = $partial; ## prepend from last sysread
396  
397 18 my @buf = split(/\015?\012/, $buf, -1); ## break into lines
398  
399 18 $partial = pop @buf;
400  
401 18 push(@{${*$cmd}{'net_cmd_lines'}}, map {"$_\n"} @buf);
402  
403 }
404 else {
405 0 $cmd->_set_status_timeout;
406 0 return;
407 }
408 }
409  
410 18 ${*$cmd}{'net_cmd_partial'} = $partial;
411  
412 18 ▶ if ($tr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr) 0 18 18
413 0 foreach my $ln (@{${*$cmd}{'net_cmd_lines'}}) {
414 0 $ln = $cmd->toebcdic($ln);
415 }
416 }
417  
418 18 shift @{${*$cmd}{'net_cmd_lines'}};
419 }
420  
421  
422 0 ▶sub ungetline {
Subroutine ungetline: not called CC=2 SCAR=17.9
Pod documented
423 0 my ($cmd, $str) = @_;
424  
425 0 ▶ ${*$cmd}{'net_cmd_lines'} ||= [];
Condition0%0/2
do { *$cmd }->{"net_cmd_lines"} ||= []
l!l
00
MC/DC0%0/1
do { *$cmd }->{"net_cmd_lines"} ||= []
do { *$cmd }->{"net_cmd_lines"}
Truth table0%0/2
A
A= do { *$cmd }->{"net_cmd_lines"}
Aresult
11
00
426 0 unshift(@{${*$cmd}{'net_cmd_lines'}}, $str);
427 }
428  
429  
430 49 ▶sub parse_response {
Subroutine parse_response: called CC=2 SCAR=7.2
Pod documented
431 return ()
432 49 ▶ unless $_[1] =~ s/^(\d\d\d)(.?)//o;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ s/^(\d\d\d)(.?)//o 0 49 49
433 49 ($1, $2 eq "-");
434 }
435  
436  
437 25 ▶sub response {
Subroutine response: called CC=8 SCAR=21.9
Pod documented
438 25 my $cmd = shift;
439 25 my ($code, $more) = (undef) x 2;
440  
441 25 $cmd->set_status($cmd->DEF_REPLY_CODE, undef); # initialize the response
442  
443 25 my $resp_bytes = 0;
444  
445 25 while (1) {
446 49 my $str = $cmd->getline();
447  
448 49 ▶ return CMD_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 49 49
449 unless defined($str);
450  
451 49 ▶ $cmd->debug_print(0, $str)
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 49 49
452 if ($cmd->debug);
453  
454 49 ($code, $more) = $cmd->parse_response($str);
455 49 ▶ unless (defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 0 49 49
456 0 ▶ carp("$cmd: response(): parse error in '$str'") if ($cmd->debug);
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 0 0
457 0 $cmd->ungetline($str);
458 0 $@ = $str; # $@ used as tunneling hack
459 0 return CMD_ERROR;
460 }
461  
462 49 ${*$cmd}{'net_cmd_code'} = $code;
463  
464 49 push(@{${*$cmd}{'net_cmd_resp'}}, $str);
465 49 $resp_bytes += length($str);
466 49 ▶ if ($resp_bytes > $cmd->max_resp_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp_bytes > $cmd->max_resp_bytes) 1 48 49
467 1 $cmd->close;
468 1 $cmd->_set_status_resp_too_large;
469 1 return CMD_ERROR;
470 }
471  
472 48 ▶ last unless ($more);
Branch: 1 branch
BranchTrueFalseTotal
unless $more 24 24 48
473 }
474  
475 24 ▶ return unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 24 24
476 24 substr($code, 0, 1);
477 }
478  
479  
480 4 ▶sub read_until_dot {
Subroutine read_until_dot: called CC=6 SCAR=17.9
Pod documented
481 4 my $cmd = shift;
482 4 my $fh = shift;
483 4 my $arr = [];
484 4 my $total = 0;
485  
486 4 while (1) {
487 36 ▶ my $str = $cmd->getline() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $str = $cmd->getline or return 36 0 36
488  
489 36 ▶ $cmd->debug_print(0, $str)
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug & 4 0 36 36
490 if ($cmd->debug & 4);
491  
492 36 ▶ last if ($str =~ /^\.\r?\n/o);
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^\.\r?\n/o 2 34 36
493  
494 34 $str =~ s/^\.\././o;
495  
496 34 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 14 20 34
497 14 print $fh $str;
498 }
499 else {
500 20 push(@$arr, $str);
501 20 $total += length($str);
502 20 ▶ if ($total > $cmd->max_dotread_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > $cmd->max_dotread_bytes) 2 18 20
503 2 $cmd->close;
504 2 $cmd->_set_status_dotread_too_large;
505 2 return;
506 }
507 }
508 }
509  
510 2 $arr;
511 }
512  
513  
514 22 ▶sub datasend {
Subroutine datasend: called CC=15 SCAR=28.4
Pod documented
515 22 my $cmd = shift;
516 22 ▶ my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 0 22 22
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1210
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
517 22 my $line = join("", @$arr);
518  
519 # Perls < 5.10.1 (with the exception of 5.8.9) have a performance problem with
520 # the substitutions below when dealing with strings stored internally in
521 # UTF-8, so downgrade them (if possible).
522 # Data passed to datasend() should be encoded to octets upstream already so
523 # shouldn't even have the UTF-8 flag on to start with, but if it so happens
524 # that the octets are stored in an upgraded string (as can sometimes occur)
525 # then they would still downgrade without fail anyway.
526 # Only Unicode codepoints > 0xFF stored in an upgraded string will fail to
527 # downgrade. We fail silently in that case, and a "Wide character in print"
528 # warning will be emitted later by syswrite().
529 22 ▶ utf8::downgrade($line, 1) if $] < 5.010001 && $] != 5.008009;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.010001" and $] != "5.008009" 0 22 22
Condition33%1/3
$] < "5.010001" and $] != "5.008009"
!ll&&!rl&&r
2200
MC/DC0%0/2
$] < "5.010001" and $] != "5.008009"
$] < "5.010001" $] != "5.008009"
Truth table33%1/3
A and B
A= $] < "5.010001"B= $] != "5.008009"
ABresult
0X0
100
111
530  
531 22 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->_is_closed 0 22 22
532 if $cmd->_is_closed;
533  
534 22 my $last_ch = ${*$cmd}{'net_cmd_last_ch'};
535  
536 # We have not send anything yet, so last_ch = "\012" means we are at the start of a line
537 22 ▶ $last_ch = ${*$cmd}{'net_cmd_last_ch'} = "\012" unless defined $last_ch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_ch 16 6 22
538  
539 22 ▶ return 1 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 1 21 22
540  
541 21 ▶ if ($cmd->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->debug) 0 21 21
542 0 foreach my $b (split(/\n/, $line)) {
543 0 $cmd->debug_print(1, "$b\n");
544 }
545 }
546  
547 21 $line =~ tr/\r\n/\015\012/ unless "\r" eq "\015";
548  
549 21 my $first_ch = '';
550  
551 21 ▶ if ($last_ch eq "\015") {
Branch: 2 branches
BranchTrueFalseTotal
if ($last_ch eq "\r") { } 4 17 21
elsif ($last_ch eq "\n") { } 16 1 17
552 # Remove \012 so it does not get prefixed with another \015 below
553 # and escape the . if there is one following it because the fixup
554 # below will not find it
555 4 ▶ $first_ch = "\012" if $line =~ s/^\012(\.?)/$1$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ s/^\012(\.?)/$1$1/ 4 0 4
556 }
557 elsif ($last_ch eq "\012") {
558 # Fixup below will not find the . as the first character of the buffer
559 16 ▶ $first_ch = "." if $line =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\./ 4 12 16
560 }
561  
562 21 $line =~ s/\015?\012(\.?)/\015\012$1$1/sg;
563  
564 21 substr($line, 0, 0) = $first_ch;
565  
566 21 ${*$cmd}{'net_cmd_last_ch'} = substr($line, -1, 1);
567  
568 21 ▶ $cmd->_syswrite_with_timeout($line)
Branch: 1 branch
BranchTrueFalseTotal
$cmd->_syswrite_with_timeout($line) or return 21 0 21
569 or return;
570  
571 21 1;
572 }
573  
574  
575 0 ▶sub rawdatasend {
Subroutine rawdatasend: not called CC=7 SCAR=40.3
Pod documented
576 0 my $cmd = shift;
577 0 ▶ my $arr = @_ == 1 && ref($_[0]) ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
578 0 my $line = join("", @$arr);
579  
580 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->_is_closed 0 0 0
581 if $cmd->_is_closed;
582  
583 0 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
584 unless length($line);
585  
586 0 ▶ if ($cmd->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->debug) 0 0 0
587 0 my $b = "$cmd>>> ";
588 0 print STDERR $b, join("\n$b", split(/\n/, $line)), "\n";
589 }
590  
591 0 ▶ $cmd->_syswrite_with_timeout($line)
Branch: 1 branch
BranchTrueFalseTotal
$cmd->_syswrite_with_timeout($line) or return 0 0 0
592 or return;
593  
594 0 1;
595 }
596  
597  
598 16 ▶sub dataend {
Subroutine dataend: called CC=6 SCAR=18.4
Pod documented
599 16 my $cmd = shift;
600  
601 16 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->_is_closed 0 16 16
602 if $cmd->_is_closed;
603  
604 16 my $ch = ${*$cmd}{'net_cmd_last_ch'};
605 16 my $tosend;
606  
607 16 ▶ if (!defined $ch) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ch) { } 0 16 16
elsif ($ch ne "\n") { } 6 10 16
608 0 return 1;
609 }
610 elsif ($ch ne "\012") {
611 6 $tosend = "\015\012";
612 }
613  
614 16 $tosend .= ".\015\012";
615  
616 16 ▶ $cmd->debug_print(1, ".\n")
Branch: 1 branch
BranchTrueFalseTotal
if $cmd->debug 0 16 16
617 if ($cmd->debug);
618  
619 16 ▶ $cmd->_syswrite_with_timeout($tosend)
Branch: 1 branch
BranchTrueFalseTotal
$cmd->_syswrite_with_timeout($tosend) or return 0 16 0 16
620 or return 0;
621  
622 16 delete ${*$cmd}{'net_cmd_last_ch'};
623  
624 16 $cmd->response() == CMD_OK;
625 }
626  
627 # read and write to tied filehandle
628 0 ▶sub tied_fh {
Subroutine tied_fh: not called CC=1 SCAR=6.9
Pod documented
629 0 my $cmd = shift;
630 0 ${*$cmd}{'net_cmd_readbuf'} = '';
631 0 my $fh = gensym();
632 0 tie *$fh, ref($cmd), $cmd;
633 0 return $fh;
634 }
635  
636 # tie to myself
637 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
638 0 my $class = shift;
639 0 my $cmd = shift;
640 0 return $cmd;
641 }
642  
643 # Tied filehandle read. Reads requested data length, returning
644 # end-of-file when the dot is encountered.
645 0 ▶sub READ {
Subroutine READ: not called CC=7 SCAR=40.3
646 0 my $cmd = shift;
647 0 my ($len, $offset) = @_[1, 2];
648 0 ▶ return unless exists ${*$cmd}{'net_cmd_readbuf'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { *$cmd }->{"net_cmd_readbuf"} 0 0 0
649 0 my $done = 0;
650 0 ▶ while (!$done and length(${*$cmd}{'net_cmd_readbuf'}) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and length do { *$cmd }->{"net_cmd_readbuf"} < $len) 0 0 0
Condition0%0/3
not $done and length do { *$cmd }->{"net_cmd_readbuf"} < $len
!ll&&!rl&&r
000
MC/DC0%0/2
not $done and length do { *$cmd }->{"net_cmd_readbuf"} < $len
not $done length do { *$cmd }->{"net_cmd_readbuf"} < $len
Truth table0%0/3
not(A) and B
A= not $doneB= length do { *$cmd }->{"net_cmd_readbuf"} < $len
ABresult
0X0
100
111
651 0 ▶ ${*$cmd}{'net_cmd_readbuf'} .= $cmd->getline() or return;
Branch: 1 branch
BranchTrueFalseTotal
do { *$cmd }->{"net_cmd_readbuf"} .= $cmd->getline or return 0 0 0
652 0 ▶ $done++ if ${*$cmd}{'net_cmd_readbuf'} =~ s/^\.\r?\n\Z//m;
Branch: 1 branch
BranchTrueFalseTotal
if do { *$cmd }->{"net_cmd_readbuf"} =~ s/^\.\r?\n\Z//m 0 0 0
653 }
654  
655 0 $_[0] = '';
656 0 substr($_[0], $offset + 0) = substr(${*$cmd}{'net_cmd_readbuf'}, 0, $len);
657 0 substr(${*$cmd}{'net_cmd_readbuf'}, 0, $len) = '';
658 0 ▶ delete ${*$cmd}{'net_cmd_readbuf'} if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
659  
660 0 return length $_[0];
661 }
662  
663  
664 0 ▶sub READLINE {
Subroutine READLINE: not called CC=3 SCAR=24.8
665 0 my $cmd = shift;
666  
667 # in this context, we use the presence of readbuf to
668 # indicate that we have not yet reached the eof
669 0 ▶ return unless exists ${*$cmd}{'net_cmd_readbuf'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists do { *$cmd }->{"net_cmd_readbuf"} 0 0 0
670 0 my $line = $cmd->getline;
671 0 ▶ return if $line =~ /^\.\r?\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\.\r?\n/ 0 0 0
672 0 $line;
673 }
674  
675  
676 0 ▶sub PRINT {
Subroutine PRINT: not called CC=3 SCAR=24.8
677 0 my $cmd = shift;
678 0 my ($buf, $len, $offset) = @_;
679 0 ▶ $len ||= length($buf);
Condition0%0/3
$len ||= length $buf
l!l&&r!l&&!r
000
MC/DC0%0/2
$len ||= length $buf
$len length $buf
Truth table0%0/3
A ||= B
A= $lenB= length $buf
ABresult
1X1
011
000
680 0 $offset += 0;
681 0 ▶ return unless $cmd->datasend(substr($buf, $offset, $len));
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd->datasend(substr($buf, $offset, $len)) 0 0 0
682 0 ${*$cmd}{'net_cmd_sending'}++; # flag that we should call dataend()
683 0 return $len;
684 }
685  
686  
687 0 ▶sub CLOSE {
Subroutine CLOSE: not called CC=2 SCAR=17.9
688 0 my $cmd = shift;
689 0 ▶ my $r = exists(${*$cmd}{'net_cmd_sending'}) ? $cmd->dataend : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists do { *$cmd }->{'net_cmd_sending'} ? : 0 0 0
690 0 delete ${*$cmd}{'net_cmd_readbuf'};
691 0 delete ${*$cmd}{'net_cmd_sending'};
692 0 $r;
693 }
694  
695 18 1;
696  
697 __END__
« FTP/I.pm ↑ Summary FTP/dataconn.pm »