xref: /freebsd/lib/libsysdecode/mklinuxtables (revision c7a063741720ef81d4caa4613242579d12f1d605)
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# $FreeBSD$
27#
28# Generates tables_linux.h
29#
30
31set -e
32
33LC_ALL=C; export LC_ALL
34
35if [ -z "$1" ]
36then
37	echo "usage: sh $0 include-dir [output-file]"
38	exit 1
39fi
40include_dir=$1
41if [ -n "$2" ]; then
42	output_file="$2"
43	output_tmp=$(mktemp -u)
44	exec > "$output_tmp"
45fi
46
47all_headers=
48#
49# Generate a table C #definitions.  The including file can define the
50# TABLE_NAME(n), TABLE_ENTRY(x), and TABLE_END macros to define what
51# the tables map to.
52#
53gen_table()
54{
55	local name grep file excl filter
56	name=$1
57	grep=$2
58	file=$3
59	excl=$4
60
61	if [ -z "$excl" ]; then
62		filter="cat"
63	else
64		filter="egrep -v"
65	fi
66	cat <<_EOF_
67TABLE_START(${name})
68_EOF_
69	if [ -e "${include_dir}/${file}" ]; then
70		all_headers="${all_headers:+${all_headers} }${file}"
71		egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
72			$include_dir/$file | ${filter} ${excl} | \
73		awk '{ for (i = 1; i <= NF; i++) \
74			if ($i ~ /define/) \
75				break; \
76			++i; \
77			sub(/LINUX_/, "", $i); \
78			printf "TABLE_ENTRY(LINUX_%s, %s)\n", $i, $i }'
79	fi
80cat <<_EOF_
81TABLE_END
82
83_EOF_
84}
85
86cat <<_EOF_
87/* This file is auto-generated. */
88
89_EOF_
90
91gen_table "atflags"     "LINUX_AT_[A-Z_]+[[:space:]]+[0-9]+"             "compat/linux/linux_file.h"
92gen_table "clockids"    "LINUX_CLOCK_[A-Z_]+[[:space:]]+[0-9]+"          "compat/linux/linux_timer.h"
93gen_table "clockflags"  "LINUX_TIMER_[A-Z_]+[[:space:]]+0x[0-9]+"        "compat/linux/linux_timer.h"
94gen_table "clockcpuids" "LINUX_CPUCLOCK_[A-Z_]+[[:space:]]+[0-9]+"       "compat/linux/linux_timer.h"	"_MASK|_MAX"
95gen_table "openflags"   "LINUX_O_[A-Z_]+[[:space:]]+[0-9]+"              "compat/linux/linux_file.h"	"O_RDONLY|O_RDWR|O_WRONLY|O_ACCMODE"
96gen_table "sigprocmaskhow" "LINUX_SIG_[A-Z]+[[:space:]]+[0-9]+"          "compat/linux/linux.h"
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