github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/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  GCC=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/types.h>
    61  #include <sys/event.h>
    62  #include <sys/socket.h>
    63  #include <sys/sockio.h>
    64  #include <sys/sysctl.h>
    65  #include <sys/mman.h>
    66  #include <sys/wait.h>
    67  #include <sys/ioctl.h>
    68  #include <net/bpf.h>
    69  #include <net/if.h>
    70  #include <net/if_types.h>
    71  #include <net/route.h>
    72  #include <netinet/in.h>
    73  #include <termios.h>
    74  #include <netinet/ip.h>
    75  #include <netinet/ip_mroute.h>
    76  '
    77  
    78  includes_Linux='
    79  #define _LARGEFILE_SOURCE
    80  #define _LARGEFILE64_SOURCE
    81  #define _FILE_OFFSET_BITS 64
    82  #define _GNU_SOURCE
    83  
    84  #include <bits/sockaddr.h>
    85  #include <sys/epoll.h>
    86  #include <sys/inotify.h>
    87  #include <sys/ioctl.h>
    88  #include <sys/mman.h>
    89  #include <sys/mount.h>
    90  #include <sys/prctl.h>
    91  #include <sys/stat.h>
    92  #include <sys/types.h>
    93  #include <sys/time.h>
    94  #include <sys/socket.h>
    95  #include <linux/if_addr.h>
    96  #include <linux/if_ether.h>
    97  #include <linux/if_tun.h>
    98  #include <linux/filter.h>
    99  #include <linux/netlink.h>
   100  #include <linux/reboot.h>
   101  #include <linux/rtnetlink.h>
   102  #include <linux/ptrace.h>
   103  #include <linux/sched.h>
   104  #include <linux/wait.h>
   105  #include <linux/icmpv6.h>
   106  #include <net/if.h>
   107  #include <net/if_arp.h>
   108  #include <net/route.h>
   109  #include <netpacket/packet.h>
   110  #include <termios.h>
   111  
   112  #ifndef MSG_FASTOPEN
   113  #define MSG_FASTOPEN    0x20000000
   114  #endif
   115  '
   116  
   117  includes_NetBSD='
   118  #include <sys/types.h>
   119  #include <sys/param.h>
   120  #include <sys/event.h>
   121  #include <sys/socket.h>
   122  #include <sys/sockio.h>
   123  #include <sys/sysctl.h>
   124  #include <sys/termios.h>
   125  #include <sys/ttycom.h>
   126  #include <sys/wait.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 <netinet/in_systm.h>
   133  #include <netinet/ip.h>
   134  #include <netinet/ip_mroute.h>
   135  #include <netinet/if_ether.h>
   136  
   137  // Needed since <sys/param.h> refers to it...
   138  #define schedppq 1
   139  '
   140  
   141  includes_OpenBSD='
   142  #include <sys/types.h>
   143  #include <sys/param.h>
   144  #include <sys/event.h>
   145  #include <sys/socket.h>
   146  #include <sys/sockio.h>
   147  #include <sys/sysctl.h>
   148  #include <sys/termios.h>
   149  #include <sys/ttycom.h>
   150  #include <sys/wait.h>
   151  #include <net/bpf.h>
   152  #include <net/if.h>
   153  #include <net/if_types.h>
   154  #include <net/route.h>
   155  #include <netinet/in.h>
   156  #include <netinet/in_systm.h>
   157  #include <netinet/ip.h>
   158  #include <netinet/ip_mroute.h>
   159  #include <netinet/if_ether.h>
   160  #include <net/if_bridge.h>
   161  '
   162  
   163  includes='
   164  #include <sys/types.h>
   165  #include <sys/file.h>
   166  #include <fcntl.h>
   167  #include <dirent.h>
   168  #include <sys/socket.h>
   169  #include <netinet/in.h>
   170  #include <netinet/ip.h>
   171  #include <netinet/ip6.h>
   172  #include <netinet/tcp.h>
   173  #include <errno.h>
   174  #include <sys/signal.h>
   175  #include <signal.h>
   176  #include <sys/resource.h>
   177  '
   178  
   179  ccflags="$@"
   180  
   181  # Write go tool cgo -godefs input.
   182  (
   183  	echo package syscall
   184  	echo
   185  	echo '/*'
   186  	indirect="includes_$(uname)"
   187  	echo "${!indirect} $includes"
   188  	echo '*/'
   189  	echo 'import "C"'
   190  	echo
   191  	echo 'const ('
   192  
   193  	# The gcc command line prints all the #defines
   194  	# it encounters while processing the input
   195  	echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags |
   196  	awk '
   197  		$1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
   198  
   199  		$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
   200  		$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
   201  		$2 ~ /^(SCM_SRCRT)$/ {next}
   202  		$2 ~ /^(MAP_FAILED)$/ {next}
   203  
   204  		$2 !~ /^ETH_/ &&
   205  		$2 !~ /^EPROC_/ &&
   206  		$2 !~ /^EQUIV_/ &&
   207  		$2 !~ /^EXPR_/ &&
   208  		$2 ~ /^E[A-Z0-9_]+$/ ||
   209  		$2 ~ /^B[0-9_]+$/ ||
   210  		$2 ~ /^V[A-Z0-9]+$/ ||
   211  		$2 ~ /^CS[A-Z0-9]/ ||
   212  		$2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
   213  		$2 ~ /^IGN/ ||
   214  		$2 ~ /^IX(ON|ANY|OFF)$/ ||
   215  		$2 ~ /^IN(LCR|PCK)$/ ||
   216  		$2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
   217  		$2 ~ /^C(LOCAL|READ)$/ ||
   218  		$2 == "BRKINT" ||
   219  		$2 == "HUPCL" ||
   220  		$2 == "PENDIN" ||
   221  		$2 == "TOSTOP" ||
   222  		$2 ~ /^PAR/ ||
   223  		$2 ~ /^SIG[^_]/ ||
   224  		$2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
   225  		$2 ~ /^IN_/ ||
   226  		$2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
   227  		$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
   228  		$2 == "ICMPV6_FILTER" ||
   229  		$2 == "SOMAXCONN" ||
   230  		$2 == "NAME_MAX" ||
   231  		$2 == "IFNAMSIZ" ||
   232  		$2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
   233  		$2 ~ /^SYSCTL_VERS/ ||
   234  		$2 ~ /^(MS|MNT)_/ ||
   235  		$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
   236  		$2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
   237  		$2 ~ /^LINUX_REBOOT_CMD_/ ||
   238  		$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
   239  		$2 !~ "NLA_TYPE_MASK" &&
   240  		$2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
   241  		$2 ~ /^SIOC/ ||
   242  		$2 ~ /^TIOC/ ||
   243  		$2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
   244  		$2 ~ /^BIOC/ ||
   245  		$2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
   246  		$2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
   247  		$2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
   248  		$2 ~ /^CLONE_[A-Z_]+/ ||
   249  		$2 !~ /^(BPF_TIMEVAL)$/ &&
   250  		$2 ~ /^(BPF|DLT)_/ ||
   251  		$2 !~ "WMESGLEN" &&
   252  		$2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
   253  		$2 ~ /^__WCOREFLAG$/ {next}
   254  		$2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
   255  
   256  		{next}
   257  	' | sort
   258  
   259  	echo ')'
   260  ) >_const.go
   261  
   262  # Pull out the error names for later.
   263  errors=$(
   264  	echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
   265  	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
   266  	sort
   267  )
   268  
   269  # Pull out the signal names for later.
   270  signals=$(
   271  	echo '#include <signal.h>' | $GCC -x c - -E -dM $ccflags |
   272  	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
   273  	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
   274  	sort
   275  )
   276  
   277  # Again, writing regexps to a file.
   278  echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
   279  	awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
   280  	sort >_error.grep
   281  echo '#include <signal.h>' | $GCC -x c - -E -dM $ccflags |
   282  	awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
   283  	egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
   284  	sort >_signal.grep
   285  
   286  echo '// mkerrors.sh' "$@"
   287  echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
   288  echo
   289  go tool cgo -godefs -- "$@" _const.go >_error.out
   290  cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
   291  echo
   292  echo '// Errors'
   293  echo 'const ('
   294  cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= Errno(\1)/'
   295  echo ')'
   296  
   297  echo
   298  echo '// Signals'
   299  echo 'const ('
   300  cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= Signal(\1)/'
   301  echo ')'
   302  
   303  # Run C program to print error and syscall strings.
   304  (
   305  	/bin/echo "
   306  #include <stdio.h>
   307  #include <errno.h>
   308  #include <ctype.h>
   309  #include <string.h>
   310  #include <signal.h>
   311  
   312  #define nelem(x) (sizeof(x)/sizeof((x)[0]))
   313  
   314  enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
   315  
   316  int errors[] = {
   317  "
   318  	for i in $errors
   319  	do
   320  		/bin/echo '	'$i,
   321  	done
   322  
   323  	/bin/echo "
   324  };
   325  
   326  int signals[] = {
   327  "
   328  	for i in $signals
   329  	do
   330  		/bin/echo '	'$i,
   331  	done
   332  
   333  	# Use /bin/echo to avoid builtin echo,
   334  	# which interprets \n itself
   335  	/bin/echo '
   336  };
   337  
   338  static int
   339  intcmp(const void *a, const void *b)
   340  {
   341  	return *(int*)a - *(int*)b;
   342  }
   343  
   344  int
   345  main(void)
   346  {
   347  	int i, j, e;
   348  	char buf[1024], *p;
   349  
   350  	printf("\n\n// Error table\n");
   351  	printf("var errors = [...]string {\n");
   352  	qsort(errors, nelem(errors), sizeof errors[0], intcmp);
   353  	for(i=0; i<nelem(errors); i++) {
   354  		e = errors[i];
   355  		if(i > 0 && errors[i-1] == e)
   356  			continue;
   357  		strcpy(buf, strerror(e));
   358  		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
   359  		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
   360  			buf[0] += a - A;
   361  		printf("\t%d: \"%s\",\n", e, buf);
   362  	}
   363  	printf("}\n\n");
   364  	
   365  	printf("\n\n// Signal table\n");
   366  	printf("var signals = [...]string {\n");
   367  	qsort(signals, nelem(signals), sizeof signals[0], intcmp);
   368  	for(i=0; i<nelem(signals); i++) {
   369  		e = signals[i];
   370  		if(i > 0 && signals[i-1] == e)
   371  			continue;
   372  		strcpy(buf, strsignal(e));
   373  		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
   374  		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
   375  			buf[0] += a - A;
   376  		// cut trailing : number.
   377  		p = strrchr(buf, ":"[0]);
   378  		if(p)
   379  			*p = '\0';
   380  		printf("\t%d: \"%s\",\n", e, buf);
   381  	}
   382  	printf("}\n\n");
   383  
   384  	return 0;
   385  }
   386  
   387  '
   388  ) >_errors.c
   389  
   390  $GCC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out