cleanfile 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #
  9. # Clean up space-tab sequences, either by removing spaces or
  10. # replacing them with tabs.
  11. sub clean_space_tabs($)
  12. {
  13. no bytes; # Tab alignment depends on characters
  14. my($li) = @_;
  15. my($lo) = '';
  16. my $pos = 0;
  17. my $nsp = 0;
  18. my($i, $c);
  19. for ($i = 0; $i < length($li); $i++) {
  20. $c = substr($li, $i, 1);
  21. if ($c eq "\t") {
  22. my $npos = ($pos+$nsp+8) & ~7;
  23. my $ntab = ($npos >> 3) - ($pos >> 3);
  24. $lo .= "\t" x $ntab;
  25. $pos = $npos;
  26. $nsp = 0;
  27. } elsif ($c eq "\n" || $c eq "\r") {
  28. $lo .= " " x $nsp;
  29. $pos += $nsp;
  30. $nsp = 0;
  31. $lo .= $c;
  32. $pos = 0;
  33. } elsif ($c eq " ") {
  34. $nsp++;
  35. } else {
  36. $lo .= " " x $nsp;
  37. $pos += $nsp;
  38. $nsp = 0;
  39. $lo .= $c;
  40. $pos++;
  41. }
  42. }
  43. $lo .= " " x $nsp;
  44. return $lo;
  45. }
  46. $name = basename($0);
  47. foreach $f ( @ARGV ) {
  48. print STDERR "$name: $f\n";
  49. if (! -f $f) {
  50. print STDERR "$f: not a file\n";
  51. next;
  52. }
  53. if (!open(FILE, '+<', $f)) {
  54. print STDERR "$name: Cannot open file: $f: $!\n";
  55. next;
  56. }
  57. binmode FILE;
  58. # First, verify that it is not a binary file; consider any file
  59. # with a zero byte to be a binary file. Is there any better, or
  60. # additional, heuristic that should be applied?
  61. $is_binary = 0;
  62. while (read(FILE, $data, 65536) > 0) {
  63. if ($data =~ /\0/) {
  64. $is_binary = 1;
  65. last;
  66. }
  67. }
  68. if ($is_binary) {
  69. print STDERR "$name: $f: binary file\n";
  70. next;
  71. }
  72. seek(FILE, 0, 0);
  73. $in_bytes = 0;
  74. $out_bytes = 0;
  75. $blank_bytes = 0;
  76. @blanks = ();
  77. @lines = ();
  78. while ( defined($line = <FILE>) ) {
  79. $in_bytes += length($line);
  80. $line =~ s/[ \t\r]*$//; # Remove trailing spaces
  81. $line = clean_space_tabs($line);
  82. if ( $line eq "\n" ) {
  83. push(@blanks, $line);
  84. $blank_bytes += length($line);
  85. } else {
  86. push(@lines, @blanks);
  87. $out_bytes += $blank_bytes;
  88. push(@lines, $line);
  89. $out_bytes += length($line);
  90. @blanks = ();
  91. $blank_bytes = 0;
  92. }
  93. }
  94. # Any blanks at the end of the file are discarded
  95. if ($in_bytes != $out_bytes) {
  96. # Only write to the file if changed
  97. seek(FILE, 0, 0);
  98. print FILE @lines;
  99. if ( !defined($where = tell(FILE)) ||
  100. !truncate(FILE, $where) ) {
  101. die "$name: Failed to truncate modified file: $f: $!\n";
  102. }
  103. }
  104. close(FILE);
  105. }