NetBSD/lib/libutil/snprintb.3

151 lines
4.9 KiB
Groff

.\" $NetBSD: snprintb.3,v 1.5 2004/12/01 00:06:46 peter 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.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\"
.\" 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 July 28, 2000
.Dt SNPRINTB 3
.Os
.Sh NAME
.Nm snprintb
.Nd bitmask output conversion
.Sh LIBRARY
.Lb libutil
.Sh SYNOPSIS
.In util.h
.Ft int
.Fn "snprintb" "char *buf" "size_t buflen" "const char *fmt" "uint64_t val"
.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 buflen ,
using a specified radix and an interpretation of
the bits within that integer as though they were flags.
.Pp
The decoding directive string
.Fa fmt
describes how the bitfield is to be interpreted and displayed.
The first character of
.Fa fmt
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
.Li \e10
.Pq octal ,
.Li \e12
.Pq decimal ,
and
.Li \e20
.Pq hexadecimal .
.Pp
The remaining characters in
.Fa fmt
are interpreted as a list of bit-position\(endescription pairs.
A bit-position\(endescription pair begins with a binary character value
that represents the position of the bit being described.
A bit position value of one describes the least significant bit.
Whereas a position value of 32
.Pq "octal 40, hexadecimal 20, the ASCII space character"
describes the most significant bit.
.Pp
The remaining characters in a bit-position\(endescription pair are the
characters to print should the bit being described be set.
Description strings are delimited by the next bit position value character
encountered
.Pq "distinguishable by its value being \*[Le] 32" ,
or the end of the decoding directive string itself.
.Sh RETURN VALUES
The
.Fn snprintb
function returns the number of characters that are required to format the
value
.Fa val
given the format string
.Fa fmt
excluding the terminating NUL.
The returned string in
.Fa buf
is always NUL-terminated.
.Sh EXAMPLES
Two examples of the old formatting style:
.Bd -literal -offset indent
snprintb(buf, buflen, "\e10\e2BITTWO\e1BITONE", 3)
\(rA "3\*[Lt]BITTWO,BITONE\*[Gt]"
snprintb(buf, buflen
"\e20\ex10NOTBOOT\ex0fFPP\ex0eSDVMA\ex0cVIDEO"
"\ex0bLORES\ex0aFPA\ex09DIAG\ex07CACHE"
"\ex06IOCACHE\ex05LOOPBACK\ex04DBGCACHE",
0xe860)
\(rA "0xe860\*[Lt]NOTBOOT,FPP,SDVMA,VIDEO,CACHE,IOCACHE\*[Gt]"
.Ed
.Sh ERRORS
If the buffer
.Fa buf
is too small to hold the formatted output,
.Fn snprintb
will still return the buffer, containing a truncated string.
.Sh SEE ALSO
.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 got implemented as
.Fn bitmap_snprintf
for
.Nx 1.6
and this version was used to implement
.Fn snprintb .
.Sh BUGS
.Fn snprintb
supports a new extended form of formatting string, which is not yet
described here.