cleanfile 3.4 KB

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