1#!/bin/sh
2# Copyright (c) 2004-2015 Dmitry V. Levin <ldv@altlinux.org>
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13# 3. The name of the author may not be used to endorse or promote products
14#    derived from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27set -efu
28
29me="${0##*/}"
30mydir="${0%/*}"
31msg()
32{
33	printf >&2 '%s\n' "$me: $*"
34}
35
36case $# in
37	1)
38		inc_dir="$1"
39		arch_dir=
40		;;
41	2)
42		inc_dir="$1"
43		arch_dir="$2"
44		;;
45	*)
46	        echo >&2 "usage: $me include-directory [arch-include-directory]"
47	        exit 1
48		;;
49esac
50
51# Check and canonicalize include-directory and arch-include-directory.
52abs_inc_dir="$(cd "$inc_dir" && pwd -P)"
53[ -z "$arch_dir" ] ||
54	abs_arch_dir="$(cd "$arch_dir" && pwd -P)"
55
56cleanup()
57{
58	trap - EXIT
59	rm -f ioctls_hex.h ioctls_sym.h
60	exit "$@"
61}
62trap 'cleanup $?' EXIT
63trap 'cleanup 1' HUP PIPE INT QUIT TERM
64
65# Fetch ioctl commands defined in hex form.
66{
67	"$mydir"/ioctls_hex.sh "$inc_dir" 03 linux/hdreg.h
68	"$mydir"/ioctls_hex.sh "$inc_dir" 22 scsi/sg.h
69	"$mydir"/ioctls_hex.sh "$inc_dir" 46 linux/fb.h
70	"$mydir"/ioctls_hex.sh "$inc_dir" 4B linux/kd.h
71	"$mydir"/ioctls_hex.sh "$inc_dir" 4C linux/loop.h
72	"$mydir"/ioctls_hex.sh "$inc_dir" 53 linux/cdrom.h scsi/scsi.h scsi/scsi_ioctl.h
73	"$mydir"/ioctls_hex.sh "$inc_dir" '\(46\|54\|66\|74\)' asm/ioctls.h asm-generic/ioctls.h
74	"$mydir"/ioctls_hex.sh "$inc_dir" 56 linux/vt.h
75	"$mydir"/ioctls_hex.sh "$inc_dir" '7[12]' linux/videotext.h
76	"$mydir"/ioctls_hex.sh "$inc_dir" 89 asm/sockios.h asm-generic/sockios.h linux/sockios.h
77	"$mydir"/ioctls_hex.sh "$inc_dir" 8B linux/wireless.h
78} > ioctls_hex.h
79msg "generated $(grep -c '^{' ioctls_hex.h) hex ioctls from $inc_dir"
80
81# Fetch ioctl commands defined in symbolic form.
82"$mydir"/ioctls_sym.sh "$inc_dir" > ioctls_sym.h
83
84# Part of android ioctl commands are defined elsewhere.
85android_dir="$inc_dir/../drivers/staging/android"
86if [ -d "$android_dir/uapi" ]; then
87	"$mydir"/ioctls_sym.sh "$android_dir" staging/android >> ioctls_sym.h
88fi
89msg "generated $(grep -c '^{' ioctls_sym.h) symbolic ioctls from $inc_dir"
90
91# Output all ioctl definitions fetched from include-directory.
92echo "/* Generated by $me from definitions found in ${inc_dir%%/}/ tree. */" > ioctls_inc.h
93LC_COLLATE=C sort -u ioctls_hex.h ioctls_sym.h >> ioctls_inc.h
94msg "generated $(grep -c '^{' ioctls_inc.h) ioctls from $inc_dir"
95
96[ -n "$arch_dir" ] || exit 0
97
98# Fetch ioctl commands defined in hex form.
99{
100	"$mydir"/ioctls_hex.sh "$arch_dir" 54 asm/ioctls.h
101	"$mydir"/ioctls_hex.sh "$arch_dir" '\(46\|54\|66\|74\)' asm/ioctls.h
102	"$mydir"/ioctls_hex.sh "$arch_dir" 89 asm/sockios.h
103} > ioctls_hex.h
104msg "generated $(grep -c '^{' ioctls_hex.h) hex ioctls from $arch_dir"
105
106# Fetch ioctl commands defined in symbolic form.
107INCLUDES="-I$abs_inc_dir/uapi -I$abs_inc_dir ${INCLUDES-}" \
108	"${0%/*}"/ioctls_sym.sh "$arch_dir" > ioctls_sym.h
109msg "generated $(grep -c '^{' ioctls_sym.h) symbolic ioctls from $arch_dir"
110
111# Output all ioctl definitions fetched from arch-include-directory.
112echo "/* Generated by $me from definitions found in ${arch_dir%%/}/ tree. */" > ioctls_arch.h
113LC_COLLATE=C sort -u ioctls_hex.h ioctls_sym.h >> ioctls_arch.h
114msg "generated $(grep -c '^{' ioctls_arch.h) ioctls from $arch_dir"
115