ia64/xen-unstable

view xen/tools/get-fields.sh @ 15910:c36292e83f07

Fix two memory leaks in xend.
Signed-off-by: Kouya Shimura <kouya@jp.fujitsu.com>
author kfraser@localhost.localdomain
date Fri Sep 14 16:04:33 2007 +0100 (2007-09-14)
parents 7b72e2536065
children 08295dc13166
line source
1 #!/bin/bash
2 test -n "$1" -a -n "$2" -a -n "$3"
3 set -ef
5 SED=sed
6 [ -x /usr/xpg4/bin/sed ] && SED=/usr/xpg4/bin/sed
8 get_fields() {
9 local level=1 aggr=0 name= fields=
10 for token in $2
11 do
12 case "$token" in
13 struct|union)
14 test $level != 1 || aggr=1 fields= name=
15 ;;
16 "{")
17 level=$(expr $level + 1)
18 ;;
19 "}")
20 level=$(expr $level - 1)
21 if [ $level = 1 -a $name = $1 ]
22 then
23 echo "$fields }"
24 return 0
25 fi
26 ;;
27 [[:alpha:]_]*)
28 test $aggr = 0 -o -n "$name" || name="$token"
29 ;;
30 esac
31 test $aggr = 0 || fields="$fields $token"
32 done
33 }
35 build_enums() {
36 local level=1 kind= fields= members= named= id= token
37 for token in $2
38 do
39 case "$token" in
40 struct|union)
41 test $level != 2 || fields=" "
42 kind="$token;$kind"
43 ;;
44 "{")
45 level=$(expr $level + 1)
46 ;;
47 "}")
48 level=$(expr $level - 1)
49 if [ $level = 1 ]
50 then
51 if [ "${kind%%;*}" = union ]
52 then
53 echo
54 echo "enum XLAT_$1 {"
55 for m in $members
56 do
57 echo " XLAT_${1}_$m,"
58 done
59 echo "};"
60 fi
61 return 0
62 elif [ $level = 2 ]
63 then
64 named='?'
65 fi
66 ;;
67 [[:alpha:]]*)
68 id=$token
69 if [ -n "$named" -a -n "${kind#*;}" ]
70 then
71 build_enums ${1}_$token "$fields"
72 named='!'
73 fi
74 ;;
75 ",")
76 test $level != 2 || members="$members $id"
77 ;;
78 ";")
79 test $level != 2 || members="$members $id"
80 test -z "$named" || kind=${kind#*;}
81 named=
82 ;;
83 esac
84 test -z "$fields" || fields="$fields $token"
85 done
86 }
88 handle_field() {
89 if [ -z "$5" ]
90 then
91 echo " \\"
92 if [ -z "$4" ]
93 then
94 echo -n "$1(_d_)->$3 = (_s_)->$3;"
95 else
96 echo -n "$1XLAT_${2}_HNDL_$(echo $3 | $SED 's,\.,_,g')(_d_, _s_);"
97 fi
98 elif [ -z "$(echo "$5" | $SED 's,[^{}],,g')" ]
99 then
100 local tag=$(echo "$5" | python -c '
101 import re,sys
102 for line in sys.stdin.readlines():
103 print re.subn(r"\s*(struct|union)\s+(compat_)?(\w+)\s.*", r"\3", line)[0].rstrip()
104 ')
105 echo " \\"
106 echo -n "${1}XLAT_$tag(&(_d_)->$3, &(_s_)->$3);"
107 else
108 local level=1 kind= fields= id= array= arrlvl=1 array_type= type= token
109 for token in $5
110 do
111 case "$token" in
112 struct|union)
113 test $level != 2 || fields=" "
114 if [ $level = 1 ]
115 then
116 kind=$token
117 if [ $kind = union ]
118 then
119 echo " \\"
120 echo -n "${1}switch ($(echo $3 | $SED 's,\.,_,g')) {"
121 fi
122 fi
123 ;;
124 "{")
125 level=$(expr $level + 1) id=
126 ;;
127 "}")
128 level=$(expr $level - 1) id=
129 if [ $level = 1 -a $kind = union ]
130 then
131 echo " \\"
132 echo -n "$1}"
133 fi
134 ;;
135 "[")
136 if [ $level != 2 -o $arrlvl != 1 ]
137 then
138 :
139 elif [ -z "$array" ]
140 then
141 array=" "
142 else
143 array="$array;"
144 fi
145 arrlvl=$(expr $arrlvl + 1)
146 ;;
147 "]")
148 arrlvl=$(expr $arrlvl - 1)
149 ;;
150 COMPAT_HANDLE\(*\))
151 if [ $level = 2 -a -z "$id" ]
152 then
153 type=${token#COMPAT_HANDLE?}
154 type=${type%?}
155 type=${type#compat_}
156 fi
157 ;;
158 compat_domain_handle_t)
159 if [ $level = 2 -a -z "$id" ]
160 then
161 array_type=$token
162 fi
163 ;;
164 [[:alpha:]]*)
165 id=$token
166 ;;
167 [\,\;])
168 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
169 then
170 if [ $kind = union ]
171 then
172 echo " \\"
173 echo -n "${1}case XLAT_${2}_$(echo $3.$id | $SED 's,\.,_,g'):"
174 handle_field "$1 " $2 $3.$id "$type" "$fields"
175 elif [ -z "$array" -a -z "$array_type" ]
176 then
177 handle_field "$1" $2 $3.$id "$type" "$fields"
178 elif [ -z "$array" ]
179 then
180 copy_array " " $3.$id
181 else
182 handle_array "$1" $2 $3.$id "${array#*;}" "$type" "$fields"
183 fi
184 test "$token" != ";" || fields= id= type=
185 array=
186 if [ $kind = union ]
187 then
188 echo " \\"
189 echo -n "$1 break;"
190 fi
191 fi
192 ;;
193 *)
194 if [ -n "$array" ]
195 then
196 array="$array $token"
197 fi
198 ;;
199 esac
200 test -z "$fields" || fields="$fields $token"
201 done
202 fi
203 }
205 copy_array() {
206 echo " \\"
207 echo "${1}if ((_d_)->$2 != (_s_)->$2) \\"
208 echo -n "$1 memcpy((_d_)->$2, (_s_)->$2, sizeof((_d_)->$2));"
209 }
211 handle_array() {
212 local i="i$(echo $4 | $SED 's,[^;], ,g' | wc -w | $SED 's,[[:space:]]*,,g')"
213 echo " \\"
214 echo "$1{ \\"
215 echo "$1 unsigned int $i; \\"
216 echo -n "$1 for ($i = 0; $i < "${4%%;*}"; ++$i) {"
217 if [ "$4" = "${4#*;}" ]
218 then
219 handle_field "$1 " $2 $3[$i] "$5" "$6"
220 else
221 handle_array "$1 " $2 $3[$i] "${4#*;}" "$5" "$6"
222 fi
223 echo " \\"
224 echo "$1 } \\"
225 echo -n "$1}"
226 }
228 build_body() {
229 echo
230 echo -n "#define XLAT_$1(_d_, _s_) do {"
231 local level=1 fields= id= array= arrlvl=1 array_type= type= token
232 for token in $2
233 do
234 case "$token" in
235 struct|union)
236 test $level != 2 || fields=" "
237 ;;
238 "{")
239 level=$(expr $level + 1) id=
240 ;;
241 "}")
242 level=$(expr $level - 1) id=
243 ;;
244 "[")
245 if [ $level != 2 -o $arrlvl != 1 ]
246 then
247 :
248 elif [ -z "$array" ]
249 then
250 array=" "
251 else
252 array="$array;"
253 fi
254 arrlvl=$(expr $arrlvl + 1)
255 ;;
256 "]")
257 arrlvl=$(expr $arrlvl - 1)
258 ;;
259 COMPAT_HANDLE\(*\))
260 if [ $level = 2 -a -z "$id" ]
261 then
262 type=${token#COMPAT_HANDLE?}
263 type=${type%?}
264 type=${type#compat_}
265 fi
266 ;;
267 compat_domain_handle_t)
268 if [ $level = 2 -a -z "$id" ]
269 then
270 array_type=$token
271 fi
272 ;;
273 [[:alpha:]_]*)
274 if [ -n "$array" ]
275 then
276 array="$array $token"
277 else
278 id=$token
279 fi
280 ;;
281 [\,\;])
282 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
283 then
284 if [ -z "$array" -a -z "$array_type" ]
285 then
286 handle_field " " $1 $id "$type" "$fields"
287 elif [ -z "$array" ]
288 then
289 copy_array " " $id
290 else
291 handle_array " " $1 $id "${array#*;}" "$type" "$fields"
292 fi
293 test "$token" != ";" || fields= id= type=
294 array=
295 fi
296 ;;
297 *)
298 if [ -n "$array" ]
299 then
300 array="$array $token"
301 fi
302 ;;
303 esac
304 test -z "$fields" || fields="$fields $token"
305 done
306 echo " \\"
307 echo "} while (0)"
308 echo ""
309 }
311 check_field() {
312 if [ -z "$(echo "$4" | $SED 's,[^{}],,g')" ]
313 then
314 echo "; \\"
315 local n=$(echo $3 | $SED 's,[^.], ,g' | wc -w | $SED 's,[[:space:]]*,,g')
316 if [ -n "$4" ]
317 then
318 for n in $4
319 do
320 case $n in
321 struct|union)
322 ;;
323 [[:alpha:]_]*)
324 echo -n " CHECK_$n"
325 break
326 ;;
327 *)
328 echo "Malformed compound declaration: '$n'" >&2
329 exit 1
330 ;;
331 esac
332 done
333 elif [ $n = 0 ]
334 then
335 echo -n " CHECK_FIELD_($1, $2, $3)"
336 else
337 echo -n " CHECK_SUBFIELD_${n}_($1, $2, $(echo $3 | $SED 's!\.!, !g'))"
338 fi
339 else
340 local level=1 fields= id= token
341 for token in $4
342 do
343 case "$token" in
344 struct|union)
345 test $level != 2 || fields=" "
346 ;;
347 "{")
348 level=$(expr $level + 1) id=
349 ;;
350 "}")
351 level=$(expr $level - 1) id=
352 ;;
353 [[:alpha:]]*)
354 id=$token
355 ;;
356 [\,\;])
357 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
358 then
359 check_field $1 $2 $3.$id "$fields"
360 test "$token" != ";" || fields= id=
361 fi
362 ;;
363 esac
364 test -z "$fields" || fields="$fields $token"
365 done
366 fi
367 }
369 build_check() {
370 echo
371 echo "#define CHECK_$1 \\"
372 local level=1 fields= kind= id= arrlvl=1 token
373 for token in $2
374 do
375 case "$token" in
376 struct|union)
377 if [ $level = 1 ]
378 then
379 kind=$token
380 echo -n " CHECK_SIZE_($kind, $1)"
381 elif [ $level = 2 ]
382 then
383 fields=" "
384 fi
385 ;;
386 "{")
387 level=$(expr $level + 1) id=
388 ;;
389 "}")
390 level=$(expr $level - 1) id=
391 ;;
392 "[")
393 arrlvl=$(expr $arrlvl + 1)
394 ;;
395 "]")
396 arrlvl=$(expr $arrlvl - 1)
397 ;;
398 [[:alpha:]_]*)
399 test $level != 2 -o $arrlvl != 1 || id=$token
400 ;;
401 [\,\;])
402 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
403 then
404 check_field $kind $1 $id "$fields"
405 test "$token" != ";" || fields= id=
406 fi
407 ;;
408 esac
409 test -z "$fields" || fields="$fields $token"
410 done
411 echo ""
412 }
414 fields="$(get_fields $(echo $2 | $SED 's,^compat_xen,compat_,') "$($SED -e 's,^[[:space:]]#.*,,' -e 's!\([]\[,;:{}]\)! \1 !g' $3)")"
415 if [ -z "$fields" ]
416 then
417 echo "Fields of '$2' not found in '$3'" >&2
418 exit 1
419 fi
420 name=${2#compat_}
421 name=${name#xen}
422 case "$1" in
423 "!")
424 build_enums $name "$fields"
425 build_body $name "$fields"
426 ;;
427 "?")
428 build_check $name "$fields"
429 ;;
430 *)
431 echo "Invalid translation indicator: '$1'" >&2
432 exit 1
433 ;;
434 esac