summaryrefslogtreecommitdiffstats
path: root/src/gen-macros.sh
blob: a7d74c28d01c8311ccd0cb52527b94ceddcdb388 (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
43
44
45
46
47
#!/bin/sh -e

echo "/* autogenerated, don't edit */"
echo "#include <string.h>"
echo "#include <stdlib.h>"

while read prefix base name type typedef; do
    echo "// $base.$name ($type)"
    case "$type" in
        string )
            echo "#define ${prefix}_has_${name}(d) ((d).${base}.${name} != 0)
#define ${prefix}_${name}(d) (const char*)((d).${base}.${name})
#define ${prefix}_${name}_length(d) strlen((d).${base}.${name})
#define ${prefix}_set_${name}(d, v)     \
    if ((d)._${base}_${name}_alloced) { \
        free((d).${base}.${name});      \
    }                                   \
    (d).${base}.${name} = strdup(v);    \
    (d)._${base}_${name}_alloced = true;"
            ;;
        bytestring )
            echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
#define ${prefix}_${name}(d) (const uint8_t*)((d).${base}.${name}.data)
#define ${prefix}_${name}_length(d) (size_t)((d).${base}.${name}.len)
#define ${prefix}_set_${name}(d, v, l) (d).${base}.has_${name} = true; (d).${base}.${name}.data = v; (d).${base}.${name}.len = l
#define ${prefix}_set_${name}_string(d, v) (d).${base}.has_${name} = true; (d).${base}.${name}.data = (uint8_t*)v; (d).${base}.${name}.len = strlen(v)"
            ;;
        bytes )
            echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
#define ${prefix}_${name}(d) (const uint8_t*)((d).${base}.${name}.data)
#define ${prefix}_${name}_length(d) (size_t)((d).${base}.${name}.len)
#define ${prefix}_set_${name}(d, v, l) (d).${base}.has_${name} = true; (d).${base}.${name}.data = (uint8_t*)v; (d).${base}.${name}.len = l"
            ;;
        enum )
            echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
#define ${prefix}_${name}(d) (enum ${typedef})((d).${base}.${name})"
            ;;
        value )
            echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
#define ${prefix}_${name}(d) (${typedef})((d).${base}.${name})
#define ${prefix}_set_${name}(d, v) (d).${base}.has_${name} = true; (d).${base}.${name} = v"
            ;;
        * )
            echo "#error \"invalid type $type for $base.$name\""
            ;;
    esac
done < "$1"