xref: /freebsd/lib/libsysdecode/mktables (revision d6dcd8d2c3ef8cd3fae94b43c6dfe9986ee33985)
19289f547SJohn Baldwin#!/bin/sh
29289f547SJohn Baldwin#
39289f547SJohn Baldwin# Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
49289f547SJohn Baldwin#
59289f547SJohn Baldwin# Redistribution and use in source and binary forms, with or without
69289f547SJohn Baldwin# modification, are permitted provided that the following conditions
79289f547SJohn Baldwin# are met:
89289f547SJohn Baldwin# 1. Redistributions of source code must retain the above copyright
99289f547SJohn Baldwin#    notice, this list of conditions and the following disclaimer.
109289f547SJohn Baldwin# 2. Redistributions in binary form must reproduce the above copyright
119289f547SJohn Baldwin#    notice, this list of conditions and the following disclaimer in the
129289f547SJohn Baldwin#    documentation and/or other materials provided with the distribution.
139289f547SJohn Baldwin#
149289f547SJohn Baldwin# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
159289f547SJohn Baldwin# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169289f547SJohn Baldwin# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179289f547SJohn Baldwin# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
189289f547SJohn Baldwin# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199289f547SJohn Baldwin# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
209289f547SJohn Baldwin# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
219289f547SJohn Baldwin# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
229289f547SJohn Baldwin# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
239289f547SJohn Baldwin# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
249289f547SJohn Baldwin# SUCH DAMAGE.
259289f547SJohn Baldwin#
269289f547SJohn Baldwin#
279289f547SJohn Baldwin# Generates tables.h
289289f547SJohn Baldwin#
299289f547SJohn Baldwin# Originally this script was 'mksubr' for kdump which generated a complete
309289f547SJohn Baldwin# C file along with function definitions.  Now this script generates tables
319289f547SJohn Baldwin# of constants and names extracted from header files.
329289f547SJohn Baldwin
339289f547SJohn Baldwinset -e
349289f547SJohn Baldwin
359289f547SJohn BaldwinLC_ALL=C; export LC_ALL
369289f547SJohn Baldwin
379289f547SJohn Baldwinif [ -z "$1" ]
389289f547SJohn Baldwinthen
395e1eb60dSBryan Drewery	echo "usage: sh $0 include-dir [output-file]"
409289f547SJohn Baldwin	exit 1
419289f547SJohn Baldwinfi
429289f547SJohn Baldwininclude_dir=$1
435e1eb60dSBryan Dreweryif [ -n "$2" ]; then
445e1eb60dSBryan Drewery	output_file="$2"
45dc89d069SBryan Drewery	output_tmp=$(mktemp -u)
46dc89d069SBryan Drewery	exec > "$output_tmp"
475e1eb60dSBryan Dreweryfi
489289f547SJohn Baldwin
495e1eb60dSBryan Dreweryall_headers=
509289f547SJohn Baldwin#
518b197806SDag-Erling Smørgrav# Generate a table from C preprocessor symbol definitions.  The
528b197806SDag-Erling Smørgrav# including file can define the TABLE_START(n), TABLE_ENTRY(x), and
538b197806SDag-Erling Smørgrav# TABLE_END macros to define what the tables map to.
549289f547SJohn Baldwin#
559289f547SJohn Baldwingen_table()
569289f547SJohn Baldwin{
578b197806SDag-Erling Smørgrav	local name=$1 grep=$2 file=$3 excl=$4
589289f547SJohn Baldwin
599289f547SJohn Baldwin	cat <<_EOF_
609289f547SJohn BaldwinTABLE_START(${name})
619289f547SJohn Baldwin_EOF_
62f944e9e2SBryan Drewery	if [ -e "${include_dir}/${file}" ]; then
63f944e9e2SBryan Drewery		all_headers="${all_headers:+${all_headers} }${file}"
648b197806SDag-Erling Smørgrav		sed -nE ${excl:+-e "/${excl}/d"} \
658b197806SDag-Erling Smørgrav		    -e "/^#[[:space:]]*define[[:space:]]+${grep}/p" \
668b197806SDag-Erling Smørgrav		    "${include_dir}/${file}" | \
678b197806SDag-Erling Smørgrav		sed -Ee 's/.*define[[:space:]]+([^[:space:]]+)\>.*/TABLE_ENTRY(\1)/'
688b197806SDag-Erling Smørgrav	fi
698b197806SDag-Erling Smørgravcat <<_EOF_
708b197806SDag-Erling SmørgravTABLE_END
718b197806SDag-Erling Smørgrav
728b197806SDag-Erling Smørgrav_EOF_
738b197806SDag-Erling Smørgrav}
748b197806SDag-Erling Smørgrav
758b197806SDag-Erling Smørgrav#
768b197806SDag-Erling Smørgrav# Generate a table from C enum definitions.  The including file can
778b197806SDag-Erling Smørgrav# define the TABLE_START(n), TABLE_ENTRY(x), and TABLE_END macros to
788b197806SDag-Erling Smørgrav# define what the tables map to.  Works with most enum definitions as
798b197806SDag-Erling Smørgrav# long as each constant is on a line by itself.
808b197806SDag-Erling Smørgrav#
818b197806SDag-Erling Smørgravgen_table_enum()
828b197806SDag-Erling Smørgrav{
838b197806SDag-Erling Smørgrav	local name=$1 grep=$2 file=$3 excl=$4
848b197806SDag-Erling Smørgrav
858b197806SDag-Erling Smørgrav	cat <<_EOF_
868b197806SDag-Erling SmørgravTABLE_START(${name})
878b197806SDag-Erling Smørgrav_EOF_
888b197806SDag-Erling Smørgrav	if [ -e "${include_dir}/${file}" ]; then
898b197806SDag-Erling Smørgrav		all_headers="${all_headers:+${all_headers} }${file}"
908b197806SDag-Erling Smørgrav		# preprocess the header
918b197806SDag-Erling Smørgrav		${CPP:-${CC:-cc} -E} "${include_dir}/${file}" |
928b197806SDag-Erling Smørgrav		# filter out directives and apply exclusion
938b197806SDag-Erling Smørgrav		sed -Ee '/^#/d' ${excl:+-e "/${excl}/d"} |
948b197806SDag-Erling Smørgrav		# extract all enum definitions
958b197806SDag-Erling Smørgrav		awk '/^(typedef[[:space:]]*)?enum([[:space:]].*)?\{/,
968b197806SDag-Erling Smørgrav                     /^\}([[:space:]].*)?;/' |
978b197806SDag-Erling Smørgrav		# pick out the constants we want
988b197806SDag-Erling Smørgrav		sed -Ene 's/^[[:space:]]*('"${grep}"')\>.*/TABLE_ENTRY(\1)/p'
99f944e9e2SBryan Drewery	fi
1009289f547SJohn Baldwincat <<_EOF_
1019289f547SJohn BaldwinTABLE_END
1029289f547SJohn Baldwin
1039289f547SJohn Baldwin_EOF_
1049289f547SJohn Baldwin}
1059289f547SJohn Baldwin
1069289f547SJohn Baldwincat <<_EOF_
1079289f547SJohn Baldwin/* This file is auto-generated. */
1089289f547SJohn Baldwin
1099289f547SJohn Baldwin_EOF_
1109289f547SJohn Baldwin
1119289f547SJohn Baldwingen_table "accessmode"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
1129289f547SJohn Baldwingen_table "acltype"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
11339a3a438SJohn Baldwingen_table "atflags"         "AT_[A-Z_]+[[:space:]]+0x[0-9]+"               "sys/fcntl.h"
1149289f547SJohn Baldwingen_table "capfcntl"        "CAP_FCNTL_[A-Z]+[[:space:]]+\(1"              "sys/capsicum.h"
115f3f3e3c4SMateusz Guzikgen_table "closerangeflags" "CLOSE_RANGE_[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
1169289f547SJohn Baldwingen_table "extattrns"       "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
1179289f547SJohn Baldwingen_table "fadvisebehav"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
1189289f547SJohn Baldwingen_table "openflags"       "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"	"O_RDONLY|O_RDWR|O_WRONLY"
1199289f547SJohn Baldwingen_table "flockops"        "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
120b79bd43fSMark Johnstongen_table "inotifyflags"    "IN_[A-Z_]+[[:space:]]+0x[0-9]+"               "sys/inotify.h"      "IN_CLOEXEC|IN_NONBLOCK"
1219289f547SJohn Baldwingen_table "kldsymcmd"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
1229289f547SJohn Baldwingen_table "kldunloadfflags" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
1239289f547SJohn Baldwingen_table "lio_listiomodes" "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
1249289f547SJohn Baldwingen_table "madvisebehav"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
1259289f547SJohn Baldwingen_table "minheritflags"   "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
1269289f547SJohn Baldwingen_table "mlockallflags"   "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
1279289f547SJohn Baldwingen_table "mmapprot"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
1289289f547SJohn Baldwingen_table "ngbtsolevel"     "SOL_[A-Z0-9]+[[:space:]]+0x[0-9A-Fa-f]+"      "netgraph/bluetooth/include/ng_btsocket.h"
1299289f547SJohn Baldwingen_table "fileflags"       "[SU]F_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"       "sys/stat.h"		"UF_COMPRESSED|UF_TRACKED|UF_SETTABLE|SF_SETTABLE"
1309289f547SJohn Baldwingen_table "filemode"        "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
131ffb66079SJohn Baldwingen_table "keventflags"     "EV_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/event.h"        "EV_SYSFLAGS|EV_DROP|EV_FLAG[12]"
132ffb66079SJohn Baldwingen_table "keventfilters"   "EVFILT_[A-Z]+[[:space:]]+\(-[0-9]+\)"         "sys/event.h"
1339289f547SJohn Baldwingen_table "mountflags"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
1349289f547SJohn Baldwingen_table "msyncflags"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
1359289f547SJohn Baldwingen_table "nfssvcflags"     "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"         "nfs/nfssvc.h"
13639a3a438SJohn Baldwingen_table "pathconfname"    "_PC_[A-Z4_]+[[:space:]]+[0-9]+"               "sys/unistd.h"
137bd23e71fSKyle Evansgen_table "pollfdevents"    "POLL[A-Z]+[[:space:]]+"                       "sys/poll.h"
1389289f547SJohn Baldwingen_table "prio"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
1399289f547SJohn Baldwingen_table "procctlcmd"      "PROC_[A-Z_]+[[:space:]]+[0-9]"                 "sys/procctl.h"	"PROC_TRACE_CTL_"
140f575573cSKonstantin Belousovgen_table "ptraceop"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
1419289f547SJohn Baldwingen_table "quotactlcmds"    "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
1429289f547SJohn Baldwingen_table "rebootopt"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
143d511e97cSBrooks Davisgen_table "rforkflags"      "RF[A-Z]+[[:space:]]+\([0-9]+[uU]?<<[0-9]+\)"       "sys/unistd.h"	"RFPPWAIT"
1449289f547SJohn Baldwingen_table "rlimit"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
145ee8aa41dSJohn Baldwingen_table "rusage"          "RUSAGE_[A-Z]+[[:space:]]+[-0-9]+"             "sys/resource.h"
14641068268SDmitry Chagingen_table "schedpolicy"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sys/sched.h"
1479289f547SJohn Baldwingen_table "sendfileflags"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
148c5c5072bSAlex Richardsongen_table "shmatflags"      "SHM_[A-Z]+[[:space:]]+[0-9]{6}"               "sys/shm.h"
1498b197806SDag-Erling Smørgravgen_table_enum "shutdownhow" "SHUT_[A-Z]+"                                 "sys/socket.h"
1509289f547SJohn Baldwingen_table "sigbuscode"      "BUS_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
1519289f547SJohn Baldwingen_table "sigchldcode"     "CLD_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
1529289f547SJohn Baldwingen_table "sigfpecode"      "FPE_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
1539289f547SJohn Baldwingen_table "sigprocmaskhow"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
1549289f547SJohn Baldwingen_table "sigillcode"      "ILL_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
1559289f547SJohn Baldwingen_table "sigsegvcode"     "SEGV_[A-Z]+[[:space:]]+[0-9]+"                "sys/signal.h"
1569289f547SJohn Baldwingen_table "sigtrapcode"     "TRAP_[A-Z]+[[:space:]]+[0-9]+"                "sys/signal.h"
1579289f547SJohn Baldwingen_table "sockdomain"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
1589289f547SJohn Baldwingen_table "sockfamily"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
1599289f547SJohn Baldwingen_table "sockipproto"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
16034c8598eSMark Johnstongen_table "sockopt"         "SO_[A-Z_]+[[:space:]]+0x[0-9]+"               "sys/socket.h"
1619289f547SJohn Baldwingen_table "sockoptip"       "(IP_[[:alnum:]_]+|MCAST_[[:alnum:]_]+_GROUP)[[:space:]]+" "netinet/in.h" "IP_DEFAULT|IP_MIN|IP_MAX|IP_PORTRANGE"
162702eb303SMichael Tuexengen_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"
163472e8009SMichael Tuexengen_table "sockoptsctp"     "SCTP_[[:alnum:]_]+[[:space:]]+[0-9]+"         "netinet/sctp.h"
1646b3db5d7SKyle Evansgen_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"
1659289f547SJohn Baldwingen_table "sockoptudp"      "UDP_[[:alnum:]]+[[:space:]]+[0-9]+"           "netinet/udp.h"      "UDP_ENCAP_"
166ed466c34SMichael Tuexengen_table "sockoptudplite"  "UDPLITE_[[:alnum:]_]+[[:space:]]+[0-9]+"      "netinet/udplite.h"
1679289f547SJohn Baldwingen_table "socktype"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
168c27f7d6bSKonstantin Belousovgen_table "thrcreateflags"  "THR_[A-Z_]+[[:space:]]+0x[0-9]+"              "sys/thr.h"
1693b27074bSKyle Evansgen_table "umtxop"          "UMTX_OP_[[:alnum:]][[:alnum:]_]*[[:space:]]+[0-9]+"      "sys/umtx.h"
1703b27074bSKyle Evansgen_table "umtxopflags"     "UMTX_OP__[[:alnum:]_]+[[:space:]]+[0-9]+"                "sys/umtx.h"
171701a9993SNathaniel Wesley Filardogen_table "vmprot"          "VM_PROT_[A-Z_]+[[:space:]]+\(\(vm_prot_t\)[[:space:]]+0x[0-9]+\)"  "vm/vm.h"
17248e68919SBjoern A. Zeebgen_table "vmresult"        "KERN_[A-Z_]+[[:space:]]+[0-9]+"               "vm/vm_param.h"
1739289f547SJohn Baldwingen_table "wait6opt"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
1749289f547SJohn Baldwingen_table "seekwhence"      "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
1759289f547SJohn Baldwingen_table "fcntlcmd"        "F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]+"   "sys/fcntl.h"	"F_CANCEL|F_..LCK"
176f3e11927SDmitry Chagingen_table "mmapflags"       "MAP_[2-3A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+"     "sys/mman.h"
1779289f547SJohn Baldwingen_table "rtpriofuncs"     "RTP_[A-Z]+[[:space:]]+[0-9]+"                 "sys/rtprio.h"
178e2576591SMark Johnstongen_table "msgflags"        "MSG_[A-Z_]+[[:space:]]+0x[0-9]+"              "sys/socket.h"	"MSG_SOCALLBCK|MSG_MORETOCOME|MSG_TLSAPPDATA"
1799289f547SJohn Baldwingen_table "sigcode"         "SI_[A-Z]+[[:space:]]+0(x[0-9abcdef]+)?"       "sys/signal.h"
1809289f547SJohn Baldwingen_table "umtxcvwaitflags" "CVWAIT_[A-Z_]+[[:space:]]+0x[0-9]+"           "sys/umtx.h"
1819289f547SJohn Baldwingen_table "umtxrwlockflags" "URWLOCK_PREFER_READER[[:space:]]+0x[0-9]+"    "sys/umtx.h"
182869199d9SMark Johnstongen_table "caprights"       "CAP_[A-Z_]+[[:space:]]+((CAPRIGHT\([0-9],[[:space:]]+0x[0-9]{16}ULL\))|(\(CAP_[A-Z_]+[[:space:]]*\|.*\)))"   "sys/capsicum.h"
183c1f0d826SMichael Tuexengen_table "sctpprpolicy"    "SCTP_PR_SCTP_[A-Z_]+[[:space:]]+0x[0-9]+"     "netinet/sctp_uio.h" "SCTP_PR_SCTP_ALL"
184a826eb5aSMichael Tuexengen_table "cmsgtypesocket"  "SCM_[A-Z_]+[[:space:]]+0x[0-9]+"              "sys/socket.h"
18539a3a438SJohn Baldwinif [ -e "${include_dir}/x86/sysarch.h" ]; then
18639a3a438SJohn Baldwin	gen_table "sysarchnum" "(AMD64|I386)_[A-Z86_]+[[:space:]]+[0-9]+"  "x86/sysarch.h"
18739a3a438SJohn Baldwinelse
18839a3a438SJohn Baldwin	gen_table "sysarchnum" "[A-Z_]+[[:space:]]+[0-9]+"                 "machine/sysarch.h"
18939a3a438SJohn Baldwinfi
190c70019ddSKyle Evansgen_table "shmflags"  "SHM_[A-Z_]+[[:space:]]+0x[0-9]+"                    "sys/mman.h"         "SHM_ANON"
1912fc3a51dSDmitry Chagingen_table "itimerwhich"     "ITIMER_[A-Z]+[[:space:]]+[0-9]+"              "sys/time.h"
192f01c5095SGleb Smirnoffgen_table_enum "pfnl_cmd"   "PFNL_CMD_[A-Z_]+"                             "netpfil/pf/pf_nl.h"
193*d6dcd8d2SMitchell Hornegen_table "nlm_flag"        "NLM_F_[A-Z_]+[[:space:]]+0x[0-9]+"            "netlink/netlink.h"
1945e1eb60dSBryan Drewery
1955e1eb60dSBryan Drewery# Generate a .depend file for our output file
1965e1eb60dSBryan Dreweryif [ -n "$output_file" ]; then
197dc89d069SBryan Drewery	depend_tmp=$(mktemp -u)
198dc89d069SBryan Drewery	{
199dc89d069SBryan Drewery		echo "$output_file: \\"
2005e1eb60dSBryan Drewery		echo "$all_headers" | tr ' ' '\n' | sort -u |
201dc89d069SBryan Drewery		    sed -e "s,^,	$include_dir/," -e 's,$, \\,'
202dc89d069SBryan Drewery		echo
203dc89d069SBryan Drewery	} > "$depend_tmp"
204dc89d069SBryan Drewery	if cmp -s "$output_tmp" "$output_file"; then
205dc89d069SBryan Drewery		rm -f "$output_tmp" "$depend_tmp"
206dc89d069SBryan Drewery	else
207dc89d069SBryan Drewery		mv -f "$depend_tmp" ".depend.${output_file}"
208dc89d069SBryan Drewery		mv -f "$output_tmp" "$output_file"
209dc89d069SBryan Drewery	fi
2105e1eb60dSBryan Dreweryfi
211