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	header_dir=tools/include/uapi/asm-generic
7	arch_header_dir=tools/arch/${hostarch}/include/uapi/asm
8else
9	header_dir=$1
10	arch_header_dir=$2
11fi
12
13arch_mman=${arch_header_dir}/mman.h
14
15# those in egrep -vw are flags, we want just the bits
16
17printf "static const char *mmap_flags[] = {\n"
18regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+MAP_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
19egrep -q $regex ${arch_mman} && \
20(egrep $regex ${arch_mman} | \
21	sed -r "s/$regex/\2 \1/g"	| \
22	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n")
23([ ! -f ${arch_mman} ] || egrep -q '#[[:space:]]*include[[:space:]]+<uapi/asm-generic/mman.*' ${arch_mman}) &&
24(egrep $regex ${header_dir}/mman-common.h | \
25	egrep -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
26	sed -r "s/$regex/\2 \1/g"	| \
27	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n")
28([ ! -f ${arch_mman} ] || egrep -q '#[[:space:]]*include[[:space:]]+<uapi/asm-generic/mman.h>.*' ${arch_mman}) &&
29(egrep $regex ${header_dir}/mman.h | \
30	sed -r "s/$regex/\2 \1/g"	| \
31	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n")
32printf "};\n"
33