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