1 #!/usr/bin/env perl 1 #!/usr/bin/env perl 2 # SPDX-License-Identifier: GPL-2.0 << 3 # 2 # 4 # Clean a patch file -- or directory of patch 3 # Clean a patch file -- or directory of patch files -- of stealth whitespace. 5 # WARNING: this can be a highly destructive op 4 # WARNING: this can be a highly destructive operation. Use with caution. 6 # 5 # 7 6 8 use warnings; 7 use warnings; 9 use bytes; 8 use bytes; 10 use File::Basename; 9 use File::Basename; 11 10 12 # Default options 11 # Default options 13 $max_width = 79; 12 $max_width = 79; 14 13 15 # Clean up space-tab sequences, either by remo 14 # Clean up space-tab sequences, either by removing spaces or 16 # replacing them with tabs. 15 # replacing them with tabs. 17 sub clean_space_tabs($) 16 sub clean_space_tabs($) 18 { 17 { 19 no bytes; # Tab alignmen 18 no bytes; # Tab alignment depends on characters 20 19 21 my($li) = @_; 20 my($li) = @_; 22 my($lo) = ''; 21 my($lo) = ''; 23 my $pos = 0; 22 my $pos = 0; 24 my $nsp = 0; 23 my $nsp = 0; 25 my($i, $c); 24 my($i, $c); 26 25 27 for ($i = 0; $i < length($li); $i++) { 26 for ($i = 0; $i < length($li); $i++) { 28 $c = substr($li, $i, 1); 27 $c = substr($li, $i, 1); 29 if ($c eq "\t") { 28 if ($c eq "\t") { 30 my $npos = ($pos+$nsp+8) & ~7; 29 my $npos = ($pos+$nsp+8) & ~7; 31 my $ntab = ($npos >> 3) - ($pos >> 30 my $ntab = ($npos >> 3) - ($pos >> 3); 32 $lo .= "\t" x $ntab; 31 $lo .= "\t" x $ntab; 33 $pos = $npos; 32 $pos = $npos; 34 $nsp = 0; 33 $nsp = 0; 35 } elsif ($c eq "\n" || $c eq "\r") { 34 } elsif ($c eq "\n" || $c eq "\r") { 36 $lo .= " " x $nsp; 35 $lo .= " " x $nsp; 37 $pos += $nsp; 36 $pos += $nsp; 38 $nsp = 0; 37 $nsp = 0; 39 $lo .= $c; 38 $lo .= $c; 40 $pos = 0; 39 $pos = 0; 41 } elsif ($c eq " ") { 40 } elsif ($c eq " ") { 42 $nsp++; 41 $nsp++; 43 } else { 42 } else { 44 $lo .= " " x $nsp; 43 $lo .= " " x $nsp; 45 $pos += $nsp; 44 $pos += $nsp; 46 $nsp = 0; 45 $nsp = 0; 47 $lo .= $c; 46 $lo .= $c; 48 $pos++; 47 $pos++; 49 } 48 } 50 } 49 } 51 $lo .= " " x $nsp; 50 $lo .= " " x $nsp; 52 return $lo; 51 return $lo; 53 } 52 } 54 53 55 # Compute the visual width of a string 54 # Compute the visual width of a string 56 sub strwidth($) { 55 sub strwidth($) { 57 no bytes; # Tab alignmen 56 no bytes; # Tab alignment depends on characters 58 57 59 my($li) = @_; 58 my($li) = @_; 60 my($c, $i); 59 my($c, $i); 61 my $pos = 0; 60 my $pos = 0; 62 my $mlen = 0; 61 my $mlen = 0; 63 62 64 for ($i = 0; $i < length($li); $i++) { 63 for ($i = 0; $i < length($li); $i++) { 65 $c = substr($li,$i,1); 64 $c = substr($li,$i,1); 66 if ($c eq "\t") { 65 if ($c eq "\t") { 67 $pos = ($pos+8) & ~7; 66 $pos = ($pos+8) & ~7; 68 } elsif ($c eq "\n") { 67 } elsif ($c eq "\n") { 69 $mlen = $pos if ($pos > $mlen); 68 $mlen = $pos if ($pos > $mlen); 70 $pos = 0; 69 $pos = 0; 71 } else { 70 } else { 72 $pos++; 71 $pos++; 73 } 72 } 74 } 73 } 75 74 76 $mlen = $pos if ($pos > $mlen); 75 $mlen = $pos if ($pos > $mlen); 77 return $mlen; 76 return $mlen; 78 } 77 } 79 78 80 $name = basename($0); 79 $name = basename($0); 81 80 82 @files = (); 81 @files = (); 83 82 84 while (defined($a = shift(@ARGV))) { 83 while (defined($a = shift(@ARGV))) { 85 if ($a =~ /^-/) { 84 if ($a =~ /^-/) { 86 if ($a eq '-width' || $a eq '-w') { 85 if ($a eq '-width' || $a eq '-w') { 87 $max_width = shift(@ARGV)+0; 86 $max_width = shift(@ARGV)+0; 88 } else { 87 } else { 89 print STDERR "Usage: $name [-width 88 print STDERR "Usage: $name [-width #] files...\n"; 90 exit 1; 89 exit 1; 91 } 90 } 92 } else { 91 } else { 93 push(@files, $a); 92 push(@files, $a); 94 } 93 } 95 } 94 } 96 95 97 foreach $f ( @files ) { 96 foreach $f ( @files ) { 98 print STDERR "$name: $f\n"; 97 print STDERR "$name: $f\n"; 99 98 100 if (! -f $f) { 99 if (! -f $f) { 101 print STDERR "$f: not a file\n"; 100 print STDERR "$f: not a file\n"; 102 next; 101 next; 103 } 102 } 104 103 105 if (!open(FILE, '+<', $f)) { 104 if (!open(FILE, '+<', $f)) { 106 print STDERR "$name: Cannot open file: 105 print STDERR "$name: Cannot open file: $f: $!\n"; 107 next; 106 next; 108 } 107 } 109 108 110 binmode FILE; 109 binmode FILE; 111 110 112 # First, verify that it is not a binary fi 111 # First, verify that it is not a binary file; consider any file 113 # with a zero byte to be a binary file. I 112 # with a zero byte to be a binary file. Is there any better, or 114 # additional, heuristic that should be app 113 # additional, heuristic that should be applied? 115 $is_binary = 0; 114 $is_binary = 0; 116 115 117 while (read(FILE, $data, 65536) > 0) { 116 while (read(FILE, $data, 65536) > 0) { 118 if ($data =~ /\0/) { 117 if ($data =~ /\0/) { 119 $is_binary = 1; 118 $is_binary = 1; 120 last; 119 last; 121 } 120 } 122 } 121 } 123 122 124 if ($is_binary) { 123 if ($is_binary) { 125 print STDERR "$name: $f: binary file\n 124 print STDERR "$name: $f: binary file\n"; 126 next; 125 next; 127 } 126 } 128 127 129 seek(FILE, 0, 0); 128 seek(FILE, 0, 0); 130 129 131 $in_bytes = 0; 130 $in_bytes = 0; 132 $out_bytes = 0; 131 $out_bytes = 0; 133 $lineno = 0; 132 $lineno = 0; 134 133 135 @lines = (); 134 @lines = (); 136 135 137 $in_hunk = 0; 136 $in_hunk = 0; 138 $err = 0; 137 $err = 0; 139 138 140 while ( defined($line = <FILE>) ) { 139 while ( defined($line = <FILE>) ) { 141 $lineno++; 140 $lineno++; 142 $in_bytes += length($line); 141 $in_bytes += length($line); 143 142 144 if (!$in_hunk) { 143 if (!$in_hunk) { 145 if ($line =~ 144 if ($line =~ 146 /^\@\@\s+\-([0-9]+),([0-9]+)\s 145 /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) { 147 $minus_lines = $2; 146 $minus_lines = $2; 148 $plus_lines = $4; 147 $plus_lines = $4; 149 if ($minus_lines || $plus_line 148 if ($minus_lines || $plus_lines) { 150 $in_hunk = 1; 149 $in_hunk = 1; 151 @hunk_lines = ($line); 150 @hunk_lines = ($line); 152 } 151 } 153 } else { 152 } else { 154 push(@lines, $line); 153 push(@lines, $line); 155 $out_bytes += length($line); 154 $out_bytes += length($line); 156 } 155 } 157 } else { 156 } else { 158 # We're in a hunk 157 # We're in a hunk 159 158 160 if ($line =~ /^\+/) { 159 if ($line =~ /^\+/) { 161 $plus_lines--; 160 $plus_lines--; 162 161 163 $text = substr($line, 1); 162 $text = substr($line, 1); 164 $text =~ s/[ \t\r]*$//; 163 $text =~ s/[ \t\r]*$//; # Remove trailing spaces 165 $text = clean_space_tabs($text 164 $text = clean_space_tabs($text); 166 165 167 $l_width = strwidth($text); 166 $l_width = strwidth($text); 168 if ($max_width && $l_width > $ 167 if ($max_width && $l_width > $max_width) { 169 print STDERR 168 print STDERR 170 "$f:$lineno: adds line 169 "$f:$lineno: adds line exceeds $max_width ", 171 "characters ($l_width) 170 "characters ($l_width)\n"; 172 } 171 } 173 172 174 push(@hunk_lines, '+'.$text); 173 push(@hunk_lines, '+'.$text); 175 } elsif ($line =~ /^\-/) { 174 } elsif ($line =~ /^\-/) { 176 $minus_lines--; 175 $minus_lines--; 177 push(@hunk_lines, $line); 176 push(@hunk_lines, $line); 178 } elsif ($line =~ /^ /) { 177 } elsif ($line =~ /^ /) { 179 $plus_lines--; 178 $plus_lines--; 180 $minus_lines--; 179 $minus_lines--; 181 push(@hunk_lines, $line); 180 push(@hunk_lines, $line); 182 } else { 181 } else { 183 print STDERR "$name: $f: malfo 182 print STDERR "$name: $f: malformed patch\n"; 184 $err = 1; 183 $err = 1; 185 last; 184 last; 186 } 185 } 187 186 188 if ($plus_lines < 0 || $minus_line 187 if ($plus_lines < 0 || $minus_lines < 0) { 189 print STDERR "$name: $f: malfo 188 print STDERR "$name: $f: malformed patch\n"; 190 $err = 1; 189 $err = 1; 191 last; 190 last; 192 } elsif ($plus_lines == 0 && $minu 191 } elsif ($plus_lines == 0 && $minus_lines == 0) { 193 # End of a hunk. Process this 192 # End of a hunk. Process this hunk. 194 my $i; 193 my $i; 195 my $l; 194 my $l; 196 my @h = (); 195 my @h = (); 197 my $adj = 0; 196 my $adj = 0; 198 my $done = 0; 197 my $done = 0; 199 198 200 for ($i = scalar(@hunk_lines)- 199 for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) { 201 $l = $hunk_lines[$i]; 200 $l = $hunk_lines[$i]; 202 if (!$done && $l eq "+\n") 201 if (!$done && $l eq "+\n") { 203 $adj++; # Skip this li 202 $adj++; # Skip this line 204 } elsif ($l =~ /^[ +]/) { 203 } elsif ($l =~ /^[ +]/) { 205 $done = 1; 204 $done = 1; 206 unshift(@h, $l); 205 unshift(@h, $l); 207 } else { 206 } else { 208 unshift(@h, $l); 207 unshift(@h, $l); 209 } 208 } 210 } 209 } 211 210 212 $l = $hunk_lines[0]; # Hunk h 211 $l = $hunk_lines[0]; # Hunk header 213 undef @hunk_lines; # Free m 212 undef @hunk_lines; # Free memory 214 213 215 if ($adj) { 214 if ($adj) { 216 die unless 215 die unless 217 ($l =~ /^\@\@\s+\-([0- 216 ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/); 218 my $mstart = $1; 217 my $mstart = $1; 219 my $mlin = $2; 218 my $mlin = $2; 220 my $pstart = $3; 219 my $pstart = $3; 221 my $plin = $4; 220 my $plin = $4; 222 my $tail = $5; # doesn't i 221 my $tail = $5; # doesn't include the final newline 223 222 224 $l = sprintf("@@ -%d,%d +% 223 $l = sprintf("@@ -%d,%d +%d,%d @@%s\n", 225 $mstart, $mli 224 $mstart, $mlin, $pstart, $plin-$adj, 226 $tail); 225 $tail); 227 } 226 } 228 unshift(@h, $l); 227 unshift(@h, $l); 229 228 230 # Transfer to the output array 229 # Transfer to the output array 231 foreach $l (@h) { 230 foreach $l (@h) { 232 $out_bytes += length($l); 231 $out_bytes += length($l); 233 push(@lines, $l); 232 push(@lines, $l); 234 } 233 } 235 234 236 $in_hunk = 0; 235 $in_hunk = 0; 237 } 236 } 238 } 237 } 239 } 238 } 240 239 241 if ($in_hunk) { 240 if ($in_hunk) { 242 print STDERR "$name: $f: malformed pat 241 print STDERR "$name: $f: malformed patch\n"; 243 $err = 1; 242 $err = 1; 244 } 243 } 245 244 246 if (!$err) { 245 if (!$err) { 247 if ($in_bytes != $out_bytes) { 246 if ($in_bytes != $out_bytes) { 248 # Only write to the file if change 247 # Only write to the file if changed 249 seek(FILE, 0, 0); 248 seek(FILE, 0, 0); 250 print FILE @lines; 249 print FILE @lines; 251 250 252 if ( !defined($where = tell(FILE)) 251 if ( !defined($where = tell(FILE)) || 253 !truncate(FILE, $where) ) { 252 !truncate(FILE, $where) ) { 254 die "$name: Failed to truncate 253 die "$name: Failed to truncate modified file: $f: $!\n"; 255 } 254 } 256 } 255 } 257 } 256 } 258 257 259 close(FILE); 258 close(FILE); 260 } 259 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.