xref: /freebsd/lib/libsysdecode/mklinuxtables (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1b69ae1a3SDmitry Chagin#!/bin/sh
2b69ae1a3SDmitry Chagin#
3b69ae1a3SDmitry Chagin# Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
4b69ae1a3SDmitry Chagin#
5b69ae1a3SDmitry Chagin# Redistribution and use in source and binary forms, with or without
6b69ae1a3SDmitry Chagin# modification, are permitted provided that the following conditions
7b69ae1a3SDmitry Chagin# are met:
8b69ae1a3SDmitry Chagin# 1. Redistributions of source code must retain the above copyright
9b69ae1a3SDmitry Chagin#    notice, this list of conditions and the following disclaimer.
10b69ae1a3SDmitry Chagin# 2. Redistributions in binary form must reproduce the above copyright
11b69ae1a3SDmitry Chagin#    notice, this list of conditions and the following disclaimer in the
12b69ae1a3SDmitry Chagin#    documentation and/or other materials provided with the distribution.
13b69ae1a3SDmitry Chagin#
14b69ae1a3SDmitry Chagin# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15b69ae1a3SDmitry Chagin# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16b69ae1a3SDmitry Chagin# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b69ae1a3SDmitry Chagin# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18b69ae1a3SDmitry Chagin# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19b69ae1a3SDmitry Chagin# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20b69ae1a3SDmitry Chagin# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21b69ae1a3SDmitry Chagin# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22b69ae1a3SDmitry Chagin# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b69ae1a3SDmitry Chagin# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b69ae1a3SDmitry Chagin# SUCH DAMAGE.
25b69ae1a3SDmitry Chagin#
26b69ae1a3SDmitry Chagin#
27b69ae1a3SDmitry Chagin# Generates tables_linux.h
28b69ae1a3SDmitry Chagin#
29b69ae1a3SDmitry Chagin
30b69ae1a3SDmitry Chaginset -e
31b69ae1a3SDmitry Chagin
32b69ae1a3SDmitry ChaginLC_ALL=C; export LC_ALL
33b69ae1a3SDmitry Chagin
34b69ae1a3SDmitry Chaginif [ -z "$1" ]
35b69ae1a3SDmitry Chaginthen
36b69ae1a3SDmitry Chagin	echo "usage: sh $0 include-dir [output-file]"
37b69ae1a3SDmitry Chagin	exit 1
38b69ae1a3SDmitry Chaginfi
39b69ae1a3SDmitry Chagininclude_dir=$1
40b69ae1a3SDmitry Chaginif [ -n "$2" ]; then
41b69ae1a3SDmitry Chagin	output_file="$2"
42b69ae1a3SDmitry Chagin	output_tmp=$(mktemp -u)
43b69ae1a3SDmitry Chagin	exec > "$output_tmp"
44b69ae1a3SDmitry Chaginfi
45b69ae1a3SDmitry Chagin
46b69ae1a3SDmitry Chaginall_headers=
47b69ae1a3SDmitry Chagin#
48b69ae1a3SDmitry Chagin# Generate a table C #definitions.  The including file can define the
49b69ae1a3SDmitry Chagin# TABLE_NAME(n), TABLE_ENTRY(x), and TABLE_END macros to define what
50b69ae1a3SDmitry Chagin# the tables map to.
51b69ae1a3SDmitry Chagin#
52b69ae1a3SDmitry Chagingen_table()
53b69ae1a3SDmitry Chagin{
54b69ae1a3SDmitry Chagin	local name grep file excl filter
55b69ae1a3SDmitry Chagin	name=$1
56b69ae1a3SDmitry Chagin	grep=$2
57b69ae1a3SDmitry Chagin	file=$3
58b69ae1a3SDmitry Chagin	excl=$4
59b69ae1a3SDmitry Chagin
60b69ae1a3SDmitry Chagin	if [ -z "$excl" ]; then
61b69ae1a3SDmitry Chagin		filter="cat"
62b69ae1a3SDmitry Chagin	else
63b69ae1a3SDmitry Chagin		filter="egrep -v"
64b69ae1a3SDmitry Chagin	fi
65b69ae1a3SDmitry Chagin	cat <<_EOF_
66b69ae1a3SDmitry ChaginTABLE_START(${name})
67b69ae1a3SDmitry Chagin_EOF_
68b69ae1a3SDmitry Chagin	if [ -e "${include_dir}/${file}" ]; then
69b69ae1a3SDmitry Chagin		all_headers="${all_headers:+${all_headers} }${file}"
70b69ae1a3SDmitry Chagin		egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
71b69ae1a3SDmitry Chagin			$include_dir/$file | ${filter} ${excl} | \
72b69ae1a3SDmitry Chagin		awk '{ for (i = 1; i <= NF; i++) \
73b69ae1a3SDmitry Chagin			if ($i ~ /define/) \
74b69ae1a3SDmitry Chagin				break; \
75b69ae1a3SDmitry Chagin			++i; \
76b69ae1a3SDmitry Chagin			sub(/LINUX_/, "", $i); \
77b69ae1a3SDmitry Chagin			printf "TABLE_ENTRY(LINUX_%s, %s)\n", $i, $i }'
78b69ae1a3SDmitry Chagin	fi
79b69ae1a3SDmitry Chagincat <<_EOF_
80b69ae1a3SDmitry ChaginTABLE_END
81b69ae1a3SDmitry Chagin
82b69ae1a3SDmitry Chagin_EOF_
83b69ae1a3SDmitry Chagin}
84b69ae1a3SDmitry Chagin
85b69ae1a3SDmitry Chagincat <<_EOF_
86b69ae1a3SDmitry Chagin/* This file is auto-generated. */
87b69ae1a3SDmitry Chagin
88b69ae1a3SDmitry Chagin_EOF_
89b69ae1a3SDmitry Chagin
90d2b3237eSDmitry Chagingen_table "atflags"     "LINUX_AT_[A-Z_]+[[:space:]]+[0-9]+"             "compat/linux/linux_file.h"
91c8a79231SDmitry Chagingen_table "clockids"    "LINUX_CLOCK_[A-Z_]+[[:space:]]+[0-9]+"          "compat/linux/linux_time.h"
92c8a79231SDmitry Chagingen_table "clockflags"  "LINUX_TIMER_[A-Z_]+[[:space:]]+0x[0-9]+"        "compat/linux/linux_time.h"
93c8a79231SDmitry Chagingen_table "clockcpuids" "LINUX_CPUCLOCK_[A-Z_]+[[:space:]]+[0-9]+"       "compat/linux/linux_time.h"	"_MASK|_MAX"
94fde775bcSDmitry Chagingen_table "openflags"   "LINUX_O_[A-Z_]+[[:space:]]+[0-9]+"              "compat/linux/linux_file.h"	"O_RDONLY|O_RDWR|O_WRONLY|O_ACCMODE"
95f587a2a7SDmitry Chagingen_table "sigprocmaskhow" "LINUX_SIG_[A-Z]+[[:space:]]+[0-9]+"          "compat/linux/linux.h"
96*bd35e83bSDmitry Chagingen_table "cloneflags"  "LINUX_CLONE_[A-Z_]+[[:space:]]+[[:alnum:]]+"    "compat/linux/linux_fork.h"	"LINUX_CLONE_LEGACY_FLAGS|LINUX_CLONE_CLEAR_SIGHAND|LINUX_CLONE_INTO_CGROUP|LINUX_CLONE_NEWTIME"
97b69ae1a3SDmitry Chagin
98b69ae1a3SDmitry Chagin# Generate a .depend file for our output file
99b69ae1a3SDmitry Chaginif [ -n "$output_file" ]; then
100b69ae1a3SDmitry Chagin	depend_tmp=$(mktemp -u)
101b69ae1a3SDmitry Chagin	{
102b69ae1a3SDmitry Chagin		echo "$output_file: \\"
103b69ae1a3SDmitry Chagin		echo "$all_headers" | tr ' ' '\n' | sort -u |
104b69ae1a3SDmitry Chagin		    sed -e "s,^,	$include_dir/," -e 's,$, \\,'
105b69ae1a3SDmitry Chagin		echo
106b69ae1a3SDmitry Chagin	} > "$depend_tmp"
107b69ae1a3SDmitry Chagin	if cmp -s "$output_tmp" "$output_file"; then
108b69ae1a3SDmitry Chagin		rm -f "$output_tmp" "$depend_tmp"
109b69ae1a3SDmitry Chagin	else
110b69ae1a3SDmitry Chagin		mv -f "$depend_tmp" ".depend.${output_file}"
111b69ae1a3SDmitry Chagin		mv -f "$output_tmp" "$output_file"
112b69ae1a3SDmitry Chagin	fi
113b69ae1a3SDmitry Chaginfi
114