process_atoms 1.14 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
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
#! /usr/local/bin/bash

function help() {
  echo commands: help header gperf filter table
}

function header() {
    echo '/* Automatically generated by process_atoms.sh,' 
    echo ' * ' `date`
    echo ' * Do not edit! */'
    echo
}

atom_id=0

function getatom() {
    atom=''
    while [ x$atom = x ]; do
	read atom ignored
	if [ x$? != x0 ] ; then
	    return 1
	fi
	case $atom in
	    \#*)
		atom=''
		;;
	esac ;
    done
    atom_id=$[ atom_id + 1 ]
}

function atom2define () {
    echo -n ATOM_$1 | tr -- -a-z _A-Z
}

if [ x$# != x1 ]; then
    help
    exit 1
fi

case $1 in
    filter)
	while getatom; do
	    echo $atom $atom_id
	done
	;;
    header)
	header
	while getatom; do
	    echo -n '#define '
	    atom2define $atom
	    echo ' ' $atom_id
	done
	;;
    gperf)
	echo '%{'
	header
	echo '%}'

	echo 'struct atom { char *name; int id; };'
	echo '%%'
	    
	while getatom; do
	    echo -n "$atom, "
	    atom2define $atom
	    echo
	done
	;;
    table)
	header
71
	echo -n '{ { "UNKNOWN", 0 }'
Niels Möller's avatar
Niels Möller committed
72
73
	while getatom; do
	    echo ','
74
	    echo -n '  {' \"$atom\", ${#atom} '}'
Niels Möller's avatar
Niels Möller committed
75
76
77
78
79
80
81
82
83
84
85
86
	done
	echo ' }'
	;;
    help)
	help
	exit 0
	;;
    *)
	help
	exit 1
	;;
esac