Char: stallion, don't fail with less than max panels
[linux/fpc-iii.git] / scripts / cleanpatch
bloba53f987708f514a42e91a273a76dc629cbb185a5
1 #!/usr/bin/perl -w
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.
7 use bytes;
8 use File::Basename;
11 # Clean up space-tab sequences, either by removing spaces or
12 # replacing them with tabs.
13 sub clean_space_tabs($)
15 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);
23 for ($i = 0; $i < length($li); $i++) {
24 $c = substr($li, $i, 1);
25 if ($c eq "\t") {
26 my $npos = ($pos+$nsp+8) & ~7;
27 my $ntab = ($npos >> 3) - ($pos >> 3);
28 $lo .= "\t" x $ntab;
29 $pos = $npos;
30 $nsp = 0;
31 } elsif ($c eq "\n" || $c eq "\r") {
32 $lo .= " " x $nsp;
33 $pos += $nsp;
34 $nsp = 0;
35 $lo .= $c;
36 $pos = 0;
37 } elsif ($c eq " ") {
38 $nsp++;
39 } else {
40 $lo .= " " x $nsp;
41 $pos += $nsp;
42 $nsp = 0;
43 $lo .= $c;
44 $pos++;
47 $lo .= " " x $nsp;
48 return $lo;
51 $name = basename($0);
53 foreach $f ( @ARGV ) {
54 print STDERR "$name: $f\n";
56 if (! -f $f) {
57 print STDERR "$f: not a file\n";
58 next;
61 if (!open(FILE, '+<', $f)) {
62 print STDERR "$name: Cannot open file: $f: $!\n";
63 next;
66 binmode FILE;
68 # First, verify that it is not a binary file; consider any file
69 # with a zero byte to be a binary file. Is there any better, or
70 # additional, heuristic that should be applied?
71 $is_binary = 0;
73 while (read(FILE, $data, 65536) > 0) {
74 if ($data =~ /\0/) {
75 $is_binary = 1;
76 last;
80 if ($is_binary) {
81 print STDERR "$name: $f: binary file\n";
82 next;
85 seek(FILE, 0, 0);
87 $in_bytes = 0;
88 $out_bytes = 0;
90 @lines = ();
92 $in_hunk = 0;
93 $err = 0;
95 while ( defined($line = <FILE>) ) {
96 $in_bytes += length($line);
98 if (!$in_hunk) {
99 if ($line =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@/) {
100 $minus_lines = $2;
101 $plus_lines = $4;
102 if ($minus_lines || $plus_lines) {
103 $in_hunk = 1;
104 @hunk_lines = ($line);
106 } else {
107 push(@lines, $line);
108 $out_bytes += length($line);
110 } else {
111 # We're in a hunk
113 if ($line =~ /^\+/) {
114 $plus_lines--;
116 $text = substr($line, 1);
117 $text =~ s/[ \t\r]*$//; # Remove trailing spaces
118 $text = clean_space_tabs($text);
120 push(@hunk_lines, '+'.$text);
121 } elsif ($line =~ /^\-/) {
122 $minus_lines--;
123 push(@hunk_lines, $line);
124 } elsif ($line =~ /^ /) {
125 $plus_lines--;
126 $minus_lines--;
127 push(@hunk_lines, $line);
128 } else {
129 print STDERR "$name: $f: malformed patch\n";
130 $err = 1;
131 last;
134 if ($plus_lines < 0 || $minus_lines < 0) {
135 print STDERR "$name: $f: malformed patch\n";
136 $err = 1;
137 last;
138 } elsif ($plus_lines == 0 && $minus_lines == 0) {
139 # End of a hunk. Process this hunk.
140 my $i;
141 my $l;
142 my @h = ();
143 my $adj = 0;
144 my $done = 0;
146 for ($i = scalar(@hunk_lines)-1; $i > 0; $i--) {
147 $l = $hunk_lines[$i];
148 if (!$done && $l eq "+\n") {
149 $adj++; # Skip this line
150 } elsif ($l =~ /^[ +]/) {
151 $done = 1;
152 unshift(@h, $l);
153 } else {
154 unshift(@h, $l);
158 $l = $hunk_lines[0]; # Hunk header
159 undef @hunk_lines; # Free memory
161 if ($adj) {
162 die unless
163 ($l =~ /^\@\@\s+\-([0-9]+),([0-9]+)\s+\+([0-9]+),([0-9]+)\s\@\@(.*)$/);
164 my $mstart = $1;
165 my $mlin = $2;
166 my $pstart = $3;
167 my $plin = $4;
168 my $tail = $5; # doesn't include the final newline
170 $l = sprintf("@@ -%d,%d +%d,%d @@%s\n",
171 $mstart, $mlin, $pstart, $plin-$adj,
172 $tail);
174 unshift(@h, $l);
176 # Transfer to the output array
177 foreach $l (@h) {
178 $out_bytes += length($l);
179 push(@lines, $l);
182 $in_hunk = 0;
187 if ($in_hunk) {
188 print STDERR "$name: $f: malformed patch\n";
189 $err = 1;
192 if (!$err) {
193 if ($in_bytes != $out_bytes) {
194 # Only write to the file if changed
195 seek(FILE, 0, 0);
196 print FILE @lines;
198 if ( !defined($where = tell(FILE)) ||
199 !truncate(FILE, $where) ) {
200 die "$name: Failed to truncate modified file: $f: $!\n";
205 close(FILE);