本站源代码
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.

671 lines
16KB

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