MIBLoadOrder.pm

filter: Statementstmt 8.0% 30 / 371 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 16.6% 3 / 18 Podpod 16.6% 1 / 6 totaltotal 5.8% 34 / 582 CC 111 SCAR 64.3
CC
111
Cov
5%
mib_load
1056.0
_parse_mib_file
1056.0
_scan_file_list
462.0
CRAP 621.4
↑ Summary
1 # Perl Module
2 #
3 # Purpose: Determine load order of a group of MIB files
4 #
5 # Written: 9/2/2003, sparsons@cpan.org
6 #
7 # Look at end of file for all POD
8 #
9 #
10 # 8/5/2004 v1.0.0 (sparsons)
11 # - changed the DEFINITION and IMPORT parser
12 # - grab DEF and IMPORT as chunks, then parse the chunck
13 # - took out -singlefile option
14 # - if a DEF is found in more than one file, function errors out
15 #
16 # 8/11/2004 v1.0.1 (sparsons)
17 # - changed MIB filehandle to be $_MIB, reason being install failure
18 # on some platforms, MIB filehandle treated as bareword
19 #
20 # 8/26/2004 v1.1.0 (sparsons)
21 # - do not error when DEFINITION found in multiple files
22 # just do warning, keep first file
23 # - account for multiple BEGIN/END blocks
24 # - allow for no extensions --ext noExt
25 #
26  
27  
28 package Net::Dev::Tools::MIB::MIBLoadOrder;
29  
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶ use Exporter();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 our $VERSION = '1.1.0';
35 1 our @ISA = qw(Exporter);
36  
37 1 our @EXPORT = qw(
38 mib_load
39 mib_load_order
40 mib_load_definitions
41 mib_load_trace
42 mib_load_warnings
43 mib_load_error
44 );
45  
46 1 our @EXPORT_OK = qw();
47  
48 }
49  
50 1 our %ARGS;
51 1 our $ERROR;
52 1 our $WARNING;
53 1 our @WARNINGS;
54 1 our %DEFINITIONS;
55 1 our @STD_MIB_FILES;
56 1 our @ENT_MIB_FILES;
57 1 our %FILE_EXT;
58 1 our @WEIGHTS_SORTED;
59 1 our @LOAD_ORDER;
60 1 our $DEBUG = 0;
61 1 our $ORDER_LOOPS = 0;
62 1 our $_TRACK_FLAG = 0;
63 1 our $_TRACK_INDEX = 0;
64 1 our %TRACK_HASH = ();
65 1 our $_TYPE ='';
66 1 our $_PRIORITY = 0;
67 1 our $_SINGLE = 1;
68 1 our @_EXCLUDE = ();
69  
70  
71  
72  
73 ##############################################################################
74 #
75 # Functions
76 #
77 ##############################################################################
78 #
79 #
80 0 ▶sub mib_load {
Subroutine mib_load: not called CC=32 SCAR=69.6
Pod documented
81 0 %ARGS = @_;
82 0 %FILE_EXT = ();
83 0 %DEFINITIONS = ();
84 0 $ERROR = undef;
85 0 @WARNINGS = ();
86 0 $WARNING = undef;
87 0 @STD_MIB_FILES = ();
88 0 @ENT_MIB_FILES = ();
89 0 @WEIGHTS_SORTED = ();
90 0 $ORDER_LOOPS = 0;
91  
92 0 my ($_arg, $_ext, $_file,
93 $_parsed, $_sorted, $_def, $_loop,
94 );
95 0 my %_extensions;
96 0 my $_files_found = undef;
97 0 my $_max_loops = '1000';
98  
99 # check arguments
100 0 foreach $_arg (keys %ARGS) {
101 0 ▶ if ($_arg =~ /^-?StandardMIBs$/i) {next;}
Branch: 8 branches
BranchTrueFalseTotal
if ($_arg =~ /^-?StandardMIBs$/i) { } 0 0 0
elsif ($_arg =~ /^-?EnterpriseMIBs$/i) { } 0 0 0
elsif ($_arg =~ /^-?Extensions$/i) { } 0 0 0
elsif ($_arg =~ /^-?exclude$/i) { } 0 0 0
elsif ($_arg =~ /^-?track$/i) { } 0 0 0
elsif ($_arg =~ /^-?prioritize$/i) { } 0 0 0
elsif ($_arg =~ /^-?maxloops$/i) { } 0 0 0
elsif ($_arg =~ /^-?debug$/i) { } 0 0 0
102 0 elsif ($_arg =~ /^-?EnterpriseMIBs$/i) {next;}
103 0 elsif ($_arg =~ /^-?Extensions$/i) {next;}
104 0 elsif ($_arg =~ /^-?exclude$/i) {next;}
105 0 elsif ($_arg =~ /^-?track$/i) {$_TRACK_FLAG = delete($ARGS{$_arg});}
106 0 elsif ($_arg =~ /^-?prioritize$/i) {$_PRIORITY = delete($ARGS{$_arg});}
107 0 elsif ($_arg =~ /^-?maxloops$/i) {$_max_loops = delete($ARGS{$_arg});}
108 0 elsif ($_arg =~ /^-?debug$/i) {$DEBUG = delete($ARGS{$_arg});}
109 else {
110 0 $ERROR = "unsupported argument: [$_arg]";
111 0 ▶ return wantarray ? (undef, undef, $ERROR) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
112 }
113 }
114  
115 # see what extensions to check for
116 0 ▶ if (defined($ARGS{Extensions})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Net::Dev::Tools::MIB::MIBLoadOrder::ARGS{'Extensions'}) { } 0 0 0
117 0 foreach $_ext ( @{$ARGS{Extensions}} ) {
118 0 $FILE_EXT{$_ext} = 1;
119 0 _myprintf("File Extension check: %s [%s]\n", $_ext, $FILE_EXT{$_ext});
120 }
121 }
122 else {
123 0 $FILE_EXT{mib} = 1;
124 0 _myprintf("File Extension check: %s [%s], default\n", 'mib', $FILE_EXT{mib});
125 }
126  
127 # see what dirs and/or files are given
128 0 _myprintf("Examine StandardMIBs list\n");
129 0 ▶ if (defined($ARGS{StandardMIBs})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Net::Dev::Tools::MIB::MIBLoadOrder::ARGS{"StandardMIBs"}) 0 0 0
130 0 foreach $_file ( @{$ARGS{StandardMIBs}} ) {
131 0 $_files_found = _scan_file_list('Standard', $_file);
132 0 ▶ if ($_files_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_files_found) { } 0 0 0
133 0 _myprintf("Files found: %s contains %s files\n", $_file, $_files_found);
134 }
135 else {
136 0 ▶ return wantarray ? (undef, undef, $ERROR) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
137 }
138 }
139 }
140 0 _myprintf("Examine EnterpriseMIBs list\n");
141 0 ▶ if (defined($ARGS{EnterpriseMIBs})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Net::Dev::Tools::MIB::MIBLoadOrder::ARGS{"EnterpriseMIBs"}) 0 0 0
142 0 foreach $_file ( @{$ARGS{EnterpriseMIBs}} ) {
143 0 $_files_found = _scan_file_list('Enterprise', $_file);
144 0 ▶ if ($_files_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_files_found) { } 0 0 0
145 0 _myprintf("Files found: %s contains %s files\n", $_file, $_files_found);
146 }
147 else {
148 0 ▶ return wantarray ? (undef, undef, $ERROR) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
149 }
150 }
151 }
152  
153 # parse the files
154 0 foreach $_file ('TYPE::STD', @STD_MIB_FILES, 'TYPE::ENT', @ENT_MIB_FILES ) {
155 # determine type
156 0 ▶ if ($_file eq "TYPE::STD") {$_TYPE = 'Standard'; next;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_file eq "TYPE::STD") 0 0 0
157 0 ▶ if ($_file eq "TYPE::ENT") {$_TYPE = 'Enterprise'; next;}
Branch: 1 branch
BranchTrueFalseTotal
if ($_file eq "TYPE::ENT") 0 0 0
158  
159 0 $_parsed = _parse_mib_file($_file);
160 0 ▶ unless ($_parsed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_parsed) 0 0 0
161 0 ▶ return wantarray ? (undef, undef, $ERROR) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
162 }
163 }
164  
165 # compute the weights for the definitions
166 0 _compute_definition_weights();
167  
168 # prioritize
169 # look at enterprise weights, make all standard weights higher
170 0 ▶ if ($_PRIORITY) { _prioritize(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Dev::Tools::MIB::MIBLoadOrder::_PRIORITY) 0 0 0
171  
172 # find the warnings
173 0 _find_warnings();
174  
175  
176 # sort weights and sort definitions until the
177 # &_sort_definitions() returns true
178  
179 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$_sorted or do { do { ++$ORDER_LOOPS; if ($ORDER_LOOPS == $_max_loops) { $ERROR = "max loops $_max_loops excedded"; return wantarray ? (undef, undef, $ERROR) : undef; } ; foreach $_def (sort keys %DEFINITIONS) { _track_it("$_def", "SORTING Weights and DEFINITIONS, loop $_loop"); } ; _sort_weights(); $_sorted = _sort_definitions() } } 0 0 0
180 0 ++$ORDER_LOOPS;
181  
182 0 ▶ if ($ORDER_LOOPS == $_max_loops) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ORDER_LOOPS == $_max_loops) 0 0 0
183 0 $ERROR = "max loops $_max_loops excedded";
184 0 ▶ return wantarray ? (undef, undef, $ERROR) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
185 }
186  
187 0 foreach $_def (sort keys %DEFINITIONS) {
188 0 _track_it("$_def", "SORTING Weights and DEFINITIONS, loop $_loop");
189 }
190 # sort the values for the weight
191 0 _sort_weights();
192  
193 # from sorted weights, make list
194 0 $_sorted = _sort_definitions();
195 } until $_sorted;
196  
197 0 ▶ return wantarray ? (\@LOAD_ORDER, scalar(@WARNINGS), $ERROR) : \@LOAD_ORDER;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
198 } # end sub new
199  
200  
201 ##############################################################################
202 #
203 # Return Reference Functions
204 #
205 ##############################################################################
206 #
207 # functions to return variables or references to variables
208  
209 0 ▶sub mib_load_order { return(\@LOAD_ORDER); }
Subroutine mib_load_order: not called CC=1 SCAR=6.9
Pod undocumented
210 0 ▶sub mib_load_definitions { return(\%DEFINITIONS); }
Subroutine mib_load_definitions: not called CC=1 SCAR=6.9
Pod undocumented
211 0 ▶sub mib_load_trace { return(\%TRACK_HASH); }
Subroutine mib_load_trace: not called CC=1 SCAR=6.9
Pod undocumented
212 0 ▶sub mib_load_warnings { return(\@WARNINGS); }
Subroutine mib_load_warnings: not called CC=1 SCAR=6.9
Pod undocumented
213 0 ▶sub mib_load_error { return($ERROR); }
Subroutine mib_load_error: not called CC=1 SCAR=6.9
Pod undocumented
214  
215  
216  
217 ##############################################################################
218 #
219 # Private Functions
220 #
221 ##############################################################################
222 #
223 # Purpose: function to scan file list
224 #
225 # Arguments:
226 # $_[0] = Source (Standard or Enterprise)
227 # $_[1] = file
228 #
229 # Return
230 # Integer indicating how many files found
231 # or undef on error
232 #
233 0 ▶sub _scan_file_list {
Subroutine _scan_file_list: not called CC=21 SCAR=61.4
234  
235 0 my $_tag = $_[0];
236 0 my $_chk_file = $_[1];
237 0 my $_match = undef;
238 0 my ($_f, $_chk_ext, $_fullname, $_separator);
239 0 my @_mib_files = ();
240  
241 0 $ERROR = "$_chk_file: no files";
242  
243 0 _myprintf(" Examining %s list item: [%s]\n", $_tag, $_chk_file);
244 # see what our dir separator is
245 # store it and strip it off the end
246 #
247 0 ▶ if ($_chk_file =~/\//) {$_separator = '/'; $_chk_file =~ s/\/$//;}
Branch: 2 branches
BranchTrueFalseTotal
if ($_chk_file =~ m[/]) { } 0 0 0
elsif ($_chk_file =~ /\\/) { } 0 0 0
248 0 elsif ($_chk_file =~/\\/) {$_separator = '\\'; $_chk_file =~ s/\\$//;}
249  
250 0 $_chk_file =~ s/\/$//;
251 # see if its a directory
252 0 ▶ if (-e $_chk_file and -d $_chk_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $_chk_file and -d $_chk_file) { } 0 0 0
elsif (-e $_chk_file and -f $_chk_file) { } 0 0 0
Condition0%0/3
-e $_chk_file and -d $_chk_file
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_chk_file and -d $_chk_file
-e $_chk_file -d $_chk_file
Truth table0%0/3
A and B
A= -e $_chk_fileB= -d $_chk_file
ABresult
0X0
100
111
253 0 _myprintf(" Determined %s list item: [%s] to be dir\n",
254 $_tag, $_chk_file
255 ) ;
256 0 ▶ if (!-r $_chk_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $_chk_file) 0 0 0
257 0 $ERROR = "$_tag: $_chk_file not readable";
258 0 _myprintf(" $_tag: $_chk_file not readable");
259 0 return(undef);
260 }
261 # read the files in the directory
262 0 opendir(DIR, $_chk_file);
263 0 ▶ while ($_f = readdir(DIR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_f = readdir DIR)) 0 0 0
264 0 ▶ next if $_f =~ /^\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_f =~ /^\.$/ 0 0 0
265 0 ▶ next if $_f =~ /^\..$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_f =~ /^\..$/ 0 0 0
266 # check the file extension
267 0 ▶ if ($_f =~ /\.(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_f =~ /\.(.+)$/) { } 0 0 0
elsif ($FILE_EXT{'noExt'}) { } 0 0 0
268 0 $_chk_ext = $1;
269 0 ▶ if (defined($FILE_EXT{$_chk_ext})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $FILE_EXT{$_chk_ext}) 0 0 0
270 0 $_fullname = sprintf("%s%s%s", $_chk_file, $_separator, $_f);
271 0 $_match++;
272 0 push(@_mib_files, $_fullname);
273 0 _myprintf(" MIB file: %s: found: [%s] [%s] \n",
274 $_tag, $_f, $_fullname
275 ) ;
276 }
277 }
278 # allow for no extensions
279 elsif ($FILE_EXT{'noExt'}) {
280 0 $_fullname = sprintf("%s%s%s", $_chk_file, $_separator, $_f);
281 0 $_match++;
282 0 push(@_mib_files, $_fullname);
283 0 _myprintf(" MIB file: %s: found: noExt [%s] [%s] \n",
284 $_tag, $_f, $_fullname
285 ) ;
286 }
287 }
288 0 closedir(DIR);
289 }
290 # see if its a file
291 ▶ elsif (-e $_chk_file and -f $_chk_file) {
Condition0%0/3
-e $_chk_file and -f $_chk_file
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_chk_file and -f $_chk_file
-e $_chk_file -f $_chk_file
Truth table0%0/3
A and B
A= -e $_chk_fileB= -f $_chk_file
ABresult
0X0
100
111
292 0 _myprintf(" Determined %s list item: [%s] to be file\n", $_tag, $_chk_file) ;
293 0 ▶ if (!-r $_chk_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $_chk_file) 0 0 0
294 0 $ERROR = "$_tag: $_chk_file not readable";
295 0 return(undef);
296 }
297 # check the file extension
298 0 ▶ if ($_chk_file =~ /\w+\.(.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_chk_file =~ /\w+\.(.+)$/) { } 0 0 0
elsif ($FILE_EXT{'noExt'}) { } 0 0 0
299 0 $_chk_ext = $1;
300 0 ▶ if (defined($FILE_EXT{$_chk_ext})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $FILE_EXT{$_chk_ext}) 0 0 0
301 0 _myprintf(" MIB file: %s: found: %s\n", $_tag, $_chk_file) ;
302 0 $_match++;
303 0 push(@_mib_files, $_chk_file);
304 }
305 }
306 elsif ($FILE_EXT{'noExt'}) {
307 0 _myprintf(" MIB file: %s: found: noExt %s\n", $_tag, $_chk_file);
308 0 $_match++;
309 0 push(@_mib_files, $_chk_file);
310 }
311 }
312 0 ▶ if (scalar(@_mib_files)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_mib_files) 0 0 0
313 0 ▶ if ($_tag =~ /Standard/) {push(@STD_MIB_FILES, @_mib_files);}
Branch: 1 branch
BranchTrueFalseTotal
if ($_tag =~ /Standard/) 0 0 0
314 0 ▶ if ($_tag =~ /Enterprise/) {push(@ENT_MIB_FILES, @_mib_files);}
Branch: 1 branch
BranchTrueFalseTotal
if ($_tag =~ /Enterprise/) 0 0 0
315 }
316  
317 0 return($_match);
318 } # end _scan_file_list
319  
320 #
321 #.............................................................................
322 #
323 # function to parse the MIB file
324 # populate global hashes
325 #
326 # Arguments
327 # $_[0] = file
328 #
329 # Return
330 # (success, error)
331 # success = 1 or undef
332 #
333 0 ▶sub _parse_mib_file {
Subroutine _parse_mib_file: not called CC=32 SCAR=69.6
334 0 my ($_def, $_import, $_lastline);
335 0 my $_begin_count = 0;
336 0 my $_definition_count = 0;
337 0 my $_definition = undef;
338 0 my $_import_flag = 0;
339 0 my $_end_count = 0;
340 0 my $_excl;
341 0 my $_match = 0;
342 0 my %_DEF = ();
343 0 my $_MIB;
344  
345 0 $ERROR = "$_[0]: failed to parse mib file";
346  
347 0 _myprintf("PARSING: %s\n", $_[0]) ;
348  
349 # see if we are excluding, check filename for pattern
350 0 ▶ if ( defined($ARGS{exclude})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ARGS{"exclude"}) 0 0 0
351 0 foreach $_excl ( @{$ARGS{exclude}} ) {
352 0 ▶ if ($_[0] =~ /$_excl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /$_excl/) 0 0 0
353 0 $_match++;
354 0 $WARNING = "exclusion match [$_excl] on [$_[0]]";
355 0 push(@WARNINGS, ['_EXCL_', "$WARNING"]);
356 0 _myprintf("Exclusion: %s\n", $WARNING);
357 }
358 }
359 0 ▶ return(1) if $_match;
Branch: 1 branch
BranchTrueFalseTotal
if $_match 0 0 0
360 }
361  
362 # open and parse the file
363 0 undef $_MIB;
364 0 ▶ open($_MIB, "$_[0]") || return (undef, "can not open $_[0]: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $_MIB, "$_[0]" or return undef, "can not open $_[0]: $!" 0 0 0
365 0 ▶ while(<$_MIB>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $_MIB)) 0 0 0
366 0 ▶ if (/^$/) {next;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^$/) 0 0 0
367 0 ▶ if (/^\s+$/) {next;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s+$/) 0 0 0
368 0 ▶ if (/^--/) {next;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^--/) 0 0 0
369 0 ▶ if (/^\s+--/) {next;}
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s+--/) 0 0 0
370
371
372 0 ▶ $_begin_count++ if /\bBEGIN\s/;
Branch: 1 branch
BranchTrueFalseTotal
if /\bBEGIN\s/ 0 0 0
373 0 ▶ $_end_count++ if /^END\b/;
Branch: 1 branch
BranchTrueFalseTotal
if /^END\b/ 0 0 0
374 # parse out definitions
375 0 ▶ if (/DEFINITIONS/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/DEFINITIONS/) 0 0 0
376 0 ▶ if (/\b([A-Z][\-?\w]{0,63})\b\s+DEFINITIONS\s+::=\s+BEGIN/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/\b([A-Z][\-?\w]{0,63})\b\s+DEFINITIONS\s+::=\s+BEGIN/) { } 0 0 0
377 0 {$_def = $1;}
378 else {
379 0 ▶ if (join(' ', $_lastline, $_) =~ /\b([A-Z][\-?\w]{0,63})\b\s+DEFINITIONS\s+::=\s+BEGIN/m)
Branch: 1 branch
BranchTrueFalseTotal
if (join(" ", $_lastline, $_) =~ /\b([A-Z][\-?\w]{0,63})\b\s+DEFINITIONS\s+::=\s+BEGIN/m) 0 0 0
380 0 {$_def= $1;}
381 }
382 0 ▶ unless ($_def =~ /--/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_def =~ /--/) 0 0 0
383 0 $_definition = $_def;
384 0 $_definition_count++;
385 0 $_import_flag = 0;
386 0 _myprintf(" DEFINITION parsed: line %-4s count: %3s %s [%s]\n",
387 $., $_definition_count, $_TYPE, $_definition
388 );
389 0 _track_it("$_definition", "defined in [$_[0]], line: $. type: $_TYPE");
390 # see if DEF already known in other file
391 0 ▶ if (defined($DEFINITIONS{$_definition}{file})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DEFINITIONS{$_definition}{"file"}) 0 0 0
392 $WARNING = sprintf("DEF: %s previously defined in: %s",
393 $_definition, $DEFINITIONS{$_definition}{file}
394 0 );
395 0 push(@WARNINGS, [$_[0], $WARNING] );
396 0 _myprintf(" %s\n", $WARNING);
397 0 return(1);
398 }
399  
400 # set warning if more than one DEF per file
401 0 ▶ if ($_definition_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_definition_count > 1) 0 0 0
402 0 $WARNING = "multiple DEFINITIONs in $_[0]";
403 0 push(@WARNINGS, ['_FILE_', $WARNING] );
404 }
405 0 $DEFINITIONS{$_definition}{file} = $_[0];
406 0 $DEFINITIONS{$_definition}{type} = $_TYPE;
407  
408 0 _track_it("$_definition", "adding $_TYPE [$_[0]] to file list");
409  
410 }
411 }
412 # we only want to extract IMPORT chunk
413 # so detect when the construct is on the current lines
414 0 ▶ if (/IMPORTS\s+/) {$_import_flag = 1;}
Branch: 1 branch
BranchTrueFalseTotal
if (/IMPORTS\s+/) 0 0 0
415  
416 # find end of DEFINITIONS
417 0 ▶ if (/^END\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^END\s*$/) 0 0 0
418 # make sure we have hash entry for those with no imports
419 0 ▶ if (!exists($_DEF{$_definition})) {$_DEF{$_definition} = '';}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_DEF{$_definition}) 0 0 0
420 0 _myprintf(" END parsed: %s begins %s ends\n", $_begin_count, $_end_count);
421 0 ▶ if ($_begin_count == $_end_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_begin_count == $_end_count) 0 0 0
422 0 _myprintf(" END DEFINITION: %s begins %s ends %s\n",
423 $_definition,$_begin_count, $_end_count
424 );
425 0 $_definition = undef;
426 0 $_import_flag = 0;
427 }
428 }
429  
430 # $_DEF{$_definition} will be a chunk that has the IMPORTS
431 0 ▶ if ($_definition && $_import_flag == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_definition and $_import_flag == 1) 0 0 0
Condition0%0/3
$_definition and $_import_flag == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$_definition and $_import_flag == 1
$_definition $_import_flag == 1
Truth table0%0/3
A and B
A= $_definitionB= $_import_flag == 1
ABresult
0X0
100
111
432 0 $_DEF{$_definition} = join('', $_DEF{$_definition}, $_);
433 0 ▶ $_import_flag = 0 if /;/;
Branch: 1 branch
BranchTrueFalseTotal
if /;/ 0 0 0
434 }
435  
436 0 $_lastline = $_;
437 }
438 0 close($_MIB);
439  
440 # if no definition found, issue warning
441 0 ▶ if ($_definition_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_definition_count == 0) 0 0 0
442 0 $WARNING = sprintf("No DEFINITION parsed in: [%s]", $_[0]);
443 0 push(@WARNINGS, ['_FILE_', $WARNING] );
444 0 return(1);
445 }
446  
447 # get the IMPORTS
448 0 foreach $_definition (sort keys %_DEF) {
449 0 _myprintf(" IMPORT check check [%s] for imports\n", $_definition);
450 # check to see if import block was parsed
451 0 ▶ if ($_DEF{$_definition} eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_DEF{$_definition} eq '') { } 0 0 0
452 0 $WARNING = sprintf("No IMPORTS parsed in: [%s]", $_[0]);
453 0 push(@WARNINGS, ['_FILE_', $WARNING] );
454 0 _myprintf(" IMPORT parsed: WARN: no imports parsed\n");
455 }
456 # extract info from import block
457 else {
458 0 $_DEF{$_definition} =~ /IMPORTS\s+(.+[A-Z][\w\-?]{0,63})\s*;/s;
459 0 $_ = $1;
460 # parse out moduleIdentifier (after 'FROM')
461 0 @{$DEFINITIONS{$_definition}{imports}} = m/FROM\s+([A-Z][\w\-]{0,63})/sg;
462 _myprintf(" IMPORT parsed: count: %s def: %s block size: %s\n",
463 0 scalar(@{$DEFINITIONS{$_definition}{imports}}),
464 $_definition,
465 0 length($_DEF{$_definition}),
466 );
467 0 ▶ if (@{$DEFINITIONS{$_definition}{imports}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$DEFINITIONS{$_definition}{'imports'};}) { } 0 0 0
468 _myprintf(" IMPORT parsed: [%s]\n",
469 0 join(', ', @{$DEFINITIONS{$_definition}{imports}})
470 );
471 0 ▶ if ($_TRACK_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_TRACK_FLAG) 0 0 0
472 0 foreach $_import (@{$DEFINITIONS{$_definition}{imports}}) {
473 0 _track_it("$_definition", "requires IMPORT $_import");
474 0 _track_it("$_import", "required import for $_definition");
475 }
476 }
477 }
478 else {
479 0 _myprintf(" IMPORT parsed: NONE: [%s] in chunk\n", $_definition);
480 0 $WARNING = sprintf("No IMPORTS parsed from IMPORT chunk: [%s]", $_definition);
481 0 push(@WARNINGS, ['_FILE_', $WARNING]);
482 0 _track_it("$_definition", "no IMPORTS parsed from IMPORT chunk");
483 }
484 }
485  
486  
487 }
488 0 return(1);
489 } # end _parse_mib_file
490  
491 #
492 #.............................................................................
493 #
494 #
495 # function to compute the weights of the DEFINITIONS
496 #
497 # Arguments
498 # none, work on global hash
499 #
500 # Return
501 # none, populate global hash
502 0 ▶sub _compute_definition_weights {
Subroutine _compute_definition_weights: not called CC=6 SCAR=37.4
503  
504 0 my $_base_weight = 2; # all definitions get this
505 0 my $_import_required = '-1'; # if definition requires imports
506 0 my $_import_weight = 5; # apply to all imports
507 0 my $_import2_weight = 1000; # apply to all imports required by prev import
508  
509 0 my ($_def, $_imp, $_imp2);
510  
511 # loop thru each definition
512 # add $_base_weight for each definition
513 0 my $_c = 0;
514 0 foreach $_def (sort keys %DEFINITIONS) {
515 0 $DEFINITIONS{$_def}{weight} = $DEFINITIONS{$_def}{weight} + $_base_weight;
516 _myprintf("Weight \(%s\): %s defined, incr %s, weight = %s\n",
517 ++$_c, $_def, $_base_weight, $DEFINITIONS{$_def}{weight}
518 0 );
519 0 _track_it($_def, "adding base weight: $_base_weight, now $DEFINITIONS{$_def}{weight}");
520 # if this definition requires imports, add $_import_required (subtraction)
521 0 ▶ if (scalar($DEFINITIONS{$_def}{imports})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $DEFINITIONS{$_def}{"imports"}) 0 0 0
522 0 $DEFINITIONS{$_def}{weight} = $DEFINITIONS{$_def}{weight} + $_import_required;
523 _myprintf(" Weight: requires IMPORTs, decr %s, weight = %s\n",
524 $_import_required, $DEFINITIONS{$_def}{weight}
525 0 );
526 0 _track_it($_def,
527 "requires imports, decr $_import_required, now $DEFINITIONS{$_def}{weight}"
528 );
529 # foreach import required, add $_import_weight to the import's definition
530 0 foreach $_imp (@{$DEFINITIONS{$_def}{imports}}) {
531 0 $DEFINITIONS{$_imp}{weight} = $DEFINITIONS{$_imp}{weight} + $_import_weight;
532 0 $DEFINITIONS{$_imp}{'import'}++;
533 _myprintf(" Weight: required IMPORT: [%s] incr %s, weight = %s\n",
534 $_imp, $_import_weight, $DEFINITIONS{$_imp}{weight}
535 0 );
536 0 _track_it($_imp,
537 "required IMPORT for $_def, incr $_import_weight, now $DEFINITIONS{$_imp}{weight}"
538 );
539 # if import requires import, add $_import2_weight to what it imports
540 0 ▶ if (scalar($DEFINITIONS{$_imp}{imports})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $DEFINITIONS{$_imp}{"imports"}) 0 0 0
541 0 foreach $_imp2 (@{$DEFINITIONS{$_imp}{imports}}) {
542 0 $DEFINITIONS{$_imp2}{weight} = $DEFINITIONS{$_imp2}{weight} + $_import2_weight;
543 _myprintf(" Weight: IMPORT requires: [%s], incr %s, weight = %s\n",
544 $_imp2, $_import2_weight, $DEFINITIONS{$_imp2}{weight}
545 0 );
546 0 _track_it($_imp2,
547 "required by import $_imp, incr $_import2_weight, now $DEFINITIONS{$_imp2}{weight}"
548 );
549 }
550 }
551 }
552 }
553 }
554 0 1;
555 }
556 #
557 #.............................................................................
558 #
559 # function to prioritize standard mibs over enterprise mibs
560 # look at all enterprise mib definitions, find highest weight
561 # look at all standard mibs, if weight is lower, make it '1' more than
562 # highest enterprise
563 #
564 # Arguments
565 # none, works on global hash
566 #
567 # Return
568 # none, works on global hash
569 #
570 0 ▶sub _prioritize {
Subroutine _prioritize: not called CC=7 SCAR=40.3
571  
572 0 my $_ent_max = 0;
573 0 my ($_def, $_prev_weight);
574  
575 # find highest enterprise weight
576 0 foreach $_def (keys %DEFINITIONS) {
577 0 ▶ if ($DEFINITIONS{$_def}{type} eq "Enterprise") {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFINITIONS{$_def}{"type"} eq "Enterprise") 0 0 0
578 0 ▶ if ($DEFINITIONS{$_def}{weight} > $_ent_max)
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFINITIONS{$_def}{"weight"} > $_ent_max) 0 0 0
579 0 {$_ent_max = $DEFINITIONS{$_def}{weight};}
580 }
581 }
582 0 _myprintf("highest Enterprise MIB weight = %s\n", $_ent_max);
583  
584 # check each standard mib, if weight is less than or equal to highest
585 # enterprise mib, change standard weight to +1 of highest enterprise
586 # this will not corrupt order, another filter will assure proper order
587 # this will only get as many standared mibs in front of enterprise mibs
588 # as possible
589 0 foreach $_def (keys %DEFINITIONS) {
590 0 ▶ if ( $DEFINITIONS{$_def}{type} eq "Standard") {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFINITIONS{$_def}{"type"} eq "Standard") 0 0 0
591 0 ▶ if ($DEFINITIONS{$_def}{weight} <= $_ent_max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFINITIONS{$_def}{"weight"} <= $_ent_max) 0 0 0
592 0 $_prev_weight = $DEFINITIONS{$_def}{weight};
593 0 $DEFINITIONS{$_def}{weight} = $_ent_max + 1;
594 _myprintf("%s %s [%s] weight less than highest enterprise, change to %s\n",
595 $DEFINITIONS{$_def}{type},
596 $_def,
597 $_prev_weight,
598 $DEFINITIONS{$_def}{weight},
599 0 );
600 0 _track_it("$_def",
601 "priority change, $_prev_weight <= $_ent_max, change to $DEFINITIONS{$_def}{weight}"
602 );
603 }
604 }
605 }
606 0 1;
607 }
608 #
609 #.............................................................................
610 #
611 # function to sort the weights
612 #
613 # Arguments
614 # none, get info from global hash
615 #
616 # Return
617 # none, make global array, sorted weight
618 #
619  
620 0 ▶sub _sort_weights {
Subroutine _sort_weights: not called CC=4 SCAR=30.0
621  
622 0 my @_weights_unsorted = ();
623 0 my %_weights = ();
624 0 my $_def_count = 0;
625 0 my ($_d);
626  
627 0 @WEIGHTS_SORTED = ();
628  
629 # extract and index the weights
630 0 foreach $_d (keys %DEFINITIONS) {
631 0 $_def_count++;
632 0 $_weights{$DEFINITIONS{$_d}{weight}} = $_weights{$DEFINITIONS{$_d}{weight}} + 1;
633 _myprintf("sorting: weight %s, [%s] %s DEFINITIONs\n",
634 $DEFINITIONS{$_d}{weight}, $_d, $_weights{$DEFINITIONS{$_d}{weight}},
635 0 );
636 0 _track_it("$_d", "sorted weight is $DEFINITIONS{$_d}{weight}");
637 }
638 0 @_weights_unsorted = keys %_weights;
639  
640 0 @WEIGHTS_SORTED = reverse sort {$a <=> $b} @_weights_unsorted;
641  
642 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
643 0 foreach (@WEIGHTS_SORTED)
644 0 {_myprintf("weight sort summary: weight %8s %s definitions\n", $_, $_weights{$_});}
645 }
646 0 _myprintf("%s sorted definitions\n", $_def_count);
647 0 1;
648 }
649 #
650 #.............................................................................
651 #
652 # function to make a sorted list of definitions based on sorted weight list
653 #
654 # Arguments
655 # none, read from global hash
656 #
657 # Return
658 # none, make global list
659  
660 0 ▶sub _sort_definitions {
Subroutine _sort_definitions: not called CC=11 SCAR=48.8
661  
662 0 my ($_w, $_def, $_imp, $_d);
663 0 my $_ok = undef;
664  
665 0 @LOAD_ORDER = ();
666  
667 0 _myprintf("### Sorting DEFINITIONs, loop: %s ###\n", $ORDER_LOOPS);
668 # cycle through each weight
669 0 foreach $_w (@WEIGHTS_SORTED) {
670 0 _myprintf("weight: %8s\n", $_w);
671 # find DEFs with this weight
672 0 foreach $_def (keys %DEFINITIONS) {
673 0 ▶ if ($DEFINITIONS{$_def}{weight} == $_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFINITIONS{$_def}{"weight"} == $_w) 0 0 0
674 0 push(@LOAD_ORDER, $_def);
675 _myprintf(" [%s] = %s, added to load ordered, %s loaded\n",
676 0 $_def, $DEFINITIONS{$_def}{weight}, scalar(@LOAD_ORDER),
677 );
678 0 _track_it("$_def",
679 "sorting definition, pushing on load order list, $DEFINITIONS{$_def}{weight}"
680 );
681 # check that its imports are loaded, based on weight
682 0 foreach $_imp (@{$DEFINITIONS{$_def}{imports}}) {
683 0 _myprintf(" IMPORT [%s] required, ", $_imp);
684 # check weights of any imports are greater than definition weight
685 0 ▶ if ($DEFINITIONS{$_imp}{weight} <= $_w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEFINITIONS{$_imp}{'weight'} <= $_w) { } 0 0 0
686 printf("not loaded, changing weight %s => ",
687 $DEFINITIONS{$_imp}{weight}
688 0 ▶ ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
689 0 _track_it("$_imp",
690 "required IMPORT has lower weight: $DEFINITIONS{$_imp}{weight}"
691 );
692 0 $DEFINITIONS{$_imp}{weight} = $_w + 1;
693 0 ▶ printf("%s\n", $DEFINITIONS{$_imp}{weight}) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
694 0 _track_it("$_imp",
695 "changed weight to $DEFINITIONS{$_imp}{weight} for requirements"
696 );
697 # update the tracking that we are resorting
698 0 ▶ unless ($_TRACK_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_TRACK_FLAG) 0 0 0
699 0 foreach $_d (keys %DEFINITIONS)
700 0 {_track_it("$_d","re-sort, $_def requires $_imp to be loaded");}
701 }
702 0 return(undef);
703 }
704 # all imports have higher weights
705 else {
706 printf("loaded, %s\n", $DEFINITIONS{$_imp}{weight})
707 0 ▶ if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
708 }
709 }
710 }
711 }
712 }
713 0 _myprintf("DEFINITIONs sorted, %s loops needed\n", $ORDER_LOOPS);
714 0 1;
715 }
716 #
717 #.............................................................................
718 #
719 # function to find the warnings
720 #
721 # loop thru all definitions, if no files exist for def, issue warning
722 #
723 # Arguments
724 # none, operate on global hash
725 #
726 # Return
727 # none, populate globah hash
728 # @WARNINGS = ([DEFINITION, cuase], [], []
729 #
730 0 ▶sub _find_warnings {
Subroutine _find_warnings: not called CC=3 SCAR=24.8
731  
732 0 my $_no_file = 'No file found for DEFINITION';
733 0 my $_multi_files = 'DEFINITION found in multiple files';
734 0 my $_def;
735 my $_keep;
736 0 my @_dump;
737 0 my $_f;
738
739 0 foreach $_def (sort keys %DEFINITIONS) {
740 0 ▶ if ( !defined($DEFINITIONS{$_def}{file}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DEFINITIONS{$_def}{"file"}) 0 0 0
741 0 push(@WARNINGS, ["$_def", "$_no_file"]);
742 0 _track_it("$_def", "issue warning: $_no_file");
743 }
744 }
745 0 1;
746 }
747 #
748 #.............................................................................
749 #
750 #
751 0 ▶sub _myprintf {
Subroutine _myprintf: not called CC=2 SCAR=17.9
752  
753 0 ▶ return unless $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless $DEBUG 0 0 0
754  
755 0 my $_format = shift;
756 0 my ($_pkg, $_line) = (caller)[0,2];
757 0 my $_func = (caller(1))[3];
758 0 $_pkg =~ s/.+://;
759 0 $_func =~ s/.+://;
760  
761 0 printf("%s: %s: [%s]: $_format", $_pkg, $_func, $_line, @_);
762 }
763  
764 #
765 #.............................................................................
766 #
767 # function to track events per DEFINITION
768 #
769 # Argument
770 # $_[0] = DEFINITION
771 # $_[1] = event
772 #
773 # Return
774 # none, populate global hash
775 # %TRACK{definition} = ([index, event], [], [], ...)
776 #
777 0 ▶sub _track_it {
Subroutine _track_it: not called CC=2 SCAR=17.9
778 0 ▶ return unless $_TRACK_FLAG;
Branch: 1 branch
BranchTrueFalseTotal
unless $_TRACK_FLAG 0 0 0
779 0 push( @{$TRACK_HASH{$_[0]}}, [++$_TRACK_INDEX, "$_[1]"] );
780 0 1;
781 }
782  
783  
784  
785 #
786 # !!!! End the Module !!!!
787 #
788  
789 1 1;
790 __END__
↑ Summary