1#!/bin/sh
2#
3
4set -e
5
6if [ $# -ne 1 ]; then
7	echo "usage: sh $0 include-dir"
8	exit 1
9fi
10
11includedir="$1"
12
13LC_ALL=C; export LC_ALL
14
15# Build a list of headers that have ioctls in them.
16# XXX should we use an ANSI cpp?
17ioctl_includes=$(
18	cd $includedir
19	set -e
20	# if /bin/sh is bash this will avoid further errors due to missing commands
21	if set -o | grep -q pipefail; then
22		set -o pipefail
23	fi
24
25	filter='tee'
26	if [ "${MK_PF}" = "no" ]; then
27		filter='egrep -v (net/pfvar|net/if_pfsync)\.h'
28	fi
29	# find -s would avoid the need to invoke sort but it is non-portable
30	find -L ./* -type f -name '*.h' | \
31		LC_ALL=C sort | \
32		$filter | \
33		xargs egrep -l \
34'^#[ 	]*define[ 	]+[A-Za-z_][A-Za-z0-9_]*[ 	]+_IO[^a-z0-9_]' |
35		awk '{printf("#include <%s>\\n", $1)}'
36)
37
38if [ -z "$ioctl_includes" ]; then
39	echo "Failed to build list of ioctl headers"
40	exit 1
41fi
42
43awk -v x="$ioctl_includes" 'BEGIN {print x}' |
44	$CPP -nostdinc -I$includedir -dM -DCOMPAT_43TTY - |
45	awk -v ioctl_includes="$ioctl_includes" '
46BEGIN {
47	print "/* XXX obnoxious prerequisites. */"
48	print "#define COMPAT_43"
49	print "#define COMPAT_43TTY"
50	print "#include <sys/param.h>"
51	print "#include <sys/devicestat.h>"
52	print "#include <sys/disklabel.h>"
53	print "#include <sys/mman.h>"
54	print "#include <sys/socket.h>"
55	print "#include <sys/time.h>"
56	print "#include <sys/tty.h>"
57	print "#include <bsm/audit.h>"
58	print "#include <net/ethernet.h>"
59	print "#include <net/if.h>"
60	print "#ifdef PF"
61	print "#include <net/pfvar.h>"
62	print "#include <net/if_pfsync.h>"
63	print "#endif"
64	print "#include <net/route.h>"
65	print "#include <netinet/in.h>"
66	print "#include <netinet/ip_mroute.h>"
67	print "#include <netinet6/in6_var.h>"
68	print "#include <netinet6/nd6.h>"
69	print "#include <netinet6/ip6_mroute.h>"
70	print "#include <stdio.h>"
71	print "#include <cam/cam.h>"
72	print "#include <cam/scsi/scsi_pass.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