Added an unknown VxD error code.
[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_ordinal = \%{$self->{FUNCTION_ORDINAL}};
233     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
234     my $function_internal_name = \%{$self->{FUNCTION_INTERNAL_NAME}};
235     my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
236     my $function_stub = \%{$self->{FUNCTION_STUB}};
237     my $function_forward = \%{$self->{FUNCTION_FORWARD}};
238     my $function_module = \%{$self->{FUNCTION_MODULE}};
239     my $modules = \%{$self->{MODULES}};
240     my $module_files = \%{$self->{MODULE_FILES}};
241
242     my $file = shift;
243
244     my %ordinals;
245     my $type;
246     my $module;
247     my $module_file;
248
249     if($$options->progress) {
250         $$output->progress("$file");
251     }
252
253     open(IN, "< $file") || die "$file: $!\n";
254     $/ = "\n";
255     my $header = 1;
256     my $lookahead = 0;
257     while($lookahead || defined($_ = <IN>)) {
258         $lookahead = 0;
259         s/^\s*(.*?)\s*$/$1/;
260         s/^(.*?)\s*#.*$/$1/;
261         /^$/ && next;
262
263         if($header)  {
264             if(/^name\s*(\S*)/) { $module = $1; }
265             if(/^file\s*(\S*)/) { $module_file = $1; }
266             if(/^type\s*(\w+)/) { $type = $1; }
267             if(/^\d+|@/) { $header = 0; $lookahead = 1; }
268             next;
269         } 
270
271         my $ordinal;
272         if(/^(\d+|@)\s+
273            (pascal|pascal16|stdcall|cdecl|register|interrupt|varargs)
274            (?:\s+(?:-noimport|-norelay|-i386|-ret64))*\s+(\S+)\s*\(\s*(.*?)\s*\)\s*(\S+)$/x)
275         {
276             my $calling_convention = $2;
277             my $external_name = $3;
278             my $arguments = $4;
279             my $internal_name = $5;
280            
281             $ordinal = $1;
282
283             # FIXME: Internal name existing more than once not handled properly
284             $$function_internal_name{$external_name} = $internal_name;
285             $$function_external_name{$internal_name} = $external_name;
286             $$function_arguments{$internal_name} = $arguments;
287             $$function_ordinal{$internal_name} = $ordinal;
288             $$function_calling_convention{$internal_name} = $calling_convention;
289             if(!$$function_module{$internal_name}) {
290                 $$function_module{$internal_name} = "$module";
291             } elsif($$function_module{$internal_name} !~ /$module/) {
292                 if(0) {
293                     $$output->write("$file: $external_name: the internal function ($internal_name) " . 
294                                     "already belongs to a module ($$function_module{$internal_name})\n");
295                 }
296                 $$function_module{$internal_name} .= " & $module";
297
298             }
299
300             if(0 && $$options->spec_mismatch) {
301                 if($external_name eq "@") {
302                     if($internal_name !~ /^\U$module\E_$ordinal$/) {
303                         $$output->write("$file: $external_name: the internal name ($internal_name) mismatch\n");
304                     }
305                 } else {
306                     my $name = $external_name;
307
308                     my $name1 = $name;
309                     $name1 =~ s/^Zw/Nt/;
310
311                     my $name2 = $name;
312                     $name2 =~ s/^(?:_|Rtl|k32|K32)//;
313
314                     my $name3 = $name;
315                     $name3 =~ s/^INT_Int[0-9a-f]{2}Handler$/BUILTIN_DefaultIntHandler/;
316
317                     my $name4 = $name;
318                     $name4 =~ s/^(VxDCall)\d$/$1/;
319
320                     # FIXME: This special case is becuase of a very ugly kludge that should be fixed IMHO
321                     my $name5 = $name;
322                     $name5 =~ s/^(.*?16)_(.*?)$/$1_fn$2/;
323
324                     if(uc($internal_name) ne uc($external_name) &&
325                        $internal_name !~ /(\Q$name\E|\Q$name1\E|\Q$name2\E|\Q$name3\E|\Q$name4\E|\Q$name5\E)/)
326                     {
327                         $$output->write("$file: $external_name: internal name ($internal_name) mismatch\n");
328                     }
329                 }
330             }
331         } elsif(/^(\d+|@)\s+stub(?:\s+(?:-noimport|-norelay|-i386|-ret64))?\s+(\S+)$/) {
332             my $external_name = $2;
333
334             $ordinal = $1;
335
336             my $internal_name;
337             if($type eq "win16") {
338                 if($external_name =~ /\d$/) {
339                     $internal_name = $external_name . "_16";
340                 } else {
341                     $internal_name = $external_name . "16";
342                 }
343             } else {
344                 $internal_name = $external_name;
345             }
346
347             # FIXME: Internal name existing more than once not handled properly
348             $$function_stub{$internal_name} = 1;
349             if(!$$function_module{$internal_name}) {
350                 $$function_module{$internal_name} = "$module";
351             } elsif($$function_module{$internal_name} !~ /$module/) {
352                 $$function_module{$internal_name} .= " & $module";
353             }
354         } elsif(/^(\d+|@)\s+forward(?:\s+(?:-noimport|-norelay|-i386|-ret64))?\s+(\S+)\s+(\S+)\.(\S+)$/) {
355             $ordinal = $1;
356
357             my $external_name = $2;
358             my $forward_module = lc($3);
359             my $forward_name = $4;
360
361             $$function_forward{$external_name} = "$module:$forward_module.$forward_name";
362         } elsif(/^(\d+|@)\s+(equate|extern|variable)/) {
363             # ignore
364         } else {
365             my $next_line = <IN>;
366             if(!defined($next_line) || $next_line =~ /^\s*\d|@/) {
367                 die "$file: $.: syntax error: '$_'\n";
368             } else {
369                 $_ .= $next_line;
370                 $lookahead = 1;
371             }
372         }
373         
374         if(defined($ordinal)) {
375             if($ordinal ne "@" && $ordinals{$ordinal}) {
376                 $$output->write("$file: ordinal redefined: $_\n");
377             }
378             $ordinals{$ordinal}++;
379         }
380     }
381     close(IN);
382
383     $$modules{$module}++;
384     if(defined($module_file)) {
385         $$module_files{$module} = $module_file;
386     } else {
387         $$module_files{$module} = "$module.drv";
388     }
389 }
390
391 sub name {
392     my $self = shift;
393     my $name = \${$self->{NAME}};
394
395     return $$name;
396 }
397
398 sub is_allowed_kind {
399     my $self = shift;
400     my $allowed_kind = \%{$self->{ALLOWED_KIND}};
401
402     my $kind = shift;
403     if(defined($kind)) {
404         return $$allowed_kind{$kind};
405     } else {
406         return 0;
407     }
408 }
409
410 sub is_limited_type {
411     my $self = shift;
412     my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
413
414     my $type = shift;
415
416     return $$allowed_modules_limited{$type};
417 }
418
419 sub allowed_type_in_module {
420     my $self = shift;
421     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
422     my $allowed_modules_limited = \%{$self->{ALLOWED_MODULES_LIMITED}};
423
424     my $type = shift;
425     my @modules = split(/ \& /, shift);
426
427     if(!$$allowed_modules_limited{$type}) { return 1; }
428
429     foreach my $module (@modules) {
430         if($$allowed_modules{$type}{$module}) { return 1; }
431     }
432
433     return 0;
434 }
435
436 sub type_used_in_module {
437     my $self = shift;
438     my $used_modules = \%{$self->{USED_MODULES}};
439
440     my $type = shift;
441     my @modules = split(/ \& /, shift);
442
443     foreach my $module (@modules) {
444         $$used_modules{$type}{$module} = 1;
445     }
446
447     return ();
448 }
449
450 sub types_not_used {
451     my $self = shift;
452     my $used_modules = \%{$self->{USED_MODULES}};
453     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
454
455     my $not_used;
456     foreach my $type (sort(keys(%$allowed_modules))) {
457         foreach my $module (sort(keys(%{$$allowed_modules{$type}}))) {
458             if(!$$used_modules{$type}{$module}) {
459                 $$not_used{$module}{$type} = 1;
460             }
461         }
462     }
463     return $not_used;
464 }
465
466 sub types_unlimited_used_in_modules {
467     my $self = shift;
468
469     my $output = \${$self->{OUTPUT}};
470     my $used_modules = \%{$self->{USED_MODULES}};
471     my $allowed_modules = \%{$self->{ALLOWED_MODULES}};
472     my $allowed_modules_unlimited = \%{$self->{ALLOWED_MODULES_UNLIMITED}};
473
474     my $used_types;
475     foreach my $type (sort(keys(%$allowed_modules_unlimited))) {
476         my $count = 0;
477         my @modules = ();
478         foreach my $module (sort(keys(%{$$used_modules{$type}}))) {
479             $count++;
480             push @modules, $module;
481         }
482         if($count) {
483             foreach my $module (@modules) {
484               $$used_types{$type}{$module} = 1;
485             }
486         }
487     }
488     return $used_types;
489 }
490
491 sub translate_argument {
492     my $self = shift;
493     my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
494
495     my $argument = shift;
496
497     return $$translate_argument{$argument};
498 }
499
500 sub all_declared_types {
501     my $self = shift;
502     my $translate_argument = \%{$self->{TRANSLATE_ARGUMENT}};
503
504     return sort(keys(%$translate_argument));
505 }
506
507 sub found_type {
508     my $self = shift;
509     my $type_found = \%{$self->{TYPE_FOUND}};
510
511     my $name = shift;
512
513     $$type_found{$name}++;
514 }
515
516 sub type_found {
517     my $self = shift;
518     my $type_found= \%{$self->{TYPE_FOUND}};
519
520     my $name = shift;
521
522     return $$type_found{$name};
523 }
524
525 sub is_allowed_type_format {
526     my $self = shift;
527     my $type_format = \%{$self->{TYPE_FORMAT}};
528
529     my $module = shift;
530     my $type = shift;
531     my $format = shift;
532
533     my $formats;
534
535     if(defined($module) && defined($type)) {
536         local $_;
537         foreach (split(/ & /, $module)) {
538             if(defined($formats)) { 
539                 $formats .= "|"; 
540             } else {
541                 $formats = "";
542             }       
543             if(defined($$type_format{$_}{$type})) {
544                 $formats .= $$type_format{$_}{$type};
545             }
546         }
547     }
548
549     if(defined($formats)) {
550         local $_;
551         foreach (split(/\|/, $formats)) {
552             if($_ eq $format) {
553                 return 1;
554             }
555         }
556     }
557
558     return 0;
559 }
560
561 sub all_modules {
562     my $self = shift;
563     my $modules = \%{$self->{MODULES}};
564
565     return sort(keys(%$modules));
566 }
567
568 sub is_module {
569     my $self = shift;
570     my $modules = \%{$self->{MODULES}};
571
572     my $name = shift;
573
574     return $$modules{$name};
575 }
576
577 sub module_file {
578     my $self = shift;
579
580     my $module = shift;
581
582     my $module_files = \%{$self->{MODULE_FILES}};
583
584     return $$module_files{$module};
585 }
586
587 sub all_functions {
588     my $self = shift;
589     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
590
591     return sort(keys(%$function_calling_convention));
592 }
593
594 sub all_functions_in_module {
595     my $self = shift;
596     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
597     my $function_module = \%{$self->{FUNCTION_MODULE}};
598
599     my $module = shift;
600
601     my @names;
602     foreach my $name (keys(%$function_calling_convention)) {
603         if($$function_module{$name} eq $module) {
604             push @names, $name;
605         }
606     }
607
608     return sort(@names);
609 }
610
611 sub all_functions_stub {
612     my $self = shift;
613     my $function_stub = \%{$self->{FUNCTION_STUB}};
614
615     return sort(keys(%$function_stub));
616 }
617
618 sub all_functions_found {
619     my $self = shift;
620     my $function_found = \%{$self->{FUNCTION_FOUND}};
621
622     return sort(keys(%$function_found));
623 }
624
625 sub function_ordinal {
626     my $self = shift;
627     my $function_ordinal = \%{$self->{FUNCTION_ORDINAL}};
628
629     my $name = shift;
630
631     return $$function_ordinal{$name};
632 }
633
634 sub function_calling_convention {
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 function_external_name {
644     my $self = shift;
645     my $function_external_name = \%{$self->{FUNCTION_EXTERNAL_NAME}};
646
647     my $name = shift;
648
649     return $$function_external_name{$name};
650 }
651
652 sub is_function {
653     my $self = shift;
654     my $function_calling_convention = \%{$self->{FUNCTION_CALLING_CONVENTION}};
655
656     my $name = shift;
657
658     return $$function_calling_convention{$name};
659 }
660
661 sub is_shared_function {
662     my $self = shift;
663     my $function_shared = \%{$self->{FUNCTION_SHARED}};
664
665     my $name = shift;
666
667     return $$function_shared{$name};
668 }
669
670 sub found_shared_function {
671     my $self = shift;
672     my $function_shared = \%{$self->{FUNCTION_SHARED}};
673
674     my $name = shift;
675
676     $$function_shared{$name} = 1;
677 }
678
679 sub function_arguments {
680     my $self = shift;
681     my $function_arguments = \%{$self->{FUNCTION_ARGUMENTS}};
682
683     my $name = shift;
684
685     return $$function_arguments{$name};
686 }
687
688 sub function_module {
689     my $self = shift;
690     my $function_module = \%{$self->{FUNCTION_MODULE}};
691
692     my $name = shift;
693
694     return $$function_module{$name};
695 }
696
697 sub function_stub {
698     my $self = shift;
699     my $function_stub = \%{$self->{FUNCTION_STUB}};
700
701     my $name = shift;
702
703     return $$function_stub{$name};
704 }
705
706 sub found_function {
707     my $self = shift;
708     my $function_found = \%{$self->{FUNCTION_FOUND}};
709
710     my $name = shift;
711
712     $$function_found{$name}++;
713 }
714
715 sub function_found {
716     my $self = shift;
717     my $function_found = \%{$self->{FUNCTION_FOUND}};
718
719     my $name = shift;
720
721     return $$function_found{$name};
722 }
723
724 1;