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