Module Name:    src
Committed By:   skrll
Date:           Thu Apr 20 09:04:45 UTC 2023

Modified Files:
        src/sys/kern: subr_optstr.c
        src/sys/sys: optstr.h

Log Message:
Extend optstr(9) to provide some functions to convert the value.

Proposed on tech-kern some time ago.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/kern/subr_optstr.c
cvs rdiff -u -r1.3 -r1.4 src/sys/sys/optstr.h

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

Modified files:

Index: src/sys/kern/subr_optstr.c
diff -u src/sys/kern/subr_optstr.c:1.7 src/sys/kern/subr_optstr.c:1.8
--- src/sys/kern/subr_optstr.c:1.7	Sat Nov 19 15:30:12 2022
+++ src/sys/kern/subr_optstr.c	Thu Apr 20 09:04:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_optstr.c,v 1.7 2022/11/19 15:30:12 skrll Exp $	*/
+/*	$NetBSD: subr_optstr.c,v 1.8 2023/04/20 09:04:45 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_optstr.c,v 1.7 2022/11/19 15:30:12 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_optstr.c,v 1.8 2023/04/20 09:04:45 skrll Exp $");
 
 #include <sys/param.h>
 #include <sys/optstr.h>
@@ -43,13 +43,15 @@ __KERNEL_RCSID(0, "$NetBSD: subr_optstr.
  * with a maximum of bufsize bytes.  If the key is found, returns true;
  * otherwise FALSE.
  */
-bool
-optstr_get(const char *optstr, const char *key, char *buf, size_t bufsize)
+
+
+static bool
+optstr_get_pointer(const char *optstr, const char *key, const char **result)
 {
 	bool found = false;
 
-	/* Skip any initial spaces until we find a word. */
-	while (*optstr == ' ')
+	/* Skip any initial spaces or tabs until we find a word. */
+	while (*optstr == ' ' || *optstr == '\t')
 		optstr++;
 
 	/* Search for the given key within the option string. */
@@ -76,17 +78,118 @@ optstr_get(const char *optstr, const cha
 		}
 	}
 
-	/* If the key was found; copy its value to the target buffer. */
 	if (found) {
-		const char *lastbuf;
+		optstr++; /* Skip '='. */
+		*result = optstr;
+	}
 
-		lastbuf = buf + (bufsize - 1);
+	return found;
+}
 
-		optstr++; /* Skip '='. */
-		while (buf != lastbuf && *optstr != ' ' && *optstr != '\0')
-			*buf++ = *optstr++;
+bool
+optstr_get(const char *optstr, const char *key, char *buf, size_t bufsize)
+{
+	const char *data;
+	bool found = optstr_get_pointer(optstr, key, &data);
+
+	/* If the key was found; copy its value to the target buffer. */
+	if (found) {
+		const char *lastbuf = buf + (bufsize - 1);
+
+		while (buf != lastbuf && *data != ' ' && *data != '\0')
+			*buf++ = *data++;
 		*buf = '\0';
 	}
+	return found;
+}
+
+
+bool
+optstr_get_string(const char *optstr, const char *key, const char **result)
+{
+	const char *data;
+	const bool found = optstr_get_pointer(optstr, key, &data);
+
+	/* If the key was found; copy its value to the target buffer. */
+	if (found) {
+		*result = data;
+	}
+	return found;
+}
+
+bool
+optstr_get_number(const char *optstr, const char *key, unsigned long *result)
+{
+	const char *data;
+	const bool found = optstr_get_pointer(optstr, key, &data);
+
+	/* If the key was found; copy its value to the target buffer. */
+	if (found) {
+		char *ep;
+		const unsigned long ulval = strtoul(data, &ep, 10);
+		if (ep == data)
+			return false;
+		*result = ulval;
+		return true;
+	}
+	return false;
+}
+
+bool
+optstr_get_number_hex(const char *optstr, const char *key,
+    unsigned long *result)
+{
+	const char *data;
+	const bool found = optstr_get_pointer(optstr, key, &data);
+
+	/* If the key was found; copy its value to the target buffer. */
+	if (found) {
+		char *ep;
+		const unsigned long ulval = strtoul(data, &ep, 16);
+		if (ep == data)
+			return false;
+		*result = ulval;
+		return true;
+	}
+	return false;
+}
+
+bool
+optstr_get_number_binary(const char *optstr, const char *key,
+    unsigned long *result)
+{
+	const char *data;
+	const bool found = optstr_get_pointer(optstr, key, &data);
+
+	/* If the key was found; copy its value to the target buffer. */
+	if (found) {
+		char *ep;
+		const unsigned long ulval = strtoul(data, &ep, 2);
+		if (ep == data)
+			return false;
+		*result = ulval;
+		return true;
+	}
+	return false;
+}
 
+
+#if NETHER > 0
+bool
+optstr_get_macaddr(const char *optstr, const char *key,
+    uint8_t result[ETHER_ADDR_LEN])
+{
+	const char *data;
+	const bool found = optstr_get_pointer(optstr, key, &data);
+
+	/* If the key was found; copy its value to the target buffer. */
+	if (found) {
+		uint8_t temp[ETHER_ADDR_LEN];
+		int error = ether_aton_r(temp, sizeof(temp), data);
+		if (error)
+			return false;
+		memcpy(result, temp, sizeof(temp));
+	}
 	return found;
 }
+#endif

Index: src/sys/sys/optstr.h
diff -u src/sys/sys/optstr.h:1.3 src/sys/sys/optstr.h:1.4
--- src/sys/sys/optstr.h:1.3	Mon Apr 28 20:24:11 2008
+++ src/sys/sys/optstr.h	Thu Apr 20 09:04:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: optstr.h,v 1.3 2008/04/28 20:24:11 martin Exp $	*/
+/*	$NetBSD: optstr.h,v 1.4 2023/04/20 09:04:45 skrll Exp $	*/
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -29,12 +29,29 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#if !defined(_SYS_OPTSTR_H_)
+#ifndef _SYS_OPTSTR_H_
 #define _SYS_OPTSTR_H_
 
+#include "ether.h"
+
+#include <sys/types.h>
+
+#if NETHER > 0
+#include <net/if_ether.h>
+#endif
+
 /*
  * Prototypes for functions defined in sys/kern/subr_optstr.c.
  */
 bool optstr_get(const char *, const char *, char *, size_t);
 
-#endif /* !defined(_SYS_OPTSTR_H_) */
+bool optstr_get_string(const char *, const char *, const char **);
+bool optstr_get_number(const char *, const char *, unsigned long *);
+bool optstr_get_number_hex(const char *, const char *, unsigned long *);
+bool optstr_get_number_binary(const char *, const char *, unsigned long *);
+
+#if NETHER > 0
+bool optstr_get_macaddr(const char *, const char *, uint8_t [ETHER_ADDR_LEN]);
+#endif
+
+#endif /* _SYS_OPTSTR_H_ */

Reply via email to