Reverse the order for deleting the items in resetcontent to correctly
[wine] / tools / winapi / winapi_cleanup
1 #! /usr/bin/perl -w
2
3 # Copyright 2002 Patrik Stridvall
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 #
19
20 use strict;
21
22 BEGIN {
23     $0 =~ m%^(.*?/?tools)/winapi/winapi_cleanup$%;
24     require "$1/winapi/setup.pm";
25 }
26
27 use config qw($current_dir $wine_dir file_absolutize file_directory);
28 use output qw($output);
29 use winapi_cleanup_options qw($options);
30
31 use nativeapi qw($nativeapi);
32 use util qw(edit_file);
33
34 if($options->progress) {
35     $output->enable_progress;
36 } else {
37     $output->disable_progress;
38 }
39
40 ########################################################################
41 # cleanup_file
42
43 sub cleanup_file($$$) {
44     local *IN = shift;
45     local *OUT = shift;
46
47     my $file = shift;
48     my $dir = do {
49         my $file2 = file_absolutize($file);
50         my $dir = file_directory($file2);
51         "$wine_dir/$dir";
52     };
53
54     my $indent;
55     my @comments = ();
56     my $format_comments = sub {
57         local $_ = "";
58         if ($#comments == 0) {
59             my $comment = $comments[0];
60
61             $_ = "$indent/*$comment */";
62         } elsif ($#comments > 0) {
63             $_ = "$indent/*\n";
64             foreach my $comment (@comments) {
65                 $_ .= "$indent *$comment\n";
66             }
67             $_ .= "$indent */";
68         }
69         $indent = "";
70         @comments = ();
71
72         return $_;
73     };
74
75     my $in_comment = 0;
76     my $modified = 0;
77     while (<IN>) {
78         chomp;
79
80         if ($options->trailing_whitespace) {
81             s/(.*?)\s+$/$1/ && do { $modified = 1; };
82         } else {
83             s/(.*?)\r$/$1/ && do { $modified = 1; };
84         }
85
86         if ($options->cpp_comments) {
87             if ($in_comment) {
88                 if(/^.*?\*\//) {
89                     $in_comment = 0;
90                 }
91             } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\*(.*?)$/) {
92                 my $indent2 = $1;
93                 my $comment = $2;
94                 if($comment !~ /^.*?\*\//) {
95                     $in_comment = 1;
96                 }
97             } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\/(.*?)\s*$/) {
98                 my $indent2 = $1;
99                 my $comment = $2;
100                 
101                 if ($indent2 =~ /^\s*$/) {
102                     if (!$indent || $indent eq $indent2) {
103                         $indent = $indent2;
104                         push @comments, $comment;
105                         next;
106                     } else {
107                         $_ .= "$indent2/*$comment */";
108                     }
109                 } else {
110                     my $comments = &$format_comments();
111                     if ($comments) {
112                         $_  = "$comments\n$indent2/*$comment */";
113                     } else {
114                         $_  = "$indent2/*$comment */";
115                     }
116
117                     $modified = 1;
118                 }
119             } else {
120                 my $comments = &$format_comments();
121                 if ($comments) {
122                     $_  = "$comments\n$_";
123                     $modified = 1;
124                 }
125             }
126         }
127
128         if ($options->include_quotes) {
129             my $module = "";
130             if ($dir =~ m%^$wine_dir/(?:windows)$%) {
131                 $module = "user";
132             } elsif ($dir =~ m%^$wine_dir/dlls/(.*?)/.*?$%) {
133                 $module = $1;
134             }
135
136             if (/^(\s*\#\s*include\s+)(?:<(.*?)>|\"(.*?)\")(.*?)\s*$/) {
137                 my $prefix = $1;
138                 my $header = $2 || $3;
139                 my $local = !defined($2) && defined($3);
140                 my $suffix = $4;
141
142                 my $exists_system = 0; {
143                     my @system_paths = qw(/usr/include /usr/local/include /usr/X11R6/include);
144                     foreach my $path (@system_paths) {
145                         $exists_system ||= (-e "$path/$header" || 0);
146                     }
147                     $exists_system ||= $nativeapi->is_conditional_header($header);
148                 }
149
150                 my $exists_windows = 0; {
151                     if ($header !~ m%^wine/% && $header ne "config.h") {
152                         $exists_windows ||= (-e "$wine_dir/include/$header" || 0);
153                     }
154                 }
155
156                 my $exists_wine = 0; {
157                     $exists_wine ||= ($header eq "config.h");
158
159                     if ($header =~ m%^wine/%) {
160                         $exists_wine ||= (-e "$wine_dir/include/$header" || 0);
161                     }
162                 }
163
164                 my $exists_local = 0; {
165                     $exists_local ||= ($header eq "y.tab.h");
166
167                     my @local_paths = ();
168                     push @local_paths, "$dir" if $dir !~ m%^$wine_dir/include%;
169                     push @local_paths, "$wine_dir/dlls/$module" if $module;
170                     foreach my $path (@local_paths) {
171                         $exists_local ||= (-e "$path/$header" || 0);
172                     }
173                 }
174
175                 if (!$exists_system && !$exists_windows && !$exists_wine && !$exists_local) {
176                     $output->write("warning: header '$header': doesn't exist\n");
177                 } elsif (($exists_system + $exists_windows + $exists_wine + $exists_local) > 1) {
178                     if ($header !~ /^(?:async\.h|comcat\.h|sql(?:ext|types)?\.h|thread\.h|user\.h)$/) {
179                         $output->write("warning: header '$header': more than one possibillity\n");
180                     }
181                 }
182
183                 if (!$local && $exists_local) {
184                     $_ = "$prefix\"$header\"$suffix";
185                     $modified = 1;
186                 } elsif ($local && !$exists_local && ($exists_system || $exists_windows || $exists_wine)) {
187                     $_ = "$prefix<$header>$suffix";
188                     $modified = 1;
189                 }
190             }
191         }
192
193         print OUT "$_\n";
194     }
195
196     my $comments = &$format_comments();
197     if ($comments) {
198         print OUT "$comments\n";
199         $modified = 1;
200     }
201
202     return $modified;
203 }
204
205 ########################################################################
206 # main
207
208 my @h_files = $options->h_files;
209 my @c_files = $options->c_files;
210
211 my $progress_output;
212 my $progress_current = 0;
213 my $progress_max = scalar(@h_files) + scalar(@c_files);
214
215 foreach my $file (@h_files) {
216     $progress_current++;
217     $output->progress("$file (file $progress_current of $progress_max)");
218     $output->prefix("$file: ");
219
220     if (edit_file($file, \&cleanup_file, @_, $file)) {
221         $output->write("$file: modified\n");
222     }
223 }
224
225 foreach my $file (@c_files) {
226     $progress_current++;
227     $output->progress("$file (file $progress_current of $progress_max)");
228     $output->prefix("$file: ");
229
230     if (edit_file($file, \&cleanup_file, $file)) {
231         $output->write("$file: modified\n");
232     }
233 }