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