ambevar-dotfiles/.scripts/formatc

52 lines
800 B
Bash
Executable File

#!/bin/sh
_printhelp()
{
cat<<EOF
Usage: ${0##*/} FILES|FOLDERS
Format C source code using 'indent'.
Alternative to indent: astyle, uncrustify.
EOF
}
if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
_printhelp
exit
fi
if [ -z "$(command -v indent)" ]; then
echo "Please install 'indent'."
exit
fi
_formatc_dir()
{
find "$1" -type f \
-name "*.[ch]" \
-print \
-exec indent -i4 -ppi4 -bli0 -cli4 -nut {} \;
## Remove backup files.
find "$1" -type f \( \
-name "*.[ch]~" -o \
-name "*.[ch]~[0-9]*~" \) \
-delete
}
for i ; do
if [ ! -e "$i" ]; then
continue
fi
if [ -d "$i" ]; then
_formatc_dir "$i"
else
indent -i4 -ppi4 -bli0 -cli4 -nut "$i"
rm -f "$i~" "$i~[0-9]*~"
fi
done