| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# This Perl script buffs (removes trailing blanks from) and/or expands |
| 4 |
# (converts tabs to spaces in) the files named on the command line. It |
| 5 |
# also ensures that each file (if not empty) ends in a proper "\n". |
| 6 |
|
| 7 |
use warnings; |
| 8 |
use strict; |
| 9 |
use integer; |
| 10 |
|
| 11 |
our $tab = 8; |
| 12 |
our $pat_makefile_name = qr/Makefile|rules/; |
| 13 |
our $usage = <<END; |
| 14 |
usage: buff-expand [options] files |
| 15 |
-s do not overwrite the files; output to stdout instead |
| 16 |
-B do not buff |
| 17 |
-X do not expand |
| 18 |
-L do not expand leading tabs |
| 19 |
-M do not expand leading tabs |
| 20 |
if the file basename is "Makefile" or "rules" |
| 21 |
-- do not process further options |
| 22 |
-h print this usage message |
| 23 |
END |
| 24 |
|
| 25 |
my $process_opts = 1; |
| 26 |
my $stdout = 0; |
| 27 |
my $buff = 1; |
| 28 |
my $lead = 1; |
| 29 |
my $lead_makefile = 1; |
| 30 |
my $expand = 1; |
| 31 |
my @file = map { |
| 32 |
eval { |
| 33 |
if ( $process_opts and my ( $opts ) = /^-(.*)/ ) { |
| 34 |
my @opts = split '', $opts; |
| 35 |
@opts or $process_opts = 0; |
| 36 |
for ( @opts ) { |
| 37 |
$process_opts or next; |
| 38 |
$_ eq '-' and $process_opts = 0, next; |
| 39 |
$_ eq 's' and $stdout = 1, next; |
| 40 |
$_ eq 'B' and $buff = 0, next; |
| 41 |
$_ eq 'X' and $lead = 0, $lead_makefile = 0, $expand = 0, next; |
| 42 |
$_ eq 'L' and $lead = 0, $lead_makefile = 0, next; |
| 43 |
$_ eq 'M' and $lead_makefile = 0, next; |
| 44 |
$_ eq 'h' || $_ eq '?' and print($usage), exit(0); |
| 45 |
print( STDERR $usage ), exit(1); |
| 46 |
} |
| 47 |
return (); |
| 48 |
} |
| 49 |
else { |
| 50 |
return ( $_ ); |
| 51 |
} |
| 52 |
} |
| 53 |
} @ARGV; |
| 54 |
|
| 55 |
sub buff_expand ($\$@) { |
| 56 |
my $is_makefile = shift; |
| 57 |
my $has_acted = shift; |
| 58 |
my @ret = map { |
| 59 |
chomp( my $a = $_ ); |
| 60 |
if ( |
| 61 |
( $is_makefile && $lead_makefile ) || |
| 62 |
( !$is_makefile && $lead ) |
| 63 |
) { s/^\t/ {$tab}/ } |
| 64 |
if ( $expand ) { |
| 65 |
my $b = ''; |
| 66 |
my $t = ''; |
| 67 |
if ( my @b = split /\t/, $a ) { |
| 68 |
$b = shift @b; |
| 69 |
if ( # if there is a leading tab we should preserve, then... |
| 70 |
!length($b) && !( |
| 71 |
( $is_makefile && $lead_makefile ) || |
| 72 |
( !$is_makefile && $lead ) |
| 73 |
) |
| 74 |
) { |
| 75 |
$t = "\t"; |
| 76 |
$b .= shift @b if @b; |
| 77 |
} |
| 78 |
$b .= ' ' x ( $tab - length( $b ) % $tab ) . $_ for @b; |
| 79 |
} |
| 80 |
$a = $t . $b; |
| 81 |
} |
| 82 |
if ( $buff ) { |
| 83 |
$a =~ s/\s*$//; |
| 84 |
} |
| 85 |
"$a\n"; |
| 86 |
} @_; |
| 87 |
if ( @ret != @_ ) { $$has_acted = 1 } |
| 88 |
else { |
| 89 |
for my $i ( 0 .. $#_ ) { |
| 90 |
$ret[$i] eq $_[$i] or $$has_acted = 1, last; |
| 91 |
} |
| 92 |
} |
| 93 |
return wantarray ? @ret : $ret[0]; |
| 94 |
} |
| 95 |
|
| 96 |
if ( @file ) { |
| 97 |
for my $file ( @file ) { |
| 98 |
my $is_makefile = $file =~ /(?:^|\/)${pat_makefile_name}$/; |
| 99 |
open FILE, '<', $file |
| 100 |
or warn( "$0: cannot read $file\n" ), next; |
| 101 |
my @line = <FILE>; |
| 102 |
close FILE; |
| 103 |
my $has_acted = 0; |
| 104 |
my @buff_expanded = buff_expand $is_makefile, $has_acted, @line; |
| 105 |
if ( $stdout ) { |
| 106 |
print @buff_expanded; |
| 107 |
} |
| 108 |
elsif ( $has_acted ) { |
| 109 |
open FILE, '>', $file |
| 110 |
or warn( "$0: cannot write $file\n" ), next; |
| 111 |
print FILE @buff_expanded; |
| 112 |
close FILE; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
else { |
| 117 |
my @line = <>; |
| 118 |
my $has_acted = 0; |
| 119 |
print buff_expand 0, $has_acted, @line; |
| 120 |
} |
| 121 |
|