Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.11
      1  1.11  pgoyette /*	$NetBSD: snprintb.c,v 1.11 2013/08/07 23:48:13 pgoyette 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.11  pgoyette __RCSID("$NetBSD: snprintb.c,v 1.11 2013/08/07 23:48:13 pgoyette 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.9       apb # else /* ! _KERNEL */
     53   1.2  christos #  include <sys/cdefs.h>
     54  1.11  pgoyette __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.11 2013/08/07 23:48:13 pgoyette 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.9       apb # endif /* ! _KERNEL */
     60   1.1  christos 
     61   1.9       apb # ifndef HAVE_SNPRINTB_M
     62   1.1  christos int
     63   1.5  pgoyette snprintb_m(char *buf, size_t buflen, const char *bitfmt, uint64_t val,
     64   1.5  pgoyette 	   size_t l_max)
     65   1.1  christos {
     66   1.5  pgoyette 	char *bp = buf, *s_bp = NULL;
     67   1.5  pgoyette 	const char *c_fmt, *s_fmt = NULL, *cur_fmt;
     68   1.1  christos 	const char *sbase;
     69   1.5  pgoyette 	int bit, ch, t_len, s_len = 0, l_len, f_len, v_len, sep;
     70   1.5  pgoyette 	int restart = 0;
     71   1.1  christos 	uint64_t field;
     72   1.1  christos 
     73   1.1  christos #ifdef _KERNEL
     74   1.1  christos 	/*
     75   1.1  christos 	 * For safety; no other *s*printf() do this, but in the kernel
     76   1.1  christos 	 * we don't usually check the return value
     77   1.1  christos 	 */
     78   1.1  christos 	(void)memset(buf, 0, buflen);
     79   1.1  christos #endif /* _KERNEL */
     80   1.1  christos 
     81   1.1  christos 	ch = *bitfmt++;
     82   1.1  christos 	switch (ch != '\177' ? ch : *bitfmt++) {
     83   1.1  christos 	case 8:
     84   1.1  christos 		sbase = "0%" PRIo64;
     85   1.1  christos 		break;
     86   1.1  christos 	case 10:
     87   1.1  christos 		sbase = "%" PRId64;
     88   1.1  christos 		break;
     89   1.1  christos 	case 16:
     90   1.1  christos 		sbase = "0x%" PRIx64;
     91   1.1  christos 		break;
     92   1.1  christos 	default:
     93   1.1  christos 		goto internal;
     94   1.1  christos 	}
     95   1.1  christos 
     96   1.5  pgoyette 	/* Reserve space for trailing blank line if needed */
     97   1.5  pgoyette 	if (l_max > 0)
     98   1.5  pgoyette 		buflen--;
     99   1.5  pgoyette 
    100   1.5  pgoyette 	t_len = snprintf(bp, buflen, sbase, val);
    101   1.5  pgoyette 	if (t_len < 0)
    102   1.1  christos 		goto internal;
    103   1.1  christos 
    104   1.5  pgoyette 	v_len = l_len = t_len;
    105   1.5  pgoyette 
    106   1.5  pgoyette 	if ((size_t)t_len < buflen)
    107   1.5  pgoyette 		bp += t_len;
    108   1.1  christos 	else
    109   1.1  christos 		bp += buflen - 1;
    110   1.1  christos 
    111   1.1  christos 	/*
    112   1.1  christos 	 * If the value we printed was 0 and we're using the old-style format,
    113   1.1  christos 	 * we're done.
    114   1.1  christos 	 */
    115   1.1  christos 	if ((val == 0) && (ch != '\177'))
    116   1.1  christos 		goto terminate;
    117   1.1  christos 
    118   1.5  pgoyette #define STORE(c) { l_len++;						\
    119   1.5  pgoyette 		   if ((size_t)(++t_len) < buflen)			\
    120   1.5  pgoyette 		   	*bp++ = (c);					\
    121   1.5  pgoyette 		 } while ( /* CONSTCOND */ 0)
    122   1.5  pgoyette 
    123   1.5  pgoyette #define	BACKUP	{ if (s_bp != NULL) {					\
    124   1.5  pgoyette 			bp = s_bp; s_bp = NULL;				\
    125   1.5  pgoyette 			t_len -= l_len - s_len;				\
    126   1.5  pgoyette 			restart = 1;					\
    127   1.5  pgoyette 			bitfmt = s_fmt;					\
    128   1.5  pgoyette 		  }							\
    129   1.5  pgoyette 		  STORE('>'); STORE('\0');				\
    130   1.5  pgoyette 		  if ((size_t)t_len < buflen)				\
    131   1.5  pgoyette 			snprintf(bp, buflen - t_len, sbase, val);	\
    132   1.5  pgoyette 		  t_len += v_len; l_len = v_len; bp += v_len;		\
    133   1.5  pgoyette 		} while ( /* CONSTCOND */ 0)
    134   1.5  pgoyette 
    135   1.5  pgoyette #define	PUTSEP								\
    136   1.5  pgoyette 		if (l_max > 0 && (size_t)l_len >= l_max) {		\
    137   1.5  pgoyette 			BACKUP;						\
    138   1.5  pgoyette 			STORE('<');					\
    139   1.5  pgoyette 		} else {						\
    140   1.5  pgoyette 			/* Remember separator location */		\
    141   1.7  christos 			if (l_max > 0 && sep != '<') {			\
    142   1.5  pgoyette 				s_len = l_len;				\
    143   1.5  pgoyette 				s_bp  = bp;				\
    144   1.5  pgoyette 				s_fmt = cur_fmt;			\
    145   1.5  pgoyette 			}						\
    146   1.5  pgoyette 			STORE(sep);					\
    147   1.5  pgoyette 			restart = 0;					\
    148   1.5  pgoyette 		}							\
    149   1.5  pgoyette 
    150   1.5  pgoyette #define	PUTCHR(c)							\
    151   1.5  pgoyette 		if (l_max > 0 && (size_t)l_len >= (l_max - 1)) {	\
    152   1.5  pgoyette 			BACKUP;						\
    153   1.5  pgoyette 			if (restart == 0) {				\
    154   1.5  pgoyette 				STORE(c);				\
    155   1.5  pgoyette 			} else						\
    156   1.5  pgoyette 				sep = '<';				\
    157   1.5  pgoyette 		} else {						\
    158   1.5  pgoyette 			STORE(c);					\
    159   1.5  pgoyette 			restart = 0;					\
    160   1.5  pgoyette 		}							\
    161   1.5  pgoyette 
    162   1.5  pgoyette #define PUTS(s) while ((ch = *(s)++) != 0) {				\
    163   1.5  pgoyette 			PUTCHR(ch);					\
    164   1.5  pgoyette 			if (restart)					\
    165   1.5  pgoyette 				break;					\
    166   1.5  pgoyette 		}
    167   1.1  christos 
    168   1.1  christos 	/*
    169   1.1  christos 	 * Chris Torek's new bitmask format is identified by a leading \177
    170   1.1  christos 	 */
    171   1.1  christos 	sep = '<';
    172   1.1  christos 	if (ch != '\177') {
    173   1.1  christos 		/* old (standard) format. */
    174   1.5  pgoyette 		for (;(bit = *bitfmt) != 0;) {
    175   1.5  pgoyette 			cur_fmt = bitfmt++;
    176   1.1  christos 			if (val & (1 << (bit - 1))) {
    177   1.5  pgoyette 				PUTSEP;
    178   1.5  pgoyette 				if (restart)
    179   1.5  pgoyette 					continue;
    180   1.1  christos 				sep = ',';
    181   1.5  pgoyette 				for (; (ch = *bitfmt) > ' '; ++bitfmt) {
    182   1.5  pgoyette 					PUTCHR(ch);
    183   1.5  pgoyette 					if (restart)
    184   1.5  pgoyette 						break;
    185   1.5  pgoyette 				}
    186   1.1  christos 			} else
    187   1.1  christos 				for (; *bitfmt > ' '; ++bitfmt)
    188   1.1  christos 					continue;
    189   1.1  christos 		}
    190   1.1  christos 	} else {
    191   1.1  christos 		/* new quad-capable format; also does fields. */
    192   1.1  christos 		field = val;
    193   1.5  pgoyette 		while (c_fmt = bitfmt, (ch = *bitfmt++) != '\0') {
    194   1.1  christos 			bit = *bitfmt++;	/* now 0-origin */
    195   1.1  christos 			switch (ch) {
    196   1.1  christos 			case 'b':
    197   1.8       uwe 				if (((unsigned int)(val >> bit) & 1) == 0)
    198   1.1  christos 					goto skip;
    199   1.5  pgoyette 				cur_fmt = c_fmt;
    200   1.5  pgoyette 				PUTSEP;
    201   1.5  pgoyette 				if (restart)
    202   1.5  pgoyette 					break;
    203   1.1  christos 				PUTS(bitfmt);
    204   1.5  pgoyette 				if (restart == 0)
    205   1.5  pgoyette 					sep = ',';
    206   1.1  christos 				break;
    207   1.1  christos 			case 'f':
    208   1.1  christos 			case 'F':
    209   1.5  pgoyette 				cur_fmt = c_fmt;
    210   1.5  pgoyette 				f_len = *bitfmt++;	/* field length */
    211   1.1  christos 				field = (val >> bit) &
    212   1.5  pgoyette 					    (((uint64_t)1 << f_len) - 1);
    213   1.6  christos 				PUTSEP;
    214   1.7  christos 				if (restart == 0)
    215   1.7  christos 					sep = ',';
    216   1.1  christos 				if (ch == 'F')	/* just extract */
    217   1.1  christos 					break;
    218  1.11  pgoyette 				if (restart == 0) {
    219   1.5  pgoyette 					PUTS(bitfmt);
    220  1.11  pgoyette 				}
    221  1.11  pgoyette 				if (restart == 0) {
    222   1.5  pgoyette 					PUTCHR('=');
    223  1.11  pgoyette 				}
    224   1.5  pgoyette 				if (restart == 0) {
    225   1.5  pgoyette 					f_len = snprintf(bp, buflen - t_len,
    226   1.5  pgoyette 							 sbase, field);
    227   1.5  pgoyette 					if (f_len < 0)
    228   1.5  pgoyette 						goto internal;
    229   1.5  pgoyette 					t_len += f_len;
    230   1.5  pgoyette 					l_len += f_len;
    231   1.5  pgoyette 					if ((size_t)t_len < buflen)
    232   1.5  pgoyette 						bp += f_len;
    233   1.5  pgoyette 					if (l_max > 0 &&
    234   1.5  pgoyette 					    (size_t)l_len > l_max) {
    235   1.5  pgoyette 						PUTCHR('#');
    236   1.5  pgoyette 					}
    237   1.5  pgoyette 				}
    238   1.1  christos 				break;
    239   1.1  christos 			case '=':
    240   1.1  christos 			case ':':
    241   1.1  christos 				/*
    242   1.1  christos 				 * Here "bit" is actually a value instead,
    243   1.1  christos 				 * to be compared against the last field.
    244   1.1  christos 				 * This only works for values in [0..255],
    245   1.1  christos 				 * of course.
    246   1.1  christos 				 */
    247   1.1  christos 				if ((int)field != bit)
    248   1.1  christos 					goto skip;
    249   1.1  christos 				if (ch == '=') {
    250   1.5  pgoyette 					PUTCHR('=');
    251   1.1  christos 				}
    252   1.1  christos 				PUTS(bitfmt);
    253   1.1  christos 				break;
    254   1.1  christos 			default:
    255   1.1  christos 			skip:
    256   1.1  christos 				while (*bitfmt++ != '\0')
    257   1.1  christos 					continue;
    258   1.1  christos 				break;
    259   1.1  christos 			}
    260   1.1  christos 		}
    261   1.1  christos 	}
    262   1.5  pgoyette 	l_len++;
    263   1.5  pgoyette 	if ((size_t)(++t_len) < buflen)
    264   1.5  pgoyette 		*bp++ = '>';
    265   1.5  pgoyette terminate:
    266   1.5  pgoyette 	*bp++ = '\0';
    267   1.5  pgoyette 	if (l_max != 0) {
    268   1.5  pgoyette 		t_len++;
    269   1.5  pgoyette 		*bp = '\0';
    270   1.1  christos 	}
    271   1.5  pgoyette 	return t_len;
    272   1.1  christos internal:
    273   1.1  christos #ifndef _KERNEL
    274   1.1  christos 	errno = EINVAL;
    275   1.1  christos #endif
    276   1.1  christos 	return -1;
    277   1.1  christos }
    278   1.5  pgoyette 
    279   1.5  pgoyette int
    280   1.5  pgoyette snprintb(char *buf, size_t buflen, const char *bitfmt, uint64_t val)
    281   1.5  pgoyette {
    282   1.5  pgoyette 	return snprintb_m(buf, buflen, bitfmt, val, 0);
    283   1.5  pgoyette }
    284   1.9       apb # endif /* ! HAVE_SNPRINTB_M */
    285   1.9       apb #endif /* ! _STANDALONE */
    286