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
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/select.h>
48#include <sys/termio.h>
49#include <termios.h>
50#include <fcntl.h>
51
52#define AF_LOCAL AF_UNIX
53'
54
55includes_Darwin='
56#define _DARWIN_C_SOURCE
57#define KERNEL 1
58#define _DARWIN_USE_64_BIT_INODE
59#define __APPLE_USE_RFC_3542
60#include <stdint.h>
61#include <sys/attr.h>
62#include <sys/clonefile.h>
63#include <sys/kern_control.h>
64#include <sys/types.h>
65#include <sys/event.h>
66#include <sys/ptrace.h>
67#include <sys/select.h>
68#include <sys/socket.h>
69#include <sys/stat.h>
70#include <sys/un.h>
71#include <sys/sockio.h>
72#include <sys/sys_domain.h>
73#include <sys/sysctl.h>
74#include <sys/mman.h>
75#include <sys/mount.h>
76#include <sys/utsname.h>
77#include <sys/wait.h>
78#include <sys/xattr.h>
79#include <sys/vsock.h>
80#include <net/bpf.h>
81#include <net/if.h>
82#include <net/if_types.h>
83#include <net/route.h>
84#include <netinet/in.h>
85#include <netinet/ip.h>
86#include <termios.h>
87
88// for backwards compatibility because moved TIOCREMOTE to Kernel.framework after MacOSX12.0.sdk.
89#define TIOCREMOTE 0x80047469
90'
91
92includes_DragonFly='
93#include <sys/types.h>
94#include <sys/event.h>
95#include <sys/select.h>
96#include <sys/socket.h>
97#include <sys/sockio.h>
98#include <sys/stat.h>
99#include <sys/sysctl.h>
100#include <sys/mman.h>
101#include <sys/mount.h>
102#include <sys/wait.h>
103#include <sys/ioctl.h>
104#include <net/bpf.h>
105#include <net/if.h>
106#include <net/if_clone.h>
107#include <net/if_types.h>
108#include <net/route.h>
109#include <netinet/in.h>
110#include <termios.h>
111#include <netinet/ip.h>
112#include <net/ip_mroute/ip_mroute.h>
113'
114
115includes_FreeBSD='
116#include <sys/capsicum.h>
117#include <sys/param.h>
118#include <sys/types.h>
119#include <sys/disk.h>
120#include <sys/event.h>
121#include <sys/sched.h>
122#include <sys/select.h>
123#include <sys/socket.h>
124#include <sys/un.h>
125#include <sys/sockio.h>
126#include <sys/stat.h>
127#include <sys/sysctl.h>
128#include <sys/mman.h>
129#include <sys/mount.h>
130#include <sys/wait.h>
131#include <sys/ioctl.h>
132#include <sys/ptrace.h>
133#include <net/bpf.h>
134#include <net/if.h>
135#include <net/if_types.h>
136#include <net/route.h>
137#include <netinet/in.h>
138#include <termios.h>
139#include <netinet/ip.h>
140#include <netinet/ip_mroute.h>
141#include <sys/extattr.h>
142
143#if __FreeBSD__ >= 10
144#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
145#undef SIOCAIFADDR
146#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
147#undef SIOCSIFPHYADDR
148#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
149#endif
150'
151
152includes_Linux='
153#define _LARGEFILE_SOURCE
154#define _LARGEFILE64_SOURCE
155#ifndef __LP64__
156#define _FILE_OFFSET_BITS 64
157#endif
158#define _GNU_SOURCE
159
160// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
161// these structures. We just include them copied from <bits/termios.h>.
162#if defined(__powerpc__)
163struct sgttyb {
164 char sg_ispeed;
165 char sg_ospeed;
166 char sg_erase;
167 char sg_kill;
168 short sg_flags;
169};
170
171struct tchars {
172 char t_intrc;
173 char t_quitc;
174 char t_startc;
175 char t_stopc;
176 char t_eofc;
177 char t_brkc;
178};
179
180struct ltchars {
181 char t_suspc;
182 char t_dsuspc;
183 char t_rprntc;
184 char t_flushc;
185 char t_werasc;
186 char t_lnextc;
187};
188#endif
189
190#include <bits/sockaddr.h>
191#include <sys/epoll.h>
192#include <sys/eventfd.h>
193#include <sys/inotify.h>
194#include <sys/ioctl.h>
195#include <sys/mman.h>
196#include <sys/mount.h>
197#include <sys/prctl.h>
198#include <sys/stat.h>
199#include <sys/types.h>
200#include <sys/time.h>
201#include <sys/select.h>
202#include <sys/signalfd.h>
203#include <sys/socket.h>
204#include <sys/timerfd.h>
205#include <sys/uio.h>
206#include <sys/xattr.h>
207#include <netinet/udp.h>
208#include <linux/audit.h>
209#include <linux/bpf.h>
210#include <linux/can.h>
211#include <linux/can/error.h>
212#include <linux/can/netlink.h>
213#include <linux/can/raw.h>
214#include <linux/capability.h>
215#include <linux/cryptouser.h>
216#include <linux/devlink.h>
217#include <linux/dm-ioctl.h>
218#include <linux/errqueue.h>
219#include <linux/ethtool_netlink.h>
220#include <linux/falloc.h>
221#include <linux/fanotify.h>
222#include <linux/fib_rules.h>
223#include <linux/filter.h>
224#include <linux/fs.h>
225#include <linux/fscrypt.h>
226#include <linux/fsverity.h>
227#include <linux/genetlink.h>
228#include <linux/hdreg.h>
229#include <linux/hidraw.h>
230#include <linux/if.h>
231#include <linux/if_addr.h>
232#include <linux/if_alg.h>
233#include <linux/if_arp.h>
234#include <linux/if_ether.h>
235#include <linux/if_ppp.h>
236#include <linux/if_tun.h>
237#include <linux/if_packet.h>
238#include <linux/if_xdp.h>
239#include <linux/input.h>
240#include <linux/kcm.h>
241#include <linux/kexec.h>
242#include <linux/keyctl.h>
243#include <linux/landlock.h>
244#include <linux/loop.h>
245#include <linux/lwtunnel.h>
246#include <linux/magic.h>
247#include <linux/memfd.h>
248#include <linux/module.h>
249#include <linux/mount.h>
250#include <linux/netfilter/nfnetlink.h>
251#include <linux/netfilter/nf_tables.h>
252#include <linux/netlink.h>
253#include <linux/net_namespace.h>
254#include <linux/nfc.h>
255#include <linux/nsfs.h>
256#include <linux/perf_event.h>
257#include <linux/pps.h>
258#include <linux/ptrace.h>
259#include <linux/random.h>
260#include <linux/reboot.h>
261#include <linux/rtc.h>
262#include <linux/rtnetlink.h>
263#include <linux/sched.h>
264#include <linux/seccomp.h>
265#include <linux/serial.h>
266#include <linux/sock_diag.h>
267#include <linux/sockios.h>
268#include <linux/taskstats.h>
269#include <linux/tipc.h>
270#include <linux/vm_sockets.h>
271#include <linux/wait.h>
272#include <linux/watchdog.h>
273#include <linux/wireguard.h>
274
275#include <mtd/ubi-user.h>
276#include <mtd/mtd-user.h>
277#include <net/route.h>
278
279#if defined(__sparc__)
280// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
281// definition in glibc. As only the error constants are needed here, include the
282// generic termibits.h (which is included by termbits.h on sparc).
283#include <asm-generic/termbits.h>
284#else
285#include <asm/termbits.h>
286#endif
287
288#ifndef PTRACE_GETREGS
289#define PTRACE_GETREGS 0xc
290#endif
291
292#ifndef PTRACE_SETREGS
293#define PTRACE_SETREGS 0xd
294#endif
295
296#ifdef SOL_BLUETOOTH
297// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
298// but it is already in bluetooth_linux.go
299#undef SOL_BLUETOOTH
300#endif
301
302// Certain constants are missing from the fs/crypto UAPI
303#define FS_KEY_DESC_PREFIX "fscrypt:"
304#define FS_KEY_DESC_PREFIX_SIZE 8
305#define FS_MAX_KEY_SIZE 64
306
307// The code generator produces -0x1 for (~0), but an unsigned value is necessary
308// for the tipc_subscr timeout __u32 field.
309#undef TIPC_WAIT_FOREVER
310#define TIPC_WAIT_FOREVER 0xffffffff
311
312// Copied from linux/netfilter/nf_nat.h
313// Including linux/netfilter/nf_nat.h here causes conflicts between linux/in.h
314// and netinet/in.h.
315#define NF_NAT_RANGE_MAP_IPS (1 << 0)
316#define NF_NAT_RANGE_PROTO_SPECIFIED (1 << 1)
317#define NF_NAT_RANGE_PROTO_RANDOM (1 << 2)
318#define NF_NAT_RANGE_PERSISTENT (1 << 3)
319#define NF_NAT_RANGE_PROTO_RANDOM_FULLY (1 << 4)
320#define NF_NAT_RANGE_PROTO_OFFSET (1 << 5)
321#define NF_NAT_RANGE_NETMAP (1 << 6)
322#define NF_NAT_RANGE_PROTO_RANDOM_ALL \
323 (NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PROTO_RANDOM_FULLY)
324#define NF_NAT_RANGE_MASK \
325 (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED | \
326 NF_NAT_RANGE_PROTO_RANDOM | NF_NAT_RANGE_PERSISTENT | \
327 NF_NAT_RANGE_PROTO_RANDOM_FULLY | NF_NAT_RANGE_PROTO_OFFSET | \
328 NF_NAT_RANGE_NETMAP)
329
330// Copied from linux/hid.h.
331// Keep in sync with the size of the referenced fields.
332#define _HIDIOCGRAWNAME_LEN 128 // sizeof_field(struct hid_device, name)
333#define _HIDIOCGRAWPHYS_LEN 64 // sizeof_field(struct hid_device, phys)
334#define _HIDIOCGRAWUNIQ_LEN 64 // sizeof_field(struct hid_device, uniq)
335
336#define _HIDIOCGRAWNAME HIDIOCGRAWNAME(_HIDIOCGRAWNAME_LEN)
337#define _HIDIOCGRAWPHYS HIDIOCGRAWPHYS(_HIDIOCGRAWPHYS_LEN)
338#define _HIDIOCGRAWUNIQ HIDIOCGRAWUNIQ(_HIDIOCGRAWUNIQ_LEN)
339
340'
341
342includes_NetBSD='
343#include <sys/types.h>
344#include <sys/param.h>
345#include <sys/event.h>
346#include <sys/extattr.h>
347#include <sys/mman.h>
348#include <sys/mount.h>
349#include <sys/sched.h>
350#include <sys/select.h>
351#include <sys/socket.h>
352#include <sys/sockio.h>
353#include <sys/sysctl.h>
354#include <sys/termios.h>
355#include <sys/ttycom.h>
356#include <sys/wait.h>
357#include <net/bpf.h>
358#include <net/if.h>
359#include <net/if_types.h>
360#include <net/route.h>
361#include <netinet/in.h>
362#include <netinet/in_systm.h>
363#include <netinet/ip.h>
364#include <netinet/ip_mroute.h>
365#include <netinet/if_ether.h>
366
367// Needed since <sys/param.h> refers to it...
368#define schedppq 1
369'
370
371includes_OpenBSD='
372#include <sys/types.h>
373#include <sys/param.h>
374#include <sys/event.h>
375#include <sys/mman.h>
376#include <sys/mount.h>
377#include <sys/select.h>
378#include <sys/sched.h>
379#include <sys/socket.h>
380#include <sys/sockio.h>
381#include <sys/stat.h>
382#include <sys/sysctl.h>
383#include <sys/termios.h>
384#include <sys/ttycom.h>
385#include <sys/unistd.h>
386#include <sys/wait.h>
387#include <net/bpf.h>
388#include <net/if.h>
389#include <net/if_types.h>
390#include <net/if_var.h>
391#include <net/route.h>
392#include <netinet/in.h>
393#include <netinet/in_systm.h>
394#include <netinet/ip.h>
395#include <netinet/ip_mroute.h>
396#include <netinet/if_ether.h>
397#include <net/if_bridge.h>
398
399// We keep some constants not supported in OpenBSD 5.5 and beyond for
400// the promise of compatibility.
401#define EMUL_ENABLED 0x1
402#define EMUL_NATIVE 0x2
403#define IPV6_FAITH 0x1d
404#define IPV6_OPTIONS 0x1
405#define IPV6_RTHDR_STRICT 0x1
406#define IPV6_SOCKOPT_RESERVED1 0x3
407#define SIOCGIFGENERIC 0xc020693a
408#define SIOCSIFGENERIC 0x80206939
409#define WALTSIG 0x4
410'
411
412includes_SunOS='
413#include <limits.h>
414#include <sys/types.h>
415#include <sys/select.h>
416#include <sys/socket.h>
417#include <sys/sockio.h>
418#include <sys/stat.h>
419#include <sys/stream.h>
420#include <sys/mman.h>
421#include <sys/wait.h>
422#include <sys/ioctl.h>
423#include <sys/mkdev.h>
424#include <net/bpf.h>
425#include <net/if.h>
426#include <net/if_arp.h>
427#include <net/if_types.h>
428#include <net/route.h>
429#include <netinet/icmp6.h>
430#include <netinet/in.h>
431#include <netinet/ip.h>
432#include <netinet/ip_mroute.h>
433#include <termios.h>
434'
435
436
437includes='
438#include <sys/types.h>
439#include <sys/file.h>
440#include <fcntl.h>
441#include <dirent.h>
442#include <sys/socket.h>
443#include <netinet/in.h>
444#include <netinet/ip.h>
445#include <netinet/ip6.h>
446#include <netinet/tcp.h>
447#include <errno.h>
448#include <sys/signal.h>
449#include <signal.h>
450#include <sys/resource.h>
451#include <time.h>
452'
453ccflags="$@"
454
455# Write go tool cgo -godefs input.
456(
457 echo package unix
458 echo
459 echo '/*'
460 indirect="includes_$(uname)"
461 echo "${!indirect} $includes"
462 echo '*/'
463 echo 'import "C"'
464 echo 'import "syscall"'
465 echo
466 echo 'const ('
467
468 # The gcc command line prints all the #defines
469 # it encounters while processing the input
470 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
471 awk '
472 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
473
474 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
475 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
476 $2 ~ /^(SCM_SRCRT)$/ {next}
477 $2 ~ /^(MAP_FAILED)$/ {next}
478 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
479
480 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
481 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
482
483 $2 !~ /^ECCAPBITS/ &&
484 $2 !~ /^ETH_/ &&
485 $2 !~ /^EPROC_/ &&
486 $2 !~ /^EQUIV_/ &&
487 $2 !~ /^EXPR_/ &&
488 $2 !~ /^EVIOC/ &&
489 $2 ~ /^E[A-Z0-9_]+$/ ||
490 $2 ~ /^B[0-9_]+$/ ||
491 $2 ~ /^(OLD|NEW)DEV$/ ||
492 $2 == "BOTHER" ||
493 $2 ~ /^CI?BAUD(EX)?$/ ||
494 $2 == "IBSHIFT" ||
495 $2 ~ /^V[A-Z0-9]+$/ ||
496 $2 ~ /^CS[A-Z0-9]/ ||
497 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
498 $2 ~ /^IGN/ ||
499 $2 ~ /^IX(ON|ANY|OFF)$/ ||
500 $2 ~ /^IN(LCR|PCK)$/ ||
501 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
502 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
503 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
504 $2 == "BRKINT" ||
505 $2 == "HUPCL" ||
506 $2 == "PENDIN" ||
507 $2 == "TOSTOP" ||
508 $2 == "XCASE" ||
509 $2 == "ALTWERASE" ||
510 $2 == "NOKERNINFO" ||
511 $2 == "NFDBITS" ||
512 $2 ~ /^PAR/ ||
513 $2 ~ /^SIG[^_]/ ||
514 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
515 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
516 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
517 $2 ~ /^O?XTABS$/ ||
518 $2 ~ /^TC[IO](ON|OFF)$/ ||
519 $2 ~ /^IN_/ ||
520 $2 ~ /^KCM/ ||
521 $2 ~ /^LANDLOCK_/ ||
522 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
523 $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
524 $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
525 $2 == "LOOP_CONFIGURE" ||
526 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|MCAST|EVFILT|NOTE|SHUT|PROT|MAP|MREMAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR|LOCAL|TCPOPT|UDP)_/ ||
527 $2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
528 $2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
529 $2 ~ /^RAW_PAYLOAD_/ ||
530 $2 ~ /^[US]F_/ ||
531 $2 ~ /^TP_STATUS_/ ||
532 $2 ~ /^FALLOC_/ ||
533 $2 ~ /^ICMPV?6?_(FILTER|SEC)/ ||
534 $2 == "SOMAXCONN" ||
535 $2 == "NAME_MAX" ||
536 $2 == "IFNAMSIZ" ||
537 $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
538 $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
539 $2 ~ /^HW_MACHINE$/ ||
540 $2 ~ /^SYSCTL_VERS/ ||
541 $2 !~ "MNT_BITS" &&
542 $2 ~ /^(MS|MNT|MOUNT|UMOUNT)_/ ||
543 $2 ~ /^NS_GET_/ ||
544 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
545 $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT|PIOD|TFD)_/ ||
546 $2 ~ /^KEXEC_/ ||
547 $2 ~ /^LINUX_REBOOT_CMD_/ ||
548 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
549 $2 ~ /^MODULE_INIT_/ ||
550 $2 !~ "NLA_TYPE_MASK" &&
551 $2 !~ /^RTC_VL_(ACCURACY|BACKUP|DATA)/ &&
552 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
553 $2 ~ /^SOCK_|SK_DIAG_|SKNLGRP_$/ ||
554 $2 ~ /^FIORDCHK$/ ||
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_MAX_DESCRIPTOR_SIZE" ||
629 $2 ~ /^_?HIDIOC/ ||
630 $2 ~ /^BUS_(USB|HIL|BLUETOOTH|VIRTUAL)$/ ||
631 $2 ~ /^MTD/ ||
632 $2 ~ /^OTP/ ||
633 $2 ~ /^MEM/ ||
634 $2 ~ /^WG/ ||
635 $2 ~ /^FIB_RULE_/ ||
636 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE|IOMIN$|IOOPT$|ALIGNOFF$|DISCARD|ROTATIONAL$|ZEROOUT$|GETDISKSEQ$)/ {printf("\t%s = C.%s\n", $2, $2)}
637 $2 ~ /^__WCOREFLAG$/ {next}
638 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
639
640 {next}
641 ' | sort
642
643 echo ')'
644) >_const.go
645
646# Pull out the error names for later.
647errors=$(
648 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
649 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
650 sort
651)
652
653# Pull out the signal names for later.
654signals=$(
655 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
656 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
657 grep -v 'SIGSTKSIZE\|SIGSTKSZ\|SIGRT\|SIGMAX64' |
658 sort
659)
660
661# Again, writing regexps to a file.
662echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
663 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
664 sort >_error.grep
665echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
666 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
667 grep -v 'SIGSTKSIZE\|SIGSTKSZ\|SIGRT\|SIGMAX64' |
668 sort >_signal.grep
669
670echo '// mkerrors.sh' "$@"
671echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
672echo
673echo "//go:build ${GOARCH} && ${GOOS}"
674echo
675go tool cgo -godefs -- "$@" _const.go >_error.out
676cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
677echo
678echo '// Errors'
679echo 'const ('
680cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
681echo ')'
682
683echo
684echo '// Signals'
685echo 'const ('
686cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
687echo ')'
688
689# Run C program to print error and syscall strings.
690(
691 echo -E "
692#include <stdio.h>
693#include <stdlib.h>
694#include <errno.h>
695#include <ctype.h>
696#include <string.h>
697#include <signal.h>
698
699#define nelem(x) (sizeof(x)/sizeof((x)[0]))
700
701enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
702
703struct tuple {
704 int num;
705 const char *name;
706};
707
708struct tuple errors[] = {
709"
710 for i in $errors
711 do
712 echo -E ' {'$i', "'$i'" },'
713 done
714
715 echo -E "
716};
717
718struct tuple signals[] = {
719"
720 for i in $signals
721 do
722 echo -E ' {'$i', "'$i'" },'
723 done
724
725 # Use -E because on some systems bash builtin interprets \n itself.
726 echo -E '
727};
728
729static int
730tuplecmp(const void *a, const void *b)
731{
732 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
733}
734
735int
736main(void)
737{
738 int i, e;
739 char buf[1024], *p;
740
741 printf("\n\n// Error table\n");
742 printf("var errorList = [...]struct {\n");
743 printf("\tnum syscall.Errno\n");
744 printf("\tname string\n");
745 printf("\tdesc string\n");
746 printf("} {\n");
747 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
748 for(i=0; i<nelem(errors); i++) {
749 e = errors[i].num;
750 if(i > 0 && errors[i-1].num == e)
751 continue;
752 strncpy(buf, strerror(e), sizeof(buf) - 1);
753 buf[sizeof(buf) - 1] = '\0';
754 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
755 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
756 buf[0] += a - A;
757 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
758 }
759 printf("}\n\n");
760
761 printf("\n\n// Signal table\n");
762 printf("var signalList = [...]struct {\n");
763 printf("\tnum syscall.Signal\n");
764 printf("\tname string\n");
765 printf("\tdesc string\n");
766 printf("} {\n");
767 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
768 for(i=0; i<nelem(signals); i++) {
769 e = signals[i].num;
770 if(i > 0 && signals[i-1].num == e)
771 continue;
772 strncpy(buf, strsignal(e), sizeof(buf) - 1);
773 buf[sizeof(buf) - 1] = '\0';
774 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
775 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
776 buf[0] += a - A;
777 // cut trailing : number.
778 p = strrchr(buf, ":"[0]);
779 if(p)
780 *p = '\0';
781 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
782 }
783 printf("}\n\n");
784
785 return 0;
786}
787
788'
789) >_errors.c
790
791$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out
View as plain text