Several additions and bug fixes.
[wine] / tools / winapi_check / winapi_options.pm
1 package winapi_options;
2
3 use strict;
4
5 sub parser_comma_list {
6     my $prefix = shift;
7     my $value = shift;
8     if(defined($prefix) && $prefix eq "no") {
9         return { active => 0, filter => 0, hash => {} };
10     } elsif(defined($value)) {
11         my %names;
12         for my $name (split /,/, $value) {
13             $names{$name} = 1;
14         }
15         return { active => 1, filter => 1, hash => \%names };
16     } else {
17         return { active => 1, filter => 0, hash => {} };
18     }
19 }
20
21 my %options = (
22     "debug" => { default => 0, description => "debug mode" },
23     "help" => { default => 0, description => "help mode" },
24     "verbose" => { default => 0, description => "verbose mode" },
25
26     "progress" => { default => 1, description => "show progress" },
27
28     "win16" => { default => 1, description => "Win16 checking" },
29     "win32" => { default => 1, description => "Win32 checking" },
30
31     "shared" =>  { default => 0, description => "show shared functions between Win16 and Win32" },
32     "shared-segmented" =>  { default => 0, description => "segmented shared functions between Win16 and Win32 checking" },
33
34     "config" => { default => 1, parent => "local", description => "check configuration include consistancy" },
35     "config-unnessary" => { default => 0, parent => "config", description => "check for unnessary #include \"config.h\"" },
36
37     "spec-mismatch" => { default => 0, description => "spec file mismatch checking" },
38
39     "local" =>  { default => 1, description => "local checking" },
40     "module" => { 
41         default => { active => 1, filter => 0, hash => {} },
42         parent => "local",
43         parser => \&parser_comma_list,
44         description => "module filter"
45     },
46
47     "argument" => { default => 1, parent => "local", description => "argument checking" },
48     "argument-count" => { default => 1, parent => "argument", description => "argument count checking" },
49     "argument-forbidden" => {
50         default => { active => 1, filter => 0, hash => {} },
51         parent => "argument",
52         parser => \&parser_comma_list,
53         description => "argument forbidden checking"
54     },
55     "argument-kind" => {
56         default => { active => 1, filter => 1, hash => { double => 1 } },
57         parent => "argument",
58         parser => \&parser_comma_list,
59         description => "argument kind checking"
60     },
61     "calling-convention" => { default => 1, parent => "local", description => "calling convention checking" },
62     "calling-convention-win16" => { default => 0, parent => "calling-convention", description => "calling convention checking (Win16)" },
63     "calling-convention-win32" => { default => 1, parent => "calling-convention", description => "calling convention checking (Win32)" },
64     "misplaced" => { default => 1, parent => "local", description => "check for misplaced functions" },
65     "statements"  => { default => 0, parent => "local", description => "check for statements inconsistances" },
66     "cross-call" => { default => 0, parent => "statements",  description => "check for cross calling functions" },
67     "cross-call-win32-win16" => { 
68         default => 0, parent => "cross-call", description => "check for cross calls between win32 and win16"
69      },
70     "cross-call-unicode-ascii" => { 
71         default => 0, parent => "cross-call", description => "check for cross calls between Unicode and ASCII" 
72     },
73     "debug-messages" => { default => 0, parent => "statements", description => "check for debug messages inconsistances" },
74
75     "documentation" => {
76         default => 1,
77         parent => "local", 
78         description => "check for documentation inconsistances"
79         },
80     "documentation-pedantic" => { 
81         default => 0, 
82         parent => "documentation", 
83         description => "be pendantic when checking for documentation inconsistances"
84         },
85
86     "documentation-arguments" => {
87         default => 1,
88         parent => "documentation",
89         description => "check for arguments documentation inconsistances\n"
90         },
91     "documentation-comment-indent" => {
92         default => 0, 
93         parent => "documentation", description => "check for documentation comment indent inconsistances"
94         },
95     "documentation-comment-width" => {
96         default => 0, 
97         parent => "documentation", description => "check for documentation comment width inconsistances"
98         },
99     "documentation-name" => {
100         default => 1,
101         parent => "documentation",
102         description => "check for documentation name inconsistances\n"
103         },
104     "documentation-ordinal" => {
105         default => 1,
106         parent => "documentation",
107         description => "check for documentation ordinal inconsistances\n"
108         },
109     "documentation-wrong" => {
110         default => 1,
111         parent => "documentation",
112         description => "check for wrong documentation\n"
113         },
114
115     "prototype" => {default => 0, parent => ["local", "headers"], description => "prototype checking" },
116     "global" => { default => 1, description => "global checking" },
117     "declared" => { default => 1, parent => "global", description => "declared checking" },
118     "implemented" => { default => 0, parent => "local", description => "implemented checking" },
119     "implemented-win32" => { default => 0, parent => "implemented", description => "implemented as win32 checking" },
120     "include" => { default => 1, parent => "global", description => "include checking" },
121     "headers" => { default => 0, parent => "global", description => "headers checking" },
122     "headers-duplicated" => { default => 0, parent => "headers", description => "duplicated function declarations checking" },
123     "headers-misplaced" => { default => 0, parent => "headers", description => "misplaced function declarations checking" },
124 );
125
126 my %short_options = (
127     "d" => "debug",
128     "?" => "help",
129     "v" => "verbose"
130 );
131
132 sub new {
133     my $proto = shift;
134     my $class = ref($proto) || $proto;
135     my $self  = {};
136     bless ($self, $class);
137
138     my $output = \${$self->{OUTPUT}};
139
140     $$output = shift;
141     my $refarguments = shift;
142     my $wine_dir = shift;
143
144     $self->options_set("default");
145
146     my $c_files = \@{$self->{C_FILES}};
147     my $h_files = \@{$self->{H_FILES}};
148     my $module = \${$self->{MODULE}};
149     my $global = \${$self->{GLOBAL}};
150
151     my @files;
152
153     if($wine_dir eq ".") {
154         $$global = 1;
155     } else {
156         $$global = 0;
157     }
158
159     while(defined($_ = shift @$refarguments)) {
160         if(/^--(all|none)$/) {
161             $self->options_set("$1");
162             next;
163         } elsif(/^-([^=]*)(=(.*))?$/) {
164             my $name;
165             my $value;
166             if(defined($2)) {
167                 $name = $1;
168                 $value = $3;
169             } else {
170                 $name = $1;
171             }
172             
173             if($name =~ /^([^-].*)$/) {
174                 $name = $short_options{$1};
175             } else {
176                 $name =~ s/^-(.*)$/$1/;
177             }
178                    
179             my $prefix;
180             if(defined($name) && $name =~ /^no-(.*)$/) {
181                 $name = $1;
182                 $prefix = "no";
183                 if(defined($value)) {
184                     $$output->write("options with prefix 'no' can't take parameters\n");
185
186                     return undef;
187                 }
188             }
189
190             my $option;
191             if(defined($name)) {
192                 $option = $options{$name};
193             }
194
195             if(defined($option)) {
196                 my $key = $$option{key};
197                 my $parser = $$option{parser};
198                 my $refvalue = \${$self->{$key}};
199                 my @parents = ();
200                 
201                 if(defined($$option{parent})) {
202                     if(ref($$option{parent}) eq "ARRAY") {
203                         @parents = @{$$option{parent}};
204                     } else {
205                         @parents = $$option{parent};
206                     }
207                 }
208
209                 if(defined($parser)) { 
210                     $$refvalue = &$parser($prefix,$value);
211                 } else {
212                     if(defined($value)) {
213                         $$refvalue = $value;
214                     } elsif(!defined($prefix)) {
215                         $$refvalue = 1;
216                     } else {
217                         $$refvalue = 0;
218                     }
219                 }
220
221                 if((ref($$refvalue) eq "HASH" && $$refvalue->{active}) || $$refvalue) {
222                     while($#parents >= 0) {
223                         my @old_parents = @parents;
224                         @parents = ();
225                         foreach my $parent (@old_parents) {
226                             my $parentkey = $options{$parent}{key};
227                             my $refparentvalue = \${$self->{$parentkey}};
228                             
229                             $$refparentvalue = 1;
230
231                             if(defined($options{$parent}{parent})) {
232                                 if(ref($options{$parent}{parent}) eq "ARRAY") {
233                                     push @parents, @{$options{$parent}{parent}};
234                                 } else {
235                                     push @parents, $options{$parent}{parent};
236                                 }
237                             }
238                         }
239                     }
240                 }
241                 next;
242             }    
243         }
244         
245         if(/^--module-dlls$/) {
246             my @dirs = `cd dlls && find . -type d ! -name CVS`;
247             my %names;
248             for my $dir (@dirs) {
249                 chomp $dir;
250                 $dir =~ s/^\.\/(.*)$/$1/;
251                 next if $dir eq "";
252                 $names{$dir} = 1;
253             }
254             $$module = { active => 1, filter => 1, hash => \%names };
255         }       
256         elsif(/^-(.*)$/) {
257             $$output->write("unknown option: $_\n"); 
258
259             return undef;
260         } else {
261             if(!-e $_) {
262                 $$output->write("$_: no such file or directory\n");
263
264                 return undef;
265             }
266
267             push @files, $_;
268         }
269     }
270
271     if($self->help) {
272         return $self;
273     }
274
275     my @paths = ();
276     my @c_files = ();
277     my @h_files = ();
278     foreach my $file (@files) {
279         if($file =~ /\.c$/) {
280             push @c_files, $file;
281         } elsif($file =~ /\.h$/) {
282             push @h_files, $file;
283         } else {
284             push @paths, $file;
285         }
286     }
287
288     if($#c_files == -1 && $#h_files == -1 &&
289        ($#paths == -1 || ($#paths == 0 && $paths[0] eq $wine_dir)))
290     {
291         @paths = ".";
292         push @h_files, "$wine_dir/include";
293     } else {
294         $$global = 0;
295     }
296
297     if($#paths != -1 || $#c_files != -1) {
298         my $c_command = "find " . join(" ", @paths, @c_files) . " -name \\*.c";
299         my %found;
300         @$c_files = sort(map {
301             s/^\.\/(.*)$/$1/;
302             if(defined($found{$_}) || /glue\.c|spec\.c$/) {
303                 ();
304             } else {
305                 $found{$_}++;
306                 $_;
307             }
308         } split(/\n/, `$c_command`));
309     }
310
311     if($#h_files != -1) {
312         my $h_command = "find " . join(" ", @h_files) . " -name \\*.h";
313         my %found;
314
315         @$h_files = sort(map {
316             s/^\.\/(.*)$/$1/;
317             if(defined($found{$_})) {
318                 ();
319             } else {
320                 $found{$_}++;
321                 $_;
322             }
323         } split(/\n/, `$h_command`));
324     }
325     return $self;
326 }
327
328 sub DESTROY {
329 }
330
331 sub options_set {
332     my $self = shift;
333
334     local $_ = shift;
335     for my $name (sort(keys(%options))) {
336         my $option = $options{$name};
337         my $key = uc($name);
338         $key =~ tr/-/_/;
339         $$option{key} = $key;
340         my $refvalue = \${$self->{$key}};
341
342         if(/^default$/) {
343             $$refvalue = $$option{default};
344         } elsif(/^all$/) {
345             if($name !~ /^help|debug|verbose|module$/) {
346                 if(ref($$refvalue) ne "HASH") {
347                     $$refvalue = 1;
348                 } else {
349                     $$refvalue = { active => 1, filter => 0, hash => {} };
350                 }
351             }
352         } elsif(/^none$/) {
353             if($name !~ /^help|debug|verbose|module$/) {
354                 if(ref($$refvalue) ne "HASH") {
355                     $$refvalue = 0;
356                 } else {
357                     $$refvalue = { active => 0, filter => 0, hash => {} };
358                 }
359             }
360         }
361     }
362 }
363
364 sub show_help {
365     my $self = shift;
366
367     my $maxname = 0;
368     for my $name (sort(keys(%options))) {
369         if(length($name) > $maxname) {
370             $maxname = length($name);
371         }
372     }
373
374     print "usage: winapi-check [--help] [<files>]\n";
375     print "\n";
376     for my $name (sort(keys(%options))) {
377         my $option = $options{$name};
378         my $description = $$option{description};
379         my $default = $$option{default};
380         my $current = ${$self->{$$option{key}}};
381
382         my $value = $current;
383         
384         my $output;
385         if(ref($value) ne "HASH") {
386             if($value) {
387                 $output = "--no-$name";
388             } else {
389                 $output = "--$name";
390             }
391         } else {
392             if($value->{active}) {
393                 $output = "--[no-]$name\[=<value>]";
394             } else {
395                 $output = "--$name\[=<value>]";
396             }
397         }
398
399         print "$output";
400         for (0..(($maxname - length($name) + 17) - (length($output) - length($name) + 1))) { print " "; }
401         if(ref($value) ne "HASH") {
402             if($value) {
403                 print "Disable ";
404             } else {
405                 print "Enable ";
406             }    
407         } else {
408             if($value->{active}) {
409                 print "(Disable) ";
410             } else {
411                 print "Enable ";
412             }
413         }
414         if($default == $current) {
415             print "$description (default)\n";
416         } else {
417             print "$description\n";
418         }    
419     }
420 }
421
422 sub AUTOLOAD {
423     my $self = shift;
424
425     my $name = $winapi_options::AUTOLOAD;
426     $name =~ s/^.*::(.[^:]*)$/\U$1/;
427
428     my $refvalue = $self->{$name};
429     if(!defined($refvalue)) {
430         die "<internal>: winapi_options.pm: member $name does not exists\n"; 
431     }
432
433     if(ref($$refvalue) ne "HASH") {
434         return $$refvalue;
435     } else {
436         return $$refvalue->{active};
437     }
438 }
439
440 sub c_files { my $self = shift; return @{$self->{C_FILES}}; }
441
442 sub h_files { my $self = shift; return @{$self->{H_FILES}}; }
443
444 sub report_module {
445     my $self = shift;
446     my $refvalue = $self->{MODULE};
447     
448     my $name = shift;
449
450     if(defined($name)) {
451         return $$refvalue->{active} && (!$$refvalue->{filter} || $$refvalue->{hash}->{$name}); 
452     } else {
453         return 0;
454     } 
455 }
456
457 sub report_argument_forbidden {
458     my $self = shift;   
459     my $refargument_forbidden = $self->{ARGUMENT_FORBIDDEN};
460
461     my $type = shift;
462
463     return $$refargument_forbidden->{active} && (!$$refargument_forbidden->{filter} || $$refargument_forbidden->{hash}->{$type}); 
464 }
465
466 sub report_argument_kind {
467     my $self = shift;
468     my $refargument_kind = $self->{ARGUMENT_KIND};
469
470     my $kind = shift;
471
472     return $$refargument_kind->{active} && (!$$refargument_kind->{filter} || $$refargument_kind->{hash}->{$kind}); 
473
474 }
475
476 1;
477