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