aboutsummaryrefslogtreecommitdiff
path: root/util/style.sh
blob: dfef6e5db7422da455ad425e432d606baaf395e4 (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
#!/bin/sh


# Check args
if [ $# -lt 1 ]; then
    echo "error: specify file"
    exit 1
fi

if [ ! -f $1 ]; then
    echo "error: file '$1' not found"
    exit 1
fi

echo "Checking file" $1


# Message about trailing space before EOL
eolerrors=$(grep -n "\s$" "$1" | cut -f1 -d: | while read -r line; do
    if [ "$line" ]; then
        echo "$1:$line: error: whitepace before EOL"
    fi
done)


# Message about non-empty last-line
linecount=$(($(wc -l "$1" | xargs | cut -f1 -d " ") + 1))
lastline=$(tail -n "+$linecount" $1)

llerrors=$(if [ "$lastline" ]; then
    echo "$1:$linecount: error: no EOL before EOF"
fi)


# Compute ranges of new lines
count=0; start=0; next=0
lines="$(grep -n "^$" "$1" | cut -f1 -d:)
0"
ranges=$(echo "$lines" | while read -r line; do
    if [ "$line" ]; then
        if [ $start -eq 0 ]; then
            start="$line"; count=1; next=$(($start + 1))
        elif [ "$line" -ne $next ]; then
            echo $start $count
            start="$line"; count=1; next=$(($start + 1))
        else
            count=$(($count + 1)); next=$(($next + 1))
        fi
    fi
done)


# Check that we have no more then 3 EOLs in the row
fewerrors=$(echo "$ranges" | while read -r line; do
    number=$(echo $line | cut -f1 -d " ")
    size=$(echo $line | cut -f2 -d " ")
    if [ $size -gt 2 ]; then
        echo "$1:$number: error: too many EOLs"
    fi
done)

# Print out errors
code=0
if [ "$eolerrors" ]; then
    echo "$eolerrors" 1>&2
    code=1
fi

if [ "$llerrors" ]; then
    echo "$llerrors" 1>&2
    code=1
fi

if [ "$fewerrors" ]; then
    echo "$fewerrors" 1>&2
    code=1
fi

exit $code