ia64/xen-unstable

view xen/tools/get-fields.sh @ 13647:057c4c2991fa

32-on-64: Fix an one-off error in compat_addr_ok().
Signed-off-by: Jan Beulich <jbeulich@novell.com>
author kaf24@localhost.localdomain
date Fri Jan 26 13:36:52 2007 +0000 (2007-01-26)
parents dccc51cac027
children 7b72e2536065
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_)"
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 }
309 check_field() {
310 if [ -z "$(echo "$4" | $SED 's,[^{}],,g')" ]
311 then
312 echo "; \\"
313 local n=$(echo $3 | $SED 's,[^.], ,g' | wc -w | $SED 's,[[:space:]]*,,g')
314 if [ -n "$4" ]
315 then
316 for n in $4
317 do
318 case $n in
319 struct|union)
320 ;;
321 [[:alpha:]_]*)
322 echo -n " CHECK_$n"
323 break
324 ;;
325 *)
326 echo "Malformed compound declaration: '$n'" >&2
327 exit 1
328 ;;
329 esac
330 done
331 elif [ $n = 0 ]
332 then
333 echo -n " CHECK_FIELD_($1, $2, $3)"
334 else
335 echo -n " CHECK_SUBFIELD_${n}_($1, $2, $(echo $3 | $SED 's!\.!, !g'))"
336 fi
337 else
338 local level=1 fields= id= token
339 for token in $4
340 do
341 case "$token" in
342 struct|union)
343 test $level != 2 || fields=" "
344 ;;
345 "{")
346 level=$(expr $level + 1) id=
347 ;;
348 "}")
349 level=$(expr $level - 1) id=
350 ;;
351 [[:alpha:]]*)
352 id=$token
353 ;;
354 [\,\;])
355 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
356 then
357 check_field $1 $2 $3.$id "$fields"
358 test "$token" != ";" || fields= id=
359 fi
360 ;;
361 esac
362 test -z "$fields" || fields="$fields $token"
363 done
364 fi
365 }
367 build_check() {
368 echo
369 echo "#define CHECK_$1 \\"
370 local level=1 fields= kind= id= arrlvl=1 token
371 for token in $2
372 do
373 case "$token" in
374 struct|union)
375 if [ $level = 1 ]
376 then
377 kind=$token
378 echo -n " CHECK_SIZE_($kind, $1)"
379 elif [ $level = 2 ]
380 then
381 fields=" "
382 fi
383 ;;
384 "{")
385 level=$(expr $level + 1) id=
386 ;;
387 "}")
388 level=$(expr $level - 1) id=
389 ;;
390 "[")
391 arrlvl=$(expr $arrlvl + 1)
392 ;;
393 "]")
394 arrlvl=$(expr $arrlvl - 1)
395 ;;
396 [[:alpha:]_]*)
397 test $level != 2 -o $arrlvl != 1 || id=$token
398 ;;
399 [\,\;])
400 if [ $level = 2 -a -n "$(echo $id | $SED 's,^_pad[[:digit:]]*,,')" ]
401 then
402 check_field $kind $1 $id "$fields"
403 test "$token" != ";" || fields= id=
404 fi
405 ;;
406 esac
407 test -z "$fields" || fields="$fields $token"
408 done
409 echo ""
410 }
412 fields="$(get_fields $(echo $2 | $SED 's,^compat_xen,compat_,') "$($SED -e 's,^[[:space:]]#.*,,' -e 's!\([]\[,;:{}]\)! \1 !g' $3)")"
413 if [ -z "$fields" ]
414 then
415 echo "Fields of '$2' not found in '$3'" >&2
416 exit 1
417 fi
418 name=${2#compat_}
419 name=${name#xen}
420 case "$1" in
421 "!")
422 build_enums $name "$fields"
423 build_body $name "$fields"
424 ;;
425 "?")
426 build_check $name "$fields"
427 ;;
428 *)
429 echo "Invalid translation indicator: '$1'" >&2
430 exit 1
431 ;;
432 esac