xref: /openbsd/usr.bin/kdump/mksubr (revision 256998b4)
11b780f51Sotto#!/bin/sh
2*256998b4Sguenther# $OpenBSD: mksubr,v 1.12 2012/07/08 10:23:36 guenther 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:
51*256998b4Sguenther# 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
601b780f51Sotto
611b780f51Sotto	cat <<_EOF_
621b780f51Sotto/* AUTO */
631b780f51Sottovoid
641b780f51Sotto$name (int arg)
651b780f51Sotto{
661b780f51Sotto	int	or = 0;
671b780f51Sotto	printf("%#x<", arg);
681b780f51Sotto_EOF_
691b780f51Sotto	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
701b780f51Sotto		$include_dir/$file | \
711b780f51Sotto	awk '{ for (i = 1; i <= NF; i++) \
721b780f51Sotto		if ($i ~ /define/) \
731b780f51Sotto			break; \
741b780f51Sotto		++i; \
751b780f51Sotto		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
761b780f51Sottocat <<_EOF_
771b780f51Sotto	printf(">");
781b780f51Sotto	if (or == 0)
791b780f51Sotto		(void)printf("<invalid>%ld", (long)arg);
801b780f51Sotto}
811b780f51Sotto
821b780f51Sotto_EOF_
831b780f51Sotto}
841b780f51Sotto
851b780f51Sotto#
8615c06776Sguenther# Like auto_or_type(), but a zero value is valid and prints as "0<>"
8715c06776Sguenther#
8815c06776Sguentherauto_orz_type () {
8915c06776Sguenther	local name grep file
9015c06776Sguenther	name=$1
9115c06776Sguenther	grep=$2
9215c06776Sguenther	file=$3
9315c06776Sguenther
9415c06776Sguenther	cat <<_EOF_
9515c06776Sguenther/* AUTO */
9615c06776Sguenthervoid
9715c06776Sguenther$name (int arg)
9815c06776Sguenther{
9915c06776Sguenther	int	or = 0;
10015c06776Sguenther	if (arg == 0) {
10115c06776Sguenther		printf("0<>");
10215c06776Sguenther		return;
10315c06776Sguenther	}
10415c06776Sguenther	printf("%#x<", arg);
10515c06776Sguenther_EOF_
10615c06776Sguenther	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
10715c06776Sguenther		$include_dir/$file | \
10815c06776Sguenther	awk '{ for (i = 1; i <= NF; i++) \
10915c06776Sguenther		if ($i ~ /define/) \
11015c06776Sguenther			break; \
11115c06776Sguenther		++i; \
11215c06776Sguenther		printf "\tif(!((arg>0)^((%s)>0)))\n\t\tif_print_or(arg, %s, or);\n", $i, $i }'
11315c06776Sguenthercat <<_EOF_
11415c06776Sguenther	printf(">");
11515c06776Sguenther	if (or == 0)
11615c06776Sguenther		(void)printf("<invalid>%ld", (long)arg);
11715c06776Sguenther}
11815c06776Sguenther
11915c06776Sguenther_EOF_
12015c06776Sguenther}
12115c06776Sguenther
12215c06776Sguenther#
123*256998b4Sguenther# Automatically generates a C function that will print out a
124*256998b4Sguenther# file flags input as a pipe-delimited string of the appropriate
125*256998b4Sguenther# #define keys. ex:
126*256998b4Sguenther# 0x30000<O_RDONLY|O_CLOEXEC|O_DIRECTORY>
127*256998b4Sguenther# This is different than the others to handle O_RDONLY correctly when
128*256998b4Sguenther# other flags are present and to diagnose an invalid O_ACCMODE value
129*256998b4Sguenther#
130*256998b4Sguentherauto_fflags_type () {
131*256998b4Sguenther	local name grep file
132*256998b4Sguenther	name=$1
133*256998b4Sguenther	grep=$2
134*256998b4Sguenther	file=$3
135*256998b4Sguenther
136*256998b4Sguenther	cat <<_EOF_
137*256998b4Sguenther/* AUTO */
138*256998b4Sguenthervoid
139*256998b4Sguenther$name (int arg)
140*256998b4Sguenther{
141*256998b4Sguenther	printf("%#x<", arg);
142*256998b4Sguenther	switch (arg & O_ACCMODE) {
143*256998b4Sguenther	case O_RDONLY:
144*256998b4Sguenther		printf("O_RDONLY");
145*256998b4Sguenther		break;
146*256998b4Sguenther	case O_WRONLY:
147*256998b4Sguenther		printf("O_WRONLY");
148*256998b4Sguenther		break;
149*256998b4Sguenther	case O_RDWR:
150*256998b4Sguenther		printf("O_RDWR");
151*256998b4Sguenther		break;
152*256998b4Sguenther	default:
153*256998b4Sguenther		printf("<invalid>O_ACCMODE");
154*256998b4Sguenther		break;
155*256998b4Sguenther	}
156*256998b4Sguenther_EOF_
157*256998b4Sguenther	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
158*256998b4Sguenther		$include_dir/$file | \
159*256998b4Sguenther	egrep -v 'O_(RD(ONLY|WR)|WRONLY|ACCMODE)' | \
160*256998b4Sguenther	awk '{ for (i = 1; i <= NF; i++) \
161*256998b4Sguenther		if ($i ~ /define/) \
162*256998b4Sguenther			break; \
163*256998b4Sguenther		++i; \
164*256998b4Sguenther		printf "\tif (arg & %s) printf (\"|%%s\", \"%s\");\n", $i, $i }'
165*256998b4Sguenthercat <<_EOF_
166*256998b4Sguenther	printf(">");
167*256998b4Sguenther}
168*256998b4Sguenther
169*256998b4Sguenther_EOF_
170*256998b4Sguenther}
171*256998b4Sguenther
172*256998b4Sguenther
173*256998b4Sguenther#
1741b780f51Sotto# Automatically generates a C function used when the argument
1751b780f51Sotto# maps to a single, specific #definition
1761b780f51Sotto#
1771b780f51Sottoauto_switch_type () {
1781b780f51Sotto	local name grep file
1791b780f51Sotto	name=$1
1801b780f51Sotto	grep=$2
1811b780f51Sotto	file=$3
1821b780f51Sotto
1831b780f51Sotto	cat <<_EOF_
1841b780f51Sotto/* AUTO */
1851b780f51Sottovoid
1861b780f51Sotto$name (int arg)
1871b780f51Sotto{
1881b780f51Sotto	switch (arg) {
1891b780f51Sotto_EOF_
1901b780f51Sotto	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
1911b780f51Sotto		$include_dir/$file | \
1921b780f51Sotto	awk '{ for (i = 1; i <= NF; i++) \
1931b780f51Sotto		if ($i ~ /define/) \
1941b780f51Sotto			break; \
1951b780f51Sotto		++i; \
1961b780f51Sotto		printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i }'
1971b780f51Sottocat <<_EOF_
1981b780f51Sotto	default: /* Should not reach */
1991b780f51Sotto		(void)printf("<invalid=%ld>", (long)arg);
2001b780f51Sotto	}
2011b780f51Sotto}
2021b780f51Sotto
2031b780f51Sotto_EOF_
2041b780f51Sotto}
2051b780f51Sotto
2061b780f51Sotto#
2071b780f51Sotto# Automatically generates a C function used when the argument
2081b780f51Sotto# maps to a #definition
2091b780f51Sotto#
2101b780f51Sottoauto_if_type () {
2111b780f51Sotto	local name grep file
2121b780f51Sotto	name=$1
2131b780f51Sotto	grep=$2
2141b780f51Sotto	file=$3
2151b780f51Sotto
2161b780f51Sotto	cat <<_EOF_
2171b780f51Sotto/* AUTO */
2181b780f51Sottovoid
2191b780f51Sotto$name (int arg)
2201b780f51Sotto{
2211b780f51Sotto_EOF_
2221b780f51Sotto	egrep "^#[[:space:]]*define[[:space:]]+"${grep}"[[:space:]]*" \
2231b780f51Sotto		$include_dir/$file | \
2241b780f51Sotto	awk '{ printf "\t"; \
2251b780f51Sotto		if (NR > 1) \
2261b780f51Sotto			printf "else " ; \
2271b780f51Sotto		printf "if (arg == %s) \n\t\tprintf(\"%s\");\n", $2, $2 }'
2281b780f51Sottocat <<_EOF_
2291b780f51Sotto	else /* Should not reach */
2301b780f51Sotto		(void)printf("<invalid=%ld>", (long)arg);
2311b780f51Sotto}
2321b780f51Sotto
2331b780f51Sotto_EOF_
2341b780f51Sotto}
2351b780f51Sotto
2361b780f51Sotto# C start
2371b780f51Sotto
2381b780f51Sottocat <<_EOF_
2391b780f51Sotto#include <stdio.h>
2404f9d0979Sotto#include <sys/param.h>
2411b780f51Sotto#include <sys/fcntl.h>
2421b780f51Sotto#include <sys/stat.h>
2431b780f51Sotto#include <sys/unistd.h>
2441b780f51Sotto#include <sys/mman.h>
2451b780f51Sotto#include <sys/wait.h>
2464f9d0979Sotto#include <sys/proc.h>
2471b780f51Sotto#define _KERNEL
2484f9d0979Sotto#define COMPAT_43
2491b780f51Sotto#include <sys/socket.h>
2501b780f51Sotto#undef _KERNEL
2511b780f51Sotto#include <netinet/in.h>
2521b780f51Sotto#include <sys/param.h>
2531b780f51Sotto#include <sys/mount.h>
2541b780f51Sotto#include <sys/ptrace.h>
2551b780f51Sotto#include <sys/resource.h>
2561b780f51Sotto#include <sys/reboot.h>
2571b780f51Sotto#include <sched.h>
2584f9d0979Sotto#if 0
2591b780f51Sotto#include <sys/linker.h>
2601b780f51Sotto#define _KERNEL
2611b780f51Sotto#include <sys/thr.h>
2621b780f51Sotto#undef _KERNEL
2631b780f51Sotto#include <sys/extattr.h>
2641b780f51Sotto#include <sys/acl.h>
2651b780f51Sotto#include <aio.h>
2664f9d0979Sotto#endif
2671b780f51Sotto#include <sys/sem.h>
2681b780f51Sotto#include <sys/ipc.h>
2694f9d0979Sotto#if 0
2701b780f51Sotto#include <sys/rtprio.h>
2714f9d0979Sotto#endif
2721b780f51Sotto#include <sys/shm.h>
2734f9d0979Sotto#if 0
2741b780f51Sotto#include <nfsserver/nfs.h>
2754f9d0979Sotto#endif
2761b780f51Sotto#include <ufs/ufs/quota.h>
2771b780f51Sotto
2781b780f51Sotto#include "kdump_subr.h"
2791b780f51Sotto
2801b780f51Sotto/*
2811b780f51Sotto * These are simple support macros. print_or utilizes a variable
2821b780f51Sotto * defined in the calling function to track whether or not it should
2831b780f51Sotto * print a logical-OR character ('|') before a string. if_print_or
2841b780f51Sotto * simply handles the necessary "if" statement used in many lines
2851b780f51Sotto * of this file.
2861b780f51Sotto */
2871b780f51Sotto#define print_or(str,orflag) do {                  \\
2881b780f51Sotto	if (orflag) putchar('|'); else orflag = 1; \\
28915c06776Sguenther	printf ("%s", str); }                      \\
2901b780f51Sotto	while (0)
2911b780f51Sotto#define if_print_or(i,flag,orflag) do {            \\
2921b780f51Sotto	if ((i & flag) == flag)                    \\
2931b780f51Sotto	print_or(#flag,orflag); }                  \\
2941b780f51Sotto	while (0)
2951b780f51Sotto
2961b780f51Sotto/* MANUAL */
2974f9d0979Sottoextern const char *const sys_signame[NSIG];
2981b780f51Sottovoid
2991b780f51Sottosigname (int sig)
3001b780f51Sotto{
3011b780f51Sotto	if (sig > 0 && sig < NSIG)
3024f9d0979Sotto		(void)printf("SIG%s", sys_signame[sig]);
3031b780f51Sotto	else
3041b780f51Sotto		(void)printf("SIG %d", sig);
3051b780f51Sotto}
3061b780f51Sotto
3071b780f51Sotto/* MANUAL */
3081b780f51Sottovoid
30915c06776Sguenthersigset (int ss)
31015c06776Sguenther{
31115c06776Sguenther	int	or = 0;
31215c06776Sguenther	int	cnt = 0;
31315c06776Sguenther	int	i;
31415c06776Sguenther
31515c06776Sguenther	for (i = 1; i < NSIG; i++)
31615c06776Sguenther		if (sigismember(&ss, i))
31715c06776Sguenther			cnt++;
31815c06776Sguenther	if (cnt > (NSIG-1)/2) {
31915c06776Sguenther		ss = ~ss;
32015c06776Sguenther		putchar('~');
32115c06776Sguenther	}
32215c06776Sguenther
32315c06776Sguenther	if (ss == 0) {
32415c06776Sguenther		(void)printf("0<>");
32515c06776Sguenther		return;
32615c06776Sguenther	}
32715c06776Sguenther
32815c06776Sguenther	printf("%#x<", ss);
32915c06776Sguenther	for (i = 1; i < NSIG; i++)
33015c06776Sguenther		if (sigismember(&ss, i)) {
33115c06776Sguenther			if (or) putchar('|'); else or=1;
33215c06776Sguenther			signame(i);
33315c06776Sguenther		}
33415c06776Sguenther	printf(">");
33515c06776Sguenther}
33615c06776Sguenther
33715c06776Sguenther/* MANUAL */
33815c06776Sguenthervoid
3391b780f51Sottosemctlname (int cmd)
3401b780f51Sotto{
3411b780f51Sotto	switch (cmd) {
3421b780f51Sotto	case GETNCNT:
3431b780f51Sotto		(void)printf("GETNCNT");
3441b780f51Sotto		break;
3451b780f51Sotto	case GETPID:
3461b780f51Sotto		(void)printf("GETPID");
3471b780f51Sotto		break;
3481b780f51Sotto	case GETVAL:
3491b780f51Sotto		(void)printf("GETVAL");
3501b780f51Sotto		break;
3511b780f51Sotto	case GETALL:
3521b780f51Sotto		(void)printf("GETALL");
3531b780f51Sotto		break;
3541b780f51Sotto	case GETZCNT:
3551b780f51Sotto		(void)printf("GETZCNT");
3561b780f51Sotto		break;
3571b780f51Sotto	case SETVAL:
3581b780f51Sotto		(void)printf("SETVAL");
3591b780f51Sotto		break;
3601b780f51Sotto	case SETALL:
3611b780f51Sotto		(void)printf("SETALL");
3621b780f51Sotto		break;
3631b780f51Sotto	case IPC_RMID:
3641b780f51Sotto		(void)printf("IPC_RMID");
3651b780f51Sotto		break;
3661b780f51Sotto	case IPC_SET:
3671b780f51Sotto		(void)printf("IPC_SET");
3681b780f51Sotto		break;
3691b780f51Sotto	case IPC_STAT:
3701b780f51Sotto		(void)printf("IPC_STAT");
3711b780f51Sotto		break;
3721b780f51Sotto	default: /* Should not reach */
3731b780f51Sotto		(void)printf("<invalid=%ld>", (long)cmd);
3741b780f51Sotto	}
3751b780f51Sotto}
3761b780f51Sotto
3771b780f51Sotto/* MANUAL */
3781b780f51Sottovoid
3791b780f51Sottoshmctlname (int cmd) {
3801b780f51Sotto	switch (cmd) {
3811b780f51Sotto	case IPC_RMID:
3821b780f51Sotto		(void)printf("IPC_RMID");
3831b780f51Sotto		break;
3841b780f51Sotto	case IPC_SET:
3851b780f51Sotto		(void)printf("IPC_SET");
3861b780f51Sotto		break;
3871b780f51Sotto	case IPC_STAT:
3881b780f51Sotto		(void)printf("IPC_STAT");
3891b780f51Sotto		break;
3901b780f51Sotto	default: /* Should not reach */
3911b780f51Sotto		(void)printf("<invalid=%ld>", (long)cmd);
3921b780f51Sotto	}
3931b780f51Sotto}
3941b780f51Sotto
3951b780f51Sotto/* MANUAL */
3961b780f51Sottovoid
3971b780f51Sottosemgetname (int flag) {
3981b780f51Sotto	int	or = 0;
3991b780f51Sotto	if_print_or(flag, IPC_CREAT, or);
4001b780f51Sotto	if_print_or(flag, IPC_EXCL, or);
4011b780f51Sotto	if_print_or(flag, SEM_R, or);
4021b780f51Sotto	if_print_or(flag, SEM_A, or);
4031b780f51Sotto	if_print_or(flag, (SEM_R>>3), or);
4041b780f51Sotto	if_print_or(flag, (SEM_A>>3), or);
4051b780f51Sotto	if_print_or(flag, (SEM_R>>6), or);
4061b780f51Sotto	if_print_or(flag, (SEM_A>>6), or);
4071b780f51Sotto}
4081b780f51Sotto
4091b780f51Sotto/*
4101b780f51Sotto * MANUAL
4111b780f51Sotto *
4121b780f51Sotto * Only used by SYS_open. Unless O_CREAT is set in flags, the
4131b780f51Sotto * mode argument is unused (and often bogus and misleading).
4141b780f51Sotto */
4151b780f51Sottovoid
416dd52b3caSottoflagsandmodename (int flags, int mode) {
4171b780f51Sotto	flagsname (flags);
4181b780f51Sotto	(void)putchar(',');
4191b780f51Sotto	if ((flags & O_CREAT) == O_CREAT) {
4201b780f51Sotto		modename (mode);
4211b780f51Sotto	} else {
4221b780f51Sotto		if (decimal) {
4231b780f51Sotto			(void)printf("<unused>%ld", (long)mode);
4241b780f51Sotto		} else {
4251b780f51Sotto			(void)printf("<unused>%#lx", (long)mode);
4261b780f51Sotto		}
4271b780f51Sotto	}
4281b780f51Sotto}
4291b780f51Sotto
4301b780f51Sotto/*
4311b780f51Sotto * MANUAL
4321b780f51Sotto *
4331b780f51Sotto * [g|s]etsockopt's level argument can either be SOL_SOCKET or a value
4341b780f51Sotto * referring to a line in /etc/protocols . It might be appropriate
4351b780f51Sotto * to use getprotoent(3) here.
4361b780f51Sotto */
4371b780f51Sottovoid
438dd52b3caSottosockoptlevelname (int level)
4391b780f51Sotto{
4401b780f51Sotto	if (level == SOL_SOCKET) {
4411b780f51Sotto		(void)printf("SOL_SOCKET");
4421b780f51Sotto	} else {
4431b780f51Sotto		if (decimal) {
4441b780f51Sotto			(void)printf("%ld", (long)level);
4451b780f51Sotto		} else {
4461b780f51Sotto			(void)printf("%#lx", (long)level);
4471b780f51Sotto		}
4481b780f51Sotto	}
4491b780f51Sotto}
4501b780f51Sotto
4511b780f51Sotto_EOF_
4521b780f51Sotto
45315c06776Sguentherauto_orz_type "modename" "S_[A-Z]+[[:space:]]+[0-6]{7}" "sys/stat.h"
454*256998b4Sguentherauto_fflags_type "flagsname" "O_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
45515c06776Sguentherauto_orz_type "atflagsname" "AT_[A-Z_]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/fcntl.h"
456b8dac8f2Sottoauto_or_type "accessmodename" "[A-Z]_OK[[:space:]]+0?x?[0-9A-Fa-f]+" "sys/unistd.h"
4571b780f51Sottoauto_or_type "mmapprotname" "PROT_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
4581b780f51Sottoauto_or_type "mmapflagsname" "MAP_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/mman.h"
45915c06776Sguentherauto_orz_type "wait4optname" "W[A-Z]+[[:space:]]+[0-9]+" "sys/wait.h"
46015c06776Sguenther#auto_or_type "timerflagsname" "TIMER_[A-Z]+[[:space:]]+0x[0-9A-Fa-f]+" "sys/time.h"
4614f9d0979Sotto#auto_or_type "getfsstatflagsname" "MNT_[A-Z]+[[:space:]]+[1-9][0-9]*" "sys/mount.h"
4624f9d0979Sotto#auto_or_type "mountflagsname" "MNT_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mount.h"
4634f9d0979Sotto#auto_or_type "rebootoptname" "RB_[A-Z]+[[:space:]]+0x[0-9]+" "sys/reboot.h"
464b8dac8f2Sottoauto_or_type "flockname" "LOCK_[A-Z]+[[:space:]]+0x[0-9]+" "sys/fcntl.h"
4654f9d0979Sotto#auto_or_type "thrcreateflagsname" "THR_[A-Z]+[[:space:]]+0x[0-9]+" "sys/thr.h"
466b8dac8f2Sottoauto_or_type "mlockallname" "MCL_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
46715c06776Sguentherauto_orz_type "shmatname" "SHM_[A-Z]+[[:space:]]+[0-9]{6}" "sys/shm.h"
4684f9d0979Sotto#auto_or_type "nfssvcname" "NFSSVC_[A-Z]+[[:space:]]+0x[0-9]+" "nfsserver/nfs.h"
4694f9d0979Sotto#
470b8dac8f2Sottoauto_switch_type "whencename" "SEEK_[A-Z]+[[:space:]]+[0-9]+" "sys/unistd.h"
471b8dac8f2Sottoauto_switch_type "rlimitname" "RLIMIT_[A-Z]+[[:space:]]+[0-9]+" "sys/resource.h"
4724f9d0979Sotto#auto_switch_type "shutdownhowname" "SHUT_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
4734f9d0979Sotto#auto_switch_type "prioname" "PRIO_[A-Z]+[[:space:]]+[0-9]" "sys/resource.h"
474b8dac8f2Sottoauto_switch_type "madvisebehavname" "_?MADV_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
475b8dac8f2Sottoauto_switch_type "msyncflagsname" "MS_[A-Z]+[[:space:]]+0x[0-9]+" "sys/mman.h"
47694db7de9Sguentherauto_switch_type "clockname" "CLOCK_[A-Z]+[[:space:]]+[0-9]+" "sys/_time.h"
4774f9d0979Sotto#auto_switch_type "schedpolicyname" "SCHED_[A-Z]+[[:space:]]+[0-9]+" "sched.h"
4784f9d0979Sotto#auto_switch_type "kldunloadfflagsname" "LINKER_UNLOAD_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
4794f9d0979Sotto#auto_switch_type "extattrctlname" "EXTATTR_NAMESPACE_[A-Z]+[[:space:]]+0x[0-9]+" "sys/extattr.h"
4804f9d0979Sotto#auto_switch_type "kldsymcmdname" "KLDSYM_[A-Z]+[[:space:]]+[0-9]+" "sys/linker.h"
4814f9d0979Sotto#auto_switch_type "sendfileflagsname" "SF_[A-Z]+[[:space:]]+[0-9]+" "sys/socket.h"
4824f9d0979Sotto#auto_switch_type "acltypename" "ACL_TYPE_[A-Z4_]+[[:space:]]+0x[0-9]+" "sys/acl.h"
4839ece112bSguentherauto_orz_type "sigactionflagname" "SA_[A-Z]+[[:space:]]+0x[0-9]+" "sys/signal.h"
4841b780f51Sottoauto_switch_type "sigprocmaskhowname" "SIG_[A-Z]+[[:space:]]+[0-9]+" "sys/signal.h"
4858b7c6933Sderaadtauto_switch_type "sigill_name" "ILL_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4868b7c6933Sderaadtauto_switch_type "sigtrap_name" "TRAP_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4878b7c6933Sderaadtauto_switch_type "sigemt_name" "EMT_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4888b7c6933Sderaadtauto_switch_type "sigfpe_name" "FPE_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4898b7c6933Sderaadtauto_switch_type "sigbus_name" "BUS_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4908b7c6933Sderaadtauto_switch_type "sigsegv_name" "SEGV_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4918b7c6933Sderaadtauto_switch_type "sigchld_name" "CLD_[A-Z]+[[:space:]]+[0-9]+" "sys/siginfo.h"
4924f9d0979Sotto#auto_switch_type "lio_listioname" "LIO_(NO)?WAIT[[:space:]]+[0-9]+" "aio.h"
493b8dac8f2Sottoauto_switch_type "minheritname" "INHERIT_[A-Z]+[[:space:]]+[0-9]+" "sys/mman.h"
4944f9d0979Sotto#auto_switch_type "quotactlname" "Q_[A-Z]+[[:space:]]+0x[0-9]+" "ufs/ufs/quota.h"
495b8dac8f2Sottoauto_if_type "sockdomainname" "PF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
496f980d548Sottoauto_if_type "sockfamilyname" "AF_[[:alnum:]]+[[:space:]]+" "sys/socket.h"
497b8dac8f2Sottoauto_if_type "sockipprotoname" "IPPROTO_[[:alnum:]]+[[:space:]]+" "netinet/in.h"
498b8dac8f2Sottoauto_switch_type "sockoptname" "SO_[A-Z]+[[:space:]]+0x[0-9]+" "sys/socket.h"
499b8dac8f2Sottoauto_switch_type "socktypename" "SOCK_[A-Z]+[[:space:]]+[1-9]+[0-9]*" "sys/socket.h"
5004f9d0979Sotto#auto_switch_type "ptraceopname" "PT_[[:alnum:]_]+[[:space:]]+[0-9]+" "sys/ptrace.h"
5011b780f51Sotto
5021b780f51Sottocat <<_EOF_
5031b780f51Sotto/*
5041b780f51Sotto * AUTO - Special
5051b780f51Sotto * F_ is used to specify fcntl commands as well as arguments. Both sets are
5061b780f51Sotto * grouped in fcntl.h, and this awk script grabs the first group.
5071b780f51Sotto */
5081b780f51Sottovoid
509dd52b3caSottofcntlcmdname (int cmd, int arg)
5101b780f51Sotto{
5111b780f51Sotto	switch (cmd) {
5121b780f51Sotto_EOF_
5134805374fSmatthewegrep "^#[[:space:]]*define[[:space:]]+F_[A-Z_]+[[:space:]]+[0-9]+[[:space:]]*" \
5141b780f51Sotto	$include_dir/sys/fcntl.h | \
5151b780f51Sotto	awk 'BEGIN { o=0 } { for (i = 1; i <= NF; i++) \
5161b780f51Sotto		if ($i ~ /define/) \
5171b780f51Sotto			break; \
5181b780f51Sotto		++i; \
5191b780f51Sotto		if (o <= $(i+1)) \
5201b780f51Sotto			printf "\tcase %s:\n\t\t(void)printf(\"%s\");\n\t\tbreak;\n", $i, $i; \
5211b780f51Sotto		else \
5221b780f51Sotto			exit; \
5231b780f51Sotto		o = $(i+1) }'
5241b780f51Sottocat <<_EOF_
5251b780f51Sotto	default: /* Should not reach */
5261b780f51Sotto		(void)printf("<invalid=%ld>", (long)cmd);
5271b780f51Sotto	}
5281b780f51Sotto	(void)putchar(',');
5291b780f51Sotto	if (cmd == F_GETFD || cmd == F_SETFD) {
5301b780f51Sotto		if (arg == FD_CLOEXEC)
5311b780f51Sotto			(void)printf("FD_CLOEXEC");
5321b780f51Sotto		else if (arg == 0)
5331b780f51Sotto			(void)printf("0");
5341b780f51Sotto		else {
5351b780f51Sotto			if (decimal)
5361b780f51Sotto				(void)printf("<invalid>%ld", (long)arg);
5371b780f51Sotto			else
5381b780f51Sotto				(void)printf("<invalid>%#lx", (long)arg);
5391b780f51Sotto		}
5401b780f51Sotto	} else if (cmd == F_SETFL) {
5411b780f51Sotto		flagsname(arg);
5421b780f51Sotto	} else {
5431b780f51Sotto		if (decimal)
5441b780f51Sotto			(void)printf("%ld", (long)arg);
5451b780f51Sotto		else
5461b780f51Sotto			(void)printf("%#lx", (long)arg);
5471b780f51Sotto	}
5481b780f51Sotto}
5491b780f51Sotto
5501b780f51Sotto/*
5511b780f51Sotto * AUTO - Special
5521b780f51Sotto *
5531b780f51Sotto * The send and recv functions have a flags argument which can be
5541b780f51Sotto * set to 0. There is no corresponding #define. The auto_ functions
5551b780f51Sotto * detect this as "invalid", which is incorrect here.
5561b780f51Sotto */
5571b780f51Sottovoid
5581b780f51Sottosendrecvflagsname (int flags)
5591b780f51Sotto{
5601b780f51Sotto	int	or = 0;
5611b780f51Sotto
5621b780f51Sotto	if (flags == 0) {
5631b780f51Sotto		(void)printf("0");
5641b780f51Sotto		return;
5651b780f51Sotto	}
5661b780f51Sotto
5671b780f51Sotto	printf("%#x<", flags);
5681b780f51Sotto_EOF_
5691b780f51Sottoegrep "^#[[:space:]]*define[[:space:]]+MSG_[A-Z]+[[:space:]]+0x[0-9]+[[:space:]]*" $include_dir/sys/socket.h | \
5701b780f51Sotto	awk '{ for (i = 1; i <= NF; i++) \
5711b780f51Sotto		if ($i ~ /define/) \
5721b780f51Sotto			break; \
5731b780f51Sotto		++i; \
5741b780f51Sotto		printf "\tif(!((flags>0)^((%s)>0)))\n\t\tif_print_or(flags, %s, or);\n", $i, $i }'
5751b780f51Sottocat <<_EOF_
5761b780f51Sotto	printf(">");
5771b780f51Sotto}
5781b780f51Sotto
5791b780f51Sotto_EOF_
580