Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.26
      1  1.26    rillig /*	$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig 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.26    rillig __RCSID("$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
     45   1.2  christos #  endif
     46   1.2  christos 
     47   1.2  christos #  include <sys/types.h>
     48  1.13       agc #  include <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.26    rillig __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig 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.24    rillig snprintb_m(char *buf, size_t bufsize, 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.19  christos 	int restart = 0, matched = 1;
     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.24    rillig 	(void)memset(buf, 0, bufsize);
     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.22  christos 		sbase = "%#jo";
     85   1.1  christos 		break;
     86   1.1  christos 	case 10:
     87  1.22  christos 		sbase = "%ju";
     88   1.1  christos 		break;
     89   1.1  christos 	case 16:
     90  1.22  christos 		sbase = "%#jx";
     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.24    rillig 		bufsize--;
     99   1.5  pgoyette 
    100  1.24    rillig 	t_len = snprintf(bp, bufsize, sbase, (uintmax_t)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.24    rillig 	if ((size_t)t_len < bufsize)
    107   1.5  pgoyette 		bp += t_len;
    108   1.1  christos 	else
    109  1.24    rillig 		bp += bufsize - 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.26    rillig 	if (val == 0 && ch != '\177')
    116   1.1  christos 		goto terminate;
    117   1.1  christos 
    118  1.26    rillig #define	STORE(c) do {							\
    119  1.26    rillig 		l_len++;						\
    120  1.26    rillig 		if ((size_t)(++t_len) < bufsize)			\
    121  1.26    rillig 			*bp++ = (c);					\
    122  1.26    rillig 	} while (0)
    123  1.26    rillig 
    124  1.26    rillig #define	BACKUP() do {							\
    125  1.26    rillig 		if (s_bp != NULL) {					\
    126  1.26    rillig 			bp = s_bp;					\
    127  1.26    rillig 			s_bp = NULL;					\
    128   1.5  pgoyette 			t_len -= l_len - s_len;				\
    129   1.5  pgoyette 			restart = 1;					\
    130   1.5  pgoyette 			bitfmt = s_fmt;					\
    131  1.26    rillig 		}							\
    132  1.26    rillig 		STORE('>');						\
    133  1.26    rillig 		STORE('\0');						\
    134  1.26    rillig 		if ((size_t)t_len < bufsize)				\
    135  1.24    rillig 			snprintf(bp, bufsize - t_len, sbase, (uintmax_t)val);\
    136  1.26    rillig 		t_len += v_len;						\
    137  1.26    rillig 		l_len = v_len;						\
    138  1.26    rillig 		bp += v_len;						\
    139  1.26    rillig 	} while (0)
    140  1.26    rillig 
    141  1.26    rillig #define	PUTSEP() do {							\
    142  1.26    rillig 		if (l_max > 0 && (size_t)l_len >= l_max) {		\
    143  1.26    rillig 			BACKUP();					\
    144  1.26    rillig 			STORE('<');					\
    145  1.26    rillig 		} else {						\
    146  1.26    rillig 			/* Remember separator location */		\
    147  1.26    rillig 			if (l_max > 0 && sep != '<') {			\
    148  1.26    rillig 				s_len = l_len;				\
    149  1.26    rillig 				s_bp  = bp;				\
    150  1.26    rillig 				s_fmt = cur_fmt;			\
    151   1.5  pgoyette 			}						\
    152  1.26    rillig 			STORE(sep);					\
    153  1.26    rillig 			restart = 0;					\
    154  1.26    rillig 		}							\
    155  1.26    rillig 	} while (0)
    156  1.12       mrg 
    157  1.12       mrg #define	PUTCHR(c) do {							\
    158  1.26    rillig 		if (l_max > 0 && (size_t)l_len >= l_max - 1) {		\
    159  1.26    rillig 			BACKUP();					\
    160  1.26    rillig 			if (restart == 0)				\
    161   1.5  pgoyette 				STORE(c);				\
    162  1.26    rillig 			else						\
    163  1.26    rillig 				sep = '<';				\
    164  1.26    rillig 		} else {						\
    165  1.26    rillig 			STORE(c);					\
    166  1.26    rillig 			restart = 0;					\
    167  1.26    rillig 		}							\
    168  1.26    rillig 	} while (0)
    169   1.5  pgoyette 
    170  1.26    rillig #define	PUTS(s) do {							\
    171  1.26    rillig 		while ((ch = *(s)++) != 0) {				\
    172   1.5  pgoyette 			PUTCHR(ch);					\
    173   1.5  pgoyette 			if (restart)					\
    174   1.5  pgoyette 				break;					\
    175  1.26    rillig 		}							\
    176  1.26    rillig 	} while (0)
    177  1.26    rillig 
    178  1.26    rillig #define	FMTSTR(sb, f) do {						\
    179  1.24    rillig 		f_len = snprintf(bp, bufsize - t_len, sb, (uintmax_t)f); \
    180  1.26    rillig 		if (f_len < 0)						\
    181  1.26    rillig 			goto internal;					\
    182  1.26    rillig 		t_len += f_len;						\
    183  1.26    rillig 		l_len += f_len;						\
    184  1.26    rillig 		if ((size_t)t_len < bufsize)				\
    185  1.26    rillig 			bp += f_len;					\
    186  1.26    rillig 	} while (0)
    187   1.1  christos 
    188   1.1  christos 	/*
    189   1.1  christos 	 * Chris Torek's new bitmask format is identified by a leading \177
    190   1.1  christos 	 */
    191   1.1  christos 	sep = '<';
    192   1.1  christos 	if (ch != '\177') {
    193   1.1  christos 		/* old (standard) format. */
    194  1.26    rillig 		while ((bit = *bitfmt) != 0) {
    195   1.5  pgoyette 			cur_fmt = bitfmt++;
    196  1.18     kamil 			if (val & (1U << (bit - 1))) {
    197  1.26    rillig 				PUTSEP();
    198   1.5  pgoyette 				if (restart)
    199   1.5  pgoyette 					continue;
    200   1.1  christos 				sep = ',';
    201   1.5  pgoyette 				for (; (ch = *bitfmt) > ' '; ++bitfmt) {
    202   1.5  pgoyette 					PUTCHR(ch);
    203   1.5  pgoyette 					if (restart)
    204   1.5  pgoyette 						break;
    205   1.5  pgoyette 				}
    206   1.1  christos 			} else
    207   1.1  christos 				for (; *bitfmt > ' '; ++bitfmt)
    208   1.1  christos 					continue;
    209   1.1  christos 		}
    210   1.1  christos 	} else {
    211   1.1  christos 		/* new quad-capable format; also does fields. */
    212   1.1  christos 		field = val;
    213   1.5  pgoyette 		while (c_fmt = bitfmt, (ch = *bitfmt++) != '\0') {
    214   1.1  christos 			bit = *bitfmt++;	/* now 0-origin */
    215   1.1  christos 			switch (ch) {
    216   1.1  christos 			case 'b':
    217  1.26    rillig 				if (((val >> bit) & 1) == 0)
    218   1.1  christos 					goto skip;
    219   1.5  pgoyette 				cur_fmt = c_fmt;
    220  1.26    rillig 				PUTSEP();
    221   1.5  pgoyette 				if (restart)
    222   1.5  pgoyette 					break;
    223   1.1  christos 				PUTS(bitfmt);
    224   1.5  pgoyette 				if (restart == 0)
    225   1.5  pgoyette 					sep = ',';
    226   1.1  christos 				break;
    227   1.1  christos 			case 'f':
    228   1.1  christos 			case 'F':
    229  1.19  christos 				matched = 0;
    230   1.5  pgoyette 				cur_fmt = c_fmt;
    231   1.5  pgoyette 				f_len = *bitfmt++;	/* field length */
    232   1.1  christos 				field = (val >> bit) &
    233  1.26    rillig 				    (((uint64_t)1 << f_len) - 1);
    234  1.26    rillig 				PUTSEP();
    235   1.7  christos 				if (restart == 0)
    236   1.7  christos 					sep = ',';
    237  1.20       kre 				if (ch == 'F') {	/* just extract */
    238  1.20       kre 					/* duplicate PUTS() effect on bitfmt */
    239  1.20       kre 					while (*bitfmt++ != '\0')
    240  1.20       kre 						continue;
    241   1.1  christos 					break;
    242  1.20       kre 				}
    243  1.14  pgoyette 				if (restart == 0)
    244   1.5  pgoyette 					PUTS(bitfmt);
    245  1.14  pgoyette 				if (restart == 0)
    246   1.5  pgoyette 					PUTCHR('=');
    247   1.5  pgoyette 				if (restart == 0) {
    248  1.19  christos 					FMTSTR(sbase, field);
    249  1.14  pgoyette 					if (l_max > 0 && (size_t)l_len > l_max)
    250   1.5  pgoyette 						PUTCHR('#');
    251   1.5  pgoyette 				}
    252   1.1  christos 				break;
    253   1.1  christos 			case '=':
    254   1.1  christos 			case ':':
    255   1.1  christos 				/*
    256   1.1  christos 				 * Here "bit" is actually a value instead,
    257   1.1  christos 				 * to be compared against the last field.
    258   1.1  christos 				 * This only works for values in [0..255],
    259   1.1  christos 				 * of course.
    260   1.1  christos 				 */
    261   1.1  christos 				if ((int)field != bit)
    262   1.1  christos 					goto skip;
    263  1.19  christos 				matched = 1;
    264  1.14  pgoyette 				if (ch == '=')
    265   1.5  pgoyette 					PUTCHR('=');
    266   1.1  christos 				PUTS(bitfmt);
    267   1.1  christos 				break;
    268  1.19  christos 			case '*':
    269  1.19  christos 				bitfmt--;
    270  1.19  christos 				if (!matched) {
    271  1.19  christos 					matched = 1;
    272  1.19  christos 					FMTSTR(bitfmt, field);
    273  1.19  christos 				}
    274  1.19  christos 				/*FALLTHROUGH*/
    275   1.1  christos 			default:
    276   1.1  christos 			skip:
    277   1.1  christos 				while (*bitfmt++ != '\0')
    278   1.1  christos 					continue;
    279   1.1  christos 				break;
    280   1.1  christos 			}
    281   1.1  christos 		}
    282   1.1  christos 	}
    283  1.23    rillig 	if (sep != '<')
    284  1.23    rillig 		STORE('>');
    285   1.5  pgoyette terminate:
    286  1.25    rillig 	if (l_max > 0) {
    287  1.25    rillig 		bufsize++;
    288  1.23    rillig 		STORE('\0');
    289  1.25    rillig 		if ((size_t)t_len >= bufsize && bufsize > 1)
    290  1.25    rillig 			buf[bufsize - 2] = '\0';
    291   1.1  christos 	}
    292  1.25    rillig 	STORE('\0');
    293  1.25    rillig 	if ((size_t)t_len >= bufsize && bufsize > 0)
    294  1.25    rillig 		buf[bufsize - 1] = '\0';
    295  1.25    rillig 	return t_len - 1;
    296   1.1  christos internal:
    297   1.1  christos #ifndef _KERNEL
    298   1.1  christos 	errno = EINVAL;
    299   1.1  christos #endif
    300   1.1  christos 	return -1;
    301   1.1  christos }
    302   1.5  pgoyette 
    303   1.5  pgoyette int
    304  1.24    rillig snprintb(char *buf, size_t bufsize, const char *bitfmt, uint64_t val)
    305   1.5  pgoyette {
    306  1.24    rillig 	return snprintb_m(buf, bufsize, bitfmt, val, 0);
    307   1.5  pgoyette }
    308   1.9       apb # endif /* ! HAVE_SNPRINTB_M */
    309   1.9       apb #endif /* ! _STANDALONE */
    310