1#! /bin/sh
2#
3# Copyright (c) 2001 Wichert Akkerman <wichert@cistron.nl>
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15#    derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28#	$Id$
29#
30
31# Validate arg count.
32case $# in
331)
34	dir="$1"
35	asm=asm
36	;;
372)
38	dir="$1"
39	asm="$2"
40	;;
41*)
42        echo "usage: $0 include-directory [asm-subdirectory]" >&2
43        exit 1
44	;;
45esac
46
47lookup_ioctls()
48{
49	type="$1"
50	shift
51
52	# Build the list of all ioctls
53	regexp='^[[:space:]]*#[[:space:]]*define[[:space:]]\+[A-Z][A-Z0-9_]*[[:space:]]\+0x'"$type"'..\>'
54	(cd "$dir" && grep "$regexp" "$@" /dev/null 2>/dev/null) |
55		sed -ne "s,$asm/,asm/,g"'
56s/^\(.*\):[[:space:]]*#[[:space:]]*define[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*\(0x'"$type"'..\).*/	{ "\1",	"\2",	\3	},/p' \
57		>> ioctls.h
58}
59
60
61> ioctls.h
62
63lookup_ioctls 03 linux/hdreg.h
64lookup_ioctls 22 scsi/sg.h
65lookup_ioctls 46 linux/fb.h
66lookup_ioctls 4B linux/kd.h
67lookup_ioctls 4C linux/loop.h
68lookup_ioctls 53 linux/cdrom.h scsi/scsi.h scsi/scsi_ioctl.h
69lookup_ioctls 54 $asm/ioctls.h asm-generic/ioctls.h
70lookup_ioctls 56 linux/vt.h
71lookup_ioctls '7[12]' linux/videotext.h
72lookup_ioctls 89 $asm/sockios.h asm-generic/sockios.h linux/sockios.h
73lookup_ioctls 8B linux/wireless.h
74
75if [ -e $dir/Kbuild ]; then
76	# kernel has exported user space headers, so query only them
77	files=$(
78		cd $dir || exit
79		find . -mindepth 2 -name Kbuild | \
80			sed -e 's:^\./::' -e 's:/Kbuild:/*:' | \
81			grep -v '^asm-'
82		echo "$asm/* asm-generic/*"
83	)
84	# special case: some headers aren't exported directly
85	files="${files} media/* net/bluetooth/* pcmcia/*"
86else
87	# older kernel so just assume some headers
88	files="linux/* $asm/* asm-generic/* scsi/* sound/*"
89fi
90
91# Build the list of all ioctls
92# Example output:
93# { "asm/ioctls.h",	"TIOCSWINSZ",	0x5414  },
94# { "asm/mce.h",	"MCE_GETCLEAR_FLAGS",	_IOC(_IOC_NONE,'M',3,0) },
95regexp='^[[:space:]]*#[[:space:]]*define[[:space:]]\+[A-Z][A-Z0-9_]*[[:space:]]\+_S\?\(IO\|IOW\|IOR\|IOWR\)\>'
96(cd $dir && grep $regexp $files 2>/dev/null) | \
97	sed -n \
98	-e "s,$asm/,asm/,g" \
99	-e 's/^\(.*\):[[:space:]]*#[[:space:]]*define[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*_S\?I.*(\([^[,]*\)[[:space:]]*,[[:space:]]*\([^,)]*\).*/	{ "\1",	"\2",	_IOC(_IOC_NONE,\3,\4,0)	},/p' \
100	>> ioctls.h
101
102# Sort and drop dups?
103# sort -u <ioctls.h >ioctls1.h && mv ioctls1.h ioctls.h
104
105
106> ioctldefs.h
107
108# Collect potential ioctl names. ('bases' is a bad name. Sigh)
109# Some use a special base to offset their ioctls on. Extract that as well.
110# Some use 2 defines: _IOC(_IOC_NONE,DM_IOCTL,DM_LIST_DEVICES_CMD,....)
111bases=$(sed -n \
112       -e 's/.*_IOC_NONE.*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]]*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]+,].*/\1\n\2/p' \
113       -e 's/.*_IOC_NONE.*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]+,].*/\1/p' \
114       ioctls.h | sort -u)
115
116for base in $bases; do
117	echo "Looking for $base"
118	regexp="^[[:space:]]*#[[:space:]]*define[[:space:]]\+$base"
119	line=$( (cd $dir && grep -h $regexp 2>/dev/null $files) | grep -v '\<_IO')
120	if [ x"$line" != x ]; then
121		echo "$base is a #define" # "($line)"
122		echo "$line" >> ioctldefs.h
123	fi
124
125	if ! grep "\<$base\>" ioctldefs.h >/dev/null 2>/dev/null; then
126		# Not all ioctl's are defines ... some (like the DM_* stuff)
127		# are enums, so we have to extract that crap ourself
128		(
129		cd $dir || exit
130		# -P: inhibit generation of linemarkers
131		${CPP:-cpp} -P $(grep -l $base $files 2>/dev/null) | sed '/^$/d' | \
132		awk -v base="$base" '{
133			if ($1 == "enum") {
134				val = 0
135				while ($NF != "};") {
136					if (!getline)
137						exit
138					gsub(/,/, "")
139					if ($0 ~ /=/)
140						val = $NF
141					if ($1 == base) {
142						print "#define " base " (" val ")"
143						exit
144					}
145					val++
146				}
147			}
148		}'
149		) >> ioctldefs.h
150		if ! grep "\<$base\>" ioctldefs.h >/dev/null 2>/dev/null; then
151			echo "Can't find the definition for $base"
152		else
153			echo "$base is an enum"
154		fi
155	fi
156done
157
158# Sort and drop dups?
159# sort -u <ioctldefs.h >ioctldefs1.h && mv ioctldefs1.h ioctldefs.h
160