1#!/bin/sh 2 3# chkfmt.sh 4# 5# COPYRIGHT: 6# Written by John Cunningham Bowler, 2010. 7# Revised by Cosmin Truta, 2022. 8# To the extent possible under law, the author has waived all copyright and 9# related or neighboring rights to this work. The author published this work 10# from the United States. 11# 12# Check the format of the source files in the current directory: 13# 14# * The lines should not exceed a predefined maximum length. 15# * Tab characters should appear only where necessary (e.g. in makefiles). 16# 17# Optionally arguments are files or directories to check. 18# 19# -v: output the long lines (makes fixing them easier) 20# -e: spawn an editor for each file that needs a change ($EDITOR must be 21# defined). When using -e the script MUST be run from an interactive 22# command line. 23 24script_name=`basename "$0"` 25 26verbose= 27edit= 28vers= 29test "$1" = "-v" && { 30 shift 31 verbose=yes 32} 33test "$1" = "-e" && { 34 shift 35 if test -n "$EDITOR" 36 then 37 edit=yes 38 39 # Copy the standard streams for the editor 40 exec 3>&0 4>&1 5>&2 41 else 42 echo "$script_name -e: EDITOR must be defined" >&2 43 exit 1 44 fi 45} 46 47# Function to edit a single file - if the file isn't changed ask the user 48# whether or not to continue. This stuff only works if the script is run 49# from the command line (otherwise, don't specify -e or you will be sorry). 50doed(){ 51 cp "$file" "$file".orig 52 "$EDITOR" "$file" 0>&3 1>&4 2>&5 3>&- 4>&- 5>&- || exit 1 53 if cmp -s "$file".orig "$file" 54 then 55 rm "$file".orig 56 echo -n "$file: file not changed, type anything to continue: " >&5 57 read ans 0>&3 58 test -n "$ans" || return 1 59 fi 60 return 0 61} 62 63# In beta versions, the version string which appears in files can be a little 64# long and cause spuriously overlong lines. To avoid this, substitute the 65# version string with a placeholder string "a.b.cc" before checking for long 66# lines. 67# (Starting from libpng version 1.6.36, we switched to a conventional Git 68# workflow, and we are no longer publishing beta versions.) 69if test -r png.h 70then 71 vers="`sed -n -e \ 72 's/^#define PNG_LIBPNG_VER_STRING .\([0-9]\.[0-9]\.[0-9][0-9a-z]*\).$/\1/p' \ 73 png.h`" 74 echo "$script_name: checking version $vers" 75fi 76if test -z "$vers" 77then 78 echo "$script_name: png.h not found, ignoring version number" >&2 79fi 80 81test -n "$1" || set -- . 82find "$@" \( -type d \( -name '.git' -o -name '.libs' -o -name 'projects' \) \ 83 -prune \) -o \( -type f \ 84 ! -name '*.[oa]' ! -name '*.l[oa]' ! -name '*.png' ! -name '*.out' \ 85 ! -name '*.jpg' ! -name '*.patch' ! -name '*.obj' ! -name '*.exe' \ 86 ! -name '*.com' ! -name '*.tar.*' ! -name '*.zip' ! -name '*.ico' \ 87 ! -name '*.res' ! -name '*.rc' ! -name '*.mms' ! -name '*.rej' \ 88 ! -name '*.dsp' ! -name '*.orig' ! -name '*.dfn' ! -name '*.swp' \ 89 ! -name '~*' ! -name '*.3' \ 90 ! -name 'missing' ! -name 'mkinstalldirs' ! -name 'depcomp' \ 91 ! -name 'aclocal.m4' ! -name 'install-sh' ! -name 'Makefile.in' \ 92 ! -name 'ltmain.sh' ! -name 'config*' -print \) | { 93 st=0 94 while read file 95 do 96 case "$file" in 97 *.mak|*[Mm]akefile.*|*[Mm]akefile) 98 # Makefiles require tabs, dependency lines can be this long. 99 check_tabs= 100 line_length=100;; 101 *.awk) 102 # Allow literal tabs. 103 check_tabs= 104 # Mainframe line printer, anyone? 105 line_length=132;; 106 */ci_*.sh) 107 check_tabs=yes 108 line_length=100;; 109 *contrib/*/*.[ch]) 110 check_tabs=yes 111 line_length=100;; 112 *) 113 check_tabs=yes 114 line_length=80;; 115 esac 116 117 # Note that vers can only contain 0-9, . and a-z 118 if test -n "$vers" 119 then 120 sed -e "s/$vers/a.b.cc/g" "$file" >"$file".$$ 121 else 122 cp "$file" "$file".$$ 123 fi 124 splt="`fold -$line_length "$file".$$ | diff -c "$file".$$ -`" 125 rm "$file".$$ 126 127 if test -n "$splt" 128 then 129 echo "$file: lines too long" 130 st=1 131 if test -n "$EDITOR" -a -n "$edit" 132 then 133 doed "$file" || exit 1 134 elif test -n "$verbose" 135 then 136 echo "$splt" 137 fi 138 fi 139 if test -n "$check_tabs" 140 then 141 tab="`tr -c -d '\t' <"$file"`" 142 if test -n "$tab" 143 then 144 echo "$file: file contains tab characters" 145 st=1 146 if test -n "$EDITOR" -a -n "$edit" 147 then 148 doed "$file" || exit 1 149 elif test -n "$verbose" 150 then 151 echo "$splt" 152 fi 153 fi 154 fi 155 done 156 exit $st 157} 158