blib/lib/dbgp-helper/DB/DbgrCommon.pm

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 256 CC 34 SCAR 37.5
CC
34
Cov
0%
enableLogger
110.0
encodeData
90.0
dblog
30.0
CRAP 42.3
« blib/lib/dbgp-helper/DB/MIME/Base64.pm ↑ Summary blib/lib/dbgp-helper/DB/DbgrURI.pm »
1 #
2 # DbgrCommon.pm -- common routines used by all the debugger modues.
3 #
4 # Copyright (c) 2003-2006 ActiveState Software Inc.
5 # All rights reserved.
6 #
7 # Xdebug compatibility, UNIX domain socket support and misc fixes
8 # by Mattia Barbon <mattia@barbon.org>
9 #
10 # This software (the Perl-DBGP package) is covered by the Artistic License
11 # (http://www.opensource.org/licenses/artistic-license.php).
12  
13 package DB::DbgrCommon;
14  
15 use strict;
16 use DB::CGI::Util qw();
17 use DB::MIME::Base64 qw();
18 use File::Basename qw(dirname);
19 use Encode;
20  
21 require Exporter;
22 our @ISA = qw(Exporter);
23 our @EXPORT = qw(
24 encodeData
25 endPropertyTag getCommonType
26 isFloat
27 makeErrorResponse namespaceAttr
28 printWithLength
29 setDefaultOutput
30 xmlHeader xmlEncode xmlAttrEncode
31 nonXmlChar_Decode
32 nonXmlChar_Encode
33  
34 DBP_E_NoError
35 DBP_E_ParseError
36 DBP_E_DuplicateArguments
37 DBP_E_InvalidOption
38 DBP_E_CommandUnimplemented
39 DBP_E_CommandNotAvailable
40 DBP_E_UnrecognizedCommand
41  
42 DBP_E_CantOpenSource
43  
44 DBP_E_BreakpointNotSet
45 DBP_E_BreakpointTypeNotSupported
46 DBP_E_Unbreakable_InvalidCodeLine
47 DBP_E_Unbreakable_EmptyCodeLine
48 DBP_E_BreakpointStateInvalid
49 DBP_E_NoSuchBreakpoint
50 DBP_E_CantSetProperty
51 DBP_E_PropertyEvalError
52  
53 DBP_E_CantGetProperty
54 DBP_E_StackDepthInvalid
55 DBP_E_ContextInvalid
56  
57 DBP_E_EncodingNotSupported
58 DBP_E_InternalException
59 DBP_E_UnknownError
60
61 NV_NAME
62 NV_VALUE
63 NV_NEED_MAIN_LEVEL_EVAL
64 NV_UNSET_FLAG
65  
66 %settings
67 logName
68 setLogFH
69 dblog
70 );
71  
72 # Leave the other parts of the logger unexported.
73  
74 our @EXPORT_OK = qw();
75  
76 # Error codes
77  
78 use constant DBP_E_NoError => 0;
79 use constant DBP_E_ParseError => 1;
80 use constant DBP_E_DuplicateArguments => 2;
81 use constant DBP_E_InvalidOption => 3;
82 use constant DBP_E_CommandUnimplemented => 4;
83 use constant DBP_E_CommandNotAvailable => 5;
84  
85 #todo: add this to protocol
86 use constant DBP_E_UnrecognizedCommand => 6;
87  
88 use constant DBP_E_CantOpenSource => 100;
89  
90 use constant DBP_E_BreakpointNotSet => 200;
91 use constant DBP_E_BreakpointTypeNotSupported => 201;
92 use constant DBP_E_Unbreakable_InvalidCodeLine => 202;
93 use constant DBP_E_Unbreakable_EmptyCodeLine => 203;
94 use constant DBP_E_BreakpointStateInvalid => 204;
95 use constant DBP_E_NoSuchBreakpoint => 205;
96 use constant DBP_E_PropertyEvalError => 206;
97 use constant DBP_E_CantSetProperty => 207;
98  
99 use constant DBP_E_CantGetProperty => 300;
100 use constant DBP_E_StackDepthInvalid => 301;
101 use constant DBP_E_ContextInvalid => 302;
102  
103 use constant DBP_E_EncodingNotSupported => 900;
104 use constant DBP_E_InternalException => 998;
105 use constant DBP_E_UnknownError => 999;
106  
107 use constant NV_NAME => 0;
108 use constant NV_VALUE => 1;
109 use constant NV_NEED_MAIN_LEVEL_EVAL => 2;
110 use constant NV_UNSET_FLAG => 3;
111  
112 # Real simple logging
113  
114 my $doLogging = 0;
115 my $outLogName;
116 my $logFH = undef;
117 my $OUT = undef;
118 our $ldebug;
119 our %settings;
120  
121 # enableLogger(filename || filehandle);
122 # dies if it fails to do logging
123  
124 sub enableLogger {
125 0 ▶ if (!@_) {
Subroutine enableLogger: not called CC=10 SCAR=47.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
126 # Enable $doLogging if we have an existing log file handle
127 0 ▶ if ($logFH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logFH) 0 0 0
128 0 $doLogging = 1;
129 0 printf $logFH "Logging reenabled at %s\n", ('' . localtime(time()));
130 }
131 0 return;
132 }
133 0 $outLogName = shift;
134 0 ▶ return unless $outLogName;
Branch: 1 branch
BranchTrueFalseTotal
unless $outLogName 0 0 0
135 0 ▶ if (ref $outLogName eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $outLogName eq 'GLOB') { } 0 0 0
136 # Make sure we can write to it -- we die if we can't
137 0 printf $outLogName "Logging enabled at %s\n", ('' . localtime(time()));
138 0 $logFH = $outLogName;
139 } else {
140 # Check to see if the filename is urlencoded
141 0 ▶ if ($outLogName =~ /\%u?[a-fA-F0-9]{2}/ && ! -d $outLogName && ! -d dirname($outLogName)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outLogName =~ /\%u?[a-fA-F0-9]{2}/ and not -d $outLogName and not -d dirname($outLogName)) 0 0 0
Condition0%0/6
$outLogName =~ /\%u?[a-fA-F0-9]{2}/ and not -d $outLogName and not -d dirname($outLogName)
!ll&&!rl&&r
000
$outLogName =~ /\%u?[a-fA-F0-9]{2}/ and not -d $outLogName
!ll&&!rl&&r
000
MC/DC0%0/3
$outLogName =~ /\%u?[a-fA-F0-9]{2}/ and not -d $outLogName and not -d dirname($outLogName)
$outLogName =~ /\%u?[a-fA-F0-9]{2}/ not -d $outLogName not -d dirname($outLogName)
Truth table0%0/4
A and not(B) and not(C)
A= $outLogName =~ /\%u?[a-fA-F0-9]{2}/B= not -d $outLogNameC= not -d dirname($outLogName)
ABCresult
0XX0
10X0
1100
1111
142 0 $outLogName = DB::CGI::Util::unescape($outLogName);
143 }
144 0 ▶ if (-d $outLogName) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $outLogName) 0 0 0
145 0 $outLogName =~ s@\\@/@g;
146 0 $outLogName =~ s@/$@@;
147 0 $outLogName .= "/perl5db.log";
148 }
149 0 my $ofh;
150 0 ▶ open $ofh, '>>', $outLogName or die "Failed to open $outLogName for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $ofh, ">>", $outLogName or die "Failed to open $outLogName for writing: $!" 0 0 0
151 0 my $oh = select $ofh;
152 0 $| = 1;
153 0 select $oh;
154 0 printf $ofh "Logging enabled at %s\n", ('' . localtime(time()));
155 0 $logFH = $ofh;
156 }
157 0 $doLogging = 1;
158 }
159  
160 sub disableLogger {
161 0 ▶ printf $logFH "Logging disabled at %s\n", ('' . localtime(time()));
Subroutine disableLogger: not called CC=1 SCAR=6.9
Pod undocumented
162 0 $doLogging = 0;
163 }
164  
165 sub closeLogger {
166 0 ▶ if ($logFH) {
Subroutine closeLogger: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ($logFH) 0 0 0
167 0 printf $logFH "Logging ended at %s\n", ('' . localtime(time()));
168 0 close ($logFH);
169 0 $logFH = undef;
170 }
171 0 $doLogging = 0;
172 }
173  
174 sub dblog {
175 0 ▶ if (@_ && $doLogging && $logFH) {
Subroutine dblog: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $doLogging and $logFH) 0 0 0
Condition0%0/6
@_ and $doLogging and $logFH
!ll&&!rl&&r
000
@_ and $doLogging
!ll&&!rl&&r
000
MC/DC0%0/3
@_ and $doLogging and $logFH
@_ $doLogging $logFH
Truth table0%0/4
A and B and C
A= @_B= $doLoggingC= $logFH
ABCresult
0XX0
10X0
1100
1111
176 0 local *DB::sub; # allows using dblog() inside DB::sub
177 0 print $logFH map { wide_char_safe_encode($_)} @_;
178 # End with a newline
179 0 ▶ print $logFH "\n" unless substr($_[-1], -1, 1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($_[-1], -1, 1) eq "\n" 0 0 0
180 }
181 }
182  
183 sub setLogFH {
184 0 ▶ $logFH = $_[0];
Subroutine setLogFH: not called CC=1 SCAR=6.9
Pod undocumented
185 }
186  
187 sub logName {
188 0 ▶ $outLogName;
Subroutine logName: not called CC=1 SCAR=6.9
Pod undocumented
189 }
190  
191 sub setDefaultOutput {
192 0 ▶ $OUT = shift;
Subroutine setDefaultOutput: not called CC=1 SCAR=6.9
Pod undocumented
193 }
194  
195 sub encodeData($$) {
196 0 ▶ my ($str, $encoding) = @_;
Subroutine encodeData: not called CC=9 SCAR=45.0
Pod undocumented
197 0 my $finalStr;
198 0 $finalStr = $str;
199 0 local $@;
200 0 eval {
201 0 ▶ if ($encoding eq 'none' || $encoding eq 'binary') {
Branch: 3 branches
BranchTrueFalseTotal
if ($encoding eq 'none' or $encoding eq 'binary') { } 0 0 0
elsif ($encoding eq 'urlescape') { } 0 0 0
elsif ($encoding eq 'base64') { } 0 0 0
Condition0%0/3
$encoding eq "none" or $encoding eq "binary"
l!l&&r!l&&!r
000
MC/DC0%0/2
$encoding eq "none" or $encoding eq "binary"
$encoding eq "none" $encoding eq "binary"
Truth table0%0/3
A or B
A= $encoding eq "none"B= $encoding eq "binary"
ABresult
1X1
011
000
202 0 $finalStr = $str;
203 } elsif ($encoding eq 'urlescape') {
204 0 $finalStr = DB::CGI::Util::escape($str);
205 } elsif ($encoding eq 'base64') {
206 0 $finalStr = DB::MIME::Base64::encode_base64($str);
207 } else {
208 0 ▶ dblog("Converting $str with unknown encoding of $encoding\n") if $ldebug;
Branch: 1 branch
BranchTrueFalseTotal
if $ldebug 0 0 0
209 0 $finalStr = $str;
210 }
211 };
212 0 ▶ if ($ldebug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ldebug) 0 0 0
213 0 my $str = safe_dump($str);
214 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
215 0 ▶ $str = (substr($str, 0, 100) . '...') if length($str) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $str > 100 0 0 0
216 0 dblog("encodeData('$str') => [$@]\n");
217 }
218 }
219 0 return $finalStr;
220 }
221  
222 sub endPropertyTag($$) {
223 0 ▶ my ($encVal, $encoding) = @_;
Subroutine endPropertyTag: not called CC=3 SCAR=24.8
Pod undocumented
224 0 my $res;
225 0 ▶ if (defined $encVal && length $encVal > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encVal and length $encVal > 0) { } 0 0 0
Condition0%0/3
defined $encVal and length $encVal > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encVal and length $encVal > 0
defined $encVal length $encVal > 0
Truth table0%0/3
A and B
A= defined $encValB= length $encVal > 0
ABresult
0X0
100
111
226 # $res .= sprintf(qq(>%s</property>\n), ($encVal));
227 0 $res = sprintf(qq(><![CDATA[%s]]></property>\n), ($encVal));
228 } else {
229 0 $res = qq(/>\n);
230 }
231 0 return $res;
232 }
233  
234 # If other names are used here, be sure to update the
235 # list in sub DB::emitTypeMapInfo
236  
237 sub getCommonType($) {
238 0 ▶ my ($val) = @_;
Subroutine getCommonType: not called CC=5 SCAR=34.0
Pod undocumented
239 0 ▶ if (! defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
240 0 return 'undef';
241 }
242 0 my $r;
243 0 ▶ if ($r = ref $val) {
Branch: 3 branches
BranchTrueFalseTotal
if ($r = ref $val) { } 0 0 0
elsif (&isInt($val)) { } 0 0 0
elsif (&isFloat($val)) { } 0 0 0
244 0 $r =~ s/\(0x\w+\)//;
245 0 return $r;
246 } elsif (isInt($val)) {
247 0 return 'int';
248 } elsif (isFloat($val)) {
249 0 return 'float';
250 } else {
251 0 return 'string';
252 }
253 }
254  
255 sub isInt($) {
256 0 ▶ my ($val) = @_;
Subroutine isInt: not called CC=1 SCAR=6.9
Pod undocumented
257 0 return $val =~ /^
258 [-+]? # leading sign always optional
259 \d+ # required digits
260 $/x;
261 }
262  
263 sub isFloat($) {
264 0 ▶ my ($val) = @_;
Subroutine isFloat: not called CC=1 SCAR=6.9
Pod undocumented
265 0 return $val =~ /^
266 (?:
267 [-+]? # leading sign always optional
268 (?:
269 \d+ # required base
270 (?:\.\d*)? # optional fractional part
271 )|(?:
272 \. # required decimal pt and number
273 \d+
274 )
275 )
276 (?:[eE][-+]?\d+)? # exponent always optional
277 $/x;
278 }
279  
280 sub makeErrorResponse($$$$) {
281 0 ▶ my ($cmd, $transactionID, $code, $error) = @_;
Subroutine makeErrorResponse: not called CC=1 SCAR=6.9
Pod undocumented
282 0 printWithLength(sprintf
283 (qq(%s\n<response %s command="%s"
284 transaction_id="%s" ><error code="%d" apperr="4">
285 <message>%s</message>
286 </error></response>),
287 xmlHeader(),
288 namespaceAttr(),
289 $cmd,
290 $transactionID,
291 $code,
292 $error));
293 0 return undef;
294 }
295  
296 sub namespaceAttr() {
297 0 ▶ return 'xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug"';
Subroutine namespaceAttr: not called CC=1 SCAR=6.9
Pod undocumented
298 }
299  
300 sub printWithLength {
301 0 ▶ my $mainArg = shift;
Subroutine printWithLength: not called CC=2 SCAR=17.9
Pod undocumented
302 0 my $argLen = length($mainArg);
303 0 my $finalStr = sprintf("%d\0%s\0", $argLen, $mainArg);
304 #### local $SIG{__WARN__} = sub {
305 #### my $msg = shift;
306 #### dblog("**************** Warn hook: {$msg}");
307 #### if ($msg !~ /Wide character in print at .*DbgrCommon\.pm/s) {
308 #### warn $msg;
309 #### dblog("Don't have wide char");
310 #### }
311 #### };
312 # dblog $finalStr;
313 0 ▶ $OUT = \*STDOUT unless $OUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $OUT 0 0 0
314 # brokn connection is handled one way or the other the next time
315 # the main look is re-entered
316 0 local $SIG{PIPE} = 'IGNORE';
317 0 print $OUT $finalStr;
318 }
319
320 # Like xmlEncode, but escape the quotes
321  
322 sub xmlAttrEncode {
323 0 ▶ my ($str) = @_;
Subroutine xmlAttrEncode: not called CC=1 SCAR=6.9
Pod undocumented
324 0 $str = xmlEncode($str);
325 0 $str =~ s/([\'\"])/'&#' . ord($1) . ';'/eg;
326 0 return $str;
327 }
328  
329 # This routine *cannot* do logging until dblog checks its caller
330 # before calling wide_char_safe_encode
331  
332 sub wide_char_safe_encode {
333 0 ▶ my ($str) = @_;
Subroutine wide_char_safe_encode: not called CC=3 SCAR=24.8
Pod undocumented
334 0 require bytes;
335 0 ▶ if (bytes::length($str) == length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&bytes::length($str) == length $str) 0 0 0
336 # don't utf-8-encode a string in latin* or cp1251
337 # just pass it to Komodo as is.
338 0 return $str;
339 }
340 0 my $enc_str = eval { Encode::encode_utf8($str); };
341 0 ▶ return $enc_str || $str;
Condition0%0/3
$enc_str || $str
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc_str || $str
$enc_str $str
Truth table0%0/3
A || B
A= $enc_strB= $str
ABresult
1X1
011
000
342 }
343  
344 sub safe_dump {
345 0 ▶ my $str = shift;
Subroutine safe_dump: not called CC=1 SCAR=6.9
Pod undocumented
346 0 $str =~ s/([^\x09\x0a\0x0d\x20-\x7f])/sprintf('\\x%02x', ord($1))/eg;
347 0 $str;
348 }
349  
350  
351 sub wide_char_safe_decode {
352 0 ▶ my ($str) = @_;
Subroutine wide_char_safe_decode: not called CC=3 SCAR=24.8
Pod undocumented
353 0 ▶ if ($str !~ /[^\x00-\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /[^\x00-\x7f]/) 0 0 0
354 0 return $str;
355 }
356  
357 # Not all strings are utf8 strings, but if a string looks like
358 # a UTF-8 string, assume that it is, and return its actual value.
359 #
360 # The problem is that strings coming from the program might be
361 # utf-8 or latin1, but strings coming from Mozilla are
362 # always latin1-encoded.
363 #
364 # The prototype of this function changed from ($) to ($;$)
365 # at version 5.8.3RC1
366 # Use an indirect function call to bypass prototype checking at
367 # compile-time. Earlier versions of Perl will process only the first arg.
368  
369 0 local $@;
370 0 my $fn_ref = \&Encode::decode_utf8;
371 0 my $enc_str = eval { $fn_ref->($str, Encode::FB_CROAK); };
372 0 ▶ return $enc_str || $str;
Condition0%0/3
$enc_str || $str
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc_str || $str
$enc_str $str
Truth table0%0/3
A || B
A= $enc_strB= $str
ABresult
1X1
011
000
373 }
374  
375 sub nonXmlChar_Encode {
376 0 ▶ my ($str) = @_;
Subroutine nonXmlChar_Encode: not called CC=1 SCAR=6.9
Pod undocumented
377 0 return wide_char_safe_encode($str);
378 }
379  
380 sub nonXmlChar_Decode {
381 0 ▶ my ($str) = @_;
Subroutine nonXmlChar_Decode: not called CC=1 SCAR=6.9
Pod undocumented
382 0 return wide_char_safe_decode($str);
383 }
384  
385 sub xmlEncode {
386 0 ▶ my ($str) = @_;
Subroutine xmlEncode: not called CC=1 SCAR=6.9
Pod undocumented
387 0 $str =~ s/\&/&amp;/g;
388 0 $str =~ s/</&lt;/g;
389 0 $str =~ s/>/&gt;/g;
390 0 $str =~ s/([\x00-\x08\x0b\x0c\x0e-\x1f])/'&#' . ord($1) . ';'/eg;
391 0 return wide_char_safe_encode($str);
392 }
393  
394 sub xmlHeader() {
395 0 ▶ return '<?xml version="1.0" encoding="' . $settings{encoding}->[0] . '" ?>';
Subroutine xmlHeader: not called CC=1 SCAR=6.9
Pod undocumented
396 }
397  
398 1;
« blib/lib/dbgp-helper/DB/MIME/Base64.pm ↑ Summary blib/lib/dbgp-helper/DB/DbgrURI.pm »