cleanpatch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Clean a patch file -- or directory of patch 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. $lineno = 0;
  113. @lines = ();
  114. $in_hunk = 0;
  115. $err = 0;
  116. while ( defined($line = <FILE>) ) {
  117. $lineno++;
  118. $in_bytes += length($line);
  119. if (!$in_hunk) {
  120. if ($line =~
  121. /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
  122. $minus_lines = $2;
  123. $plus_lines = $4;
  124. if ($minus_lines || $plus_lines) {
  125. $in_hunk = 1;
  126. @hunk_lines = ($line);
  127. }
  128. } else {
  129. push(@lines, $line);
  130. $out_bytes += length($line);
  131. }
  132. } else {
  133. # We're in a hunk
  134. if ($line =~ /^\+/) {
  135. $plus_lines--;
  136. $text = substr($line, 1);
  137. $text =~ s/[ \t\r]*$//; # Remove trailing spaces
  138. $text = clean_space_tabs($text);
  139. $l_width = strwidth($text);
  140. if ($max_width && $l_width > $max_width) {
  141. print STDERR
  142. "$f:$lineno: adds line exceeds $max_width ",
  143. "characters ($l_width)\n";
  144. }
  145. push(@hunk_lines, '+'.$text);
  146. } elsif ($line =~ /^\-/) {
  147. $minus_lines--;
  148. push(@hunk_lines, $line);
  149. } elsif ($line =~ /^ /) {
  150. $plus_lines--;
  151. $minus_lines--;
  152. push(@hunk_lines, $line);
  153. } else {
  154. print STDERR "$name: $f: malformed patch\n";
  155. $err = 1;
  156. last;
  157. }
  158. if ($plus_lines < 0 || $minus_lines < 0) {
  159. print STDERR "$name: $f: malformed patch\n";
  160. $err = 1;
  161. last;
  162. } elsif ($plus_lines == 0 && $minus_lines == 0) {
  163. # End of a hunk. Process this hunk.
  164. my $i;
  165. my $l;
  166. my @h = ();
  167. my $adj = 0;
  168. my $done = 0;
  169. for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) {
  170. $l = $hunk_lines[$i];
  171. if (!$done && $l eq "+\n") {
  172. $adj++; # Skip this line
  173. } elsif ($l =~ /^[ +]/) {
  174. $done = 1;
  175. unshift(@h, $l);
  176. } else {
  177. unshift(@h, $l);
  178. }
  179. }
  180. $l = $hunk_lines[0]; # Hunk header
  181. undef @hunk_lines; # Free memory
  182. if ($adj) {
  183. die unless
  184. ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/);
  185. my $mstart = $1;
  186. my $mlin = $2;
  187. my $pstart = $3;
  188. my $plin = $4;
  189. my $tail = $5; # doesn't include the final newline
  190. $l = sprintf("@@ -%d,%d +%d,%d @@%s\n",
  191. $mstart, $mlin, $pstart, $plin-$adj,
  192. $tail);
  193. }
  194. unshift(@h, $l);
  195. # Transfer to the output array
  196. foreach $l (@h) {
  197. $out_bytes += length($l);
  198. push(@lines, $l);
  199. }
  200. $in_hunk = 0;
  201. }
  202. }
  203. }
  204. if ($in_hunk) {
  205. print STDERR "$name: $f: malformed patch\n";
  206. $err = 1;
  207. }
  208. if (!$err) {
  209. if ($in_bytes != $out_bytes) {
  210. # Only write to the file if changed
  211. seek(FILE, 0, 0);
  212. print FILE @lines;
  213. if ( !defined($where = tell(FILE)) ||
  214. !truncate(FILE, $where) ) {
  215. die "$name: Failed to truncate modified file: $f: $!\n";
  216. }
  217. }
  218. }
  219. close(FILE);
  220. }