PageRenderTime 15ms CodeModel.GetById 12ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/maint/whitespace-detect.in

#
Autoconf | 150 lines | 94 code | 25 blank | 31 comment | 22 complexity | 496d0fbfa35de28e846303ca32a39740 MD5 | raw file
  1#! @PERL@
  2# -*- perl -*-
  3
  4#
  5# whitespace-detect.in
  6#
  7# Copyright (C) 2008, 2009 Francesco Salvestrini
  8#
  9# This program is free software; you can redistribute it and/or modify
 10# it under the terms of the GNU General Public License as published by
 11# the Free Software Foundation; either version 2 of the License, or
 12# (at your option) any later version.
 13#
 14# This program is distributed in the hope that it will be useful,
 15# but WITHOUT ANY WARRANTY; without even the implied warranty of
 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17# GNU General Public License for more details.
 18#
 19# You should have received a copy of the GNU General Public License along
 20# with this program; if not, write to the Free Software Foundation, Inc.,
 21# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 22#
 23
 24use strict;
 25use warnings;
 26
 27my $program = "whitespace-detect";
 28my $version = "0.1";
 29
 30sub hint ($)
 31{
 32    my $string = shift;
 33
 34    if (defined($string)) {
 35	print "$program: " . $string . "\n";
 36    }
 37    print "Try `$program --help' for more information.\n";
 38}
 39
 40sub help ()
 41{
 42    print "Usage: $program [OPTION]... FILE...\n";
 43    print "\n";
 44    print "    Check input file trailing whitespaces.\n";
 45    print "\n";
 46    print "OPTIONS:\n";
 47    print "\n";
 48    print "    --help,    -h    display this help and exit\n";
 49    print "    --version, -v    output version information and exit\n";
 50    print "\n";
 51    print "Report bugs to <" . '@PACKAGE_BUGREPORT@' . ">\n";
 52}
 53
 54sub version ()
 55{
 56    print "$program $version\n";
 57    print "Written by Francesco Salvestrini.\n";
 58    print "\n";
 59    print "This is free software; see the source for copying conditions.  There is NO\n";
 60    print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
 61}
 62
 63if ($#ARGV < 0) {
 64    hint("Missing parameters");
 65    exit 1;
 66}
 67
 68if (($ARGV[0] eq "--help") ||
 69    ($ARGV[0] eq "-h")) {
 70    help();
 71    exit 0
 72}
 73
 74if (($ARGV[0] eq "--version") ||
 75    ($ARGV[0] eq "-v")) {
 76    version();
 77    exit 0;
 78}
 79
 80my $found = 0;
 81for my $i (0 .. $#ARGV) {
 82    my $filename;
 83    my $filehandle;
 84
 85    $filename = $ARGV[$i];
 86
 87    if (!open($filehandle, "<", $filename)) {
 88	print STDERR "$program: Cannot open \`" . $filename . "' for input\n";
 89	exit 1;
 90    }
 91
 92    my $lineno = 0;
 93    my $skip   = 0;
 94    while (<$filehandle>) {
 95        my $line = $_;
 96
 97        $lineno++;
 98
 99        # Check for continuation ending character '\'
100        if ($line =~ /\\$/) {
101
102            # Skip appended lines if previously found
103            if ($skip == 1) {
104                next;
105            }
106            my $tmp = $line;
107
108            # Clean up the string from non-quoting characters
109            $tmp =~ s/\\[\'\"]//g;
110            $tmp =~ s/[^\'\"]//g;
111
112            # Clean up balanced quoting characters
113            $tmp =~ s/\'\'//g;
114            $tmp =~ s/\"\"//g;
115
116            # If the string is empty the ending '\' appends
117            # the next line
118            if ($tmp eq "") {
119                $skip = 1;
120                next;
121            }
122        } else {
123            # This is the last appended line, skip to the next one
124            $skip = 0;
125            next;
126        }
127
128	if (($line =~ /^[ ]*[\t]+[ ]*/) && ($skip == 0)) {
129	    $found = 1;
130	    print STDERR
131		"$program: File \`" . $filename . "' " .
132		"contains initial tabs at line " . $lineno . "\n";
133
134	    #last;
135	}
136	if (($line =~ /[ \t]+$/) && ($skip == 0)) {
137	    $found = 1;
138	    print STDERR
139		"$program: File \`" . $filename . "' " .
140		"contains trailing whites at line " . $lineno . "\n";
141
142	    #last;
143	}
144    }
145
146    close($filehandle);
147}
148
149#exit $found;
150exit 0;