CVS commit: src/common/lib/libutil

2024-06-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 16 19:41:39 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
libutil/snprintb: factor out common subexpression

GCC 10 was not able to optimize the code size on its own, so offer a
little help.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.48 src/common/lib/libutil/snprintb.c:1.49
--- src/common/lib/libutil/snprintb.c:1.48	Sun Apr  7 15:20:16 2024
+++ src/common/lib/libutil/snprintb.c	Sun Jun 16 19:41:39 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.48 2024/04/07 15:20:16 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.49 2024/06/16 19:41:39 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS)
-__RCSID("$NetBSD: snprintb.c,v 1.48 2024/04/07 15:20:16 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.49 2024/06/16 19:41:39 rillig Exp $");
 #  endif
 
 #  include 
@@ -46,7 +46,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.48 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.48 2024/04/07 15:20:16 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.49 2024/06/16 19:41:39 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -252,17 +252,21 @@ finish_buffer(state *s)
 	if (s->line_max > 0) {
 		store_eol(s);
 		store(s, '\0');
-		if (s->bufsize >= 3 && s->total_len > s->bufsize)
+		if (s->total_len <= s->bufsize)
+			return;
+		if (s->bufsize >= 3)
 			s->buf[s->bufsize - 3] = '#';
-		if (s->bufsize >= 2 && s->total_len > s->bufsize)
+		if (s->bufsize >= 2)
 			s->buf[s->bufsize - 2] = '\0';
-		if (s->bufsize >= 1 && s->total_len > s->bufsize)
+		if (s->bufsize >= 1)
 			s->buf[s->bufsize - 1] = '\0';
 	} else {
 		store(s, '\0');
-		if (s->bufsize >= 2 && s->total_len > s->bufsize)
+		if (s->total_len <= s->bufsize)
+			return;
+		if (s->bufsize >= 2)
 			s->buf[s->bufsize - 2] = '#';
-		if (s->bufsize >= 1 && s->total_len > s->bufsize)
+		if (s->bufsize >= 1)
 			s->buf[s->bufsize - 1] = '\0';
 	}
 }



CVS commit: src/common/lib/libutil

2024-06-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sun Jun 16 19:41:39 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
libutil/snprintb: factor out common subexpression

GCC 10 was not able to optimize the code size on its own, so offer a
little help.


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-04-01 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Apr  1 08:53:42 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: remove redundant memset in kernel mode

The provided buffer is already null-terminated by finish_buffer, even in
error cases, there is no need to repeat the same work.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.44 src/common/lib/libutil/snprintb.c:1.45
--- src/common/lib/libutil/snprintb.c:1.44	Mon Mar 25 20:39:26 2024
+++ src/common/lib/libutil/snprintb.c	Mon Apr  1 08:53:42 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.44 2024/03/25 20:39:26 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.45 2024/04/01 08:53:42 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS)
-__RCSID("$NetBSD: snprintb.c,v 1.44 2024/03/25 20:39:26 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.45 2024/04/01 08:53:42 rillig Exp $");
 #  endif
 
 #  include 
