github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/syscall/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  CC=${CC:-gcc}
    15  
    16  if [[ "$GOOS" -eq "solaris" ]]; then
    17  	# Assumes GNU versions of utilities in PATH.
    18  	export PATH=/usr/gnu/bin:$PATH
    19  fi
    20  
    21  uname=$(uname)
    22  
    23  includes_AIX='
    24  #include <net/if.h>
    25  #include <net/netopt.h>
    26  #include <netinet/ip_mroute.h>
    27  #include <sys/mman.h>
    28  #include <sys/protosw.h>
    29  #include <sys/ptrace.h>
    30  #include <sys/stropts.h>
    31  '
    32  
    33  includes_Darwin='
    34  #define _DARWIN_C_SOURCE
    35  #define KERNEL
    36  #define _DARWIN_USE_64_BIT_INODE
    37  #include <sys/types.h>
    38  #include <sys/event.h>
    39  #include <sys/ptrace.h>
    40  #include <sys/socket.h>
    41  #include <sys/sockio.h>
    42  #include <sys/sysctl.h>
    43  #include <sys/mman.h>
    44  #include <sys/wait.h>
    45  #include <net/bpf.h>
    46  #include <net/if.h>
    47  #include <net/if_types.h>
    48  #include <net/route.h>
    49  #include <netinet/in.h>
    50  #include <netinet/ip.h>
    51  #include <netinet/ip_mroute.h>
    52  #include <termios.h>
    53  '
    54  
    55  includes_DragonFly='
    56  #include <sys/types.h>
    57  #include <sys/event.h>
    58  #include <sys/socket.h>
    59  #include <sys/sockio.h>
    60  #include <sys/sysctl.h>
    61  #include <sys/mman.h>
    62  #include <sys/wait.h>
    63  #include <sys/ioctl.h>
    64  #include <net/bpf.h>
    65  #include <net/if.h>
    66  #include <net/if_types.h>
    67  #include <net/route.h>
    68  #include <netinet/in.h>
    69  #include <termios.h>
    70  #include <netinet/ip.h>
    71  #include <net/ip_mroute/ip_mroute.h>
    72  '
    73  
    74  includes_FreeBSD='
    75  #include <sys/param.h>
    76  #include <sys/types.h>
    77  #include <sys/event.h>
    78  #include <sys/socket.h>
    79  #include <sys/sockio.h>
    80  #include <sys/sysctl.h>
    81  #include <sys/mman.h>
    82  #include <sys/wait.h>
    83  #include <sys/ioctl.h>
    84  #include <net/bpf.h>
    85  #include <net/if.h>
    86  #include <net/if_types.h>
    87  #include <net/route.h>
    88  #include <netinet/in.h>
    89  #include <termios.h>
    90  #include <netinet/ip.h>
    91  #include <netinet/ip_mroute.h>
    92  
    93  #if __FreeBSD__ >= 10
    94  #define IFT_CARP	0xf8	// IFT_CARP is deprecated in FreeBSD 10
    95  #undef SIOCAIFADDR
    96  #define SIOCAIFADDR	_IOW(105, 26, struct oifaliasreq)	// ifaliasreq contains if_data
    97  #undef SIOCSIFPHYADDR
    98  #define SIOCSIFPHYADDR	_IOW(105, 70, struct oifaliasreq)	// ifaliasreq contains if_data
    99  #endif
   100  '
   101  
   102  includes_Linux='
   103  #define _LARGEFILE_SOURCE
   104  #define _LARGEFILE64_SOURCE
   105  #ifndef __LP64__
   106  #define _FILE_OFFSET_BITS 64
   107  #endif
   108  #define _GNU_SOURCE
   109  
   110  #include <bits/sockaddr.h>
   111  #include <sys/epoll.h>
   112  #include <sys/inotify.h>
   113  #include <sys/ioctl.h>
   114  #include <sys/mman.h>
   115  #include <sys/mount.h>
   116  #include <sys/prctl.h>
   117  #include <sys/stat.h>
   118  #include <sys/types.h>
   119  #include <sys/time.h>
   120  #include <sys/socket.h>
   121  #include <linux/if.h>
   122  #include <linux/if_arp.h>
   123  #include <linux/if_ether.h>
   124  #include <linux/if_tun.h>
   125  #include <linux/if_packet.h>
   126  #include <linux/if_addr.h>
   127  #include <linux/filter.h>
   128  #include <linux/netlink.h>
   129  #include <linux/reboot.h>
   130  #include <linux/rtnetlink.h>
   131  #include <linux/ptrace.h>
   132  #include <linux/sched.h>
   133  #include <linux/wait.h>
   134  #include <linux/icmpv6.h>
   135  #include <net/route.h>
   136  #include <termios.h>
   137  
   138  #ifndef MSG_FASTOPEN
   139  #define MSG_FASTOPEN    0x20000000
   140  #endif
   141  
   142  #ifndef PTRACE_GETREGS
   143  #define PTRACE_GETREGS	0xc
   144  #endif
   145  
   146  #ifndef PTRACE_SETREGS
   147  #define PTRACE_SETREGS	0xd
   148  #endif
   149  '
   150  
   151  includes_NetBSD='
   152  #include <sys/types.h>
   153  #include <sys/param.h>
   154  #include <sys/event.h>
   155  #include <sys/mman.h>
   156  #include <sys/socket.h>
   157  #include <sys/sockio.h>
   158  #include <sys/sysctl.h>
   159  #include <sys/termios.h>
   160  #include <sys/ttycom.h>
   161  #include <sys/wait.h>
   162  #include <net/bpf.h>
   163  #include <net/if.h>
   164  #include <net/if_types.h>
   165  #include <net/route.h>
   166  #include <netinet/in.h>
   167  #include <netinet/in_systm.h>
   168  #include <netinet/ip.h>
   169  #include <netinet/ip_mroute.h>
   170  #include <netinet/if_ether.h>
   171  
   172  // Needed since <sys/param.h> refers to it...
   173  #define schedppq 1
   174  '
   175  
   176  includes_OpenBSD='
   177  #include <sys/types.h>
   178  #include <sys/param.h>
   179  #include <sys/event.h>
   180  #include <sys/mman.h>
   181  #include <sys/socket.h>
   182  #include <sys/sockio.h>
   183  #include <sys/sysctl.h>
   184  #include <sys/termios.h>
   185  #include <sys/ttycom.h>
   186  #include <sys/wait.h>
   187  #include <net/bpf.h>
   188  #include <net/if.h>
   189  #include <net/if_types.h>
   190  #include <net/if_var.h>
   191  #include <net/route.h>
   192  #include <netinet/in.h>
   193  #include <netinet/in_systm.h>
   194  #include <netinet/ip.h>
   195  #include <netinet/ip_mroute.h>
   196  #include <netinet/if_ether.h>
   197  #include <net/if_bridge.h>
   198  
   199  // We keep some constants not supported in OpenBSD 5.5 and beyond for
   200  // the promise of compatibility.
   201  #define EMUL_ENABLED		0x1
   202  #define EMUL_NATIVE		0x2
   203  #define IPV6_FAITH		0x1d
   204  #define IPV6_OPTIONS		0x1
   205  #define IPV6_RTHDR_STRICT	0x1
   206  #define IPV6_SOCKOPT_RESERVED1	0x3
   207  #define SIOCGIFGENERIC		0xc020693a
   208  #define SIOCSIFGENERIC		0x80206939
   209  #define WALTSIG			0x4
   210  '
   211  
   212  includes_SunOS='
   213  #include <limits.h>
   214  #include <sys/types.h>
   215  #include <sys/socket.h>
   216  #include <sys/sockio.h>
   217  #include <sys/mman.h>
   218  #include <sys/wait.h>
   219  #include <sys/ioctl.h>
   220  #include <net/bpf.h>
   221  #include <net/if.h>
   222  #include <net/if_arp.h>
   223  #include <net/if_types.h>
   224  #include <net/route.h>
   225  #include <netinet/in.h>
   226  #include <termios.h>
   227  #include <netinet/ip.h>
   228  #include <netinet/ip_mroute.h>
   229  '
   230  
   231  includes='
   232  #include <sys/types.h>
   233  #include <sys/file.h>
   234  #include <fcntl.h>
   235  #include <dirent.h>
   236  #include <sys/socket.h>
   237  #include <netinet/in.h>
   238  #include <netinet/ip.h>
   239  #include <netinet/ip6.h>
   240  #include <netinet/tcp.h>
   241  #include <errno.h>
   242  #include <sys/signal.h>
   243  #include <signal.h>
   244  #include <sys/resource.h>
   245  '
   246  
   247  ccflags="$@"
   248  
   249  # Write go tool cgo -godefs input.
   250  (
   251  	echo package syscall
   252  	echo
   253  	echo '/*'
   254  	indirect="includes_$(uname)"
   255  	echo "${!indirect} $includes"
   256  	echo '*/'
   257  	echo 'import "C"'
   258  	echo
   259  	echo 'const ('
   260  
   261  	# The gcc command line prints all the #defines
   262  	# it encounters while processing the input
   263  	echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
   264  	awk '
   265  		$1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
   266  
   267  		$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
   268  		$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
   269  		$2 ~ /^(SCM_SRCRT)$/ {next}
   270  		$2 ~ /^(MAP_FAILED)$/ {next}
   271  		$2 ~ /^ELF_.*$/ {next}	# <asm/elf.h> contains ELF_ARCH, etc.
   272  
   273  		$2 !~ /^ETH_/ &&
   274  		$2 !~ /^EPROC_/ &&
   275  		$2 !~ /^EQUIV_/ &&
   276  		$2 !~ /^EXPR_/ &&
   277  		$2 ~ /^E[A-Z0-9_]+$/ ||
   278  		$2 ~ /^B[0-9_]+$/ ||
   279  		$2 ~ /^V[A-Z0-9]+$/ ||
   280  		$2 ~ /^CS[A-Z0-9]/ ||
   281  		$2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
   282  		$2 ~ /^IGN/ ||
   283  		$2 ~ /^IX(ON|ANY|OFF)$/ ||
   284  		$2 ~ /^IN(LCR|PCK)$/ ||
   285  		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
   286  		$2 ~ /^C(LOCAL|READ)$/ ||
   287  		$2 == "BRKINT" ||
   288  		$2 == "HUPCL" ||
   289  		$2 == "PENDIN" ||
   290  		$2 == "TOSTOP" ||
   291  		$2 ~ /^PAR/ ||
   292  		$2 ~ /^SIG[^_]/ ||
   293  		$2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
   294  		$2 ~ /^IN_/ ||
   295  		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
   296  		$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
   297  		$2 == "ICMPV6_FILTER" ||
   298  		$2 == "SOMAXCONN" ||
   299  		$2 == "NAME_MAX" ||
   300  		$2 == "IFNAMSIZ" ||
   301  		$2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
   302  		$2 ~ /^SYSCTL_VERS/ ||
   303  		$2 ~ /^(MS|MNT)_/ ||
   304  		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
   305  		$2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
   306  		$2 ~ /^LINUX_REBOOT_CMD_/ ||
   307  		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
   308  		$2 !~ "NLA_TYPE_MASK" &&
   309  		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
   310  		$2 ~ /^SIOC/ ||
   311  		$2 ~ /^TIOC/ ||
   312  		$2 !~ "RTF_BITS" &&
   313  		$2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
   314  		$2 ~ /^BIOC/ ||
   315  		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
   316  		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
   317  		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
   318  		$2 ~ /^CLONE_[A-Z_]+/ ||
   319  		$2 !~ /^(BPF_TIMEVAL)$/ &&
   320  		$2 ~ /^(BPF|DLT)_/ ||
   321  		$2 !~ "WMESGLEN" &&
   322  		$2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
   323  		$2 ~ /^__WCOREFLAG$/ {next}
   324  		$2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
   325  
   326  		{next}
   327  	' | sort
   328  
   329  	echo ')'
   330  ) >_const.go
   331  
   332  # Pull out the error names for later.
   333  errors=$(
   334  	echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
   335  	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
   336  	sort
   337  )
   338  
   339  # Pull out the signal names for later.
   340  signals=$(
   341  	echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
   342  	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
   343  	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
   344  	sort
   345  )
   346  
   347  # Again, writing regexps to a file.
   348  echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
   349  	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
   350  	sort >_error.grep
   351  echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
   352  	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
   353  	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
   354  	sort >_signal.grep
   355  
   356  echo '// mkerrors.sh' "$@"
   357  echo '// Code generated by the command above; DO NOT EDIT.'
   358  echo
   359  go tool cgo -godefs -- "$@" _const.go >_error.out
   360  cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
   361  echo
   362  echo '// Errors'
   363  echo 'const ('
   364  cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= Errno(\1)/'
   365  echo ')'
   366  
   367  echo
   368  echo '// Signals'
   369  echo 'const ('
   370  cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= Signal(\1)/'
   371  echo ')'
   372  
   373  # Run C program to print error and syscall strings.
   374  (
   375  	echo -E "
   376  #include <stdio.h>
   377  #include <stdlib.h>
   378  #include <errno.h>
   379  #include <ctype.h>
   380  #include <string.h>
   381  #include <signal.h>
   382  
   383  #define nelem(x) (sizeof(x)/sizeof((x)[0]))
   384  
   385  enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
   386  
   387  int errors[] = {
   388  "
   389  	for i in $errors
   390  	do
   391  		echo -E '	'$i,
   392  	done
   393  
   394  	echo -E "
   395  };
   396  
   397  int signals[] = {
   398  "
   399  	for i in $signals
   400  	do
   401  		echo -E '	'$i,
   402  	done
   403  
   404  	# Use -E because on some systems bash builtin interprets \n itself.
   405  	echo -E '
   406  };
   407  
   408  static int
   409  intcmp(const void *a, const void *b)
   410  {
   411  	return *(int*)a - *(int*)b;
   412  }
   413  
   414  int
   415  main(void)
   416  {
   417  	int i, j, e;
   418  	char buf[1024], *p;
   419  
   420  	printf("\n\n// Error table\n");
   421  	printf("var errors = [...]string {\n");
   422  	qsort(errors, nelem(errors), sizeof errors[0], intcmp);
   423  	for(i=0; i<nelem(errors); i++) {
   424  		e = errors[i];
   425  		if(i > 0 && errors[i-1] == e)
   426  			continue;
   427  		strcpy(buf, strerror(e));
   428  		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
   429  		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
   430  			buf[0] += a - A;
   431  		printf("\t%d: \"%s\",\n", e, buf);
   432  	}
   433  	printf("}\n\n");
   434  	
   435  	printf("\n\n// Signal table\n");
   436  	printf("var signals = [...]string {\n");
   437  	qsort(signals, nelem(signals), sizeof signals[0], intcmp);
   438  	for(i=0; i<nelem(signals); i++) {
   439  		e = signals[i];
   440  		if(i > 0 && signals[i-1] == e)
   441  			continue;
   442  		strcpy(buf, strsignal(e));
   443  		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
   444  		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
   445  			buf[0] += a - A;
   446  		// cut trailing : number.
   447  		p = strrchr(buf, ":"[0]);
   448  		if(p)
   449  			*p = '\0';
   450  		printf("\t%d: \"%s\",\n", e, buf);
   451  	}
   452  	printf("}\n\n");
   453  
   454  	return 0;
   455  }
   456  
   457  '
   458  ) >_errors.c
   459  
   460  $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out