3 # Clean a patch file -- or directory of patch files -- of stealth whitespace.
4 # WARNING: this can be a highly destructive operation. Use with caution.
14 # Clean up space-tab sequences, either by removing spaces or
15 # replacing them with tabs.
16 sub clean_space_tabs
($)
18 no bytes
; # Tab alignment depends on characters
26 for ($i = 0; $i < length($li); $i++) {
27 $c = substr($li, $i, 1);
29 my $npos = ($pos+$nsp+8) & ~7;
30 my $ntab = ($npos >> 3) - ($pos >> 3);
34 } elsif ($c eq "\n" || $c eq "\r") {
54 # Compute the visual width of a string
56 no bytes
; # Tab alignment depends on characters
63 for ($i = 0; $i < length($li); $i++) {
64 $c = substr($li,$i,1);
67 } elsif ($c eq "\n") {
68 $mlen = $pos if ($pos > $mlen);
75 $mlen = $pos if ($pos > $mlen);
83 while (defined($a = shift(@ARGV))) {
85 if ($a eq '-width' || $a eq '-w') {
86 $max_width = shift(@ARGV)+0;
88 print STDERR
"Usage: $name [-width #] files...\n";
96 foreach $f ( @files ) {
97 print STDERR
"$name: $f\n";
100 print STDERR
"$f: not a file\n";
104 if (!open(FILE
, '+<', $f)) {
105 print STDERR
"$name: Cannot open file: $f: $!\n";
111 # First, verify that it is not a binary file; consider any file
112 # with a zero byte to be a binary file. Is there any better, or
113 # additional, heuristic that should be applied?
116 while (read(FILE
, $data, 65536) > 0) {
124 print STDERR
"$name: $f: binary file\n";
139 while ( defined($line = <FILE
>) ) {
141 $in_bytes += length($line);
145 /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
148 if ($minus_lines || $plus_lines) {
150 @hunk_lines = ($line);
154 $out_bytes += length($line);
159 if ($line =~ /^\+/) {
162 $text = substr($line, 1);
163 $text =~ s/[ \t\r]*$//; # Remove trailing spaces
164 $text = clean_space_tabs
($text);
166 $l_width = strwidth
($text);
167 if ($max_width && $l_width > $max_width) {
169 "$f:$lineno: adds line exceeds $max_width ",
170 "characters ($l_width)\n";
173 push(@hunk_lines, '+'.$text);
174 } elsif ($line =~ /^\-/) {
176 push(@hunk_lines, $line);
177 } elsif ($line =~ /^ /) {
180 push(@hunk_lines, $line);
182 print STDERR
"$name: $f: malformed patch\n";
187 if ($plus_lines < 0 || $minus_lines < 0) {
188 print STDERR
"$name: $f: malformed patch\n";
191 } elsif ($plus_lines == 0 && $minus_lines == 0) {
192 # End of a hunk. Process this hunk.
199 for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) {
200 $l = $hunk_lines[$i];
201 if (!$done && $l eq "+\n") {
202 $adj++; # Skip this line
203 } elsif ($l =~ /^[ +]/) {
211 $l = $hunk_lines[0]; # Hunk header
212 undef @hunk_lines; # Free memory
216 ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/);
221 my $tail = $5; # doesn't include the final newline
223 $l = sprintf("@@ -%d,%d +%d,%d @@%s\n",
224 $mstart, $mlin, $pstart, $plin-$adj,
229 # Transfer to the output array
231 $out_bytes += length($l);
241 print STDERR
"$name: $f: malformed patch\n";
246 if ($in_bytes != $out_bytes) {
247 # Only write to the file if changed
251 if ( !defined($where = tell(FILE
)) ||
252 !truncate(FILE
, $where) ) {
253 die "$name: Failed to truncate modified file: $f: $!\n";