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