aboutsummaryrefslogtreecommitdiffstats
path: root/Tools/scripts/indent_make_if.pl
blob: 7e2758c0606acab3ba95579890e02e563cfaa8c0 (plain) (blame)
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
#!/usr/bin/env perl
# $FreeBSD$
# perltidy -bext=/ -i=8 -et=8 -l=132 -pt=2 -ce -cti=1

use strict;
use utf8;
use warnings;

my $extension = '.orig';
my $oldargv   = q{};
my $spaces    = 2;
my $indent;
my $argvout;

sub dotindent {
    my $amount = shift;
    return '.' . (' ' x ($spaces * $amount));
}

LINE: while (<>) {

    # For each file, save a .orig backup.
    if ($ARGV ne $oldargv) {
        my $backup;
        if ($extension !~ /[*]/) {
            $backup = $ARGV . $extension;
        } else {
            ($backup = $extension) =~ s/[*]/$ARGV/g;
        }
        rename $ARGV, $backup;
        open $argvout, '>', $ARGV or die "Error for $ARGV: $!";
        $oldargv = $ARGV;
        $indent  = 0;
    }

    if (/^[.]\s*(?:if|for)/o) {    # if/for -> indent and increase indent
        s/^[.]\s*/dotindent($indent)/oe;
        $indent++;
    } elsif (/^[.]\s*end(?:if|for)/o) {    # endif/endfor -> decrease indent and indent
        $indent--;
        s/^[.]\s*/dotindent($indent)/oe;
    } elsif (/^[.]\s*(?:else|elif)/o) {    # else/elif -> indent one level down
        s/^[.]\s*/dotindent($indent-1)/oe;
    }
} continue {

    # Print the line.
    print {$argvout} $_;
}