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