summaryrefslogtreecommitdiff
path: root/lib/libsysdecode/mktables
blob: 3a0284a2deb4be1b0816bdf6d1cb65852d9308d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
#
# Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
# Generates tables.h
#
# Originally this script was 'mksubr' for kdump which generated a complete
# C file along with function definitions.  Now this script generates tables
# of constants and names extracted from header files.

set -e

LC_ALL=C; export LC_ALL

if [ -z "$1" ]
then
	echo "usage: sh $0 include-dir [output-file]"
	exit 1
fi
include_dir=$1
if [ -n "$2" ]; then
	output_file="$2"
	output_tmp=$(mktemp -u)
	exec > "$output_tmp"
fi

all_headers=
#
# Generate a table C #definitions.  The including file can define the
# TABLE_NAME(n), TABLE_ENTRY(x), and TABLE_END macros to define what
# the tables map to.
#
gen_table()
{
	local name grep file excl filter
	name=$1
	grep=$2
	file=$3
	excl=$4

	if [ -z "$excl" ]; then
		filter="cat"
	else
		filter="egrep -v"
	fi
	cat <<_EOF_
TABLE_START(${name})
_EOF_
	if [ -e "${include_dir}/${file}" ]; then
		all_headers="${all_headers:+${all_headers} }${file}"
		egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
			$include_dir/$file | ${filter} ${excl} | \
		awk '{ for (i = 1; i <= NF; i++) \
			if ($i ~ /define/) \
				break; \
			++i; \
			printf "TABLE_ENTRY(%s)\n", $i }'
	fi
cat <<_EOF_
TABLE_END

_EOF_
}

cat <<_EOF_
/* This file is auto-generated. */

_EOF_

