5 use output qw($output);
6 use options qw($options);
10 my $function_create_callback = shift;
11 my $function_found_callback = shift;
12 my $type_create_callback = shift;
13 my $type_found_callback = shift;
14 my $preprocessor_found_callback = shift;
17 my $debug_channels = [];
23 my $documentation_line;
28 my $calling_convention;
29 my $internal_name = "";
32 my $argument_documentations;
35 $function_begin = sub {
36 $documentation_line = shift;
37 $documentation = shift;
38 $function_line = shift;
41 $calling_convention = shift;
42 $internal_name = shift;
43 $argument_types = shift;
44 $argument_names = shift;
45 $argument_documentations = shift;
47 if(defined($argument_names) && defined($argument_types) &&
48 $#$argument_names == -1)
50 foreach my $n (0..$#$argument_types) {
51 push @$argument_names, "";
55 if(defined($argument_documentations) &&
56 $#$argument_documentations == -1)
58 foreach my $n (0..$#$argument_documentations) {
59 push @$argument_documentations, "";
69 my $function = &$function_create_callback();
71 if(!defined($documentation_line)) {
72 $documentation_line = 0;
75 $function->file($file);
76 $function->debug_channels([@$debug_channels]);
77 $function->documentation_line($documentation_line);
78 $function->documentation($documentation);
79 $function->function_line($function_line);
80 $function->linkage($linkage);
81 $function->return_type($return_type);
82 $function->calling_convention($calling_convention);
83 $function->internal_name($internal_name);
84 if(defined($argument_types)) {
85 $function->argument_types([@$argument_types]);
87 if(defined($argument_names)) {
88 $function->argument_names([@$argument_names]);
90 if(defined($argument_documentations)) {
91 $function->argument_documentations([@$argument_documentations]);
93 $function->statements($statements);
95 &$function_found_callback($function);
115 foreach my $name (@$names) {
116 if($type =~ /^(?:struct|enum)/) {
117 # $output->write("typedef $type {\n");
118 # $output->write("} $name;\n");
120 # $output->write("typedef $type $name;\n");
127 my %regs_entrypoints;
128 my @comment_lines = ();
135 my $lookahead_count = 0;
137 print STDERR "Processing file '$file' ... " if $options->verbose;
138 open(IN, "< $file") || die "<internal>: $file: $!\n";
140 while($again || defined(my $line = <IN>)) {
150 $lookahead_count = 0;
152 print " $level($lookahead_count): $line\n" if $options->debug >= 2;
153 print "*** $_\n" if $options->debug >= 3;
155 $lookahead_count = 0;
159 # CVS merge conflicts in file?
160 if(/^(<<<<<<<|=======|>>>>>>>)/) {
161 $output->write("$file: merge conflicts in file\n");
166 if(s/^(.*?)(\/\*.*?\*\/)(.*)$/$1 $3/s) {
167 push @comment_lines, $.;
177 # remove C++ comments
178 while(s/^(.*?)\/\/.*?$/$1\n/s) { $again = 1 }
182 if(/^\s*$/) { next; }
184 # remove preprocessor directives
189 } elsif(s/^\#\s*(.*?)(\s+(.*?))?\s*$//m) {
191 &$preprocessor_found_callback($1, $3);
193 &$preprocessor_found_callback($1, "");
200 if(s/^\s*extern\s+"C"\s+\{//m) {
206 my $documentation_line;
208 my @argument_documentations = ();
211 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
212 $comments[$n] =~ /^\/\*\*+\/$/))
217 if(defined($comments[$n]) && $n >= 0) {
218 my @lines = split(/\n/, $comments[$n]);
220 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
221 $documentation = $comments[$n];
223 for(my $m=$n+1; $m <= $#comments; $m++) {
224 if($comments[$m] =~ /^\/\*\*+\/$/ ||
225 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
227 @argument_documentations = ();
230 push @argument_documentations, $comments[$m];
241 s/^([^\{\}\'\"]*)//s;
245 while(/^./ && !s/^\'//) {
262 while(/^./ && !s/^\"//) {
283 print "+1: \{$_\n" if $options->debug >= 2;
287 $line .= "}" if $level > 1;
288 print "-1: \}$_\n" if $options->debug >= 2;
290 if($level == -1 && $extern_c) {
296 if(!defined($statements)) {
300 if($line !~ /^\s*$/) {
301 $statements .= "$line\n";
306 &$function_end($statements);
309 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
310 my @parts = split(/\s*,\s*/, $1);
311 &$type_end([@parts]);
313 die "$file: $.: syntax error: '$_'\n";
320 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
321 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
322 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
324 my @lines = split(/\n/, $&);
325 my $function_line = $. - scalar(@lines) + 1;
334 my $return_type = $2;
335 my $calling_convention = $7;
339 if(!defined($linkage)) {
343 if(!defined($calling_convention)) {
344 $calling_convention = "";
347 $linkage =~ s/\s*$//;
349 $return_type =~ s/\s*$//;
350 $return_type =~ s/\s*\*\s*/*/g;
351 $return_type =~ s/(\*+)/ $1/g;
353 if($regs_entrypoints{$name}) {
354 $name = $regs_entrypoints{$name};
357 $arguments =~ y/\t\n/ /;
358 $arguments =~ s/^\s*(.*?)\s*$/$1/;
359 if($arguments eq "") { $arguments = "..." }
363 my @arguments = split(/,/, $arguments);
364 foreach my $n (0..$#arguments) {
365 my $argument_type = "";
366 my $argument_name = "";
367 my $argument = $arguments[$n];
368 $argument =~ s/^\s*(.*?)\s*$/$1/;
369 # print " " . ($n + 1) . ": '$argument'\n";
370 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
371 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
372 if($argument =~ /^\.\.\.$/) {
373 $argument_type = "...";
374 $argument_name = "...";
375 } elsif($argument =~ /^
376 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
377 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
378 ((?:const)?\s*(?:\*\s*?)*)\s*
379 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
381 $argument_type = "$1";
383 $argument_type .= " $2";
387 $argument_type =~ s/\s*const\s*/ /;
388 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
390 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
392 die "$file: $.: syntax error: '$argument'\n";
394 $argument_types[$n] = $argument_type;
395 $argument_names[$n] = $argument_name;
396 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
398 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
399 $#argument_types = -1;
400 $#argument_names = -1;
403 if($options->debug) {
404 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
407 &$function_begin($documentation_line, $documentation,
408 $function_line, $linkage, $return_type, $calling_convention, $name,
409 \@argument_types,\@argument_names,\@argument_documentations);
411 &$function_end($statements);
414 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
415 my @lines = split(/\n/, $&);
416 my $function_line = $. - scalar(@lines) + 1;
420 &$function_begin($documentation_line, $documentation,
421 $function_line, "", "void", "__asm", $1);
423 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
424 my @lines = split(/\n/, $&);
425 my $function_line = $. - scalar(@lines) + 1;
428 my @arguments16 = ("HWAVEIN16");
429 my @arguments32 = ("HWAVEIN");
430 &$function_begin($documentation_line, $documentation,
431 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
433 &$function_begin($documentation_line, $documentation,
434 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
436 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
437 my @lines = split(/\n/, $&);
438 my $function_line = $. - scalar(@lines) + 1;
442 my @arguments16 = ("HWAVEOUT16");
443 my @arguments32 = ("HWAVEOUT");
444 &$function_begin($documentation_line, $documentation,
445 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
447 &$function_begin($documentation_line, $documentation,
448 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
450 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
451 my @lines = split(/\n/, $&);
452 my $function_line = $. - scalar(@lines) + 1;
457 my @arguments16 = ("HWAVEOUT16", $4);
458 my @arguments32 = ("HWAVEOUT", $4);
459 &$function_begin($documentation_line, $documentation,
460 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
462 &$function_begin($documentation_line, $documentation,
463 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
466 my @arguments16 = ("UINT16", $4);
467 my @arguments32 = ("UINT", $4);
468 &$function_begin($documentation_line, $documentation,
469 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
471 &$function_begin($documentation_line, $documentation,
472 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
475 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
477 $regs_entrypoints{$2} = $1;
478 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
480 unshift @$debug_channels, $1;
481 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
483 push @$debug_channels, $1;
484 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
493 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
496 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?
497 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
505 my @parts = split(/\s*,\s*/, $2);
506 foreach my $part (@parts) {
507 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
519 &$type_end([@names]);
521 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
524 \((?:(\w+)\s+)?\s*\*\s*(\w+)\s*\)\s*
525 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
529 if(defined($2) || defined($3)) {
532 $type = "$1 ($cc *)($5)";
534 $type = "$1 ($cc *)[$6]";
538 $type = "$1 (*)($5)";
540 $type = "$1 (*)[$6]";
546 } elsif(/typedef[^\{;]*;/s) {
548 $output->write("$file: $.: can't parse: '$&'\n");
549 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
551 $output->write("$file: $.: can't parse: '$&'\n");
552 } elsif(/\'[^\']*\'/s) {
554 } elsif(/\"[^\"]*\"/s) {
558 } elsif(/extern\s+"C"\s+{/s) {
562 print "+1: $_\n" if $options->debug >= 2;
569 print STDERR "done\n" if $options->verbose;
570 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;