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