tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

MI boot option helper functions



Any objections / improvements to this diff to add MI boot option helper functions?

Thanks,
Nick
diff --git a/sys/kern/subr_optstr.c b/sys/kern/subr_optstr.c
index c557dc71e341..0e7cf76d27ef 100644
--- a/sys/kern/subr_optstr.c
+++ b/sys/kern/subr_optstr.c
@@ -43,13 +43,15 @@ __KERNEL_RCSID(0, "$NetBSD: subr_optstr.c,v 1.7 2022/11/19 15:30:12 skrll Exp $"
  * 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 char *key, char *buf, size_t bufsize)
 		}
 	}
 
-	/* 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
diff --git a/sys/sys/optstr.h b/sys/sys/optstr.h
index bca2780228df..4a4bb757df3e 100644
--- a/sys/sys/optstr.h
+++ b/sys/sys/optstr.h
@@ -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_ */


Home | Main Index | Thread Index | Old Index