aboutsummaryrefslogtreecommitdiff
path: root/models/openid-php-openid-782224d/admin/brace_style.pl
blob: ed8332f311523e9efc00e46c645f0687e2eb8f65 (plain)
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
#!/usr/bin/env perl -w

use strict;

my $filename = $ARGV[0];

if (!$filename) {
    print "Usage: modified_otb.pl <filename>\n";
    exit(1);
}

my @results = ();
my $line_num = 0;
my ($NONE, $BRACE, $PAREN) = (0, 1, 2);
my $looking_for = $NONE;
my $last_func_name = "";

open(HANDLE, "<", $filename) or die "Cannot open $filename\n";

# Read the file and track the lines with length > $max_length.
while (<HANDLE>) {
    $line_num++;
    # Subtract one because the newline doesn't count toward the
    # length.
    chomp;

    if (!$looking_for &&
        ($_ =~ /^\s*function/) &&
        ($_ =~ /\{/)) {
        # Done (bad): we found a function whose opening line ends with
        # a brace, which goes against the PEAR coding guidelines.

        ($last_func_name) = $_ =~ /function\s*(.*)\(/;

        push @results, "'$last_func_name' prototype ends with opening ".
            "brace, line $line_num";
    } elsif (!$looking_for &&
               ($_ =~ /^\s*function/) &&
               ($_ !~ /\)/)) {
        ($last_func_name) = $_ =~ /function\s*(.*)\(/;
        $looking_for = $PAREN;
    } elsif (($looking_for == $PAREN) &&
               ($_ =~ /\)/) &&
               ($_ =~ /\{/)) {
        # Done (bad): function prototype and brace are on the same
        # line.
        push @results, "'$last_func_name' prototype ends with with ".
            "opening brace, line $line_num";
        $looking_for = $NONE;
    } elsif (($looking_for == $PAREN) &&
               ($_ =~ /\)/) &&
               ($_ !~ /\{/)) {
        $looking_for = $BRACE;
    } elsif (!$looking_for &&
               ($_ =~ /^\s*function/) &&
               ($_ =~ /\)/) &&
               ($_ !~ /\{/)) {
        ($last_func_name) = $_ =~ /function\s*(.*)\(/;
        $looking_for = $BRACE;
    } elsif (($looking_for == $BRACE) &&
               ($_ eq "{")) {
        $looking_for = $NONE;
        # Done (good): the brace was found on the line after the
        # function prototype.
    } else {
        # We got here because we got a line that we're not interested
        # in.
        $looking_for = $NONE;
    }
}

# If any long lines were found, notify and exit(1); otherwise,
# exit(0).
if (@results) {
    foreach my $result (@results) {
        print "$filename: $result\n";
    }
    exit(1);
} else {
    exit(0);
}