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