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# $FreeBSD$
27#
28# Generates kdump_subr.c
29# mkioctls is a special-purpose script, and works fine as it is
30# now, so it remains independent. The idea behind how it generates
31# its list was heavily borrowed here.
32#
33# Some functions here are automatically generated. This can mean
34# the user will see unusual kdump output or errors while building
35# if the underlying .h files are changed significantly.
36#
37# Key:
38# AUTO: Completely auto-generated with either the "or" or the "switch"
39# method.
40# AUTO - Special: Generated automatically, but with some extra commands
41# that the auto_*_type() functions are inappropriate for.
42# MANUAL: Manually entered and must therefore be manually updated.
43
44set -e
45
46LC_ALL=C; export LC_ALL
47
48if [ -z "$1" ]
49then
50	echo "usage: sh $0 include-dir"
51	exit 1
52fi
53include_dir=$1
54
55#
56# Automatically generates a C function that will print out the
57# numeric input as a pipe-delimited string of the appropriate
58# #define keys. ex:
59# S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
60# The XOR is necessary to prevent including the "0"-value in every
61# line.
62#
63auto_or_type () {
64	local name grep file
65	name=$1
66	grep=$2
67	file=$3
68
69	cat <<_EOF_
70/* AUTO */
71void
72$name(intmax_t arg)
73{
74	int or = 0;
75	printf("%#jx<", (uintmax_t)arg);
76_EOF_
77	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
78		$include_dir/$file | \
79	awk '{ for (i = 1; i <= NF; i++) \
80		if ($i ~ /define/) \
81			break; \
82		++i; \
83		printf "\tif (!((arg > 0) ^ ((%s) > 0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
84cat <<_EOF_
85	printf(">");
86	if (or == 0)
87		printf("<invalid>%jd", arg);
88}
89
90_EOF_
91}
92
93#
94# Automatically generates a C function used when the argument
95# maps to a single, specific #definition
96#
97auto_switch_type () {
98	local name grep file
99	name=$1
100	grep=$2
101	file=$3
102
103	cat <<_EOF_
104/* AUTO */
105void
106$name(intmax_t arg)
107{
108	switch (arg) {
109_EOF_
110	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
111		$include_dir/$file | \
112	awk '{ for (i = 1; i <= NF; i++) \
113		if ($i ~ /define/) \
114			break; \
115		++i; \
116		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
117cat <<_EOF_
118	default: /* Should not reach */
119		printf("<invalid=%jd>", arg);
120	}
121}
122
123_EOF_
124}
125
126#
127# Automatically generates a C function used when the argument
128# maps to a #definition
129#
130auto_if_type () {
131	local name grep file
132	name=$1
133	grep=$2
134	file=$3
135
136	cat <<_EOF_
137/* AUTO */
138void
139$name(intmax_t arg)
140{
141_EOF_
142	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
143		$include_dir/$file | \
144	awk '{ printf "\t"; \
145		if (NR > 1) \
146			printf "else " ; \
147		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
148cat <<_EOF_
149	else /* Should not reach */
150		printf("<invalid=%jd>", arg);
151}
152
153_EOF_
154}
155
156# C start
157
158cat <<_EOF_
159#include <stdint.h>
160#include <stdio.h>
161#include <sys/fcntl.h>
162#include <sys/stat.h>
163#include <sys/unistd.h>
164#include <sys/mman.h>
165#include <sys/wait.h>
166#define _KERNEL
167#include <sys/socket.h>
168#undef _KERNEL
169#include <netinet/in.h>
170#include <sys/param.h>
171#include <sys/mount.h>
172#include <sys/procctl.h>
173#include <sys/ptrace.h>
174#include <sys/resource.h>
175#include <sys/reboot.h>
176#include <sched.h>
177#include <sys/linker.h>
178#define _KERNEL
179#include <sys/thr.h>
180#undef _KERNEL
181#include <sys/extattr.h>
182#include <sys/acl.h>
183#include <aio.h>
184#include <sys/sem.h>
185#include <sys/ipc.h>
186#include <sys/rtprio.h>
187#include <sys/shm.h>
188#include <nfsserver/nfs.h>
189#include <ufs/ufs/quota.h>
190#include <sys/capability.h>
191#include <vm/vm.h>
192#include <vm/vm_param.h>
193
194#include "kdump_subr.h"
195
196/*
197 * These are simple support macros. print_or utilizes a variable
198 * defined in the calling function to track whether or not it should
199 * print a logical-OR character ('|') before a string. if_print_or
200 * simply handles the necessary "if" statement used in many lines
201 * of this file.
202 */
203#define print_or(str,orflag) do {                  \\
204	if (orflag) putchar('|'); else orflag = 1; \\
205	printf (str); }                            \\
206	while (0)
207#define if_print_or(i,flag,orflag) do {            \\
208	if ((i & flag) == flag)                    \\
209	print_or(#flag,orflag); }                  \\
210	while (0)
211
212/* MANUAL */
213void
214atfdname(int fd, int decimal)
215{
216	if (fd == AT_FDCWD)
217		printf("AT_FDCWD");
218	else if (decimal)
219		printf("%d", fd);
220	else
221		printf("%#x", fd);
222}
223
224/* MANUAL */
225extern char *signames[]; /* from kdump.c */
226void
227signame(int sig)
228{
229	if (sig > 0 && sig < NSIG)
230		printf("SIG%s",signames[sig]);
231	else
232		printf("SIG %d", sig);
233}
234
235/* MANUAL */
236void
237semctlname(int cmd)
238{
239	switch (cmd) {
240	case GETNCNT:
241		printf("GETNCNT");
242		break;
243	case GETPID:
244		printf("GETPID");
245		break;
246	case GETVAL:
247		printf("GETVAL");
248		break;
249	case GETALL:
250		printf("GETALL");
251		break;
252	case GETZCNT:
253		printf("GETZCNT");
254		break;
255	case SETVAL:
256		printf("SETVAL");
257		break;
258	case SETALL:
259		printf("SETALL");
260		break;
261	case IPC_RMID:
262		printf("IPC_RMID");
263		break;
264	case IPC_SET:
265		printf("IPC_SET");
266		break;
267	case IPC_STAT:
268		printf("IPC_STAT");
269		break;
270	default: /* Should not reach */
271		printf("<invalid=%d>", cmd);
272	}
273}
274
275/* MANUAL */
276void
277shmctlname(int cmd)
278{
279	switch (cmd) {
280	case IPC_RMID:
281		printf("IPC_RMID");
282		break;
283	case IPC_SET:
284		printf("IPC_SET");
285		break;
286	case IPC_STAT:
287		printf("IPC_STAT");
288		break;
289	default: /* Should not reach */
290		printf("<invalid=%d>", cmd);
291	}
292}
293
294/* MANUAL */
295void
296semgetname(int flag)
297{
298	int or = 0;
299	if_print_or(flag, IPC_CREAT, or);
300	if_print_or(flag, IPC_EXCL, or);
301	if_print_or(flag, SEM_R, or);
302	if_print_or(flag, SEM_A, or);
303	if_print_or(flag, (SEM_R>>3), or);
304	if_print_or(flag, (SEM_A>>3), or);
305	if_print_or(flag, (SEM_R>>6), or);
306	if_print_or(flag, (SEM_A>>6), or);
307}
308
309/*
310 * MANUAL
311 *
312 * Only used by SYS_open. Unless O_CREAT is set in flags, the
313 * mode argument is unused (and often bogus and misleading).
314 */
315void
316flagsandmodename(int flags, int mode, int decimal)
317{
318	flagsname(flags);
319	putchar(',');
320	if ((flags & O_CREAT) == O_CREAT) {
321		modename (mode);
322	} else {
323		if (decimal) {
324			printf("<unused>%d", mode);
325		} else {
326			printf("<unused>%#x", (unsigned int)mode);
327		}
328	}
329}
330
331/* MANUAL */
332void
333idtypename(idtype_t idtype, int decimal)
334{
335	switch(idtype) {
336	case P_PID:
337		printf("P_PID");
338		break;
339	case P_PPID:
340		printf("P_PPID");
341		break;
342	case P_PGID:
343		printf("P_PGID");
344		break;
345	case P_SID:
346		printf("P_SID");
347		break;
348	case P_CID:
349		printf("P_CID");
350		break;
351	case P_UID:
352		printf("P_UID");
353		break;
354	case P_GID:
355		printf("P_GID");
356		break;
357	case P_ALL:
358		printf("P_ALL");
359		break;
360	case P_LWPID:
361		printf("P_LWPID");
362		break;
363	case P_TASKID:
364		printf("P_TASKID");
365		break;
366	case P_PROJID:
367		printf("P_PROJID");
368		break;
369	case P_POOLID:
370		printf("P_POOLID");
371		break;
372	case P_JAILID:
373		printf("P_JAILID");
374		break;
375	case P_CTID:
376		printf("P_CTID");
377		break;
378	case P_CPUID:
379		printf("P_CPUID");
380		break;
381	case P_PSETID:
382		printf("P_PSETID");
383		break;
384	default:
385		if (decimal) {
386			printf("%d", idtype);
387		} else {
388			printf("%#x", idtype);
389		}
390	}
391}
392
393/*
394 * MANUAL
395 *
396 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
397 * referring to a line in /etc/protocols . It might be appropriate
398 * to use getprotoent(3) here.
399 */
400void
401sockoptlevelname(int level, int decimal)
402{
403	if (level == SOL_SOCKET) {
404		printf("SOL_SOCKET");
405	} else {
406		if (decimal) {
407			printf("%d", level);
408		} else {
409			printf("%#x", (unsigned int)level);
410		}
411	}
412}
413
414/*
415 * MANUAL
416 *
417 * Used for page fault type.  Cannot use auto_or_type since the macro
418 * values contain a cast.  Also, VM_PROT_NONE has to be handled specially.
419 */
420void
421vmprotname (int type)
422{
423	int	or = 0;
424
425	if (type == VM_PROT_NONE) {
426		(void)printf("VM_PROT_NONE");
427		return;
428	}
429	if_print_or(type, VM_PROT_READ, or);
430	if_print_or(type, VM_PROT_WRITE, or);
431	if_print_or(type, VM_PROT_EXECUTE, or);
432	if_print_or(type, VM_PROT_COPY, or);
433}
434
435/*
436 * MANUAL
437 */
438void
439socktypenamewithflags(int type)
440{
441	if (type & SOCK_CLOEXEC)
442		printf("SOCK_CLOEXEC|"), type &= ~SOCK_CLOEXEC;
443	if (type & SOCK_NONBLOCK)
444		printf("SOCK_NONBLOCK|"), type &= ~SOCK_NONBLOCK;
445	socktypename(type);
446}
447_EOF_
448
449auto_or_type     "accessmodename"      "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+"         "sys/unistd.h"
450auto_switch_type "acltypename"         "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+"        "sys/acl.h"
451auto_or_type     "capfcntlname"        "CAP_FCNTL_[A-Z]+[[:space:]]+\(1"              "sys/capability.h"
452auto_switch_type "extattrctlname"      "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
453auto_switch_type "fadvisebehavname"    "POSIX_FADV_[A-Z]+[[:space:]]+[0-9]+"          "sys/fcntl.h"
454auto_or_type     "flagsname"           "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"           "sys/fcntl.h"
455auto_or_type     "flockname"           "LOCK_[A-Z]+[[:space:]]+0x[0-9]+"              "sys/fcntl.h"
456auto_or_type     "getfsstatflagsname"  "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*"            "sys/mount.h"
457auto_switch_type "kldsymcmdname"       "KLDSYM_[A-Z]+[[:space:]]+[0-9]+"              "sys/linker.h"
458auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+"       "sys/linker.h"
459auto_switch_type "lio_listioname"      "LIO_(NO)?WAIT[[:space:]]+[0-9]+"              "aio.h"
460auto_switch_type "madvisebehavname"    "_?MADV_[A-Z]+[[:space:]]+[0-9]+"              "sys/mman.h"
461auto_switch_type "minheritname"        "INHERIT_[A-Z]+[[:space:]]+[0-9]+"             "sys/mman.h"
462auto_or_type     "mlockallname"        "MCL_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mman.h"
463auto_or_type     "mmapprotname"        "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+"        "sys/mman.h"
464auto_or_type     "modename"            "S_[A-Z]+[[:space:]]+[0-6]{7}"                 "sys/stat.h"
465auto_or_type     "mountflagsname"      "MNT_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/mount.h"
466auto_switch_type "msyncflagsname"      "MS_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/mman.h"
467auto_or_type     "nfssvcname"          "NFSSVC_[A-Z0-9]+[[:space:]]+0x[0-9]+"            "nfs/nfssvc.h"
468auto_switch_type "prioname"            "PRIO_[A-Z]+[[:space:]]+[0-9]"                 "sys/resource.h"
469auto_switch_type "procctlcmdname"      "PROC_[A-Z]+[[:space:]]+[0-9]"                 "sys/procctl.h"
470auto_switch_type "ptraceopname"        "PT_[[:alnum:]_]+[[:space:]]+[0-9]+"           "sys/ptrace.h"
471auto_switch_type "quotactlname"        "Q_[A-Z]+[[:space:]]+0x[0-9]+"                 "ufs/ufs/quota.h"
472auto_or_type     "rebootoptname"       "RB_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/reboot.h"
473auto_or_type     "rforkname"           "RF[A-Z]+[[:space:]]+\([0-9]+<<[0-9]+\)"       "sys/unistd.h"
474auto_switch_type "rlimitname"          "RLIMIT_[A-Z]+[[:space:]]+[0-9]+"              "sys/resource.h"
475auto_switch_type "schedpolicyname"     "SCHED_[A-Z]+[[:space:]]+[0-9]+"               "sched.h"
476auto_switch_type "sendfileflagsname"   "SF_[A-Z]+[[:space:]]+[0-9]+"                  "sys/socket.h"
477auto_or_type     "shmatname"           "SHM_[A-Z]+[[:space:]]+[0-9]{6}+"              "sys/shm.h"
478auto_switch_type "shutdownhowname"     "SHUT_[A-Z]+[[:space:]]+[0-9]+"                "sys/socket.h"
479auto_switch_type "sigbuscodename"      "BUS_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
480auto_switch_type "sigchldcodename"     "CLD_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
481auto_switch_type "sigfpecodename"      "FPE_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
482auto_switch_type "sigprocmaskhowname"  "SIG_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
483auto_switch_type "sigillcodename"      "ILL_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
484auto_switch_type "sigsegvcodename"     "SEGV_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
485auto_switch_type "sigtrapcodename"     "TRAP_[A-Z]+[[:space:]]+[0-9]+"                 "sys/signal.h"
486auto_if_type     "sockdomainname"      "PF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
487auto_if_type     "sockfamilyname"      "AF_[[:alnum:]]+[[:space:]]+"                  "sys/socket.h"
488auto_if_type     "sockipprotoname"     "IPPROTO_[[:alnum:]]+[[:space:]]+"             "netinet/in.h"
489auto_switch_type "sockoptname"         "SO_[A-Z]+[[:space:]]+0x[0-9]+"                "sys/socket.h"
490auto_switch_type "socktypename"        "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*"          "sys/socket.h"
491auto_or_type     "thrcreateflagsname"  "THR_[A-Z]+[[:space:]]+0x[0-9]+"               "sys/thr.h"
492auto_switch_type "vmresultname"        "KERN_[A-Z]+[[:space:]]+[0-9]+"                "vm/vm_param.h"
493auto_or_type     "wait6optname"        "W[A-Z]+[[:space:]]+[0-9]+"                    "sys/wait.h"
494auto_switch_type "whencename"          "SEEK_[A-Z]+[[:space:]]+[0-9]+"                "sys/unistd.h"
495
496cat <<_EOF_
497/*
498 * AUTO - Special
499 * F_ is used to specify fcntl commands as well as arguments. Both sets are
500 * grouped in fcntl.h, and this awk script grabs the first group.
501 */
502void
503fcntlcmdname(int cmd, int arg, int decimal)
504{
505	switch (cmd) {
506_EOF_
507egrep "^#[[:space:]]*define[[:space:]]+F_[A-Z0-9_]+[[:space:]]+[0-9]+[[:space:]]*" \
508	$include_dir/sys/fcntl.h | \
509	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
510		if ($i ~ /define/) \
511			break; \
512		++i; \
513		if (o <= $(i+1)) \
514			printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i; \
515		else \
516			exit; \
517		o = $(i+1) }'
518cat <<_EOF_
519	default: /* Should not reach */
520		printf("<invalid=%d>", cmd);
521	}
522	putchar(',');
523	if (cmd == F_GETFD || cmd == F_SETFD) {
524		if (arg == FD_CLOEXEC)
525			printf("FD_CLOEXEC");
526		else if (arg == 0)
527			printf("0");
528		else {
529			if (decimal)
530				printf("<invalid>%d", arg);
531			else
532				printf("<invalid>%#x", (unsigned int)arg);
533		}
534	} else if (cmd == F_SETFL) {
535		flagsname(arg);
536	} else {
537		if (decimal)
538			printf("%d", arg);
539		else
540			printf("%#x", (unsigned int)arg);
541	}
542}
543
544/*
545 * AUTO - Special
546 *
547 * The MAP_ALIGNED flag requires special handling.
548 */
549void
550mmapflagsname(int flags)
551{
552	int align;
553	int or = 0;
554	printf("%#x<", flags);
555_EOF_
556egrep "^#[[:space:]]*define[[:space:]]+MAP_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+[[:space:]]*" \
557	$include_dir/sys/mman.h | grep -v MAP_ALIGNED | \
558	awk '{ for (i = 1; i <= NF; i++) \
559		if ($i ~ /define/) \
560			break; \
561		++i; \
562		printf "\tif (!((flags > 0) ^ ((%s) > 0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
563cat <<_EOF_
564#ifdef MAP_32BIT
565	if (!((flags > 0) ^ ((MAP_32BIT) > 0)))
566		if_print_or(flags, MAP_32BIT, or);
567#endif
568	align = flags & MAP_ALIGNMENT_MASK;
569	if (align != 0) {
570		if (align == MAP_ALIGNED_SUPER)
571			print_or("MAP_ALIGNED_SUPER", or);
572		else {
573			print_or("MAP_ALIGNED", or);
574			printf("(%d)", align >> MAP_ALIGNMENT_SHIFT);
575		}
576	}
577	printf(">");
578	if (or == 0)
579		printf("<invalid>%d", flags);
580}
581
582/*
583 * AUTO - Special
584 *
585 * The only reason this is not fully automated is due to the
586 * grep -v RTP_PRIO statement. A better egrep line should
587 * make this capable of being a auto_switch_type() function.
588 */
589void
590rtprioname(int func)
591{
592	switch (func) {
593_EOF_
594egrep "^#[[:space:]]*define[[:space:]]+RTP_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
595	$include_dir/sys/rtprio.h | grep -v RTP_PRIO | \
596	awk '{ for (i = 1; i <= NF; i++) \
597		if ($i ~ /define/) \
598			break; \
599		++i; \
600		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
601cat <<_EOF_
602	default: /* Should not reach */
603		printf("<invalid=%d>", func);
604	}
605}
606
607/*
608 * AUTO - Special
609 *
610 * The send and recv functions have a flags argument which can be
611 * set to 0. There is no corresponding #define. The auto_ functions
612 * detect this as "invalid", which is incorrect here.
613 */
614void
615sendrecvflagsname(int flags)
616{
617	int or = 0;
618
619	if (flags == 0) {
620		printf("0");
621		return;
622	}
623
624	printf("%#x<", flags);
625_EOF_
626egrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
627	awk '{ for (i = 1; i <= NF; i++) \
628		if ($i ~ /define/) \
629			break; \
630		++i; \
631		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
632cat <<_EOF_
633	printf(">");
634}
635
636/*
637 * AUTO - Special
638 *
639 * Check general codes first, then defer to signal-specific codes.
640 */
641void
642sigcodename(int sig, int code)
643{
644	switch (code) {
645_EOF_
646egrep "^#[[:space:]]*define[[:space:]]+SI_[A-Z]+[[:space:]]+0(x[0-9abcdef]+)?[[:space:]]*" \
647	$include_dir/sys/signal.h | grep -v SI_UNDEFINED | \
648	awk '{ for (i = 1; i <= NF; i++) \
649		if ($i ~ /define/) \
650			break; \
651		++i; \
652		printf "\tcase %s:\n\t\tprintf(\"%s\");\n\t\tbreak;\n", $i, $i }'
653cat <<_EOF_
654	default:
655		switch (sig) {
656		case SIGILL:
657			sigillcodename(code);
658			break;
659		case SIGBUS:
660			sigbuscodename(code);
661			break;
662		case SIGSEGV:
663			sigsegvcodename(code);
664			break;
665		case SIGFPE:
666			sigfpecodename(code);
667			break;
668		case SIGTRAP:
669			sigtrapcodename(code);
670			break;
671		case SIGCHLD:
672			sigchldcodename(code);
673			break;
674		default:
675			printf("<invalid=%#x>", code);
676		}
677	}
678}
679
680_EOF_
681egrep '#define[[:space:]]+CAP_[A-Z_]+[[:space:]]+CAPRIGHT\([0-9],[[:space:]]+0x[0-9]{16}ULL\)' \
682	$include_dir/sys/capability.h | \
683	sed -E 's/[	]+/ /g' | \
684	awk -F '[   \(,\)]' '
685	BEGIN {
686		printf "void\n"
687		printf "capname(const cap_rights_t *rightsp)\n"
688		printf "{\n"
689		printf "\tint comma = 0;\n\n"
690		printf "\tprintf(\"<\");\n"
691	}
692	{
693		printf "\tif ((rightsp->cr_rights[%s] & %s) == %s) {\n", $4, $2, $2
694		printf "\t\tif (comma) printf(\",\"); else comma = 1;\n"
695		printf "\t\tprintf(\"%s\");\n", $2
696		printf "\t}\n"
697	}
698	END {
699		printf "\tprintf(\">\");\n"
700		printf "}\n"
701	}'
702