OSDN Git Service

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