radare2/sys/indent.sh

106 lines
2.6 KiB
Bash
Raw Normal View History

#!/bin/sh
2015-12-08 10:25:12 +00:00
IFILE="$1"
if [ -z "${IFILE}" ]; then
echo "Usage: indent.sh [-i|-u] [file] [...]"
echo " -i indent in place (modify file)"
echo " -u unified diff of the file"
2015-12-08 10:25:12 +00:00
exit 1
fi
CWD="$PWD"
INPLACE=0
UNIFIED=0
ROOTDIR=/
2015-12-08 10:25:12 +00:00
if [ "${IFILE}" = "-i" ]; then
shift
INPLACE=1
IFILE="$1"
fi
if [ "${IFILE}" = "-u" ]; then
shift
UNIFIED=1
IFILE="$1"
fi
# yell, rather than overwrite an innocent file
if ! type clang-format >/dev/null; then
echo This script requires clang-format to function
exit 1
fi
indentFile() {
if [ ! -f "${IFILE}" ]; then
echo "Cannot find $IFILE"
return
fi
echo "Indenting ${IFILE} ..." > /dev/stderr
cp -f doc/clang-format ${CWD}/.clang-format
(
cd "$CWD"
clang-format "${IFILE}" > .tmp-format
# fix ternary conditional indent
perl -ne 's/ \? /? /g;print' < .tmp-format > .tmp-format2
cat .tmp-format2 | sed -e 's, : ,: ,g' > .tmp-format
mv .tmp-format .tmp-format2
# do not space before parenthesis on function signatures
awk '{if (/^static/ || /^R_API/) { gsub(/ \(/,"("); }; print;}' \
< .tmp-format2 > .tmp-format
# allow oneliner else statements
mv .tmp-format .tmp-format2
perl -ne 's/\telse\n[ \t]*/\telse /g;print' < .tmp-format2 | \
awk '{if (/\telse \t+/) {gsub(/\telse \t+/, "\telse ");} print;}' > .tmp-format
mv .tmp-format .tmp-format2
perl -ne 's/} else\n[ \t]*/} else /g;print' < .tmp-format2 | \
awk '{if (/} else \t+/) {gsub(/} else \t+/, "} else ");} print;}' > .tmp-format
2015-12-08 12:24:21 +00:00
# do not place spaces after tabs
mv .tmp-format .tmp-format2
perl -ne 's,\t[ ]+,\t,g;print' < .tmp-format2 > .tmp-format
2015-12-08 22:55:23 +00:00
# drop spaces an multiline backslashes
mv .tmp-format .tmp-format2
2015-12-08 23:27:31 +00:00
perl -ne 's/[ ]+\\$/\\/g;print' < .tmp-format2 > .tmp-format
2015-12-08 22:55:23 +00:00
# spaces in { brackets
mv .tmp-format .tmp-format2
#perl -ne 's/{\s/{ /g;print' < .tmp-format2 > .tmp-format
2015-12-08 23:41:44 +00:00
perl -ne 's/{([^ \n])/{ \1/g if(!/"/);print' < .tmp-format2 > .tmp-format
2015-12-08 22:55:23 +00:00
# spaces in } brackets
mv .tmp-format .tmp-format2
2015-12-08 23:41:44 +00:00
perl -ne 's/([^ \t])}/$1 }/g if(!/"/);print' < .tmp-format2 > .tmp-format
2015-12-08 22:55:23 +00:00
# _( macro
mv .tmp-format .tmp-format2
perl -ne 's/_\s\(/_(/g;print' < .tmp-format2 > .tmp-format
2015-12-08 23:27:31 +00:00
# 0xa0
mv .tmp-format .tmp-format2
perl -ne 's/[\xa0\xc2]//g;print' < .tmp-format2 > .tmp-format
if [ "$UNIFIED" = 1 ]; then
diff -ru "${IFILE}" .tmp-format
rm .tmp-format
elif [ "$INPLACE" = 1 ]; then
mv .tmp-format "${IFILE}"
else
cat .tmp-format
rm .tmp-format
fi
rm -f .tmp-format2
)
rm -f ${CWD}/.clang-format
}
2015-12-08 10:25:12 +00:00
while : ; do
[ "$PWD" = / ] && break
if [ -f doc/clang-format ]; then
ROOTDIR=$PWD
while : ; do
[ -z "${IFILE}" ] && break
indentFile
shift
IFILE="$1"
done
2015-12-08 10:25:12 +00:00
fi
cd ..
2015-12-08 10:25:12 +00:00
done