CHK_C_LIST="include/seccomp.h.in \
include/seccomp-syscalls.h \
src/*.c src/*.h \
tests/*.c tests/*.h \
tools/*.c tools/*.h"
CHK_C_EXCLUDE=""
function verify_deps() {
[[ -z "$1" ]] && return
if ! which "$1" >& /dev/null; then
echo "error: install \"$1\" and include it in your \$PATH"
exit 1
fi
}
function usage() {
cat << EOF
usage: check-syntax [-h]
libseccomp code syntax checking tool
optional arguments:
-h show this help message and exit
-f fix the file formatting
EOF
}
function tool_c_style() {
astyle --options=none --lineend=linux --mode=c \
--style=linux \
--indent=force-tab=8 \
--indent-preprocessor \
--indent-col1-comments \
--min-conditional-indent=0 \
--max-instatement-indent=80 \
--pad-oper \
--align-pointer=name \
--align-reference=name \
--max-code-length=80 \
--break-after-logical < "$1"
}
function tool_c_style_check() {
[[ -z "$1" || ! -r "$1" ]] && return
tool_c_style "$1" | diff -pu --label="$1.orig" "$1" --label="$1" -
}
function tool_c_style_fix() {
[[ -z "$1" || ! -r "$1" ]] && return
tmp="$(mktemp --tmpdir=$(dirname "$1"))"
tool_c_style "$1" > "$tmp"
mv "$tmp" "$1"
}
function check_c() {
for i in $CHK_C_LIST; do
echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
echo "Differences for $i"
tool_c_style_check "$i"
done
}
function fix_c() {
for i in $CHK_C_LIST; do
echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
echo "Fixing $i"
tool_c_style_fix "$i"
done
}
verify_deps astyle
opt_fix=0
while getopts "fh" opt; do
case $opt in
f)
opt_fix=1
;;
h|*)
usage
exit 1
;;
esac
done
echo "=============== $(date) ==============="
echo "Code Syntax Check Results (\"check-syntax $*\")"
if [[ $opt_fix -eq 1 ]]; then
fix_c
else
check_c
fi
echo "============================================================"
exit 0