summaryrefslogtreecommitdiff
path: root/gcr/gcr-mkoids
blob: 47d109534e5036c3d445cb98dc4a97beca233cfd (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
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
#!/bin/sh

set -euf

generate_header ()
{
	echo "/* WARNING: file is autogenerated */"
	echo ""
	echo "#include <glib.h>"
	echo ""
	echo "#ifndef ${UPPER}_OIDS_"
	echo "#define ${UPPER}_OIDS_"
	echo ""
	echo "void _${LOWER}_oids_init (void);"

	while read name oid; do
		if [ -n "$name" ]; then
			echo "GQuark ${UPPER}_OID_${name};"
		fi
	done

	echo ""
	echo "#endif /* ${UPPER}_OIDS_ */"
}

generate_source ()
{
	echo "/* WARNING: file is autogenerated */"
	echo ""
	echo "#include \"$HEADER\""
	echo ""

	echo "void"
	echo "_${LOWER}_oids_init (void)"
	echo "{"
	echo "	static volatile gsize initialized = 0;"
	echo "	if (g_once_init_enter (&initialized)) {"

	while read name oid; do
		if [ -n "$name" ]; then
			echo "		${UPPER}_OID_${name} = g_quark_from_static_string (\"${oid}\");"
		fi
	done

	echo "		g_once_init_leave (&initialized, 1);"
	echo "	}"
	echo "}"
}

UPPER="PREFIX"
LOWER="prefix"

while getopts 'c:h:p:' arg; do
	case $arg in
	p)
		UPPER=$(echo "$OPTARG" | tr '[:lower:]' '[:upper:]')
		LOWER=$(echo "$OPTARG" | tr '[:upper:]' '[:lower:]')
		;;
	c)
		SOURCE="$OPTARG"
		;;
	h)
		HEADER="$OPTARG"
		;;
	*)
		echo "gcr-mkoids: invalid argument: $arg" >&2
		exit 2
		;;
	esac
done

shift $(expr $OPTIND - 1)
if [ $# -ne 1 ]; then
	echo "gcr-mkoids: specify input file on command line"
	exit 2
fi

INPUT="$1"

if [ -n "$HEADER" ]; then
	generate_header < $INPUT > $HEADER
fi
if [ -n "$SOURCE" ]; then
	generate_source  < $INPUT > $SOURCE
fi