PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/maint/whitespace-detect.in

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