ia64/xen-unstable

view xen/tools/get-fields.sh @ 18842:9be2fe3de567

shadow: fix the fix for promotion/resync race.

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