blob: 51ac3356ffee1284e2f424d53ebd1a433d4c4bcf [file] [log] [blame]
Devon H. O'Dell553be842009-11-14 15:29:09 -08001#!/usr/bin/env bash
Russ Cox602a4462009-06-01 22:14:57 -07002# 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
Russ Coxfd1add22009-11-01 11:13:27 -08006# Generate Go code listing errors and other #defined constant
7# values (ENAMETOOLONG etc.), by asking the preprocessor
8# about the definitions.
Russ Cox602a4462009-06-01 22:14:57 -07009
Russ Cox0b126c12009-12-01 16:53:43 -080010unset LANG
11export LC_ALL=C
12export LC_CTYPE=C
13
Kai Backman534dbc72010-08-23 13:25:14 +030014GCC=gcc
Kai Backmane5c884f2009-10-01 06:55:01 -070015
Russ Coxfd1add22009-11-01 11:13:27 -080016uname=$(uname)
Russ Cox602a4462009-06-01 22:14:57 -070017
Russ Coxfd1add22009-11-01 11:13:27 -080018includes_Linux='
19#define _LARGEFILE_SOURCE
20#define _LARGEFILE64_SOURCE
21#define _FILE_OFFSET_BITS 64
22#define _GNU_SOURCE
23
Mikio Hara1d550bd2011-05-18 16:33:41 -070024#include <bits/sockaddr.h>
Russ Coxfd1add22009-11-01 11:13:27 -080025#include <sys/epoll.h>
Balazs Lecz4bfcfcf2010-09-27 11:44:26 -040026#include <sys/inotify.h>
Albert Strasheim6442f382010-11-01 16:42:33 -040027#include <sys/ioctl.h>
Albert Strasheimcf6c2122010-12-07 13:40:14 -050028#include <sys/mman.h>
David Andersonad102e12011-03-09 05:58:47 -080029#include <sys/mount.h>
Albert Strasheimcf6c2122010-12-07 13:40:14 -050030#include <sys/stat.h>
Mikio Hara1d550bd2011-05-18 16:33:41 -070031#include <sys/types.h>
32#include <linux/if_addr.h>
Mikio Hara10f14362011-06-20 14:51:34 -040033#include <linux/if_ether.h>
Mikio Hara1d550bd2011-05-18 16:33:41 -070034#include <linux/if_tun.h>
Mikio Hara10f14362011-06-20 14:51:34 -040035#include <linux/filter.h>
Mikio Hara1d550bd2011-05-18 16:33:41 -070036#include <linux/netlink.h>
37#include <linux/reboot.h>
38#include <linux/rtnetlink.h>
Russ Coxfd1add22009-11-01 11:13:27 -080039#include <linux/ptrace.h>
40#include <linux/wait.h>
Mikio Hara6555cfc2010-12-09 13:55:59 -050041#include <net/if.h>
Mikio Hara4d118832011-05-26 17:04:58 -040042#include <net/if_arp.h>
Mikio Harab390c4b9d62010-10-12 09:48:56 -040043#include <netpacket/packet.h>
Russ Coxfd1add22009-11-01 11:13:27 -080044'
45
46includes_Darwin='
Russ Coxacd858e2011-01-18 14:02:41 -050047#define _DARWIN_C_SOURCE
Russ Coxfd1add22009-11-01 11:13:27 -080048#define KERNEL
49#define _DARWIN_USE_64_BIT_INODE
Mikio Hara9c97af92011-02-11 14:34:00 -050050#include <sys/types.h>
Russ Coxfd1add22009-11-01 11:13:27 -080051#include <sys/event.h>
Jeff Hodges0ce57a72011-06-14 12:56:46 -040052#include <sys/ptrace.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050053#include <sys/socket.h>
54#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050055#include <sys/sysctl.h>
Russ Cox48ae1f22011-04-06 17:52:02 -040056#include <sys/mman.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050057#include <sys/wait.h>
Mikio Hara380f4ab2011-04-21 16:58:20 -040058#include <net/bpf.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050059#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040060#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050061#include <net/route.h>
62#include <netinet/in.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050063#include <netinet/ip.h>
64#include <netinet/ip_mroute.h>
Russ Coxfd1add22009-11-01 11:13:27 -080065'
66
Devon H. O'Dell553be842009-11-14 15:29:09 -080067includes_FreeBSD='
Mikio Hara9c97af92011-02-11 14:34:00 -050068#include <sys/types.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080069#include <sys/event.h>
Mikio Hara400ea842011-02-01 08:46:21 -050070#include <sys/socket.h>
71#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050072#include <sys/sysctl.h>
73#include <sys/wait.h>
Mikio Hara5a59b9e2011-04-04 15:40:40 -040074#include <net/bpf.h>
Mikio Hara400ea842011-02-01 08:46:21 -050075#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040076#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050077#include <net/route.h>
Mikio Hara400ea842011-02-01 08:46:21 -050078#include <netinet/in.h>
79#include <netinet/ip.h>
80#include <netinet/ip_mroute.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080081'
82
Russ Coxfd1add22009-11-01 11:13:27 -080083includes='
84#include <sys/types.h>
85#include <fcntl.h>
86#include <dirent.h>
87#include <sys/socket.h>
88#include <netinet/in.h>
Russ Cox8fbe8be2010-03-30 14:32:59 -070089#include <netinet/ip.h>
90#include <netinet/ip6.h>
Russ Coxfd1add22009-11-01 11:13:27 -080091#include <netinet/tcp.h>
92#include <errno.h>
93#include <sys/signal.h>
94#include <signal.h>
95'
Russ Cox602a4462009-06-01 22:14:57 -070096
Russ Cox7906e312010-04-29 23:34:06 -070097ccflags=""
98next=false
99for i
100do
101 if $next; then
102 ccflags="$ccflags $i"
103 next=false
104 elif [ "$i" = "-f" ]; then
105 next=true
106 fi
107done
108
Russ Cox602a4462009-06-01 22:14:57 -0700109# Write godefs input.
110(
Russ Coxfd1add22009-11-01 11:13:27 -0800111 indirect="includes_$(uname)"
112 echo "${!indirect} $includes"
113 echo
Russ Cox602a4462009-06-01 22:14:57 -0700114 echo 'enum {'
Russ Coxfd1add22009-11-01 11:13:27 -0800115
116 # The gcc command line prints all the #defines
117 # it encounters while processing the input
Russ Cox7906e312010-04-29 23:34:06 -0700118 echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags |
Russ Coxfd1add22009-11-01 11:13:27 -0800119 awk '
120 $1 != "#define" || $2 ~ /\(/ {next}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500121
Russ Cox23bf4082010-05-03 11:11:01 -0700122 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
Russ Coxfd1add22009-11-01 11:13:27 -0800123 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
Albert Strasheimcf6c2122010-12-07 13:40:14 -0500124 $2 ~ /^(SCM_SRCRT)$/ {next}
125 $2 ~ /^(MAP_FAILED)$/ {next}
Russ Coxfd1add22009-11-01 11:13:27 -0800126
Mikio Hara6555cfc2010-12-09 13:55:59 -0500127 $2 !~ /^ETH_/ &&
Mikio Hara9c97af92011-02-11 14:34:00 -0500128 $2 !~ /^EPROC_/ &&
129 $2 !~ /^EQUIV_/ &&
130 $2 !~ /^EXPR_/ &&
Russ Coxfd1add22009-11-01 11:13:27 -0800131 $2 ~ /^E[A-Z0-9_]+$/ ||
132 $2 ~ /^SIG[^_]/ ||
Balazs Lecz4bfcfcf2010-09-27 11:44:26 -0400133 $2 ~ /^IN_/ ||
Albert Strasheimc45a08e2011-04-06 16:19:22 -0400134 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|EVFILT|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV)_/ ||
Russ Coxfd1add22009-11-01 11:13:27 -0800135 $2 == "SOMAXCONN" ||
136 $2 == "NAME_MAX" ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500137 $2 == "IFNAMSIZ" ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500138 $2 == "CTL_NET" ||
139 $2 == "CTL_MAXNAME" ||
David Andersonad102e12011-03-09 05:58:47 -0800140 $2 ~ /^(MS|MNT)_/ ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500141 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Jeff Hodges0ce57a72011-06-14 12:56:46 -0400142 $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
David Anderson05660b72011-03-09 05:45:08 -0800143 $2 ~ /^LINUX_REBOOT_CMD_/ ||
144 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
Mikio Hara1d550bd2011-05-18 16:33:41 -0700145 $2 !~ "NLA_TYPE_MASK" &&
Mikio Hara10f14362011-06-20 14:51:34 -0400146 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|RTM|RTN|RTPROT|RTA|RTAX|RTNH|ARPHRD|ETH_P)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500147 $2 ~ /^SIOC/ ||
Mikio Hara12376c92011-05-26 20:02:03 -0400148 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400149 $2 ~ /^BIOC/ ||
Mikio Hara380f4ab2011-04-21 16:58:20 -0400150 $2 !~ /^(BPF_TIMEVAL)$/ &&
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400151 $2 ~ /^(BPF|DLT)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500152 $2 !~ "WMESGLEN" &&
Russ Coxfd1add22009-11-01 11:13:27 -0800153 $2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)}
Russ Cox23bf4082010-05-03 11:11:01 -0700154 $2 ~ /^__WCOREFLAG$/ {next}
Russ Coxfd1add22009-11-01 11:13:27 -0800155 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500156
Russ Coxfd1add22009-11-01 11:13:27 -0800157 {next}
158 ' | sort
159
Russ Cox602a4462009-06-01 22:14:57 -0700160 echo '};'
Russ Coxfd1add22009-11-01 11:13:27 -0800161) >_const.c
162
163# Pull out just the error names for later.
164errors=$(
Russ Cox7906e312010-04-29 23:34:06 -0700165 echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
Russ Cox0b126c12009-12-01 16:53:43 -0800166 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
167 sort
Russ Coxfd1add22009-11-01 11:13:27 -0800168)
Russ Cox602a4462009-06-01 22:14:57 -0700169
Rob Pikecd324982009-08-13 13:22:37 -0700170echo '// mkerrors.sh' "$@"
Russ Cox602a4462009-06-01 22:14:57 -0700171echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
172echo
Russ Cox23bf4082010-05-03 11:11:01 -0700173godefs -c $GCC "$@" -gsyscall "$@" _const.c
Russ Cox602a4462009-06-01 22:14:57 -0700174
175# Run C program to print error strings.
176(
Russ Cox9feee912009-08-24 11:03:23 -0700177 /bin/echo "
Russ Cox602a4462009-06-01 22:14:57 -0700178#include <stdio.h>
179#include <errno.h>
180#include <ctype.h>
181#include <string.h>
182
183#define nelem(x) (sizeof(x)/sizeof((x)[0]))
184
185enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
186
187int errors[] = {
188"
189 for i in $errors
190 do
Russ Cox9feee912009-08-24 11:03:23 -0700191 /bin/echo ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700192 done
193
Russ Cox9feee912009-08-24 11:03:23 -0700194 # Use /bin/echo to avoid builtin echo,
195 # which interprets \n itself
196 /bin/echo '
Russ Cox602a4462009-06-01 22:14:57 -0700197};
198
Russ Cox9a96fb32010-09-24 13:37:02 -0400199static int
200intcmp(const void *a, const void *b)
201{
202 return *(int*)a - *(int*)b;
203}
204
Russ Cox602a4462009-06-01 22:14:57 -0700205int
206main(void)
207{
208 int i, j, e;
209 char buf[1024];
210
211 printf("\n\n// Error table\n");
212 printf("var errors = [...]string {\n");
Russ Cox9a96fb32010-09-24 13:37:02 -0400213 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
Russ Cox602a4462009-06-01 22:14:57 -0700214 for(i=0; i<nelem(errors); i++) {
215 e = errors[i];
Russ Cox9a96fb32010-09-24 13:37:02 -0400216 if(i > 0 && errors[i-1] == e)
217 continue;
Russ Cox602a4462009-06-01 22:14:57 -0700218 strcpy(buf, strerror(e));
219 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
220 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
221 buf[0] += a - A;
222 printf("\t%d: \"%s\",\n", e, buf);
Russ Cox602a4462009-06-01 22:14:57 -0700223 }
224 printf("}\n\n");
Russ Coxfd1add22009-11-01 11:13:27 -0800225 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700226}
227
228'
229) >_errors.c
230
Russ Cox9a96fb32010-09-24 13:37:02 -0400231$GCC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.c