summaryrefslogtreecommitdiffstats
path: root/util/getdefs
blob: 869dff6bc08c3cf59567dff5739b55eaf97c97a5 (plain) (blame)
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
#!/bin/sh

set -e

text="$(cat)"

expr='\(0x[0-9a-fA-F]\+\|[0-9]\+\)[Ll]*[Uu]*[Ll]*'
expr='\(\*\|\/\|[+~-]\|<<\|>>\)\?\s*'"$expr"
expr='\('"$expr"'\|(\|)\)'
expr='\s*\('"$expr"'\s*\)\+'

filter () {
	sed -n 's/^\s*#\s*define\s\+\('"$1"'\)\s\+\('"$expr"'\)\s*\(\/.*\)\?$/\1 \2/p'
}

if test $# = 0; then
	printf '%s\n' "$text" | filter '[A-Z0-9_]\+'
else
	for prefix; do
		printf '%s\n' "$text" | filter "$prefix"'_[A-Z0-9_]\+'
	done
fi | \
while read name value; do
	value="$(printf '%s\n' "$value" | tr -d ULul)"
	value=$(printf '%s\n' "$(( $value ))" | cut -d . -f 1)
	printf '%s %s\n' "$name" "$value"
done