2009-11-14 16:29:09 -07:00
|
|
|
#!/usr/bin/env bash
|
2009-06-01 23:14:57 -06:00
|
|
|
# Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style
|
|
|
|
# license that can be found in the LICENSE file.
|
|
|
|
|
2009-11-01 12:13:27 -07:00
|
|
|
# Generate Go code listing errors and other #defined constant
|
|
|
|
# values (ENAMETOOLONG etc.), by asking the preprocessor
|
|
|
|
# about the definitions.
|
2009-06-01 23:14:57 -06:00
|
|
|
|
2009-12-01 17:53:43 -07:00
|
|
|
unset LANG
|
|
|
|
export LC_ALL=C
|
|
|
|
export LC_CTYPE=C
|
|
|
|
|
2010-08-23 04:25:14 -06:00
|
|
|
GCC=gcc
|
2009-10-01 07:55:01 -06:00
|
|
|
|
2009-11-01 12:13:27 -07:00
|
|
|
uname=$(uname)
|
2009-06-01 23:14:57 -06:00
|
|
|
|
2009-11-01 12:13:27 -07:00
|
|
|
includes_Linux='
|
|
|
|
#define _LARGEFILE_SOURCE
|
|
|
|
#define _LARGEFILE64_SOURCE
|
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/epoll.h>
|
2010-09-27 09:44:26 -06:00
|
|
|
#include <sys/inotify.h>
|
2010-11-01 14:42:33 -06:00
|
|
|
#include <sys/ioctl.h>
|
2010-12-07 11:40:14 -07:00
|
|
|
#include <sys/mman.h>
|
2011-03-09 06:58:47 -07:00
|
|
|
#include <sys/mount.h>
|
2010-12-07 11:40:14 -07:00
|
|
|
#include <sys/stat.h>
|
2009-11-01 12:13:27 -07:00
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/wait.h>
|
2010-12-09 11:55:59 -07:00
|
|
|
#include <linux/if_tun.h>
|
2011-03-09 06:45:08 -07:00
|
|
|
#include <linux/reboot.h>
|
2010-12-09 11:55:59 -07:00
|
|
|
#include <net/if.h>
|
2010-10-12 07:48:56 -06:00
|
|
|
#include <netpacket/packet.h>
|
2009-11-01 12:13:27 -07:00
|
|
|
'
|
|
|
|
|
|
|
|
includes_Darwin='
|
2011-01-18 12:02:41 -07:00
|
|
|
#define _DARWIN_C_SOURCE
|
2009-11-01 12:13:27 -07:00
|
|
|
#define KERNEL
|
|
|
|
#define _DARWIN_USE_64_BIT_INODE
|
2011-02-11 12:34:00 -07:00
|
|
|
#include <sys/types.h>
|
2009-11-01 12:13:27 -07:00
|
|
|
#include <sys/event.h>
|
2011-01-31 10:50:50 -07:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
2011-02-11 12:34:00 -07:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/wait.h>
|
2011-01-31 10:50:50 -07:00
|
|
|
#include <net/if.h>
|
2011-02-11 12:34:00 -07:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
2011-01-31 10:50:50 -07:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_mroute.h>
|
2009-11-01 12:13:27 -07:00
|
|
|
'
|
|
|
|
|
2009-11-14 16:29:09 -07:00
|
|
|
includes_FreeBSD='
|
2011-02-11 12:34:00 -07:00
|
|
|
#include <sys/types.h>
|
2009-11-14 16:29:09 -07:00
|
|
|
#include <sys/event.h>
|
2011-02-01 06:46:21 -07:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sockio.h>
|
2011-02-11 12:34:00 -07:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/wait.h>
|
2011-04-04 13:40:40 -06:00
|
|
|
#include <net/bpf.h>
|
2011-02-01 06:46:21 -07:00
|
|
|
#include <net/if.h>
|
2011-02-11 12:34:00 -07:00
|
|
|
#include <net/route.h>
|
2011-02-01 06:46:21 -07:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_mroute.h>
|
2009-11-14 16:29:09 -07:00
|
|
|
'
|
|
|
|
|
2009-11-01 12:13:27 -07:00
|
|
|
includes='
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2010-03-30 15:32:59 -06:00
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip6.h>
|
2009-11-01 12:13:27 -07:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <signal.h>
|
|
|
|
'
|
2009-06-01 23:14:57 -06:00
|
|
|
|
2010-04-30 00:34:06 -06:00
|
|
|
ccflags=""
|
|
|
|
next=false
|
|
|
|
for i
|
|
|
|
do
|
|
|
|
if $next; then
|
|
|
|
ccflags="$ccflags $i"
|
|
|
|
next=false
|
|
|
|
elif [ "$i" = "-f" ]; then
|
|
|
|
next=true
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
# Write godefs input.
|
|
|
|
(
|
2009-11-01 12:13:27 -07:00
|
|
|
indirect="includes_$(uname)"
|
|
|
|
echo "${!indirect} $includes"
|
|
|
|
echo
|
2009-06-01 23:14:57 -06:00
|
|
|
echo 'enum {'
|
2009-11-01 12:13:27 -07:00
|
|
|
|
|
|
|
# The gcc command line prints all the #defines
|
|
|
|
# it encounters while processing the input
|
2010-04-30 00:34:06 -06:00
|
|
|
echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags |
|
2009-11-01 12:13:27 -07:00
|
|
|
awk '
|
|
|
|
$1 != "#define" || $2 ~ /\(/ {next}
|
2010-12-09 11:55:59 -07:00
|
|
|
|
2010-05-03 12:11:01 -06:00
|
|
|
$2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
|
2009-11-01 12:13:27 -07:00
|
|
|
$2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
|
2010-12-07 11:40:14 -07:00
|
|
|
$2 ~ /^(SCM_SRCRT)$/ {next}
|
|
|
|
$2 ~ /^(MAP_FAILED)$/ {next}
|
2009-11-01 12:13:27 -07:00
|
|
|
|
2010-12-09 11:55:59 -07:00
|
|
|
$2 !~ /^ETH_/ &&
|
2011-02-11 12:34:00 -07:00
|
|
|
$2 !~ /^EPROC_/ &&
|
|
|
|
$2 !~ /^EQUIV_/ &&
|
|
|
|
$2 !~ /^EXPR_/ &&
|
2009-11-01 12:13:27 -07:00
|
|
|
$2 ~ /^E[A-Z0-9_]+$/ ||
|
|
|
|
$2 ~ /^SIG[^_]/ ||
|
2010-09-27 09:44:26 -06:00
|
|
|
$2 ~ /^IN_/ ||
|
2011-04-06 14:19:22 -06:00
|
|
|
$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|EVFILT|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV)_/ ||
|
2009-11-01 12:13:27 -07:00
|
|
|
$2 == "SOMAXCONN" ||
|
|
|
|
$2 == "NAME_MAX" ||
|
2010-12-09 11:55:59 -07:00
|
|
|
$2 == "IFNAMSIZ" ||
|
2011-02-11 12:34:00 -07:00
|
|
|
$2 == "CTL_NET" ||
|
|
|
|
$2 == "CTL_MAXNAME" ||
|
2011-03-09 06:58:47 -07:00
|
|
|
$2 ~ /^(MS|MNT)_/ ||
|
2010-12-09 11:55:59 -07:00
|
|
|
$2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
|
2009-11-01 12:13:27 -07:00
|
|
|
$2 ~ /^(O|F|FD|NAME|S|PTRACE)_/ ||
|
2011-03-09 06:45:08 -07:00
|
|
|
$2 ~ /^LINUX_REBOOT_CMD_/ ||
|
|
|
|
$2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
|
2011-02-11 12:34:00 -07:00
|
|
|
$2 ~ /^SIOC/ ||
|
2011-04-04 13:40:40 -06:00
|
|
|
$2 ~ /^(IFF|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
|
|
|
|
$2 ~ /^BIOC/ ||
|
|
|
|
$2 ~ /^(BPF|DLT)_/ ||
|
2011-02-11 12:34:00 -07:00
|
|
|
$2 !~ "WMESGLEN" &&
|
2009-11-01 12:13:27 -07:00
|
|
|
$2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)}
|
2010-05-03 12:11:01 -06:00
|
|
|
$2 ~ /^__WCOREFLAG$/ {next}
|
2009-11-01 12:13:27 -07:00
|
|
|
$2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)}
|
2010-12-09 11:55:59 -07:00
|
|
|
|
2009-11-01 12:13:27 -07:00
|
|
|
{next}
|
|
|
|
' | sort
|
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
echo '};'
|
2009-11-01 12:13:27 -07:00
|
|
|
) >_const.c
|
|
|
|
|
|
|
|
# Pull out just the error names for later.
|
|
|
|
errors=$(
|
2010-04-30 00:34:06 -06:00
|
|
|
echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
|
2009-12-01 17:53:43 -07:00
|
|
|
awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
|
|
|
|
sort
|
2009-11-01 12:13:27 -07:00
|
|
|
)
|
2009-06-01 23:14:57 -06:00
|
|
|
|
2009-08-13 14:22:37 -06:00
|
|
|
echo '// mkerrors.sh' "$@"
|
2009-06-01 23:14:57 -06:00
|
|
|
echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
|
|
|
|
echo
|
2010-05-03 12:11:01 -06:00
|
|
|
godefs -c $GCC "$@" -gsyscall "$@" _const.c
|
2009-06-01 23:14:57 -06:00
|
|
|
|
|
|
|
# Run C program to print error strings.
|
|
|
|
(
|
2009-08-24 12:03:23 -06:00
|
|
|
/bin/echo "
|
2009-06-01 23:14:57 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
|
|
|
|
|
|
|
|
enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
|
|
|
|
|
|
|
|
int errors[] = {
|
|
|
|
"
|
|
|
|
for i in $errors
|
|
|
|
do
|
2009-08-24 12:03:23 -06:00
|
|
|
/bin/echo ' '$i,
|
2009-06-01 23:14:57 -06:00
|
|
|
done
|
|
|
|
|
2009-08-24 12:03:23 -06:00
|
|
|
# Use /bin/echo to avoid builtin echo,
|
|
|
|
# which interprets \n itself
|
|
|
|
/bin/echo '
|
2009-06-01 23:14:57 -06:00
|
|
|
};
|
|
|
|
|
2010-09-24 11:37:02 -06:00
|
|
|
static int
|
|
|
|
intcmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return *(int*)a - *(int*)b;
|
|
|
|
}
|
|
|
|
|
2009-06-01 23:14:57 -06:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
int i, j, e;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
printf("\n\n// Error table\n");
|
|
|
|
printf("var errors = [...]string {\n");
|
2010-09-24 11:37:02 -06:00
|
|
|
qsort(errors, nelem(errors), sizeof errors[0], intcmp);
|
2009-06-01 23:14:57 -06:00
|
|
|
for(i=0; i<nelem(errors); i++) {
|
|
|
|
e = errors[i];
|
2010-09-24 11:37:02 -06:00
|
|
|
if(i > 0 && errors[i-1] == e)
|
|
|
|
continue;
|
2009-06-01 23:14:57 -06:00
|
|
|
strcpy(buf, strerror(e));
|
|
|
|
// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
|
|
|
|
if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
|
|
|
|
buf[0] += a - A;
|
|
|
|
printf("\t%d: \"%s\",\n", e, buf);
|
|
|
|
}
|
|
|
|
printf("}\n\n");
|
2009-11-01 12:13:27 -07:00
|
|
|
return 0;
|
2009-06-01 23:14:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
'
|
|
|
|
) >_errors.c
|
|
|
|
|
2010-09-24 11:37:02 -06:00
|
|
|
$GCC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.c
|