OSDN Git Service

f959d640fdc8b2c8d78a45e5402233165864ad39
[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 #define _GNU_SOURCE
29 #define _LARGEFILE_SOURCE
30 #define _FILE_OFFSET_BITS 64
31
32 #include <sys/types.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <netinet/in.h>
37 /* <netinet/tcp.h> needs u_char/u_short, but <sys/bsd_types> is only
38    included by <netinet/in.h> if _SGIAPI (i.e. _SGI_SOURCE
39    && !_XOPEN_SOURCE.
40    <sys/termios.h> only defines TIOCNOTTY if !_XOPEN_SOURCE, while
41    <sys/ttold.h> does so unconditionally.  */
42 #ifdef __sgi__
43 #include <sys/bsd_types.h>
44 #include <sys/ttold.h>
45 #endif
46 #include <netinet/tcp.h>
47 #include <signal.h>
48 #include <sys/ioctl.h>
49 #include <termios.h>
50 #if defined(HAVE_SYSCALL_H)
51 #include <syscall.h>
52 #endif
53 #if defined(HAVE_SYS_SYSCALL_H)
54 #include <sys/syscall.h>
55 #endif
56 #if defined(HAVE_SYS_EPOLL_H)
57 #include <sys/epoll.h>
58 #endif
59 #if defined(HAVE_SYS_MMAN_H)
60 #include <sys/mman.h>
61 #endif
62 #if defined(HAVE_SYS_PTRACE_H)
63 #include <sys/ptrace.h>
64 #endif
65 #include <sys/resource.h>
66 #include <sys/uio.h>
67 #include <sys/socket.h>
68 #include <sys/stat.h>
69 #include <sys/time.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 EOF
97
98 ${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c
99
100 echo 'package syscall' > ${OUT}
101 echo 'import "unsafe"' >> ${OUT}
102 echo 'type _ unsafe.Pointer' >> ${OUT}
103
104 # Get all the consts and types, skipping ones which could not be
105 # represented in Go and ones which we need to rewrite.  We also skip
106 # function declarations, as we don't need them here.  All the symbols
107 # will all have a leading underscore.
108 grep -v '^// ' gen-sysinfo.go | \
109   grep -v '^func' | \
110   grep -v '^type _timeval ' | \
111   grep -v '^type _timespec_t ' | \
112   grep -v '^type _timespec ' | \
113   grep -v '^type _timestruc_t ' | \
114   grep -v '^type _epoll_' | \
115   grep -v 'in6_addr' | \
116   grep -v 'sockaddr_in6' | \
117   sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
118       -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
119       -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
120       -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
121     >> ${OUT}
122
123 # The errno constants.
124 grep '^const _E' gen-sysinfo.go | \
125   sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
126
127 # The O_xxx flags.
128 egrep '^const _(O|F|FD)_' gen-sysinfo.go | \
129   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
130 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
131   echo "const O_ASYNC = 0" >> ${OUT}
132 fi
133 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
134   echo "const O_CLOEXEC = 0" >> ${OUT}
135 fi
136
137 # The signal numbers.
138 grep '^const _SIG[^_]' gen-sysinfo.go | \
139   grep -v '^const _SIGEV_' | \
140   sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
141
142 # The syscall numbers.  We force the names to upper case.
143 grep '^const _SYS_' gen-sysinfo.go | \
144   sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
145   while read sys; do
146     sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
147     echo "const $sup = _$sys" >> ${OUT}
148   done
149
150 # Stat constants.
151 grep '^const _S_' gen-sysinfo.go | \
152   sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
153
154 # Mmap constants.
155 grep '^const _PROT_' gen-sysinfo.go | \
156   sed -e 's/^\(const \)_\(PROT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
157 grep '^const _MAP_' gen-sysinfo.go | \
158   sed -e 's/^\(const \)_\(MAP_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
159
160 # Process status constants.
161 grep '^const _W' gen-sysinfo.go |
162   sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
163 # WSTOPPED was introduced in glibc 2.3.4.
164 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
165   if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
166     echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
167   else
168     echo 'const WSTOPPED = 2' >> ${OUT}
169   fi
170 fi
171 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
172    && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
173   echo 'const WALL = ___WALL' >> ${OUT}
174 fi
175
176 # Networking constants.
177 egrep '^const _(AF|SOCK|SOL|SO|IPPROTO|TCP|IP|IPV6)_' gen-sysinfo.go |
178   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
179 grep '^const _SOMAXCONN' gen-sysinfo.go |
180   sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
181     >> ${OUT}
182 grep '^const _SHUT_' gen-sysinfo.go |
183   sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
184
185 # The net package requires a definition for IPV6ONLY.
186 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev/null 2>&1; then
187   echo "const IPV6_V6ONLY = 0" >> ${OUT}
188 fi
189
190 # pathconf constants.
191 grep '^const __PC' gen-sysinfo.go |
192   sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
193
194 # The epoll constants were picked up by the errno constants, but we
195 # need to be sure the EPOLLRDHUP is defined.
196 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
197   echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
198 fi
199
200 # Ptrace constants.  We don't expose all the PTRACE flags, just the
201 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
202 grep '^const _PTRACE_O' gen-sysinfo.go |
203   sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
204 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
205   sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
206 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
207 # defined in older versions of glibc.
208 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
209   echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
210 fi
211 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
212   echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
213 fi
214 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
215   echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
216 fi
217 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
218   echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
219 fi
220 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
221   echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
222 fi
223 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
224   echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
225 fi
226 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
227   echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
228 fi
229 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
230   echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
231 fi
232 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
233   echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
234 fi
235 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
236   echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
237 fi
238 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
239   echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
240 fi
241 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
242   echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
243 fi
244 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
245   echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
246 fi
247 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
248   echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
249 fi
250 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
251   echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
252 fi
253 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
254   echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
255 fi
256 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
257   echo "const _PTRACE_TRACEME = 0" >> ${OUT}
258 fi
259
260 # The registers returned by PTRACE_GETREGS.  This is probably
261 # GNU/Linux specific; it should do no harm if there is no
262 # _user_regs_struct.
263 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
264 if test "$regs" != ""; then
265   regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
266   regs=`echo $regs | sed -e s'/^ *//'`
267   nregs=
268   while test -n "$regs"; do
269     field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
270     regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
271     # Capitalize the first character of the field.
272     f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
273     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
274     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
275     field="$f$r"
276     nregs="$nregs $field;"
277   done
278   echo "type PtraceRegs struct {$nregs }" >> ${OUT}
279 fi
280
281 # Some basic types.
282 echo 'type Size_t _size_t' >> ${OUT}
283 echo "type Ssize_t _ssize_t" >> ${OUT}
284 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
285   echo "type Offset_t _off64_t" >> ${OUT}
286 else
287   echo "type Offset_t _off_t" >> ${OUT}
288 fi
289 echo "type Mode_t _mode_t" >> ${OUT}
290 echo "type Pid_t _pid_t" >> ${OUT}
291 echo "type Uid_t _uid_t" >> ${OUT}
292 echo "type Gid_t _gid_t" >> ${OUT}
293 echo "type Socklen_t _socklen_t" >> ${OUT}
294
295 # The long type, needed because that is the type that ptrace returns.
296 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
297 if test "$sizeof_long" = "4"; then
298   echo "type _C_long int32" >> ${OUT}
299 elif test "$sizeof_long" = "8"; then
300   echo "type _C_long int64" >> ${OUT}
301 else
302   echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
303   exit 1
304 fi
305
306 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
307 # double is commented by -fdump-go-spec.
308 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
309   echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
310 fi
311 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
312   echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
313 fi
314
315 # The time structures need special handling: we need to name the
316 # types, so that we can cast integers to the right types when
317 # assigning to the structures.
318 timeval=`grep '^type _timeval ' gen-sysinfo.go`
319 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
320 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
321 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
322 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
323 echo $timeval | \
324   sed -e 's/type _timeval /type Timeval /' \
325       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
326       -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
327 timespec=`grep '^type _timespec ' gen-sysinfo.go || true`
328 if test "$timespec" = ""; then
329   # IRIX 6.5 has __timespec instead.
330   timespec=`grep '^type ___timespec ' gen-sysinfo.go || true`
331 fi
332 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
333 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
334 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
335 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
336 echo $timespec | \
337   sed -e 's/^type ___timespec /type Timespec /' \
338       -e 's/^type _timespec /type Timespec /' \
339       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
340       -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
341
342 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
343 if test "$timestruc" != ""; then
344   timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
345   timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
346   echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
347   echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
348   echo $timestruc | \
349     sed -e 's/^type _timestruc_t /type Timestruc /' \
350         -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
351         -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
352 fi
353
354 # The stat type.
355 # Prefer largefile variant if available.
356 stat=`grep '^type _stat64 ' gen-sysinfo.go || true`
357 if test "$stat" != ""; then
358   grep '^type _stat64 ' gen-sysinfo.go
359 else
360   grep '^type _stat ' gen-sysinfo.go
361 fi | sed -e 's/type _stat64/type Stat_t/' \
362          -e 's/type _stat/type Stat_t/' \
363          -e 's/st_dev/Dev/' \
364          -e 's/st_ino/Ino/g' \
365          -e 's/st_nlink/Nlink/' \
366          -e 's/st_mode/Mode/' \
367          -e 's/st_uid/Uid/' \
368          -e 's/st_gid/Gid/' \
369          -e 's/st_rdev/Rdev/' \
370          -e 's/st_size/Size/' \
371          -e 's/st_blksize/Blksize/' \
372          -e 's/st_blocks/Blocks/' \
373          -e 's/st_atim/Atime/' \
374          -e 's/st_mtim/Mtime/' \
375          -e 's/st_ctim/Ctime/' \
376          -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
377          -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
378          -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
379          -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
380          -e 's/Godump_[0-9] struct { \([^;]*;\) };/\1/g' \
381        >> ${OUT}
382
383 # The directory searching types.
384 # Prefer largefile variant if available.
385 dirent=`grep '^type _dirent64 ' gen-sysinfo.go || true`
386 if test "$dirent" != ""; then
387   grep '^type _dirent64 ' gen-sysinfo.go
388 else
389   grep '^type _dirent ' gen-sysinfo.go
390 fi | sed -e 's/type _dirent64/type Dirent/' \
391          -e 's/type _dirent/type Dirent/' \
392          -e 's/d_name \[0+1\]/d_name [0+256]/' \
393          -e 's/d_name/Name/' \
394          -e 's/]int8/]byte/' \
395          -e 's/d_ino/Ino/' \
396          -e 's/d_off/Off/' \
397          -e 's/d_reclen/Reclen/' \
398          -e 's/d_type/Type/' \
399       >> ${OUT}
400 echo "type DIR _DIR" >> ${OUT}
401
402 # The rusage struct.
403 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
404 if test "$rusage" != ""; then
405   rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
406   rusage=`echo $rusage | sed -e 's/^ *//'`
407   nrusage=
408   while test -n "$rusage"; do
409     field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
410     rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
411     # Drop the leading ru_, capitalize the next character.
412     field=`echo $field | sed -e 's/^ru_//'`
413     f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
414     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
415     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
416     # Fix _timeval _timespec, and _timestruc_t.
417     r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
418     r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
419     r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
420     field="$f$r"
421     nrusage="$nrusage $field;"
422   done
423   echo "type Rusage struct {$nrusage }" >> ${OUT}
424 else
425   echo "type Rusage struct {}" >> ${OUT}
426 fi
427
428 # The RUSAGE constants.
429 grep '^const _RUSAGE_' gen-sysinfo.go | \
430   sed -e 's/^\(const \)_\(RUSAGE_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
431
432 # The utsname struct.
433 grep '^type _utsname ' gen-sysinfo.go | \
434     sed -e 's/_utsname/Utsname/' \
435       -e 's/sysname/Sysname/' \
436       -e 's/nodename/Nodename/' \
437       -e 's/release/Release/' \
438       -e 's/version/Version/' \
439       -e 's/machine/Machine/' \
440       -e 's/domainname/Domainname/' \
441     >> ${OUT}
442
443 # The iovec struct.
444 iovec=`grep '^type _iovec ' gen-sysinfo.go`
445 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
446 echo "type Iovec_len_t $iovec_len" >> ${OUT}
447 echo $iovec | \
448     sed -e 's/_iovec/Iovec/' \
449       -e 's/iov_base/Base/' \
450       -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
451     >> ${OUT}
452
453 # The msghdr struct.
454 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
455 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
456 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
457 echo $msghdr | \
458     sed -e 's/_msghdr/Msghdr/' \
459       -e 's/msg_name/Name/' \
460       -e 's/msg_namelen/Namelen/' \
461       -e 's/msg_iov/Iov/' \
462       -e 's/msg_iovlen/Iovlen/' \
463       -e 's/_iovec/Iovec/' \
464       -e 's/msg_control/Control/' \
465       -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
466       -e 's/msg_flags/Flags/' \
467     >> ${OUT}
468
469 # The ip_mreq struct
470 grep '^type _ip_mreq ' gen-sysinfo.go | \
471     sed -e 's/_ip_mreq/IPMreq/' \
472       -e 's/imr_multiaddr/Multiaddr/' \
473       -e 's/imr_interface/Interface/' \
474       -e 's/_in_addr/[4]byte/g' \
475     >> ${OUT}
476
477 # Try to guess the type to use for fd_set.
478 fd_set=`grep '^type _fd_set ' gen-sysinfo.go || true`
479 fds_bits_type="_C_long"
480 if test "$fd_set" != ""; then
481     fds_bits_type=`echo $fd_set | sed -e 's/.*[]]\([^;]*\); }$/\1/'`
482 fi
483 echo "type fds_bits_type $fds_bits_type" >> ${OUT}
484
485 # The addrinfo struct.
486 grep '^type _addrinfo ' gen-sysinfo.go | \
487     sed -e 's/_addrinfo/Addrinfo/g' \
488       -e 's/ ai_/ Ai_/g' \
489     >> ${OUT}
490
491 # The addrinfo flags.
492 grep '^const _AI_' gen-sysinfo.go | \
493   sed -e 's/^\(const \)_\(AI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
494
495 # The passwd struct.
496 grep '^type _passwd ' gen-sysinfo.go | \
497     sed -e 's/_passwd/Passwd/' \
498       -e 's/ pw_/ Pw_/g' \
499     >> ${OUT}
500
501 # The ioctl flags for the controlling TTY.
502 grep '^const _TIOC' gen-sysinfo.go | \
503     sed -e 's/^\(const \)_\(TIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
504
505 # ioctl constants.  Might fall back to 0 if TIOCNXCL is missing, too, but
506 # needs handling in syscalls.exec.go.
507 if ! grep '^const _TIOCSCTTY ' gen-sysinfo.go >/dev/null 2>&1; then
508   if grep '^const _TIOCNXCL ' gen-sysinfo.go >/dev/null 2>&1; then
509     echo "const TIOCSCTTY = TIOCNXCL" >> ${OUT}
510   fi
511 fi
512
513 # The nlmsghdr struct.
514 grep '^type _nlmsghdr ' gen-sysinfo.go | \
515     sed -e 's/_nlmsghdr/NlMsghdr/' \
516       -e 's/nlmsg_len/Len/' \
517       -e 's/nlmsg_type/Type/' \
518       -e 's/nlmsg_flags/Flags/' \
519       -e 's/nlmsg_seq/Seq/' \
520       -e 's/nlmsg_pid/Pid/' \
521     >> ${OUT}
522
523 # The nlmsg flags and operators.
524 grep '^const _NLM' gen-sysinfo.go | \
525     sed -e 's/^\(const \)_\(NLM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
526
527 # NLMSG_HDRLEN is defined as an expression using sizeof.
528 if ! grep '^const NLMSG_HDRLEN' ${OUT} > /dev/null 2>&1; then
529   if grep '^type NlMsghdr ' ${OUT} > /dev/null 2>&1; then
530     echo 'var NLMSG_HDRLEN = int((unsafe.Sizeof(NlMsghdr{}) + (NLMSG_ALIGNTO-1)) &^ (NLMSG_ALIGNTO-1))' >> ${OUT}
531   fi
532 fi
533
534 # The rtgenmsg struct.
535 grep '^type _rtgenmsg ' gen-sysinfo.go | \
536     sed -e 's/_rtgenmsg/RtGenmsg/' \
537       -e 's/rtgen_family/Family/' \
538     >> ${OUT}
539
540 # The routing message flags.
541 grep '^const _RTA' gen-sysinfo.go | \
542     sed -e 's/^\(const \)_\(RTA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
543 grep '^const _RTM' gen-sysinfo.go | \
544     sed -e 's/^\(const \)_\(RTM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
545
546 # The size of the rtgenmsg struct.
547 if grep 'type RtGenmsg ' ${OUT} > /dev/null 2>&1; then
548   echo 'var SizeofRtGenmsg = int(unsafe.Sizeof(RtGenmsg{}))' >> ${OUT}
549 fi
550
551 # The ifinfomsg struct.
552 grep '^type _ifinfomsg ' gen-sysinfo.go | \
553     sed -e 's/_ifinfomsg/IfInfomsg/' \
554       -e 's/ifi_family/Family/' \
555       -e 's/ifi_type/Type/' \
556       -e 's/ifi_index/Index/' \
557       -e 's/ifi_flags/Flags/' \
558       -e 's/ifi_change/Change/' \
559     >> ${OUT}
560
561 # The interface information types and flags.
562 grep '^const _IFA' gen-sysinfo.go | \
563     sed -e 's/^\(const \)_\(IFA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
564 grep '^const _IFLA' gen-sysinfo.go | \
565     sed -e 's/^\(const \)_\(IFLA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
566 grep '^const _IFF' gen-sysinfo.go | \
567     sed -e 's/^\(const \)_\(IFF[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
568
569 # The size of the ifinfomsg struct.
570 if grep 'type IfInfomsg ' ${OUT} > /dev/null 2>&1; then
571   echo 'var SizeofIfInfomsg = int(unsafe.Sizeof(IfInfomsg{}))' >> ${OUT}
572 fi
573
574 # The ifaddrmsg struct.
575 grep '^type _ifaddrmsg ' gen-sysinfo.go | \
576     sed -e 's/_ifaddrmsg/IfAddrmsg/' \
577       -e 's/ifa_family/Family/' \
578       -e 's/ifa_prefixlen/Prefixlen/' \
579       -e 's/ifa_flags/Flags/' \
580       -e 's/ifa_scope/Scope/' \
581       -e 's/ifa_index/Index/' \
582     >> ${OUT}
583
584 # The size of the ifaddrmsg struct.
585 if grep 'type IfAddrmsg ' ${OUT} > /dev/null 2>&1; then
586   echo 'var SizeofIfAddrmsg = int(unsafe.Sizeof(IfAddrmsg{}))' >> ${OUT}
587 fi
588
589 # The rtattr struct.
590 grep '^type _rtattr ' gen-sysinfo.go | \
591     sed -e 's/_rtattr/RtAttr/' \
592       -e 's/rta_len/Len/' \
593       -e 's/rta_type/Type/' \
594     >> ${OUT}
595
596 # The size of the rtattr struct.
597 if grep 'type RtAttr ' ${OUT} > /dev/null 2>&1; then
598   echo 'var SizeofRtAttr = int(unsafe.Sizeof(RtAttr{}))' >> ${OUT}
599 fi
600
601 exit $?