1#!/bin/sh
2# SPDX-License-Identifier: LGPL-2.1
3
4if [ $# -ne 2 ] ; then
5	[ $# -eq 1 ] && hostarch=$1 || hostarch=`uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/`
6	asm_header_dir=tools/include/uapi/asm-generic
7	arch_header_dir=tools/arch/${hostarch}/include/uapi/asm
8else
9	asm_header_dir=$1
10	arch_header_dir=$2
11fi
12
13common_mman=${asm_header_dir}/mman-common.h
14arch_mman=${arch_header_dir}/mman.h
15
16prefix="PROT"
17
18printf "static const char *mmap_prot[] = {\n"
19regex=`printf '^[[:space:]]*#[[:space:]]*define[[:space:]]+%s_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*' ${prefix}`
20([ ! -f ${arch_mman} ] || egrep -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
21(egrep $regex ${common_mman} | \
22	egrep -vw PROT_NONE | \
23	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
24	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef ${prefix}_%s\n#define ${prefix}_%s %s\n#endif\n")
25[ -f ${arch_mman} ] && egrep -q $regex ${arch_mman} &&
26(egrep $regex ${arch_mman} | \
27	egrep -vw PROT_NONE | \
28	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
29	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef ${prefix}_%s\n#define ${prefix}_%s %s\n#endif\n")
30printf "};\n"
31