]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - scripts/formater.pl
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / scripts / formater.pl
index 40a92a103254a5de6b72dd2977ee3ff8fe0b3965..7ae193419ceb3b69ac0474d76781223804a0f274 100755 (executable)
@@ -1,5 +1,12 @@
 #!/usr/bin/perl
 #
+## Copyright (C) 1996-2018 The Squid Software Foundation and contributors
+##
+## Squid software is distributed under GPLv2+ license and includes
+## contributions from numerous individuals and organizations.
+## Please see the COPYING and CONTRIBUTORS files for details.
+##
+#
 # Author: Tsantilas Christos
 # email:  christos@chtsanti.net
 #
@@ -7,7 +14,7 @@
 # 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,
+# 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.
 # 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 .
+# See COPYING or http://www.gnu.org/licenses/gpl.html for details.
 #
 
-
+use strict;
 use IPC::Open2;
 
 #
-# NP: The Squid code requires astyle version 1.22 or later
+# NP: The Squid code requires astyle version 2.04 (exactly for now)
 #
-#$ASTYLE_BIN="/usr/local/bin/astyle";
-$ASTYLE_BIN="/usr/bin/astyle";
-
-#$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
-$ASTYLE_ARGS ="--mode=c -s4 -O -l";
+my $ASTYLE_BIN="/usr/local/bin/astyle";
+if (! -x $ASTYLE_BIN) {
+  $ASTYLE_BIN="/usr/bin/astyle";
+}
+if (! -x $ASTYLE_BIN) {
+  $ASTYLE_BIN="/usr/local/src/astyle-2.04/bin/astyle";
+}
 
+my $ASTYLE_ARGS ="--mode=c -s4 --convert-tabs --keep-one-line-blocks --lineend=linux";
 #$ASTYLE_ARGS="--mode=c -s4 -O --break-blocks -l";
-#$ASTYLE_BIN="/usr/local/src/astyle-1.22/bin/astyle";
 
 
-if(! -e $ASTYLE_BIN){
+if(! -e $ASTYLE_BIN || ! -x $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 = "";
+my $INDENT = "";
 
-$out = shift @ARGV;
+my $out = shift @ARGV;
 #read options, currently no options available
 while($out eq "" ||  $out =~ /^-\w+$/){
    if($out eq "-h") {
@@ -66,7 +75,7 @@ while($out){
          next;
     }
 
-    $in= "$out.astylebak";
+    my $in= "$out.astylebak";
     my($new_in) = $in;
     my($i) = 0;
     while(-e $new_in) {
@@ -77,14 +86,14 @@ while($out){
     rename($out, $in);
     
     local (*FROM_ASTYLE, *TO_ASTYLE);
-    $pid_style=open2(\*FROM_ASTYLE, \*TO_ASTYLE, $ASTYLE_BIN);
+    my $pid_style=open2(\*FROM_ASTYLE, \*TO_ASTYLE, $ASTYLE_BIN);
     
     if(!$pid_style){
        print "An error while open2\n";
        exit -1;
     }
-    
-    
+
+    my $pid;
     if($pid=fork()){
        #do parrent staf
        close(FROM_ASTYLE);
@@ -111,7 +120,7 @@ while($out){
     else{
        # child staf
        close(TO_ASTYLE);
-       
+
        if(!open(OUT,">$out")){
            print "Can't open output file: $out\n";
            exit -1;
@@ -135,14 +144,13 @@ while($out){
     $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;
@@ -155,16 +163,18 @@ sub input_filter{
 
        if($$line =~ /(.*)\s*int\s+([^:]*):\s*(\w+)\s*\;(.*)/s){
 #          print ">>>>> ".$$line."    ($1)\n";
-            local($prx,$name,$val,$extra)=($1,$2,$3,$4);
+            my ($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;
+#          print ">>>>> ".$$line."    ($1)\n";
+            my ($name,$val,$extra)=($1,$2,$3);
+            my $prx =~ s/\s*$//g;
            $$line= "unsigned ".$name."__FORASTYLE__".$val.";".$extra;
-       }       
+#          print "----->".$$line."\n";
+       }
        return 1;
     }
 
@@ -179,11 +189,24 @@ sub input_filter{
     return 1;
 }
 
+my $last_line_was_empty=0;
+#param: a reference to input line
+#retval 1: print line
+#retval 0: don't print line
 sub output_filter{
     my($line)=@_;
 
+    # collapse multiple empty lines onto the first one
     if($$line =~ /^\s*$/){
-       return 1;
+      if ($last_line_was_empty==1) {
+        $$line="";
+        return 0;
+      } else {
+        $last_line_was_empty=1;
+        return 1;
+      }
+    } else {
+      $last_line_was_empty=0;
     }
 
     if($$line =~ s/\s*\/\/__ASTYLECOMMENT__//) {