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