Devon H. O'Dell | 553be84 | 2009-11-14 15:29:09 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 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 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 6 | # Generate Go code listing errors and other #defined constant |
| 7 | # values (ENAMETOOLONG etc.), by asking the preprocessor |
| 8 | # about the definitions. |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 9 | |
Russ Cox | 0b126c1 | 2009-12-01 16:53:43 -0800 | [diff] [blame] | 10 | unset LANG |
| 11 | export LC_ALL=C |
| 12 | export LC_CTYPE=C |
| 13 | |
Kai Backman | 534dbc7 | 2010-08-23 13:25:14 +0300 | [diff] [blame] | 14 | GCC=gcc |
Kai Backman | e5c884f | 2009-10-01 06:55:01 -0700 | [diff] [blame] | 15 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 16 | uname=$(uname) |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 17 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 18 | includes_Linux=' |
| 19 | #define _LARGEFILE_SOURCE |
| 20 | #define _LARGEFILE64_SOURCE |
| 21 | #define _FILE_OFFSET_BITS 64 |
| 22 | #define _GNU_SOURCE |
| 23 | |
Mikio Hara | 1d550bd | 2011-05-18 16:33:41 -0700 | [diff] [blame] | 24 | #include <bits/sockaddr.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 25 | #include <sys/epoll.h> |
Balazs Lecz | 4bfcfcf | 2010-09-27 11:44:26 -0400 | [diff] [blame] | 26 | #include <sys/inotify.h> |
Albert Strasheim | 6442f38 | 2010-11-01 16:42:33 -0400 | [diff] [blame] | 27 | #include <sys/ioctl.h> |
Albert Strasheim | cf6c212 | 2010-12-07 13:40:14 -0500 | [diff] [blame] | 28 | #include <sys/mman.h> |
David Anderson | ad102e1 | 2011-03-09 05:58:47 -0800 | [diff] [blame] | 29 | #include <sys/mount.h> |
Albert Strasheim | cf6c212 | 2010-12-07 13:40:14 -0500 | [diff] [blame] | 30 | #include <sys/stat.h> |
Mikio Hara | 1d550bd | 2011-05-18 16:33:41 -0700 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <linux/if_addr.h> |
Mikio Hara | 10f1436 | 2011-06-20 14:51:34 -0400 | [diff] [blame^] | 33 | #include <linux/if_ether.h> |
Mikio Hara | 1d550bd | 2011-05-18 16:33:41 -0700 | [diff] [blame] | 34 | #include <linux/if_tun.h> |
Mikio Hara | 10f1436 | 2011-06-20 14:51:34 -0400 | [diff] [blame^] | 35 | #include <linux/filter.h> |
Mikio Hara | 1d550bd | 2011-05-18 16:33:41 -0700 | [diff] [blame] | 36 | #include <linux/netlink.h> |
| 37 | #include <linux/reboot.h> |
| 38 | #include <linux/rtnetlink.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 39 | #include <linux/ptrace.h> |
| 40 | #include <linux/wait.h> |
Mikio Hara | 6555cfc | 2010-12-09 13:55:59 -0500 | [diff] [blame] | 41 | #include <net/if.h> |
Mikio Hara | 4d11883 | 2011-05-26 17:04:58 -0400 | [diff] [blame] | 42 | #include <net/if_arp.h> |
Mikio Hara | b390c4b9d6 | 2010-10-12 09:48:56 -0400 | [diff] [blame] | 43 | #include <netpacket/packet.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 44 | ' |
| 45 | |
| 46 | includes_Darwin=' |
Russ Cox | acd858e | 2011-01-18 14:02:41 -0500 | [diff] [blame] | 47 | #define _DARWIN_C_SOURCE |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 48 | #define KERNEL |
| 49 | #define _DARWIN_USE_64_BIT_INODE |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 50 | #include <sys/types.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 51 | #include <sys/event.h> |
Jeff Hodges | 0ce57a7 | 2011-06-14 12:56:46 -0400 | [diff] [blame] | 52 | #include <sys/ptrace.h> |
Mikio Hara | 5c0aab9 | 2011-01-31 12:50:50 -0500 | [diff] [blame] | 53 | #include <sys/socket.h> |
| 54 | #include <sys/sockio.h> |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 55 | #include <sys/sysctl.h> |
Russ Cox | 48ae1f2 | 2011-04-06 17:52:02 -0400 | [diff] [blame] | 56 | #include <sys/mman.h> |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 57 | #include <sys/wait.h> |
Mikio Hara | 380f4ab | 2011-04-21 16:58:20 -0400 | [diff] [blame] | 58 | #include <net/bpf.h> |
Mikio Hara | 5c0aab9 | 2011-01-31 12:50:50 -0500 | [diff] [blame] | 59 | #include <net/if.h> |
Mikio Hara | 12376c9 | 2011-05-26 20:02:03 -0400 | [diff] [blame] | 60 | #include <net/if_types.h> |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 61 | #include <net/route.h> |
| 62 | #include <netinet/in.h> |
Mikio Hara | 5c0aab9 | 2011-01-31 12:50:50 -0500 | [diff] [blame] | 63 | #include <netinet/ip.h> |
| 64 | #include <netinet/ip_mroute.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 65 | ' |
| 66 | |
Devon H. O'Dell | 553be84 | 2009-11-14 15:29:09 -0800 | [diff] [blame] | 67 | includes_FreeBSD=' |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 68 | #include <sys/types.h> |
Devon H. O'Dell | 553be84 | 2009-11-14 15:29:09 -0800 | [diff] [blame] | 69 | #include <sys/event.h> |
Mikio Hara | 400ea84 | 2011-02-01 08:46:21 -0500 | [diff] [blame] | 70 | #include <sys/socket.h> |
| 71 | #include <sys/sockio.h> |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 72 | #include <sys/sysctl.h> |
| 73 | #include <sys/wait.h> |
Mikio Hara | 5a59b9e | 2011-04-04 15:40:40 -0400 | [diff] [blame] | 74 | #include <net/bpf.h> |
Mikio Hara | 400ea84 | 2011-02-01 08:46:21 -0500 | [diff] [blame] | 75 | #include <net/if.h> |
Mikio Hara | 12376c9 | 2011-05-26 20:02:03 -0400 | [diff] [blame] | 76 | #include <net/if_types.h> |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 77 | #include <net/route.h> |
Mikio Hara | 400ea84 | 2011-02-01 08:46:21 -0500 | [diff] [blame] | 78 | #include <netinet/in.h> |
| 79 | #include <netinet/ip.h> |
| 80 | #include <netinet/ip_mroute.h> |
Devon H. O'Dell | 553be84 | 2009-11-14 15:29:09 -0800 | [diff] [blame] | 81 | ' |
| 82 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 83 | includes=' |
| 84 | #include <sys/types.h> |
| 85 | #include <fcntl.h> |
| 86 | #include <dirent.h> |
| 87 | #include <sys/socket.h> |
| 88 | #include <netinet/in.h> |
Russ Cox | 8fbe8be | 2010-03-30 14:32:59 -0700 | [diff] [blame] | 89 | #include <netinet/ip.h> |
| 90 | #include <netinet/ip6.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 91 | #include <netinet/tcp.h> |
| 92 | #include <errno.h> |
| 93 | #include <sys/signal.h> |
| 94 | #include <signal.h> |
| 95 | ' |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 96 | |
Russ Cox | 7906e31 | 2010-04-29 23:34:06 -0700 | [diff] [blame] | 97 | ccflags="" |
| 98 | next=false |
| 99 | for i |
| 100 | do |
| 101 | if $next; then |
| 102 | ccflags="$ccflags $i" |
| 103 | next=false |
| 104 | elif [ "$i" = "-f" ]; then |
| 105 | next=true |
| 106 | fi |
| 107 | done |
| 108 | |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 109 | # Write godefs input. |
| 110 | ( |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 111 | indirect="includes_$(uname)" |
| 112 | echo "${!indirect} $includes" |
| 113 | echo |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 114 | echo 'enum {' |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 115 | |
| 116 | # The gcc command line prints all the #defines |
| 117 | # it encounters while processing the input |
Russ Cox | 7906e31 | 2010-04-29 23:34:06 -0700 | [diff] [blame] | 118 | echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 119 | awk ' |
| 120 | $1 != "#define" || $2 ~ /\(/ {next} |
Mikio Hara | 6555cfc | 2010-12-09 13:55:59 -0500 | [diff] [blame] | 121 | |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 122 | $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 123 | $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next} |
Albert Strasheim | cf6c212 | 2010-12-07 13:40:14 -0500 | [diff] [blame] | 124 | $2 ~ /^(SCM_SRCRT)$/ {next} |
| 125 | $2 ~ /^(MAP_FAILED)$/ {next} |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 126 | |
Mikio Hara | 6555cfc | 2010-12-09 13:55:59 -0500 | [diff] [blame] | 127 | $2 !~ /^ETH_/ && |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 128 | $2 !~ /^EPROC_/ && |
| 129 | $2 !~ /^EQUIV_/ && |
| 130 | $2 !~ /^EXPR_/ && |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 131 | $2 ~ /^E[A-Z0-9_]+$/ || |
| 132 | $2 ~ /^SIG[^_]/ || |
Balazs Lecz | 4bfcfcf | 2010-09-27 11:44:26 -0400 | [diff] [blame] | 133 | $2 ~ /^IN_/ || |
Albert Strasheim | c45a08e | 2011-04-06 16:19:22 -0400 | [diff] [blame] | 134 | $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|EVFILT|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV)_/ || |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 135 | $2 == "SOMAXCONN" || |
| 136 | $2 == "NAME_MAX" || |
Mikio Hara | 6555cfc | 2010-12-09 13:55:59 -0500 | [diff] [blame] | 137 | $2 == "IFNAMSIZ" || |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 138 | $2 == "CTL_NET" || |
| 139 | $2 == "CTL_MAXNAME" || |
David Anderson | ad102e1 | 2011-03-09 05:58:47 -0800 | [diff] [blame] | 140 | $2 ~ /^(MS|MNT)_/ || |
Mikio Hara | 6555cfc | 2010-12-09 13:55:59 -0500 | [diff] [blame] | 141 | $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ || |
Jeff Hodges | 0ce57a7 | 2011-06-14 12:56:46 -0400 | [diff] [blame] | 142 | $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ || |
David Anderson | 05660b7 | 2011-03-09 05:45:08 -0800 | [diff] [blame] | 143 | $2 ~ /^LINUX_REBOOT_CMD_/ || |
| 144 | $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ || |
Mikio Hara | 1d550bd | 2011-05-18 16:33:41 -0700 | [diff] [blame] | 145 | $2 !~ "NLA_TYPE_MASK" && |
Mikio Hara | 10f1436 | 2011-06-20 14:51:34 -0400 | [diff] [blame^] | 146 | $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|RTM|RTN|RTPROT|RTA|RTAX|RTNH|ARPHRD|ETH_P)_/ || |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 147 | $2 ~ /^SIOC/ || |
Mikio Hara | 12376c9 | 2011-05-26 20:02:03 -0400 | [diff] [blame] | 148 | $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ || |
Mikio Hara | 5a59b9e | 2011-04-04 15:40:40 -0400 | [diff] [blame] | 149 | $2 ~ /^BIOC/ || |
Mikio Hara | 380f4ab | 2011-04-21 16:58:20 -0400 | [diff] [blame] | 150 | $2 !~ /^(BPF_TIMEVAL)$/ && |
Mikio Hara | 5a59b9e | 2011-04-04 15:40:40 -0400 | [diff] [blame] | 151 | $2 ~ /^(BPF|DLT)_/ || |
Mikio Hara | 9c97af9 | 2011-02-11 14:34:00 -0500 | [diff] [blame] | 152 | $2 !~ "WMESGLEN" && |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 153 | $2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)} |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 154 | $2 ~ /^__WCOREFLAG$/ {next} |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 155 | $2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)} |
Mikio Hara | 6555cfc | 2010-12-09 13:55:59 -0500 | [diff] [blame] | 156 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 157 | {next} |
| 158 | ' | sort |
| 159 | |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 160 | echo '};' |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 161 | ) >_const.c |
| 162 | |
| 163 | # Pull out just the error names for later. |
| 164 | errors=$( |
Russ Cox | 7906e31 | 2010-04-29 23:34:06 -0700 | [diff] [blame] | 165 | echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags | |
Russ Cox | 0b126c1 | 2009-12-01 16:53:43 -0800 | [diff] [blame] | 166 | awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' | |
| 167 | sort |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 168 | ) |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 169 | |
Rob Pike | cd32498 | 2009-08-13 13:22:37 -0700 | [diff] [blame] | 170 | echo '// mkerrors.sh' "$@" |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 171 | echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT' |
| 172 | echo |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 173 | godefs -c $GCC "$@" -gsyscall "$@" _const.c |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 174 | |
| 175 | # Run C program to print error strings. |
| 176 | ( |
Russ Cox | 9feee91 | 2009-08-24 11:03:23 -0700 | [diff] [blame] | 177 | /bin/echo " |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 178 | #include <stdio.h> |
| 179 | #include <errno.h> |
| 180 | #include <ctype.h> |
| 181 | #include <string.h> |
| 182 | |
| 183 | #define nelem(x) (sizeof(x)/sizeof((x)[0])) |
| 184 | |
| 185 | enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below |
| 186 | |
| 187 | int errors[] = { |
| 188 | " |
| 189 | for i in $errors |
| 190 | do |
Russ Cox | 9feee91 | 2009-08-24 11:03:23 -0700 | [diff] [blame] | 191 | /bin/echo ' '$i, |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 192 | done |
| 193 | |
Russ Cox | 9feee91 | 2009-08-24 11:03:23 -0700 | [diff] [blame] | 194 | # Use /bin/echo to avoid builtin echo, |
| 195 | # which interprets \n itself |
| 196 | /bin/echo ' |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Russ Cox | 9a96fb3 | 2010-09-24 13:37:02 -0400 | [diff] [blame] | 199 | static int |
| 200 | intcmp(const void *a, const void *b) |
| 201 | { |
| 202 | return *(int*)a - *(int*)b; |
| 203 | } |
| 204 | |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 205 | int |
| 206 | main(void) |
| 207 | { |
| 208 | int i, j, e; |
| 209 | char buf[1024]; |
| 210 | |
| 211 | printf("\n\n// Error table\n"); |
| 212 | printf("var errors = [...]string {\n"); |
Russ Cox | 9a96fb3 | 2010-09-24 13:37:02 -0400 | [diff] [blame] | 213 | qsort(errors, nelem(errors), sizeof errors[0], intcmp); |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 214 | for(i=0; i<nelem(errors); i++) { |
| 215 | e = errors[i]; |
Russ Cox | 9a96fb3 | 2010-09-24 13:37:02 -0400 | [diff] [blame] | 216 | if(i > 0 && errors[i-1] == e) |
| 217 | continue; |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 218 | strcpy(buf, strerror(e)); |
| 219 | // lowercase first letter: Bad -> bad, but STREAM -> STREAM. |
| 220 | if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z) |
| 221 | buf[0] += a - A; |
| 222 | printf("\t%d: \"%s\",\n", e, buf); |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 223 | } |
| 224 | printf("}\n\n"); |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 225 | return 0; |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ' |
| 229 | ) >_errors.c |
| 230 | |
Russ Cox | 9a96fb3 | 2010-09-24 13:37:02 -0400 | [diff] [blame] | 231 | $GCC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.c |