fix_ws.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/bash
  2. # Whitespace fixer
  3. # Usage: fix_ws [dir]...
  4. temp="/tmp/fix_ws.$$.$RANDOM"
  5. # Using $'xxx' bashism
  6. begin8sp_tab=$'s/^ /\t/'
  7. beginchar7sp_chartab=$'s/^\\([^ \t]\\) /\\1\t/'
  8. tab8sp_tabtab=$'s/\t /\t\t/g'
  9. tab8sptab_tabtabtab=$'s/\t \t/\t\t\t/g'
  10. begin17sptab_tab=$'s/^ \\{1,7\\}\t/\t/'
  11. tab17sptab_tabtab=$'s/\t \\{1,7\\}\t/\t\t/g'
  12. trailingws_=$'s/[ \t]*$//'
  13. #>fix_ws.diff
  14. find "$@" -type f \
  15. | while read name; do
  16. test "YES" = "${name/*.bz2/YES}" && continue
  17. test "YES" = "${name/*.gz/YES}" && continue
  18. test "YES" = "${name/*.png/YES}" && continue
  19. test "YES" = "${name/*.gif/YES}" && continue
  20. test "YES" = "${name/*.jpg/YES}" && continue
  21. test "YES" = "${name/*.diff/YES}" && continue
  22. test "YES" = "${name/*.patch/YES}" && continue
  23. # shell testsuite entries are not to be touched too
  24. test "YES" = "${name/*.right/YES}" && continue
  25. if test "YES" = "${name/*.[chsS]/YES}" \
  26. -o "YES" = "${name/*.sh/YES}" \
  27. -o "YES" = "${name/*.txt/YES}" \
  28. -o "YES" = "${name/*.html/YES}" \
  29. -o "YES" = "${name/*.htm/YES}" \
  30. -o "YES" = "${name/*Config.in*/YES}" \
  31. ; then
  32. # More aggressive whitespace fixes for known file types
  33. echo "Formatting: $name" >&2
  34. cat "$name" \
  35. | sed -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
  36. -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
  37. | sed "$begin17sptab_tab" \
  38. | sed -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
  39. -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
  40. -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
  41. | sed "$trailingws_"
  42. elif test "YES" = "${name/*Makefile*/YES}" \
  43. -o "YES" = "${name/*Kbuild*/YES}" \
  44. ; then
  45. # For Makefiles, never convert "1-7spaces+tab" into "tabtab"
  46. echo "Makefile: $name" >&2
  47. cat "$name" \
  48. | sed -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
  49. -e "$tab8sptab_tabtabtab" -e "$tab8sptab_tabtabtab" \
  50. | sed -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
  51. -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
  52. -e "$tab17sptab_tabtab" -e "$tab17sptab_tabtab" \
  53. | sed "$trailingws_"
  54. else
  55. # Only remove trailing WS for the rest
  56. echo "Removing trailing whitespace: $name" >&2
  57. cat "$name" \
  58. | sed "$trailingws_"
  59. fi >"$temp"
  60. # diff -u "$temp" "$name" >>fix_ws.diff
  61. # Conserve mode/symlink:
  62. cat "$temp" >"$name"
  63. done
  64. rm "$temp" 2>/dev/null