Wes Hardaker | 3804da6 | 2000-06-02 18:40:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | #!/usr/bin/perl -w |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 3 | |
| 4 | # |
Robert Story | 5b3c1f4 | 2003-07-25 00:55:01 +0000 | [diff] [blame] | 5 | # $Id$ |
| 6 | # |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 7 | # Description: |
| 8 | # |
| 9 | # This program, given an OID reference as an argument, creates some |
Wes Hardaker | 06f99a4 | 2002-03-13 04:35:07 +0000 | [diff] [blame] | 10 | # template mib module files to be used with the net-snmp agent. It is |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 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 | # |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 18 | |
Wes Hardaker | 15ddba0 | 2001-12-05 16:28:59 +0000 | [diff] [blame] | 19 | # SNMP |
| 20 | my $havesnmp = eval {require SNMP;}; |
Wes Hardaker | 0878b92 | 2004-01-06 22:38:19 +0000 | [diff] [blame] | 21 | my $havenetsnmpoid = eval {require NetSNMP::OID;}; |
Wes Hardaker | 15ddba0 | 2001-12-05 16:28:59 +0000 | [diff] [blame] | 22 | |
| 23 | if (!$havesnmp) { |
| 24 | print " |
| 25 | ERROR: You don't have the SNMP perl module installed. Please obtain |
Wes Hardaker | 06f99a4 | 2002-03-13 04:35:07 +0000 | [diff] [blame] | 26 | this by getting the latest source release of the net-snmp toolkit from |
Robert Story | cc9eb1f | 2002-07-05 18:55:20 +0000 | [diff] [blame] | 27 | http://www.net-snmp.org/download/ . Once you download the source and |
| 28 | unpack it, the perl module is contained in the perl/SNMP directory. |
Robert Story | 4bc764e | 2003-07-27 23:56:08 +0000 | [diff] [blame] | 29 | See the README file there for instructions. |
Wes Hardaker | 15ddba0 | 2001-12-05 16:28:59 +0000 | [diff] [blame] | 30 | |
| 31 | "; |
| 32 | exit; |
| 33 | } |
| 34 | |
| 35 | if ($havesnmp) { |
| 36 | eval { import SNMP; } |
| 37 | } |
Wes Hardaker | 0878b92 | 2004-01-06 22:38:19 +0000 | [diff] [blame] | 38 | if ($havenetsnmp) { |
| 39 | eval { import NetSNMP::OID; } |
| 40 | } |
Wes Hardaker | 3412484 | 1999-04-20 04:01:15 +0000 | [diff] [blame] | 41 | use FileHandle; |
| 42 | |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 43 | #use strict 'vars'; |
| 44 | $SNMP::save_descriptions=1; |
| 45 | $SNMP::use_long_names=1; |
| 46 | $SNMP::use_enums=1; |
| 47 | SNMP::initMib(); |
| 48 | |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 49 | $configfile="mib2c.conf"; |
Wes Hardaker | 572f621 | 1999-03-05 00:32:16 +0000 | [diff] [blame] | 50 | $debug=0; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 51 | $quiet=0; |
Robert Story | 02c26dc | 2003-09-30 13:50:01 +0000 | [diff] [blame] | 52 | $strict_unk_token = 0; |
Wes Hardaker | 3e26b90 | 2002-09-13 14:35:07 +0000 | [diff] [blame] | 53 | $noindent = 0; |
Robert Story | 3308c5a | 2010-04-06 01:52:45 +0000 | [diff] [blame] | 54 | $nosed = 0; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 55 | $currentline = 0; |
| 56 | $currentlevel = -1; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 57 | %assignments; |
| 58 | %outputs; |
Robert Story | 9d0c3c7 | 2003-09-30 23:46:08 +0000 | [diff] [blame] | 59 | @def_search_dirs = ("."); |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 60 | @search_dirs = (); |
Robert Story | 9d0c3c7 | 2003-09-30 23:46:08 +0000 | [diff] [blame] | 61 | if($ENV{MIB2C_DIR}) { |
Robert Story | cee0592 | 2003-12-30 23:19:02 +0000 | [diff] [blame] | 62 | push @def_search_dirs, split(/:/, $ENV{MIB2C_DIR}); |
Robert Story | 9d0c3c7 | 2003-09-30 23:46:08 +0000 | [diff] [blame] | 63 | } |
| 64 | push @def_search_dirs, "/usr/local/share/snmp/"; |
Robert Story | 4288fcf | 2004-06-18 16:41:58 +0000 | [diff] [blame] | 65 | push @def_search_dirs, "/usr/local/share/snmp/mib2c-data"; |
Dave Shield | d7b9dd6 | 2004-09-10 12:30:15 +0000 | [diff] [blame] | 66 | push @def_search_dirs, "./mib2c-conf.d"; |
Robert Story | 9d0c3c7 | 2003-09-30 23:46:08 +0000 | [diff] [blame] | 67 | |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 68 | sub usage { |
Wes Hardaker | 572f621 | 1999-03-05 00:32:16 +0000 | [diff] [blame] | 69 | print "$0 [-h] [-c configfile] [-f prefix] mibNode\n\n"; |
| 70 | print " -h\t\tThis message.\n\n"; |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 71 | print " -c configfile\tSpecifies the configuration file to use\n\t\tthat dictates what the output of mib2c will look like.\n\n"; |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 72 | print " -I PATH\tSpecifies a path to look for configuration files in\n\n"; |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 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"; |
Dave Shield | 43a5c72 | 2004-06-15 14:27:47 +0000 | [diff] [blame] | 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"; |
Robert Story | 3308c5a | 2010-04-06 01:52:45 +0000 | [diff] [blame] | 77 | print " -s\t\tDon't look for mibNode.sed and run sed on the resulting code\n\n"; |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 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"; |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 79 | 1; |
| 80 | } |
| 81 | |
Wes Hardaker | 0570d3f | 2003-11-07 18:34:03 +0000 | [diff] [blame] | 82 | my @origargs = @ARGV; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 83 | my $args_done = 0; |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 84 | while($#ARGV >= 0) { |
| 85 | $_ = shift; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 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); |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 99 | $assignments{$var} = $val; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 100 | } elsif (/^-q/) { |
| 101 | $quiet = 1; |
| 102 | } elsif (/^-i/) { |
| 103 | $noindent = 1; |
Robert Story | 3308c5a | 2010-04-06 01:52:45 +0000 | [diff] [blame] | 104 | } elsif (/^-s/) { |
| 105 | $nosed = 1; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 106 | } elsif (/^-h/) { |
| 107 | usage && exit(1); |
| 108 | } elsif (/^-f/) { |
| 109 | $outputName = shift; |
| 110 | } elsif (/^-I/) { |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 111 | my $dirs = shift; |
| 112 | push @search_dirs, split(/,/,$dirs); |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 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 = $_ ; |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 122 | } |
Wes Hardaker | 0486f40 | 1999-02-17 01:05:45 +0000 | [diff] [blame] | 123 | } |
Wes Hardaker | 572f621 | 1999-03-05 00:32:16 +0000 | [diff] [blame] | 124 | |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 125 | # |
| 126 | # internal conversion tables |
| 127 | # |
Wes Hardaker | e44e8c9 | 1998-05-11 15:09:58 +0000 | [diff] [blame] | 128 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 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 |
Wes Hardaker | efdce81 | 2001-12-11 15:34:28 +0000 | [diff] [blame] | 141 | BITS ASN_OCTET_STR |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 142 | TICKS ASN_TIMETICKS |
| 143 | GAUGE ASN_GAUGE |
| 144 | OPAQUE ASN_OPAQUE); |
Wes Hardaker | 9cd215b | 2001-12-20 00:33:40 +0000 | [diff] [blame] | 145 | %perltodecl = ("OCTETSTR", "char", |
| 146 | "INTEGER", "long", |
| 147 | "INTEGER32", "long", |
| 148 | "UNSIGNED32", "u_long", |
| 149 | "UINTEGER", "u_long", |
| 150 | "OBJECTID", "oid", |
Robert Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 151 | "COUNTER64", "U64", |
Wes Hardaker | 9cd215b | 2001-12-20 00:33:40 +0000 | [diff] [blame] | 152 | "COUNTER", "u_long", |
Jan Safranek | a925613 | 2009-07-10 07:46:43 +0000 | [diff] [blame] | 153 | "IPADDR", "in_addr_t", |
Wes Hardaker | 9cd215b | 2001-12-20 00:33:40 +0000 | [diff] [blame] | 154 | "BITS", "char", |
| 155 | "TICKS", "u_long", |
| 156 | "GAUGE", "u_long", |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 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"); |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 171 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 172 | my $mibnode = $SNMP::MIB{$oid}; |
Wes Hardaker | 0570d3f | 2003-11-07 18:34:03 +0000 | [diff] [blame] | 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 | "; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 194 | exit 1; |
Wes Hardaker | 0570d3f | 2003-11-07 18:34:03 +0000 | [diff] [blame] | 195 | } |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 196 | |
| 197 | # setup |
| 198 | $outputName = $mibnode->{'label'} if (!defined($outputName)); |
Dave Shield | 1cb4732 | 2008-03-24 17:54:20 +0000 | [diff] [blame] | 199 | $outputName =~ s/-/_/g; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 200 | $vars{'name'} = $outputName; |
| 201 | $vars{'oid'} = $oid; |
Robert Story | ff17cee | 2004-06-07 18:10:29 +0000 | [diff] [blame] | 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 | " ***************************************************/"; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 210 | |
| 211 | # loop through mib nodes, remembering stuff. |
| 212 | setup_data($mibnode); |
| 213 | |
Robert Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 214 | if(($ENV{HOME}) && (-f "$ENV{HOME}/.snmp/mib2c.conf")) { |
| 215 | $fh = open_conf("$ENV{HOME}/.snmp/mib2c.conf"); |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 216 | process("-balanced"); |
Robert Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 217 | $fh->close; |
| 218 | } |
| 219 | |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 220 | my $defaults = find_conf("default-$configfile",1); |
| 221 | if (-f "$defaults" ) { |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 222 | $fh = open_conf($defaults); |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 223 | process("-balanced"); |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 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 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 233 | $configfile = find_conf($configfile,0); |
Wes Hardaker | 1dc2dc8 | 2003-02-20 00:51:45 +0000 | [diff] [blame] | 234 | $fh = open_conf($configfile); |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 235 | process("-balanced"); |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 236 | $fh->close; |
| 237 | |
Robert Story | 3308c5a | 2010-04-06 01:52:45 +0000 | [diff] [blame] | 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 | |
Wes Hardaker | 3e26b90 | 2002-09-13 14:35:07 +0000 | [diff] [blame] | 247 | if (!$noindent) { |
| 248 | foreach $i (keys(%written)) { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 249 | next if ($i eq "-"); |
Robert Story | cb9314e | 2003-08-17 22:19:32 +0000 | [diff] [blame] | 250 | next if (!($i =~ /\.[ch]$/)); |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 251 | print STDERR "running indent on $i\n" if (!$quiet); |
Dave Shield | 740721d | 2004-05-04 08:13:42 +0000 | [diff] [blame] | 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"); |
Wes Hardaker | 3e26b90 | 2002-09-13 14:35:07 +0000 | [diff] [blame] | 253 | } |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 256 | sub m2c_die { |
| 257 | warn "ERROR: ". $_[0] . "\n"; |
| 258 | die " at $currentfile:$currentline\n"; |
| 259 | } |
| 260 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 261 | sub tocommas { |
| 262 | my $oid = $_[0]; |
| 263 | $oid =~ s/\./,/g; |
| 264 | $oid =~ s/^\s*,//; |
| 265 | return $oid; |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 266 | } |
Wes Hardaker | 18febdb | 1999-02-17 23:16:17 +0000 | [diff] [blame] | 267 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 268 | sub oidlength { |
Robert Story | f2e2a87 | 2003-07-25 02:29:15 +0000 | [diff] [blame] | 269 | return (scalar split(/\./, $_[0])) - 1; |
Wes Hardaker | 18febdb | 1999-02-17 23:16:17 +0000 | [diff] [blame] | 270 | } |
Wes Hardaker | 18febdb | 1999-02-17 23:16:17 +0000 | [diff] [blame] | 271 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 272 | # replaces $VAR type expressions and $VAR.subcomponent expressions |
| 273 | # with data from the mib tree and loop variables. |
| 274 | # possible uses: |
Wes Hardaker | 8e9f126 | 1999-04-20 22:31:35 +0000 | [diff] [blame] | 275 | # |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 276 | # $var -- as defined by loops, etc. |
| 277 | # ${var}otherstuff -- appending text to variable contents |
| 278 | # $var.uc -- all upper case version of $var |
Wes Hardaker | 8e9f126 | 1999-04-20 22:31:35 +0000 | [diff] [blame] | 279 | # |
Wes Hardaker | 68db640 | 2004-04-29 00:23:35 +0000 | [diff] [blame] | 280 | # NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages |
| 281 | # |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 282 | # Mib components, $var must first expand to a mib node name: |
| 283 | # |
Wes Hardaker | 008e676 | 2004-04-28 23:44:43 +0000 | [diff] [blame] | 284 | # $var.uc -- all upper case version of $var |
Wes Hardaker | 66762ea | 2004-04-28 23:58:36 +0000 | [diff] [blame] | 285 | # |
| 286 | # $var.objectID -- dotted, fully-qualified, and numeric OID |
| 287 | # $var.commaoid -- comma separated numeric OID for array initialization |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 288 | # $var.oidlength -- length of the oid |
Wes Hardaker | 66762ea | 2004-04-28 23:58:36 +0000 | [diff] [blame] | 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 | # |
Dave Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 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 |
Robert Story | 16d9e33 | 2001-12-11 04:43:07 +0000 | [diff] [blame] | 304 | # $var.noaccess -- 1 if not-accessible, 0 if not |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 305 | # $var.accessible -- 1 if accessible, 0 if not |
Robert Story | da33a02 | 2010-02-12 02:57:46 +0000 | [diff] [blame] | 306 | # $var.storagetype -- 1 if an object is a StorageType object, 0 if not |
Dave Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 307 | # $var.rowstatus -- 1 if an object is a RowStatus object, 0 if not |
Robert Story | da33a02 | 2010-02-12 02:57:46 +0000 | [diff] [blame] | 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 |
Wes Hardaker | 66762ea | 2004-04-28 23:58:36 +0000 | [diff] [blame] | 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. |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 318 | # $var.access -- node's access type |
| 319 | # $var.status -- node's status |
| 320 | # $var.syntax -- node's syntax |
Wes Hardaker | 554ead7 | 2004-06-04 20:37:42 +0000 | [diff] [blame] | 321 | # $var.reference -- node's reference |
Robert Story | 4cd414d | 2004-06-18 02:09:47 +0000 | [diff] [blame] | 322 | # $var.description -- node's description |
Wes Hardaker | f9bb479 | 2003-06-26 06:15:18 +0000 | [diff] [blame] | 323 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 324 | sub process_vars { |
| 325 | my $it = shift; |
| 326 | |
| 327 | # mib substitutions ($var.type -> $mibnode->{'type'}) |
Robert Story | ff17cee | 2004-06-07 18:10:29 +0000 | [diff] [blame] | 328 | if ( $it =~ /\$(\w+)\.(\w+)/ ) { |
Dave Shield | 0f64978 | 2011-04-14 20:52:59 +0000 | [diff] [blame] | 329 | if ($SNMP::MIB{$vars{$1}} && defined($tables{$SNMP::MIB{$vars{$1}}{'label'}})) { |
Dave Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 330 | $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 Story | c3acd7a | 2005-03-18 22:46:44 +0000 | [diff] [blame] | 333 | $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 Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 335 | } |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 336 | $it =~ s/\$(\w+)\.(uc)/uc($vars{$1})/eg; # make something uppercase |
Wes Hardaker | 9cd215b | 2001-12-20 00:33:40 +0000 | [diff] [blame] | 337 | $it =~ s/\$(\w+)\.(commaoid)/tocommas($SNMP::MIB{$vars{$1}}{objectID})/eg; |
| 338 | $it =~ s/\$(\w+)\.(oidlength)/oidlength($SNMP::MIB{$vars{$1}}{objectID})/eg; |
Wes Hardaker | 3985157 | 2003-11-25 23:48:29 +0000 | [diff] [blame] | 339 | $it =~ s/\$(\w+)\.(description)/$SNMP::MIB{$vars{$1}}{description}/g; |
Wes Hardaker | 9cd215b | 2001-12-20 00:33:40 +0000 | [diff] [blame] | 340 | $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 Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 343 | $it =~ s/\$(\w+)\.(module)/$SNMP::MIB{$vars{$1}}{moduleID}/g; |
Dave Shield | f21cd5f | 2003-04-25 15:01:54 +0000 | [diff] [blame] | 344 | $it =~ s/\$(\w+)\.(settable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(ReadWrite|Create|WriteOnly)\/)?1:0)/eg; |
Dave Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 345 | $it =~ s/\$(\w+)\.(creatable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Create)\/)?1:0)/eg; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 346 | $it =~ s/\$(\w+)\.(readable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Read|Create)\/)?1:0)/eg; |
Wes Hardaker | 9cd215b | 2001-12-20 00:33:40 +0000 | [diff] [blame] | 347 | $it =~ s/\$(\w+)\.(noaccess)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(NoAccess)\/)?1:0)/eg; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 348 | $it =~ s/\$(\w+)\.(accessible)/(($SNMP::MIB{$vars{$1}}{access} !~ \/(NoAccess)\/)?1:0)/eg; |
Wes Hardaker | 554ead7 | 2004-06-04 20:37:42 +0000 | [diff] [blame] | 349 | $it =~ s/\$(\w+)\.(objectID|label|subID|access|status|syntax|reference)/$SNMP::MIB{$vars{$1}}{$2}/g; |
Dave Shield | 991b95b | 2002-07-18 15:22:24 +0000 | [diff] [blame] | 350 | $it =~ s/\$(\w+)\.(decl)/$perltodecl{$SNMP::MIB{$vars{$1}}{type}}/g; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 351 | $it =~ s/\$(\w+)\.(needlength)/$perltolen{$SNMP::MIB{$vars{$1}}{type}}/g; |
Wes Hardaker | 6273b8a | 2003-06-25 21:41:40 +0000 | [diff] [blame] | 352 | $it =~ s/\$(\w+)\.(iscolumn)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} =~ \/Entry$\/) ? 1 : 0/eg; |
Wes Hardaker | f9bb479 | 2003-06-26 06:15:18 +0000 | [diff] [blame] | 353 | $it =~ s/\$(\w+)\.(isscalar)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} !~ \/Entry$\/ && $SNMP::MIB{$vars{$1}}{access}) ? 1 : 0/eg; |
Wes Hardaker | 6273b8a | 2003-06-25 21:41:40 +0000 | [diff] [blame] | 354 | $it =~ s/\$(\w+)\.(parent)/$SNMP::MIB{$vars{$1}}{'parent'}{'label'}/g; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 355 | $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 Story | 0ddde4c | 2003-07-30 17:51:39 +0000 | [diff] [blame] | 360 | $it =~ s/\$(\w+)\.(ranges)/($#{$SNMP::MIB{$vars{$1}}{'ranges'}} == -1) ? 0 : 1/eg; |
Robert Story | 744c123 | 2003-07-16 04:50:53 +0000 | [diff] [blame] | 361 | # check for enums |
| 362 | $it =~ s/\$(\w+)\.(enums)/(%{$SNMP::MIB{$vars{$1}}{'enums'}} == 0) ? 0 : 1/eg; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 363 | $it =~ s/\$(\w+)\.(enumrange)/%{$SNMP::MIB{$vars{$1}}{'enums'}}/eg; |
Dave Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 364 | $it =~ s/\$(\w+)\.(rowstatus)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(RowStatus)\/)?1:0)/eg; |
Robert Story | da33a02 | 2010-02-12 02:57:46 +0000 | [diff] [blame] | 365 | $it =~ s/\$(\w+)\.(storagetype)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(StorageType)\/)?1:0)/eg; |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 366 | if ( $it =~ /\$(\w+)\.(\w+)/ ) { |
| 367 | warn "Possible unknown variable attribute \$$1.$2 at $currentfile:$currentline\n"; |
| 368 | } |
Robert Story | ff17cee | 2004-06-07 18:10:29 +0000 | [diff] [blame] | 369 | } |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 370 | # normal variable substitions |
| 371 | $it =~ s/\$\{(\w+)\}/$vars{$1}/g; |
| 372 | $it =~ s/\$(\w+)/$vars{$1}/g; |
Robert Story | 0ddde4c | 2003-07-30 17:51:39 +0000 | [diff] [blame] | 373 | # use $@var to put literal '$var' |
| 374 | $it =~ s/\$\@(\w+)/\$$1/g; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 375 | return $it; |
| 376 | } |
| 377 | |
| 378 | # process various types of statements |
| 379 | # |
Wes Hardaker | 68db640 | 2004-04-29 00:23:35 +0000 | [diff] [blame] | 380 | # NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 381 | # which include: |
| 382 | # @open FILE@ |
| 383 | # writes generated output to FILE |
Wes Hardaker | 68db640 | 2004-04-29 00:23:35 +0000 | [diff] [blame] | 384 | # 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 Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 389 | # @push@ |
Wes Hardaker | 008e676 | 2004-04-28 23:44:43 +0000 | [diff] [blame] | 390 | # 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 Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 393 | # @pop@ |
| 394 | # pop up the process() stack one level. Use after a @push@ to return to |
| 395 | # the previous set of open files. |
Wes Hardaker | e89b8e6 | 2002-05-24 16:53:07 +0000 | [diff] [blame] | 396 | # @foreach $VAR scalar@ |
| 397 | # repeat iterate over code until @end@ setting $VAR to all known scalars |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 398 | # @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 Story | 5b3c1f4 | 2003-07-25 00:55:01 +0000 | [diff] [blame] | 404 | # @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 Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 408 | # @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 Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 416 | # @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 Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 420 | # @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 Hardaker | 9026167 | 2003-02-06 06:05:56 +0000 | [diff] [blame] | 425 | # @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 Hardaker | 5dd7d13 | 2004-05-19 20:12:52 +0000 | [diff] [blame] | 431 | # @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 Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 435 | # @while expression@ |
| 436 | # repeat iterate over code until the expression is false |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 437 | # @eval $VAR = expression@ |
Wes Hardaker | 68db640 | 2004-04-29 00:23:35 +0000 | [diff] [blame] | 438 | # 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 Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 442 | # @perleval STUFF@ |
| 443 | # evaluates STUFF directly in perl. Note that all mib2c variables |
Robert Story | c3acd7a | 2005-03-18 22:46:44 +0000 | [diff] [blame] | 444 | # 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 Hardaker | 008e676 | 2004-04-28 23:44:43 +0000 | [diff] [blame] | 447 | # @startperl@ |
| 448 | # @endperl@ |
| 449 | # treats everything between these tags as perl code, and evaluates it. |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 450 | # @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 Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 454 | # @if expression@ |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 455 | # evaluates expression, and if expression is true processes |
Robert Story | c3acd7a | 2005-03-18 22:46:44 +0000 | [diff] [blame] | 456 | # 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 Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 470 | # @define NAME@ |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 471 | # @enddefine@ |
Wes Hardaker | 008e676 | 2004-04-28 23:44:43 +0000 | [diff] [blame] | 472 | # Memorizes "stuff" between the define and enddefine tags for |
| 473 | # later calling as NAME by @calldefine NAME@. |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 474 | # @calldefine NAME@ |
| 475 | # Executes stuff previously memorized as NAME. |
Wes Hardaker | 68db640 | 2004-04-29 00:23:35 +0000 | [diff] [blame] | 476 | # @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 Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 489 | # @quit@ |
| 490 | # Bail out (silently) |
Wes Hardaker | 68db640 | 2004-04-29 00:23:35 +0000 | [diff] [blame] | 491 | # @exit@ |
| 492 | # Bail out! |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 493 | # |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 494 | sub skippart { |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 495 | 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 Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 501 | while(get_next_line()) { |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 502 | $currentline++; |
| 503 | $_ = process_vars($_) if ($debug); |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 504 | print "$currentfile.$currentline:P$currentlevel:S$endcount.$rtnelse:$_" if ($debug); |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 505 | 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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 513 | return "end" if ($endcount == 1); |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 514 | $endcount--; |
| 515 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 516 | elsif (/\@\s*elseif.*\@/) { |
| 517 | m2c_die "use 'elsif' instead of 'elseif'\n"; |
| 518 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 519 | elsif (/\@\s*else\@/) { |
| 520 | return "else" if (($endcount == 1) && ($rtnelse == 1)); |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 521 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 522 | elsif (/\@\s*elsif\s+([^\@]+)\@/) { |
| 523 | return "else" if (($endcount == 1) && ($rtnelse == 1) && (eval(process_vars($1)))); |
| 524 | } |
Dave Shield | a6e7335 | 2007-01-02 16:49:07 +0000 | [diff] [blame] | 525 | elsif (/\@\s*(foreach|if|while)/) { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 526 | $endcount++; |
| 527 | } |
Wes Hardaker | 8e9f126 | 1999-04-20 22:31:35 +0000 | [diff] [blame] | 528 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 529 | print "skippart EOF\n"; |
| 530 | m2c_die "unbalanced code detected in skippart: EOF when $endcount levels deep" if($endcount != 1); |
| 531 | return "eof"; |
Wes Hardaker | 8e9f126 | 1999-04-20 22:31:35 +0000 | [diff] [blame] | 532 | } |
Wes Hardaker | 572f621 | 1999-03-05 00:32:16 +0000 | [diff] [blame] | 533 | |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 534 | sub 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 | |
| 549 | sub close_files { |
| 550 | foreach $name (keys(%outputs)) { |
| 551 | close_file($name); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | sub 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 Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 563 | return if ($outputs{$name}); |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 564 | $outputs{$name} = new IO::File; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 565 | $outputs{$name}->open(">$spec") || m2c_die "failed to open $name"; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 566 | print STDERR "writing to $name\n" if (!$quiet && !$written{$name}); |
| 567 | $written{$name} = '1'; |
| 568 | } |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 569 | |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 570 | sub process_file { |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 571 | my ($file, $missingok, $keepvars) = (@_); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 572 | my $oldfh = $fh; |
| 573 | my $oldfile = $currentfile; |
| 574 | my $oldline = $currentline; |
| 575 | # keep old copy of @vars and just build on it. |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 576 | my %oldvars; |
| 577 | |
| 578 | %oldvars = %vars if ($keepvars != 1); |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 579 | |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 580 | $file = find_conf($file,$missingok); |
| 581 | return if (! $file); |
| 582 | |
| 583 | $fh = open_conf($file); |
| 584 | $currentline = 0; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 585 | process("-balanced"); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 586 | $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 Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 593 | %vars = %oldvars if ($keepvars != 1); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 596 | sub get_next_line { |
| 597 | if ($#process_lines > -1) { |
| 598 | return $_ = shift @process_lines; |
| 599 | } |
| 600 | return $_ = <$fh>; |
| 601 | } |
| 602 | |
| 603 | sub do_tell { |
| 604 | my $stash; |
| 605 | $stash->{'startpos'} = $fh->tell(); |
| 606 | $stash->{'startline'} = $currentline; |
| 607 | @{$stash->{'lines'}} = @process_lines; |
| 608 | return $stash; |
| 609 | } |
| 610 | |
| 611 | sub do_seek { |
| 612 | my $stash = shift; |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 613 | |
| 614 | # save current line number |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 615 | $currentline = $stash->{'startline'}; |
| 616 | $fh->seek($stash->{'startpos'}, 0); # go to top of section. |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 617 | |
| 618 | # save current process_lines state. |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 619 | @process_lines = @{$stash->{'lines'}}; |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 620 | |
| 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 | |
| 628 | sub 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 | |
| 635 | sub do_a_loop { |
| 636 | my $stash = shift; |
| 637 | do_seek($stash, @_); |
| 638 | my $return = process(); |
| 639 | do_unseek($stash); |
| 640 | return $return; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 643 | sub process { |
| 644 | my $arg = shift; |
| 645 | my $elseok = 0; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 646 | my $balanced = 0; |
| 647 | my $startlevel; |
| 648 | my $return = "eof"; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 649 | while ($arg =~ s/-(\w+)\s*//) { |
| 650 | $elseok = 1 if ($1 eq "elseok"); |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 651 | $balanced = 1 if ($1 eq "balanced"); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | $currentlevel++; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 655 | $startlevel = $currentlevel; |
| 656 | if($balanced) { |
| 657 | $balanced = $currentlevel; |
| 658 | } |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 659 | while(get_next_line()) { |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 660 | $currentline++; |
| 661 | if ($debug) { |
| 662 | # my $line = process_vars($_); |
| 663 | # chop $line; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 664 | print "$currentfile.$currentline:P$currentlevel.$elseok:$return:$_"; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | next if (/^\s*\#\#/); # noop, it's a comment |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 668 | if (! /^\s*\@/ ) { # output |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 669 | my $line = process_vars($_); |
| 670 | foreach $file (values(%outputs)) { |
| 671 | print $file "$line"; |
| 672 | } |
| 673 | } #################################################################### |
| 674 | elsif (/\@\s*exit\@/) { # EXIT |
Robert Story | c26d644 | 2004-07-08 22:38:14 +0000 | [diff] [blame] | 675 | close_files; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 676 | die "exiting at conf file ($currentfile:$currentline) request\n"; |
Dave Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 677 | } elsif (/\@\s*quit\@/) { # QUIT |
| 678 | close_files; |
| 679 | exit; |
Robert Story | c6a4c08 | 2003-09-11 22:47:50 +0000 | [diff] [blame] | 680 | } elsif (/\@\s*debug\s+([^\@]+)\@/) { # DEBUG |
| 681 | if ($1 eq "on") { |
| 682 | $debug = 1; |
| 683 | } |
| 684 | else { |
| 685 | $debug = 0; |
| 686 | } |
Robert Story | 02c26dc | 2003-09-30 13:50:01 +0000 | [diff] [blame] | 687 | } 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 Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 694 | } elsif (/\@\s*balanced\@/) { # BALANCED |
| 695 | $balanced = $currentlevel; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 696 | } elsif (/\@\s*open\s+([^\@]+)\@/) { # OPEN |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 697 | 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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 704 | } elsif (/\@\s*close\s+([^\@]+)\@/) { # CLOSE |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 705 | my $spec = process_vars($1); |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 706 | close_file($spec); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 707 | } elsif (/\@\s*append\s+([^\@]+)\@/) { # APPEND |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 708 | 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 Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 716 | } elsif (/\@\s*define\s*(.*)\@/) { # DEFINE |
| 717 | my $it = $1; |
| 718 | while (<$fh>) { |
| 719 | last if (/\@\s*enddefine\s*@/); |
| 720 | push @{$defines{$it}}, $_; |
| 721 | } |
Wes Hardaker | 5dd7d13 | 2004-05-19 20:12:52 +0000 | [diff] [blame] | 722 | } elsif (/\@\s*calldefine\s+(\w+)@/) { |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 723 | if ($#{$defines{$1}} == -1) { |
| 724 | warn "called a define of $1 which didn't exist\n"; |
| 725 | warn "$currentfile:$currentline [$_]\n"; |
| 726 | } else { |
Wes Hardaker | 5dd7d13 | 2004-05-19 20:12:52 +0000 | [diff] [blame] | 727 | unshift @process_lines, @{$defines{$1}}; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 728 | } |
| 729 | } elsif (/\@\s*run (.*)\@/) { # RUN |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 730 | 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 Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 738 | my $spec = process_vars($arg); |
| 739 | next if (!$again && $ranalready{$spec}); |
| 740 | $ranalready{$spec} = 1; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 741 | my %oldout = %outputs; |
| 742 | my %emptyarray; |
| 743 | %outputs = %emptyoutputs; |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 744 | process_file($spec,0,0); |
| 745 | close_files; |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 746 | %outputs = %oldout; |
Robert Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 747 | } 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 Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 755 | $return = "pop"; |
| 756 | last; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 757 | } 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 Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 762 | } |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 763 | my $spec = process_vars($arg); |
| 764 | process_file($spec,$missingok,1); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 765 | } 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 Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 771 | my $file = find_conf(process_vars($arg),1); # missingok |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 772 | $ok = (-f $file); |
Robert Story | c3acd7a | 2005-03-18 22:46:44 +0000 | [diff] [blame] | 773 | } elsif ($type eq dir) { |
| 774 | $ok = (-d $arg); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 775 | } else { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 776 | m2c_die "unknown if modifier ($type)\n"; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 777 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 778 | # act on condition |
| 779 | if ($ok) { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 780 | $return = process("-elseok"); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 781 | } else { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 782 | $return = skippart("-else"); |
| 783 | $return = process("-elseok") if ($return eq "else"); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 784 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 785 | 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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 790 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 791 | 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 Story | cb9314e | 2003-08-17 22:19:32 +0000 | [diff] [blame] | 797 | if ($elseok != 1) { |
| 798 | chop $_; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 799 | m2c_die "unexpected els$1\n"; |
Robert Story | cb9314e | 2003-08-17 22:19:32 +0000 | [diff] [blame] | 800 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 801 | $return = skippart(); |
| 802 | if ($return ne "end") { |
| 803 | m2c_die "unbalanced els$1 / rtn $rtn\n"; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 804 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 805 | $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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 812 | } elsif (/\@\s*end\@/) { # END |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 813 | $return = "end"; |
| 814 | last; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 815 | } elsif (/\@\s*eval\s+\$(\w+)\s*=\s*([^\@]*)/) { # EVAL |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 816 | my ($v, $e) = ($1, $2); |
Wes Hardaker | dc59483 | 2003-05-14 22:17:03 +0000 | [diff] [blame] | 817 | # print STDERR "eval: $e\n"; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 818 | my $e = process_vars($e); |
| 819 | $vars{$v} = eval($e); |
Robert Story | c6a4c08 | 2003-09-11 22:47:50 +0000 | [diff] [blame] | 820 | if (!defined($vars{$v})) { |
| 821 | warn "$@"; |
| 822 | warn "$currentfile:$currentline [$_]\n"; |
| 823 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 824 | } elsif (/\@\s*perleval\s*(.*)\@/) { # PERLEVAL |
Wes Hardaker | 3985157 | 2003-11-25 23:48:29 +0000 | [diff] [blame] | 825 | # print STDERR "perleval: $1\n"; |
Wes Hardaker | dc59483 | 2003-05-14 22:17:03 +0000 | [diff] [blame] | 826 | my $res = eval($1); |
Robert Story | c6a4c08 | 2003-09-11 22:47:50 +0000 | [diff] [blame] | 827 | if ($res) { |
| 828 | warn "$@"; |
| 829 | warn "$currentfile:$currentline [$_]\n"; |
| 830 | } |
Wes Hardaker | a216167 | 2004-04-28 22:47:01 +0000 | [diff] [blame] | 831 | } 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 Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 843 | } elsif (/\@\s*printf\s+(\"[^\"]+\")\s*,?(.*)\@/) { # PRINTF |
Wes Hardaker | 3985157 | 2003-11-25 23:48:29 +0000 | [diff] [blame] | 844 | 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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 852 | } elsif (/\@\s*foreach\s+\$([^\@]+)\s+scalars*\s*\@/) { # SCALARS |
Wes Hardaker | e89b8e6 | 2002-05-24 16:53:07 +0000 | [diff] [blame] | 853 | my $var = $1; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 854 | my $stash = do_tell(); |
Wes Hardaker | e89b8e6 | 2002-05-24 16:53:07 +0000 | [diff] [blame] | 855 | my $scalar; |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 856 | my @thekeys = keys(%scalars); |
| 857 | if ($#thekeys == -1) { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 858 | $return = skippart(); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 859 | } else { |
Wes Hardaker | 0878b92 | 2004-01-06 22:38:19 +0000 | [diff] [blame] | 860 | if ($havenetsnmpoid) { |
| 861 | @thekeys = sort { |
| 862 | new NetSNMP::OID($a) <=> |
| 863 | new NetSNMP::OID($b) } @thekeys; |
| 864 | } |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 865 | foreach $scalar (@thekeys) { |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 866 | $return = do_a_loop($stash, \$vars{$var}, $scalar, |
| 867 | \$currentscalar, $scalar, |
| 868 | \$currentvar, $scalar); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 869 | } |
Wes Hardaker | e89b8e6 | 2002-05-24 16:53:07 +0000 | [diff] [blame] | 870 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 871 | m2c_die("foreach did not end with \@end@") if($return ne "end"); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 872 | } elsif (/\@\s*foreach\s+\$([^\@]+)\s+notifications*\s*\@/) { |
| 873 | my $var = $1; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 874 | my $stash = do_tell(); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 875 | my $notify; |
| 876 | my @thekeys = keys(%notifications); |
| 877 | if ($#thekeys == -1) { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 878 | $return = skippart(); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 879 | } else { |
Wes Hardaker | 0878b92 | 2004-01-06 22:38:19 +0000 | [diff] [blame] | 880 | if ($havenetsnmpoid) { |
| 881 | @thekeys = sort { |
| 882 | new NetSNMP::OID($a) <=> |
| 883 | new NetSNMP::OID($b) } @thekeys; |
| 884 | } |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 885 | foreach $notify (@thekeys) { |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 886 | $return = do_a_loop($stash, \$vars{$var}, $notify, |
| 887 | \$currentnotify, $notify); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 890 | m2c_die("foreach did not end with \@end@") if($return ne "end"); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 891 | } elsif (/\@\s*foreach\s+\$([^\@]+)\s+varbinds\s*\@/) { |
| 892 | my $var = $1; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 893 | my $stash = do_tell(); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 894 | my $varbind; |
| 895 | if ($#{$notifyvars{$currentnotify}} == -1) { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 896 | $return = skippart(); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 897 | } else { |
| 898 | foreach $varbind (@{$notifyvars{$currentnotify}}) { |
| 899 | # print "looping on $var for $varbind\n"; |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 900 | $return = do_a_loop($stash, \$vars{$var}, $varbind, |
Dave Shield | ed75c89 | 2011-04-14 20:44:37 +0000 | [diff] [blame] | 901 | \$currentvarbind, $varbind, |
| 902 | \$currentvar, $varbind); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 905 | m2c_die("foreach did not end with \@end@") if($return ne "end"); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 906 | } elsif (/\@\s*foreach\s+\$([^\@]+)\s+tables*\s*\@/) { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 907 | my $var = $1; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 908 | my $stash = do_tell(); |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 909 | my $table; |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 910 | my @thekeys = keys(%tables); |
| 911 | if ($#thekeys == -1) { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 912 | $return = skippart(); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 913 | } else { |
Wes Hardaker | 0878b92 | 2004-01-06 22:38:19 +0000 | [diff] [blame] | 914 | if ($havenetsnmpoid) { |
| 915 | @thekeys = sort { |
| 916 | new NetSNMP::OID($a) <=> |
| 917 | new NetSNMP::OID($b) } @thekeys; |
| 918 | } |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 919 | foreach $table (@thekeys) { |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 920 | $return = do_a_loop($stash, \$vars{$var}, $table, |
| 921 | \$currenttable, $table); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 922 | } |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 923 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 924 | m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end"); |
Wes Hardaker | 5dd7d13 | 2004-05-19 20:12:52 +0000 | [diff] [blame] | 925 | } 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 Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 939 | } elsif (/\@\s*foreach\s+\$([^\@]+)\s+(column|index|internalindex|externalindex|nonindex)\s*\@/) { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 940 | my ($var, $type) = ($1, $2); |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 941 | my $stash = do_tell(); |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 942 | my $column; |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 943 | if ($#{$tables{$currenttable}{$type}} == -1) { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 944 | $return = skippart(); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 945 | } else { |
| 946 | foreach $column (@{$tables{$currenttable}{$type}}) { |
| 947 | # print "looping on $var for $type -> $column\n"; |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 948 | $return = do_a_loop($stash, \$vars{$var}, $column, |
| 949 | \$currentcolumn, $column, |
| 950 | \$currentvar, $column); |
Wes Hardaker | 7129999 | 2002-09-03 17:51:11 +0000 | [diff] [blame] | 951 | } |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 952 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 953 | m2c_die("foreach did not end with \@end@") if($return ne "end"); |
Wes Hardaker | 9026167 | 2003-02-06 06:05:56 +0000 | [diff] [blame] | 954 | } elsif (/\@\s*foreach\s+\$([^\@]+)\s+\$([^\@]+)\s+range\s+([^\@]+)\@/) { |
| 955 | my ($svar, $evar, $node) = ($1, $2, $3); |
Wes Hardaker | c83bbc0 | 2011-01-26 16:50:52 +0000 | [diff] [blame] | 956 | $svar =~ s/,$//; |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 957 | my $stash = do_tell(); |
Wes Hardaker | 9026167 | 2003-02-06 06:05:56 +0000 | [diff] [blame] | 958 | 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 Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 965 | $return = do_a_loop($stash, \$vars{$svar}, $range->{'low'}, |
| 966 | \$vars{$evar}, $range->{'high'}); |
Wes Hardaker | 9026167 | 2003-02-06 06:05:56 +0000 | [diff] [blame] | 967 | } |
| 968 | } else { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 969 | $return = skippart(); |
Wes Hardaker | 9026167 | 2003-02-06 06:05:56 +0000 | [diff] [blame] | 970 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 971 | m2c_die("foreach did not end with \@end@") if($return ne "end"); |
Wes Hardaker | fbbb934 | 2003-02-22 04:07:38 +0000 | [diff] [blame] | 972 | } elsif (/\@\s*foreach\s+\$([^\@,]+)\s*,*\s+\$([^\@]+)\s+(enums*)\s*\@/) { |
Wes Hardaker | c234935 | 2002-02-22 18:56:37 +0000 | [diff] [blame] | 973 | my ($varvar, $varval, $type) = ($1, $2, $3); |
Wes Hardaker | 882ab8b | 2004-02-27 00:21:45 +0000 | [diff] [blame] | 974 | my $stash = do_tell(); |
Robert Story | ff17cee | 2004-06-07 18:10:29 +0000 | [diff] [blame] | 975 | my $enum, $enum2; |
Robert Story | 744c123 | 2003-07-16 04:50:53 +0000 | [diff] [blame] | 976 | |
| 977 | my @keys = sort { $SNMP::MIB{$currentvar}{'enums'}{$a} <=> |
| 978 | $SNMP::MIB{$currentvar}{'enums'}{$b} } (keys(%{$SNMP::MIB{$currentvar}{'enums'}})); |
Wes Hardaker | c234935 | 2002-02-22 18:56:37 +0000 | [diff] [blame] | 979 | if ($#keys > -1) { |
| 980 | foreach $enum (@keys) { |
Robert Story | ff17cee | 2004-06-07 18:10:29 +0000 | [diff] [blame] | 981 | ($enum2 = $enum) =~ s/-/_/g; |
| 982 | $return = do_a_loop($stash, \$vars{$varvar}, $enum2, |
Wes Hardaker | 7d8ab2c | 2004-02-27 00:44:57 +0000 | [diff] [blame] | 983 | \$vars{$varval}, |
| 984 | $SNMP::MIB{$currentvar}{'enums'}{$enum}); |
Wes Hardaker | c234935 | 2002-02-22 18:56:37 +0000 | [diff] [blame] | 985 | } |
| 986 | } else { |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 987 | $return = skippart(); |
Wes Hardaker | c234935 | 2002-02-22 18:56:37 +0000 | [diff] [blame] | 988 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 989 | m2c_die("foreach did not end with \@end@") if($return ne "end"); |
Dave Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 990 | } 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 Shield | 121dfa2 | 2011-04-14 21:10:56 +0000 | [diff] [blame] | 994 | my $everlooped = 0; |
Dave Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 995 | |
| 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 Shield | 121dfa2 | 2011-04-14 21:10:56 +0000 | [diff] [blame] | 1012 | $everlooped = 1; |
Dave Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 1013 | } else { |
Dave Shield | 121dfa2 | 2011-04-14 21:10:56 +0000 | [diff] [blame] | 1014 | if (!$everlooped) { |
| 1015 | $return = skippart(); |
| 1016 | } |
Dave Shield | f302010 | 2005-07-28 11:47:58 +0000 | [diff] [blame] | 1017 | $loop = 0; |
| 1018 | } |
| 1019 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1020 | } elsif (/\@\s*prompt\s+\$(\S+)\s*(.*)\@/) { # PROMPT |
Wes Hardaker | f9bb479 | 2003-06-26 06:15:18 +0000 | [diff] [blame] | 1021 | 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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1031 | } elsif (/\@\s*print\s+([^@]*)\@/) { # PRINT |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 1032 | my $line = process_vars($1); |
| 1033 | print "$line\n"; |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 1034 | } else { |
Robert Story | 39c6baa | 2003-07-18 20:58:36 +0000 | [diff] [blame] | 1035 | my $line = process_vars($_); |
Wes Hardaker | a216167 | 2004-04-28 22:47:01 +0000 | [diff] [blame] | 1036 | mib2c_output($line); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1037 | chop $_; |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 1038 | 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 Story | a3bc202 | 2005-07-18 20:26:25 +0000 | [diff] [blame] | 1040 | warn "$currentfile:$currentline [$_]\n"; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 1041 | m2c_die if ($strict_unk_token == 1); |
Wes Hardaker | 8bc3a9d | 1999-07-21 23:06:52 +0000 | [diff] [blame] | 1042 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 1043 | # $return = "eof"; |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 1044 | } |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 1045 | 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 Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1055 | $currentlevel--; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 1056 | return $return; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Wes Hardaker | a216167 | 2004-04-28 22:47:01 +0000 | [diff] [blame] | 1059 | sub mib2c_output { |
| 1060 | my $line = shift; |
| 1061 | foreach $file (values(%outputs)) { |
| 1062 | print $file "$line"; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1067 | sub setup_data { |
| 1068 | my $mib = shift; |
Dave Shield | 0f64978 | 2011-04-14 20:52:59 +0000 | [diff] [blame] | 1069 | if ($mib->{label} =~ /Table$/ && $#{$mib->{children}} != -1) { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1070 | my $tablename = $mib->{label}; |
| 1071 | my $entry = $mib->{children}; |
| 1072 | my $columns = $entry->[0]{children}; |
Robert Story | 5b3c1f4 | 2003-07-25 00:55:01 +0000 | [diff] [blame] | 1073 | my $augments = $entry->[0]{'augments'}; |
Robert Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 1074 | 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 Story | 5b3c1f4 | 2003-07-25 00:55:01 +0000 | [diff] [blame] | 1078 | 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 Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 1088 | push @{$tables{$tablename}{'externalindex'}}, $index; |
Robert Story | 5b3c1f4 | 2003-07-25 00:55:01 +0000 | [diff] [blame] | 1089 | } |
| 1090 | my $columns = $entry->[0]{children}; |
| 1091 | } |
Robert Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 1092 | else { |
| 1093 | foreach my $index (@{$entry->[0]{'indexes'}}) { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1094 | my $node = $SNMP::MIB{$index} || |
Robert Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 1095 | 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 Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 1106 | foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) { |
Robert Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 1107 | next if ( "@{$tables{$tablename}{'index'}}" =~ /$col->{'label'}\b/ ); |
Robert Story | 8766206 | 2003-07-23 23:43:09 +0000 | [diff] [blame] | 1108 | push @{$tables{$tablename}{'nonindex'}}, $col->{'label'}; |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1109 | } |
Robert Story | e796e88 | 2003-12-23 00:56:21 +0000 | [diff] [blame] | 1110 | # 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 Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1114 | } else { |
| 1115 | my $children = $mib->{children}; |
Wes Hardaker | e89b8e6 | 2002-05-24 16:53:07 +0000 | [diff] [blame] | 1116 | if ($#children == -1 && $mib->{type}) { |
| 1117 | # scalar |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 1118 | 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 Hardaker | d29287c | 2005-11-01 20:24:59 +0000 | [diff] [blame] | 1125 | $scalars{$mib->{label}} = 1 if ($mib->{'access'} ne 'Notify'); |
Dave Shield | d10f4e8 | 2003-06-24 13:20:14 +0000 | [diff] [blame] | 1126 | } |
Wes Hardaker | e89b8e6 | 2002-05-24 16:53:07 +0000 | [diff] [blame] | 1127 | } else { |
| 1128 | my $i; |
| 1129 | for($i = 0; $i <= $#$children; $i++) { |
| 1130 | setup_data($children->[$i]); |
| 1131 | } |
Wes Hardaker | 572f621 | 1999-03-05 00:32:16 +0000 | [diff] [blame] | 1132 | } |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 1133 | } |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | sub min { |
| 1137 | return $_[0] if ($_[0] < $_[1]); |
| 1138 | return $_[1]; |
Wes Hardaker | 81fda6e | 1998-05-07 04:08:37 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | sub max { |
Wes Hardaker | 2ece3b3 | 2001-11-22 06:33:25 +0000 | [diff] [blame] | 1142 | return $_[0] if ($_[0] > $_[1]); |
| 1143 | return $_[1]; |
Wes Hardaker | 8bc3a9d | 1999-07-21 23:06:52 +0000 | [diff] [blame] | 1144 | } |
Wes Hardaker | 1dc2dc8 | 2003-02-20 00:51:45 +0000 | [diff] [blame] | 1145 | |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1146 | sub find_conf { |
| 1147 | my ($configfile, $missingok) = (@_); |
| 1148 | |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 1149 | foreach my $d (@search_dirs, @def_search_dirs) { |
Robert Story | c3acd7a | 2005-03-18 22:46:44 +0000 | [diff] [blame] | 1150 | # print STDERR "using $d/$configfile" if (-f "$d/$configfile"); |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 1151 | return "$d/$configfile" if (-f "$d/$configfile"); |
| 1152 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1153 | return $configfile if (-f "$configfile"); |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1154 | return if ($missingok); |
| 1155 | |
| 1156 | print STDERR "Can't find a configuration file called $configfile\n"; |
Wes Hardaker | ea7bc48 | 2003-09-29 22:41:58 +0000 | [diff] [blame] | 1157 | 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 Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 1160 | exit 1; |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Wes Hardaker | 1dc2dc8 | 2003-02-20 00:51:45 +0000 | [diff] [blame] | 1163 | sub open_conf { |
| 1164 | my $configfile = shift; |
| 1165 | # process .conf file |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1166 | if (! -f "$configfile") { |
Wes Hardaker | 1dc2dc8 | 2003-02-20 00:51:45 +0000 | [diff] [blame] | 1167 | print STDERR "Can't find a configuration file called $configfile\n"; |
Robert Story | 97b019a | 2004-02-02 19:23:20 +0000 | [diff] [blame] | 1168 | exit 1; |
Wes Hardaker | 1dc2dc8 | 2003-02-20 00:51:45 +0000 | [diff] [blame] | 1169 | } |
Robert Story | 6907380 | 2003-07-19 17:52:49 +0000 | [diff] [blame] | 1170 | $currentfile = $configfile; |
| 1171 | my $fh = new IO::File; |
| 1172 | $fh->open("$configfile"); |
Wes Hardaker | 1dc2dc8 | 2003-02-20 00:51:45 +0000 | [diff] [blame] | 1173 | return $fh; |
| 1174 | } |
Wes Hardaker | f9bb479 | 2003-06-26 06:15:18 +0000 | [diff] [blame] | 1175 | |
| 1176 | sub count_scalars { |
| 1177 | my @k = keys(%scalars); |
| 1178 | return $#k + 1; |
| 1179 | } |
| 1180 | |
| 1181 | sub count_tables { |
| 1182 | my @k = keys(%tables); |
| 1183 | return $#k + 1; |
| 1184 | } |
| 1185 | |
Dave Shield | 536b787 | 2003-06-26 09:26:16 +0000 | [diff] [blame] | 1186 | sub count_columns { |
| 1187 | my $table = shift; |
| 1188 | return $#{$tables{$table}{'column'}} + 1; |
| 1189 | } |
| 1190 | |
Robert Story | 0ddde4c | 2003-07-30 17:51:39 +0000 | [diff] [blame] | 1191 | sub 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 Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 1204 | sub 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 Shield | 339f142 | 2004-08-23 09:29:30 +0000 | [diff] [blame] | 1217 | sub 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 Story | c3acd7a | 2005-03-18 22:46:44 +0000 | [diff] [blame] | 1230 | sub 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 | |
| 1244 | sub 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 Shield | 536b787 | 2003-06-26 09:26:16 +0000 | [diff] [blame] | 1257 | sub count_indexes { |
| 1258 | my $table = shift; |
| 1259 | return $#{$tables{$table}{'index'}} + 1; |
| 1260 | } |
| 1261 | |
Robert Story | f339acf | 2003-10-14 18:18:33 +0000 | [diff] [blame] | 1262 | sub count_external_indexes { |
| 1263 | my $table = shift; |
| 1264 | return $#{$tables{$table}{'externalindex'}} + 1; |
| 1265 | } |
| 1266 | |
Wes Hardaker | f9bb479 | 2003-06-26 06:15:18 +0000 | [diff] [blame] | 1267 | sub count_notifications { |
| 1268 | my @k = keys(%notifications); |
| 1269 | return $#k + 1; |
| 1270 | } |
Dave Shield | 536b787 | 2003-06-26 09:26:16 +0000 | [diff] [blame] | 1271 | |
| 1272 | sub count_varbinds { |
| 1273 | my $notify = shift; |
| 1274 | return $#{$notifyvars{$notify}} + 1; |
| 1275 | } |