mkerrors.sh 16 KB

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