snprintb.3 revision 1.30
$NetBSD: snprintb.3,v 1.30 2024/02/01 22:18:34 rillig Exp $

Copyright (c) 1998 The NetBSD Foundation, Inc.
All rights reserved.

This code is derived from software contributed to The NetBSD Foundation
by Jeremy Cooper.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

.Dd February 1, 2024 .Dt SNPRINTB 3 .Os .Sh NAME .Nm snprintb , .Nm snprintb_m .Nd bitmask output conversion .Sh LIBRARY .Lb libutil .Sh SYNOPSIS n util.h .Ft int .Fn "snprintb" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val" .Ft int .Fn "snprintb_m" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val" \ "size_t max" .Sh DESCRIPTION The .Fn snprintb function formats a bitmask into a mnemonic form suitable for printing.

p This conversion is useful for decoding bit fields in device registers. It formats the integer .Fa val into the buffer .Fa buf , of size .Fa buflen , using a specified radix and an interpretation of the bits within that integer as though they were flags or groups of bits. The buffer is always .Tn NUL Ns -terminated. If the buffer .Fa buf is too small to hold the formatted output, .Fn snprintb will fill as much as it can, and return the number of bytes that it would have written if the buffer were long enough excluding the terminating .Tn NUL .

p The .Fn snprintb_m function accepts an additional .Fa max argument. If this argument is zero, the .Fn snprintb_m function returns exactly the same results in the .Fa buf as the .Fn snprintb function. If the .Fa max argument has a non-zero value, it represents the maximum length of a formatted string. If the formatted string would require more than .Fa max characters, the .Fn snprintb_m function returns multiple formatted strings in the output buffer .Fa buf . Each string is .Tn NUL Ns -terminated , and the last string is followed by an additional .Tn NUL character

q or, if you prefer, a zero-length string .

p The decoding directive in .Fa fmt describes how the bitfield is to be interpreted and displayed. It follows two possible syntaxes, referred to as .Dq old and .Dq new . The main advantage of the .Dq new formatting is that it is capable of handling multi-bit fields.

p The first character of .Fa fmt may be .Ql \e177 , indicating that the remainder of the .Fa fmt argument follows the .Dq new syntax. The second character

q the first for the old format is a binary character representation of the output numeral base in which the bitfield will be printed before it is decoded. Recognized radix values

q in C escape-character format are .Ql \e10

q octal , .Ql \e12

q decimal , and .Ql \e20

q hexadecimal .

p The remaining characters in the .Fa fmt argument are interpreted as a list of formatting directives. . .Ss Old Syntax

