snprintb(3) - NetBSD Manual Pages

Command: Section: Arch: Collection:  
SNPRINTB(3)             NetBSD Library Functions Manual            SNPRINTB(3)


NAME
snprintb, snprintb_m -- bitmask output conversion
LIBRARY
System Utilities Library (libutil, -lutil)
SYNOPSIS
#include <util.h> int snprintb(char *buf, size_t bufsize, const char *fmt, uint64_t val); int snprintb_m(char *buf, size_t bufsize, const char *fmt, uint64_t val, size_t max);
DESCRIPTION
The snprintb() function formats a bitmask into a mnemonic form suitable for printing. This conversion is useful for decoding bit fields in device registers. It formats the integer val into the buffer buf, of size bufsize, inter- preting the bits within that integer as flags or groups of bits. The buffer is always NUL-terminated. If the buffer buf is too small to hold the formatted output, 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 NUL. If bufsize is zero, nothing is written and arg may be a null pointer. The snprintb_m() function accepts an additional max argument. If this argument is zero, the snprintb_m() function behaves exactly like the snprintb() function. If the max argument has a non-zero value, it repre- sents the maximum length of a formatted string. If the formatted string would require more than max characters, the snprintb_m() function returns multiple formatted strings in the output buffer buf. Each string is NUL-terminated, and the last string is followed by an additional NUL character (or, if you prefer, a zero-length string). The decoding directive in fmt describes how the bitfield is to be inter- preted and displayed. It follows two possible syntaxes, referred to as ``old'' and ``new''. The main advantage of the ``new'' formatting is that it is capable of handling multi-bit fields. If the first character of fmt is `\177', the remainder of the fmt argu- ment follows the ``new'' syntax. The second character (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. Recog- nized radix values (in C escape-character format) are `\10' (octal), `\12' (decimal), and `\20' (hexadecimal). The remaining characters in the fmt argument are interpreted as a list of formatting directives. Old Syntax The ``old'' format syntax is a series of bit-position-description pairs. Each directive begins with a binary character value that represents the position of the bit being described. NB: the bit positions in the old syntax are 1-based! A bit position value of 1 (`\1') describes the least significant bit. Whereas a posi- tion value of 32 (octal `\040', hexadecimal `\x20', the ASCII space character) describes the most significant bit. The old syntax is limited to 32-bit values. The remaining characters are the description to print should the bit being described be set. Descriptions are delimited by the next bit position value character encountered (distinguishable by its value being <= 32), or by the end of the format string itself. New Syntax For the ``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 sig- nificant bit is bit-position zero. Each description is terminated by a NUL byte. b\B Describes a single bit at bit-position B. The remaining characters are the description to print should the bit being described be set. This field directive is similar in func- tion to the old format. When converting old formats to the new syntax don't forget that the new syntax uses zero-based bit positions. f\B\L Describes a multi-bit field beginning at bit-position B and having a bit-length of L. The remaining characters are printed as a description of the field followed by `=' and the value of the field. The value of the field is printed in the base specified as the second character of the fmt argument. F\B\L Describes a multi-bit field like `f', but just extracts the value for use with the `=' and `:' formatting directives described below. =\V The field previously extracted by the last `f' or `F' direc- tive is compared to the byte value V (for values 0 through 255). If they are equal, `=' followed by the description string following V is printed. This and the `:' directive may be repeated to annotate multiple possible values. :\V Operates like the `=' directive, but omits the leading `='. *FMT This provides a ``default'' case that prints the extracted field value using the printf(3) format FMT when other `:' or `=' directives have not matched. FMT may contain a uintmax_t format specification that prints the value that did not match, since the field can be more than 32 bits wide. The new format is terminated by an additional NUL character at the end, following that delimiting the last bit-position-description pair. This NUL is supplied by the compiler to terminate the string literal and doesn't need to be written explicitly.
RETURN VALUES
The snprintb() and 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 snprintb_m(), the NUL characters terminating each individual string are included in the total number of bytes.
EXAMPLES
Two examples of the old formatting style: snprintb(buf, bufsize, "\10\2BITTWO\1BITONE", 3) => "03<BITTWO,BITONE>" snprintb(buf, bufsize, "\20" "\x10NOTBOOT" "\x0f""FPP" "\x0eSDVMA" "\x0cVIDEO" "\x0bLORES" "\x0a""FPA" "\x09""DIAG" "\x07""CACHE" "\x06IOCACHE" "\x05LOOPBACK" "\x04""DBGCACHE", 0xe860) => "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>" An example of the new formatting style: snprintb(buf, bufsize, "\177\020" "b\0LSB\0" "b\1BITONE\0" "f\4\4NIBBLE2\0" "f\x10\4BURST\0" "=\4FOUR\0" "=\xf""FIFTEEN\0" "b\x1fMSB\0", 0x800f0701) => "0x800f0701<LSB,NIBBLE2=0,BURST=0xf=FIFTEEN,MSB>" The same example using snprintb_m: snprintb_m(buf, bufsize, "\177\020" "b\0LSB\0" "b\1BITONE\0" "f\4\4NIBBLE2\0" "f\x10\4BURST\0" "=\4FOUR\0" "=\xf""FIFTEEN\0" "b\x1fMSB\0", 0x800f0701, 34) => "0x800f0701<LSB,NIBBLE2=0>\0" "0x800f0701<BURST=0xf=FIFTEEN,MSB>\0" "" A more complex example from <sys/mman.h> that uses both the bit position `b' formatting as well as the `F' multibit field formatting with a default case (`*'): #define MAP_FMT "\177\020" \ "b\0" "SHARED\0" \ "b\1" "PRIVATE\0" \ "b\2" "COPY\0" \ "b\4" "FIXED\0" \ "b\5" "RENAME\0" \ "b\6" "NORESERVE\0" \ "b\7" "INHERIT\0" \ "b\11" "HASSEMAPHORE\0" \ "b\12" "TRYFIXED\0" \ "b\13" "WIRED\0" \ "F\14\1\0" \ ":\0" "FILE\0" \ ":\1" "ANONYMOUS\0" \ "b\15" "STACK\0" \ "F\30\010\0" \ ":\000" "ALIGN=NONE\0" \ ":\012" "ALIGN=1KB\0" \ ":\013" "ALIGN=2KB\0" \ ":\014" "ALIGN=4KB\0" \ ":\015" "ALIGN=8KB\0" \ ":\016" "ALIGN=16KB\0" \ ":\017" "ALIGN=32KB\0" \ ":\020" "ALIGN=64KB\0" \ ":\021" "ALIGN=128KB\0" \ ":\022" "ALIGN=256KB\0" \ ":\023" "ALIGN=512KB\0" \ ":\024" "ALIGN=1MB\0" \ ":\025" "ALIGN=2MB\0" \ ":\026" "ALIGN=4MB\0" \ ":\027" "ALIGN=8MB\0" \ ":\030" "ALIGN=16MB\0" \ ":\034" "ALIGN=256MB\0" \ ":\040" "ALIGN=4GB\0" \ ":\044" "ALIGN=64GB\0" \ ":\050" "ALIGN=1TB\0" \ ":\054" "ALIGN=16TB\0" \ ":\060" "ALIGN=256TB\0" \ ":\064" "ALIGN=4PB\0" \ ":\070" "ALIGN=64PB\0" \ ":\074" "ALIGN=1EB\0" \ "*" "ALIGN=2^%ju\0" snprintb(buf, bufsize, MAP_FMT, 0x0d001234) => "0xd001234<COPY,FIXED,RENAME,HASSEMAPHORE,ANONYMOUS,ALIGN=8KB>" snprintb(buf, bufsize, MAP_FMT, 0x2e000000) => "0x2e000000<FILE,ALIGN=2^46>"
ERRORS
snprintb() will fail if: [EINVAL] The leading character (for the ``old'' format) or the second character (for the ``new'' format) does not describe a supported numeral base, or a bit number in the fmt argument is out of bounds, or snprintf() failed.
SEE ALSO
printf(3), snprintf(3)
HISTORY
The snprintb() function was originally implemented as a non-standard %b format string for the kernel printf() function in NetBSD 1.5 and earlier releases. It was called bitmask_snprintf() in NetBSD 5.0 and earlier releases.
AUTHORS
The ``new'' format was the invention of Chris Torek. NetBSD 10.99 February 22, 2024 NetBSD 10.99
Powered by man-cgi (2021-06-01). Maintained for NetBSD by Kimmo Suominen. Based on man-cgi by Panagiotis Christias.