A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

mkerrors.sh 5.9KB

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