xref: /freebsd/lib/libsysdecode/mkioctls (revision cddbc3b40812213ff00041f79174cac0be360a2a)
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/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 <stdbool.h>"
73	print "#include <stddef.h>"
74	print "#include <stdint.h>"
75	print "#include <sysdecode.h>"
76	print ""
77	print ioctl_includes
78	print ""
79	print "const char *"
80	print "sysdecode_ioctlname(unsigned long val)"
81	print "{"
82	print "\tconst char *str = NULL;"
83	print ""
84}
85
86/^#[ 	]*define[ 	]+[A-Za-z_][A-Za-z0-9_]*[ 	]+_IO/ {
87
88	# find where the name starts
89	for (i = 1; i <= NF; i++)
90		if ($i ~ /define/)
91			break;
92	++i;
93	#
94	printf("\t");
95	if (n++ > 0)
96		printf("else ");
97	printf("if (val == %s)\n", $i);
98	printf("\t\tstr = \"%s\";\n", $i);
99}
100END {
101	print ""
102	print "\treturn (str);"
103	print "}"
104}
105'
106