Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports
Path: blob/main/Tools/scripts/indent_make_if.pl
18157 views
1
#!/usr/bin/env perl
2
# perltidy -bext=/ -i=8 -et=8 -l=132 -pt=2 -ce -cti=1
3
4
use strict;
5
use utf8;
6
use warnings;
7
8
my $extension = '.orig';
9
my $oldargv = q{};
10
my $spaces = 2;
11
my $indent;
12
my $argvout;
13
14
sub dotindent {
15
my $amount = shift;
16
return '.' . (' ' x ($spaces * $amount));
17
}
18
19
LINE: while (<>) {
20
21
# For each file, save a .orig backup.
22
if ($ARGV ne $oldargv) {
23
my $backup;
24
if ($extension !~ /[*]/) {
25
$backup = $ARGV . $extension;
26
} else {
27
($backup = $extension) =~ s/[*]/$ARGV/g;
28
}
29
rename $ARGV, $backup;
30
open $argvout, '>', $ARGV or die "Error for $ARGV: $!";
31
$oldargv = $ARGV;
32
$indent = 0;
33
}
34
35
if (/^[.]\s*(?:if|for)/o) { # if/for -> indent and increase indent
36
s/^[.]\s*/dotindent($indent)/oe;
37
$indent++;
38
} elsif (/^[.]\s*end(?:if|for)/o) { # endif/endfor -> decrease indent and indent
39
$indent--;
40
s/^[.]\s*/dotindent($indent)/oe;
41
} elsif (/^[.]\s*(?:else|elif)/o) { # else/elif -> indent one level down
42
s/^[.]\s*/dotindent($indent-1)/oe;
43
}
44
} continue {
45
46
# Print the line.
47
print {$argvout} $_;
48
}
49
50