1
0

cleanfile 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env perl
  2. #
  3. # Clean a text file -- or directory of text files -- of stealth whitespace.
  4. # WARNING: this can be a highly destructive operation. Use with caution.
  5. #
  6. use bytes;
  7. use File::Basename;
  8. use warnings;
  9. # Default options
  10. $max_width = 79;
  11. # Clean up space-tab sequences, either by removing spaces or
  12. # replacing them with tabs.
  13. sub clean_space_tabs($)
  14. {
  15. no bytes; # Tab alignment depends on characters
  16. my($li) = @_;
  17. my($lo) = '';
  18. my $pos = 0;
  19. my $nsp = 0;
  20. my($i, $c);
  21. for ($i = 0; $i < length($li); $i++) {
  22. $c = substr($li, $i, 1);
  23. if ($c eq "\t") {
  24. my $npos = ($pos+$nsp+8) & ~7;
  25. my $ntab = ($npos >> 3) - ($pos >> 3);
  26. $lo .= "\t" x $ntab;
  27. $pos = $npos;
  28. $nsp = 0;
  29. } elsif ($c eq "\n" || $c eq "\r") {
  30. $lo .= " " x $nsp;
  31. $pos += $nsp;
  32. $nsp = 0;
  33. $lo .= $c;
  34. $pos = 0;
  35. } elsif ($c eq " ") {
  36. $nsp++;
  37. } else {
  38. $lo .= " " x $nsp;
  39. $pos += $nsp;
  40. $nsp = 0;
  41. $lo .= $c;
  42. $pos++;
  43. }
  44. }
  45. $lo .= " " x $nsp;
  46. return $lo;
  47. }
  48. # Compute the visual width of a string
  49. sub strwidth($) {
  50. no bytes; # Tab alignment depends on characters
  51. my($li) = @_;
  52. my($c, $i);
  53. my $pos = 0;
  54. my $mlen = 0;
  55. for ($i = 0; $i < length($li); $i++) {
  56. $c = substr($li,$i,1);
  57. if ($c eq "\t") {
  58. $pos = ($pos+8) & ~7;
  59. } elsif ($c eq "\n") {
  60. $mlen = $pos if ($pos > $mlen);
  61. $pos = 0;
  62. } else {
  63. $pos++;
  64. }
  65. }
  66. $mlen = $pos if ($pos > $mlen);
  67. return $mlen;
  68. }
  69. $name = basename($0);
  70. @files = ();
  71. while (defined($a = shift(@ARGV))) {
  72. if ($a =~ /^-/) {
  73. if ($a eq '-width' || $a eq '-w') {
  74. $max_width = shift(@ARGV)+0;
  75. } else {
  76. print STDERR "Usage: $name [-width #] files...\n";
  77. exit 1;
  78. }
  79. } else {
  80. push(@files, $a);
  81. }
  82. }
  83. foreach $f ( @files ) {
  84. print STDERR "$name: $f\n";
  85. if (! -f $f) {
  86. print STDERR "$f: not a file\n";
  87. next;
  88. }
  89. if (!open(FILE, '+<', $f)) {
  90. print STDERR "$name: Cannot open file: $f: $!\n";
  91. next;
  92. }
  93. binmode FILE;
  94. # First, verify that it is not a binary file; consider any file
  95. # with a zero byte to be a binary file. Is there any better, or
  96. # additional, heuristic that should be applied?
  97. $is_binary = 0;
  98. while (read(FILE, $data, 65536) > 0) {
  99. if ($data =~ /\0/) {
  100. $is_binary = 1;
  101. last;
  102. }
  103. }
  104. if ($is_binary) {
  105. print STDERR "$name: $f: binary file\n";
  106. next;
  107. }
  108. seek(FILE, 0, 0);
  109. $in_bytes = 0;
  110. $out_bytes = 0;
  111. $blank_bytes = 0;
  112. @blanks = ();
  113. @lines = ();
  114. $lineno = 0;
  115. while ( defined($line = <FILE>) ) {
  116. $lineno++;
  117. $in_bytes += length($line);
  118. $line =~ s/[ \t\r]*$//; # Remove trailing spaces
  119. $line = clean_space_tabs($line);
  120. if ( $line eq "\n" ) {
  121. push(@blanks, $line);
  122. $blank_bytes += length($line);
  123. } else {
  124. push(@lines, @blanks);
  125. $out_bytes += $blank_bytes;
  126. push(@lines, $line);
  127. $out_bytes += length($line);
  128. @blanks = ();
  129. $blank_bytes = 0;
  130. }
  131. $l_width = strwidth($line);
  132. if ($max_width && $l_width > $max_width) {
  133. print STDERR
  134. "$f:$lineno: line exceeds $max_width characters ($l_width)\n";
  135. }
  136. }
  137. # Any blanks at the end of the file are discarded
  138. if ($in_bytes != $out_bytes) {
  139. # Only write to the file if changed
  140. seek(FILE, 0, 0);
  141. print FILE @lines;
  142. if ( !defined($where = tell(FILE)) ||
  143. !truncate(FILE, $where) ) {
  144. die "$name: Failed to truncate modified file: $f: $!\n";
  145. }
  146. }
  147. close(FILE);
  148. }