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