p The .Dq old format syntax is a series of bit-position\(endescription pairs.

p Each directive begins with a binary character value that represents the position of the bit being described.

p .Sy NB : the bit positions in the old syntax are .Em 1-based\^ ! A bit position value of 1

q Ql \e1 describes the least significant bit. Whereas a position value of 32

o octal .Ql \e040 , hexadecimal .Ql \ex20 , the ASCII space character

c describes the most significant bit. The old syntax is limited to 32-bit values.

p The remaining characters are the description to print should the bit being described be set.

p Descriptions are delimited by the next bit position value character encountered

q distinguishable by its value being \*[Le] 32 , or by the end of the format string itself. . .Ss New Syntax

p For the .Dq new format syntax, a formatting directive begins with a field type followed by a binary field position and possibly a field length, followed by a description. The bit positions are 0-based, the least significant bit is bit-position zero. Each description is terminated by a .Tn NUL byte. . l -tag -width Cm . t Cm b\e Ns Ar B Describes a single bit at bit-position .Ar B . The remaining characters are the description to print should the bit being described be set. This field directive is similar in function to the old format. When converting old formats to the new syntax don't forget that the new syntax uses zero-based bit positions. . t Cm f\e Ns Ar B Ns Cm \e Ns Ar L Describes a multi-bit field beginning at bit-position .Ar B and having a bit-length of .Ar L . The remaining characters are printed as a description of the field followed by .Ql = and the value of the field. The value of the field is printed in the base specified as the second character of the .Ar fmt argument. . t Cm F\e Ns Ar B Ns Cm \e Ns Ar L Describes a multi-bit field like .Sq Cm f , but just extracts the value for use with the .Sq Cm = and .Sq Cm : formatting directives described below. . t Cm =\e Ns Ar V The field previously extracted by the last .Sq Cm f or .Sq Cm F directive is compared to the byte value .Ar V

q for values 0 through 255 . If they are equal, .Ql = followed by the description string following .Ar V is printed. This and the .Sq Cm : directive may be repeated to annotate multiple possible values. . t Cm :\e Ns Ar V Operates like the .Sq Cm = directive, but omits the leading .Ql = . . t Cm * Ns Ar FMT This provides a .Dq default case that prints the extracted field value using the .Xr printf 3 format .Ar FMT when other .Sq Cm : or .Sq Cm = directives have not matched. .Ar FMT may contain a .Vt uintmax_t format specification that prints the value that did not match, since the field can be more than 32 bits wide. .El

p The new format is terminated by an additional .Tn NUL character at the end, following that delimiting the last bit-position\(endescription pair. This .Tn NUL is supplied by the compiler to terminate the string literal and doesn't need to be written explicitly. .Sh RETURN VALUES The .Fn snprintb and .Fn snprintb_m functions return the number of bytes that they would have written to the buffer if there was adequate space, excluding the final terminating NUL, or -1 in case an error occurred. For .Fn snprintb_m , the NUL characters terminating each individual string are included in the total number of bytes. .Sh EXAMPLES Two examples of the old formatting style: d -literal -offset indent snprintb(buf, buflen, "\e10\e2BITTWO\e1BITONE", 3) \(rA "03<BITTWO,BITONE>" snprintb(buf, buflen, "\e20" "\ex10NOTBOOT" "\ex0f""FPP" "\ex0eSDVMA" "\ex0cVIDEO" "\ex0bLORES" "\ex0a""FPA" "\ex09""DIAG" "\ex07""CACHE" "\ex06IOCACHE" "\ex05LOOPBACK" "\ex04""DBGCACHE", 0xe860) \(rA "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>" .Ed

p An example of the new formatting style: d -literal -offset indent snprintb(buf, buflen, "\e177\e020" "b\e0LSB\e0" "b\e1BITONE\e0" "f\e4\e4NIBBLE2\e0" "f\ex10\e4BURST\e0" "=\e4FOUR\e0" "=\exf""FIFTEEN\e0" "b\ex1fMSB\e0", 0x800f0701) \(rA "0x800f0701<LSB,NIBBLE2=0x0,BURST=0xf=FIFTEEN,MSB>" .Ed

p The same example using snprintb_m: d -literal -offset indent snprintb_m(buf, buflen, "\e177\e020" "b\e0LSB\e0" "b\e1BITONE\e0" "f\e4\e4NIBBLE2\e0" "f\ex10\e4BURST\e0" "=\e4FOUR\e0" "=\exf""FIFTEEN\e0" "b\ex1fMSB\e0", 0x800f0701, 34) \(rA "0x800f0701<LSB,NIBBLE2=0x0>\e0" "0x800f0701<BURST=0xf=FIFTEEN,MSB>\e0" "" .Ed

p A more complex example from n sys/mman.h that uses the both bit position .Sq Cm b formatting as well as the .Sq Cm F multibit field formatting with a default case

q Sq Cm * : d -literal -offset indent #define MAP_FMT "\e177\e020" \e "b\e0" "SHARED\e0" \e "b\e1" "PRIVATE\e0" \e "b\e2" "COPY\e0" \e "b\e4" "FIXED\e0" \e "b\e5" "RENAME\e0" \e "b\e6" "NORESERVE\e0" \e "b\e7" "INHERIT\e0" \e "b\e11" "HASSEMAPHORE\e0" \e "b\e12" "TRYFIXED\e0" \e "b\e13" "WIRED\e0" \e "F\e14\e1\e0" \e ":\e0" "FILE\e0" \e ":\e1" "ANONYMOUS\e0" \e "b\e15" "STACK\e0" \e "F\e30\e010\e0" \e ":\e000" "ALIGN=NONE\e0" \e ":\e012" "ALIGN=1KB\e0" \e ":\e013" "ALIGN=2KB\e0" \e ":\e014" "ALIGN=4KB\e0" \e ":\e015" "ALIGN=8KB\e0" \e ":\e016" "ALIGN=16KB\e0" \e ":\e017" "ALIGN=32KB\e0" \e ":\e020" "ALIGN=64KB\e0" \e ":\e021" "ALIGN=128KB\e0" \e ":\e022" "ALIGN=256KB\e0" \e ":\e023" "ALIGN=512KB\e0" \e ":\e024" "ALIGN=1MB\e0" \e ":\e025" "ALIGN=2MB\e0" \e ":\e026" "ALIGN=4MB\e0" \e ":\e027" "ALIGN=8MB\e0" \e ":\e030" "ALIGN=16MB\e0" \e ":\e034" "ALIGN=256MB\e0" \e ":\e040" "ALIGN=4GB\e0" \e ":\e044" "ALIGN=64GB\e0" \e ":\e050" "ALIGN=1TB\e0" \e ":\e054" "ALIGN=16TB\e0" \e ":\e060" "ALIGN=256TB\e0" \e ":\e064" "ALIGN=4PB\e0" \e ":\e070" "ALIGN=64PB\e0" \e ":\e074" "ALIGN=256PB\e0" \e "*" "ALIGN=2^%ju\e0" snprintb(buf, buflen, MAP_FMT, 0x0d001234) \(rA "0xd001234<COPY,FIXED,RENAME,HASSEMAPHORE,ANONYMOUS,ALIGN=8KB>" snprintb(buf, buflen, MAP_FMT, 0x2e000000) \(rA "0xd001234<0x2e000000<FILE,ALIGN=2^46>" .Ed .Sh ERRORS .Fn snprintb will fail if: l -tag -width Er t Bq Er EINVAL The leading character

o for the .Dq old format

c or the second character

o for the .Dq new format

c does not describe a supported numeral base, or .Fn snprintf failed. .El .Sh SEE ALSO .Xr printf 3 , .Xr snprintf 3 .Sh HISTORY The .Fn snprintb function was originally implemented as a non-standard .Li %b format string for the kernel .Fn printf function in .Nx 1.5 and earlier releases. It was called .Fn bitmask_snprintf in .Nx 5.0 and earlier releases. .Sh AUTHORS The .Dq new format was the invention of .An Chris Torek .