]>
Commit | Line | Data |
---|---|---|
9b12e4fe JC |
1 | #!/usr/bin/env bash |
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. | |
5 | ||
6 | # Generate Go code listing errors and other #defined constant | |
7 | # values (ENAMETOOLONG etc.), by asking the preprocessor | |
8 | # about the definitions. | |
9 | ||
10 | unset LANG | |
11 | export LC_ALL=C | |
12 | export LC_CTYPE=C | |
13 | ||
14 | if test -z "$GOARCH" -o -z "$GOOS"; then | |
15 | echo 1>&2 "GOARCH or GOOS not defined in environment" | |
16 | exit 1 | |
17 | fi | |
18 | ||
19 | CC=${CC:-cc} | |
20 | ||
21 | if [[ "$GOOS" -eq "solaris" ]]; then | |
22 | # Assumes GNU versions of utilities in PATH. | |
23 | export PATH=/usr/gnu/bin:$PATH | |
24 | fi | |
25 | ||
26 | uname=$(uname) | |
27 | ||
28 | includes_Darwin=' | |
29 | #define _DARWIN_C_SOURCE | |
30 | #define KERNEL | |
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> | |
38 | #include <sys/mman.h> | |
39 | #include <sys/wait.h> | |
40 | #include <net/bpf.h> | |
41 | #include <net/if.h> | |
42 | #include <net/if_types.h> | |
43 | #include <net/route.h> | |
44 | #include <netinet/in.h> | |
45 | #include <netinet/ip.h> | |
46 | #include <termios.h> | |
47 | ' | |
48 | ||
49 | includes_DragonFly=' | |
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> | |
55 | #include <sys/mman.h> | |
56 | #include <sys/wait.h> | |
57 | #include <sys/ioctl.h> | |
58 | #include <net/bpf.h> | |
59 | #include <net/if.h> | |
60 | #include <net/if_types.h> | |
61 | #include <net/route.h> | |
62 | #include <netinet/in.h> | |
63 | #include <termios.h> | |
64 | #include <netinet/ip.h> | |
65 | #include <net/ip_mroute/ip_mroute.h> | |
66 | ' | |
67 | ||
68 | includes_FreeBSD=' | |
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> | |
75 | #include <sys/mman.h> | |
76 | #include <sys/wait.h> | |
77 | #include <sys/ioctl.h> | |
78 | #include <net/bpf.h> | |
79 | #include <net/if.h> | |
80 | #include <net/if_types.h> | |
81 | #include <net/route.h> | |
82 | #include <netinet/in.h> | |
83 | #include <termios.h> | |
84 | #include <netinet/ip.h> | |
85 | #include <netinet/ip_mroute.h> | |
86 | #include <sys/extattr.h> | |
87 | ||
88 | #if __FreeBSD__ >= 10 | |
89 | #define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10 | |
90 | #undef SIOCAIFADDR | |
91 | #define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data | |
92 | #undef SIOCSIFPHYADDR | |
93 | #define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data | |
94 | #endif | |
95 | ' | |
96 | ||
97 | includes_Linux=' | |
98 | #define _LARGEFILE_SOURCE | |
99 | #define _LARGEFILE64_SOURCE | |
100 | #ifndef __LP64__ | |
101 | #define _FILE_OFFSET_BITS 64 | |
102 | #endif | |
103 | #define _GNU_SOURCE | |
104 | ||
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> | |
132 | ||
133 | #ifndef MSG_FASTOPEN | |
134 | #define MSG_FASTOPEN 0x20000000 | |
135 | #endif | |
136 | ||
137 | #ifndef PTRACE_GETREGS | |
138 | #define PTRACE_GETREGS 0xc | |
139 | #endif | |
140 | ||
141 | #ifndef PTRACE_SETREGS | |
142 | #define PTRACE_SETREGS 0xd | |
143 | #endif | |
144 | ' | |
145 | ||
146 | includes_NetBSD=' | |
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> | |
157 | #include <net/bpf.h> | |
158 | #include <net/if.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> | |
166 | ||
167 | // Needed since <sys/param.h> refers to it... | |
168 | #define schedppq 1 | |
169 | ' | |
170 | ||
171 | includes_OpenBSD=' | |
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> | |
182 | #include <net/bpf.h> | |
183 | #include <net/if.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> | |
193 | ||
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 | |
204 | #define WALTSIG 0x4 | |
205 | ' | |
206 | ||
207 | includes_SunOS=' | |
208 | #include <limits.h> | |
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> | |
215 | #include <net/bpf.h> | |
216 | #include <net/if.h> | |
217 | #include <net/if_arp.h> | |
218 | #include <net/if_types.h> | |
219 | #include <net/route.h> | |
220 | #include <netinet/in.h> | |
221 | #include <termios.h> | |
222 | #include <netinet/ip.h> | |
223 | #include <netinet/ip_mroute.h> | |
224 | ' | |
225 | ||
226 | ||
227 | includes=' | |
228 | #include <sys/types.h> | |
229 | #include <sys/file.h> | |
230 | #include <fcntl.h> | |
231 | #include <dirent.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> | |
237 | #include <errno.h> | |
238 | #include <sys/signal.h> | |
239 | #include <signal.h> | |
240 | #include <sys/resource.h> | |
241 | #include <time.h> | |
242 | ' | |
243 | ccflags="$@" | |
244 | ||
245 | # Write go tool cgo -godefs input. | |
246 | ( | |
247 | echo package unix | |
248 | echo | |
249 | echo '/*' | |
250 | indirect="includes_$(uname)" | |
251 | echo "${!indirect} $includes" | |
252 | echo '*/' | |
253 | echo 'import "C"' | |
254 | echo 'import "syscall"' | |
255 | echo | |
256 | echo 'const (' | |
257 | ||
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 | | |
261 | awk ' | |
262 | $1 != "#define" || $2 ~ /\(/ || $3 == "" {next} | |
263 | ||
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. | |
269 | ||
270 | $2 ~ /^EXTATTR_NAMESPACE_NAMES/ || | |
271 | $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next} | |
272 | ||
273 | $2 !~ /^ETH_/ && | |
274 | $2 !~ /^EPROC_/ && | |
275 | $2 !~ /^EQUIV_/ && | |
276 | $2 !~ /^EXPR_/ && | |
277 | $2 ~ /^E[A-Z0-9_]+$/ || | |
278 | $2 ~ /^B[0-9_]+$/ || | |
279 | $2 == "BOTHER" || | |
280 | $2 ~ /^CI?BAUD(EX)?$/ || | |
281 | $2 == "IBSHIFT" || | |
282 | $2 ~ /^V[A-Z0-9]+$/ || | |
283 | $2 ~ /^CS[A-Z0-9]/ || | |
284 | $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ || | |
285 | $2 ~ /^IGN/ || | |
286 | $2 ~ /^IX(ON|ANY|OFF)$/ || | |
287 | $2 ~ /^IN(LCR|PCK)$/ || | |
288 | $2 ~ /(^FLU?SH)|(FLU?SH$)/ || | |
289 | $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ || | |
290 | $2 == "BRKINT" || | |
291 | $2 == "HUPCL" || | |
292 | $2 == "PENDIN" || | |
293 | $2 == "TOSTOP" || | |
294 | $2 == "XCASE" || | |
295 | $2 == "ALTWERASE" || | |
296 | $2 == "NOKERNINFO" || | |
297 | $2 ~ /^PAR/ || | |
298 | $2 ~ /^SIG[^_]/ || | |
299 | $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ || | |
300 | $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ || | |
301 | $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ || | |
302 | $2 ~ /^O?XTABS$/ || | |
303 | $2 ~ /^TC[IO](ON|OFF)$/ || | |
304 | $2 ~ /^IN_/ || | |
305 | $2 ~ /^LOCK_(SH|EX|NB|UN)$/ || | |
306 | $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ || | |
307 | $2 == "ICMPV6_FILTER" || | |
308 | $2 == "SOMAXCONN" || | |
309 | $2 == "NAME_MAX" || | |
310 | $2 == "IFNAMSIZ" || | |
311 | $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ || | |
312 | $2 ~ /^SYSCTL_VERS/ || | |
313 | $2 ~ /^(MS|MNT)_/ || | |
314 | $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ || | |
315 | $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ || | |
316 | $2 ~ /^LINUX_REBOOT_CMD_/ || | |
317 | $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ || | |
318 | $2 !~ "NLA_TYPE_MASK" && | |
319 | $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ || | |
320 | $2 ~ /^SIOC/ || | |
321 | $2 ~ /^TIOC/ || | |
322 | $2 ~ /^TCGET/ || | |
323 | $2 ~ /^TCSET/ || | |
324 | $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ || | |
325 | $2 !~ "RTF_BITS" && | |
326 | $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ || | |
327 | $2 ~ /^BIOC/ || | |
328 | $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ || | |
329 | $2 ~ /^RLIMIT_(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)_/ || | |
334 | $2 ~ /^CLOCK_/ || | |
335 | $2 !~ "WMESGLEN" && | |
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)} | |
339 | ||
340 | {next} | |
341 | ' | sort | |
342 | ||
343 | echo ')' | |
344 | ) >_const.go | |
345 | ||
346 | # Pull out the error names for later. | |
347 | errors=$( | |
348 | echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags | | |
349 | awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' | | |
350 | sort | |
351 | ) | |
352 | ||
353 | # Pull out the signal names for later. | |
354 | signals=$( | |
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)' | | |
358 | sort | |
359 | ) | |
360 | ||
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]*=" }' | | |
364 | sort >_error.grep | |
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)' | | |
368 | sort >_signal.grep | |
369 | ||
370 | echo '// mkerrors.sh' "$@" | |
371 | echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT' | |
372 | echo | |
373 | echo "// +build ${GOARCH},${GOOS}" | |
374 | echo | |
375 | go tool cgo -godefs -- "$@" _const.go >_error.out | |
376 | cat _error.out | grep -vf _error.grep | grep -vf _signal.grep | |
377 | echo | |
378 | echo '// Errors' | |
379 | echo 'const (' | |
380 | cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/' | |
381 | echo ')' | |
382 | ||
383 | echo | |
384 | echo '// Signals' | |
385 | echo 'const (' | |
386 | cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/' | |
387 | echo ')' | |
388 | ||
389 | # Run C program to print error and syscall strings. | |
390 | ( | |
391 | echo -E " | |
392 | #include <stdio.h> | |
393 | #include <stdlib.h> | |
394 | #include <errno.h> | |
395 | #include <ctype.h> | |
396 | #include <string.h> | |
397 | #include <signal.h> | |
398 | ||
399 | #define nelem(x) (sizeof(x)/sizeof((x)[0])) | |
400 | ||
401 | enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below | |
402 | ||
403 | int errors[] = { | |
404 | " | |
405 | for i in $errors | |
406 | do | |
407 | echo -E ' '$i, | |
408 | done | |
409 | ||
410 | echo -E " | |
411 | }; | |
412 | ||
413 | int signals[] = { | |
414 | " | |
415 | for i in $signals | |
416 | do | |
417 | echo -E ' '$i, | |
418 | done | |
419 | ||
420 | # Use -E because on some systems bash builtin interprets \n itself. | |
421 | echo -E ' | |
422 | }; | |
423 | ||
424 | static int | |
425 | intcmp(const void *a, const void *b) | |
426 | { | |
427 | return *(int*)a - *(int*)b; | |
428 | } | |
429 | ||
430 | int | |
431 | main(void) | |
432 | { | |
433 | int i, j, e; | |
434 | char buf[1024], *p; | |
435 | ||
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++) { | |
440 | e = errors[i]; | |
441 | if(i > 0 && errors[i-1] == e) | |
442 | continue; | |
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) | |
446 | buf[0] += a - A; | |
447 | printf("\t%d: \"%s\",\n", e, buf); | |
448 | } | |
449 | printf("}\n\n"); | |
450 | ||
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++) { | |
455 | e = signals[i]; | |
456 | if(i > 0 && signals[i-1] == e) | |
457 | continue; | |
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) | |
461 | buf[0] += a - A; | |
462 | // cut trailing : number. | |
463 | p = strrchr(buf, ":"[0]); | |
464 | if(p) | |
465 | *p = '\0'; | |
466 | printf("\t%d: \"%s\",\n", e, buf); | |
467 | } | |
468 | printf("}\n\n"); | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
473 | ' | |
474 | ) >_errors.c | |
475 | ||
476 | $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out |