blob: fc86d8bd7e052cb72357fff7e9e6c328c66d8014 [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
Mikio Harafe330cf2014-02-24 16:34:51 +090014CC=${CC:-gcc}
Kai Backmane5c884f2009-10-01 06:55:01 -070015
Shawn Walker-Salasaf7c9a42015-09-04 16:53:19 -070016if [[ "$GOOS" -eq "solaris" ]]; then
17 # Assumes GNU versions of utilities in PATH.
18 export PATH=/usr/gnu/bin:$PATH
19fi
20
Russ Coxfd1add22009-11-01 11:13:27 -080021uname=$(uname)
Russ Cox602a4462009-06-01 22:14:57 -070022
Clément Chigot8f9902d2018-09-28 15:29:58 +020023includes_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>
Clément Chigot63cad962019-04-10 17:16:48 +020031#include <termios.h>
Clément Chigot8f9902d2018-09-28 15:29:58 +020032'
33
Russ Coxfd1add22009-11-01 11:13:27 -080034includes_Darwin='
Russ Coxacd858e2011-01-18 14:02:41 -050035#define _DARWIN_C_SOURCE
Russ Coxfd1add22009-11-01 11:13:27 -080036#define KERNEL
37#define _DARWIN_USE_64_BIT_INODE
Mikio Hara9c97af92011-02-11 14:34:00 -050038#include <sys/types.h>
Russ Coxfd1add22009-11-01 11:13:27 -080039#include <sys/event.h>
Jeff Hodges0ce57a72011-06-14 12:56:46 -040040#include <sys/ptrace.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050041#include <sys/socket.h>
42#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050043#include <sys/sysctl.h>
Russ Cox48ae1f22011-04-06 17:52:02 -040044#include <sys/mman.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050045#include <sys/wait.h>
Mikio Hara380f4ab2011-04-21 16:58:20 -040046#include <net/bpf.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050047#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040048#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050049#include <net/route.h>
50#include <netinet/in.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050051#include <netinet/ip.h>
52#include <netinet/ip_mroute.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040053#include <termios.h>
Russ Coxfd1add22009-11-01 11:13:27 -080054'
55
Joel Sing8f3f4c92013-08-24 01:51:25 +100056includes_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
Devon H. O'Dell553be842009-11-14 15:29:09 -080075includes_FreeBSD='
Mikio Hara737efeb2014-03-04 09:26:01 +090076#include <sys/param.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050077#include <sys/types.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080078#include <sys/event.h>
Mikio Hara400ea842011-02-01 08:46:21 -050079#include <sys/socket.h>
80#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050081#include <sys/sysctl.h>
Dave Cheney9dd92d52013-03-09 16:25:30 +110082#include <sys/mman.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050083#include <sys/wait.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040084#include <sys/ioctl.h>
Mikio Hara5a59b9e2011-04-04 15:40:40 -040085#include <net/bpf.h>
Mikio Hara400ea842011-02-01 08:46:21 -050086#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040087#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050088#include <net/route.h>
Mikio Hara400ea842011-02-01 08:46:21 -050089#include <netinet/in.h>
Russ Coxdfacfd62011-06-27 11:02:32 -040090#include <termios.h>
Mikio Hara400ea842011-02-01 08:46:21 -050091#include <netinet/ip.h>
92#include <netinet/ip_mroute.h>
Mikio Hara737efeb2014-03-04 09:26:01 +090093
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
Devon H. O'Dell553be842009-11-14 15:29:09 -0800101'
102
Mikio Hara055b4f72011-12-16 19:51:25 +0900103includes_Linux='
104#define _LARGEFILE_SOURCE
105#define _LARGEFILE64_SOURCE
Ian Lance Taylor08ba7db2015-05-11 20:49:32 -0700106#ifndef __LP64__
Mikio Hara055b4f72011-12-16 19:51:25 +0900107#define _FILE_OFFSET_BITS 64
Ian Lance Taylor08ba7db2015-05-11 20:49:32 -0700108#endif
Mikio Hara055b4f72011-12-16 19:51:25 +0900109#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>
Albert Strasheim2cb6fcf2012-01-09 21:37:46 +0900117#include <sys/prctl.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900118#include <sys/stat.h>
119#include <sys/types.h>
Shenghou Ma46e30c72013-06-11 02:47:04 +0800120#include <sys/time.h>
Mikio Haraadbe59e2013-05-23 16:22:05 +0900121#include <sys/socket.h>
Mikio Harabe9c5142014-02-15 00:47:28 +0900122#include <linux/if.h>
123#include <linux/if_arp.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900124#include <linux/if_ether.h>
125#include <linux/if_tun.h>
Mikio Harabe9c5142014-02-15 00:47:28 +0900126#include <linux/if_packet.h>
127#include <linux/if_addr.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900128#include <linux/filter.h>
129#include <linux/netlink.h>
130#include <linux/reboot.h>
131#include <linux/rtnetlink.h>
132#include <linux/ptrace.h>
Rémy Oudomphengd4f719e2013-08-23 22:26:49 +0200133#include <linux/sched.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900134#include <linux/wait.h>
Mikio Haraadbe59e2013-05-23 16:22:05 +0900135#include <linux/icmpv6.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900136#include <net/route.h>
Dave Cheneybd9cd6e2013-09-13 15:01:22 +1000137#include <termios.h>
Brad Fitzpatrickabf57002013-02-27 15:51:51 -0800138
139#ifndef MSG_FASTOPEN
140#define MSG_FASTOPEN 0x20000000
141#endif
Aram Hăvărneanud0d93102015-03-08 14:21:00 +0100142
143#ifndef PTRACE_GETREGS
144#define PTRACE_GETREGS 0xc
145#endif
146
147#ifndef PTRACE_SETREGS
148#define PTRACE_SETREGS 0xd
149#endif
Mikio Hara055b4f72011-12-16 19:51:25 +0900150'
151
Christopher Nielsen5425db82011-12-20 03:57:58 +1100152includes_NetBSD='
153#include <sys/types.h>
154#include <sys/param.h>
155#include <sys/event.h>
Joel Sing8f984432014-01-07 23:04:17 +1100156#include <sys/mman.h>
Christopher Nielsen5425db82011-12-20 03:57:58 +1100157#include <sys/socket.h>
158#include <sys/sockio.h>
159#include <sys/sysctl.h>
160#include <sys/termios.h>
161#include <sys/ttycom.h>
162#include <sys/wait.h>
163#include <net/bpf.h>
164#include <net/if.h>
165#include <net/if_types.h>
166#include <net/route.h>
167#include <netinet/in.h>
168#include <netinet/in_systm.h>
169#include <netinet/ip.h>
170#include <netinet/ip_mroute.h>
171#include <netinet/if_ether.h>
Joel Sing2a47e042012-05-14 10:40:13 -0700172
173// Needed since <sys/param.h> refers to it...
Joel Sing495a9dc2012-05-23 01:33:48 +1000174#define schedppq 1
Christopher Nielsen5425db82011-12-20 03:57:58 +1100175'
176
Joel Singbf2d4032011-08-22 23:24:32 -0400177includes_OpenBSD='
178#include <sys/types.h>
179#include <sys/param.h>
180#include <sys/event.h>
Joel Singf40dd8f2014-01-13 11:25:48 +1100181#include <sys/mman.h>
Joel Singbf2d4032011-08-22 23:24:32 -0400182#include <sys/socket.h>
183#include <sys/sockio.h>
184#include <sys/sysctl.h>
185#include <sys/termios.h>
186#include <sys/ttycom.h>
187#include <sys/wait.h>
188#include <net/bpf.h>
189#include <net/if.h>
190#include <net/if_types.h>
Mikio Hara40e7f642014-05-16 06:49:15 +0900191#include <net/if_var.h>
Joel Singbf2d4032011-08-22 23:24:32 -0400192#include <net/route.h>
193#include <netinet/in.h>
194#include <netinet/in_systm.h>
195#include <netinet/ip.h>
196#include <netinet/ip_mroute.h>
197#include <netinet/if_ether.h>
198#include <net/if_bridge.h>
Mikio Hara40e7f642014-05-16 06:49:15 +0900199
200// We keep some constants not supported in OpenBSD 5.5 and beyond for
201// the promise of compatibility.
202#define EMUL_ENABLED 0x1
203#define EMUL_NATIVE 0x2
204#define IPV6_FAITH 0x1d
205#define IPV6_OPTIONS 0x1
206#define IPV6_RTHDR_STRICT 0x1
207#define IPV6_SOCKOPT_RESERVED1 0x3
208#define SIOCGIFGENERIC 0xc020693a
209#define SIOCSIFGENERIC 0x80206939
210#define WALTSIG 0x4
Joel Singbf2d4032011-08-22 23:24:32 -0400211'
212
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100213includes_SunOS='
Shawn Walker-Salasaf7c9a42015-09-04 16:53:19 -0700214#include <limits.h>
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100215#include <sys/types.h>
216#include <sys/socket.h>
217#include <sys/sockio.h>
218#include <sys/mman.h>
219#include <sys/wait.h>
220#include <sys/ioctl.h>
221#include <net/bpf.h>
222#include <net/if.h>
223#include <net/if_arp.h>
224#include <net/if_types.h>
225#include <net/route.h>
226#include <netinet/in.h>
227#include <termios.h>
228#include <netinet/ip.h>
229#include <netinet/ip_mroute.h>
230'
231
Russ Coxfd1add22009-11-01 11:13:27 -0800232includes='
233#include <sys/types.h>
Andrea Spadaccini83c30f32011-12-08 15:12:08 +0900234#include <sys/file.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800235#include <fcntl.h>
236#include <dirent.h>
237#include <sys/socket.h>
238#include <netinet/in.h>
Russ Cox8fbe8be2010-03-30 14:32:59 -0700239#include <netinet/ip.h>
240#include <netinet/ip6.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800241#include <netinet/tcp.h>
242#include <errno.h>
243#include <sys/signal.h>
244#include <signal.h>
Sébastien Paolaccie2467f02011-11-19 15:17:40 +0900245#include <sys/resource.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800246'
Russ Cox602a4462009-06-01 22:14:57 -0700247
Russ Coxdd2abe52011-11-10 19:08:28 -0500248ccflags="$@"
Russ Cox7906e312010-04-29 23:34:06 -0700249
Mikio Hara44122ed2012-02-03 12:22:40 +0900250# Write go tool cgo -godefs input.
Russ Cox602a4462009-06-01 22:14:57 -0700251(
Russ Coxdd2abe52011-11-10 19:08:28 -0500252 echo package syscall
253 echo
254 echo '/*'
Russ Coxfd1add22009-11-01 11:13:27 -0800255 indirect="includes_$(uname)"
256 echo "${!indirect} $includes"
Russ Coxdd2abe52011-11-10 19:08:28 -0500257 echo '*/'
258 echo 'import "C"'
Russ Coxfd1add22009-11-01 11:13:27 -0800259 echo
Russ Coxdd2abe52011-11-10 19:08:28 -0500260 echo 'const ('
Russ Coxfd1add22009-11-01 11:13:27 -0800261
262 # The gcc command line prints all the #defines
263 # it encounters while processing the input
Mikio Harafe330cf2014-02-24 16:34:51 +0900264 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
Russ Coxfd1add22009-11-01 11:13:27 -0800265 awk '
Joel Singbf2d4032011-08-22 23:24:32 -0400266 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500267
Russ Cox23bf4082010-05-03 11:11:01 -0700268 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
Russ Coxfd1add22009-11-01 11:13:27 -0800269 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
Albert Strasheimcf6c2122010-12-07 13:40:14 -0500270 $2 ~ /^(SCM_SRCRT)$/ {next}
271 $2 ~ /^(MAP_FAILED)$/ {next}
Shenghou Mac74a4d42014-08-12 19:49:31 -0400272 $2 ~ /^ELF_.*$/ {next} # <asm/elf.h> contains ELF_ARCH, etc.
Russ Coxfd1add22009-11-01 11:13:27 -0800273
Mikio Hara6555cfc2010-12-09 13:55:59 -0500274 $2 !~ /^ETH_/ &&
Mikio Hara9c97af92011-02-11 14:34:00 -0500275 $2 !~ /^EPROC_/ &&
276 $2 !~ /^EQUIV_/ &&
277 $2 !~ /^EXPR_/ &&
Russ Coxfd1add22009-11-01 11:13:27 -0800278 $2 ~ /^E[A-Z0-9_]+$/ ||
Francisco Souza61060ac2012-05-03 17:33:19 -0400279 $2 ~ /^B[0-9_]+$/ ||
280 $2 ~ /^V[A-Z0-9]+$/ ||
281 $2 ~ /^CS[A-Z0-9]/ ||
282 $2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
283 $2 ~ /^IGN/ ||
284 $2 ~ /^IX(ON|ANY|OFF)$/ ||
285 $2 ~ /^IN(LCR|PCK)$/ ||
286 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
287 $2 ~ /^C(LOCAL|READ)$/ ||
288 $2 == "BRKINT" ||
289 $2 == "HUPCL" ||
290 $2 == "PENDIN" ||
291 $2 == "TOSTOP" ||
292 $2 ~ /^PAR/ ||
Russ Coxfd1add22009-11-01 11:13:27 -0800293 $2 ~ /^SIG[^_]/ ||
Francisco Souza61060ac2012-05-03 17:33:19 -0400294 $2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
Balazs Lecz4bfcfcf2010-09-27 11:44:26 -0400295 $2 ~ /^IN_/ ||
Andrea Spadaccini83c30f32011-12-08 15:12:08 +0900296 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
Mikio Hara3bf75e62014-07-30 10:01:32 +0900297 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
Mikio Haraadbe59e2013-05-23 16:22:05 +0900298 $2 == "ICMPV6_FILTER" ||
Russ Coxfd1add22009-11-01 11:13:27 -0800299 $2 == "SOMAXCONN" ||
300 $2 == "NAME_MAX" ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500301 $2 == "IFNAMSIZ" ||
Joel Sing495a9dc2012-05-23 01:33:48 +1000302 $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
303 $2 ~ /^SYSCTL_VERS/ ||
David Andersonad102e12011-03-09 05:58:47 -0800304 $2 ~ /^(MS|MNT)_/ ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500305 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Jeff Hodges0ce57a72011-06-14 12:56:46 -0400306 $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
David Anderson05660b72011-03-09 05:45:08 -0800307 $2 ~ /^LINUX_REBOOT_CMD_/ ||
308 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
Mikio Hara1d550bd2011-05-18 16:33:41 -0700309 $2 !~ "NLA_TYPE_MASK" &&
Mikio Harab1d51c62013-02-24 12:04:48 +0900310 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500311 $2 ~ /^SIOC/ ||
Ken Rockotc8443d72011-06-22 18:07:20 -0400312 $2 ~ /^TIOC/ ||
Mikio Hara753bdc02014-03-28 13:27:14 +0900313 $2 !~ "RTF_BITS" &&
Mikio Hara12376c92011-05-26 20:02:03 -0400314 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400315 $2 ~ /^BIOC/ ||
Sébastien Paolaccie2467f02011-11-19 15:17:40 +0900316 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
317 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
Shenghou Ma46e30c72013-06-11 02:47:04 +0800318 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
Rémy Oudomphengd4f719e2013-08-23 22:26:49 +0200319 $2 ~ /^CLONE_[A-Z_]+/ ||
Mikio Hara380f4ab2011-04-21 16:58:20 -0400320 $2 !~ /^(BPF_TIMEVAL)$/ &&
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400321 $2 ~ /^(BPF|DLT)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500322 $2 !~ "WMESGLEN" &&
Russ Coxdd2abe52011-11-10 19:08:28 -0500323 $2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
Russ Cox23bf4082010-05-03 11:11:01 -0700324 $2 ~ /^__WCOREFLAG$/ {next}
Russ Coxdd2abe52011-11-10 19:08:28 -0500325 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500326
Russ Coxfd1add22009-11-01 11:13:27 -0800327 {next}
328 ' | sort
329
Russ Coxdd2abe52011-11-10 19:08:28 -0500330 echo ')'
331) >_const.go
Russ Coxfd1add22009-11-01 11:13:27 -0800332
Russ Cox35586f72012-02-13 13:52:37 -0500333# Pull out the error names for later.
Russ Coxfd1add22009-11-01 11:13:27 -0800334errors=$(
Mikio Harafe330cf2014-02-24 16:34:51 +0900335 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
Russ Cox0b126c12009-12-01 16:53:43 -0800336 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
337 sort
Russ Coxfd1add22009-11-01 11:13:27 -0800338)
Russ Cox602a4462009-06-01 22:14:57 -0700339
Russ Cox35586f72012-02-13 13:52:37 -0500340# Pull out the signal names for later.
341signals=$(
Mikio Harafe330cf2014-02-24 16:34:51 +0900342 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
Russ Cox35586f72012-02-13 13:52:37 -0500343 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
344 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
345 sort
346)
347
Russ Coxc017a822011-11-13 22:44:52 -0500348# Again, writing regexps to a file.
Mikio Harafe330cf2014-02-24 16:34:51 +0900349echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
Russ Coxc017a822011-11-13 22:44:52 -0500350 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
351 sort >_error.grep
Mikio Harafe330cf2014-02-24 16:34:51 +0900352echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
Russ Cox35586f72012-02-13 13:52:37 -0500353 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
354 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
355 sort >_signal.grep
Russ Coxc017a822011-11-13 22:44:52 -0500356
Rob Pikecd324982009-08-13 13:22:37 -0700357echo '// mkerrors.sh' "$@"
Tobias Klauser10313802018-06-14 11:49:42 +0200358echo '// Code generated by the command above; DO NOT EDIT.'
Russ Cox602a4462009-06-01 22:14:57 -0700359echo
Mikio Hara44122ed2012-02-03 12:22:40 +0900360go tool cgo -godefs -- "$@" _const.go >_error.out
Russ Cox35586f72012-02-13 13:52:37 -0500361cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
Russ Coxc017a822011-11-13 22:44:52 -0500362echo
363echo '// Errors'
364echo 'const ('
365cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= Errno(\1)/'
366echo ')'
Russ Cox602a4462009-06-01 22:14:57 -0700367
Russ Cox35586f72012-02-13 13:52:37 -0500368echo
369echo '// Signals'
370echo 'const ('
371cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= Signal(\1)/'
372echo ')'
373
374# Run C program to print error and syscall strings.
Russ Cox602a4462009-06-01 22:14:57 -0700375(
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100376 echo -E "
Russ Cox602a4462009-06-01 22:14:57 -0700377#include <stdio.h>
Mikio Hara65675a32014-02-09 17:20:59 +0900378#include <stdlib.h>
Russ Cox602a4462009-06-01 22:14:57 -0700379#include <errno.h>
380#include <ctype.h>
381#include <string.h>
Russ Cox35586f72012-02-13 13:52:37 -0500382#include <signal.h>
Russ Cox602a4462009-06-01 22:14:57 -0700383
384#define nelem(x) (sizeof(x)/sizeof((x)[0]))
385
386enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
387
388int errors[] = {
389"
390 for i in $errors
391 do
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100392 echo -E ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700393 done
394
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100395 echo -E "
Russ Cox35586f72012-02-13 13:52:37 -0500396};
397
398int signals[] = {
399"
400 for i in $signals
401 do
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100402 echo -E ' '$i,
Russ Cox35586f72012-02-13 13:52:37 -0500403 done
404
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100405 # Use -E because on some systems bash builtin interprets \n itself.
406 echo -E '
Russ Cox602a4462009-06-01 22:14:57 -0700407};
408
Russ Cox9a96fb32010-09-24 13:37:02 -0400409static int
410intcmp(const void *a, const void *b)
411{
412 return *(int*)a - *(int*)b;
413}
414
Russ Cox602a4462009-06-01 22:14:57 -0700415int
416main(void)
417{
418 int i, j, e;
Russ Cox35586f72012-02-13 13:52:37 -0500419 char buf[1024], *p;
Russ Cox602a4462009-06-01 22:14:57 -0700420
421 printf("\n\n// Error table\n");
422 printf("var errors = [...]string {\n");
Russ Cox9a96fb32010-09-24 13:37:02 -0400423 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
Russ Cox602a4462009-06-01 22:14:57 -0700424 for(i=0; i<nelem(errors); i++) {
425 e = errors[i];
Russ Cox9a96fb32010-09-24 13:37:02 -0400426 if(i > 0 && errors[i-1] == e)
427 continue;
Russ Cox602a4462009-06-01 22:14:57 -0700428 strcpy(buf, strerror(e));
429 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
430 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
431 buf[0] += a - A;
432 printf("\t%d: \"%s\",\n", e, buf);
Russ Cox602a4462009-06-01 22:14:57 -0700433 }
434 printf("}\n\n");
Russ Cox35586f72012-02-13 13:52:37 -0500435
436 printf("\n\n// Signal table\n");
437 printf("var signals = [...]string {\n");
438 qsort(signals, nelem(signals), sizeof signals[0], intcmp);
439 for(i=0; i<nelem(signals); i++) {
440 e = signals[i];
441 if(i > 0 && signals[i-1] == e)
442 continue;
443 strcpy(buf, strsignal(e));
444 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
445 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
446 buf[0] += a - A;
447 // cut trailing : number.
448 p = strrchr(buf, ":"[0]);
449 if(p)
450 *p = '\0';
451 printf("\t%d: \"%s\",\n", e, buf);
452 }
453 printf("}\n\n");
454
Russ Coxfd1add22009-11-01 11:13:27 -0800455 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700456}
457
458'
459) >_errors.c
460
Mikio Harafe330cf2014-02-24 16:34:51 +0900461$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out