ambevar-dotfiles/.scripts/prettyc

48 lines
763 B
Bash
Executable File

#!/bin/sh
_printhelp()
{
cat<<EOF
Usage: ${0##*/} FILES|FOLDERS
Prettify C source code using 'indent'.
Alternative to indent: astyle, uncrustify.
EOF
}
if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
_printhelp
exit
fi
if ! command -v indent >/dev/null 2>&1; then
echo >&2 "'indent' not found in PATH. Exiting."
exit 1
fi
CMD="indent -i4 -ppi4 -bli0 -cli4 -nut"
_formatc_dir()
{
## Note that '+' does not work together with {} concatenation. Don't know
## why...
find "$1" -type f \
-name "*.[ch]" \
-print \
-exec $CMD {} \; \
-exec rm {}"~" \;
}
for i ; do
[ ! -e "$i" ] && continue
if [ -d "$i" ]; then
_formatc_dir "$i"
else
$CMD "$i"
rm -f "$i~"
fi
done