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