summaryrefslogtreecommitdiffstats
path: root/src/kash/mknodes.sh
blob: f11fc2efaf0cd0fbc808635ea33f0efd023ec6d0 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#! /bin/sh
#	$NetBSD: mknodes.sh,v 1.1 2004/01/16 23:24:38 dsl Exp $

# Copyright (c) 2003 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by David Laight.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

nodetypes=$1
nodes_pat=$2
objdir="$3"

exec <$nodetypes
exec >$objdir/nodes.h.tmp

echo "/*"
echo " * This file was generated by mknodes.sh"
echo " */"
echo

tagno=0
while IFS=; read -r line; do
	line="${line%%#*}"
	IFS=' 	'
	set -- $line
	IFS=
	[ -z "$2" ] && continue
	case "$line" in
	[" 	"]* )
		IFS=' '
		[ $field = 0 ] && struct_list="$struct_list $struct"
		eval field_${struct}_$field=\"\$*\"
		eval numfld_$struct=\$field
		field=$(($field + 1))
		;;
	* )
		define=$1
		struct=$2
		echo "#define $define $tagno"
		tagno=$(($tagno + 1))
		eval define_$struct=\"\$define_$struct \$define\"
		struct_define="$struct_define $struct"
		field=0
		;;
	esac
done

echo

## @todo inconsistent indentation here.
IFS=' '
for struct in $struct_list; do
	echo
	echo
	echo "struct $struct {"
        echo "#ifdef KASH_SEPARATE_PARSER_ALLOCATOR"
        echo "	struct pstack_block *pblock;"
        echo "#endif"
	field=0
	while
		eval line=\"\$field_${struct}_$field\"
		field=$(($field + 1))
		[ -n "$line" ]
	do
		IFS=' '
		set -- $line
		name=$1
		case $2 in
		nodeptr ) type="union node *";;
		nodelist ) type="struct nodelist *";;
		string ) type="char *";;
		int ) type="int ";;
		* ) name=; shift 2; type="$*";;
		esac
		echo "	$type$name;"
	done
	echo "};"
done

echo
echo
echo "union node {"
echo "#ifdef KASH_SEPARATE_PARSER_ALLOCATOR"
echo "# ifdef __GNUC__"
echo "	__extension__"
echo "# endif"
echo "	struct {"
echo "		struct pstack_block *pblock;"
echo "		int type;"
echo "	};"
echo "#else"
echo "	int type;"
echo "#endif"
for struct in $struct_list; do
	echo "	struct $struct $struct;"
done
echo "};"
echo
echo
echo "struct nodelist {"
echo "	struct nodelist *next;"
echo "	union node *n;"
echo "};"
echo
echo
echo "union node *copyfunc(struct shinstance *, union node *);"
echo "void freefunc(struct shinstance *, union node *);"

exec <$nodes_pat
exec >$objdir/nodes.c.tmp
mv -f $objdir/nodes.h.tmp $objdir/nodes.h || exit 1

echo "/*"
echo " * This file was generated by mknodes.sh"
echo " */"
echo

while IFS=; read -r line; do
	IFS=' 	'
	set -- $line
	IFS=
	case "$1" in
	'%SIZES' )
		echo "static const short nodesize[$tagno] = {"
		IFS=' '
		for struct in $struct_define; do
			echo "      SHELL_ALIGN(sizeof (struct $struct)),"
		done
		echo "};"
		;;
	'%CALCSIZE' )
		echo "      if (n == NULL)"
		echo "	    return;"
		echo "      funcblocksize += nodesize[n->type];"
		echo "      switch (n->type) {"
		IFS=' '
		for struct in $struct_list; do
			eval defines=\"\$define_$struct\"
			for define in $defines; do
				echo "      case $define:"
			done
			eval field=\$numfld_$struct
			while
				[ $field != 0 ]
			do
				eval line=\"\$field_${struct}_$field\"
				field=$(($field - 1))
				IFS=' '
				set -- $line
				name=$1
				cl=")"
				case $2 in
				nodeptr ) fn=calcsize;;
				nodelist ) fn=sizenodelist;;
				string ) fn="funcstringsize += strlen"
					cl=") + 1";;
				* ) continue;;
				esac
				echo "	    ${fn}(n->$struct.$name${cl};"
			done
			echo "	    break;"
		done
		echo "      };"
		;;
	'%COPY' )
		echo "      if (n == NULL)"
		echo "	    return NULL;"
		echo "      new = funcblock;"
		echo "      funcblock = (char *) funcblock + nodesize[n->type];"
		echo "      switch (n->type) {"
		IFS=' '
		for struct in $struct_list; do
			eval defines=\"\$define_$struct\"
			for define in $defines; do
				echo "      case $define:"
			done
			eval field=\$numfld_$struct
			while
				[ $field != 0 ]
			do
				eval line=\"\$field_${struct}_$field\"
				field=$(($field - 1))
				IFS=' '
				set -- $line
				name=$1
				case $2 in
				nodeptr ) fn="copynode(";;
				nodelist ) fn="copynodelist(";;
				string ) fn="nodesavestr(";;
				int ) fn=;;
				temp )  echo "unexpected 'temp' node type" >&2; exit 2;;
				* ) continue;;
				esac
				f="$struct.$name"
				echo "	    new->$f = ${fn}n->$f${fn:+)};"
			done
			echo "	    break;"
		done
		echo "      };"
		echo "      new->type = n->type;"
		;;
	* ) echo "$line";;
	esac
done

exec >/dev/null
mv -f $objdir/nodes.c.tmp $objdir/nodes.c || exit 1