blob: 67b84828ac063a289809f08e7553c91803e47965 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001#!/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
10unset LANG
11export LC_ALL=C
12export LC_CTYPE=C
13
14if test -z "$GOARCH" -o -z "$GOOS"; then
15 echo 1>&2 "GOARCH or GOOS not defined in environment"
16 exit 1
17fi
18
19# Check that we are using the new build system if we should
20if [[ "$GOOS" = "linux" ]] && [[ "$GOLANG_SYS_BUILD" != "docker" ]]; then
21 echo 1>&2 "In the Docker based build system, mkerrors should not be called directly."
22 echo 1>&2 "See README.md"
23 exit 1
24fi
25
26if [[ "$GOOS" = "aix" ]]; then
27 CC=${CC:-gcc}
28else
29 CC=${CC:-cc}
30fi
31
32if [[ "$GOOS" = "solaris" ]]; then
33 # Assumes GNU versions of utilities in PATH.
34 export PATH=/usr/gnu/bin:$PATH
35fi
36
37uname=$(uname)
38
39includes_AIX='
40#include <net/if.h>
41#include <net/netopt.h>
42#include <netinet/ip_mroute.h>
43#include <sys/protosw.h>
44#include <sys/stropts.h>
45#include <sys/mman.h>
46#include <sys/poll.h>
47#include <sys/termio.h>
48#include <termios.h>
49#include <fcntl.h>
50
51#define AF_LOCAL AF_UNIX
52'
53
54includes_Darwin='
55#define _DARWIN_C_SOURCE
56#define KERNEL
57#define _DARWIN_USE_64_BIT_INODE
58#include <stdint.h>
59#include <sys/attr.h>
60#include <sys/types.h>
61#include <sys/event.h>
62#include <sys/ptrace.h>
Don Newton7577f072020-01-06 12:41:11 -050063#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -040064#include <sys/socket.h>
65#include <sys/sockio.h>
66#include <sys/sysctl.h>
67#include <sys/mman.h>
68#include <sys/mount.h>
69#include <sys/utsname.h>
70#include <sys/wait.h>
71#include <sys/xattr.h>
72#include <net/bpf.h>
73#include <net/if.h>
74#include <net/if_types.h>
75#include <net/route.h>
76#include <netinet/in.h>
77#include <netinet/ip.h>
78#include <termios.h>
79'
80
81includes_DragonFly='
82#include <sys/types.h>
83#include <sys/event.h>
Don Newton7577f072020-01-06 12:41:11 -050084#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -040085#include <sys/socket.h>
86#include <sys/sockio.h>
87#include <sys/stat.h>
88#include <sys/sysctl.h>
89#include <sys/mman.h>
90#include <sys/mount.h>
91#include <sys/wait.h>
92#include <sys/ioctl.h>
93#include <net/bpf.h>
94#include <net/if.h>
95#include <net/if_types.h>
96#include <net/route.h>
97#include <netinet/in.h>
98#include <termios.h>
99#include <netinet/ip.h>
100#include <net/ip_mroute/ip_mroute.h>
101'
102
103includes_FreeBSD='
104#include <sys/capsicum.h>
105#include <sys/param.h>
106#include <sys/types.h>
107#include <sys/event.h>
Don Newton7577f072020-01-06 12:41:11 -0500108#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -0400109#include <sys/socket.h>
110#include <sys/sockio.h>
111#include <sys/stat.h>
112#include <sys/sysctl.h>
113#include <sys/mman.h>
114#include <sys/mount.h>
115#include <sys/wait.h>
116#include <sys/ioctl.h>
117#include <net/bpf.h>
118#include <net/if.h>
119#include <net/if_types.h>
120#include <net/route.h>
121#include <netinet/in.h>
122#include <termios.h>
123#include <netinet/ip.h>
124#include <netinet/ip_mroute.h>
125#include <sys/extattr.h>
126
127#if __FreeBSD__ >= 10
128#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
129#undef SIOCAIFADDR
130#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
131#undef SIOCSIFPHYADDR
132#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
133#endif
134'
135
136includes_Linux='
137#define _LARGEFILE_SOURCE
138#define _LARGEFILE64_SOURCE
139#ifndef __LP64__
140#define _FILE_OFFSET_BITS 64
141#endif
142#define _GNU_SOURCE
143
144// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
145// these structures. We just include them copied from <bits/termios.h>.
146#if defined(__powerpc__)
147struct sgttyb {
148 char sg_ispeed;
149 char sg_ospeed;
150 char sg_erase;
151 char sg_kill;
152 short sg_flags;
153};
154
155struct tchars {
156 char t_intrc;
157 char t_quitc;
158 char t_startc;
159 char t_stopc;
160 char t_eofc;
161 char t_brkc;
162};
163
164struct ltchars {
165 char t_suspc;
166 char t_dsuspc;
167 char t_rprntc;
168 char t_flushc;
169 char t_werasc;
170 char t_lnextc;
171};
172#endif
173
174#include <bits/sockaddr.h>
175#include <sys/epoll.h>
176#include <sys/eventfd.h>
177#include <sys/inotify.h>
178#include <sys/ioctl.h>
179#include <sys/mman.h>
180#include <sys/mount.h>
181#include <sys/prctl.h>
182#include <sys/stat.h>
183#include <sys/types.h>
184#include <sys/time.h>
Don Newton7577f072020-01-06 12:41:11 -0500185#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -0400186#include <sys/signalfd.h>
187#include <sys/socket.h>
188#include <sys/xattr.h>
Don Newton7577f072020-01-06 12:41:11 -0500189#include <linux/bpf.h>
190#include <linux/can.h>
191#include <linux/capability.h>
192#include <linux/cryptouser.h>
Don Newton98fd8812019-09-23 15:15:02 -0400193#include <linux/errqueue.h>
Don Newton7577f072020-01-06 12:41:11 -0500194#include <linux/falloc.h>
195#include <linux/fanotify.h>
196#include <linux/filter.h>
197#include <linux/fs.h>
198#include <linux/genetlink.h>
199#include <linux/hdreg.h>
200#include <linux/icmpv6.h>
Don Newton98fd8812019-09-23 15:15:02 -0400201#include <linux/if.h>
Don Newton7577f072020-01-06 12:41:11 -0500202#include <linux/if_addr.h>
Don Newton98fd8812019-09-23 15:15:02 -0400203#include <linux/if_alg.h>
204#include <linux/if_arp.h>
205#include <linux/if_ether.h>
206#include <linux/if_ppp.h>
207#include <linux/if_tun.h>
208#include <linux/if_packet.h>
Don Newton7577f072020-01-06 12:41:11 -0500209#include <linux/if_xdp.h>
Don Newton98fd8812019-09-23 15:15:02 -0400210#include <linux/kexec.h>
211#include <linux/keyctl.h>
Don Newton7577f072020-01-06 12:41:11 -0500212#include <linux/loop.h>
Don Newton98fd8812019-09-23 15:15:02 -0400213#include <linux/magic.h>
214#include <linux/memfd.h>
215#include <linux/module.h>
216#include <linux/netfilter/nfnetlink.h>
217#include <linux/netlink.h>
218#include <linux/net_namespace.h>
Don Newton7577f072020-01-06 12:41:11 -0500219#include <linux/nsfs.h>
Don Newton98fd8812019-09-23 15:15:02 -0400220#include <linux/perf_event.h>
Don Newton7577f072020-01-06 12:41:11 -0500221#include <linux/ptrace.h>
Don Newton98fd8812019-09-23 15:15:02 -0400222#include <linux/random.h>
223#include <linux/reboot.h>
Don Newton7577f072020-01-06 12:41:11 -0500224#include <linux/rtc.h>
Don Newton98fd8812019-09-23 15:15:02 -0400225#include <linux/rtnetlink.h>
226#include <linux/sched.h>
227#include <linux/seccomp.h>
Don Newtone0d34a82019-11-14 10:58:06 -0500228#include <linux/serial.h>
Don Newton7577f072020-01-06 12:41:11 -0500229#include <linux/sockios.h>
Don Newtone0d34a82019-11-14 10:58:06 -0500230#include <linux/taskstats.h>
Don Newton7577f072020-01-06 12:41:11 -0500231#include <linux/tipc.h>
232#include <linux/vm_sockets.h>
233#include <linux/wait.h>
Don Newton98fd8812019-09-23 15:15:02 -0400234#include <linux/watchdog.h>
Don Newton7577f072020-01-06 12:41:11 -0500235
Don Newton98fd8812019-09-23 15:15:02 -0400236#include <mtd/ubi-user.h>
237#include <net/route.h>
238
239#if defined(__sparc__)
240// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
241// definition in glibc. As only the error constants are needed here, include the
242// generic termibits.h (which is included by termbits.h on sparc).
243#include <asm-generic/termbits.h>
244#else
245#include <asm/termbits.h>
246#endif
247
248#ifndef MSG_FASTOPEN
249#define MSG_FASTOPEN 0x20000000
250#endif
251
252#ifndef PTRACE_GETREGS
253#define PTRACE_GETREGS 0xc
254#endif
255
256#ifndef PTRACE_SETREGS
257#define PTRACE_SETREGS 0xd
258#endif
259
260#ifndef SOL_NETLINK
261#define SOL_NETLINK 270
262#endif
263
264#ifdef SOL_BLUETOOTH
265// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
266// but it is already in bluetooth_linux.go
267#undef SOL_BLUETOOTH
268#endif
269
270// Certain constants are missing from the fs/crypto UAPI
271#define FS_KEY_DESC_PREFIX "fscrypt:"
272#define FS_KEY_DESC_PREFIX_SIZE 8
273#define FS_MAX_KEY_SIZE 64
Don Newton7577f072020-01-06 12:41:11 -0500274
275// The code generator produces -0x1 for (~0), but an unsigned value is necessary
276// for the tipc_subscr timeout __u32 field.
277#undef TIPC_WAIT_FOREVER
278#define TIPC_WAIT_FOREVER 0xffffffff
Don Newton98fd8812019-09-23 15:15:02 -0400279'
280
281includes_NetBSD='
282#include <sys/types.h>
283#include <sys/param.h>
284#include <sys/event.h>
285#include <sys/extattr.h>
286#include <sys/mman.h>
287#include <sys/mount.h>
Don Newton7577f072020-01-06 12:41:11 -0500288#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -0400289#include <sys/socket.h>
290#include <sys/sockio.h>
291#include <sys/sysctl.h>
292#include <sys/termios.h>
293#include <sys/ttycom.h>
294#include <sys/wait.h>
295#include <net/bpf.h>
296#include <net/if.h>
297#include <net/if_types.h>
298#include <net/route.h>
299#include <netinet/in.h>
300#include <netinet/in_systm.h>
301#include <netinet/ip.h>
302#include <netinet/ip_mroute.h>
303#include <netinet/if_ether.h>
304
305// Needed since <sys/param.h> refers to it...
306#define schedppq 1
307'
308
309includes_OpenBSD='
310#include <sys/types.h>
311#include <sys/param.h>
312#include <sys/event.h>
313#include <sys/mman.h>
314#include <sys/mount.h>
Don Newton7577f072020-01-06 12:41:11 -0500315#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -0400316#include <sys/socket.h>
317#include <sys/sockio.h>
318#include <sys/stat.h>
319#include <sys/sysctl.h>
320#include <sys/termios.h>
321#include <sys/ttycom.h>
322#include <sys/unistd.h>
323#include <sys/wait.h>
324#include <net/bpf.h>
325#include <net/if.h>
326#include <net/if_types.h>
327#include <net/if_var.h>
328#include <net/route.h>
329#include <netinet/in.h>
330#include <netinet/in_systm.h>
331#include <netinet/ip.h>
332#include <netinet/ip_mroute.h>
333#include <netinet/if_ether.h>
334#include <net/if_bridge.h>
335
336// We keep some constants not supported in OpenBSD 5.5 and beyond for
337// the promise of compatibility.
338#define EMUL_ENABLED 0x1
339#define EMUL_NATIVE 0x2
340#define IPV6_FAITH 0x1d
341#define IPV6_OPTIONS 0x1
342#define IPV6_RTHDR_STRICT 0x1
343#define IPV6_SOCKOPT_RESERVED1 0x3
344#define SIOCGIFGENERIC 0xc020693a
345#define SIOCSIFGENERIC 0x80206939
346#define WALTSIG 0x4
347'
348
349includes_SunOS='
350#include <limits.h>
351#include <sys/types.h>
Don Newton7577f072020-01-06 12:41:11 -0500352#include <sys/select.h>
Don Newton98fd8812019-09-23 15:15:02 -0400353#include <sys/socket.h>
354#include <sys/sockio.h>
355#include <sys/stat.h>
356#include <sys/mman.h>
357#include <sys/wait.h>
358#include <sys/ioctl.h>
359#include <sys/mkdev.h>
360#include <net/bpf.h>
361#include <net/if.h>
362#include <net/if_arp.h>
363#include <net/if_types.h>
364#include <net/route.h>
365#include <netinet/in.h>
366#include <termios.h>
367#include <netinet/ip.h>
368#include <netinet/ip_mroute.h>
369'
370
371
372includes='
373#include <sys/types.h>
374#include <sys/file.h>
375#include <fcntl.h>
376#include <dirent.h>
377#include <sys/socket.h>
378#include <netinet/in.h>
379#include <netinet/ip.h>
380#include <netinet/ip6.h>
381#include <netinet/tcp.h>
382#include <errno.h>
383#include <sys/signal.h>
384#include <signal.h>
385#include <sys/resource.h>
386#include <time.h>
387'
388ccflags="$@"
389
390# Write go tool cgo -godefs input.
391(
392 echo package unix
393 echo
394 echo '/*'
395 indirect="includes_$(uname)"
396 echo "${!indirect} $includes"
397 echo '*/'
398 echo 'import "C"'
399 echo 'import "syscall"'
400 echo
401 echo 'const ('
402
403 # The gcc command line prints all the #defines
404 # it encounters while processing the input
405 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
406 awk '
407 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
408
409 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
410 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
411 $2 ~ /^(SCM_SRCRT)$/ {next}
412 $2 ~ /^(MAP_FAILED)$/ {next}
413 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
414
415 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
416 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
417
418 $2 !~ /^ECCAPBITS/ &&
419 $2 !~ /^ETH_/ &&
420 $2 !~ /^EPROC_/ &&
421 $2 !~ /^EQUIV_/ &&
422 $2 !~ /^EXPR_/ &&
423 $2 ~ /^E[A-Z0-9_]+$/ ||
424 $2 ~ /^B[0-9_]+$/ ||
425 $2 ~ /^(OLD|NEW)DEV$/ ||
426 $2 == "BOTHER" ||
427 $2 ~ /^CI?BAUD(EX)?$/ ||
428 $2 == "IBSHIFT" ||
429 $2 ~ /^V[A-Z0-9]+$/ ||
430 $2 ~ /^CS[A-Z0-9]/ ||
431 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
432 $2 ~ /^IGN/ ||
433 $2 ~ /^IX(ON|ANY|OFF)$/ ||
434 $2 ~ /^IN(LCR|PCK)$/ ||
435 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
436 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
437 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
438 $2 == "BRKINT" ||
439 $2 == "HUPCL" ||
440 $2 == "PENDIN" ||
441 $2 == "TOSTOP" ||
442 $2 == "XCASE" ||
443 $2 == "ALTWERASE" ||
444 $2 == "NOKERNINFO" ||
Don Newton7577f072020-01-06 12:41:11 -0500445 $2 == "NFDBITS" ||
Don Newton98fd8812019-09-23 15:15:02 -0400446 $2 ~ /^PAR/ ||
447 $2 ~ /^SIG[^_]/ ||
448 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
449 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
450 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
451 $2 ~ /^O?XTABS$/ ||
452 $2 ~ /^TC[IO](ON|OFF)$/ ||
453 $2 ~ /^IN_/ ||
454 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
Don Newton7577f072020-01-06 12:41:11 -0500455 $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
456 $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
457 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|MCAST|EVFILT|NOTE|EV|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
Don Newton98fd8812019-09-23 15:15:02 -0400458 $2 ~ /^TP_STATUS_/ ||
459 $2 ~ /^FALLOC_/ ||
460 $2 == "ICMPV6_FILTER" ||
461 $2 == "SOMAXCONN" ||
462 $2 == "NAME_MAX" ||
463 $2 == "IFNAMSIZ" ||
464 $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
465 $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
466 $2 ~ /^HW_MACHINE$/ ||
467 $2 ~ /^SYSCTL_VERS/ ||
468 $2 !~ "MNT_BITS" &&
469 $2 ~ /^(MS|MNT|UMOUNT)_/ ||
Don Newton7577f072020-01-06 12:41:11 -0500470 $2 ~ /^NS_GET_/ ||
Don Newton98fd8812019-09-23 15:15:02 -0400471 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
472 $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT)_/ ||
473 $2 ~ /^KEXEC_/ ||
474 $2 ~ /^LINUX_REBOOT_CMD_/ ||
475 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
476 $2 ~ /^MODULE_INIT_/ ||
477 $2 !~ "NLA_TYPE_MASK" &&
478 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
479 $2 ~ /^SIOC/ ||
480 $2 ~ /^TIOC/ ||
481 $2 ~ /^TCGET/ ||
482 $2 ~ /^TCSET/ ||
483 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
484 $2 !~ "RTF_BITS" &&
485 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
486 $2 ~ /^BIOC/ ||
487 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
488 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
489 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
490 $2 ~ /^CLONE_[A-Z_]+/ ||
Don Newton7577f072020-01-06 12:41:11 -0500491 $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
Don Newton98fd8812019-09-23 15:15:02 -0400492 $2 ~ /^(BPF|DLT)_/ ||
493 $2 ~ /^(CLOCK|TIMER)_/ ||
494 $2 ~ /^CAN_/ ||
495 $2 ~ /^CAP_/ ||
496 $2 ~ /^ALG_/ ||
497 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE|IOC_(GET|SET)_ENCRYPTION)/ ||
498 $2 ~ /^GRND_/ ||
499 $2 ~ /^RND/ ||
500 $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
501 $2 ~ /^KEYCTL_/ ||
502 $2 ~ /^PERF_EVENT_IOC_/ ||
503 $2 ~ /^SECCOMP_MODE_/ ||
504 $2 ~ /^SPLICE_/ ||
505 $2 ~ /^SYNC_FILE_RANGE_/ ||
506 $2 !~ /^AUDIT_RECORD_MAGIC/ &&
507 $2 !~ /IOC_MAGIC/ &&
508 $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
509 $2 ~ /^(VM|VMADDR)_/ ||
510 $2 ~ /^IOCTL_VM_SOCKETS_/ ||
511 $2 ~ /^(TASKSTATS|TS)_/ ||
512 $2 ~ /^CGROUPSTATS_/ ||
513 $2 ~ /^GENL_/ ||
514 $2 ~ /^STATX_/ ||
515 $2 ~ /^RENAME/ ||
516 $2 ~ /^UBI_IOC[A-Z]/ ||
517 $2 ~ /^UTIME_/ ||
518 $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
519 $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
520 $2 ~ /^FSOPT_/ ||
521 $2 ~ /^WDIOC_/ ||
522 $2 ~ /^NFN/ ||
523 $2 ~ /^XDP_/ ||
524 $2 ~ /^(HDIO|WIN|SMART)_/ ||
Don Newton7577f072020-01-06 12:41:11 -0500525 $2 ~ /^CRYPTO_/ ||
526 $2 ~ /^TIPC_/ ||
Don Newton98fd8812019-09-23 15:15:02 -0400527 $2 !~ "WMESGLEN" &&
528 $2 ~ /^W[A-Z0-9]+$/ ||
529 $2 ~/^PPPIOC/ ||
Don Newton7577f072020-01-06 12:41:11 -0500530 $2 ~ /^FAN_|FANOTIFY_/ ||
Don Newton98fd8812019-09-23 15:15:02 -0400531 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
532 $2 ~ /^__WCOREFLAG$/ {next}
533 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
534
535 {next}
536 ' | sort
537
538 echo ')'
539) >_const.go
540
541# Pull out the error names for later.
542errors=$(
543 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
544 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
545 sort
546)
547
548# Pull out the signal names for later.
549signals=$(
550 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
551 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
552 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
553 sort
554)
555
556# Again, writing regexps to a file.
557echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
558 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
559 sort >_error.grep
560echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
561 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
562 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
563 sort >_signal.grep
564
565echo '// mkerrors.sh' "$@"
566echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
567echo
568echo "// +build ${GOARCH},${GOOS}"
569echo
570go tool cgo -godefs -- "$@" _const.go >_error.out
571cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
572echo
573echo '// Errors'
574echo 'const ('
575cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
576echo ')'
577
578echo
579echo '// Signals'
580echo 'const ('
581cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
582echo ')'
583
584# Run C program to print error and syscall strings.
585(
586 echo -E "
587#include <stdio.h>
588#include <stdlib.h>
589#include <errno.h>
590#include <ctype.h>
591#include <string.h>
592#include <signal.h>
593
594#define nelem(x) (sizeof(x)/sizeof((x)[0]))
595
596enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
597
598struct tuple {
599 int num;
600 const char *name;
601};
602
603struct tuple errors[] = {
604"
605 for i in $errors
606 do
607 echo -E ' {'$i', "'$i'" },'
608 done
609
610 echo -E "
611};
612
613struct tuple signals[] = {
614"
615 for i in $signals
616 do
617 echo -E ' {'$i', "'$i'" },'
618 done
619
620 # Use -E because on some systems bash builtin interprets \n itself.
621 echo -E '
622};
623
624static int
625tuplecmp(const void *a, const void *b)
626{
627 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
628}
629
630int
631main(void)
632{
633 int i, e;
634 char buf[1024], *p;
635
636 printf("\n\n// Error table\n");
637 printf("var errorList = [...]struct {\n");
638 printf("\tnum syscall.Errno\n");
639 printf("\tname string\n");
640 printf("\tdesc string\n");
641 printf("} {\n");
642 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
643 for(i=0; i<nelem(errors); i++) {
644 e = errors[i].num;
645 if(i > 0 && errors[i-1].num == e)
646 continue;
647 strcpy(buf, strerror(e));
648 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
649 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
650 buf[0] += a - A;
651 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
652 }
653 printf("}\n\n");
654
655 printf("\n\n// Signal table\n");
656 printf("var signalList = [...]struct {\n");
657 printf("\tnum syscall.Signal\n");
658 printf("\tname string\n");
659 printf("\tdesc string\n");
660 printf("} {\n");
661 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
662 for(i=0; i<nelem(signals); i++) {
663 e = signals[i].num;
664 if(i > 0 && signals[i-1].num == e)
665 continue;
666 strcpy(buf, strsignal(e));
667 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
668 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
669 buf[0] += a - A;
670 // cut trailing : number.
671 p = strrchr(buf, ":"[0]);
672 if(p)
673 *p = '\0';
674 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
675 }
676 printf("}\n\n");
677
678 return 0;
679}
680
681'
682) >_errors.c
683
684$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out