OSDN Git Service

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