You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mkerrors.sh 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #!/usr/bin/env bash
  2. # 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. # Generate Go code listing errors and other #defined constant
  6. # values (ENAMETOOLONG etc.), by asking the preprocessor
  7. # about the definitions.
  8. unset LANG
  9. export LC_ALL=C
  10. export LC_CTYPE=C
  11. if test -z "$GOARCH" -o -z "$GOOS"; then
  12. echo 1>&2 "GOARCH or GOOS not defined in environment"
  13. exit 1
  14. fi
  15. # Check that we are using the new build system if we should
  16. if [[ "$GOOS" = "linux" ]] && [[ "$GOLANG_SYS_BUILD" != "docker" ]]; then
  17. echo 1>&2 "In the Docker based build system, mkerrors should not be called directly."
  18. echo 1>&2 "See README.md"
  19. exit 1
  20. fi
  21. if [[ "$GOOS" = "aix" ]]; then
  22. CC=${CC:-gcc}
  23. else
  24. CC=${CC:-cc}
  25. fi
  26. if [[ "$GOOS" = "solaris" ]]; then
  27. # Assumes GNU versions of utilities in PATH.
  28. export PATH=/usr/gnu/bin:$PATH
  29. fi
  30. uname=$(uname)
  31. includes_AIX='
  32. #include <net/if.h>
  33. #include <net/netopt.h>
  34. #include <netinet/ip_mroute.h>
  35. #include <sys/protosw.h>
  36. #include <sys/stropts.h>
  37. #include <sys/mman.h>
  38. #include <sys/poll.h>
  39. #include <sys/termio.h>
  40. #include <termios.h>
  41. #include <fcntl.h>
  42. #define AF_LOCAL AF_UNIX
  43. '
  44. includes_Darwin='
  45. #define _DARWIN_C_SOURCE
  46. #define KERNEL
  47. #define _DARWIN_USE_64_BIT_INODE
  48. #include <stdint.h>
  49. #include <sys/attr.h>
  50. #include <sys/types.h>
  51. #include <sys/event.h>
  52. #include <sys/ptrace.h>
  53. #include <sys/socket.h>
  54. #include <sys/sockio.h>
  55. #include <sys/sysctl.h>
  56. #include <sys/mman.h>
  57. #include <sys/mount.h>
  58. #include <sys/utsname.h>
  59. #include <sys/wait.h>
  60. #include <sys/xattr.h>
  61. #include <net/bpf.h>
  62. #include <net/if.h>
  63. #include <net/if_types.h>
  64. #include <net/route.h>
  65. #include <netinet/in.h>
  66. #include <netinet/ip.h>
  67. #include <termios.h>
  68. '
  69. includes_DragonFly='
  70. #include <sys/types.h>
  71. #include <sys/event.h>
  72. #include <sys/socket.h>
  73. #include <sys/sockio.h>
  74. #include <sys/stat.h>
  75. #include <sys/sysctl.h>
  76. #include <sys/mman.h>
  77. #include <sys/mount.h>
  78. #include <sys/wait.h>
  79. #include <sys/ioctl.h>
  80. #include <net/bpf.h>
  81. #include <net/if.h>
  82. #include <net/if_types.h>
  83. #include <net/route.h>
  84. #include <netinet/in.h>
  85. #include <termios.h>
  86. #include <netinet/ip.h>
  87. #include <net/ip_mroute/ip_mroute.h>
  88. '
  89. includes_FreeBSD='
  90. #include <sys/capsicum.h>
  91. #include <sys/param.h>
  92. #include <sys/types.h>
  93. #include <sys/event.h>
  94. #include <sys/socket.h>
  95. #include <sys/sockio.h>
  96. #include <sys/stat.h>
  97. #include <sys/sysctl.h>
  98. #include <sys/mman.h>
  99. #include <sys/mount.h>
  100. #include <sys/wait.h>
  101. #include <sys/ioctl.h>
  102. #include <net/bpf.h>
  103. #include <net/if.h>
  104. #include <net/if_types.h>
  105. #include <net/route.h>
  106. #include <netinet/in.h>
  107. #include <termios.h>
  108. #include <netinet/ip.h>
  109. #include <netinet/ip_mroute.h>
  110. #include <sys/extattr.h>
  111. #if __FreeBSD__ >= 10
  112. #define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
  113. #undef SIOCAIFADDR
  114. #define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
  115. #undef SIOCSIFPHYADDR
  116. #define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
  117. #endif
  118. '
  119. includes_Linux='
  120. #define _LARGEFILE_SOURCE
  121. #define _LARGEFILE64_SOURCE
  122. #ifndef __LP64__
  123. #define _FILE_OFFSET_BITS 64
  124. #endif
  125. #define _GNU_SOURCE
  126. // <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
  127. // these structures. We just include them copied from <bits/termios.h>.
  128. #if defined(__powerpc__)
  129. struct sgttyb {
  130. char sg_ispeed;
  131. char sg_ospeed;
  132. char sg_erase;
  133. char sg_kill;
  134. short sg_flags;
  135. };
  136. struct tchars {
  137. char t_intrc;
  138. char t_quitc;
  139. char t_startc;
  140. char t_stopc;
  141. char t_eofc;
  142. char t_brkc;
  143. };
  144. struct ltchars {
  145. char t_suspc;
  146. char t_dsuspc;
  147. char t_rprntc;
  148. char t_flushc;
  149. char t_werasc;
  150. char t_lnextc;
  151. };
  152. #endif
  153. #include <bits/sockaddr.h>
  154. #include <sys/epoll.h>
  155. #include <sys/eventfd.h>
  156. #include <sys/inotify.h>
  157. #include <sys/ioctl.h>
  158. #include <sys/mman.h>
  159. #include <sys/mount.h>
  160. #include <sys/prctl.h>
  161. #include <sys/stat.h>
  162. #include <sys/types.h>
  163. #include <sys/time.h>
  164. #include <sys/signalfd.h>
  165. #include <sys/socket.h>
  166. #include <sys/xattr.h>
  167. #include <linux/bpf.h>
  168. #include <linux/errqueue.h>
  169. #include <linux/if.h>
  170. #include <linux/if_alg.h>
  171. #include <linux/if_arp.h>
  172. #include <linux/if_ether.h>
  173. #include <linux/if_ppp.h>
  174. #include <linux/if_tun.h>
  175. #include <linux/if_packet.h>
  176. #include <linux/if_addr.h>
  177. #include <linux/falloc.h>
  178. #include <linux/fanotify.h>
  179. #include <linux/filter.h>
  180. #include <linux/fs.h>
  181. #include <linux/kexec.h>
  182. #include <linux/keyctl.h>
  183. #include <linux/magic.h>
  184. #include <linux/memfd.h>
  185. #include <linux/module.h>
  186. #include <linux/netfilter/nfnetlink.h>
  187. #include <linux/netlink.h>
  188. #include <linux/net_namespace.h>
  189. #include <linux/perf_event.h>
  190. #include <linux/random.h>
  191. #include <linux/reboot.h>
  192. #include <linux/rtnetlink.h>
  193. #include <linux/ptrace.h>
  194. #include <linux/sched.h>
  195. #include <linux/seccomp.h>
  196. #include <linux/sockios.h>
  197. #include <linux/wait.h>
  198. #include <linux/icmpv6.h>
  199. #include <linux/serial.h>
  200. #include <linux/can.h>
  201. #include <linux/vm_sockets.h>
  202. #include <linux/taskstats.h>
  203. #include <linux/genetlink.h>
  204. #include <linux/watchdog.h>
  205. #include <linux/hdreg.h>
  206. #include <linux/rtc.h>
  207. #include <linux/if_xdp.h>
  208. #include <linux/cryptouser.h>
  209. #include <mtd/ubi-user.h>
  210. #include <net/route.h>
  211. #if defined(__sparc__)
  212. // On sparc{,64}, the kernel defines struct termios2 itself which clashes with the
  213. // definition in glibc. As only the error constants are needed here, include the
  214. // generic termibits.h (which is included by termbits.h on sparc).
  215. #include <asm-generic/termbits.h>
  216. #else
  217. #include <asm/termbits.h>
  218. #endif
  219. #ifndef MSG_FASTOPEN
  220. #define MSG_FASTOPEN 0x20000000
  221. #endif
  222. #ifndef PTRACE_GETREGS
  223. #define PTRACE_GETREGS 0xc
  224. #endif
  225. #ifndef PTRACE_SETREGS
  226. #define PTRACE_SETREGS 0xd
  227. #endif
  228. #ifndef SOL_NETLINK
  229. #define SOL_NETLINK 270
  230. #endif
  231. #ifdef SOL_BLUETOOTH
  232. // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
  233. // but it is already in bluetooth_linux.go
  234. #undef SOL_BLUETOOTH
  235. #endif
  236. // Certain constants are missing from the fs/crypto UAPI
  237. #define FS_KEY_DESC_PREFIX "fscrypt:"
  238. #define FS_KEY_DESC_PREFIX_SIZE 8
  239. #define FS_MAX_KEY_SIZE 64
  240. '
  241. includes_NetBSD='
  242. #include <sys/types.h>
  243. #include <sys/param.h>
  244. #include <sys/event.h>
  245. #include <sys/extattr.h>
  246. #include <sys/mman.h>
  247. #include <sys/mount.h>
  248. #include <sys/socket.h>
  249. #include <sys/sockio.h>
  250. #include <sys/sysctl.h>
  251. #include <sys/termios.h>
  252. #include <sys/ttycom.h>
  253. #include <sys/wait.h>
  254. #include <net/bpf.h>
  255. #include <net/if.h>
  256. #include <net/if_types.h>
  257. #include <net/route.h>
  258. #include <netinet/in.h>
  259. #include <netinet/in_systm.h>
  260. #include <netinet/ip.h>
  261. #include <netinet/ip_mroute.h>
  262. #include <netinet/if_ether.h>
  263. // Needed since <sys/param.h> refers to it...
  264. #define schedppq 1
  265. '
  266. includes_OpenBSD='
  267. #include <sys/types.h>
  268. #include <sys/param.h>
  269. #include <sys/event.h>
  270. #include <sys/mman.h>
  271. #include <sys/mount.h>
  272. #include <sys/socket.h>
  273. #include <sys/sockio.h>
  274. #include <sys/stat.h>
  275. #include <sys/sysctl.h>
  276. #include <sys/termios.h>
  277. #include <sys/ttycom.h>
  278. #include <sys/unistd.h>
  279. #include <sys/wait.h>
  280. #include <net/bpf.h>
  281. #include <net/if.h>
  282. #include <net/if_types.h>
  283. #include <net/if_var.h>
  284. #include <net/route.h>
  285. #include <netinet/in.h>
  286. #include <netinet/in_systm.h>
  287. #include <netinet/ip.h>
  288. #include <netinet/ip_mroute.h>
  289. #include <netinet/if_ether.h>
  290. #include <net/if_bridge.h>
  291. // We keep some constants not supported in OpenBSD 5.5 and beyond for
  292. // the promise of compatibility.
  293. #define EMUL_ENABLED 0x1
  294. #define EMUL_NATIVE 0x2
  295. #define IPV6_FAITH 0x1d
  296. #define IPV6_OPTIONS 0x1
  297. #define IPV6_RTHDR_STRICT 0x1
  298. #define IPV6_SOCKOPT_RESERVED1 0x3
  299. #define SIOCGIFGENERIC 0xc020693a
  300. #define SIOCSIFGENERIC 0x80206939
  301. #define WALTSIG 0x4
  302. '
  303. includes_SunOS='
  304. #include <limits.h>
  305. #include <sys/types.h>
  306. #include <sys/socket.h>
  307. #include <sys/sockio.h>
  308. #include <sys/stat.h>
  309. #include <sys/mman.h>
  310. #include <sys/wait.h>
  311. #include <sys/ioctl.h>
  312. #include <sys/mkdev.h>
  313. #include <net/bpf.h>
  314. #include <net/if.h>
  315. #include <net/if_arp.h>
  316. #include <net/if_types.h>
  317. #include <net/route.h>
  318. #include <netinet/in.h>
  319. #include <termios.h>
  320. #include <netinet/ip.h>
  321. #include <netinet/ip_mroute.h>
  322. '
  323. includes='
  324. #include <sys/types.h>
  325. #include <sys/file.h>
  326. #include <fcntl.h>
  327. #include <dirent.h>
  328. #include <sys/socket.h>
  329. #include <netinet/in.h>
  330. #include <netinet/ip.h>
  331. #include <netinet/ip6.h>
  332. #include <netinet/tcp.h>
  333. #include <errno.h>
  334. #include <sys/signal.h>
  335. #include <signal.h>
  336. #include <sys/resource.h>
  337. #include <time.h>
  338. '
  339. ccflags="$@"
  340. # Write go tool cgo -godefs input.
  341. (
  342. echo package unix
  343. echo
  344. echo '/*'
  345. indirect="includes_$(uname)"
  346. echo "${!indirect} $includes"
  347. echo '*/'
  348. echo 'import "C"'
  349. echo 'import "syscall"'
  350. echo
  351. echo 'const ('
  352. # The gcc command line prints all the #defines
  353. # it encounters while processing the input
  354. echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
  355. awk '
  356. $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
  357. $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
  358. $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
  359. $2 ~ /^(SCM_SRCRT)$/ {next}
  360. $2 ~ /^(MAP_FAILED)$/ {next}
  361. $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
  362. $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
  363. $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
  364. $2 !~ /^ECCAPBITS/ &&
  365. $2 !~ /^ETH_/ &&
  366. $2 !~ /^EPROC_/ &&
  367. $2 !~ /^EQUIV_/ &&
  368. $2 !~ /^EXPR_/ &&
  369. $2 ~ /^E[A-Z0-9_]+$/ ||
  370. $2 ~ /^B[0-9_]+$/ ||
  371. $2 ~ /^(OLD|NEW)DEV$/ ||
  372. $2 == "BOTHER" ||
  373. $2 ~ /^CI?BAUD(EX)?$/ ||
  374. $2 == "IBSHIFT" ||
  375. $2 ~ /^V[A-Z0-9]+$/ ||
  376. $2 ~ /^CS[A-Z0-9]/ ||
  377. $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
  378. $2 ~ /^IGN/ ||
  379. $2 ~ /^IX(ON|ANY|OFF)$/ ||
  380. $2 ~ /^IN(LCR|PCK)$/ ||
  381. $2 !~ "X86_CR3_PCID_NOFLUSH" &&
  382. $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
  383. $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
  384. $2 == "BRKINT" ||
  385. $2 == "HUPCL" ||
  386. $2 == "PENDIN" ||
  387. $2 == "TOSTOP" ||
  388. $2 == "XCASE" ||
  389. $2 == "ALTWERASE" ||
  390. $2 == "NOKERNINFO" ||
  391. $2 ~ /^PAR/ ||
  392. $2 ~ /^SIG[^_]/ ||
  393. $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
  394. $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
  395. $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
  396. $2 ~ /^O?XTABS$/ ||
  397. $2 ~ /^TC[IO](ON|OFF)$/ ||
  398. $2 ~ /^IN_/ ||
  399. $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
  400. $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|MCAST|EVFILT|NOTE|EV|SHUT|PROT|MAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
  401. $2 ~ /^TP_STATUS_/ ||
  402. $2 ~ /^FALLOC_/ ||
  403. $2 == "ICMPV6_FILTER" ||
  404. $2 == "SOMAXCONN" ||
  405. $2 == "NAME_MAX" ||
  406. $2 == "IFNAMSIZ" ||
  407. $2 ~ /^CTL_(HW|KERN|MAXNAME|NET|QUERY)$/ ||
  408. $2 ~ /^KERN_(HOSTNAME|OS(RELEASE|TYPE)|VERSION)$/ ||
  409. $2 ~ /^HW_MACHINE$/ ||
  410. $2 ~ /^SYSCTL_VERS/ ||
  411. $2 !~ "MNT_BITS" &&
  412. $2 ~ /^(MS|MNT|UMOUNT)_/ ||
  413. $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
  414. $2 ~ /^(O|F|[ES]?FD|NAME|S|PTRACE|PT)_/ ||
  415. $2 ~ /^KEXEC_/ ||
  416. $2 ~ /^LINUX_REBOOT_CMD_/ ||
  417. $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
  418. $2 ~ /^MODULE_INIT_/ ||
  419. $2 !~ "NLA_TYPE_MASK" &&
  420. $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTC|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P|NETNSA)_/ ||
  421. $2 ~ /^SIOC/ ||
  422. $2 ~ /^TIOC/ ||
  423. $2 ~ /^TCGET/ ||
  424. $2 ~ /^TCSET/ ||
  425. $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
  426. $2 !~ "RTF_BITS" &&
  427. $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
  428. $2 ~ /^BIOC/ ||
  429. $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
  430. $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|LOCKS|MEMLOCK|MSGQUEUE|NICE|NOFILE|NPROC|RSS|RTPRIO|RTTIME|SIGPENDING|STACK)|RLIM_INFINITY/ ||
  431. $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
  432. $2 ~ /^CLONE_[A-Z_]+/ ||
  433. $2 !~ /^(BPF_TIMEVAL|BPF_FIB_LOOKUP_[A-Z]+)$/ &&
  434. $2 ~ /^(BPF|DLT)_/ ||
  435. $2 ~ /^(CLOCK|TIMER)_/ ||
  436. $2 ~ /^CAN_/ ||
  437. $2 ~ /^CAP_/ ||
  438. $2 ~ /^ALG_/ ||
  439. $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE|IOC_(GET|SET)_ENCRYPTION)/ ||
  440. $2 ~ /^GRND_/ ||
  441. $2 ~ /^RND/ ||
  442. $2 ~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
  443. $2 ~ /^KEYCTL_/ ||
  444. $2 ~ /^PERF_EVENT_IOC_/ ||
  445. $2 ~ /^SECCOMP_MODE_/ ||
  446. $2 ~ /^SPLICE_/ ||
  447. $2 ~ /^SYNC_FILE_RANGE_/ ||
  448. $2 !~ /^AUDIT_RECORD_MAGIC/ &&
  449. $2 !~ /IOC_MAGIC/ &&
  450. $2 ~ /^[A-Z][A-Z0-9_]+_MAGIC2?$/ ||
  451. $2 ~ /^(VM|VMADDR)_/ ||
  452. $2 ~ /^IOCTL_VM_SOCKETS_/ ||
  453. $2 ~ /^(TASKSTATS|TS)_/ ||
  454. $2 ~ /^CGROUPSTATS_/ ||
  455. $2 ~ /^GENL_/ ||
  456. $2 ~ /^STATX_/ ||
  457. $2 ~ /^RENAME/ ||
  458. $2 ~ /^UBI_IOC[A-Z]/ ||
  459. $2 ~ /^UTIME_/ ||
  460. $2 ~ /^XATTR_(CREATE|REPLACE|NO(DEFAULT|FOLLOW|SECURITY)|SHOWCOMPRESSION)/ ||
  461. $2 ~ /^ATTR_(BIT_MAP_COUNT|(CMN|VOL|FILE)_)/ ||
  462. $2 ~ /^FSOPT_/ ||
  463. $2 ~ /^WDIOC_/ ||
  464. $2 ~ /^NFN/ ||
  465. $2 ~ /^XDP_/ ||
  466. $2 ~ /^(HDIO|WIN|SMART)_/ ||
  467. $2 ~ /^CRYPTO_/ ||
  468. $2 !~ "WMESGLEN" &&
  469. $2 ~ /^W[A-Z0-9]+$/ ||
  470. $2 ~/^PPPIOC/ ||
  471. $2 ~ /^FAN_|FANOTIFY_/ ||
  472. $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
  473. $2 ~ /^__WCOREFLAG$/ {next}
  474. $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
  475. {next}
  476. ' | sort
  477. echo ')'
  478. ) >_const.go
  479. # Pull out the error names for later.
  480. errors=$(
  481. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  482. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
  483. sort
  484. )
  485. # Pull out the signal names for later.
  486. signals=$(
  487. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  488. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
  489. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
  490. sort
  491. )
  492. # Again, writing regexps to a file.
  493. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  494. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
  495. sort >_error.grep
  496. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  497. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
  498. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT|SIGMAX64)' |
  499. sort >_signal.grep
  500. echo '// mkerrors.sh' "$@"
  501. echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
  502. echo
  503. echo "// +build ${GOARCH},${GOOS}"
  504. echo
  505. go tool cgo -godefs -- "$@" _const.go >_error.out
  506. cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
  507. echo
  508. echo '// Errors'
  509. echo 'const ('
  510. cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
  511. echo ')'
  512. echo
  513. echo '// Signals'
  514. echo 'const ('
  515. cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
  516. echo ')'
  517. # Run C program to print error and syscall strings.
  518. (
  519. echo -E "
  520. #include <stdio.h>
  521. #include <stdlib.h>
  522. #include <errno.h>
  523. #include <ctype.h>
  524. #include <string.h>
  525. #include <signal.h>
  526. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  527. enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
  528. struct tuple {
  529. int num;
  530. const char *name;
  531. };
  532. struct tuple errors[] = {
  533. "
  534. for i in $errors
  535. do
  536. echo -E ' {'$i', "'$i'" },'
  537. done
  538. echo -E "
  539. };
  540. struct tuple signals[] = {
  541. "
  542. for i in $signals
  543. do
  544. echo -E ' {'$i', "'$i'" },'
  545. done
  546. # Use -E because on some systems bash builtin interprets \n itself.
  547. echo -E '
  548. };
  549. static int
  550. tuplecmp(const void *a, const void *b)
  551. {
  552. return ((struct tuple *)a)->num - ((struct tuple *)b)->num;
  553. }
  554. int
  555. main(void)
  556. {
  557. int i, e;
  558. char buf[1024], *p;
  559. printf("\n\n// Error table\n");
  560. printf("var errorList = [...]struct {\n");
  561. printf("\tnum syscall.Errno\n");
  562. printf("\tname string\n");
  563. printf("\tdesc string\n");
  564. printf("} {\n");
  565. qsort(errors, nelem(errors), sizeof errors[0], tuplecmp);
  566. for(i=0; i<nelem(errors); i++) {
  567. e = errors[i].num;
  568. if(i > 0 && errors[i-1].num == e)
  569. continue;
  570. strcpy(buf, strerror(e));
  571. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  572. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  573. buf[0] += a - A;
  574. printf("\t{ %d, \"%s\", \"%s\" },\n", e, errors[i].name, buf);
  575. }
  576. printf("}\n\n");
  577. printf("\n\n// Signal table\n");
  578. printf("var signalList = [...]struct {\n");
  579. printf("\tnum syscall.Signal\n");
  580. printf("\tname string\n");
  581. printf("\tdesc string\n");
  582. printf("} {\n");
  583. qsort(signals, nelem(signals), sizeof signals[0], tuplecmp);
  584. for(i=0; i<nelem(signals); i++) {
  585. e = signals[i].num;
  586. if(i > 0 && signals[i-1].num == e)
  587. continue;
  588. strcpy(buf, strsignal(e));
  589. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  590. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  591. buf[0] += a - A;
  592. // cut trailing : number.
  593. p = strrchr(buf, ":"[0]);
  594. if(p)
  595. *p = '\0';
  596. printf("\t{ %d, \"%s\", \"%s\" },\n", e, signals[i].name, buf);
  597. }
  598. printf("}\n\n");
  599. return 0;
  600. }
  601. '
  602. ) >_errors.c
  603. $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out