| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use File::Copy; |
| 5 |
use File::Basename; |
| 6 |
use IO::File; |
| 7 |
use Getopt::Std; |
| 8 |
|
| 9 |
my $ID_NOREAD = "NETSNMP_NO_READ_SUPPORT"; |
| 10 |
my $ID_NOWRITE = "NETSNMP_NO_WRITE_SUPPORT"; |
| 11 |
# my $ID_MIN = "NETSNMP_MINIMAL_CODE"; |
| 12 |
|
| 13 |
my($ST_OFF, $ST_IF, $ST_ELSE, $ST_IFN, $ST_ELSEN) = |
| 14 |
("off", "if", "else", "ifnot", "elsenot"); |
| 15 |
|
| 16 |
my %opts = (); |
| 17 |
my %thash = (); |
| 18 |
my $canwrite = 1; # current write state |
| 19 |
|
| 20 |
my($appname,$apppath) = fileparse($0); |
| 21 |
|
| 22 |
my $minimal_include_path = "include/net-snmp/net-snmp-features.h"; |
| 23 |
|
| 24 |
|
| 25 |
if ( (!getopts("rwmi:v",\%opts)) || (1 != $#ARGV) ) { |
| 26 |
print "$appname [options] from-directory to-direpctory\n"; |
| 27 |
print "-r parse out code unneeded by $ID_NOREAD ifdef\n"; |
| 28 |
print "-w parse out code unneeded by $ID_NOWRITE ifdef (DEFAULT)\n"; |
| 29 |
print "-m parse out code unneeded according minimalist ifdefs\n"; |
| 30 |
print " requires from-directory to be the top of the net-snmp tree\n"; |
| 31 |
print " (This is multiple ifdefs auto selected depending\n"; |
| 32 |
print " on the net-snmp source configuration)\n\n"; |
| 33 |
print "-i 'ignore-file' file of files to ignore (not copy)\n"; |
| 34 |
print "-v print verbose info to standard out\n"; |
| 35 |
die "Error: two command line arguments required\n"; |
| 36 |
} |
| 37 |
|
| 38 |
#default to everything |
| 39 |
if ( (!exists $opts{r}) && (!exists $opts{w}) && (!exists $opts{m}) ) { |
| 40 |
$thash{"$ID_NOWRITE"} = "$ST_OFF"; |
| 41 |
} |
| 42 |
else { |
| 43 |
$thash{"$ID_NOREAD"} = "$ST_OFF" if ( exists $opts{r} ); |
| 44 |
$thash{"$ID_NOWRITE"} = "$ST_OFF" if ( exists $opts{w} ); |
| 45 |
} |
| 46 |
|
| 47 |
my $fromdir = $ARGV[0]; |
| 48 |
my $todir = $ARGV[1]; |
| 49 |
|
| 50 |
if ( !(-e $fromdir) ) { |
| 51 |
die "Error: $appname: from directory does not exist: '$fromdir'\n"; |
| 52 |
} |
| 53 |
if ( !(-d $fromdir) ) { |
| 54 |
die "Error: $appname: from directory, '$fromdir', must be a directory\n"; |
| 55 |
} |
| 56 |
|
| 57 |
if ( exists $opts{m} ) { |
| 58 |
load_minamal_ifdefs(); |
| 59 |
} |
| 60 |
|
| 61 |
# create search string from tags |
| 62 |
# minimal must be done before this |
| 63 |
my $search = join "|", (keys %thash); |
| 64 |
|
| 65 |
|
| 66 |
# check for and load ignore file |
| 67 |
my $igstring = ""; |
| 68 |
if ( exists $opts{i} ) { |
| 69 |
open my($IH), "< $opts{i}" or |
| 70 |
die "Could not open ignore-file '$opts{i}': $!"; |
| 71 |
my @iglist = <$IH>; |
| 72 |
$IH->close(); |
| 73 |
chomp @iglist; |
| 74 |
$igstring = join "|", @iglist; |
| 75 |
$igstring = "(" . $igstring . ")"; |
| 76 |
print "ignore string: \'$igstring\'\n" if (exists $opts{v}); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if ( -e $todir ) { |
| 82 |
if ( ((-d $fromdir) && !(-d $todir)) || |
| 83 |
(!(-d $fromdir) && (-d $todir)) ) { |
| 84 |
die "Error: $appname: from-directory and to-directory must both either be a file or both be a directory\n"; |
| 85 |
} |
| 86 |
} |
| 87 |
else { |
| 88 |
if (-d $fromdir) { |
| 89 |
print "Warning: $appname: '$todir' does not exist, creating\n"; |
| 90 |
mkdir "$todir" or |
| 91 |
die "Error: $appname: Unable to create to-directory '$todir': $!\n"; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (-d $fromdir) { |
| 96 |
parsedirectory($fromdir, $todir); |
| 97 |
} |
| 98 |
else { |
| 99 |
parsefile($fromdir, $todir); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
exit(0); |
| 105 |
|
| 106 |
|
| 107 |
# PROCEDURES PROCEDURES PROCEDURES PROCEDURES PROCEDURES |
| 108 |
|
| 109 |
|
| 110 |
sub parsedirectory { |
| 111 |
my($fdir, $tdir) = @_; |
| 112 |
|
| 113 |
my @ldirs = (); |
| 114 |
my $DH; |
| 115 |
opendir my($DH), $fdir or die "Could not open directory '$fdir': $!"; |
| 116 |
if ( !(-e $tdir) || !(-d $tdir) ) { |
| 117 |
mkdir $tdir or die "Could not create directory '$tdir': $!"; |
| 118 |
} |
| 119 |
my @flist = readdir $DH; |
| 120 |
closedir $DH; |
| 121 |
# remove . and .. |
| 122 |
@flist = grep (! /(^\.$|^\.\.$)/ , @flist); |
| 123 |
|
| 124 |
while (my $name = shift @flist) { |
| 125 |
if (-d "$fdir/$name") { |
| 126 |
push @ldirs, "$name"; |
| 127 |
} |
| 128 |
else { |
| 129 |
parsefile("$fdir/$name", "$tdir/$name"); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
while (my $name = shift @ldirs) { |
| 134 |
parsedirectory("$fdir/$name", "$tdir/$name") |
| 135 |
} |
| 136 |
|
| 137 |
} # parsedirectory |
| 138 |
|
| 139 |
|
| 140 |
# returns 1 if current state for tag is write, 0 otherwise |
| 141 |
sub iswritestate { |
| 142 |
my $tag = ""; |
| 143 |
|
| 144 |
foreach $tag (keys %thash) { |
| 145 |
if ( ($thash{$tag} eq "$ST_ELSE") || |
| 146 |
($thash{$tag} eq "$ST_IFN") ) { |
| 147 |
return(0); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return(1); |
| 152 |
} # iswritestate |
| 153 |
|
| 154 |
|
| 155 |
# Check $line for ifdef state changes for all of the tags and change |
| 156 |
# state. |
| 157 |
# If there is a state change error return 0, otherwise return 1; |
| 158 |
|
| 159 |
sub checkifdef { |
| 160 |
my($TF, $line, $fromfilename) = @_; |
| 161 |
|
| 162 |
if ( $line =~ /(#ifdef|#ifndef|#else|#endif).*($search)(\s|$|\*)/ ) { |
| 163 |
my $copt = $1; |
| 164 |
my $tag = $2; |
| 165 |
|
| 166 |
if ( $copt eq "#ifdef" ) { |
| 167 |
if ($thash{"$tag"} eq "$ST_OFF") { |
| 168 |
$thash{"$tag"} = "$ST_IF"; |
| 169 |
print "state change $tag: $ST_OFF -> $ST_IF\n" if (exists $opts{v}); |
| 170 |
$canwrite = iswritestate(); |
| 171 |
} |
| 172 |
else { |
| 173 |
print "Error: $fromfilename: Found '#ifdef $tag' with state $thash{$tag}\n"; |
| 174 |
return 0; |
| 175 |
} |
| 176 |
} |
| 177 |
elsif ( $copt eq "#ifndef" ) { |
| 178 |
if ($thash{"$tag"} eq "$ST_OFF") { |
| 179 |
# before changing to a non-write state (ifn) print #IFNDEF |
| 180 |
# line, if current state is a write state. |
| 181 |
print $TF "$line" if ( $canwrite ); |
| 182 |
$thash{"$tag"} = "$ST_IFN"; |
| 183 |
print "state change $tag: $ST_OFF -> $ST_IFN\n" if (exists $opts{v}); |
| 184 |
$canwrite = iswritestate(); |
| 185 |
} |
| 186 |
else { |
| 187 |
print "Error: $fromfilename: Found '#ifndef $tag' with state $thash{$tag}\n"; |
| 188 |
return 0; |
| 189 |
} |
| 190 |
} |
| 191 |
elsif ( $copt eq "#else" ) { |
| 192 |
if ($thash{"$tag"} eq "$ST_IF") { |
| 193 |
# before changing to a non-write state (else) print #else |
| 194 |
# line, if current state is a write state. |
| 195 |
print $TF "$line" if ( $canwrite ); |
| 196 |
$thash{"$tag"} = "$ST_ELSE"; |
| 197 |
print "state change $tag: $ST_IF -> $ST_ELSE\n" if (exists $opts{v}); |
| 198 |
$canwrite = iswritestate(); |
| 199 |
} |
| 200 |
elsif ($thash{"$tag"} eq "$ST_IFN") { |
| 201 |
$thash{"$tag"} = "$ST_ELSEN"; |
| 202 |
print "state change $tag: $ST_IFN -> $ST_ELSEN\n" if (exists $opts{v}); |
| 203 |
$canwrite = iswritestate(); |
| 204 |
} |
| 205 |
else { |
| 206 |
print "Error: $fromfilename: Found '#else (...) $tag' with state $thash{$tag}\n"; |
| 207 |
return 0; |
| 208 |
} |
| 209 |
} |
| 210 |
elsif ( $copt eq "#endif" ) { |
| 211 |
if (($thash{"$tag"} eq "$ST_ELSE") || ($thash{"$tag"} eq "$ST_IF") || |
| 212 |
($thash{"$tag"} eq "$ST_ELSEN") || ($thash{"$tag"} eq "$ST_IFN")) |
| 213 |
{ |
| 214 |
print "state change $tag: $thash{$tag} -> $ST_OFF\n" |
| 215 |
if (exists $opts{v}); |
| 216 |
$thash{"$tag"} = "$ST_OFF"; |
| 217 |
$canwrite = iswritestate(); |
| 218 |
} |
| 219 |
else { |
| 220 |
print "Error: Found '#endif (...) $tag' with state $thash{$tag}\n"; |
| 221 |
return 0; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
} # foreach tag |
| 226 |
|
| 227 |
return 1; |
| 228 |
} # checkifdef |
| 229 |
|
| 230 |
|
| 231 |
sub parsefile { |
| 232 |
my($fname, $tname) = @_; |
| 233 |
my $FF; my $TF; |
| 234 |
my @fromfile = (); |
| 235 |
$canwrite = 1; |
| 236 |
|
| 237 |
# ignore file for file names |
| 238 |
if ( (exists $opts{i}) && ("$fname" =~ /$igstring/) ) { |
| 239 |
print "IGNORING $fname\n" if ( exists $opts{v} ); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
|
| 243 |
print "Info: Opening '$fname'\n" if ( exists $opts{v} ); |
| 244 |
if ( !(open($FF, "< $fname")) ) { |
| 245 |
print "Warning: unable to open input file, skipping: '$fname': $!\n"; |
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
my @fromfile = <$FF>; |
| 250 |
$FF->close(); |
| 251 |
|
| 252 |
if ( !(open($TF, "> $tname")) ) { |
| 253 |
print "Warning: unable to open output file, skipping: '$tname': $!\n"; |
| 254 |
return 0; |
| 255 |
} |
| 256 |
my $mode = (stat("$fname"))[2]; |
| 257 |
if ($mode) { my $resp = chmod $mode, "$tname"; } |
| 258 |
|
| 259 |
my $line = ""; |
| 260 |
my @tout = (); |
| 261 |
my $retval = 1; |
| 262 |
|
| 263 |
while ( $line = shift @fromfile ) { |
| 264 |
# check for any ifdef state changes |
| 265 |
if ( ! checkifdef($TF, $line, $fname) ) { |
| 266 |
$FF->close(); |
| 267 |
$TF->close(); |
| 268 |
die "Error: tag error in file \'$fname\', exiting\n"; |
| 269 |
} |
| 270 |
|
| 271 |
if ( $canwrite ) { |
| 272 |
print $TF "$line"; |
| 273 |
} |
| 274 |
else { |
| 275 |
print "Info: not copying: $fname: $line" if ( exists $opts{v} ); |
| 276 |
} |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
if (! $canwrite) { |
| 281 |
print "End of $fname reached and we're not reset into 'canwrite' state\n"; |
| 282 |
} |
| 283 |
$TF->close(); |
| 284 |
|
| 285 |
return $retval; |
| 286 |
} # parsefile |
| 287 |
|
| 288 |
|
| 289 |
# note, fromdir should have already been checked to exist and be a |
| 290 |
# directory |
| 291 |
sub load_minamal_ifdefs { |
| 292 |
my @filelist = (); |
| 293 |
my $MF; |
| 294 |
|
| 295 |
if ( !(open($MF, "< $fromdir/$minimal_include_path")) ) { |
| 296 |
die "Unable to open main minimal feature file: '$fromdir/$minimal_include_path'\n"; |
| 297 |
} |
| 298 |
my $line; |
| 299 |
# skip preceding lines |
| 300 |
while ( ($line = <$MF>) && |
| 301 |
($line !~ /^#else.*NETSNMP_FEATURE_CHECKING/ ) ) { |
| 302 |
} |
| 303 |
# grab the fetaure .h files |
| 304 |
while ( ($line = <$MF>) && |
| 305 |
($line !~ /^#endif.*NET_SNMP_FEATURE_CHECKING/) ) { |
| 306 |
if ($line =~ /include.*<(.*.h)>/) { |
| 307 |
push @filelist, $1; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
close($MF); |
| 312 |
|
| 313 |
while (my $fname = shift @filelist) { |
| 314 |
if ( !( -e "$fromdir/include/$fname" ) ) { |
| 315 |
print "Warn: feature file does not exist, skipping: '$fromdir/include/$fname'"; |
| 316 |
next; |
| 317 |
} |
| 318 |
if ( !(open($MF, "< $fromdir/include/$fname")) ) { |
| 319 |
die "Unable to open minimal feature file: '$fromdir/include/$fname'\n"; |
| 320 |
} |
| 321 |
while ( ($line = <$MF>) ) { |
| 322 |
if ( $line =~ /^#define.*(NETSNMP_FEATURE_REMOVE[^ ]+) / ) { |
| 323 |
$thash{"$1"} = $ST_OFF; |
| 324 |
} |
| 325 |
} |
| 326 |
close($MF); |
| 327 |
} |
| 328 |
|
| 329 |
} # load_minamal_ifdefs |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|