chkfmt 4.1 KB

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