OSDN Git Service

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