cleanfile 3.5 KB

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