mkerrors.sh raw

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