File: buff-expand

package info (click to toggle)
derivations 0.53.20120414-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,208 kB
  • ctags: 486
  • sloc: cpp: 1,634; perl: 600; makefile: 153; sh: 116
file content (121 lines) | stat: -rw-r--r-- 3,265 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /usr/bin/perl

# This Perl script buffs (removes trailing blanks from) and/or expands
# (converts tabs to spaces in) the files named on the command line.  It
# also ensures that each file (if not empty) ends in a proper "\n".

use warnings;
use strict;
use integer;

our $tab               =  8;
our $pat_makefile_name = qr/Makefile(?:[-.].*)?|rules/;
our $usage             = <<END;
usage: buff-expand [options] files
    -s  do not overwrite the files; output to stdout instead
    -B  do not buff
    -X  do not expand
    -L  do not expand leading tabs
    -M  do not expand leading tabs
          if the file basename is "Makefile" or "rules"
    --  do not process further options
    -h  print this usage message
END

my $process_opts    = 1;
my $stdout          = 0;
my $buff            = 1;
my $lead            = 1;
my $lead_makefile   = 1;
my $expand          = 1;
my @file = map {
  eval {
    if ( $process_opts and my ( $opts ) = /^-(.*)/ ) {
      my @opts = split '', $opts;
      @opts or $process_opts = 0;
      for ( @opts ) {
        $process_opts or next;
        $_ eq '-' and $process_opts = 0, next;
        $_ eq 's' and $stdout       = 1, next;
        $_ eq 'B' and $buff         = 0, next;
        $_ eq 'X' and $lead = 0, $lead_makefile = 0, $expand = 0, next;
        $_ eq 'L' and $lead = 0, $lead_makefile = 0,              next;
        $_ eq 'M' and            $lead_makefile = 0,              next;
        $_ eq 'h' || $_ eq '?' and print($usage), exit(0);
        print( STDERR $usage ), exit(1);
      }
      return ();
    }
    else {
      return ( $_ );
    }
  }
} @ARGV;

sub buff_expand ($\$@) {
  my $is_makefile = shift;
  my $has_acted   = shift;
  my @ret = map {
    chomp( my $a = $_ );
    if (
      (  $is_makefile && $lead_makefile ) ||
      ( !$is_makefile && $lead          )
    ) { s/^\t/ {$tab}/ }
    if ( $expand ) {
      my $b = '';
      my $t = '';
      if ( my @b = split /\t/, $a ) {
        $b  = shift @b;
        if ( # if there is a leading tab we should preserve, then...
          !length($b) && !(
            (  $is_makefile && $lead_makefile ) ||
            ( !$is_makefile && $lead          )
          )
        ) {
          $t  = "\t";
          $b .= shift @b if @b;
        }
        $b .= ' ' x ( $tab - length( $b ) % $tab ) . $_ for @b;
      }
      $a = $t . $b;
    }
    if ( $buff ) {
      $a =~ s/\s*$//;
    }
    "$a\n";
  } @_;
  if ( @ret != @_ ) { $$has_acted = 1 }
  else {
    for my $i ( 0 .. $#_ ) {
      $ret[$i] eq $_[$i] or $$has_acted = 1, last;
    }
  }
  return wantarray ? @ret : $ret[0];
}

if ( @file ) {
  for my $file ( @file ) {
    my $is_makefile = $file =~ /(?:^|\/)${pat_makefile_name}$/;
    open  FILE, '<', $file
      or warn( "$0: cannot read $file\n" ), next;
    my @line          = <FILE>;
    close FILE;
    my $has_acted     = 0;
    my @buff_expanded = buff_expand $is_makefile, $has_acted, @line;
    if    ( $stdout    ) {
      print @buff_expanded;
    }
    elsif ( $has_acted ) {
      open  FILE, '>', $file
        or warn( "$0: cannot write $file\n" ), next;
      print FILE @buff_expanded;
      close FILE;
    }
  }
}
else {
  my @line      = <>;
  my $has_acted = 0;
  print buff_expand 0, $has_acted, @line;
}