1 .\" $NetBSD: snprintb.3,v 1.13 2009/05/05 13:12:25 pgoyette Exp $
3 .\" Copyright (c) 1998 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd bitmask output conversion
41 .Fn "snprintb" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val"
43 .Fn "snprintb_m" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val" \
48 function formats a bitmask into a mnemonic form suitable for printing.
50 This conversion is useful for decoding bit fields in device registers.
51 It formats the integer
57 using a specified radix and an interpretation of
58 the bits within that integer as though they were flags.
59 The buffer is always NUL-terminated.
62 is too small to hold the formatted output,
64 will fill as much as it can, and return the number of bytes
65 that would have written if the buffer was long enough excluding the
68 The decoding directive string
70 describes how the bitfield is to be interpreted and displayed.
71 It follows two possible syntaxes, referred to as
75 The main advantage of the
77 formatting is that it is capable of handling multi-bit fields.
79 The first character of
83 indicating that the remainder of the format string follows the
87 .Pq the first for the old format
88 is a binary character representation of the
89 output numeral base in which the bitfield will be printed before it is decoded.
90 Recognized radix values
91 .Pq in C escape-character format
101 The remaining characters in
103 are interpreted as a list of bit-position\(endescription pairs.
104 From here the syntaxes diverge.
108 format syntax is series of bit-position\(endescription pairs.
109 Each begins with a binary character value that represents the position
110 of the bit being described.
111 A bit position value of one describes the least significant bit.
112 Whereas a position value of 32
113 .Pq octal 40, hexadecimal 20, the ASCII space character
114 describes the most significant bit.
116 The remaining characters in a bit-position\(endescription pair are the
117 characters to print should the bit being described be set.
118 Description strings are delimited by the next bit position value character
120 .Pq distinguishable by its value being \*[Le] 32 ,
121 or the end of the decoding directive string itself.
125 format syntax, a bit-position\(endescription begins with a field type
126 followed by a binary bit-position and possibly a field length.
127 The least significant bit is bit-position zero, unlike the
129 syntax where it is one.
130 .Bl -tag -width "xxxxx"
132 Describes a bit position.
135 indicates the corresponding bit, as in the
139 Describes a multi-bit field beginning at bit-position
141 and having a bit-length of
143 The remaining characters are printed as a description of the field
146 and the value of the field.
147 The value of the field is printed in the base specified as the second
148 character of the decoding directive string
151 Describes a multi-bit field like
153 but just extracts the value for use with the
157 formatting directives described below.
159 The field previously extracted by the last
163 operator is compared to the byte
165 .Pq for values 0 through 255 .
168 followed by the string following
173 operator may be repeated to annotate multiple possible values.
177 operator, but omits the leading
181 Finally, each field is delimited by a NUL
184 By convention, the format string has an additional NUL character at
185 the end, following that delimiting the last bit-position\(endescription
190 function accepts an additional
193 If this argument is zero, the
195 function returns exactly the same results in the
202 argument is present and has a non-zero value, it represents the maximum
203 length of a formatted string.
204 If the formatted string would require more than
208 function returns multiple formatted strings in the output buffer
210 Each string is NUL-terminated, and the last string is followed by an
211 additional NUL character (or, if you prefer, a zero-length string).
217 functions return the number of bytes that would have written to the buffer
218 if there was adequate space, excluding the final terminating NUL, or \-1 in
219 case an error occurred.
222 the NUL characters terminating each individual string are included in the
223 total number of bytes.
225 Two examples of the old formatting style:
226 .Bd -literal -offset indent
227 snprintb(buf, buflen, "\e10\e2BITTWO\e1BITONE", 3)
228 \(rA "3\*[Lt]BITTWO,BITONE\*[Gt]"
230 snprintb(buf, buflen,
231 "\e20\ex10NOTBOOT\ex0fFPP\ex0eSDVMA\ex0cVIDEO"
232 "\ex0bLORES\ex0aFPA\ex09DIAG\ex07CACHE"
233 "\ex06IOCACHE\ex05LOOPBACK\ex04DBGCACHE",
235 \(rA "e860\*[Lt]NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE\*[Gt]"
238 An example of the new formatting style:
239 .Bd -literal -offset indent
240 snprintb(buf, buflen,
241 "\e177\e020b\e0LSB\e0b\e1_BITONE\e0f\e4\e4NIBBLE2\e0"
242 "f\ex10\e4BURST\e0=\e4FOUR\e0=\exfSIXTEEN\e0"
245 \(rA "800f0701\*[Lt]LSB,NIBBLE2=0,BURST=f=SIXTEEN,MSB\*[Gt]"
252 The leading character does not describe a supported format,
263 function was originally implemented as a non-standard
265 format string for the kernel
269 and earlier releases.
274 and earlier releases.
278 format was the invention of