OSDN Git Service

PR go/52084
[pf3gnuchains/gcc-fork.git] / libgo / mksysinfo.sh
1 #!/bin/sh
2
3 # Copyright 2009 The Go Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
6
7 # Create sysinfo.go.
8
9 # This shell script creates the sysinfo.go file which holds types and
10 # constants extracted from the system header files.  This relies on a
11 # hook in gcc: the -fdump-go-spec option will generate debugging
12 # information in Go syntax.
13
14 # We currently #include all the files at once, which works, but leads
15 # to exposing some names which ideally should not be exposed, as they
16 # match grep patterns.  E.g., WCHAR_MIN gets exposed because it starts
17 # with W, like the wait flags.
18
19 CC=${CC:-gcc}
20 OUT=tmp-sysinfo.go
21
22 set -e
23
24 rm -f sysinfo.c
25 cat > sysinfo.c <<EOF
26 #include "config.h"
27
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <netinet/in.h>
33 /* <netinet/tcp.h> needs u_char/u_short, but <sys/bsd_types> is only
34    included by <netinet/in.h> if _SGIAPI (i.e. _SGI_SOURCE
35    && !_XOPEN_SOURCE.
36    <sys/termios.h> only defines TIOCNOTTY if !_XOPEN_SOURCE, while
37    <sys/ttold.h> does so unconditionally.  */
38 #ifdef __sgi__
39 #include <sys/bsd_types.h>
40 #include <sys/ttold.h>
41 #endif
42 #include <netinet/tcp.h>
43 #include <signal.h>
44 #include <sys/ioctl.h>
45 #include <termios.h>
46 #if defined(HAVE_SYSCALL_H)
47 #include <syscall.h>
48 #endif
49 #if defined(HAVE_SYS_SYSCALL_H)
50 #include <sys/syscall.h>
51 #endif
52 #if defined(HAVE_SYS_EPOLL_H)
53 #include <sys/epoll.h>
54 #endif
55 #if defined(HAVE_SYS_MMAN_H)
56 #include <sys/mman.h>
57 #endif
58 #if defined(HAVE_SYS_PRCTL_H)
59 #include <sys/prctl.h>
60 #endif
61 #if defined(HAVE_SYS_PTRACE_H)
62 #include <sys/ptrace.h>
63 #endif
64 #include <sys/resource.h>
65 #include <sys/uio.h>
66 #include <sys/socket.h>
67 #include <sys/stat.h>
68 #include <sys/time.h>
69 #include <sys/times.h>
70 #include <sys/wait.h>
71 #include <sys/un.h>
72 #if defined(HAVE_SYS_USER_H)
73 #include <sys/user.h>
74 #endif
75 #if defined(HAVE_SYS_UTSNAME_H)
76 #include <sys/utsname.h>
77 #endif
78 #if defined(HAVE_SYS_SELECT_H)
79 #include <sys/select.h>
80 #endif
81 #include <unistd.h>
82 #include <netdb.h>
83 #include <pwd.h>
84 #if defined(HAVE_LINUX_FILTER_H)
85 #include <linux/filter.h>
86 #endif
87 #if defined(HAVE_LINUX_NETLINK_H)
88 #include <linux/netlink.h>
89 #endif
90 #if defined(HAVE_LINUX_RTNETLINK_H)
91 #include <linux/rtnetlink.h>
92 #endif
93 #if defined(HAVE_NET_IF_H)
94 #include <net/if.h>
95 #endif
96
97 /* Constants that may only be defined as expressions on some systems,
98    expressions too complex for -fdump-go-spec to handle.  These are
99    handled specially below.  */
100 enum {
101 #ifdef TIOCGWINSZ
102   TIOCGWINSZ_val = TIOCGWINSZ,
103 #endif
104 };
105 EOF
106
107 ${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c
108
109 echo 'package syscall' > ${OUT}
110 echo 'import "unsafe"' >> ${OUT}
111 echo 'type _ unsafe.Pointer' >> ${OUT}
112
113 # Get all the consts and types, skipping ones which could not be
114 # represented in Go and ones which we need to rewrite.  We also skip
115 # function declarations, as we don't need them here.  All the symbols
116 # will all have a leading underscore.
117 grep -v '^// ' gen-sysinfo.go | \
118   grep -v '^func' | \
119   grep -v '^type _timeval ' | \
120   grep -v '^type _timespec_t ' | \
121   grep -v '^type _timespec ' | \
122   grep -v '^type _timestruc_t ' | \
123   grep -v '^type _epoll_' | \
124   grep -v 'in6_addr' | \
125   grep -v 'sockaddr_in6' | \
126   sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
127       -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
128       -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
129       -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
130     >> ${OUT}
131
132 # The errno constants.  These get type Errno.
133 echo '#include <errno.h>' | ${CC} -x c - -E -dM | \
134   egrep '#define E[A-Z0-9_]+ ' | \
135   sed -e 's/^#define \(E[A-Z0-9_]*\) .*$/const \1 = Errno(_\1)/' >> ${OUT}
136
137 # The O_xxx flags.
138 egrep '^const _(O|F|FD)_' gen-sysinfo.go | \
139   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
140 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
141   echo "const O_ASYNC = 0" >> ${OUT}
142 fi
143 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
144   echo "const O_CLOEXEC = 0" >> ${OUT}
145 fi
146
147 # The signal numbers.
148 grep '^const _SIG[^_]' gen-sysinfo.go | \
149   grep -v '^const _SIGEV_' | \
150   sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
151
152 # The syscall numbers.  We force the names to upper case.
153 grep '^const _SYS_' gen-sysinfo.go | \
154   sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
155   while read sys; do
156     sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
157     echo "const $sup = _$sys" >> ${OUT}
158   done
159
160 # Stat constants.
161 grep '^const _S_' gen-sysinfo.go | \
162   sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
163
164 # Mmap constants.
165 grep '^const _PROT_' gen-sysinfo.go | \
166   sed -e 's/^\(const \)_\(PROT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
167 grep '^const _MAP_' gen-sysinfo.go | \
168   sed -e 's/^\(const \)_\(MAP_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
169
170 # Process status constants.
171 grep '^const _W' gen-sysinfo.go |
172   sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
173 # WSTOPPED was introduced in glibc 2.3.4.
174 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
175   if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
176     echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
177   else
178     echo 'const WSTOPPED = 2' >> ${OUT}
179   fi
180 fi
181 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
182    && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
183   echo 'const WALL = ___WALL' >> ${OUT}
184 fi
185
186 # Networking constants.
187 egrep '^const _(AF|SOCK|SOL|SO|IPPROTO|TCP|IP|IPV6)_' gen-sysinfo.go |
188   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
189 grep '^const _SOMAXCONN' gen-sysinfo.go |
190   sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
191     >> ${OUT}
192 grep '^const _SHUT_' gen-sysinfo.go |
193   sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
194
195 # The net package requires some const definitions.
196 for m in IP_PKTINFO IPV6_V6ONLY IPPROTO_IPV6 IPV6_JOIN_GROUP IPV6_LEAVE_GROUP IPV6_TCLASS; do
197   if ! grep "^const $m " ${OUT} >/dev/null 2>&1; then
198     echo "const $m = 0" >> ${OUT}
199   fi
200 done
201
202 # pathconf constants.
203 grep '^const __PC' gen-sysinfo.go |
204   sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
205
206 # epoll constants.
207 grep '^const _EPOLL' gen-sysinfo.go |
208   sed -e 's/^\(const \)_\(EPOLL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
209 # Make sure EPOLLRDHUP and EPOLL_CLOEXEC are defined.
210 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
211   echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
212 fi
213 if ! grep '^const EPOLL_CLOEXEC' ${OUT} >/dev/null 2>&1; then
214   echo "const EPOLL_CLOEXEC = 02000000" >> ${OUT}
215 fi
216
217 # Prctl constants.
218 grep '^const _PR_' gen-sysinfo.go |
219   sed -e 's/^\(const \)_\(PR_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
220
221 # Ptrace constants.
222 grep '^const _PTRACE' gen-sysinfo.go |
223   sed -e 's/^\(const \)_\(PTRACE[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
224 # We need some ptrace options that are not defined in older versions
225 # of glibc.
226 if ! grep '^const PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
227   echo "const PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
228 fi
229 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
230   echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
231 fi
232 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
233   echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
234 fi
235 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
236   echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
237 fi
238 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
239   echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
240 fi
241 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
242   echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
243 fi
244 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
245   echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
246 fi
247 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
248   echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
249 fi
250 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
251   echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
252 fi
253 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
254   echo "const PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
255 fi
256 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
257   echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
258 fi
259 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
260   echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
261 fi
262 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
263   echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
264 fi
265 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
266   echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
267 fi
268 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
269   echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
270 fi
271 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
272   echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
273 fi
274 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
275   echo "const _PTRACE_TRACEME = 0" >> ${OUT}
276 fi
277
278 # The registers returned by PTRACE_GETREGS.  This is probably
279 # GNU/Linux specific; it should do no harm if there is no
280 # _user_regs_struct.
281 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
282 if test "$regs" != ""; then
283   regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
284   regs=`echo $regs | sed -e s'/^ *//'`
285   nregs=
286   while test -n "$regs"; do
287     field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
288     regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
289     # Capitalize the first character of the field.
290     f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
291     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
292     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
293     field="$f$r"
294     nregs="$nregs $field;"
295   done
296   echo "type PtraceRegs struct {$nregs }" >> ${OUT}
297 fi
298
299 # Some basic types.
300 echo 'type Size_t _size_t' >> ${OUT}
301 echo "type Ssize_t _ssize_t" >> ${OUT}
302 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
303   echo "type Offset_t _off64_t" >> ${OUT}
304 else
305   echo "type Offset_t _off_t" >> ${OUT}
306 fi
307 echo "type Mode_t _mode_t" >> ${OUT}
308 echo "type Pid_t _pid_t" >> ${OUT}
309 echo "type Uid_t _uid_t" >> ${OUT}
310 echo "type Gid_t _gid_t" >> ${OUT}
311 echo "type Socklen_t _socklen_t" >> ${OUT}
312
313 # The long type, needed because that is the type that ptrace returns.
314 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
315 if test "$sizeof_long" = "4"; then
316   echo "type _C_long int32" >> ${OUT}
317 elif test "$sizeof_long" = "8"; then
318   echo "type _C_long int64" >> ${OUT}
319 else
320   echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
321   exit 1
322 fi
323
324 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
325 # double is commented by -fdump-go-spec.
326 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
327   echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
328 fi
329 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
330   echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
331 fi
332
333 # The time structures need special handling: we need to name the
334 # types, so that we can cast integers to the right types when
335 # assigning to the structures.
336 timeval=`grep '^type _timeval ' gen-sysinfo.go`
337 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
338 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
339 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
340 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
341 echo $timeval | \
342   sed -e 's/type _timeval /type Timeval /' \
343       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
344       -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
345 timespec=`grep '^type _timespec ' gen-sysinfo.go || true`
346 if test "$timespec" = ""; then
347   # IRIX 6.5 has __timespec instead.
348   timespec=`grep '^type ___timespec ' gen-sysinfo.go || true`
349 fi
350 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
351 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
352 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
353 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
354 echo $timespec | \
355   sed -e 's/^type ___timespec /type Timespec /' \
356       -e 's/^type _timespec /type Timespec /' \
357       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
358       -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
359
360 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
361 if test "$timestruc" != ""; then
362   timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
363   timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
364   echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
365   echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
366   echo $timestruc | \
367     sed -e 's/^type _timestruc_t /type Timestruc /' \
368         -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
369         -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
370 fi
371
372 # The tms struct.
373 grep '^type _tms ' gen-sysinfo.go | \
374     sed -e 's/type _tms/type Tms/' \
375       -e 's/tms_utime/Utime/' \
376       -e 's/tms_stime/Stime/' \
377       -e 's/tms_cutime/Cutime/' \
378       -e 's/tms_cstime/Cstime/' \
379     >> ${OUT}
380
381 # The stat type.
382 # Prefer largefile variant if available.
383 stat=`grep '^type _stat64 ' gen-sysinfo.go || true`
384 if test "$stat" != ""; then
385   grep '^type _stat64 ' gen-sysinfo.go
386 else
387   grep '^type _stat ' gen-sysinfo.go
388 fi | sed -e 's/type _stat64/type Stat_t/' \
389          -e 's/type _stat/type Stat_t/' \
390          -e 's/st_dev/Dev/' \
391          -e 's/st_ino/Ino/g' \
392          -e 's/st_nlink/Nlink/' \
393          -e 's/st_mode/Mode/' \
394          -e 's/st_uid/Uid/' \
395          -e 's/st_gid/Gid/' \
396          -e 's/st_rdev/Rdev/' \
397          -e 's/st_size/Size/' \
398          -e 's/st_blksize/Blksize/' \
399          -e 's/st_blocks/Blocks/' \
400          -e 's/st_atim/Atime/' \
401          -e 's/st_mtim/Mtime/' \
402          -e 's/st_ctim/Ctime/' \
403          -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
404          -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
405          -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
406          -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
407          -e 's/Godump_[0-9] struct { \([^;]*;\) };/\1/g' \
408        >> ${OUT}
409
410 # The directory searching types.
411 # Prefer largefile variant if available.
412 dirent=`grep '^type _dirent64 ' gen-sysinfo.go || true`
413 if test "$dirent" != ""; then
414   grep '^type _dirent64 ' gen-sysinfo.go
415 else
416   grep '^type _dirent ' gen-sysinfo.go
417 fi | sed -e 's/type _dirent64/type Dirent/' \
418          -e 's/type _dirent/type Dirent/' \
419          -e 's/d_name \[0+1\]/d_name [0+256]/' \
420          -e 's/d_name/Name/' \
421          -e 's/]int8/]byte/' \
422          -e 's/d_ino/Ino/' \
423          -e 's/d_off/Off/' \
424          -e 's/d_reclen/Reclen/' \
425          -e 's/d_type/Type/' \
426       >> ${OUT}
427 echo "type DIR _DIR" >> ${OUT}
428
429 # The rusage struct.
430 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
431 if test "$rusage" != ""; then
432   rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
433   rusage=`echo $rusage | sed -e 's/^ *//'`
434   nrusage=
435   while test -n "$rusage"; do
436     field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
437     rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
438     # Drop the leading ru_, capitalize the next character.
439     field=`echo $field | sed -e 's/^ru_//'`
440     f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
441     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
442     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
443     # Fix _timeval _timespec, and _timestruc_t.
444     r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
445     r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
446     r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
447     field="$f$r"
448     nrusage="$nrusage $field;"
449   done
450   echo "type Rusage struct {$nrusage }" >> ${OUT}
451 else
452   echo "type Rusage struct {}" >> ${OUT}
453 fi
454
455 # The RUSAGE constants.
456 grep '^const _RUSAGE_' gen-sysinfo.go | \
457   sed -e 's/^\(const \)_\(RUSAGE_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
458
459 # The utsname struct.
460 grep '^type _utsname ' gen-sysinfo.go | \
461     sed -e 's/_utsname/Utsname/' \
462       -e 's/sysname/Sysname/' \
463       -e 's/nodename/Nodename/' \
464       -e 's/release/Release/' \
465       -e 's/version/Version/' \
466       -e 's/machine/Machine/' \
467       -e 's/domainname/Domainname/' \
468     >> ${OUT}
469
470 # The iovec struct.
471 iovec=`grep '^type _iovec ' gen-sysinfo.go`
472 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
473 echo "type Iovec_len_t $iovec_len" >> ${OUT}
474 echo $iovec | \
475     sed -e 's/_iovec/Iovec/' \
476       -e 's/iov_base/Base/' \
477       -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
478     >> ${OUT}
479
480 # The msghdr struct.
481 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
482 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
483 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
484 echo $msghdr | \
485     sed -e 's/_msghdr/Msghdr/' \
486       -e 's/msg_name/Name/' \
487       -e 's/msg_namelen/Namelen/' \
488       -e 's/msg_iov/Iov/' \
489       -e 's/msg_iovlen/Iovlen/' \
490       -e 's/_iovec/Iovec/' \
491       -e 's/msg_control/Control/' \
492       -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
493       -e 's/msg_flags/Flags/' \
494     >> ${OUT}
495
496 # The MSG_ flags for Msghdr.
497 grep '^const _MSG_' gen-sysinfo.go | \
498   sed -e 's/^\(const \)_\(MSG_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
499
500 # The cmsghdr struct.
501 cmsghdr=`grep '^type _cmsghdr ' gen-sysinfo.go`
502 if test -n "$cmsghdr"; then
503   cmsghdr_len=`echo $cmsghdr | sed -n -e 's/^.*cmsg_len \([^ ]*\);.*$/\1/p'`
504   echo "type Cmsghdr_len_t $cmsghdr_len" >> ${OUT}
505   echo "$cmsghdr" | \
506       sed -e 's/_cmsghdr/Cmsghdr/' \
507         -e 's/cmsg_len *[a-zA-Z0-9_]*/Len Cmsghdr_len_t/' \
508         -e 's/cmsg_level/Level/' \
509         -e 's/cmsg_type/Type/' \
510         -e 's/\[\]/[0]/' \
511       >> ${OUT}
512
513   # The size of the cmsghdr struct.
514   echo 'var SizeofCmsghdr = int(unsafe.Sizeof(Cmsghdr{}))' >> ${OUT}
515 fi
516
517 # The SCM_ flags for Cmsghdr.
518 grep '^const _SCM_' gen-sysinfo.go | \
519   sed -e 's/^\(const \)_\(SCM_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
520
521 # The ucred struct.
522 grep '^type _ucred ' gen-sysinfo.go | \
523     sed -e 's/_ucred/Ucred/' \
524       -e 's/pid/Pid/' \
525       -e 's/uid/Uid/' \
526       -e 's/gid/Gid/' \
527     >> ${OUT}
528
529 # The size of the ucred struct.
530 if grep 'type Ucred ' ${OUT} >/dev/null 2>&1; then
531   echo 'var SizeofUcred = int(unsafe.Sizeof(Ucred{}))' >> ${OUT}
532 fi  
533
534 # The ip_mreq struct.
535 grep '^type _ip_mreq ' gen-sysinfo.go | \
536     sed -e 's/_ip_mreq/IPMreq/' \
537       -e 's/imr_multiaddr/Multiaddr/' \
538       -e 's/imr_interface/Interface/' \
539       -e 's/_in_addr/[4]byte/g' \
540     >> ${OUT}
541
542 # We need IPMreq to compile the net package.
543 if ! grep 'type IPMreq ' ${OUT} >/dev/null 2>&1; then
544   echo 'type IPMreq struct { Multiaddr [4]byte; Interface [4]byte; }' >> ${OUT}
545 fi
546
547 # The size of the ip_mreq struct.
548 echo 'var SizeofIPMreq = int(unsafe.Sizeof(IPMreq{}))' >> ${OUT}
549
550 # The ipv6_mreq struct.
551 grep '^type _ipv6_mreq ' gen-sysinfo.go | \
552     sed -e 's/_ipv6_mreq/IPv6Mreq/' \
553       -e 's/ipv6mr_multiaddr/Multiaddr/' \
554       -e 's/ipv6mr_interface/Interface/' \
555       -e 's/_in6_addr/[16]byte/' \
556     >> ${OUT}
557
558 # We need IPv6Mreq to compile the net package.
559 if ! grep 'type IPv6Mreq ' ${OUT} >/dev/null 2>&1; then
560   echo 'type IPv6Mreq struct { Multiaddr [16]byte; Interface uint32; }' >> ${OUT}
561 fi
562
563 # The size of the ipv6_mreq struct.
564 echo 'var SizeofIPv6Mreq = int(unsafe.Sizeof(IPv6Mreq{}))' >> ${OUT}
565
566 # The ip_mreqn struct.
567 grep '^type _ip_mreqn ' gen-sysinfo.go | \
568     sed -e 's/_ip_mreqn/IPMreqn/' \
569       -e 's/imr_multiaddr/Multiaddr/' \
570       -e 's/imr_address/Address/' \
571       -e 's/imr_ifindex/Ifindex/' \
572       -e 's/_in_addr/[4]byte/g' \
573     >> ${OUT}
574
575 # We need IPMreq to compile the net package.
576 if ! grep 'type IPMreqn ' ${OUT} >/dev/null 2>&1; then
577   echo 'type IPMreqn struct { Multiaddr [4]byte; Interface [4]byte; Ifindex int32 }' >> ${OUT}
578 fi
579
580 # The size of the ip_mreqn struct.
581 echo 'var SizeofIPMreqn = int(unsafe.Sizeof(IPMreqn{}))' >> ${OUT}
582
583 # Try to guess the type to use for fd_set.
584 fd_set=`grep '^type _fd_set ' gen-sysinfo.go || true`
585 fds_bits_type="_C_long"
586 if test "$fd_set" != ""; then
587     fds_bits_type=`echo $fd_set | sed -e 's/.*[]]\([^;]*\); }$/\1/'`
588 fi
589 echo "type fds_bits_type $fds_bits_type" >> ${OUT}
590
591 # The addrinfo struct.
592 grep '^type _addrinfo ' gen-sysinfo.go | \
593     sed -e 's/_addrinfo/Addrinfo/g' \
594       -e 's/ ai_/ Ai_/g' \
595     >> ${OUT}
596
597 # The addrinfo flags and errors.
598 grep '^const _AI_' gen-sysinfo.go | \
599   sed -e 's/^\(const \)_\(AI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
600 grep '^const _EAI_' gen-sysinfo.go | \
601   sed -e 's/^\(const \)_\(EAI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
602
603 # The passwd struct.
604 grep '^type _passwd ' gen-sysinfo.go | \
605     sed -e 's/_passwd/Passwd/' \
606       -e 's/ pw_/ Pw_/g' \
607     >> ${OUT}
608
609 # The ioctl flags for the controlling TTY.
610 grep '^const _TIOC' gen-sysinfo.go | \
611     grep -v '_val =' | \
612     sed -e 's/^\(const \)_\(TIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
613 # We need TIOCGWINSZ.
614 if ! grep '^const TIOCGWINSZ' ${OUT} >/dev/null 2>&1; then
615   if grep '^const _TIOCGWINSZ_val' ${OUT} >/dev/null 2>&1; then
616     echo 'const TIOCGWINSZ = _TIOCGWINSZ_val' >> ${OUT}
617   fi
618 fi
619
620 # The ioctl flags for terminal control
621 grep '^const _TC[GS]ET' gen-sysinfo.go | \
622     sed -e 's/^\(const \)_\(TC[GS]ET[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
623
624 # ioctl constants.  Might fall back to 0 if TIOCNXCL is missing, too, but
625 # needs handling in syscalls.exec.go.
626 if ! grep '^const _TIOCSCTTY ' gen-sysinfo.go >/dev/null 2>&1; then
627   if grep '^const _TIOCNXCL ' gen-sysinfo.go >/dev/null 2>&1; then
628     echo "const TIOCSCTTY = TIOCNXCL" >> ${OUT}
629   fi
630 fi
631
632 # The nlmsghdr struct.
633 grep '^type _nlmsghdr ' gen-sysinfo.go | \
634     sed -e 's/_nlmsghdr/NlMsghdr/' \
635       -e 's/nlmsg_len/Len/' \
636       -e 's/nlmsg_type/Type/' \
637       -e 's/nlmsg_flags/Flags/' \
638       -e 's/nlmsg_seq/Seq/' \
639       -e 's/nlmsg_pid/Pid/' \
640     >> ${OUT}
641
642 # The nlmsg flags and operators.
643 grep '^const _NLM' gen-sysinfo.go | \
644     sed -e 's/^\(const \)_\(NLM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
645
646 # NLMSG_HDRLEN is defined as an expression using sizeof.
647 if ! grep '^const NLMSG_HDRLEN' ${OUT} > /dev/null 2>&1; then
648   if grep '^type NlMsghdr ' ${OUT} > /dev/null 2>&1; then
649     echo 'var NLMSG_HDRLEN = int((unsafe.Sizeof(NlMsghdr{}) + (NLMSG_ALIGNTO-1)) &^ (NLMSG_ALIGNTO-1))' >> ${OUT}
650   fi
651 fi
652
653 # The rtmsg struct.
654 grep '^type _rtmsg ' gen-sysinfo.go | \
655     sed -e 's/_rtmsg/RtMsg/' \
656       -e 's/rtm_family/Family/' \
657       -e 's/rtm_dst_len/Dst_len/' \
658       -e 's/rtm_src_len/Src_len/' \
659       -e 's/rtm_tos/Tos/' \
660       -e 's/rtm_table/Table/' \
661       -e 's/rtm_protocol/Procotol/' \
662       -e 's/rtm_scope/Scope/' \
663       -e 's/rtm_type/Type/' \
664       -e 's/rtm_flags/Flags/' \
665     >> ${OUT}
666
667 # The size of the rtmsg struct.
668 if grep 'type RtMsg ' ${OUT} > /dev/null 2>&1; then
669   echo 'var SizeofRtMsg = int(unsafe.Sizeof(RtMsg{}))' >> ${OUT}
670 fi
671
672 # The rtgenmsg struct.
673 grep '^type _rtgenmsg ' gen-sysinfo.go | \
674     sed -e 's/_rtgenmsg/RtGenmsg/' \
675       -e 's/rtgen_family/Family/' \
676     >> ${OUT}
677
678 # The routing message flags.
679 grep '^const _RTA' gen-sysinfo.go | \
680     sed -e 's/^\(const \)_\(RTA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
681 grep '^const _RTM' gen-sysinfo.go | \
682     sed -e 's/^\(const \)_\(RTM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
683
684 # The size of the rtgenmsg struct.
685 if grep 'type RtGenmsg ' ${OUT} > /dev/null 2>&1; then
686   echo 'var SizeofRtGenmsg = int(unsafe.Sizeof(RtGenmsg{}))' >> ${OUT}
687 fi
688
689 # The ifinfomsg struct.
690 grep '^type _ifinfomsg ' gen-sysinfo.go | \
691     sed -e 's/_ifinfomsg/IfInfomsg/' \
692       -e 's/ifi_family/Family/' \
693       -e 's/ifi_type/Type/' \
694       -e 's/ifi_index/Index/' \
695       -e 's/ifi_flags/Flags/' \
696       -e 's/ifi_change/Change/' \
697     >> ${OUT}
698
699 # The interface information types and flags.
700 grep '^const _IFA' gen-sysinfo.go | \
701     sed -e 's/^\(const \)_\(IFA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
702 grep '^const _IFLA' gen-sysinfo.go | \
703     sed -e 's/^\(const \)_\(IFLA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
704 grep '^const _IFF' gen-sysinfo.go | \
705     sed -e 's/^\(const \)_\(IFF[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
706
707 # The size of the ifinfomsg struct.
708 if grep 'type IfInfomsg ' ${OUT} > /dev/null 2>&1; then
709   echo 'var SizeofIfInfomsg = int(unsafe.Sizeof(IfInfomsg{}))' >> ${OUT}
710 fi
711
712 # The ifaddrmsg struct.
713 grep '^type _ifaddrmsg ' gen-sysinfo.go | \
714     sed -e 's/_ifaddrmsg/IfAddrmsg/' \
715       -e 's/ifa_family/Family/' \
716       -e 's/ifa_prefixlen/Prefixlen/' \
717       -e 's/ifa_flags/Flags/' \
718       -e 's/ifa_scope/Scope/' \
719       -e 's/ifa_index/Index/' \
720     >> ${OUT}
721
722 # The size of the ifaddrmsg struct.
723 if grep 'type IfAddrmsg ' ${OUT} > /dev/null 2>&1; then
724   echo 'var SizeofIfAddrmsg = int(unsafe.Sizeof(IfAddrmsg{}))' >> ${OUT}
725 fi
726
727 # The rtattr struct.
728 grep '^type _rtattr ' gen-sysinfo.go | \
729     sed -e 's/_rtattr/RtAttr/' \
730       -e 's/rta_len/Len/' \
731       -e 's/rta_type/Type/' \
732     >> ${OUT}
733
734 # The size of the rtattr struct.
735 if grep 'type RtAttr ' ${OUT} > /dev/null 2>&1; then
736   echo 'var SizeofRtAttr = int(unsafe.Sizeof(RtAttr{}))' >> ${OUT}
737 fi
738
739 # The termios struct.
740 grep '^type _termios ' gen-sysinfo.go | \
741     sed -e 's/_termios/Termios/' \
742       -e 's/c_iflag/Iflag/' \
743       -e 's/c_oflag/Oflag/' \
744       -e 's/c_cflag/Cflag/' \
745       -e 's/c_lflag/Lflag/' \
746       -e 's/c_line/Line/' \
747       -e 's/c_cc/Cc/' \
748       -e 's/c_ispeed/Ispeed/' \
749       -e 's/c_ospeed/Ospeed/' \
750     >> ${OUT}
751
752 # The termios constants.
753 for n in IGNBRK BRKINT IGNPAR PARMRK INPCK ISTRIP INLCR IGNCR ICRNL IUCLC \
754     IXON IXANY IXOFF IMAXBEL IUTF8 OPOST OLCUC ONLCR OCRNL ONOCR ONLRET \
755     OFILL OFDEL NLDLY NL0 NL1 CRDLY CR0 CR1 CR2 CR3 TABDLY BSDLY VTDLY \
756     FFDLY CBAUD CBAUDEX CSIZE CSTOPB CREAD PARENB PARODD HUPCL CLOCAL \
757     LOBLK CIBAUD CMSPAR CRTSCTS ISIG ICANON XCASE ECHO ECHOE ECHOK ECHONL \
758     ECHOCTL ECHOPRT ECHOKE DEFECHO FLUSHO NOFLSH TOSTOP PENDIN IEXTEN VINTR \
759     VQUIT VERASE VKILL VEOF VMIN VEOL VTIME VEOL2 VSWTCH VSTART VSTOP VSUSP \
760     VDSUSP VLNEXT VWERASE VREPRINT VDISCARD VSTATUS TCSANOW TCSADRAIN \
761     TCSAFLUSH TCIFLUSH TCOFLUSH TCIOFLUSH TCOOFF TCOON TCIOFF TCION B0 B50 \
762     B75 B110 B134 B150 B200 B300 B600 B1200 B1800 B2400 B4800 B9600 B19200 \
763     B38400 B57600 B115200 B230400; do
764
765     grep "^const _$n " gen-sysinfo.go | \
766         sed -e 's/^\(const \)_\([^=]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
767 done
768
769 exit $?