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 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/epoll.h> |
| 26 | #include <linux/ptrace.h> |
| 27 | #include <linux/wait.h> |
| 28 | ' |
| 29 | |
| 30 | includes_Darwin=' |
| 31 | #define __DARWIN_UNIX03 0 |
| 32 | #define KERNEL |
| 33 | #define _DARWIN_USE_64_BIT_INODE |
| 34 | #include <sys/wait.h> |
| 35 | #include <sys/event.h> |
| 36 | ' |
| 37 | |
Devon H. O'Dell | 553be84 | 2009-11-14 15:29:09 -0800 | [diff] [blame] | 38 | includes_FreeBSD=' |
| 39 | #include <sys/wait.h> |
| 40 | #include <sys/event.h> |
| 41 | ' |
| 42 | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 43 | includes=' |
| 44 | #include <sys/types.h> |
| 45 | #include <fcntl.h> |
| 46 | #include <dirent.h> |
| 47 | #include <sys/socket.h> |
| 48 | #include <netinet/in.h> |
Russ Cox | 8fbe8be | 2010-03-30 14:32:59 -0700 | [diff] [blame] | 49 | #include <netinet/ip.h> |
| 50 | #include <netinet/ip6.h> |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 51 | #include <netinet/tcp.h> |
| 52 | #include <errno.h> |
| 53 | #include <sys/signal.h> |
| 54 | #include <signal.h> |
| 55 | ' |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 56 | |
Russ Cox | 7906e31 | 2010-04-29 23:34:06 -0700 | [diff] [blame] | 57 | ccflags="" |
| 58 | next=false |
| 59 | for i |
| 60 | do |
| 61 | if $next; then |
| 62 | ccflags="$ccflags $i" |
| 63 | next=false |
| 64 | elif [ "$i" = "-f" ]; then |
| 65 | next=true |
| 66 | fi |
| 67 | done |
| 68 | |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 69 | # Write godefs input. |
| 70 | ( |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 71 | indirect="includes_$(uname)" |
| 72 | echo "${!indirect} $includes" |
| 73 | echo |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 74 | echo 'enum {' |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 75 | |
| 76 | # The gcc command line prints all the #defines |
| 77 | # it encounters while processing the input |
Russ Cox | 7906e31 | 2010-04-29 23:34:06 -0700 | [diff] [blame] | 78 | echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags | |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 79 | awk ' |
| 80 | $1 != "#define" || $2 ~ /\(/ {next} |
| 81 | |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 82 | $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] | 83 | $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next} |
| 84 | |
| 85 | $2 ~ /^E[A-Z0-9_]+$/ || |
| 86 | $2 ~ /^SIG[^_]/ || |
Russ Cox | 8fbe8be | 2010-03-30 14:32:59 -0700 | [diff] [blame] | 87 | $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|EVFILT|EV|SHUT|PROT|MAP)_/ || |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 88 | $2 == "SOMAXCONN" || |
| 89 | $2 == "NAME_MAX" || |
| 90 | $2 ~ /^(O|F|FD|NAME|S|PTRACE)_/ || |
| 91 | $2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)} |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 92 | $2 ~ /^__WCOREFLAG$/ {next} |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 93 | $2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)} |
| 94 | |
| 95 | {next} |
| 96 | ' | sort |
| 97 | |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 98 | echo '};' |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 99 | ) >_const.c |
| 100 | |
| 101 | # Pull out just the error names for later. |
| 102 | errors=$( |
Russ Cox | 7906e31 | 2010-04-29 23:34:06 -0700 | [diff] [blame] | 103 | echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags | |
Russ Cox | 0b126c1 | 2009-12-01 16:53:43 -0800 | [diff] [blame] | 104 | awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' | |
| 105 | sort |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 106 | ) |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 107 | |
Rob Pike | cd32498 | 2009-08-13 13:22:37 -0700 | [diff] [blame] | 108 | echo '// mkerrors.sh' "$@" |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 109 | echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT' |
| 110 | echo |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 111 | godefs -c $GCC "$@" -gsyscall "$@" _const.c |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 112 | |
| 113 | # Run C program to print error strings. |
| 114 | ( |
Russ Cox | 9feee91 | 2009-08-24 11:03:23 -0700 | [diff] [blame] | 115 | /bin/echo " |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 116 | #include <stdio.h> |
| 117 | #include <errno.h> |
| 118 | #include <ctype.h> |
| 119 | #include <string.h> |
| 120 | |
| 121 | #define nelem(x) (sizeof(x)/sizeof((x)[0])) |
| 122 | |
| 123 | enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below |
| 124 | |
| 125 | int errors[] = { |
| 126 | " |
| 127 | for i in $errors |
| 128 | do |
Russ Cox | 9feee91 | 2009-08-24 11:03:23 -0700 | [diff] [blame] | 129 | /bin/echo ' '$i, |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 130 | done |
| 131 | |
Russ Cox | 9feee91 | 2009-08-24 11:03:23 -0700 | [diff] [blame] | 132 | # Use /bin/echo to avoid builtin echo, |
| 133 | # which interprets \n itself |
| 134 | /bin/echo ' |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | int |
| 138 | main(void) |
| 139 | { |
| 140 | int i, j, e; |
| 141 | char buf[1024]; |
| 142 | |
| 143 | printf("\n\n// Error table\n"); |
| 144 | printf("var errors = [...]string {\n"); |
| 145 | for(i=0; i<nelem(errors); i++) { |
| 146 | e = errors[i]; |
| 147 | for(j=0; j<i; j++) |
| 148 | if(errors[j] == e) // duplicate value |
| 149 | goto next; |
| 150 | strcpy(buf, strerror(e)); |
| 151 | // lowercase first letter: Bad -> bad, but STREAM -> STREAM. |
| 152 | if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z) |
| 153 | buf[0] += a - A; |
| 154 | printf("\t%d: \"%s\",\n", e, buf); |
| 155 | next:; |
| 156 | } |
| 157 | printf("}\n\n"); |
Russ Cox | fd1add2 | 2009-11-01 11:13:27 -0800 | [diff] [blame] | 158 | return 0; |
Russ Cox | 602a446 | 2009-06-01 22:14:57 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | ' |
| 162 | ) >_errors.c |
| 163 | |
Russ Cox | 23bf408 | 2010-05-03 11:11:01 -0700 | [diff] [blame] | 164 | $GCC $ccflags -static -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.c |