initial commit
[govpp.git] / vendor / golang.org / x / sys / unix / mkerrors.sh
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
6 # Generate Go code listing errors and other #defined constant
7 # values (ENAMETOOLONG etc.), by asking the preprocessor
8 # about the definitions.
9
10 unset LANG
11 export LC_ALL=C
12 export LC_CTYPE=C
13
14 if test -z "$GOARCH" -o -z "$GOOS"; then
15         echo 1>&2 "GOARCH or GOOS not defined in environment"
16         exit 1
17 fi
18
19 # Check that we are using the new build system if we should
20 if [[ "$GOOS" -eq "linux" ]] && [[ "$GOARCH" != "sparc64" ]]; then
21         if [[ "$GOLANG_SYS_BUILD" -ne "docker" ]]; then
22                 echo 1>&2 "In the new build system, mkerrors should not be called directly."
23                 echo 1>&2 "See README.md"
24                 exit 1
25         fi
26 fi
27
28 CC=${CC:-cc}
29
30 if [[ "$GOOS" -eq "solaris" ]]; then
31         # Assumes GNU versions of utilities in PATH.
32         export PATH=/usr/gnu/bin:$PATH
33 fi
34
35 uname=$(uname)
36
37 includes_Darwin='
38 #define _DARWIN_C_SOURCE
39 #define KERNEL
40 #define _DARWIN_USE_64_BIT_INODE
41 #include <sys/types.h>
42 #include <sys/event.h>
43 #include <sys/ptrace.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
47 #include <sys/mman.h>
48 #include <sys/wait.h>
49 #include <net/bpf.h>
50 #include <net/if.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <termios.h>
56 '
57
58 includes_DragonFly='
59 #include <sys/types.h>
60 #include <sys/event.h>
61 #include <sys/socket.h>
62 #include <sys/sockio.h>
63 #include <sys/sysctl.h>
64 #include <sys/mman.h>
65 #include <sys/wait.h>
66 #include <sys/ioctl.h>
67 #include <net/bpf.h>
68 #include <net/if.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <termios.h>
73 #include <netinet/ip.h>
74 #include <net/ip_mroute/ip_mroute.h>
75 '
76
77 includes_FreeBSD='
78 #include <sys/param.h>
79 #include <sys/types.h>
80 #include <sys/event.h>
81 #include <sys/socket.h>
82 #include <sys/sockio.h>
83 #include <sys/sysctl.h>
84 #include <sys/mman.h>
85 #include <sys/wait.h>
86 #include <sys/ioctl.h>
87 #include <net/bpf.h>
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <termios.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_mroute.h>
95 #include <sys/extattr.h>
96
97 #if __FreeBSD__ >= 10
98 #define IFT_CARP        0xf8    // IFT_CARP is deprecated in FreeBSD 10
99 #undef SIOCAIFADDR
100 #define SIOCAIFADDR     _IOW(105, 26, struct oifaliasreq)       // ifaliasreq contains if_data
101 #undef SIOCSIFPHYADDR
102 #define SIOCSIFPHYADDR  _IOW(105, 70, struct oifaliasreq)       // ifaliasreq contains if_data
103 #endif
104 '
105
106 includes_Linux='
107 #define _LARGEFILE_SOURCE
108 #define _LARGEFILE64_SOURCE
109 #ifndef __LP64__
110 #define _FILE_OFFSET_BITS 64
111 #endif
112 #define _GNU_SOURCE
113
114 // <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
115 // these structures. We just include them copied from <bits/termios.h>.
116 #if defined(__powerpc__)
117 struct sgttyb {
118         char    sg_ispeed;
119         char    sg_ospeed;
120         char    sg_erase;
121         char    sg_kill;
122         short   sg_flags;
123 };
124
125 struct tchars {
126         char    t_intrc;
127         char    t_quitc;
128         char    t_startc;
129         char    t_stopc;
130         char    t_eofc;
131         char    t_brkc;
132 };
133
134 struct ltchars {
135         char    t_suspc;
136         char    t_dsuspc;
137         char    t_rprntc;
138         char    t_flushc;
139         char    t_werasc;
140         char    t_lnextc;
141 };
142 #endif
143
144 #include <bits/sockaddr.h>
145 #include <sys/epoll.h>
146 #include <sys/inotify.h>
147 #include <sys/ioctl.h>
148 #include <sys/mman.h>
149 #include <sys/mount.h>
150 #include <sys/prctl.h>
151 #include <sys/stat.h>
152 #include <sys/types.h>
153 #include <sys/time.h>
154 #include <sys/socket.h>
155 #include <linux/if.h>
156 #include <linux/if_alg.h>
157 #include <linux/if_arp.h>
158 #include <linux/if_ether.h>
159 #include <linux/if_tun.h>
160 #include <linux/if_packet.h>
161 #include <linux/if_addr.h>
162 #include <linux/falloc.h>
163 #include <linux/filter.h>
164 #include <linux/fs.h>
165 #include <linux/netlink.h>
166 #include <linux/random.h>
167 #include <linux/reboot.h>
168 #include <linux/rtnetlink.h>
169 #include <linux/ptrace.h>
170 #include <linux/sched.h>
171 #include <linux/wait.h>
172 #include <linux/icmpv6.h>
173 #include <linux/serial.h>
174 #include <linux/can.h>
175 #include <linux/vm_sockets.h>
176 #include <net/route.h>
177 #include <asm/termbits.h>
178
179 #ifndef MSG_FASTOPEN
180 #define MSG_FASTOPEN    0x20000000
181 #endif
182
183 #ifndef PTRACE_GETREGS
184 #define PTRACE_GETREGS  0xc
185 #endif
186
187 #ifndef PTRACE_SETREGS
188 #define PTRACE_SETREGS  0xd
189 #endif
190
191 #ifndef SOL_NETLINK
192 #define SOL_NETLINK     270
193 #endif
194
195 #ifdef SOL_BLUETOOTH
196 // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
197 // but it is already in bluetooth_linux.go
198 #undef SOL_BLUETOOTH
199 #endif
200
201 // Certain constants are missing from the fs/crypto UAPI
202 #define FS_KEY_DESC_PREFIX              "fscrypt:"
203 #define FS_KEY_DESC_PREFIX_SIZE         8
204 #define FS_MAX_KEY_SIZE                 64
205 '
206
207 includes_NetBSD='
208 #include <sys/types.h>
209 #include <sys/param.h>
210 #include <sys/event.h>
211 #include <sys/mman.h>
212 #include <sys/socket.h>
213 #include <sys/sockio.h>
214 #include <sys/sysctl.h>
215 #include <sys/termios.h>
216 #include <sys/ttycom.h>
217 #include <sys/wait.h>
218 #include <net/bpf.h>
219 #include <net/if.h>
220 #include <net/if_types.h>
221 #include <net/route.h>
222 #include <netinet/in.h>
223 #include <netinet/in_systm.h>
224 #include <netinet/ip.h>
225 #include <netinet/ip_mroute.h>
226 #include <netinet/if_ether.h>
227
228 // Needed since <sys/param.h> refers to it...
229 #define schedppq 1
230 '
231
232 includes_OpenBSD='
233 #include <sys/types.h>
234 #include <sys/param.h>
235 #include <sys/event.h>
236 #include <sys/mman.h>
237 #include <sys/socket.h>
238 #include <sys/sockio.h>
239 #include <sys/sysctl.h>
240 #include <sys/termios.h>
241 #include <sys/ttycom.h>
242 #include <sys/wait.h>
243 #include <net/bpf.h>
244 #include <net/if.h>
245 #include <net/if_types.h>
246 #include <net/if_var.h>
247 #include <net/route.h>
248 #include <netinet/in.h>
249 #include <netinet/in_systm.h>
250 #include <netinet/ip.h>
251 #include <netinet/ip_mroute.h>
252 #include <netinet/if_ether.h>
253 #include <net/if_bridge.h>
254
255 // We keep some constants not supported in OpenBSD 5.5 and beyond for
256 // the promise of compatibility.
257 #define EMUL_ENABLED            0x1
258 #define EMUL_NATIVE             0x2
259 #define IPV6_FAITH              0x1d
260 #define IPV6_OPTIONS            0x1
261 #define IPV6_RTHDR_STRICT       0x1
262 #define IPV6_SOCKOPT_RESERVED1  0x3
263 #define SIOCGIFGENERIC          0xc020693a
264 #define SIOCSIFGENERIC          0x80206939
265 #define WALTSIG                 0x4
266 '
267
268 includes_SunOS='
269 #include <limits.h>
270 #include <sys/types.h>
271 #include <sys/socket.h>
272 #include <sys/sockio.h>
273 #include <sys/mman.h>
274 #include <sys/wait.h>
275 #include <sys/ioctl.h>
276 #include <net/bpf.h>
277 #include <net/if.h>
278 #include <net/if_arp.h>
279 #include <net/if_types.h>
280 #include <net/route.h>
281 #include <netinet/in.h>
282 #include <termios.h>
283 #include <netinet/ip.h>
284 #include <netinet/ip_mroute.h>
285 '
286
287
288 includes='
289 #include <sys/types.h>
290 #include <sys/file.h>
291 #include <fcntl.h>
292 #include <dirent.h>
293 #include <sys/socket.h>
294 #include <netinet/in.h>
295 #include <netinet/ip.h>
296 #include <netinet/ip6.h>
297 #include <netinet/tcp.h>
298 #include <errno.h>
299 #include <sys/signal.h>
300 #include <signal.h>
301 #include <sys/resource.h>
302 #include <time.h>
303 '
304 ccflags="$@"
305
306 # Write go tool cgo -godefs input.
307 (
308         echo package unix
309         echo
310         echo '/*'
311         indirect="includes_$(uname)"
312         echo "${!indirect} $includes"
313         echo '*/'
314         echo 'import "C"'
315         echo 'import "syscall"'
316         echo
317         echo 'const ('
318
319         # The gcc command line prints all the #defines
320         # it encounters while processing the input
321         echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
322         awk '
323                 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
324
325                 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next}  # 386 registers
326                 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
327                 $2 ~ /^(SCM_SRCRT)$/ {next}
328                 $2 ~ /^(MAP_FAILED)$/ {next}
329                 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
330
331                 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
332                 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
333
334                 $2 !~ /^ETH_/ &&
335                 $2 !~ /^EPROC_/ &&
336                 $2 !~ /^EQUIV_/ &&
337                 $2 !~ /^EXPR_/ &&
338                 $2 ~ /^E[A-Z0-9_]+$/ ||
339                 $2 ~ /^B[0-9_]+$/ ||
340                 $2 == "BOTHER" ||
341                 $2 ~ /^CI?BAUD(EX)?$/ ||
342                 $2 == "IBSHIFT" ||
343                 $2 ~ /^V[A-Z0-9]+$/ ||
344                 $2 ~ /^CS[A-Z0-9]/ ||
345                 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
346                 $2 ~ /^IGN/ ||
347                 $2 ~ /^IX(ON|ANY|OFF)$/ ||
348                 $2 ~ /^IN(LCR|PCK)$/ ||
349                 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
350                 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
351                 $2 == "BRKINT" ||
352                 $2 == "HUPCL" ||
353                 $2 == "PENDIN" ||
354                 $2 == "TOSTOP" ||
355                 $2 == "XCASE" ||
356                 $2 == "ALTWERASE" ||
357                 $2 == "NOKERNINFO" ||
358                 $2 ~ /^PAR/ ||
359                 $2 ~ /^SIG[^_]/ ||
360                 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
361                 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
362                 $2 ~ /^(NL|CR|TAB|BS|VT|FF)[0-9]$/ ||
363                 $2 ~ /^O?XTABS$/ ||
364                 $2 ~ /^TC[IO](ON|OFF)$/ ||
365                 $2 ~ /^IN_/ ||
366                 $2 ~ /^LOCK_(SH|EX|NB|UN)$/ ||
367                 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|ICMP6|TCP|EVFILT|NOTE|EV|SHUT|PROT|MAP|PACKET|MSG|SCM|MCL|DT|MADV|PR)_/ ||
368                 $2 ~ /^FALLOC_/ ||
369                 $2 == "ICMPV6_FILTER" ||
370                 $2 == "SOMAXCONN" ||
371                 $2 == "NAME_MAX" ||
372                 $2 == "IFNAMSIZ" ||
373                 $2 ~ /^CTL_(MAXNAME|NET|QUERY)$/ ||
374                 $2 ~ /^SYSCTL_VERS/ ||
375                 $2 ~ /^(MS|MNT)_/ ||
376                 $2 ~ /^TUN(SET|GET|ATTACH|DETACH)/ ||
377                 $2 ~ /^(O|F|FD|NAME|S|PTRACE|PT)_/ ||
378                 $2 ~ /^LINUX_REBOOT_CMD_/ ||
379                 $2 ~ /^LINUX_REBOOT_MAGIC[12]$/ ||
380                 $2 !~ "NLA_TYPE_MASK" &&
381                 $2 ~ /^(NETLINK|NLM|NLMSG|NLA|IFA|IFAN|RT|RTCF|RTN|RTPROT|RTNH|ARPHRD|ETH_P)_/ ||
382                 $2 ~ /^SIOC/ ||
383                 $2 ~ /^TIOC/ ||
384                 $2 ~ /^TCGET/ ||
385                 $2 ~ /^TCSET/ ||
386                 $2 ~ /^TC(FLSH|SBRKP?|XONC)$/ ||
387                 $2 !~ "RTF_BITS" &&
388                 $2 ~ /^(IFF|IFT|NET_RT|RTM|RTF|RTV|RTA|RTAX)_/ ||
389                 $2 ~ /^BIOC/ ||
390                 $2 ~ /^RUSAGE_(SELF|CHILDREN|THREAD)/ ||
391                 $2 ~ /^RLIMIT_(AS|CORE|CPU|DATA|FSIZE|NOFILE|STACK)|RLIM_INFINITY/ ||
392                 $2 ~ /^PRIO_(PROCESS|PGRP|USER)/ ||
393                 $2 ~ /^CLONE_[A-Z_]+/ ||
394                 $2 !~ /^(BPF_TIMEVAL)$/ &&
395                 $2 ~ /^(BPF|DLT)_/ ||
396                 $2 ~ /^CLOCK_/ ||
397                 $2 ~ /^CAN_/ ||
398                 $2 ~ /^ALG_/ ||
399                 $2 ~ /^FS_(POLICY_FLAGS|KEY_DESC|ENCRYPTION_MODE|[A-Z0-9_]+_KEY_SIZE|IOC_(GET|SET)_ENCRYPTION)/ ||
400                 $2 ~ /^GRND_/ ||
401                 $2 ~ /^SPLICE_/ ||
402                 $2 ~ /^(VM|VMADDR)_/ ||
403                 $2 !~ "WMESGLEN" &&
404                 $2 ~ /^W[A-Z0-9]+$/ ||
405                 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
406                 $2 ~ /^__WCOREFLAG$/ {next}
407                 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
408
409                 {next}
410         ' | sort
411
412         echo ')'
413 ) >_const.go
414
415 # Pull out the error names for later.
416 errors=$(
417         echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
418         awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
419         sort
420 )
421
422 # Pull out the signal names for later.
423 signals=$(
424         echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
425         awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
426         egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
427         sort
428 )
429
430 # Again, writing regexps to a file.
431 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
432         awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
433         sort >_error.grep
434 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
435         awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
436         egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
437         sort >_signal.grep
438
439 echo '// mkerrors.sh' "$@"
440 echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
441 echo
442 echo "// +build ${GOARCH},${GOOS}"
443 echo
444 go tool cgo -godefs -- "$@" _const.go >_error.out
445 cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
446 echo
447 echo '// Errors'
448 echo 'const ('
449 cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
450 echo ')'
451
452 echo
453 echo '// Signals'
454 echo 'const ('
455 cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
456 echo ')'
457
458 # Run C program to print error and syscall strings.
459 (
460         echo -E "
461 #include <stdio.h>
462 #include <stdlib.h>
463 #include <errno.h>
464 #include <ctype.h>
465 #include <string.h>
466 #include <signal.h>
467
468 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
469
470 enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
471
472 int errors[] = {
473 "
474         for i in $errors
475         do
476                 echo -E '       '$i,
477         done
478
479         echo -E "
480 };
481
482 int signals[] = {
483 "
484         for i in $signals
485         do
486                 echo -E '       '$i,
487         done
488
489         # Use -E because on some systems bash builtin interprets \n itself.
490         echo -E '
491 };
492
493 static int
494 intcmp(const void *a, const void *b)
495 {
496         return *(int*)a - *(int*)b;
497 }
498
499 int
500 main(void)
501 {
502         int i, e;
503         char buf[1024], *p;
504
505         printf("\n\n// Error table\n");
506         printf("var errors = [...]string {\n");
507         qsort(errors, nelem(errors), sizeof errors[0], intcmp);
508         for(i=0; i<nelem(errors); i++) {
509                 e = errors[i];
510                 if(i > 0 && errors[i-1] == e)
511                         continue;
512                 strcpy(buf, strerror(e));
513                 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
514                 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
515                         buf[0] += a - A;
516                 printf("\t%d: \"%s\",\n", e, buf);
517         }
518         printf("}\n\n");
519
520         printf("\n\n// Signal table\n");
521         printf("var signals = [...]string {\n");
522         qsort(signals, nelem(signals), sizeof signals[0], intcmp);
523         for(i=0; i<nelem(signals); i++) {
524                 e = signals[i];
525                 if(i > 0 && signals[i-1] == e)
526                         continue;
527                 strcpy(buf, strsignal(e));
528                 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
529                 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
530                         buf[0] += a - A;
531                 // cut trailing : number.
532                 p = strrchr(buf, ":"[0]);
533                 if(p)
534                         *p = '\0';
535                 printf("\t%d: \"%s\",\n", e, buf);
536         }
537         printf("}\n\n");
538
539         return 0;
540 }
541
542 '
543 ) >_errors.c
544
545 $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out