InKey.pm

filter: Statementstmt 59.1% 87 / 147 Branchbran 26.4% 18 / 68 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.3% 7 / 12 Podpod 0.0% 0 / 10 totaltotal 45.0% 114 / 253 CC 41 SCAR 34.1
CC
41
Cov
48%
WinReadKey
90.0
ReadPassword
33.9
WinClear
30.0
CRAP 30.3
↑ Summary demo.pl »
1 package Term::InKey;
2  
3 # Ariel Brosh (R.I.P), November 2001, for Raz Information Systems
4 # Now manitained by Oded S. Resnik Raz Information Systems
5  
6 1 require Exporter;
7 1 ▶use strict qw(vars subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw(@ISA @EXPORT $VERSION $WIN32CONSOLE $BAD_CLS $BAD_RKEY $TER_CLS);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 @ISA = qw(Exporter);
10 1 @EXPORT = qw(ReadKey Clear ReadPassword);
11  
12 1 $VERSION = '1.04';
13  
14 0 ▶sub WinSetConsole {
Subroutine WinSetConsole: not called CC=2 SCAR=17.9
Pod undocumented
15 0 ▶ return $WIN32CONSOLE if $WIN32CONSOLE;
Branch: 1 branch
BranchTrueFalseTotal
if $Term::InKey::WIN32CONSOLE 0 0 0
16 0 require Win32::Console;
17 0 import Win32::Console;
18 {
19 0 local *STDERR;
20 0 open STDERR, ">/dev/null";
21 0 $WIN32CONSOLE = Win32::Console->
22 new(Win32::Console->STD_INPUT_HANDLE);
23 }
24 0 return $WIN32CONSOLE;
25 }
26  
27 0 ▶sub WinReadKey {
Subroutine WinReadKey: not called CC=9 SCAR=45.0
Pod undocumented
28 0 my $y;
29 0 eval {
30 0 ▶ if(&WinSetConsole)
Branch: 1 branch
BranchTrueFalseTotal
if (&WinSetConsole) 0 0 0
31 {
32 0 ▶ my $mode = $WIN32CONSOLE->Mode || die $^E;
Condition0%0/2
$Term::InKey::WIN32CONSOLE->Mode || die($^E)
l!l
00
MC/DC0%0/1
$Term::InKey::WIN32CONSOLE->Mode || die($^E)
$Term::InKey::WIN32CONSOLE->Mode
Truth table0%0/2
A
A= $Term::InKey::WIN32CONSOLE->Mode
Aresult
11
00
33 0 my $newmode = $mode;
34 0 $newmode &= ~(&ENABLE_LINE_INPUT | &ENABLE_ECHO_INPUT);
35 0 ▶ $WIN32CONSOLE->Mode($newmode) || die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$Term::InKey::WIN32CONSOLE->Mode($newmode) or die $^E 0 0 0
36 0 ▶ $WIN32CONSOLE->Flush || die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$Term::InKey::WIN32CONSOLE->Flush or die $^E 0 0 0
37  
38 0 $y = $WIN32CONSOLE->InputChar(1);
39 0 ▶ $WIN32CONSOLE->Flush || die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$Term::InKey::WIN32CONSOLE->Flush or die $^E 0 0 0
40 0 ▶ $WIN32CONSOLE->Mode($mode) || die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$Term::InKey::WIN32CONSOLE->Mode($mode) or die $^E 0 0 0
41 0 ▶ die $^E unless defined($y);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 0 0 0
42 }
43 };
44 0 ▶ die "Not implemented on $^O: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
45 0 $y;
46 }
47  
48 0 ▶sub BadReadKey {
Subroutine BadReadKey: not called CC=2 SCAR=17.9
Pod undocumented
49 0 ▶ if ($^O !~ /Win32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $^O =~ /Win32/i) 0 0 0
50 0 $BAD_RKEY =1;
51 0 system "stty raw -echo";
52 0 my $ch = getc;
53 0 system "stty -raw echo";
54 0 $ch;
55 }
56 }
57  
58 73 ▶sub ReadKey {
Subroutine ReadKey: called CC=4 SCAR=13.9
Pod undocumented
59 73 ▶ if ($^O =~ /Win32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win32/i) 0 73 73
60 0 return &WinReadKey;
61 };
62  
63 73 my $save;
64  
65 73 ▶ &BadReadKey if $BAD_RKEY;
Branch: 1 branch
BranchTrueFalseTotal
if $Term::InKey::BAD_RKEY 0 73 73
66  
67 73 eval {
68 73 require POSIX;
69 73 import POSIX;
70  
71 73 $save = new POSIX::Termios;
72 };
73 73 ▶ return &BadReadKey if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 73 73
74  
75 73 $save->getattr(0);
76  
77 73 my $x = new POSIX::Termios;
78  
79 73 $x->getattr(0);
80  
81 73 my %flags;
82  
83 73 &getit($x, \%flags);
84  
85 # +raw
86 {
87 73 $flags{'i'} &= ~(&IGNBRK|&BRKINT|&PARMRK|&ISTRIP
88 |&INLCR|&IGNCR|&ICRNL|&IXON);
89 73 $flags{'o'} &= ~&OPOST;
90 73 $flags{'l'} &= ~(&ECHO|&ECHONL|&ICANON|&ISIG|&IEXTEN);
91 73 $flags{'c'} &= ~(&CSIZE|&PARENB);
92 73 $flags{'c'} |= &CS8;
93 }
94 73 &setit($x, \%flags);
95  
96 73 $x->setattr(0);
97  
98 73 my $ch = getc;
99  
100 73 $save->setattr(0);
101  
102 73 $ch;
103 }
104  
105 73 ▶sub getit {
Subroutine getit: called CC=2 SCAR=6.9
Pod undocumented
106 73 my ($x, $flags) = @_;
107 73 foreach (qw(i o c l)) {
108 292 my $meth = $x->can("get${_}flag");
109 292 $flags->{$_} = &$meth($x);
110 }
111 }
112  
113 73 ▶sub setit {
Subroutine setit: called CC=2 SCAR=6.9
Pod undocumented
114 73 my ($x, $flags) = @_;
115 73 foreach (qw(i o c l)) {
116 292 my $meth = $x->can("set${_}flag");
117 292 &$meth($x, $flags->{$_});
118 }
119 }
120  
121 0 ▶sub WinClear {
Subroutine WinClear: not called CC=5 SCAR=34.0
Pod undocumented
122  
123 0 ▶&BadClear if $BAD_CLS;
Branch: 1 branch
BranchTrueFalseTotal
if $Term::InKey::BAD_CLS 0 0 0
124  
125 0 eval {
126 0 ▶ if(&WinSetConsole)
Branch: 1 branch
BranchTrueFalseTotal
if (&WinSetConsole) { } 0 0 0
127 {
128 0 local *STDERR;
129 0 open STDERR, ">/dev/null";
130 0 ▶ $WIN32CONSOLE->Cls || die $^E;
Branch: 1 branch
BranchTrueFalseTotal
$Term::InKey::WIN32CONSOLE->Cls or die $^E 0 0 0
131 0 $WIN32CONSOLE->Display;
132 }
133 else {
134 0 &BadClear;
135 };
136 };
137 0 ▶ &BadClear if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
138 }
139  
140  
141 0 ▶sub BadClear {
Subroutine BadClear: not called CC=3 SCAR=24.8
Pod undocumented
142 0 $BAD_CLS = 1;
143 0 ▶ if ($^O =~ /Win/i || $^O =~ /Dos/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win/i or $^O =~ /Dos/i) 0 0 0
Condition0%0/3
$^O =~ /Win/i or $^O =~ /Dos/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O =~ /Win/i or $^O =~ /Dos/i
$^O =~ /Win/i $^O =~ /Dos/i
Truth table0%0/3
A or B
A= $^O =~ /Win/iB= $^O =~ /Dos/i
ABresult
1X1
011
000
144 0 system "cls";
145 0 return;
146 }
147  
148 0 system "clear";
149 }
150  
151 1 ▶sub Clear {
Subroutine Clear: called CC=7 SCAR=20.8
Pod undocumented
152  
153 1 ▶ &BadClear if $BAD_CLS;
Branch: 1 branch
BranchTrueFalseTotal
if $Term::InKey::BAD_CLS 0 1 1
154  
155 1 ▶ if ($^O =~ /Win32/i || $^O =~ /Dos/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win32/i or $^O =~ /Dos/i) 0 1 1
Condition33%1/3
$^O =~ /Win32/i or $^O =~ /Dos/i
l!l&&r!l&&!r
001
MC/DC0%0/2
$^O =~ /Win32/i or $^O =~ /Dos/i
$^O =~ /Win32/i $^O =~ /Dos/i
Truth table33%1/3
A or B
A= $^O =~ /Win32/iB= $^O =~ /Dos/i
ABresult
1X1
011
000
156 0 &WinClear;
157 0 return;
158 }
159
160  
161 1 ▶ unless ($TER_CLS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Term::InKey::TER_CLS) 1 0 1
162  
163 1 my $speed = 9600;
164  
165 1 eval {
166 1 require POSIX;
167 1 import POSIX;
168  
169 1 my $x = new POSIX::Termios;
170 1 POSIX::Termios::getattr($x, 0);
171 1 $speed = $x->getospeed;
172 };
173  
174 1 eval {
175 1 require Term::Cap;
176 1 ▶ my $emu = $ENV{'TERM'} || 'vt100';
Condition50%1/2
$ENV{'TERM'} || 'vt100'
l!l
10
MC/DC0%0/1
$ENV{'TERM'} || 'vt100'
$ENV{'TERM'}
Truth table50%1/2
A
A= $ENV{'TERM'}
Aresult
11
00
177 1 my $term = Term::Cap->Tgetent({'TERM' => $emu,
178 'OSPEED' => $speed});
179 1 $TER_CLS = $term->Tputs('cl');
180 };
181 }
182  
183 1 ▶ unless ($TER_CLS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Term::InKey::TER_CLS) 0 1 1
184 0 &BadClear;
185 0 return;
186 }
187  
188 1 my $desc = select;
189 1 select STDOUT;
190 1 my $pipe = $|;
191 1 $| = 1;
192 1 print $TER_CLS;
193  
194 1 $| = $pipe;
195 1 select $desc;
196 }
197  
198 1 ▶sub ReadPassword {
Subroutine ReadPassword: called CC=14 SCAR=35.2
Pod undocumented
199 1 my ($opt) = @_;
200 1 my $bullet = "*";
201 1 my ($bs, $ws, $nl) = ("\b", " ", "\n");
202 1 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) 1 0 1
203 1 ▶ $bullet = $opt if length($opt) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $opt == 1 1 0 1
204 1 ▶ ($bs, $ws, $nl, $bullet)
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /-\d+/ 0 1 1
205 = () if ($opt =~ /-\d+/);
206 }
207 1 my $save = $|;
208 1 $| = 1;
209 1 my $pass = '';
210 1 for (;;) {
211 72 my $ch = &ReadKey;
212 72 ▶ if ($ch eq "\3") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\3") 0 72 72
213 0 $pass = "";
214 0 $ch = "\n";
215 }
216 72 ▶ if ($ch =~ /[\r\n]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[\r\n]/) 1 71 72
217 1 $| = $save;
218 1 ▶ print $nl if $nl;
Branch: 1 branch
BranchTrueFalseTotal
if $nl 1 0 1
219 1 return $pass;
220 }
221 71 ▶ if ($ch =~ /[\b\x7F]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /[\b\x7F]/) 0 71 71
222 0 ▶ next unless $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless $pass 0 0 0
223 0 chop $pass;
224 0 ▶ print "$bs$ws$bs" if $bs;
Branch: 1 branch
BranchTrueFalseTotal
if $bs 0 0 0
225 0 next;
226 }
227 71 ▶ if ($ch eq "\025") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\25") 0 71 71
228 0 my $len = length($pass);
229 0 ▶ if ($ws) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ws) 0 0 0
230 0 my $res = ($bs x $len) . ($ws x $len) .
231 ($bs x $len);
232 0 print "$res";
233 }
234 0 $pass = '';
235 }
236 71 ▶ if (ord($ch) < 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $ch < 32) 0 71 71
237 0 print "\7";
238 0 next;
239 }
240 71 $pass .= $ch;
241 71 ▶ print $bullet if $bullet;
Branch: 1 branch
BranchTrueFalseTotal
if $bullet 71 0 71
242 }
243 }
244  
245  
246 1 1;
247 __END__
↑ Summary demo.pl »