@@ -46,7 +46,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.44 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.44 2024/03/25 20:39:26 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.45 2024/04/01 08:53:42 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -251,15 +251,6 @@ int
 snprintb_m(char *buf, size_t bufsize, const char *bitfmt, uint64_t val,
 	   size_t line_max)
 {
-#ifdef _KERNEL
-	/*
-	 * For safety; no other *s*printf() do this, but in the kernel
-	 * we don't usually check the return value.
-	 */
-	if (bufsize > 0)
-		(void)memset(buf, 0, bufsize);
-#endif /* _KERNEL */
-
 	int old = *bitfmt != '\177';
 	if (!old)
 		bitfmt++;



CVS commit: src/common/lib/libutil

2024-04-01 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Mon Apr  1 08:53:42 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: remove redundant memset in kernel mode

The provided buffer is already null-terminated by finish_buffer, even in
error cases, there is no need to repeat the same work.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-03-04 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Mar  5 07:37:08 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: shrink code size, both in source and binary


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-03-04 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Tue Mar  5 07:37:08 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: shrink code size, both in source and binary


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.42 src/common/lib/libutil/snprintb.c:1.43
--- src/common/lib/libutil/snprintb.c:1.42	Mon Mar  4 21:35:28 2024
+++ src/common/lib/libutil/snprintb.c	Tue Mar  5 07:37:08 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.42 2024/03/04 21:35:28 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.43 2024/03/05 07:37:08 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
@@ -35,17 +35,18 @@
 
 #  include 
 #  if defined(LIBC_SCCS)
-__RCSID("$NetBSD: snprintb.c,v 1.42 2024/03/04 21:35:28 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.43 2024/03/05 07:37:08 rillig Exp $");
 #  endif
 
 #  include 
 #  include 
 #  include 
+#  include 
 #  include 
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.42 2024/03/04 21:35:28 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.43 2024/03/05 07:37:08 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -60,7 +61,7 @@ typedef struct {
 	uint64_t const val;
 	size_t const line_max;
 
-	const char *const num_fmt;
+	char num_fmt[5];
 	size_t total_len;
 	size_t line_pos;
 	size_t comma_pos;
@@ -255,48 +256,42 @@ snprintb_m(char *buf, size_t bufsize, co
 	if (!old)
 		bitfmt++;
 
-	const char *num_fmt;
-	switch (*bitfmt++) {
-	case 8:
-		num_fmt = "%#jo";
-		break;
-	case 10:
-		num_fmt = "%ju";
-		break;
-	case 16:
-		num_fmt = "%#jx";
-		break;
-	default:
-		num_fmt = NULL;
-	}
-
 	state s = {
 		.buf = buf,
 		.bufsize = bufsize,
 		.bitfmt = bitfmt,
 		.val = val,
 		.line_max = line_max,
-		.num_fmt = num_fmt,
 	};
-	if (num_fmt == NULL)
-		goto internal;
+	int had_error = 0;
 
-	store_num(, num_fmt, val);
+	switch (*s.bitfmt++) {
+	case 8:
+		memcpy(s.num_fmt, "%#jo", 4);
+		break;
+	case 10:
+		memcpy(s.num_fmt, "%ju", 4);
+		break;
+	case 16:
+		memcpy(s.num_fmt, "%#jx", 4);
+		break;
+	default:
+		goto had_error;
+	}
 
-	if ((old ? old_style() : new_style()) < 0)
-		goto internal;
+	store_num(, s.num_fmt, val);
 
-	if (s.in_angle_brackets)
-		store(, '>');
-	finish_buffer();
-	return (int)(s.total_len - 1);
-internal:
+	if ((old ? old_style() : new_style()) < 0) {
+had_error:
 #ifndef _KERNEL
-	errno = EINVAL;
+		errno = EINVAL;
 #endif
-	store(, '#');
+		had_error = 1;
+		store(, '#');
+	} else if (s.in_angle_brackets)
+		store(, '>');
 	finish_buffer();
-	return -1;
+	return had_error ? -1 : (int)(s.total_len - 1);
 }
 
 int



CVS commit: src/common/lib/libutil

2024-02-24 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Feb 24 12:44:11 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: clean up

Use consistent data types for buffer positions and lengths, to avoid
type casts.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-24 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Feb 24 12:44:11 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: clean up

Use consistent data types for buffer positions and lengths, to avoid
type casts.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.40 src/common/lib/libutil/snprintb.c:1.41
--- src/common/lib/libutil/snprintb.c:1.40	Sat Feb 24 12:40:00 2024
+++ src/common/lib/libutil/snprintb.c	Sat Feb 24 12:44:11 2024
@@ -1,7 +1,7 @@
-/*	$NetBSD: snprintb.c,v 1.40 2024/02/24 12:40:00 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.41 2024/02/24 12:44:11 rillig Exp $	*/
 
 /*-
- * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,12 +26,6 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- * snprintb: print an interpreted bitmask to a buffer
- *
- * => returns the length of the buffer that would be required to print the
- *string minus the terminating NUL.
- */
 #ifndef _STANDALONE
 # ifndef _KERNEL
 
@@ -40,8 +34,8 @@
 #  endif
 
 #  include 
-#  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.40 2024/02/24 12:40:00 rillig Exp $");
+#  if defined(LIBC_SCCS)
+__RCSID("$NetBSD: snprintb.c,v 1.41 2024/02/24 12:44:11 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +45,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.40 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.40 2024/02/24 12:40:00 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.41 2024/02/24 12:44:11 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -67,10 +61,10 @@ typedef struct {
 	size_t const line_max;
 
 	const char *const num_fmt;
-	unsigned total_len;
-	unsigned line_pos;
-	unsigned comma_pos;
-	char sep;
+	size_t total_len;
+	size_t line_pos;
+	size_t comma_pos;
+	int in_angle_brackets;
 } state;
 
 static void
@@ -94,39 +88,39 @@ store_num(state *s, const char *fmt, uin
 }
 
 static void
-put_eol(state *s)
+store_eol(state *s)
 {
 	if (s->total_len - s->line_pos > s->line_max) {
-		s->total_len = (unsigned)(s->line_pos + s->line_max - 1);
+		s->total_len = s->line_pos + s->line_max - 1;
 		store(s, '#');
 	}
 	store(s, '\0');
 	s->line_pos = s->total_len;
 	s->comma_pos = 0;
-	s->sep = '<';
+	s->in_angle_brackets = 0;
 }
 
 static void
-put_sep(state *s)
+store_delimiter(state *s)
 {
-	if (s->sep == ',') {
+	if (s->in_angle_brackets) {
 		s->comma_pos = s->total_len;
 		store(s, ',');
 	} else {
 		store(s, '<');
-		s->sep = ',';
+		s->in_angle_brackets = 1;
 	}
 }
 
 static void
-wrap_if_necessary(state *s, const char *bitfmt)
+maybe_wrap_line(state *s, const char *bitfmt)
 {
 	if (s->line_max > 0
 	&& s->comma_pos > 0
 	&& s->total_len - s->line_pos >= s->line_max) {
 		s->total_len = s->comma_pos;
 		store(s, '>');
-		put_eol(s);
+		store_eol(s);
 		store_num(s, s->num_fmt, s->val);
 		s->bitfmt = bitfmt;
 	}
@@ -141,10 +135,10 @@ old_style(state *s)
 		if (bit > ' ')
 			return -1;
 		if (s->val & (1U << (bit - 1))) {
-			put_sep(s);
+			store_delimiter(s);
 			while ((uint8_t)*++s->bitfmt > ' ')
 store(s, *s->bitfmt);
-			wrap_if_necessary(s, cur_bitfmt);
+			maybe_wrap_line(s, cur_bitfmt);
 		} else
 			while ((uint8_t)*++s->bitfmt > ' ')
 continue;
@@ -170,10 +164,10 @@ new_style(state *s)
 			s->bitfmt += 2;
 			if (((s->val >> b_bit) & 1) == 0)
 goto skip_description;
-			put_sep(s);
+			store_delimiter(s);
 			while (*s->bitfmt++ != '\0')
 store(s, s->bitfmt[-1]);
-			wrap_if_necessary(s, cur_bitfmt);
+			maybe_wrap_line(s, cur_bitfmt);
 			break;
 		case 'f':
 		case 'F':
@@ -189,14 +183,14 @@ new_style(state *s)
 			if (f_width < 64)
 field &= ((uint64_t) 1 << f_width) - 1;
 			s->bitfmt += 3;
-			put_sep(s);
+			store_delimiter(s);
 			if (kind == 'F')
 goto skip_description;
 			while (*s->bitfmt++ != '\0')
 store(s, s->bitfmt[-1]);
 			store(s, '=');
 			store_num(s, s->num_fmt, field);
-			wrap_if_necessary(s, cur_bitfmt);
+			maybe_wrap_line(s, cur_bitfmt);
 			break;
 		case '=':
 		case ':':
@@ -209,7 +203,7 @@ new_style(state *s)
 store(s, '=');
 			while (*s->bitfmt++ != '\0')
 store(s, s->bitfmt[-1]);
-			wrap_if_necessary(s, prev_bitfmt);
+			maybe_wrap_line(s, prev_bitfmt);
 			break;
 		case '*':
 			s->bitfmt++;
@@ -218,7 +212,7 @@ new_style(state *s)
 			matched = 1;
 			if (store_num(s, s->bitfmt, field) < 0)
 return -1;
-			wrap_if_necessary(s, prev_bitfmt);
+			maybe_wrap_line(s, prev_bitfmt);
 			goto skip_description;
 		default:
 			s->bitfmt += 2;
@@ -235,7 +229,7 @@ static void
 finish_buffer(state 

CVS commit: src/common/lib/libutil

2024-02-17 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Feb 17 10:23:30 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: convert macros to local functions

Let the compiler decide whether to inline the functions; allow stepping
through the code in a debugger.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.34 src/common/lib/libutil/snprintb.c:1.35
--- src/common/lib/libutil/snprintb.c:1.34	Fri Feb 16 21:25:46 2024
+++ src/common/lib/libutil/snprintb.c	Sat Feb 17 10:23:30 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.35 2024/02/17 10:23:30 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.35 2024/02/17 10:23:30 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.34 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.35 2024/02/17 10:23:30 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -59,14 +59,204 @@ __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v
 # endif /* ! _KERNEL */
 
 # ifndef HAVE_SNPRINTB_M
+typedef struct {
+	char *const buf;
+	size_t const bufsize;
+	const char *bitfmt;
+	uint64_t const val;
+	size_t const line_max;
+
+	const char *const num_fmt;
+	unsigned const val_len;
+	unsigned total_len;
+	unsigned line_len;
+
+	const char *cur_bitfmt;
+	int restart;
+
+	const char *sep_bitfmt;
+	unsigned sep_line_len;
+	char sep;
+} state;
+
+static void
+store(state *s, char c)
+{
+	if (s->total_len < s->bufsize)
+		s->buf[s->total_len] = c;
+	s->total_len++;
+	s->line_len++;
+}
+
+static void
+backup(state *s)
+{
+	if (s->sep_line_len > 0) {
+		s->total_len -= s->line_len - s->sep_line_len;
+		s->sep_line_len = 0;
+		s->restart = 1;
+		s->bitfmt = s->sep_bitfmt;
+	}
+	store(s, '>');
+	store(s, '\0');
+	if (s->total_len < s->bufsize)
+		snprintf(s->buf + s->total_len, s->bufsize - s->total_len,
+		s->num_fmt, (uintmax_t)s->val);
+	s->total_len += s->val_len;
+	s->line_len = s->val_len;
+}
+
+static void
+put_sep(state *s)
+{
+	if (s->line_max > 0 && s->line_len >= s->line_max) {
+		backup(s);
+		store(s, '<');
+	} else {
+		if (s->line_max > 0 && s->sep != '<') {
+			s->sep_line_len = s->line_len;
+			s->sep_bitfmt = s->cur_bitfmt;
+		}
+		store(s, s->sep);
+		s->restart = 0;
+	}
+}
+
+static void
+put_chr(state *s, char c)
+{
+	if (s->line_max > 0 && s->line_len >= s->line_max - 1) {
+		backup(s);
+		if (s->restart == 0)
+			store(s, c);
+		else
+			s->sep = '<';
+	} else {
+		store(s, c);
+		s->restart = 0;
+	}
+}
+
+static void
+put_bitfmt(state *s)
+{
+	while (*s->bitfmt++ != 0) {
+		put_chr(s, s->bitfmt[-1]);
+		if (s->restart)
+			break;
+	}
+}
+
+static int
+put_num(state *s, const char *fmt, uintmax_t v)
+{
+	char *bp = s->total_len < s->bufsize ? s->buf + s->total_len : NULL;
+	size_t n = s->total_len < s->bufsize ? s->bufsize - s->total_len : 0;
+	int fmt_len = snprintf(bp, n, fmt, v);
+	if (fmt_len >= 0) {
+		s->total_len += fmt_len;
+		s->line_len += fmt_len;
+	}
+	return fmt_len;
+}
+
+static void
+old_style(state *s)
+{
+	for (uint8_t bit; (bit = *s->bitfmt) != 0;) {
+		s->cur_bitfmt = s->bitfmt++;
+		if (s->val & (1U << (bit - 1))) {
+			put_sep(s);
+			if (s->restart)
+continue;
+			s->sep = ',';
+			for (; *s->bitfmt > ' '; ++s->bitfmt) {
+put_chr(s, *s->bitfmt);
+if (s->restart)
+	break;
+			}
+		} else
+			for (; *s->bitfmt > ' '; ++s->bitfmt)
+continue;
+	}
+}
+
+static int
+new_style(state *s)
+{
+	uint64_t field = s->val;
+	int matched = 1;
+	while (*s->bitfmt != '\0') {
+		uint8_t kind = *s->bitfmt++;
+		uint8_t bit = *s->bitfmt++;
+		switch (kind) {
+		case 'b':
+			if (((s->val >> bit) & 1) == 0)
+goto skip;
+			s->cur_bitfmt = s->bitfmt - 2;
+			put_sep(s);
+			if (s->restart)
+break;
+			put_bitfmt(s);
+			if (s->restart == 0)
+s->sep = ',';
+			break;
+		case 'f':
+		case 'F':
+			matched = 0;
+			s->cur_bitfmt = s->bitfmt - 2;
+			uint8_t field_width = *s->bitfmt++;
+			field = (s->val >> bit) &
+			(((uint64_t)1 << field_width) - 1);
+			put_sep(s);
+			if (s->restart == 0)
+s->sep = ',';
+			if (kind == 'F')
+goto skip;
+			if (s->restart == 0)
+put_bitfmt(s);
+			if (s->restart == 0)
+put_chr(s, '=');
+			if (s->restart == 0) {
+if (put_num(s, s->num_fmt, field) < 0)
+	return -1;
+if (s->line_max > 0
+&& s->line_len > s->line_max)
+	

CVS commit: src/common/lib/libutil

2024-02-17 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Sat Feb 17 10:23:30 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: convert macros to local functions

Let the compiler decide whether to inline the functions; allow stepping
through the code in a debugger.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 21:25:46 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: do not modify bufsize when producing multiple lines


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.33 src/common/lib/libutil/snprintb.c:1.34
--- src/common/lib/libutil/snprintb.c:1.33	Fri Feb 16 19:53:40 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 21:25:46 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.33 2024/02/16 19:53:40 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.33 2024/02/16 19:53:40 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.33 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.33 2024/02/16 19:53:40 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.34 2024/02/16 21:25:46 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -92,10 +92,6 @@ snprintb_m(char *buf, size_t bufsize, co
 		goto internal;
 	}
 
-	/* Reserve space for trailing blank line if needed */
-	if (line_max > 0)
-		bufsize--;
-
 	int val_len = snprintf(buf, bufsize, num_fmt, (uintmax_t)val);
 	if (val_len < 0)
 		goto internal;
@@ -268,13 +264,12 @@ snprintb_m(char *buf, size_t bufsize, co
 	if (sep != '<')
 		STORE('>');
 	if (line_max > 0) {
-		bufsize++;
 		STORE('\0');
-		if (total_len >= bufsize && bufsize > 1)
+		if (bufsize >= 2 && total_len > bufsize - 2)
 			buf[bufsize - 2] = '\0';
 	}
 	STORE('\0');
-	if (total_len >= bufsize && bufsize > 0)
+	if (bufsize >= 1 && total_len > bufsize - 1)
 		buf[bufsize - 1] = '\0';
 	return (int)(total_len - 1);
 internal:



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 21:25:46 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: do not modify bufsize when producing multiple lines


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 19:31:25 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: use size_t for buffer sizes and positions


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.31 src/common/lib/libutil/snprintb.c:1.32
--- src/common/lib/libutil/snprintb.c:1.31	Fri Feb 16 19:20:38 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 19:31:25 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.31 2024/02/16 19:20:38 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.32 2024/02/16 19:31:25 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.31 2024/02/16 19:20:38 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.32 2024/02/16 19:31:25 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.31 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.31 2024/02/16 19:20:38 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.32 2024/02/16 19:31:25 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -65,7 +65,6 @@ snprintb_m(char *buf, size_t bufsize, co
 {
 	char *bp = buf, *sep_bp = NULL;
 	const char *num_fmt, *cur_bitfmt, *sep_bitfmt = NULL;
-	int total_len, val_len, line_len, sep_line_len = 0;
 	char sep;
 	int restart = 0;
 
@@ -98,20 +97,20 @@ snprintb_m(char *buf, size_t bufsize, co
 	if (line_max > 0)
 		bufsize--;
 
-	total_len = snprintf(bp, bufsize, num_fmt, (uintmax_t)val);
-	if (total_len < 0)
+	int val_len = snprintf(bp, bufsize, num_fmt, (uintmax_t)val);
+	if (val_len < 0)
 		goto internal;
 
-	val_len = line_len = total_len;
+	size_t total_len = val_len, line_len = val_len, sep_line_len = 0;
 
-	if ((size_t)total_len < bufsize)
+	if (total_len < bufsize)
 		bp += total_len;
 	else
 		bp += bufsize - 1;
 
 #define	STORE(c) do {			\
 		line_len++;		\
-		if ((size_t)(++total_len) < bufsize)			\
+		if (++total_len < bufsize)\
 			*bp++ = (c);	\
 	} while (0)
 
@@ -125,7 +124,7 @@ snprintb_m(char *buf, size_t bufsize, co
 		}			\
 		STORE('>');		\
 		STORE('\0');		\
-		if ((size_t)total_len < bufsize)			\
+		if (total_len < bufsize)\
 			snprintf(bp, bufsize - total_len, num_fmt,	\
 			(uintmax_t)val);\
 		total_len += val_len;	\
@@ -134,7 +133,7 @@ snprintb_m(char *buf, size_t bufsize, co
 	} while (0)
 
 #define	PUTSEP() do {			\
-		if (line_max > 0 && (size_t)line_len >= line_max) {	\
+		if (line_max > 0 && line_len >= line_max) {		\
 			BACKUP();	\
 			STORE('<');	\
 		} else {		\
@@ -150,7 +149,7 @@ snprintb_m(char *buf, size_t bufsize, co
 	} while (0)
 
 #define	PUTCHR(c) do {			\
-		if (line_max > 0 && (size_t)line_len >= line_max - 1) {	\
+		if (line_max > 0 && line_len >= line_max - 1) {		\
 			BACKUP();	\
 			if (restart == 0)\
 STORE(c);\
@@ -171,14 +170,14 @@ snprintb_m(char *buf, size_t bufsize, co
 	} while (0)
 
 #define	FMTSTR(sb, f) do {		\
-		size_t n = (size_t)total_len < bufsize			\
+		size_t n = total_len < bufsize\
 		? bufsize - total_len : 0;\
 		int fmt_len = snprintf(bp, n, sb, (uintmax_t)f);	\
 		if (fmt_len < 0)	\
 			goto internal;	\
 		total_len += fmt_len;	\
 		line_len += fmt_len;	\
-		if ((size_t)total_len < bufsize)			\
+		if (total_len < bufsize)\
 			bp += fmt_len;	\
 	} while (0)
 
@@ -243,7 +242,7 @@ snprintb_m(char *buf, size_t bufsize, co
 if (restart == 0) {
 	FMTSTR(num_fmt, field);
 	if (line_max > 0
-	&& (size_t)line_len > line_max)
+	&& line_len > line_max)
 		PUTCHR('#');
 }
 break;
@@ -282,13 +281,13 @@ snprintb_m(char *buf, size_t bufsize, co
 	if (line_max > 0) {
 		bufsize++;
 		STORE('\0');
-		if ((size_t)total_len >= bufsize && bufsize > 1)
+		if (total_len >= bufsize && bufsize > 1)
 			buf[bufsize - 2] = '\0';
 	}
 	STORE('\0');
-	if ((size_t)total_len >= bufsize && bufsize > 0)
+	if (total_len >= bufsize && bufsize > 0)
 		buf[bufsize - 1] = '\0';
-	return total_len - 1;
+	return (int)(total_len - 1);
 internal:
 #ifndef _KERNEL
 	errno = EINVAL;



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 19:31:25 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: use size_t for buffer sizes and positions


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 18:17:10 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: use unsigned integers for parsing the bitfmt


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.29 src/common/lib/libutil/snprintb.c:1.30
--- src/common/lib/libutil/snprintb.c:1.29	Fri Feb 16 18:09:15 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 18:17:10 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.29 2024/02/16 18:09:15 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.30 2024/02/16 18:17:10 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.29 2024/02/16 18:09:15 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.30 2024/02/16 18:17:10 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.29 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.29 2024/02/16 18:09:15 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.30 2024/02/16 18:17:10 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -184,7 +184,7 @@ snprintb_m(char *buf, size_t bufsize, co
 	sep = '<';
 	if (old_style) {
 		/* old-style format, 32-bit, 1-origin. */
-		for (int bit; (bit = *bitfmt) != 0;) {
+		for (uint8_t bit; (bit = *bitfmt) != 0;) {
 			cur_bitfmt = bitfmt++;
 			if (val & (1U << (bit - 1))) {
 PUTSEP();
@@ -205,7 +205,7 @@ snprintb_m(char *buf, size_t bufsize, co
 		uint64_t field = val;
 		int matched = 1;
 		while (*bitfmt != '\0') {
-			char kind = *bitfmt++;
+			uint8_t kind = *bitfmt++;
 			uint8_t bit = *bitfmt++;
 			switch (kind) {
 			case 'b':
@@ -223,7 +223,7 @@ snprintb_m(char *buf, size_t bufsize, co
 			case 'F':
 matched = 0;
 cur_bitfmt = bitfmt - 2;
-int field_width = *bitfmt++;
+uint8_t field_width = *bitfmt++;
 field = (val >> bit) &
 (((uint64_t)1 << field_width) - 1);
 PUTSEP();



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 18:17:10 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: use unsigned integers for parsing the bitfmt


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 18:03:16 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: eliminate a few local variables


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.27 src/common/lib/libutil/snprintb.c:1.28
--- src/common/lib/libutil/snprintb.c:1.27	Fri Feb 16 17:42:49 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 18:03:16 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.28 2024/02/16 18:03:16 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.28 2024/02/16 18:03:16 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.27 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.28 2024/02/16 18:03:16 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -64,9 +64,9 @@ snprintb_m(char *buf, size_t bufsize, co
 	   size_t line_max)
 {
 	char *bp = buf, *sep_bp = NULL;
-	const char *c_fmt, *sep_fmt = NULL, *cur_fmt;
-	const char *num_fmt;
-	int ch, total_len, sep_line_len = 0, line_len, val_len, sep;
+	const char *num_fmt, *cur_bitfmt, *sep_bitfmt = NULL;
+	int total_len, val_len, line_len, sep_line_len = 0;
+	char sep;
 	int restart = 0;
 
 #ifdef _KERNEL
@@ -77,8 +77,10 @@ snprintb_m(char *buf, size_t bufsize, co
 	(void)memset(buf, 0, bufsize);
 #endif /* _KERNEL */
 
-	ch = *bitfmt++;
-	switch (ch != '\177' ? ch : *bitfmt++) {
+	int old_style = *bitfmt != '\177';
+	if (!old_style)
+		bitfmt++;
+	switch (*bitfmt++) {
 	case 8:
 		num_fmt = "%#jo";
 		break;
@@ -107,9 +109,6 @@ snprintb_m(char *buf, size_t bufsize, co
 	else
 		bp += bufsize - 1;
 
-	if (val == 0 && ch != '\177')
-		goto done;
-
 #define	STORE(c) do {			\
 		line_len++;		\
 		if ((size_t)(++total_len) < bufsize)			\
@@ -122,7 +121,7 @@ snprintb_m(char *buf, size_t bufsize, co
 			sep_bp = NULL;	\
 			total_len -= line_len - sep_line_len;		\
 			restart = 1;	\
-			bitfmt = sep_fmt;\
+			bitfmt = sep_bitfmt;\
 		}			\
 		STORE('>');		\
 		STORE('\0');		\
@@ -143,7 +142,7 @@ snprintb_m(char *buf, size_t bufsize, co
 			if (line_max > 0 && sep != '<') {		\
 sep_line_len = line_len;		\
 sep_bp = bp;\
-sep_fmt = cur_fmt;			\
+sep_bitfmt = cur_bitfmt;		\
 			}		\
 			STORE(sep);	\
 			restart = 0;	\
@@ -164,8 +163,8 @@ snprintb_m(char *buf, size_t bufsize, co
 	} while (0)
 
 #define	PUTS(s) do {			\
-		while ((ch = *(s)++) != 0) {\
-			PUTCHR(ch);	\
+		while ((*(s)++) != 0) {	\
+			PUTCHR((s)[-1]);\
 			if (restart)	\
 break;	\
 		}			\
@@ -183,17 +182,17 @@ snprintb_m(char *buf, size_t bufsize, co
 	} while (0)
 
 	sep = '<';
-	if (ch != '\177') {
+	if (old_style) {
 		/* old-style format, 32-bit, 1-origin. */
 		for (int bit; (bit = *bitfmt) != 0;) {
-			cur_fmt = bitfmt++;
+			cur_bitfmt = bitfmt++;
 			if (val & (1U << (bit - 1))) {
 PUTSEP();
 if (restart)
 	continue;
 sep = ',';
-for (; (ch = *bitfmt) > ' '; ++bitfmt) {
-	PUTCHR(ch);
+for (; *bitfmt > ' '; ++bitfmt) {
+	PUTCHR(*bitfmt);
 	if (restart)
 		break;
 }
@@ -205,13 +204,14 @@ snprintb_m(char *buf, size_t bufsize, co
 		/* new-style format, 64-bit, 0-origin; also does fields. */
 		uint64_t field = val;
 		int matched = 1;
-		while (c_fmt = bitfmt, (ch = *bitfmt++) != '\0') {
+		while (*bitfmt != '\0') {
+			char kind = *bitfmt++;
 			int bit = *bitfmt++;
-			switch (ch) {
+			switch (kind) {
 			case 'b':
 if (((val >> bit) & 1) == 0)
 	goto skip;
-cur_fmt = c_fmt;
+cur_bitfmt = bitfmt - 2;
 PUTSEP();
 if (restart)
 	break;
@@ -222,14 +222,14 @@ snprintb_m(char *buf, size_t bufsize, co
 			case 'f':
 			case 'F':
 matched = 0;
-cur_fmt = c_fmt;
+cur_bitfmt = bitfmt - 2;
 int field_width = *bitfmt++;
 field = (val >> bit) &
 (((uint64_t)1 << field_width) - 1);
 PUTSEP();
 if (restart == 0)
 	sep = ',';
-if (ch == 'F') {	/* just extract */
+if (kind == 'F') {	/* just extract */
 	/* duplicate PUTS() effect on bitfmt */
 	while (*bitfmt++ != '\0')
 		continue;
@@ -257,7 +257,7 @@ snprintb_m(char *buf, size_t bufsize, co
 if ((int)field != bit)
 	goto skip;
 matched = 1;
-if (ch == '=')
+if (kind == '=')
 	PUTCHR('=');
 PUTS(bitfmt);
 

CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 18:03:16 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: eliminate a few local variables


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 17:42:50 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: rename local variables

The single-letter variables 't', 's', 'l' and 'c' were too hard to
decipher.

The variable 'f_len' was used for two independent purposes.

Use a narrow scope for some variables, to avoid having to keep track of
22 individual variables at the same time.

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.26 src/common/lib/libutil/snprintb.c:1.27
--- src/common/lib/libutil/snprintb.c:1.26	Fri Feb 16 01:57:50 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 17:42:49 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.26 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.27 2024/02/16 17:42:49 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -61,14 +61,13 @@ __KERNEL_RCSID(0, "$NetBSD: snprintb.c,v
 # ifndef HAVE_SNPRINTB_M
 int
 snprintb_m(char *buf, size_t bufsize, const char *bitfmt, uint64_t val,
-	   size_t l_max)
+	   size_t line_max)
 {
-	char *bp = buf, *s_bp = NULL;
-	const char *c_fmt, *s_fmt = NULL, *cur_fmt;
-	const char *sbase;
-	int bit, ch, t_len, s_len = 0, l_len, f_len, v_len, sep;
-	int restart = 0, matched = 1;
-	uint64_t field;
+	char *bp = buf, *sep_bp = NULL;
+	const char *c_fmt, *sep_fmt = NULL, *cur_fmt;
+	const char *num_fmt;
+	int ch, total_len, sep_line_len = 0, line_len, val_len, sep;
+	int restart = 0;
 
 #ifdef _KERNEL
 	/*
@@ -81,73 +80,70 @@ snprintb_m(char *buf, size_t bufsize, co
 	ch = *bitfmt++;
 	switch (ch != '\177' ? ch : *bitfmt++) {
 	case 8:
-		sbase = "%#jo";
+		num_fmt = "%#jo";
 		break;
 	case 10:
-		sbase = "%ju";
+		num_fmt = "%ju";
 		break;
 	case 16:
-		sbase = "%#jx";
+		num_fmt = "%#jx";
 		break;
 	default:
 		goto internal;
 	}
 
 	/* Reserve space for trailing blank line if needed */
-	if (l_max > 0)
+	if (line_max > 0)
 		bufsize--;
 
-	t_len = snprintf(bp, bufsize, sbase, (uintmax_t)val);
-	if (t_len < 0)
+	total_len = snprintf(bp, bufsize, num_fmt, (uintmax_t)val);
+	if (total_len < 0)
 		goto internal;
 
-	v_len = l_len = t_len;
+	val_len = line_len = total_len;
 
-	if ((size_t)t_len < bufsize)
-		bp += t_len;
+	if ((size_t)total_len < bufsize)
+		bp += total_len;
 	else
 		bp += bufsize - 1;
 
-	/*
-	 * If the value we printed was 0 and we're using the old-style format,
-	 * we're done.
-	 */
 	if (val == 0 && ch != '\177')
-		goto terminate;
+		goto done;
 
 #define	STORE(c) do {			\
-		l_len++;		\
-		if ((size_t)(++t_len) < bufsize)			\
+		line_len++;		\
+		if ((size_t)(++total_len) < bufsize)			\
 			*bp++ = (c);	\
 	} while (0)
 
 #define	BACKUP() do {			\
-		if (s_bp != NULL) {	\
-			bp = s_bp;	\
-			s_bp = NULL;	\
-			t_len -= l_len - s_len;\
+		if (sep_bp != NULL) {	\
+			bp = sep_bp;	\
+			sep_bp = NULL;	\
+			total_len -= line_len - sep_line_len;		\
 			restart = 1;	\
-			bitfmt = s_fmt;	\
+			bitfmt = sep_fmt;\
 		}			\
 		STORE('>');		\
 		STORE('\0');		\
-		if ((size_t)t_len < bufsize)\
-			snprintf(bp, bufsize - t_len, sbase, (uintmax_t)val);\
-		t_len += v_len;		\
-		l_len = v_len;		\
-		bp += v_len;		\
+		if ((size_t)total_len < bufsize)			\
+			snprintf(bp, bufsize - total_len, num_fmt,	\
+			(uintmax_t)val);\
+		total_len += val_len;	\
+		line_len = val_len;	\
+		bp += val_len;		\
 	} while (0)
 
 #define	PUTSEP() do {			\
-		if (l_max > 0 && (size_t)l_len >= l_max) {		\
+		if (line_max > 0 && (size_t)line_len >= line_max) {	\
 			BACKUP();	\
 			STORE('<');	\
 		} else {		\
 			/* Remember separator location */		\
-			if (l_max > 0 && sep != '<') {			\
-s_len = l_len;\
-s_bp  = bp;\
-s_fmt = cur_fmt;			\
+			if (line_max > 0 && sep != '<') {		\
+sep_line_len = line_len;		\
+sep_bp = bp;\
+sep_fmt = cur_fmt;			\
 			}		\
 			STORE(sep);	\
 			restart = 0;	\
@@ -155,7 +151,7 @@ snprintb_m(char *buf, size_t bufsize, co
 	} while (0)
 
 #define	PUTCHR(c) do {			\
-		if (l_max > 0 && (size_t)l_len >= 

CVS commit: src/common/lib/libutil

2024-02-16 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 17:42:50 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: rename local variables

The single-letter variables 't', 's', 'l' and 'c' were too hard to
decipher.

The variable 'f_len' was used for two independent purposes.

Use a narrow scope for some variables, to avoid having to keep track of
22 individual variables at the same time.

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2024-02-15 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 01:57:51 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: clean up

Remove redundant parentheses and casts.

Indent statement-like macros consistently, use separate lines for each
statement, add parentheses to macro definitions.

Remove CONSTCOND comments as lint doesn't need them anymore.

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.25 src/common/lib/libutil/snprintb.c:1.26
--- src/common/lib/libutil/snprintb.c:1.25	Thu Feb 15 23:48:51 2024
+++ src/common/lib/libutil/snprintb.c	Fri Feb 16 01:57:50 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.25 2024/02/15 23:48:51 rillig Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.25 2024/02/15 23:48:51 rillig Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.25 2024
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.25 2024/02/15 23:48:51 rillig Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.26 2024/02/16 01:57:50 rillig Exp $");
 #  include 
 #  include 
 #  include 
@@ -112,70 +112,78 @@ snprintb_m(char *buf, size_t bufsize, co
 	 * If the value we printed was 0 and we're using the old-style format,
 	 * we're done.
 	 */
-	if ((val == 0) && (ch != '\177'))
+	if (val == 0 && ch != '\177')
 		goto terminate;
 
-#define STORE(c) do { l_len++;		\
-		   if ((size_t)(++t_len) < bufsize)			\
-		   	*bp++ = (c);	\
-		 } while ( /* CONSTCOND */ 0)
-
-#define	BACKUP	do { if (s_bp != NULL) {\
-			bp = s_bp; s_bp = NULL;\
+#define	STORE(c) do {			\
+		l_len++;		\
+		if ((size_t)(++t_len) < bufsize)			\
+			*bp++ = (c);	\
+	} while (0)
+
+#define	BACKUP() do {			\
+		if (s_bp != NULL) {	\
+			bp = s_bp;	\
+			s_bp = NULL;	\
 			t_len -= l_len - s_len;\
 			restart = 1;	\
 			bitfmt = s_fmt;	\
-		  }			\
-		  STORE('>'); STORE('\0');\
-		  if ((size_t)t_len < bufsize)\
+		}			\
+		STORE('>');		\
+		STORE('\0');		\
+		if ((size_t)t_len < bufsize)\
 			snprintf(bp, bufsize - t_len, sbase, (uintmax_t)val);\
-		  t_len += v_len; l_len = v_len; bp += v_len;		\
-		} while ( /* CONSTCOND */ 0)
-
-#define	PUTSEP do {			\
-			if (l_max > 0 && (size_t)l_len >= l_max) {	\
-BACKUP;	\
-STORE('<');\
-			} else {	\
-/* Remember separator location */	\
-if (l_max > 0 && sep != '<') {		\
-	s_len = l_len;			\
-	s_bp  = bp;			\
-	s_fmt = cur_fmt;		\
-}	\
-STORE(sep);\
-restart = 0;\
+		t_len += v_len;		\
+		l_len = v_len;		\
+		bp += v_len;		\
+	} while (0)
+
+#define	PUTSEP() do {			\
+		if (l_max > 0 && (size_t)l_len >= l_max) {		\
+			BACKUP();	\
+			STORE('<');	\
+		} else {		\
+			/* Remember separator location */		\
+			if (l_max > 0 && sep != '<') {			\
+s_len = l_len;\
+s_bp  = bp;\
+s_fmt = cur_fmt;			\
 			}		\
-		} while ( /* CONSTCOND */ 0)
+			STORE(sep);	\
+			restart = 0;	\
+		}			\
+	} while (0)
 
 #define	PUTCHR(c) do {			\
-			if (l_max > 0 && (size_t)l_len >= (l_max - 1)) {\
-BACKUP;	\
-if (restart == 0)			\
-	STORE(c);			\
-else	\
-	sep = '<';			\
-			} else {	\
+		if (l_max > 0 && (size_t)l_len >= l_max - 1) {		\
+			BACKUP();	\
+			if (restart == 0)\
 STORE(c);\
-restart = 0;\
-			}		\
-		} while ( /* CONSTCOND */ 0)
+			else		\
+sep = '<';\
+		} else {		\
+			STORE(c);	\
+			restart = 0;	\
+		}			\
+	} while (0)
 
-#define PUTS(s) while ((ch = *(s)++) != 0) {\
+#define	PUTS(s) do {			\
+		while ((ch = *(s)++) != 0) {\
 			PUTCHR(ch);	\
 			if (restart)	\
 break;	\
-		}
-#define FMTSTR(sb, f) 			\
-	do { \
+		}			\
+	} while (0)
+
+#define	FMTSTR(sb, f) do {		\
 		f_len = snprintf(bp, bufsize - t_len, sb, (uintmax_t)f); \
-		if (f_len < 0) 		\
-			goto internal; 	\
-		t_len += f_len; 	\
-		l_len += f_len; 	\
-		if ((size_t)t_len < bufsize) \
-			bp += f_len; 	\
-	} while (/*CONSTCOND*/0)
+		if (f_len < 0)		\
+			goto internal;	\
+		t_len += f_len;		\
+		l_len += f_len;		\
+		if ((size_t)t_len < bufsize)\
+			bp += f_len;	\
+	} while (0)
 
 	/*
 	 * Chris Torek's new bitmask format is identified by a leading \177
@@ -183,10 +191,10 @@ 

CVS commit: src/common/lib/libutil

2024-02-15 Thread Roland Illig
Module Name:src
Committed By:   rillig
Date:   Fri Feb 16 01:57:51 UTC 2024

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
snprintb: clean up

Remove redundant parentheses and casts.

Indent statement-like macros consistently, use separate lines for each
statement, add parentheses to macro definitions.

Remove CONSTCOND comments as lint doesn't need them anymore.

No binary change.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2019-06-17 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Jun 17 17:03:58 UTC 2019

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
PR/54300: Andreas Gustafsson: Remove useless case. should fix
lib/libutil/t_snprintb test regression on sparc


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/common/lib/libutil

2019-06-17 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Jun 17 17:03:58 UTC 2019

Modified Files:
src/common/lib/libutil: snprintb.c

Log Message:
PR/54300: Andreas Gustafsson: Remove useless case. should fix
lib/libutil/t_snprintb test regression on sparc


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/common/lib/libutil/snprintb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/common/lib/libutil/snprintb.c
diff -u src/common/lib/libutil/snprintb.c:1.20 src/common/lib/libutil/snprintb.c:1.21
--- src/common/lib/libutil/snprintb.c:1.20	Mon Apr 29 03:55:38 2019
+++ src/common/lib/libutil/snprintb.c	Mon Jun 17 13:03:58 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: snprintb.c,v 1.20 2019/04/29 07:55:38 kre Exp $	*/
+/*	$NetBSD: snprintb.c,v 1.21 2019/06/17 17:03:58 christos Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -41,7 +41,7 @@
 
 #  include 
 #  if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: snprintb.c,v 1.20 2019/04/29 07:55:38 kre Exp $");
+__RCSID("$NetBSD: snprintb.c,v 1.21 2019/06/17 17:03:58 christos Exp $");
 #  endif
 
 #  include 
@@ -51,7 +51,7 @@ __RCSID("$NetBSD: snprintb.c,v 1.20 2019
 #  include 
 # else /* ! _KERNEL */
 #  include 
-__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.20 2019/04/29 07:55:38 kre Exp $");
+__KERNEL_RCSID(0, "$NetBSD: snprintb.c,v 1.21 2019/06/17 17:03:58 christos Exp $");
 #  include 
 #  include 
 #  include 
@@ -168,7 +168,7 @@ snprintb_m(char *buf, size_t buflen, con
 		}
 #define FMTSTR(sb, f) 			\
 	do { \
-		f_len = snprintf(bp, buflen - t_len, sb, (int)f);	\
+		f_len = snprintf(bp, buflen - t_len, sb, f);		\
 		if (f_len < 0) 		\
 			goto internal; 	\
 		t_len += f_len; 	\