1#!/bin/sh
2#
3# $FreeBSD$
4
5set -e
6
7if [ $# -ne 1 ]; then
8	echo "usage: sh $0 include-dir"
9	exit 1
10fi
11
12includedir="$1"
13
14LC_ALL=C; export LC_ALL
15
16# Build a list of headers that have ioctls in them.
17# XXX should we use an ANSI cpp?
18ioctl_includes=$(
19	cd $includedir
20	set -e
21	# if /bin/sh is bash this will avoid further errors due to missing commands
22	if set -o | grep -q pipefail; then
23		set -o pipefail
24	fi
25
26	filter='tee'
27	if [ "${MK_PF}" = "no" ]; then
28		filter='egrep -v (net/pfvar|net/if_pfsync)\.h'
29	fi
30	# find -s would avoid the need to invoke sort but it is non-portable
31	find -L ./* -type f -name '*.h' | \
32		LC_ALL=C sort | \
33		$filter | \
34		xargs egrep -l \
35'^#[ 	]*define[ 	]+[A-Za-z_][A-Za-z0-9_]*[ 	]+_IO[^a-z0-9_]' |
36		awk '{printf("#include <%s>\\n", $1)}'
37)
38
39if [ -z "$ioctl_includes" ]; then
40	echo "Failed to build list of ioctl headers"
41	exit 1
42fi
43
44awk -v x="$ioctl_includes" 'BEGIN {print x}' |
45	$CPP -nostdinc -I$includedir -dM -DCOMPAT_43TTY - |
46	awk -v ioctl_includes="$ioctl_includes" '
47BEGIN {
48	print "/* XXX obnoxious prerequisites. */"
49	print "#define COMPAT_43"
50	print "#define COMPAT_43TTY"
51	print "#include <sys/param.h>"
52	print "#include <sys/devicestat.h>"
53	print "#include <sys/disklabel.h>"
54	print "#include <sys/mman.h>"
55	print "#include <sys/socket.h>"
56	print "#include <sys/time.h>"
57	print "#include <sys/tty.h>"
58	print "#include <bsm/audit.h>"
59	print "#include <net/ethernet.h>"
60	print "#include <net/if.h>"
61	print "#ifdef PF"
62	print "#include <net/pfvar.h>"
63	print "#include <net/if_pfsync.h>"
64	print "#endif"
65	print "#include <net/route.h>"
66	print "#include <netinet/in.h>"
67	print "#include <netinet/ip_mroute.h>"
68	print "#include <netinet6/in6_var.h>"
69	print "#include <netinet6/nd6.h>"
70	print "#include <netinet6/ip6_mroute.h>"
71	print "#include <stdio.h>"
72	print "#include <cam/cam.h>"
73	print "#include <stdbool.h>"
74	print "#include <stddef.h>"
75	print "#include <stdint.h>"
76	print "#include <sysdecode.h>"
77	print ""
78	print ioctl_includes
79	print ""
80	print "const char *"
81	print "sysdecode_ioctlname(unsigned long val)"
82	print "{"
83	print "\tconst char *str = NULL;"
84	print ""
85}
86
87/^#[ 	]*define[ 	]+[A-Za-z_][A-Za-z0-9_]*[ 	]+_IO/ {
88
89	# find where the name starts
90	for (i = 1; i <= NF; i++)
91		if ($i ~ /define/)
92			break;
93	++i;
94	#
95	printf("\t");
96	if (n++ > 0)
97		printf("else ");
98	printf("if (val == %s)\n", $i);
99	printf("\t\tstr = \"%s\";\n", $i);
100}
101END {
102	print ""
103	print "\treturn (str);"
104	print "}"
105}
106'
107