git: 781ca0afcd6c - main - stand: Introduce devparse to parse device / path strings

From: Warner Losh <imp_at_FreeBSD.org>
Date: Wed, 30 Nov 2022 22:31:13 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=781ca0afcd6c64ac3fbaf2a19b1880734cdfe947

commit 781ca0afcd6c64ac3fbaf2a19b1880734cdfe947
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-11-30 22:08:42 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-11-30 22:30:31 +0000

    stand: Introduce devparse to parse device / path strings
    
    devparse is now the preferred interface to use to parse device
    strings or device:/path strings. It parses the passed in string,
    mallocs the device's particular devdesc string and returns the
    'remainder' of the device:/path for further processing.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D37338
---
 stand/libsa/dev.c   | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 stand/libsa/libsa.3 | 38 +++++++++++++++++++++++--
 stand/libsa/stand.h |  1 +
 3 files changed, 119 insertions(+), 2 deletions(-)

diff --git a/stand/libsa/dev.c b/stand/libsa/dev.c
index 2ccdce22d596..c0bcce07718b 100644
--- a/stand/libsa/dev.c
+++ b/stand/libsa/dev.c
@@ -67,3 +67,85 @@ devformat(struct devdesc *d)
 	snprintf(name, sizeof(name), "%s%d:", d->d_dev->dv_name, d->d_unit);
 	return (name);
 }
+
+/* NB: devspec points to the remainder of the device name after dv_name */
+static int
+default_parsedev(struct devdesc **dev, const char *devspec,
+    const char **path)
+{
+	struct devdesc *idev;
+	int unit, err;
+	char *cp;
+
+	idev = malloc(sizeof(struct devdesc));
+	if (idev == NULL)
+		return (ENOMEM);
+
+	unit = 0;
+	cp = (char *)devspec;	/* strtol interface, alas */
+
+	if (*devspec != '\0' && *devspec != ':') {
+		errno = 0;
+		unit = strtol(devspec, &cp, 0);
+		if (errno != 0 || cp == devspec) {
+			err = EUNIT;
+			goto fail;
+		}
+	}
+	if (*cp != '\0' && *cp != ':') {
+		err = EINVAL;
+		goto fail;
+	}
+
+	idev->d_unit = unit;
+	if (path != NULL)
+		*path = (*cp == 0) ? cp : cp + 1;
+	if (dev != NULL)	/* maybe this can be required? */
+		*dev = idev;
+	else
+		free(idev);
+	return (0);
+fail:
+	free(idev);
+	return (err);
+}
+
+/* NB: devspec points to the whole device spec, and possible trailing path */
+int
+devparse(struct devdesc **dev, const char *devspec, const char **path)
+{
+	struct devdesc *idev;
+	struct devsw *dv;
+	int i, err;
+	const char *np;
+
+	/* minimum length check */
+	if (strlen(devspec) < 2)
+		return (EINVAL);
+
+	/* look for a device that matches */
+	for (i = 0; devsw[i] != NULL; i++) {
+		dv = devsw[i];
+		if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name)))
+			break;
+	}
+	if (devsw[i] == NULL)
+		return (ENOENT);
+	idev = NULL;
+	err = 0;
+	if (dv->dv_parsedev) {
+		err = dv->dv_parsedev(&idev, np, path);
+	} else {
+		np = devspec + strlen(dv->dv_name);
+		err = default_parsedev(&idev, np, path);
+	}
+	if (err != 0)
+		return (err);
+
+	idev->d_dev = dv;
+	if (dev != NULL)
+		*dev = idev;
+	else
+		free(idev);
+	return (0);
+}
diff --git a/stand/libsa/libsa.3 b/stand/libsa/libsa.3
index f732595bc647..210f54a5aa6a 100644
--- a/stand/libsa/libsa.3
+++ b/stand/libsa/libsa.3
@@ -508,6 +508,35 @@ Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
 .Pp
 Format the specified device as a string.
 .It Xo
+.Ft int
+.Fn devparse "struct devdesc **dev" "const char *devdesc" "const char **path"
+.Xc
+.Pp
+Parse the
+.Dv devdesc
+string of the form
+.Sq device:[/path/to/file] .
+The
+.Dv devsw
+table is used to match the start of the
+.Sq device
+string with
+.Fa dv_name .
+If
+.Fa dv_parsedev
+is non-NULL, then it will be called to parse the rest of the string and allocate
+the
+.Dv struct devdesc
+for this path.
+If NULL, then a default routine will be called that will allocate a simple
+.Dv struct devdesc ,
+parse a unit number and ensure there's no trailing characters.
+If
+.Dv path
+is non-NULL, then a pointer to the remainder of the
+.Dv devdesc
+string after the device specification is written.
+.It Xo
 .Ft void
 .Fn twiddle void
 .Xc
@@ -765,8 +794,10 @@ The
 is, by convention, the part of the device specification that follows the
 .Fa dv_name
 part of the string.
-So when parsing the string
-.Dq disk3p5:/xxx
+So when
+.Fa devparse
+is parsing the string
+.Dq disk3p5:/xxx ,
 .Dv devpart
 will point to the
 .Sq 3
@@ -781,6 +812,9 @@ This routine should set
 to point to the portion of the string after device specification, or
 .Dq /xxx
 in the earlier example.
+Generally, code needing to parse a path will use
+.Fa devparse
+instead of calling this routine directly.
 .El
 .Sh HISTORY
 The
diff --git a/stand/libsa/stand.h b/stand/libsa/stand.h
index 716486107b27..6e52325ec166 100644
--- a/stand/libsa/stand.h
+++ b/stand/libsa/stand.h
@@ -187,6 +187,7 @@ struct devdesc {
 };
 
 char *devformat(struct devdesc *d);
+int devparse(struct devdesc **, const char *, const char **);
 
 struct open_file {
     int			f_flags;	/* see F_* below */