blob: 2016f06805c7f67638c9e8f243fcb28c256b3325 [file] [log] [blame]
Wes Hardaker3804da62000-06-02 18:40:37 +00001#!/usr/bin/perl
2#!/usr/bin/perl -w
Wes Hardaker81fda6e1998-05-07 04:08:37 +00003
4#
Robert Story5b3c1f42003-07-25 00:55:01 +00005# $Id$
6#
Wes Hardaker81fda6e1998-05-07 04:08:37 +00007# Description:
8#
9# This program, given an OID reference as an argument, creates some
Wes Hardaker06f99a42002-03-13 04:35:07 +000010# template mib module files to be used with the net-snmp agent. It is
Wes Hardaker81fda6e1998-05-07 04:08:37 +000011# 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#
Wes Hardaker81fda6e1998-05-07 04:08:37 +000018
Wes Hardaker15ddba02001-12-05 16:28:59 +000019# SNMP
20my $havesnmp = eval {require SNMP;};
Wes Hardaker0878b922004-01-06 22:38:19 +000021my $havenetsnmpoid = eval {require NetSNMP::OID;};
Wes Hardaker15ddba02001-12-05 16:28:59 +000022
23if (!$havesnmp) {
24 print "
25ERROR: You don't have the SNMP perl module installed. Please obtain
Wes Hardaker06f99a42002-03-13 04:35:07 +000026this by getting the latest source release of the net-snmp toolkit from
Robert Storycc9eb1f2002-07-05 18:55:20 +000027http://www.net-snmp.org/download/ . Once you download the source and
28unpack it, the perl module is contained in the perl/SNMP directory.
Robert Story4bc764e2003-07-27 23:56:08 +000029See the README file there for instructions.
Wes Hardaker15ddba02001-12-05 16:28:59 +000030
31";
32 exit;
33}
34
35if ($havesnmp) {
36 eval { import SNMP; }
37}
Wes Hardaker0878b922004-01-06 22:38:19 +000038if ($havenetsnmp) {
39 eval { import NetSNMP::OID; }
40}
Wes Hardaker34124841999-04-20 04:01:15 +000041use FileHandle;
42
Wes Hardaker81fda6e1998-05-07 04:08:37 +000043#use strict 'vars';
44$SNMP::save_descriptions=1;
45$SNMP::use_long_names=1;
46$SNMP::use_enums=1;
47SNMP::initMib();
48
Wes Hardaker0486f401999-02-17 01:05:45 +000049$configfile="mib2c.conf";
Wes Hardaker572f6211999-03-05 00:32:16 +000050$debug=0;
Wes Hardaker2ece3b32001-11-22 06:33:25 +000051$quiet=0;
Robert Story02c26dc2003-09-30 13:50:01 +000052$strict_unk_token = 0;
Wes Hardaker3e26b902002-09-13 14:35:07 +000053$noindent = 0;
Robert Story3308c5a2010-04-06 01:52:45 +000054$nosed = 0;
Robert Story69073802003-07-19 17:52:49 +000055$currentline = 0;
56$currentlevel = -1;
Robert Story39c6baa2003-07-18 20:58:36 +000057%assignments;
58%outputs;
Robert Story9d0c3c72003-09-30 23:46:08 +000059@def_search_dirs = (".");
Wes Hardakerea7bc482003-09-29 22:41:58 +000060@search_dirs = ();
Robert Story9d0c3c72003-09-30 23:46:08 +000061if($ENV{MIB2C_DIR}) {
Robert Storycee05922003-12-30 23:19:02 +000062 push @def_search_dirs, split(/:/, $ENV{MIB2C_DIR});
Robert Story9d0c3c72003-09-30 23:46:08 +000063}
64push @def_search_dirs, "/usr/local/share/snmp/";
Robert Story4288fcf2004-06-18 16:41:58 +000065push @def_search_dirs, "/usr/local/share/snmp/mib2c-data";
Dave Shieldd7b9dd62004-09-10 12:30:15 +000066push @def_search_dirs, "./mib2c-conf.d";
Robert Story9d0c3c72003-09-30 23:46:08 +000067
Wes Hardaker0486f401999-02-17 01:05:45 +000068sub usage {
Wes Hardaker572f6211999-03-05 00:32:16 +000069 print "$0 [-h] [-c configfile] [-f prefix] mibNode\n\n";
70 print " -h\t\tThis message.\n\n";
Wes Hardaker0486f401999-02-17 01:05:45 +000071 print " -c configfile\tSpecifies the configuration file to use\n\t\tthat dictates what the output of mib2c will look like.\n\n";
Wes Hardakerea7bc482003-09-29 22:41:58 +000072 print " -I PATH\tSpecifies a path to look for configuration files in\n\n";
Wes Hardaker0486f401999-02-17 01:05:45 +000073 print " -f prefix\tSpecifies the output prefix to use. All code\n\t\twill be put into prefix.c and prefix.h\n\n";
Dave Shield43a5c722004-06-15 14:27:47 +000074 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";
Robert Story3308c5a2010-04-06 01:52:45 +000077 print " -s\t\tDon't look for mibNode.sed and run sed on the resulting code\n\n";
Wes Hardakerea7bc482003-09-29 22:41:58 +000078 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";
Wes Hardaker0486f401999-02-17 01:05:45 +000079 1;
80}
81
Wes Hardaker0570d3f2003-11-07 18:34:03 +000082my @origargs = @ARGV;
Robert Story97b019a2004-02-02 19:23:20 +000083my $args_done = 0;
Wes Hardaker0486f401999-02-17 01:05:45 +000084while($#ARGV >= 0) {
85 $_ = shift;
Robert Story97b019a2004-02-02 19:23:20 +000086 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);
Robert Story39c6baa2003-07-18 20:58:36 +000099 $assignments{$var} = $val;
Robert Story97b019a2004-02-02 19:23:20 +0000100 } elsif (/^-q/) {
101 $quiet = 1;
102 } elsif (/^-i/) {
103 $noindent = 1;
Robert Story3308c5a2010-04-06 01:52:45 +0000104 } elsif (/^-s/) {
105 $nosed = 1;
Robert Story97b019a2004-02-02 19:23:20 +0000106 } elsif (/^-h/) {
107 usage && exit(1);
108 } elsif (/^-f/) {
109 $outputName = shift;
110 } elsif (/^-I/) {
Wes Hardakerea7bc482003-09-29 22:41:58 +0000111 my $dirs = shift;
112 push @search_dirs, split(/,/,$dirs);
Robert Story97b019a2004-02-02 19:23:20 +0000113 } 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 = $_ ;
Wes Hardakerea7bc482003-09-29 22:41:58 +0000122 }
Wes Hardaker0486f401999-02-17 01:05:45 +0000123}
Wes Hardaker572f6211999-03-05 00:32:16 +0000124
Wes Hardaker81fda6e1998-05-07 04:08:37 +0000125#
126# internal conversion tables
127#
Wes Hardakere44e8c91998-05-11 15:09:58 +0000128
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000129%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
Wes Hardakerefdce812001-12-11 15:34:28 +0000141 BITS ASN_OCTET_STR
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000142 TICKS ASN_TIMETICKS
143 GAUGE ASN_GAUGE
144 OPAQUE ASN_OPAQUE);
Wes Hardaker9cd215b2001-12-20 00:33:40 +0000145%perltodecl = ("OCTETSTR", "char",
146 "INTEGER", "long",
147 "INTEGER32", "long",
148 "UNSIGNED32", "u_long",
149 "UINTEGER", "u_long",
150 "OBJECTID", "oid",
Robert Storyf339acf2003-10-14 18:18:33 +0000151 "COUNTER64", "U64",
Wes Hardaker9cd215b2001-12-20 00:33:40 +0000152 "COUNTER", "u_long",
Jan Safraneka9256132009-07-10 07:46:43 +0000153 "IPADDR", "in_addr_t",
Wes Hardaker9cd215b2001-12-20 00:33:40 +0000154 "BITS", "char",
155 "TICKS", "u_long",
156 "GAUGE", "u_long",
Robert Story39c6baa2003-07-18 20:58:36 +0000157 "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");
Wes Hardaker81fda6e1998-05-07 04:08:37 +0000171
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000172my $mibnode = $SNMP::MIB{$oid};
Wes Hardaker0570d3f2003-11-07 18:34:03 +0000173
174if (!$mibnode) {
175
176print STDERR "
177You didn't give mib2c a valid OID to start with. IE, I could not find
178any information about the mib node \"$oid\". This could be caused
179because you supplied an incorrectly node, or by the MIB that you're
180trying to generate code from isn't loaded. To make sure your mib is
181loaded, run mib2c using this as an example:
182
183 env MIBS=\"+MY-PERSONAL-MIB\" mib2c " . join(" ",@origargs) . "
184
185You 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
189And making sure you can get snmptranslate to display information about
190your MIB node. Once snmptranslate works, then come back and try mib2c
191again.
192
193";
Robert Story97b019a2004-02-02 19:23:20 +0000194exit 1;
Wes Hardaker0570d3f2003-11-07 18:34:03 +0000195}
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000196
197# setup
198$outputName = $mibnode->{'label'} if (!defined($outputName));
Dave Shield1cb47322008-03-24 17:54:20 +0000199$outputName =~ s/-/_/g;
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000200$vars{'name'} = $outputName;
201$vars{'oid'} = $oid;
Robert Storyff17cee2004-06-07 18:10:29 +0000202$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" ***************************************************/";
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000210
211# loop through mib nodes, remembering stuff.
212setup_data($mibnode);
213
Robert Storyf339acf2003-10-14 18:18:33 +0000214if(($ENV{HOME}) && (-f "$ENV{HOME}/.snmp/mib2c.conf")) {
215 $fh = open_conf("$ENV{HOME}/.snmp/mib2c.conf");
Robert Story97b019a2004-02-02 19:23:20 +0000216 process("-balanced");
Robert Storyf339acf2003-10-14 18:18:33 +0000217 $fh->close;
218}
219
Robert Story69073802003-07-19 17:52:49 +0000220my $defaults = find_conf("default-$configfile",1);
221if (-f "$defaults" ) {
Robert Story39c6baa2003-07-18 20:58:36 +0000222 $fh = open_conf($defaults);
Robert Story97b019a2004-02-02 19:23:20 +0000223 process("-balanced");
Robert Story39c6baa2003-07-18 20:58:36 +0000224 $fh->close;
225}
226
227my @theassignments = keys(%assignments);
228if ($#theassignments != -1) {
229 foreach $var (@theassignments) {
230 $vars{$var} = $assignments{$var};
231 }
232}
Robert Story69073802003-07-19 17:52:49 +0000233$configfile = find_conf($configfile,0);
Wes Hardaker1dc2dc82003-02-20 00:51:45 +0000234$fh = open_conf($configfile);
Robert Story97b019a2004-02-02 19:23:20 +0000235process("-balanced");
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000236$fh->close;
237
Robert Story3308c5a2010-04-06 01:52:45 +0000238if (-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
Wes Hardaker3e26b902002-09-13 14:35:07 +0000247if (!$noindent) {
248 foreach $i (keys(%written)) {
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000249 next if ($i eq "-");
Robert Storycb9314e2003-08-17 22:19:32 +0000250 next if (!($i =~ /\.[ch]$/));
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000251 print STDERR "running indent on $i\n" if (!$quiet);
Dave Shield740721d2004-05-04 08:13:42 +0000252 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");
Wes Hardaker3e26b902002-09-13 14:35:07 +0000253 }
Wes Hardaker81fda6e1998-05-07 04:08:37 +0000254}
255
Robert Story97b019a2004-02-02 19:23:20 +0000256sub m2c_die {
257 warn "ERROR: ". $_[0] . "\n";
258 die " at $currentfile:$currentline\n";
259}
260
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000261sub tocommas {
262 my $oid = $_[0];
263 $oid =~ s/\./,/g;
264 $oid =~ s/^\s*,//;
265 return $oid;
Wes Hardaker81fda6e1998-05-07 04:08:37 +0000266}
Wes Hardaker18febdb1999-02-17 23:16:17 +0000267
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000268sub oidlength {
Robert Storyf2e2a872003-07-25 02:29:15 +0000269 return (scalar split(/\./, $_[0])) - 1;
Wes Hardaker18febdb1999-02-17 23:16:17 +0000270}
Wes Hardaker18febdb1999-02-17 23:16:17 +0000271
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000272# replaces $VAR type expressions and $VAR.subcomponent expressions
273# with data from the mib tree and loop variables.
274# possible uses:
Wes Hardaker8e9f1261999-04-20 22:31:35 +0000275#
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000276# $var -- as defined by loops, etc.
277# ${var}otherstuff -- appending text to variable contents
278# $var.uc -- all upper case version of $var
Wes Hardaker8e9f1261999-04-20 22:31:35 +0000279#
Wes Hardaker68db6402004-04-29 00:23:35 +0000280# NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages
281#
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000282# Mib components, $var must first expand to a mib node name:
283#
Wes Hardaker008e6762004-04-28 23:44:43 +0000284# $var.uc -- all upper case version of $var
Wes Hardaker66762ea2004-04-28 23:58:36 +0000285#
286# $var.objectID -- dotted, fully-qualified, and numeric OID
287# $var.commaoid -- comma separated numeric OID for array initialization
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000288# $var.oidlength -- length of the oid
Wes Hardaker66762ea2004-04-28 23:58:36 +0000289# $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#
Dave Shield339f1422004-08-23 09:29:30 +0000301# $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
Robert Story16d9e332001-12-11 04:43:07 +0000304# $var.noaccess -- 1 if not-accessible, 0 if not
Robert Story39c6baa2003-07-18 20:58:36 +0000305# $var.accessible -- 1 if accessible, 0 if not
Robert Storyda33a022010-02-12 02:57:46 +0000306# $var.storagetype -- 1 if an object is a StorageType object, 0 if not
Dave Shield339f1422004-08-23 09:29:30 +0000307# $var.rowstatus -- 1 if an object is a RowStatus object, 0 if not
Robert Storyda33a022010-02-12 02:57:46 +0000308# '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
Wes Hardaker66762ea2004-04-28 23:58:36 +0000311#
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.
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000318# $var.access -- node's access type
319# $var.status -- node's status
320# $var.syntax -- node's syntax
Wes Hardaker554ead72004-06-04 20:37:42 +0000321# $var.reference -- node's reference
Robert Story4cd414d2004-06-18 02:09:47 +0000322# $var.description -- node's description
Wes Hardakerf9bb4792003-06-26 06:15:18 +0000323
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000324sub process_vars {
325 my $it = shift;
326
327 # mib substitutions ($var.type -> $mibnode->{'type'})
Robert Storyff17cee2004-06-07 18:10:29 +0000328 if ( $it =~ /\$(\w+)\.(\w+)/ ) {
Dave Shield0f649782011-04-14 20:52:59 +0000329 if ($SNMP::MIB{$vars{$1}} && defined($tables{$SNMP::MIB{$vars{$1}}{'label'}})) {
Dave Shield339f1422004-08-23 09:29:30 +0000330 $it =~ s/\$(\w+)\.(settable)/(table_is_writable($SNMP::MIB{$vars{$1}}{label}))/eg;
331 $it =~ s/\$(\w+)\.(creatable)/(table_has_create($SNMP::MIB{$vars{$1}}{label}))/eg;
332 $it =~ s/\$(\w+)\.(rowstatus)/(table_has_rowstatus($SNMP::MIB{$vars{$1}}{label}))/eg;
Robert Storyc3acd7a2005-03-18 22:46:44 +0000333 $it =~ s/\$(\w+)\.(lastchange)/(table_has_lastchange($SNMP::MIB{$vars{$1}}{label}))/eg;
334 $it =~ s/\$(\w+)\.(storagetype)/(table_has_storagetype($SNMP::MIB{$vars{$1}}{label}))/eg;
Dave Shield339f1422004-08-23 09:29:30 +0000335 }
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000336 $it =~ s/\$(\w+)\.(uc)/uc($vars{$1})/eg; # make something uppercase
Wes Hardaker9cd215b2001-12-20 00:33:40 +0000337 $it =~ s/\$(\w+)\.(commaoid)/tocommas($SNMP::MIB{$vars{$1}}{objectID})/eg;
338 $it =~ s/\$(\w+)\.(oidlength)/oidlength($SNMP::MIB{$vars{$1}}{objectID})/eg;
Wes Hardaker39851572003-11-25 23:48:29 +0000339 $it =~ s/\$(\w+)\.(description)/$SNMP::MIB{$vars{$1}}{description}/g;
Wes Hardaker9cd215b2001-12-20 00:33:40 +0000340 $it =~ s/\$(\w+)\.(perltype)/$SNMP::MIB{$vars{$1}}{type}/g;
341 $it =~ s/\$(\w+)\.(type)/$perltoctypes{$SNMP::MIB{$vars{$1}}{$2}}/g;
342 $it =~ s/\$(\w+)\.(subid)/$SNMP::MIB{$vars{$1}}{subID}/g;
Robert Story39c6baa2003-07-18 20:58:36 +0000343 $it =~ s/\$(\w+)\.(module)/$SNMP::MIB{$vars{$1}}{moduleID}/g;
Dave Shieldf21cd5f2003-04-25 15:01:54 +0000344 $it =~ s/\$(\w+)\.(settable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(ReadWrite|Create|WriteOnly)\/)?1:0)/eg;
Dave Shield339f1422004-08-23 09:29:30 +0000345 $it =~ s/\$(\w+)\.(creatable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Create)\/)?1:0)/eg;
Robert Story39c6baa2003-07-18 20:58:36 +0000346 $it =~ s/\$(\w+)\.(readable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Read|Create)\/)?1:0)/eg;
Wes Hardaker9cd215b2001-12-20 00:33:40 +0000347 $it =~ s/\$(\w+)\.(noaccess)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(NoAccess)\/)?1:0)/eg;
Robert Story39c6baa2003-07-18 20:58:36 +0000348 $it =~ s/\$(\w+)\.(accessible)/(($SNMP::MIB{$vars{$1}}{access} !~ \/(NoAccess)\/)?1:0)/eg;
Wes Hardaker554ead72004-06-04 20:37:42 +0000349 $it =~ s/\$(\w+)\.(objectID|label|subID|access|status|syntax|reference)/$SNMP::MIB{$vars{$1}}{$2}/g;
Dave Shield991b95b2002-07-18 15:22:24 +0000350 $it =~ s/\$(\w+)\.(decl)/$perltodecl{$SNMP::MIB{$vars{$1}}{type}}/g;
Robert Story39c6baa2003-07-18 20:58:36 +0000351 $it =~ s/\$(\w+)\.(needlength)/$perltolen{$SNMP::MIB{$vars{$1}}{type}}/g;
Wes Hardaker6273b8a2003-06-25 21:41:40 +0000352 $it =~ s/\$(\w+)\.(iscolumn)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} =~ \/Entry$\/) ? 1 : 0/eg;
Wes Hardakerf9bb4792003-06-26 06:15:18 +0000353 $it =~ s/\$(\w+)\.(isscalar)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} !~ \/Entry$\/ && $SNMP::MIB{$vars{$1}}{access}) ? 1 : 0/eg;
Wes Hardaker6273b8a2003-06-25 21:41:40 +0000354 $it =~ s/\$(\w+)\.(parent)/$SNMP::MIB{$vars{$1}}{'parent'}{'label'}/g;
Robert Story39c6baa2003-07-18 20:58:36 +0000355 $it =~ s/\$(\w+)\.(children)/($#{$SNMP::MIB{$vars{$1}}{'children'}} == 0) ? 0 : 1/eg;
356 $it =~ s/\$(\w+)\.(hasdefval)/(length($SNMP::MIB{$vars{$1}}{'defaultValue'}) == 0) ? 0 : 1/eg;
357 $it =~ s/\$(\w+)\.(defval)/$SNMP::MIB{$vars{$1}}{'defaultValue'}/g;
358 $it =~ s/\$(\w+)\.(hashint)/(length($SNMP::MIB{$vars{$1}}{'hint'}) == 0) ? 0 : 1/eg;
359 $it =~ s/\$(\w+)\.(hint)/$SNMP::MIB{$vars{$1}}{'hint'}/g;
Robert Story0ddde4c2003-07-30 17:51:39 +0000360 $it =~ s/\$(\w+)\.(ranges)/($#{$SNMP::MIB{$vars{$1}}{'ranges'}} == -1) ? 0 : 1/eg;
Robert Story744c1232003-07-16 04:50:53 +0000361 # check for enums
362 $it =~ s/\$(\w+)\.(enums)/(%{$SNMP::MIB{$vars{$1}}{'enums'}} == 0) ? 0 : 1/eg;
Robert Story39c6baa2003-07-18 20:58:36 +0000363 $it =~ s/\$(\w+)\.(enumrange)/%{$SNMP::MIB{$vars{$1}}{'enums'}}/eg;
Dave Shield339f1422004-08-23 09:29:30 +0000364 $it =~ s/\$(\w+)\.(rowstatus)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(RowStatus)\/)?1:0)/eg;
Robert Storyda33a022010-02-12 02:57:46 +0000365 $it =~ s/\$(\w+)\.(storagetype)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(StorageType)\/)?1:0)/eg;
Robert Story87662062003-07-23 23:43:09 +0000366 if ( $it =~ /\$(\w+)\.(\w+)/ ) {
367 warn "Possible unknown variable attribute \$$1.$2 at $currentfile:$currentline\n";
368 }
Robert Storyff17cee2004-06-07 18:10:29 +0000369 }
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000370 # normal variable substitions
371 $it =~ s/\$\{(\w+)\}/$vars{$1}/g;
372 $it =~ s/\$(\w+)/$vars{$1}/g;
Robert Story0ddde4c2003-07-30 17:51:39 +0000373 # use $@var to put literal '$var'
374 $it =~ s/\$\@(\w+)/\$$1/g;
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000375 return $it;
376}
377
378# process various types of statements
379#
Wes Hardaker68db6402004-04-29 00:23:35 +0000380# NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000381# which include:
382# @open FILE@
383# writes generated output to FILE
Wes Hardaker68db6402004-04-29 00:23:35 +0000384# note that for file specifications, opening '-' will print to stdout.
385# @append FILE@
386# appends the given FILE
387# @close FILE@
388# closes the given FILE
Robert Storye796e882003-12-23 00:56:21 +0000389# @push@
Wes Hardaker008e6762004-04-28 23:44:43 +0000390# save the current outputs, then clear outputs. Use with @open@
391# and @pop@ to write to a new file without interfering with current
392# outputs.
Robert Storye796e882003-12-23 00:56:21 +0000393# @pop@
394# pop up the process() stack one level. Use after a @push@ to return to
395# the previous set of open files.
Wes Hardakere89b8e62002-05-24 16:53:07 +0000396# @foreach $VAR scalar@
397# repeat iterate over code until @end@ setting $VAR to all known scalars
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000398# @foreach $VAR table@
399# repeat iterate over code until @end@ setting $VAR to all known tables
400# @foreach $VAR column@
401# repeat iterate over code until @end@ setting $VAR to all known
402# columns within a given table. Obviously this must be called
403# within a foreach-table clause.
Robert Story5b3c1f42003-07-25 00:55:01 +0000404# @foreach $VAR nonindex@
405# repeat iterate over code until @end@ setting $VAR to all known
406# non-index columns within a given table. Obviously this must be called
407# within a foreach-table clause.
Robert Storye796e882003-12-23 00:56:21 +0000408# @foreach $VAR internalindex@
409# repeat iterate over code until @end@ setting $VAR to all known internal
410# index columns within a given table. Obviously this must be called
411# within a foreach-table clause.
412# @foreach $VAR externalindex@
413# repeat iterate over code until @end@ setting $VAR to all known external
414# index columns within a given table. Obviously this must be called
415# within a foreach-table clause.
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000416# @foreach $VAR index@
417# repeat iterate over code until @end@ setting $VAR to all known
418# indexes within a given table. Obviously this must be called
419# within a foreach-table clause.
Dave Shieldd10f4e82003-06-24 13:20:14 +0000420# @foreach $VAR notifications@
421# repeat iterate over code until @end@ setting $VAR to all known notifications
422# @foreach $VAR varbinds@
423# repeat iterate over code until @end@ setting $VAR to all known varbinds
424# Obviously this must be called within a foreach-notifications clause.
Wes Hardaker90261672003-02-06 06:05:56 +0000425# @foreach $LABEL, $VALUE enum@
426# repeat iterate over code until @end@ setting $LABEL and $VALUE
427# to the label and values from the enum list.
428# @foreach $RANGE_START, $RANGE_END range NODE@
429# repeat iterate over code until @end@ setting $RANGE_START and $RANGE_END
430# to the legal accepted range set for a given mib NODE.
Wes Hardaker5dd7d132004-05-19 20:12:52 +0000431# @foreach $var stuff a b c d@
432# repeat iterate over values a, b, c, d as assigned generically
433# (ie, the values are taken straight from the list with no
434# mib-expansion, etc).
Dave Shieldf3020102005-07-28 11:47:58 +0000435# @while expression@
436# repeat iterate over code until the expression is false
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000437# @eval $VAR = expression@
Wes Hardaker68db6402004-04-29 00:23:35 +0000438# evaluates expression and assigns the results to $VAR. This is
439# not a full perl eval, but sort of a "psuedo" eval useful for
440# simple expressions while keeping the same variable name space.
441# See below for a full-blown export to perl.
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000442# @perleval STUFF@
443# evaluates STUFF directly in perl. Note that all mib2c variables
Robert Storyc3acd7a2005-03-18 22:46:44 +0000444# interpereted within .conf files are in $vars{NAME} and that
445# a warning will be printed if STUFF does not return 0. (adding a
446# 'return 0;' at the end of STUFF is a workaround.
Wes Hardaker008e6762004-04-28 23:44:43 +0000447# @startperl@
448# @endperl@
449# treats everything between these tags as perl code, and evaluates it.
Robert Story97b019a2004-02-02 19:23:20 +0000450# @next@
451# restart foreach; should only be used inside a conditional.
452# skips out of current conditional, then continues to skip to
453# end for the current foreach clause.
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000454# @if expression@
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000455# evaluates expression, and if expression is true processes
Robert Storyc3acd7a2005-03-18 22:46:44 +0000456# contained part until appropriate @end@ is reached. If the
457# expression is false, the next @elsif expression@ expression
458# (if it exists) will be evaluated, until an expression is
459# true. If no such expression exists and an @else@
460# clause is found, it will be evaluated.
461# @ifconf file@
462# If the specified file can be found in the conf file search path,
463# and if found processes contained part until an appropriate @end@ is
464# found. As with a regular @if expression@, @elsif expression@ and
465# @else@ can be used.
466# @ifdir dir@
467# If the specified directory exists, process contained part until an
468# appropriate @end@ is found. As with a regular @if expression@,
469# @elsif expression@ and @else@ can be used.
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000470# @define NAME@
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000471# @enddefine@
Wes Hardaker008e6762004-04-28 23:44:43 +0000472# Memorizes "stuff" between the define and enddefine tags for
473# later calling as NAME by @calldefine NAME@.
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000474# @calldefine NAME@
475# Executes stuff previously memorized as NAME.
Wes Hardaker68db6402004-04-29 00:23:35 +0000476# @printf "expression" stuff1, stuff2, ...@
477# Like all the other printf's you know and love.
478# @run FILE@
479# Sources the contents of FILE as a mib2c file,
480# but does not affect current files opened.
481# @include FILE@
482# Sources the contents of FILE as a mib2c file and appends its
483# output to the current output.
484# @prompt $var QUESTION@
485# Presents the user with QUESTION, expects a response and puts it in $var
486# @print STUFF@
487# Prints stuff directly to the users screen (ie, not to where
488# normal mib2c output goes)
Dave Shieldf3020102005-07-28 11:47:58 +0000489# @quit@
490# Bail out (silently)
Wes Hardaker68db6402004-04-29 00:23:35 +0000491# @exit@
492# Bail out!
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000493#
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000494sub skippart {
Robert Story69073802003-07-19 17:52:49 +0000495 my $endcount = 1;
496 my $arg = shift;
497 my $rtnelse = 0;
498 while ($arg =~ s/-(\w+)\s*//) {
499 $rtnelse = 1 if ($1 eq "else");
500 }
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000501 while(get_next_line()) {
Robert Story69073802003-07-19 17:52:49 +0000502 $currentline++;
503 $_ = process_vars($_) if ($debug);
Robert Story97b019a2004-02-02 19:23:20 +0000504 print "$currentfile.$currentline:P$currentlevel:S$endcount.$rtnelse:$_" if ($debug);
Robert Story87662062003-07-23 23:43:09 +0000505 next if ( /^\s*\#\#/ ); # noop, it's a comment
506 next if (! /^\s*\@/ ); # output
507 if (! /^\s*\@.*\@/ ) {
508 warn "$currentfile:$currentline contained a line that started with a @ but did not match any mib2c configuration tokens.\n";
509 warn "(maybe missing the trailing @?)\n";
510 warn "$currentfile:$currentline [$_]\n";
511 }
512 elsif (/\@\s*end\@/) {
Robert Story69073802003-07-19 17:52:49 +0000513 return "end" if ($endcount == 1);
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000514 $endcount--;
515 }
Robert Story97b019a2004-02-02 19:23:20 +0000516 elsif (/\@\s*elseif.*\@/) {
517 m2c_die "use 'elsif' instead of 'elseif'\n";
518 }
Robert Story69073802003-07-19 17:52:49 +0000519 elsif (/\@\s*else\@/) {
520 return "else" if (($endcount == 1) && ($rtnelse == 1));
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000521 }
Robert Story69073802003-07-19 17:52:49 +0000522 elsif (/\@\s*elsif\s+([^\@]+)\@/) {
523 return "else" if (($endcount == 1) && ($rtnelse == 1) && (eval(process_vars($1))));
524 }
Dave Shielda6e73352007-01-02 16:49:07 +0000525 elsif (/\@\s*(foreach|if|while)/) {
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000526 $endcount++;
527 }
Wes Hardaker8e9f1261999-04-20 22:31:35 +0000528 }
Robert Story97b019a2004-02-02 19:23:20 +0000529 print "skippart EOF\n";
530 m2c_die "unbalanced code detected in skippart: EOF when $endcount levels deep" if($endcount != 1);
531 return "eof";
Wes Hardaker8e9f1261999-04-20 22:31:35 +0000532}
Wes Hardaker572f6211999-03-05 00:32:16 +0000533
Robert Story39c6baa2003-07-18 20:58:36 +0000534sub close_file {
535 my $name = shift;
536 if (!$name) {
537 print "close_file w/out name!\n";
538 return;
539 }
540 if(!$outputs{$name}) {
541 print "no handle for $name\n";
542 return;
543 }
544 $outputs{$name}->close();
545 delete $outputs{$name};
546# print STDERR "closing $name\n" if (!$quiet);
547}
548
549sub close_files {
550 foreach $name (keys(%outputs)) {
551 close_file($name);
552 }
553}
554
555sub open_file {
556 my $multiple = shift;
557 my $spec = shift;
558 my $name = $spec;
559 $name =~ s/>//;
560 if ($multiple == 0) {
561 close_files();
562 }
Robert Story87662062003-07-23 23:43:09 +0000563 return if ($outputs{$name});
Robert Story39c6baa2003-07-18 20:58:36 +0000564 $outputs{$name} = new IO::File;
Robert Story97b019a2004-02-02 19:23:20 +0000565 $outputs{$name}->open(">$spec") || m2c_die "failed to open $name";
Robert Story39c6baa2003-07-18 20:58:36 +0000566 print STDERR "writing to $name\n" if (!$quiet && !$written{$name});
567 $written{$name} = '1';
568}
Wes Hardaker81fda6e1998-05-07 04:08:37 +0000569
Robert Story69073802003-07-19 17:52:49 +0000570sub process_file {
Robert Story87662062003-07-23 23:43:09 +0000571 my ($file, $missingok, $keepvars) = (@_);
Robert Story69073802003-07-19 17:52:49 +0000572 my $oldfh = $fh;
573 my $oldfile = $currentfile;
574 my $oldline = $currentline;
575 # keep old copy of @vars and just build on it.
Robert Story87662062003-07-23 23:43:09 +0000576 my %oldvars;
577
578 %oldvars = %vars if ($keepvars != 1);
Robert Story39c6baa2003-07-18 20:58:36 +0000579
Robert Story69073802003-07-19 17:52:49 +0000580 $file = find_conf($file,$missingok);
581 return if (! $file);
582
583 $fh = open_conf($file);
584 $currentline = 0;
Robert Story97b019a2004-02-02 19:23:20 +0000585 process("-balanced");
Robert Story69073802003-07-19 17:52:49 +0000586 $fh->close();
587
588 $fh = $oldfh;
589 $currentfile = $oldfile;
590 $currentline = $oldline;
591
592 # don't keep values in replaced vars. Revert to ours.
Robert Story87662062003-07-23 23:43:09 +0000593 %vars = %oldvars if ($keepvars != 1);
Robert Story69073802003-07-19 17:52:49 +0000594}
595
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000596sub get_next_line {
597 if ($#process_lines > -1) {
598 return $_ = shift @process_lines;
599 }
600 return $_ = <$fh>;
601}
602
603sub do_tell {
604 my $stash;
605 $stash->{'startpos'} = $fh->tell();
606 $stash->{'startline'} = $currentline;
607 @{$stash->{'lines'}} = @process_lines;
608 return $stash;
609}
610
611sub do_seek {
612 my $stash = shift;
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000613
614 # save current line number
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000615 $currentline = $stash->{'startline'};
616 $fh->seek($stash->{'startpos'}, 0); # go to top of section.
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000617
618 # save current process_lines state.
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000619 @process_lines = @{$stash->{'lines'}};
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000620
621 # save state of a number of variables (references), and new assignments
622 for (my $i = 0; $i <= $#_; $i += 2) {
623 push @{$stash->{'vars'}}, $_[$i], ${$_[$i]};
624 ${$_[$i]} = $_[$i+1];
625 }
626}
627
628sub do_unseek {
629 my $stash = shift;
630 for (my $i = 0; $i <= $#{$stash->{'vars'}}; $i += 2) {
631 ${$stash->{'vars'}[$i]} = $stash->{'vars'}[$i+1];
632 }
633}
634
635sub do_a_loop {
636 my $stash = shift;
637 do_seek($stash, @_);
638 my $return = process();
639 do_unseek($stash);
640 return $return;
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000641}
642
Robert Story69073802003-07-19 17:52:49 +0000643sub process {
644 my $arg = shift;
645 my $elseok = 0;
Robert Story97b019a2004-02-02 19:23:20 +0000646 my $balanced = 0;
647 my $startlevel;
648 my $return = "eof";
Robert Story69073802003-07-19 17:52:49 +0000649 while ($arg =~ s/-(\w+)\s*//) {
650 $elseok = 1 if ($1 eq "elseok");
Robert Story97b019a2004-02-02 19:23:20 +0000651 $balanced = 1 if ($1 eq "balanced");
Robert Story69073802003-07-19 17:52:49 +0000652 }
653
654 $currentlevel++;
Robert Story97b019a2004-02-02 19:23:20 +0000655 $startlevel = $currentlevel;
656 if($balanced) {
657 $balanced = $currentlevel;
658 }
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000659 while(get_next_line()) {
Robert Story69073802003-07-19 17:52:49 +0000660 $currentline++;
661 if ($debug) {
662# my $line = process_vars($_);
663# chop $line;
Robert Story97b019a2004-02-02 19:23:20 +0000664 print "$currentfile.$currentline:P$currentlevel.$elseok:$return:$_";
Robert Story69073802003-07-19 17:52:49 +0000665 }
666
667 next if (/^\s*\#\#/); # noop, it's a comment
Robert Story87662062003-07-23 23:43:09 +0000668 if (! /^\s*\@/ ) { # output
Robert Story69073802003-07-19 17:52:49 +0000669 my $line = process_vars($_);
670 foreach $file (values(%outputs)) {
671 print $file "$line";
672 }
673 } ####################################################################
674 elsif (/\@\s*exit\@/) { # EXIT
Robert Storyc26d6442004-07-08 22:38:14 +0000675 close_files;
Robert Story69073802003-07-19 17:52:49 +0000676 die "exiting at conf file ($currentfile:$currentline) request\n";
Dave Shieldf3020102005-07-28 11:47:58 +0000677 } elsif (/\@\s*quit\@/) { # QUIT
678 close_files;
679 exit;
Robert Storyc6a4c082003-09-11 22:47:50 +0000680 } elsif (/\@\s*debug\s+([^\@]+)\@/) { # DEBUG
681 if ($1 eq "on") {
682 $debug = 1;
683 }
684 else {
685 $debug = 0;
686 }
Robert Story02c26dc2003-09-30 13:50:01 +0000687 } elsif (/\@\s*strict token\s+([^\@]+)\@/) { # STRICT
688 if ($1 eq "on") {
689 $strict_unk_token = 1;
690 }
691 else {
692 $strict_unk_token = 0;
693 }
Robert Story97b019a2004-02-02 19:23:20 +0000694 } elsif (/\@\s*balanced\@/) { # BALANCED
695 $balanced = $currentlevel;
Robert Story69073802003-07-19 17:52:49 +0000696 } elsif (/\@\s*open\s+([^\@]+)\@/) { # OPEN
Robert Story39c6baa2003-07-18 20:58:36 +0000697 my $arg = $1;
698 my ($multiple) = (0);
699 while ($arg =~ s/-(\w+)\s+//) {
700 $multiple = 1 if ($1 eq 'multiple');
701 }
702 my $spec = process_vars($arg);
703 open_file($multiple, $spec);
Robert Story69073802003-07-19 17:52:49 +0000704 } elsif (/\@\s*close\s+([^\@]+)\@/) { # CLOSE
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000705 my $spec = process_vars($1);
Robert Story39c6baa2003-07-18 20:58:36 +0000706 close_file($spec);
Robert Story69073802003-07-19 17:52:49 +0000707 } elsif (/\@\s*append\s+([^\@]+)\@/) { # APPEND
Robert Story39c6baa2003-07-18 20:58:36 +0000708 my $arg = $1;
709 my ($multiple) = (0);
710 while ($arg =~ s/-(\w+)\s+//) {
711 $multiple = 1 if ($1 eq 'multiple');
712 }
713 my $spec = process_vars($arg);
714 $spec=">$spec";
715 open_file($multiple,$spec);
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000716 } elsif (/\@\s*define\s*(.*)\@/) { # DEFINE
717 my $it = $1;
718 while (<$fh>) {
719 last if (/\@\s*enddefine\s*@/);
720 push @{$defines{$it}}, $_;
721 }
Wes Hardaker5dd7d132004-05-19 20:12:52 +0000722 } elsif (/\@\s*calldefine\s+(\w+)@/) {
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000723 if ($#{$defines{$1}} == -1) {
724 warn "called a define of $1 which didn't exist\n";
725 warn "$currentfile:$currentline [$_]\n";
726 } else {
Wes Hardaker5dd7d132004-05-19 20:12:52 +0000727 unshift @process_lines, @{$defines{$1}};
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000728 }
729 } elsif (/\@\s*run (.*)\@/) { # RUN
Robert Story39c6baa2003-07-18 20:58:36 +0000730 my $arg = $1;
731 my ($again) = (0);
732 while ($arg =~ s/-(\w+)\s+//) {
733 $again = 1 if ($1 eq 'again');
734# if ($1 eq 'file') {
735# my ($filearg) = ($arg =~ s/^(\w+)//);
736# }
737 }
Robert Story87662062003-07-23 23:43:09 +0000738 my $spec = process_vars($arg);
739 next if (!$again && $ranalready{$spec});
740 $ranalready{$spec} = 1;
Robert Story39c6baa2003-07-18 20:58:36 +0000741 my %oldout = %outputs;
742 my %emptyarray;
743 %outputs = %emptyoutputs;
Robert Story87662062003-07-23 23:43:09 +0000744 process_file($spec,0,0);
745 close_files;
Robert Story39c6baa2003-07-18 20:58:36 +0000746 %outputs = %oldout;
Robert Storye796e882003-12-23 00:56:21 +0000747 } elsif (/\@\s*push\@/) { # PUSH
748 my %oldout = %outputs;
749 my %emptyarray;
750 %outputs = %emptyoutputs;
751 process($arg);
752 close_files;
753 %outputs = %oldout;
754 } elsif (/\@\s*pop\s*\@/) { # POP
Robert Story97b019a2004-02-02 19:23:20 +0000755 $return = "pop";
756 last;
Robert Story69073802003-07-19 17:52:49 +0000757 } elsif (/\@\s*include (.*)\@/) { # INCLUDE
758 my $arg = $1;
759 my ($missingok) = (0);
760 while ($arg =~ s/-(\w+)\s+//) {
761 $missingok = 1 if ($1 eq 'ifexists');
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000762 }
Robert Story87662062003-07-23 23:43:09 +0000763 my $spec = process_vars($arg);
764 process_file($spec,$missingok,1);
Robert Story69073802003-07-19 17:52:49 +0000765 } elsif (/\@\s*if([a-z]*)\s+([^@]+)\@/) { # IF
766 my ($type,$arg,$ok) = ($1,$2,0);
767 # check condition based on type
768 if (! $type) {
769 $ok = eval(process_vars($arg));
770 } elsif ($type eq conf) {
Robert Story87662062003-07-23 23:43:09 +0000771 my $file = find_conf(process_vars($arg),1); # missingok
Robert Story69073802003-07-19 17:52:49 +0000772 $ok = (-f $file);
Robert Storyc3acd7a2005-03-18 22:46:44 +0000773 } elsif ($type eq dir) {
774 $ok = (-d $arg);
Robert Story69073802003-07-19 17:52:49 +0000775 } else {
Robert Story97b019a2004-02-02 19:23:20 +0000776 m2c_die "unknown if modifier ($type)\n";
Robert Story69073802003-07-19 17:52:49 +0000777 }
Robert Story69073802003-07-19 17:52:49 +0000778 # act on condition
779 if ($ok) {
Robert Story97b019a2004-02-02 19:23:20 +0000780 $return = process("-elseok");
Robert Story69073802003-07-19 17:52:49 +0000781 } else {
Robert Story97b019a2004-02-02 19:23:20 +0000782 $return = skippart("-else");
783 $return = process("-elseok") if ($return eq "else");
Robert Story69073802003-07-19 17:52:49 +0000784 }
Robert Story97b019a2004-02-02 19:23:20 +0000785 if ($return eq "next") {
786 $return = skippart();
787 m2c_die("unbalanced code detected while exiting next/2 (returned $return)") if ($return ne "end");
788# $return = "next";
789 last;
Robert Story69073802003-07-19 17:52:49 +0000790 }
Robert Story97b019a2004-02-02 19:23:20 +0000791 if (($return ne "end") && ($return ne "else")) {
792 m2c_die "unbalanced if / return $return\n";
793 }
794 } elsif (/\@\s*elseif.*\@/) { # bogus elseif
795 m2c_die "error: use 'elsif' instead of 'elseif'\n";
796 } elsif (/\@\s*els(e|if).*\@/) { # ELSE/ELSIF
Robert Storycb9314e2003-08-17 22:19:32 +0000797 if ($elseok != 1) {
798 chop $_;
Robert Story97b019a2004-02-02 19:23:20 +0000799 m2c_die "unexpected els$1\n";
Robert Storycb9314e2003-08-17 22:19:32 +0000800 }
Robert Story97b019a2004-02-02 19:23:20 +0000801 $return = skippart();
802 if ($return ne "end") {
803 m2c_die "unbalanced els$1 / rtn $rtn\n";
Robert Story69073802003-07-19 17:52:49 +0000804 }
Robert Story97b019a2004-02-02 19:23:20 +0000805 $return = "else";
806 last;
807 } elsif (/\@\s*next\s*\@/) { # NEXT
808 $return = skippart();
809 m2c_die "unbalanced code detected while exiting next/1 (returned $return)" if ($return ne "end");
810 $return = "next";
811 last;
Robert Story69073802003-07-19 17:52:49 +0000812 } elsif (/\@\s*end\@/) { # END
Robert Story97b019a2004-02-02 19:23:20 +0000813 $return = "end";
814 last;
Robert Story69073802003-07-19 17:52:49 +0000815 } elsif (/\@\s*eval\s+\$(\w+)\s*=\s*([^\@]*)/) { # EVAL
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000816 my ($v, $e) = ($1, $2);
Wes Hardakerdc594832003-05-14 22:17:03 +0000817# print STDERR "eval: $e\n";
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000818 my $e = process_vars($e);
819 $vars{$v} = eval($e);
Robert Storyc6a4c082003-09-11 22:47:50 +0000820 if (!defined($vars{$v})) {
821 warn "$@";
822 warn "$currentfile:$currentline [$_]\n";
823 }
Robert Story69073802003-07-19 17:52:49 +0000824 } elsif (/\@\s*perleval\s*(.*)\@/) { # PERLEVAL
Wes Hardaker39851572003-11-25 23:48:29 +0000825# print STDERR "perleval: $1\n";
Wes Hardakerdc594832003-05-14 22:17:03 +0000826 my $res = eval($1);
Robert Storyc6a4c082003-09-11 22:47:50 +0000827 if ($res) {
828 warn "$@";
829 warn "$currentfile:$currentline [$_]\n";
830 }
Wes Hardakera2161672004-04-28 22:47:01 +0000831 } elsif (/\@\s*startperl\s*\@/) { # STARTPERL
832 my $text;
833 while (get_next_line()) {
834 last if (/\@\s*endperl\s*\@/);
835 $text .= $_;
836 }
837 my $res = eval($text);
838 if ($res) {
839 warn "$@";
840 warn "$currentfile:$currentline [$_]\n";
841 }
842# print STDERR "perleval: $1\n";
Robert Storye796e882003-12-23 00:56:21 +0000843 } elsif (/\@\s*printf\s+(\"[^\"]+\")\s*,?(.*)\@/) { # PRINTF
Wes Hardaker39851572003-11-25 23:48:29 +0000844 my ($f, $rest) = ($1, $2);
845 $rest = process_vars($rest);
846 my @args = split(/\s*,\s*/,$rest);
847 $f = eval $f;
848# print STDERR "printf: $f, ", join(", ",@args),"\n";
849 foreach $file (values(%outputs)) {
850 printf $file (eval {$f}, @args);
851 }
Robert Story69073802003-07-19 17:52:49 +0000852 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+scalars*\s*\@/) { # SCALARS
Wes Hardakere89b8e62002-05-24 16:53:07 +0000853 my $var = $1;
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000854 my $stash = do_tell();
Wes Hardakere89b8e62002-05-24 16:53:07 +0000855 my $scalar;
Wes Hardaker71299992002-09-03 17:51:11 +0000856 my @thekeys = keys(%scalars);
857 if ($#thekeys == -1) {
Robert Story97b019a2004-02-02 19:23:20 +0000858 $return = skippart();
Wes Hardaker71299992002-09-03 17:51:11 +0000859 } else {
Wes Hardaker0878b922004-01-06 22:38:19 +0000860 if ($havenetsnmpoid) {
861 @thekeys = sort {
862 new NetSNMP::OID($a) <=>
863 new NetSNMP::OID($b) } @thekeys;
864 }
Wes Hardaker71299992002-09-03 17:51:11 +0000865 foreach $scalar (@thekeys) {
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000866 $return = do_a_loop($stash, \$vars{$var}, $scalar,
867 \$currentscalar, $scalar,
868 \$currentvar, $scalar);
Wes Hardaker71299992002-09-03 17:51:11 +0000869 }
Wes Hardakere89b8e62002-05-24 16:53:07 +0000870 }
Robert Story97b019a2004-02-02 19:23:20 +0000871 m2c_die("foreach did not end with \@end@") if($return ne "end");
Dave Shieldd10f4e82003-06-24 13:20:14 +0000872 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+notifications*\s*\@/) {
873 my $var = $1;
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000874 my $stash = do_tell();
Dave Shieldd10f4e82003-06-24 13:20:14 +0000875 my $notify;
876 my @thekeys = keys(%notifications);
877 if ($#thekeys == -1) {
Robert Story97b019a2004-02-02 19:23:20 +0000878 $return = skippart();
Dave Shieldd10f4e82003-06-24 13:20:14 +0000879 } else {
Wes Hardaker0878b922004-01-06 22:38:19 +0000880 if ($havenetsnmpoid) {
881 @thekeys = sort {
882 new NetSNMP::OID($a) <=>
883 new NetSNMP::OID($b) } @thekeys;
884 }
Dave Shieldd10f4e82003-06-24 13:20:14 +0000885 foreach $notify (@thekeys) {
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000886 $return = do_a_loop($stash, \$vars{$var}, $notify,
887 \$currentnotify, $notify);
Dave Shieldd10f4e82003-06-24 13:20:14 +0000888 }
889 }
Robert Story97b019a2004-02-02 19:23:20 +0000890 m2c_die("foreach did not end with \@end@") if($return ne "end");
Dave Shieldd10f4e82003-06-24 13:20:14 +0000891 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+varbinds\s*\@/) {
892 my $var = $1;
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000893 my $stash = do_tell();
Dave Shieldd10f4e82003-06-24 13:20:14 +0000894 my $varbind;
895 if ($#{$notifyvars{$currentnotify}} == -1) {
Robert Story97b019a2004-02-02 19:23:20 +0000896 $return = skippart();
Dave Shieldd10f4e82003-06-24 13:20:14 +0000897 } else {
898 foreach $varbind (@{$notifyvars{$currentnotify}}) {
899 # print "looping on $var for $varbind\n";
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000900 $return = do_a_loop($stash, \$vars{$var}, $varbind,
Dave Shielded75c892011-04-14 20:44:37 +0000901 \$currentvarbind, $varbind,
902 \$currentvar, $varbind);
Dave Shieldd10f4e82003-06-24 13:20:14 +0000903 }
904 }
Robert Story97b019a2004-02-02 19:23:20 +0000905 m2c_die("foreach did not end with \@end@") if($return ne "end");
Wes Hardaker71299992002-09-03 17:51:11 +0000906 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+tables*\s*\@/) {
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000907 my $var = $1;
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000908 my $stash = do_tell();
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000909 my $table;
Wes Hardaker71299992002-09-03 17:51:11 +0000910 my @thekeys = keys(%tables);
911 if ($#thekeys == -1) {
Robert Story97b019a2004-02-02 19:23:20 +0000912 $return = skippart();
Wes Hardaker71299992002-09-03 17:51:11 +0000913 } else {
Wes Hardaker0878b922004-01-06 22:38:19 +0000914 if ($havenetsnmpoid) {
915 @thekeys = sort {
916 new NetSNMP::OID($a) <=>
917 new NetSNMP::OID($b) } @thekeys;
918 }
Wes Hardaker71299992002-09-03 17:51:11 +0000919 foreach $table (@thekeys) {
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000920 $return = do_a_loop($stash, \$vars{$var}, $table,
921 \$currenttable, $table);
Wes Hardaker71299992002-09-03 17:51:11 +0000922 }
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000923 }
Robert Story97b019a2004-02-02 19:23:20 +0000924 m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end");
Wes Hardaker5dd7d132004-05-19 20:12:52 +0000925 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+stuff\s*(.*)\@/) {
926 my $var = $1;
927 my $stuff = $2;
928 my @stuff = split(/[,\s]+/, $stuff);
929 my $stash = do_tell();
930 if ($#stuff == -1) {
931 $return = skippart();
932 } else {
933 foreach $st (@stuff) {
934 $return = do_a_loop($stash, \$vars{$var}, $st,
935 \$currentstuff, $st);
936 }
937 }
938 m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end");
Robert Storyf339acf2003-10-14 18:18:33 +0000939 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+(column|index|internalindex|externalindex|nonindex)\s*\@/) {
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000940 my ($var, $type) = ($1, $2);
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000941 my $stash = do_tell();
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000942 my $column;
Wes Hardaker71299992002-09-03 17:51:11 +0000943 if ($#{$tables{$currenttable}{$type}} == -1) {
Robert Story97b019a2004-02-02 19:23:20 +0000944 $return = skippart();
Wes Hardaker71299992002-09-03 17:51:11 +0000945 } else {
946 foreach $column (@{$tables{$currenttable}{$type}}) {
947 # print "looping on $var for $type -> $column\n";
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000948 $return = do_a_loop($stash, \$vars{$var}, $column,
949 \$currentcolumn, $column,
950 \$currentvar, $column);
Wes Hardaker71299992002-09-03 17:51:11 +0000951 }
Wes Hardaker2ece3b32001-11-22 06:33:25 +0000952 }
Robert Story97b019a2004-02-02 19:23:20 +0000953 m2c_die("foreach did not end with \@end@") if($return ne "end");
Wes Hardaker90261672003-02-06 06:05:56 +0000954 } elsif (/\@\s*foreach\s+\$([^\@]+)\s+\$([^\@]+)\s+range\s+([^\@]+)\@/) {
955 my ($svar, $evar, $node) = ($1, $2, $3);
Wes Hardakerc83bbc02011-01-26 16:50:52 +0000956 $svar =~ s/,$//;
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000957 my $stash = do_tell();
Wes Hardaker90261672003-02-06 06:05:56 +0000958 my $range;
959 $node = $currentcolumn if (!$node);
960 my $mibn = $SNMP::MIB{process_vars($node)};
961 die "no such mib node: $node" if (!$mibn);
962 my @ranges = @{$mibn->{'ranges'}};
963 if ($#ranges > -1) {
964 foreach $range (@ranges) {
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000965 $return = do_a_loop($stash, \$vars{$svar}, $range->{'low'},
966 \$vars{$evar}, $range->{'high'});
Wes Hardaker90261672003-02-06 06:05:56 +0000967 }
968 } else {
Robert Story97b019a2004-02-02 19:23:20 +0000969 $return = skippart();
Wes Hardaker90261672003-02-06 06:05:56 +0000970 }
Robert Story97b019a2004-02-02 19:23:20 +0000971 m2c_die("foreach did not end with \@end@") if($return ne "end");
Wes Hardakerfbbb9342003-02-22 04:07:38 +0000972 } elsif (/\@\s*foreach\s+\$([^\@,]+)\s*,*\s+\$([^\@]+)\s+(enums*)\s*\@/) {
Wes Hardakerc2349352002-02-22 18:56:37 +0000973 my ($varvar, $varval, $type) = ($1, $2, $3);
Wes Hardaker882ab8b2004-02-27 00:21:45 +0000974 my $stash = do_tell();
Robert Storyff17cee2004-06-07 18:10:29 +0000975 my $enum, $enum2;
Robert Story744c1232003-07-16 04:50:53 +0000976
977 my @keys = sort { $SNMP::MIB{$currentvar}{'enums'}{$a} <=>
978 $SNMP::MIB{$currentvar}{'enums'}{$b} } (keys(%{$SNMP::MIB{$currentvar}{'enums'}}));
Wes Hardakerc2349352002-02-22 18:56:37 +0000979 if ($#keys > -1) {
980 foreach $enum (@keys) {
Robert Storyff17cee2004-06-07 18:10:29 +0000981 ($enum2 = $enum) =~ s/-/_/g;
982 $return = do_a_loop($stash, \$vars{$varvar}, $enum2,
Wes Hardaker7d8ab2c2004-02-27 00:44:57 +0000983 \$vars{$varval},
984 $SNMP::MIB{$currentvar}{'enums'}{$enum});
Wes Hardakerc2349352002-02-22 18:56:37 +0000985 }
986 } else {
Robert Story97b019a2004-02-02 19:23:20 +0000987 $return = skippart();
Wes Hardakerc2349352002-02-22 18:56:37 +0000988 }
Robert Story97b019a2004-02-02 19:23:20 +0000989 m2c_die("foreach did not end with \@end@") if($return ne "end");
Dave Shieldf3020102005-07-28 11:47:58 +0000990 } elsif (/\@\s*while([a-z]*)\s+([^@]+)\@/) { # WHILE
991 my ($type,$arg,$ok) = ($1,$2,0);
992 my $stash = do_tell();
993 my $loop = 1;
Dave Shield121dfa22011-04-14 21:10:56 +0000994 my $everlooped = 0;
Dave Shieldf3020102005-07-28 11:47:58 +0000995
996 while ($loop) {
997 # check condition based on type
998 if (! $type) {
999 $ok = eval(process_vars($arg));
1000 } elsif ($type eq conf) {
1001 my $file = find_conf(process_vars($arg),1); # missingok
1002 $ok = (-f $file);
1003 } elsif ($type eq dir) {
1004 $ok = (-d $arg);
1005 } else {
1006 m2c_die "unknown while modifier ($type)\n";
1007 }
1008
1009 # act on condition
1010 if ($ok) {
1011 $return = do_a_loop($stash, \$vars{$type}, $ok, \$vars{$args});
Dave Shield121dfa22011-04-14 21:10:56 +00001012 $everlooped = 1;
Dave Shieldf3020102005-07-28 11:47:58 +00001013 } else {
Dave Shield121dfa22011-04-14 21:10:56 +00001014 if (!$everlooped) {
1015 $return = skippart();
1016 }
Dave Shieldf3020102005-07-28 11:47:58 +00001017 $loop = 0;
1018 }
1019 }
Robert Story69073802003-07-19 17:52:49 +00001020 } elsif (/\@\s*prompt\s+\$(\S+)\s*(.*)\@/) { # PROMPT
Wes Hardakerf9bb4792003-06-26 06:15:18 +00001021 my ($var, $prompt) = ($1, $2);
1022 if (!$term) {
1023 my $haveit = eval { require Term::ReadLine };
1024 if ($haveit) {
1025 $term = new Term::ReadLine 'mib2c';
1026 }
1027 }
1028 if ($term) {
1029 $vars{$var} = $term->readline(process_vars($prompt));
1030 }
Robert Story69073802003-07-19 17:52:49 +00001031 } elsif (/\@\s*print\s+([^@]*)\@/) { # PRINT
Robert Story39c6baa2003-07-18 20:58:36 +00001032 my $line = process_vars($1);
1033 print "$line\n";
Wes Hardaker81fda6e1998-05-07 04:08:37 +00001034 } else {
Robert Story39c6baa2003-07-18 20:58:36 +00001035 my $line = process_vars($_);
Wes Hardakera2161672004-04-28 22:47:01 +00001036 mib2c_output($line);
Robert Story69073802003-07-19 17:52:49 +00001037 chop $_;
Robert Story87662062003-07-23 23:43:09 +00001038 warn "$currentfile:$currentline contained a line that started with a @ but did not match any mib2c configuration tokens.\n";
1039 warn "(maybe missing the trailing @?)\n";
Robert Storya3bc2022005-07-18 20:26:25 +00001040 warn "$currentfile:$currentline [$_]\n";
Robert Story97b019a2004-02-02 19:23:20 +00001041 m2c_die if ($strict_unk_token == 1);
Wes Hardaker8bc3a9d1999-07-21 23:06:52 +00001042 }
Robert Story97b019a2004-02-02 19:23:20 +00001043# $return = "eof";
Wes Hardaker81fda6e1998-05-07 04:08:37 +00001044 }
Robert Story97b019a2004-02-02 19:23:20 +00001045 print "< Balanced $balanced / level $currentlevel / rtn $return / $_\n" if($debug);
1046 if((!$_) && ($return ne "eof")) {
1047# warn "switching return of '$return' to EOF\n" if($debug);
1048 $return = "eof";
1049 }
1050 if ($balanced) {
1051 if(($balanced != $currentlevel) || ($return ne "eof")) {
1052 m2c_die "\@balanced@ specified, but processing terminated with '$return' before EOF!";
1053 }
1054 }
Robert Story69073802003-07-19 17:52:49 +00001055 $currentlevel--;
Robert Story97b019a2004-02-02 19:23:20 +00001056 return $return;
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001057}
1058
Wes Hardakera2161672004-04-28 22:47:01 +00001059sub mib2c_output {
1060 my $line = shift;
1061 foreach $file (values(%outputs)) {
1062 print $file "$line";
1063 }
1064}
1065
1066
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001067sub setup_data {
1068 my $mib = shift;
Dave Shield0f649782011-04-14 20:52:59 +00001069 if ($mib->{label} =~ /Table$/ && $#{$mib->{children}} != -1) {
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001070 my $tablename = $mib->{label};
1071 my $entry = $mib->{children};
1072 my $columns = $entry->[0]{children};
Robert Story5b3c1f42003-07-25 00:55:01 +00001073 my $augments = $entry->[0]{'augments'};
Robert Storye796e882003-12-23 00:56:21 +00001074 foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) {
1075 # store by numeric key so we can sort them later
1076 push @{$tables{$tablename}{'column'}}, $col->{'label'};
1077 }
Robert Story5b3c1f42003-07-25 00:55:01 +00001078 if ($augments) {
1079 my $mib = $SNMP::MIB{$augments} ||
1080 die "can't find info about augmented table $augments in table $tablename\n";
1081 $mib = $mib->{parent} ||
1082 die "can't find info about augmented table $augments in table $tablename\n";
1083 my $entry = $mib->{children};
1084 foreach my $index (@{$entry->[0]{'indexes'}}) {
1085 my $node = $SNMP::MIB{$index} ||
1086 die "can't find info about index $index in table $tablename\n";
1087 push @{$tables{$tablename}{'index'}}, $index;
Robert Storyf339acf2003-10-14 18:18:33 +00001088 push @{$tables{$tablename}{'externalindex'}}, $index;
Robert Story5b3c1f42003-07-25 00:55:01 +00001089 }
1090 my $columns = $entry->[0]{children};
1091 }
Robert Storye796e882003-12-23 00:56:21 +00001092 else {
1093 foreach my $index (@{$entry->[0]{'indexes'}}) {
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001094 my $node = $SNMP::MIB{$index} ||
Robert Storye796e882003-12-23 00:56:21 +00001095 die "can't find info about index $index in table $tablename\n";
1096 push @{$tables{$tablename}{'index'}}, $index;
1097 if("@{$tables{$tablename}{'column'}}" =~ /$index\b/ ) {
1098# print "idx INT $index\n";
1099 push @{$tables{$tablename}{'internalindex'}}, $index;
1100 } else {
1101# print "idx EXT $index\n";
1102 push @{$tables{$tablename}{'externalindex'}}, $index;
1103 }
1104 }
1105 }
Robert Story87662062003-07-23 23:43:09 +00001106 foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) {
Robert Storye796e882003-12-23 00:56:21 +00001107 next if ( "@{$tables{$tablename}{'index'}}" =~ /$col->{'label'}\b/ );
Robert Story87662062003-07-23 23:43:09 +00001108 push @{$tables{$tablename}{'nonindex'}}, $col->{'label'};
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001109 }
Robert Storye796e882003-12-23 00:56:21 +00001110# print "indexes: @{$tables{$tablename}{'index'}}\n";
1111# print "internal indexes: @{$tables{$tablename}{'internalindex'}}\n";
1112# print "external indexes: @{$tables{$tablename}{'externalindex'}}\n";
1113# print "non-indexes: @{$tables{$tablename}{'nonindex'}}\n";
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001114 } else {
1115 my $children = $mib->{children};
Wes Hardakere89b8e62002-05-24 16:53:07 +00001116 if ($#children == -1 && $mib->{type}) {
1117 # scalar
Dave Shieldd10f4e82003-06-24 13:20:14 +00001118 if ($mib->{type} eq "NOTIF" ||
1119 $mib->{type} eq "TRAP") {
1120 my $notifyname = $mib->{label};
1121 my @varlist = ();
1122 $notifications{$notifyname} = 1;
1123 $notifyvars{$notifyname} = $mib->{varbinds};
1124 } else {
Wes Hardakerd29287c2005-11-01 20:24:59 +00001125 $scalars{$mib->{label}} = 1 if ($mib->{'access'} ne 'Notify');
Dave Shieldd10f4e82003-06-24 13:20:14 +00001126 }
Wes Hardakere89b8e62002-05-24 16:53:07 +00001127 } else {
1128 my $i;
1129 for($i = 0; $i <= $#$children; $i++) {
1130 setup_data($children->[$i]);
1131 }
Wes Hardaker572f6211999-03-05 00:32:16 +00001132 }
Wes Hardaker81fda6e1998-05-07 04:08:37 +00001133 }
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001134}
1135
1136sub min {
1137 return $_[0] if ($_[0] < $_[1]);
1138 return $_[1];
Wes Hardaker81fda6e1998-05-07 04:08:37 +00001139}
1140
1141sub max {
Wes Hardaker2ece3b32001-11-22 06:33:25 +00001142 return $_[0] if ($_[0] > $_[1]);
1143 return $_[1];
Wes Hardaker8bc3a9d1999-07-21 23:06:52 +00001144}
Wes Hardaker1dc2dc82003-02-20 00:51:45 +00001145
Robert Story69073802003-07-19 17:52:49 +00001146sub find_conf {
1147 my ($configfile, $missingok) = (@_);
1148
Wes Hardakerea7bc482003-09-29 22:41:58 +00001149 foreach my $d (@search_dirs, @def_search_dirs) {
Robert Storyc3acd7a2005-03-18 22:46:44 +00001150# print STDERR "using $d/$configfile" if (-f "$d/$configfile");
Wes Hardakerea7bc482003-09-29 22:41:58 +00001151 return "$d/$configfile" if (-f "$d/$configfile");
1152 }
Robert Story69073802003-07-19 17:52:49 +00001153 return $configfile if (-f "$configfile");
Robert Story69073802003-07-19 17:52:49 +00001154 return if ($missingok);
1155
1156 print STDERR "Can't find a configuration file called $configfile\n";
Wes Hardakerea7bc482003-09-29 22:41:58 +00001157 print STDERR "(referenced at $currentfile:$currentline)\n" if ($currentfile);
1158 print STDERR "I looked in:\n";
1159 print " " . join("\n ", @search_dirs, @def_search_dirs), "\n";
Robert Story97b019a2004-02-02 19:23:20 +00001160 exit 1;
Robert Story69073802003-07-19 17:52:49 +00001161}
1162
Wes Hardaker1dc2dc82003-02-20 00:51:45 +00001163sub open_conf {
1164 my $configfile = shift;
1165# process .conf file
Robert Story69073802003-07-19 17:52:49 +00001166 if (! -f "$configfile") {
Wes Hardaker1dc2dc82003-02-20 00:51:45 +00001167 print STDERR "Can't find a configuration file called $configfile\n";
Robert Story97b019a2004-02-02 19:23:20 +00001168 exit 1;
Wes Hardaker1dc2dc82003-02-20 00:51:45 +00001169 }
Robert Story69073802003-07-19 17:52:49 +00001170 $currentfile = $configfile;
1171 my $fh = new IO::File;
1172 $fh->open("$configfile");
Wes Hardaker1dc2dc82003-02-20 00:51:45 +00001173 return $fh;
1174}
Wes Hardakerf9bb4792003-06-26 06:15:18 +00001175
1176sub count_scalars {
1177 my @k = keys(%scalars);
1178 return $#k + 1;
1179}
1180
1181sub count_tables {
1182 my @k = keys(%tables);
1183 return $#k + 1;
1184}
1185
Dave Shield536b7872003-06-26 09:26:16 +00001186sub count_columns {
1187 my $table = shift;
1188 return $#{$tables{$table}{'column'}} + 1;
1189}
1190
Robert Story0ddde4c2003-07-30 17:51:39 +00001191sub table_is_writable {
1192 my $table = shift;
1193 my $column;
1194 my $result = 0;
1195 foreach $column (@{$tables{$table}{'column'}}) {
1196 if($SNMP::MIB{$column}{access} =~ /(ReadWrite|Create|WriteOnly)/) {
1197 $result = 1;
1198 last;
1199 }
1200 }
1201 return $result;
1202}
1203
Robert Storyf339acf2003-10-14 18:18:33 +00001204sub table_has_create {
1205 my $table = shift;
1206 my $column;
1207 my $result = 0;
1208 foreach $column (@{$tables{$table}{'column'}}) {
1209 if($SNMP::MIB{$column}{access} =~ /(Create)/) {
1210 $result = 1;
1211 last;
1212 }
1213 }
1214 return $result;
1215}
1216
Dave Shield339f1422004-08-23 09:29:30 +00001217sub table_has_rowstatus {
1218 my $table = shift;
1219 my $column;
1220 my $result = 0;
1221 foreach $column (@{$tables{$table}{'column'}}) {
1222 if($SNMP::MIB{$column}{syntax} =~ /(RowStatus)/) {
1223 $result = 1;
1224 last;
1225 }
1226 }
1227 return $result;
1228}
1229
Robert Storyc3acd7a2005-03-18 22:46:44 +00001230sub table_has_lastchange {
1231 my $table = shift;
1232 my $column;
1233 my $result = 0;
1234 foreach $column (@{$tables{$table}{'column'}}) {
1235 if(($SNMP::MIB{$column}{syntax} =~ /(TimeStamp)/) &&
1236 ($SNMP::MIB{$column}{label} =~ /(LastChange)/)) {
1237 $result = 1;
1238 last;
1239 }
1240 }
1241 return $result;
1242}
1243
1244sub table_has_storagetype {
1245 my $table = shift;
1246 my $column;
1247 my $result = 0;
1248 foreach $column (@{$tables{$table}{'column'}}) {
1249 if($SNMP::MIB{$column}{syntax} =~ /(StorageType)/) {
1250 $result = 1;
1251 last;
1252 }
1253 }
1254 return $result;
1255}
1256
Dave Shield536b7872003-06-26 09:26:16 +00001257sub count_indexes {
1258 my $table = shift;
1259 return $#{$tables{$table}{'index'}} + 1;
1260}
1261
Robert Storyf339acf2003-10-14 18:18:33 +00001262sub count_external_indexes {
1263 my $table = shift;
1264 return $#{$tables{$table}{'externalindex'}} + 1;
1265}
1266
Wes Hardakerf9bb4792003-06-26 06:15:18 +00001267sub count_notifications {
1268 my @k = keys(%notifications);
1269 return $#k + 1;
1270}
Dave Shield536b7872003-06-26 09:26:16 +00001271
1272sub count_varbinds {
1273 my $notify = shift;
1274 return $#{$notifyvars{$notify}} + 1;
1275}