OSDN Git Service

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