gen_table "accessmode"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
gen_table "acltype"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
gen_table "atflags"         "AT_[A-Z_]+[[:space:]]+0x[0-9]+"               "sys/fcntl.h"
gen_table "capfcntl"        "CAP_FCNTL_[A-Z]+[[:space:]]+\(1"              "sys/capsicum.h"
gen_table "extattrns"       "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
gen_table "fadvisebehav"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
gen_table "openflags"       "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"	"O_RDONLY|O_RDWR|O_WRONLY"
gen_table "flockops"        "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
gen_table "kldsymcmd"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
gen_table "kldunloadfflags" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
gen_table "lio_listiomodes" "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
gen_table "madvisebehav"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
gen_table "minheritflags"   "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
gen_table "mlockallflags"   "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
gen_table "mmapprot"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
gen_table "ngbtsolevel"     "SOL_[A-Z0-9]+[[:space:]]+0x[0-9A-Fa-f]+"      "netgraph/bluetooth/include/ng_btsocket.h"
gen_table "fileflags"       "[SU]F_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"       "sys/stat.h"		"UF_COMPRESSED|UF_TRACKED|UF_SETTABLE|SF_SETTABLE"
gen_table "filemode"        "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
gen_table "keventflags"     "EV_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/event.h"        "EV_SYSFLAGS|EV_DROP|EV_FLAG[12]"
gen_table "keventfilters"   "EVFILT_[A-Z]+[[:space:]]+\(-[0-9]+\)"         "sys/event.h"
gen_table "mountflags"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
gen_table "msyncflags"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
gen_table "nfssvcflags"     "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"         "nfs/nfssvc.h"
gen_table "pathconfname"    "_PC_[A-Z4_]+[[:space:]]+[0-9]+"               "sys/unistd.h"
gen_table "prio"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
gen_table "procctlcmd"      "PROC_[A-Z_]+[[:space:]]+[0-9]"                 "sys/procctl.h"	"PROC_TRACE_CTL_"
gen_table "ptraceop"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
gen_table "quotactlcmds"    "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
gen_table "rebootopt"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
gen_table "rforkflags"      "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
gen_table "rlimit"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
gen_table "rusage"          "RUSAGE_[A-Z]+[[:space:]]+[-0-9]+"             "sys/resource.h"
gen_table "schedpolicy"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sched.h"
gen_table "sendfileflags"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
gen_table "shmatflags"      "SHM_[A-Z]+[[:space:]]+[0-9]{6}"               "sys/shm.h"
gen_table "shutdownhow"     "SHUT_[A-Z]+[[:space:]]+[0-9]+"                "sys/socket.h"
gen_table "sigbuscode"      "BUS_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
gen_table "sigchldcode"     "CLD_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
gen_table "sigfpecode"      "FPE_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
gen_table "sigprocmaskhow"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
gen_table "sigillcode"      "ILL_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
gen_table "sigsegvcode"     "SEGV_[A-Z]+[[:space:]]+[0-9]+"                "sys/signal.h"
gen_table "sigtrapcode"     "TRAP_[A-Z]+[[:space:]]+[0-9]+"                "sys/signal.h"
gen_table "sockdomain"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
gen_table "sockfamily"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
gen_table "sockipproto"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
gen_table "sockopt"         "SO_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/socket.h"
gen_table "sockoptip"       "(IP_[[:alnum:]_]+|MCAST_[[:alnum:]_]+_GROUP)[[:space:]]+" "netinet/in.h" "IP_DEFAULT|IP_MIN|IP_MAX|IP_PORTRANGE"
gen_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"
gen_table "sockoptsctp"     "SCTP_[[:alnum:]_]+[[:space:]]+[0-9]+"         "netinet/sctp.h"
gen_table "sockopttcp"      "TCP_[[:alnum:]_]+[[:space:]]+[0-9]+"          "netinet/tcp.h"      "TCP_MIN|TCP_MAX[^S]|TCP_MSS|TCP_[[:alnum:]_]+_MAX"
gen_table "sockoptudp"      "UDP_[[:alnum:]]+[[:space:]]+[0-9]+"           "netinet/udp.h"      "UDP_ENCAP_"
gen_table "sockoptudplite"  "UDPLITE_[[:alnum:]_]+[[:space:]]+[0-9]+"      "netinet/udplite.h"
gen_table "socktype"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
gen_table "thrcreateflags"  "THR_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/thr.h"
gen_table "umtxop"          "UMTX_OP_[[:alnum:]][[:alnum:]_]*[[:space:]]+[0-9]+"      "sys/umtx.h"
gen_table "umtxopflags"     "UMTX_OP__[[:alnum:]_]+[[:space:]]+[0-9]+"                "sys/umtx.h"
gen_table "vmprot"          "VM_PROT_[A-Z]+[[:space:]]+\(\(vm_prot_t\)[[:space:]]+0x[0-9]+\)"  "vm/vm.h"
gen_table "vmresult"        "KERN_[A-Z_]+[[:space:]]+[0-9]+"               "vm/vm_param.h"
gen_table "wait6opt"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
gen_table "seekwhence"      "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
gen_table "fcntlcmd"        "F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]+"   "sys/fcntl.h"	"F_CANCEL|F_..LCK"
gen_table "mmapflags"       "MAP_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
gen_table "rtpriofuncs"     "RTP_[A-Z]+[[:space:]]+[0-9]+"                 "sys/rtprio.h"
gen_table "msgflags"        "MSG_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/socket.h"	"MSG_SOCALLBCK|MSG_MORETOCOME|MSG_TLSAPPDATA"
gen_table "sigcode"         "SI_[A-Z]+[[:space:]]+0(x[0-9abcdef]+)?"       "sys/signal.h"
gen_table "umtxcvwaitflags" "CVWAIT_[A-Z_]+[[:space:]]+0x[0-9]+"           "sys/umtx.h"
gen_table "umtxrwlockflags" "URWLOCK_PREFER_READER[[:space:]]+0x[0-9]+"    "sys/umtx.h"
gen_table "caprights"       "CAP_[A-Z_]+[[:space:]]+CAPRIGHT\([0-9],[[:space:]]+0x[0-9]{16}ULL\)"   "sys/capsicum.h"
gen_table "sctpprpolicy"    "SCTP_PR_SCTP_[A-Z_]+[[:space:]]+0x[0-9]+"     "netinet/sctp_uio.h" "SCTP_PR_SCTP_ALL"
gen_table "cmsgtypesocket"  "SCM_[A-Z_]+[[:space:]]+0x[0-9]+"              "sys/socket.h"
if [ -e "${include_dir}/x86/sysarch.h" ]; then
	gen_table "sysarchnum" "(AMD64|I386)_[A-Z86_]+[[:space:]]+[0-9]+"  "x86/sysarch.h"
else
	gen_table "sysarchnum" "[A-Z_]+[[:space:]]+[0-9]+"                 "machine/sysarch.h"
fi
gen_table "shmflags"  "SHM_[A-Z_]+[[:space:]]+0x[0-9]+"                    "sys/mman.h"         "SHM_ANON"

# Generate a .depend file for our output file
if [ -n "$output_file" ]; then
	depend_tmp=$(mktemp -u)
	{
		echo "$output_file: \\"
		echo "$all_headers" | tr ' ' '\n' | sort -u |
		    sed -e "s,^,	$include_dir/," -e 's,$, \\,'
		echo
	} > "$depend_tmp"
	if cmp -s "$output_tmp" "$output_file"; then
		rm -f "$output_tmp" "$depend_tmp"
	else
		mv -f "$depend_tmp" ".depend.${output_file}"
		mv -f "$output_tmp" "$output_file"
	fi
fi