Mamfile_indent 758 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env sh
  2. IFS=''; set -fCu # safe mode: no split/glob = no quoting headaches
  3. let() { return $((!($1))); }
  4. # Automatically (re-)indent make...done blocks in a Mamfile.
  5. # Usage: Mamfile_indent <Mamfile >Mamfile.new
  6. #
  7. # Should work on all current POSIX compliant shells.
  8. # By Martijn Dekker <martijn@inlv.org>, 2021. Public domain.
  9. # Spacing per indentation level. Edit to change style.
  10. indent=' ' # one tab
  11. # Remove existing indentation, add new indentation.
  12. indentlvl=0
  13. sed 's/^[[:space:]]*//' \
  14. | while read -r line
  15. do case $line in
  16. '') continue ;;
  17. done*) let "indentlvl -= 1" ;;
  18. esac
  19. spc=
  20. i=0
  21. while let "(i += 1) <= indentlvl"
  22. do spc=$indent$spc
  23. done
  24. printf '%s\n' $spc$line
  25. case $line in
  26. make*) let "indentlvl += 1" ;;
  27. esac
  28. done