1#!/bin/sh
2#
3# This file is part of mbed TLS (https://tls.mbed.org)
4#
5# Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
6#
7# Purpose
8#
9# This script confirms that the naming of all symbols and identifiers in mbed
10# TLS are consistent with the house style and are also self-consistent.
11#
12set -eu
13
14if grep --version|head -n1|grep GNU >/dev/null; then :; else
15    echo "This script requires GNU grep."
16    exit 1
17fi
18
19printf "Analysing source code...\n"
20
21tests/scripts/list-macros.sh
22tests/scripts/list-enum-consts.pl
23tests/scripts/list-identifiers.sh
24tests/scripts/list-symbols.sh
25
26FAIL=0
27
28printf "\nExported symbols declared in header: "
29UNDECLARED=$( diff exported-symbols identifiers | sed -n -e 's/^< //p' )
30if [ "x$UNDECLARED" = "x" ]; then
31    echo "PASS"
32else
33    echo "FAIL"
34    echo "$UNDECLARED"
35    FAIL=1
36fi
37
38diff macros identifiers | sed -n -e 's/< //p' > actual-macros
39
40for THING in actual-macros enum-consts; do
41    printf "Names of $THING: "
42    test -r $THING
43    BAD=$( grep -v '^MBEDTLS_[0-9A-Z_]*[0-9A-Z]$\|^YOTTA_[0-9A-Z_]*[0-9A-Z]$' $THING || true )
44    if [ "x$BAD" = "x" ]; then
45        echo "PASS"
46    else
47        echo "FAIL"
48        echo "$BAD"
49        FAIL=1
50    fi
51done
52
53for THING in identifiers; do
54    printf "Names of $THING: "
55    test -r $THING
56    BAD=$( grep -v '^mbedtls_[0-9a-z_]*[0-9a-z]$' $THING || true )
57    if [ "x$BAD" = "x" ]; then
58        echo "PASS"
59    else
60        echo "FAIL"
61        echo "$BAD"
62        FAIL=1
63    fi
64done
65
66printf "Likely typos: "
67sort -u actual-macros enum-consts > _caps
68HEADERS=$( ls include/mbedtls/*.h | egrep -v 'compat-1\.3\.h' )
69NL='
70'
71sed -n 's/MBED..._[A-Z0-9_]*/\'"$NL"'&\'"$NL"/gp \
72    $HEADERS library/*.c \
73    | grep MBEDTLS | sort -u > _MBEDTLS_XXX
74TYPOS=$( diff _caps _MBEDTLS_XXX | sed -n 's/^> //p' \
75            | egrep -v 'XXX|__|_$|^MBEDTLS_.*CONFIG_FILE$' || true )
76rm _MBEDTLS_XXX _caps
77if [ "x$TYPOS" = "x" ]; then
78    echo "PASS"
79else
80    echo "FAIL"
81    echo "$TYPOS"
82    FAIL=1
83fi
84
85printf "\nOverall: "
86if [ "$FAIL" -eq 0 ]; then
87    rm macros actual-macros enum-consts identifiers exported-symbols
88    echo "PASSED"
89    exit 0
90else
91    echo "FAILED"
92    exit 1
93fi
94