| 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # libseccomp code syntax checking tool |
| 5 | # |
| 6 | # Copyright (c) 2013,2015 Red Hat <[email protected]> |
| 7 | # Author: Paul Moore <[email protected]> |
| 8 | # |
| 9 | |
| 10 | # |
| 11 | # This library is free software; you can redistribute it and/or modify it |
| 12 | # under the terms of version 2.1 of the GNU Lesser General Public License as |
| 13 | # published by the Free Software Foundation. |
| 14 | # |
| 15 | # This library is distributed in the hope that it will be useful, but WITHOUT |
| 16 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 18 | # for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU Lesser General Public License |
| 21 | # along with this library; if not, see <http://www.gnu.org/licenses>. |
| 22 | # |
| 23 | |
| 24 | CHK_C_LIST="include/seccomp.h.in \ |
| 25 | include/seccomp-syscalls.h \ |
| 26 | src/*.c src/*.h \ |
| 27 | tests/*.c tests/*.h \ |
| 28 | tools/*.c tools/*.h" |
| 29 | CHK_C_EXCLUDE="src/syscalls.perf.c" |
| 30 | |
| 31 | CHK_SPELL_EXCLUDE_WORDS="" |
| 32 | |
| 33 | #### |
| 34 | # functions |
| 35 | |
| 36 | # |
| 37 | # Dependency verification |
| 38 | # |
| 39 | # Arguments: |
| 40 | # 1 Dependency to check for |
| 41 | # |
| 42 | function verify_deps() { |
| 43 | [[ -z "$1" ]] && return |
| 44 | if ! which "$1" >& /dev/null; then |
| 45 | echo "error: install \"$1\" and include it in your \$PATH" |
| 46 | exit 1 |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | # |
| 51 | # Print out script usage details |
| 52 | # |
| 53 | function usage() { |
| 54 | cat << EOF |
| 55 | usage: check-syntax [-h] |
| 56 | |
| 57 | libseccomp code syntax checking tool |
| 58 | optional arguments: |
| 59 | -h show this help message and exit |
| 60 | -f fix the file formatting |
| 61 | EOF |
| 62 | } |
| 63 | |
| 64 | # |
| 65 | # Spellcheck source code |
| 66 | # |
| 67 | function tool_spell_check() { |
| 68 | local tfile |
| 69 | |
| 70 | tfile=$(mktemp -t check-syntax-XXXXXX) |
| 71 | cat - > $tfile |
| 72 | codespell -q 16 -d -w -L "$CHK_SPELL_EXCLUDE_WORDS" $tfile |
| 73 | cat $tfile |
| 74 | rm -f $tfile |
| 75 | } |
| 76 | |
| 77 | # |
| 78 | # Generate a properly formatted C source/header file |
| 79 | # |
| 80 | function tool_c_style() { |
| 81 | cat - | /home/thromatk/git/libseccomp/astyle/AStyle/build/gcc/bin/astyle --options=none --lineend=linux --mode=c \ |
| 82 | --style=linux \ |
| 83 | --indent=force-tab=8 \ |
| 84 | --indent-col1-comments \ |
| 85 | --min-conditional-indent=0 \ |
| 86 | --max-continuation-indent=80 \ |
| 87 | --pad-oper \ |
| 88 | --align-pointer=name \ |
| 89 | --align-reference=name \ |
| 90 | --max-code-length=80 \ |
| 91 | --break-after-logical |
| 92 | } |
| 93 | |
| 94 | # |
| 95 | # Generate a properly formatted and spellchecked C source/header file |
| 96 | # |
| 97 | # Arguments: |
| 98 | # 1 Source file |
| 99 | # |
| 100 | function tool_c_ideal() { |
| 101 | cat "$1" | tool_spell_check | tool_c_style |
| 102 | } |
| 103 | |
| 104 | # |
| 105 | # Check the formatting on a C source/header file |
| 106 | # |
| 107 | # Arguments: |
| 108 | # 1 File to check |
| 109 | # |
| 110 | function tool_c_style_check() { |
| 111 | [[ -z "$1" || ! -r "$1" ]] && return |
| 112 | |
| 113 | tool_c_ideal "$1" | diff -pu --label="$1.orig" "$1" --label="$1" - |
| 114 | } |
| 115 | |
| 116 | # |
| 117 | # Fix the formatting on a C source/header file |
| 118 | # |
| 119 | # Arguments: |
| 120 | # 1 File to fix |
| 121 | # |
| 122 | function tool_c_style_fix() { |
| 123 | [[ -z "$1" || ! -r "$1" ]] && return |
| 124 | |
| 125 | tmp="$(mktemp --tmpdir=$(dirname "$1"))" |
| 126 | tool_c_ideal "$1" > "$tmp" |
| 127 | mv "$tmp" "$1" |
| 128 | } |
| 129 | |
| 130 | # |
| 131 | # Perform all known syntax checks for the configured C sources/headers |
| 132 | # |
| 133 | function check_c() { |
| 134 | for i in $CHK_C_LIST; do |
| 135 | echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue |
| 136 | echo "Differences for $i" |
| 137 | tool_c_style_check "$i" |
| 138 | done |
| 139 | } |
| 140 | |
| 141 | # |
| 142 | # Perform all known syntax fixess for the configured C sources/headers |
| 143 | # |
| 144 | function fix_c() { |
| 145 | for i in $CHK_C_LIST; do |
| 146 | echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue |
| 147 | echo "Fixing $i" |
| 148 | tool_c_style_fix "$i" |
| 149 | done |
| 150 | } |
| 151 | |
| 152 | #### |
| 153 | # main |
| 154 | |
| 155 | verify_deps astyle |
| 156 | verify_deps codespell |
| 157 | |
| 158 | opt_fix=0 |
| 159 | |
| 160 | while getopts "fh" opt; do |
| 161 | case $opt in |
| 162 | f) |
| 163 | opt_fix=1 |
| 164 | ;; |
| 165 | h|*) |
| 166 | usage |
| 167 | exit 1 |
| 168 | ;; |
| 169 | esac |
| 170 | done |
| 171 | |
| 172 | # display the results |
| 173 | echo "=============== $(date) ===============" |
| 174 | echo "Code Syntax Check Results (\"check-syntax $*\")" |
| 175 | if [[ $opt_fix -eq 1 ]]; then |
| 176 | fix_c |
| 177 | else |
| 178 | check_c |
| 179 | fi |
| 180 | echo "============================================================" |
| 181 | |
| 182 | # exit |
| 183 | exit 0 |