blib/lib/Term/ReadPassword/Win32.pm

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