summaryrefslogtreecommitdiff
path: root/scripts/cleanup-whitespace.sh
blob: bfab7410203fc4810d5dc6cd94680ef0a46b8ec3 (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
#!/bin/bash

# Replace TABs with sequences of 8 spaces in all given files.
replace_tabs() {
    local _file _tmp

    for _file in $*; do
        _tmp=$_file.tabs
        cp $_file $_tmp && \
            cat $_tmp | \
                sed 's/\t/        /g' > $_file && \
        rm -f $_tmp
    done
}


# Replaces lines containing only spaces with empty lines in all given files.
strip_empty_lines() {
    local _file _tmp

    for _file in $*; do
        _tmp=$_file.spaces
        cp $_file $_tmp && \
            cat $_tmp | \
                sed 's/^ [ ]*$//g' > $_file && \
        rm -f $_tmp
    done
}


# Strip trailing white space from all the given files.
strip_trailing_ws() {
    local _file _tmp

    for _file in $*; do
        _tmp=$_file.spaces
        cp $_file $_tmp && \
            cat $_tmp | \
                sed 's/ *$//g' > $_file && \
        rm -f $_tmp
    done
}


# Clean up TABS and empty lines in all given or found files.
if [ -n "$*" ]; then
    replace_tabs $* && \
        strip_empty_lines $* && \
            strip_trailing_ws $*
else
    files=$(find . -name \*.h -o -name \*.c)
    replace_tabs $files && \
        strip_empty_lines $files && \
            strip_trailing_ws $files
fi