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