.\" $NetBSD: snprintb.3,v 1.35 2024/02/22 21:04:24 rillig Exp $ .\" .\" Copyright (c) 1998, 2024 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 22, 2024 .Dt SNPRINTB 3 .Os .Sh NAME .Nm snprintb , .Nm snprintb_m .Nd bitmask output conversion .Sh LIBRARY .Lb libutil .Sh SYNOPSIS .In util.h .Ft int .Fn "snprintb" "char *buf" "size_t bufsize" "const char *fmt" "uint64_t val" .Ft int .Fn "snprintb_m" "char *buf" "size_t bufsize" "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. .Pp 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 bufsize , interpreting the bits within that integer as 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 . If .Fa bufsize is zero, nothing is written and .Fa arg may be a null pointer. .Pp The .Fn snprintb_m function accepts an additional .Fa max argument. If this argument is zero, the .Fn snprintb_m function behaves exactly like 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 .Pq or, if you prefer, a zero-length string . .Pp 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. .Pp If the first character of .Fa fmt is .Ql \e177 , the remainder of the .Fa fmt argument follows the .Dq new syntax. The second character .Pq 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 .Pq in C escape-character format are .Ql \e10 .Pq octal , .Ql \e12 .Pq decimal , and .Ql \e20 .Pq hexadecimal . .Pp The remaining characters in the .Fa fmt argument are interpreted as a list of formatting directives. . .Ss Old Syntax .Pp The .Dq old format syntax is a series of bit-position\(endescription pairs. .Pp Each directive begins with a binary character value that represents the position of the bit being described. .Pp .Sy NB : the bit positions in the old syntax are .Em 1-based\^ ! A bit position value of 1 .Pq Ql \e1 describes the least significant bit. Whereas a position value of 32 .Po octal .Ql \e040 , hexadecimal .Ql \ex20 , the ASCII space character .Pc describes the most significant bit. The old syntax is limited to 32-bit values. .Pp The remaining characters are the description to print should the bit being described be set. .Pp Descriptions are delimited by the next bit position value character encountered .Pq distinguishable by its value being \*[Le] 32 , or by the end of the format string itself. . .Ss New Syntax .Pp 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. . .Bl -tag -width Cm . .It 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. . .It 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. . .It 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. . .It 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 .Pq 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. . .It Cm \&:\e Ns Ar V Operates like the .Sq Cm \&= directive, but omits the leading .Ql \&= . . .It 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 .Pp 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: .Bd -literal -offset indent snprintb(buf, bufsize, "\e10\e2BITTWO\e1BITONE", 3) \(rA "03" snprintb(buf, bufsize, "\e20" "\ex10NOTBOOT" "\ex0f""FPP" "\ex0eSDVMA" "\ex0cVIDEO" "\ex0bLORES" "\ex0a""FPA" "\ex09""DIAG" "\ex07""CACHE" "\ex06IOCACHE" "\ex05LOOPBACK" "\ex04""DBGCACHE", 0xe860) \(rA "0xe860" .Ed .Pp An example of the new formatting style: .Bd -literal -offset indent snprintb(buf, bufsize, "\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" .Ed .Pp The same example using snprintb_m: .Bd -literal -offset indent snprintb_m(buf, bufsize, "\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\e0" "0x800f0701\e0" "" .Ed .Pp A more complex example from .In sys/mman.h that uses both the bit position .Sq Cm b formatting as well as the .Sq Cm F multibit field formatting with a default case .Pq Sq Cm \&* : .Bd -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=1EB\e0" \e "*" "ALIGN=2^%ju\e0" snprintb(buf, bufsize, MAP_FMT, 0x0d001234) \(rA "0xd001234" snprintb(buf, bufsize, MAP_FMT, 0x2e000000) \(rA "0x2e000000" .Ed .Sh ERRORS .Fn snprintb will fail if: .Bl -tag -width Er .It Bq Er EINVAL The leading character .Po for the .Dq old format .Pc or the second character .Po for the .Dq new format .Pc does not describe a supported numeral base, or a bit number in the .Ar fmt argument is out of bounds, 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 .