- Made remove trailing whitespace a default off option.
[wine] / tools / winapi / winapi_fixup
1 #!/usr/bin/perl -w
2
3 # Copyright 2001 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_fixup$%;
24     require "$1/winapi/setup.pm";
25 }
26
27 use config qw(
28     &file_type &files_filter
29     &file_skip &files_skip
30     &file_normalize
31     &get_spec_files
32     $current_dir $wine_dir $winapi_dir $winapi_check_dir
33 );
34 use output qw($output);
35 use winapi_fixup_options qw($options);
36
37 if($options->progress) {
38     $output->enable_progress;
39 } else {
40     $output->disable_progress;
41 }
42
43 use c_parser;
44 use type;
45
46 use winapi_fixup_documentation qw(&fixup_documentation);
47 use winapi_fixup_editor;
48 use winapi_fixup_statements qw(&fixup_statements);
49
50 my @c_files = $options->c_files;
51 @c_files = files_skip(@c_files);
52 @c_files = files_filter("winelib", @c_files);
53
54 my $progress_output;
55 my $progress_current = 0;
56 my $progress_max = scalar(@c_files);
57
58 foreach my $file (@c_files) {
59     my $editor = new winapi_fixup_editor($file);
60
61     $progress_current++;
62     $output->progress("$file (file $progress_current of $progress_max)");
63     $output->prefix("$file:");
64
65     {
66         open(IN, "< $file");
67         local $/ = undef;
68         $_ = <IN>;
69         close(IN);
70     }
71
72     my $max_line = 0;
73     {
74       local $_ = $_;
75       while(s/^.*?\n//) { $max_line++; }
76       if($_) { $max_line++; }
77     }
78
79     my $parser = new c_parser($file);
80
81     my $function;
82     my $line;
83
84     my $update_output = sub {
85         my $progress = "";
86         my $prefix = "";
87
88         $progress .= "$file (file $progress_current of $progress_max)";
89         $prefix .= "$file:";
90
91         if(defined($function)) {
92             my $name = $function->name;
93             my $begin_line = $function->begin_line;
94             my $begin_column = $function->begin_column;
95
96             $progress .= ": function $name";
97             $prefix .= "$begin_line.$begin_column: function $name: ";
98         }
99
100         if(defined($line)) {
101             $progress .= ": line $line of $max_line";
102         }
103
104         $output->progress($progress);
105         $output->prefix($prefix);
106     };
107
108     my $found_preprocessor = sub {
109         my $begin_line = shift;
110         my $begin_column = shift;
111         my $preprocessor = shift;
112
113         # $output->write("$begin_line.$begin_column: preprocessor: $preprocessor\n");
114
115         return 1;
116     };
117
118     $parser->set_found_preprocessor_callback($found_preprocessor);
119
120     my $found_comment = sub {
121         my $begin_line = shift;
122         my $begin_column = shift;
123         my $comment = shift;
124
125         # $output->write("$begin_line.$begin_column: comment: $comment\n");
126
127         return 1;
128     };
129
130     $parser->set_found_comment_callback($found_comment);
131
132     my $found_line = sub {
133         $line = shift;
134         # local $_ = shift;
135
136         &$update_output;
137
138         # $output->progress("$file: line $line of ?");
139     };
140
141     $parser->set_found_line_callback($found_line);
142
143     my $found_declaration = sub {
144         my $begin_line = shift;
145         my $begin_column = shift;
146         my $end_line = shift;
147         my $end_column = shift;
148         my $declaration = shift;
149
150         # $output->write("$begin_line.$begin_column-$end_line.$end_column: declaration: \\\n$declaration\n");
151
152         return 1;
153     };
154
155     $parser->set_found_declaration_callback($found_declaration);
156
157     my $found_function = sub {
158         $function = shift;
159
160         &$update_output;
161
162         my $name = $function->name;
163         my $begin_line = $function->begin_line;
164         my $begin_column = $function->begin_column;
165         my $end_line = $function->end_line;
166         my $end_column = $function->end_column;
167
168         if($options->documentation) {
169             # fixup_documentation($function, $editor);
170         }
171
172         if($options->statements) {
173             fixup_statements($function, $editor);
174         }
175
176         my $statements = $function->statements;
177         if(!defined($statements)) {
178             $function = undef;
179             $output->prefix("$file: ");
180         } else {
181             # $output->write("$begin_line.$begin_column-$end_line.$end_column: function $name\n");
182         }
183
184         return 0;
185     };
186
187     $parser->set_found_function_callback($found_function);
188
189     my $found_variable = sub {
190         my $begin_line = shift;
191         my $begin_column = shift;
192         my $linkage = shift;
193         my $type = shift;
194         my $name = shift;
195
196         # $output->write("$begin_line.$begin_column: $linkage $type $name = /* ... */\n");
197
198         return 1;
199     };
200
201     $parser->set_found_variable_callback($found_variable);
202
203     my $found_function_call = sub {
204         my $begin_line = shift;
205         my $begin_column = shift;
206         my $end_line = shift;
207         my $end_column = shift;
208         my $name = shift;
209         my $arguments = shift;
210
211         $output->write("$begin_line.$begin_column-$end_line.$end_column: $name(" . join(", ", @$arguments) . ")\n");
212
213         return 1;
214     };
215
216     $parser->set_found_function_call_callback($found_function_call);
217
218     {
219         my $line = 1;
220         my $column = 0;
221         if(!$parser->parse_c_file(\$_, \$line, \$column)) {
222             $output->write("can't parse file\n");
223         }
224     }
225
226     $output->prefix("");
227
228     $editor->flush;
229 }