1#! /bin/sh
2#
3# Automated Testing Framework (atf)
4#
5# Copyright (c) 2007 The NetBSD Foundation, Inc.
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
18# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
22# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# A utility to sanity check the coding style of all source files in the
33# project tree.
34#
35
36Prog_Name=${0##*/}
37
38#
39# err message
40#
41err() {
42    echo "${Prog_Name}: ${@}" 1>&2
43    exit 1
44}
45
46#
47# warn message
48#
49warn() {
50    echo "${Prog_Name}: ${@}" 1>&2
51}
52
53#
54# guess_topdir
55#
56# Locates the project's top directory and prints its path.  The caller
57# must verify if the result is correct or not.
58#
59guess_topdir() {
60    olddir=$(pwd)
61    topdir=$(pwd)
62    while [ ${topdir} != / ]; do
63        if [ -f ./atf-c.h ]; then
64            break
65        else
66            cd ..
67            topdir=$(pwd)
68        fi
69    done
70    cd ${olddir}
71    echo ${topdir}
72}
73
74#
75# find_sources
76#
77# Locates all source files within the project, relative to the current
78# directory, and prints their paths.
79#
80find_sources() {
81    find . \( -name "AUTHORS" -o \
82              -name "COPYING" -o \
83              -name "ChangeLog" -o \
84              -name "NEWS" -o \
85              -name "README" -o \
86              -name "TODO" -o \
87              -name "*.[0-9]" -o \
88              -name "*.ac" -o \
89              -name "*.at" -o \
90              -name "*.awk" -o \
91              -name "*.c" -o \
92              -name "*.cpp" -o \
93              -name "*.h" -o \
94              -name "*.h.in" -o \
95              -name "*.hpp" -o \
96              -name "*.m4" -o \
97              -name "*.sh" \
98           \) -a \( \
99              \! -path "*/atf-[0-9]*" -a \
100              \! -path "*autom4te*" -a \
101              -type f -a \
102              \! -name "aclocal.m4" \
103              \! -name "bconfig.h" \
104              \! -name "defs.h" \
105              \! -name "libtool.m4" \
106              \! -name "ltoptions.m4" \
107              \! -name "ltsugar.m4" \
108              \! -name "lt~obsolete.m4" \
109              \! -name "*.so.*" \
110           \)
111}
112
113#
114# guess_formats file
115#
116# Guesses the formats applicable to the given file and prints the resulting
117# list.
118#
119guess_formats() {
120    case ${1} in
121        */ltmain.sh)
122            ;;
123        *.[0-9])
124            echo common man
125            ;;
126        *.c|*.h)
127            echo common c
128            ;;
129        *.cpp|*.hpp)
130            echo common cpp
131            ;;
132        *.sh)
133            echo common shell
134            ;;
135        *)
136            echo common
137            ;;
138    esac
139}
140
141#
142# check_file file
143#
144# Checks the validity of the given file.
145#
146check_file() {
147    err=0
148    for format in $(guess_formats ${1}); do
149        awk -f ${topdir}/admin/check-style-${format}.awk ${1} || err=1
150    done
151
152    return ${err}
153}
154
155#
156# main [file list]
157#
158# Entry point.
159#
160main() {
161    topdir=$(guess_topdir)
162    if [ ! -f ${topdir}/atf-c.h ]; then
163        err "Could not locate the project's top directory"
164    fi
165
166    if [ ${#} -gt 0 ]; then
167        sources=${@}
168    else
169        cd ${topdir}
170        sources=$(find_sources)
171    fi
172
173    ok=0
174    for file in ${sources}; do
175        file=$(echo ${file} | sed -e "s,\\./,,")
176
177        if [ ! -f ${file} ]; then
178            err "Could not open ${file}"
179        else
180            check_file ${file} || ok=1
181        fi
182    done
183
184    return ${ok}
185}
186
187main "${@}"
188
189# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
190