5 use output qw($output);
11 my $function_found_callback = shift;
12 my $preprocessor_found_callback = shift;
15 my $debug_channels = [];
18 my $documentation_line;
23 my $calling_convention;
24 my $internal_name = "";
27 my $argument_documentations;
30 my $function_begin = sub {
31 $documentation_line = shift;
32 $documentation = shift;
33 $function_line = shift;
36 $calling_convention = shift;
37 $internal_name = shift;
38 $argument_types = shift;
39 $argument_names = shift;
40 $argument_documentations = shift;
42 if(defined($argument_names) && defined($argument_types) &&
43 $#$argument_names == -1)
45 foreach my $n (0..$#$argument_types) {
46 push @$argument_names, "";
50 if(defined($argument_documentations) &&
51 $#$argument_documentations == -1)
53 foreach my $n (0..$#$argument_documentations) {
54 push @$argument_documentations, "";
60 my $function_end = sub {
61 my $function = 'winapi_function'->new;
63 if(!defined($documentation_line)) {
64 $documentation_line = 0;
67 $function->file($file);
68 $function->debug_channels([@$debug_channels]);
69 $function->documentation_line($documentation_line);
70 $function->documentation($documentation);
71 $function->function_line($function_line);
72 $function->linkage($linkage);
73 $function->return_type($return_type);
74 $function->calling_convention($calling_convention);
75 $function->internal_name($internal_name);
76 if(defined($argument_types)) {
77 $function->argument_types([@$argument_types]);
79 if(defined($argument_names)) {
80 $function->argument_names([@$argument_names]);
82 if(defined($argument_documentations)) {
83 $function->argument_documentations([@$argument_documentations]);
85 $function->statements($statements);
87 &$function_found_callback($function);
91 my @comment_lines = ();
97 my $lookahead_count = 0;
99 print STDERR "Processing file '$file' ... " if $options->verbose;
100 open(IN, "< $file") || die "<internal>: $file: $!\n";
102 while($again || defined(my $line = <IN>)) {
112 $lookahead_count = 0;
114 print " $level($lookahead_count): $line\n" if $options->debug >= 2;
115 print "*** $_\n" if $options->debug >= 3;
117 $lookahead_count = 0;
121 # CVS merge conflicts in file?
122 if(/^(<<<<<<<|=======|>>>>>>>)/) {
123 $output->write("$file: merge conflicts in file\n");
128 if(s/^(.*?)(\/\*.*?\*\/)(.*)$/$1 $3/s) {
129 push @comment_lines, $.;
139 # remove C++ comments
140 while(s/^(.*?)\/\/.*?$/$1\n/s) { $again = 1 }
144 if(/^\s*$/) { next; }
146 # remove preprocessor directives
151 } elsif(s/^\#\s*(.*?)(\s+(.*?))?\s*$//m) {
153 &$preprocessor_found_callback($1, $3);
155 &$preprocessor_found_callback($1, "");
162 if(s/^\s*extern\s+"C"\s+\{//m) {
168 my $documentation_line;
170 my @argument_documentations = ();
173 while($n >= 0 && ($comments[$n] !~ /^\/\*\*/ ||
174 $comments[$n] =~ /^\/\*\*+\/$/))
179 if(defined($comments[$n]) && $n >= 0) {
180 my @lines = split(/\n/, $comments[$n]);
182 $documentation_line = $comment_lines[$n] - scalar(@lines) + 1;
183 $documentation = $comments[$n];
185 for(my $m=$n+1; $m <= $#comments; $m++) {
186 if($comments[$m] =~ /^\/\*\*+\/$/ ||
187 $comments[$m] =~ /^\/\*\s*(?:\!)?defined/) # FIXME: Kludge
189 @argument_documentations = ();
192 push @argument_documentations, $comments[$m];
203 s/^([^\{\}\'\"]*)//s;
207 while(/^./ && !s/^\'//) {
224 while(/^./ && !s/^\"//) {
245 print "+1: \{$_\n" if $options->debug >= 2;
249 $line .= "}" if $level > 1;
250 print "-1: \}$_\n" if $options->debug >= 2;
252 if($level == -1 && $extern_c) {
258 if(!defined($statements)) {
262 if($line !~ /^\s*$/) {
263 $statements .= "$line\n";
266 if($internal_name && $level == 0) {
270 } elsif(/(extern\s+|static\s+)?((struct\s+|union\s+|enum\s+|signed\s+|unsigned\s+)?\w+((\s*\*)+\s*|\s+))
271 ((__cdecl|__stdcall|CDECL|VFWAPIV|VFWAPI|WINAPIV|WINAPI|CALLBACK)\s+)?
272 (\w+(\(\w+\))?)\s*\(([^\)]*)\)\s*(\{|\;)/sx)
274 my @lines = split(/\n/, $&);
275 my $function_line = $. - scalar(@lines) + 1;
284 my $return_type = $2;
285 my $calling_convention = $7;
289 if(!defined($linkage)) {
293 if(!defined($calling_convention)) {
294 $calling_convention = "";
297 $linkage =~ s/\s*$//;
299 $return_type =~ s/\s*$//;
300 $return_type =~ s/\s*\*\s*/*/g;
301 $return_type =~ s/(\*+)/ $1/g;
303 if($regs_entrypoints{$name}) {
304 $name = $regs_entrypoints{$name};
307 $arguments =~ y/\t\n/ /;
308 $arguments =~ s/^\s*(.*?)\s*$/$1/;
309 if($arguments eq "") { $arguments = "..." }
313 my @arguments = split(/,/, $arguments);
314 foreach my $n (0..$#arguments) {
315 my $argument_type = "";
316 my $argument_name = "";
317 my $argument = $arguments[$n];
318 $argument =~ s/^\s*(.*?)\s*$/$1/;
319 # print " " . ($n + 1) . ": '$argument'\n";
320 $argument =~ s/^(IN OUT(?=\s)|IN(?=\s)|OUT(?=\s)|\s*)\s*//;
321 $argument =~ s/^(const(?=\s)|CONST(?=\s)|\s*)\s*//;
322 if($argument =~ /^\.\.\.$/) {
323 $argument_type = "...";
324 $argument_name = "...";
325 } elsif($argument =~ /^
326 ((?:struct\s+|union\s+|enum\s+|(?:signed\s+|unsigned\s+)
327 (?:short\s+(?=int)|long\s+(?=int))?)?\w+)\s*
328 ((?:const)?\s*(?:\*\s*?)*)\s*
329 (?:WINE_UNUSED\s+)?(\w*)\s*(?:\[\]|\s+OPTIONAL)?/x)
331 $argument_type = "$1";
333 $argument_type .= " $2";
337 $argument_type =~ s/\s*const\s*/ /;
338 $argument_type =~ s/^\s*(.*?)\s*$/$1/;
340 $argument_name =~ s/^\s*(.*?)\s*$/$1/;
342 die "$file: $.: syntax error: '$argument'\n";
344 $argument_types[$n] = $argument_type;
345 $argument_names[$n] = $argument_name;
346 # print " " . ($n + 1) . ": '" . $argument_types[$n] . "', '" . $argument_names[$n] . "'\n";
348 if($#argument_types == 0 && $argument_types[0] =~ /^void$/i) {
349 $#argument_types = -1;
350 $#argument_names = -1;
353 if($options->debug) {
354 print "$file: $return_type $calling_convention $name(" . join(",", @arguments) . ")\n";
357 &$function_begin($documentation_line, $documentation,
358 $function_line, $linkage, $return_type, $calling_convention, $name,
359 \@argument_types,\@argument_names,\@argument_documentations);
363 } elsif(/__ASM_GLOBAL_FUNC\(\s*(.*?)\s*,/s) {
364 my @lines = split(/\n/, $&);
365 my $function_line = $. - scalar(@lines) + 1;
369 &$function_begin($documentation_line, $documentation,
370 $function_line, "", "void", "__asm", $1);
373 } elsif(/DC_(GET_X_Y_16|GET_VAL_16)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s){
374 my @lines = split(/\n/, $&);
375 my $function_line = $. - scalar(@lines) + 1;
379 my $return16 = $2 . "16";
380 my $name16 = $3 . "16";
382 $return16 =~ s/^(COLORREF|DWORD)16$/$1/;
384 my @arguments = ("HDC16");
385 &$function_begin($documentation_line, $documentation,
386 $function_line, "", $return16, "WINAPI", $name16, \@arguments);
389 } elsif(/DC_(GET_VAL_32)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,.*?\)/s) {
390 my @lines = split(/\n/, $&);
391 my $function_line = $. - scalar(@lines) + 1;
397 my @arguments32 = ("HDC");
400 &$function_begin($documentation_line, $documentation,
401 $function_line, "", $return32, "WINAPI", $name32, \@arguments32);
404 } elsif(/DC_(GET_VAL_EX_16)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
405 my @lines = split(/\n/, $&);
406 my $function_line = $. - scalar(@lines) + 1;
410 my @arguments16 = ("HDC16", "LP" . $5 . "16");
411 &$function_begin($documentation_line, $documentation,
412 $function_line, "", "BOOL16", "WINAPI", $2 . "16", \@arguments16);
415 } elsif(/DC_(GET_VAL_EX_32)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
416 my @lines = split(/\n/, $&);
417 my $function_line = $. - scalar(@lines) + 1;
421 my @arguments32 = ("HDC", "LP" . $5);
422 &$function_begin($documentation_line, $documentation,
423 $function_line, "", "BOOL", "WINAPI", $2, \@arguments32);
426 } elsif(/DC_(SET_MODE_16)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
427 my @lines = split(/\n/, $&);
428 my $function_line = $. - scalar(@lines) + 1;
432 my @arguments16 = ("HDC16", "INT16");
433 &$function_begin($documentation_line, $documentation,
434 $function_line, "", "INT16", "WINAPI", $2 . "16", \@arguments16);
437 } elsif(/DC_(SET_MODE_32)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
438 my @lines = split(/\n/, $&);
439 my $function_line = $. - scalar(@lines) + 1;
443 my @arguments32 = ("HDC", "INT");
444 &$function_begin($documentation_line, $documentation,
445 $function_line, "", "INT", "WINAPI", $2, \@arguments32);
448 } elsif(/WAVEIN_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
450 my @arguments16 = ("HWAVEIN16");
451 my @arguments32 = ("HWAVEIN");
452 &$function_begin($documentation_line, $documentation,
453 $function_line, "", "UINT16", "WINAPI", "waveIn" . $1 . "16", \@arguments16);
456 &$function_begin($documentation_line, $documentation,
457 $function_line, "", "UINT", "WINAPI", "waveIn" . $1, \@arguments32);
460 } elsif(/WAVEOUT_SHORTCUT_0\s*\(\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
461 my @lines = split(/\n/, $&);
462 my $function_line = $. - scalar(@lines) + 1;
466 my @arguments16 = ("HWAVEOUT16");
467 my @arguments32 = ("HWAVEOUT");
468 &$function_begin($documentation_line, $documentation,
469 $function_line, "", "UINT16", "WINAPI", "waveOut" . $1 . "16", \@arguments16);
472 &$function_begin($documentation_line, $documentation,
473 $function_line, "", "UINT", "WINAPI", "waveOut" . $1, \@arguments32);
475 } elsif(/WAVEOUT_SHORTCUT_(1|2)\s*\(\s*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)\s*\)/s) {
476 my @lines = split(/\n/, $&);
477 my $function_line = $. - scalar(@lines) + 1;
482 my @arguments16 = ("HWAVEOUT16", $4);
483 my @arguments32 = ("HWAVEOUT", $4);
484 &$function_begin($documentation_line, $documentation,
485 $function_line, "", "UINT16", "WINAPI", "waveOut" . $2 . "16", \@arguments16);
488 &$function_begin($documentation_line, $documentation,
489 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
492 my @arguments16 = ("UINT16", $4);
493 my @arguments32 = ("UINT", $4);
494 &$function_begin($documentation_line, $documentation,
495 $function_line, "", "UINT16", "WINAPI", "waveOut". $2 . "16", \@arguments16);
498 &$function_begin($documentation_line, $documentation,
499 $function_line, "", "UINT", "WINAPI", "waveOut" . $2, \@arguments32);
502 } elsif(/DEFINE_REGS_ENTRYPOINT_\d+\(\s*(\S*)\s*,\s*([^\s,\)]*).*?\)/s) {
504 $regs_entrypoints{$2} = $1;
505 } elsif(/DEFAULT_DEBUG_CHANNEL\s*\((\S+)\)/s) {
507 unshift @$debug_channels, $1;
508 } elsif(/(DEFAULT|DECLARE)_DEBUG_CHANNEL\s*\((\S+)\)/s) {
510 push @$debug_channels, $1;
511 } elsif(/\'[^\']*\'/s) {
513 } elsif(/\"[^\"]*\"/s) {
517 } elsif(/extern\s+"C"\s+{/s) {
521 print "+1: $_\n" if $options->debug >= 2;
528 print STDERR "done\n" if $options->verbose;
529 $output->write("$file: not at toplevel at end of file\n") unless $level == 0;