diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2020-03-30 21:30:45 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2020-03-30 21:30:45 +0200 |
commit | ee770c2346eb37e0dcb8b6cf3eaacf3d8efd6bbc (patch) | |
tree | 58f05092be1a17a939e861f8cadcda1b6ca2ecef /tools/style-check.sh | |
parent | 0da9e21872802cfc6e975b1ebaf9efb9e5934d84 (diff) | |
parent | fef76e17ed4c607ea73b81279f9ef1d7121be900 (diff) |
Merge branch 'release/experimental/1.0.29-1_experimental1'experimental/1.0.29-1_experimental1
Diffstat (limited to 'tools/style-check.sh')
-rwxr-xr-x | tools/style-check.sh | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tools/style-check.sh b/tools/style-check.sh new file mode 100755 index 0000000..479f7bb --- /dev/null +++ b/tools/style-check.sh @@ -0,0 +1,99 @@ +#!/bin/sh -u +# tools/style-check.sh -- for conformance or --fix to conform +# Copyright (C) 2017 Olaf Meeuwissen +# +# License: GPL-3.0+ + +check_final_newline() { + test x = "x$(tail -c 1 $1)" +} + +insert_final_newline() { + check_final_newline $1 || echo >> $1 +} + +check_trailing_whitespace() { + test -z "$(sed -n '/[ \t]$/{p;q}' $1)" +} + +trim_trailing_whitespace() { + sed -i 's/[ \t]*$//' $1 +} + +check_trailing_blank_lines() { + test -z "$(sed -n '${/^$/s/^/blank/p}' $1)" +} + +trim_trailing_blank_lines() { + sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $1 +} + +check_leading_blank_lines() { + test -z "$(sed -n '1{/^$/s/^/blank/p;q}' $1)" +} + +trim_leading_blank_lines() { + sed -i '/./,$!d' $1 +} + +check_utf_8_charset() { + err=$(iconv -f utf-8 -t utf-8 < $1 2>&1 > /dev/null) + if test x != "x$err"; then + echo "charset not UTF-8: $1" >&2 + echo "$err" >&2 + return 1 + fi +} + +fix=false +case $1 in + --fix) fix=true; shift;; +esac + +status=0 +for file in "$@"; do + test -d $file && continue # skip directories, just in case + file=$(echo $file | sed 's,^\.\/,,') + case $file in + COPYING) ;; # hands off of the GPL + *.gif) ;; # don't touch image files + *.jpg) ;; + *.png) ;; + *.pnm) ;; + *.patch) ;; # patch output may have trailing lines or whitespace + Makefile.in) ;; # skip automake outputs + */Makefile.in) ;; + aclocal.m4) ;; # skip autoconf outputs + include/sane/config.h.in) ;; + m4/libtool.m4) ;; # courtesy of libtool + m4/lt~obsolete.m4) ;; + ABOUT-NLS) ;; # courtesy of gettext + doc/doxygen-*.conf.in) ;; # don't fix doxygen -g comments + + *) + if `$fix`; then + trim_trailing_whitespace $file + insert_final_newline $file + trim_trailing_blank_lines $file + else + if ! check_trailing_whitespace $file; then + status=1 + echo "trailing whitespace: $file" >&2 + fi + if ! check_final_newline $file; then + status=1 + echo "final newline missing: $file" >&2 + fi + if ! check_trailing_blank_lines $file; then + status=1 + echo "trailing blank lines: $file" >&2 + fi + if ! check_utf_8_charset $file; then + status=1 + fi + fi + ;; + esac +done + +exit $status |