summaryrefslogtreecommitdiff
path: root/lib/enum-to-names-vals
blob: bc7224ae8ea46da25bf705230fa15eaaa22e4d60 (plain)
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
#! /bin/sh

if [ $# = 0 ]
then
    cat 1>&2 <<EOF
Usage: $(basename $0) ENUMPREFIX [FILES]
EOF
fi

type="$1"
shift

cat <<EOF
static
const struct {
	int value;
	const char *name;
} ${type}_names_vals[] = {
EOF
awk -vtype=$type -vtype_regex="^${type}_" '		\
BEGIN {                                                 \
    IGNORECASE = 1;                                     \
}      				                        \
      				                        \
$1 ~ type_regex {					\
    name=$1;                            		\
    symbol=$1;                            		\
    type_regex="^" type "_";				\
    gsub(/,.*$/, "", symbol);       			\
    gsub(type_regex, "", name);       			\
    gsub(/,.*$/, "", name);       			\
    gsub(/_/, "-", name);               		\
    print "\t{ " symbol ", \"" tolower(name) "\" },";	\
    valid = valid tolower(name) " ";			\
}                                                       \
                                                        \
END {                                                   \
    print "	{ 0, NULL }";                           \
    print "};";                                         \
    print "const char " type "_valid_str[] = \"" valid "\";";  \
    print "";                                           \
} ' $@