Several bug fixes and additions.
[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 => 0,
106         parent => "documentation",
107         description => "check for documentation ordinal inconsistances\n"
108         },
109
110     "prototype" => {default => 0, parent => ["local", "headers"], description => "prototype checking" },
111     "global" => { default => 1, description => "global checking" },
112     "declared" => { default => 1, parent => "global", description => "declared checking" },
113     "implemented" => { default => 0, parent => "local", description => "implemented checking" },
114     "implemented-win32" => { default => 0, parent => "implemented", description => "implemented as win32 checking" },
115     "include" => { default => 1, parent => "global", description => "include checking" },
116     "headers" => { default => 0, parent => "global", description => "headers checking" },
117     "headers-duplicated" => { default => 0, parent => "headers", description => "duplicated function declarations checking" },
118     "headers-misplaced" => { default => 0, parent => "headers", description => "misplaced function declarations checking" },
119     "stubs" => { default => 0, parent => "global", description => "stubs checking" }
120 );
121
122 my %short_options = (
123     "d" => "debug",
124     "?" => "help",
125     "v" => "verbose"
126 );
127
128 sub new {
129     my $proto = shift;
130     my $class = ref($proto) || $proto;
131     my $self  = {};
132     bless ($self, $class);
133
134     my $output = \${$self->{OUTPUT}};
135
136     $$output = shift;
137     my $refarguments = shift;
138     my $wine_dir = shift;
139
140     $self->options_set("default");
141
142     my $c_files = \@{$self->{C_FILES}};
143     my $h_files = \@{$self->{H_FILES}};
144     my $module = \${$self->{MODULE}};
145     my $global = \${$self->{GLOBAL}};
146
147     my @files;
148
149     if($wine_dir eq ".") {
150         $$global = 1;
151     } else {
152         $$global = 0;
153     }
154
155     while(defined($_ = shift @$refarguments)) {
156         if(/^--(all|none)$/) {
157             $self->options_set("$1");
158             next;
159         } elsif(/^-([^=]*)(=(.*))?$/) {
160             my $name;
161             my $value;
162             if(defined($2)) {
163                 $name = $1;
164                 $value = $3;
165             } else {
166                 $name = $1;
167             }
168             
169             if($name =~ /^([^-].*)$/) {
170                 $name = $short_options{$1};
171             } else {
172                 $name =~ s/^-(.*)$/$1/;
173             }
174                    
175             my $prefix;
176             if(defined($name) && $name =~ /^no-(.*)$/) {
177                 $name = $1;
178                 $prefix = "no";
179                 if(defined($value)) {
180                     $$output->write("options with prefix 'no' can't take parameters\n");
181
182                     return undef;
183                 }
184             }
185
186             my $option;
187             if(defined($name)) {
188                 $option = $options{$name};
189             }
190
191             if(defined($option)) {
192                 my $key = $$option{key};
193                 my $parser = $$option{parser};
194                 my $refvalue = \${$self->{$key}};
195                 my @parents = ();
196                 
197                 if(defined($$option{parent})) {
198                     if(ref($$option{parent}) eq "ARRAY") {
199                         @parents = @{$$option{parent}};
200                     } else {
201                         @parents = $$option{parent};
202                     }
203                 }
204
205                 if(defined($parser)) { 
206                     $$refvalue = &$parser($prefix,$value);
207                 } else {
208                     if(defined($value)) {
209                         $$refvalue = $value;
210                     } elsif(!defined($prefix)) {
211                         $$refvalue = 1;
212                     } else {
213                         $$refvalue = 0;
214                     }
215                 }
216
217                 if((ref($$refvalue) eq "HASH" && $$refvalue->{active}) || $$refvalue) {
218                     while($#parents >= 0) {
219                         my @old_parents = @parents;
220                         @parents = ();
221                         foreach my $parent (@old_parents) {
222                             my $parentkey = $options{$parent}{key};
223                             my $refparentvalue = \${$self->{$parentkey}};
224                             
225                             $$refparentvalue = 1;
226
227                             if(defined($options{$parent}{parent})) {
228                                 if(ref($options{$parent}{parent}) eq "ARRAY") {
229                                     push @parents, @{$options{$parent}{parent}};
230                                 } else {
231                                     push @parents, $options{$parent}{parent};
232                                 }
233                             }
234                         }
235                     }
236                 }
237                 next;
238             }    
239         }
240         
241         if(/^--module-dlls$/) {
242             my @dirs = `cd dlls && find . -type d ! -name CVS`;
243             my %names;
244             for my $dir (@dirs) {
245                 chomp $dir;
246                 $dir =~ s/^\.\/(.*)$/$1/;
247                 next if $dir eq "";
248                 $names{$dir} = 1;
249             }
250             $$module = { active => 1, filter => 1, hash => \%names };
251         }       
252         elsif(/^-(.*)$/) {
253             $$output->write("unknown option: $_\n"); 
254
255             return undef;
256         } else {
257             if(!-e $_) {
258                 $$output->write("$_: no such file or directory\n");
259
260                 return undef;
261             }
262
263             push @files, $_;
264         }
265     }
266
267     if($self->help) {
268         return $self;
269     }
270
271     my @paths = ();
272     my @c_files = ();
273     my @h_files = ();
274     foreach my $file (@files) {
275         if($file =~ /\.c$/) {
276             push @c_files, $file;
277         } elsif($file =~ /\.h$/) {
278             push @h_files, $file;
279         } else {
280             push @paths, $file;
281         }
282     }
283
284     if($#c_files == -1 && $#h_files == -1 &&
285        ($#paths == -1 || ($#paths == 0 && $paths[0] eq $wine_dir)))
286     {
287         @paths = ".";
288         push @h_files, "$wine_dir/include";
289     } else {
290         $$global = 0;
291     }
292
293     if($#paths != -1 || $#c_files != -1) {
294         my $c_command = "find " . join(" ", @paths, @c_files) . " -name \\*.c";
295         my %found;
296         @$c_files = sort(map {
297             s/^\.\/(.*)$/$1/;
298             if(defined($found{$_}) || /glue\.c|spec\.c$/) {
299                 ();
300             } else {
301                 $found{$_}++;
302                 $_;
303             }
304         } split(/\n/, `$c_command`));
305     }
306
307     if($#h_files != -1) {
308         my $h_command = "find " . join(" ", @h_files) . " -name \\*.h";
309         my %found;
310
311         @$h_files = sort(map {
312             s/^\.\/(.*)$/$1/;
313             if(defined($found{$_})) {
314                 ();
315             } else {
316                 $found{$_}++;
317                 $_;
318             }
319         } split(/\n/, `$h_command`));
320     }
321     return $self;
322 }
323
324 sub DESTROY {
325 }
326
327 sub options_set {
328     my $self = shift;
329
330     local $_ = shift;
331     for my $name (sort(keys(%options))) {
332         my $option = $options{$name};
333         my $key = uc($name);
334         $key =~ tr/-/_/;
335         $$option{key} = $key;
336         my $refvalue = \${$self->{$key}};
337
338         if(/^default$/) {
339             $$refvalue = $$option{default};
340         } elsif(/^all$/) {
341             if($name !~ /^help|debug|verbose|module$/) {
342                 if(ref($$refvalue) ne "HASH") {
343                     $$refvalue = 1;
344                 } else {
345                     $$refvalue = { active => 1, filter => 0, hash => {} };
346                 }
347             }
348         } elsif(/^none$/) {
349             if($name !~ /^help|debug|verbose|module$/) {
350                 if(ref($$refvalue) ne "HASH") {
351                     $$refvalue = 0;
352                 } else {
353                     $$refvalue = { active => 0, filter => 0, hash => {} };
354                 }
355             }
356         }
357     }
358 }
359
360 sub show_help {
361     my $self = shift;
362
363     my $maxname = 0;
364     for my $name (sort(keys(%options))) {
365         if(length($name) > $maxname) {
366             $maxname = length($name);
367         }
368     }
369
370     print "usage: winapi-check [--help] [<files>]\n";
371     print "\n";
372     for my $name (sort(keys(%options))) {
373         my $option = $options{$name};
374         my $description = $$option{description};
375         my $default = $$option{default};
376         my $current = ${$self->{$$option{key}}};
377
378         my $value = $current;
379         
380         my $output;
381         if(ref($value) ne "HASH") {
382             if($value) {
383                 $output = "--no-$name";
384             } else {
385                 $output = "--$name";
386             }
387         } else {
388             if($value->{active}) {
389                 $output = "--[no-]$name\[=<value>]";
390             } else {
391                 $output = "--$name\[=<value>]";
392             }
393         }
394
395         print "$output";
396         for (0..(($maxname - length($name) + 17) - (length($output) - length($name) + 1))) { print " "; }
397         if(ref($value) ne "HASH") {
398             if($value) {
399                 print "Disable ";
400             } else {
401                 print "Enable ";
402             }    
403         } else {
404             if($value->{active}) {
405                 print "(Disable) ";
406             } else {
407                 print "Enable ";
408             }
409         }
410         if($default == $current) {
411             print "$description (default)\n";
412         } else {
413             print "$description\n";
414         }    
415     }
416 }
417
418 sub AUTOLOAD {
419     my $self = shift;
420
421     my $name = $winapi_options::AUTOLOAD;
422     $name =~ s/^.*::(.[^:]*)$/\U$1/;
423
424     my $refvalue = $self->{$name};
425     if(!defined($refvalue)) {
426         die "<internal>: winapi_options.pm: member $name does not exists\n"; 
427     }
428
429     if(ref($$refvalue) ne "HASH") {
430         return $$refvalue;
431     } else {
432         return $$refvalue->{active};
433     }
434 }
435
436 sub c_files { my $self = shift; return @{$self->{C_FILES}}; }
437
438 sub h_files { my $self = shift; return @{$self->{H_FILES}}; }
439
440 sub report_module {
441     my $self = shift;
442     my $refvalue = $self->{MODULE};
443     
444     my $name = shift;
445
446     if(defined($name)) {
447         return $$refvalue->{active} && (!$$refvalue->{filter} || $$refvalue->{hash}->{$name}); 
448     } else {
449         return 0;
450     } 
451 }
452
453 sub report_argument_forbidden {
454     my $self = shift;   
455     my $refargument_forbidden = $self->{ARGUMENT_FORBIDDEN};
456
457     my $type = shift;
458
459     return $$refargument_forbidden->{active} && (!$$refargument_forbidden->{filter} || $$refargument_forbidden->{hash}->{$type}); 
460 }
461
462 sub report_argument_kind {
463     my $self = shift;
464     my $refargument_kind = $self->{ARGUMENT_KIND};
465
466     my $kind = shift;
467
468     return $$refargument_kind->{active} && (!$$refargument_kind->{filter} || $$refargument_kind->{hash}->{$kind}); 
469
470 }
471
472 1;
473