]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/formater.pl
SourceFormat: report broken file and continue after errors.
[thirdparty/squid.git] / scripts / formater.pl
1 #!/usr/bin/perl
2 #
3 # Author: Tsantilas Christos
4 # email: christos@chtsanti.net
5 #
6 # Distributed under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # The ldap_manager 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 # Library General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # See LICENSE or http://www.gnu.org/licenses/gpl.html for details .
20 #
21
22
23 use IPC::Open2;
24
25 #
26 # NP: The Squid code requires astyle version 1.22 or later
27 #
28 $ASTYLE_BIN="/usr/local/bin/astyle";
29 #$ASTYLE_BIN="/usr/bin/astyle";
30 #$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
31
32 $ASTYLE_ARGS ="--mode=c -s4 -O -l";
33 #$ASTYLE_ARGS="--mode=c -s4 -O --break-blocks -l";
34
35
36 if(! -e $ASTYLE_BIN){
37 print "\nFile $ASTYLE_BIN not found\n";
38 print "Please fix the ASTYLE_BIN variable in this script!\n\n";
39 exit -1;
40 }
41 $ASTYLE_BIN=$ASTYLE_BIN." ".$ASTYLE_ARGS;
42
43 $INDENT = "";
44
45 $out = shift @ARGV;
46 #read options, currently no options available
47 while($out eq "" || $out =~ /^-\w+$/){
48 if($out eq "-h") {
49 usage($0);
50 exit 0;
51 }
52 else {
53 usage($0);
54 exit -1;
55 }
56 }
57
58
59 while($out){
60
61 if( $out !~ /\.cc$|\.cci$|\.h$|\.c$/) {
62 print "Unknown suffix for file $out, ignoring....\n";
63 $out = shift @ARGV;
64 next;
65 }
66
67 $in= "$out.astylebak";
68 my($new_in) = $in;
69 my($i) = 0;
70 while(-e $new_in) {
71 $new_in=$in.".".$i;
72 $i++;
73 }
74 $in=$new_in;
75 rename($out, $in);
76
77 local (*FROM_ASTYLE, *TO_ASTYLE);
78 $pid_style=open2(\*FROM_ASTYLE, \*TO_ASTYLE, $ASTYLE_BIN);
79
80 if(!$pid_style){
81 print "An error while open2\n";
82 exit -1;
83 }
84
85 if($pid=fork()){
86 #do parrent staf
87 close(FROM_ASTYLE);
88
89 if(!open(IN, "<$in")){
90 print "Can not open input file: $in\n";
91 exit -1;
92 }
93 my($line) = '';
94 while(<IN>){
95 $line=$line.$_;
96 if(input_filter(\$line)==0){
97 next;
98 }
99 print TO_ASTYLE $line;
100 $line = '';
101 }
102 if($line){
103 print TO_ASTYLE $line;
104 }
105 close(TO_ASTYLE);
106 waitpid($pid,0);
107 }
108 else{
109 # child staf
110 close(TO_ASTYLE);
111
112 if(!open(OUT,">$out")){
113 print "Can't open output file: $out\n";
114 exit -1;
115 }
116 my($line)='';
117 while(<FROM_ASTYLE>){
118 $line = $line.$_;
119 if(output_filter(\$line)==0){
120 next;
121 }
122 print OUT $line;
123 $line = '';
124 }
125 if($line){
126 print OUT $line;
127 }
128 close(OUT);
129 exit 0;
130 }
131
132 $out = shift @ARGV;
133 }
134
135
136 sub input_filter{
137 my($line)=@_;
138 #if we have integer declaration, get it all before processing it..
139
140 if($$line =~/\s+int\s+.*/s || $$line=~ /\s+unsigned\s+.*/s ||
141 $$line =~/^int\s+.*/s || $$line=~ /^unsigned\s+.*/s
142 ){
143 if( $$line =~ /(\(|,|\)|\#|typedef)/s ){
144 #excluding int/unsigned appeared inside function prototypes,typedefs etc....
145 return 1;
146 }
147
148 if(index($$line,";") == -1){
149 # print "Getting one more for \"".$$line."\"\n";
150 return 0;
151 }
152
153 if($$line =~ /(.*)\s*int\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
154 # print ">>>>> ".$$line." ($1)\n";
155 local($prx,$name,$val,$extra)=($1,$2,$3,$4);
156 $prx =~ s/\s*$//g;
157 $$line= $prx." int ".$name."__FORASTYLE__".$val.";".$extra;
158 # print "----->".$$line."\n";
159 }
160 elsif($$line =~ /\s*unsigned\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
161 local($name,$val,$extra)=($1,$2,$3);
162 $prx =~ s/\s*$//g;
163 $$line= "unsigned ".$name."__FORASTYLE__".$val.";".$extra;
164 }
165 return 1;
166 }
167
168 if($$line =~ /\#endif/ ||
169 $$line =~ /\#else/ ||
170 $$line =~ /\#if/
171 ){
172 $$line =$$line."//__ASTYLECOMMENT__\n";
173 return 1;
174 }
175
176 return 1;
177 }
178
179 sub output_filter{
180 my($line)=@_;
181
182 if($$line =~ /^\s*$/){
183 return 1;
184 }
185
186 if($$line =~ s/\s*\/\/__ASTYLECOMMENT__//) {
187 chomp($$line);
188 }
189
190 # "The "unsigned int:1; case ....."
191 $$line =~ s/__FORASTYLE__/:/;
192
193 return 1;
194 }
195
196 sub usage{
197 my($name)=@_;
198 print "Usage:\n $name file1 file2 file3 ....\n";
199 }