]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/formater.pl
Merge from trunk
[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
31 #$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
32 $ASTYLE_ARGS ="--mode=c -s4 -O -l";
33
34 #$ASTYLE_ARGS="--mode=c -s4 -O --break-blocks -l";
35 #$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
36
37
38 if(! -e $ASTYLE_BIN){
39 print "\nFile $ASTYLE_BIN not found\n";
40 print "Please fix the ASTYLE_BIN variable in this script!\n\n";
41 exit -1;
42 }
43 $ASTYLE_BIN=$ASTYLE_BIN." ".$ASTYLE_ARGS;
44
45 $INDENT = "";
46
47 $out = shift @ARGV;
48 #read options, currently no options available
49 while($out eq "" || $out =~ /^-\w+$/){
50 if($out eq "-h") {
51 usage($0);
52 exit 0;
53 }
54 else {
55 usage($0);
56 exit -1;
57 }
58 }
59
60
61 while($out){
62
63 if( $out !~ /\.cc$|\.cci$|\.h$|\.c$/) {
64 print "Unknown suffix for file $out, ignoring....\n";
65 $out = shift @ARGV;
66 next;
67 }
68
69 $in= "$out.astylebak";
70 my($new_in) = $in;
71 my($i) = 0;
72 while(-e $new_in) {
73 $new_in=$in.".".$i;
74 $i++;
75 }
76 $in=$new_in;
77 rename($out, $in);
78
79 local (*FROM_ASTYLE, *TO_ASTYLE);
80 $pid_style=open2(\*FROM_ASTYLE, \*TO_ASTYLE, $ASTYLE_BIN);
81
82 if(!$pid_style){
83 print "An error while open2\n";
84 exit -1;
85 }
86
87
88 if($pid=fork()){
89 #do parrent staf
90 close(FROM_ASTYLE);
91
92 if(!open(IN, "<$in")){
93 print "Can not open input file: $in\n";
94 exit -1;
95 }
96 my($line) = '';
97 while(<IN>){
98 $line=$line.$_;
99 if(input_filter(\$line)==0){
100 next;
101 }
102 print TO_ASTYLE $line;
103 $line = '';
104 }
105 if($line){
106 print TO_ASTYLE $line;
107 }
108 close(TO_ASTYLE);
109 waitpid($pid,0);
110 }
111 else{
112 # child staf
113 close(TO_ASTYLE);
114
115 if(!open(OUT,">$out")){
116 print "Can't open output file: $out\n";
117 exit -1;
118 }
119 my($line)='';
120 while(<FROM_ASTYLE>){
121 $line = $line.$_;
122 if(output_filter(\$line)==0){
123 next;
124 }
125 print OUT $line;
126 $line = '';
127 }
128 if($line){
129 print OUT $line;
130 }
131 close(OUT);
132 exit 0;
133 }
134
135 $out = shift @ARGV;
136 }
137
138
139 sub input_filter{
140 my($line)=@_;
141 #if we have integer declaration, get it all before processing it..
142
143 if($$line =~/\s+int\s+.*/s || $$line=~ /\s+unsigned\s+.*/s ||
144 $$line =~/^int\s+.*/s || $$line=~ /^unsigned\s+.*/s
145 ){
146 if( $$line =~ /(\(|,|\)|\#|typedef)/s ){
147 #excluding int/unsigned appeared inside function prototypes,typedefs etc....
148 return 1;
149 }
150
151 if(index($$line,";") == -1){
152 # print "Getting one more for \"".$$line."\"\n";
153 return 0;
154 }
155
156 if($$line =~ /(.*)\s*int\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
157 # print ">>>>> ".$$line." ($1)\n";
158 local($prx,$name,$val,$extra)=($1,$2,$3,$4);
159 $prx =~ s/\s*$//g;
160 $$line= $prx." int ".$name."__FORASTYLE__".$val.";".$extra;
161 # print "----->".$$line."\n";
162 }
163 elsif($$line =~ /\s*unsigned\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
164 local($name,$val,$extra)=($1,$2,$3);
165 $prx =~ s/\s*$//g;
166 $$line= "unsigned ".$name."__FORASTYLE__".$val.";".$extra;
167 }
168 return 1;
169 }
170
171 if($$line =~ /\#endif/ ||
172 $$line =~ /\#else/ ||
173 $$line =~ /\#if/
174 ){
175 $$line =$$line."//__ASTYLECOMMENT__\n";
176 return 1;
177 }
178
179 return 1;
180 }
181
182 sub output_filter{
183 my($line)=@_;
184
185 if($$line =~ /^\s*$/){
186 return 1;
187 }
188
189 if($$line =~ s/\s*\/\/__ASTYLECOMMENT__//) {
190 chomp($$line);
191 }
192
193 # "The "unsigned int:1; case ....."
194 $$line =~ s/__FORASTYLE__/:/;
195
196 return 1;
197 }
198
199 sub usage{
200 my($name)=@_;
201 print "Usage:\n $name file1 file2 file3 ....\n";
202 }