Merged msacm and msacm32 dlls.
[wine] / tools / winapi_check / winapi.pm
1 package winapi;
2
3 use strict;
4
5 sub new {
6     my $proto = shift;
7     my $class = ref($proto) || $proto;
8     my $self  = {};
9     bless ($self, $class);
10
11     my $options = \${$self->{OPTIONS}};
12     my $output = \${$self->{OUTPUT}};
13     my $name = \${$self->{NAME}};
14
15     $$options = shift;
16     $$output = shift;
17     $$name = shift;
18     my $path = shift;
19
20     my @files = map {
21         s/^.\/(.*)$/$1/;
22         $_; 
23     } split(/\n/, `find $path -name \\*.api`);
24   
25     foreach my $file (@files) {
26         my $module = $file;
27         $module =~ s/.*?\/([^\/]*?)\.api$/$1/;
28         $self->parse_api_file($file,$module);
29     }   
30
31     return $self;
32 }
33
34 sub parse_api_file {
35     my $self = shift;
36
37     my $options = \${$self->{OPTIONS}};
38     my $output = \${$self->{OUTPUT}};
39     my $allowed_kind = \%{$self->{ALLOWED_KIND}};
40     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
41     my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
42     my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
43     my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
44     my $type_format = \%{$self->{TYPE_FORMAT}};
45
46     my $file = shift;
47     my $module = shift;
48
49     my $kind;
50     my $format;
51     my $extension = 0;
52     my $forbidden = 0;
53
54     if($$options->progress) {
55         $$output->progress("$file");
56     }
57
58     open(IN, "< $file") || die "$file: $!\n";
59     $/ = "\n";
60     while(<IN>) {
61         s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begin and end of line
62         s/^(.*?)\s*#.*$/$1/;  # remove comments
63         /^$/ && next;         # skip empty lines
64
65         if(s/^%(\S+)\s*//) {
66             $kind = $1;
67             $format = undef;
68             $forbidden = 0;
69             $extension = 0;
70
71             $$allowed_kind{$kind} = 1;
72             if(/^--forbidden/) {
73                 $forbidden = 1;
74             } elsif(/^--extension/) {
75                 $extension = 1;
76             } elsif(/^--format=(\".*?\"|\S*)/) {
77                 $format = $1;
78                 $format =~ s/^\"(.*?)\"$/$1/;
79             }
80
81             if(!defined($format)) {
82                 if($kind eq "long") {
83                     $format  = "%d|%u|%x|%X|";
84                     $format .= "%hd|%hu|%hx|%hX|";
85                     $format .= "%ld|%lu|%lx|%lX|";
86                     $format .= "%04x|%04X|0x%04x|0x%04X|";
87                     $format .= "%08x|%08X|0x%08x|0x%08X|";
88                     $format .= "%08lx|%08lX|0x%08lx|0x%08lX";
89                 } elsif($kind eq "longlong") {
90                     $format = "%lld";
91                 } elsif($kind eq "ptr") {
92                     $format = "%p";
93                 } elsif($kind eq "segptr") {
94                     $format = "%p";
95                 } elsif($kind eq "str") {
96                     $format = "%p|%s";
97                 } elsif($kind eq "wstr") {
98                     $format = "%p|%s";
99                 } elsif($kind eq "word") {
100                     $format  = "%d|%u|%x|%X|";
101                     $format .= "%hd|%hu|%hx|%hX|";
102                     $format .= "%04x|%04X|0x%04x|0x%04X";
103                 } else {
104                     $format = "<unknown>";
105                 }
106             }
107         } elsif(defined($kind)) {
108             my $type = $_;
109             if(!$forbidden) {
110                 if(defined($module)) {
111                     if($$allowed_modules_unlimited{$type}) {
112                         $$output->write("$file: type ($type) already specificed as an unlimited type\n");
113                     } elsif(!$$allowed_modules{$type}{$module}) {
114                         $$allowed_modules{$type}{$module} = 1;
115                         $$allowed_modules_limited{$type} = 1;
116                     } else {
117                         $$output->write("$file: type ($type) already specificed\n");
118                     }
119                 } else {
120                     $$allowed_modules_unlimited{$type} = 1;
121                 }
122             } else {
123                 $$allowed_modules_limited{$type} = 1;
124             }
125             if(defined($$translate_argument{$type}) && $$translate_argument{$type} ne $kind) {
126                 $$output->write("$file: type ($type) respecified as different kind ($kind != $$translate_argument{$type})\n");
127             } else {
128                 $$translate_argument{$type} = $kind;
129             }
130                 
131             $$type_format{$module}{$type} = $format;
132         } else {
133             $$output->write("$file: file must begin with %<type> statement\n");
134             exit 1;
135         }
136     }
137     close(IN);
138 }
139
140 sub get_spec_file_type {
141     my $proto = shift;
142     my $class = ref($proto) || $proto;
143
144     my $file = shift;
145
146     my $module;
147     my $type;
148
149     open(IN, "< $file") || die "$file: $!\n";
150     local $/ = "\n";
151     while(<IN>) {
152         s/^\s*(.*?)\s*$/$1/;
153         s/^(.*?)\s*#.*$/$1/;
154         /^$/ && next;
155
156         if(/^name\s*(\S*)/) { $module = $1; }
157         if(/^type\s*(\w+)/) { $type = $1; }
158
159         if(defined($module) && defined($type)) { last; }
160     }
161     close(IN);
162
163     return ($type, $module);
164 }
165
166 sub read_spec_files {
167     my $proto = shift;
168     my $class = ref($proto) || $proto;
169
170     my $modules = shift;
171     my $wine_dir = shift;
172     my $current_dir = shift;
173     my $files = shift;
174     my $win16api = shift;
175     my $win32api = shift;
176
177     foreach my $file (@$files) {
178         (my $type, my $module) = 'winapi'->get_spec_file_type("$wine_dir/$file");
179         $modules->spec_file_module($file, $module);
180         if($type eq "win16") {
181             $win16api->parse_spec_file("$wine_dir/$file");
182         } elsif($type eq "win32") {
183             $win32api->parse_spec_file("$wine_dir/$file");
184         }
185     }
186
187     foreach my $self ($win16api, $win32api) {
188         my $function_forward = \%{$self->{FUNCTION_FORWARD}};
189         my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
190         my $function_module = \%{$self->{FUNCTION_MODULE}};
191         
192         foreach my $forward_name (sort(keys(%$function_forward))) {
193             $$function_forward{$forward_name} =~ /^(\S*):(\S*)\.(\S*)$/;
194             (my $from_module, my $to_module, my $external_name) = ($1, $2, $3);
195             my $internal_name = $$function_internal_name{$external_name};
196             if(defined($internal_name)) {
197                 $$function_module{$internal_name} .= " & $from_module";
198             }
199         }
200     }
201 }
202
203 sub read_all_spec_files {
204     my $proto = shift;
205     my $class = ref($proto) || $proto;
206
207     my $modules = shift;    
208     my $wine_dir = shift;
209     my $current_dir = shift;
210     my $file_type = shift;
211     my $win16api = shift;
212     my $win32api = shift;
213
214     my @files = map {
215         s/^$wine_dir\/(.*)$/$1/;
216         if(&$file_type($_) eq "library") {
217             $_;
218         } else {
219             ();
220         }
221     } split(/\n/, `find $wine_dir -name \\*.spec`);
222     
223     'winapi'->read_spec_files($modules, $wine_dir, $current_dir, \@files, $win16api, $win32api); 
224 }
225
226 sub parse_spec_file {
227     my $self = shift;
228
229     my $options = \${$self->{OPTIONS}};
230     my $output = \${$self->{OUTPUT}};
231     my $function_arguments = \%{$self->{FUNCTION_ARGUMENTS}};
232     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
233     my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
234     my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
235     my $function_stub = \%{$self->{FUNCTION_STUB}};
236     my $function_forward = \%{$self->{FUNCTION_FORWARD}};
237     my $function_module = \%{$self->{FUNCTION_MODULE}};
238     my $modules = \%{$self->{MODULES}};
239     my $module_files = \%{$self->{MODULE_FILES}};
240
241     my $file = shift;
242
243     my %ordinals;
244     my $type;
245     my $module;
246     my $module_file;
247
248     if($$options->progress) {
249         $$output->progress("$file");
250     }
251
252     open(IN, "< $file") || die "$file: $!\n";
253     $/ = "\n";
254     my $header = 1;
255     my $lookahead = 0;
256     while($lookahead || defined($_ = <IN>)) {
257         $lookahead = 0;
258         s/^\s*(.*?)\s*$/$1/;
259         s/^(.*?)\s*#.*$/$1/;
260         /^$/ && next;
261
262         if($header)  {
263             if(/^name\s*(\S*)/) { $module = $1; }
264             if(/^file\s*(\S*)/) { $module_file = $1; }
265             if(/^type\s*(\w+)/) { $type = $1; }
266             if(/^\d+|@/) { $header = 0; $lookahead = 1; }
267             next;
268         } 
269
270         my $ordinal;
271         if(/^(\d+|@)\s+(pascal|pascal16|stdcall|cdecl|register|interrupt|varargs)\s+(\S+)\s*\(\s*(.*?)\s*\)\s*(\S+)$/) {
272             my $calling_convention = $2;
273             my $external_name = $3;
274             my $arguments = $4;
275             my $internal_name = $5;
276            
277             $ordinal = $1;
278
279             # FIXME: Internal name existing more than once not handled properly
280             $$function_internal_name{$external_name} = $internal_name;
281             $$function_external_name{$internal_name} = $external_name;
282             $$function_arguments{$internal_name} = $arguments;
283             $$function_calling_convention{$internal_name} = $calling_convention;
284             if(!$$function_module{$internal_name}) {
285                 $$function_module{$internal_name} = "$module";
286             } elsif($$function_module{$internal_name} !~ /$module/) {
287                 if(0) {
288                     $$output->write("$file: $external_name: the internal function ($internal_name) " . 
289                                     "already belongs to a module ($$function_module{$internal_name})\n");
290                 }
291                 $$function_module{$internal_name} .= " & $module";
292
293             }
294
295             if(0 && $$options->spec_mismatch) {
296                 if($external_name eq "@") {
297                     if($internal_name !~ /^\U$module\E_$ordinal$/) {
298                         $$output->write("$file: $external_name: the internal name ($internal_name) mismatch\n");
299                     }
300                 } else {
301                     my $name = $external_name;
302
303                     my $name1 = $name;
304                     $name1 =~ s/^Zw/Nt/;
305
306                     my $name2 = $name;
307                     $name2 =~ s/^(?:_|Rtl|k32|K32)//;
308
309                     my $name3 = $name;
310                     $name3 =~ s/^INT_Int[0-9a-f]{2}Handler$/BUILTIN_DefaultIntHandler/;
311
312                     my $name4 = $name;
313                     $name4 =~ s/^(VxDCall)\d$/$1/;
314
315                     # FIXME: This special case is becuase of a very ugly kludge that should be fixed IMHO
316                     my $name5 = $name;
317                     $name5 =~ s/^(.*?16)_(.*?)$/$1_fn$2/;
318
319                     if(uc($internal_name) ne uc($external_name) &&
320                        $internal_name !~ /(\Q$name\E|\Q$name1\E|\Q$name2\E|\Q$name3\E|\Q$name4\E|\Q$name5\E)/)
321                     {
322                         $$output->write("$file: $external_name: internal name ($internal_name) mismatch\n");
323                     }
324                 }
325             }
326         } elsif(/^(\d+|@)\s+stub\s+(\S+)$/) {
327             my $external_name = $2;
328
329             $ordinal = $1;
330
331             my $internal_name;
332             if($type eq "win16") {
333                 $internal_name = $external_name . "16";
334             } else {
335                 $internal_name = $external_name;
336             }
337
338             # FIXME: Internal name existing more than once not handled properly
339             $$function_stub{$internal_name} = 1;
340             if(!$$function_module{$internal_name}) {
341                 $$function_module{$internal_name} = "$module";
342             } elsif($$function_module{$internal_name} !~ /$module/) {
343                 $$function_module{$internal_name} .= " & $module";
344             }
345         } elsif(/^(\d+|@)\s+forward\s+(\S+)\s+(\S+)\.(\S+)$/) {
346             $ordinal = $1;
347
348             my $external_name = $2;
349             my $forward_module = lc($3);
350             my $forward_name = $4;
351
352             $$function_forward{$external_name} = "$module:$forward_module.$forward_name";
353         } elsif(/^(\d+|@)\s+(equate|long|word|extern|forward)/) {
354             # ignore
355         } else {
356             my $next_line = <IN>;
357             if(!defined($next_line) || $next_line =~ /^\s*\d|@/) {
358                 die "$file: $.: syntax error: '$_'\n";
359             } else {
360                 $_ .= $next_line;
361                 $lookahead = 1;
362             }
363         }
364         
365         if(defined($ordinal)) {
366             if($ordinal ne "@" && $ordinals{$ordinal}) {
367                 $$output->write("$file: ordinal redefined: $_\n");
368             }
369             $ordinals{$ordinal}++;
370         }
371     }
372     close(IN);
373
374     $$modules{$module}++;
375     if(defined($module_file)) {
376         $$module_files{$module} = $module_file;
377     } else {
378         $$module_files{$module} = "$module.drv";
379     }
380 }
381
382 sub name {
383     my $self = shift;
384     my $name = \${$self->{NAME}};
385
386     return $$name;
387 }
388
389 sub is_allowed_kind {
390     my $self = shift;
391     my $allowed_kind = \%{$self->{ALLOWED_KIND}};
392
393     my $kind = shift;
394     if(defined($kind)) {
395         return $$allowed_kind{$kind};
396     } else {
397         return 0;
398     }
399 }
400
401 sub is_limited_type {
402     my $self = shift;
403     my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
404
405     my $type = shift;
406
407     return $$allowed_modules_limited{$type};
408 }
409
410 sub allowed_type_in_module {
411     my $self = shift;
412     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
413     my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
414
415     my $type = shift;
416     my @modules = split(/ \& /, shift);
417
418     if(!$$allowed_modules_limited{$type}) { return 1; }
419
420     foreach my $module (@modules) {
421         if($$allowed_modules{$type}{$module}) { return 1; }
422     }
423
424     return 0;
425 }
426
427 sub type_used_in_module {
428     my $self = shift;
429     my $used_modules = \%{$self->{USED_MODULES}};
430
431     my $type = shift;
432     my @modules = split(/ \& /, shift);
433
434     foreach my $module (@modules) {
435         $$used_modules{$type}{$module} = 1;
436     }
437
438     return ();
439 }
440
441 sub types_not_used {
442     my $self = shift;
443     my $used_modules = \%{$self->{USED_MODULES}};
444     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
445
446     my $not_used;
447     foreach my $type (sort(keys(%$allowed_modules))) {
448         foreach my $module (sort(keys(%{$$allowed_modules{$type}}))) {
449             if(!$$used_modules{$type}{$module}) {
450                 $$not_used{$module}{$type} = 1;
451             }
452         }
453     }
454     return $not_used;
455 }
456
457 sub types_unlimited_used_in_modules {
458     my $self = shift;
459
460     my $output = \${$self->{OUTPUT}};
461     my $used_modules = \%{$self->{USED_MODULES}};
462     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
463     my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
464
465     my $used_types;
466     foreach my $type (sort(keys(%$allowed_modules_unlimited))) {
467         my $count = 0;
468         my @modules = ();
469         foreach my $module (sort(keys(%{$$used_modules{$type}}))) {
470             $count++;
471             push @modules, $module;
472         }
473         if($count) {
474             foreach my $module (@modules) {
475               $$used_types{$type}{$module} = 1;
476             }
477         }
478     }
479     return $used_types;
480 }
481
482 sub translate_argument {
483     my $self = shift;
484     my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
485
486     my $argument = shift;
487
488     return $$translate_argument{$argument};
489 }
490
491 sub all_declared_types {
492     my $self = shift;
493     my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
494
495     return sort(keys(%$translate_argument));
496 }
497
498 sub found_type {
499     my $self = shift;
500     my $type_found = \%{$self->{TYPE_FOUND}};
501
502     my $name = shift;
503
504     $$type_found{$name}++;
505 }
506
507 sub type_found {
508     my $self = shift;
509     my $type_found= \%{$self->{TYPE_FOUND}};
510
511     my $name = shift;
512
513     return $$type_found{$name};
514 }
515
516 sub is_allowed_type_format {
517     my $self = shift;
518     my $type_format = \%{$self->{TYPE_FORMAT}};
519
520     my $module = shift;
521     my $type = shift;
522     my $format = shift;
523
524     my $formats;
525
526     if(defined($module) && defined($type)) {
527         local $_;
528         foreach (split(/ & /, $module)) {
529             if(defined($formats)) { 
530                 $formats .= "|"; 
531             } else {
532                 $formats = "";
533             }       
534             if(defined($$type_format{$_}{$type})) {
535                 $formats .= $$type_format{$_}{$type};
536             }
537         }
538     }
539
540     if(defined($formats)) {
541         local $_;
542         foreach (split(/\|/, $formats)) {
543             if($_ eq $format) {
544                 return 1;
545             }
546         }
547     }
548
549     return 0;
550 }
551
552 sub all_modules {
553     my $self = shift;
554     my $modules = \%{$self->{MODULES}};
555
556     return sort(keys(%$modules));
557 }
558
559 sub is_module {
560     my $self = shift;
561     my $modules = \%{$self->{MODULES}};
562
563     my $name = shift;
564
565     return $$modules{$name};
566 }
567
568 sub module_file {
569     my $self = shift;
570
571     my $module = shift;
572
573     my $module_files = \%{$self->{MODULE_FILES}};
574
575     return $$module_files{$module};
576 }
577
578 sub all_functions {
579     my $self = shift;
580     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
581
582     return sort(keys(%$function_calling_convention));
583 }
584
585 sub all_functions_in_module {
586     my $self = shift;
587     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
588     my $function_module = \%{$self->{FUNCTION_MODULE}};
589
590     my $module = shift;
591
592     my @names;
593     foreach my $name (keys(%$function_calling_convention)) {
594         if($$function_module{$name} eq $module) {
595             push @names, $name;
596         }
597     }
598
599     return sort(@names);
600 }
601
602 sub all_functions_stub {
603     my $self = shift;
604     my $function_stub = \%{$self->{FUNCTION_STUB}};
605
606     return sort(keys(%$function_stub));
607 }
608
609 sub all_functions_found {
610     my $self = shift;
611     my $function_found = \%{$self->{FUNCTION_FOUND}};
612
613     return sort(keys(%$function_found));
614 }
615
616 sub function_calling_convention {
617     my $self = shift;
618     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
619
620     my $name = shift;
621
622     return $$function_calling_convention{$name};
623 }
624
625 sub function_external_name {
626     my $self = shift;
627     my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
628
629     my $name = shift;
630
631     return $$function_external_name{$name};
632 }
633
634 sub is_function {
635     my $self = shift;
636     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
637
638     my $name = shift;
639
640     return $$function_calling_convention{$name};
641 }
642
643 sub is_shared_function {
644     my $self = shift;
645     my $function_shared = \%{$self->{FUNCTION_SHARED}};
646
647     my $name = shift;
648
649     return $$function_shared{$name};
650 }
651
652 sub found_shared_function {
653     my $self = shift;
654     my $function_shared = \%{$self->{FUNCTION_SHARED}};
655
656     my $name = shift;
657
658     $$function_shared{$name} = 1;
659 }
660
661 sub function_arguments {
662     my $self = shift;
663     my $function_arguments = \%{$self->{FUNCTION_ARGUMENTS}};
664
665     my $name = shift;
666
667     return $$function_arguments{$name};
668 }
669
670 sub function_module {
671     my $self = shift;
672     my $function_module = \%{$self->{FUNCTION_MODULE}};
673
674     my $name = shift;
675
676     return $$function_module{$name};
677 }
678
679 sub function_stub {
680     my $self = shift;
681     my $function_stub = \%{$self->{FUNCTION_STUB}};
682
683     my $name = shift;
684
685     return $$function_stub{$name};
686 }
687
688 sub found_function {
689     my $self = shift;
690     my $function_found = \%{$self->{FUNCTION_FOUND}};
691
692     my $name = shift;
693
694     $$function_found{$name}++;
695 }
696
697 sub function_found {
698     my $self = shift;
699     my $function_found = \%{$self->{FUNCTION_FOUND}};
700
701     my $name = shift;
702
703     return $$function_found{$name};
704 }
705
706 1;