ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#! /bin/sh
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt# Copyright (c) 2007 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# are met:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# A utility to sanity check the coding style of all source files in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# project tree.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntProg_Name=${0##*/}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# err message
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunterr() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo "${Prog_Name}: ${@}" 1>&2
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt exit 1
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# warn message
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntwarn() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo "${Prog_Name}: ${@}" 1>&2
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# guess_topdir
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Locates the project's top directory and prints its path. The caller
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# must verify if the result is correct or not.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntguess_topdir() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt olddir=$(pwd)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt topdir=$(pwd)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt while [ ${topdir} != / ]; do
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if [ -f ./atf-c.h ]; then
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt break
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cd ..
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt topdir=$(pwd)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fi
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt done
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cd ${olddir}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo ${topdir}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# find_sources
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Locates all source files within the project, relative to the current
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# directory, and prints their paths.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntfind_sources() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt find . \( -name "AUTHORS" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "COPYING" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "ChangeLog" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "NEWS" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "README" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "TODO" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.[0-9]" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.ac" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.at" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.awk" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.c" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.cpp" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.h" -o \
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt -name "*.h.in" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.hpp" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.m4" -o \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -name "*.sh" \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \) -a \( \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -path "*/atf-[0-9]*" -a \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -path "*autom4te*" -a \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt -type f -a \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -name "aclocal.m4" \
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews \! -name "config.h" \
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt \! -name "defs.h" \
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews \! -name "defs.hpp" \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -name "libtool.m4" \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -name "ltoptions.m4" \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -name "ltsugar.m4" \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -name "lt~obsolete.m4" \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt \! -name "*.so.*" \
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews \) | grep -v tools/
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# guess_formats file
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Guesses the formats applicable to the given file and prints the resulting
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# list.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntguess_formats() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt case ${1} in
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */ltmain.sh)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ;;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *.[0-9])
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo common man
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ;;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *.c|*.h)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo common c
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ;;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *.cpp|*.hpp)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo common cpp
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ;;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *.sh)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo common shell
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ;;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt echo common
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ;;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt esac
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# check_file file
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Checks the validity of the given file.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntcheck_file() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err=0
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for format in $(guess_formats ${1}); do
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt awk -f ${topdir}/admin/check-style-${format}.awk ${1} || err=1
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt done
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return ${err}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# main [file list]
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# Entry point.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntmain() {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt topdir=$(guess_topdir)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if [ ! -f ${topdir}/atf-c.h ]; then
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err "Could not locate the project's top directory"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fi
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if [ ${#} -gt 0 ]; then
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sources=${@}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cd ${topdir}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt sources=$(find_sources)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fi
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ok=0
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for file in ${sources}; do
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt file=$(echo ${file} | sed -e "s,\\./,,")
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if [ ! -f ${file} ]; then
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err "Could not open ${file}"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_file ${file} || ok=1
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fi
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt done
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return ${ok}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntmain "${@}"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4