mkerrors.sh 17 KB

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