xref: /openbsd/usr.bin/kdump/mksubr (revision 471dbed6)
11b780f51Sotto#!/bin/sh
2*471dbed6Svisa# $OpenBSD: mksubr,v 1.40 2023/08/13 08:29:28 visa Exp $
31b780f51Sotto#
41b780f51Sotto# Copyright (c) 2006 David Kirchner <dpk@dpk.net>
51b780f51Sotto#
61b780f51Sotto# Permission to use, copy, modify, and distribute this software for any
71b780f51Sotto# purpose with or without fee is hereby granted, provided that the above
81b780f51Sotto# copyright notice and this permission notice appear in all copies.
91b780f51Sotto#
101b780f51Sotto# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111b780f51Sotto# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121b780f51Sotto# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131b780f51Sotto# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141b780f51Sotto# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151b780f51Sotto# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161b780f51Sotto# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171b780f51Sotto#
181b780f51Sotto# $FreeBSD: src/usr.bin/kdump/mksubr,v 1.17 2011/06/06 19:00:38 dchagin Exp $
191b780f51Sotto#
201b780f51Sotto# Generates kdump_subr.c
211b780f51Sotto# mkioctls is a special-purpose script, and works fine as it is
221b780f51Sotto# now, so it remains independent. The idea behind how it generates
231b780f51Sotto# its list was heavily borrowed here.
241b780f51Sotto#
251b780f51Sotto# Some functions here are automatically generated. This can mean
261b780f51Sotto# the user will see unusual kdump output or errors while building
271b780f51Sotto# if the underlying .h files are changed significantly.
281b780f51Sotto#
291b780f51Sotto# Key:
301b780f51Sotto# AUTO: Completely auto-generated with either the "or" or the "switch"
311b780f51Sotto# method.
321b780f51Sotto# AUTO - Special: Generated automatically, but with some extra commands
331b780f51Sotto# that the auto_*_type() functions are inappropriate for.
341b780f51Sotto# MANUAL: Manually entered and must therefore be manually updated.
351b780f51Sotto
361b780f51Sottoset -e
371b780f51Sotto
381b780f51SottoLC_ALL=C; export LC_ALL
391b780f51Sotto
401b780f51Sottoif [ -z "$1" ]
411b780f51Sottothen
421b780f51Sotto	echo "usage: sh $0 include-dir"
431b780f51Sotto	exit 1
441b780f51Sottofi
451b780f51Sottoinclude_dir=$1
461b780f51Sotto
471b780f51Sotto#
481b780f51Sotto# Automatically generates a C function that will print out the
491b780f51Sotto# numeric input as a pipe-delimited string of the appropriate
501b780f51Sotto# #define keys. ex:
51256998b4Sguenther# 0x1a4<S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH>
521b780f51Sotto# The XOR is necessary to prevent including the "0"-value in every
531b780f51Sotto# line.
541b780f51Sotto#
551b780f51Sottoauto_or_type () {
561b780f51Sotto	local name grep file
571b780f51Sotto	name=$1
581b780f51Sotto	grep=$2
591b780f51Sotto	file=$3
60fd279959Sguenther	format=${4-%#x}
611b780f51Sotto
621b780f51Sotto	cat <<_EOF_
631b780f51Sotto/* AUTO */
641b780f51Sottovoid
651b780f51Sotto$name (int arg)
661b780f51Sotto{
671b780f51Sotto	int	or = 0;
68fd279959Sguenther	printf("$format<", arg);
691b780f51Sotto_EOF_
701b780f51Sotto	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
711b780f51Sotto		$include_dir/$file | \
721b780f51Sotto	awk '{ for (i = 1; i <= NF; i++) \
731b780f51Sotto		if ($i ~ /define/) \
741b780f51Sotto			break; \
751b780f51Sotto		++i; \
761b780f51Sotto		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
771b780f51Sottocat <<_EOF_
781b780f51Sotto	printf(">");
791b780f51Sotto	if (or == 0)
8099636776Sguenther		(void)printf("<invalid>%d", arg);
811b780f51Sotto}
821b780f51Sotto
831b780f51Sotto_EOF_
841b780f51Sotto}
851b780f51Sotto
861b780f51Sotto#
8715c06776Sguenther# Like auto_or_type(), but a zero value is valid and prints as "0<>"
8815c06776Sguenther#
8915c06776Sguentherauto_orz_type () {
9015c06776Sguenther	local name grep file
9115c06776Sguenther	name=$1
9215c06776Sguenther	grep=$2
9315c06776Sguenther	file=$3
94fd279959Sguenther	format=${4-%#x}
9515c06776Sguenther
9615c06776Sguenther	cat <<_EOF_
9715c06776Sguenther/* AUTO */
9815c06776Sguenthervoid
9915c06776Sguenther$name (int arg)
10015c06776Sguenther{
10115c06776Sguenther	int	or = 0;
10215c06776Sguenther	if (arg == 0) {
10315c06776Sguenther		printf("0<>");
10415c06776Sguenther		return;
10515c06776Sguenther	}
106fd279959Sguenther	printf("$format<", arg);
10715c06776Sguenther_EOF_
10815c06776Sguenther	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
10915c06776Sguenther		$include_dir/$file | \
11015c06776Sguenther	awk '{ for (i = 1; i <= NF; i++) \
11115c06776Sguenther		if ($i ~ /define/) \
11215c06776Sguenther			break; \
11315c06776Sguenther		++i; \
11415c06776Sguenther		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
11515c06776Sguenthercat <<_EOF_
11615c06776Sguenther	printf(">");
11715c06776Sguenther	if (or == 0)
11899636776Sguenther		(void)printf("<invalid>%d", arg);
11915c06776Sguenther}
12015c06776Sguenther
12115c06776Sguenther_EOF_
12215c06776Sguenther}
12315c06776Sguenther
12415c06776Sguenther#
125256998b4Sguenther# Automatically generates a C function that will print out a
126256998b4Sguenther# file flags input as a pipe-delimited string of the appropriate
127256998b4Sguenther# #define keys. ex:
128256998b4Sguenther# 0x30000<O_RDONLY|O_CLOEXEC|O_DIRECTORY>
129256998b4Sguenther# This is different than the others to handle O_RDONLY correctly when
130256998b4Sguenther# other flags are present and to diagnose an invalid O_ACCMODE value
131256998b4Sguenther#
132256998b4Sguentherauto_fflags_type () {
133256998b4Sguenther	local name grep file
134256998b4Sguenther	name=$1
135256998b4Sguenther	grep=$2
136256998b4Sguenther	file=$3
137256998b4Sguenther
138256998b4Sguenther	cat <<_EOF_
139256998b4Sguenther/* AUTO */
140256998b4Sguenthervoid
1414841054fSguenther$name (int arg, int show_accmode)
142256998b4Sguenther{
1434841054fSguenther	int	or = 0;
1444841054fSguenther
145256998b4Sguenther	printf("%#x<", arg);
1464841054fSguenther	if (show_accmode || (arg & O_ACCMODE)) {
1474841054fSguenther		or = 1;
148256998b4Sguenther		switch (arg & O_ACCMODE) {
149256998b4Sguenther		case O_RDONLY:
150256998b4Sguenther			printf("O_RDONLY");
151256998b4Sguenther			break;
152256998b4Sguenther		case O_WRONLY:
153256998b4Sguenther			printf("O_WRONLY");
154256998b4Sguenther			break;
155256998b4Sguenther		case O_RDWR:
156256998b4Sguenther			printf("O_RDWR");
157256998b4Sguenther			break;
158256998b4Sguenther		default:
159256998b4Sguenther			printf("<invalid>O_ACCMODE");
160256998b4Sguenther			break;
161256998b4Sguenther		}
1624841054fSguenther	}
163256998b4Sguenther_EOF_
164256998b4Sguenther	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
165256998b4Sguenther		$include_dir/$file | \
166256998b4Sguenther	egrep -v 'O_(RD(ONLY|WR)|WRONLY|ACCMODE)' | \
167256998b4Sguenther	awk '{ for (i = 1; i <= NF; i++) \
168256998b4Sguenther		if ($i ~ /define/) \
169256998b4Sguenther			break; \
170256998b4Sguenther		++i; \
1714841054fSguenther		printf "\tif_print_or(arg, %s, or);\n", $i }'
172256998b4Sguenthercat <<_EOF_
173256998b4Sguenther	printf(">");
174256998b4Sguenther}
175256998b4Sguenther
1764841054fSguenther/*
17799636776Sguenther * Wrappers of the above to use with pn()
1784841054fSguenther */
1794841054fSguenthervoid
1804841054fSguentherflagsname(int flags)
1814841054fSguenther{
1824841054fSguenther	doflagsname(flags, 0);
1834841054fSguenther}
1844841054fSguenther
18599636776Sguenthervoid
18699636776Sguentheropenflagsname(int flags)
18799636776Sguenther{
18899636776Sguenther	doflagsname(flags, 1);
18999636776Sguenther}
19099636776Sguenther
1914841054fSguenther
192256998b4Sguenther_EOF_
193256998b4Sguenther}
194256998b4Sguenther
195256998b4Sguenther
196256998b4Sguenther#
1971b780f51Sotto# Automatically generates a C function used when the argument
1981b780f51Sotto# maps to a single, specific #definition
1991b780f51Sotto#
2001b780f51Sottoauto_switch_type () {
2011b780f51Sotto	local name grep file
2021b780f51Sotto	name=$1
2031b780f51Sotto	grep=$2
2041b780f51Sotto	file=$3
2051b780f51Sotto
2061b780f51Sotto	cat <<_EOF_
2071b780f51Sotto/* AUTO */
2081b780f51Sottovoid
2091b780f51Sotto$name (int arg)
2101b780f51Sotto{
2111b780f51Sotto	switch (arg) {
2121b780f51Sotto_EOF_
2131b780f51Sotto	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
2141b780f51Sotto		$include_dir/$file | \
2151b780f51Sotto	awk '{ for (i = 1; i <= NF; i++) \
2161b780f51Sotto		if ($i ~ /define/) \
2171b780f51Sotto			break; \
2181b780f51Sotto		++i; \
2191b780f51Sotto		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
2201b780f51Sottocat <<_EOF_
2211b780f51Sotto	default: /* Should not reach */
22299636776Sguenther		(void)printf("<invalid=%d>", arg);
2231b780f51Sotto	}
2241b780f51Sotto}
2251b780f51Sotto
2261b780f51Sotto_EOF_
2271b780f51Sotto}
2281b780f51Sotto
2291b780f51Sotto#
2301b780f51Sotto# Automatically generates a C function used when the argument
2311b780f51Sotto# maps to a #definition
2321b780f51Sotto#
2331b780f51Sottoauto_if_type () {
2341b780f51Sotto	local name grep file
2351b780f51Sotto	name=$1
2361b780f51Sotto	grep=$2
2371b780f51Sotto	file=$3
2381b780f51Sotto
2391b780f51Sotto	cat <<_EOF_
2401b780f51Sotto/* AUTO */
2411b780f51Sottovoid
2421b780f51Sotto$name (int arg)
2431b780f51Sotto{
2441b780f51Sotto_EOF_
2451b780f51Sotto	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
2461b780f51Sotto		$include_dir/$file | \
2471b780f51Sotto	awk '{ printf "\t"; \
2481b780f51Sotto		if (NR > 1) \
2491b780f51Sotto			printf "else " ; \
2501b780f51Sotto		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
2511b780f51Sottocat <<_EOF_
2521b780f51Sotto	else /* Should not reach */
25399636776Sguenther		(void)printf("<invalid=%d>", arg);
2541b780f51Sotto}
2551b780f51Sotto
2561b780f51Sotto_EOF_
2571b780f51Sotto}
2581b780f51Sotto
2591b780f51Sotto# C start
2601b780f51Sotto
2611b780f51Sottocat <<_EOF_
26220105486Sderaadt#include <sys/types.h>
26320105486Sderaadt#include <sys/signal.h>
2648449a441Sguenther#include <sys/event.h>
2651b780f51Sotto#include <sys/fcntl.h>
2661b780f51Sotto#include <sys/stat.h>
2671b780f51Sotto#include <sys/unistd.h>
268391dcabbSmatthew#define _KERNEL
2691b780f51Sotto#include <sys/mman.h>
270391dcabbSmatthew#undef _KERNEL
2711b780f51Sotto#include <sys/wait.h>
2724f9d0979Sotto#include <sys/proc.h>
2731b780f51Sotto#include <sys/socket.h>
2741b780f51Sotto#include <netinet/in.h>
2751b780f51Sotto#include <sys/mount.h>
276ee6ae511Sguenther#include <sys/poll.h>
2771b780f51Sotto#include <sys/ptrace.h>
2781b780f51Sotto#include <sys/resource.h>
2791b780f51Sotto#include <sys/reboot.h>
280a70ae221Sguenther#include <sys/uio.h>
281a70ae221Sguenther#include <sys/ktrace.h>
2821b780f51Sotto#include <sched.h>
2834f9d0979Sotto#if 0
2841b780f51Sotto#include <sys/linker.h>
2851b780f51Sotto#define _KERNEL
2861b780f51Sotto#include <sys/thr.h>
2871b780f51Sotto#undef _KERNEL
2881b780f51Sotto#include <sys/extattr.h>
2891b780f51Sotto#include <sys/acl.h>
2901b780f51Sotto#include <aio.h>
2914f9d0979Sotto#endif
2921b780f51Sotto#include <sys/sem.h>
2931b780f51Sotto#include <sys/ipc.h>
2944f9d0979Sotto#if 0
2951b780f51Sotto#include <sys/rtprio.h>
2964f9d0979Sotto#endif
2971b780f51Sotto#include <sys/shm.h>
2984f9d0979Sotto#if 0
2991b780f51Sotto#include <nfsserver/nfs.h>
3004f9d0979Sotto#endif
3011b780f51Sotto#include <ufs/ufs/quota.h>
302968c3b13Sguenther#include <sys/syslog.h>
303c7d77250Smpi#include <sys/futex.h>
30420105486Sderaadt#include <stdio.h>
3051b780f51Sotto
3061b780f51Sotto#include "kdump_subr.h"
3071b780f51Sotto
3081b780f51Sotto_EOF_
3091b780f51Sotto
310fd279959Sguentherauto_orz_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h" "%#o"
3114841054fSguentherauto_fflags_type "doflagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
31215c06776Sguentherauto_orz_type "atflagsname" "AT_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
313b8dac8f2Sottoauto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
3141b780f51Sottoauto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
315653a2889Smatthewauto_or_type "mmapflagsname" "(__)?MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
3169f38b5b2Sguentherauto_orz_type "wait4optname" "W(NOHANG|UNTRACED|CONTINUED)[[:space:]]+[0-9]+" "sys/wait.h"
3179f38b5b2Sguentherauto_or_type "waitidoptname" "W(NO[A-Z]+|[A-T][A-Z]*ED)[[:space:]]+([0-9]+|W[A-Z]+)" "sys/wait.h"
31815c06776Sguenther#auto_or_type "timerflagsname" "TIMER_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/time.h"
3194f9d0979Sotto#auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
3207188589bSottoauto_orz_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
32199636776Sguentherauto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
322b8dac8f2Sottoauto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
3234f9d0979Sotto#auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
324b8dac8f2Sottoauto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
32515c06776Sguentherauto_orz_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}" "sys/shm.h"
3264f9d0979Sotto#auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
3274f9d0979Sotto#
328b8dac8f2Sottoauto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
329ee6ae511Sguentherauto_switch_type "pathconfname" "_PC_[_A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
330b8dac8f2Sottoauto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
331ee6ae511Sguentherauto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
33299636776Sguentherauto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
33372adc110Sottoauto_switch_type "madvisebehavname" "MADV_[A-Z]+[[:space:]]+[0-9A-Z_]+" "sys/mman.h"
334b8dac8f2Sottoauto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
3357b36c281Sguentherauto_switch_type "clocktypename" "CLOCK_[_A-Z]+[[:space:]]+[0-9]+" "sys/_time.h"
3364f9d0979Sotto#auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
3374f9d0979Sotto#auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
3384f9d0979Sotto#auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
3394f9d0979Sotto#auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
3404f9d0979Sotto#auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
3414f9d0979Sotto#auto_switch_type "acltypename" "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+" "sys/acl.h"
342ec5a39e0Sguentherauto_switch_type "rusagewho" "RUSAGE_[A-Z]+[[:space:]]+[-0-9()]+" "sys/resource.h"
3439ece112bSguentherauto_orz_type "sigactionflagname" "SA_[A-Z]+[[:space:]]+0x[0-9]+" "sys/signal.h"
3441b780f51Sottoauto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
3458b7c6933Sderaadtauto_switch_type "sigill_name" "ILL_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3468b7c6933Sderaadtauto_switch_type "sigtrap_name" "TRAP_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3478b7c6933Sderaadtauto_switch_type "sigemt_name" "EMT_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3488b7c6933Sderaadtauto_switch_type "sigfpe_name" "FPE_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3498b7c6933Sderaadtauto_switch_type "sigbus_name" "BUS_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3508b7c6933Sderaadtauto_switch_type "sigsegv_name" "SEGV_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3518b7c6933Sderaadtauto_switch_type "sigchld_name" "CLD_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
3524f9d0979Sotto#auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
353b59026c7Sottoauto_switch_type "minheritname" "MAP_INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
3541338505aSguentherauto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
3556bbfbb88Sguenther#auto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
356f980d548Sottoauto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
357b8dac8f2Sottoauto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
358b8dac8f2Sottoauto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
3594f9d0979Sotto#auto_switch_type "ptraceopname" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h"
3608449a441Sguenther# exclude KTRFAC_MASK
361a70ae221Sguentherauto_orz_type "ktracefacname" "KTRFAC_[^M][[:alnum:]_]+" "sys/ktrace.h"
362a70ae221Sguentherauto_switch_type "itimername" "ITIMER_[[:alnum:]_]+" "sys/time.h"
3638449a441Sguentherauto_switch_type "evfiltername" "EVFILT_[[:alnum:]_]+[[:space:]]+[(]" "sys/event.h"
364cd632112Sderaadtauto_orz_type "pollfdeventname" "POLL[^_][[:alnum:]_]+[[:space:]]+0x" "sys/poll.h"
3658449a441Sguenther# exclude EV_{SYSFLAGS,FLAG1}
3668449a441Sguentherauto_orz_type "evflagsname" "EV_[^S][A-Z]+[[:space:]]+0x" "sys/event.h"
367968c3b13Sguentherauto_orz_type "syslogflagname" "LOG_[A-Z]+[[:space:]]+0x0*[1248]0*[[:space:]]" "sys/syslog.h"
368c7d77250Smpiauto_orz_type "futexflagname" "FUTEX_[A-Z_]+[[:space:]]+[0-9]+" "sys/futex.h"
3695c402730Santonauto_switch_type "flocktypename" "F_[A-Z]+LCK" "sys/fcntl.h"
3701b780f51Sotto
3711b780f51Sottocat <<_EOF_
3721b780f51Sotto/*
3731b780f51Sotto * AUTO - Special
3741b780f51Sotto * F_ is used to specify fcntl commands as well as arguments. Both sets are
3751b780f51Sotto * grouped in fcntl.h, and this awk script grabs the first group.
3761b780f51Sotto */
3771b780f51Sottovoid
37899636776Sguentherfcntlcmdname (int arg)
3791b780f51Sotto{
38051c33b3fSguenther	int noarg = 0;
38151c33b3fSguenther
38299636776Sguenther	switch (arg1) {
3831b780f51Sotto_EOF_
3844805374fSmatthewegrep "^#[[:space:]]*define[[:space:]]+F_[A-Z_]+[[:space:]]+[0-9]+[[:space:]]*" \
3851b780f51Sotto	$include_dir/sys/fcntl.h | \
38651c33b3fSguenther	awk 'BEGIN { o=0; \
38751c33b3fSguenther		noarg["F_GETFD"] = 1; \
38851c33b3fSguenther		noarg["F_GETFL"] = 1; \
38951c33b3fSguenther		noarg["F_ISATTY"] = 1; \
39051c33b3fSguenther		noarg["F_GETOWN"] = 1; \
39151c33b3fSguenther	     }{ for (i = 1; i <= NF; i++) \
3921b780f51Sotto		if ($i ~ /define/) \
3931b780f51Sotto			break; \
3941b780f51Sotto		++i; \
39551c33b3fSguenther		if (o > $(i+1)) \
3961b780f51Sotto			exit; \
39751c33b3fSguenther		printf "\tcase %s:\n\t\t(void)printf(\"%s\");%s\n\t\tbreak;\n", $i, $i, \
39851c33b3fSguenther			noarg[$i] ? "\n\t\tnoarg = 1;" : ""; \
3991b780f51Sotto		o = $(i+1) }'
4001b780f51Sottocat <<_EOF_
4011b780f51Sotto	default: /* Should not reach */
40299636776Sguenther		(void)printf("<invalid=%d>", arg1);
4031b780f51Sotto	}
40499636776Sguenther	if (arg1 == F_SETFD) {
4051b780f51Sotto		(void)putchar(',');
4061b780f51Sotto		if (arg == FD_CLOEXEC)
4071b780f51Sotto			(void)printf("FD_CLOEXEC");
4081b780f51Sotto		else if (arg == 0)
4091b780f51Sotto			(void)printf("0");
4101b780f51Sotto		else
41199636776Sguenther			(void)printf("<invalid>%#x", arg);
412ee6ae511Sguenther
41399636776Sguenther	} else if (arg1 == F_SETFL) {
414ee6ae511Sguenther		(void)putchar(',');
41599636776Sguenther		doflagsname(arg, 0);
41651c33b3fSguenther	} else if (!fancy || !noarg)
41799636776Sguenther		(void)printf(",%#x", arg);
4181b780f51Sotto}
4191b780f51Sotto
4201b780f51Sotto/*
4211b780f51Sotto * AUTO - Special
4221b780f51Sotto *
4231b780f51Sotto * The send and recv functions have a flags argument which can be
4241b780f51Sotto * set to 0. There is no corresponding #define. The auto_ functions
4251b780f51Sotto * detect this as "invalid", which is incorrect here.
4261b780f51Sotto */
4271b780f51Sottovoid
4281b780f51Sottosendrecvflagsname (int flags)
4291b780f51Sotto{
4301b780f51Sotto	int	or = 0;
4311b780f51Sotto
4321b780f51Sotto	if (flags == 0) {
4331b780f51Sotto		(void)printf("0");
4341b780f51Sotto		return;
4351b780f51Sotto	}
4361b780f51Sotto
4371b780f51Sotto	printf("%#x<", flags);
4381b780f51Sotto_EOF_
4394841054fSguentheregrep "^#[[:space:]]*define[[:space:]]+MSG_[_A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
4401b780f51Sotto	awk '{ for (i = 1; i <= NF; i++) \
4411b780f51Sotto		if ($i ~ /define/) \
4421b780f51Sotto			break; \
4431b780f51Sotto		++i; \
4441b780f51Sotto		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
4451b780f51Sottocat <<_EOF_
4461b780f51Sotto	printf(">");
4471b780f51Sotto}
4481b780f51Sotto
4494841054fSguenther/*
4504841054fSguenther * AUTO - Special
4514841054fSguenther *
4524841054fSguenther * SOCK_NONBLOCK and SOCK_CLOEXEC are or'ed into the type
4534841054fSguenther */
4544841054fSguentherstatic void
4554841054fSguentherdosocktypename (int arg, int show_type)
4564841054fSguenther{
4574841054fSguenther	int	type = arg & 0xff;		/* XXX */
4584841054fSguenther	int	or = 0;
4594841054fSguenther	
4604841054fSguenther	printf("%#x<", arg);
4614841054fSguenther	if (show_type || type) {
4624841054fSguenther		or = 1;
4634841054fSguenther		switch (type) {
4644841054fSguenther_EOF_
4654841054fSguenther	egrep "^#[[:space:]]*define[[:space:]]+SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*[[:space:]]*" \
4664841054fSguenther		$include_dir/sys/socket.h | \
4674841054fSguenther	awk '{ for (i = 1; i <= NF; i++) \
4684841054fSguenther		if ($i ~ /define/) \
4694841054fSguenther			break; \
4704841054fSguenther		++i; \
471a394ada8Sguenther		printf "\t\tcase %s:\n\t\t\t(void)printf(\"%s\");\n\t\t\tbreak;\n", $i, $i }'
4724841054fSguenthercat <<_EOF_
4734841054fSguenther		default: /* Should not reach */
4744841054fSguenther			(void)printf("<invalid=%d>", arg);
4754841054fSguenther		}
4764841054fSguenther	}
4774841054fSguenther
4784841054fSguenther_EOF_
4794841054fSguenther	egrep "^#[[:space:]]*define[[:space:]]+SOCK_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" \
4804841054fSguenther		$include_dir/sys/socket.h | \
4814841054fSguenther	awk '{ for (i = 1; i <= NF; i++) \
4824841054fSguenther		if ($i ~ /define/) \
4834841054fSguenther			break; \
4844841054fSguenther		++i; \
4854841054fSguenther		printf "\tif_print_or(arg, %s, or);\n", $i }'
4864841054fSguenthercat <<_EOF_
4874841054fSguenther	printf(">");
4884841054fSguenther}
4894841054fSguenther
4904841054fSguenthervoid
4914841054fSguenthersocktypename (int arg)
4924841054fSguenther{
4934841054fSguenther	dosocktypename(arg, 1);
4944841054fSguenther}
4954841054fSguenther
4964841054fSguenthervoid
4974841054fSguenthersockflagsname (int arg)
4984841054fSguenther{
4994841054fSguenther	dosocktypename(arg, 0);
5004841054fSguenther}
5014841054fSguenther
5021338505aSguenthervoid
5031338505aSguentherquotactlcmdname(int cmd)
5041338505aSguenther{
505a394ada8Sguenther	printf("%#x<QCMD(", cmd);
5061338505aSguenther	quotactlname(cmd >> SUBCMDSHIFT);
5071338505aSguenther	switch (cmd & SUBCMDMASK) {
5081338505aSguenther	case USRQUOTA:
509a394ada8Sguenther		printf(",%s)>", "USRQUOTA");
5101338505aSguenther		break;
5111338505aSguenther	case GRPQUOTA:
512a394ada8Sguenther		printf(",%s)>", "GRPQUOTA");
5131338505aSguenther		break;
5141338505aSguenther	default:
515a394ada8Sguenther		printf(",<invalid>%#x)>", cmd & SUBCMDMASK);
5161338505aSguenther		break;
5171338505aSguenther	}
5181338505aSguenther}
5191338505aSguenther
5208449a441Sguenther/*
5218449a441Sguenther * AUTO - Special
5228449a441Sguenther *
5238449a441Sguenther * kevent() NOTE_* interpretation depends on the filter type
5248449a441Sguenther */
5258449a441Sguenthervoid
5268449a441Sguentherevfflagsname (int filter, int fflags)
5278449a441Sguenther{
5288449a441Sguenther	int	or = 0;
5298449a441Sguenther
5308449a441Sguenther	if (fflags == 0) {
5318449a441Sguenther		printf("0<>");
5328449a441Sguenther		return;
5338449a441Sguenther	}
5348449a441Sguenther	printf("%#x<", fflags);
5358449a441Sguenther	switch (filter) {
5368449a441Sguenther	case EVFILT_READ:
5378449a441Sguenther	case EVFILT_WRITE:
5388449a441Sguenther		if_print_or(fflags, NOTE_LOWAT, or);
5398449a441Sguenther		if_print_or(fflags, NOTE_EOF, or);
5408449a441Sguenther		break;
5418449a441Sguenther	case EVFILT_VNODE:
5428449a441Sguenther_EOF_
5438449a441Sguenther	egrep "^#[[:space:]]*define[[:space:]]+NOTE_.[^O][A-Z]+[[:space:]]+0x[01248]{4}[^[:alnum:]]" \
5448449a441Sguenther		$include_dir/sys/event.h | \
5458449a441Sguenther	awk '{ for (i = 1; i <= NF; i++) \
5468449a441Sguenther		if ($i ~ /define/) \
5478449a441Sguenther			break; \
5488449a441Sguenther		++i; \
5498449a441Sguenther		printf "\t\tif_print_or(fflags, %s, or);\n", $i }'
5508449a441Sguenthercat <<_EOF_
5518449a441Sguenther		break;
5528449a441Sguenther	case EVFILT_PROC:
5538449a441Sguenther_EOF_
5548449a441Sguenther	egrep "^#[[:space:]]*define[[:space:]]+NOTE_[^S][A-Z]+[[:space:]]+0x[01248]{8}" \
5558449a441Sguenther		$include_dir/sys/event.h | \
5568449a441Sguenther	awk '{ for (i = 1; i <= NF; i++) \
5578449a441Sguenther		if ($i ~ /define/) \
5588449a441Sguenther			break; \
5598449a441Sguenther		++i; \
5608449a441Sguenther		printf "\t\tif_print_or(fflags, %s, or);\n", $i }'
5618449a441Sguenthercat <<_EOF_
5628449a441Sguenther		break;
563*471dbed6Svisa	case EVFILT_TIMER:
564*471dbed6Svisa#define NOTE_TIMER_UNITMASK \
565*471dbed6Svisa    (NOTE_SECONDS|NOTE_MSECONDS|NOTE_USECONDS|NOTE_NSECONDS)
566*471dbed6Svisa		switch (fflags & NOTE_TIMER_UNITMASK) {
567*471dbed6Svisa		case NOTE_SECONDS:
568*471dbed6Svisa			printf("NOTE_SECONDS");
569*471dbed6Svisa			break;
570*471dbed6Svisa		case NOTE_MSECONDS:
571*471dbed6Svisa			printf("NOTE_MSECONDS");
572*471dbed6Svisa			break;
573*471dbed6Svisa		case NOTE_USECONDS:
574*471dbed6Svisa			printf("NOTE_USECONDS");
575*471dbed6Svisa			break;
576*471dbed6Svisa		case NOTE_NSECONDS:
577*471dbed6Svisa			printf("NOTE_NSECONDS");
578*471dbed6Svisa			break;
579*471dbed6Svisa		default:
580*471dbed6Svisa			printf("invalid");
581*471dbed6Svisa			break;
582*471dbed6Svisa		}
583*471dbed6Svisa		or = 1;
584*471dbed6Svisa		if_print_or(fflags, NOTE_ABSTIME, or);
585*471dbed6Svisa		break;
5868449a441Sguenther	}
5878449a441Sguenther	printf(">");
5888449a441Sguenther}
5898449a441Sguenther
5901b780f51Sotto_EOF_
591