mkerrors.sh 15 KB

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