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