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