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