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