2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / mksysinfo.sh
blobca9e5f686b1d5ba568a4fdf6670351c544cc5972
1 #!/bin/sh
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.
7 # Create sysinfo.go.
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.
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.
19 CC=${CC:-gcc}
20 OUT=tmp-sysinfo.go
22 set -e
24 rm -f sysinfo.c
25 cat > sysinfo.c <<EOF
26 #include "config.h"
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_PTRACE_H)
59 #include <sys/ptrace.h>
60 #endif
61 #include <sys/resource.h>
62 #include <sys/uio.h>
63 #include <sys/socket.h>
64 #include <sys/stat.h>
65 #include <sys/time.h>
66 #include <sys/wait.h>
67 #include <sys/un.h>
68 #if defined(HAVE_SYS_USER_H)
69 #include <sys/user.h>
70 #endif
71 #if defined(HAVE_SYS_UTSNAME_H)
72 #include <sys/utsname.h>
73 #endif
74 #if defined(HAVE_SYS_SELECT_H)
75 #include <sys/select.h>
76 #endif
77 #include <unistd.h>
78 #include <netdb.h>
79 #include <pwd.h>
80 #if defined(HAVE_LINUX_FILTER_H)
81 #include <linux/filter.h>
82 #endif
83 #if defined(HAVE_LINUX_NETLINK_H)
84 #include <linux/netlink.h>
85 #endif
86 #if defined(HAVE_LINUX_RTNETLINK_H)
87 #include <linux/rtnetlink.h>
88 #endif
89 #if defined(HAVE_NET_IF_H)
90 #include <net/if.h>
91 #endif
92 EOF
94 ${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c
96 echo 'package syscall' > ${OUT}
97 echo 'import "unsafe"' >> ${OUT}
98 echo 'type _ unsafe.Pointer' >> ${OUT}
100 # Get all the consts and types, skipping ones which could not be
101 # represented in Go and ones which we need to rewrite. We also skip
102 # function declarations, as we don't need them here. All the symbols
103 # will all have a leading underscore.
104 grep -v '^// ' gen-sysinfo.go | \
105 grep -v '^func' | \
106 grep -v '^type _timeval ' | \
107 grep -v '^type _timespec_t ' | \
108 grep -v '^type _timespec ' | \
109 grep -v '^type _timestruc_t ' | \
110 grep -v '^type _epoll_' | \
111 grep -v 'in6_addr' | \
112 grep -v 'sockaddr_in6' | \
113 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
114 -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
115 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
116 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
117 >> ${OUT}
119 # The errno constants. These get type Errno.
120 echo '#include <errno.h>' | ${CC} -x c - -E -dM | \
121 egrep '#define E[A-Z0-9_]+ ' | \
122 sed -e 's/^#define \(E[A-Z0-9_]*\) .*$/const \1 = Errno(_\1)/' >> ${OUT}
124 # The O_xxx flags.
125 egrep '^const _(O|F|FD)_' gen-sysinfo.go | \
126 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
127 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
128 echo "const O_ASYNC = 0" >> ${OUT}
130 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
131 echo "const O_CLOEXEC = 0" >> ${OUT}
134 # The signal numbers.
135 grep '^const _SIG[^_]' gen-sysinfo.go | \
136 grep -v '^const _SIGEV_' | \
137 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
139 # The syscall numbers. We force the names to upper case.
140 grep '^const _SYS_' gen-sysinfo.go | \
141 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
142 while read sys; do
143 sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
144 echo "const $sup = _$sys" >> ${OUT}
145 done
147 # Stat constants.
148 grep '^const _S_' gen-sysinfo.go | \
149 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
151 # Mmap constants.
152 grep '^const _PROT_' gen-sysinfo.go | \
153 sed -e 's/^\(const \)_\(PROT_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
154 grep '^const _MAP_' gen-sysinfo.go | \
155 sed -e 's/^\(const \)_\(MAP_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
157 # Process status constants.
158 grep '^const _W' gen-sysinfo.go |
159 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
160 # WSTOPPED was introduced in glibc 2.3.4.
161 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
162 if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
163 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
164 else
165 echo 'const WSTOPPED = 2' >> ${OUT}
168 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
169 && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
170 echo 'const WALL = ___WALL' >> ${OUT}
173 # Networking constants.
174 egrep '^const _(AF|SOCK|SOL|SO|IPPROTO|TCP|IP|IPV6)_' gen-sysinfo.go |
175 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
176 grep '^const _SOMAXCONN' gen-sysinfo.go |
177 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
178 >> ${OUT}
179 grep '^const _SHUT_' gen-sysinfo.go |
180 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
182 # The net package requires some const definitions.
183 for m in IPV6_V6ONLY IPPROTO_IPV6 IPV6_JOIN_GROUP IPV6_LEAVE_GROUP; do
184 if ! grep "^const $m " ${OUT} >/dev/null 2>&1; then
185 echo "const $m = 0" >> ${OUT}
187 done
189 # pathconf constants.
190 grep '^const __PC' gen-sysinfo.go |
191 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
193 # epoll constants.
194 grep '^const _EPOLL' gen-sysinfo.go |
195 sed -e 's/^\(const \)_\(EPOLL[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
196 # Make sure EPOLLRDHUP is defined.
197 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
198 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
201 # Ptrace constants.
202 grep '^const _PTRACE' gen-sysinfo.go |
203 sed -e 's/^\(const \)_\(PTRACE[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
204 # We need some ptrace options that are not defined in older versions
205 # of glibc.
206 if ! grep '^const PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
207 echo "const PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
209 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
210 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
212 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
213 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
215 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
216 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
218 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
219 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
221 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
222 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
224 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
225 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
227 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
228 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
230 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
231 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
233 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
234 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
236 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
237 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
239 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
240 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
242 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
243 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
245 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
246 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
248 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
249 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
251 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
252 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
254 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
255 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
258 # The registers returned by PTRACE_GETREGS. This is probably
259 # GNU/Linux specific; it should do no harm if there is no
260 # _user_regs_struct.
261 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
262 if test "$regs" != ""; then
263 regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
264 regs=`echo $regs | sed -e s'/^ *//'`
265 nregs=
266 while test -n "$regs"; do
267 field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
268 regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
269 # Capitalize the first character of the field.
270 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
271 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
272 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
273 field="$f$r"
274 nregs="$nregs $field;"
275 done
276 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
279 # Some basic types.
280 echo 'type Size_t _size_t' >> ${OUT}
281 echo "type Ssize_t _ssize_t" >> ${OUT}
282 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
283 echo "type Offset_t _off64_t" >> ${OUT}
284 else
285 echo "type Offset_t _off_t" >> ${OUT}
287 echo "type Mode_t _mode_t" >> ${OUT}
288 echo "type Pid_t _pid_t" >> ${OUT}
289 echo "type Uid_t _uid_t" >> ${OUT}
290 echo "type Gid_t _gid_t" >> ${OUT}
291 echo "type Socklen_t _socklen_t" >> ${OUT}
293 # The long type, needed because that is the type that ptrace returns.
294 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
295 if test "$sizeof_long" = "4"; then
296 echo "type _C_long int32" >> ${OUT}
297 elif test "$sizeof_long" = "8"; then
298 echo "type _C_long int64" >> ${OUT}
299 else
300 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
301 exit 1
304 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
305 # double is commented by -fdump-go-spec.
306 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
307 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
309 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
310 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
313 # The time structures need special handling: we need to name the
314 # types, so that we can cast integers to the right types when
315 # assigning to the structures.
316 timeval=`grep '^type _timeval ' gen-sysinfo.go`
317 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
318 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
319 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
320 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
321 echo $timeval | \
322 sed -e 's/type _timeval /type Timeval /' \
323 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
324 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
325 timespec=`grep '^type _timespec ' gen-sysinfo.go || true`
326 if test "$timespec" = ""; then
327 # IRIX 6.5 has __timespec instead.
328 timespec=`grep '^type ___timespec ' gen-sysinfo.go || true`
330 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
331 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
332 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
333 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
334 echo $timespec | \
335 sed -e 's/^type ___timespec /type Timespec /' \
336 -e 's/^type _timespec /type Timespec /' \
337 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
338 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
340 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
341 if test "$timestruc" != ""; then
342 timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
343 timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
344 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
345 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
346 echo $timestruc | \
347 sed -e 's/^type _timestruc_t /type Timestruc /' \
348 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
349 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
352 # The stat type.
353 # Prefer largefile variant if available.
354 stat=`grep '^type _stat64 ' gen-sysinfo.go || true`
355 if test "$stat" != ""; then
356 grep '^type _stat64 ' gen-sysinfo.go
357 else
358 grep '^type _stat ' gen-sysinfo.go
359 fi | sed -e 's/type _stat64/type Stat_t/' \
360 -e 's/type _stat/type Stat_t/' \
361 -e 's/st_dev/Dev/' \
362 -e 's/st_ino/Ino/g' \
363 -e 's/st_nlink/Nlink/' \
364 -e 's/st_mode/Mode/' \
365 -e 's/st_uid/Uid/' \
366 -e 's/st_gid/Gid/' \
367 -e 's/st_rdev/Rdev/' \
368 -e 's/st_size/Size/' \
369 -e 's/st_blksize/Blksize/' \
370 -e 's/st_blocks/Blocks/' \
371 -e 's/st_atim/Atime/' \
372 -e 's/st_mtim/Mtime/' \
373 -e 's/st_ctim/Ctime/' \
374 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
375 -e 's/\([^a-zA-Z0-9_]\)_timespec_t\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
376 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
377 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
378 -e 's/Godump_[0-9] struct { \([^;]*;\) };/\1/g' \
379 >> ${OUT}
381 # The directory searching types.
382 # Prefer largefile variant if available.
383 dirent=`grep '^type _dirent64 ' gen-sysinfo.go || true`
384 if test "$dirent" != ""; then
385 grep '^type _dirent64 ' gen-sysinfo.go
386 else
387 grep '^type _dirent ' gen-sysinfo.go
388 fi | sed -e 's/type _dirent64/type Dirent/' \
389 -e 's/type _dirent/type Dirent/' \
390 -e 's/d_name \[0+1\]/d_name [0+256]/' \
391 -e 's/d_name/Name/' \
392 -e 's/]int8/]byte/' \
393 -e 's/d_ino/Ino/' \
394 -e 's/d_off/Off/' \
395 -e 's/d_reclen/Reclen/' \
396 -e 's/d_type/Type/' \
397 >> ${OUT}
398 echo "type DIR _DIR" >> ${OUT}
400 # The rusage struct.
401 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
402 if test "$rusage" != ""; then
403 rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
404 rusage=`echo $rusage | sed -e 's/^ *//'`
405 nrusage=
406 while test -n "$rusage"; do
407 field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
408 rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
409 # Drop the leading ru_, capitalize the next character.
410 field=`echo $field | sed -e 's/^ru_//'`
411 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
412 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
413 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
414 # Fix _timeval _timespec, and _timestruc_t.
415 r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
416 r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
417 r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
418 field="$f$r"
419 nrusage="$nrusage $field;"
420 done
421 echo "type Rusage struct {$nrusage }" >> ${OUT}
422 else
423 echo "type Rusage struct {}" >> ${OUT}
426 # The RUSAGE constants.
427 grep '^const _RUSAGE_' gen-sysinfo.go | \
428 sed -e 's/^\(const \)_\(RUSAGE_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
430 # The utsname struct.
431 grep '^type _utsname ' gen-sysinfo.go | \
432 sed -e 's/_utsname/Utsname/' \
433 -e 's/sysname/Sysname/' \
434 -e 's/nodename/Nodename/' \
435 -e 's/release/Release/' \
436 -e 's/version/Version/' \
437 -e 's/machine/Machine/' \
438 -e 's/domainname/Domainname/' \
439 >> ${OUT}
441 # The iovec struct.
442 iovec=`grep '^type _iovec ' gen-sysinfo.go`
443 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
444 echo "type Iovec_len_t $iovec_len" >> ${OUT}
445 echo $iovec | \
446 sed -e 's/_iovec/Iovec/' \
447 -e 's/iov_base/Base/' \
448 -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
449 >> ${OUT}
451 # The msghdr struct.
452 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
453 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
454 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
455 echo $msghdr | \
456 sed -e 's/_msghdr/Msghdr/' \
457 -e 's/msg_name/Name/' \
458 -e 's/msg_namelen/Namelen/' \
459 -e 's/msg_iov/Iov/' \
460 -e 's/msg_iovlen/Iovlen/' \
461 -e 's/_iovec/Iovec/' \
462 -e 's/msg_control/Control/' \
463 -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
464 -e 's/msg_flags/Flags/' \
465 >> ${OUT}
467 # The ip_mreq struct.
468 grep '^type _ip_mreq ' gen-sysinfo.go | \
469 sed -e 's/_ip_mreq/IPMreq/' \
470 -e 's/imr_multiaddr/Multiaddr/' \
471 -e 's/imr_interface/Interface/' \
472 -e 's/_in_addr/[4]byte/g' \
473 >> ${OUT}
475 # We need IPMreq to compile the net package.
476 if ! grep 'type IPMreq ' ${OUT} >/dev/null 2>&1; then
477 echo 'type IPMreq struct { Multiaddr [4]byte; Interface [4]byte; }' >> ${OUT}
480 # The size of the ip_mreq struct.
481 echo 'var SizeofIPMreq = int(unsafe.Sizeof(IPMreq{}))' >> ${OUT}
483 # The ipv6_mreq struct.
484 grep '^type _ipv6_mreq ' gen-sysinfo.go | \
485 sed -e 's/_ipv6_mreq/IPv6Mreq/' \
486 -e 's/ipv6mr_multiaddr/Multiaddr/' \
487 -e 's/ipv6mr_interface/Interface/' \
488 -e 's/_in6_addr/[16]byte/' \
489 >> ${OUT}
491 # We need IPv6Mreq to compile the net package.
492 if ! grep 'type IPv6Mreq ' ${OUT} >/dev/null 2>&1; then
493 echo 'type IPv6Mreq struct { Multiaddr [16]byte; Interface uint32; }' >> ${OUT}
496 # Try to guess the type to use for fd_set.
497 fd_set=`grep '^type _fd_set ' gen-sysinfo.go || true`
498 fds_bits_type="_C_long"
499 if test "$fd_set" != ""; then
500 fds_bits_type=`echo $fd_set | sed -e 's/.*[]]\([^;]*\); }$/\1/'`
502 echo "type fds_bits_type $fds_bits_type" >> ${OUT}
504 # The addrinfo struct.
505 grep '^type _addrinfo ' gen-sysinfo.go | \
506 sed -e 's/_addrinfo/Addrinfo/g' \
507 -e 's/ ai_/ Ai_/g' \
508 >> ${OUT}
510 # The addrinfo flags and errors.
511 grep '^const _AI_' gen-sysinfo.go | \
512 sed -e 's/^\(const \)_\(AI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
513 grep '^const _EAI_' gen-sysinfo.go | \
514 sed -e 's/^\(const \)_\(EAI_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
516 # The passwd struct.
517 grep '^type _passwd ' gen-sysinfo.go | \
518 sed -e 's/_passwd/Passwd/' \
519 -e 's/ pw_/ Pw_/g' \
520 >> ${OUT}
522 # The ioctl flags for the controlling TTY.
523 grep '^const _TIOC' gen-sysinfo.go | \
524 sed -e 's/^\(const \)_\(TIOC[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
526 # The ioctl flags for terminal control
527 grep '^const _TC[GS]ET' gen-sysinfo.go | \
528 sed -e 's/^\(const \)_\(TC[GS]ET[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
530 # ioctl constants. Might fall back to 0 if TIOCNXCL is missing, too, but
531 # needs handling in syscalls.exec.go.
532 if ! grep '^const _TIOCSCTTY ' gen-sysinfo.go >/dev/null 2>&1; then
533 if grep '^const _TIOCNXCL ' gen-sysinfo.go >/dev/null 2>&1; then
534 echo "const TIOCSCTTY = TIOCNXCL" >> ${OUT}
538 # The nlmsghdr struct.
539 grep '^type _nlmsghdr ' gen-sysinfo.go | \
540 sed -e 's/_nlmsghdr/NlMsghdr/' \
541 -e 's/nlmsg_len/Len/' \
542 -e 's/nlmsg_type/Type/' \
543 -e 's/nlmsg_flags/Flags/' \
544 -e 's/nlmsg_seq/Seq/' \
545 -e 's/nlmsg_pid/Pid/' \
546 >> ${OUT}
548 # The nlmsg flags and operators.
549 grep '^const _NLM' gen-sysinfo.go | \
550 sed -e 's/^\(const \)_\(NLM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
552 # NLMSG_HDRLEN is defined as an expression using sizeof.
553 if ! grep '^const NLMSG_HDRLEN' ${OUT} > /dev/null 2>&1; then
554 if grep '^type NlMsghdr ' ${OUT} > /dev/null 2>&1; then
555 echo 'var NLMSG_HDRLEN = int((unsafe.Sizeof(NlMsghdr{}) + (NLMSG_ALIGNTO-1)) &^ (NLMSG_ALIGNTO-1))' >> ${OUT}
559 # The rtmsg struct.
560 grep '^type _rtmsg ' gen-sysinfo.go | \
561 sed -e 's/_rtmsg/RtMsg/' \
562 -e 's/rtm_family/Family/' \
563 -e 's/rtm_dst_len/Dst_len/' \
564 -e 's/rtm_src_len/Src_len/' \
565 -e 's/rtm_tos/Tos/' \
566 -e 's/rtm_table/Table/' \
567 -e 's/rtm_protocol/Procotol/' \
568 -e 's/rtm_scope/Scope/' \
569 -e 's/rtm_type/Type/' \
570 -e 's/rtm_flags/Flags/' \
571 >> ${OUT}
573 # The size of the rtmsg struct.
574 if grep 'type RtMsg ' ${OUT} > /dev/null 2>&1; then
575 echo 'var SizeofRtMsg = int(unsafe.Sizeof(RtMsg{}))' >> ${OUT}
578 # The rtgenmsg struct.
579 grep '^type _rtgenmsg ' gen-sysinfo.go | \
580 sed -e 's/_rtgenmsg/RtGenmsg/' \
581 -e 's/rtgen_family/Family/' \
582 >> ${OUT}
584 # The routing message flags.
585 grep '^const _RTA' gen-sysinfo.go | \
586 sed -e 's/^\(const \)_\(RTA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
587 grep '^const _RTM' gen-sysinfo.go | \
588 sed -e 's/^\(const \)_\(RTM[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
590 # The size of the rtgenmsg struct.
591 if grep 'type RtGenmsg ' ${OUT} > /dev/null 2>&1; then
592 echo 'var SizeofRtGenmsg = int(unsafe.Sizeof(RtGenmsg{}))' >> ${OUT}
595 # The ifinfomsg struct.
596 grep '^type _ifinfomsg ' gen-sysinfo.go | \
597 sed -e 's/_ifinfomsg/IfInfomsg/' \
598 -e 's/ifi_family/Family/' \
599 -e 's/ifi_type/Type/' \
600 -e 's/ifi_index/Index/' \
601 -e 's/ifi_flags/Flags/' \
602 -e 's/ifi_change/Change/' \
603 >> ${OUT}
605 # The interface information types and flags.
606 grep '^const _IFA' gen-sysinfo.go | \
607 sed -e 's/^\(const \)_\(IFA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
608 grep '^const _IFLA' gen-sysinfo.go | \
609 sed -e 's/^\(const \)_\(IFLA[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
610 grep '^const _IFF' gen-sysinfo.go | \
611 sed -e 's/^\(const \)_\(IFF[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
613 # The size of the ifinfomsg struct.
614 if grep 'type IfInfomsg ' ${OUT} > /dev/null 2>&1; then
615 echo 'var SizeofIfInfomsg = int(unsafe.Sizeof(IfInfomsg{}))' >> ${OUT}
618 # The ifaddrmsg struct.
619 grep '^type _ifaddrmsg ' gen-sysinfo.go | \
620 sed -e 's/_ifaddrmsg/IfAddrmsg/' \
621 -e 's/ifa_family/Family/' \
622 -e 's/ifa_prefixlen/Prefixlen/' \
623 -e 's/ifa_flags/Flags/' \
624 -e 's/ifa_scope/Scope/' \
625 -e 's/ifa_index/Index/' \
626 >> ${OUT}
628 # The size of the ifaddrmsg struct.
629 if grep 'type IfAddrmsg ' ${OUT} > /dev/null 2>&1; then
630 echo 'var SizeofIfAddrmsg = int(unsafe.Sizeof(IfAddrmsg{}))' >> ${OUT}
633 # The rtattr struct.
634 grep '^type _rtattr ' gen-sysinfo.go | \
635 sed -e 's/_rtattr/RtAttr/' \
636 -e 's/rta_len/Len/' \
637 -e 's/rta_type/Type/' \
638 >> ${OUT}
640 # The size of the rtattr struct.
641 if grep 'type RtAttr ' ${OUT} > /dev/null 2>&1; then
642 echo 'var SizeofRtAttr = int(unsafe.Sizeof(RtAttr{}))' >> ${OUT}
645 # The termios struct.
646 grep '^type _termios ' gen-sysinfo.go | \
647 sed -e 's/_termios/Termios/' \
648 -e 's/c_iflag/Iflag/' \
649 -e 's/c_oflag/Oflag/' \
650 -e 's/c_cflag/Cflag/' \
651 -e 's/c_lflag/Lflag/' \
652 -e 's/c_line/Line/' \
653 -e 's/c_cc/Cc/' \
654 -e 's/c_ispeed/Ispeed/' \
655 -e 's/c_ospeed/Ospeed/' \
656 >> ${OUT}
658 # The termios constants.
659 for n in IGNBRK BRKINT IGNPAR PARMRK INPCK ISTRIP INLCR IGNCR ICRNL IUCLC \
660 IXON IXANY IXOFF IMAXBEL IUTF8 OPOST OLCUC ONLCR OCRNL ONOCR ONLRET \
661 OFILL OFDEL NLDLY NL0 NL1 CRDLY CR0 CR1 CR2 CR3 TABDLY BSDLY VTDLY \
662 FFDLY CBAUD CBAUDEX CSIZE CSTOPB CREAD PARENB PARODD HUPCL CLOCAL \
663 LOBLK CIBAUD CMSPAR CRTSCTS ISIG ICANON XCASE ECHO ECHOE ECHOK ECHONL \
664 ECHOCTL ECHOPRT ECHOKE DEFECHO FLUSHO NOFLSH TOSTOP PENDIN IEXTEN VINTR \
665 VQUIT VERASE VKILL VEOF VMIN VEOL VTIME VEOL2 VSWTCH VSTART VSTOP VSUSP \
666 VDSUSP VLNEXT VWERASE VREPRINT VDISCARD VSTATUS TCSANOW TCSADRAIN \
667 TCSAFLUSH TCIFLUSH TCOFLUSH TCIOFLUSH TCOOFF TCOON TCIOFF TCION B0 B50 \
668 B75 B110 B134 B150 B200 B300 B600 B1200 B1800 B2400 B4800 B9600 B19200 \
669 B38400 B57600 B115200 B230400; do
671 grep "^const _$n " gen-sysinfo.go | \
672 sed -e 's/^\(const \)_\([^=]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
673 done
675 exit $?