| 1 |
#!/usr/local/bin/perl |
| 2 |
|
| 3 |
my $ignore_re = '^(' . join("|", |
| 4 |
qw( |
| 5 |
_ |
| 6 |
[a-z] |
| 7 |
DBM |
| 8 |
DBC |
| 9 |
DB_AM_ |
| 10 |
DB_BT_ |
| 11 |
DB_RE_ |
| 12 |
DB_HS_ |
| 13 |
DB_FUNC_ |
| 14 |
DB_DBT_ |
| 15 |
DB_DBM |
| 16 |
DB_TSL |
| 17 |
MP |
| 18 |
TXN |
| 19 |
DB_TXN_GETPGNOS |
| 20 |
DB_TXN_BACKWARD_ALLOC |
| 21 |
DB_ALIGN8 |
| 22 |
)) . ')' ; |
| 23 |
|
| 24 |
my %ignore_def = map {$_, 1} qw() ; |
| 25 |
|
| 26 |
%ignore_enums = map {$_, 1} qw( ACTION db_status_t db_notices db_lockmode_t ) ; |
| 27 |
|
| 28 |
my %ignore_exact_enum = map { $_ => 1} |
| 29 |
qw( |
| 30 |
DB_TXN_GETPGNOS |
| 31 |
DB_TXN_BACKWARD_ALLOC |
| 32 |
); |
| 33 |
|
| 34 |
my $filler = ' ' x 26 ; |
| 35 |
|
| 36 |
chdir "libraries" || die "Cannot chdir into './libraries': $!\n"; |
| 37 |
|
| 38 |
foreach my $name (sort tuple glob "[2-9]*") |
| 39 |
{ |
| 40 |
next if $name =~ /(NOHEAP|NC|private)$/; |
| 41 |
|
| 42 |
my $inc = "$name/include/db.h" ; |
| 43 |
next unless -f $inc ; |
| 44 |
|
| 45 |
my $file = readFile($inc) ; |
| 46 |
StripCommentsAndStrings($file) ; |
| 47 |
my $result = scan($name, $file) ; |
| 48 |
print "\n\t#########\n\t# $name\n\t#########\n\n$result" |
| 49 |
if $result; |
| 50 |
} |
| 51 |
exit ; |
| 52 |
|
| 53 |
|
| 54 |
sub scan |
| 55 |
{ |
| 56 |
my $version = shift ; |
| 57 |
my $file = shift ; |
| 58 |
|
| 59 |
my %seen_define = () ; |
| 60 |
my $result = "" ; |
| 61 |
|
| 62 |
if (1) { |
| 63 |
# Preprocess all tri-graphs |
| 64 |
# including things stuck in quoted string constants. |
| 65 |
$file =~ s/\?\?=/#/g; # | ??=| #| |
| 66 |
$file =~ s/\?\?\!/|/g; # | ??!| || |
| 67 |
$file =~ s/\?\?'/^/g; # | ??'| ^| |
| 68 |
$file =~ s/\?\?\(/[/g; # | ??(| [| |
| 69 |
$file =~ s/\?\?\)/]/g; # | ??)| ]| |
| 70 |
$file =~ s/\?\?\-/~/g; # | ??-| ~| |
| 71 |
$file =~ s/\?\?\//\\/g; # | ??/| \| |
| 72 |
$file =~ s/\?\?</{/g; # | ??<| {| |
| 73 |
$file =~ s/\?\?>/}/g; # | ??>| }| |
| 74 |
} |
| 75 |
|
| 76 |
while ( $file =~ /^\s*#\s*define\s+([\$\w]+)\b(?!\()\s*(.*)/gm ) |
| 77 |
{ |
| 78 |
my $def = $1; |
| 79 |
my $rest = $2; |
| 80 |
my $ignore = 0 ; |
| 81 |
|
| 82 |
$ignore = 1 if $ignore_def{$def} || $def =~ /$ignore_re/o ; |
| 83 |
|
| 84 |
# Cannot do: (-1) and ((LHANDLE)3) are OK: |
| 85 |
#print("Skip non-wordy $def => $rest\n"), |
| 86 |
|
| 87 |
$rest =~ s/\s*$//; |
| 88 |
#next if $rest =~ /[^\w\$]/; |
| 89 |
|
| 90 |
#print "Matched $_ ($def)\n" ; |
| 91 |
|
| 92 |
next if $before{$def} ++ ; |
| 93 |
|
| 94 |
if ($ignore) |
| 95 |
{ $seen_define{$def} = 'IGNORE' } |
| 96 |
elsif ($rest =~ /"/) |
| 97 |
{ $seen_define{$def} = 'STRING' } |
| 98 |
else |
| 99 |
{ $seen_define{$def} = 'DEFINE' } |
| 100 |
} |
| 101 |
|
| 102 |
foreach $define (sort keys %seen_define) |
| 103 |
{ |
| 104 |
my $out = $filler ; |
| 105 |
substr($out,0, length $define) = $define; |
| 106 |
$result .= "\t$out => $seen_define{$define},\n" ; |
| 107 |
} |
| 108 |
|
| 109 |
while ($file =~ /\btypedef\s+enum\s*{(.*?)}\s*(\w+)/gs ) |
| 110 |
{ |
| 111 |
my $enum = $1 ; |
| 112 |
my $name = $2 ; |
| 113 |
my $ignore = 0 ; |
| 114 |
|
| 115 |
$ignore = 1 if $ignore_enums{$name} ; |
| 116 |
|
| 117 |
#$enum =~ s/\s*=\s*\S+\s*(,?)\s*\n/$1/g; |
| 118 |
$enum =~ s/^\s*//; |
| 119 |
$enum =~ s/\s*$//; |
| 120 |
|
| 121 |
my @tokens = map { s/\s*=.*// ; $_} split /\s*,\s*/, $enum ; |
| 122 |
my @new = grep { ! $Enums{$_}++ } @tokens ; |
| 123 |
if (@new) |
| 124 |
{ |
| 125 |
my $value ; |
| 126 |
if ($ignore) |
| 127 |
{ $value = "IGNORE, # $version" } |
| 128 |
else |
| 129 |
{ $value = "'$version'," } |
| 130 |
|
| 131 |
$result .= "\n\t# enum $name\n"; |
| 132 |
my $out = $filler ; |
| 133 |
foreach $name (@new) |
| 134 |
{ |
| 135 |
next if $ignore_exact_enum{$name} ; |
| 136 |
$out = $filler ; |
| 137 |
substr($out,0, length $name) = $name; |
| 138 |
$result .= "\t$out => $value\n" ; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return $result ; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
sub StripCommentsAndStrings |
| 148 |
{ |
| 149 |
|
| 150 |
# Strip C & C++ coments |
| 151 |
# From the perlfaq |
| 152 |
$_[0] =~ |
| 153 |
|
| 154 |
s{ |
| 155 |
/\* ## Start of /* ... */ comment |
| 156 |
[^*]*\*+ ## Non-* followed by 1-or-more *'s |
| 157 |
( |
| 158 |
[^/*][^*]*\*+ |
| 159 |
)* ## 0-or-more things which don't start with / |
| 160 |
## but do end with '*' |
| 161 |
/ ## End of /* ... */ comment |
| 162 |
|
| 163 |
| ## OR C++ Comment |
| 164 |
// ## Start of C++ comment // |
| 165 |
[^\n]* ## followed by 0-or-more non end of line characters |
| 166 |
|
| 167 |
| ## OR various things which aren't comments: |
| 168 |
|
| 169 |
( |
| 170 |
" ## Start of " ... " string |
| 171 |
( |
| 172 |
\\. ## Escaped char |
| 173 |
| ## OR |
| 174 |
[^"\\] ## Non "\ |
| 175 |
)* |
| 176 |
" ## End of " ... " string |
| 177 |
|
| 178 |
| ## OR |
| 179 |
|
| 180 |
' ## Start of ' ... ' string |
| 181 |
( |
| 182 |
\\. ## Escaped char |
| 183 |
| ## OR |
| 184 |
[^'\\] ## Non '\ |
| 185 |
)* |
| 186 |
' ## End of ' ... ' string |
| 187 |
|
| 188 |
| ## OR |
| 189 |
|
| 190 |
. ## Anything other char |
| 191 |
[^/"'\\]* ## Chars which doesn't start a comment, string or escape |
| 192 |
) |
| 193 |
}{$2}gxs; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
# Remove double-quoted strings. |
| 198 |
#$_[0] =~ s#"(\\.|[^"\\])*"##g; |
| 199 |
|
| 200 |
# Remove single-quoted strings. |
| 201 |
#$_[0] =~ s#'(\\.|[^'\\])*'##g; |
| 202 |
|
| 203 |
# Remove leading whitespace. |
| 204 |
$_[0] =~ s/\A\s+//m ; |
| 205 |
|
| 206 |
# Remove trailing whitespace. |
| 207 |
$_[0] =~ s/\s+\Z//m ; |
| 208 |
|
| 209 |
# Replace all multiple whitespace by a single space. |
| 210 |
#$_[0] =~ s/\s+/ /g ; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
sub readFile |
| 215 |
{ |
| 216 |
my $filename = shift ; |
| 217 |
open F, "<$filename" || die "Cannot open $filename: $!\n" ; |
| 218 |
local $/ ; |
| 219 |
my $x = <F> ; |
| 220 |
close F ; |
| 221 |
return $x ; |
| 222 |
} |
| 223 |
|
| 224 |
sub tuple |
| 225 |
{ |
| 226 |
my (@a) = split(/\./, $a) ; |
| 227 |
my (@b) = split(/\./, $b) ; |
| 228 |
if (@a != @b) { |
| 229 |
my $diff = @a - @b ; |
| 230 |
push @b, (0 x $diff) if $diff > 0 ; |
| 231 |
push @a, (0 x -$diff) if $diff < 0 ; |
| 232 |
} |
| 233 |
foreach $A (@a) { |
| 234 |
$B = shift @b ; |
| 235 |
$A == $B or return $A <=> $B ; |
| 236 |
} |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
__END__ |
| 241 |
|