File: waffle-enum

package info (click to toggle)
waffle 1.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,140 kB
  • sloc: ansic: 18,784; xml: 2,909; objc: 465; cpp: 267; sh: 125; makefile: 18
file content (149 lines) | stat: -rwxr-xr-x 3,884 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash

# Copyright 2012 Intel Corporation
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e

waffle_enum_h="include/waffle/waffle_enum.h"

function prog_name() {
    echo $(basename "$0")
}

function show_help() {
    local p="$(prog_name)"

    echo "NAME"
    echo "    $p - a tool for waffle_enum.h"
    echo
    echo "DESCRIPTION"
    echo "    This script validates the enum values in waffle_enum.h. It must"
    echo "    be called from the top of the waffle repo."
    echo
    echo "USAGE"
    echo "    $p check"
    echo "        Check waffle_enum.h for errors. If there is an error, then"
    echo "        print a diagnostic message and exit 1. Otherwise, exit 0."
    echo
    echo "        Possible errors are duplicate values and ill-formed enum"
    echo "        values. Enum values must have form 0x????, where ? is a hex"
    echo "        digit."
    echo
    echo "    $p -h"
    echo "        Show this help message."
}

function usage_error() {
    local p="$(prog_name)"

    echo "$p: usage error. see \`$p -h\`"
    exit 1
}

function fatal_error() {
    local p="$(prog_name)"

    echo "$p: error: $1"
    exit 1
}

function print_raw_values() {
    local file_in="$waffle_enum_h"
    local file_out="$(mktemp)"

    sed \
        -e '/WAFFLE_NONE/d' \
        -e '/WAFFLE_DONT_CARE/d' \
        -e '/WAFFLE_.*=/!d' \
        -e 's/^.*=[[:space:]]*\(.*\),.*$/\1/' \
        < "$file_in" \
        > "$file_out"

    echo "$file_out"
}

function check_syntax_raw() {
    sed -e '/^0x[[:xdigit:]]\{4\}$/d' "$1"
}

function check_syntax() {
    local file_raw_values="$1"

    if [[ "$(check_syntax_raw "$file_raw_values" | wc -l)" = 0 ]]
    then
        return 0
    else
        echo "The enum values below are ill-formed."
        check_syntax_raw "$file_raw_values"
        exit 1
    fi
}

function check_duplicates_raw() {
    sort < "$1" | uniq --repeated
}

function check_duplicates() {
    local file_raw_values="$1"

    if [[ "$(check_duplicates_raw "$file_raw_values" | wc -l)" = 0 ]]
    then
        return 0
    else
        echo "The enum values below are duplicates."
        check_duplicates_raw "$file_raw_values"
        exit 1
    fi
}

function check() {
    local file_raw_values="$1"

    check_syntax "$file_raw_values"
    check_duplicates "$file_raw_values"
}

function main() {
    if [[ $# != 1 ]]
    then
        show_help
        exit 1
    fi

    case "$1" in
        "-h")
            show_help
            ;;
        "check")
            check "$(print_raw_values)"
            ;;
        *)
            usage_error
            ;;
    esac
}

main "$@"