blob: 3ae11aa14114cf5a9a79c4c3783ed3c474763cf6 [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 Hara94093202011-09-08 13:59:34 -040043#include <net/route.h>
Mikio Harab390c4b9d62010-10-12 09:48:56 -040044#include <netpacket/packet.h>
Russ Coxfd1add22009-11-01 11:13:27 -080045'
46
47includes_Darwin='
Russ Coxacd858e2011-01-18 14:02:41 -050048#define _DARWIN_C_SOURCE
Russ Coxfd1add22009-11-01 11:13:27 -080049#define KERNEL
50#define _DARWIN_USE_64_BIT_INODE
Mikio Hara9c97af92011-02-11 14:34:00 -050051#include <sys/types.h>
Russ Coxfd1add22009-11-01 11:13:27 -080052#include <sys/event.h>
Jeff Hodges0ce57a72011-06-14 12:56:46 -040053#include <sys/ptrace.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050054#include <sys/socket.h>
55#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050056#include <sys/sysctl.h>
Russ Cox48ae1f22011-04-06 17:52:02 -040057#include <sys/mman.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050058#include <sys/wait.h>
Mikio Hara380f4ab2011-04-21 16:58:20 -040059#include <net/bpf.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050060#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040061#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050062#include <net/route.h>
63#include <netinet/in.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050064#include <netinet/ip.h>
65#include <netinet/ip_mroute.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040066#include <termios.h>
Russ Coxfd1add22009-11-01 11:13:27 -080067'
68
Devon H. O'Dell553be842009-11-14 15:29:09 -080069includes_FreeBSD='
Mikio Hara9c97af92011-02-11 14:34:00 -050070#include <sys/types.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080071#include <sys/event.h>
Mikio Hara400ea842011-02-01 08:46:21 -050072#include <sys/socket.h>
73#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050074#include <sys/sysctl.h>
75#include <sys/wait.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040076#include <sys/ioctl.h>
Mikio Hara5a59b9e2011-04-04 15:40:40 -040077#include <net/bpf.h>
Mikio Hara400ea842011-02-01 08:46:21 -050078#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040079#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050080#include <net/route.h>
Mikio Hara400ea842011-02-01 08:46:21 -050081#include <netinet/in.h>
Russ Coxdfacfd62011-06-27 11:02:32 -040082#include <termios.h>
Mikio Hara400ea842011-02-01 08:46:21 -050083#include <netinet/ip.h>
84#include <netinet/ip_mroute.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080085'
86
Joel Singbf2d4032011-08-22 23:24:32 -040087includes_OpenBSD='
88#include <sys/types.h>
89#include <sys/param.h>
90#include <sys/event.h>
91#include <sys/socket.h>
92#include <sys/sockio.h>
93#include <sys/sysctl.h>
94#include <sys/termios.h>
95#include <sys/ttycom.h>
96#include <sys/wait.h>
97#include <net/bpf.h>
98#include <net/if.h>
99#include <net/if_types.h>
100#include <net/route.h>
101#include <netinet/in.h>
102#include <netinet/in_systm.h>
103#include <netinet/ip.h>
104#include <netinet/ip_mroute.h>
105#include <netinet/if_ether.h>
106#include <net/if_bridge.h>
107'
108
Russ Coxfd1add22009-11-01 11:13:27 -0800109includes='
110#include <sys/types.h>
111#include <fcntl.h>
112#include <dirent.h>
113#include <sys/socket.h>
114#include <netinet/in.h>
Russ Cox8fbe8be2010-03-30 14:32:59 -0700115#include <netinet/ip.h>
116#include <netinet/ip6.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800117#include <netinet/tcp.h>
118#include <errno.h>
119#include <sys/signal.h>
120#include <signal.h>
121'
Russ Cox602a4462009-06-01 22:14:57 -0700122
Russ Coxdd2abe52011-11-10 19:08:28 -0500123ccflags="$@"
Russ Cox7906e312010-04-29 23:34:06 -0700124
Russ Coxdd2abe52011-11-10 19:08:28 -0500125# Write cgo -godefs input.
Russ Cox602a4462009-06-01 22:14:57 -0700126(
Russ Coxdd2abe52011-11-10 19:08:28 -0500127 echo package syscall
128 echo
129 echo '/*'
Russ Coxfd1add22009-11-01 11:13:27 -0800130 indirect="includes_$(uname)"
131 echo "${!indirect} $includes"
Russ Coxdd2abe52011-11-10 19:08:28 -0500132 echo '*/'
133 echo 'import "C"'
Russ Coxfd1add22009-11-01 11:13:27 -0800134 echo
Russ Coxdd2abe52011-11-10 19:08:28 -0500135 echo 'const ('
Russ Coxfd1add22009-11-01 11:13:27 -0800136
137 # The gcc command line prints all the #defines
138 # it encounters while processing the input
Russ Cox7906e312010-04-29 23:34:06 -0700139 echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags |
Russ Coxfd1add22009-11-01 11:13:27 -0800140 awk '
Joel Singbf2d4032011-08-22 23:24:32 -0400141 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500142
Russ Cox23bf4082010-05-03 11:11:01 -0700143 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
Russ Coxfd1add22009-11-01 11:13:27 -0800144 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
Albert Strasheimcf6c2122010-12-07 13:40:14 -0500145 $2 ~ /^(SCM_SRCRT)$/ {next}
146 $2 ~ /^(MAP_FAILED)$/ {next}
Russ Coxfd1add22009-11-01 11:13:27 -0800147
Mikio Hara6555cfc2010-12-09 13:55:59 -0500148 $2 !~ /^ETH_/ &&
Mikio Hara9c97af92011-02-11 14:34:00 -0500149 $2 !~ /^EPROC_/ &&
150 $2 !~ /^EQUIV_/ &&
151 $2 !~ /^EXPR_/ &&
Russ Coxfd1add22009-11-01 11:13:27 -0800152 $2 ~ /^E[A-Z0-9_]+$/ ||
153 $2 ~ /^SIG[^_]/ ||
Balazs Lecz4bfcfcf2010-09-27 11:44:26 -0400154 $2 ~ /^IN_/ ||
Albert Strasheimc45a08e2011-04-06 16:19:22 -0400155 $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 -0800156 $2 == "SOMAXCONN" ||
157 $2 == "NAME_MAX" ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500158 $2 == "IFNAMSIZ" ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500159 $2 == "CTL_NET" ||
160 $2 == "CTL_MAXNAME" ||
David Andersonad102e12011-03-09 05:58:47 -0800161 $2 ~ /^(MS|MNT)_/ ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500162 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Jeff Hodges0ce57a72011-06-14 12:56:46 -0400163 $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
David Anderson05660b72011-03-09 05:45:08 -0800164 $2 ~ /^LINUX_REBOOT_CMD_/ ||
165 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
Mikio Hara1d550bd2011-05-18 16:33:41 -0700166 $2 !~ "NLA_TYPE_MASK" &&
Mikio Hara94093202011-09-08 13:59:34 -0400167 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500168 $2 ~ /^SIOC/ ||
Ken Rockotc8443d72011-06-22 18:07:20 -0400169 $2 ~ /^TIOC/ ||
Mikio Hara12376c92011-05-26 20:02:03 -0400170 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400171 $2 ~ /^BIOC/ ||
Mikio Hara380f4ab2011-04-21 16:58:20 -0400172 $2 !~ /^(BPF_TIMEVAL)$/ &&
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400173 $2 ~ /^(BPF|DLT)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500174 $2 !~ "WMESGLEN" &&
Russ Coxdd2abe52011-11-10 19:08:28 -0500175 $2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
Russ Cox23bf4082010-05-03 11:11:01 -0700176 $2 ~ /^__WCOREFLAG$/ {next}
Russ Coxdd2abe52011-11-10 19:08:28 -0500177 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500178
Russ Coxfd1add22009-11-01 11:13:27 -0800179 {next}
180 ' | sort
181
Russ Coxdd2abe52011-11-10 19:08:28 -0500182 echo ')'
183) >_const.go
Russ Coxfd1add22009-11-01 11:13:27 -0800184
185# Pull out just the error names for later.
186errors=$(
Russ Cox7906e312010-04-29 23:34:06 -0700187 echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
Russ Cox0b126c12009-12-01 16:53:43 -0800188 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
189 sort
Russ Coxfd1add22009-11-01 11:13:27 -0800190)
Russ Cox602a4462009-06-01 22:14:57 -0700191
Rob Pikecd324982009-08-13 13:22:37 -0700192echo '// mkerrors.sh' "$@"
Russ Cox602a4462009-06-01 22:14:57 -0700193echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
194echo
Russ Coxdd2abe52011-11-10 19:08:28 -0500195cgo -godefs -- "$@" _const.go
Russ Cox602a4462009-06-01 22:14:57 -0700196
197# Run C program to print error strings.
198(
Russ Cox9feee912009-08-24 11:03:23 -0700199 /bin/echo "
Russ Cox602a4462009-06-01 22:14:57 -0700200#include <stdio.h>
201#include <errno.h>
202#include <ctype.h>
203#include <string.h>
204
205#define nelem(x) (sizeof(x)/sizeof((x)[0]))
206
207enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
208
209int errors[] = {
210"
211 for i in $errors
212 do
Russ Cox9feee912009-08-24 11:03:23 -0700213 /bin/echo ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700214 done
215
Russ Cox9feee912009-08-24 11:03:23 -0700216 # Use /bin/echo to avoid builtin echo,
217 # which interprets \n itself
218 /bin/echo '
Russ Cox602a4462009-06-01 22:14:57 -0700219};
220
Russ Cox9a96fb32010-09-24 13:37:02 -0400221static int
222intcmp(const void *a, const void *b)
223{
224 return *(int*)a - *(int*)b;
225}
226
Russ Cox602a4462009-06-01 22:14:57 -0700227int
228main(void)
229{
230 int i, j, e;
231 char buf[1024];
232
233 printf("\n\n// Error table\n");
234 printf("var errors = [...]string {\n");
Russ Cox9a96fb32010-09-24 13:37:02 -0400235 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
Russ Cox602a4462009-06-01 22:14:57 -0700236 for(i=0; i<nelem(errors); i++) {
237 e = errors[i];
Russ Cox9a96fb32010-09-24 13:37:02 -0400238 if(i > 0 && errors[i-1] == e)
239 continue;
Russ Cox602a4462009-06-01 22:14:57 -0700240 strcpy(buf, strerror(e));
241 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
242 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
243 buf[0] += a - A;
244 printf("\t%d: \"%s\",\n", e, buf);
Russ Cox602a4462009-06-01 22:14:57 -0700245 }
246 printf("}\n\n");
Russ Coxfd1add22009-11-01 11:13:27 -0800247 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700248}
249
250'
251) >_errors.c
252
Russ Coxdd2abe52011-11-10 19:08:28 -0500253$GCC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go