ReadPassword/Win32.pm

filter: Statementstmt 20.8% 20 / 96 Branchbran 2.2% 1 / 44 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 5 totaltotal 13.7% 26 / 189 CC 30 SCAR 65.0
CC
30
Cov
13%
read_password
870.0
DiffTick
6.0
ReadPasswd
2.0
CRAP 664.5
« ReadPassword.pm ↑ Summary
1 package Term::ReadPassword::Win32;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 $SUPPRESS_NEWLINE $INPUT_LIMIT
10 $USE_STARS $STAR_STRING $UNSTAR_STRING
11 1 );
12  
13 1 our @EXPORT = qw(read_password);
14 1 our @EXPORT_OK = qw(ReadPasswd read_passwd);
15  
16 1 our $VERSION = '0.11_02';
17  
18 1 ▶if ( IsWin32() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (IsWin32()) { } 0 1 1
19 0 eval('use Win32');
20 0 eval('use Win32::Console');
21 0 eval('use Win32API::File');
22 }
23 else {
24 1 eval('require Term::ReadPassword');
25 }
26  
27 # The maximum amount of data for the input buffer to hold
28 1 $INPUT_LIMIT = 1000;
29  
30 0 ▶sub ReadPasswd {
Subroutine ReadPasswd: not called CC=1 SCAR=6.9
Pod undocumented
31 0 read_password(@_);
32 }
33  
34 0 ▶sub read_passwd {
Subroutine read_passwd: not called CC=1 SCAR=6.9
Pod undocumented
35 0 read_password(@_);
36 }
37  
38 0 ▶sub read_password {
Subroutine read_password: not called CC=29 SCAR=67.7
Pod undocumented
39 0 my ( $prompt, $idle_limit, $interruptmode ) = @_;
40 0 ▶ $prompt = '' unless defined $prompt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt 0 0 0
41 0 ▶ $idle_limit = 0 unless defined $idle_limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idle_limit 0 0 0
42 0 ▶ $interruptmode = 0 unless defined $interruptmode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $interruptmode 0 0 0
43  
44 0 ▶ if ( !IsWin32() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (IsWin32()) 0 0 0
45 0 ▶ my $interruptable = ( $interruptmode > 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$interruptmode > 0 ? : 0 0 0
46  
47 0 $Term::ReadPassword::SUPPRESS_NEWLINE = $SUPPRESS_NEWLINE;
48 0 $Term::ReadPassword::INPUT_LIMIT = $INPUT_LIMIT;
49 0 $Term::ReadPassword::USE_STARS = $USE_STARS;
50 0 $Term::ReadPassword::STAR_STRING = $STAR_STRING;
51 0 $Term::ReadPassword::UNSTAR_STRING = $UNSTAR_STRING;
52  
53 0 return Term::ReadPassword::read_password( $prompt, $idle_limit,
54 $interruptable );
55 }
56  
57 0 $idle_limit *= 1000; # sec -> msec
58  
59 0 my $CONIN = Win32::Console->new( Win32::Console::STD_INPUT_HANDLE() );
60 0 my $CONOUT = Win32::Console->new( Win32::Console::STD_ERROR_HANDLE() );
61  
62 # make sure that input and output are not redirected
63 0 my $hStdin = $CONIN->{'handle'};
64 0 $CONIN->{'handle'} = Win32API::File::createFile( 'CONIN$', 'rw' );
65 0 my $hStderr = $CONOUT->{'handle'};
66 0 $CONOUT->{'handle'} = Win32API::File::createFile( 'CONOUT$', 'rw' );
67  
68 0 $CONOUT->Write($prompt);
69  
70 0 $CONIN->Flush();
71  
72 0 my $conmode = $CONIN->Mode();
73 0 ▶ if ( $interruptmode <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interruptmode <= 1) 0 0 0
74  
75 # disable the system for processing Ctrl+C
76 0 $CONIN->Mode( $conmode & ~Win32::Console::ENABLE_PROCESSED_INPUT() );
77 }
78  
79 # Optionally echo stars in place of password characters.
80 0 ▶ my $star_string = $USE_STARS ? ( $STAR_STRING || '*' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$Term::ReadPassword::Win32::USE_STARS ? : 0 0 0
Condition0%0/2
$Term::ReadPassword::Win32::STAR_STRING or "*"
l!l
00
MC/DC0%0/1
$Term::ReadPassword::Win32::STAR_STRING or "*"
$Term::ReadPassword::Win32::STAR_STRING
Truth table0%0/2
A
A= $Term::ReadPassword::Win32::STAR_STRING
Aresult
11
00
81 0 ▶ my $unstar_string = $USE_STARS ? ( $UNSTAR_STRING || "\b \b" ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$Term::ReadPassword::Win32::USE_STARS ? : 0 0 0
Condition0%0/2
$Term::ReadPassword::Win32::UNSTAR_STRING or "\b \b"
l!l
00
MC/DC0%0/1
$Term::ReadPassword::Win32::UNSTAR_STRING or "\b \b"
$Term::ReadPassword::Win32::UNSTAR_STRING
Truth table0%0/2
A
A= $Term::ReadPassword::Win32::UNSTAR_STRING
Aresult
11
00
82  
83 # the input buffer
84 0 my $input = '';
85  
86 0 my $tick = Win32::GetTickCount();
87 0 my $tick2 = $tick;
88 keyin:
89 0 while (1) {
90 0 ▶ while ( $CONIN->GetEvents() == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CONIN->GetEvents == 0) 0 0 0
91 0 Win32::Sleep(10);
92 0 $tick2 = Win32::GetTickCount();
93 0 ▶ if ( $idle_limit && ( DiffTick( $tick2, $tick ) > $idle_limit ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($idle_limit and DiffTick($tick2, $tick) > $idle_limit) 0 0 0
Condition0%0/3
$idle_limit and DiffTick($tick2, $tick) > $idle_limit
!ll&&!rl&&r
000
MC/DC0%0/2
$idle_limit and DiffTick($tick2, $tick) > $idle_limit
$idle_limit DiffTick($tick2, $tick) > $idle_limit
Truth table0%0/3
A and B
A= $idle_limitB= DiffTick($tick2, $tick) > $idle_limit
ABresult
0X0
100
111
94 {
95  
96 # timeout
97 0 undef $input;
98 0 last keyin;
99 }
100 }
101 0 $tick = $tick2;
102  
103 # read console
104 0 my ( $evtype, $keydown, undef, $keycode, undef, $ch, undef )
105 = $CONIN->Input();
106  
107 # next if not a keydown event
108 0 ▶ next if ( $evtype != 1 || !$keydown );
Branch: 1 branch
BranchTrueFalseTotal
if $evtype != 1 or not $keydown 0 0 0
Condition0%0/3
$evtype != 1 or not $keydown
l!l&&r!l&&!r
000
MC/DC0%0/2
$evtype != 1 or not $keydown
$evtype != 1 not $keydown
Truth table0%0/3
A or not(B)
A= $evtype != 1B= not $keydown
ABresult
1X1
011
000
109  
110 0 ▶ $ch = 0x7f if ( $keycode == 0x2e ); # Del
Branch: 1 branch
BranchTrueFalseTotal
if $keycode == 46 0 0 0
111 0 ▶ next if ( $ch == 0x00 ); # Special Keys
Branch: 1 branch
BranchTrueFalseTotal
if $ch == 0 0 0 0
112 0 $ch &= 0xff; # for multibyte chars
113  
114 0 ▶ if ( $ch == 0x0d || $ch == 0x0a ) { # Enter
Branch: 4 branches
BranchTrueFalseTotal
if ($ch == 13 or $ch == 10) { } 0 0 0
elsif ($ch == 8 or $ch == 127) { } 0 0 0
elsif ($ch == 21) { } 0 0 0
elsif ($interruptmode > 0 and $ch == 27 || $ch == 3) { } 0 0 0
Condition0%0/3
$ch == 13 or $ch == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$ch == 13 or $ch == 10
$ch == 13 $ch == 10
Truth table0%0/3
A or B
A= $ch == 13B= $ch == 10
ABresult
1X1
011
000
115 # end
116 0 last;
117 }
118 ▶ elsif ( $ch == 0x08 || $ch == 0x7f ) { # BS, Del
Condition0%0/3
$ch == 8 or $ch == 127
l!l&&r!l&&!r
000
MC/DC0%0/2
$ch == 8 or $ch == 127
$ch == 8 $ch == 127
Truth table0%0/3
A or B
A= $ch == 8B= $ch == 127
ABresult
1X1
011
000
119 0 ▶ if ( length($input) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input > 0) 0 0 0
120  
121 # delete the last char
122 #
123 # BUG: If the last char is multibyte character,
124 # this doesn't work well.
125 0 chop $input;
126  
127 0 $CONOUT->Write($unstar_string);
128 }
129 }
130 elsif ( $ch == 0x15 ) { # Ctrl+U
131 0 $CONOUT->Write( $unstar_string x length($input) );
132 0 $input = ''; # clear all
133 }
134 ▶ elsif (( $interruptmode > 0 )
Condition0%0/6
$interruptmode > 0 and $ch == 27 || $ch == 3
!ll&&!rl&&r
000
$ch == 27 or $ch == 3
l!l&&r!l&&!r
000
MC/DC0%0/3
$interruptmode > 0 and $ch == 27 || $ch == 3
$interruptmode > 0 $ch == 27 $ch == 3
Truth table0%0/4
A and B or C
A= $interruptmode > 0B= $ch == 27C= $ch == 3
ABCresult
0XX0
1000
11X1
1011
135 && ( $ch == 0x1b || $ch == 0x03 ) )
136 { # Esc, Ctrl+C
137 # cancel
138 0 undef $input;
139 0 last;
140 }
141 else {
142 # normal chars
143 0 $input .= chr($ch);
144 0 $CONOUT->Write($star_string);
145 }
146 0 ▶ if ( length($input) > $INPUT_LIMIT ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input > $Term::ReadPassword::Win32::INPUT_LIMIT) 0 0 0
147 0 $input = substr( $input, 0, $INPUT_LIMIT );
148 }
149 }
150  
151 0 ▶ $CONOUT->Write("\n") unless $SUPPRESS_NEWLINE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Term::ReadPassword::Win32::SUPPRESS_NEWLINE 0 0 0
152  
153 # restore console mode
154 0 $CONIN->Mode($conmode);
155  
156 # restore console handles
157 0 Win32API::File::CloseHandle( $CONIN->{'handle'} );
158 0 $CONIN->{'handle'} = $hStdin;
159 0 Win32API::File::CloseHandle( $CONOUT->{'handle'} );
160 0 $CONOUT->{'handle'} = $hStderr;
161  
162 0 $CONIN = undef;
163 0 close STDIN;
164 0 open STDIN, '+<CONIN$';
165 0 return $input;
166 }
167  
168 0 ▶sub DiffTick {
Subroutine DiffTick: not called CC=2 SCAR=17.9
Pod undocumented
169 0 my ( $tick1, $tick2 ) = @_;
170 0 $tick1 &= 0xFFFFFFFF;
171 0 $tick2 &= 0xFFFFFFFF;
172  
173 0 ▶ if ( $tick1 >= $tick2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tick1 >= $tick2) { } 0 0 0
174 0 return $tick1 - $tick2;
175 }
176 else {
177 0 return 0xFFFFFFFF + 1 + $tick1 - $tick2;
178 }
179 }
180  
181 1 ▶sub IsWin32 {
Subroutine IsWin32: called CC=1 SCAR=0.0
Pod undocumented
182 1 return ( $^O eq 'MSWin32' );
183 }
184  
185 1 1;
186  
187 __END__
« ReadPassword.pm ↑ Summary