chkfmt 3.9 KB

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