2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # Generate Go code listing errors and other #defined constant
7 # values (ENAMETOOLONG etc.), by asking the preprocessor
8 # about the definitions.
14 if test -z "$GOARCH" -o -z "$GOOS"; then
15 echo 1>&2 "GOARCH or GOOS not defined in environment"
21 if [[ "$GOOS" -eq "solaris" ]]; then
22 # Assumes GNU versions of utilities in PATH.
23 export PATH
=/usr
/gnu
/bin
:$PATH
29 #define _DARWIN_C_SOURCE
31 #define _DARWIN_USE_64_BIT_INODE
32 #include <sys/types.h>
33 #include <sys/event.h>
34 #include <sys/ptrace.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/ip.h>
50 #include <sys/types.h>
51 #include <sys/event.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
57 #include <sys/ioctl.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
64 #include <netinet/ip.h>
65 #include <net/ip_mroute/ip_mroute.h>
69 #include <sys/param.h>
70 #include <sys/types.h>
71 #include <sys/event.h>
72 #include <sys/socket.h>
73 #include <sys/sockio.h>
74 #include <sys/sysctl.h>
77 #include <sys/ioctl.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
82 #include <netinet/in.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_mroute.h>
86 #include <sys/extattr.h>
89 #define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
91 #define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
93 #define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
98 #define _LARGEFILE_SOURCE
99 #define _LARGEFILE64_SOURCE
101 #define _FILE_OFFSET_BITS 64
105 #include <bits/sockaddr.h>
106 #include <sys/epoll.h>
107 #include <sys/inotify.h>
108 #include <sys/ioctl.h>
109 #include <sys/mman.h>
110 #include <sys/mount.h>
111 #include <sys/prctl.h>
112 #include <sys/stat.h>
113 #include <sys/types.h>
114 #include <sys/time.h>
115 #include <sys/socket.h>
116 #include <linux/if.h>
117 #include <linux/if_arp.h>
118 #include <linux/if_ether.h>
119 #include <linux/if_tun.h>
120 #include <linux/if_packet.h>
121 #include <linux/if_addr.h>
122 #include <linux/filter.h>
123 #include <linux/netlink.h>
124 #include <linux/reboot.h>
125 #include <linux/rtnetlink.h>
126 #include <linux/ptrace.h>
127 #include <linux/sched.h>
128 #include <linux/wait.h>
129 #include <linux/icmpv6.h>
130 #include <net/route.h>
131 #include <asm/termbits.h>
134 #define MSG_FASTOPEN 0x20000000
137 #ifndef PTRACE_GETREGS
138 #define PTRACE_GETREGS 0xc
141 #ifndef PTRACE_SETREGS
142 #define PTRACE_SETREGS 0xd
147 #include <sys/types.h>
148 #include <sys/param.h>
149 #include <sys/event.h>
150 #include <sys/mman.h>
151 #include <sys/socket.h>
152 #include <sys/sockio.h>
153 #include <sys/sysctl.h>
154 #include <sys/termios.h>
155 #include <sys/ttycom.h>
156 #include <sys/wait.h>
159 #include <net/if_types.h>
160 #include <net/route.h>
161 #include <netinet/in.h>
162 #include <netinet/in_systm.h>
163 #include <netinet/ip.h>
164 #include <netinet/ip_mroute.h>
165 #include <netinet/if_ether.h>
167 // Needed since <sys/param.h> refers to it...
172 #include <sys/types.h>
173 #include <sys/param.h>
174 #include <sys/event.h>
175 #include <sys/mman.h>
176 #include <sys/socket.h>
177 #include <sys/sockio.h>
178 #include <sys/sysctl.h>
179 #include <sys/termios.h>
180 #include <sys/ttycom.h>
181 #include <sys/wait.h>
184 #include <net/if_types.h>
185 #include <net/if_var.h>
186 #include <net/route.h>
187 #include <netinet/in.h>
188 #include <netinet/in_systm.h>
189 #include <netinet/ip.h>
190 #include <netinet/ip_mroute.h>
191 #include <netinet/if_ether.h>
192 #include <net/if_bridge.h>
194 // We keep some constants not supported in OpenBSD 5.5 and beyond for
195 // the promise of compatibility.
196 #define EMUL_ENABLED 0x1
197 #define EMUL_NATIVE 0x2
198 #define IPV6_FAITH 0x1d
199 #define IPV6_OPTIONS 0x1
200 #define IPV6_RTHDR_STRICT 0x1
201 #define IPV6_SOCKOPT_RESERVED1 0x3
202 #define SIOCGIFGENERIC 0xc020693a
203 #define SIOCSIFGENERIC 0x80206939
209 #include <sys/types.h>
210 #include <sys/socket.h>
211 #include <sys/sockio.h>
212 #include <sys/mman.h>
213 #include <sys/wait.h>
214 #include <sys/ioctl.h>
217 #include <net/if_arp.h>
218 #include <net/if_types.h>
219 #include <net/route.h>
220 #include <netinet/in.h>
222 #include <netinet/ip.h>
223 #include <netinet/ip_mroute.h>
228 #include <sys/types.h>
229 #include <sys/file.h>
232 #include <sys/socket.h>
233 #include <netinet/in.h>
234 #include <netinet/ip.h>
235 #include <netinet/ip6.h>
236 #include <netinet/tcp.h>
238 #include <sys/signal.h>
240 #include <sys/resource.h>
245 # Write go tool cgo -godefs input.
250 indirect
="includes_$(uname)"
251 echo "${!indirect} $includes"
254 echo 'import "syscall"'
258 # The gcc command line prints all the #defines
259 # it encounters while processing the input
260 echo "${!indirect} $includes" | $CC -x c
- -E -dM $ccflags |
262 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
264 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
265 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
266 $2 ~ /^(SCM_SRCRT)$/ {next}
267 $2 ~ /^(MAP_FAILED)$/ {next}
268 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
270 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
271 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
277 $2 ~ /^E[A-Z0-9_]+$/ ||
280 $2 ~ /^CI?BAUD(EX)?$/ ||
282 $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T_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
330 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
331 $2 ~ /^CLONE_[A-Z_]+/ ||
332 $2 !~ /^(BPF_TIMEVAL)$/ &&
333 $2 ~ /^(BPF|DLT)_/ ||
336 $2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
337 $2 ~ /^__WCOREFLAG$/ {next}
338 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
346 # Pull out the error names for later.
348 echo '#include <errno.h>' | $CC -x c
- -E -dM $ccflags |
349 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
353 # Pull out the signal names for later.
355 echo '#include <signal.h>' | $CC -x c
- -E -dM $ccflags |
356 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
357 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
361 # Again, writing regexps to a file.
362 echo '#include <errno.h>' | $CC -x c
- -E -dM $ccflags |
363 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
365 echo '#include <signal.h>' | $CC -x c
- -E -dM $ccflags |
366 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
367 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
370 echo '// mkerrors.sh' "$@"
371 echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
373 echo "// +build ${GOARCH},${GOOS}"
375 go tool cgo
-godefs -- "$@" _const.go
>_error.out
376 cat _error.out
| grep -vf _error.
grep | grep -vf _signal.
grep
380 cat _error.out
| grep -f _error.
grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
386 cat _error.out
| grep -f _signal.
grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
389 # Run C program to print error and syscall strings.
399 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
401 enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
420 # Use -E because on some systems bash builtin interprets \n itself.
425 intcmp(const void *a, const void *b)
427 return *(int*)a - *(int*)b;
436 printf("\n\n// Error table\n");
437 printf("var errors = [...]string {\n");
438 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
439 for(i=0; i<nelem(errors); i++) {
441 if(i > 0 && errors[i-1] == e)
443 strcpy(buf, strerror(e));
444 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
445 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
447 printf("\t%d: \"%s\",\n", e, buf);
451 printf("\n\n// Signal table\n");
452 printf("var signals = [...]string {\n");
453 qsort(signals, nelem(signals), sizeof signals[0], intcmp);
454 for(i=0; i<nelem(signals); i++) {
456 if(i > 0 && signals[i-1] == e)
458 strcpy(buf, strsignal(e));
459 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
460 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
462 // cut trailing : number.
463 p = strrchr(buf, ":"[0]);
466 printf("\t%d: \"%s\",\n", e, buf);
476 $CC $ccflags -o _errors _errors.c
&& $GORUN .
/_errors
&& rm -f _errors.c _errors _const.go _error.
grep _signal.
grep _error.out