snprintb.c revision 1.11 1 /* $NetBSD: snprintb.c,v 1.11 2013/08/07 23:48:13 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * snprintb: print an interpreted bitmask to a buffer
31 *
32 * => returns the length of the buffer that would be required to print the
33 * string minus the terminating NUL.
34 */
35 #ifndef _STANDALONE
36 # ifndef _KERNEL
37
38 # if HAVE_NBTOOL_CONFIG_H
39 # include "nbtool_config.h"
40 # endif
41
42 # include <sys/cdefs.h>
43 # if defined(LIBC_SCCS) && !defined(lint)
44 __RCSID("$NetBSD: snprintb.c,v 1.11 2013/08/07 23:48:13 pgoyette Exp $");
45 # endif
46
47 # include <sys/types.h>
48 # include <sys/inttypes.h>
49 # include <stdio.h>
50 # include <util.h>
51 # include <errno.h>
52 # else /* ! _KERNEL */
53 # include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.11 2013/08/07 23:48:13 pgoyette Exp $");
55 # include <sys/param.h>
56 # include <sys/inttypes.h>
57 # include <sys/systm.h>
58 # include <lib/libkern/libkern.h>
59 # endif /* ! _KERNEL */
60
61 # ifndef HAVE_SNPRINTB_M
62 int
63 snprintb_m(char *buf, size_t buflen, const char *bitfmt, uint64_t val,
64 size_t l_max)
65 {
66 char *bp = buf, *s_bp = NULL;
67 const char *c_fmt, *s_fmt = NULL, *cur_fmt;
68 const char *sbase;
69 int bit, ch, t_len, s_len = 0, l_len, f_len, v_len, sep;
70 int restart = 0;
71 uint64_t field;
72
73 #ifdef _KERNEL
74 /*
75 * For safety; no other *s*printf() do this, but in the kernel
76 * we don't usually check the return value
77 */
78 (void)memset(buf, 0, buflen);
79 #endif /* _KERNEL */
80
81 ch = *bitfmt++;
82 switch (ch != '\177' ? ch : *bitfmt++) {
83 case 8:
84 sbase = "0%" PRIo64;
85 break;
86 case 10:
87 sbase = "%" PRId64;
88 break;
89 case 16:
90 sbase = "0x%" PRIx64;
91 break;
92 default:
93 goto internal;
94 }
95
96 /* Reserve space for trailing blank line if needed */
97 if (l_max > 0)
98 buflen--;
99
100 t_len = snprintf(bp, buflen, sbase, val);
101 if (t_len < 0)
102 goto internal;
103
104 v_len = l_len = t_len;
105
106 if ((size_t)t_len < buflen)
107 bp += t_len;
108 else
109 bp += buflen - 1;
110
111 /*
112 * If the value we printed was 0 and we're using the old-style format,
113 * we're done.
114 */
115 if ((val == 0) && (ch != '\177'))
116 goto terminate;
117
118 #define STORE(c) { l_len++; \
119 if ((size_t)(++t_len) < buflen) \
120 *bp++ = (c); \
121 } while ( /* CONSTCOND */ 0)
122
123 #define BACKUP { if (s_bp != NULL) { \
124 bp = s_bp; s_bp = NULL; \
125 t_len -= l_len - s_len; \
126 restart = 1; \
127 bitfmt = s_fmt; \
128 } \
129 STORE('>'); STORE('\0'); \
130 if ((size_t)t_len < buflen) \
131 snprintf(bp, buflen - t_len, sbase, val); \
132 t_len += v_len; l_len = v_len; bp += v_len; \
133 } while ( /* CONSTCOND */ 0)
134
135 #define PUTSEP \
136 if (l_max > 0 && (size_t)l_len >= l_max) { \
137 BACKUP; \
138 STORE('<'); \
139 } else { \
140 /* Remember separator location */ \
141 if (l_max > 0 && sep != '<') { \
142 s_len = l_len; \
143 s_bp = bp; \
144 s_fmt = cur_fmt; \
145 } \
146 STORE(sep); \
147 restart = 0; \
148 } \
149
150 #define PUTCHR(c) \
151 if (l_max > 0 && (size_t)l_len >= (l_max - 1)) { \
152 BACKUP; \
153 if (restart == 0) { \
154 STORE(c); \
155 } else \
156 sep = '<'; \
157 } else { \
158 STORE(c); \
159 restart = 0; \
160 } \
161
162 #define PUTS(s) while ((ch = *(s)++) != 0) { \
163 PUTCHR(ch); \
164 if (restart) \
165 break; \
166 }
167
168 /*
169 * Chris Torek's new bitmask format is identified by a leading \177
170 */
171 sep = '<';
172 if (ch != '\177') {
173 /* old (standard) format. */
174 for (;(bit = *bitfmt) != 0;) {
175 cur_fmt = bitfmt++;
176 if (val & (1 << (bit - 1))) {
177 PUTSEP;
178 if (restart)
179 continue;
180 sep = ',';
181 for (; (ch = *bitfmt) > ' '; ++bitfmt) {
182 PUTCHR(ch);
183 if (restart)
184 break;
185 }
186 } else
187 for (; *bitfmt > ' '; ++bitfmt)
188 continue;
189 }
190 } else {
191 /* new quad-capable format; also does fields. */
192 field = val;
193 while (c_fmt = bitfmt, (ch = *bitfmt++) != '\0') {
194 bit = *bitfmt++; /* now 0-origin */
195 switch (ch) {
196 case 'b':
197 if (((unsigned int)(val >> bit) & 1) == 0)
198 goto skip;
199 cur_fmt = c_fmt;
200 PUTSEP;
201 if (restart)
202 break;
203 PUTS(bitfmt);
204 if (restart == 0)
205 sep = ',';
206 break;
207 case 'f':
208 case 'F':
209 cur_fmt = c_fmt;
210 f_len = *bitfmt++; /* field length */
211 field = (val >> bit) &
212 (((uint64_t)1 << f_len) - 1);
213 PUTSEP;
214 if (restart == 0)
215 sep = ',';
216 if (ch == 'F') /* just extract */
217 break;
218 if (restart == 0) {
219 PUTS(bitfmt);
220 }
221 if (restart == 0) {
222 PUTCHR('=');
223 }
224 if (restart == 0) {
225 f_len = snprintf(bp, buflen - t_len,
226 sbase, field);
227 if (f_len < 0)
228 goto internal;
229 t_len += f_len;
230 l_len += f_len;
231 if ((size_t)t_len < buflen)
232 bp += f_len;
233 if (l_max > 0 &&
234 (size_t)l_len > l_max) {
235 PUTCHR('#');
236 }
237 }
238 break;
239 case '=':
240 case ':':
241 /*
242 * Here "bit" is actually a value instead,
243 * to be compared against the last field.
244 * This only works for values in [0..255],
245 * of course.
246 */
247 if ((int)field != bit)
248 goto skip;
249 if (ch == '=') {
250 PUTCHR('=');
251 }
252 PUTS(bitfmt);
253 break;
254 default:
255 skip:
256 while (*bitfmt++ != '\0')
257 continue;
258 break;
259 }
260 }
261 }
262 l_len++;
263 if ((size_t)(++t_len) < buflen)
264 *bp++ = '>';
265 terminate:
266 *bp++ = '\0';
267 if (l_max != 0) {
268 t_len++;
269 *bp = '\0';
270 }
271 return t_len;
272 internal:
273 #ifndef _KERNEL
274 errno = EINVAL;
275 #endif
276 return -1;
277 }
278
279 int
280 snprintb(char *buf, size_t buflen, const char *bitfmt, uint64_t val)
281 {
282 return snprintb_m(buf, buflen, bitfmt, val, 0);
283 }
284 # endif /* ! HAVE_SNPRINTB_M */
285 #endif /* ! _STANDALONE */
286