blob: d25527bbf92fca0fab865d12a7e15026ac643706 [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
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_Darwin='
Russ Coxacd858e2011-01-18 14:02:41 -050019#define _DARWIN_C_SOURCE
Russ Coxfd1add22009-11-01 11:13:27 -080020#define KERNEL
21#define _DARWIN_USE_64_BIT_INODE
Mikio Hara9c97af92011-02-11 14:34:00 -050022#include <sys/types.h>
Russ Coxfd1add22009-11-01 11:13:27 -080023#include <sys/event.h>
Jeff Hodges0ce57a72011-06-14 12:56:46 -040024#include <sys/ptrace.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050025#include <sys/socket.h>
26#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050027#include <sys/sysctl.h>
Russ Cox48ae1f22011-04-06 17:52:02 -040028#include <sys/mman.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050029#include <sys/wait.h>
Mikio Hara380f4ab2011-04-21 16:58:20 -040030#include <net/bpf.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050031#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040032#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050033#include <net/route.h>
34#include <netinet/in.h>
Mikio Hara5c0aab92011-01-31 12:50:50 -050035#include <netinet/ip.h>
36#include <netinet/ip_mroute.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040037#include <termios.h>
Russ Coxfd1add22009-11-01 11:13:27 -080038'
39
Joel Sing8f3f4c92013-08-24 01:51:25 +100040includes_DragonFly='
41#include <sys/types.h>
42#include <sys/event.h>
43#include <sys/socket.h>
44#include <sys/sockio.h>
45#include <sys/sysctl.h>
46#include <sys/mman.h>
47#include <sys/wait.h>
48#include <sys/ioctl.h>
49#include <net/bpf.h>
50#include <net/if.h>
51#include <net/if_types.h>
52#include <net/route.h>
53#include <netinet/in.h>
54#include <termios.h>
55#include <netinet/ip.h>
56#include <net/ip_mroute/ip_mroute.h>
57'
58
Devon H. O'Dell553be842009-11-14 15:29:09 -080059includes_FreeBSD='
Mikio Hara737efeb2014-03-04 09:26:01 +090060#include <sys/param.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050061#include <sys/types.h>
Devon H. O'Dell553be842009-11-14 15:29:09 -080062#include <sys/event.h>
Mikio Hara400ea842011-02-01 08:46:21 -050063#include <sys/socket.h>
64#include <sys/sockio.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050065#include <sys/sysctl.h>
Dave Cheney9dd92d52013-03-09 16:25:30 +110066#include <sys/mman.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050067#include <sys/wait.h>
Ken Rockotc8443d72011-06-22 18:07:20 -040068#include <sys/ioctl.h>
Mikio Hara5a59b9e2011-04-04 15:40:40 -040069#include <net/bpf.h>
Mikio Hara400ea842011-02-01 08:46:21 -050070#include <net/if.h>
Mikio Hara12376c92011-05-26 20:02:03 -040071#include <net/if_types.h>
Mikio Hara9c97af92011-02-11 14:34:00 -050072#include <net/route.h>
Mikio Hara400ea842011-02-01 08:46:21 -050073#include <netinet/in.h>
Russ Coxdfacfd62011-06-27 11:02:32 -040074#include <termios.h>
Mikio Hara400ea842011-02-01 08:46:21 -050075#include <netinet/ip.h>
76#include <netinet/ip_mroute.h>
Mikio Hara737efeb2014-03-04 09:26:01 +090077
78#if __FreeBSD__ >= 10
79#define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
80#undef SIOCAIFADDR
81#define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
82#undef SIOCSIFPHYADDR
83#define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
84#endif
Devon H. O'Dell553be842009-11-14 15:29:09 -080085'
86
Mikio Hara055b4f72011-12-16 19:51:25 +090087includes_Linux='
88#define _LARGEFILE_SOURCE
89#define _LARGEFILE64_SOURCE
90#define _FILE_OFFSET_BITS 64
91#define _GNU_SOURCE
92
93#include <bits/sockaddr.h>
94#include <sys/epoll.h>
95#include <sys/inotify.h>
96#include <sys/ioctl.h>
97#include <sys/mman.h>
98#include <sys/mount.h>
Albert Strasheim2cb6fcf2012-01-09 21:37:46 +090099#include <sys/prctl.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900100#include <sys/stat.h>
101#include <sys/types.h>
Shenghou Ma46e30c72013-06-11 02:47:04 +0800102#include <sys/time.h>
Mikio Haraadbe59e2013-05-23 16:22:05 +0900103#include <sys/socket.h>
Mikio Harabe9c5142014-02-15 00:47:28 +0900104#include <linux/if.h>
105#include <linux/if_arp.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900106#include <linux/if_ether.h>
107#include <linux/if_tun.h>
Mikio Harabe9c5142014-02-15 00:47:28 +0900108#include <linux/if_packet.h>
109#include <linux/if_addr.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900110#include <linux/filter.h>
111#include <linux/netlink.h>
112#include <linux/reboot.h>
113#include <linux/rtnetlink.h>
114#include <linux/ptrace.h>
Rémy Oudomphengd4f719e2013-08-23 22:26:49 +0200115#include <linux/sched.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900116#include <linux/wait.h>
Mikio Haraadbe59e2013-05-23 16:22:05 +0900117#include <linux/icmpv6.h>
Mikio Hara055b4f72011-12-16 19:51:25 +0900118#include <net/route.h>
Dave Cheneybd9cd6e2013-09-13 15:01:22 +1000119#include <termios.h>
Brad Fitzpatrickabf57002013-02-27 15:51:51 -0800120
121#ifndef MSG_FASTOPEN
122#define MSG_FASTOPEN 0x20000000
123#endif
Aram Hăvărneanud0d93102015-03-08 14:21:00 +0100124
125#ifndef PTRACE_GETREGS
126#define PTRACE_GETREGS 0xc
127#endif
128
129#ifndef PTRACE_SETREGS
130#define PTRACE_SETREGS 0xd
131#endif
Mikio Hara055b4f72011-12-16 19:51:25 +0900132'
133
Christopher Nielsen5425db82011-12-20 03:57:58 +1100134includes_NetBSD='
135#include <sys/types.h>
136#include <sys/param.h>
137#include <sys/event.h>
Joel Sing8f984432014-01-07 23:04:17 +1100138#include <sys/mman.h>
Christopher Nielsen5425db82011-12-20 03:57:58 +1100139#include <sys/socket.h>
140#include <sys/sockio.h>
141#include <sys/sysctl.h>
142#include <sys/termios.h>
143#include <sys/ttycom.h>
144#include <sys/wait.h>
145#include <net/bpf.h>
146#include <net/if.h>
147#include <net/if_types.h>
148#include <net/route.h>
149#include <netinet/in.h>
150#include <netinet/in_systm.h>
151#include <netinet/ip.h>
152#include <netinet/ip_mroute.h>
153#include <netinet/if_ether.h>
Joel Sing2a47e042012-05-14 10:40:13 -0700154
155// Needed since <sys/param.h> refers to it...
Joel Sing495a9dc2012-05-23 01:33:48 +1000156#define schedppq 1
Christopher Nielsen5425db82011-12-20 03:57:58 +1100157'
158
Joel Singbf2d4032011-08-22 23:24:32 -0400159includes_OpenBSD='
160#include <sys/types.h>
161#include <sys/param.h>
162#include <sys/event.h>
Joel Singf40dd8f2014-01-13 11:25:48 +1100163#include <sys/mman.h>
Joel Singbf2d4032011-08-22 23:24:32 -0400164#include <sys/socket.h>
165#include <sys/sockio.h>
166#include <sys/sysctl.h>
167#include <sys/termios.h>
168#include <sys/ttycom.h>
169#include <sys/wait.h>
170#include <net/bpf.h>
171#include <net/if.h>
172#include <net/if_types.h>
Mikio Hara40e7f642014-05-16 06:49:15 +0900173#include <net/if_var.h>
Joel Singbf2d4032011-08-22 23:24:32 -0400174#include <net/route.h>
175#include <netinet/in.h>
176#include <netinet/in_systm.h>
177#include <netinet/ip.h>
178#include <netinet/ip_mroute.h>
179#include <netinet/if_ether.h>
180#include <net/if_bridge.h>
Mikio Hara40e7f642014-05-16 06:49:15 +0900181
182// We keep some constants not supported in OpenBSD 5.5 and beyond for
183// the promise of compatibility.
184#define EMUL_ENABLED 0x1
185#define EMUL_NATIVE 0x2
186#define IPV6_FAITH 0x1d
187#define IPV6_OPTIONS 0x1
188#define IPV6_RTHDR_STRICT 0x1
189#define IPV6_SOCKOPT_RESERVED1 0x3
190#define SIOCGIFGENERIC 0xc020693a
191#define SIOCSIFGENERIC 0x80206939
192#define WALTSIG 0x4
Joel Singbf2d4032011-08-22 23:24:32 -0400193'
194
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100195includes_SunOS='
196#include <sys/types.h>
197#include <sys/socket.h>
198#include <sys/sockio.h>
199#include <sys/mman.h>
200#include <sys/wait.h>
201#include <sys/ioctl.h>
202#include <net/bpf.h>
203#include <net/if.h>
204#include <net/if_arp.h>
205#include <net/if_types.h>
206#include <net/route.h>
207#include <netinet/in.h>
208#include <termios.h>
209#include <netinet/ip.h>
210#include <netinet/ip_mroute.h>
211'
212
Russ Coxfd1add22009-11-01 11:13:27 -0800213includes='
214#include <sys/types.h>
Andrea Spadaccini83c30f32011-12-08 15:12:08 +0900215#include <sys/file.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800216#include <fcntl.h>
217#include <dirent.h>
218#include <sys/socket.h>
219#include <netinet/in.h>
Russ Cox8fbe8be2010-03-30 14:32:59 -0700220#include <netinet/ip.h>
221#include <netinet/ip6.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800222#include <netinet/tcp.h>
223#include <errno.h>
224#include <sys/signal.h>
225#include <signal.h>
Sébastien Paolaccie2467f02011-11-19 15:17:40 +0900226#include <sys/resource.h>
Russ Coxfd1add22009-11-01 11:13:27 -0800227'
Russ Cox602a4462009-06-01 22:14:57 -0700228
Russ Coxdd2abe52011-11-10 19:08:28 -0500229ccflags="$@"
Russ Cox7906e312010-04-29 23:34:06 -0700230
Mikio Hara44122ed2012-02-03 12:22:40 +0900231# Write go tool cgo -godefs input.
Russ Cox602a4462009-06-01 22:14:57 -0700232(
Russ Coxdd2abe52011-11-10 19:08:28 -0500233 echo package syscall
234 echo
235 echo '/*'
Russ Coxfd1add22009-11-01 11:13:27 -0800236 indirect="includes_$(uname)"
237 echo "${!indirect} $includes"
Russ Coxdd2abe52011-11-10 19:08:28 -0500238 echo '*/'
239 echo 'import "C"'
Russ Coxfd1add22009-11-01 11:13:27 -0800240 echo
Russ Coxdd2abe52011-11-10 19:08:28 -0500241 echo 'const ('
Russ Coxfd1add22009-11-01 11:13:27 -0800242
243 # The gcc command line prints all the #defines
244 # it encounters while processing the input
Mikio Harafe330cf2014-02-24 16:34:51 +0900245 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
Russ Coxfd1add22009-11-01 11:13:27 -0800246 awk '
Joel Singbf2d4032011-08-22 23:24:32 -0400247 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500248
Russ Cox23bf4082010-05-03 11:11:01 -0700249 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
Russ Coxfd1add22009-11-01 11:13:27 -0800250 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
Albert Strasheimcf6c2122010-12-07 13:40:14 -0500251 $2 ~ /^(SCM_SRCRT)$/ {next}
252 $2 ~ /^(MAP_FAILED)$/ {next}
Shenghou Mac74a4d42014-08-12 19:49:31 -0400253 $2 ~ /^ELF_.*$/ {next} # <asm/elf.h> contains ELF_ARCH, etc.
Russ Coxfd1add22009-11-01 11:13:27 -0800254
Mikio Hara6555cfc2010-12-09 13:55:59 -0500255 $2 !~ /^ETH_/ &&
Mikio Hara9c97af92011-02-11 14:34:00 -0500256 $2 !~ /^EPROC_/ &&
257 $2 !~ /^EQUIV_/ &&
258 $2 !~ /^EXPR_/ &&
Russ Coxfd1add22009-11-01 11:13:27 -0800259 $2 ~ /^E[A-Z0-9_]+$/ ||
Francisco Souza61060ac2012-05-03 17:33:19 -0400260 $2 ~ /^B[0-9_]+$/ ||
261 $2 ~ /^V[A-Z0-9]+$/ ||
262 $2 ~ /^CS[A-Z0-9]/ ||
263 $2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
264 $2 ~ /^IGN/ ||
265 $2 ~ /^IX(ON|ANY|OFF)$/ ||
266 $2 ~ /^IN(LCR|PCK)$/ ||
267 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
268 $2 ~ /^C(LOCAL|READ)$/ ||
269 $2 == "BRKINT" ||
270 $2 == "HUPCL" ||
271 $2 == "PENDIN" ||
272 $2 == "TOSTOP" ||
273 $2 ~ /^PAR/ ||
Russ Coxfd1add22009-11-01 11:13:27 -0800274 $2 ~ /^SIG[^_]/ ||
Francisco Souza61060ac2012-05-03 17:33:19 -0400275 $2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
Balazs Lecz4bfcfcf2010-09-27 11:44:26 -0400276 $2 ~ /^IN_/ ||
Andrea Spadaccini83c30f32011-12-08 15:12:08 +0900277 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
Mikio Hara3bf75e62014-07-30 10:01:32 +0900278 $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 +0900279 $2 == "ICMPV6_FILTER" ||
Russ Coxfd1add22009-11-01 11:13:27 -0800280 $2 == "SOMAXCONN" ||
281 $2 == "NAME_MAX" ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500282 $2 == "IFNAMSIZ" ||
Joel Sing495a9dc2012-05-23 01:33:48 +1000283 $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
284 $2 ~ /^SYSCTL_VERS/ ||
David Andersonad102e12011-03-09 05:58:47 -0800285 $2 ~ /^(MS|MNT)_/ ||
Mikio Hara6555cfc2010-12-09 13:55:59 -0500286 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
Jeff Hodges0ce57a72011-06-14 12:56:46 -0400287 $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
David Anderson05660b72011-03-09 05:45:08 -0800288 $2 ~ /^LINUX_REBOOT_CMD_/ ||
289 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
Mikio Hara1d550bd2011-05-18 16:33:41 -0700290 $2 !~ "NLA_TYPE_MASK" &&
Mikio Harab1d51c62013-02-24 12:04:48 +0900291 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500292 $2 ~ /^SIOC/ ||
Ken Rockotc8443d72011-06-22 18:07:20 -0400293 $2 ~ /^TIOC/ ||
Mikio Hara753bdc02014-03-28 13:27:14 +0900294 $2 !~ "RTF_BITS" &&
Mikio Hara12376c92011-05-26 20:02:03 -0400295 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400296 $2 ~ /^BIOC/ ||
Sébastien Paolaccie2467f02011-11-19 15:17:40 +0900297 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
298 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
Shenghou Ma46e30c72013-06-11 02:47:04 +0800299 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
Rémy Oudomphengd4f719e2013-08-23 22:26:49 +0200300 $2 ~ /^CLONE_[A-Z_]+/ ||
Mikio Hara380f4ab2011-04-21 16:58:20 -0400301 $2 !~ /^(BPF_TIMEVAL)$/ &&
Mikio Hara5a59b9e2011-04-04 15:40:40 -0400302 $2 ~ /^(BPF|DLT)_/ ||
Mikio Hara9c97af92011-02-11 14:34:00 -0500303 $2 !~ "WMESGLEN" &&
Russ Coxdd2abe52011-11-10 19:08:28 -0500304 $2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
Russ Cox23bf4082010-05-03 11:11:01 -0700305 $2 ~ /^__WCOREFLAG$/ {next}
Russ Coxdd2abe52011-11-10 19:08:28 -0500306 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
Mikio Hara6555cfc2010-12-09 13:55:59 -0500307
Russ Coxfd1add22009-11-01 11:13:27 -0800308 {next}
309 ' | sort
310
Russ Coxdd2abe52011-11-10 19:08:28 -0500311 echo ')'
312) >_const.go
Russ Coxfd1add22009-11-01 11:13:27 -0800313
Russ Cox35586f72012-02-13 13:52:37 -0500314# Pull out the error names for later.
Russ Coxfd1add22009-11-01 11:13:27 -0800315errors=$(
Mikio Harafe330cf2014-02-24 16:34:51 +0900316 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
Russ Cox0b126c12009-12-01 16:53:43 -0800317 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
318 sort
Russ Coxfd1add22009-11-01 11:13:27 -0800319)
Russ Cox602a4462009-06-01 22:14:57 -0700320
Russ Cox35586f72012-02-13 13:52:37 -0500321# Pull out the signal names for later.
322signals=$(
Mikio Harafe330cf2014-02-24 16:34:51 +0900323 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
Russ Cox35586f72012-02-13 13:52:37 -0500324 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
325 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
326 sort
327)
328
Russ Coxc017a822011-11-13 22:44:52 -0500329# Again, writing regexps to a file.
Mikio Harafe330cf2014-02-24 16:34:51 +0900330echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
Russ Coxc017a822011-11-13 22:44:52 -0500331 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
332 sort >_error.grep
Mikio Harafe330cf2014-02-24 16:34:51 +0900333echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
Russ Cox35586f72012-02-13 13:52:37 -0500334 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
335 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
336 sort >_signal.grep
Russ Coxc017a822011-11-13 22:44:52 -0500337
Rob Pikecd324982009-08-13 13:22:37 -0700338echo '// mkerrors.sh' "$@"
Russ Cox602a4462009-06-01 22:14:57 -0700339echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
340echo
Mikio Hara44122ed2012-02-03 12:22:40 +0900341go tool cgo -godefs -- "$@" _const.go >_error.out
Russ Cox35586f72012-02-13 13:52:37 -0500342cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
Russ Coxc017a822011-11-13 22:44:52 -0500343echo
344echo '// Errors'
345echo 'const ('
346cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= Errno(\1)/'
347echo ')'
Russ Cox602a4462009-06-01 22:14:57 -0700348
Russ Cox35586f72012-02-13 13:52:37 -0500349echo
350echo '// Signals'
351echo 'const ('
352cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= Signal(\1)/'
353echo ')'
354
355# Run C program to print error and syscall strings.
Russ Cox602a4462009-06-01 22:14:57 -0700356(
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100357 echo -E "
Russ Cox602a4462009-06-01 22:14:57 -0700358#include <stdio.h>
Mikio Hara65675a32014-02-09 17:20:59 +0900359#include <stdlib.h>
Russ Cox602a4462009-06-01 22:14:57 -0700360#include <errno.h>
361#include <ctype.h>
362#include <string.h>
Russ Cox35586f72012-02-13 13:52:37 -0500363#include <signal.h>
Russ Cox602a4462009-06-01 22:14:57 -0700364
365#define nelem(x) (sizeof(x)/sizeof((x)[0]))
366
367enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
368
369int errors[] = {
370"
371 for i in $errors
372 do
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100373 echo -E ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700374 done
375
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100376 echo -E "
Russ Cox35586f72012-02-13 13:52:37 -0500377};
378
379int signals[] = {
380"
381 for i in $signals
382 do
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100383 echo -E ' '$i,
Russ Cox35586f72012-02-13 13:52:37 -0500384 done
385
Aram Hăvărneanu1c986192014-02-25 21:12:10 +1100386 # Use -E because on some systems bash builtin interprets \n itself.
387 echo -E '
Russ Cox602a4462009-06-01 22:14:57 -0700388};
389
Russ Cox9a96fb32010-09-24 13:37:02 -0400390static int
391intcmp(const void *a, const void *b)
392{
393 return *(int*)a - *(int*)b;
394}
395
Russ Cox602a4462009-06-01 22:14:57 -0700396int
397main(void)
398{
399 int i, j, e;
Russ Cox35586f72012-02-13 13:52:37 -0500400 char buf[1024], *p;
Russ Cox602a4462009-06-01 22:14:57 -0700401
402 printf("\n\n// Error table\n");
403 printf("var errors = [...]string {\n");
Russ Cox9a96fb32010-09-24 13:37:02 -0400404 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
Russ Cox602a4462009-06-01 22:14:57 -0700405 for(i=0; i<nelem(errors); i++) {
406 e = errors[i];
Russ Cox9a96fb32010-09-24 13:37:02 -0400407 if(i > 0 && errors[i-1] == e)
408 continue;
Russ Cox602a4462009-06-01 22:14:57 -0700409 strcpy(buf, strerror(e));
410 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
411 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
412 buf[0] += a - A;
413 printf("\t%d: \"%s\",\n", e, buf);
Russ Cox602a4462009-06-01 22:14:57 -0700414 }
415 printf("}\n\n");
Russ Cox35586f72012-02-13 13:52:37 -0500416
417 printf("\n\n// Signal table\n");
418 printf("var signals = [...]string {\n");
419 qsort(signals, nelem(signals), sizeof signals[0], intcmp);
420 for(i=0; i<nelem(signals); i++) {
421 e = signals[i];
422 if(i > 0 && signals[i-1] == e)
423 continue;
424 strcpy(buf, strsignal(e));
425 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
426 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
427 buf[0] += a - A;
428 // cut trailing : number.
429 p = strrchr(buf, ":"[0]);
430 if(p)
431 *p = '\0';
432 printf("\t%d: \"%s\",\n", e, buf);
433 }
434 printf("}\n\n");
435
Russ Coxfd1add22009-11-01 11:13:27 -0800436 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700437}
438
439'
440) >_errors.c
441
Mikio Harafe330cf2014-02-24 16:34:51 +0900442$CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out