1 | #!/bin/bash
|
---|
2 | # Whitespace fixer
|
---|
3 | # Usage: fix_ws [dir]...
|
---|
4 |
|
---|
5 | temp="/tmp/fix_ws.$$.$RANDOM"
|
---|
6 |
|
---|
7 | # Using $'xxx' bashism
|
---|
8 | begin8sp_tab=$'s/^ /\t/'
|
---|
9 | beginchar7sp_chartab=$'s/^\\([^ \t]\\) /\\1\t/'
|
---|
10 | tab8sp_tabtab=$'s/\t /\t\t/g'
|
---|
11 | tab8sptab_tabtabtab=$'s/\t \t/\t\t\t/g'
|
---|
12 | begin17sptab_tab=$'s/^ \\{1,7\\}\t/\t/'
|
---|
13 | tab17sptab_tabtab=$'s/\t \\{1,7\\}\t/\t\t/g'
|
---|
14 | trailingws_=$'s/[ \t]*$//'
|
---|
15 |
|
---|
16 | #>fix_ws.diff
|
---|
17 |
|
---|
18 | find "$@" -type f \
|
---|
19 | | while read name; do
|
---|
20 | test "YES" = "${name/*.bz2/YES}" && continue
|
---|
21 | test "YES" = "${name/*.gz/YES}" && continue
|
---|
22 | test "YES" = "${name/*.png/YES}" && continue
|
---|
23 | test "YES" = "${name/*.gif/YES}" && continue
|
---|
24 | test "YES" = "${name/*.jpg/YES}" && continue
|
---|
25 | test "YES" = "${name/*.diff/YES}" && continue
|
---|
26 | test "YES" = "${name/*.patch/YES}" && continue
|
---|
27 | # shell testsuite entries are not to be touched too
|
---|
28 | test "YES" = "${name/*.right/YES}" && continue
|
---|
29 |
|
---|
30 | if test "YES" = "${name/*.[chsS]/YES}" \
|
---|
31 | -o "YES" = "${name/*.sh/YES}" \
|
---|
32 | -o "YES" = "${name/*.txt/YES}" \
|
---|
33 | -o "YES" = "${name/*.html/YES}" \
|
---|
34 | -o "YES" = "${name/*.htm/YES}" \
|
---|
35 | -o "YES" = "${name/*Config.in*/YES}" \
|
---|
36 | ; then
|
---|
37 | # More aggressive whitespace fixes for known file types
|
---|
38 | echo "Formatting: $name" >&2
|
---|
39 | cat "$name" \
|
---|
40 | | sed -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
|
---|
41 | -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
|
---|
42 | | sed "$begin17sptab_tab" \
|
---|
43 | | sed -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
|
---|
44 | -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
|
---|
45 | -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
|
---|
46 | | sed "$trailingws_"
|
---|
47 | elif test "YES" = "${name/*Makefile*/YES}" \
|
---|
48 | -o "YES" = "${name/*Kbuild*/YES}" \
|
---|
49 | ; then
|
---|
50 | # For Makefiles, never convert "1-7spaces+tab" into "tabtab"
|
---|
51 | echo "Makefile: $name" >&2
|
---|
52 | cat "$name" \
|
---|
53 | | sed -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
|
---|
54 | -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
|
---|
55 | | sed -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
|
---|
56 | -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
|
---|
57 | -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
|
---|
58 | | sed "$trailingws_"
|
---|
59 | else
|
---|
60 | # Only remove trailing WS for the rest
|
---|
61 | echo "Removing trailing whitespace: $name" >&2
|
---|
62 | cat "$name" \
|
---|
63 | | sed "$trailingws_"
|
---|
64 | fi >"$temp"
|
---|
65 |
|
---|
66 | # diff -u "$temp" "$name" >>fix_ws.diff
|
---|
67 |
|
---|
68 | # Conserve mode/symlink:
|
---|
69 | cat "$temp" >"$name"
|
---|
70 | done
|
---|
71 | rm "$temp" 2>/dev/null
|
---|