1175936Sdes#!/bin/sh
2175936Sdes#
3223173Snetchild# Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
4223173Snetchild#
5223173Snetchild# Redistribution and use in source and binary forms, with or without
6223173Snetchild# modification, are permitted provided that the following conditions
7223173Snetchild# are met:
8223173Snetchild# 1. Redistributions of source code must retain the above copyright
9223173Snetchild#    notice, this list of conditions and the following disclaimer.
10223173Snetchild# 2. Redistributions in binary form must reproduce the above copyright
11223173Snetchild#    notice, this list of conditions and the following disclaimer in the
12223173Snetchild#    documentation and/or other materials provided with the distribution.
13223173Snetchild#
14223173Snetchild# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15223173Snetchild# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16223173Snetchild# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17223173Snetchild# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18223173Snetchild# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19223173Snetchild# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20223173Snetchild# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21223173Snetchild# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22223173Snetchild# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23223173Snetchild# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24223173Snetchild# SUCH DAMAGE.
25223173Snetchild#
26175936Sdes# $FreeBSD$
27175936Sdes#
28158766Snetchild# Generates kdump_subr.c
29158766Snetchild# mkioctls is a special-purpose script, and works fine as it is
30158766Snetchild# now, so it remains independent. The idea behind how it generates
31158766Snetchild# its list was heavily borrowed here.
32158766Snetchild#
33158766Snetchild# Some functions here are automatically generated. This can mean
34158766Snetchild# the user will see unusual kdump output or errors while building
35158766Snetchild# if the underlying .h files are changed significantly.
36158766Snetchild#
37158766Snetchild# Key:
38158766Snetchild# AUTO: Completely auto-generated with either the "or" or the "switch"
39158766Snetchild# method.
40158766Snetchild# AUTO - Special: Generated automatically, but with some extra commands
41158766Snetchild# that the auto_*_type() functions are inappropriate for.
42158766Snetchild# MANUAL: Manually entered and must therefore be manually updated.
43158766Snetchild
44175936Sdesset -e
45158766Snetchild
46158766SnetchildLC_ALL=C; export LC_ALL
47158766Snetchild
48158766Snetchildif [ -z "$1" ]
49158766Snetchildthen
50158766Snetchild	echo "usage: sh $0 include-dir"
51158766Snetchild	exit 1
52158766Snetchildfi
53158766Snetchildinclude_dir=$1
54158766Snetchild
55158766Snetchild#
56158766Snetchild# Automatically generates a C function that will print out the
57158766Snetchild# numeric input as a pipe-delimited string of the appropriate
58158766Snetchild# #define keys. ex:
59158766Snetchild# S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
60158766Snetchild# The XOR is necessary to prevent including the "0"-value in every
61158766Snetchild# line.
62158766Snetchild#
63158766Snetchildauto_or_type () {
64158766Snetchild	local name grep file
65158766Snetchild	name=$1
66158766Snetchild	grep=$2
67158766Snetchild	file=$3
68158766Snetchild
69158766Snetchild	cat <<_EOF_
70158766Snetchild/* AUTO */
71158766Snetchildvoid
72226145Sdes$name(intmax_t arg)
73158766Snetchild{
74226145Sdes	int or = 0;
75226145Sdes	printf("%#jx<", (uintmax_t)arg);
76158766Snetchild_EOF_
77158766Snetchild	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
78158766Snetchild		$include_dir/$file | \
79158766Snetchild	awk '{ for (i = 1; i <= NF; i++) \
80158766Snetchild		if ($i ~ /define/) \
81158766Snetchild			break; \
82158766Snetchild		++i; \
83226145Sdes		printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
84158766Snetchildcat <<_EOF_
85212728Srpaulo	printf(">");
86158766Snetchild	if (or == 0)
87226153Sdes		printf("<invalid>%jd", arg);
88158766Snetchild}
89158766Snetchild
90158766Snetchild_EOF_
91158766Snetchild}
92158766Snetchild
93158766Snetchild#
94158766Snetchild# Automatically generates a C function used when the argument
95158766Snetchild# maps to a single, specific #definition
96158766Snetchild#
97158766Snetchildauto_switch_type () {
98158766Snetchild	local name grep file
99158766Snetchild	name=$1
100158766Snetchild	grep=$2
101158766Snetchild	file=$3
102158766Snetchild
103158766Snetchild	cat <<_EOF_
104158766Snetchild/* AUTO */
105158766Snetchildvoid
106226145Sdes$name(intmax_t arg)
107158766Snetchild{
108158766Snetchild	switch (arg) {
109158766Snetchild_EOF_
110158766Snetchild	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
111158766Snetchild		$include_dir/$file | \
112158766Snetchild	awk '{ for (i = 1; i <= NF; i++) \
113158766Snetchild		if ($i ~ /define/) \
114158766Snetchild			break; \
115158766Snetchild		++i; \
116226153Sdes		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
117158766Snetchildcat <<_EOF_
118158766Snetchild	default: /* Should not reach */
119226153Sdes		printf("<invalid=%jd>", arg);
120158766Snetchild	}
121158766Snetchild}
122158766Snetchild
123158766Snetchild_EOF_
124158766Snetchild}
125158766Snetchild
126165756Srodrigc#
127165756Srodrigc# Automatically generates a C function used when the argument
128165756Srodrigc# maps to a #definition
129165756Srodrigc#
130165756Srodrigcauto_if_type () {
131165756Srodrigc	local name grep file
132165756Srodrigc	name=$1
133165756Srodrigc	grep=$2
134165756Srodrigc	file=$3
135165756Srodrigc
136165756Srodrigc	cat <<_EOF_
137165756Srodrigc/* AUTO */
138165756Srodrigcvoid
139226145Sdes$name(intmax_t arg)
140165756Srodrigc{
141165756Srodrigc_EOF_
142165756Srodrigc	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
143165756Srodrigc		$include_dir/$file | \
144165756Srodrigc	awk '{ printf "\t"; \
145165756Srodrigc		if (NR > 1) \
146165756Srodrigc			printf "else " ; \
147165756Srodrigc		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
148165756Srodrigccat <<_EOF_
149165756Srodrigc	else /* Should not reach */
150226153Sdes		printf("<invalid=%jd>", arg);
151165756Srodrigc}
152165756Srodrigc
153165756Srodrigc_EOF_
154165756Srodrigc}
155165756Srodrigc
156158766Snetchild# C start
157158766Snetchild
158158766Snetchildcat <<_EOF_
159275843Sjhb#include <errno.h>
160226145Sdes#include <stdint.h>
161158766Snetchild#include <stdio.h>
162158766Snetchild#include <sys/fcntl.h>
163158766Snetchild#include <sys/stat.h>
164158766Snetchild#include <sys/unistd.h>
165158766Snetchild#include <sys/mman.h>
166158766Snetchild#include <sys/wait.h>
167158766Snetchild#define _KERNEL
168158766Snetchild#include <sys/socket.h>
169158766Snetchild#undef _KERNEL
170165758Srodrigc#include <netinet/in.h>
171158766Snetchild#include <sys/param.h>
172158766Snetchild#include <sys/mount.h>
173255708Sjhb#include <sys/procctl.h>
174168543Semaste#include <sys/ptrace.h>
175158766Snetchild#include <sys/resource.h>
176158766Snetchild#include <sys/reboot.h>
177158766Snetchild#include <sched.h>
178158766Snetchild#include <sys/linker.h>
179158766Snetchild#define _KERNEL
180158766Snetchild#include <sys/thr.h>
181158766Snetchild#undef _KERNEL
182158766Snetchild#include <sys/extattr.h>
183158766Snetchild#include <sys/acl.h>
184158766Snetchild#include <aio.h>
185158766Snetchild#include <sys/sem.h>
186158766Snetchild#include <sys/ipc.h>
187158766Snetchild#include <sys/rtprio.h>
188158766Snetchild#include <sys/shm.h>
189275843Sjhb#include <machine/atomic.h>
190275843Sjhb#include <sys/umtx.h>
191158766Snetchild#include <nfsserver/nfs.h>
192158766Snetchild#include <ufs/ufs/quota.h>
193280250Srwatson#include <sys/capsicum.h>
194233925Sjhb#include <vm/vm.h>
195233925Sjhb#include <vm/vm_param.h>
196158766Snetchild
197158766Snetchild#include "kdump_subr.h"
198158766Snetchild
199158766Snetchild/*
200158766Snetchild * These are simple support macros. print_or utilizes a variable
201158766Snetchild * defined in the calling function to track whether or not it should
202158766Snetchild * print a logical-OR character ('|') before a string. if_print_or
203158766Snetchild * simply handles the necessary "if" statement used in many lines
204158766Snetchild * of this file.
205158766Snetchild */
206158766Snetchild#define print_or(str,orflag) do {                  \\
207158766Snetchild	if (orflag) putchar('|'); else orflag = 1; \\
208158766Snetchild	printf (str); }                            \\
209158766Snetchild	while (0)
210158766Snetchild#define if_print_or(i,flag,orflag) do {            \\
211158766Snetchild	if ((i & flag) == flag)                    \\
212158766Snetchild	print_or(#flag,orflag); }                  \\
213158766Snetchild	while (0)
214158766Snetchild
215158766Snetchild/* MANUAL */
216254291Sjillesvoid
217254291Sjillesatfdname(int fd, int decimal)
218254291Sjilles{
219254291Sjilles	if (fd == AT_FDCWD)
220254291Sjilles		printf("AT_FDCWD");
221254291Sjilles	else if (decimal)
222254291Sjilles		printf("%d", fd);
223254291Sjilles	else
224254291Sjilles		printf("%#x", fd);
225254291Sjilles}
226254291Sjilles
227254291Sjilles/* MANUAL */
228158766Snetchildextern char *signames[]; /* from kdump.c */
229158766Snetchildvoid
230226145Sdessigname(int sig)
231158766Snetchild{
232160295Skib	if (sig > 0 && sig < NSIG)
233226153Sdes		printf("SIG%s",signames[sig]);
234160295Skib	else
235226153Sdes		printf("SIG %d", sig);
236158766Snetchild}
237158766Snetchild
238158766Snetchild/* MANUAL */
239158766Snetchildvoid
240226145Sdessemctlname(int cmd)
241158766Snetchild{
242158766Snetchild	switch (cmd) {
243158766Snetchild	case GETNCNT:
244226153Sdes		printf("GETNCNT");
245158766Snetchild		break;
246158766Snetchild	case GETPID:
247226153Sdes		printf("GETPID");
248158766Snetchild		break;
249158766Snetchild	case GETVAL:
250226153Sdes		printf("GETVAL");
251158766Snetchild		break;
252158766Snetchild	case GETALL:
253226153Sdes		printf("GETALL");
254158766Snetchild		break;
255158766Snetchild	case GETZCNT:
256226153Sdes		printf("GETZCNT");
257158766Snetchild		break;
258158766Snetchild	case SETVAL:
259226153Sdes		printf("SETVAL");
260158766Snetchild		break;
261158766Snetchild	case SETALL:
262226153Sdes		printf("SETALL");
263158766Snetchild		break;
264158766Snetchild	case IPC_RMID:
265226153Sdes		printf("IPC_RMID");
266158766Snetchild		break;
267158766Snetchild	case IPC_SET:
268226153Sdes		printf("IPC_SET");
269158766Snetchild		break;
270158766Snetchild	case IPC_STAT:
271226153Sdes		printf("IPC_STAT");
272158766Snetchild		break;
273158766Snetchild	default: /* Should not reach */
274226153Sdes		printf("<invalid=%d>", cmd);
275158766Snetchild	}
276158766Snetchild}
277158766Snetchild
278158766Snetchild/* MANUAL */
279158766Snetchildvoid
280226145Sdesshmctlname(int cmd)
281226145Sdes{
282158766Snetchild	switch (cmd) {
283158766Snetchild	case IPC_RMID:
284226153Sdes		printf("IPC_RMID");
285158766Snetchild		break;
286158766Snetchild	case IPC_SET:
287226153Sdes		printf("IPC_SET");
288158766Snetchild		break;
289158766Snetchild	case IPC_STAT:
290226153Sdes		printf("IPC_STAT");
291158766Snetchild		break;
292158766Snetchild	default: /* Should not reach */
293226153Sdes		printf("<invalid=%d>", cmd);
294158766Snetchild	}
295158766Snetchild}
296158766Snetchild
297158766Snetchild/* MANUAL */
298158766Snetchildvoid
299226145Sdessemgetname(int flag)
300226145Sdes{
301226145Sdes	int or = 0;
302216130Sdelphij	if_print_or(flag, IPC_CREAT, or);
303216130Sdelphij	if_print_or(flag, IPC_EXCL, or);
304158766Snetchild	if_print_or(flag, SEM_R, or);
305158766Snetchild	if_print_or(flag, SEM_A, or);
306158766Snetchild	if_print_or(flag, (SEM_R>>3), or);
307158766Snetchild	if_print_or(flag, (SEM_A>>3), or);
308158766Snetchild	if_print_or(flag, (SEM_R>>6), or);
309158766Snetchild	if_print_or(flag, (SEM_A>>6), or);
310158766Snetchild}
311158766Snetchild
312158766Snetchild/*
313158766Snetchild * MANUAL
314158766Snetchild *
315158766Snetchild * Only used by SYS_open. Unless O_CREAT is set in flags, the
316158766Snetchild * mode argument is unused (and often bogus and misleading).
317158766Snetchild */
318158766Snetchildvoid
319226145Sdesflagsandmodename(int flags, int mode, int decimal)
320226145Sdes{
321226145Sdes	flagsname(flags);
322226153Sdes	putchar(',');
323158766Snetchild	if ((flags & O_CREAT) == O_CREAT) {
324158766Snetchild		modename (mode);
325158766Snetchild	} else {
326158766Snetchild		if (decimal) {
327226153Sdes			printf("<unused>%d", mode);
328158766Snetchild		} else {
329226153Sdes			printf("<unused>%#x", (unsigned int)mode);
330158766Snetchild		}
331158766Snetchild	}
332158766Snetchild}
333158766Snetchild
334255493Sjhb/* MANUAL */
335255493Sjhbvoid
336255493Sjhbidtypename(idtype_t idtype, int decimal)
337255493Sjhb{
338255493Sjhb	switch(idtype) {
339255493Sjhb	case P_PID:
340255493Sjhb		printf("P_PID");
341255493Sjhb		break;
342255493Sjhb	case P_PPID:
343255493Sjhb		printf("P_PPID");
344255493Sjhb		break;
345255493Sjhb	case P_PGID:
346255493Sjhb		printf("P_PGID");
347255493Sjhb		break;
348255493Sjhb	case P_SID:
349255493Sjhb		printf("P_SID");
350255493Sjhb		break;
351255493Sjhb	case P_CID:
352255493Sjhb		printf("P_CID");
353255493Sjhb		break;
354255493Sjhb	case P_UID:
355255493Sjhb		printf("P_UID");
356255493Sjhb		break;
357255493Sjhb	case P_GID:
358255493Sjhb		printf("P_GID");
359255493Sjhb		break;
360255493Sjhb	case P_ALL:
361255493Sjhb		printf("P_ALL");
362255493Sjhb		break;
363255493Sjhb	case P_LWPID:
364255493Sjhb		printf("P_LWPID");
365255493Sjhb		break;
366255493Sjhb	case P_TASKID:
367255493Sjhb		printf("P_TASKID");
368255493Sjhb		break;
369255493Sjhb	case P_PROJID:
370255493Sjhb		printf("P_PROJID");
371255493Sjhb		break;
372255493Sjhb	case P_POOLID:
373255493Sjhb		printf("P_POOLID");
374255493Sjhb		break;
375255493Sjhb	case P_JAILID:
376255493Sjhb		printf("P_JAILID");
377255493Sjhb		break;
378255493Sjhb	case P_CTID:
379255493Sjhb		printf("P_CTID");
380255493Sjhb		break;
381255493Sjhb	case P_CPUID:
382255493Sjhb		printf("P_CPUID");
383255493Sjhb		break;
384255493Sjhb	case P_PSETID:
385255493Sjhb		printf("P_PSETID");
386255493Sjhb		break;
387255493Sjhb	default:
388255493Sjhb		if (decimal) {
389255493Sjhb			printf("%d", idtype);
390255493Sjhb		} else {
391255493Sjhb			printf("%#x", idtype);
392255493Sjhb		}
393255493Sjhb	}
394255493Sjhb}
395255493Sjhb
396158766Snetchild/*
397158766Snetchild * MANUAL
398158766Snetchild *
399158766Snetchild * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
400158766Snetchild * referring to a line in /etc/protocols . It might be appropriate
401158766Snetchild * to use getprotoent(3) here.
402158766Snetchild */
403158766Snetchildvoid
404226145Sdessockoptlevelname(int level, int decimal)
405158766Snetchild{
406158766Snetchild	if (level == SOL_SOCKET) {
407226153Sdes		printf("SOL_SOCKET");
408158766Snetchild	} else {
409158766Snetchild		if (decimal) {
410226153Sdes			printf("%d", level);
411158766Snetchild		} else {
412226153Sdes			printf("%#x", (unsigned int)level);
413158766Snetchild		}
414158766Snetchild	}
415158766Snetchild}
416158766Snetchild
417233925Sjhb/*
418233925Sjhb * MANUAL
419233925Sjhb *
420233925Sjhb * Used for page fault type.  Cannot use auto_or_type since the macro
421233925Sjhb * values contain a cast.  Also, VM_PROT_NONE has to be handled specially.
422233925Sjhb */
423233925Sjhbvoid
424233925Sjhbvmprotname (int type)
425233925Sjhb{
426233925Sjhb	int	or = 0;
427233925Sjhb
428233925Sjhb	if (type == VM_PROT_NONE) {
429233925Sjhb		(void)printf("VM_PROT_NONE");
430233925Sjhb		return;
431233925Sjhb	}
432233925Sjhb	if_print_or(type, VM_PROT_READ, or);
433233925Sjhb	if_print_or(type, VM_PROT_WRITE, or);
434233925Sjhb	if_print_or(type, VM_PROT_EXECUTE, or);
435233925Sjhb	if_print_or(type, VM_PROT_COPY, or);
436233925Sjhb}
437254922Sjilles
438254922Sjilles/*
439254922Sjilles * MANUAL
440254922Sjilles */
441254922Sjillesvoid
442254922Sjillessocktypenamewithflags(int type)
443254922Sjilles{
444254922Sjilles	if (type & SOCK_CLOEXEC)
445254922Sjilles		printf("SOCK_CLOEXEC|"), type &= ~SOCK_CLOEXEC;
446254922Sjilles	if (type & SOCK_NONBLOCK)
447254922Sjilles		printf("SOCK_NONBLOCK|"), type &= ~SOCK_NONBLOCK;
448254922Sjilles	socktypename(type);
449254922Sjilles}
450158766Snetchild_EOF_
451158766Snetchild
452226147Sdesauto_or_type     "accessmodename"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
453226147Sdesauto_switch_type "acltypename"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
454280250Srwatsonauto_or_type     "capfcntlname"        "CAP_FCNTL_[A-Z]+[[:space:]]+\(1"              "sys/capsicum.h"
455226147Sdesauto_switch_type "extattrctlname"      "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
456232072Sjhbauto_switch_type "fadvisebehavname"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
457226147Sdesauto_or_type     "flagsname"           "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"
458226147Sdesauto_or_type     "flockname"           "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
459226147Sdesauto_or_type     "getfsstatflagsname"  "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*"            "sys/mount.h"
460226147Sdesauto_switch_type "kldsymcmdname"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
461226147Sdesauto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
462226147Sdesauto_switch_type "lio_listioname"      "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
463226147Sdesauto_switch_type "madvisebehavname"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
464226147Sdesauto_switch_type "minheritname"        "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
465226147Sdesauto_or_type     "mlockallname"        "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
466226147Sdesauto_or_type     "mmapprotname"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
467226147Sdesauto_or_type     "modename"            "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
468226147Sdesauto_or_type     "mountflagsname"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
469226147Sdesauto_switch_type "msyncflagsname"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
470246711Spluknetauto_or_type     "nfssvcname"          "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"            "nfs/nfssvc.h"
471226147Sdesauto_switch_type "prioname"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
472255708Sjhbauto_switch_type "procctlcmdname"      "PROC_[A-Z]+[[:space:]]+[0-9]"                 "sys/procctl.h"
473226147Sdesauto_switch_type "ptraceopname"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
474226147Sdesauto_switch_type "quotactlname"        "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
475226147Sdesauto_or_type     "rebootoptname"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
476226147Sdesauto_or_type     "rforkname"           "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
477226147Sdesauto_switch_type "rlimitname"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
478226147Sdesauto_switch_type "schedpolicyname"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sched.h"
479226147Sdesauto_switch_type "sendfileflagsname"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
480226147Sdesauto_or_type     "shmatname"           "SHM_[A-Z]+[[:space:]]+[0-9]{6}+"              "sys/shm.h"
481226147Sdesauto_switch_type "shutdownhowname"     "SHUT_[A-Z]+[[:space:]]+[0-9]+"                "sys/socket.h"
482240820Sjillesauto_switch_type "sigbuscodename"      "BUS_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
483240820Sjillesauto_switch_type "sigchldcodename"     "CLD_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
484240820Sjillesauto_switch_type "sigfpecodename"      "FPE_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
485226147Sdesauto_switch_type "sigprocmaskhowname"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
486240820Sjillesauto_switch_type "sigillcodename"      "ILL_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
487240820Sjillesauto_switch_type "sigsegvcodename"     "SEGV_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
488240820Sjillesauto_switch_type "sigtrapcodename"     "TRAP_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
489226147Sdesauto_if_type     "sockdomainname"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
490226147Sdesauto_if_type     "sockfamilyname"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
491226147Sdesauto_if_type     "sockipprotoname"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
492226147Sdesauto_switch_type "sockoptname"         "SO_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/socket.h"
493226147Sdesauto_switch_type "socktypename"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
494226147Sdesauto_or_type     "thrcreateflagsname"  "THR_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/thr.h"
495275843Sjhbauto_switch_type "umtxopname"          "UMTX_OP_[[:alnum:]_]+[[:space:]]+[0-9]+"            "sys/umtx.h"
496233925Sjhbauto_switch_type "vmresultname"        "KERN_[A-Z]+[[:space:]]+[0-9]+"                "vm/vm_param.h"
497255493Sjhbauto_or_type     "wait6optname"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
498226147Sdesauto_switch_type "whencename"          "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
499158766Snetchild
500158766Snetchildcat <<_EOF_
501158766Snetchild/*
502158766Snetchild * AUTO - Special
503158766Snetchild * F_ is used to specify fcntl commands as well as arguments. Both sets are
504158766Snetchild * grouped in fcntl.h, and this awk script grabs the first group.
505158766Snetchild */
506158766Snetchildvoid
507226145Sdesfcntlcmdname(int cmd, int arg, int decimal)
508158766Snetchild{
509158766Snetchild	switch (cmd) {
510158766Snetchild_EOF_
511242482Sjillesegrep "^#[[:space:]]*define[[:space:]]+F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]*" \
512158766Snetchild	$include_dir/sys/fcntl.h | \
513158766Snetchild	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
514158766Snetchild		if ($i ~ /define/) \
515158766Snetchild			break; \
516158766Snetchild		++i; \
517158766Snetchild		if (o <= $(i+1)) \
518226153Sdes			printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i; \
519158766Snetchild		else \
520158766Snetchild			exit; \
521158766Snetchild		o = $(i+1) }'
522158766Snetchildcat <<_EOF_
523158766Snetchild	default: /* Should not reach */
524226153Sdes		printf("<invalid=%d>", cmd);
525158766Snetchild	}
526226153Sdes	putchar(',');
527158766Snetchild	if (cmd == F_GETFD || cmd == F_SETFD) {
528158766Snetchild		if (arg == FD_CLOEXEC)
529226153Sdes			printf("FD_CLOEXEC");
530158766Snetchild		else if (arg == 0)
531226153Sdes			printf("0");
532158766Snetchild		else {
533158766Snetchild			if (decimal)
534226153Sdes				printf("<invalid>%d", arg);
535158766Snetchild			else
536226153Sdes				printf("<invalid>%#x", (unsigned int)arg);
537158766Snetchild		}
538158766Snetchild	} else if (cmd == F_SETFL) {
539158766Snetchild		flagsname(arg);
540158766Snetchild	} else {
541158766Snetchild		if (decimal)
542226153Sdes			printf("%d", arg);
543158766Snetchild		else
544226153Sdes			printf("%#x", (unsigned int)arg);
545158766Snetchild	}
546158766Snetchild}
547158766Snetchild
548158766Snetchild/*
549158766Snetchild * AUTO - Special
550158766Snetchild *
551254430Sjhb * The MAP_ALIGNED flag requires special handling.
552254430Sjhb */
553254430Sjhbvoid
554254430Sjhbmmapflagsname(int flags)
555254430Sjhb{
556254430Sjhb	int align;
557254430Sjhb	int or = 0;
558254430Sjhb	printf("%#x<", flags);
559254430Sjhb_EOF_
560254430Sjhbegrep "^#[[:space:]]*define[[:space:]]+MAP_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+[[:space:]]*" \
561254430Sjhb	$include_dir/sys/mman.h | grep -v MAP_ALIGNED | \
562254430Sjhb	awk '{ for (i = 1; i <= NF; i++) \
563254430Sjhb		if ($i ~ /define/) \
564254430Sjhb			break; \
565254430Sjhb		++i; \
566254430Sjhb		printf "\tif (!((flags > 0) ^ ((%s) > 0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
567254430Sjhbcat <<_EOF_
568255426Sjhb#ifdef MAP_32BIT
569255426Sjhb	if (!((flags > 0) ^ ((MAP_32BIT) > 0)))
570255426Sjhb		if_print_or(flags, MAP_32BIT, or);
571255426Sjhb#endif
572254430Sjhb	align = flags & MAP_ALIGNMENT_MASK;
573254430Sjhb	if (align != 0) {
574254430Sjhb		if (align == MAP_ALIGNED_SUPER)
575254430Sjhb			print_or("MAP_ALIGNED_SUPER", or);
576254430Sjhb		else {
577254430Sjhb			print_or("MAP_ALIGNED", or);
578254430Sjhb			printf("(%d)", align >> MAP_ALIGNMENT_SHIFT);
579254430Sjhb		}
580254430Sjhb	}
581254430Sjhb	printf(">");
582254430Sjhb	if (or == 0)
583254430Sjhb		printf("<invalid>%d", flags);
584254430Sjhb}
585254430Sjhb
586254430Sjhb/*
587254430Sjhb * AUTO - Special
588254430Sjhb *
589158766Snetchild * The only reason this is not fully automated is due to the
590158766Snetchild * grep -v RTP_PRIO statement. A better egrep line should
591158766Snetchild * make this capable of being a auto_switch_type() function.
592158766Snetchild */
593158766Snetchildvoid
594226145Sdesrtprioname(int func)
595158766Snetchild{
596158766Snetchild	switch (func) {
597158766Snetchild_EOF_
598158766Snetchildegrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
599158766Snetchild	$include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
600158766Snetchild	awk '{ for (i = 1; i <= NF; i++) \
601158766Snetchild		if ($i ~ /define/) \
602158766Snetchild			break; \
603158766Snetchild		++i; \
604226153Sdes		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
605158766Snetchildcat <<_EOF_
606158766Snetchild	default: /* Should not reach */
607226153Sdes		printf("<invalid=%d>", func);
608158766Snetchild	}
609158766Snetchild}
610158766Snetchild
611158766Snetchild/*
612158766Snetchild * AUTO - Special
613158766Snetchild *
614158766Snetchild * The send and recv functions have a flags argument which can be
615158766Snetchild * set to 0. There is no corresponding #define. The auto_ functions
616158766Snetchild * detect this as "invalid", which is incorrect here.
617158766Snetchild */
618158766Snetchildvoid
619226145Sdessendrecvflagsname(int flags)
620158766Snetchild{
621226145Sdes	int or = 0;
622175936Sdes
623158766Snetchild	if (flags == 0) {
624226153Sdes		printf("0");
625158766Snetchild		return;
626158766Snetchild	}
627212727Srpaulo
628212728Srpaulo	printf("%#x<", flags);
629158766Snetchild_EOF_
630158766Snetchildegrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
631158766Snetchild	awk '{ for (i = 1; i <= NF; i++) \
632158766Snetchild		if ($i ~ /define/) \
633158766Snetchild			break; \
634158766Snetchild		++i; \
635158766Snetchild		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
636158766Snetchildcat <<_EOF_
637212728Srpaulo	printf(">");
638158766Snetchild}
639158766Snetchild
640240820Sjilles/*
641240820Sjilles * AUTO - Special
642240820Sjilles *
643240820Sjilles * Check general codes first, then defer to signal-specific codes.
644240820Sjilles */
645240820Sjillesvoid
646240820Sjillessigcodename(int sig, int code)
647240820Sjilles{
648240820Sjilles	switch (code) {
649158766Snetchild_EOF_
650240820Sjillesegrep "^#[[:space:]]*define[[:space:]]+SI_[A-Z]+[[:space:]]+0(x[0-9abcdef]+)?[[:space:]]*" \
651240820Sjilles	$include_dir/sys/signal.h | grep -v SI_UNDEFINED | \
652240820Sjilles	awk '{ for (i = 1; i <= NF; i++) \
653240820Sjilles		if ($i ~ /define/) \
654240820Sjilles			break; \
655240820Sjilles		++i; \
656240820Sjilles		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
657240820Sjillescat <<_EOF_
658240820Sjilles	default:
659240820Sjilles		switch (sig) {
660240820Sjilles		case SIGILL:
661240820Sjilles			sigillcodename(code);
662240820Sjilles			break;
663240820Sjilles		case SIGBUS:
664240820Sjilles			sigbuscodename(code);
665240820Sjilles			break;
666240820Sjilles		case SIGSEGV:
667240820Sjilles			sigsegvcodename(code);
668240820Sjilles			break;
669240820Sjilles		case SIGFPE:
670240820Sjilles			sigfpecodename(code);
671240820Sjilles			break;
672240820Sjilles		case SIGTRAP:
673240820Sjilles			sigtrapcodename(code);
674240820Sjilles			break;
675240820Sjilles		case SIGCHLD:
676240820Sjilles			sigchldcodename(code);
677240820Sjilles			break;
678240820Sjilles		default:
679240820Sjilles			printf("<invalid=%#x>", code);
680240820Sjilles		}
681240820Sjilles	}
682240820Sjilles}
683255219Spjd
684275843Sjhb/*
685275843Sjhb * AUTO - Special
686275843Sjhb *
687275843Sjhb * Just print 0 as 0.
688275843Sjhb */
689275843Sjhbvoid
690275843Sjhbumtxcvwaitflags(intmax_t arg)
691275843Sjhb{
692275843Sjhb	int or = 0;
693275843Sjhb	if (arg == 0) {
694275843Sjhb		printf("0");
695275843Sjhb		return;
696275843Sjhb	}
697275843Sjhb	printf("%#jx<", (uintmax_t)arg);
698255219Spjd_EOF_
699275843Sjhb	egrep "^#[[:space:]]*define[[:space:]]+CVWAIT_[A-Z_]+[[:space:]]+0x[0-9]+[[:space:]]*" \
700275843Sjhb		$include_dir/sys/umtx.h | \
701275843Sjhb	awk '{ for (i = 1; i <= NF; i++) \
702275843Sjhb		if ($i ~ /define/) \
703275843Sjhb			break; \
704275843Sjhb		++i; \
705275843Sjhb		printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
706275843Sjhbcat <<_EOF_
707275843Sjhb	printf(">");
708275843Sjhb	if (or == 0)
709275843Sjhb		printf("<invalid>%jd", arg);
710275843Sjhb}
711275843Sjhb
712275843Sjhb
713275843Sjhb/*
714275843Sjhb * AUTO - Special
715275843Sjhb *
716275843Sjhb * Just print 0 as 0.
717275843Sjhb */
718275843Sjhbvoid
719275843Sjhbumtxrwlockflags(intmax_t arg)
720275843Sjhb{
721275843Sjhb	int or = 0;
722275843Sjhb	if (arg == 0) {
723275843Sjhb		printf("0");
724275843Sjhb		return;
725275843Sjhb	}
726275843Sjhb	printf("%#jx<", (uintmax_t)arg);
727275843Sjhb_EOF_
728275843Sjhb	egrep "^#[[:space:]]*define[[:space:]]+URWLOCK_PREFER_READER[[:space:]]+0x[0-9]+[[:space:]]*" \
729275843Sjhb		$include_dir/sys/umtx.h | \
730275843Sjhb	awk '{ for (i = 1; i <= NF; i++) \
731275843Sjhb		if ($i ~ /define/) \
732275843Sjhb			break; \
733275843Sjhb		++i; \
734275843Sjhb		printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
735275843Sjhbcat <<_EOF_
736275843Sjhb	printf(">");
737275843Sjhb	if (or == 0)
738275843Sjhb		printf("<invalid>%jd", arg);
739275843Sjhb}
740275843Sjhb_EOF_
741255219Spjdegrep '#define[[:space:]]+CAP_[A-Z_]+[[:space:]]+CAPRIGHT\([0-9],[[:space:]]+0x[0-9]{16}ULL\)' \
742280250Srwatson	$include_dir/sys/capsicum.h | \
743255219Spjd	sed -E 's/[	]+/ /g' | \
744255219Spjd	awk -F '[   \(,\)]' '
745255219Spjd	BEGIN {
746255219Spjd		printf "void\n"
747255219Spjd		printf "capname(const cap_rights_t *rightsp)\n"
748255219Spjd		printf "{\n"
749255219Spjd		printf "\tint comma = 0;\n\n"
750255219Spjd		printf "\tprintf(\"<\");\n"
751255219Spjd	}
752255219Spjd	{
753255219Spjd		printf "\tif ((rightsp->cr_rights[%s] & %s) == %s) {\n", $4, $2, $2
754255219Spjd		printf "\t\tif (comma) printf(\",\"); else comma = 1;\n"
755255219Spjd		printf "\t\tprintf(\"%s\");\n", $2
756255219Spjd		printf "\t}\n"
757255219Spjd	}
758255219Spjd	END {
759255219Spjd		printf "\tprintf(\">\");\n"
760255219Spjd		printf "}\n"
761255219Spjd	}'
762