mkerrors.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. CC=${CC:-gcc}
  12. uname=$(uname)
  13. includes='
  14. #include <sys/types.h>
  15. #include <sys/file.h>
  16. #include <fcntl.h>
  17. #include <dirent.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <netinet/ip.h>
  21. #include <netinet/ip6.h>
  22. #include <netinet/tcp.h>
  23. #include <errno.h>
  24. #include <sys/signal.h>
  25. #include <signal.h>
  26. #include <sys/resource.h>
  27. '
  28. ccflags="$@"
  29. # Write go tool cgo -godefs input.
  30. (
  31. echo package unix
  32. echo
  33. echo '/*'
  34. indirect="includes_$(uname)"
  35. echo "${!indirect} $includes"
  36. echo '*/'
  37. echo 'import "C"'
  38. echo 'import "syscall"'
  39. echo
  40. echo 'const ('
  41. # The gcc command line prints all the #defines
  42. # it encounters while processing the input
  43. echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
  44. awk '
  45. $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
  46. $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
  47. $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
  48. $2 ~ /^(SCM_SRCRT)$/ {next}
  49. $2 ~ /^(MAP_FAILED)$/ {next}
  50. $2 !~ /^ETH_/ &&
  51. $2 !~ /^EPROC_/ &&
  52. $2 !~ /^EQUIV_/ &&
  53. $2 !~ /^EXPR_/ &&
  54. $2 ~ /^E[A-Z0-9_]+$/ ||
  55. $2 ~ /^B[0-9_]+$/ ||
  56. $2 ~ /^V[A-Z0-9]+$/ ||
  57. $2 ~ /^CS[A-Z0-9]/ ||
  58. $2 ~ /^I(SIG|CANON|CRNL|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
  59. $2 ~ /^IGN/ ||
  60. $2 ~ /^IX(ON|ANY|OFF)$/ ||
  61. $2 ~ /^IN(LCR|PCK)$/ ||
  62. $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
  63. $2 ~ /^C(LOCAL|READ)$/ ||
  64. $2 == "BRKINT" ||
  65. $2 == "HUPCL" ||
  66. $2 == "PENDIN" ||
  67. $2 == "TOSTOP" ||
  68. $2 ~ /^PAR/ ||
  69. $2 ~ /^SIG[^_]/ ||
  70. $2 ~ /^O[CNPFP][A-Z]+[^_][A-Z]+$/ ||
  71. $2 ~ /^IN_/ ||
  72. $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
  73. $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
  74. $2 == "ICMPV6_FILTER" ||
  75. $2 == "SOMAXCONN" ||
  76. $2 == "NAME_MAX" ||
  77. $2 == "IFNAMSIZ" ||
  78. $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
  79. $2 ~ /^SYSCTL_VERS/ ||
  80. $2 ~ /^(MS|MNT)_/ ||
  81. $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
  82. $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
  83. $2 ~ /^LINUX_REBOOT_CMD_/ ||
  84. $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
  85. $2 !~ "NLA_TYPE_MASK" &&
  86. $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
  87. $2 ~ /^SIOC/ ||
  88. $2 ~ /^TIOC/ ||
  89. $2 !~ "RTF_BITS" &&
  90. $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
  91. $2 ~ /^BIOC/ ||
  92. $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
  93. $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
  94. $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
  95. $2 ~ /^CLONE_[A-Z_]+/ ||
  96. $2 !~ /^(BPF_TIMEVAL)$/ &&
  97. $2 ~ /^(BPF|DLT)_/ ||
  98. $2 !~ "WMESGLEN" &&
  99. $2 ~ /^W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", $2, $2)}
  100. $2 ~ /^__WCOREFLAG$/ {next}
  101. $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
  102. {next}
  103. ' | sort
  104. echo ')'
  105. ) >_const.go
  106. # Pull out the error names for later.
  107. errors=$(
  108. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  109. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
  110. sort
  111. )
  112. # Pull out the signal names for later.
  113. signals=$(
  114. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  115. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
  116. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
  117. sort
  118. )
  119. # Again, writing regexps to a file.
  120. echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
  121. awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
  122. sort >_error.grep
  123. echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
  124. awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
  125. egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
  126. sort >_signal.grep
  127. echo '// mkerrors.sh' "$@"
  128. echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
  129. echo
  130. go tool cgo -godefs -- "$@" _const.go >_error.out
  131. cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
  132. echo
  133. echo '// Errors'
  134. echo 'const ('
  135. cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
  136. echo ')'
  137. echo
  138. echo '// Signals'
  139. echo 'const ('
  140. cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
  141. echo ')'
  142. # Run C program to print error and syscall strings.
  143. (
  144. echo -E "
  145. #include <stdio.h>
  146. #include <stdlib.h>
  147. #include <errno.h>
  148. #include <ctype.h>
  149. #include <string.h>
  150. #include <signal.h>
  151. #define nelem(x) (sizeof(x)/sizeof((x)[0]))
  152. enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
  153. int errors[] = {
  154. "
  155. for i in $errors
  156. do
  157. echo -E ' '$i,
  158. done
  159. echo -E "
  160. };
  161. int signals[] = {
  162. "
  163. for i in $signals
  164. do
  165. echo -E ' '$i,
  166. done
  167. # Use -E because on some systems bash builtin interprets \n itself.
  168. echo -E '
  169. };
  170. static int
  171. intcmp(const void *a, const void *b)
  172. {
  173. return *(int*)a - *(int*)b;
  174. }
  175. int
  176. main(void)
  177. {
  178. int i, j, e;
  179. char buf[1024], *p;
  180. printf("\n\n// Error table\n");
  181. printf("var errors = [...]string {\n");
  182. qsort(errors, nelem(errors), sizeof errors[0], intcmp);
  183. for(i=0; i<nelem(errors); i++) {
  184. e = errors[i];
  185. if(i > 0 && errors[i-1] == e)
  186. continue;
  187. strcpy(buf, strerror(e));
  188. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  189. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  190. buf[0] += a - A;
  191. printf("\t%d: \"%s\",\n", e, buf);
  192. }
  193. printf("}\n\n");
  194. printf("\n\n// Signal table\n");
  195. printf("var signals = [...]string {\n");
  196. qsort(signals, nelem(signals), sizeof signals[0], intcmp);
  197. for(i=0; i<nelem(signals); i++) {
  198. e = signals[i];
  199. if(i > 0 && signals[i-1] == e)
  200. continue;
  201. strcpy(buf, strsignal(e));
  202. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  203. if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
  204. buf[0] += a - A;
  205. // cut trailing : number.
  206. p = strrchr(buf, ":"[0]);
  207. if(p)
  208. *p = '\0';
  209. printf("\t%d: \"%s\",\n", e, buf);
  210. }
  211. printf("}\n\n");
  212. return 0;
  213. }
  214. '
  215. ) >_errors.c
  216. $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out