ambevar-dotfiles/.scripts/formatc

51 lines
836 B
Bash
Executable File

#!/bin/sh
if [ "$1" = "-h" ]; then
cat<<EOF
Usage: ${0##*/} [FILES|FOLDERS]
Format C source code using 'indent'.
Alternative to indent: astyle, uncrustify.
EOF
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
}
if [ $# -eq 0 ]; then
echo "Working on current dir"
_formatc_dir "$PWD"
exit
fi
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