xref: /freebsd/cddl/lib/libdtrace/mkpriv (revision 434283fda99e89af20c3fe95fde427624ae96d04)
1#!/bin/sh
2# -*- tab-width: 4 -*- ;; Emacs
3# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
4############################################################ LICENSE
5#
6# Copyright (c) 2026 Devin Teske <dteske@FreeBSD.org>
7#
8# SPDX-License-Identifier: BSD-2-Clause
9#
10############################################################ IDENT(1)
11#
12# $Title: Generate priv.d from <sys/priv.h> for libdtrace(3) $
13# $Copyright: 2026 Devin Teske. All rights reserved. $
14#
15############################################################ GLOBALS
16
17VERSION='$Version: 1.0 $'
18
19pgm="${0##*/}" # Program basename
20
21#
22# Global exit status
23#
24SUCCESS=0
25FAILURE=1
26
27#
28# Command-line options
29#
30OUTPUT_FILE=	# -o file
31
32#
33# Miscellaneous
34#
35PRIV_H=
36
37############################################################ FUNCTIONS
38
39usage()
40{
41	local fmt="$1"
42	local optfmt="\t%-5s %s\n"
43
44	exec >&2
45	if [ "$fmt" ]; then
46		shift 1 # fmt
47		printf "%s: $fmt\n" "$pgm" "$@"
48	fi
49	printf "Usage: %s [-hv] [-o file] priv.h\n" "$pgm"
50	printf "Options:\n"
51	printf "$optfmt" "-h" "Print this usage statment and exit."
52	printf "$optfmt" "-o file" "Write to output file instead of stdout."
53	printf "$optfmt" "-v" "Print version information and exit."
54	exit $FAILURE
55}
56
57############################################################ MAIN
58
59#
60# Process command-line options
61#
62while getopts ho:v flag; do
63	case "$flag" in
64	o) OUTPUT_FILE="$OPTARG" ;;
65	v) VERSION="${VERSION#*: }"
66		echo "${VERSION% $}"
67		exit $SUCCESS ;;
68	*) usage # NOTREACHED
69	esac
70done
71shift $(( $OPTIND - 1 ))
72
73#
74# Check command-line arguments
75#
76[ $# -gt 0 ] || usage "Too few arguments" # NOTREACHED
77[ $# -eq 1 ] || usage "Too many arguments" # NOTREACHED
78[ "$1" ] || usage "Missing priv.h argument" # NOTREACHED
79[ -e "$1" ] || usage "%s: No such file or directory" "$1" # NOTREACHED
80
81#
82# Generate priv.d contents from priv.h input
83#
84PRIV_H="$1"
85exec 9<<PRIV_D_EOF
86/*-
87 * Copyright (c) 2026 Devin Teske <dteske@FreeBSD.org>
88 *
89 * SPDX-License-Identifier: BSD-2-Clause
90 */
91
92/*
93 * Map priv(9) privilege values to their symbolic names.
94 * NB: Mechanically generated from <sys/priv.h> -- keep in-sync.
95 * NB: Generated by /usr/src/cddl/lib/libdtrace/$pgm
96 */
97
98#pragma D binding "1.13" priv_string
99inline string priv_string[int priv] =
100$( awk '
101	/^#define[[:space:]]+PRIV_[A-Z0-9_]+[[:space:]]+[0-9]+/ {
102		for (i = 0; ++i <= NF; ) if ($i ~ /define/) break
103		name = $++i
104		value = $++i
105		printf "\tpriv == %s ?\t\t\"%s\" :\n", value, name
106	}' "$PRIV_H"
107)
108	strjoin("PRIV_UNKNOWN#", lltostr(priv));
109PRIV_D_EOF
110
111#
112# Output contents
113#
114[ ! "$OUTPUT_FILE" ] || exec > "$OUTPUT_FILE"
115cat <&9
116
117################################################################################
118# END
119################################################################################
120