]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - scripts/cleanfile
3 # Clean a text file -- or directory of text 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";
138 while ( defined($line = <FILE
>) ) {
140 $in_bytes += length($line);
141 $line =~ s/[ \t\r]*$//; # Remove trailing spaces
142 $line = clean_space_tabs
($line);
144 if ( $line eq "\n" ) {
145 push(@blanks, $line);
146 $blank_bytes += length($line);
148 push(@lines, @blanks);
149 $out_bytes += $blank_bytes;
151 $out_bytes += length($line);
156 $l_width = strwidth
($line);
157 if ($max_width && $l_width > $max_width) {
159 "$f:$lineno: line exceeds $max_width characters ($l_width)\n";
163 # Any blanks at the end of the file are discarded
165 if ($in_bytes != $out_bytes) {
166 # Only write to the file if changed
170 if ( !defined($where = tell(FILE
)) ||
171 !truncate(FILE
, $where) ) {
172 die "$name: Failed to truncate modified file: $f: $!\n";