File Coverage

blib/lib/Biblio/bp/lib/bp-p-debug.pl
Criterion Covered Total %
statement 52 154 33.7
branch 25 82 30.4
condition 0 9 0.0
subroutine 4 8 50.0
pod n/a
total 81 253 32.0


line stmt bran cond sub pod time code
1             #
2             # bibliography package for Perl
3             #
4             # debugging subroutines
5             #
6             # Dana Jacobsen (dana@acm.org)
7             # 14 January 1995
8              
9             ##################
10             #
11             # Debugging code. We handle assertions, panics with variable dumps,
12             # debugging statements (with variable levels), consistency checks, and
13             # variable dumping.
14             #
15              
16             # This is assert.pl by Tom Christiansen, but changed slightly.
17             #
18             # We should use:
19             #
20             # &panic("function called with no arguments") unless defined $foo;
21             #
22             # instead, if that's what you're doing. First, it's quite a bit faster,
23             # and second, because panic can be changed to give a usage message.
24             #
25              
26             sub assert {
27 2 0   2   8905 &panic("Assertion failed: $_[$[]",$@) unless eval $_[$[];
  2     0   5350  
  2         6031  
  0         0  
28             }
29              
30             sub panic {
31 0     0   0 select(STDERR);
32 0         0 print "\nBP ERROR: @_\n";
33              
34 0 0       0 if ($] >= 5.000) {
35 0         0 local($i,$_);
36 0         0 local($p,$f,$l,$s,$h,$w,$a,@a,@sub);
37 0         0 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
38 0         0 @a = @DB'args;
39 0         0 for (@a) {
40 0 0 0     0 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
41 0         0 $_ = sprintf("%s",$_);
42             }
43             else {
44 0         0 s/'/\\'/g;
45 0 0       0 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
46 0         0 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  0         0  
47 0         0 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  0         0  
48             }
49             }
50 0 0       0 $w = $w ? '@ = ' : '$ = ';
51 0 0       0 $a = $h ? '(' . join(', ', @a) . ')' : '';
52 0         0 push(@sub, "$w&$s$a from file $f line $l");
53             }
54 0         0 print join("\n", @sub), "\n";
55             }
56 0         0 &debug_dump('all');
57 0         0 exit 1;
58             }
59              
60              
61             # debugging statement. Use level for increasing severity.
62              
63             sub debugs {
64 3410     3410   6993 local($statement, $level, $mod) = @_;
65 3410         3814 local($debl);
66              
67 3410 50       6342 &panic("debugging called with no arguments") unless defined $statement;
68 3410 50       6271 &panic("debugging called with no level") unless defined $level;
69              
70 3410 100       5905 $debl = (defined $mod) ? $glb_moddebug : $glb_debug;
71              
72             # False
73 3410 50       13947 return if $debl == 0;
74             # True
75 0 0 0     0 return if ( ($debl == 1) && ($opt_default_debug_level > $level) );
76             # some number
77 0 0       0 return if $debl > $level;
78              
79 0         0 local($p,$f,$l,$s,$h,$w);
80 0 0       0 if ($] >= 5.000) {
81 0         0 ($p,$f,$l,$s,$h,$w) = caller(1);
82 0 0       0 $s = '' unless defined $s; # to initialize
83 0         0 $s =~ s/^bib:://;
84             } else {
85             # sigh -- caller is broken in perl 4 apparently, so make the best of it
86 0 0       0 if (defined $mod) {
87 0         0 $s = 'mod ' . $glb_current_fmt;
88             } else {
89 0         0 ($p,$f,$l) = caller;
90 0 0       0 if ($p ne 'bib') {
91 0         0 $s = 'pkg ' . $p;
92             } else {
93 0 0       0 substr($f, 0, rindex($f, '/')+1) = '' if $f =~ /\//;
94 0 0       0 if ($f eq 'bp.pl') {
95 0         0 $s = 'bp';
96             } else {
97             # if it's one of our packages, strip the header/trailer
98 0         0 $f =~ s/^${glb_bpprefix}p-(\w+)\.pl/$1/;
99 0         0 $s = $f;
100             }
101             }
102             }
103             }
104              
105 0         0 local($width) = 16 - &log2($level);
106              
107 0         0 printf STDERR "BPD: (%14s) %s%s\n", $s, ' ' x $width, $statement;
108             }
109              
110             sub log2 {
111 0     0   0 log($_[$[]) / log(2);
112             }
113              
114              
115             #
116             # Consistency checker.
117             #
118             # This is called in various spots throughout the package, usually before and
119             # after opening and closing a file, and when changing formats. This will
120             # probably go with the production version, but just called fewer times. It's
121             # not that long right now, and it doesn't get called often.
122             #
123             # The more assertions and double checks here, the better.
124              
125             sub check_consist {
126 280     280   428 local(@incons);
127 280         345 local(%aainter);
128              
129 280         461 &debugs("Checking bp variable consistency", 8192);
130              
131             # if we're at our maximum debugging level, then spit out copious information
132             # each time we're here.
133 280 50       522 &debug_dump('all') if $glb_debug == 2;
134              
135 280         418 local(@ifiles, @ofiles);
136 280         945 @ifiles = (keys %glb_Irfmt, keys %glb_Ircset, keys %glb_Ifilemap, keys %glb_filelocmap);
137 280         744 @ofiles = (keys %glb_Orfmt, keys %glb_Orcset, keys %glb_Ofilemap);
138              
139 280         434 undef %aainter;
140 280         1485 @ifiles = grep($aainter{$_}++ == 0, @ifiles);
141 280         483 undef %aainter;
142 280         1046 @ofiles = grep($aainter{$_}++ == 0, @ofiles);
143 280         616 undef %aainter;
144              
145 280         500 foreach $file (@ifiles) {
146 266 50       590 push(@incons, "filelocmap{$file} is undefined") unless defined $glb_filelocmap{$file};
147              
148 266 50       497 if (!defined $glb_Irfmt{$file}) {
149 0         0 push(@incons, "Irfmt{$file} is undefined");
150             } else {
151 266 50       1010 push(@incons, "file: $file has no format: " . &okprint($glb_Irfmt{$file}))
152             unless defined $formats{$glb_Irfmt{$file}, 'i_name'};
153             }
154 266 50       484 if (!defined $glb_Ircset{$file}) {
155 0         0 push(@incons, "Ircset{$file} is undefined");
156             } else {
157 266 50       1414 push(@incons, "file: $file has no cset: " . &okprint($glb_Ircset{$file}))
158             unless defined $charsets{$glb_Ircset{$file}, 'i_name'};
159             }
160 266 50       964 push(@incons, "filemap{$file} is undefined") unless defined $glb_Ifilemap{$file};
161             }
162 280         547 foreach $file (@ofiles) {
163 278 50       517 if (!defined $glb_Orfmt{$file}) {
164 0         0 push(@incons, "Orfmt{$file} is undefined");
165             } else {
166 278 50       774 push(@incons, "file: $file has no format: " . &okprint($glb_Orfmt{$file}))
167             unless defined $formats{$glb_Orfmt{$file}, 'i_name'};
168             }
169 278 50       647 if (!defined $glb_Orcset{$file}) {
170 0         0 push(@incons, "Orcset{$file} is undefined");
171             } else {
172 278 50       723 push(@incons, "file: $file has no cset: " . &okprint($glb_Orcset{$file}))
173             unless defined $charsets{$glb_Orcset{$file}, 'i_name'};
174             }
175 278 50       905 push(@incons, "filemap{$file} is undefined") unless defined $glb_Ofilemap{$file};
176             }
177              
178 280         403 local($format, $function);
179 280         513 local(%aaformats);
180 280         2058 foreach $fmts (keys %formats) {
181 12390         25281 ($format, $function) = split(/$;/, $fmts);
182 12390         18018 $aaformats{$format} = 1;
183             }
184 280         1369 foreach $fmt (keys %aaformats) {
185 826 50       3193 if (!defined $formats{$fmt, 'i_name'}) {
    50          
186 0         0 push(@incons, "format $fmt has no i_name");
187             } elsif ( $formats{$fmt, 'i_name'} ne $fmt ) {
188             # This one might be questionable, as we allow this, but it is confusing.
189 0         0 push(@incons, "format $fmt calls itself $formats{$fmt, 'i_name'}");
190             }
191 826 50       1867 push(@incons, "format $fmt has no i_sname") unless defined $formats{$fmt, 'i_sname'};
192             #push(@incons, "format $fmt has no i_charset") unless defined $formats{$fmt, 'i_charset'};
193              
194 826         1247 foreach $f ( @glb_expfuncs ) {
195 8260 50       16164 if (defined $formats{$fmt, $f}) {
196 8260         12836 $function = $formats{$fmt, $f};
197 8260 50       23447 push(@incons, "format $fmt routine $f ($function) isn't defined")
198             unless defined &$function;
199             } else {
200 0         0 push(@incons, "format $fmt missing sub $f");
201             }
202             }
203             }
204              
205             # XXXXX check that each function in charsets, and special_converters
206             # is actually defined.
207              
208 280 50       687 if (@incons) {
209             #@incons = grep($aainter{$_}++ == 0, @incons);
210             #undef %aainter;
211 0         0 print STDERR "---------------- BP package inconsistencies ----------------\n";
212 0         0 print STDERR join("\n", @incons), "\n";
213 0         0 print STDERR "------------------------------------------------------------\n";
214 0         0 return 0;
215             }
216 280         407 undef %aainter;
217 280         549 undef %aaformats;
218 280         1089 1;
219             }
220              
221              
222             sub debug_dump {
223 0     0   0 local($what) = @_;
224 0         0 local($file);
225 0         0 local($name);
226              
227 0 0       0 if ($what =~ /\bconsist\w*\b/) {
228 0         0 &check_consist;
229             } else {
230             # for now, do everything.
231 0         0 local($oldfh);
232 0         0 $oldfh = select(STDERR);
233 0         0 print "---------------- Debugging dump ----------------\n";
234              
235 0         0 print "debug: ", &okprint($glb_debug), " : ", &okprint($glb_moddebug), "\n";
236             # print "prefix: ", &okprint($glb_bpprefix), "\n";
237 0         0 print "Iformat: ", &okprint($glb_Iformat), "\n";
238 0         0 print "Oformat: ", &okprint($glb_Oformat), "\n";
239 0         0 print "current fmt: ", &okprint($glb_current_fmt), "\n";
240 0         0 print "current fh: ", &okprint($glb_current_fh), "\n";
241 0         0 print "Ifilename: ", &okprint($glb_Ifilename), "\n";
242 0         0 print "Ofilename: ", &okprint($glb_Ofilename), "\n";
243 0         0 print "vloc: ", &okprint($glb_vloc), "\n";
244 0         0 print "cvtname: ", &okprint($glb_cvtname), "\n";
245 0         0 printf "warn level %d with %d warnings\n",
246             &okprint($glb_warn_level), &okprint($glb_num_warns);
247 0         0 printf "error level %d with %d errors\n",
248             &okprint($glb_error_level), &okprint($glb_num_errors);
249              
250 0         0 local(@ifnames, @ofnames, @fnames, @cnames);
251              
252 0         0 print "files: file format charset locmap handle\n";
253 0         0 @ifnames = sort keys %glb_Irfmt;
254 0         0 @ofnames = sort keys %glb_Orfmt;
255 0         0 foreach $file (@ifnames) {
256 0         0 printf " < %10s %10s %10s %5s %15s\n", $file,
257             &okprint($glb_Irfmt{$file}), &okprint($glb_Ircset{$file}),
258             &okprint($glb_filelocmap{$file}), &okprint($glb_Ifilemap{$file});
259             }
260 0         0 foreach $file (@ofnames) {
261 0         0 printf " > %10s %10s %10s %10s %15s\n", $file,
262             &okprint($glb_Orfmt{$file}), &okprint($glb_Orcset{$file}),
263             '', &okprint($glb_Ofilemap{$file});
264             }
265 0 0 0     0 if ( (!@ifnames) && (!@ofnames) ) {
266 0         0 print " (none)\n";
267             }
268              
269 0         0 print "formats: name sname charset package\n";
270 0         0 @fnames = sort grep(/i_name$/, keys %formats);
271 0 0       0 if (@fnames) {
272 0         0 foreach $fname (@fnames) {
273 0         0 $name = $formats{$fname};
274 0         0 printf " %10s (%s) %8s %10s\n",
275             &okprint($name),
276             &okprint($formats{$name, 'i_sname'}),
277             &okprint($formats{$name, 'i_charset'}),
278             &okprint($formats{$name, 'i_package'});
279             }
280             } else {
281 0         0 print " (none)\n";
282             }
283              
284 0         0 print "converters:\n";
285 0         0 @cnames = sort grep(/i_name$/, keys %special_converters);
286 0 0       0 if (@cnames) {
287 0         0 foreach $fname (@cnames) {
288 0         0 $name = $special_converters{$fname};
289 0         0 printf " %10s\n",
290             &okprint($name);
291             }
292             } else {
293 0         0 print " (none)\n";
294             }
295              
296 0         0 local($fmts, $csets) = &find_bp_files();
297 0         0 print "found fmts: $fmts\n";
298 0         0 print "found csets: $csets\n";
299              
300             # XXXXX dump filemap
301              
302             # XXXXX dump formats and charsets
303              
304 0         0 print "------------------------------------------------\n";
305 0         0 select($oldfh);
306             }
307             }
308              
309              
310              
311             sub okprint {
312 280 100   280   819 return '' unless defined $_[0];
313 269         774 $_[0];
314             }
315              
316             1;