Blame tools/check-syntax

Packit 56e23f
#!/bin/bash
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# libseccomp code syntax checking tool
Packit 56e23f
#
Packit 56e23f
# Copyright (c) 2013,2015 Red Hat <pmoore@redhat.com>
Packit 56e23f
# Author: Paul Moore <paul@paul-moore.com>
Packit 56e23f
#
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# This library is free software; you can redistribute it and/or modify it
Packit 56e23f
# under the terms of version 2.1 of the GNU Lesser General Public License as
Packit 56e23f
# published by the Free Software Foundation.
Packit 56e23f
#
Packit 56e23f
# This library is distributed in the hope that it will be useful, but WITHOUT
Packit 56e23f
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 56e23f
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
Packit 56e23f
# for more details.
Packit 56e23f
#
Packit 56e23f
# You should have received a copy of the GNU Lesser General Public License
Packit 56e23f
# along with this library; if not, see <http://www.gnu.org/licenses>.
Packit 56e23f
#
Packit 56e23f
Packit 56e23f
CHK_C_LIST="include/seccomp.h.in \
Packit 56e23f
	    include/seccomp-syscalls.h \
Packit 56e23f
	    src/*.c src/*.h \
Packit 56e23f
	    tests/*.c tests/*.h \
Packit 56e23f
	    tools/*.c tools/*.h"
Packit Service 10c312
CHK_C_EXCLUDE="src/syscalls.perf.c"
Packit 56e23f
Packit 56e23f
####
Packit 56e23f
# functions
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Dependency verification
Packit 56e23f
#
Packit 56e23f
# Arguments:
Packit 56e23f
#     1    Dependency to check for
Packit 56e23f
#
Packit 56e23f
function verify_deps() {
Packit 56e23f
	[[ -z "$1" ]] && return
Packit 56e23f
	if ! which "$1" >& /dev/null; then
Packit 56e23f
		echo "error: install \"$1\" and include it in your \$PATH"
Packit 56e23f
		exit 1
Packit 56e23f
	fi
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Print out script usage details
Packit 56e23f
#
Packit 56e23f
function usage() {
Packit 56e23f
cat << EOF
Packit 56e23f
usage: check-syntax [-h]
Packit 56e23f
Packit 56e23f
libseccomp code syntax checking tool
Packit 56e23f
optional arguments:
Packit 56e23f
  -h             show this help message and exit
Packit 56e23f
  -f             fix the file formatting
Packit 56e23f
EOF
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Generate a properly formatted C source/header file
Packit 56e23f
#
Packit 56e23f
# Arguments:
Packit 56e23f
#     1    Source file
Packit 56e23f
#
Packit 56e23f
function tool_c_style() {
Packit 56e23f
	astyle --options=none --lineend=linux --mode=c \
Packit 56e23f
		--style=linux \
Packit 56e23f
		--indent=force-tab=8 \
Packit 56e23f
		--indent-preprocessor \
Packit 56e23f
		--indent-col1-comments \
Packit 56e23f
		--min-conditional-indent=0 \
Packit 56e23f
		--max-instatement-indent=80 \
Packit 56e23f
		--pad-oper \
Packit 56e23f
		--align-pointer=name \
Packit 56e23f
		--align-reference=name \
Packit 56e23f
		--max-code-length=80 \
Packit 56e23f
		--break-after-logical < "$1"
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Check the formatting on a C source/header file
Packit 56e23f
#
Packit 56e23f
# Arguments:
Packit 56e23f
#     1    File to check
Packit 56e23f
#
Packit 56e23f
function tool_c_style_check() {
Packit 56e23f
	[[ -z "$1" || ! -r "$1" ]] && return
Packit 56e23f
Packit 56e23f
	tool_c_style "$1" | diff -pu --label="$1.orig" "$1" --label="$1" -
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Fix the formatting on a C source/header file
Packit 56e23f
#
Packit 56e23f
# Arguments:
Packit 56e23f
#     1    File to fix
Packit 56e23f
#
Packit 56e23f
function tool_c_style_fix() {
Packit 56e23f
	[[ -z "$1" || ! -r "$1" ]] && return
Packit 56e23f
Packit 56e23f
	tmp="$(mktemp --tmpdir=$(dirname "$1"))"
Packit 56e23f
	tool_c_style "$1" > "$tmp"
Packit 56e23f
	mv "$tmp" "$1"
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Perform all known syntax checks for the configured C sources/headers
Packit 56e23f
#
Packit 56e23f
function check_c() {
Packit 56e23f
	for i in $CHK_C_LIST; do
Packit 56e23f
		echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
Packit 56e23f
		echo "Differences for $i"
Packit 56e23f
		tool_c_style_check "$i"
Packit 56e23f
	done
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
#
Packit 56e23f
# Perform all known syntax fixess for the configured C sources/headers
Packit 56e23f
#
Packit 56e23f
function fix_c() {
Packit 56e23f
	for i in $CHK_C_LIST; do
Packit 56e23f
		echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue
Packit 56e23f
		echo "Fixing $i"
Packit 56e23f
		tool_c_style_fix "$i"
Packit 56e23f
	done
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
####
Packit 56e23f
# main
Packit 56e23f
Packit 56e23f
verify_deps astyle
Packit 56e23f
Packit 56e23f
opt_fix=0
Packit 56e23f
Packit 56e23f
while getopts "fh" opt; do
Packit 56e23f
	case $opt in
Packit 56e23f
	f)
Packit 56e23f
		opt_fix=1
Packit 56e23f
		;;
Packit 56e23f
	h|*)
Packit 56e23f
		usage
Packit 56e23f
		exit 1
Packit 56e23f
		;;
Packit 56e23f
	esac
Packit 56e23f
done
Packit 56e23f
Packit 56e23f
# display the results
Packit 56e23f
echo "=============== $(date) ==============="
Packit 56e23f
echo "Code Syntax Check Results (\"check-syntax $*\")"
Packit 56e23f
if [[ $opt_fix -eq 1 ]]; then
Packit 56e23f
	fix_c
Packit 56e23f
else
Packit 56e23f
	check_c
Packit 56e23f
fi
Packit 56e23f
echo "============================================================"
Packit 56e23f
Packit 56e23f
# exit
Packit 56e23f
exit 0