xref: /netbsd-src/lib/libutil/snprintb.3 (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1.\"     $NetBSD: snprintb.3,v 1.17 2017/10/22 16:59:18 abhinav Exp $
2.\"
3.\" Copyright (c) 1998 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Jeremy Cooper.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.
17.\"
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.
29.\"
30.Dd May 7, 2009
31.Dt SNPRINTB 3
32.Os
33.Sh NAME
34.Nm snprintb ,
35.Nm snprintb_m
36.Nd bitmask output conversion
37.Sh LIBRARY
38.Lb libutil
39.Sh SYNOPSIS
40.In util.h
41.Ft int
42.Fn "snprintb" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val"
43.Ft int
44.Fn "snprintb_m" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val" \
45"size_t max"
46.Sh DESCRIPTION
47The
48.Fn snprintb
49function formats a bitmask into a mnemonic form suitable for printing.
50.Pp
51This conversion is useful for decoding bit fields in device registers.
52It formats the integer
53.Fa val
54into the buffer
55.Fa buf ,
56of size
57.Fa buflen ,
58using a specified radix and an interpretation of
59the bits within that integer as though they were flags.
60The buffer is always NUL-terminated.
61If the buffer
62.Fa buf
63is too small to hold the formatted output,
64.Fn snprintb
65will fill as much as it can, and return the number of bytes
66that would have written if the buffer was long enough excluding the
67terminating NUL.
68.Pp
69The decoding directive string
70.Fa fmt
71describes how the bitfield is to be interpreted and displayed.
72It follows two possible syntaxes, referred to as
73.Dq old
74and
75.Dq new .
76The main advantage of the
77.Dq new
78formatting is that it is capable of handling multi-bit fields.
79.Pp
80The first character of
81.Fa fmt
82may be
83.Li \e177 ,
84indicating that the remainder of the format string follows the
85.Dq new
86syntax.
87The second character
88.Pq the first for the old format
89is a binary character representation of the
90output numeral base in which the bitfield will be printed before it is decoded.
91Recognized radix values
92.Pq in C escape-character format
93are
94.Li \e10
95.Pq octal ,
96.Li \e12
97.Pq decimal ,
98and
99.Li \e20
100.Pq hexadecimal .
101.Pp
102The remaining characters in
103.Fa fmt
104are interpreted as a list of bit-position\(endescription pairs.
105From here the syntaxes diverge.
106.Pp
107The
108.Dq old
109format syntax is series of bit-position\(endescription pairs.
110Each begins with a binary character value that represents the position
111of the bit being described.
112A bit position value of one describes the least significant bit.
113Whereas a position value of 32
114.Pq octal 40, hexadecimal 20, the ASCII space character
115describes the most significant bit.
116.Pp
117The remaining characters in a bit-position\(endescription pair are the
118characters to print should the bit being described be set.
119Description strings are delimited by the next bit position value character
120encountered
121.Pq distinguishable by its value being \*[Le] 32 ,
122or the end of the decoding directive string itself.
123.Pp
124For the
125.Dq new
126format syntax, a bit-position\(endescription begins with a field type
127followed by a binary bit-position and possibly a field length.
128The least significant bit is bit-position zero, unlike the
129.Dq old
130syntax where it is one.
131.Bl -tag -width "xxxxx"
132.It Cm b\eB
133Describes a bit position.
134The bit-position
135.Fa B
136indicates the corresponding bit, as in the
137.Dq old
138format.
139.It Cm f\eB\eL
140Describes a multi-bit field beginning at bit-position
141.Fa B
142and having a bit-length of
143.Fa L .
144The remaining characters are printed as a description of the field
145followed by
146.Sq \&=
147and the value of the field.
148The value of the field is printed in the base specified as the second
149character of the decoding directive string
150.Ar fmt .
151.It Cm F\eB\eL
152Describes a multi-bit field like
153.Sq f ,
154but just extracts the value for use with the
155.Sq \&=
156and
157.Sq \&:
158formatting directives described below.
159.It Cm \&=\eV
160The field previously extracted by the last
161.Sq f
162or
163.Sq F
164operator is compared to the byte
165.Sq Cm V
166.Pq for values 0 through 255 .
167If they are equal,
168.Sq \&=
169followed by the string following
170.Sq Cm V
171is printed.
172This and the
173.Sq \&:
174operator may be repeated to annotate multiple possible values.
175.It Cm :\eV
176Operates like the
177.Sq \&=
178operator, but omits the leading
179.Sq \&= .
180.El
181.Pp
182Finally, each field is delimited by a NUL
183.Pq Sq \e0
184character.
185By convention, the format string has an additional NUL character at
186the end, following that delimiting the last bit-position\(endescription
187pair.
188.Pp
189The
190.Fn snprintb_m
191function accepts an additional
192.Fa max
193argument.
194If this argument is zero, the
195.Fn snprintb_m
196function returns exactly the same results in the
197.Fa buf
198as the
199.Fn snprintb
200function.
201If the
202.Fa max
203argument is present and has a non-zero value, it represents the maximum
204length of a formatted string.
205If the formatted string would require more than
206.Fa max
207characters, the
208.Fn snprintb_m
209function returns multiple formatted strings in the output buffer
210.Fa buf .
211Each string is NUL-terminated, and the last string is followed by an
212additional NUL character (or, if you prefer, a zero-length string).
213.Sh RETURN VALUES
214The
215.Fn snprintb
216and
217.Fn snprintb_m
218functions return the number of bytes that would have written to the buffer
219if there was adequate space, excluding the final terminating NUL, or \-1 in
220case an error occurred.
221For
222.Fn snprintb_m ,
223the NUL characters terminating each individual string are included in the
224total number of bytes.
225.Sh EXAMPLES
226Two examples of the old formatting style:
227.Bd -literal -offset indent
228snprintb(buf, buflen, "\e10\e2BITTWO\e1BITONE", 3)
229\(rA "03<BITTWO,BITONE>"
230
231snprintb(buf, buflen,
232       "\e20\ex10NOTBOOT\ex0f" "FPP\ex0eSDVMA\ex0cVIDEO"
233       "\ex0bLORES\ex0a" "FPA\ex09" "DIAG\ex07" "CACHE"
234       "\ex06IOCACHE\ex05LOOPBACK\ex04" "DBGCACHE",
235       0xe860)
236\(rA "0xe860<NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE>"
237.Ed
238.Pp
239An example of the new formatting style:
240.Bd -literal -offset indent
241snprintb(buf, buflen,
242       "\e177\e020b\e0LSB\e0b\e1_BITONE\e0f\e4\e4NIBBLE2\e0"
243       "f\ex10\e4BURST\e0=\e4FOUR\e0=\exfSIXTEEN\e0"
244       "b\ex1fMSB\e0\e0",
245       0x800f0701)
246\(rA "0x800f0701<LSB,NIBBLE2=0x0,BURST=0xf=SIXTEEN,MSB>"
247.Ed
248.Pp
249An example using snprintb_m:
250.Bd -literal -offset indent
251snprintb_m(buf, buflen,
252       "\e177\e020b\e0LSB\e0b\e1_BITONE\e0f\e4\e4NIBBLE2\e0"
253       "f\ex10\e4BURST\e0=\e4FOUR\e0=\exfSIXTEEN\e0"
254       "b\ex1fMSB\e0\e0",
255       0x800f0701, 34)
256\(rA "0x800f0701<LSB,NIBBLE2=0x0>\e00x800f0701<BURST=0xf=SIXTEEN,MSB>\e0"
257.Ed
258.Sh ERRORS
259.Fn snprintb
260will fail if:
261.Bl -tag -width Er
262.It Bq Er EINVAL
263The leading character does not describe a supported format,
264or
265.Fn snprintf
266failed.
267.El
268.Sh SEE ALSO
269.Xr printf 3 ,
270.Xr snprintf 3
271.Sh HISTORY
272The
273.Fn snprintb
274function was originally implemented as a non-standard
275.Li %b
276format string for the kernel
277.Fn printf
278function in
279.Nx 1.5
280and earlier releases.
281It was called
282.Fn bitmask_snprintf
283in
284.Nx 5.0
285and earlier releases.
286.Sh AUTHORS
287The
288.Dq new
289format was the invention of
290.An Chris Torek .
291