2004-10-25 21:50:50 +00:00
|
|
|
#! /usr/bin/perl -w
|
2002-03-09 23:29:33 +00:00
|
|
|
#
|
2001-01-04 19:45:49 +00:00
|
|
|
# Copyright 2000 Patrik Stridvall
|
2002-03-09 23:29:33 +00:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-09 23:29:33 +00:00
|
|
|
#
|
2001-01-04 19:45:49 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
2004-10-25 21:50:50 +00:00
|
|
|
my $name0=$0;
|
|
|
|
$name0 =~ s%^.*/%%;
|
|
|
|
|
2001-01-04 19:45:49 +00:00
|
|
|
my $invert = 0;
|
|
|
|
my $pattern;
|
|
|
|
my @files = ();
|
2004-10-25 21:50:50 +00:00
|
|
|
my $usage;
|
2001-01-04 19:45:49 +00:00
|
|
|
|
|
|
|
while(defined($_ = shift)) {
|
2004-10-25 21:50:50 +00:00
|
|
|
if (/^-v$/) {
|
|
|
|
$invert = 1;
|
|
|
|
} elsif (/^--?(\?|h|help)$/) {
|
|
|
|
$usage=0;
|
|
|
|
} elsif (/^-/) {
|
|
|
|
print STDERR "$name0:error: unknown option '$_'\n";
|
|
|
|
$usage=2;
|
|
|
|
last;
|
|
|
|
} elsif(!defined($pattern)) {
|
|
|
|
$pattern = $_;
|
2001-01-04 19:45:49 +00:00
|
|
|
} else {
|
2004-10-25 21:50:50 +00:00
|
|
|
push @files, $_;
|
2001-01-04 19:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-25 21:50:50 +00:00
|
|
|
if (defined $usage)
|
|
|
|
{
|
|
|
|
print "Usage: $name0 [--help] [-v] pattern files...\n";
|
|
|
|
print "where:\n";
|
|
|
|
print "--help Prints this help message\n";
|
|
|
|
print "-v Return functions that do not match pattern\n";
|
|
|
|
print "pattern A regular expression for the function name\n";
|
|
|
|
print "files... A list of files to search the function in\n";
|
|
|
|
exit $usage;
|
|
|
|
}
|
2001-01-04 19:45:49 +00:00
|
|
|
|
|
|
|
foreach my $file (@files) {
|
2006-08-08 10:19:31 +00:00
|
|
|
open(IN, "< $file") || die "Error: Can't open $file: $!\n";
|
2001-01-04 19:45:49 +00:00
|
|
|
|
|
|
|
my $level = 0;
|
|
|
|
my $extern_c = 0;
|
|
|
|
|
|
|
|
my $again = 0;
|
|
|
|
my $lookahead = 0;
|
|
|
|
while($again || defined(my $line = <IN>)) {
|
|
|
|
if(!$again) {
|
|
|
|
chomp $line;
|
|
|
|
if($lookahead) {
|
|
|
|
$lookahead = 0;
|
|
|
|
$_ .= "\n" . $line;
|
|
|
|
} else {
|
|
|
|
$_ = $line;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$again = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove C comments
|
2005-01-03 20:18:58 +00:00
|
|
|
if(s/^(|.*?[^\/])(\/\*.*?\*\/)(.*)$/$1 $3/s) {
|
2001-01-04 19:45:49 +00:00
|
|
|
$again = 1;
|
|
|
|
next;
|
|
|
|
} elsif(/^(.*?)\/\*/s) {
|
|
|
|
$lookahead = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove C++ comments
|
|
|
|
while(s/^(.*?)\/\/.*?$/$1\n/s) { $again = 1; }
|
|
|
|
if($again) { next; }
|
|
|
|
|
|
|
|
# remove empty rows
|
|
|
|
if(/^\s*$/) { next; }
|
|
|
|
|
|
|
|
# remove preprocessor directives
|
|
|
|
if(s/^\s*\#/\#/m) {
|
2004-12-20 16:50:35 +00:00
|
|
|
if(/^\#[.\n\r]*?\\$/m) {
|
2001-01-04 19:45:49 +00:00
|
|
|
$lookahead = 1;
|
|
|
|
next;
|
|
|
|
} elsif(s/^\#\s*(.*?)(\s+(.*?))?\s*$//m) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove extern "C"
|
2005-01-03 20:18:58 +00:00
|
|
|
if(s/^\s*extern[\s\n]+"C"[\s\n]+\{//m) {
|
2001-01-04 19:45:49 +00:00
|
|
|
$extern_c = 1;
|
|
|
|
$again = 1;
|
2002-06-01 02:55:48 +00:00
|
|
|
next;
|
2005-01-03 20:18:58 +00:00
|
|
|
} elsif(m/^\s*extern[\s\n]+"C"/m) {
|
|
|
|
$lookahead = 1;
|
|
|
|
next;
|
2001-01-04 19:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($level > 0)
|
|
|
|
{
|
|
|
|
my $line = "";
|
|
|
|
while(/^[^\{\}]/) {
|
|
|
|
s/^([^\{\}\'\"]*)//s;
|
|
|
|
$line .= $1;
|
|
|
|
if(s/^\'//) {
|
|
|
|
$line .= "\'";
|
|
|
|
while(/^./ && !s/^\'//) {
|
|
|
|
s/^([^\'\\]*)//s;
|
|
|
|
$line .= $1;
|
|
|
|
if(s/^\\//) {
|
|
|
|
$line .= "\\";
|
|
|
|
if(s/^(.)//s) {
|
|
|
|
$line .= $1;
|
|
|
|
if($1 eq "0") {
|
|
|
|
s/^(\d{0,3})//s;
|
|
|
|
$line .= $1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$line .= "\'";
|
|
|
|
} elsif(s/^\"//) {
|
|
|
|
$line .= "\"";
|
|
|
|
while(/^./ && !s/^\"//) {
|
|
|
|
s/^([^\"\\]*)//s;
|
|
|
|
$line .= $1;
|
|
|
|
if(s/^\\//) {
|
|
|
|
$line .= "\\";
|
|
|
|
if(s/^(.)//s) {
|
|
|
|
$line .= $1;
|
|
|
|
if($1 eq "0") {
|
|
|
|
s/^(\d{0,3})//s;
|
|
|
|
$line .= $1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$line .= "\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(s/^\{//) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
$line .= "{";
|
|
|
|
$level++;
|
|
|
|
} elsif(s/^\}//) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
$line .= "}" if $level > 1;
|
|
|
|
$level--;
|
|
|
|
if($level == -1 && $extern_c) {
|
|
|
|
$extern_c = 0;
|
|
|
|
$level = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next;
|
|
|
|
} elsif(/^class[^\}]*{/) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
$level++;
|
|
|
|
next;
|
|
|
|
} elsif(/^class[^\}]*$/) {
|
|
|
|
$lookahead = 1;
|
|
|
|
next;
|
|
|
|
} elsif(/^typedef[^\}]*;/) {
|
|
|
|
next;
|
|
|
|
} elsif(/(extern\s+|static\s+)?
|
|
|
|
(?:__inline__\s+|__inline\s+|inline\s+)?
|
|
|
|
((struct\s+|union\s+|enum\s+)?(?:\w+(?:\:\:(?:\s*operator\s*[^\)\s]+)?)?)+((\s*(?:\*|\&))+\s*|\s+))
|
|
|
|
((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
|
|
|
|
((?:\w+(?:\:\:)?)+(\(\w+\))?)\s*\(([^\)]*)\)\s*
|
|
|
|
(?:\w+(?:\s*\([^\)]*\))?\s*)*\s*
|
|
|
|
(\{|\;)/sx)
|
|
|
|
{
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
if($11 eq "{") {
|
|
|
|
$level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $linkage = $1;
|
|
|
|
my $return_type = $2;
|
|
|
|
my $calling_convention = $7;
|
|
|
|
my $name = $8;
|
|
|
|
my $arguments = $10;
|
2002-06-01 02:55:48 +00:00
|
|
|
|
2001-01-04 19:45:49 +00:00
|
|
|
if(!defined($linkage)) {
|
|
|
|
$linkage = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!defined($calling_convention)) {
|
|
|
|
$calling_convention = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$linkage =~ s/\s*$//;
|
|
|
|
|
|
|
|
$return_type =~ s/\s*$//;
|
|
|
|
$return_type =~ s/\s*\*\s*/*/g;
|
|
|
|
$return_type =~ s/(\*+)/ $1/g;
|
|
|
|
|
|
|
|
$arguments =~ y/\t\n/ /;
|
|
|
|
$arguments =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
if($arguments eq "") { $arguments = "void" }
|
|
|
|
|
|
|
|
my @argument_types;
|
|
|
|
my @argument_names;
|
|
|
|
my @arguments = split(/,/, $arguments);
|
|
|
|
foreach my $n (0..$#arguments) {
|
|
|
|
my $argument_type = "";
|
|
|
|
my $argument_name = "";
|
|
|
|
my $argument = $arguments[$n];
|
|
|
|
$argument =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
# print " " . ($n + 1) . ": '$argument'\n";
|
|
|
|
$argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
|
|
|
|
$argument =~ s/^(const(?=\s)|CONST(?=\s)|__const(?=\s)|__restrict(?=\s)|\s*)\s*//;
|
|
|
|
if($argument =~ /^\.\.\.$/) {
|
|
|
|
$argument_type = "...";
|
|
|
|
$argument_name = "...";
|
|
|
|
} elsif($argument =~ /^
|
|
|
|
((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
|
|
|
|
(?:short\s+(?=int)|long\s+(?=int))?)?(?:\w+(?:\:\:)?)+)\s*
|
|
|
|
((?:const(?=\s)|CONST(?=\s)|__const(?=\s)|__restrict(?=\s))?\s*(?:\*\s*?)*)\s*
|
|
|
|
(?:const(?=\s)|CONST(?=\s)|__const(?=\s)|__restrict(?=\s))?\s*
|
|
|
|
(\w*)\s*
|
|
|
|
(?:\[\]|\s+OPTIONAL)?/x)
|
|
|
|
{
|
|
|
|
$argument_type = "$1";
|
|
|
|
if($2 ne "") {
|
|
|
|
$argument_type .= " $2";
|
|
|
|
}
|
|
|
|
$argument_name = $3;
|
|
|
|
|
|
|
|
$argument_type =~ s/\s*const\s*/ /;
|
|
|
|
$argument_type =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
|
|
|
|
$argument_name =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
} else {
|
|
|
|
die "$file: $.: syntax error: '$argument'\n";
|
|
|
|
}
|
|
|
|
$argument_types[$n] = $argument_type;
|
|
|
|
$argument_names[$n] = $argument_name;
|
|
|
|
# print " " . ($n + 1) . ": '$argument_type': '$argument_name'\n";
|
|
|
|
}
|
|
|
|
if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
|
|
|
|
$#argument_types = -1;
|
|
|
|
$#argument_names = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@arguments = ();
|
|
|
|
foreach my $n (0..$#argument_types) {
|
|
|
|
if($argument_names[$n] && $argument_names[$n] ne "...") {
|
|
|
|
if($argument_types[$n] !~ /\*$/) {
|
|
|
|
$arguments[$n] = $argument_types[$n] . " " . $argument_names[$n];
|
|
|
|
} else {
|
|
|
|
$arguments[$n] = $argument_types[$n] . $argument_names[$n];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$arguments[$n] = $argument_types[$n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$arguments = join(", ", @arguments);
|
|
|
|
if(!$arguments) { $arguments = "void"; }
|
|
|
|
|
|
|
|
if((!$invert && $name =~ /$pattern/) || ($invert && $name !~ /$pattern/)) {
|
|
|
|
if($calling_convention) {
|
|
|
|
print "$return_type $calling_convention $name($arguments)\n";
|
|
|
|
} else {
|
|
|
|
if($return_type =~ /\*$/) {
|
|
|
|
print "$return_type$name($arguments)\n";
|
|
|
|
} else {
|
|
|
|
print "$return_type $name($arguments)\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-10-25 21:50:50 +00:00
|
|
|
} elsif(/\'(?:[^\\\']*|\\.)*\'/s) {
|
2001-01-04 19:45:49 +00:00
|
|
|
$_ = $'; $again = 1;
|
2004-10-25 21:50:50 +00:00
|
|
|
} elsif(/\"(?:[^\\\"]*|\\.)*\"/s) {
|
2001-01-04 19:45:49 +00:00
|
|
|
$_ = $'; $again = 1;
|
|
|
|
} elsif(/;/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
} elsif(/extern\s+"C"\s+{/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
} elsif(/\{/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
$level++;
|
|
|
|
} else {
|
|
|
|
$lookahead = 1;
|
|
|
|
}
|
|
|
|
}
|
2002-06-01 02:55:48 +00:00
|
|
|
close(IN);
|
2001-01-04 19:45:49 +00:00
|
|
|
}
|