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