Home | History | Annotate | Line # | Download | only in libutil
snprintb.c revision 1.27
      1  1.27    rillig /*	$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 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.27    rillig __RCSID("$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 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.27    rillig __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 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.27    rillig 	   size_t line_max)
     65   1.1  christos {
     66  1.27    rillig 	char *bp = buf, *sep_bp = NULL;
     67  1.27    rillig 	const char *c_fmt, *sep_fmt = NULL, *cur_fmt;
     68  1.27    rillig 	const char *num_fmt;
     69  1.27    rillig 	int ch, total_len, sep_line_len = 0, line_len, val_len, sep;
     70  1.27    rillig 	int restart = 0;
     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.24    rillig 	(void)memset(buf, 0, bufsize);
     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.27    rillig 		num_fmt = "%#jo";
     84   1.1  christos 		break;
     85   1.1  christos 	case 10:
     86  1.27    rillig 		num_fmt = "%ju";
     87   1.1  christos 		break;
     88   1.1  christos 	case 16:
     89  1.27    rillig 		num_fmt = "%#jx";
     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.5  pgoyette 	/* Reserve space for trailing blank line if needed */
     96  1.27    rillig 	if (line_max > 0)
     97  1.24    rillig 		bufsize--;
     98   1.5  pgoyette 
     99  1.27    rillig 	total_len = snprintf(bp, bufsize, num_fmt, (uintmax_t)val);
    100  1.27    rillig 	if (total_len < 0)
    101   1.1  christos 		goto internal;
    102   1.1  christos 
    103  1.27    rillig 	val_len = line_len = total_len;
    104   1.5  pgoyette 
    105  1.27    rillig 	if ((size_t)total_len < bufsize)
    106  1.27    rillig 		bp += total_len;
    107   1.1  christos 	else
    108  1.24    rillig 		bp += bufsize - 1;
    109   1.1  christos 
    110  1.26    rillig 	if (val == 0 && ch != '\177')
    111  1.27    rillig 		goto done;
    112   1.1  christos 
    113  1.26    rillig #define	STORE(c) do {							\
    114  1.27    rillig 		line_len++;						\
    115  1.27    rillig 		if ((size_t)(++total_len) < bufsize)			\
    116  1.26    rillig 			*bp++ = (c);					\
    117  1.26    rillig 	} while (0)
    118  1.26    rillig 
    119  1.26    rillig #define	BACKUP() do {							\
    120  1.27    rillig 		if (sep_bp != NULL) {					\
    121  1.27    rillig 			bp = sep_bp;					\
    122  1.27    rillig 			sep_bp = NULL;					\
    123  1.27    rillig 			total_len -= line_len - sep_line_len;		\
    124   1.5  pgoyette 			restart = 1;					\
    125  1.27    rillig 			bitfmt = sep_fmt;				\
    126  1.26    rillig 		}							\
    127  1.26    rillig 		STORE('>');						\
    128  1.26    rillig 		STORE('\0');						\
    129  1.27    rillig 		if ((size_t)total_len < bufsize)			\
    130  1.27    rillig 			snprintf(bp, bufsize - total_len, num_fmt,	\
    131  1.27    rillig 			    (uintmax_t)val);				\
    132  1.27    rillig 		total_len += val_len;					\
    133  1.27    rillig 		line_len = val_len;					\
    134  1.27    rillig 		bp += val_len;						\
    135  1.26    rillig 	} while (0)
    136  1.26    rillig 
    137  1.26    rillig #define	PUTSEP() do {							\
    138  1.27    rillig 		if (line_max > 0 && (size_t)line_len >= line_max) {	\
    139  1.26    rillig 			BACKUP();					\
    140  1.26    rillig 			STORE('<');					\
    141  1.26    rillig 		} else {						\
    142  1.26    rillig 			/* Remember separator location */		\
    143  1.27    rillig 			if (line_max > 0 && sep != '<') {		\
    144  1.27    rillig 				sep_line_len = line_len;		\
    145  1.27    rillig 				sep_bp = bp;				\
    146  1.27    rillig 				sep_fmt = cur_fmt;			\
    147   1.5  pgoyette 			}						\
    148  1.26    rillig 			STORE(sep);					\
    149  1.26    rillig 			restart = 0;					\
    150  1.26    rillig 		}							\
    151  1.26    rillig 	} while (0)
    152  1.12       mrg 
    153  1.12       mrg #define	PUTCHR(c) do {							\
    154  1.27    rillig 		if (line_max > 0 && (size_t)line_len >= line_max - 1) {	\
    155  1.26    rillig 			BACKUP();					\
    156  1.26    rillig 			if (restart == 0)				\
    157   1.5  pgoyette 				STORE(c);				\
    158  1.26    rillig 			else						\
    159  1.26    rillig 				sep = '<';				\
    160  1.26    rillig 		} else {						\
    161  1.26    rillig 			STORE(c);					\
    162  1.26    rillig 			restart = 0;					\
    163  1.26    rillig 		}							\
    164  1.26    rillig 	} while (0)
    165   1.5  pgoyette 
    166  1.26    rillig #define	PUTS(s) do {							\
    167  1.26    rillig 		while ((ch = *(s)++) != 0) {				\
    168   1.5  pgoyette 			PUTCHR(ch);					\
    169   1.5  pgoyette 			if (restart)					\
    170   1.5  pgoyette 				break;					\
    171  1.26    rillig 		}							\
    172  1.26    rillig 	} while (0)
    173  1.26    rillig 
    174  1.26    rillig #define	FMTSTR(sb, f) do {						\
    175  1.27    rillig 		int fmt_len = snprintf(bp, bufsize - total_len, sb,	\
    176  1.27    rillig 		    (uintmax_t)f);					\
    177  1.27    rillig 		if (fmt_len < 0)					\
    178  1.26    rillig 			goto internal;					\
    179  1.27    rillig 		total_len += fmt_len;					\
    180  1.27    rillig 		line_len += fmt_len;					\
    181  1.27    rillig 		if ((size_t)total_len < bufsize)			\
    182  1.27    rillig 			bp += fmt_len;					\
    183  1.26    rillig 	} while (0)
    184   1.1  christos 
    185   1.1  christos 	sep = '<';
    186   1.1  christos 	if (ch != '\177') {
    187  1.27    rillig 		/* old-style format, 32-bit, 1-origin. */
    188  1.27    rillig 		for (int bit; (bit = *bitfmt) != 0;) {
    189   1.5  pgoyette 			cur_fmt = bitfmt++;
    190  1.18     kamil 			if (val & (1U << (bit - 1))) {
    191  1.26    rillig 				PUTSEP();
    192   1.5  pgoyette 				if (restart)
    193   1.5  pgoyette 					continue;
    194   1.1  christos 				sep = ',';
    195   1.5  pgoyette 				for (; (ch = *bitfmt) > ' '; ++bitfmt) {
    196  1.27    rillig 					PUTCHR(ch);
    197   1.5  pgoyette 					if (restart)
    198   1.5  pgoyette 						break;
    199   1.5  pgoyette 				}
    200   1.1  christos 			} else
    201   1.1  christos 				for (; *bitfmt > ' '; ++bitfmt)
    202   1.1  christos 					continue;
    203   1.1  christos 		}
    204   1.1  christos 	} else {
    205  1.27    rillig 		/* new-style format, 64-bit, 0-origin; also does fields. */
    206  1.27    rillig 		uint64_t field = val;
    207  1.27    rillig 		int matched = 1;
    208   1.5  pgoyette 		while (c_fmt = bitfmt, (ch = *bitfmt++) != '\0') {
    209  1.27    rillig 			int bit = *bitfmt++;
    210   1.1  christos 			switch (ch) {
    211   1.1  christos 			case 'b':
    212  1.26    rillig 				if (((val >> bit) & 1) == 0)
    213   1.1  christos 					goto skip;
    214   1.5  pgoyette 				cur_fmt = c_fmt;
    215  1.26    rillig 				PUTSEP();
    216   1.5  pgoyette 				if (restart)
    217   1.5  pgoyette 					break;
    218   1.1  christos 				PUTS(bitfmt);
    219   1.5  pgoyette 				if (restart == 0)
    220   1.5  pgoyette 					sep = ',';
    221   1.1  christos 				break;
    222   1.1  christos 			case 'f':
    223   1.1  christos 			case 'F':
    224  1.19  christos 				matched = 0;
    225   1.5  pgoyette 				cur_fmt = c_fmt;
    226  1.27    rillig 				int field_width = *bitfmt++;
    227   1.1  christos 				field = (val >> bit) &
    228  1.27    rillig 				    (((uint64_t)1 << field_width) - 1);
    229  1.26    rillig 				PUTSEP();
    230   1.7  christos 				if (restart == 0)
    231   1.7  christos 					sep = ',';
    232  1.20       kre 				if (ch == 'F') {	/* just extract */
    233  1.20       kre 					/* duplicate PUTS() effect on bitfmt */
    234  1.20       kre 					while (*bitfmt++ != '\0')
    235  1.20       kre 						continue;
    236   1.1  christos 					break;
    237  1.20       kre 				}
    238  1.14  pgoyette 				if (restart == 0)
    239   1.5  pgoyette 					PUTS(bitfmt);
    240  1.14  pgoyette 				if (restart == 0)
    241   1.5  pgoyette 					PUTCHR('=');
    242   1.5  pgoyette 				if (restart == 0) {
    243  1.27    rillig 					FMTSTR(num_fmt, field);
    244  1.27    rillig 					if (line_max > 0
    245  1.27    rillig 					    && (size_t)line_len > line_max)
    246   1.5  pgoyette 						PUTCHR('#');
    247   1.5  pgoyette 				}
    248   1.1  christos 				break;
    249   1.1  christos 			case '=':
    250   1.1  christos 			case ':':
    251   1.1  christos 				/*
    252   1.1  christos 				 * Here "bit" is actually a value instead,
    253   1.1  christos 				 * to be compared against the last field.
    254   1.1  christos 				 * This only works for values in [0..255],
    255   1.1  christos 				 * of course.
    256   1.1  christos 				 */
    257   1.1  christos 				if ((int)field != bit)
    258   1.1  christos 					goto skip;
    259  1.19  christos 				matched = 1;
    260  1.14  pgoyette 				if (ch == '=')
    261   1.5  pgoyette 					PUTCHR('=');
    262   1.1  christos 				PUTS(bitfmt);
    263   1.1  christos 				break;
    264  1.19  christos 			case '*':
    265  1.19  christos 				bitfmt--;
    266  1.19  christos 				if (!matched) {
    267  1.19  christos 					matched = 1;
    268  1.19  christos 					FMTSTR(bitfmt, field);
    269  1.19  christos 				}
    270  1.19  christos 				/*FALLTHROUGH*/
    271   1.1  christos 			default:
    272   1.1  christos 			skip:
    273   1.1  christos 				while (*bitfmt++ != '\0')
    274   1.1  christos 					continue;
    275   1.1  christos 				break;
    276   1.1  christos 			}
    277   1.1  christos 		}
    278   1.1  christos 	}
    279  1.23    rillig 	if (sep != '<')
    280  1.23    rillig 		STORE('>');
    281  1.27    rillig done:
    282  1.27    rillig 	if (line_max > 0) {
    283  1.25    rillig 		bufsize++;
    284  1.23    rillig 		STORE('\0');
    285  1.27    rillig 		if ((size_t)total_len >= bufsize && bufsize > 1)
    286  1.25    rillig 			buf[bufsize - 2] = '\0';
    287   1.1  christos 	}
    288  1.25    rillig 	STORE('\0');
    289  1.27    rillig 	if ((size_t)total_len >= bufsize && bufsize > 0)
    290  1.25    rillig 		buf[bufsize - 1] = '\0';
    291  1.27    rillig 	return total_len - 1;
    292   1.1  christos internal:
    293   1.1  christos #ifndef _KERNEL
    294   1.1  christos 	errno = EINVAL;
    295   1.1  christos #endif
    296   1.1  christos 	return -1;
    297   1.1  christos }
    298   1.5  pgoyette 
    299   1.5  pgoyette int
    300  1.24    rillig snprintb(char *buf, size_t bufsize, const char *bitfmt, uint64_t val)
    301   1.5  pgoyette {
    302  1.24    rillig 	return snprintb_m(buf, bufsize, bitfmt, val, 0);
    303   1.5  pgoyette }
    304   1.9       apb # endif /* ! HAVE_SNPRINTB_M */
    305   1.9       apb #endif /* ! _STANDALONE */
    306