fukasawa e60969
#!/bin/sh
fukasawa e60969
#
fukasawa e60969
# Check the format of the source files in the current directory - checks for a
fukasawa e60969
# line length of 80 characters max and no tab characters.
fukasawa e60969
#
fukasawa e60969
# Optionally arguments are files or directories to check.
fukasawa e60969
#
fukasawa e60969
# -v: output the long lines (makes fixing them easier)
fukasawa e60969
# -e: spawn an editor for each file that needs a change ($EDITOR must be
fukasawa e60969
#     defined).  When using -e the script MUST be run from an interactive
fukasawa e60969
#     command line.
fukasawa e60969
verbose=
fukasawa e60969
edit=
fukasawa e60969
vers=
fukasawa e60969
test "$1" = "-v" && {
fukasawa e60969
   shift
fukasawa e60969
   verbose=yes
fukasawa e60969
}
fukasawa e60969
test "$1" = "-e" && {
fukasawa e60969
   shift
fukasawa e60969
   if test -n "$EDITOR"
fukasawa e60969
   then
fukasawa e60969
      edit=yes
fukasawa e60969
fukasawa e60969
      # Copy the standard streams for the editor
fukasawa e60969
      exec 3>&0 4>&1 5>&2
fukasawa e60969
   else
fukasawa e60969
      echo "chkfmt -e: EDITOR must be defined" >&2
fukasawa e60969
      exit 1
fukasawa e60969
   fi
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# Function to edit a single file - if the file isn't changed ask the user
fukasawa e60969
# whether or not to continue.  This stuff only works if the script is run from
fukasawa e60969
# the command line (otherwise, don't specify -e or you will be sorry).
fukasawa e60969
doed(){
fukasawa e60969
   cp "$file" "$file".orig
fukasawa e60969
   "$EDITOR" "$file" 0>&3 1>&4 2>&5 3>&- 4>&- 5>&- || exit 1
fukasawa e60969
   if cmp -s "$file".orig "$file"
fukasawa e60969
   then
fukasawa e60969
      rm "$file".orig
fukasawa e60969
      echo -n "$file: file not changed, type anything to continue: " >&5
fukasawa e60969
      read ans 0>&3
fukasawa e60969
      test -n "$ans" || return 1
fukasawa e60969
   fi
fukasawa e60969
   return 0
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
# In beta versions the version string which appears in files can be a little
fukasawa e60969
# long and cause spuriously overlong lines.  To avoid this subtitute the version
fukasawa e60969
# string with a 'standard' version a.b.cc before checking for long lines.
fukasawa e60969
if test -r png.h
fukasawa e60969
then
fukasawa e60969
   vers="`sed -n -e \
fukasawa e60969
   's/^#define PNG_LIBPNG_VER_STRING .\([0-9]\.[0-9]\.[0-9][0-9a-z]*\).$/\1/p' \
fukasawa e60969
   png.h`"
fukasawa e60969
   echo "chkfmt: checking version $vers"
fukasawa e60969
fi
fukasawa e60969
if test -z "$vers"
fukasawa e60969
then
fukasawa e60969
   echo "chkfmt: png.h not found, ignoring version number" >&2
fukasawa e60969
fi
fukasawa e60969
fukasawa e60969
test -n "$1" || set -- .
fukasawa e60969
find "$@" \( -type d \( -name '.git' -o -name '.libs' -o -name 'projects' \) \
fukasawa e60969
   -prune \) -o \( -type f \
fukasawa e60969
   ! -name '*.[oa]' ! -name '*.l[oa]' !  -name '*.png' ! -name '*.out' \
fukasawa e60969
   ! -name '*.jpg' ! -name '*.patch' ! -name '*.obj' ! -name '*.exe' \
fukasawa e60969
   ! -name '*.com' ! -name '*.tar.*' ! -name '*.zip' ! -name '*.ico' \
fukasawa e60969
   ! -name '*.res' ! -name '*.rc' ! -name '*.mms' ! -name '*.rej' \
fukasawa e60969
   ! -name '*.dsp' ! -name '*.orig' ! -name '*.dfn' ! -name '*.swp' \
fukasawa e60969
   ! -name '~*' ! -name '*.3' \
fukasawa e60969
   ! -name 'missing' ! -name 'mkinstalldirs' ! -name 'depcomp' \
fukasawa e60969
   ! -name 'aclocal.m4' ! -name 'install-sh' ! -name 'Makefile.in' \
fukasawa e60969
   ! -name 'ltmain.sh' ! -name 'config*' -print \) | {
fukasawa e60969
   st=0
fukasawa e60969
   while read file
fukasawa e60969
   do
fukasawa e60969
      case "$file" in
fukasawa e60969
      *.mak|*[Mm]akefile.*|*[Mm]akefile)
fukasawa e60969
         # Makefiles require tabs, dependency lines can be this long.
fukasawa e60969
         check_tabs=
fukasawa e60969
         line_length=100;;
fukasawa e60969
      *.awk)
fukasawa e60969
         # Includes literal tabs
fukasawa e60969
         check_tabs=
fukasawa e60969
         # The following is arbitrary
fukasawa e60969
         line_length=132;;
fukasawa e60969
      *contrib/*/*.[ch])
fukasawa e60969
         check_tabs=yes
fukasawa e60969
         line_length=96;;
fukasawa e60969
      *)
fukasawa e60969
         check_tabs=yes
fukasawa e60969
         line_length=80;;
fukasawa e60969
      esac
fukasawa e60969
fukasawa e60969
      # Note that vers can only contain 0-9, . and a-z
fukasawa e60969
      if test -n "$vers"
fukasawa e60969
      then
fukasawa e60969
         sed -e "s/$vers/a.b.cc/g" "$file" >"$file".$$
fukasawa e60969
      else
fukasawa e60969
         cp "$file" "$file".$$
fukasawa e60969
      fi
fukasawa e60969
      splt="`fold -$line_length "$file".$$ | diff -c "$file".$$ -`"
fukasawa e60969
      rm "$file".$$
fukasawa e60969
fukasawa e60969
      if test -n "$splt"
fukasawa e60969
      then
fukasawa e60969
         echo "$file: lines too long"
fukasawa e60969
         st=1
fukasawa e60969
         if test -n "$EDITOR" -a -n "$edit"
fukasawa e60969
         then
fukasawa e60969
            doed "$file" || exit 1
fukasawa e60969
         elif test -n "$verbose"
fukasawa e60969
         then
fukasawa e60969
            echo "$splt"
fukasawa e60969
         fi
fukasawa e60969
      fi
fukasawa e60969
      if test -n "$check_tabs"
fukasawa e60969
      then
fukasawa e60969
         tab="`tr -c -d '\t' <"$file"`"
fukasawa e60969
         if test -n "$tab"
fukasawa e60969
         then
fukasawa e60969
            echo "$file: file contains tab characters"
fukasawa e60969
            st=1
fukasawa e60969
            if test -n "$EDITOR" -a -n "$edit"
fukasawa e60969
            then
fukasawa e60969
               doed "$file" || exit 1
fukasawa e60969
            elif test -n "$verbose"
fukasawa e60969
            then
fukasawa e60969
               echo "$splt"
fukasawa e60969
            fi
fukasawa e60969
         fi
fukasawa e60969
      fi
fukasawa e60969
   done
fukasawa e60969
   exit $st
fukasawa e60969
}