1 | #! /bin/sh
|
---|
2 | # $NetBSD: mknodes.sh,v 1.1 2004/01/16 23:24:38 dsl Exp $
|
---|
3 |
|
---|
4 | # Copyright (c) 2003 The NetBSD Foundation, Inc.
|
---|
5 | # All rights reserved.
|
---|
6 | #
|
---|
7 | # This code is derived from software contributed to The NetBSD Foundation
|
---|
8 | # by David Laight.
|
---|
9 | #
|
---|
10 | # Redistribution and use in source and binary forms, with or without
|
---|
11 | # modification, are permitted provided that the following conditions
|
---|
12 | # are met:
|
---|
13 | # 1. Redistributions of source code must retain the above copyright
|
---|
14 | # notice, this list of conditions and the following disclaimer.
|
---|
15 | # 2. Redistributions in binary form must reproduce the above copyright
|
---|
16 | # notice, this list of conditions and the following disclaimer in the
|
---|
17 | # documentation and/or other materials provided with the distribution.
|
---|
18 | # 3. Neither the name of The NetBSD Foundation nor the names of its
|
---|
19 | # contributors may be used to endorse or promote products derived
|
---|
20 | # from this software without specific prior written permission.
|
---|
21 | #
|
---|
22 | # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
---|
23 | # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
---|
24 | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
25 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
---|
26 | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
27 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
28 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
29 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
30 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
31 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
32 | # POSSIBILITY OF SUCH DAMAGE.
|
---|
33 |
|
---|
34 | nodetypes=$1
|
---|
35 | nodes_pat=$2
|
---|
36 | objdir="$3"
|
---|
37 |
|
---|
38 | exec <$nodetypes
|
---|
39 | exec >$objdir/nodes.h.tmp
|
---|
40 |
|
---|
41 | echo "/*"
|
---|
42 | echo " * This file was generated by mknodes.sh"
|
---|
43 | echo " */"
|
---|
44 | echo
|
---|
45 |
|
---|
46 | tagno=0
|
---|
47 | while IFS=; read -r line; do
|
---|
48 | line="${line%%#*}"
|
---|
49 | IFS=' '
|
---|
50 | set -- $line
|
---|
51 | IFS=
|
---|
52 | [ -z "$2" ] && continue
|
---|
53 | case "$line" in
|
---|
54 | [" "]* )
|
---|
55 | IFS=' '
|
---|
56 | [ $field = 0 ] && struct_list="$struct_list $struct"
|
---|
57 | eval field_${struct}_$field=\"\$*\"
|
---|
58 | eval numfld_$struct=\$field
|
---|
59 | field=$(($field + 1))
|
---|
60 | ;;
|
---|
61 | * )
|
---|
62 | define=$1
|
---|
63 | struct=$2
|
---|
64 | echo "#define $define $tagno"
|
---|
65 | tagno=$(($tagno + 1))
|
---|
66 | eval define_$struct=\"\$define_$struct \$define\"
|
---|
67 | struct_define="$struct_define $struct"
|
---|
68 | field=0
|
---|
69 | ;;
|
---|
70 | esac
|
---|
71 | done
|
---|
72 |
|
---|
73 | echo
|
---|
74 |
|
---|
75 | ## @todo inconsistent indentation here.
|
---|
76 | IFS=' '
|
---|
77 | for struct in $struct_list; do
|
---|
78 | echo
|
---|
79 | echo
|
---|
80 | echo "struct $struct {"
|
---|
81 | echo "#ifdef KASH_SEPARATE_PARSER_ALLOCATOR"
|
---|
82 | echo " struct pstack_block *pblock;"
|
---|
83 | echo "#endif"
|
---|
84 | field=0
|
---|
85 | while
|
---|
86 | eval line=\"\$field_${struct}_$field\"
|
---|
87 | field=$(($field + 1))
|
---|
88 | [ -n "$line" ]
|
---|
89 | do
|
---|
90 | IFS=' '
|
---|
91 | set -- $line
|
---|
92 | name=$1
|
---|
93 | case $2 in
|
---|
94 | nodeptr ) type="union node *";;
|
---|
95 | nodelist ) type="struct nodelist *";;
|
---|
96 | string ) type="char *";;
|
---|
97 | int ) type="int ";;
|
---|
98 | * ) name=; shift 2; type="$*";;
|
---|
99 | esac
|
---|
100 | echo " $type$name;"
|
---|
101 | done
|
---|
102 | echo "};"
|
---|
103 | done
|
---|
104 |
|
---|
105 | echo
|
---|
106 | echo
|
---|
107 | echo "union node {"
|
---|
108 | echo "#ifdef KASH_SEPARATE_PARSER_ALLOCATOR"
|
---|
109 | echo "# ifdef __GNUC__"
|
---|
110 | echo " __extension__"
|
---|
111 | echo "# endif"
|
---|
112 | echo " struct {"
|
---|
113 | echo " struct pstack_block *pblock;"
|
---|
114 | echo " int type;"
|
---|
115 | echo " };"
|
---|
116 | echo "#else"
|
---|
117 | echo " int type;"
|
---|
118 | echo "#endif"
|
---|
119 | for struct in $struct_list; do
|
---|
120 | echo " struct $struct $struct;"
|
---|
121 | done
|
---|
122 | echo "};"
|
---|
123 | echo
|
---|
124 | echo
|
---|
125 | echo "struct nodelist {"
|
---|
126 | echo " struct nodelist *next;"
|
---|
127 | echo " union node *n;"
|
---|
128 | echo "};"
|
---|
129 | echo
|
---|
130 | echo
|
---|
131 | echo "union node *copyfunc(struct shinstance *, union node *);"
|
---|
132 | echo "void freefunc(struct shinstance *, union node *);"
|
---|
133 |
|
---|
134 | exec <$nodes_pat
|
---|
135 | exec >$objdir/nodes.c.tmp
|
---|
136 | mv -f $objdir/nodes.h.tmp $objdir/nodes.h || exit 1
|
---|
137 |
|
---|
138 | echo "/*"
|
---|
139 | echo " * This file was generated by mknodes.sh"
|
---|
140 | echo " */"
|
---|
141 | echo
|
---|
142 |
|
---|
143 | while IFS=; read -r line; do
|
---|
144 | IFS=' '
|
---|
145 | set -- $line
|
---|
146 | IFS=
|
---|
147 | case "$1" in
|
---|
148 | '%SIZES' )
|
---|
149 | echo "static const short nodesize[$tagno] = {"
|
---|
150 | IFS=' '
|
---|
151 | for struct in $struct_define; do
|
---|
152 | echo " SHELL_ALIGN(sizeof (struct $struct)),"
|
---|
153 | done
|
---|
154 | echo "};"
|
---|
155 | ;;
|
---|
156 | '%CALCSIZE' )
|
---|
157 | echo " if (n == NULL)"
|
---|
158 | echo " return;"
|
---|
159 | echo " funcblocksize += nodesize[n->type];"
|
---|
160 | echo " switch (n->type) {"
|
---|
161 | IFS=' '
|
---|
162 | for struct in $struct_list; do
|
---|
163 | eval defines=\"\$define_$struct\"
|
---|
164 | for define in $defines; do
|
---|
165 | echo " case $define:"
|
---|
166 | done
|
---|
167 | eval field=\$numfld_$struct
|
---|
168 | while
|
---|
169 | [ $field != 0 ]
|
---|
170 | do
|
---|
171 | eval line=\"\$field_${struct}_$field\"
|
---|
172 | field=$(($field - 1))
|
---|
173 | IFS=' '
|
---|
174 | set -- $line
|
---|
175 | name=$1
|
---|
176 | cl=")"
|
---|
177 | case $2 in
|
---|
178 | nodeptr ) fn=calcsize;;
|
---|
179 | nodelist ) fn=sizenodelist;;
|
---|
180 | string ) fn="funcstringsize += strlen"
|
---|
181 | cl=") + 1";;
|
---|
182 | * ) continue;;
|
---|
183 | esac
|
---|
184 | echo " ${fn}(n->$struct.$name${cl};"
|
---|
185 | done
|
---|
186 | echo " break;"
|
---|
187 | done
|
---|
188 | echo " };"
|
---|
189 | ;;
|
---|
190 | '%COPY' )
|
---|
191 | echo " if (n == NULL)"
|
---|
192 | echo " return NULL;"
|
---|
193 | echo " new = funcblock;"
|
---|
194 | echo " funcblock = (char *) funcblock + nodesize[n->type];"
|
---|
195 | echo " switch (n->type) {"
|
---|
196 | IFS=' '
|
---|
197 | for struct in $struct_list; do
|
---|
198 | eval defines=\"\$define_$struct\"
|
---|
199 | for define in $defines; do
|
---|
200 | echo " case $define:"
|
---|
201 | done
|
---|
202 | eval field=\$numfld_$struct
|
---|
203 | while
|
---|
204 | [ $field != 0 ]
|
---|
205 | do
|
---|
206 | eval line=\"\$field_${struct}_$field\"
|
---|
207 | field=$(($field - 1))
|
---|
208 | IFS=' '
|
---|
209 | set -- $line
|
---|
210 | name=$1
|
---|
211 | case $2 in
|
---|
212 | nodeptr ) fn="copynode(";;
|
---|
213 | nodelist ) fn="copynodelist(";;
|
---|
214 | string ) fn="nodesavestr(";;
|
---|
215 | int ) fn=;;
|
---|
216 | temp ) echo "unexpected 'temp' node type" >&2; exit 2;;
|
---|
217 | * ) continue;;
|
---|
218 | esac
|
---|
219 | f="$struct.$name"
|
---|
220 | echo " new->$f = ${fn}n->$f${fn:+)};"
|
---|
221 | done
|
---|
222 | echo " break;"
|
---|
223 | done
|
---|
224 | echo " };"
|
---|
225 | echo " new->type = n->type;"
|
---|
226 | ;;
|
---|
227 | * ) echo "$line";;
|
---|
228 | esac
|
---|
229 | done
|
---|
230 |
|
---|
231 | exec >/dev/null
|
---|
232 | mv -f $objdir/nodes.c.tmp $objdir/nodes.c || exit 1
|
---|