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# 27# Generates tables.h 28# 29# Originally this script was 'mksubr' for kdump which generated a complete 30# C file along with function definitions. Now this script generates tables 31# of constants and names extracted from header files. 32 33set -e 34 35LC_ALL=C; export LC_ALL 36 37if [ -z "$1" ] 38then 39 echo "usage: sh $0 include-dir [output-file]" 40 exit 1 41fi 42include_dir=$1 43if [ -n "$2" ]; then 44 output_file="$2" 45 output_tmp=$(mktemp -u) 46 exec > "$output_tmp" 47fi 48 49all_headers= 50# 51# Generate a table from C preprocessor symbol definitions. The 52# including file can define the TABLE_START(n), TABLE_ENTRY(x), and 53# TABLE_END macros to define what the tables map to. 54# 55gen_table() 56{ 57 local name=$1 grep=$2 file=$3 excl=$4 58 59 cat <<_EOF_ 60TABLE_START(${name}) 61_EOF_ 62 if [ -e "${include_dir}/${file}" ]; then 63 all_headers="${all_headers:+${all_headers} }${file}" 64 sed -nE ${excl:+-e "/${excl}/d"} \ 65 -e "/^#[[:space:]]*define[[:space:]]+${grep}/p" \ 66 "${include_dir}/${file}" | \ 67 sed -Ee 's/.*define[[:space:]]+([^[:space:]]+)\>.*/TABLE_ENTRY(\1)/' 68 fi 69cat <<_EOF_ 70TABLE_END 71 72_EOF_ 73} 74 75# 76# Generate a table from C enum definitions. The including file can 77# define the TABLE_START(n), TABLE_ENTRY(x), and TABLE_END macros to 78# define what the tables map to. Works with most enum definitions as 79# long as each constant is on a line by itself. 80# 81gen_table_enum() 82{ 83 local name=$1 grep=$2 file=$3 excl=$4 84 85 cat <<_EOF_ 86TABLE_START(${name}) 87_EOF_ 88 if [ -e "${include_dir}/${file}" ]; then 89 all_headers="${all_headers:+${all_headers} }${file}" 90 # preprocess the header 91 ${CPP:-${CC:-cc} -E} "${include_dir}/${file}" | 92 # filter out directives and apply exclusion 93 sed -Ee '/^#/d' ${excl:+-e "/${excl}/d"} | 94 # extract all enum definitions 95 awk '/^(typedef[[:space:]]*)?enum([[:space:]].*)?\{/, 96 /^\}([[:space:]].*)?;/' | 97 # pick out the constants we want 98 sed -Ene 's/^[[:space:]]*('"${grep}"')\>.*/TABLE_ENTRY(\1)/p' 99 fi 100cat <<_EOF_ 101TABLE_END 102 103_EOF_ 104} 105 106cat <<_EOF_ 107/* This file is auto-generated. */ 108 109_EOF_ 110 111gen_table "accessmode" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h" 112gen_table "acltype" "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+" "sys/acl.h" 113gen_table "atflags" "AT_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/fcntl.h" 114gen_table "capfcntl" "CAP_FCNTL_[A-Z]+[[:space:]]+\(1" "sys/capsicum.h" 115gen_table "closerangeflags" "CLOSE_RANGE_[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)" "sys/unistd.h" 116gen_table "extattrns" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h" 117gen_table "fadvisebehav" "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+" "sys/fcntl.h" 118gen_table "openflags" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h" "O_RDONLY|O_RDWR|O_WRONLY" 119gen_table "flockops" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h" 120gen_table "inotifyflags" "IN_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/inotify.h" "IN_CLOEXEC|IN_NONBLOCK" 121gen_table "kldsymcmd" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h" 122gen_table "kldunloadfflags" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h" 123gen_table "lio_listiomodes" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h" 124gen_table "madvisebehav" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 125gen_table "minheritflags" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h" 126gen_table "mlockallflags" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 127gen_table "mmapprot" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 128gen_table "ngbtsolevel" "SOL_[A-Z0-9]+[[:space:]]+0x[0-9A-Fa-f]+" "netgraph/bluetooth/include/ng_btsocket.h" 129gen_table "fileflags" "[SU]F_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/stat.h" "UF_COMPRESSED|UF_TRACKED|UF_SETTABLE|SF_SETTABLE" 130gen_table "filemode" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" 131gen_table "keventflags" "EV_[A-Z]+[[:space:]]+0x[0-9]+" "sys/event.h" "EV_SYSFLAGS|EV_DROP|EV_FLAG[12]" 132gen_table "keventfilters" "EVFILT_[A-Z]+[[:space:]]+\(-[0-9]+\)" "sys/event.h" 133gen_table "mountflags" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h" 134gen_table "msyncflags" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h" 135gen_table "nfssvcflags" "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+" "nfs/nfssvc.h" 136gen_table "pathconfname" "_PC_[A-Z4_]+[[:space:]]+[0-9]+" "sys/unistd.h" 137gen_table "pollfdevents" "POLL[A-Z]+[[:space:]]+" "sys/poll.h" 138gen_table "prio" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h" 139gen_table "procctlcmd" "PROC_[A-Z_]+[[:space:]]+[0-9]" "sys/procctl.h" "PROC_TRACE_CTL_" 140gen_table "ptraceop" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h" 141gen_table "quotactlcmds" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h" 142gen_table "rebootopt" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h" 143gen_table "rforkflags" "RF[A-Z]+[[:space:]]+\([0-9]+[uU]?<<[0-9]+\)" "sys/unistd.h" "RFPPWAIT" 144gen_table "rlimit" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h" 145gen_table "rusage" "RUSAGE_[A-Z]+[[:space:]]+[-0-9]+" "sys/resource.h" 146gen_table "schedpolicy" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sys/sched.h" 147gen_table "sendfileflags" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h" 148gen_table "shmatflags" "SHM_[A-Z]+[[:space:]]+[0-9]{6}" "sys/shm.h" 149gen_table_enum "shutdownhow" "SHUT_[A-Z]+" "sys/socket.h" 150gen_table "sigbuscode" "BUS_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 151gen_table "sigchldcode" "CLD_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 152gen_table "sigfpecode" "FPE_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 153gen_table "sigprocmaskhow" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 154gen_table "sigillcode" "ILL_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 155gen_table "sigsegvcode" "SEGV_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 156gen_table "sigtrapcode" "TRAP_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h" 157gen_table "sockdomain" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h" 158gen_table "sockfamily" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h" 159gen_table "sockipproto" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h" 160gen_table "sockopt" "SO_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/socket.h" 161gen_table "sockoptip" "(IP_[[:alnum:]_]+|MCAST_[[:alnum:]_]+_GROUP)[[:space:]]+" "netinet/in.h" "IP_DEFAULT|IP_MIN|IP_MAX|IP_PORTRANGE" 162gen_table "sockoptipv6" "IPV6_[[:alnum:]_]+[[:space:]]+[0-9]+" "netinet6/in6.h" "IPV6_ADDR_|IPV6_TAG_DIRECT|IPV6_OPTIONS|IPV6_RECVOPTS|IPV6_RECVRETOPTS|IPV6_RECVDSTADDR|IPV6_RETOPTS|IPV6_2292|IPV6_RECVRTHDRDSTOPTS|IPV6_REACHCONF|IPV6_PKTOPTIONS" 163gen_table "sockoptsctp" "SCTP_[[:alnum:]_]+[[:space:]]+[0-9]+" "netinet/sctp.h" 164gen_table "sockopttcp" "TCP_[[:alnum:]_]+[[:space:]]+[0-9]+" "netinet/tcp.h" "TCP_MIN|TCP_MAX[^S]|TCP_MSS|TCP_[[:alnum:]_]+_MAX|TCP_FASTOPEN_MIN_COOKIE_LEN|TCP_FASTOPEN_PSK_LEN|TCP_USE_DDP" 165gen_table "sockoptudp" "UDP_[[:alnum:]]+[[:space:]]+[0-9]+" "netinet/udp.h" "UDP_ENCAP_" 166gen_table "sockoptudplite" "UDPLITE_[[:alnum:]_]+[[:space:]]+[0-9]+" "netinet/udplite.h" 167gen_table "socktype" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h" 168gen_table "thrcreateflags" "THR_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/thr.h" 169gen_table "umtxop" "UMTX_OP_[[:alnum:]][[:alnum:]_]*[[:space:]]+[0-9]+" "sys/umtx.h" 170gen_table "umtxopflags" "UMTX_OP__[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/umtx.h" 171gen_table "vmprot" "VM_PROT_[A-Z_]+[[:space:]]+\(\(vm_prot_t\)[[:space:]]+0x[0-9]+\)" "vm/vm.h" 172gen_table "vmresult" "KERN_[A-Z_]+[[:space:]]+[0-9]+" "vm/vm_param.h" 173gen_table "wait6opt" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h" 174gen_table "seekwhence" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h" 175gen_table "fcntlcmd" "F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]+" "sys/fcntl.h" "F_CANCEL|F_..LCK" 176gen_table "mmapflags" "MAP_[2-3A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h" 177gen_table "rtpriofuncs" "RTP_[A-Z]+[[:space:]]+[0-9]+" "sys/rtprio.h" 178gen_table "msgflags" "MSG_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/socket.h" "MSG_SOCALLBCK|MSG_MORETOCOME|MSG_TLSAPPDATA" 179gen_table "sigcode" "SI_[A-Z]+[[:space:]]+0(x[0-9abcdef]+)?" "sys/signal.h" 180gen_table "umtxcvwaitflags" "CVWAIT_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/umtx.h" 181gen_table "umtxrwlockflags" "URWLOCK_PREFER_READER[[:space:]]+0x[0-9]+" "sys/umtx.h" 182gen_table "caprights" "CAP_[A-Z_]+[[:space:]]+((CAPRIGHT\([0-9],[[:space:]]+0x[0-9]{16}ULL\))|(\(CAP_[A-Z_]+[[:space:]]*\|.*\)))" "sys/capsicum.h" 183gen_table "sctpprpolicy" "SCTP_PR_SCTP_[A-Z_]+[[:space:]]+0x[0-9]+" "netinet/sctp_uio.h" "SCTP_PR_SCTP_ALL" 184gen_table "cmsgtypesocket" "SCM_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/socket.h" 185if [ -e "${include_dir}/x86/sysarch.h" ]; then 186 gen_table "sysarchnum" "(AMD64|I386)_[A-Z86_]+[[:space:]]+[0-9]+" "x86/sysarch.h" 187else 188 gen_table "sysarchnum" "[A-Z_]+[[:space:]]+[0-9]+" "machine/sysarch.h" 189fi 190gen_table "shmflags" "SHM_[A-Z_]+[[:space:]]+0x[0-9]+" "sys/mman.h" "SHM_ANON" 191gen_table "itimerwhich" "ITIMER_[A-Z]+[[:space:]]+[0-9]+" "sys/time.h" 192gen_table_enum "pfnl_cmd" "PFNL_CMD_[A-Z_]+" "netpfil/pf/pf_nl.h" 193gen_table "nlm_flag" "NLM_F_[A-Z]+[[:space:]]+0x[0-9]+" "netlink/netlink.h" 194 195# Generate a .depend file for our output file 196if [ -n "$output_file" ]; then 197 depend_tmp=$(mktemp -u) 198 { 199 echo "$output_file: \\" 200 echo "$all_headers" | tr ' ' '\n' | sort -u | 201 sed -e "s,^, $include_dir/," -e 's,$, \\,' 202 echo 203 } > "$depend_tmp" 204 if cmp -s "$output_tmp" "$output_file"; then 205 rm -f "$output_tmp" "$depend_tmp" 206 else 207 mv -f "$depend_tmp" ".depend.${output_file}" 208 mv -f "$output_tmp" "$output_file" 209 fi 210fi 211