1#!/bin/sh -u 2# tools/style-check.sh -- for conformance or --fix to conform 3# Copyright (C) 2017 Olaf Meeuwissen 4# 5# License: GPL-3.0+ 6 7check_final_newline() { 8 test x = "x$(tail -c 1 $1)" 9} 10 11insert_final_newline() { 12 check_final_newline $1 || echo >> $1 13} 14 15check_trailing_whitespace() { 16 test -z "$(sed -n '/[ \t]$/{p;q}' $1)" 17} 18 19trim_trailing_whitespace() { 20 sed -i 's/[ \t]*$//' $1 21} 22 23check_trailing_blank_lines() { 24 test -z "$(sed -n '${/^$/s/^/blank/p}' $1)" 25} 26 27trim_trailing_blank_lines() { 28 sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $1 29} 30 31check_leading_blank_lines() { 32 test -z "$(sed -n '1{/^$/s/^/blank/p;q}' $1)" 33} 34 35trim_leading_blank_lines() { 36 sed -i '/./,$!d' $1 37} 38 39check_utf_8_charset() { 40 err=$(iconv -f utf-8 -t utf-8 < $1 2>&1 > /dev/null) 41 if test x != "x$err"; then 42 echo "charset not UTF-8: $1" >&2 43 echo "$err" >&2 44 return 1 45 fi 46} 47 48fix=false 49case $1 in 50 --fix) fix=true; shift;; 51esac 52 53status=0 54for file in "$@"; do 55 test -d $file && continue # skip directories, just in case 56 file=$(echo $file | sed 's,^\.\/,,') 57 case $file in 58 COPYING) ;; # hands off of the GPL 59 *.gif) ;; # don't touch image files 60 *.jpg) ;; 61 *.png) ;; 62 *.pnm) ;; 63 *.patch) ;; # patch output may have trailing lines or whitespace 64 Makefile.in) ;; # skip automake outputs 65 */Makefile.in) ;; 66 aclocal.m4) ;; # skip autoconf outputs 67 include/sane/config.h.in) ;; 68 m4/libtool.m4) ;; # courtesy of libtool 69 m4/lt~obsolete.m4) ;; 70 ABOUT-NLS) ;; # courtesy of gettext 71 doc/doxygen-*.conf.in) ;; # don't fix doxygen -g comments 72 73 *) 74 if `$fix`; then 75 trim_trailing_whitespace $file 76 insert_final_newline $file 77 trim_trailing_blank_lines $file 78 else 79 if ! check_trailing_whitespace $file; then 80 status=1 81 echo "trailing whitespace: $file" >&2 82 fi 83 if ! check_final_newline $file; then 84 status=1 85 echo "final newline missing: $file" >&2 86 fi 87 if ! check_trailing_blank_lines $file; then 88 status=1 89 echo "trailing blank lines: $file" >&2 90 fi 91 if ! check_utf_8_charset $file; then 92 status=1 93 fi 94 fi 95 ;; 96 esac 97done 98 99exit $status 100