summaryrefslogtreecommitdiff
path: root/src/lib/syscall/mkerrors
blob: 015f0214577bb761abd79b1711741e0660a1232d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.


# Generate Go code listing error values (ENAMETOOLONG etc)
# and signal values (SIGALRM etc).  They're unrelated except
# that we use the same method for finding them.

errors=$(
	echo '#include <errno.h>' |
	# The gcc command line prints all the #defines
	# it encounters while processing the input
	gcc -x c - -E -dM |
	egrep -h '#define E[A-Z0-9_]+ ' $files |
	sed 's/#define //; s/ .*//'
)

signals=$(
	echo '#include <sys/signal.h>' |
	gcc -x c - -E -dM |
	egrep -h '#define SIG[^_]' |
	egrep -v '#define (SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))' |
	sed 's/#define //; s/ .*//'
)

# Write godefs input.
(
	echo '#include <errno.h>'
	echo '#include <signal.h>'
	echo 'enum {'
	for i in $errors $signals
	do
		echo '$'"$i = $i,"
	done
	echo '};'
) >_errors.c

echo '// mkerrors' "$@"
echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
echo
godefs -gsyscall "$@" _errors.c

# Run C program to print error strings.
(
	echo "
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>

#define nelem(x) (sizeof(x)/sizeof((x)[0]))

enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below

int errors[] = {
"
	for i in $errors
	do
		echo '	'$i,
	done

	echo '
};

int
main(void)
{
	int i, j, e;
	char buf[1024];

	printf("\n\n// Error table\n");
	printf("var errors = [...]string {\n");
	for(i=0; i<nelem(errors); i++) {
		e = errors[i];
		for(j=0; j<i; j++)
			if(errors[j] == e)	// duplicate value
				goto next;
		strcpy(buf, strerror(e));
		// lowercase first letter: Bad -> bad, but STREAM -> STREAM.
		if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
			buf[0] += a - A;
		printf("\t%d: \"%s\",\n", e, buf);
	next:;
	}
	printf("}\n\n");
}

'
) >_errors.c

gcc -o _errors _errors.c && ./_errors
rm -f _errors.c _errors