blob: c90cd1c00ec7cf16ab262be6ccf45d9c40cfb0ae [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>
Ken Rockotc8443d72011-06-22 18:07:20 -040065#include <termios.h>
Russ Coxfd1add22009-11-01 11:13:27 -080066'
67
Devon H. O'Dell553be842009-11-14 15:29:09 -080068includes_FreeBSD='
Mikio Hara9c97af92011-02-11 14:34:00 -050069#include <sys/types.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080070#include <sys/event.h>
Mikio Hara400ea842011-02-01 08:46:21 -050071#include <sys/socket.h>
72#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050073#include <sys/sysctl.h>
74#include <sys/wait.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040075#include <sys/ioctl.h>
Mikio Hara5a59b9e2011-04-04 15:40:40 -040076#include <net/bpf.h>
Mikio Hara400ea842011-02-01 08:46:21 -050077#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040078#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050079#include <net/route.h>
Mikio Hara400ea842011-02-01 08:46:21 -050080#include <netinet/in.h>
Russ Coxdfacfd62011-06-27 11:02:32 -040081#include <termios.h>
Mikio Hara400ea842011-02-01 08:46:21 -050082#include <netinet/ip.h>
83#include <netinet/ip_mroute.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080084'
85
Russ Coxfd1add22009-11-01 11:13:27 -080086includes='
87#include <sys/types.h>
88#include <fcntl.h>
89#include <dirent.h>
90#include <sys/socket.h>
91#include <netinet/in.h>
Russ Cox8fbe8be2010-03-30 14:32:59 -070092#include <netinet/ip.h>
93#include <netinet/ip6.h>
Russ Coxfd1add22009-11-01 11:13:27 -080094#include <netinet/tcp.h>
95#include <errno.h>
96#include <sys/signal.h>
97#include <signal.h>
98'
Russ Cox602a4462009-06-01 22:14:57 -070099
Russ Cox7906e312010-04-29 23:34:06 -0700100ccflags=""
101next=false
102for i
103do
104 if $next; then
105 ccflags="$ccflags $i"
106 next=false
107 elif [ "$i" = "-f" ]; then
108 next=true
109 fi
110done
111
Russ Cox602a4462009-06-01 22:14:57 -0700112# Write godefs input.
113(
Russ Coxfd1add22009-11-01 11:13:27 -0800114 indirect="includes_$(uname)"
115 echo "${!indirect} $includes"
116 echo
Russ Cox602a4462009-06-01 22:14:57 -0700117 echo 'enum {'
Russ Coxfd1add22009-11-01 11:13:27 -0800118
119 # The gcc command line prints all the #defines
120 # it encounters while processing the input
Russ Cox7906e312010-04-29 23:34:06 -0700121 echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags |
Russ Coxfd1add22009-11-01 11:13:27 -0800122 awk '
123 $1 != "#define" || $2 ~ /\(/ {next}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500124
Russ Cox23bf4082010-05-03 11:11:01 -0700125 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
Russ Coxfd1add22009-11-01 11:13:27 -0800126 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
Albert Strasheimcf6c2122010-12-07 13:40:14 -0500127 $2 ~ /^(SCM_SRCRT)$/ {next}
128 $2 ~ /^(MAP_FAILED)$/ {next}
Russ Coxfd1add22009-11-01 11:13:27 -0800129
Mikio Hara6555cfc2010-12-09 13:55:59 -0500130 $2 !~ /^ETH_/ &&
Mikio Hara9c97af92011-02-11 14:34:00 -0500131 $2 !~ /^EPROC_/ &&
132 $2 !~ /^EQUIV_/ &&
133 $2 !~ /^EXPR_/ &&
Russ Coxfd1add22009-11-01 11:13:27 -0800134 $2 ~ /^E[A-Z0-9_]+$/ ||
135 $2 ~ /^SIG[^_]/ ||
Balazs Lecz4bfcfcf2010-09-27 11:44:26 -0400136 $2 ~ /^IN_/ ||
Albert Strasheimc45a08e2011-04-06 16:19:22 -0400137 $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 -0800138 $2 == "SOMAXCONN" ||
139 $2 == "NAME_MAX" ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500140 $2 == "IFNAMSIZ" ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500141 $2 == "CTL_NET" ||
142 $2 == "CTL_MAXNAME" ||
David Andersonad102e12011-03-09 05:58:47 -0800143 $2 ~ /^(MS|MNT)_/ ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500144 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Jeff Hodges0ce57a72011-06-14 12:56:46 -0400145 $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
David Anderson05660b72011-03-09 05:45:08 -0800146 $2 ~ /^LINUX_REBOOT_CMD_/ ||
147 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
Mikio Hara1d550bd2011-05-18 16:33:41 -0700148 $2 !~ "NLA_TYPE_MASK" &&
Mikio Hara10f14362011-06-20 14:51:34 -0400149 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|RTM|RTN|RTPROT|RTA|RTAX|RTNH|ARPHRD|ETH_P)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500150 $2 ~ /^SIOC/ ||
Ken Rockotc8443d72011-06-22 18:07:20 -0400151 $2 ~ /^TIOC/ ||
Mikio Hara12376c92011-05-26 20:02:03 -0400152 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400153 $2 ~ /^BIOC/ ||
Mikio Hara380f4ab2011-04-21 16:58:20 -0400154 $2 !~ /^(BPF_TIMEVAL)$/ &&
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400155 $2 ~ /^(BPF|DLT)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500156 $2 !~ "WMESGLEN" &&
Russ Coxfd1add22009-11-01 11:13:27 -0800157 $2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)}
Russ Cox23bf4082010-05-03 11:11:01 -0700158 $2 ~ /^__WCOREFLAG$/ {next}
Russ Coxfd1add22009-11-01 11:13:27 -0800159 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500160
Russ Coxfd1add22009-11-01 11:13:27 -0800161 {next}
162 ' | sort
163
Russ Cox602a4462009-06-01 22:14:57 -0700164 echo '};'
Russ Coxfd1add22009-11-01 11:13:27 -0800165) >_const.c
166
167# Pull out just the error names for later.
168errors=$(
Russ Cox7906e312010-04-29 23:34:06 -0700169 echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
Russ Cox0b126c12009-12-01 16:53:43 -0800170 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
171 sort
Russ Coxfd1add22009-11-01 11:13:27 -0800172)
Russ Cox602a4462009-06-01 22:14:57 -0700173
Rob Pikecd324982009-08-13 13:22:37 -0700174echo '// mkerrors.sh' "$@"
Russ Cox602a4462009-06-01 22:14:57 -0700175echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
176echo
Russ Cox23bf4082010-05-03 11:11:01 -0700177godefs -c $GCC "$@" -gsyscall "$@" _const.c
Russ Cox602a4462009-06-01 22:14:57 -0700178
179# Run C program to print error strings.
180(
Russ Cox9feee912009-08-24 11:03:23 -0700181 /bin/echo "
Russ Cox602a4462009-06-01 22:14:57 -0700182#include <stdio.h>
183#include <errno.h>
184#include <ctype.h>
185#include <string.h>
186
187#define nelem(x) (sizeof(x)/sizeof((x)[0]))
188
189enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
190
191int errors[] = {
192"
193 for i in $errors
194 do
Russ Cox9feee912009-08-24 11:03:23 -0700195 /bin/echo ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700196 done
197
Russ Cox9feee912009-08-24 11:03:23 -0700198 # Use /bin/echo to avoid builtin echo,
199 # which interprets \n itself
200 /bin/echo '
Russ Cox602a4462009-06-01 22:14:57 -0700201};
202
Russ Cox9a96fb32010-09-24 13:37:02 -0400203static int
204intcmp(const void *a, const void *b)
205{
206 return *(int*)a - *(int*)b;
207}
208
Russ Cox602a4462009-06-01 22:14:57 -0700209int
210main(void)
211{
212 int i, j, e;
213 char buf[1024];
214
215 printf("\n\n// Error table\n");
216 printf("var errors = [...]string {\n");
Russ Cox9a96fb32010-09-24 13:37:02 -0400217 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
Russ Cox602a4462009-06-01 22:14:57 -0700218 for(i=0; i<nelem(errors); i++) {
219 e = errors[i];
Russ Cox9a96fb32010-09-24 13:37:02 -0400220 if(i > 0 && errors[i-1] == e)
221 continue;
Russ Cox602a4462009-06-01 22:14:57 -0700222 strcpy(buf, strerror(e));
223 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
224 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
225 buf[0] += a - A;
226 printf("\t%d: \"%s\",\n", e, buf);
Russ Cox602a4462009-06-01 22:14:57 -0700227 }
228 printf("}\n\n");
Russ Coxfd1add22009-11-01 11:13:27 -0800229 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700230}
231
232'
233) >_errors.c
234
Russ Cox9a96fb32010-09-24 13:37:02 -0400235$GCC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.c