]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Author: Tsantilas Christos <christos@chtsanti.net>
authorAmos Jeffries <squid3@treenet.co.nz>
Sun, 14 Sep 2008 13:40:03 +0000 (01:40 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Sun, 14 Sep 2008 13:40:03 +0000 (01:40 +1200)
Source code formatting scripts

formater.pl
    Script to format source files with the astyle utility
    NP: Squid code requires astyle version 1.22 or later

md5checker.sh
    Scritp to validate the formater.pl script did not alter the code
    syntax in any way. All non-whitespace alterations are reported
    as bungled files for manual auditing.

scripts/formater.pl [new file with mode: 0755]
scripts/md5checker.sh [new file with mode: 0755]

diff --git a/scripts/formater.pl b/scripts/formater.pl
new file mode 100755 (executable)
index 0000000..af929fc
--- /dev/null
@@ -0,0 +1,201 @@
+#!/usr/bin/perl
+#
+# Author: Tsantilas Christos
+# email:  christos@chtsanti.net
+#
+# Distributed under the terms of the GNU General Public License as published
+# by the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# The ldap_manager library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See LICENSE or http://www.gnu.org/licenses/gpl.html for details .
+#
+
+
+use IPC::Open2;
+
+#
+# NP: The Squid code requires astyle version 1.22 or later
+#
+$ASTYLE_BIN="/usr/bin/astyle";
+
+#$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
+$ASTYLE_ARGS ="--mode=c -s4 -O -l";
+
+#$ASTYLE_ARGS="--mode=c -s4 -O --break-blocks -l";
+#$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
+
+
+if(! -e $ASTYLE_BIN){
+    print "\nFile $ASTYLE_BIN not found\n";
+    print "Please fix the ASTYLE_BIN variable in this script!\n\n";
+    exit -1;
+}
+$ASTYLE_BIN=$ASTYLE_BIN." ".$ASTYLE_ARGS;
+
+$INDENT = "";
+
+$out = shift @ARGV;
+#read options, currently no options available
+while($out eq "" ||  $out =~ /^-\w+$/){
+   if($out eq "-h") {
+        usage($0);
+        exit 0;
+   }
+   else {
+       usage($0);
+       exit -1;
+   }
+}
+
+
+while($out){
+
+    if( $out !~ /\.cc$|\.cci$|\.h$|\.c$/) {
+         print "Unknown suffix for file $out, ignoring....\n";
+         $out = shift @ARGV;
+         next;
+    }
+
+    $in= "$out.astylebak";
+    my($new_in) = $in;
+    my($i) = 0;
+    while(-e $new_in) {
+       $new_in=$in.".".$i;
+       $i++;
+    }
+    $in=$new_in;
+    rename($out, $in);
+    
+    local (*FROM_ASTYLE, *TO_ASTYLE);
+    $pid_style=open2(\*FROM_ASTYLE, \*TO_ASTYLE, $ASTYLE_BIN);
+    
+    if(!$pid_style){
+       print "An error while open2\n";
+       exit -1;
+    }
+    
+    
+    if($pid=fork()){
+       #do parrent staf
+       close(FROM_ASTYLE);
+       
+       if(!open(IN, "<$in")){
+           print "Can not open input file: $in\n";
+           exit -1;
+       }
+       my($line) = '';
+       while(<IN>){
+           $line=$line.$_;
+           if(input_filter(\$line)==0){
+               next;
+           }
+           print TO_ASTYLE $line;
+           $line = '';
+       }
+       if($line){
+           print TO_ASTYLE $line;
+       }
+       close(TO_ASTYLE);
+       waitpid($pid,0);
+    }
+    else{
+       # child staf
+       close(TO_ASTYLE);
+       
+       if(!open(OUT,">$out")){
+           print "Can't open output file: $out\n";
+           exit -1;
+       }
+       my($line)='';
+       while(<FROM_ASTYLE>){
+           $line = $line.$_;
+           if(output_filter(\$line)==0){
+               next;
+           }
+           print OUT $line;
+           $line = '';
+       }
+       if($line){
+           print OUT $line;
+       }
+       close(OUT);
+       exit 0;
+    }
+
+    $out = shift @ARGV;
+}
+
+
+sub input_filter{
+    my($line)=@_;
+     #if we have integer declaration, get it all before processing it..
+
+    if($$line =~/\s+int\s+.*/s || $$line=~ /\s+unsigned\s+.*/s ||
+       $$line =~/^int\s+.*/s || $$line=~ /^unsigned\s+.*/s
+       ){ 
+       if( $$line =~ /(\(|,|\)|\#|typedef)/s ){
+           #excluding int/unsigned appeared inside function prototypes,typedefs etc....
+           return 1;
+       }
+
+       if(index($$line,";") == -1){
+#          print "Getting one more for \"".$$line."\"\n";
+           return 0;
+       }
+
+       if($$line =~ /(.*)\s*int\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
+#          print ">>>>> ".$$line."    ($1)\n";
+            local($prx,$name,$val,$extra)=($1,$2,$3,$4);
+            $prx =~ s/\s*$//g;
+           $$line= $prx." int ".$name."__FORASTYLE__".$val.";".$extra;
+#          print "----->".$$line."\n";
+       }       
+       elsif($$line =~ /\s*unsigned\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
+            local($name,$val,$extra)=($1,$2,$3);
+            $prx =~ s/\s*$//g;
+           $$line= "unsigned ".$name."__FORASTYLE__".$val.";".$extra;
+       }       
+       return 1;
+    }
+
+    if($$line =~ /\#endif/ ||
+       $$line =~ /\#else/ ||
+       $$line =~ /\#if/
+       ){
+       $$line =$$line."//__ASTYLECOMMENT__\n";
+       return 1;
+    }
+
+    return 1;
+}
+
+sub output_filter{
+    my($line)=@_;
+
+    if($$line =~ /^\s*$/){
+       return 1;
+    }
+
+    if($$line =~ s/\s*\/\/__ASTYLECOMMENT__//) {
+       chomp($$line);
+    }
+    
+   # "The "unsigned int:1; case ....."
+   $$line =~ s/__FORASTYLE__/:/;
+
+   return 1;
+}
+
+sub usage{
+    my($name)=@_;
+    print "Usage:\n   $name file1 file2 file3 ....\n";
+}
diff --git a/scripts/md5checker.sh b/scripts/md5checker.sh
new file mode 100755 (executable)
index 0000000..bcd7dd0
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/bash
+# A checker
+
+
+for FILENAME in $*; do
+
+    if test -e $FILENAME -a -e "$FILENAME.astylebak"; then
+       md51=`cat  $FILENAME| tr -d "\n \t\r" | md5sum|sed 's/  -//'`;
+       md52=`cat  $FILENAME.astylebak| tr -d "\n \t\r" | md5sum|sed 's/  -//'`;
+       
+       if test $md51 != $md52; then
+           echo "File $FILENAME not converted well";
+       fi
+    else
+       echo "can not check file: $FILENAME";
+    fi
+
+done
+
+
+
+
+