xref: /titanic_41/usr/src/lib/libpp/common/gentab.sh (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1986-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                 Glenn Fowler <gsf@research.att.com>                  #
18#                                                                      #
19########################################################################
20:
21# Glenn Fowler
22# AT&T Bell Laboratories
23#
24# @(#)gentab (gsf@research.att.com) 07/17/94
25#
26# C table generator
27#
28#	%flags [ prefix=<prefix> ] [ index=<index> ] [ init=<init> ]
29#
30#	%keyword <name> [ prefix=<prefix> ] [ index=<index> ] [ init=<init> ] [ first=<id> ] [ last=<id> ]
31#
32#	%sequence [ prefix=<prefix> ] [ index=<index> ] [ init=<init> ]
33#
34
35case `(typeset -u s=a n=0; ((n=n+1)); print $s$n) 2>/dev/null` in
36A1)	shell=ksh
37	typeset -u ID
38	typeset -i counter err_line
39	;;
40*)	shell=bsh
41	;;
42esac
43command=$0
44counter=0
45define=1
46err_line=0
47type=""
48index=""
49first=""
50last=""
51table=1
52while	:
53do	case $1 in
54	-d)	table=0 ;;
55	-t)	define=0 ;;
56	*)	break ;;
57	esac
58	shift
59done
60case $1 in
61"")	err_file=""
62	;;
63*)	exec <$1
64	err_file="\"$1\", "
65	;;
66esac
67while	read line
68do	case $shell in
69	ksh)	((err_line=err_line+1)) ;;
70	*)	err_line=`expr $err_line + 1` ;;
71	esac
72	set '' $line
73	shift
74	case $1 in
75	[#]*)	echo "/*"
76		while	:
77		do	case $1 in
78			[#]*)	shift
79				echo " * $*"
80				read line
81				set '' $line
82				shift
83				;;
84			*)	break
85				;;
86			esac
87		done
88		echo " */"
89		echo
90		;;
91	esac
92	eval set '""' $line
93	shift
94	case $1 in
95	"")	;;
96	%flags|%keywords|%sequence)
97		case $define:$last in
98		1:?*)	case $shell in
99			ksh)	((n=counter-1)) ;;
100			*)	n=`expr $counter - 1` ;;
101			esac
102			echo "#define $prefix$last	$n"
103			;;
104		esac
105		case $type in
106		%flags|%sequence)
107			if	test $define = 1
108			then	echo
109			fi
110			;;
111		%keywords)
112			if	test $table = 1
113			then	echo "	0,	0"
114				echo "};"
115				echo
116			elif	test $define = 1
117			then	echo
118			fi
119			;;
120		esac
121		case $index in
122		?*)	eval $index=$counter ;;
123		esac
124		type=$1
125		shift
126		name=""
127		prefix=""
128		index=""
129		init=""
130		first=""
131		last=""
132		case $type in
133		%keywords)
134			case $1 in
135			"")	echo "$command: ${err_file}line $err_line: $type table name omitted" >&2
136				exit 1
137				;;
138			esac
139			name=$1
140			shift
141			if	test $table = 1
142			then	echo "$name"'[] ='
143				echo "{"
144			fi
145			;;
146		esac
147		eval "$@"
148		case $init in
149		"")	case $type in
150			%flags|%sequence)
151				init=0
152				;;
153			*)	init=1
154				;;
155			esac
156			;;
157		esac
158		case $index in
159		"")	counter=$init
160			;;
161		*)	eval value=\$$index
162			case $value in
163			"")		counter=$init ;;
164			[0123456789]*)	counter=$value ;;
165			esac
166			;;
167		esac
168		case $define:$first in
169		1:?*)	echo "#define $prefix$first	$counter" ;;
170		esac
171		;;
172	%*)	echo "$command: ${err_file}line $err_line: $1: unknown keyword" >&2
173		exit 1
174		;;
175	*)	while	:
176		do	case $1 in
177			"")	break
178				;;
179			*)	case $shell in
180				ksh)	ID=${1#[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]} ;;
181				*)	ID=`echo $1 | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ | sed 's/^[^ABCDEFGHIJKLMNOPQRSTUVWXYZ_]//'` ;;
182				esac
183				case $type in
184				%flags)	if	test $define = 1
185					then	case $counter in
186						32) echo "$command: ${err_file}line $err_line: warning: $1: too many flag bits" >&2 ;;
187						1[56789]|[23][0123456789]) long=L ;;
188						*) long= ;;
189						esac
190						echo "#define $prefix$ID	(1$long<<$counter)"
191					fi
192					;;
193				%keywords)
194					if	test $define = 1
195					then	echo "#define $prefix$ID	$counter"
196					fi
197					if	test $table = 1
198					then	echo "	\"$1\",	$prefix$ID,"
199					fi
200					;;
201				%sequence)
202					if	test $define = 1
203					then	echo "#define $prefix$ID	$counter"
204					fi
205					;;
206				esac
207				case $shell in
208				ksh)	((counter=counter+1)) ;;
209				*)	counter=`expr $counter + 1` ;;
210				esac
211				shift
212				;;
213			esac
214		done
215		;;
216	esac
217done
218case $define:$last in
2191:?*)	case $shell in
220	ksh)	((n=counter-1)) ;;
221	*)	n=`expr $counter - 1` ;;
222	esac
223	echo "#define $prefix$last	$n"
224	;;
225esac
226case $type in
227%keywords)
228	if	test $table = 1
229	then	echo "	0,	0"
230		echo "};"
231	fi
232	;;
233esac
234exit 0
235