| 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # libseccomp code syntax checking tool |
| 5 | # |
| 6 | # Copyright (c) 2013,2015 Red Hat <pmoore@redhat.com> |
| 7 | # Author: Paul Moore <paul@paul-moore.com> |
| 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 | src/*.c src/*.h \ |
| 26 | tests/*.c tests/*.h \ |
| 27 | tools/*.c tools/*.h" |
| 28 | CHK_C_EXCLUDE="" |
| 29 | |
| 30 | #### |
| 31 | # functions |
| 32 | |
| 33 | # |
| 34 | # Dependency verification |
| 35 | # |
| 36 | # Arguments: |
| 37 | # 1 Dependency to check for |
| 38 | # |
| 39 | function verify_deps() { |
| 40 | [[ -z "$1" ]] && return |
| 41 | if ! which "$1" >& /dev/null; then |
| 42 | echo "error: install \"$1\" and include it in your \$PATH" |
| 43 | exit 1 |
| 44 | fi |
| 45 | } |
| 46 | |
| 47 | # |
| 48 | # Print out script usage details |
| 49 | # |
| 50 | function usage() { |
| 51 | cat << EOF |
| 52 | usage: check-syntax [-h] |
| 53 | |
| 54 | libseccomp code syntax checking tool |
| 55 | optional arguments: |
| 56 | -h show this help message and exit |
| 57 | -f fix the file formatting |
| 58 | EOF |
| 59 | } |
| 60 | |
| 61 | # |
| 62 | # Generate a properly formatted C source/header file |
| 63 | # |
| 64 | # Arguments: |
| 65 | # 1 Source file |
| 66 | # |
| 67 | function tool_c_style() { |
| 68 | astyle --options=none --lineend=linux --mode=c \ |
| 69 | --style=linux \ |
| 70 | --indent=force-tab=8 \ |
| 71 | --indent-preprocessor \ |
| 72 | --indent-col1-comments \ |
| 73 | --min-conditional-indent=0 \ |
| 74 | --max-instatement-indent=80 \ |
| 75 | --pad-oper \ |
| 76 | --align-pointer=name \ |
| 77 | --align-reference=name \ |
| 78 | --max-code-length=80 \ |
| 79 | --break-after-logical < "$1" |
| 80 | } |
| 81 | |
| 82 | # |
| 83 | # Check the formatting on a C source/header file |
| 84 | # |
| 85 | # Arguments: |
| 86 | # 1 File to check |
| 87 | # |
| 88 | function tool_c_style_check() { |
| 89 | [[ -z "$1" || ! -r "$1" ]] && return |
| 90 | |
| 91 | tool_c_style "$1" | diff -pu --label="$1.orig" "$1" --label="$1" - |
| 92 | } |
| 93 | |
| 94 | # |
| 95 | # Fix the formatting on a C source/header file |
| 96 | # |
| 97 | # Arguments: |
| 98 | # 1 File to fix |
| 99 | # |
| 100 | function tool_c_style_fix() { |
| 101 | [[ -z "$1" || ! -r "$1" ]] && return |
| 102 | |
| 103 | tmp="$(mktemp --tmpdir=$(dirname "$1"))" |
| 104 | tool_c_style "$1" > "$tmp" |
| 105 | mv "$tmp" "$1" |
| 106 | } |
| 107 | |
| 108 | # |
| 109 | # Perform all known syntax checks for the configured C sources/headers |
| 110 | # |
| 111 | function check_c() { |
| 112 | for i in $CHK_C_LIST; do |
| 113 | echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue |
| 114 | echo "Differences for $i" |
| 115 | tool_c_style_check "$i" |
| 116 | done |
| 117 | } |
| 118 | |
| 119 | # |
| 120 | # Perform all known syntax fixess for the configured C sources/headers |
| 121 | # |
| 122 | function fix_c() { |
| 123 | for i in $CHK_C_LIST; do |
| 124 | echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue |
| 125 | echo "Fixing $i" |
| 126 | tool_c_style_fix "$i" |
| 127 | done |
| 128 | } |
| 129 | |
| 130 | #### |
| 131 | # main |
| 132 | |
| 133 | verify_deps astyle |
| 134 | |
| 135 | opt_fix=0 |
| 136 | |
| 137 | while getopts "fh" opt; do |
| 138 | case $opt in |
| 139 | f) |
| 140 | opt_fix=1 |
| 141 | ;; |
| 142 | h|*) |
| 143 | usage |
| 144 | exit 1 |
| 145 | ;; |
| 146 | esac |
| 147 | done |
| 148 | |
| 149 | # display the results |
| 150 | echo "=============== $(date) ===============" |
| 151 | echo "Code Syntax Check Results (\"check-syntax $*\")" |
| 152 | if [[ $opt_fix -eq 1 ]]; then |
| 153 | fix_c |
| 154 | else |
| 155 | check_c |
| 156 | fi |
| 157 | echo "============================================================" |
| 158 | |
| 159 | # exit |
| 160 | exit 0 |