summaryrefslogtreecommitdiff
path: root/scripts/cleanup-whitespace.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/cleanup-whitespace.sh')
-rwxr-xr-xscripts/cleanup-whitespace.sh55
1 files changed, 55 insertions, 0 deletions
diff --git a/scripts/cleanup-whitespace.sh b/scripts/cleanup-whitespace.sh
new file mode 100755
index 0000000..bfab741
--- /dev/null
+++ b/scripts/cleanup-whitespace.sh
@@ -0,0 +1,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