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($line !~ /^\s*$/) {
297 $statements .= "$line\n";
302 &$function_end($statements);
305 if(/^\s*(?:WINE_PACKED\s+)?((?:\*\s*)?\w+\s*(?:\s*,\s*(?:\*+\s*)?\w+)*\s*);/s) {
306 my @parts = split(/\s*,\s*/, $1);
307 &$type_end([@parts]);
309 die "$file: $.: syntax error: '$_'\n";
316 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
317 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
318 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
320 my @lines = split(/\n/, $&);
321 my $function_line = $. - scalar(@lines) + 1;
330 my $return_type = $2;
331 my $calling_convention = $7;
335 if(!defined($linkage)) {
339 if(!defined($calling_convention)) {
340 $calling_convention = "";
343 $linkage =~ s/\s*$//;
345 $return_type =~ s/\s*$//;
346 $return_type =~ s/\s*\*\s*/*/g;
347 $return_type =~ s/(\*+)/ $1/g;
349 if($regs_entrypoints{$name}) {
350 $name = $regs_entrypoints{$name};
353 $arguments =~ y/\t\n/ /;
354 $arguments =~ s/^\s*(.*?)\s*$/$1/;
355 if($arguments eq "") { $arguments = "..." }
359 my @arguments = split(/,/, $arguments);
360 foreach my $n (0..$#arguments) {
361 my $argument_type = "";
362 my $argument_name = "";
363 my $argument = $arguments[$n];
364 $argument =~ s/^\s*(.*?)\s*$/$1/;
365 # print " " . ($n + 1) . ": '$argument'\n";
366 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
367 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
368 if($argument =~ /^\.\.\.$/) {
369 $argument_type = "...";
370 $argument_name = "...";
371 } elsif($argument =~ /^
372 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
373 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
374 ((?:const)?\s*(?:\*\s*?)*)\s*
375 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
377 $argument_type = "$1";
379 $argument_type .= " $2";
383 $argument_type =~ s/\s*const\s*/ /;
384 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
386 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
388 die "$file: $.: syntax error: '$argument'\n";
390 $argument_types[$n] = $argument_type;
391 $argument_names[$n] = $argument_name;
392 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
394 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
395 $#argument_types = -1;
396 $#argument_names = -1;
399 if($options->debug) {
400 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
403 &$function_begin($documentation_line, $documentation,
404 $function_line, $linkage, $return_type, $calling_convention, $name,
405 \@argument_types,\@argument_names,\@argument_documentations);
407 &$function_end(undef);
410 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
411 my @lines = split(/\n/, $&);
412 my $function_line = $. - scalar(@lines) + 1;
416 &$function_begin($documentation_line, $documentation,
417 $function_line, "", "void", "__asm", $1);
419 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
420 my @lines = split(/\n/, $&);
421 my $function_line = $. - scalar(@lines) + 1;
424 my @arguments16 = ("HWAVEIN16");
425 my @arguments32 = ("HWAVEIN");
426 &$function_begin($documentation_line, $documentation,
427 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
429 &$function_begin($documentation_line, $documentation,
430 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
432 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
433 my @lines = split(/\n/, $&);
434 my $function_line = $. - scalar(@lines) + 1;
438 my @arguments16 = ("HWAVEOUT16");
439 my @arguments32 = ("HWAVEOUT");
440 &$function_begin($documentation_line, $documentation,
441 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
443 &$function_begin($documentation_line, $documentation,
444 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
446 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
447 my @lines = split(/\n/, $&);
448 my $function_line = $. - scalar(@lines) + 1;
453 my @arguments16 = ("HWAVEOUT16", $4);
454 my @arguments32 = ("HWAVEOUT", $4);
455 &$function_begin($documentation_line, $documentation,
456 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
458 &$function_begin($documentation_line, $documentation,
459 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
462 my @arguments16 = ("UINT16", $4);
463 my @arguments32 = ("UINT", $4);
464 &$function_begin($documentation_line, $documentation,
465 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
467 &$function_begin($documentation_line, $documentation,
468 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
471 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
473 $regs_entrypoints{$2} = $1;
474 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
476 unshift @$debug_channels, $1;
477 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
479 push @$debug_channels, $1;
480 } elsif(/typedef\s+(enum|struct|union)(?:\s+(\w+))?\s*\{/s) {
489 ((?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
492 ((?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?
493 (?:\s*,\s*(?:\s*\*+\s*|\s+)\w+\s*(?:\[[^\]]*\])?)*)
501 my @parts = split(/\s*,\s*/, $2);
502 foreach my $part (@parts) {
503 if($part =~ /(?:\s*(\*+)\s*|\s+)(\w+)\s*(\[[^\]]*\])?/) {
515 &$type_end([@names]);
517 (?:(?:const\s+|enum\s+|long\s+|signed\s+|short\s+|struct\s+|union\s+|unsigned\s+)*?)
520 \((?:(\w+)\s+)?\s*\*\s*(\w+)\s*\)\s*
521 (?:\(([^\)]*)\)|\[([^\]]*)\])\s*;/sx)
525 if(defined($2) || defined($3)) {
528 $type = "$1 ($cc *)($5)";
530 $type = "$1 ($cc *)[$6]";
534 $type = "$1 (*)($5)";
536 $type = "$1 (*)[$6]";
542 } elsif(/typedef[^\{;]*;/s) {
544 $output->write("$file: $.: can't parse: '$&'\n");
545 } elsif(/typedef[^\{]*\{[^\}]*\}[^;];/s) {
547 $output->write("$file: $.: can't parse: '$&'\n");
548 } elsif(/\'[^\']*\'/s) {
550 } elsif(/\"[^\"]*\"/s) {
554 } elsif(/extern\s+"C"\s+{/s) {
558 print "+1: $_\n" if $options->debug >= 2;
565 print STDERR "done\n" if $options->verbose;
566 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;