blob: 96bf2a9197879647d43af23d2c97fc0cc7e50d1a [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001#!/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>
Matteo Scandolof9d43412021-01-12 11:11:34 -080047#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070048#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
58#define _DARWIN_USE_64_BIT_INODE
59#include <stdint.h>
60#include <sys/attr.h>
61#include <sys/types.h>
62#include <sys/event.h>
63#include <sys/ptrace.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -080064#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070065#include <sys/socket.h>
66#include <sys/sockio.h>
67#include <sys/sysctl.h>
68#include <sys/mman.h>
69#include <sys/mount.h>
70#include <sys/utsname.h>
71#include <sys/wait.h>
72#include <sys/xattr.h>
73#include <net/bpf.h>
74#include <net/if.h>
75#include <net/if_types.h>
76#include <net/route.h>
77#include <netinet/in.h>
78#include <netinet/ip.h>
79#include <termios.h>
80'
81
82includes_DragonFly='
83#include <sys/types.h>
84#include <sys/event.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -080085#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070086#include <sys/socket.h>
87#include <sys/sockio.h>
88#include <sys/stat.h>
89#include <sys/sysctl.h>
90#include <sys/mman.h>
91#include <sys/mount.h>
92#include <sys/wait.h>
93#include <sys/ioctl.h>
94#include <net/bpf.h>
95#include <net/if.h>
96#include <net/if_types.h>
97#include <net/route.h>
98#include <netinet/in.h>
99#include <termios.h>
100#include <netinet/ip.h>
101#include <net/ip_mroute/ip_mroute.h>
102'
103
104includes_FreeBSD='
105#include <sys/capsicum.h>
106#include <sys/param.h>
107#include <sys/types.h>
108#include <sys/event.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800109#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700110#include <sys/socket.h>
111#include <sys/sockio.h>
112#include <sys/stat.h>
113#include <sys/sysctl.h>
114#include <sys/mman.h>
115#include <sys/mount.h>
116#include <sys/wait.h>
117#include <sys/ioctl.h>
118#include <net/bpf.h>
119#include <net/if.h>
120#include <net/if_types.h>
121#include <net/route.h>
122#include <netinet/in.h>
123#include <termios.h>
124#include <netinet/ip.h>
125#include <netinet/ip_mroute.h>
126#include <sys/extattr.h>
127
128#if __FreeBSD__ >= 10
129#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
130#undef SIOCAIFADDR
131#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
132#undef SIOCSIFPHYADDR
133#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
134#endif
135'
136
137includes_Linux='
138#define _LARGEFILE_SOURCE
139#define _LARGEFILE64_SOURCE
140#ifndef __LP64__
141#define _FILE_OFFSET_BITS 64
142#endif
143#define _GNU_SOURCE
144
145// <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
146// these structures. We just include them copied from <bits/termios.h>.
147#if defined(__powerpc__)
148struct sgttyb {
149 char sg_ispeed;
150 char sg_ospeed;
151 char sg_erase;
152 char sg_kill;
153 short sg_flags;
154};
155
156struct tchars {
157 char t_intrc;
158 char t_quitc;
159 char t_startc;
160 char t_stopc;
161 char t_eofc;
162 char t_brkc;
163};
164
165struct ltchars {
166 char t_suspc;
167 char t_dsuspc;
168 char t_rprntc;
169 char t_flushc;
170 char t_werasc;
171 char t_lnextc;
172};
173#endif
174
175#include <bits/sockaddr.h>
176#include <sys/epoll.h>
177#include <sys/eventfd.h>
178#include <sys/inotify.h>
179#include <sys/ioctl.h>
180#include <sys/mman.h>
181#include <sys/mount.h>
182#include <sys/prctl.h>
183#include <sys/stat.h>
184#include <sys/types.h>
185#include <sys/time.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800186#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700187#include <sys/signalfd.h>
188#include <sys/socket.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800189#include <sys/uio.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700190#include <sys/xattr.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800191#include <linux/bpf.h>
192#include <linux/can.h>
193#include <linux/capability.h>
194#include <linux/cryptouser.h>
195#include <linux/devlink.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700196#include <linux/errqueue.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800197#include <linux/falloc.h>
198#include <linux/fanotify.h>
199#include <linux/filter.h>
200#include <linux/fs.h>
201#include <linux/fscrypt.h>
202#include <linux/genetlink.h>
203#include <linux/hdreg.h>
204#include <linux/icmpv6.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700205#include <linux/if.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800206#include <linux/if_addr.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700207#include <linux/if_alg.h>
208#include <linux/if_arp.h>
209#include <linux/if_ether.h>
210#include <linux/if_ppp.h>
211#include <linux/if_tun.h>
212#include <linux/if_packet.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800213#include <linux/if_xdp.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700214#include <linux/kexec.h>
215#include <linux/keyctl.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800216#include <linux/loop.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700217#include <linux/magic.h>
218#include <linux/memfd.h>
219#include <linux/module.h>
220#include <linux/netfilter/nfnetlink.h>
221#include <linux/netlink.h>
222#include <linux/net_namespace.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800223#include <linux/nsfs.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700224#include <linux/perf_event.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800225#include <linux/ptrace.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700226#include <linux/random.h>
227#include <linux/reboot.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800228#include <linux/rtc.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700229#include <linux/rtnetlink.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700230#include <linux/sched.h>
231#include <linux/seccomp.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700232#include <linux/serial.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800233#include <linux/sockios.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700234#include <linux/taskstats.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800235#include <linux/tipc.h>
236#include <linux/vm_sockets.h>
237#include <linux/wait.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700238#include <linux/watchdog.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800239
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700240#include <mtd/ubi-user.h>
241#include <net/route.h>
242
243#if defined(__sparc__)
244// On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
245// definition in glibc. As only the error constants are needed here, include the
246// generic termibits.h (which is included by termbits.h on sparc).
247#include <asm-generic/termbits.h>
248#else
249#include <asm/termbits.h>
250#endif
251
252#ifndef MSG_FASTOPEN
253#define MSG_FASTOPEN 0x20000000
254#endif
255
256#ifndef PTRACE_GETREGS
257#define PTRACE_GETREGS 0xc
258#endif
259
260#ifndef PTRACE_SETREGS
261#define PTRACE_SETREGS 0xd
262#endif
263
264#ifndef SOL_NETLINK
265#define SOL_NETLINK 270
266#endif
267
268#ifdef SOL_BLUETOOTH
269// SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
270// but it is already in bluetooth_linux.go
271#undef SOL_BLUETOOTH
272#endif
273
274// Certain constants are missing from the fs/crypto UAPI
275#define FS_KEY_DESC_PREFIX "fscrypt:"
276#define FS_KEY_DESC_PREFIX_SIZE 8
277#define FS_MAX_KEY_SIZE 64
Matteo Scandolof9d43412021-01-12 11:11:34 -0800278
279// The code generator produces -0x1 for (~0), but an unsigned value is necessary
280// for the tipc_subscr timeout __u32 field.
281#undef TIPC_WAIT_FOREVER
282#define TIPC_WAIT_FOREVER 0xffffffff
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700283'
284
285includes_NetBSD='
286#include <sys/types.h>
287#include <sys/param.h>
288#include <sys/event.h>
289#include <sys/extattr.h>
290#include <sys/mman.h>
291#include <sys/mount.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800292#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700293#include <sys/socket.h>
294#include <sys/sockio.h>
295#include <sys/sysctl.h>
296#include <sys/termios.h>
297#include <sys/ttycom.h>
298#include <sys/wait.h>
299#include <net/bpf.h>
300#include <net/if.h>
301#include <net/if_types.h>
302#include <net/route.h>
303#include <netinet/in.h>
304#include <netinet/in_systm.h>
305#include <netinet/ip.h>
306#include <netinet/ip_mroute.h>
307#include <netinet/if_ether.h>
308
309// Needed since <sys/param.h> refers to it...
310#define schedppq 1
311'
312
313includes_OpenBSD='
314#include <sys/types.h>
315#include <sys/param.h>
316#include <sys/event.h>
317#include <sys/mman.h>
318#include <sys/mount.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800319#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700320#include <sys/socket.h>
321#include <sys/sockio.h>
322#include <sys/stat.h>
323#include <sys/sysctl.h>
324#include <sys/termios.h>
325#include <sys/ttycom.h>
326#include <sys/unistd.h>
327#include <sys/wait.h>
328#include <net/bpf.h>
329#include <net/if.h>
330#include <net/if_types.h>
331#include <net/if_var.h>
332#include <net/route.h>
333#include <netinet/in.h>
334#include <netinet/in_systm.h>
335#include <netinet/ip.h>
336#include <netinet/ip_mroute.h>
337#include <netinet/if_ether.h>
338#include <net/if_bridge.h>
339
340// We keep some constants not supported in OpenBSD 5.5 and beyond for
341// the promise of compatibility.
342#define EMUL_ENABLED 0x1
343#define EMUL_NATIVE 0x2
344#define IPV6_FAITH 0x1d
345#define IPV6_OPTIONS 0x1
346#define IPV6_RTHDR_STRICT 0x1
347#define IPV6_SOCKOPT_RESERVED1 0x3
348#define SIOCGIFGENERIC 0xc020693a
349#define SIOCSIFGENERIC 0x80206939
350#define WALTSIG 0x4
351'
352
353includes_SunOS='
354#include <limits.h>
355#include <sys/types.h>
Matteo Scandolof9d43412021-01-12 11:11:34 -0800356#include <sys/select.h>
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700357#include <sys/socket.h>
358#include <sys/sockio.h>
359#include <sys/stat.h>
360#include <sys/mman.h>
361#include <sys/wait.h>
362#include <sys/ioctl.h>
363#include <sys/mkdev.h>
364#include <net/bpf.h>
365#include <net/if.h>
366#include <net/if_arp.h>
367#include <net/if_types.h>
368#include <net/route.h>
369#include <netinet/in.h>
370#include <termios.h>
371#include <netinet/ip.h>
372#include <netinet/ip_mroute.h>
373'
374
375
376includes='
377#include <sys/types.h>
378#include <sys/file.h>
379#include <fcntl.h>
380#include <dirent.h>
381#include <sys/socket.h>
382#include <netinet/in.h>
383#include <netinet/ip.h>
384#include <netinet/ip6.h>
385#include <netinet/tcp.h>
386#include <errno.h>
387#include <sys/signal.h>
388#include <signal.h>
389#include <sys/resource.h>
390#include <time.h>
391'
392ccflags="$@"
393
394# Write go tool cgo -godefs input.
395(
396 echo package unix
397 echo
398 echo '/*'
399 indirect="includes_$(uname)"
400 echo "${!indirect} $includes"
401 echo '*/'
402 echo 'import "C"'
403 echo 'import "syscall"'
404 echo
405 echo 'const ('
406
407 # The gcc command line prints all the #defines
408 # it encounters while processing the input
409 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
410 awk '
411 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
412
413 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
414 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
415 $2 ~ /^(SCM_SRCRT)$/ {next}
416 $2 ~ /^(MAP_FAILED)$/ {next}
417 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
418
419 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
420 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
421
422 $2 !~ /^ECCAPBITS/ &&
423 $2 !~ /^ETH_/ &&
424 $2 !~ /^EPROC_/ &&
425 $2 !~ /^EQUIV_/ &&
426 $2 !~ /^EXPR_/ &&
427 $2 ~ /^E[A-Z0-9_]+$/ ||
428 $2 ~ /^B[0-9_]+$/ ||
429 $2 ~ /^(OLD|NEW)DEV$/ ||
430 $2 == "BOTHER" ||
431 $2 ~ /^CI?BAUD(EX)?$/ ||
432 $2 == "IBSHIFT" ||
433 $2 ~ /^V[A-Z0-9]+$/ ||
434 $2 ~ /^CS[A-Z0-9]/ ||
435 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
436 $2 ~ /^IGN/ ||
437 $2 ~ /^IX(ON|ANY|OFF)$/ ||
438 $2 ~ /^IN(LCR|PCK)$/ ||
439 $2 !~ "X86_CR3_PCID_NOFLUSH" &&
440 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
441 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
442 $2 == "BRKINT" ||
443 $2 == "HUPCL" ||
444 $2 == "PENDIN" ||
445 $2 == "TOSTOP" ||
446 $2 == "XCASE" ||
447 $2 == "ALTWERASE" ||
448 $2 == "NOKERNINFO" ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800449 $2 == "NFDBITS" ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700450 $2 ~ /^PAR/ ||
451 $2 ~ /^SIG[^_]/ ||
452 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
453 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
454 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
455 $2 ~ /^O?XTABS$/ ||
456 $2 ~ /^TC[IO](ON|OFF)$/ ||
457 $2 ~ /^IN_/ ||
458 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800459 $2 ~ /^LO_(KEY|NAME)_SIZE$/ ||
460 $2 ~ /^LOOP_(CLR|CTL|GET|SET)_/ ||
461 $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)_/ ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700462 $2 ~ /^TP_STATUS_/ ||
463 $2 ~ /^FALLOC_/ ||
464 $2 == "ICMPV6_FILTER" ||
465 $2 == "SOMAXCONN" ||
466 $2 == "NAME_MAX" ||
467 $2 == "IFNAMSIZ" ||
468 $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
469 $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
470 $2 ~ /^HW_MACHINE$/ ||
471 $2 ~ /^SYSCTL_VERS/ ||
472 $2 !~ "MNT_BITS" &&
473 $2 ~ /^(MS|MNT|UMOUNT)_/ ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800474 $2 ~ /^NS_GET_/ ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700475 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
476 $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT)_/ ||
477 $2 ~ /^KEXEC_/ ||
478 $2 ~ /^LINUX_REBOOT_CMD_/ ||
479 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
480 $2 ~ /^MODULE_INIT_/ ||
481 $2 !~ "NLA_TYPE_MASK" &&
482 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
483 $2 ~ /^SIOC/ ||
484 $2 ~ /^TIOC/ ||
485 $2 ~ /^TCGET/ ||
486 $2 ~ /^TCSET/ ||
487 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
488 $2 !~ "RTF_BITS" &&
Matteo Scandolof9d43412021-01-12 11:11:34 -0800489 $2 ~ /^(IFF|IFT|NET_RT|RTM(GRP)?|RTF|RTV|RTA|RTAX)_/ ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700490 $2 ~ /^BIOC/ ||
491 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
492 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
493 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
494 $2 ~ /^CLONE_[A-Z_]+/ ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800495 $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700496 $2 ~ /^(BPF|DLT)_/ ||
497 $2 ~ /^(CLOCK|TIMER)_/ ||
498 $2 ~ /^CAN_/ ||
499 $2 ~ /^CAP_/ ||
500 $2 ~ /^ALG_/ ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800501 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE)/ ||
502 $2 ~ /^FS_IOC_.*ENCRYPTION/ ||
503 $2 ~ /^FSCRYPT_/ ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700504 $2 ~ /^GRND_/ ||
505 $2 ~ /^RND/ ||
506 $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
507 $2 ~ /^KEYCTL_/ ||
508 $2 ~ /^PERF_EVENT_IOC_/ ||
509 $2 ~ /^SECCOMP_MODE_/ ||
510 $2 ~ /^SPLICE_/ ||
511 $2 ~ /^SYNC_FILE_RANGE_/ ||
512 $2 !~ /^AUDIT_RECORD_MAGIC/ &&
513 $2 !~ /IOC_MAGIC/ &&
514 $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
515 $2 ~ /^(VM|VMADDR)_/ ||
516 $2 ~ /^IOCTL_VM_SOCKETS_/ ||
517 $2 ~ /^(TASKSTATS|TS)_/ ||
518 $2 ~ /^CGROUPSTATS_/ ||
519 $2 ~ /^GENL_/ ||
520 $2 ~ /^STATX_/ ||
521 $2 ~ /^RENAME/ ||
522 $2 ~ /^UBI_IOC[A-Z]/ ||
523 $2 ~ /^UTIME_/ ||
524 $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
525 $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
526 $2 ~ /^FSOPT_/ ||
527 $2 ~ /^WDIOC_/ ||
528 $2 ~ /^NFN/ ||
529 $2 ~ /^XDP_/ ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800530 $2 ~ /^RWF_/ ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700531 $2 ~ /^(HDIO|WIN|SMART)_/ ||
Matteo Scandolof9d43412021-01-12 11:11:34 -0800532 $2 ~ /^CRYPTO_/ ||
533 $2 ~ /^TIPC_/ ||
534 $2 ~ /^DEVLINK_/ ||
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700535 $2 !~ "WMESGLEN" &&
536 $2 ~ /^W[A-Z0-9]+$/ ||
537 $2 ~/^PPPIOC/ ||
538 $2 ~ /^FAN_|FANOTIFY_/ ||
539 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
540 $2 ~ /^__WCOREFLAG$/ {next}
541 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
542
543 {next}
544 ' | sort
545
546 echo ')'
547) >_const.go
548
549# Pull out the error names for later.
550errors=$(
551 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
552 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
553 sort
554)
555
556# Pull out the signal names for later.
557signals=$(
558 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
559 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
560 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
561 sort
562)
563
564# Again, writing regexps to a file.
565echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
566 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
567 sort >_error.grep
568echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
569 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
570 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
571 sort >_signal.grep
572
573echo '// mkerrors.sh' "$@"
574echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
575echo
576echo "// +build ${GOARCH},${GOOS}"
577echo
578go tool cgo -godefs -- "$@" _const.go >_error.out
579cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
580echo
581echo '// Errors'
582echo 'const ('
583cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
584echo ')'
585
586echo
587echo '// Signals'
588echo 'const ('
589cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
590echo ')'
591
592# Run C program to print error and syscall strings.
593(
594 echo -E "
595#include <stdio.h>
596#include <stdlib.h>
597#include <errno.h>
598#include <ctype.h>
599#include <string.h>
600#include <signal.h>
601
602#define nelem(x) (sizeof(x)/sizeof((x)[0]))
603
604enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
605
606struct tuple {
607 int num;
608 const char *name;
609};
610
611struct tuple errors[] = {
612"
613 for i in $errors
614 do
615 echo -E ' {'$i', "'$i'" },'
616 done
617
618 echo -E "
619};
620
621struct tuple signals[] = {
622"
623 for i in $signals
624 do
625 echo -E ' {'$i', "'$i'" },'
626 done
627
628 # Use -E because on some systems bash builtin interprets \n itself.
629 echo -E '
630};
631
632static int
633tuplecmp(const void *a, const void *b)
634{
635 return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
636}
637
638int
639main(void)
640{
641 int i, e;
642 char buf[1024], *p;
643
644 printf("\n\n// Error table\n");
645 printf("var errorList = [...]struct {\n");
646 printf("\tnum syscall.Errno\n");
647 printf("\tname string\n");
648 printf("\tdesc string\n");
649 printf("} {\n");
650 qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
651 for(i=0; i<nelem(errors); i++) {
652 e = errors[i].num;
653 if(i > 0 && errors[i-1].num == e)
654 continue;
655 strcpy(buf, strerror(e));
656 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
657 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
658 buf[0] += a - A;
659 printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
660 }
661 printf("}\n\n");
662
663 printf("\n\n// Signal table\n");
664 printf("var signalList = [...]struct {\n");
665 printf("\tnum syscall.Signal\n");
666 printf("\tname string\n");
667 printf("\tdesc string\n");
668 printf("} {\n");
669 qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
670 for(i=0; i<nelem(signals); i++) {
671 e = signals[i].num;
672 if(i > 0 && signals[i-1].num == e)
673 continue;
674 strcpy(buf, strsignal(e));
675 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
676 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
677 buf[0] += a - A;
678 // cut trailing : number.
679 p = strrchr(buf, ":"[0]);
680 if(p)
681 *p = '\0';
682 printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
683 }
684 printf("}\n\n");
685
686 return 0;
687}
688
689'
690) >_errors.c
691
692$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out