github.com/binkynet/BinkyNet@v1.12.1-0.20240421190447-da4e34c20be0/proto_vendor/golang.org/x/sys/unix/mkerrors.sh (about)

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