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.
13 # Clean up space-tab sequences, either by removing spaces or
14 # replacing them with tabs.
15 sub clean_space_tabs($)
17 no bytes; # Tab alignment depends on characters
25 for ($i = 0; $i < length($li); $i++) {
26 $c = substr($li, $i, 1);
28 my $npos = ($pos+$nsp+8) & ~7;
29 my $ntab = ($npos >> 3) - ($pos >> 3);
33 } elsif ($c eq "\n" || $c eq "\r") {
53 # Compute the visual width of a string
55 no bytes; # Tab alignment depends on characters
62 for ($i = 0; $i < length($li); $i++) {
63 $c = substr($li,$i,1);
66 } elsif ($c eq "\n") {
67 $mlen = $pos if ($pos > $mlen);
74 $mlen = $pos if ($pos > $mlen);
82 while (defined($a = shift(@ARGV))) {
84 if ($a eq '-width' || $a eq '-w') {
85 $max_width = shift(@ARGV)+0;
87 print STDERR "Usage: $name [-width #] files...\n";
95 foreach $f ( @files ) {
96 print STDERR "$name: $f\n";
99 print STDERR "$f: not a file\n";
103 if (!open(FILE, '+<', $f)) {
104 print STDERR "$name: Cannot open file: $f: $!\n";
110 # First, verify that it is not a binary file; consider any file
111 # with a zero byte to be a binary file. Is there any better, or
112 # additional, heuristic that should be applied?
115 while (read(FILE, $data, 65536) > 0) {
123 print STDERR "$name: $f: binary file\n";
138 while ( defined($line = <FILE>) ) {
140 $in_bytes += length($line);
144 /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
147 if ($minus_lines || $plus_lines) {
149 @hunk_lines = ($line);
153 $out_bytes += length($line);
158 if ($line =~ /^\+/) {
161 $text = substr($line, 1);
162 $text =~ s/[ \t\r]*$//; # Remove trailing spaces
163 $text = clean_space_tabs($text);
165 $l_width = strwidth($text);
166 if ($max_width && $l_width > $max_width) {
168 "$f:$lineno: adds line exceeds $max_width ",
169 "characters ($l_width)\n";
172 push(@hunk_lines, '+'.$text);
173 } elsif ($line =~ /^\-/) {
175 push(@hunk_lines, $line);
176 } elsif ($line =~ /^ /) {
179 push(@hunk_lines, $line);
181 print STDERR "$name: $f: malformed patch\n";
186 if ($plus_lines < 0 || $minus_lines < 0) {
187 print STDERR "$name: $f: malformed patch\n";
190 } elsif ($plus_lines == 0 && $minus_lines == 0) {
191 # End of a hunk. Process this hunk.
198 for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) {
199 $l = $hunk_lines[$i];
200 if (!$done && $l eq "+\n") {
201 $adj++; # Skip this line
202 } elsif ($l =~ /^[ +]/) {
210 $l = $hunk_lines[0]; # Hunk header
211 undef @hunk_lines; # Free memory
215 ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/);
220 my $tail = $5; # doesn't include the final newline
222 $l = sprintf("@@ -%d,%d +%d,%d @@%s\n",
223 $mstart, $mlin, $pstart, $plin-$adj,
228 # Transfer to the output array
230 $out_bytes += length($l);
240 print STDERR "$name: $f: malformed patch\n";
245 if ($in_bytes != $out_bytes) {
246 # Only write to the file if changed
250 if ( !defined($where = tell(FILE)) ||
251 !truncate(FILE, $where) ) {
252 die "$name: Failed to truncate modified file: $f: $!\n";