1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-only
3#
4# Generate a syscall number header.
5#
6# Each line of the syscall table should have the following format:
7#
8# NR ABI NAME [NATIVE] [COMPAT]
9#
10# NR       syscall number
11# ABI      ABI name
12# NAME     syscall name
13# NATIVE   native entry point (optional)
14# COMPAT   compat entry point (optional)
15set -e
16
17usage() {
18	echo >&2 "usage: $0 [--abis ABIS] [--prefix PREFIX] INFILE OUTFILE" >&2
19	echo >&2
20	echo >&2 "  INFILE    input syscall table"
21	echo >&2 "  OUTFILE   output header file"
22	echo >&2
23	echo >&2 "options:"
24	echo >&2 "  --abis ABIS        ABI(s) to handle (By default, all lines are handled)"
25	echo >&2 "  --prefix PREFIX    The prefix to the macro like __NR_<PREFIX><NAME>"
26	exit 1
27}
28
29# default unless specified by options
30abis=
31prefix=
32
33while [ $# -gt 0 ]
34do
35	case $1 in
36	--abis)
37		abis=$(echo "($2)" | tr ',' '|')
38		shift 2;;
39	--prefix)
40		prefix=$2
41		shift 2;;
42	-*)
43		echo "$1: unknown option" >&2
44		usage;;
45	*)
46		break;;
47	esac
48done
49
50if [ $# -ne 2 ]; then
51	usage
52fi
53
54infile="$1"
55outfile="$2"
56
57guard=_ASM_$(basename "$outfile" |
58	sed -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
59	-e 's/[^A-Z0-9_]/_/g' -e 's/__/_/g')
60
61grep -E "^[0-9A-Fa-fXx]+[[:space:]]+$abis" "$infile" | sort -n | {
62	echo "#ifndef $guard"
63	echo "#define $guard"
64	echo
65
66	max=0
67	while read nr abi name native compat ; do
68		max=$nr
69	done
70
71	echo "#define __NR_${prefix}syscalls $(($max + 1))"
72	echo
73	echo "#endif /* $guard */"
74} > "$outfile"
75