mirror of
https://github.com/freebsd/freebsd-src
synced 2024-07-21 18:27:22 +00:00
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
This commit is contained in:
parent
a0aad69f95
commit
781ca0afcd
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in a new issue