Debian Codegraph @

local/mib2c

raw ◀ ▶
2 Debian patches touch this file
1 #!/usr/bin/perl
2 #!/usr/bin/perl -w
3
4 #
5 # $Id$
6 #
7 # Description:
8 #
9 # This program, given an OID reference as an argument, creates some
10 # template mib module files to be used with the net-snmp agent. It is
11 # far from perfect and will not generate working modules, but it
12 # significantly shortens development time by outlining the basic
13 # structure.
14 #
15 # Its up to you to verify what it does and change the default values
16 # it returns.
17 #
18
19 # SNMP
20 my $havesnmp = eval {require SNMP;};
21 my $havenetsnmpoid = eval {require NetSNMP::OID;};
22
23 if (!$havesnmp) {
24 print "
25 ERROR: You don't have the SNMP perl module installed. Please obtain
•26 this by running the command:
27
28 apt-get install libsnmp-perl
29
30 ";
31 exit;
32 }
33
34 if ($havesnmp) {
35 eval { import SNMP; }
36 }
37 if ($havenetsnmp) {
38 eval { import NetSNMP::OID; }
39 }
40 use FileHandle;
41
42 #use strict 'vars';
43 $SNMP::save_descriptions=1;
44 $SNMP::use_long_names=1;
45 $SNMP::use_enums=1;
46 SNMP::initMib();
47
48 $configfile="mib2c.conf";
49 $debug=0;
50 $quiet=0;
51 $strict_unk_token = 0;
52 $noindent = 0;
53 $nosed = 0;
54 $currentline = 0;
55 $currentlevel = -1;
56 %assignments;
57 %outputs;
58 @def_search_dirs = (".");
59 @search_dirs = ();
60 if($ENV{MIB2C_DIR}) {
61 push @def_search_dirs, split(/:/, $ENV{MIB2C_DIR});
62 }
•63 push @def_search_dirs, "/etc/snmp/";
64 push @def_search_dirs, "/usr/share/snmp/";
65 push @def_search_dirs, "/usr/share/snmp/mib2c-data";
66 push @def_search_dirs, "./mib2c-conf.d";
67
68 sub usage {
69 print "$0 [-h] [-c configfile] [-f prefix] mibNode\n\n";
70 print " -h\t\tThis message.\n\n";
71 print " -c configfile\tSpecifies the configuration file to use\n\t\tthat dictates what the output of mib2c will look like.\n\n";
72 print " -I PATH\tSpecifies a path to look for configuration files in\n\n";
73 print " -f prefix\tSpecifies the output prefix to use. All code\n\t\twill be put into prefix.c and prefix.h\n\n";
74 print " -d\t\tdebugging output (don't do it. trust me.)\n\n";
75 print " -S VAR=VAL\tSet \$VAR variable to \$VAL\n\n";
76 print " -i\t\tDon't run indent on the resulting code\n\n";
77 print " -s\t\tDon't look for mibNode.sed and run sed on the resulting code\n\n";
78 print " mibNode\tThe name of the top level mib node you want to\n\t\tgenerate code for. By default, the code will be stored in\n\t\tmibNode.c and mibNode.h (use the -f flag to change this)\n\n";
79 1;
80 }
81
82 my @origargs = @ARGV;
83 my $args_done = 0;
84 while($#ARGV >= 0) {
85 $_ = shift;
86 if (/^-/) {
87 if ($args_done != 0) {
88 warn "all argument must be specified before the mibNode!\n";
89 usage;
90 exit 1;
91 } elsif (/^-c/) {
92 $configfile = shift;
93 } elsif (/^-d/) {
94 $debug = 1;
95 } elsif (/^-S/) {
96 my $expr = shift;
97 my ($var, $val) = ($expr =~ /([^=]*)=(.*)/);
98 die "no variable specified for -S flag." if (!$var);
99 $assignments{$var} = $val;
100 } elsif (/^-q/) {
101 $quiet = 1;
102 } elsif (/^-i/) {
103 $noindent = 1;
104 } elsif (/^-s/) {
105 $nosed = 1;
106 } elsif (/^-h/) {
107 usage && exit(1);
108 } elsif (/^-f/) {
109 $outputName = shift;
110 } elsif (/^-I/) {
111 my $dirs = shift;
112 push @search_dirs, split(/,/,$dirs);
113 } else {
114 warn "Unknown option '$_'\n";
115 usage;
116 exit 1;
117 }
118 } else {
119 $args_done = 1;
120 warn "Replacing previous mibNode $oid with $_\n" if ($oid);
121 $oid = $_ ;
122 }
123 }
124
125 #
126 # internal conversion tables
127 #
128
129 %accessToIsWritable = qw(ReadOnly 0 ReadWrite 1
130 WriteOnly 1 Create 1);
131 %perltoctypes = qw(OCTETSTR ASN_OCTET_STR
132 INTEGER ASN_INTEGER
133 INTEGER32 ASN_INTEGER
134 UNSIGNED32 ASN_UNSIGNED
135 OBJECTID ASN_OBJECT_ID
136 COUNTER64 ASN_COUNTER64
137 COUNTER ASN_COUNTER
138 NETADDR ASN_COUNTER
139 UINTEGER ASN_UINTEGER
140 IPADDR ASN_IPADDRESS
141 BITS ASN_OCTET_STR
142 TICKS ASN_TIMETICKS
143 GAUGE ASN_GAUGE
144 OPAQUE ASN_OPAQUE);
145 %perltodecl = ("OCTETSTR", "char",
146 "INTEGER", "long",
147 "INTEGER32", "long",
148 "UNSIGNED32", "u_long",
149 "UINTEGER", "u_long",
150 "OBJECTID", "oid",
151 "COUNTER64", "struct counter64",
152 "COUNTER", "u_long",
153 "IPADDR", "in_addr_t",
154 "BITS", "char",
155 "TICKS", "u_long",
156 "GAUGE", "u_long",
157 "OPAQUE", "u_char");
158 %perltolen = ("OCTETSTR", "1",
159 "INTEGER", "0",
160 "INTEGER32", "0",
161 "UNSIGNED32", "0",
162 "UINTEGER", "0",
163 "OBJECTID", "1",
164 "COUNTER64", "0",
165 "COUNTER", "0",
166 "IPADDR", "0",
167 "BITS", "1",
168 "TICKS", "0",
169 "GAUGE", "0",
170 "OPAQUE", "1");
171
172 my $mibnode = $SNMP::MIB{$oid};
173
174 if (!$mibnode) {
175
176 print STDERR "
177 You didn't give mib2c a valid OID to start with. IE, I could not find
178 any information about the mib node \"$oid\". This could be caused
179 because you supplied an incorrectly node, or by the MIB that you're
180 trying to generate code from isn't loaded. To make sure your mib is
181 loaded, run mib2c using this as an example:
182
183 env MIBS=\"+MY-PERSONAL-MIB\" mib2c " . join(" ",@origargs) . "
184
185 You might wish to start by reading the MIB loading tutorial at:
186
187 http://www.net-snmp.org/tutorial-5/commands/mib-options.html
188
189 And making sure you can get snmptranslate to display information about
190 your MIB node. Once snmptranslate works, then come back and try mib2c
191 again.
192
193 ";
194 exit 1;
195 }
196
197 # setup
198 $outputName = $mibnode->{'label'} if (!defined($outputName));
199 $outputName =~ s/-/_/g;
200 $vars{'name'} = $outputName;
201 $vars{'oid'} = $oid;
202 $vars{'example_start'} = " /*\n" .
203 " ***************************************************\n" .
204 " *** START EXAMPLE CODE ***\n" .
205 " ***---------------------------------------------***/";
206 $vars{'example_end'} = " /*\n" .
207 " ***---------------------------------------------***\n" .
208 " *** END EXAMPLE CODE ***\n" .
209 " ***************************************************/";
210
211 # loop through mib nodes, remembering stuff.
212 setup_data($mibnode);
213
214 if(($ENV{HOME}) && (-f "$ENV{HOME}/.snmp/mib2c.conf")) {
215 $fh = open_conf("$ENV{HOME}/.snmp/mib2c.conf");
216 process("-balanced");
217 $fh->close;
218 }
219
220 my $defaults = find_conf("default-$configfile",1);
221 if (-f "$defaults" ) {
222 $fh = open_conf($defaults);
223 process("-balanced");
224 $fh->close;
225 }
226
227 my @theassignments = keys(%assignments);
228 if ($#theassignments != -1) {
229 foreach $var (@theassignments) {
230 $vars{$var} = $assignments{$var};
231 }
232 }
233 $configfile = find_conf($configfile,0);
234 $fh = open_conf($configfile);
235 process("-balanced");
236 $fh->close;
237
238 if (-f "$outputName.sed" && !$nosed) {
239 foreach $i (keys(%written)) {
240 next if ($i eq "-");
241 next if (!($i =~ /\.[ch]$/));
242 print STDERR "running sed --in-place=.orig --file=$outputName.sed $i\n" if (!$quiet);
243 system("sed --in-place=.orig --file=$outputName.sed $i");
244 }
245 }
246
247 if (!$noindent) {
248 foreach $i (keys(%written)) {
249 next if ($i eq "-");
250 next if (!($i =~ /\.[ch]$/));
251 print STDERR "running indent on $i\n" if (!$quiet);
252 system("indent -orig -nbc -bap -nut -nfca -T size_t -T netsnmp_mib_handler -T netsnmp_handler_registration -T netsnmp_delegated_cache -T netsnmp_mib_handler_methods -T netsnmp_old_api_info -T netsnmp_old_api_cache -T netsnmp_set_info -T netsnmp_request_info -T netsnmp_set_info -T netsnmp_tree_cache -T netsnmp_agent_request_info -T netsnmp_cachemap -T netsnmp_agent_session -T netsnmp_array_group_item -T netsnmp_array_group -T netsnmp_table_array_callbacks -T netsnmp_table_row -T netsnmp_table_data -T netsnmp_table_data_set_storage -T netsnmp_table_data_set -T netsnmp_column_info -T netsnmp_table_registration_info -T netsnmp_table_request_info -T netsnmp_iterator_info -T netsnmp_data_list -T netsnmp_oid_array_header -T netsnmp_oid_array_header_wrapper -T netsnmp_oid_stash_node -T netsnmp_pdu -T netsnmp_request_list -T netsnmp_callback_pass -T netsnmp_callback_info -T netsnmp_transport -T netsnmp_transport_list -T netsnmp_tdomain $i");
253 }
254 }
255
256 sub m2c_die {
257 warn "ERROR: ". $_[0] . "\n";
258 die " at $currentfile:$currentline\n";
259 }
260
261 sub tocommas {
262 my $oid = $_[0];
263 $oid =~ s/\./,/g;
264 $oid =~ s/^\s*,//;
265 return $oid;
266 }
267
268 sub oidlength {
269 return (scalar split(/\./, $_[0])) - 1;
270 }
271
272 # replaces $VAR type expressions and $VAR.subcomponent expressions
273 # with data from the mib tree and loop variables.
274 # possible uses:
275 #
276 # $var -- as defined by loops, etc.
277 # ${var}otherstuff -- appending text to variable contents
278 # $var.uc -- all upper case version of $var
279 #
280 # NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages
281 #
282 # Mib components, $var must first expand to a mib node name:
283 #
284 # $var.uc -- all upper case version of $var
285 #
286 # $var.objectID -- dotted, fully-qualified, and numeric OID
287 # $var.commaoid -- comma separated numeric OID for array initialization
288 # $var.oidlength -- length of the oid
289 # $var.subid -- last number component of oid
290 # $var.module -- MIB name that the object comes from
291 # $var.parent -- contains the label of the parent node of $var.
292 #
293 # $var.isscalar -- returns 1 if var contains the name of a scalar
294 # $var.iscolumn -- returns 1 if var contains the name of a column
295 # $var.children -- returns 1 if var has children
296 #
297 # $var.perltype -- node's perl SYNTAX ($SNMP::MIB{node}{'syntax'})
298 # $var.type -- node's ASN_XXX type (Net-SNMP specific #define)
299 # $var.decl -- C data type (char, u_long, ...)
300 #
301 # $var.readable -- 1 if an object is readable, 0 if not
302 # $var.settable -- 1 if an object is writable, 0 if not
303 # $var.creatable -- 1 if a column object can be created as part of a new row, 0 if not
304 # $var.noaccess -- 1 if not-accessible, 0 if not
305 # $var.accessible -- 1 if accessible, 0 if not
306 # $var.storagetype -- 1 if an object is a StorageType object, 0 if not
307 # $var.rowstatus -- 1 if an object is a RowStatus object, 0 if not
308 # 'settable', 'creatable', 'lastchange', 'storagetype' and 'rowstatus' can
309 # also be used with table variables to indicate whether it contains
310 # writable, creatable, LastChange, StorageType or RowStatus column objects
311 #
312 # $var.hasdefval -- returns 1 if var has a DEFVAL clause
313 # $var.defval -- node's DEFVAL
314 # $var.hashint -- returns 1 if var has a HINT clause
315 # $var.hint -- node's HINT
316 # $var.ranges -- returns 1 if var has a value range defined
317 # $var.enums -- returns 1 if var has enums defined for it.
318 # $var.access -- node's access type
319 # $var.status -- node's status
320 # $var.syntax -- node's syntax
321 # $var.reference -- node's reference
322 # $var.description -- node's description
323
324 sub process_vars {
325 my $it = shift;
326
327 # mib substitutions ($var.type -> $mibnode->{'type'})
328 if ( $it =~ /\$(\w+)\.(\w+)/ ) {
329 if (!defined($vars{$1})) {
330 m2c_die "Undefined variable \$$1";
331 }
332 if ($SNMP::MIB{$vars{$1}} && defined($tables{$SNMP::MIB{$vars{$1}}{'label'}})) {
333 $it =~ s/\$(\w+)\.(settable)/(table_is_writable($SNMP::MIB{$vars{$1}}{label}))/eg;
334 $it =~ s/\$(\w+)\.(creatable)/(table_has_create($SNMP::MIB{$vars{$1}}{label}))/eg;
335 $it =~ s/\$(\w+)\.(rowstatus)/(table_has_rowstatus($SNMP::MIB{$vars{$1}}{label}))/eg;
336 $it =~ s/\$(\w+)\.(lastchange)/(table_has_lastchange($SNMP::MIB{$vars{$1}}{label}))/eg;
337 $it =~ s/\$(\w+)\.(storagetype)/(table_has_storagetype($SNMP::MIB{$vars{$1}}{label}))/eg;
338 }
339 $it =~ s/\$(\w+)\.(uc)/uc($vars{$1})/eg; # make something uppercase
340 $it =~ s/\$(\w+)\.(commaoid)/tocommas($SNMP::MIB{$vars{$1}}{objectID})/eg;
341 $it =~ s/\$(\w+)\.(oidlength)/oidlength($SNMP::MIB{$vars{$1}}{objectID})/eg;
342 $it =~ s/\$(\w+)\.(description)/$SNMP::MIB{$vars{$1}}{description}/g;
343 $it =~ s/\$(\w+)\.(perltype)/$SNMP::MIB{$vars{$1}}{type}/g;
344 $it =~ s/\$(\w+)\.(type)/$perltoctypes{$SNMP::MIB{$vars{$1}}{$2}}/g;
345 $it =~ s/\$(\w+)\.(subid)/$SNMP::MIB{$vars{$1}}{subID}/g;
346 $it =~ s/\$(\w+)\.(module)/$SNMP::MIB{$vars{$1}}{moduleID}/g;
347 $it =~ s/\$(\w+)\.(settable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(ReadWrite|Create|WriteOnly)\/)?1:0)/eg;
348 $it =~ s/\$(\w+)\.(creatable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Create)\/)?1:0)/eg;
349 $it =~ s/\$(\w+)\.(readable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Read|Create)\/)?1:0)/eg;
350 $it =~ s/\$(\w+)\.(noaccess)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(NoAccess)\/)?1:0)/eg;
351 $it =~ s/\$(\w+)\.(accessible)/(($SNMP::MIB{$vars{$1}}{access} !~ \/(NoAccess)\/)?1:0)/eg;
352 $it =~ s/\$(\w+)\.(objectID|label|subID|access|status|syntax|reference)/$SNMP::MIB{$vars{$1}}{$2}/g;
353 $it =~ s/\$(\w+)\.(decl)/$perltodecl{$SNMP::MIB{$vars{$1}}{type}}/g;
354 $it =~ s/\$(\w+)\.(needlength)/$perltolen{$SNMP::MIB{$vars{$1}}{type}}/g;
355 $it =~ s/\$(\w+)\.(iscolumn)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} =~ \/Entry$\/) ? 1 : 0/eg;
356 $it =~ s/\$(\w+)\.(isscalar)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} !~ \/Entry$\/ && $SNMP::MIB{$vars{$1}}{access}) ? 1 : 0/eg;
357 $it =~ s/\$(\w+)\.(parent)/$SNMP::MIB{$vars{$1}}{'parent'}{'label'}/g;
358 $it =~ s/\$(\w+)\.(children)/($#{$SNMP::MIB{$vars{$1}}{'children'}} == 0) ? 0 : 1/eg;
359 $it =~ s/\$(\w+)\.(hasdefval)/(length($SNMP::MIB{$vars{$1}}{'defaultValue'}) == 0) ? 0 : 1/eg;
360 $it =~ s/\$(\w+)\.(defval)/$SNMP::MIB{$vars{$1}}{'defaultValue'}/g;
361 $it =~ s/\$(\w+)\.(hashint)/(length($SNMP::MIB{$vars{$1}}{'hint'}) == 0) ? 0 : 1/eg;
362 $it =~ s/\$(\w+)\.(hint)/$SNMP::MIB{$vars{$1}}{'hint'}/g;
363 $it =~ s/\$(\w+)\.(ranges)/($#{$SNMP::MIB{$vars{$1}}{'ranges'}} == -1) ? 0 : 1/eg;
364 # check for enums
365 $it =~ s/\$(\w+)\.(enums)/(%{$SNMP::MIB{$vars{$1}}{'enums'}} == 0) ? 0 : 1/eg;
366 $it =~ s/\$(\w+)\.(enumrange)/%{$SNMP::MIB{$vars{$1}}{'enums'}}/eg;
367 $it =~ s/\$(\w+)\.(rowstatus)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(RowStatus)\/)?1:0)/eg;
368 $it =~ s/\$(\w+)\.(storagetype)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(StorageType)\/)?1:0)/eg;
369 if ( $it =~ /\$(\w+)\.(\w+)/ ) {
370 warn "Possible unknown variable attribute \$$1.$2 at $currentfile:$currentline\n";
371 }
372 }
373 # normal variable substitions
374 $it =~ s/\$\{(\w+)\}/$vars{$1}/g;
375 $it =~ s/\$(\w+)/$vars{$1}/g;
376 # use $@var to put literal '$var'
377 $it =~ s/\$\@(\w+)/\$$1/g;
378 return $it;
379 }
380
381 # process various types of statements
382 #
383 # NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages
384 # which include:
385 # @open FILE@
386 # writes generated output to FILE
387 # note that for file specifications, opening '-' will print to stdout.
388 # @append FILE@
389 # appends the given FILE
390 # @close FILE@
391 # closes the given FILE
392 # @push@
393 # save the current outputs, then clear outputs. Use with @open@
394 # and @pop@ to write to a new file without interfering with current
395 # outputs.
396 # @pop@
397 # pop up the process() stack one level. Use after a @push@ to return to
398 # the previous set of open files.
399 # @foreach $VAR scalar@
400 # repeat iterate over code until @end@ setting $VAR to all known scalars
401 # @foreach $VAR table@
402 # repeat iterate over code until @end@ setting $VAR to all known tables
403 # @foreach $VAR column@
404 # repeat iterate over code until @end@ setting $VAR to all known
405 # columns within a given table. Obviously this must be called
406 # within a foreach-table clause.
407 # @foreach $VAR nonindex@
408 # repeat iterate over code until @end@ setting $VAR to all known
409 # non-index columns within a given table. Obviously this must be called
410 # within a foreach-table clause.
411 # @foreach $VAR internalindex@
412 # repeat iterate over code until @end@ setting $VAR to all known internal
413 # index columns within a given table. Obviously this must be called
414 # within a foreach-table clause.
415 # @foreach $VAR externalindex@
416 # repeat iterate over code until @end@ setting $VAR to all known external
417 # index columns within a given table. Obviously this must be called
418 # within a foreach-table clause.
419 # @foreach $VAR index@
420 # repeat iterate over code until @end@ setting $VAR to all known
421 # indexes within a given table. Obviously this must be called
422 # within a foreach-table clause.
423 # @foreach $VAR notifications@
424 # repeat iterate over code until @end@ setting $VAR to all known notifications
425 # @foreach $VAR varbinds@
426 # repeat iterate over code until @end@ setting $VAR to all known varbinds
427 # Obviously this must be called within a foreach-notifications clause.
428 # @foreach $LABEL, $VALUE enum@
429 # repeat iterate over code until @end@ setting $LABEL and $VALUE
430 # to the label and values from the enum list.
431 # @foreach $RANGE_START, $RANGE_END range NODE@
432 # repeat iterate over code until @end@ setting $RANGE_START and $RANGE_END
433 # to the legal accepted range set for a given mib NODE.
434 # @foreach $var stuff a b c d@
435 # repeat iterate over values a, b, c, d as assigned generically
436 # (ie, the values are taken straight from the list with no
437 # mib-expansion, etc).
438 # @while expression@
439 # repeat iterate over code until the expression is false
440 # @eval $VAR = expression@
441 # evaluates expression and assigns the results to $VAR. This is
442 # not a full perl eval, but sort of a "psuedo" eval useful for
443 # simple expressions while keeping the same variable name space.
444 # See below for a full-blown export to perl.
445 # @perleval STUFF@
446 # evaluates STUFF directly in perl. Note that all mib2c variables
447 # interpereted within .conf files are in $vars{NAME} and that
448 # a warning will be printed if STUFF does not return 0. (adding a
449 # 'return 0;' at the end of STUFF is a workaround.
450 # @startperl@
451 # @endperl@
452 # treats everything between these tags as perl code, and evaluates it.
453 # @next@
454 # restart foreach; should only be used inside a conditional.
455 # skips out of current conditional, then continues to skip to
456 # end for the current foreach clause.
457 # @if expression@
458 # evaluates expression, and if expression is true processes
459 # contained part until appropriate @end@ is reached. If the
460 # expression is false, the next @elsif expression@ expression
461 # (if it exists) will be evaluated, until an expression is
462 # true. If no such expression exists and an @else@
463 # clause is found, it will be evaluated.
464 # @ifconf file@
465 # If the specified file can be found in the conf file search path,
466 # and if found processes contained part until an appropriate @end@ is
467 # found. As with a regular @if expression@, @elsif expression@ and
468 # @else@ can be used.
469 # @ifdir dir@
470 # If the specified directory exists, process contained part until an
471 # appropriate @end@ is found. As with a regular @if expression@,
472 # @elsif expression@ and @else@ can be used.
473 # @define NAME@
474 # @enddefine@
475 # Memorizes "stuff" between the define and enddefine tags for
476 # later calling as NAME by @calldefine NAME@.
477 # @calldefine NAME@
478 # Executes stuff previously memorized as NAME.
479 # @printf "expression" stuff1, stuff2, ...@
480 # Like all the other printf's you know and love.
481 # @run FILE@
482 # Sources the contents of FILE as a mib2c file,
483 # but does not affect current files opened.
484 # @include FILE@
485 # Sources the contents of FILE as a mib2c file and appends its
486 # output to the current output.
487 # @prompt $var QUESTION@
488 # Presents the user with QUESTION, expects a response and puts it in $var
489 # @print STUFF@
490 # Prints stuff directly to the users screen (ie, not to where
491 # normal mib2c output goes)
492 # @quit@
493 # Bail out (silently)
494 # @exit@
495 # Bail out!
496 #
497 sub skippart {
498 my $endcount = 1;
499 my $arg = shift;
500 my $rtnelse = 0;
501 while ($arg =~ s/-(\w+)\s*//) {
502 $rtnelse = 1 if ($1 eq "else");
503 }
504 while(get_next_line()) {
505 $currentline++;
506 $_ = process_vars($_) if ($debug);
507 print "$currentfile.$currentline:P$currentlevel:S$endcount.$rtnelse:$_" if ($debug);
508 next if ( /^\s*\#\#/ ); # noop, it's a comment
509 next if (! /^\s*\@/ ); # output
510 if (! /^\s*\@.*\@/ ) {
511 warn "$currentfile:$currentline contained a line that started with a @ but did not match any mib2c configuration tokens.\n";
512 warn "(maybe missing the trailing @?)\n";
513 warn "$currentfile:$currentline [$_]\n";
514 }
515 elsif (/\@\s*end\@/) {
516 return "end" if ($endcount == 1);
517 $endcount--;
518 }
519 elsif (/\@\s*elseif.*\@/) {
520 m2c_die "use 'elsif' instead of 'elseif'\n";
521 }
522 elsif (/\@\s*else\@/) {
523 return "else" if (($endcount == 1) && ($rtnelse == 1));
524 }
525 elsif (/\@\s*elsif\s+([^\@]+)\@/) {
526 return "else" if (($endcount == 1) && ($rtnelse == 1) && (eval(process_vars($1))));
527 }
528 elsif (/\@\s*(foreach|if|while)/) {
529 $endcount++;
530 }
531 }
532 print "skippart EOF\n";
533 m2c_die "unbalanced code detected in skippart: EOF when $endcount levels deep" if($endcount != 1);
534 return "eof";
535 }
536
537 sub close_file {
538 my $name = shift;
539 if (!$name) {
540 print "close_file w/out name!\n";
541 return;
542 }
543 if(!$outputs{$name}) {
544 print "no handle for $name\n";
545 return;
546 }
547 $outputs{$name}->close();
548 delete $outputs{$name};
549 # print STDERR "closing $name\n" if (!$quiet);
550 }
551
552 sub close_files {
553 foreach $name (keys(%outputs)) {
554 close_file($name);
555 }
556 }
557
558 sub open_file {
559 my $multiple = shift;
560 my $spec = shift;
561 my $name = $spec;
562 $name =~ s/>//;
563 if ($multiple == 0) {
564 close_files();
565 }
566 return if ($outputs{$name});
567 $outputs{$name} = new IO::File;
568 $outputs{$name}->open(">$spec") || m2c_die "failed to open $name";
569 print STDERR "writing to $name\n" if (!$quiet && !$written{$name});
570 $written{$name} = '1';
571 }
572
573 sub process_file {
574 my ($file, $missingok, $keepvars) = (@_);
575 my $oldfh = $fh;
576 my $oldfile = $currentfile;
577 my $oldline = $currentline;
578 # keep old copy of @vars and just build on it.
579 my %oldvars;
580
581 %oldvars = %vars if ($keepvars != 1);
582
583 $file = find_conf($file,$missingok);
584 return if (! $file);
585
586 $fh = open_conf($file);
587 $currentline = 0;
588 process("-balanced");
589 $fh->close();
590
591 $fh = $oldfh;
592 $currentfile = $oldfile;
593 $currentline = $oldline;
594
595 # don't keep values in replaced vars. Revert to ours.
596 %vars = %oldvars if ($keepvars != 1);
597 }
598
599 sub get_next_line {
600 if ($#process_lines > -1) {
601 return $_ = shift @process_lines;
602 }
603 return $_ = <$fh>;
604 }
605
606 sub do_tell {
607 my $stash;
608 $stash->{'startpos'} = $fh->tell();
609 $stash->{'startline'} = $currentline;
610 @{$stash->{'lines'}} = @process_lines;
611 return $stash;
612 }
613
614 sub do_seek {
615 my $stash = shift;
616
617 # save current line number
618 $currentline = $stash->{'startline'};
619 $fh->seek($stash->{'startpos'}, 0); # go to top of section.
620
621 # save current process_lines state.
622 @process_lines = @{$stash->{'lines'}};
623
624 # save state of a number of variables (references), and new assignments
625 for (my $i = 0; $i <= $#_; $i += 2) {
626 push @{$stash->{'vars'}}, $_[$i], ${$_[$i]};
627 ${$_[$i]} = $_[$i+1];
628 }
629 }
630
631 sub do_unseek {
632 my $stash = shift;
633 for (my $i = 0; $i <= $#{$stash->{'vars'}}; $i += 2) {
634 ${$stash->{'vars'}[$i]} = $stash->{'vars'}[$i+1];
635 }
636 }
637
638 sub do_a_loop {
639 my $stash = shift;
640 do_seek($stash, @_);
641 my $return = process();
642 do_unseek($stash);
643 return $return;
644 }
645
646 sub process {
647 my $arg = shift;
648 my $elseok = 0;
649 my $balanced = 0;
650 my $startlevel;
651 my $return = "eof";
652 while ($arg =~ s/-(\w+)\s*//) {
653 $elseok = 1 if ($1 eq "elseok");
654 $balanced = 1 if ($1 eq "balanced");
655 }
656
657 $currentlevel++;
658 $startlevel = $currentlevel;
659 if($balanced) {
660 $balanced = $currentlevel;
661 }
662 while(get_next_line()) {
663 $currentline++;
664 if ($debug) {
665 # my $line = process_vars($_);
666 # chop $line;
667 print "$currentfile.$currentline:P$currentlevel.$elseok:$return:$_";
668 }
669
670 next if (/^\s*\#\#/); # noop, it's a comment
671 if (! /^\s*\@/ ) { # output
672 my $line = process_vars($_);
673 foreach $file (values(%outputs)) {
674 print $file "$line";
675 }
676 } ####################################################################
677 elsif (/\@\s*exit\@/) { # EXIT
678 close_files;
679 die "exiting at conf file ($currentfile:$currentline) request\n";
680 } elsif (/\@\s*quit\@/) { # QUIT
681 close_files;
682 exit;
683 } elsif (/\@\s*debug\s+([^\@]+)\@/) { # DEBUG
684 if ($1 eq "on") {
685 $debug = 1;
686 }
687 else {
688 $debug = 0;
689 }
690 } elsif (/\@\s*strict token\s+([^\@]+)\@/) { # STRICT
691 if ($1 eq "on") {
692 $strict_unk_token = 1;
693 }
694 else {
695 $strict_unk_token = 0;
696 }
697 } elsif (/\@\s*balanced\@/) { # BALANCED
698 $balanced = $currentlevel;
699 } elsif (/\@\s*open\s+([^\@]+)\@/) { # OPEN
700 my $arg = $1;
701 my ($multiple) = (0);
702 while ($arg =~ s/-(\w+)\s+//) {
703 $multiple = 1 if ($1 eq 'multiple');
704 }
705 my $spec = process_vars($arg);
706 open_file($multiple, $spec);
707 } elsif (/\@\s*close\s+([^\@]+)\@/) { # CLOSE
708 my $spec = process_vars($1);
709 close_file($spec);
710 } elsif (/\@\s*append\s+([^\@]+)\@/) { # APPEND
711 my $arg = $1;
712 my ($multiple) = (0);
713 while ($arg =~ s/-(\w+)\s+//) {
714 $multiple = 1 if ($1 eq 'multiple');
715 }
716 my $spec = process_vars($arg);
717 $spec=">$spec";
718 open_file($multiple,$spec);
719 } elsif (/\@\s*define\s*(.*)\@/) { # DEFINE
720 my $it = $1;
721 while (<$fh>) {
722 last if (/\@\s*enddefine\s*@/);
723 push @{$defines{$it}}, $_;
724 }
725 } elsif (/\@\s*calldefine\s+(\w+)@/) {
726 if ($#{$defines{$1}} == -1) {
727 warn "called a define of $1 which didn't exist\n";
728 warn "$currentfile:$currentline [$_]\n";
729 } else {
730 unshift @process_lines, @{$defines{$1}};
731 }
732 } elsif (/\@\s*run (.*)\@/) { # RUN
733 my $arg = $1;
734 my ($again) = (0);
735 while ($arg =~ s/-(\w+)\s+//) {
736 $again = 1 if ($1 eq 'again');
737 # if ($1 eq 'file') {
738 # my ($filearg) = ($arg =~ s/^(\w+)//);
739 # }
740 }
741 my $spec = process_vars($arg);
742 next if (!$again && $ranalready{$spec});
743 $ranalready{$spec} = 1;
744 my %oldout = %outputs;
745 my %emptyarray;
746 %outputs = %emptyoutputs;
747 process_file($spec,0,0);
748 close_files;
749 %outputs = %oldout;
750 } elsif (/\@\s*push\@/) { # PUSH
751 my %oldout = %outputs;
752 my %emptyarray;
753 %outputs = %emptyoutputs;
754 process($arg);
755 close_files;
756 %outputs = %oldout;
757 } elsif (/\@\s*pop\s*\@/) { # POP
758 $return = "pop";
759 last;
760 } elsif (/\@\s*include (.*)\@/) { # INCLUDE
761 my $arg = $1;
762 my ($missingok) = (0);
763 while ($arg =~ s/-(\w+)\s+//) {
764 $missingok = 1 if ($1 eq 'ifexists');
765 }
766 my $spec = process_vars($arg);
767 process_file($spec,$missingok,1);
768 } elsif (/\@\s*if([a-z]*)\s+([^@]+)\@/) { # IF
769 my ($type,$arg,$ok) = ($1,$2,0);
770 # check condition based on type
771 if (! $type) {
772 $ok = eval(process_vars($arg));
773 } elsif ($type eq conf) {
774 my $file = find_conf(process_vars($arg),1); # missingok
775 $ok = (-f $file);
776 } elsif ($type eq dir) {
777 $ok = (-d $arg);
778 } else {
779 m2c_die "unknown if modifier ($type)\n";
780 }
781 # act on condition
782 if ($ok) {
783 $return = process("-elseok");
784 } else {
785 $return = skippart("-else");
786 $return = process("-elseok") if ($return eq "else");
787 }
788 if ($return eq "next") {
789 $return = skippart();
790 m2c_die("unbalanced code detected while exiting next/2 (returned $return)") if ($return ne "end");
791 # $return = "next";
792 last;
793 }
794 if (($return ne "end") && ($return ne "else")) {
795 m2c_die "unbalanced if / return $return\n";
796 }
797 } elsif (/\@\s*elseif.*\@/) { # bogus elseif
798 m2c_die "error: use 'elsif' instead of 'elseif'\n";
799 } elsif (/\@\s*els(e|if).*\@/) { # ELSE/ELSIF
800 if ($elseok != 1) {
801 chop $_;
802 m2c_die "unexpected els$1\n";
803 }
804 $return = skippart();
805 if ($return ne "end") {
806 m2c_die "unbalanced els$1 / rtn $rtn\n";
807 }
808 $return = "else";
809 last;
810 } elsif (/\@\s*next\s*\@/) { # NEXT
811 $return = skippart();
812 m2c_die "unbalanced code detected while exiting next/1 (returned $return)" if ($return ne "end");
813 $return = "next";
814 last;
815 } elsif (/\@\s*end\@/) { # END
816 $return = "end";
817 last;
818 } elsif (/\@\s*eval\s+\$(\w+)\s*=\s*([^\@]*)/) { # EVAL
819 my ($v, $e) = ($1, $2);
820 # print STDERR "eval: $e\n";
821 my $e = process_vars($e);
822 $vars{$v} = eval($e);
823 if (!defined($vars{$v})) {
824 warn "$@";
825 warn "$currentfile:$currentline [$_]\n";
826 }
827 } elsif (/\@\s*perleval\s*(.*)\@/) { # PERLEVAL
828 # print STDERR "perleval: $1\n";
829 my $res = eval($1);
830 if ($res) {
831 warn "$@";
832 warn "$currentfile:$currentline [$_]\n";
833 }
834 } elsif (/\@\s*startperl\s*\@/) { # STARTPERL
835 my $text;
836 while (get_next_line()) {
837 last if (/\@\s*endperl\s*\@/);
838 $text .= $_;
839 }
840 my $res = eval($text);
841 if ($res) {
842 warn "$@";
843 warn "$currentfile:$currentline [$_]\n";
844 }
845 # print STDERR "perleval: $1\n";
846 } elsif (/\@\s*printf\s+(\"[^\"]+\")\s*,?(.*)\@/) { # PRINTF
847 my ($f, $rest) = ($1, $2);
848 $rest = process_vars($rest);
849 my @args = split(/\s*,\s*/,$rest);
850 $f = eval $f;
851 # print STDERR "printf: $f, ", join(", ",@args),"\n";
852 foreach $file (values(%outputs)) {
853 printf $file (eval {$f}, @args);
854 }
855 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+scalars*\s*\@/) { # SCALARS
856 my $var = $1;
857 my $stash = do_tell();
858 my $scalar;
859 my @thekeys = keys(%scalars);
860 if ($#thekeys == -1) {
861 $return = skippart();
862 } else {
863 if ($havenetsnmpoid) {
864 @thekeys = sort {
865 new NetSNMP::OID($a) <=>
866 new NetSNMP::OID($b) } @thekeys;
867 }
868 foreach $scalar (@thekeys) {
869 $return = do_a_loop($stash, \$vars{$var}, $scalar,
870 \$currentscalar, $scalar,
871 \$currentvar, $scalar);
872 }
873 }
874 m2c_die("foreach did not end with \@end@") if($return ne "end");
875 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+notifications*\s*\@/) {
876 my $var = $1;
877 my $stash = do_tell();
878 my $notify;
879 my @thekeys = keys(%notifications);
880 if ($#thekeys == -1) {
881 $return = skippart();
882 } else {
883 if ($havenetsnmpoid) {
884 @thekeys = sort {
885 new NetSNMP::OID($a) <=>
886 new NetSNMP::OID($b) } @thekeys;
887 }
888 foreach $notify (@thekeys) {
889 $return = do_a_loop($stash, \$vars{$var}, $notify,
890 \$currentnotify, $notify);
891 }
892 }
893 m2c_die("foreach did not end with \@end@") if($return ne "end");
894 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+varbinds\s*\@/) {
895 my $var = $1;
896 my $stash = do_tell();
897 my $varbind;
898 if ($#{$notifyvars{$currentnotify}} == -1) {
899 $return = skippart();
900 } else {
901 foreach $varbind (@{$notifyvars{$currentnotify}}) {
902 # print "looping on $var for $varbind\n";
903 $return = do_a_loop($stash, \$vars{$var}, $varbind,
904 \$currentvarbind, $varbind,
905 \$currentvar, $varbind);
906 }
907 }
908 m2c_die("foreach did not end with \@end@") if($return ne "end");
909 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+tables*\s*\@/) {
910 my $var = $1;
911 my $stash = do_tell();
912 my $table;
913 my @thekeys = keys(%tables);
914 if ($#thekeys == -1) {
915 $return = skippart();
916 } else {
917 if ($havenetsnmpoid) {
918 @thekeys = sort {
919 new NetSNMP::OID($a) <=>
920 new NetSNMP::OID($b) } @thekeys;
921 }
922 foreach $table (@thekeys) {
923 $return = do_a_loop($stash, \$vars{$var}, $table,
924 \$currenttable, $table);
925 }
926 }
927 m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end");
928 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+stuff\s*(.*)\@/) {
929 my $var = $1;
930 my $stuff = $2;
931 my @stuff = split(/[,\s]+/, $stuff);
932 my $stash = do_tell();
933 if ($#stuff == -1) {
934 $return = skippart();
935 } else {
936 foreach $st (@stuff) {
937 $return = do_a_loop($stash, \$vars{$var}, $st,
938 \$currentstuff, $st);
939 }
940 }
941 m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end");
942 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+(column|index|internalindex|externalindex|nonindex)\s*\@/) {
943 my ($var, $type) = ($1, $2);
944 my $stash = do_tell();
945 my $column;
946 if ($#{$tables{$currenttable}{$type}} == -1) {
947 $return = skippart();
948 } else {
949 foreach $column (@{$tables{$currenttable}{$type}}) {
950 # print "looping on $var for $type -> $column\n";
951 $return = do_a_loop($stash, \$vars{$var}, $column,
952 \$currentcolumn, $column,
953 \$currentvar, $column);
954 }
955 }
956 m2c_die("foreach did not end with \@end@") if($return ne "end");
957 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+\$([^\@]+)\s+range\s+([^\@]+)\@/) {
958 my ($svar, $evar, $node) = ($1, $2, $3);
959 $svar =~ s/,$//;
960 my $stash = do_tell();
961 my $range;
962 $node = $currentcolumn if (!$node);
963 my $mibn = $SNMP::MIB{process_vars($node)};
964 die "no such mib node: $node" if (!$mibn);
965 my @ranges = @{$mibn->{'ranges'}};
966 if ($#ranges > -1) {
967 foreach $range (@ranges) {
968 $return = do_a_loop($stash, \$vars{$svar}, $range->{'low'},
969 \$vars{$evar}, $range->{'high'});
970 }
971 } else {
972 $return = skippart();
973 }
974 m2c_die("foreach did not end with \@end@") if($return ne "end");
975 } elsif (/\@\s*foreach\s+\$([^\@,]+)\s*,*\s+\$([^\@]+)\s+(enums*)\s*\@/) {
976 my ($varvar, $varval, $type) = ($1, $2, $3);
977 my $stash = do_tell();
978 my $enum, $enum2;
979
980 my @keys = sort { $SNMP::MIB{$currentvar}{'enums'}{$a} <=>
981 $SNMP::MIB{$currentvar}{'enums'}{$b} } (keys(%{$SNMP::MIB{$currentvar}{'enums'}}));
982 if ($#keys > -1) {
983 foreach $enum (@keys) {
984 ($enum2 = $enum) =~ s/-/_/g;
985 $return = do_a_loop($stash, \$vars{$varvar}, $enum2,
986 \$vars{$varval},
987 $SNMP::MIB{$currentvar}{'enums'}{$enum});
988 }
989 } else {
990 $return = skippart();
991 }
992 m2c_die("foreach did not end with \@end@") if($return ne "end");
993 } elsif (/\@\s*while([a-z]*)\s+([^@]+)\@/) { # WHILE
994 my ($type,$arg,$ok) = ($1,$2,0);
995 my $stash = do_tell();
996 my $loop = 1;
997 my $everlooped = 0;
998
999 while ($loop) {
1000 # check condition based on type
1001 if (! $type) {
1002 $ok = eval(process_vars($arg));
1003 } elsif ($type eq conf) {
1004 my $file = find_conf(process_vars($arg),1); # missingok
1005 $ok = (-f $file);
1006 } elsif ($type eq dir) {
1007 $ok = (-d $arg);
1008 } else {
1009 m2c_die "unknown while modifier ($type)\n";
1010 }
1011
1012 # act on condition
1013 if ($ok) {
1014 $return = do_a_loop($stash, \$vars{$type}, $ok, \$vars{$args});
1015 $everlooped = 1;
1016 } else {
1017 if (!$everlooped) {
1018 $return = skippart();
1019 }
1020 $loop = 0;
1021 }
1022 }
1023 } elsif (/\@\s*prompt\s+\$(\S+)\s*(.*)\@/) { # PROMPT
1024 my ($var, $prompt) = ($1, $2);
1025 if (!$term) {
1026 my $haveit = eval { require Term::ReadLine };
1027 if ($haveit) {
1028 $term = new Term::ReadLine 'mib2c';
1029 }
1030 }
1031 if ($term) {
1032 $vars{$var} = $term->readline(process_vars($prompt));
1033 }
1034 } elsif (/\@\s*print\s+([^@]*)\@/) { # PRINT
1035 my $line = process_vars($1);
1036 print "$line\n";
1037 } else {
1038 my $line = process_vars($_);
1039 mib2c_output($line);
1040 chop $_;
1041 warn "$currentfile:$currentline contained a line that started with a @ but did not match any mib2c configuration tokens.\n";
1042 warn "(maybe missing the trailing @?)\n";
1043 warn "$currentfile:$currentline [$_]\n";
1044 m2c_die if ($strict_unk_token == 1);
1045 }
1046 # $return = "eof";
1047 }
1048 print "< Balanced $balanced / level $currentlevel / rtn $return / $_\n" if($debug);
1049 if((!$_) && ($return ne "eof")) {
1050 # warn "switching return of '$return' to EOF\n" if($debug);
1051 $return = "eof";
1052 }
1053 if ($balanced) {
1054 if(($balanced != $currentlevel) || ($return ne "eof")) {
1055 m2c_die "\@balanced@ specified, but processing terminated with '$return' before EOF!";
1056 }
1057 }
1058 $currentlevel--;
1059 return $return;
1060 }
1061
1062 sub mib2c_output {
1063 my $line = shift;
1064 foreach $file (values(%outputs)) {
1065 print $file "$line";
1066 }
1067 }
1068
1069
1070 sub setup_data {
1071 my $mib = shift;
1072 if ($mib->{label} =~ /Table$/ && $#{$mib->{children}} != -1) {
1073 my $tablename = $mib->{label};
1074 my $entry = $mib->{children};
1075 my $columns = $entry->[0]{children};
1076 my $augments = $entry->[0]{'augments'};
1077 foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) {
1078 # store by numeric key so we can sort them later
1079 push @{$tables{$tablename}{'column'}}, $col->{'label'};
1080 }
1081 if ($augments) {
1082 my $mib = $SNMP::MIB{$augments} ||
1083 die "can't find info about augmented table $augments in table $tablename\n";
1084 $mib = $mib->{parent} ||
1085 die "can't find info about augmented table $augments in table $tablename\n";
1086 my $entry = $mib->{children};
1087 foreach my $index (@{$entry->[0]{'indexes'}}) {
1088 my $node = $SNMP::MIB{$index} ||
1089 die "can't find info about index $index in table $tablename\n";
1090 push @{$tables{$tablename}{'index'}}, $index;
1091 push @{$tables{$tablename}{'externalindex'}}, $index;
1092 }
1093 my $columns = $entry->[0]{children};
1094 }
1095 else {
1096 foreach my $index (@{$entry->[0]{'indexes'}}) {
1097 my $node = $SNMP::MIB{$index} ||
1098 die "can't find info about index $index in table $tablename\n";
1099 push @{$tables{$tablename}{'index'}}, $index;
1100 if("@{$tables{$tablename}{'column'}}" =~ /$index\b/ ) {
1101 # print "idx INT $index\n";
1102 push @{$tables{$tablename}{'internalindex'}}, $index;
1103 } else {
1104 # print "idx EXT $index\n";
1105 push @{$tables{$tablename}{'externalindex'}}, $index;
1106 }
1107 }
1108 }
1109 foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) {
1110 next if ( "@{$tables{$tablename}{'index'}}" =~ /$col->{'label'}\b/ );
1111 push @{$tables{$tablename}{'nonindex'}}, $col->{'label'};
1112 }
1113 # print "indexes: @{$tables{$tablename}{'index'}}\n";
1114 # print "internal indexes: @{$tables{$tablename}{'internalindex'}}\n";
1115 # print "external indexes: @{$tables{$tablename}{'externalindex'}}\n";
1116 # print "non-indexes: @{$tables{$tablename}{'nonindex'}}\n";
1117 } else {
1118 my $children = $mib->{children};
1119 if ($#children == -1 && $mib->{type}) {
1120 # scalar
1121 if ($mib->{type} eq "NOTIF" ||
1122 $mib->{type} eq "TRAP") {
1123 my $notifyname = $mib->{label};
1124 my @varlist = ();
1125 $notifications{$notifyname} = 1;
1126 $notifyvars{$notifyname} = $mib->{varbinds};
1127 } else {
1128 $scalars{$mib->{label}} = 1 if ($mib->{'access'} ne 'Notify');
1129 }
1130 } else {
1131 my $i;
1132 for($i = 0; $i <= $#$children; $i++) {
1133 setup_data($children->[$i]);
1134 }
1135 }
1136 }
1137 }
1138
1139 sub min {
1140 return $_[0] if ($_[0] < $_[1]);
1141 return $_[1];
1142 }
1143
1144 sub max {
1145 return $_[0] if ($_[0] > $_[1]);
1146 return $_[1];
1147 }
1148
1149 sub find_conf {
1150 my ($configfile, $missingok) = (@_);
1151
1152 foreach my $d (@search_dirs, @def_search_dirs) {
1153 # print STDERR "using $d/$configfile" if (-f "$d/$configfile");
1154 return "$d/$configfile" if (-f "$d/$configfile");
1155 }
1156 return $configfile if (-f "$configfile");
1157 return if ($missingok);
1158
1159 print STDERR "Can't find a configuration file called $configfile\n";
1160 print STDERR "(referenced at $currentfile:$currentline)\n" if ($currentfile);
1161 print STDERR "I looked in:\n";
1162 print " " . join("\n ", @search_dirs, @def_search_dirs), "\n";
1163 exit 1;
1164 }
1165
1166 sub open_conf {
1167 my $configfile = shift;
1168 # process .conf file
1169 if (! -f "$configfile") {
1170 print STDERR "Can't find a configuration file called $configfile\n";
1171 exit 1;
1172 }
1173 $currentfile = $configfile;
1174 my $fh = new IO::File;
1175 $fh->open("$configfile");
1176 return $fh;
1177 }
1178
1179 sub count_scalars {
1180 my @k = keys(%scalars);
1181 return $#k + 1;
1182 }
1183
1184 sub count_tables {
1185 my @k = keys(%tables);
1186 return $#k + 1;
1187 }
1188
1189 sub count_columns {
1190 my $table = shift;
1191 return $#{$tables{$table}{'column'}} + 1;
1192 }
1193
1194 sub table_is_writable {
1195 my $table = shift;
1196 my $column;
1197 my $result = 0;
1198 foreach $column (@{$tables{$table}{'column'}}) {
1199 if($SNMP::MIB{$column}{access} =~ /(ReadWrite|Create|WriteOnly)/) {
1200 $result = 1;
1201 last;
1202 }
1203 }
1204 return $result;
1205 }
1206
1207 sub table_has_create {
1208 my $table = shift;
1209 my $column;
1210 my $result = 0;
1211 foreach $column (@{$tables{$table}{'column'}}) {
1212 if($SNMP::MIB{$column}{access} =~ /(Create)/) {
1213 $result = 1;
1214 last;
1215 }
1216 }
1217 return $result;
1218 }
1219
1220 sub table_has_rowstatus {
1221 my $table = shift;
1222 my $column;
1223 my $result = 0;
1224 foreach $column (@{$tables{$table}{'column'}}) {
1225 if($SNMP::MIB{$column}{syntax} =~ /(RowStatus)/) {
1226 $result = 1;
1227 last;
1228 }
1229 }
1230 return $result;
1231 }
1232
1233 sub table_has_lastchange {
1234 my $table = shift;
1235 my $column;
1236 my $result = 0;
1237 foreach $column (@{$tables{$table}{'column'}}) {
1238 if(($SNMP::MIB{$column}{syntax} =~ /(TimeStamp)/) &&
1239 ($SNMP::MIB{$column}{label} =~ /(LastChange)/)) {
1240 $result = 1;
1241 last;
1242 }
1243 }
1244 return $result;
1245 }
1246
1247 sub table_has_storagetype {
1248 my $table = shift;
1249 my $column;
1250 my $result = 0;
1251 foreach $column (@{$tables{$table}{'column'}}) {
1252 if($SNMP::MIB{$column}{syntax} =~ /(StorageType)/) {
1253 $result = 1;
1254 last;
1255 }
1256 }
1257 return $result;
1258 }
1259
1260 sub count_indexes {
1261 my $table = shift;
1262 return $#{$tables{$table}{'index'}} + 1;
1263 }
1264
1265 sub count_external_indexes {
1266 my $table = shift;
1267 return $#{$tables{$table}{'externalindex'}} + 1;
1268 }
1269
1270 sub count_notifications {
1271 my @k = keys(%notifications);
1272 return $#k + 1;
1273 }
1274
1275 sub count_varbinds {
1276 my $notify = shift;
1277 return $#{$notifyvars{$notify}} + 1;
1278 }