11#! /usr/bin/env bash
22
3+ # Ensure that the common script exists and is readable, then verify it has no
4+ # syntax errors and defines the required function.
5+ common_script=" $( dirname " $0 " ) /../../scripts/common.sh"
6+ [ -r " $common_script " ] || { echo " [!] '$common_script ' not found or not readable." >&2 ; exit 1; }
7+ bash -n " $common_script " > /dev/null 2>&1 || { echo " [!] '$common_script ' contains syntax errors." >&2 ; exit 1; }
8+ source " $common_script "
9+ declare -F set_colors > /dev/null 2>&1 || { echo " [!] '$common_script ' does not define the required function." >&2 ; exit 1; }
10+
311# Build unmatched suppressions for each *.c file.
412cppcheck_build_unmatched () {
513 local file suppression=" "
@@ -64,26 +72,25 @@ CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
6472CPPCHECK_OPTS+=" --force $( cppcheck_suppressions) $( cppcheck_build_unmatched) "
6573CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
6674
75+ set_colors
76+
6777RETURN=0
6878
6979# Disallow non-ASCII characters in workspace path
7080workspace=$( git rev-parse --show-toplevel)
7181if echo " $workspace " | grep -q " [一-龥]" ; then
72- echo " [!] The workspace path '$workspace ' contains non-ASCII characters." >&2
73- exit 1
82+ throw " The workspace path '$workspace ' contains non-ASCII characters."
7483fi
7584
7685CLANG_FORMAT=$( which clang-format)
7786if [ $? -ne 0 ]; then
78- echo " [!] clang-format not installed. Unable to check source file format policy." >&2
79- exit 1
87+ throw " clang-format not installed. Unable to check source file format policy."
8088fi
8189
8290CPPCHECK=$( which cppcheck)
8391mkdir -p .out
8492if [ $? -ne 0 ]; then
85- echo " [!] cppcheck not installed. Unable to perform static analysis." >&2
86- exit 1
93+ throw " cppcheck not installed. Unable to perform static analysis."
8794fi
8895
8996# Check that cppcheck's version is at least 1.90.
@@ -94,21 +101,18 @@ else
94101 # For version 1.x, extract the minor version and compare.
95102 minor_version=$( echo " $cppcheck_ver " | sed -Ee ' s/Cppcheck 1\.([0-9]+)/\1/;q' )
96103 if [ " $minor_version " -lt 90 ]; then
97- echo " [!] cppcheck version must be at least 1.90." >&2
98- echo -e " See Developer Info for building cppcheck from source:\n"
99- echo -e " https://cppcheck.sourceforge.io/devinfo/" >&2
100- exit 1
104+ throw " cppcheck version must be at least 1.90.\n\
105+ See Developer Info for building cppcheck from source:\n\
106+ https://cppcheck.sourceforge.io/devinfo/"
101107 fi
102108fi
103109
104110ASPELL=$( which aspell)
105111if [ $? -ne 0 ]; then
106- echo " [!] aspell not installed. Unable to do spelling check." >&2
107- exit 1
112+ throw " aspell not installed. Unable to do spelling check."
108113fi
109114if [ -z " $( aspell dump dicts | grep -E ' ^en$' ) " ]; then
110- echo " [!] aspell-en not installed. Unable to do spelling check." >&2
111- exit 1
115+ throw " aspell-en not installed. Unable to do spelling check."
112116fi
113117
114118DIFF=$( which colordiff)
@@ -121,8 +125,7 @@ if command -v sha1sum >/dev/null 2>&1; then
121125elif command -v shasum > /dev/null 2>&1 ; then
122126 SHA1SUM=" shasum"
123127else
124- echo " [!] sha1sum or shasum not installed." >&2
125- exit 1
128+ throw " sha1sum or shasum not installed."
126129fi
127130
128131# Get staged filenames (added, copied, or modified) into an array.
@@ -137,12 +140,12 @@ for file in "${FILES[@]}"; do
137140
138141 if echo " $mime_info " | grep -qi binary; then
139142 binary_files+=(" $name " )
140- echo " [!] '$name ' appears to be a binary blob."
143+ printf " ${RED} [!]${NC} '${YELLOW} $ name${NC} ' appears to be a binary blob.\n "
141144 fi
142145done
143146
144147if [ " ${# binary_files[@]} " -gt 0 ]; then
145- echo " WARNING: Binary data found"
148+ printf " ${RED} [!] ${NC} Binary data found.\n "
146149fi
147150
148151FILES=$( git diff --cached --name-only --diff-filter=ACMR | grep -E " \.(c|cpp|h)$" )
180183
181184$SHA1SUM -c scripts/checksums 2> /dev/null > /dev/null
182185if [ $? -ne 0 ]; then
183- echo " [!] You are not allowed to change the header file queue.h or list.h" >&2
184- exit 1
186+ throw " You are not allowed to change the header file queue.h or list.h"
185187fi
186188
187189# Prevent unsafe functions
0 commit comments