1240116Smarcel#! /bin/sh
2240116Smarcel#
3240116Smarcel# Automated Testing Framework (atf)
4240116Smarcel#
5240116Smarcel# Copyright (c) 2007 The NetBSD Foundation, Inc.
6240116Smarcel# All rights reserved.
7240116Smarcel#
8240116Smarcel# Redistribution and use in source and binary forms, with or without
9240116Smarcel# modification, are permitted provided that the following conditions
10240116Smarcel# are met:
11240116Smarcel# 1. Redistributions of source code must retain the above copyright
12240116Smarcel#    notice, this list of conditions and the following disclaimer.
13240116Smarcel# 2. Redistributions in binary form must reproduce the above copyright
14240116Smarcel#    notice, this list of conditions and the following disclaimer in the
15240116Smarcel#    documentation and/or other materials provided with the distribution.
16240116Smarcel#
17240116Smarcel# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
18240116Smarcel# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19240116Smarcel# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20240116Smarcel# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21240116Smarcel# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
22240116Smarcel# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23240116Smarcel# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24240116Smarcel# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25240116Smarcel# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26240116Smarcel# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27240116Smarcel# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28240116Smarcel# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29240116Smarcel#
30240116Smarcel
31240116Smarcel#
32240116Smarcel# A utility to sanity check the coding style of all source files in the
33240116Smarcel# project tree.
34240116Smarcel#
35240116Smarcel
36240116SmarcelProg_Name=${0##*/}
37240116Smarcel
38240116Smarcel#
39240116Smarcel# err message
40240116Smarcel#
41240116Smarcelerr() {
42240116Smarcel    echo "${Prog_Name}: ${@}" 1>&2
43240116Smarcel    exit 1
44240116Smarcel}
45240116Smarcel
46240116Smarcel#
47240116Smarcel# warn message
48240116Smarcel#
49240116Smarcelwarn() {
50240116Smarcel    echo "${Prog_Name}: ${@}" 1>&2
51240116Smarcel}
52240116Smarcel
53240116Smarcel#
54240116Smarcel# guess_topdir
55240116Smarcel#
56240116Smarcel# Locates the project's top directory and prints its path.  The caller
57240116Smarcel# must verify if the result is correct or not.
58240116Smarcel#
59240116Smarcelguess_topdir() {
60240116Smarcel    olddir=$(pwd)
61240116Smarcel    topdir=$(pwd)
62240116Smarcel    while [ ${topdir} != / ]; do
63240116Smarcel        if [ -f ./atf-c.h ]; then
64240116Smarcel            break
65240116Smarcel        else
66240116Smarcel            cd ..
67240116Smarcel            topdir=$(pwd)
68240116Smarcel        fi
69240116Smarcel    done
70240116Smarcel    cd ${olddir}
71240116Smarcel    echo ${topdir}
72240116Smarcel}
73240116Smarcel
74240116Smarcel#
75240116Smarcel# find_sources
76240116Smarcel#
77240116Smarcel# Locates all source files within the project, relative to the current
78240116Smarcel# directory, and prints their paths.
79240116Smarcel#
80240116Smarcelfind_sources() {
81240116Smarcel    find . \( -name "AUTHORS" -o \
82240116Smarcel              -name "COPYING" -o \
83240116Smarcel              -name "ChangeLog" -o \
84240116Smarcel              -name "NEWS" -o \
85240116Smarcel              -name "README" -o \
86240116Smarcel              -name "TODO" -o \
87240116Smarcel              -name "*.[0-9]" -o \
88240116Smarcel              -name "*.ac" -o \
89240116Smarcel              -name "*.at" -o \
90240116Smarcel              -name "*.awk" -o \
91240116Smarcel              -name "*.c" -o \
92240116Smarcel              -name "*.cpp" -o \
93240116Smarcel              -name "*.h" -o \
94240116Smarcel              -name "*.h.in" -o \
95240116Smarcel              -name "*.hpp" -o \
96240116Smarcel              -name "*.m4" -o \
97240116Smarcel              -name "*.sh" \
98240116Smarcel           \) -a \( \
99240116Smarcel              \! -path "*/atf-[0-9]*" -a \
100240116Smarcel              \! -path "*autom4te*" -a \
101240116Smarcel              -type f -a \
102240116Smarcel              \! -name "aclocal.m4" \
103240116Smarcel              \! -name "bconfig.h" \
104240116Smarcel              \! -name "defs.h" \
105240116Smarcel              \! -name "libtool.m4" \
106240116Smarcel              \! -name "ltoptions.m4" \
107240116Smarcel              \! -name "ltsugar.m4" \
108240116Smarcel              \! -name "lt~obsolete.m4" \
109240116Smarcel              \! -name "*.so.*" \
110240116Smarcel           \)
111240116Smarcel}
112240116Smarcel
113240116Smarcel#
114240116Smarcel# guess_formats file
115240116Smarcel#
116240116Smarcel# Guesses the formats applicable to the given file and prints the resulting
117240116Smarcel# list.
118240116Smarcel#
119240116Smarcelguess_formats() {
120240116Smarcel    case ${1} in
121240116Smarcel        */ltmain.sh)
122240116Smarcel            ;;
123240116Smarcel        *.[0-9])
124240116Smarcel            echo common man
125240116Smarcel            ;;
126240116Smarcel        *.c|*.h)
127240116Smarcel            echo common c
128240116Smarcel            ;;
129240116Smarcel        *.cpp|*.hpp)
130240116Smarcel            echo common cpp
131240116Smarcel            ;;
132240116Smarcel        *.sh)
133240116Smarcel            echo common shell
134240116Smarcel            ;;
135240116Smarcel        *)
136240116Smarcel            echo common
137240116Smarcel            ;;
138240116Smarcel    esac
139240116Smarcel}
140240116Smarcel
141240116Smarcel#
142240116Smarcel# check_file file
143240116Smarcel#
144240116Smarcel# Checks the validity of the given file.
145240116Smarcel#
146240116Smarcelcheck_file() {
147240116Smarcel    err=0
148240116Smarcel    for format in $(guess_formats ${1}); do
149240116Smarcel        awk -f ${topdir}/admin/check-style-${format}.awk ${1} || err=1
150240116Smarcel    done
151240116Smarcel
152240116Smarcel    return ${err}
153240116Smarcel}
154240116Smarcel
155240116Smarcel#
156240116Smarcel# main [file list]
157240116Smarcel#
158240116Smarcel# Entry point.
159240116Smarcel#
160240116Smarcelmain() {
161240116Smarcel    topdir=$(guess_topdir)
162240116Smarcel    if [ ! -f ${topdir}/atf-c.h ]; then
163240116Smarcel        err "Could not locate the project's top directory"
164240116Smarcel    fi
165240116Smarcel
166240116Smarcel    if [ ${#} -gt 0 ]; then
167240116Smarcel        sources=${@}
168240116Smarcel    else
169240116Smarcel        cd ${topdir}
170240116Smarcel        sources=$(find_sources)
171240116Smarcel    fi
172240116Smarcel
173240116Smarcel    ok=0
174240116Smarcel    for file in ${sources}; do
175240116Smarcel        file=$(echo ${file} | sed -e "s,\\./,,")
176240116Smarcel
177240116Smarcel        if [ ! -f ${file} ]; then
178240116Smarcel            err "Could not open ${file}"
179240116Smarcel        else
180240116Smarcel            check_file ${file} || ok=1
181240116Smarcel        fi
182240116Smarcel    done
183240116Smarcel
184240116Smarcel    return ${ok}
185240116Smarcel}
186240116Smarcel
187240116Smarcelmain "${@}"
188240116Smarcel
189240116Smarcel# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
190