1999-09-23 17:14:20 +02:00
|
|
|
package winapi_parser;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
sub parse_c_file {
|
|
|
|
my $options = shift;
|
1999-10-31 03:08:38 +01:00
|
|
|
my $output = shift;
|
1999-09-23 17:14:20 +02:00
|
|
|
my $file = shift;
|
|
|
|
my $function_found_callback = shift;
|
1999-10-24 23:45:39 +02:00
|
|
|
my $preprocessor_found_callback = shift;
|
|
|
|
|
1999-10-31 03:08:38 +01:00
|
|
|
my $documentation;
|
2000-03-24 21:39:51 +01:00
|
|
|
my $linkage;
|
1999-10-24 23:45:39 +02:00
|
|
|
my $return_type;
|
|
|
|
my $calling_convention;
|
|
|
|
my $function = "";
|
2000-05-11 23:46:17 +02:00
|
|
|
my $argument_types;
|
|
|
|
my $argument_names;
|
1999-10-24 23:45:39 +02:00
|
|
|
my $statements;
|
|
|
|
|
|
|
|
my $function_begin = sub {
|
1999-10-31 03:08:38 +01:00
|
|
|
$documentation = shift;
|
2000-03-24 21:39:51 +01:00
|
|
|
$linkage = shift;
|
1999-10-24 23:45:39 +02:00
|
|
|
$return_type= shift;
|
|
|
|
$calling_convention = shift;
|
|
|
|
$function = shift;
|
2000-05-11 23:46:17 +02:00
|
|
|
$argument_types = shift;
|
|
|
|
$argument_names = shift;
|
|
|
|
|
|
|
|
if($#$argument_names == -1) {
|
|
|
|
foreach my $n (0..$#$argument_types) {
|
|
|
|
push @$argument_names, "";
|
|
|
|
}
|
|
|
|
}
|
1999-10-24 23:45:39 +02:00
|
|
|
|
|
|
|
$statements = "";
|
|
|
|
};
|
|
|
|
my $function_end = sub {
|
2000-05-11 23:46:17 +02:00
|
|
|
&$function_found_callback($documentation,$linkage,$return_type,$calling_convention,
|
|
|
|
$function,$argument_types,$argument_names,$statements);
|
1999-10-24 23:45:39 +02:00
|
|
|
$function = "";
|
|
|
|
};
|
1999-09-23 17:14:20 +02:00
|
|
|
|
1999-12-12 00:01:45 +01:00
|
|
|
my %regs_entrypoints;
|
1999-10-31 03:08:38 +01:00
|
|
|
my @comments = ();
|
1999-09-23 17:14:20 +02:00
|
|
|
my $level = 0;
|
2000-04-06 22:11:04 +02:00
|
|
|
my $extern_c = 0;
|
1999-09-23 17:14:20 +02:00
|
|
|
my $again = 0;
|
|
|
|
my $lookahead = 0;
|
|
|
|
my $lookahead_count = 0;
|
|
|
|
|
|
|
|
print STDERR "Processing file '$file' ... " if $options->verbose;
|
|
|
|
open(IN, "< $file") || die "<internal>: $file: $!\n";
|
|
|
|
$/ = "\n";
|
|
|
|
while($again || defined(my $line = <IN>)) {
|
|
|
|
if(!$again) {
|
|
|
|
chomp $line;
|
|
|
|
|
|
|
|
if($lookahead) {
|
|
|
|
$lookahead = 0;
|
|
|
|
$_ .= "\n" . $line;
|
|
|
|
} else {
|
|
|
|
$_ = $line;
|
|
|
|
$lookahead_count = 0;
|
|
|
|
}
|
|
|
|
$lookahead_count++;
|
2000-04-06 22:11:04 +02:00
|
|
|
print " $level($lookahead_count): $line\n" if $options->debug >= 2;
|
|
|
|
print "*** $_\n" if $options->debug >= 3;
|
1999-09-23 17:14:20 +02:00
|
|
|
} else {
|
|
|
|
$lookahead_count = 0;
|
|
|
|
$again = 0;
|
|
|
|
}
|
1999-10-31 03:08:38 +01:00
|
|
|
|
|
|
|
# Merge conflicts in file?
|
|
|
|
if(/^(<<<<<<<|=======|>>>>>>>)/) {
|
|
|
|
$output->write("$file: merge conflicts in file\n");
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
|
2000-05-18 02:07:31 +02:00
|
|
|
# remove C comments
|
|
|
|
if(s/^(.*?)(\/\*.*?\*\/)(.*)$/$1 $3/s) { push @comments, $2; $again = 1; next }
|
1999-09-23 17:14:20 +02:00
|
|
|
if(/^(.*?)\/\*/s) {
|
|
|
|
$lookahead = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2000-05-18 02:07:31 +02:00
|
|
|
# remove C++ comments
|
|
|
|
while(s/^(.*?)\/\/.*?$/$1\n/s) { $again = 1 }
|
|
|
|
if($again) { next; }
|
|
|
|
|
1999-09-23 17:14:20 +02:00
|
|
|
# remove empty rows
|
|
|
|
if(/^\s*$/) { next; }
|
|
|
|
|
|
|
|
# remove preprocessor directives
|
1999-10-24 23:45:39 +02:00
|
|
|
if(s/^\s*\#/\#/m) {
|
|
|
|
if(/^\\#.*?\\$/m) {
|
|
|
|
$lookahead = 1;
|
|
|
|
next;
|
|
|
|
} elsif(s/^\#\s*(.*?)(\s+(.*?))?\s*$//m) {
|
|
|
|
if(defined($3)) {
|
|
|
|
&$preprocessor_found_callback($1, $3);
|
|
|
|
} else {
|
|
|
|
&$preprocessor_found_callback($1, "");
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
1999-09-23 17:14:20 +02:00
|
|
|
|
2000-04-06 22:11:04 +02:00
|
|
|
# Remove extern "C"
|
|
|
|
if(s/^\s*extern\s+"C"\s+\{//m) {
|
|
|
|
$extern_c = 1;
|
|
|
|
$again = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
1999-10-31 03:08:38 +01:00
|
|
|
my $documentation;
|
|
|
|
{
|
|
|
|
my $n = $#comments;
|
2000-03-24 21:39:51 +01:00
|
|
|
while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ || $comments[$n] =~ /^\/\*\*+\//)) { $n-- }
|
1999-10-31 03:08:38 +01:00
|
|
|
if(defined($comments[$n]) && $n >= 0) {
|
|
|
|
$documentation = $comments[$n];
|
|
|
|
} else {
|
|
|
|
$documentation = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-23 17:14:20 +02:00
|
|
|
if($level > 0)
|
|
|
|
{
|
2000-03-24 21:39:51 +01:00
|
|
|
my $line = "";
|
|
|
|
while(/^[^\{\}]/) {
|
|
|
|
s/^([^\{\}\'\"]*)//s;
|
1999-10-24 23:45:39 +02:00
|
|
|
$line .= $1;
|
2000-03-24 21:39:51 +01:00
|
|
|
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 .= "{";
|
|
|
|
print "+1: \{$_\n" if $options->debug >= 2;
|
1999-09-23 17:14:20 +02:00
|
|
|
$level++;
|
2000-03-24 21:39:51 +01:00
|
|
|
} elsif(s/^\}//) {
|
1999-09-23 17:14:20 +02:00
|
|
|
$_ = $'; $again = 1;
|
2000-03-24 21:39:51 +01:00
|
|
|
$line .= "}" if $level > 1;
|
|
|
|
print "-1: \}$_\n" if $options->debug >= 2;
|
1999-09-23 17:14:20 +02:00
|
|
|
$level--;
|
2000-04-06 22:11:04 +02:00
|
|
|
if($level == -1 && $extern_c) {
|
|
|
|
$extern_c = 0;
|
|
|
|
$level = 0;
|
|
|
|
}
|
1999-09-23 17:14:20 +02:00
|
|
|
}
|
2000-03-24 21:39:51 +01:00
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
if($line !~ /^\s*$/) {
|
|
|
|
$statements .= "$line\n";
|
2000-03-24 21:39:51 +01:00
|
|
|
}
|
|
|
|
|
1999-10-24 23:45:39 +02:00
|
|
|
if($function && $level == 0) {
|
|
|
|
&$function_end;
|
|
|
|
}
|
2000-04-06 22:11:04 +02:00
|
|
|
next;
|
2000-06-11 22:02:29 +02:00
|
|
|
} elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+)?\w+((\s*\*)+\s*|\s+))
|
|
|
|
((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
|
|
|
|
(\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
|
|
|
|
{
|
1999-09-23 17:14:20 +02:00
|
|
|
$_ = $'; $again = 1;
|
2000-03-24 21:39:51 +01:00
|
|
|
|
|
|
|
if($11 eq "{") {
|
1999-09-23 17:14:20 +02:00
|
|
|
$level++;
|
2000-03-24 21:39:51 +01:00
|
|
|
}
|
2000-04-15 22:39:55 +02:00
|
|
|
|
2000-03-24 21:39:51 +01:00
|
|
|
my $linkage = $1;
|
|
|
|
my $return_type = $2;
|
|
|
|
my $calling_convention = $7;
|
|
|
|
my $name = $8;
|
|
|
|
my $arguments = $10;
|
|
|
|
|
|
|
|
if(!defined($linkage)) {
|
|
|
|
$linkage = "";
|
|
|
|
}
|
1999-10-24 23:45:39 +02:00
|
|
|
|
|
|
|
if(!defined($calling_convention)) {
|
|
|
|
$calling_convention = "";
|
|
|
|
}
|
1999-09-23 17:14:20 +02:00
|
|
|
|
2000-03-24 21:39:51 +01:00
|
|
|
$linkage =~ s/\s*$//;
|
|
|
|
|
1999-09-23 17:14:20 +02:00
|
|
|
$return_type =~ s/\s*$//;
|
|
|
|
$return_type =~ s/\s*\*\s*/*/g;
|
|
|
|
$return_type =~ s/(\*+)/ $1/g;
|
|
|
|
|
1999-12-12 00:01:45 +01:00
|
|
|
if($regs_entrypoints{$name}) {
|
|
|
|
$name = $regs_entrypoints{$name};
|
|
|
|
}
|
1999-09-23 17:14:20 +02:00
|
|
|
|
|
|
|
$arguments =~ y/\t\n/ /;
|
|
|
|
$arguments =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
if($arguments eq "") { $arguments = "void" }
|
|
|
|
|
2000-05-11 23:46:17 +02:00
|
|
|
my @argument_types;
|
|
|
|
my @argument_names;
|
1999-09-23 17:14:20 +02:00
|
|
|
my @arguments = split(/,/, $arguments);
|
|
|
|
foreach my $n (0..$#arguments) {
|
2000-05-11 23:46:17 +02:00
|
|
|
my $argument_type = "";
|
|
|
|
my $argument_name = "";
|
1999-09-23 17:14:20 +02:00
|
|
|
my $argument = $arguments[$n];
|
|
|
|
$argument =~ s/^\s*(.*?)\s*$/$1/;
|
2000-05-11 23:46:17 +02:00
|
|
|
# print " " . ($n + 1) . ": '$argument'\n";
|
1999-10-24 23:45:39 +02:00
|
|
|
$argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
|
1999-12-12 00:01:45 +01:00
|
|
|
$argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
|
2000-04-29 16:24:11 +02:00
|
|
|
if($argument =~ /^\.\.\.$/) {
|
2000-05-11 23:46:17 +02:00
|
|
|
$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*(?:\*\s*?)*)\s*(?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/) {
|
|
|
|
$argument_type = "$1";
|
|
|
|
if($2 ne "") {
|
|
|
|
$argument_type .= " $2";
|
1999-09-23 17:14:20 +02:00
|
|
|
}
|
2000-05-11 23:46:17 +02:00
|
|
|
$argument_name = $3;
|
|
|
|
|
|
|
|
$argument_type =~ s/\s*const\s*/ /;
|
|
|
|
$argument_type =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
|
|
|
|
$argument_name =~ s/^\s*(.*?)\s*$/$1/;
|
1999-09-23 17:14:20 +02:00
|
|
|
} else {
|
|
|
|
die "$file: $.: syntax error: '$argument'\n";
|
|
|
|
}
|
2000-05-11 23:46:17 +02:00
|
|
|
$argument_types[$n] = $argument_type;
|
|
|
|
$argument_names[$n] = $argument_name;
|
|
|
|
# print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
|
|
|
|
}
|
|
|
|
if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
|
|
|
|
$#argument_types = -1;
|
|
|
|
$#argument_names = -1;
|
1999-09-23 17:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($options->debug) {
|
|
|
|
print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
|
|
|
|
}
|
|
|
|
|
2000-05-11 23:46:17 +02:00
|
|
|
&$function_begin($documentation,$linkage,$return_type,$calling_convention,$name,\@argument_types,\@argument_names);
|
2000-03-24 21:39:51 +01:00
|
|
|
if($level == 0) {
|
|
|
|
&$function_end;
|
|
|
|
}
|
1999-09-23 17:14:20 +02:00
|
|
|
} elsif(/DC_(GET_X_Y|GET_VAL_16)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
my @arguments = ("HDC16");
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", $2, "WINAPI", $3, \@arguments);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-23 17:14:20 +02:00
|
|
|
} elsif(/DC_(GET_VAL_32)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,.*?\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
my @arguments = ("HDC");
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", $2, "WINAPI", $3, \@arguments);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-23 17:14:20 +02:00
|
|
|
} elsif(/DC_(GET_VAL_EX)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
my @arguments16 = ("HDC16", "LP" . $5 . "16");
|
|
|
|
my @arguments32 = ("HDC", "LP" . $5);
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "BOOL16", "WINAPI", $2 . "16", \@arguments16);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "BOOL", "WINAPI", $2, \@arguments32);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-23 17:14:20 +02:00
|
|
|
} elsif(/DC_(SET_MODE)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
my @arguments16 = ("HDC16", "INT16");
|
|
|
|
my @arguments32 = ("HDC", "INT");
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "INT16", "WINAPI", $2 . "16", \@arguments16);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "INT", "WINAPI", $2, \@arguments32);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-29 12:22:00 +02:00
|
|
|
} elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
my @arguments16 = ("HWAVEIN16");
|
|
|
|
my @arguments32 = ("HWAVEIN");
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-29 12:22:00 +02:00
|
|
|
} elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
my @arguments16 = ("HWAVEOUT16");
|
|
|
|
my @arguments32 = ("HWAVEOUT");
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-23 17:14:20 +02:00
|
|
|
} elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
if($1 eq "1") {
|
|
|
|
my @arguments16 = ("HWAVEOUT16", $4);
|
|
|
|
my @arguments32 = ("HWAVEOUT", $4);
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-23 17:14:20 +02:00
|
|
|
} elsif($1 eq 2) {
|
|
|
|
my @arguments16 = ("UINT16", $4);
|
|
|
|
my @arguments32 = ("UINT", $4);
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
2000-03-24 21:39:51 +01:00
|
|
|
&$function_begin($documentation, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
|
1999-10-24 23:45:39 +02:00
|
|
|
&$function_end;
|
1999-09-23 17:14:20 +02:00
|
|
|
}
|
1999-12-12 00:01:45 +01:00
|
|
|
} elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
$regs_entrypoints{$2} = $1;
|
2000-03-24 21:39:51 +01:00
|
|
|
} elsif(/\'[^\']*\'/s) {
|
1999-09-23 17:14:20 +02:00
|
|
|
$_ = $'; $again = 1;
|
2000-03-24 21:39:51 +01:00
|
|
|
} elsif(/\"[^\"]*\"/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
} elsif(/;/s) {
|
1999-09-23 17:14:20 +02:00
|
|
|
$_ = $'; $again = 1;
|
2000-04-06 22:11:04 +02:00
|
|
|
} elsif(/extern\s+"C"\s+{/s) {
|
|
|
|
$_ = $'; $again = 1;
|
2000-03-24 21:39:51 +01:00
|
|
|
} elsif(/\{/s) {
|
|
|
|
$_ = $'; $again = 1;
|
|
|
|
print "+1: $_\n" if $options->debug >= 2;
|
|
|
|
$level++;
|
1999-09-23 17:14:20 +02:00
|
|
|
} else {
|
|
|
|
$lookahead = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(IN);
|
|
|
|
print STDERR "done\n" if $options->verbose;
|
1999-10-31 03:08:38 +01:00
|
|
|
$output->write("$file: not at toplevel at end of file\n") unless $level == 0;
|
1999-09-23 17:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|